[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * HTML API: WP_HTML_Processor class 4 * 5 * @package WordPress 6 * @subpackage HTML-API 7 * @since 6.4.0 8 */ 9 10 /** 11 * Core class used to safely parse and modify an HTML document. 12 * 13 * The HTML Processor class properly parses and modifies HTML5 documents. 14 * 15 * It supports a subset of the HTML5 specification, and when it encounters 16 * unsupported markup, it aborts early to avoid unintentionally breaking 17 * the document. The HTML Processor should never break an HTML document. 18 * 19 * While the `WP_HTML_Tag_Processor` is a valuable tool for modifying 20 * attributes on individual HTML tags, the HTML Processor is more capable 21 * and useful for the following operations: 22 * 23 * - Querying based on nested HTML structure. 24 * 25 * Eventually the HTML Processor will also support: 26 * - Wrapping a tag in surrounding HTML. 27 * - Unwrapping a tag by removing its parent. 28 * - Inserting and removing nodes. 29 * - Reading and changing inner content. 30 * - Navigating up or around HTML structure. 31 * 32 * ## Usage 33 * 34 * Use of this class requires three steps: 35 * 36 * 1. Call a static creator method with your input HTML document. 37 * 2. Find the location in the document you are looking for. 38 * 3. Request changes to the document at that location. 39 * 40 * Example: 41 * 42 * $processor = WP_HTML_Processor::create_fragment( $html ); 43 * if ( $processor->next_tag( array( 'breadcrumbs' => array( 'DIV', 'FIGURE', 'IMG' ) ) ) ) { 44 * $processor->add_class( 'responsive-image' ); 45 * } 46 * 47 * #### Breadcrumbs 48 * 49 * Breadcrumbs represent the stack of open elements from the root 50 * of the document or fragment down to the currently-matched node, 51 * if one is currently selected. Call WP_HTML_Processor::get_breadcrumbs() 52 * to inspect the breadcrumbs for a matched tag. 53 * 54 * Breadcrumbs can specify nested HTML structure and are equivalent 55 * to a CSS selector comprising tag names separated by the child 56 * combinator, such as "DIV > FIGURE > IMG". 57 * 58 * Since all elements find themselves inside a full HTML document 59 * when parsed, the return value from `get_breadcrumbs()` will always 60 * contain any implicit outermost elements. For example, when parsing 61 * with `create_fragment()` in the `BODY` context (the default), any 62 * tag in the given HTML document will contain `array( 'HTML', 'BODY', … )` 63 * in its breadcrumbs. 64 * 65 * Despite containing the implied outermost elements in their breadcrumbs, 66 * tags may be found with the shortest-matching breadcrumb query. That is, 67 * `array( 'IMG' )` matches all IMG elements and `array( 'P', 'IMG' )` 68 * matches all IMG elements directly inside a P element. To ensure that no 69 * partial matches erroneously match it's possible to specify in a query 70 * the full breadcrumb match all the way down from the root HTML element. 71 * 72 * Example: 73 * 74 * $html = '<figure><img><figcaption>A <em>lovely</em> day outside</figcaption></figure>'; 75 * // ----- Matches here. 76 * $processor->next_tag( array( 'breadcrumbs' => array( 'FIGURE', 'IMG' ) ) ); 77 * 78 * $html = '<figure><img><figcaption>A <em>lovely</em> day outside</figcaption></figure>'; 79 * // ---- Matches here. 80 * $processor->next_tag( array( 'breadcrumbs' => array( 'FIGURE', 'FIGCAPTION', 'EM' ) ) ); 81 * 82 * $html = '<div><img></div><img>'; 83 * // ----- Matches here, because IMG must be a direct child of the implicit BODY. 84 * $processor->next_tag( array( 'breadcrumbs' => array( 'BODY', 'IMG' ) ) ); 85 * 86 * ## HTML Support 87 * 88 * This class implements a small part of the HTML5 specification. 89 * It's designed to operate within its support and abort early whenever 90 * encountering circumstances it can't properly handle. This is 91 * the principle way in which this class remains as simple as possible 92 * without cutting corners and breaking compliance. 93 * 94 * ### Supported elements 95 * 96 * If any unsupported element appears in the HTML input the HTML Processor 97 * will abort early and stop all processing. This draconian measure ensures 98 * that the HTML Processor won't break any HTML it doesn't fully understand. 99 * 100 * The HTML Processor supports all elements other than a specific set: 101 * 102 * - Any element inside a TABLE. 103 * - Any element inside foreign content, including SVG and MATH. 104 * - Any element outside the IN BODY insertion mode, e.g. doctype declarations, meta, links. 105 * 106 * ### Supported markup 107 * 108 * Some kinds of non-normative HTML involve reconstruction of formatting elements and 109 * re-parenting of mis-nested elements. For example, a DIV tag found inside a TABLE 110 * may in fact belong _before_ the table in the DOM. If the HTML Processor encounters 111 * such a case it will stop processing. 112 * 113 * The following list illustrates some common examples of unexpected HTML inputs that 114 * the HTML Processor properly parses and represents: 115 * 116 * - HTML with optional tags omitted, e.g. `<p>one<p>two`. 117 * - HTML with unexpected tag closers, e.g. `<p>one </span> more</p>`. 118 * - Non-void tags with self-closing flag, e.g. `<div/>the DIV is still open.</div>`. 119 * - Heading elements which close open heading elements of another level, e.g. `<h1>Closed by </h2>`. 120 * - Elements containing text that looks like other tags but isn't, e.g. `<title>The <img> is plaintext</title>`. 121 * - SCRIPT and STYLE tags containing text that looks like HTML but isn't, e.g. `<script>document.write('<p>Hi</p>');</script>`. 122 * - SCRIPT content which has been escaped, e.g. `<script><!-- document.write('<script>console.log("hi")</script>') --></script>`. 123 * 124 * ### Unsupported Features 125 * 126 * This parser does not report parse errors. 127 * 128 * Normally, when additional HTML or BODY tags are encountered in a document, if there 129 * are any additional attributes on them that aren't found on the previous elements, 130 * the existing HTML and BODY elements adopt those missing attribute values. This 131 * parser does not add those additional attributes. 132 * 133 * In certain situations, elements are moved to a different part of the document in 134 * a process called "adoption" and "fostering." Because the nodes move to a location 135 * in the document that the parser had already processed, this parser does not support 136 * these situations and will bail. 137 * 138 * @since 6.4.0 139 * 140 * @see WP_HTML_Tag_Processor 141 * @see https://html.spec.whatwg.org/ 142 */ 143 class WP_HTML_Processor extends WP_HTML_Tag_Processor { 144 /** 145 * The maximum number of bookmarks allowed to exist at any given time. 146 * 147 * HTML processing requires more bookmarks than basic tag processing, 148 * so this class constant from the Tag Processor is overwritten. 149 * 150 * @since 6.4.0 151 * 152 * @var int 153 */ 154 const MAX_BOOKMARKS = 100; 155 156 /** 157 * Holds the working state of the parser, including the stack of 158 * open elements and the stack of active formatting elements. 159 * 160 * Initialized in the constructor. 161 * 162 * @since 6.4.0 163 * 164 * @var WP_HTML_Processor_State 165 */ 166 private $state; 167 168 /** 169 * Used to create unique bookmark names. 170 * 171 * This class sets a bookmark for every tag in the HTML document that it encounters. 172 * The bookmark name is auto-generated and increments, starting with `1`. These are 173 * internal bookmarks and are automatically released when the referring WP_HTML_Token 174 * goes out of scope and is garbage-collected. 175 * 176 * @since 6.4.0 177 * 178 * @see WP_HTML_Processor::$release_internal_bookmark_on_destruct 179 * 180 * @var int 181 */ 182 private $bookmark_counter = 0; 183 184 /** 185 * Stores an explanation for why something failed, if it did. 186 * 187 * @see self::get_last_error 188 * 189 * @since 6.4.0 190 * 191 * @var string|null 192 */ 193 private $last_error = null; 194 195 /** 196 * Stores context for why the parser bailed on unsupported HTML, if it did. 197 * 198 * @see self::get_unsupported_exception 199 * 200 * @since 6.7.0 201 * 202 * @var WP_HTML_Unsupported_Exception|null 203 */ 204 private $unsupported_exception = null; 205 206 /** 207 * Releases a bookmark when PHP garbage-collects its wrapping WP_HTML_Token instance. 208 * 209 * This function is created inside the class constructor so that it can be passed to 210 * the stack of open elements and the stack of active formatting elements without 211 * exposing it as a public method on the class. 212 * 213 * @since 6.4.0 214 * 215 * @var Closure|null 216 */ 217 private $release_internal_bookmark_on_destruct = null; 218 219 /** 220 * Stores stack events which arise during parsing of the 221 * HTML document, which will then supply the "match" events. 222 * 223 * @since 6.6.0 224 * 225 * @var WP_HTML_Stack_Event[] 226 */ 227 private $element_queue = array(); 228 229 /** 230 * Stores the current breadcrumbs. 231 * 232 * @since 6.7.0 233 * 234 * @var string[] 235 */ 236 private $breadcrumbs = array(); 237 238 /** 239 * Current stack event, if set, representing a matched token. 240 * 241 * Because the parser may internally point to a place further along in a document 242 * than the nodes which have already been processed (some "virtual" nodes may have 243 * appeared while scanning the HTML document), this will point at the "current" node 244 * being processed. It comes from the front of the element queue. 245 * 246 * @since 6.6.0 247 * 248 * @var WP_HTML_Stack_Event|null 249 */ 250 private $current_element = null; 251 252 /** 253 * Context node if created as a fragment parser. 254 * 255 * @var WP_HTML_Token|null 256 */ 257 private $context_node = null; 258 259 /* 260 * Public Interface Functions 261 */ 262 263 /** 264 * Creates an HTML processor in the fragment parsing mode. 265 * 266 * Use this for cases where you are processing chunks of HTML that 267 * will be found within a bigger HTML document, such as rendered 268 * block output that exists within a post, `the_content` inside a 269 * rendered site layout. 270 * 271 * Fragment parsing occurs within a context, which is an HTML element 272 * that the document will eventually be placed in. It becomes important 273 * when special elements have different rules than others, such as inside 274 * a TEXTAREA or a TITLE tag where things that look like tags are text, 275 * or inside a SCRIPT tag where things that look like HTML syntax are JS. 276 * 277 * The context value should be a representation of the tag into which the 278 * HTML is found. For most cases this will be the body element. The HTML 279 * form is provided because a context element may have attributes that 280 * impact the parse, such as with a SCRIPT tag and its `type` attribute. 281 * 282 * ## Current HTML Support 283 * 284 * - The only supported context is `<body>`, which is the default value. 285 * - The only supported document encoding is `UTF-8`, which is the default value. 286 * 287 * @since 6.4.0 288 * @since 6.6.0 Returns `static` instead of `self` so it can create subclass instances. 289 * 290 * @param string $html Input HTML fragment to process. 291 * @param string $context Context element for the fragment, must be default of `<body>`. 292 * @param string $encoding Text encoding of the document; must be default of 'UTF-8'. 293 * @return static|null The created processor if successful, otherwise null. 294 */ 295 public static function create_fragment( $html, $context = '<body>', $encoding = 'UTF-8' ) { 296 if ( '<body>' !== $context || 'UTF-8' !== $encoding ) { 297 return null; 298 } 299 300 $context_processor = static::create_full_parser( "<!DOCTYPE html>{$context}", $encoding ); 301 if ( null === $context_processor ) { 302 return null; 303 } 304 305 while ( $context_processor->next_tag() ) { 306 if ( ! $context_processor->is_virtual() ) { 307 $context_processor->set_bookmark( 'final_node' ); 308 } 309 } 310 311 if ( 312 ! $context_processor->has_bookmark( 'final_node' ) || 313 ! $context_processor->seek( 'final_node' ) 314 ) { 315 _doing_it_wrong( __METHOD__, __( 'No valid context element was detected.' ), '6.8.0' ); 316 return null; 317 } 318 319 return $context_processor->create_fragment_at_current_node( $html ); 320 } 321 322 /** 323 * Creates an HTML processor in the full parsing mode. 324 * 325 * It's likely that a fragment parser is more appropriate, unless sending an 326 * entire HTML document from start to finish. Consider a fragment parser with 327 * a context node of `<body>`. 328 * 329 * UTF-8 is the only allowed encoding. If working with a document that 330 * isn't UTF-8, first convert the document to UTF-8, then pass in the 331 * converted HTML. 332 * 333 * @param string $html Input HTML document to process. 334 * @param string|null $known_definite_encoding Optional. If provided, specifies the charset used 335 * in the input byte stream. Currently must be UTF-8. 336 * @return static|null The created processor if successful, otherwise null. 337 */ 338 public static function create_full_parser( $html, $known_definite_encoding = 'UTF-8' ) { 339 if ( 'UTF-8' !== $known_definite_encoding ) { 340 return null; 341 } 342 343 $processor = new static( $html, self::CONSTRUCTOR_UNLOCK_CODE ); 344 $processor->state->encoding = $known_definite_encoding; 345 $processor->state->encoding_confidence = 'certain'; 346 347 return $processor; 348 } 349 350 /** 351 * Constructor. 352 * 353 * Do not use this method. Use the static creator methods instead. 354 * 355 * @access private 356 * 357 * @since 6.4.0 358 * 359 * @see WP_HTML_Processor::create_fragment() 360 * 361 * @param string $html HTML to process. 362 * @param string|null $use_the_static_create_methods_instead This constructor should not be called manually. 363 */ 364 public function __construct( $html, $use_the_static_create_methods_instead = null ) { 365 parent::__construct( $html ); 366 367 if ( self::CONSTRUCTOR_UNLOCK_CODE !== $use_the_static_create_methods_instead ) { 368 _doing_it_wrong( 369 __METHOD__, 370 sprintf( 371 /* translators: %s: WP_HTML_Processor::create_fragment(). */ 372 __( 'Call %s to create an HTML Processor instead of calling the constructor directly.' ), 373 '<code>WP_HTML_Processor::create_fragment()</code>' 374 ), 375 '6.4.0' 376 ); 377 } 378 379 $this->state = new WP_HTML_Processor_State(); 380 381 $this->state->stack_of_open_elements->set_push_handler( 382 function ( WP_HTML_Token $token ): void { 383 $is_virtual = ! isset( $this->state->current_token ) || $this->is_tag_closer(); 384 $same_node = isset( $this->state->current_token ) && $token->node_name === $this->state->current_token->node_name; 385 $provenance = ( ! $same_node || $is_virtual ) ? 'virtual' : 'real'; 386 $this->element_queue[] = new WP_HTML_Stack_Event( $token, WP_HTML_Stack_Event::PUSH, $provenance ); 387 388 $this->change_parsing_namespace( $token->integration_node_type ? 'html' : $token->namespace ); 389 } 390 ); 391 392 $this->state->stack_of_open_elements->set_pop_handler( 393 function ( WP_HTML_Token $token ): void { 394 $is_virtual = ! isset( $this->state->current_token ) || ! $this->is_tag_closer(); 395 $same_node = isset( $this->state->current_token ) && $token->node_name === $this->state->current_token->node_name; 396 $provenance = ( ! $same_node || $is_virtual ) ? 'virtual' : 'real'; 397 $this->element_queue[] = new WP_HTML_Stack_Event( $token, WP_HTML_Stack_Event::POP, $provenance ); 398 399 $adjusted_current_node = $this->get_adjusted_current_node(); 400 401 if ( $adjusted_current_node ) { 402 $this->change_parsing_namespace( $adjusted_current_node->integration_node_type ? 'html' : $adjusted_current_node->namespace ); 403 } else { 404 $this->change_parsing_namespace( 'html' ); 405 } 406 } 407 ); 408 409 /* 410 * Create this wrapper so that it's possible to pass 411 * a private method into WP_HTML_Token classes without 412 * exposing it to any public API. 413 */ 414 $this->release_internal_bookmark_on_destruct = function ( string $name ): void { 415 parent::release_bookmark( $name ); 416 }; 417 } 418 419 /** 420 * Creates a fragment processor at the current node. 421 * 422 * HTML Fragment parsing always happens with a context node. HTML Fragment Processors can be 423 * instantiated with a `BODY` context node via `WP_HTML_Processor::create_fragment( $html )`. 424 * 425 * The context node may impact how a fragment of HTML is parsed. For example, consider the HTML 426 * fragment `<td />Inside TD?</td>`. 427 * 428 * A BODY context node will produce the following tree: 429 * 430 * └─#text Inside TD? 431 * 432 * Notice that the `<td>` tags are completely ignored. 433 * 434 * Compare that with an SVG context node that produces the following tree: 435 * 436 * ├─svg:td 437 * └─#text Inside TD? 438 * 439 * Here, a `td` node in the `svg` namespace is created, and its self-closing flag is respected. 440 * This is a peculiarity of parsing HTML in foreign content like SVG. 441 * 442 * Finally, consider the tree produced with a TABLE context node: 443 * 444 * └─TBODY 445 * └─TR 446 * └─TD 447 * └─#text Inside TD? 448 * 449 * These examples demonstrate how important the context node may be when processing an HTML 450 * fragment. Special care must be taken when processing fragments that are expected to appear 451 * in specific contexts. SVG and TABLE are good examples, but there are others. 452 * 453 * @see https://html.spec.whatwg.org/multipage/parsing.html#html-fragment-parsing-algorithm 454 * 455 * @since 6.8.0 456 * 457 * @param string $html Input HTML fragment to process. 458 * @return static|null The created processor if successful, otherwise null. 459 */ 460 private function create_fragment_at_current_node( string $html ) { 461 if ( $this->get_token_type() !== '#tag' || $this->is_tag_closer() ) { 462 _doing_it_wrong( 463 __METHOD__, 464 __( 'The context element must be a start tag.' ), 465 '6.8.0' 466 ); 467 return null; 468 } 469 470 $tag_name = $this->current_element->token->node_name; 471 $namespace = $this->current_element->token->namespace; 472 473 if ( 'html' === $namespace && self::is_void( $tag_name ) ) { 474 _doing_it_wrong( 475 __METHOD__, 476 sprintf( 477 // translators: %s: A tag name like INPUT or BR. 478 __( 'The context element cannot be a void element, found "%s".' ), 479 $tag_name 480 ), 481 '6.8.0' 482 ); 483 return null; 484 } 485 486 /* 487 * Prevent creating fragments at nodes that require a special tokenizer state. 488 * This is unsupported by the HTML Processor. 489 */ 490 if ( 491 'html' === $namespace && 492 in_array( $tag_name, array( 'IFRAME', 'NOEMBED', 'NOFRAMES', 'SCRIPT', 'STYLE', 'TEXTAREA', 'TITLE', 'XMP', 'PLAINTEXT' ), true ) 493 ) { 494 _doing_it_wrong( 495 __METHOD__, 496 sprintf( 497 // translators: %s: A tag name like IFRAME or TEXTAREA. 498 __( 'The context element "%s" is not supported.' ), 499 $tag_name 500 ), 501 '6.8.0' 502 ); 503 return null; 504 } 505 506 $fragment_processor = new static( $html, self::CONSTRUCTOR_UNLOCK_CODE ); 507 508 $fragment_processor->compat_mode = $this->compat_mode; 509 510 // @todo Create "fake" bookmarks for non-existent but implied nodes. 511 $fragment_processor->bookmarks['root-node'] = new WP_HTML_Span( 0, 0 ); 512 $root_node = new WP_HTML_Token( 513 'root-node', 514 'HTML', 515 false 516 ); 517 $fragment_processor->state->stack_of_open_elements->push( $root_node ); 518 519 $fragment_processor->bookmarks['context-node'] = new WP_HTML_Span( 0, 0 ); 520 $fragment_processor->context_node = clone $this->current_element->token; 521 $fragment_processor->context_node->bookmark_name = 'context-node'; 522 $fragment_processor->context_node->on_destroy = null; 523 524 $fragment_processor->breadcrumbs = array( 'HTML', $fragment_processor->context_node->node_name ); 525 526 if ( 'TEMPLATE' === $fragment_processor->context_node->node_name ) { 527 $fragment_processor->state->stack_of_template_insertion_modes[] = WP_HTML_Processor_State::INSERTION_MODE_IN_TEMPLATE; 528 } 529 530 $fragment_processor->reset_insertion_mode_appropriately(); 531 532 /* 533 * > Set the parser's form element pointer to the nearest node to the context element that 534 * > is a form element (going straight up the ancestor chain, and including the element 535 * > itself, if it is a form element), if any. (If there is no such form element, the 536 * > form element pointer keeps its initial value, null.) 537 */ 538 foreach ( $this->state->stack_of_open_elements->walk_up() as $element ) { 539 if ( 'FORM' === $element->node_name && 'html' === $element->namespace ) { 540 $fragment_processor->state->form_element = clone $element; 541 $fragment_processor->state->form_element->bookmark_name = null; 542 $fragment_processor->state->form_element->on_destroy = null; 543 break; 544 } 545 } 546 547 $fragment_processor->state->encoding_confidence = 'irrelevant'; 548 549 /* 550 * Update the parsing namespace near the end of the process. 551 * This is important so that any push/pop from the stack of open 552 * elements does not change the parsing namespace. 553 */ 554 $fragment_processor->change_parsing_namespace( 555 $this->current_element->token->integration_node_type ? 'html' : $namespace 556 ); 557 558 return $fragment_processor; 559 } 560 561 /** 562 * Stops the parser and terminates its execution when encountering unsupported markup. 563 * 564 * @throws WP_HTML_Unsupported_Exception Halts execution of the parser. 565 * 566 * @since 6.7.0 567 * 568 * @param string $message Explains support is missing in order to parse the current node. 569 */ 570 private function bail( string $message ) { 571 $here = $this->bookmarks[ $this->state->current_token->bookmark_name ]; 572 $token = substr( $this->html, $here->start, $here->length ); 573 574 $open_elements = array(); 575 foreach ( $this->state->stack_of_open_elements->stack as $item ) { 576 $open_elements[] = $item->node_name; 577 } 578 579 $active_formats = array(); 580 foreach ( $this->state->active_formatting_elements->walk_down() as $item ) { 581 $active_formats[] = $item->node_name; 582 } 583 584 $this->last_error = self::ERROR_UNSUPPORTED; 585 586 $this->unsupported_exception = new WP_HTML_Unsupported_Exception( 587 $message, 588 $this->state->current_token->node_name, 589 $here->start, 590 $token, 591 $open_elements, 592 $active_formats 593 ); 594 595 throw $this->unsupported_exception; 596 } 597 598 /** 599 * Returns the last error, if any. 600 * 601 * Various situations lead to parsing failure but this class will 602 * return `false` in all those cases. To determine why something 603 * failed it's possible to request the last error. This can be 604 * helpful to know to distinguish whether a given tag couldn't 605 * be found or if content in the document caused the processor 606 * to give up and abort processing. 607 * 608 * Example 609 * 610 * $processor = WP_HTML_Processor::create_fragment( '<template><strong><button><em><p><em>' ); 611 * false === $processor->next_tag(); 612 * WP_HTML_Processor::ERROR_UNSUPPORTED === $processor->get_last_error(); 613 * 614 * @since 6.4.0 615 * 616 * @see self::ERROR_UNSUPPORTED 617 * @see self::ERROR_EXCEEDED_MAX_BOOKMARKS 618 * 619 * @return string|null The last error, if one exists, otherwise null. 620 */ 621 public function get_last_error(): ?string { 622 return $this->last_error; 623 } 624 625 /** 626 * Returns context for why the parser aborted due to unsupported HTML, if it did. 627 * 628 * This is meant for debugging purposes, not for production use. 629 * 630 * @since 6.7.0 631 * 632 * @see self::$unsupported_exception 633 * 634 * @return WP_HTML_Unsupported_Exception|null 635 */ 636 public function get_unsupported_exception() { 637 return $this->unsupported_exception; 638 } 639 640 /** 641 * Finds the next tag matching the $query. 642 * 643 * @todo Support matching the class name and tag name. 644 * 645 * @since 6.4.0 646 * @since 6.6.0 Visits all tokens, including virtual ones. 647 * 648 * @throws Exception When unable to allocate a bookmark for the next token in the input HTML document. 649 * 650 * @param array|string|null $query { 651 * Optional. Which tag name to find, having which class, etc. Default is to find any tag. 652 * 653 * @type string|null $tag_name Which tag to find, or `null` for "any tag." 654 * @type string $tag_closers 'visit' to pause at tag closers, 'skip' or unset to only visit openers. 655 * @type int|null $match_offset Find the Nth tag matching all search criteria. 656 * 1 for "first" tag, 3 for "third," etc. 657 * Defaults to first tag. 658 * @type string|null $class_name Tag must contain this whole class name to match. 659 * @type string[] $breadcrumbs DOM sub-path at which element is found, e.g. `array( 'FIGURE', 'IMG' )`. 660 * May also contain the wildcard `*` which matches a single element, e.g. `array( 'SECTION', '*' )`. 661 * } 662 * @return bool Whether a tag was matched. 663 */ 664 public function next_tag( $query = null ): bool { 665 $visit_closers = isset( $query['tag_closers'] ) && 'visit' === $query['tag_closers']; 666 667 if ( null === $query ) { 668 while ( $this->next_token() ) { 669 if ( '#tag' !== $this->get_token_type() ) { 670 continue; 671 } 672 673 if ( ! $this->is_tag_closer() || $visit_closers ) { 674 return true; 675 } 676 } 677 678 return false; 679 } 680 681 if ( is_string( $query ) ) { 682 $query = array( 'breadcrumbs' => array( $query ) ); 683 } 684 685 if ( ! is_array( $query ) ) { 686 _doing_it_wrong( 687 __METHOD__, 688 __( 'Please pass a query array to this function.' ), 689 '6.4.0' 690 ); 691 return false; 692 } 693 694 if ( isset( $query['tag_name'] ) ) { 695 $query['tag_name'] = strtoupper( $query['tag_name'] ); 696 } 697 698 $needs_class = ( isset( $query['class_name'] ) && is_string( $query['class_name'] ) ) 699 ? $query['class_name'] 700 : null; 701 702 if ( ! ( array_key_exists( 'breadcrumbs', $query ) && is_array( $query['breadcrumbs'] ) ) ) { 703 while ( $this->next_token() ) { 704 if ( '#tag' !== $this->get_token_type() ) { 705 continue; 706 } 707 708 if ( isset( $query['tag_name'] ) && $query['tag_name'] !== $this->get_token_name() ) { 709 continue; 710 } 711 712 if ( isset( $needs_class ) && ! $this->has_class( $needs_class ) ) { 713 continue; 714 } 715 716 if ( ! $this->is_tag_closer() || $visit_closers ) { 717 return true; 718 } 719 } 720 721 return false; 722 } 723 724 $breadcrumbs = $query['breadcrumbs']; 725 $match_offset = isset( $query['match_offset'] ) ? (int) $query['match_offset'] : 1; 726 727 while ( $match_offset > 0 && $this->next_token() ) { 728 if ( '#tag' !== $this->get_token_type() || $this->is_tag_closer() ) { 729 continue; 730 } 731 732 if ( isset( $needs_class ) && ! $this->has_class( $needs_class ) ) { 733 continue; 734 } 735 736 if ( $this->matches_breadcrumbs( $breadcrumbs ) && 0 === --$match_offset ) { 737 return true; 738 } 739 } 740 741 return false; 742 } 743 744 /** 745 * Finds the next token in the HTML document. 746 * 747 * This doesn't currently have a way to represent non-tags and doesn't process 748 * semantic rules for text nodes. For access to the raw tokens consider using 749 * WP_HTML_Tag_Processor instead. 750 * 751 * @since 6.5.0 Added for internal support; do not use. 752 * @since 6.7.2 Refactored so subclasses may extend. 753 * 754 * @return bool Whether a token was parsed. 755 */ 756 public function next_token(): bool { 757 return $this->next_visitable_token(); 758 } 759 760 /** 761 * Ensures internal accounting is maintained for HTML semantic rules while 762 * the underlying Tag Processor class is seeking to a bookmark. 763 * 764 * This doesn't currently have a way to represent non-tags and doesn't process 765 * semantic rules for text nodes. For access to the raw tokens consider using 766 * WP_HTML_Tag_Processor instead. 767 * 768 * Note that this method may call itself recursively. This is why it is not 769 * implemented as {@see WP_HTML_Processor::next_token()}, which instead calls 770 * this method similarly to how {@see WP_HTML_Tag_Processor::next_token()} 771 * calls the {@see WP_HTML_Tag_Processor::base_class_next_token()} method. 772 * 773 * @since 6.7.2 Added for internal support. 774 * 775 * @access private 776 * 777 * @return bool 778 */ 779 private function next_visitable_token(): bool { 780 $this->current_element = null; 781 782 if ( isset( $this->last_error ) ) { 783 return false; 784 } 785 786 /* 787 * Prime the events if there are none. 788 * 789 * @todo In some cases, probably related to the adoption agency 790 * algorithm, this call to step() doesn't create any new 791 * events. Calling it again creates them. Figure out why 792 * this is and if it's inherent or if it's a bug. Looping 793 * until there are events or until there are no more 794 * tokens works in the meantime and isn't obviously wrong. 795 */ 796 if ( empty( $this->element_queue ) && $this->step() ) { 797 return $this->next_visitable_token(); 798 } 799 800 // Process the next event on the queue. 801 $this->current_element = array_shift( $this->element_queue ); 802 if ( ! isset( $this->current_element ) ) { 803 // There are no tokens left, so close all remaining open elements. 804 while ( $this->state->stack_of_open_elements->pop() ) { 805 continue; 806 } 807 808 return empty( $this->element_queue ) ? false : $this->next_visitable_token(); 809 } 810 811 $is_pop = WP_HTML_Stack_Event::POP === $this->current_element->operation; 812 813 /* 814 * The root node only exists in the fragment parser, and closing it 815 * indicates that the parse is complete. Stop before popping it from 816 * the breadcrumbs. 817 */ 818 if ( 'root-node' === $this->current_element->token->bookmark_name ) { 819 return $this->next_visitable_token(); 820 } 821 822 // Adjust the breadcrumbs for this event. 823 if ( $is_pop ) { 824 array_pop( $this->breadcrumbs ); 825 } else { 826 $this->breadcrumbs[] = $this->current_element->token->node_name; 827 } 828 829 // Avoid sending close events for elements which don't expect a closing. 830 if ( $is_pop && ! $this->expects_closer( $this->current_element->token ) ) { 831 return $this->next_visitable_token(); 832 } 833 834 return true; 835 } 836 837 /** 838 * Indicates if the current tag token is a tag closer. 839 * 840 * Example: 841 * 842 * $p = WP_HTML_Processor::create_fragment( '<div></div>' ); 843 * $p->next_tag( array( 'tag_name' => 'div', 'tag_closers' => 'visit' ) ); 844 * $p->is_tag_closer() === false; 845 * 846 * $p->next_tag( array( 'tag_name' => 'div', 'tag_closers' => 'visit' ) ); 847 * $p->is_tag_closer() === true; 848 * 849 * @since 6.6.0 Subclassed for HTML Processor. 850 * 851 * @return bool Whether the current tag is a tag closer. 852 */ 853 public function is_tag_closer(): bool { 854 return $this->is_virtual() 855 ? ( WP_HTML_Stack_Event::POP === $this->current_element->operation && '#tag' === $this->get_token_type() ) 856 : parent::is_tag_closer(); 857 } 858 859 /** 860 * Indicates if the currently-matched token is virtual, created by a stack operation 861 * while processing HTML, rather than a token found in the HTML text itself. 862 * 863 * @since 6.6.0 864 * 865 * @return bool Whether the current token is virtual. 866 */ 867 private function is_virtual(): bool { 868 return ( 869 isset( $this->current_element->provenance ) && 870 'virtual' === $this->current_element->provenance 871 ); 872 } 873 874 /** 875 * Indicates if the currently-matched tag matches the given breadcrumbs. 876 * 877 * A "*" represents a single tag wildcard, where any tag matches, but not no tags. 878 * 879 * At some point this function _may_ support a `**` syntax for matching any number 880 * of unspecified tags in the breadcrumb stack. This has been intentionally left 881 * out, however, to keep this function simple and to avoid introducing backtracking, 882 * which could open up surprising performance breakdowns. 883 * 884 * Example: 885 * 886 * $processor = WP_HTML_Processor::create_fragment( '<div><span><figure><img></figure></span></div>' ); 887 * $processor->next_tag( 'img' ); 888 * true === $processor->matches_breadcrumbs( array( 'figure', 'img' ) ); 889 * true === $processor->matches_breadcrumbs( array( 'span', 'figure', 'img' ) ); 890 * false === $processor->matches_breadcrumbs( array( 'span', 'img' ) ); 891 * true === $processor->matches_breadcrumbs( array( 'span', '*', 'img' ) ); 892 * 893 * @since 6.4.0 894 * 895 * @param string[] $breadcrumbs DOM sub-path at which element is found, e.g. `array( 'FIGURE', 'IMG' )`. 896 * May also contain the wildcard `*` which matches a single element, e.g. `array( 'SECTION', '*' )`. 897 * @return bool Whether the currently-matched tag is found at the given nested structure. 898 */ 899 public function matches_breadcrumbs( $breadcrumbs ): bool { 900 // Everything matches when there are zero constraints. 901 if ( 0 === count( $breadcrumbs ) ) { 902 return true; 903 } 904 905 // Start at the last crumb. 906 $crumb = end( $breadcrumbs ); 907 908 if ( '*' !== $crumb && $this->get_tag() !== strtoupper( $crumb ) ) { 909 return false; 910 } 911 912 for ( $i = count( $this->breadcrumbs ) - 1; $i >= 0; $i-- ) { 913 $node = $this->breadcrumbs[ $i ]; 914 $crumb = strtoupper( current( $breadcrumbs ) ); 915 916 if ( '*' !== $crumb && $node !== $crumb ) { 917 return false; 918 } 919 920 if ( false === prev( $breadcrumbs ) ) { 921 return true; 922 } 923 } 924 925 return false; 926 } 927 928 /** 929 * Indicates if the currently-matched node expects a closing 930 * token, or if it will self-close on the next step. 931 * 932 * Most HTML elements expect a closer, such as a P element or 933 * a DIV element. Others, like an IMG element are void and don't 934 * have a closing tag. Special elements, such as SCRIPT and STYLE, 935 * are treated just like void tags. Text nodes and self-closing 936 * foreign content will also act just like a void tag, immediately 937 * closing as soon as the processor advances to the next token. 938 * 939 * @since 6.6.0 940 * 941 * @param WP_HTML_Token|null $node Optional. Node to examine, if provided. 942 * Default is to examine current node. 943 * @return bool|null Whether to expect a closer for the currently-matched node, 944 * or `null` if not matched on any token. 945 */ 946 public function expects_closer( ?WP_HTML_Token $node = null ): ?bool { 947 $token_name = $node->node_name ?? $this->get_token_name(); 948 949 if ( ! isset( $token_name ) ) { 950 return null; 951 } 952 953 $token_namespace = $node->namespace ?? $this->get_namespace(); 954 $token_has_self_closing = $node->has_self_closing_flag ?? $this->has_self_closing_flag(); 955 956 return ! ( 957 // Comments, text nodes, and other atomic tokens. 958 '#' === $token_name[0] || 959 // Doctype declarations. 960 'html' === $token_name || 961 // Void elements. 962 ( 'html' === $token_namespace && self::is_void( $token_name ) ) || 963 // Special atomic elements. 964 ( 'html' === $token_namespace && in_array( $token_name, array( 'IFRAME', 'NOEMBED', 'NOFRAMES', 'SCRIPT', 'STYLE', 'TEXTAREA', 'TITLE', 'XMP' ), true ) ) || 965 // Self-closing elements in foreign content. 966 ( 'html' !== $token_namespace && $token_has_self_closing ) 967 ); 968 } 969 970 /** 971 * Steps through the HTML document and stop at the next tag, if any. 972 * 973 * @since 6.4.0 974 * 975 * @throws Exception When unable to allocate a bookmark for the next token in the input HTML document. 976 * 977 * @see self::PROCESS_NEXT_NODE 978 * @see self::REPROCESS_CURRENT_NODE 979 * 980 * @param string $node_to_process Whether to parse the next node or reprocess the current node. 981 * @return bool Whether a tag was matched. 982 */ 983 public function step( $node_to_process = self::PROCESS_NEXT_NODE ): bool { 984 // Refuse to proceed if there was a previous error. 985 if ( null !== $this->last_error ) { 986 return false; 987 } 988 989 if ( self::REPROCESS_CURRENT_NODE !== $node_to_process ) { 990 /* 991 * Void elements still hop onto the stack of open elements even though 992 * there's no corresponding closing tag. This is important for managing 993 * stack-based operations such as "navigate to parent node" or checking 994 * on an element's breadcrumbs. 995 * 996 * When moving on to the next node, therefore, if the bottom-most element 997 * on the stack is a void element, it must be closed. 998 */ 999 $top_node = $this->state->stack_of_open_elements->current_node(); 1000 if ( isset( $top_node ) && ! $this->expects_closer( $top_node ) ) { 1001 $this->state->stack_of_open_elements->pop(); 1002 } 1003 } 1004 1005 if ( self::PROCESS_NEXT_NODE === $node_to_process ) { 1006 parent::next_token(); 1007 if ( WP_HTML_Tag_Processor::STATE_TEXT_NODE === $this->parser_state ) { 1008 parent::subdivide_text_appropriately(); 1009 } 1010 } 1011 1012 // Finish stepping when there are no more tokens in the document. 1013 if ( 1014 WP_HTML_Tag_Processor::STATE_INCOMPLETE_INPUT === $this->parser_state || 1015 WP_HTML_Tag_Processor::STATE_COMPLETE === $this->parser_state 1016 ) { 1017 return false; 1018 } 1019 1020 $adjusted_current_node = $this->get_adjusted_current_node(); 1021 $is_closer = $this->is_tag_closer(); 1022 $is_start_tag = WP_HTML_Tag_Processor::STATE_MATCHED_TAG === $this->parser_state && ! $is_closer; 1023 $token_name = $this->get_token_name(); 1024 1025 if ( self::REPROCESS_CURRENT_NODE !== $node_to_process ) { 1026 $this->state->current_token = new WP_HTML_Token( 1027 $this->bookmark_token(), 1028 $token_name, 1029 $this->has_self_closing_flag(), 1030 $this->release_internal_bookmark_on_destruct 1031 ); 1032 } 1033 1034 $parse_in_current_insertion_mode = ( 1035 0 === $this->state->stack_of_open_elements->count() || 1036 'html' === $adjusted_current_node->namespace || 1037 ( 1038 'math' === $adjusted_current_node->integration_node_type && 1039 ( 1040 ( $is_start_tag && ! in_array( $token_name, array( 'MGLYPH', 'MALIGNMARK' ), true ) ) || 1041 '#text' === $token_name 1042 ) 1043 ) || 1044 ( 1045 'math' === $adjusted_current_node->namespace && 1046 'ANNOTATION-XML' === $adjusted_current_node->node_name && 1047 $is_start_tag && 'SVG' === $token_name 1048 ) || 1049 ( 1050 'html' === $adjusted_current_node->integration_node_type && 1051 ( $is_start_tag || '#text' === $token_name ) 1052 ) 1053 ); 1054 1055 try { 1056 if ( ! $parse_in_current_insertion_mode ) { 1057 return $this->step_in_foreign_content(); 1058 } 1059 1060 switch ( $this->state->insertion_mode ) { 1061 case WP_HTML_Processor_State::INSERTION_MODE_INITIAL: 1062 return $this->step_initial(); 1063 1064 case WP_HTML_Processor_State::INSERTION_MODE_BEFORE_HTML: 1065 return $this->step_before_html(); 1066 1067 case WP_HTML_Processor_State::INSERTION_MODE_BEFORE_HEAD: 1068 return $this->step_before_head(); 1069 1070 case WP_HTML_Processor_State::INSERTION_MODE_IN_HEAD: 1071 return $this->step_in_head(); 1072 1073 case WP_HTML_Processor_State::INSERTION_MODE_IN_HEAD_NOSCRIPT: 1074 return $this->step_in_head_noscript(); 1075 1076 case WP_HTML_Processor_State::INSERTION_MODE_AFTER_HEAD: 1077 return $this->step_after_head(); 1078 1079 case WP_HTML_Processor_State::INSERTION_MODE_IN_BODY: 1080 return $this->step_in_body(); 1081 1082 case WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE: 1083 return $this->step_in_table(); 1084 1085 case WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_TEXT: 1086 return $this->step_in_table_text(); 1087 1088 case WP_HTML_Processor_State::INSERTION_MODE_IN_CAPTION: 1089 return $this->step_in_caption(); 1090 1091 case WP_HTML_Processor_State::INSERTION_MODE_IN_COLUMN_GROUP: 1092 return $this->step_in_column_group(); 1093 1094 case WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY: 1095 return $this->step_in_table_body(); 1096 1097 case WP_HTML_Processor_State::INSERTION_MODE_IN_ROW: 1098 return $this->step_in_row(); 1099 1100 case WP_HTML_Processor_State::INSERTION_MODE_IN_CELL: 1101 return $this->step_in_cell(); 1102 1103 case WP_HTML_Processor_State::INSERTION_MODE_IN_SELECT: 1104 return $this->step_in_select(); 1105 1106 case WP_HTML_Processor_State::INSERTION_MODE_IN_SELECT_IN_TABLE: 1107 return $this->step_in_select_in_table(); 1108 1109 case WP_HTML_Processor_State::INSERTION_MODE_IN_TEMPLATE: 1110 return $this->step_in_template(); 1111 1112 case WP_HTML_Processor_State::INSERTION_MODE_AFTER_BODY: 1113 return $this->step_after_body(); 1114 1115 case WP_HTML_Processor_State::INSERTION_MODE_IN_FRAMESET: 1116 return $this->step_in_frameset(); 1117 1118 case WP_HTML_Processor_State::INSERTION_MODE_AFTER_FRAMESET: 1119 return $this->step_after_frameset(); 1120 1121 case WP_HTML_Processor_State::INSERTION_MODE_AFTER_AFTER_BODY: 1122 return $this->step_after_after_body(); 1123 1124 case WP_HTML_Processor_State::INSERTION_MODE_AFTER_AFTER_FRAMESET: 1125 return $this->step_after_after_frameset(); 1126 1127 // This should be unreachable but PHP doesn't have total type checking on switch. 1128 default: 1129 $this->bail( "Unaware of the requested parsing mode: '{$this->state->insertion_mode}'." ); 1130 } 1131 } catch ( WP_HTML_Unsupported_Exception $e ) { 1132 /* 1133 * Exceptions are used in this class to escape deep call stacks that 1134 * otherwise might involve messier calling and return conventions. 1135 */ 1136 return false; 1137 } 1138 } 1139 1140 /** 1141 * Computes the HTML breadcrumbs for the currently-matched node, if matched. 1142 * 1143 * Breadcrumbs start at the outermost parent and descend toward the matched element. 1144 * They always include the entire path from the root HTML node to the matched element. 1145 * 1146 * Example: 1147 * 1148 * $processor = WP_HTML_Processor::create_fragment( '<p><strong><em><img></em></strong></p>' ); 1149 * $processor->next_tag( 'IMG' ); 1150 * $processor->get_breadcrumbs() === array( 'HTML', 'BODY', 'P', 'STRONG', 'EM', 'IMG' ); 1151 * 1152 * @since 6.4.0 1153 * 1154 * @return string[] Array of tag names representing path to matched node. 1155 */ 1156 public function get_breadcrumbs(): array { 1157 return $this->breadcrumbs; 1158 } 1159 1160 /** 1161 * Returns the nesting depth of the current location in the document. 1162 * 1163 * Example: 1164 * 1165 * $processor = WP_HTML_Processor::create_fragment( '<div><p></p></div>' ); 1166 * // The processor starts in the BODY context, meaning it has depth from the start: HTML > BODY. 1167 * 2 === $processor->get_current_depth(); 1168 * 1169 * // Opening the DIV element increases the depth. 1170 * $processor->next_token(); 1171 * 3 === $processor->get_current_depth(); 1172 * 1173 * // Opening the P element increases the depth. 1174 * $processor->next_token(); 1175 * 4 === $processor->get_current_depth(); 1176 * 1177 * // The P element is closed during `next_token()` so the depth is decreased to reflect that. 1178 * $processor->next_token(); 1179 * 3 === $processor->get_current_depth(); 1180 * 1181 * @since 6.6.0 1182 * 1183 * @return int Nesting-depth of current location in the document. 1184 */ 1185 public function get_current_depth(): int { 1186 return count( $this->breadcrumbs ); 1187 } 1188 1189 /** 1190 * Normalizes an HTML fragment by serializing it. 1191 * 1192 * This method assumes that the given HTML snippet is found in BODY context. 1193 * For normalizing full documents or fragments found in other contexts, create 1194 * a new processor using {@see WP_HTML_Processor::create_fragment} or 1195 * {@see WP_HTML_Processor::create_full_parser} and call {@see WP_HTML_Processor::serialize} 1196 * on the created instances. 1197 * 1198 * Many aspects of an input HTML fragment may be changed during normalization. 1199 * 1200 * - Attribute values will be double-quoted. 1201 * - Duplicate attributes will be removed. 1202 * - Omitted tags will be added. 1203 * - Tag and attribute name casing will be lower-cased, 1204 * except for specific SVG and MathML tags or attributes. 1205 * - Text will be re-encoded, null bytes handled, 1206 * and invalid UTF-8 replaced with U+FFFD. 1207 * - Any incomplete syntax trailing at the end will be omitted, 1208 * for example, an unclosed comment opener will be removed. 1209 * 1210 * Example: 1211 * 1212 * echo WP_HTML_Processor::normalize( '<a href=#anchor v=5 href="/" enabled>One</a another v=5><!--' ); 1213 * // <a href="#anchor" v="5" enabled>One</a> 1214 * 1215 * echo WP_HTML_Processor::normalize( '<div></p>fun<table><td>cell</div>' ); 1216 * // <div><p></p>fun<table><tbody><tr><td>cell</td></tr></tbody></table></div> 1217 * 1218 * echo WP_HTML_Processor::normalize( '<![CDATA[invalid comment]]> syntax < <> "oddities"' ); 1219 * // <!--[CDATA[invalid comment]]--> syntax < <> "oddities" 1220 * 1221 * @since 6.7.0 1222 * 1223 * @param string $html Input HTML to normalize. 1224 * 1225 * @return string|null Normalized output, or `null` if unable to normalize. 1226 */ 1227 public static function normalize( string $html ): ?string { 1228 return static::create_fragment( $html )->serialize(); 1229 } 1230 1231 /** 1232 * Returns normalized HTML for a fragment by serializing it. 1233 * 1234 * This differs from {@see WP_HTML_Processor::normalize} in that it starts with 1235 * a specific HTML Processor, which _must_ not have already started scanning; 1236 * it must be in the initial ready state and will be in the completed state once 1237 * serialization is complete. 1238 * 1239 * Many aspects of an input HTML fragment may be changed during normalization. 1240 * 1241 * - Attribute values will be double-quoted. 1242 * - Duplicate attributes will be removed. 1243 * - Omitted tags will be added. 1244 * - Tag and attribute name casing will be lower-cased, 1245 * except for specific SVG and MathML tags or attributes. 1246 * - Text will be re-encoded, null bytes handled, 1247 * and invalid UTF-8 replaced with U+FFFD. 1248 * - Any incomplete syntax trailing at the end will be omitted, 1249 * for example, an unclosed comment opener will be removed. 1250 * 1251 * Example: 1252 * 1253 * $processor = WP_HTML_Processor::create_fragment( '<a href=#anchor v=5 href="/" enabled>One</a another v=5><!--' ); 1254 * echo $processor->serialize(); 1255 * // <a href="#anchor" v="5" enabled>One</a> 1256 * 1257 * $processor = WP_HTML_Processor::create_fragment( '<div></p>fun<table><td>cell</div>' ); 1258 * echo $processor->serialize(); 1259 * // <div><p></p>fun<table><tbody><tr><td>cell</td></tr></tbody></table></div> 1260 * 1261 * $processor = WP_HTML_Processor::create_fragment( '<![CDATA[invalid comment]]> syntax < <> "oddities"' ); 1262 * echo $processor->serialize(); 1263 * // <!--[CDATA[invalid comment]]--> syntax < <> "oddities" 1264 * 1265 * @since 6.7.0 1266 * 1267 * @return string|null Normalized HTML markup represented by processor, 1268 * or `null` if unable to generate serialization. 1269 */ 1270 public function serialize(): ?string { 1271 if ( WP_HTML_Tag_Processor::STATE_READY !== $this->parser_state ) { 1272 wp_trigger_error( 1273 __METHOD__, 1274 'An HTML Processor which has already started processing cannot serialize its contents. Serialize immediately after creating the instance.', 1275 E_USER_WARNING 1276 ); 1277 return null; 1278 } 1279 1280 $html = ''; 1281 while ( $this->next_token() ) { 1282 $html .= $this->serialize_token(); 1283 } 1284 1285 if ( null !== $this->get_last_error() ) { 1286 wp_trigger_error( 1287 __METHOD__, 1288 "Cannot serialize HTML Processor with parsing error: {$this->get_last_error()}.", 1289 E_USER_WARNING 1290 ); 1291 return null; 1292 } 1293 1294 return $html; 1295 } 1296 1297 /** 1298 * Serializes the currently-matched token. 1299 * 1300 * This method produces a fully-normative HTML string for the currently-matched token, 1301 * if able. If not matched at any token or if the token doesn't correspond to any HTML 1302 * it will return an empty string (for example, presumptuous end tags are ignored). 1303 * 1304 * @see static::serialize() 1305 * 1306 * @since 6.7.0 1307 * 1308 * @return string Serialization of token, or empty string if no serialization exists. 1309 */ 1310 protected function serialize_token(): string { 1311 $html = ''; 1312 $token_type = $this->get_token_type(); 1313 1314 switch ( $token_type ) { 1315 case '#doctype': 1316 $doctype = $this->get_doctype_info(); 1317 if ( null === $doctype ) { 1318 break; 1319 } 1320 1321 $html .= '<!DOCTYPE'; 1322 1323 if ( $doctype->name ) { 1324 $html .= " {$doctype->name}"; 1325 } 1326 1327 if ( null !== $doctype->public_identifier ) { 1328 $quote = str_contains( $doctype->public_identifier, '"' ) ? "'" : '"'; 1329 $html .= " PUBLIC {$quote}{$doctype->public_identifier}{$quote}"; 1330 } 1331 if ( null !== $doctype->system_identifier ) { 1332 if ( null === $doctype->public_identifier ) { 1333 $html .= ' SYSTEM'; 1334 } 1335 $quote = str_contains( $doctype->system_identifier, '"' ) ? "'" : '"'; 1336 $html .= " {$quote}{$doctype->system_identifier}{$quote}"; 1337 } 1338 1339 $html .= '>'; 1340 break; 1341 1342 case '#text': 1343 $html .= htmlspecialchars( $this->get_modifiable_text(), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, 'UTF-8' ); 1344 break; 1345 1346 // Unlike the `<>` which is interpreted as plaintext, this is ignored entirely. 1347 case '#presumptuous-tag': 1348 break; 1349 1350 case '#funky-comment': 1351 case '#comment': 1352 $html .= "<!--{$this->get_full_comment_text()}-->"; 1353 break; 1354 1355 case '#cdata-section': 1356 $html .= "<![CDATA[{$this->get_modifiable_text()}]]>"; 1357 break; 1358 } 1359 1360 if ( '#tag' !== $token_type ) { 1361 return $html; 1362 } 1363 1364 $tag_name = str_replace( "\x00", "\u{FFFD}", $this->get_tag() ); 1365 $in_html = 'html' === $this->get_namespace(); 1366 $qualified_name = $in_html ? strtolower( $tag_name ) : $this->get_qualified_tag_name(); 1367 1368 if ( $this->is_tag_closer() ) { 1369 $html .= "</{$qualified_name}>"; 1370 return $html; 1371 } 1372 1373 $attribute_names = $this->get_attribute_names_with_prefix( '' ); 1374 if ( ! isset( $attribute_names ) ) { 1375 $html .= "<{$qualified_name}>"; 1376 return $html; 1377 } 1378 1379 $html .= "<{$qualified_name}"; 1380 foreach ( $attribute_names as $attribute_name ) { 1381 $html .= " {$this->get_qualified_attribute_name( $attribute_name )}"; 1382 $value = $this->get_attribute( $attribute_name ); 1383 1384 if ( is_string( $value ) ) { 1385 $html .= '="' . htmlspecialchars( $value, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5 ) . '"'; 1386 } 1387 1388 $html = str_replace( "\x00", "\u{FFFD}", $html ); 1389 } 1390 1391 if ( ! $in_html && $this->has_self_closing_flag() ) { 1392 $html .= ' /'; 1393 } 1394 1395 $html .= '>'; 1396 1397 // Flush out self-contained elements. 1398 if ( $in_html && in_array( $tag_name, array( 'IFRAME', 'NOEMBED', 'NOFRAMES', 'SCRIPT', 'STYLE', 'TEXTAREA', 'TITLE', 'XMP' ), true ) ) { 1399 $text = $this->get_modifiable_text(); 1400 1401 switch ( $tag_name ) { 1402 case 'IFRAME': 1403 case 'NOEMBED': 1404 case 'NOFRAMES': 1405 $text = ''; 1406 break; 1407 1408 case 'SCRIPT': 1409 case 'STYLE': 1410 break; 1411 1412 default: 1413 $text = htmlspecialchars( $text, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, 'UTF-8' ); 1414 } 1415 1416 $html .= "{$text}</{$qualified_name}>"; 1417 } 1418 1419 return $html; 1420 } 1421 1422 /** 1423 * Parses next element in the 'initial' insertion mode. 1424 * 1425 * This internal function performs the 'initial' insertion mode 1426 * logic for the generalized WP_HTML_Processor::step() function. 1427 * 1428 * @since 6.7.0 1429 * 1430 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 1431 * 1432 * @see https://html.spec.whatwg.org/#the-initial-insertion-mode 1433 * @see WP_HTML_Processor::step 1434 * 1435 * @return bool Whether an element was found. 1436 */ 1437 private function step_initial(): bool { 1438 $token_name = $this->get_token_name(); 1439 $token_type = $this->get_token_type(); 1440 $op_sigil = '#tag' === $token_type ? ( parent::is_tag_closer() ? '-' : '+' ) : ''; 1441 $op = "{$op_sigil}{$token_name}"; 1442 1443 switch ( $op ) { 1444 /* 1445 * > A character token that is one of U+0009 CHARACTER TABULATION, 1446 * > U+000A LINE FEED (LF), U+000C FORM FEED (FF), 1447 * > U+000D CARRIAGE RETURN (CR), or U+0020 SPACE 1448 * 1449 * Parse error: ignore the token. 1450 */ 1451 case '#text': 1452 if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) { 1453 return $this->step(); 1454 } 1455 goto initial_anything_else; 1456 break; 1457 1458 /* 1459 * > A comment token 1460 */ 1461 case '#comment': 1462 case '#funky-comment': 1463 case '#presumptuous-tag': 1464 $this->insert_html_element( $this->state->current_token ); 1465 return true; 1466 1467 /* 1468 * > A DOCTYPE token 1469 */ 1470 case 'html': 1471 $doctype = $this->get_doctype_info(); 1472 if ( null !== $doctype && 'quirks' === $doctype->indicated_compatability_mode ) { 1473 $this->compat_mode = WP_HTML_Tag_Processor::QUIRKS_MODE; 1474 } 1475 1476 /* 1477 * > Then, switch the insertion mode to "before html". 1478 */ 1479 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_BEFORE_HTML; 1480 $this->insert_html_element( $this->state->current_token ); 1481 return true; 1482 } 1483 1484 /* 1485 * > Anything else 1486 */ 1487 initial_anything_else: 1488 $this->compat_mode = WP_HTML_Tag_Processor::QUIRKS_MODE; 1489 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_BEFORE_HTML; 1490 return $this->step( self::REPROCESS_CURRENT_NODE ); 1491 } 1492 1493 /** 1494 * Parses next element in the 'before html' insertion mode. 1495 * 1496 * This internal function performs the 'before html' insertion mode 1497 * logic for the generalized WP_HTML_Processor::step() function. 1498 * 1499 * @since 6.7.0 1500 * 1501 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 1502 * 1503 * @see https://html.spec.whatwg.org/#the-before-html-insertion-mode 1504 * @see WP_HTML_Processor::step 1505 * 1506 * @return bool Whether an element was found. 1507 */ 1508 private function step_before_html(): bool { 1509 $token_name = $this->get_token_name(); 1510 $token_type = $this->get_token_type(); 1511 $is_closer = parent::is_tag_closer(); 1512 $op_sigil = '#tag' === $token_type ? ( $is_closer ? '-' : '+' ) : ''; 1513 $op = "{$op_sigil}{$token_name}"; 1514 1515 switch ( $op ) { 1516 /* 1517 * > A DOCTYPE token 1518 */ 1519 case 'html': 1520 // Parse error: ignore the token. 1521 return $this->step(); 1522 1523 /* 1524 * > A comment token 1525 */ 1526 case '#comment': 1527 case '#funky-comment': 1528 case '#presumptuous-tag': 1529 $this->insert_html_element( $this->state->current_token ); 1530 return true; 1531 1532 /* 1533 * > A character token that is one of U+0009 CHARACTER TABULATION, 1534 * > U+000A LINE FEED (LF), U+000C FORM FEED (FF), 1535 * > U+000D CARRIAGE RETURN (CR), or U+0020 SPACE 1536 * 1537 * Parse error: ignore the token. 1538 */ 1539 case '#text': 1540 if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) { 1541 return $this->step(); 1542 } 1543 goto before_html_anything_else; 1544 break; 1545 1546 /* 1547 * > A start tag whose tag name is "html" 1548 */ 1549 case '+HTML': 1550 $this->insert_html_element( $this->state->current_token ); 1551 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_BEFORE_HEAD; 1552 return true; 1553 1554 /* 1555 * > An end tag whose tag name is one of: "head", "body", "html", "br" 1556 * 1557 * Closing BR tags are always reported by the Tag Processor as opening tags. 1558 */ 1559 case '-HEAD': 1560 case '-BODY': 1561 case '-HTML': 1562 /* 1563 * > Act as described in the "anything else" entry below. 1564 */ 1565 goto before_html_anything_else; 1566 break; 1567 } 1568 1569 /* 1570 * > Any other end tag 1571 */ 1572 if ( $is_closer ) { 1573 // Parse error: ignore the token. 1574 return $this->step(); 1575 } 1576 1577 /* 1578 * > Anything else. 1579 * 1580 * > Create an html element whose node document is the Document object. 1581 * > Append it to the Document object. Put this element in the stack of open elements. 1582 * > Switch the insertion mode to "before head", then reprocess the token. 1583 */ 1584 before_html_anything_else: 1585 $this->insert_virtual_node( 'HTML' ); 1586 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_BEFORE_HEAD; 1587 return $this->step( self::REPROCESS_CURRENT_NODE ); 1588 } 1589 1590 /** 1591 * Parses next element in the 'before head' insertion mode. 1592 * 1593 * This internal function performs the 'before head' insertion mode 1594 * logic for the generalized WP_HTML_Processor::step() function. 1595 * 1596 * @since 6.7.0 Stub implementation. 1597 * 1598 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 1599 * 1600 * @see https://html.spec.whatwg.org/#the-before-head-insertion-mode 1601 * @see WP_HTML_Processor::step 1602 * 1603 * @return bool Whether an element was found. 1604 */ 1605 private function step_before_head(): bool { 1606 $token_name = $this->get_token_name(); 1607 $token_type = $this->get_token_type(); 1608 $is_closer = parent::is_tag_closer(); 1609 $op_sigil = '#tag' === $token_type ? ( $is_closer ? '-' : '+' ) : ''; 1610 $op = "{$op_sigil}{$token_name}"; 1611 1612 switch ( $op ) { 1613 /* 1614 * > A character token that is one of U+0009 CHARACTER TABULATION, 1615 * > U+000A LINE FEED (LF), U+000C FORM FEED (FF), 1616 * > U+000D CARRIAGE RETURN (CR), or U+0020 SPACE 1617 * 1618 * Parse error: ignore the token. 1619 */ 1620 case '#text': 1621 if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) { 1622 return $this->step(); 1623 } 1624 goto before_head_anything_else; 1625 break; 1626 1627 /* 1628 * > A comment token 1629 */ 1630 case '#comment': 1631 case '#funky-comment': 1632 case '#presumptuous-tag': 1633 $this->insert_html_element( $this->state->current_token ); 1634 return true; 1635 1636 /* 1637 * > A DOCTYPE token 1638 */ 1639 case 'html': 1640 // Parse error: ignore the token. 1641 return $this->step(); 1642 1643 /* 1644 * > A start tag whose tag name is "html" 1645 */ 1646 case '+HTML': 1647 return $this->step_in_body(); 1648 1649 /* 1650 * > A start tag whose tag name is "head" 1651 */ 1652 case '+HEAD': 1653 $this->insert_html_element( $this->state->current_token ); 1654 $this->state->head_element = $this->state->current_token; 1655 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_HEAD; 1656 return true; 1657 1658 /* 1659 * > An end tag whose tag name is one of: "head", "body", "html", "br" 1660 * > Act as described in the "anything else" entry below. 1661 * 1662 * Closing BR tags are always reported by the Tag Processor as opening tags. 1663 */ 1664 case '-HEAD': 1665 case '-BODY': 1666 case '-HTML': 1667 goto before_head_anything_else; 1668 break; 1669 } 1670 1671 if ( $is_closer ) { 1672 // Parse error: ignore the token. 1673 return $this->step(); 1674 } 1675 1676 /* 1677 * > Anything else 1678 * 1679 * > Insert an HTML element for a "head" start tag token with no attributes. 1680 */ 1681 before_head_anything_else: 1682 $this->state->head_element = $this->insert_virtual_node( 'HEAD' ); 1683 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_HEAD; 1684 return $this->step( self::REPROCESS_CURRENT_NODE ); 1685 } 1686 1687 /** 1688 * Parses next element in the 'in head' insertion mode. 1689 * 1690 * This internal function performs the 'in head' insertion mode 1691 * logic for the generalized WP_HTML_Processor::step() function. 1692 * 1693 * @since 6.7.0 1694 * 1695 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 1696 * 1697 * @see https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead 1698 * @see WP_HTML_Processor::step 1699 * 1700 * @return bool Whether an element was found. 1701 */ 1702 private function step_in_head(): bool { 1703 $token_name = $this->get_token_name(); 1704 $token_type = $this->get_token_type(); 1705 $is_closer = parent::is_tag_closer(); 1706 $op_sigil = '#tag' === $token_type ? ( $is_closer ? '-' : '+' ) : ''; 1707 $op = "{$op_sigil}{$token_name}"; 1708 1709 switch ( $op ) { 1710 case '#text': 1711 /* 1712 * > A character token that is one of U+0009 CHARACTER TABULATION, 1713 * > U+000A LINE FEED (LF), U+000C FORM FEED (FF), 1714 * > U+000D CARRIAGE RETURN (CR), or U+0020 SPACE 1715 */ 1716 if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) { 1717 // Insert the character. 1718 $this->insert_html_element( $this->state->current_token ); 1719 return true; 1720 } 1721 1722 goto in_head_anything_else; 1723 break; 1724 1725 /* 1726 * > A comment token 1727 */ 1728 case '#comment': 1729 case '#funky-comment': 1730 case '#presumptuous-tag': 1731 $this->insert_html_element( $this->state->current_token ); 1732 return true; 1733 1734 /* 1735 * > A DOCTYPE token 1736 */ 1737 case 'html': 1738 // Parse error: ignore the token. 1739 return $this->step(); 1740 1741 /* 1742 * > A start tag whose tag name is "html" 1743 */ 1744 case '+HTML': 1745 return $this->step_in_body(); 1746 1747 /* 1748 * > A start tag whose tag name is one of: "base", "basefont", "bgsound", "link" 1749 */ 1750 case '+BASE': 1751 case '+BASEFONT': 1752 case '+BGSOUND': 1753 case '+LINK': 1754 $this->insert_html_element( $this->state->current_token ); 1755 return true; 1756 1757 /* 1758 * > A start tag whose tag name is "meta" 1759 */ 1760 case '+META': 1761 $this->insert_html_element( $this->state->current_token ); 1762 1763 // All following conditions depend on "tentative" encoding confidence. 1764 if ( 'tentative' !== $this->state->encoding_confidence ) { 1765 return true; 1766 } 1767 1768 /* 1769 * > If the active speculative HTML parser is null, then: 1770 * > - If the element has a charset attribute, and getting an encoding from 1771 * > its value results in an encoding, and the confidence is currently 1772 * > tentative, then change the encoding to the resulting encoding. 1773 */ 1774 $charset = $this->get_attribute( 'charset' ); 1775 if ( is_string( $charset ) ) { 1776 $this->bail( 'Cannot yet process META tags with charset to determine encoding.' ); 1777 } 1778 1779 /* 1780 * > - Otherwise, if the element has an http-equiv attribute whose value is 1781 * > an ASCII case-insensitive match for the string "Content-Type", and 1782 * > the element has a content attribute, and applying the algorithm for 1783 * > extracting a character encoding from a meta element to that attribute's 1784 * > value returns an encoding, and the confidence is currently tentative, 1785 * > then change the encoding to the extracted encoding. 1786 */ 1787 $http_equiv = $this->get_attribute( 'http-equiv' ); 1788 $content = $this->get_attribute( 'content' ); 1789 if ( 1790 is_string( $http_equiv ) && 1791 is_string( $content ) && 1792 0 === strcasecmp( $http_equiv, 'Content-Type' ) 1793 ) { 1794 $this->bail( 'Cannot yet process META tags with http-equiv Content-Type to determine encoding.' ); 1795 } 1796 1797 return true; 1798 1799 /* 1800 * > A start tag whose tag name is "title" 1801 */ 1802 case '+TITLE': 1803 $this->insert_html_element( $this->state->current_token ); 1804 return true; 1805 1806 /* 1807 * > A start tag whose tag name is "noscript", if the scripting flag is enabled 1808 * > A start tag whose tag name is one of: "noframes", "style" 1809 * 1810 * The scripting flag is never enabled in this parser. 1811 */ 1812 case '+NOFRAMES': 1813 case '+STYLE': 1814 $this->insert_html_element( $this->state->current_token ); 1815 return true; 1816 1817 /* 1818 * > A start tag whose tag name is "noscript", if the scripting flag is disabled 1819 */ 1820 case '+NOSCRIPT': 1821 $this->insert_html_element( $this->state->current_token ); 1822 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_HEAD_NOSCRIPT; 1823 return true; 1824 1825 /* 1826 * > A start tag whose tag name is "script" 1827 * 1828 * @todo Could the adjusted insertion location be anything other than the current location? 1829 */ 1830 case '+SCRIPT': 1831 $this->insert_html_element( $this->state->current_token ); 1832 return true; 1833 1834 /* 1835 * > An end tag whose tag name is "head" 1836 */ 1837 case '-HEAD': 1838 $this->state->stack_of_open_elements->pop(); 1839 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_AFTER_HEAD; 1840 return true; 1841 1842 /* 1843 * > An end tag whose tag name is one of: "body", "html", "br" 1844 * 1845 * BR tags are always reported by the Tag Processor as opening tags. 1846 */ 1847 case '-BODY': 1848 case '-HTML': 1849 /* 1850 * > Act as described in the "anything else" entry below. 1851 */ 1852 goto in_head_anything_else; 1853 break; 1854 1855 /* 1856 * > A start tag whose tag name is "template" 1857 * 1858 * @todo Could the adjusted insertion location be anything other than the current location? 1859 */ 1860 case '+TEMPLATE': 1861 $this->state->active_formatting_elements->insert_marker(); 1862 $this->state->frameset_ok = false; 1863 1864 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TEMPLATE; 1865 $this->state->stack_of_template_insertion_modes[] = WP_HTML_Processor_State::INSERTION_MODE_IN_TEMPLATE; 1866 1867 $this->insert_html_element( $this->state->current_token ); 1868 return true; 1869 1870 /* 1871 * > An end tag whose tag name is "template" 1872 */ 1873 case '-TEMPLATE': 1874 if ( ! $this->state->stack_of_open_elements->contains( 'TEMPLATE' ) ) { 1875 // @todo Indicate a parse error once it's possible. 1876 return $this->step(); 1877 } 1878 1879 $this->generate_implied_end_tags_thoroughly(); 1880 if ( ! $this->state->stack_of_open_elements->current_node_is( 'TEMPLATE' ) ) { 1881 // @todo Indicate a parse error once it's possible. 1882 } 1883 1884 $this->state->stack_of_open_elements->pop_until( 'TEMPLATE' ); 1885 $this->state->active_formatting_elements->clear_up_to_last_marker(); 1886 array_pop( $this->state->stack_of_template_insertion_modes ); 1887 $this->reset_insertion_mode_appropriately(); 1888 return true; 1889 } 1890 1891 /* 1892 * > A start tag whose tag name is "head" 1893 * > Any other end tag 1894 */ 1895 if ( '+HEAD' === $op || $is_closer ) { 1896 // Parse error: ignore the token. 1897 return $this->step(); 1898 } 1899 1900 /* 1901 * > Anything else 1902 */ 1903 in_head_anything_else: 1904 $this->state->stack_of_open_elements->pop(); 1905 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_AFTER_HEAD; 1906 return $this->step( self::REPROCESS_CURRENT_NODE ); 1907 } 1908 1909 /** 1910 * Parses next element in the 'in head noscript' insertion mode. 1911 * 1912 * This internal function performs the 'in head noscript' insertion mode 1913 * logic for the generalized WP_HTML_Processor::step() function. 1914 * 1915 * @since 6.7.0 Stub implementation. 1916 * 1917 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 1918 * 1919 * @see https://html.spec.whatwg.org/#parsing-main-inheadnoscript 1920 * @see WP_HTML_Processor::step 1921 * 1922 * @return bool Whether an element was found. 1923 */ 1924 private function step_in_head_noscript(): bool { 1925 $token_name = $this->get_token_name(); 1926 $token_type = $this->get_token_type(); 1927 $is_closer = parent::is_tag_closer(); 1928 $op_sigil = '#tag' === $token_type ? ( $is_closer ? '-' : '+' ) : ''; 1929 $op = "{$op_sigil}{$token_name}"; 1930 1931 switch ( $op ) { 1932 /* 1933 * > A character token that is one of U+0009 CHARACTER TABULATION, 1934 * > U+000A LINE FEED (LF), U+000C FORM FEED (FF), 1935 * > U+000D CARRIAGE RETURN (CR), or U+0020 SPACE 1936 * 1937 * Parse error: ignore the token. 1938 */ 1939 case '#text': 1940 if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) { 1941 return $this->step_in_head(); 1942 } 1943 1944 goto in_head_noscript_anything_else; 1945 break; 1946 1947 /* 1948 * > A DOCTYPE token 1949 */ 1950 case 'html': 1951 // Parse error: ignore the token. 1952 return $this->step(); 1953 1954 /* 1955 * > A start tag whose tag name is "html" 1956 */ 1957 case '+HTML': 1958 return $this->step_in_body(); 1959 1960 /* 1961 * > An end tag whose tag name is "noscript" 1962 */ 1963 case '-NOSCRIPT': 1964 $this->state->stack_of_open_elements->pop(); 1965 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_HEAD; 1966 return true; 1967 1968 /* 1969 * > A comment token 1970 * > 1971 * > A start tag whose tag name is one of: "basefont", "bgsound", 1972 * > "link", "meta", "noframes", "style" 1973 */ 1974 case '#comment': 1975 case '#funky-comment': 1976 case '#presumptuous-tag': 1977 case '+BASEFONT': 1978 case '+BGSOUND': 1979 case '+LINK': 1980 case '+META': 1981 case '+NOFRAMES': 1982 case '+STYLE': 1983 return $this->step_in_head(); 1984 1985 /* 1986 * > An end tag whose tag name is "br" 1987 * 1988 * This should never happen, as the Tag Processor prevents showing a BR closing tag. 1989 */ 1990 } 1991 1992 /* 1993 * > A start tag whose tag name is one of: "head", "noscript" 1994 * > Any other end tag 1995 */ 1996 if ( '+HEAD' === $op || '+NOSCRIPT' === $op || $is_closer ) { 1997 // Parse error: ignore the token. 1998 return $this->step(); 1999 } 2000 2001 /* 2002 * > Anything else 2003 * 2004 * Anything here is a parse error. 2005 */ 2006 in_head_noscript_anything_else: 2007 $this->state->stack_of_open_elements->pop(); 2008 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_HEAD; 2009 return $this->step( self::REPROCESS_CURRENT_NODE ); 2010 } 2011 2012 /** 2013 * Parses next element in the 'after head' insertion mode. 2014 * 2015 * This internal function performs the 'after head' insertion mode 2016 * logic for the generalized WP_HTML_Processor::step() function. 2017 * 2018 * @since 6.7.0 Stub implementation. 2019 * 2020 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 2021 * 2022 * @see https://html.spec.whatwg.org/#the-after-head-insertion-mode 2023 * @see WP_HTML_Processor::step 2024 * 2025 * @return bool Whether an element was found. 2026 */ 2027 private function step_after_head(): bool { 2028 $token_name = $this->get_token_name(); 2029 $token_type = $this->get_token_type(); 2030 $is_closer = parent::is_tag_closer(); 2031 $op_sigil = '#tag' === $token_type ? ( $is_closer ? '-' : '+' ) : ''; 2032 $op = "{$op_sigil}{$token_name}"; 2033 2034 switch ( $op ) { 2035 /* 2036 * > A character token that is one of U+0009 CHARACTER TABULATION, 2037 * > U+000A LINE FEED (LF), U+000C FORM FEED (FF), 2038 * > U+000D CARRIAGE RETURN (CR), or U+0020 SPACE 2039 */ 2040 case '#text': 2041 if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) { 2042 // Insert the character. 2043 $this->insert_html_element( $this->state->current_token ); 2044 return true; 2045 } 2046 goto after_head_anything_else; 2047 break; 2048 2049 /* 2050 * > A comment token 2051 */ 2052 case '#comment': 2053 case '#funky-comment': 2054 case '#presumptuous-tag': 2055 $this->insert_html_element( $this->state->current_token ); 2056 return true; 2057 2058 /* 2059 * > A DOCTYPE token 2060 */ 2061 case 'html': 2062 // Parse error: ignore the token. 2063 return $this->step(); 2064 2065 /* 2066 * > A start tag whose tag name is "html" 2067 */ 2068 case '+HTML': 2069 return $this->step_in_body(); 2070 2071 /* 2072 * > A start tag whose tag name is "body" 2073 */ 2074 case '+BODY': 2075 $this->insert_html_element( $this->state->current_token ); 2076 $this->state->frameset_ok = false; 2077 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY; 2078 return true; 2079 2080 /* 2081 * > A start tag whose tag name is "frameset" 2082 */ 2083 case '+FRAMESET': 2084 $this->insert_html_element( $this->state->current_token ); 2085 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_FRAMESET; 2086 return true; 2087 2088 /* 2089 * > A start tag whose tag name is one of: "base", "basefont", "bgsound", 2090 * > "link", "meta", "noframes", "script", "style", "template", "title" 2091 * 2092 * Anything here is a parse error. 2093 */ 2094 case '+BASE': 2095 case '+BASEFONT': 2096 case '+BGSOUND': 2097 case '+LINK': 2098 case '+META': 2099 case '+NOFRAMES': 2100 case '+SCRIPT': 2101 case '+STYLE': 2102 case '+TEMPLATE': 2103 case '+TITLE': 2104 /* 2105 * > Push the node pointed to by the head element pointer onto the stack of open elements. 2106 * > Process the token using the rules for the "in head" insertion mode. 2107 * > Remove the node pointed to by the head element pointer from the stack of open elements. (It might not be the current node at this point.) 2108 */ 2109 $this->bail( 'Cannot process elements after HEAD which reopen the HEAD element.' ); 2110 /* 2111 * Do not leave this break in when adding support; it's here to prevent 2112 * WPCS from getting confused at the switch structure without a return, 2113 * because it doesn't know that `bail()` always throws. 2114 */ 2115 break; 2116 2117 /* 2118 * > An end tag whose tag name is "template" 2119 */ 2120 case '-TEMPLATE': 2121 return $this->step_in_head(); 2122 2123 /* 2124 * > An end tag whose tag name is one of: "body", "html", "br" 2125 * 2126 * Closing BR tags are always reported by the Tag Processor as opening tags. 2127 */ 2128 case '-BODY': 2129 case '-HTML': 2130 /* 2131 * > Act as described in the "anything else" entry below. 2132 */ 2133 goto after_head_anything_else; 2134 break; 2135 } 2136 2137 /* 2138 * > A start tag whose tag name is "head" 2139 * > Any other end tag 2140 */ 2141 if ( '+HEAD' === $op || $is_closer ) { 2142 // Parse error: ignore the token. 2143 return $this->step(); 2144 } 2145 2146 /* 2147 * > Anything else 2148 * > Insert an HTML element for a "body" start tag token with no attributes. 2149 */ 2150 after_head_anything_else: 2151 $this->insert_virtual_node( 'BODY' ); 2152 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY; 2153 return $this->step( self::REPROCESS_CURRENT_NODE ); 2154 } 2155 2156 /** 2157 * Parses next element in the 'in body' insertion mode. 2158 * 2159 * This internal function performs the 'in body' insertion mode 2160 * logic for the generalized WP_HTML_Processor::step() function. 2161 * 2162 * @since 6.4.0 2163 * 2164 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 2165 * 2166 * @see https://html.spec.whatwg.org/#parsing-main-inbody 2167 * @see WP_HTML_Processor::step 2168 * 2169 * @return bool Whether an element was found. 2170 */ 2171 private function step_in_body(): bool { 2172 $token_name = $this->get_token_name(); 2173 $token_type = $this->get_token_type(); 2174 $op_sigil = '#tag' === $token_type ? ( parent::is_tag_closer() ? '-' : '+' ) : ''; 2175 $op = "{$op_sigil}{$token_name}"; 2176 2177 switch ( $op ) { 2178 case '#text': 2179 /* 2180 * > A character token that is U+0000 NULL 2181 * 2182 * Any successive sequence of NULL bytes is ignored and won't 2183 * trigger active format reconstruction. Therefore, if the text 2184 * only comprises NULL bytes then the token should be ignored 2185 * here, but if there are any other characters in the stream 2186 * the active formats should be reconstructed. 2187 */ 2188 if ( parent::TEXT_IS_NULL_SEQUENCE === $this->text_node_classification ) { 2189 // Parse error: ignore the token. 2190 return $this->step(); 2191 } 2192 2193 $this->reconstruct_active_formatting_elements(); 2194 2195 /* 2196 * Whitespace-only text does not affect the frameset-ok flag. 2197 * It is probably inter-element whitespace, but it may also 2198 * contain character references which decode only to whitespace. 2199 */ 2200 if ( parent::TEXT_IS_GENERIC === $this->text_node_classification ) { 2201 $this->state->frameset_ok = false; 2202 } 2203 2204 $this->insert_html_element( $this->state->current_token ); 2205 return true; 2206 2207 case '#comment': 2208 case '#funky-comment': 2209 case '#presumptuous-tag': 2210 $this->insert_html_element( $this->state->current_token ); 2211 return true; 2212 2213 /* 2214 * > A DOCTYPE token 2215 * > Parse error. Ignore the token. 2216 */ 2217 case 'html': 2218 return $this->step(); 2219 2220 /* 2221 * > A start tag whose tag name is "html" 2222 */ 2223 case '+HTML': 2224 if ( ! $this->state->stack_of_open_elements->contains( 'TEMPLATE' ) ) { 2225 /* 2226 * > Otherwise, for each attribute on the token, check to see if the attribute 2227 * > is already present on the top element of the stack of open elements. If 2228 * > it is not, add the attribute and its corresponding value to that element. 2229 * 2230 * This parser does not currently support this behavior: ignore the token. 2231 */ 2232 } 2233 2234 // Ignore the token. 2235 return $this->step(); 2236 2237 /* 2238 * > A start tag whose tag name is one of: "base", "basefont", "bgsound", "link", 2239 * > "meta", "noframes", "script", "style", "template", "title" 2240 * > 2241 * > An end tag whose tag name is "template" 2242 */ 2243 case '+BASE': 2244 case '+BASEFONT': 2245 case '+BGSOUND': 2246 case '+LINK': 2247 case '+META': 2248 case '+NOFRAMES': 2249 case '+SCRIPT': 2250 case '+STYLE': 2251 case '+TEMPLATE': 2252 case '+TITLE': 2253 case '-TEMPLATE': 2254 return $this->step_in_head(); 2255 2256 /* 2257 * > A start tag whose tag name is "body" 2258 * 2259 * This tag in the IN BODY insertion mode is a parse error. 2260 */ 2261 case '+BODY': 2262 if ( 2263 1 === $this->state->stack_of_open_elements->count() || 2264 'BODY' !== ( $this->state->stack_of_open_elements->at( 2 )->node_name ?? null ) || 2265 $this->state->stack_of_open_elements->contains( 'TEMPLATE' ) 2266 ) { 2267 // Ignore the token. 2268 return $this->step(); 2269 } 2270 2271 /* 2272 * > Otherwise, set the frameset-ok flag to "not ok"; then, for each attribute 2273 * > on the token, check to see if the attribute is already present on the body 2274 * > element (the second element) on the stack of open elements, and if it is 2275 * > not, add the attribute and its corresponding value to that element. 2276 * 2277 * This parser does not currently support this behavior: ignore the token. 2278 */ 2279 $this->state->frameset_ok = false; 2280 return $this->step(); 2281 2282 /* 2283 * > A start tag whose tag name is "frameset" 2284 * 2285 * This tag in the IN BODY insertion mode is a parse error. 2286 */ 2287 case '+FRAMESET': 2288 if ( 2289 1 === $this->state->stack_of_open_elements->count() || 2290 'BODY' !== ( $this->state->stack_of_open_elements->at( 2 )->node_name ?? null ) || 2291 false === $this->state->frameset_ok 2292 ) { 2293 // Ignore the token. 2294 return $this->step(); 2295 } 2296 2297 /* 2298 * > Otherwise, run the following steps: 2299 */ 2300 $this->bail( 'Cannot process non-ignored FRAMESET tags.' ); 2301 break; 2302 2303 /* 2304 * > An end tag whose tag name is "body" 2305 */ 2306 case '-BODY': 2307 if ( ! $this->state->stack_of_open_elements->has_element_in_scope( 'BODY' ) ) { 2308 // Parse error: ignore the token. 2309 return $this->step(); 2310 } 2311 2312 /* 2313 * > Otherwise, if there is a node in the stack of open elements that is not either a 2314 * > dd element, a dt element, an li element, an optgroup element, an option element, 2315 * > a p element, an rb element, an rp element, an rt element, an rtc element, a tbody 2316 * > element, a td element, a tfoot element, a th element, a thread element, a tr 2317 * > element, the body element, or the html element, then this is a parse error. 2318 * 2319 * There is nothing to do for this parse error, so don't check for it. 2320 */ 2321 2322 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_AFTER_BODY; 2323 /* 2324 * The BODY element is not removed from the stack of open elements. 2325 * Only internal state has changed, this does not qualify as a "step" 2326 * in terms of advancing through the document to another token. 2327 * Nothing has been pushed or popped. 2328 * Proceed to parse the next item. 2329 */ 2330 return $this->step(); 2331 2332 /* 2333 * > An end tag whose tag name is "html" 2334 */ 2335 case '-HTML': 2336 if ( ! $this->state->stack_of_open_elements->has_element_in_scope( 'BODY' ) ) { 2337 // Parse error: ignore the token. 2338 return $this->step(); 2339 } 2340 2341 /* 2342 * > Otherwise, if there is a node in the stack of open elements that is not either a 2343 * > dd element, a dt element, an li element, an optgroup element, an option element, 2344 * > a p element, an rb element, an rp element, an rt element, an rtc element, a tbody 2345 * > element, a td element, a tfoot element, a th element, a thread element, a tr 2346 * > element, the body element, or the html element, then this is a parse error. 2347 * 2348 * There is nothing to do for this parse error, so don't check for it. 2349 */ 2350 2351 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_AFTER_BODY; 2352 return $this->step( self::REPROCESS_CURRENT_NODE ); 2353 2354 /* 2355 * > A start tag whose tag name is one of: "address", "article", "aside", 2356 * > "blockquote", "center", "details", "dialog", "dir", "div", "dl", 2357 * > "fieldset", "figcaption", "figure", "footer", "header", "hgroup", 2358 * > "main", "menu", "nav", "ol", "p", "search", "section", "summary", "ul" 2359 */ 2360 case '+ADDRESS': 2361 case '+ARTICLE': 2362 case '+ASIDE': 2363 case '+BLOCKQUOTE': 2364 case '+CENTER': 2365 case '+DETAILS': 2366 case '+DIALOG': 2367 case '+DIR': 2368 case '+DIV': 2369 case '+DL': 2370 case '+FIELDSET': 2371 case '+FIGCAPTION': 2372 case '+FIGURE': 2373 case '+FOOTER': 2374 case '+HEADER': 2375 case '+HGROUP': 2376 case '+MAIN': 2377 case '+MENU': 2378 case '+NAV': 2379 case '+OL': 2380 case '+P': 2381 case '+SEARCH': 2382 case '+SECTION': 2383 case '+SUMMARY': 2384 case '+UL': 2385 if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) { 2386 $this->close_a_p_element(); 2387 } 2388 2389 $this->insert_html_element( $this->state->current_token ); 2390 return true; 2391 2392 /* 2393 * > A start tag whose tag name is one of: "h1", "h2", "h3", "h4", "h5", "h6" 2394 */ 2395 case '+H1': 2396 case '+H2': 2397 case '+H3': 2398 case '+H4': 2399 case '+H5': 2400 case '+H6': 2401 if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) { 2402 $this->close_a_p_element(); 2403 } 2404 2405 if ( 2406 in_array( 2407 $this->state->stack_of_open_elements->current_node()->node_name, 2408 array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), 2409 true 2410 ) 2411 ) { 2412 // @todo Indicate a parse error once it's possible. 2413 $this->state->stack_of_open_elements->pop(); 2414 } 2415 2416 $this->insert_html_element( $this->state->current_token ); 2417 return true; 2418 2419 /* 2420 * > A start tag whose tag name is one of: "pre", "listing" 2421 */ 2422 case '+PRE': 2423 case '+LISTING': 2424 if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) { 2425 $this->close_a_p_element(); 2426 } 2427 2428 /* 2429 * > If the next token is a U+000A LINE FEED (LF) character token, 2430 * > then ignore that token and move on to the next one. (Newlines 2431 * > at the start of pre blocks are ignored as an authoring convenience.) 2432 * 2433 * This is handled in `get_modifiable_text()`. 2434 */ 2435 2436 $this->insert_html_element( $this->state->current_token ); 2437 $this->state->frameset_ok = false; 2438 return true; 2439 2440 /* 2441 * > A start tag whose tag name is "form" 2442 */ 2443 case '+FORM': 2444 $stack_contains_template = $this->state->stack_of_open_elements->contains( 'TEMPLATE' ); 2445 2446 if ( isset( $this->state->form_element ) && ! $stack_contains_template ) { 2447 // Parse error: ignore the token. 2448 return $this->step(); 2449 } 2450 2451 if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) { 2452 $this->close_a_p_element(); 2453 } 2454 2455 $this->insert_html_element( $this->state->current_token ); 2456 if ( ! $stack_contains_template ) { 2457 $this->state->form_element = $this->state->current_token; 2458 } 2459 2460 return true; 2461 2462 /* 2463 * > A start tag whose tag name is "li" 2464 * > A start tag whose tag name is one of: "dd", "dt" 2465 */ 2466 case '+DD': 2467 case '+DT': 2468 case '+LI': 2469 $this->state->frameset_ok = false; 2470 $node = $this->state->stack_of_open_elements->current_node(); 2471 $is_li = 'LI' === $token_name; 2472 2473 in_body_list_loop: 2474 /* 2475 * The logic for LI and DT/DD is the same except for one point: LI elements _only_ 2476 * close other LI elements, but a DT or DD element closes _any_ open DT or DD element. 2477 */ 2478 if ( $is_li ? 'LI' === $node->node_name : ( 'DD' === $node->node_name || 'DT' === $node->node_name ) ) { 2479 $node_name = $is_li ? 'LI' : $node->node_name; 2480 $this->generate_implied_end_tags( $node_name ); 2481 if ( ! $this->state->stack_of_open_elements->current_node_is( $node_name ) ) { 2482 // @todo Indicate a parse error once it's possible. This error does not impact the logic here. 2483 } 2484 2485 $this->state->stack_of_open_elements->pop_until( $node_name ); 2486 goto in_body_list_done; 2487 } 2488 2489 if ( 2490 'ADDRESS' !== $node->node_name && 2491 'DIV' !== $node->node_name && 2492 'P' !== $node->node_name && 2493 self::is_special( $node ) 2494 ) { 2495 /* 2496 * > If node is in the special category, but is not an address, div, 2497 * > or p element, then jump to the step labeled done below. 2498 */ 2499 goto in_body_list_done; 2500 } else { 2501 /* 2502 * > Otherwise, set node to the previous entry in the stack of open elements 2503 * > and return to the step labeled loop. 2504 */ 2505 foreach ( $this->state->stack_of_open_elements->walk_up( $node ) as $item ) { 2506 $node = $item; 2507 break; 2508 } 2509 goto in_body_list_loop; 2510 } 2511 2512 in_body_list_done: 2513 if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) { 2514 $this->close_a_p_element(); 2515 } 2516 2517 $this->insert_html_element( $this->state->current_token ); 2518 return true; 2519 2520 case '+PLAINTEXT': 2521 if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) { 2522 $this->close_a_p_element(); 2523 } 2524 2525 /* 2526 * @todo This may need to be handled in the Tag Processor and turn into 2527 * a single self-contained tag like TEXTAREA, whose modifiable text 2528 * is the rest of the input document as plaintext. 2529 */ 2530 $this->bail( 'Cannot process PLAINTEXT elements.' ); 2531 break; 2532 2533 /* 2534 * > A start tag whose tag name is "button" 2535 */ 2536 case '+BUTTON': 2537 if ( $this->state->stack_of_open_elements->has_element_in_scope( 'BUTTON' ) ) { 2538 // @todo Indicate a parse error once it's possible. This error does not impact the logic here. 2539 $this->generate_implied_end_tags(); 2540 $this->state->stack_of_open_elements->pop_until( 'BUTTON' ); 2541 } 2542 2543 $this->reconstruct_active_formatting_elements(); 2544 $this->insert_html_element( $this->state->current_token ); 2545 $this->state->frameset_ok = false; 2546 2547 return true; 2548 2549 /* 2550 * > An end tag whose tag name is one of: "address", "article", "aside", "blockquote", 2551 * > "button", "center", "details", "dialog", "dir", "div", "dl", "fieldset", 2552 * > "figcaption", "figure", "footer", "header", "hgroup", "listing", "main", 2553 * > "menu", "nav", "ol", "pre", "search", "section", "summary", "ul" 2554 */ 2555 case '-ADDRESS': 2556 case '-ARTICLE': 2557 case '-ASIDE': 2558 case '-BLOCKQUOTE': 2559 case '-BUTTON': 2560 case '-CENTER': 2561 case '-DETAILS': 2562 case '-DIALOG': 2563 case '-DIR': 2564 case '-DIV': 2565 case '-DL': 2566 case '-FIELDSET': 2567 case '-FIGCAPTION': 2568 case '-FIGURE': 2569 case '-FOOTER': 2570 case '-HEADER': 2571 case '-HGROUP': 2572 case '-LISTING': 2573 case '-MAIN': 2574 case '-MENU': 2575 case '-NAV': 2576 case '-OL': 2577 case '-PRE': 2578 case '-SEARCH': 2579 case '-SECTION': 2580 case '-SUMMARY': 2581 case '-UL': 2582 if ( ! $this->state->stack_of_open_elements->has_element_in_scope( $token_name ) ) { 2583 // @todo Report parse error. 2584 // Ignore the token. 2585 return $this->step(); 2586 } 2587 2588 $this->generate_implied_end_tags(); 2589 if ( ! $this->state->stack_of_open_elements->current_node_is( $token_name ) ) { 2590 // @todo Record parse error: this error doesn't impact parsing. 2591 } 2592 $this->state->stack_of_open_elements->pop_until( $token_name ); 2593 return true; 2594 2595 /* 2596 * > An end tag whose tag name is "form" 2597 */ 2598 case '-FORM': 2599 if ( ! $this->state->stack_of_open_elements->contains( 'TEMPLATE' ) ) { 2600 $node = $this->state->form_element; 2601 $this->state->form_element = null; 2602 2603 /* 2604 * > If node is null or if the stack of open elements does not have node 2605 * > in scope, then this is a parse error; return and ignore the token. 2606 * 2607 * @todo It's necessary to check if the form token itself is in scope, not 2608 * simply whether any FORM is in scope. 2609 */ 2610 if ( 2611 null === $node || 2612 ! $this->state->stack_of_open_elements->has_element_in_scope( 'FORM' ) 2613 ) { 2614 // Parse error: ignore the token. 2615 return $this->step(); 2616 } 2617 2618 $this->generate_implied_end_tags(); 2619 if ( $node !== $this->state->stack_of_open_elements->current_node() ) { 2620 // @todo Indicate a parse error once it's possible. This error does not impact the logic here. 2621 $this->bail( 'Cannot close a FORM when other elements remain open as this would throw off the breadcrumbs for the following tokens.' ); 2622 } 2623 2624 $this->state->stack_of_open_elements->remove_node( $node ); 2625 return true; 2626 } else { 2627 /* 2628 * > If the stack of open elements does not have a form element in scope, 2629 * > then this is a parse error; return and ignore the token. 2630 * 2631 * Note that unlike in the clause above, this is checking for any FORM in scope. 2632 */ 2633 if ( ! $this->state->stack_of_open_elements->has_element_in_scope( 'FORM' ) ) { 2634 // Parse error: ignore the token. 2635 return $this->step(); 2636 } 2637 2638 $this->generate_implied_end_tags(); 2639 2640 if ( ! $this->state->stack_of_open_elements->current_node_is( 'FORM' ) ) { 2641 // @todo Indicate a parse error once it's possible. This error does not impact the logic here. 2642 } 2643 2644 $this->state->stack_of_open_elements->pop_until( 'FORM' ); 2645 return true; 2646 } 2647 break; 2648 2649 /* 2650 * > An end tag whose tag name is "p" 2651 */ 2652 case '-P': 2653 if ( ! $this->state->stack_of_open_elements->has_p_in_button_scope() ) { 2654 $this->insert_html_element( $this->state->current_token ); 2655 } 2656 2657 $this->close_a_p_element(); 2658 return true; 2659 2660 /* 2661 * > An end tag whose tag name is "li" 2662 * > An end tag whose tag name is one of: "dd", "dt" 2663 */ 2664 case '-DD': 2665 case '-DT': 2666 case '-LI': 2667 if ( 2668 /* 2669 * An end tag whose tag name is "li": 2670 * If the stack of open elements does not have an li element in list item scope, 2671 * then this is a parse error; ignore the token. 2672 */ 2673 ( 2674 'LI' === $token_name && 2675 ! $this->state->stack_of_open_elements->has_element_in_list_item_scope( 'LI' ) 2676 ) || 2677 /* 2678 * An end tag whose tag name is one of: "dd", "dt": 2679 * If the stack of open elements does not have an element in scope that is an 2680 * HTML element with the same tag name as that of the token, then this is a 2681 * parse error; ignore the token. 2682 */ 2683 ( 2684 'LI' !== $token_name && 2685 ! $this->state->stack_of_open_elements->has_element_in_scope( $token_name ) 2686 ) 2687 ) { 2688 /* 2689 * This is a parse error, ignore the token. 2690 * 2691 * @todo Indicate a parse error once it's possible. 2692 */ 2693 return $this->step(); 2694 } 2695 2696 $this->generate_implied_end_tags( $token_name ); 2697 2698 if ( ! $this->state->stack_of_open_elements->current_node_is( $token_name ) ) { 2699 // @todo Indicate a parse error once it's possible. This error does not impact the logic here. 2700 } 2701 2702 $this->state->stack_of_open_elements->pop_until( $token_name ); 2703 return true; 2704 2705 /* 2706 * > An end tag whose tag name is one of: "h1", "h2", "h3", "h4", "h5", "h6" 2707 */ 2708 case '-H1': 2709 case '-H2': 2710 case '-H3': 2711 case '-H4': 2712 case '-H5': 2713 case '-H6': 2714 if ( ! $this->state->stack_of_open_elements->has_element_in_scope( '(internal: H1 through H6 - do not use)' ) ) { 2715 /* 2716 * This is a parse error; ignore the token. 2717 * 2718 * @todo Indicate a parse error once it's possible. 2719 */ 2720 return $this->step(); 2721 } 2722 2723 $this->generate_implied_end_tags(); 2724 2725 if ( ! $this->state->stack_of_open_elements->current_node_is( $token_name ) ) { 2726 // @todo Record parse error: this error doesn't impact parsing. 2727 } 2728 2729 $this->state->stack_of_open_elements->pop_until( '(internal: H1 through H6 - do not use)' ); 2730 return true; 2731 2732 /* 2733 * > A start tag whose tag name is "a" 2734 */ 2735 case '+A': 2736 foreach ( $this->state->active_formatting_elements->walk_up() as $item ) { 2737 switch ( $item->node_name ) { 2738 case 'marker': 2739 break 2; 2740 2741 case 'A': 2742 $this->run_adoption_agency_algorithm(); 2743 $this->state->active_formatting_elements->remove_node( $item ); 2744 $this->state->stack_of_open_elements->remove_node( $item ); 2745 break 2; 2746 } 2747 } 2748 2749 $this->reconstruct_active_formatting_elements(); 2750 $this->insert_html_element( $this->state->current_token ); 2751 $this->state->active_formatting_elements->push( $this->state->current_token ); 2752 return true; 2753 2754 /* 2755 * > A start tag whose tag name is one of: "b", "big", "code", "em", "font", "i", 2756 * > "s", "small", "strike", "strong", "tt", "u" 2757 */ 2758 case '+B': 2759 case '+BIG': 2760 case '+CODE': 2761 case '+EM': 2762 case '+FONT': 2763 case '+I': 2764 case '+S': 2765 case '+SMALL': 2766 case '+STRIKE': 2767 case '+STRONG': 2768 case '+TT': 2769 case '+U': 2770 $this->reconstruct_active_formatting_elements(); 2771 $this->insert_html_element( $this->state->current_token ); 2772 $this->state->active_formatting_elements->push( $this->state->current_token ); 2773 return true; 2774 2775 /* 2776 * > A start tag whose tag name is "nobr" 2777 */ 2778 case '+NOBR': 2779 $this->reconstruct_active_formatting_elements(); 2780 2781 if ( $this->state->stack_of_open_elements->has_element_in_scope( 'NOBR' ) ) { 2782 // Parse error. 2783 $this->run_adoption_agency_algorithm(); 2784 $this->reconstruct_active_formatting_elements(); 2785 } 2786 2787 $this->insert_html_element( $this->state->current_token ); 2788 $this->state->active_formatting_elements->push( $this->state->current_token ); 2789 return true; 2790 2791 /* 2792 * > An end tag whose tag name is one of: "a", "b", "big", "code", "em", "font", "i", 2793 * > "nobr", "s", "small", "strike", "strong", "tt", "u" 2794 */ 2795 case '-A': 2796 case '-B': 2797 case '-BIG': 2798 case '-CODE': 2799 case '-EM': 2800 case '-FONT': 2801 case '-I': 2802 case '-NOBR': 2803 case '-S': 2804 case '-SMALL': 2805 case '-STRIKE': 2806 case '-STRONG': 2807 case '-TT': 2808 case '-U': 2809 $this->run_adoption_agency_algorithm(); 2810 return true; 2811 2812 /* 2813 * > A start tag whose tag name is one of: "applet", "marquee", "object" 2814 */ 2815 case '+APPLET': 2816 case '+MARQUEE': 2817 case '+OBJECT': 2818 $this->reconstruct_active_formatting_elements(); 2819 $this->insert_html_element( $this->state->current_token ); 2820 $this->state->active_formatting_elements->insert_marker(); 2821 $this->state->frameset_ok = false; 2822 return true; 2823 2824 /* 2825 * > A end tag token whose tag name is one of: "applet", "marquee", "object" 2826 */ 2827 case '-APPLET': 2828 case '-MARQUEE': 2829 case '-OBJECT': 2830 if ( ! $this->state->stack_of_open_elements->has_element_in_scope( $token_name ) ) { 2831 // Parse error: ignore the token. 2832 return $this->step(); 2833 } 2834 2835 $this->generate_implied_end_tags(); 2836 if ( ! $this->state->stack_of_open_elements->current_node_is( $token_name ) ) { 2837 // This is a parse error. 2838 } 2839 2840 $this->state->stack_of_open_elements->pop_until( $token_name ); 2841 $this->state->active_formatting_elements->clear_up_to_last_marker(); 2842 return true; 2843 2844 /* 2845 * > A start tag whose tag name is "table" 2846 */ 2847 case '+TABLE': 2848 /* 2849 * > If the Document is not set to quirks mode, and the stack of open elements 2850 * > has a p element in button scope, then close a p element. 2851 */ 2852 if ( 2853 WP_HTML_Tag_Processor::QUIRKS_MODE !== $this->compat_mode && 2854 $this->state->stack_of_open_elements->has_p_in_button_scope() 2855 ) { 2856 $this->close_a_p_element(); 2857 } 2858 2859 $this->insert_html_element( $this->state->current_token ); 2860 $this->state->frameset_ok = false; 2861 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE; 2862 return true; 2863 2864 /* 2865 * > An end tag whose tag name is "br" 2866 * 2867 * This is prevented from happening because the Tag Processor 2868 * reports all closing BR tags as if they were opening tags. 2869 */ 2870 2871 /* 2872 * > A start tag whose tag name is one of: "area", "br", "embed", "img", "keygen", "wbr" 2873 */ 2874 case '+AREA': 2875 case '+BR': 2876 case '+EMBED': 2877 case '+IMG': 2878 case '+KEYGEN': 2879 case '+WBR': 2880 $this->reconstruct_active_formatting_elements(); 2881 $this->insert_html_element( $this->state->current_token ); 2882 $this->state->frameset_ok = false; 2883 return true; 2884 2885 /* 2886 * > A start tag whose tag name is "input" 2887 */ 2888 case '+INPUT': 2889 $this->reconstruct_active_formatting_elements(); 2890 $this->insert_html_element( $this->state->current_token ); 2891 2892 /* 2893 * > If the token does not have an attribute with the name "type", or if it does, 2894 * > but that attribute's value is not an ASCII case-insensitive match for the 2895 * > string "hidden", then: set the frameset-ok flag to "not ok". 2896 */ 2897 $type_attribute = $this->get_attribute( 'type' ); 2898 if ( ! is_string( $type_attribute ) || 'hidden' !== strtolower( $type_attribute ) ) { 2899 $this->state->frameset_ok = false; 2900 } 2901 2902 return true; 2903 2904 /* 2905 * > A start tag whose tag name is one of: "param", "source", "track" 2906 */ 2907 case '+PARAM': 2908 case '+SOURCE': 2909 case '+TRACK': 2910 $this->insert_html_element( $this->state->current_token ); 2911 return true; 2912 2913 /* 2914 * > A start tag whose tag name is "hr" 2915 */ 2916 case '+HR': 2917 if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) { 2918 $this->close_a_p_element(); 2919 } 2920 $this->insert_html_element( $this->state->current_token ); 2921 $this->state->frameset_ok = false; 2922 return true; 2923 2924 /* 2925 * > A start tag whose tag name is "image" 2926 */ 2927 case '+IMAGE': 2928 /* 2929 * > Parse error. Change the token's tag name to "img" and reprocess it. (Don't ask.) 2930 * 2931 * Note that this is handled elsewhere, so it should not be possible to reach this code. 2932 */ 2933 $this->bail( "Cannot process an IMAGE tag. (Don't ask.)" ); 2934 break; 2935 2936 /* 2937 * > A start tag whose tag name is "textarea" 2938 */ 2939 case '+TEXTAREA': 2940 $this->insert_html_element( $this->state->current_token ); 2941 2942 /* 2943 * > If the next token is a U+000A LINE FEED (LF) character token, then ignore 2944 * > that token and move on to the next one. (Newlines at the start of 2945 * > textarea elements are ignored as an authoring convenience.) 2946 * 2947 * This is handled in `get_modifiable_text()`. 2948 */ 2949 2950 $this->state->frameset_ok = false; 2951 2952 /* 2953 * > Switch the insertion mode to "text". 2954 * 2955 * As a self-contained node, this behavior is handled in the Tag Processor. 2956 */ 2957 return true; 2958 2959 /* 2960 * > A start tag whose tag name is "xmp" 2961 */ 2962 case '+XMP': 2963 if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) { 2964 $this->close_a_p_element(); 2965 } 2966 2967 $this->reconstruct_active_formatting_elements(); 2968 $this->state->frameset_ok = false; 2969 2970 /* 2971 * > Follow the generic raw text element parsing algorithm. 2972 * 2973 * As a self-contained node, this behavior is handled in the Tag Processor. 2974 */ 2975 $this->insert_html_element( $this->state->current_token ); 2976 return true; 2977 2978 /* 2979 * A start tag whose tag name is "iframe" 2980 */ 2981 case '+IFRAME': 2982 $this->state->frameset_ok = false; 2983 2984 /* 2985 * > Follow the generic raw text element parsing algorithm. 2986 * 2987 * As a self-contained node, this behavior is handled in the Tag Processor. 2988 */ 2989 $this->insert_html_element( $this->state->current_token ); 2990 return true; 2991 2992 /* 2993 * > A start tag whose tag name is "noembed" 2994 * > A start tag whose tag name is "noscript", if the scripting flag is enabled 2995 * 2996 * The scripting flag is never enabled in this parser. 2997 */ 2998 case '+NOEMBED': 2999 $this->insert_html_element( $this->state->current_token ); 3000 return true; 3001 3002 /* 3003 * > A start tag whose tag name is "select" 3004 */ 3005 case '+SELECT': 3006 $this->reconstruct_active_formatting_elements(); 3007 $this->insert_html_element( $this->state->current_token ); 3008 $this->state->frameset_ok = false; 3009 3010 switch ( $this->state->insertion_mode ) { 3011 /* 3012 * > If the insertion mode is one of "in table", "in caption", "in table body", "in row", 3013 * > or "in cell", then switch the insertion mode to "in select in table". 3014 */ 3015 case WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE: 3016 case WP_HTML_Processor_State::INSERTION_MODE_IN_CAPTION: 3017 case WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY: 3018 case WP_HTML_Processor_State::INSERTION_MODE_IN_ROW: 3019 case WP_HTML_Processor_State::INSERTION_MODE_IN_CELL: 3020 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_SELECT_IN_TABLE; 3021 break; 3022 3023 /* 3024 * > Otherwise, switch the insertion mode to "in select". 3025 */ 3026 default: 3027 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_SELECT; 3028 break; 3029 } 3030 return true; 3031 3032 /* 3033 * > A start tag whose tag name is one of: "optgroup", "option" 3034 */ 3035 case '+OPTGROUP': 3036 case '+OPTION': 3037 if ( $this->state->stack_of_open_elements->current_node_is( 'OPTION' ) ) { 3038 $this->state->stack_of_open_elements->pop(); 3039 } 3040 $this->reconstruct_active_formatting_elements(); 3041 $this->insert_html_element( $this->state->current_token ); 3042 return true; 3043 3044 /* 3045 * > A start tag whose tag name is one of: "rb", "rtc" 3046 */ 3047 case '+RB': 3048 case '+RTC': 3049 if ( $this->state->stack_of_open_elements->has_element_in_scope( 'RUBY' ) ) { 3050 $this->generate_implied_end_tags(); 3051 3052 if ( $this->state->stack_of_open_elements->current_node_is( 'RUBY' ) ) { 3053 // @todo Indicate a parse error once it's possible. 3054 } 3055 } 3056 3057 $this->insert_html_element( $this->state->current_token ); 3058 return true; 3059 3060 /* 3061 * > A start tag whose tag name is one of: "rp", "rt" 3062 */ 3063 case '+RP': 3064 case '+RT': 3065 if ( $this->state->stack_of_open_elements->has_element_in_scope( 'RUBY' ) ) { 3066 $this->generate_implied_end_tags( 'RTC' ); 3067 3068 $current_node_name = $this->state->stack_of_open_elements->current_node()->node_name; 3069 if ( 'RTC' === $current_node_name || 'RUBY' === $current_node_name ) { 3070 // @todo Indicate a parse error once it's possible. 3071 } 3072 } 3073 3074 $this->insert_html_element( $this->state->current_token ); 3075 return true; 3076 3077 /* 3078 * > A start tag whose tag name is "math" 3079 */ 3080 case '+MATH': 3081 $this->reconstruct_active_formatting_elements(); 3082 3083 /* 3084 * @todo Adjust MathML attributes for the token. (This fixes the case of MathML attributes that are not all lowercase.) 3085 * @todo Adjust foreign attributes for the token. (This fixes the use of namespaced attributes, in particular XLink.) 3086 * 3087 * These ought to be handled in the attribute methods. 3088 */ 3089 $this->state->current_token->namespace = 'math'; 3090 $this->insert_html_element( $this->state->current_token ); 3091 if ( $this->state->current_token->has_self_closing_flag ) { 3092 $this->state->stack_of_open_elements->pop(); 3093 } 3094 return true; 3095 3096 /* 3097 * > A start tag whose tag name is "svg" 3098 */ 3099 case '+SVG': 3100 $this->reconstruct_active_formatting_elements(); 3101 3102 /* 3103 * @todo Adjust SVG attributes for the token. (This fixes the case of SVG attributes that are not all lowercase.) 3104 * @todo Adjust foreign attributes for the token. (This fixes the use of namespaced attributes, in particular XLink in SVG.) 3105 * 3106 * These ought to be handled in the attribute methods. 3107 */ 3108 $this->state->current_token->namespace = 'svg'; 3109 $this->insert_html_element( $this->state->current_token ); 3110 if ( $this->state->current_token->has_self_closing_flag ) { 3111 $this->state->stack_of_open_elements->pop(); 3112 } 3113 return true; 3114 3115 /* 3116 * > A start tag whose tag name is one of: "caption", "col", "colgroup", 3117 * > "frame", "head", "tbody", "td", "tfoot", "th", "thead", "tr" 3118 */ 3119 case '+CAPTION': 3120 case '+COL': 3121 case '+COLGROUP': 3122 case '+FRAME': 3123 case '+HEAD': 3124 case '+TBODY': 3125 case '+TD': 3126 case '+TFOOT': 3127 case '+TH': 3128 case '+THEAD': 3129 case '+TR': 3130 // Parse error. Ignore the token. 3131 return $this->step(); 3132 } 3133 3134 if ( ! parent::is_tag_closer() ) { 3135 /* 3136 * > Any other start tag 3137 */ 3138 $this->reconstruct_active_formatting_elements(); 3139 $this->insert_html_element( $this->state->current_token ); 3140 return true; 3141 } else { 3142 /* 3143 * > Any other end tag 3144 */ 3145 3146 /* 3147 * Find the corresponding tag opener in the stack of open elements, if 3148 * it exists before reaching a special element, which provides a kind 3149 * of boundary in the stack. For example, a `</custom-tag>` should not 3150 * close anything beyond its containing `P` or `DIV` element. 3151 */ 3152 foreach ( $this->state->stack_of_open_elements->walk_up() as $node ) { 3153 if ( 'html' === $node->namespace && $token_name === $node->node_name ) { 3154 break; 3155 } 3156 3157 if ( self::is_special( $node ) ) { 3158 // This is a parse error, ignore the token. 3159 return $this->step(); 3160 } 3161 } 3162 3163 $this->generate_implied_end_tags( $token_name ); 3164 if ( $node !== $this->state->stack_of_open_elements->current_node() ) { 3165 // @todo Record parse error: this error doesn't impact parsing. 3166 } 3167 3168 foreach ( $this->state->stack_of_open_elements->walk_up() as $item ) { 3169 $this->state->stack_of_open_elements->pop(); 3170 if ( $node === $item ) { 3171 return true; 3172 } 3173 } 3174 } 3175 3176 $this->bail( 'Should not have been able to reach end of IN BODY processing. Check HTML API code.' ); 3177 // This unnecessary return prevents tools from inaccurately reporting type errors. 3178 return false; 3179 } 3180 3181 /** 3182 * Parses next element in the 'in table' insertion mode. 3183 * 3184 * This internal function performs the 'in table' insertion mode 3185 * logic for the generalized WP_HTML_Processor::step() function. 3186 * 3187 * @since 6.7.0 3188 * 3189 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 3190 * 3191 * @see https://html.spec.whatwg.org/#parsing-main-intable 3192 * @see WP_HTML_Processor::step 3193 * 3194 * @return bool Whether an element was found. 3195 */ 3196 private function step_in_table(): bool { 3197 $token_name = $this->get_token_name(); 3198 $token_type = $this->get_token_type(); 3199 $op_sigil = '#tag' === $token_type ? ( parent::is_tag_closer() ? '-' : '+' ) : ''; 3200 $op = "{$op_sigil}{$token_name}"; 3201 3202 switch ( $op ) { 3203 /* 3204 * > A character token, if the current node is table, 3205 * > tbody, template, tfoot, thead, or tr element 3206 */ 3207 case '#text': 3208 $current_node = $this->state->stack_of_open_elements->current_node(); 3209 $current_node_name = $current_node ? $current_node->node_name : null; 3210 if ( 3211 $current_node_name && ( 3212 'TABLE' === $current_node_name || 3213 'TBODY' === $current_node_name || 3214 'TEMPLATE' === $current_node_name || 3215 'TFOOT' === $current_node_name || 3216 'THEAD' === $current_node_name || 3217 'TR' === $current_node_name 3218 ) 3219 ) { 3220 /* 3221 * If the text is empty after processing HTML entities and stripping 3222 * U+0000 NULL bytes then ignore the token. 3223 */ 3224 if ( parent::TEXT_IS_NULL_SEQUENCE === $this->text_node_classification ) { 3225 return $this->step(); 3226 } 3227 3228 /* 3229 * This follows the rules for "in table text" insertion mode. 3230 * 3231 * Whitespace-only text nodes are inserted in-place. Otherwise 3232 * foster parenting is enabled and the nodes would be 3233 * inserted out-of-place. 3234 * 3235 * > If any of the tokens in the pending table character tokens 3236 * > list are character tokens that are not ASCII whitespace, 3237 * > then this is a parse error: reprocess the character tokens 3238 * > in the pending table character tokens list using the rules 3239 * > given in the "anything else" entry in the "in table" 3240 * > insertion mode. 3241 * > 3242 * > Otherwise, insert the characters given by the pending table 3243 * > character tokens list. 3244 * 3245 * @see https://html.spec.whatwg.org/#parsing-main-intabletext 3246 */ 3247 if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) { 3248 $this->insert_html_element( $this->state->current_token ); 3249 return true; 3250 } 3251 3252 // Non-whitespace would trigger fostering, unsupported at this time. 3253 $this->bail( 'Foster parenting is not supported.' ); 3254 break; 3255 } 3256 break; 3257 3258 /* 3259 * > A comment token 3260 */ 3261 case '#comment': 3262 case '#funky-comment': 3263 case '#presumptuous-tag': 3264 $this->insert_html_element( $this->state->current_token ); 3265 return true; 3266 3267 /* 3268 * > A DOCTYPE token 3269 */ 3270 case 'html': 3271 // Parse error: ignore the token. 3272 return $this->step(); 3273 3274 /* 3275 * > A start tag whose tag name is "caption" 3276 */ 3277 case '+CAPTION': 3278 $this->state->stack_of_open_elements->clear_to_table_context(); 3279 $this->state->active_formatting_elements->insert_marker(); 3280 $this->insert_html_element( $this->state->current_token ); 3281 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_CAPTION; 3282 return true; 3283 3284 /* 3285 * > A start tag whose tag name is "colgroup" 3286 */ 3287 case '+COLGROUP': 3288 $this->state->stack_of_open_elements->clear_to_table_context(); 3289 $this->insert_html_element( $this->state->current_token ); 3290 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_COLUMN_GROUP; 3291 return true; 3292 3293 /* 3294 * > A start tag whose tag name is "col" 3295 */ 3296 case '+COL': 3297 $this->state->stack_of_open_elements->clear_to_table_context(); 3298 3299 /* 3300 * > Insert an HTML element for a "colgroup" start tag token with no attributes, 3301 * > then switch the insertion mode to "in column group". 3302 */ 3303 $this->insert_virtual_node( 'COLGROUP' ); 3304 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_COLUMN_GROUP; 3305 return $this->step( self::REPROCESS_CURRENT_NODE ); 3306 3307 /* 3308 * > A start tag whose tag name is one of: "tbody", "tfoot", "thead" 3309 */ 3310 case '+TBODY': 3311 case '+TFOOT': 3312 case '+THEAD': 3313 $this->state->stack_of_open_elements->clear_to_table_context(); 3314 $this->insert_html_element( $this->state->current_token ); 3315 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY; 3316 return true; 3317 3318 /* 3319 * > A start tag whose tag name is one of: "td", "th", "tr" 3320 */ 3321 case '+TD': 3322 case '+TH': 3323 case '+TR': 3324 $this->state->stack_of_open_elements->clear_to_table_context(); 3325 /* 3326 * > Insert an HTML element for a "tbody" start tag token with no attributes, 3327 * > then switch the insertion mode to "in table body". 3328 */ 3329 $this->insert_virtual_node( 'TBODY' ); 3330 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY; 3331 return $this->step( self::REPROCESS_CURRENT_NODE ); 3332 3333 /* 3334 * > A start tag whose tag name is "table" 3335 * 3336 * This tag in the IN TABLE insertion mode is a parse error. 3337 */ 3338 case '+TABLE': 3339 if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TABLE' ) ) { 3340 return $this->step(); 3341 } 3342 3343 $this->state->stack_of_open_elements->pop_until( 'TABLE' ); 3344 $this->reset_insertion_mode_appropriately(); 3345 return $this->step( self::REPROCESS_CURRENT_NODE ); 3346 3347 /* 3348 * > An end tag whose tag name is "table" 3349 */ 3350 case '-TABLE': 3351 if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TABLE' ) ) { 3352 // @todo Indicate a parse error once it's possible. 3353 return $this->step(); 3354 } 3355 3356 $this->state->stack_of_open_elements->pop_until( 'TABLE' ); 3357 $this->reset_insertion_mode_appropriately(); 3358 return true; 3359 3360 /* 3361 * > An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr" 3362 */ 3363 case '-BODY': 3364 case '-CAPTION': 3365 case '-COL': 3366 case '-COLGROUP': 3367 case '-HTML': 3368 case '-TBODY': 3369 case '-TD': 3370 case '-TFOOT': 3371 case '-TH': 3372 case '-THEAD': 3373 case '-TR': 3374 // Parse error: ignore the token. 3375 return $this->step(); 3376 3377 /* 3378 * > A start tag whose tag name is one of: "style", "script", "template" 3379 * > An end tag whose tag name is "template" 3380 */ 3381 case '+STYLE': 3382 case '+SCRIPT': 3383 case '+TEMPLATE': 3384 case '-TEMPLATE': 3385 /* 3386 * > Process the token using the rules for the "in head" insertion mode. 3387 */ 3388 return $this->step_in_head(); 3389 3390 /* 3391 * > A start tag whose tag name is "input" 3392 * 3393 * > If the token does not have an attribute with the name "type", or if it does, but 3394 * > that attribute's value is not an ASCII case-insensitive match for the string 3395 * > "hidden", then: act as described in the "anything else" entry below. 3396 */ 3397 case '+INPUT': 3398 $type_attribute = $this->get_attribute( 'type' ); 3399 if ( ! is_string( $type_attribute ) || 'hidden' !== strtolower( $type_attribute ) ) { 3400 goto anything_else; 3401 } 3402 // @todo Indicate a parse error once it's possible. 3403 $this->insert_html_element( $this->state->current_token ); 3404 return true; 3405 3406 /* 3407 * > A start tag whose tag name is "form" 3408 * 3409 * This tag in the IN TABLE insertion mode is a parse error. 3410 */ 3411 case '+FORM': 3412 if ( 3413 $this->state->stack_of_open_elements->has_element_in_scope( 'TEMPLATE' ) || 3414 isset( $this->state->form_element ) 3415 ) { 3416 return $this->step(); 3417 } 3418 3419 // This FORM is special because it immediately closes and cannot have other children. 3420 $this->insert_html_element( $this->state->current_token ); 3421 $this->state->form_element = $this->state->current_token; 3422 $this->state->stack_of_open_elements->pop(); 3423 return true; 3424 } 3425 3426 /* 3427 * > Anything else 3428 * > Parse error. Enable foster parenting, process the token using the rules for the 3429 * > "in body" insertion mode, and then disable foster parenting. 3430 * 3431 * @todo Indicate a parse error once it's possible. 3432 */ 3433 anything_else: 3434 $this->bail( 'Foster parenting is not supported.' ); 3435 } 3436 3437 /** 3438 * Parses next element in the 'in table text' insertion mode. 3439 * 3440 * This internal function performs the 'in table text' insertion mode 3441 * logic for the generalized WP_HTML_Processor::step() function. 3442 * 3443 * @since 6.7.0 Stub implementation. 3444 * 3445 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 3446 * 3447 * @see https://html.spec.whatwg.org/#parsing-main-intabletext 3448 * @see WP_HTML_Processor::step 3449 * 3450 * @return bool Whether an element was found. 3451 */ 3452 private function step_in_table_text(): bool { 3453 $this->bail( 'No support for parsing in the ' . WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_TEXT . ' state.' ); 3454 } 3455 3456 /** 3457 * Parses next element in the 'in caption' insertion mode. 3458 * 3459 * This internal function performs the 'in caption' insertion mode 3460 * logic for the generalized WP_HTML_Processor::step() function. 3461 * 3462 * @since 6.7.0 3463 * 3464 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 3465 * 3466 * @see https://html.spec.whatwg.org/#parsing-main-incaption 3467 * @see WP_HTML_Processor::step 3468 * 3469 * @return bool Whether an element was found. 3470 */ 3471 private function step_in_caption(): bool { 3472 $tag_name = $this->get_tag(); 3473 $op_sigil = $this->is_tag_closer() ? '-' : '+'; 3474 $op = "{$op_sigil}{$tag_name}"; 3475 3476 switch ( $op ) { 3477 /* 3478 * > An end tag whose tag name is "caption" 3479 * > A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr" 3480 * > An end tag whose tag name is "table" 3481 * 3482 * These tag handling rules are identical except for the final instruction. 3483 * Handle them in a single block. 3484 */ 3485 case '-CAPTION': 3486 case '+CAPTION': 3487 case '+COL': 3488 case '+COLGROUP': 3489 case '+TBODY': 3490 case '+TD': 3491 case '+TFOOT': 3492 case '+TH': 3493 case '+THEAD': 3494 case '+TR': 3495 case '-TABLE': 3496 if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'CAPTION' ) ) { 3497 // Parse error: ignore the token. 3498 return $this->step(); 3499 } 3500 3501 $this->generate_implied_end_tags(); 3502 if ( ! $this->state->stack_of_open_elements->current_node_is( 'CAPTION' ) ) { 3503 // @todo Indicate a parse error once it's possible. 3504 } 3505 3506 $this->state->stack_of_open_elements->pop_until( 'CAPTION' ); 3507 $this->state->active_formatting_elements->clear_up_to_last_marker(); 3508 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE; 3509 3510 // If this is not a CAPTION end tag, the token should be reprocessed. 3511 if ( '-CAPTION' === $op ) { 3512 return true; 3513 } 3514 return $this->step( self::REPROCESS_CURRENT_NODE ); 3515 3516 /** 3517 * > An end tag whose tag name is one of: "body", "col", "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr" 3518 */ 3519 case '-BODY': 3520 case '-COL': 3521 case '-COLGROUP': 3522 case '-HTML': 3523 case '-TBODY': 3524 case '-TD': 3525 case '-TFOOT': 3526 case '-TH': 3527 case '-THEAD': 3528 case '-TR': 3529 // Parse error: ignore the token. 3530 return $this->step(); 3531 } 3532 3533 /** 3534 * > Anything else 3535 * > Process the token using the rules for the "in body" insertion mode. 3536 */ 3537 return $this->step_in_body(); 3538 } 3539 3540 /** 3541 * Parses next element in the 'in column group' insertion mode. 3542 * 3543 * This internal function performs the 'in column group' insertion mode 3544 * logic for the generalized WP_HTML_Processor::step() function. 3545 * 3546 * @since 6.7.0 3547 * 3548 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 3549 * 3550 * @see https://html.spec.whatwg.org/#parsing-main-incolgroup 3551 * @see WP_HTML_Processor::step 3552 * 3553 * @return bool Whether an element was found. 3554 */ 3555 private function step_in_column_group(): bool { 3556 $token_name = $this->get_token_name(); 3557 $token_type = $this->get_token_type(); 3558 $op_sigil = '#tag' === $token_type ? ( parent::is_tag_closer() ? '-' : '+' ) : ''; 3559 $op = "{$op_sigil}{$token_name}"; 3560 3561 switch ( $op ) { 3562 /* 3563 * > A character token that is one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), 3564 * > U+000C FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE 3565 */ 3566 case '#text': 3567 if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) { 3568 // Insert the character. 3569 $this->insert_html_element( $this->state->current_token ); 3570 return true; 3571 } 3572 3573 goto in_column_group_anything_else; 3574 break; 3575 3576 /* 3577 * > A comment token 3578 */ 3579 case '#comment': 3580 case '#funky-comment': 3581 case '#presumptuous-tag': 3582 $this->insert_html_element( $this->state->current_token ); 3583 return true; 3584 3585 /* 3586 * > A DOCTYPE token 3587 */ 3588 case 'html': 3589 // @todo Indicate a parse error once it's possible. 3590 return $this->step(); 3591 3592 /* 3593 * > A start tag whose tag name is "html" 3594 */ 3595 case '+HTML': 3596 return $this->step_in_body(); 3597 3598 /* 3599 * > A start tag whose tag name is "col" 3600 */ 3601 case '+COL': 3602 $this->insert_html_element( $this->state->current_token ); 3603 $this->state->stack_of_open_elements->pop(); 3604 return true; 3605 3606 /* 3607 * > An end tag whose tag name is "colgroup" 3608 */ 3609 case '-COLGROUP': 3610 if ( ! $this->state->stack_of_open_elements->current_node_is( 'COLGROUP' ) ) { 3611 // @todo Indicate a parse error once it's possible. 3612 return $this->step(); 3613 } 3614 $this->state->stack_of_open_elements->pop(); 3615 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE; 3616 return true; 3617 3618 /* 3619 * > An end tag whose tag name is "col" 3620 */ 3621 case '-COL': 3622 // Parse error: ignore the token. 3623 return $this->step(); 3624 3625 /* 3626 * > A start tag whose tag name is "template" 3627 * > An end tag whose tag name is "template" 3628 */ 3629 case '+TEMPLATE': 3630 case '-TEMPLATE': 3631 return $this->step_in_head(); 3632 } 3633 3634 in_column_group_anything_else: 3635 /* 3636 * > Anything else 3637 */ 3638 if ( ! $this->state->stack_of_open_elements->current_node_is( 'COLGROUP' ) ) { 3639 // @todo Indicate a parse error once it's possible. 3640 return $this->step(); 3641 } 3642 $this->state->stack_of_open_elements->pop(); 3643 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE; 3644 return $this->step( self::REPROCESS_CURRENT_NODE ); 3645 } 3646 3647 /** 3648 * Parses next element in the 'in table body' insertion mode. 3649 * 3650 * This internal function performs the 'in table body' insertion mode 3651 * logic for the generalized WP_HTML_Processor::step() function. 3652 * 3653 * @since 6.7.0 3654 * 3655 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 3656 * 3657 * @see https://html.spec.whatwg.org/#parsing-main-intbody 3658 * @see WP_HTML_Processor::step 3659 * 3660 * @return bool Whether an element was found. 3661 */ 3662 private function step_in_table_body(): bool { 3663 $tag_name = $this->get_tag(); 3664 $op_sigil = $this->is_tag_closer() ? '-' : '+'; 3665 $op = "{$op_sigil}{$tag_name}"; 3666 3667 switch ( $op ) { 3668 /* 3669 * > A start tag whose tag name is "tr" 3670 */ 3671 case '+TR': 3672 $this->state->stack_of_open_elements->clear_to_table_body_context(); 3673 $this->insert_html_element( $this->state->current_token ); 3674 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_ROW; 3675 return true; 3676 3677 /* 3678 * > A start tag whose tag name is one of: "th", "td" 3679 */ 3680 case '+TH': 3681 case '+TD': 3682 // @todo Indicate a parse error once it's possible. 3683 $this->state->stack_of_open_elements->clear_to_table_body_context(); 3684 $this->insert_virtual_node( 'TR' ); 3685 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_ROW; 3686 return $this->step( self::REPROCESS_CURRENT_NODE ); 3687 3688 /* 3689 * > An end tag whose tag name is one of: "tbody", "tfoot", "thead" 3690 */ 3691 case '-TBODY': 3692 case '-TFOOT': 3693 case '-THEAD': 3694 if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( $tag_name ) ) { 3695 // Parse error: ignore the token. 3696 return $this->step(); 3697 } 3698 3699 $this->state->stack_of_open_elements->clear_to_table_body_context(); 3700 $this->state->stack_of_open_elements->pop(); 3701 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE; 3702 return true; 3703 3704 /* 3705 * > A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "tfoot", "thead" 3706 * > An end tag whose tag name is "table" 3707 */ 3708 case '+CAPTION': 3709 case '+COL': 3710 case '+COLGROUP': 3711 case '+TBODY': 3712 case '+TFOOT': 3713 case '+THEAD': 3714 case '-TABLE': 3715 if ( 3716 ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TBODY' ) && 3717 ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'THEAD' ) && 3718 ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TFOOT' ) 3719 ) { 3720 // Parse error: ignore the token. 3721 return $this->step(); 3722 } 3723 $this->state->stack_of_open_elements->clear_to_table_body_context(); 3724 $this->state->stack_of_open_elements->pop(); 3725 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE; 3726 return $this->step( self::REPROCESS_CURRENT_NODE ); 3727 3728 /* 3729 * > An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html", "td", "th", "tr" 3730 */ 3731 case '-BODY': 3732 case '-CAPTION': 3733 case '-COL': 3734 case '-COLGROUP': 3735 case '-HTML': 3736 case '-TD': 3737 case '-TH': 3738 case '-TR': 3739 // Parse error: ignore the token. 3740 return $this->step(); 3741 } 3742 3743 /* 3744 * > Anything else 3745 * > Process the token using the rules for the "in table" insertion mode. 3746 */ 3747 return $this->step_in_table(); 3748 } 3749 3750 /** 3751 * Parses next element in the 'in row' insertion mode. 3752 * 3753 * This internal function performs the 'in row' insertion mode 3754 * logic for the generalized WP_HTML_Processor::step() function. 3755 * 3756 * @since 6.7.0 3757 * 3758 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 3759 * 3760 * @see https://html.spec.whatwg.org/#parsing-main-intr 3761 * @see WP_HTML_Processor::step 3762 * 3763 * @return bool Whether an element was found. 3764 */ 3765 private function step_in_row(): bool { 3766 $tag_name = $this->get_tag(); 3767 $op_sigil = $this->is_tag_closer() ? '-' : '+'; 3768 $op = "{$op_sigil}{$tag_name}"; 3769 3770 switch ( $op ) { 3771 /* 3772 * > A start tag whose tag name is one of: "th", "td" 3773 */ 3774 case '+TH': 3775 case '+TD': 3776 $this->state->stack_of_open_elements->clear_to_table_row_context(); 3777 $this->insert_html_element( $this->state->current_token ); 3778 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_CELL; 3779 $this->state->active_formatting_elements->insert_marker(); 3780 return true; 3781 3782 /* 3783 * > An end tag whose tag name is "tr" 3784 */ 3785 case '-TR': 3786 if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TR' ) ) { 3787 // Parse error: ignore the token. 3788 return $this->step(); 3789 } 3790 3791 $this->state->stack_of_open_elements->clear_to_table_row_context(); 3792 $this->state->stack_of_open_elements->pop(); 3793 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY; 3794 return true; 3795 3796 /* 3797 * > A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "tfoot", "thead", "tr" 3798 * > An end tag whose tag name is "table" 3799 */ 3800 case '+CAPTION': 3801 case '+COL': 3802 case '+COLGROUP': 3803 case '+TBODY': 3804 case '+TFOOT': 3805 case '+THEAD': 3806 case '+TR': 3807 case '-TABLE': 3808 if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TR' ) ) { 3809 // Parse error: ignore the token. 3810 return $this->step(); 3811 } 3812 3813 $this->state->stack_of_open_elements->clear_to_table_row_context(); 3814 $this->state->stack_of_open_elements->pop(); 3815 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY; 3816 return $this->step( self::REPROCESS_CURRENT_NODE ); 3817 3818 /* 3819 * > An end tag whose tag name is one of: "tbody", "tfoot", "thead" 3820 */ 3821 case '-TBODY': 3822 case '-TFOOT': 3823 case '-THEAD': 3824 if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( $tag_name ) ) { 3825 // Parse error: ignore the token. 3826 return $this->step(); 3827 } 3828 3829 if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TR' ) ) { 3830 // Ignore the token. 3831 return $this->step(); 3832 } 3833 3834 $this->state->stack_of_open_elements->clear_to_table_row_context(); 3835 $this->state->stack_of_open_elements->pop(); 3836 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY; 3837 return $this->step( self::REPROCESS_CURRENT_NODE ); 3838 3839 /* 3840 * > An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html", "td", "th" 3841 */ 3842 case '-BODY': 3843 case '-CAPTION': 3844 case '-COL': 3845 case '-COLGROUP': 3846 case '-HTML': 3847 case '-TD': 3848 case '-TH': 3849 // Parse error: ignore the token. 3850 return $this->step(); 3851 } 3852 3853 /* 3854 * > Anything else 3855 * > Process the token using the rules for the "in table" insertion mode. 3856 */ 3857 return $this->step_in_table(); 3858 } 3859 3860 /** 3861 * Parses next element in the 'in cell' insertion mode. 3862 * 3863 * This internal function performs the 'in cell' insertion mode 3864 * logic for the generalized WP_HTML_Processor::step() function. 3865 * 3866 * @since 6.7.0 3867 * 3868 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 3869 * 3870 * @see https://html.spec.whatwg.org/#parsing-main-intd 3871 * @see WP_HTML_Processor::step 3872 * 3873 * @return bool Whether an element was found. 3874 */ 3875 private function step_in_cell(): bool { 3876 $tag_name = $this->get_tag(); 3877 $op_sigil = $this->is_tag_closer() ? '-' : '+'; 3878 $op = "{$op_sigil}{$tag_name}"; 3879 3880 switch ( $op ) { 3881 /* 3882 * > An end tag whose tag name is one of: "td", "th" 3883 */ 3884 case '-TD': 3885 case '-TH': 3886 if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( $tag_name ) ) { 3887 // Parse error: ignore the token. 3888 return $this->step(); 3889 } 3890 3891 $this->generate_implied_end_tags(); 3892 3893 /* 3894 * @todo This needs to check if the current node is an HTML element, meaning that 3895 * when SVG and MathML support is added, this needs to differentiate between an 3896 * HTML element of the given name, such as `<center>`, and a foreign element of 3897 * the same given name. 3898 */ 3899 if ( ! $this->state->stack_of_open_elements->current_node_is( $tag_name ) ) { 3900 // @todo Indicate a parse error once it's possible. 3901 } 3902 3903 $this->state->stack_of_open_elements->pop_until( $tag_name ); 3904 $this->state->active_formatting_elements->clear_up_to_last_marker(); 3905 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_ROW; 3906 return true; 3907 3908 /* 3909 * > A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "td", 3910 * > "tfoot", "th", "thead", "tr" 3911 */ 3912 case '+CAPTION': 3913 case '+COL': 3914 case '+COLGROUP': 3915 case '+TBODY': 3916 case '+TD': 3917 case '+TFOOT': 3918 case '+TH': 3919 case '+THEAD': 3920 case '+TR': 3921 /* 3922 * > Assert: The stack of open elements has a td or th element in table scope. 3923 * 3924 * Nothing to do here, except to verify in tests that this never appears. 3925 */ 3926 3927 $this->close_cell(); 3928 return $this->step( self::REPROCESS_CURRENT_NODE ); 3929 3930 /* 3931 * > An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html" 3932 */ 3933 case '-BODY': 3934 case '-CAPTION': 3935 case '-COL': 3936 case '-COLGROUP': 3937 case '-HTML': 3938 // Parse error: ignore the token. 3939 return $this->step(); 3940 3941 /* 3942 * > An end tag whose tag name is one of: "table", "tbody", "tfoot", "thead", "tr" 3943 */ 3944 case '-TABLE': 3945 case '-TBODY': 3946 case '-TFOOT': 3947 case '-THEAD': 3948 case '-TR': 3949 if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( $tag_name ) ) { 3950 // Parse error: ignore the token. 3951 return $this->step(); 3952 } 3953 $this->close_cell(); 3954 return $this->step( self::REPROCESS_CURRENT_NODE ); 3955 } 3956 3957 /* 3958 * > Anything else 3959 * > Process the token using the rules for the "in body" insertion mode. 3960 */ 3961 return $this->step_in_body(); 3962 } 3963 3964 /** 3965 * Parses next element in the 'in select' insertion mode. 3966 * 3967 * This internal function performs the 'in select' insertion mode 3968 * logic for the generalized WP_HTML_Processor::step() function. 3969 * 3970 * @since 6.7.0 3971 * 3972 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 3973 * 3974 * @see https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inselect 3975 * @see WP_HTML_Processor::step 3976 * 3977 * @return bool Whether an element was found. 3978 */ 3979 private function step_in_select(): bool { 3980 $token_name = $this->get_token_name(); 3981 $token_type = $this->get_token_type(); 3982 $op_sigil = '#tag' === $token_type ? ( parent::is_tag_closer() ? '-' : '+' ) : ''; 3983 $op = "{$op_sigil}{$token_name}"; 3984 3985 switch ( $op ) { 3986 /* 3987 * > Any other character token 3988 */ 3989 case '#text': 3990 /* 3991 * > A character token that is U+0000 NULL 3992 * 3993 * If a text node only comprises null bytes then it should be 3994 * entirely ignored and should not return to calling code. 3995 */ 3996 if ( parent::TEXT_IS_NULL_SEQUENCE === $this->text_node_classification ) { 3997 // Parse error: ignore the token. 3998 return $this->step(); 3999 } 4000 4001 $this->insert_html_element( $this->state->current_token ); 4002 return true; 4003 4004 /* 4005 * > A comment token 4006 */ 4007 case '#comment': 4008 case '#funky-comment': 4009 case '#presumptuous-tag': 4010 $this->insert_html_element( $this->state->current_token ); 4011 return true; 4012 4013 /* 4014 * > A DOCTYPE token 4015 */ 4016 case 'html': 4017 // Parse error: ignore the token. 4018 return $this->step(); 4019 4020 /* 4021 * > A start tag whose tag name is "html" 4022 */ 4023 case '+HTML': 4024 return $this->step_in_body(); 4025 4026 /* 4027 * > A start tag whose tag name is "option" 4028 */ 4029 case '+OPTION': 4030 if ( $this->state->stack_of_open_elements->current_node_is( 'OPTION' ) ) { 4031 $this->state->stack_of_open_elements->pop(); 4032 } 4033 $this->insert_html_element( $this->state->current_token ); 4034 return true; 4035 4036 /* 4037 * > A start tag whose tag name is "optgroup" 4038 * > A start tag whose tag name is "hr" 4039 * 4040 * These rules are identical except for the treatment of the self-closing flag and 4041 * the subsequent pop of the HR void element, all of which is handled elsewhere in the processor. 4042 */ 4043 case '+OPTGROUP': 4044 case '+HR': 4045 if ( $this->state->stack_of_open_elements->current_node_is( 'OPTION' ) ) { 4046 $this->state->stack_of_open_elements->pop(); 4047 } 4048 4049 if ( $this->state->stack_of_open_elements->current_node_is( 'OPTGROUP' ) ) { 4050 $this->state->stack_of_open_elements->pop(); 4051 } 4052 4053 $this->insert_html_element( $this->state->current_token ); 4054 return true; 4055 4056 /* 4057 * > An end tag whose tag name is "optgroup" 4058 */ 4059 case '-OPTGROUP': 4060 $current_node = $this->state->stack_of_open_elements->current_node(); 4061 if ( $current_node && 'OPTION' === $current_node->node_name ) { 4062 foreach ( $this->state->stack_of_open_elements->walk_up( $current_node ) as $parent ) { 4063 break; 4064 } 4065 if ( $parent && 'OPTGROUP' === $parent->node_name ) { 4066 $this->state->stack_of_open_elements->pop(); 4067 } 4068 } 4069 4070 if ( $this->state->stack_of_open_elements->current_node_is( 'OPTGROUP' ) ) { 4071 $this->state->stack_of_open_elements->pop(); 4072 return true; 4073 } 4074 4075 // Parse error: ignore the token. 4076 return $this->step(); 4077 4078 /* 4079 * > An end tag whose tag name is "option" 4080 */ 4081 case '-OPTION': 4082 if ( $this->state->stack_of_open_elements->current_node_is( 'OPTION' ) ) { 4083 $this->state->stack_of_open_elements->pop(); 4084 return true; 4085 } 4086 4087 // Parse error: ignore the token. 4088 return $this->step(); 4089 4090 /* 4091 * > An end tag whose tag name is "select" 4092 * > A start tag whose tag name is "select" 4093 * 4094 * > It just gets treated like an end tag. 4095 */ 4096 case '-SELECT': 4097 case '+SELECT': 4098 if ( ! $this->state->stack_of_open_elements->has_element_in_select_scope( 'SELECT' ) ) { 4099 // Parse error: ignore the token. 4100 return $this->step(); 4101 } 4102 $this->state->stack_of_open_elements->pop_until( 'SELECT' ); 4103 $this->reset_insertion_mode_appropriately(); 4104 return true; 4105 4106 /* 4107 * > A start tag whose tag name is one of: "input", "keygen", "textarea" 4108 * 4109 * All three of these tags are considered a parse error when found in this insertion mode. 4110 */ 4111 case '+INPUT': 4112 case '+KEYGEN': 4113 case '+TEXTAREA': 4114 if ( ! $this->state->stack_of_open_elements->has_element_in_select_scope( 'SELECT' ) ) { 4115 // Ignore the token. 4116 return $this->step(); 4117 } 4118 $this->state->stack_of_open_elements->pop_until( 'SELECT' ); 4119 $this->reset_insertion_mode_appropriately(); 4120 return $this->step( self::REPROCESS_CURRENT_NODE ); 4121 4122 /* 4123 * > A start tag whose tag name is one of: "script", "template" 4124 * > An end tag whose tag name is "template" 4125 */ 4126 case '+SCRIPT': 4127 case '+TEMPLATE': 4128 case '-TEMPLATE': 4129 return $this->step_in_head(); 4130 } 4131 4132 /* 4133 * > Anything else 4134 * > Parse error: ignore the token. 4135 */ 4136 return $this->step(); 4137 } 4138 4139 /** 4140 * Parses next element in the 'in select in table' insertion mode. 4141 * 4142 * This internal function performs the 'in select in table' insertion mode 4143 * logic for the generalized WP_HTML_Processor::step() function. 4144 * 4145 * @since 6.7.0 4146 * 4147 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 4148 * 4149 * @see https://html.spec.whatwg.org/#parsing-main-inselectintable 4150 * @see WP_HTML_Processor::step 4151 * 4152 * @return bool Whether an element was found. 4153 */ 4154 private function step_in_select_in_table(): bool { 4155 $token_name = $this->get_token_name(); 4156 $token_type = $this->get_token_type(); 4157 $op_sigil = '#tag' === $token_type ? ( parent::is_tag_closer() ? '-' : '+' ) : ''; 4158 $op = "{$op_sigil}{$token_name}"; 4159 4160 switch ( $op ) { 4161 /* 4162 * > A start tag whose tag name is one of: "caption", "table", "tbody", "tfoot", "thead", "tr", "td", "th" 4163 */ 4164 case '+CAPTION': 4165 case '+TABLE': 4166 case '+TBODY': 4167 case '+TFOOT': 4168 case '+THEAD': 4169 case '+TR': 4170 case '+TD': 4171 case '+TH': 4172 // @todo Indicate a parse error once it's possible. 4173 $this->state->stack_of_open_elements->pop_until( 'SELECT' ); 4174 $this->reset_insertion_mode_appropriately(); 4175 return $this->step( self::REPROCESS_CURRENT_NODE ); 4176 4177 /* 4178 * > An end tag whose tag name is one of: "caption", "table", "tbody", "tfoot", "thead", "tr", "td", "th" 4179 */ 4180 case '-CAPTION': 4181 case '-TABLE': 4182 case '-TBODY': 4183 case '-TFOOT': 4184 case '-THEAD': 4185 case '-TR': 4186 case '-TD': 4187 case '-TH': 4188 // @todo Indicate a parse error once it's possible. 4189 if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( $token_name ) ) { 4190 return $this->step(); 4191 } 4192 $this->state->stack_of_open_elements->pop_until( 'SELECT' ); 4193 $this->reset_insertion_mode_appropriately(); 4194 return $this->step( self::REPROCESS_CURRENT_NODE ); 4195 } 4196 4197 /* 4198 * > Anything else 4199 */ 4200 return $this->step_in_select(); 4201 } 4202 4203 /** 4204 * Parses next element in the 'in template' insertion mode. 4205 * 4206 * This internal function performs the 'in template' insertion mode 4207 * logic for the generalized WP_HTML_Processor::step() function. 4208 * 4209 * @since 6.7.0 Stub implementation. 4210 * 4211 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 4212 * 4213 * @see https://html.spec.whatwg.org/#parsing-main-intemplate 4214 * @see WP_HTML_Processor::step 4215 * 4216 * @return bool Whether an element was found. 4217 */ 4218 private function step_in_template(): bool { 4219 $token_name = $this->get_token_name(); 4220 $token_type = $this->get_token_type(); 4221 $is_closer = $this->is_tag_closer(); 4222 $op_sigil = '#tag' === $token_type ? ( $is_closer ? '-' : '+' ) : ''; 4223 $op = "{$op_sigil}{$token_name}"; 4224 4225 switch ( $op ) { 4226 /* 4227 * > A character token 4228 * > A comment token 4229 * > A DOCTYPE token 4230 */ 4231 case '#text': 4232 case '#comment': 4233 case '#funky-comment': 4234 case '#presumptuous-tag': 4235 case 'html': 4236 return $this->step_in_body(); 4237 4238 /* 4239 * > A start tag whose tag name is one of: "base", "basefont", "bgsound", "link", 4240 * > "meta", "noframes", "script", "style", "template", "title" 4241 * > An end tag whose tag name is "template" 4242 */ 4243 case '+BASE': 4244 case '+BASEFONT': 4245 case '+BGSOUND': 4246 case '+LINK': 4247 case '+META': 4248 case '+NOFRAMES': 4249 case '+SCRIPT': 4250 case '+STYLE': 4251 case '+TEMPLATE': 4252 case '+TITLE': 4253 case '-TEMPLATE': 4254 return $this->step_in_head(); 4255 4256 /* 4257 * > A start tag whose tag name is one of: "caption", "colgroup", "tbody", "tfoot", "thead" 4258 */ 4259 case '+CAPTION': 4260 case '+COLGROUP': 4261 case '+TBODY': 4262 case '+TFOOT': 4263 case '+THEAD': 4264 array_pop( $this->state->stack_of_template_insertion_modes ); 4265 $this->state->stack_of_template_insertion_modes[] = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE; 4266 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE; 4267 return $this->step( self::REPROCESS_CURRENT_NODE ); 4268 4269 /* 4270 * > A start tag whose tag name is "col" 4271 */ 4272 case '+COL': 4273 array_pop( $this->state->stack_of_template_insertion_modes ); 4274 $this->state->stack_of_template_insertion_modes[] = WP_HTML_Processor_State::INSERTION_MODE_IN_COLUMN_GROUP; 4275 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_COLUMN_GROUP; 4276 return $this->step( self::REPROCESS_CURRENT_NODE ); 4277 4278 /* 4279 * > A start tag whose tag name is "tr" 4280 */ 4281 case '+TR': 4282 array_pop( $this->state->stack_of_template_insertion_modes ); 4283 $this->state->stack_of_template_insertion_modes[] = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY; 4284 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY; 4285 return $this->step( self::REPROCESS_CURRENT_NODE ); 4286 4287 /* 4288 * > A start tag whose tag name is one of: "td", "th" 4289 */ 4290 case '+TD': 4291 case '+TH': 4292 array_pop( $this->state->stack_of_template_insertion_modes ); 4293 $this->state->stack_of_template_insertion_modes[] = WP_HTML_Processor_State::INSERTION_MODE_IN_ROW; 4294 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_ROW; 4295 return $this->step( self::REPROCESS_CURRENT_NODE ); 4296 } 4297 4298 /* 4299 * > Any other start tag 4300 */ 4301 if ( ! $is_closer ) { 4302 array_pop( $this->state->stack_of_template_insertion_modes ); 4303 $this->state->stack_of_template_insertion_modes[] = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY; 4304 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY; 4305 return $this->step( self::REPROCESS_CURRENT_NODE ); 4306 } 4307 4308 /* 4309 * > Any other end tag 4310 */ 4311 if ( $is_closer ) { 4312 // Parse error: ignore the token. 4313 return $this->step(); 4314 } 4315 4316 /* 4317 * > An end-of-file token 4318 */ 4319 if ( ! $this->state->stack_of_open_elements->contains( 'TEMPLATE' ) ) { 4320 // Stop parsing. 4321 return false; 4322 } 4323 4324 // @todo Indicate a parse error once it's possible. 4325 $this->state->stack_of_open_elements->pop_until( 'TEMPLATE' ); 4326 $this->state->active_formatting_elements->clear_up_to_last_marker(); 4327 array_pop( $this->state->stack_of_template_insertion_modes ); 4328 $this->reset_insertion_mode_appropriately(); 4329 return $this->step( self::REPROCESS_CURRENT_NODE ); 4330 } 4331 4332 /** 4333 * Parses next element in the 'after body' insertion mode. 4334 * 4335 * This internal function performs the 'after body' insertion mode 4336 * logic for the generalized WP_HTML_Processor::step() function. 4337 * 4338 * @since 6.7.0 Stub implementation. 4339 * 4340 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 4341 * 4342 * @see https://html.spec.whatwg.org/#parsing-main-afterbody 4343 * @see WP_HTML_Processor::step 4344 * 4345 * @return bool Whether an element was found. 4346 */ 4347 private function step_after_body(): bool { 4348 $tag_name = $this->get_token_name(); 4349 $token_type = $this->get_token_type(); 4350 $op_sigil = '#tag' === $token_type ? ( $this->is_tag_closer() ? '-' : '+' ) : ''; 4351 $op = "{$op_sigil}{$tag_name}"; 4352 4353 switch ( $op ) { 4354 /* 4355 * > A character token that is one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), 4356 * > U+000C FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE 4357 * 4358 * > Process the token using the rules for the "in body" insertion mode. 4359 */ 4360 case '#text': 4361 if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) { 4362 return $this->step_in_body(); 4363 } 4364 goto after_body_anything_else; 4365 break; 4366 4367 /* 4368 * > A comment token 4369 */ 4370 case '#comment': 4371 case '#funky-comment': 4372 case '#presumptuous-tag': 4373 $this->bail( 'Content outside of BODY is unsupported.' ); 4374 break; 4375 4376 /* 4377 * > A DOCTYPE token 4378 */ 4379 case 'html': 4380 // Parse error: ignore the token. 4381 return $this->step(); 4382 4383 /* 4384 * > A start tag whose tag name is "html" 4385 */ 4386 case '+HTML': 4387 return $this->step_in_body(); 4388 4389 /* 4390 * > An end tag whose tag name is "html" 4391 * 4392 * > If the parser was created as part of the HTML fragment parsing algorithm, 4393 * > this is a parse error; ignore the token. (fragment case) 4394 * > 4395 * > Otherwise, switch the insertion mode to "after after body". 4396 */ 4397 case '-HTML': 4398 if ( isset( $this->context_node ) ) { 4399 return $this->step(); 4400 } 4401 4402 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_AFTER_AFTER_BODY; 4403 /* 4404 * The HTML element is not removed from the stack of open elements. 4405 * Only internal state has changed, this does not qualify as a "step" 4406 * in terms of advancing through the document to another token. 4407 * Nothing has been pushed or popped. 4408 * Proceed to parse the next item. 4409 */ 4410 return $this->step(); 4411 } 4412 4413 /* 4414 * > Parse error. Switch the insertion mode to "in body" and reprocess the token. 4415 */ 4416 after_body_anything_else: 4417 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY; 4418 return $this->step( self::REPROCESS_CURRENT_NODE ); 4419 } 4420 4421 /** 4422 * Parses next element in the 'in frameset' insertion mode. 4423 * 4424 * This internal function performs the 'in frameset' insertion mode 4425 * logic for the generalized WP_HTML_Processor::step() function. 4426 * 4427 * @since 6.7.0 Stub implementation. 4428 * 4429 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 4430 * 4431 * @see https://html.spec.whatwg.org/#parsing-main-inframeset 4432 * @see WP_HTML_Processor::step 4433 * 4434 * @return bool Whether an element was found. 4435 */ 4436 private function step_in_frameset(): bool { 4437 $tag_name = $this->get_token_name(); 4438 $token_type = $this->get_token_type(); 4439 $op_sigil = '#tag' === $token_type ? ( $this->is_tag_closer() ? '-' : '+' ) : ''; 4440 $op = "{$op_sigil}{$tag_name}"; 4441 4442 switch ( $op ) { 4443 /* 4444 * > A character token that is one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), 4445 * > U+000C FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE 4446 * > 4447 * > Insert the character. 4448 * 4449 * This algorithm effectively strips non-whitespace characters from text and inserts 4450 * them under HTML. This is not supported at this time. 4451 */ 4452 case '#text': 4453 if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) { 4454 return $this->step_in_body(); 4455 } 4456 $this->bail( 'Non-whitespace characters cannot be handled in frameset.' ); 4457 break; 4458 4459 /* 4460 * > A comment token 4461 */ 4462 case '#comment': 4463 case '#funky-comment': 4464 case '#presumptuous-tag': 4465 $this->insert_html_element( $this->state->current_token ); 4466 return true; 4467 4468 /* 4469 * > A DOCTYPE token 4470 */ 4471 case 'html': 4472 // Parse error: ignore the token. 4473 return $this->step(); 4474 4475 /* 4476 * > A start tag whose tag name is "html" 4477 */ 4478 case '+HTML': 4479 return $this->step_in_body(); 4480 4481 /* 4482 * > A start tag whose tag name is "frameset" 4483 */ 4484 case '+FRAMESET': 4485 $this->insert_html_element( $this->state->current_token ); 4486 return true; 4487 4488 /* 4489 * > An end tag whose tag name is "frameset" 4490 */ 4491 case '-FRAMESET': 4492 /* 4493 * > If the current node is the root html element, then this is a parse error; 4494 * > ignore the token. (fragment case) 4495 */ 4496 if ( $this->state->stack_of_open_elements->current_node_is( 'HTML' ) ) { 4497 return $this->step(); 4498 } 4499 4500 /* 4501 * > Otherwise, pop the current node from the stack of open elements. 4502 */ 4503 $this->state->stack_of_open_elements->pop(); 4504 4505 /* 4506 * > If the parser was not created as part of the HTML fragment parsing algorithm 4507 * > (fragment case), and the current node is no longer a frameset element, then 4508 * > switch the insertion mode to "after frameset". 4509 */ 4510 if ( ! isset( $this->context_node ) && ! $this->state->stack_of_open_elements->current_node_is( 'FRAMESET' ) ) { 4511 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_AFTER_FRAMESET; 4512 } 4513 4514 return true; 4515 4516 /* 4517 * > A start tag whose tag name is "frame" 4518 * 4519 * > Insert an HTML element for the token. Immediately pop the 4520 * > current node off the stack of open elements. 4521 * > 4522 * > Acknowledge the token's self-closing flag, if it is set. 4523 */ 4524 case '+FRAME': 4525 $this->insert_html_element( $this->state->current_token ); 4526 $this->state->stack_of_open_elements->pop(); 4527 return true; 4528 4529 /* 4530 * > A start tag whose tag name is "noframes" 4531 */ 4532 case '+NOFRAMES': 4533 return $this->step_in_head(); 4534 } 4535 4536 // Parse error: ignore the token. 4537 return $this->step(); 4538 } 4539 4540 /** 4541 * Parses next element in the 'after frameset' insertion mode. 4542 * 4543 * This internal function performs the 'after frameset' insertion mode 4544 * logic for the generalized WP_HTML_Processor::step() function. 4545 * 4546 * @since 6.7.0 Stub implementation. 4547 * 4548 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 4549 * 4550 * @see https://html.spec.whatwg.org/#parsing-main-afterframeset 4551 * @see WP_HTML_Processor::step 4552 * 4553 * @return bool Whether an element was found. 4554 */ 4555 private function step_after_frameset(): bool { 4556 $tag_name = $this->get_token_name(); 4557 $token_type = $this->get_token_type(); 4558 $op_sigil = '#tag' === $token_type ? ( $this->is_tag_closer() ? '-' : '+' ) : ''; 4559 $op = "{$op_sigil}{$tag_name}"; 4560 4561 switch ( $op ) { 4562 /* 4563 * > A character token that is one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), 4564 * > U+000C FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE 4565 * > 4566 * > Insert the character. 4567 * 4568 * This algorithm effectively strips non-whitespace characters from text and inserts 4569 * them under HTML. This is not supported at this time. 4570 */ 4571 case '#text': 4572 if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) { 4573 return $this->step_in_body(); 4574 } 4575 $this->bail( 'Non-whitespace characters cannot be handled in after frameset' ); 4576 break; 4577 4578 /* 4579 * > A comment token 4580 */ 4581 case '#comment': 4582 case '#funky-comment': 4583 case '#presumptuous-tag': 4584 $this->insert_html_element( $this->state->current_token ); 4585 return true; 4586 4587 /* 4588 * > A DOCTYPE token 4589 */ 4590 case 'html': 4591 // Parse error: ignore the token. 4592 return $this->step(); 4593 4594 /* 4595 * > A start tag whose tag name is "html" 4596 */ 4597 case '+HTML': 4598 return $this->step_in_body(); 4599 4600 /* 4601 * > An end tag whose tag name is "html" 4602 */ 4603 case '-HTML': 4604 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_AFTER_AFTER_FRAMESET; 4605 /* 4606 * The HTML element is not removed from the stack of open elements. 4607 * Only internal state has changed, this does not qualify as a "step" 4608 * in terms of advancing through the document to another token. 4609 * Nothing has been pushed or popped. 4610 * Proceed to parse the next item. 4611 */ 4612 return $this->step(); 4613 4614 /* 4615 * > A start tag whose tag name is "noframes" 4616 */ 4617 case '+NOFRAMES': 4618 return $this->step_in_head(); 4619 } 4620 4621 // Parse error: ignore the token. 4622 return $this->step(); 4623 } 4624 4625 /** 4626 * Parses next element in the 'after after body' insertion mode. 4627 * 4628 * This internal function performs the 'after after body' insertion mode 4629 * logic for the generalized WP_HTML_Processor::step() function. 4630 * 4631 * @since 6.7.0 Stub implementation. 4632 * 4633 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 4634 * 4635 * @see https://html.spec.whatwg.org/#the-after-after-body-insertion-mode 4636 * @see WP_HTML_Processor::step 4637 * 4638 * @return bool Whether an element was found. 4639 */ 4640 private function step_after_after_body(): bool { 4641 $tag_name = $this->get_token_name(); 4642 $token_type = $this->get_token_type(); 4643 $op_sigil = '#tag' === $token_type ? ( $this->is_tag_closer() ? '-' : '+' ) : ''; 4644 $op = "{$op_sigil}{$tag_name}"; 4645 4646 switch ( $op ) { 4647 /* 4648 * > A comment token 4649 */ 4650 case '#comment': 4651 case '#funky-comment': 4652 case '#presumptuous-tag': 4653 $this->bail( 'Content outside of HTML is unsupported.' ); 4654 break; 4655 4656 /* 4657 * > A DOCTYPE token 4658 * > A start tag whose tag name is "html" 4659 * 4660 * > Process the token using the rules for the "in body" insertion mode. 4661 */ 4662 case 'html': 4663 case '+HTML': 4664 return $this->step_in_body(); 4665 4666 /* 4667 * > A character token that is one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), 4668 * > U+000C FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE 4669 * > 4670 * > Process the token using the rules for the "in body" insertion mode. 4671 */ 4672 case '#text': 4673 if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) { 4674 return $this->step_in_body(); 4675 } 4676 goto after_after_body_anything_else; 4677 break; 4678 } 4679 4680 /* 4681 * > Parse error. Switch the insertion mode to "in body" and reprocess the token. 4682 */ 4683 after_after_body_anything_else: 4684 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY; 4685 return $this->step( self::REPROCESS_CURRENT_NODE ); 4686 } 4687 4688 /** 4689 * Parses next element in the 'after after frameset' insertion mode. 4690 * 4691 * This internal function performs the 'after after frameset' insertion mode 4692 * logic for the generalized WP_HTML_Processor::step() function. 4693 * 4694 * @since 6.7.0 Stub implementation. 4695 * 4696 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 4697 * 4698 * @see https://html.spec.whatwg.org/#the-after-after-frameset-insertion-mode 4699 * @see WP_HTML_Processor::step 4700 * 4701 * @return bool Whether an element was found. 4702 */ 4703 private function step_after_after_frameset(): bool { 4704 $tag_name = $this->get_token_name(); 4705 $token_type = $this->get_token_type(); 4706 $op_sigil = '#tag' === $token_type ? ( $this->is_tag_closer() ? '-' : '+' ) : ''; 4707 $op = "{$op_sigil}{$tag_name}"; 4708 4709 switch ( $op ) { 4710 /* 4711 * > A comment token 4712 */ 4713 case '#comment': 4714 case '#funky-comment': 4715 case '#presumptuous-tag': 4716 $this->bail( 'Content outside of HTML is unsupported.' ); 4717 break; 4718 4719 /* 4720 * > A DOCTYPE token 4721 * > A start tag whose tag name is "html" 4722 * 4723 * > Process the token using the rules for the "in body" insertion mode. 4724 */ 4725 case 'html': 4726 case '+HTML': 4727 return $this->step_in_body(); 4728 4729 /* 4730 * > A character token that is one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), 4731 * > U+000C FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE 4732 * > 4733 * > Process the token using the rules for the "in body" insertion mode. 4734 * 4735 * This algorithm effectively strips non-whitespace characters from text and inserts 4736 * them under HTML. This is not supported at this time. 4737 */ 4738 case '#text': 4739 if ( parent::TEXT_IS_WHITESPACE === $this->text_node_classification ) { 4740 return $this->step_in_body(); 4741 } 4742 $this->bail( 'Non-whitespace characters cannot be handled in after after frameset.' ); 4743 break; 4744 4745 /* 4746 * > A start tag whose tag name is "noframes" 4747 */ 4748 case '+NOFRAMES': 4749 return $this->step_in_head(); 4750 } 4751 4752 // Parse error: ignore the token. 4753 return $this->step(); 4754 } 4755 4756 /** 4757 * Parses next element in the 'in foreign content' insertion mode. 4758 * 4759 * This internal function performs the 'in foreign content' insertion mode 4760 * logic for the generalized WP_HTML_Processor::step() function. 4761 * 4762 * @since 6.7.0 Stub implementation. 4763 * 4764 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 4765 * 4766 * @see https://html.spec.whatwg.org/#parsing-main-inforeign 4767 * @see WP_HTML_Processor::step 4768 * 4769 * @return bool Whether an element was found. 4770 */ 4771 private function step_in_foreign_content(): bool { 4772 $tag_name = $this->get_token_name(); 4773 $token_type = $this->get_token_type(); 4774 $op_sigil = '#tag' === $token_type ? ( $this->is_tag_closer() ? '-' : '+' ) : ''; 4775 $op = "{$op_sigil}{$tag_name}"; 4776 4777 /* 4778 * > A start tag whose name is "font", if the token has any attributes named "color", "face", or "size" 4779 * 4780 * This section drawn out above the switch to more easily incorporate 4781 * the additional rules based on the presence of the attributes. 4782 */ 4783 if ( 4784 '+FONT' === $op && 4785 ( 4786 null !== $this->get_attribute( 'color' ) || 4787 null !== $this->get_attribute( 'face' ) || 4788 null !== $this->get_attribute( 'size' ) 4789 ) 4790 ) { 4791 $op = '+FONT with attributes'; 4792 } 4793 4794 switch ( $op ) { 4795 case '#text': 4796 /* 4797 * > A character token that is U+0000 NULL 4798 * 4799 * This is handled by `get_modifiable_text()`. 4800 */ 4801 4802 /* 4803 * Whitespace-only text does not affect the frameset-ok flag. 4804 * It is probably inter-element whitespace, but it may also 4805 * contain character references which decode only to whitespace. 4806 */ 4807 if ( parent::TEXT_IS_GENERIC === $this->text_node_classification ) { 4808 $this->state->frameset_ok = false; 4809 } 4810 4811 $this->insert_foreign_element( $this->state->current_token, false ); 4812 return true; 4813 4814 /* 4815 * CDATA sections are alternate wrappers for text content and therefore 4816 * ought to follow the same rules as text nodes. 4817 */ 4818 case '#cdata-section': 4819 /* 4820 * NULL bytes and whitespace do not change the frameset-ok flag. 4821 */ 4822 $current_token = $this->bookmarks[ $this->state->current_token->bookmark_name ]; 4823 $cdata_content_start = $current_token->start + 9; 4824 $cdata_content_length = $current_token->length - 12; 4825 if ( strspn( $this->html, "\0 \t\n\f\r", $cdata_content_start, $cdata_content_length ) !== $cdata_content_length ) { 4826 $this->state->frameset_ok = false; 4827 } 4828 4829 $this->insert_foreign_element( $this->state->current_token, false ); 4830 return true; 4831 4832 /* 4833 * > A comment token 4834 */ 4835 case '#comment': 4836 case '#funky-comment': 4837 case '#presumptuous-tag': 4838 $this->insert_foreign_element( $this->state->current_token, false ); 4839 return true; 4840 4841 /* 4842 * > A DOCTYPE token 4843 */ 4844 case 'html': 4845 // Parse error: ignore the token. 4846 return $this->step(); 4847 4848 /* 4849 * > A start tag whose tag name is "b", "big", "blockquote", "body", "br", "center", 4850 * > "code", "dd", "div", "dl", "dt", "em", "embed", "h1", "h2", "h3", "h4", "h5", 4851 * > "h6", "head", "hr", "i", "img", "li", "listing", "menu", "meta", "nobr", "ol", 4852 * > "p", "pre", "ruby", "s", "small", "span", "strong", "strike", "sub", "sup", 4853 * > "table", "tt", "u", "ul", "var" 4854 * 4855 * > A start tag whose name is "font", if the token has any attributes named "color", "face", or "size" 4856 * 4857 * > An end tag whose tag name is "br", "p" 4858 * 4859 * Closing BR tags are always reported by the Tag Processor as opening tags. 4860 */ 4861 case '+B': 4862 case '+BIG': 4863 case '+BLOCKQUOTE': 4864 case '+BODY': 4865 case '+BR': 4866 case '+CENTER': 4867 case '+CODE': 4868 case '+DD': 4869 case '+DIV': 4870 case '+DL': 4871 case '+DT': 4872 case '+EM': 4873 case '+EMBED': 4874 case '+H1': 4875 case '+H2': 4876 case '+H3': 4877 case '+H4': 4878 case '+H5': 4879 case '+H6': 4880 case '+HEAD': 4881 case '+HR': 4882 case '+I': 4883 case '+IMG': 4884 case '+LI': 4885 case '+LISTING': 4886 case '+MENU': 4887 case '+META': 4888 case '+NOBR': 4889 case '+OL': 4890 case '+P': 4891 case '+PRE': 4892 case '+RUBY': 4893 case '+S': 4894 case '+SMALL': 4895 case '+SPAN': 4896 case '+STRONG': 4897 case '+STRIKE': 4898 case '+SUB': 4899 case '+SUP': 4900 case '+TABLE': 4901 case '+TT': 4902 case '+U': 4903 case '+UL': 4904 case '+VAR': 4905 case '+FONT with attributes': 4906 case '-BR': 4907 case '-P': 4908 // @todo Indicate a parse error once it's possible. 4909 foreach ( $this->state->stack_of_open_elements->walk_up() as $current_node ) { 4910 if ( 4911 'math' === $current_node->integration_node_type || 4912 'html' === $current_node->integration_node_type || 4913 'html' === $current_node->namespace 4914 ) { 4915 break; 4916 } 4917 4918 $this->state->stack_of_open_elements->pop(); 4919 } 4920 goto in_foreign_content_process_in_current_insertion_mode; 4921 } 4922 4923 /* 4924 * > Any other start tag 4925 */ 4926 if ( ! $this->is_tag_closer() ) { 4927 $this->insert_foreign_element( $this->state->current_token, false ); 4928 4929 /* 4930 * > If the token has its self-closing flag set, then run 4931 * > the appropriate steps from the following list: 4932 * > 4933 * > ↪ the token's tag name is "script", and the new current node is in the SVG namespace 4934 * > Acknowledge the token's self-closing flag, and then act as 4935 * > described in the steps for a "script" end tag below. 4936 * > 4937 * > ↪ Otherwise 4938 * > Pop the current node off the stack of open elements and 4939 * > acknowledge the token's self-closing flag. 4940 * 4941 * Since the rules for SCRIPT below indicate to pop the element off of the stack of 4942 * open elements, which is the same for the Otherwise condition, there's no need to 4943 * separate these checks. The difference comes when a parser operates with the scripting 4944 * flag enabled, and executes the script, which this parser does not support. 4945 */ 4946 if ( $this->state->current_token->has_self_closing_flag ) { 4947 $this->state->stack_of_open_elements->pop(); 4948 } 4949 return true; 4950 } 4951 4952 /* 4953 * > An end tag whose name is "script", if the current node is an SVG script element. 4954 */ 4955 if ( $this->is_tag_closer() && 'SCRIPT' === $this->state->current_token->node_name && 'svg' === $this->state->current_token->namespace ) { 4956 $this->state->stack_of_open_elements->pop(); 4957 return true; 4958 } 4959 4960 /* 4961 * > Any other end tag 4962 */ 4963 if ( $this->is_tag_closer() ) { 4964 $node = $this->state->stack_of_open_elements->current_node(); 4965 if ( $tag_name !== $node->node_name ) { 4966 // @todo Indicate a parse error once it's possible. 4967 } 4968 in_foreign_content_end_tag_loop: 4969 if ( $node === $this->state->stack_of_open_elements->at( 1 ) ) { 4970 return true; 4971 } 4972 4973 /* 4974 * > If node's tag name, converted to ASCII lowercase, is the same as the tag name 4975 * > of the token, pop elements from the stack of open elements until node has 4976 * > been popped from the stack, and then return. 4977 */ 4978 if ( 0 === strcasecmp( $node->node_name, $tag_name ) ) { 4979 foreach ( $this->state->stack_of_open_elements->walk_up() as $item ) { 4980 $this->state->stack_of_open_elements->pop(); 4981 if ( $node === $item ) { 4982 return true; 4983 } 4984 } 4985 } 4986 4987 foreach ( $this->state->stack_of_open_elements->walk_up( $node ) as $item ) { 4988 $node = $item; 4989 break; 4990 } 4991 4992 if ( 'html' !== $node->namespace ) { 4993 goto in_foreign_content_end_tag_loop; 4994 } 4995 4996 in_foreign_content_process_in_current_insertion_mode: 4997 switch ( $this->state->insertion_mode ) { 4998 case WP_HTML_Processor_State::INSERTION_MODE_INITIAL: 4999 return $this->step_initial(); 5000 5001 case WP_HTML_Processor_State::INSERTION_MODE_BEFORE_HTML: 5002 return $this->step_before_html(); 5003 5004 case WP_HTML_Processor_State::INSERTION_MODE_BEFORE_HEAD: 5005 return $this->step_before_head(); 5006 5007 case WP_HTML_Processor_State::INSERTION_MODE_IN_HEAD: 5008 return $this->step_in_head(); 5009 5010 case WP_HTML_Processor_State::INSERTION_MODE_IN_HEAD_NOSCRIPT: 5011 return $this->step_in_head_noscript(); 5012 5013 case WP_HTML_Processor_State::INSERTION_MODE_AFTER_HEAD: 5014 return $this->step_after_head(); 5015 5016 case WP_HTML_Processor_State::INSERTION_MODE_IN_BODY: 5017 return $this->step_in_body(); 5018 5019 case WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE: 5020 return $this->step_in_table(); 5021 5022 case WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_TEXT: 5023 return $this->step_in_table_text(); 5024 5025 case WP_HTML_Processor_State::INSERTION_MODE_IN_CAPTION: 5026 return $this->step_in_caption(); 5027 5028 case WP_HTML_Processor_State::INSERTION_MODE_IN_COLUMN_GROUP: 5029 return $this->step_in_column_group(); 5030 5031 case WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY: 5032 return $this->step_in_table_body(); 5033 5034 case WP_HTML_Processor_State::INSERTION_MODE_IN_ROW: 5035 return $this->step_in_row(); 5036 5037 case WP_HTML_Processor_State::INSERTION_MODE_IN_CELL: 5038 return $this->step_in_cell(); 5039 5040 case WP_HTML_Processor_State::INSERTION_MODE_IN_SELECT: 5041 return $this->step_in_select(); 5042 5043 case WP_HTML_Processor_State::INSERTION_MODE_IN_SELECT_IN_TABLE: 5044 return $this->step_in_select_in_table(); 5045 5046 case WP_HTML_Processor_State::INSERTION_MODE_IN_TEMPLATE: 5047 return $this->step_in_template(); 5048 5049 case WP_HTML_Processor_State::INSERTION_MODE_AFTER_BODY: 5050 return $this->step_after_body(); 5051 5052 case WP_HTML_Processor_State::INSERTION_MODE_IN_FRAMESET: 5053 return $this->step_in_frameset(); 5054 5055 case WP_HTML_Processor_State::INSERTION_MODE_AFTER_FRAMESET: 5056 return $this->step_after_frameset(); 5057 5058 case WP_HTML_Processor_State::INSERTION_MODE_AFTER_AFTER_BODY: 5059 return $this->step_after_after_body(); 5060 5061 case WP_HTML_Processor_State::INSERTION_MODE_AFTER_AFTER_FRAMESET: 5062 return $this->step_after_after_frameset(); 5063 5064 // This should be unreachable but PHP doesn't have total type checking on switch. 5065 default: 5066 $this->bail( "Unaware of the requested parsing mode: '{$this->state->insertion_mode}'." ); 5067 } 5068 } 5069 5070 $this->bail( 'Should not have been able to reach end of IN FOREIGN CONTENT processing. Check HTML API code.' ); 5071 // This unnecessary return prevents tools from inaccurately reporting type errors. 5072 return false; 5073 } 5074 5075 /* 5076 * Internal helpers 5077 */ 5078 5079 /** 5080 * Creates a new bookmark for the currently-matched token and returns the generated name. 5081 * 5082 * @since 6.4.0 5083 * @since 6.5.0 Renamed from bookmark_tag() to bookmark_token(). 5084 * 5085 * @throws Exception When unable to allocate requested bookmark. 5086 * 5087 * @return string|false Name of created bookmark, or false if unable to create. 5088 */ 5089 private function bookmark_token() { 5090 if ( ! parent::set_bookmark( ++$this->bookmark_counter ) ) { 5091 $this->last_error = self::ERROR_EXCEEDED_MAX_BOOKMARKS; 5092 throw new Exception( 'could not allocate bookmark' ); 5093 } 5094 5095 return "{$this->bookmark_counter}"; 5096 } 5097 5098 /* 5099 * HTML semantic overrides for Tag Processor 5100 */ 5101 5102 /** 5103 * Indicates the namespace of the current token, or "html" if there is none. 5104 * 5105 * @return string One of "html", "math", or "svg". 5106 */ 5107 public function get_namespace(): string { 5108 if ( ! isset( $this->current_element ) ) { 5109 return parent::get_namespace(); 5110 } 5111 5112 return $this->current_element->token->namespace; 5113 } 5114 5115 /** 5116 * Returns the uppercase name of the matched tag. 5117 * 5118 * The semantic rules for HTML specify that certain tags be reprocessed 5119 * with a different tag name. Because of this, the tag name presented 5120 * by the HTML Processor may differ from the one reported by the HTML 5121 * Tag Processor, which doesn't apply these semantic rules. 5122 * 5123 * Example: 5124 * 5125 * $processor = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' ); 5126 * $processor->next_tag() === true; 5127 * $processor->get_tag() === 'DIV'; 5128 * 5129 * $processor->next_tag() === false; 5130 * $processor->get_tag() === null; 5131 * 5132 * @since 6.4.0 5133 * 5134 * @return string|null Name of currently matched tag in input HTML, or `null` if none found. 5135 */ 5136 public function get_tag(): ?string { 5137 if ( null !== $this->last_error ) { 5138 return null; 5139 } 5140 5141 if ( $this->is_virtual() ) { 5142 return $this->current_element->token->node_name; 5143 } 5144 5145 $tag_name = parent::get_tag(); 5146 5147 /* 5148 * > A start tag whose tag name is "image" 5149 * > Change the token's tag name to "img" and reprocess it. (Don't ask.) 5150 */ 5151 return ( 'IMAGE' === $tag_name && 'html' === $this->get_namespace() ) 5152 ? 'IMG' 5153 : $tag_name; 5154 } 5155 5156 /** 5157 * Indicates if the currently matched tag contains the self-closing flag. 5158 * 5159 * No HTML elements ought to have the self-closing flag and for those, the self-closing 5160 * flag will be ignored. For void elements this is benign because they "self close" 5161 * automatically. For non-void HTML elements though problems will appear if someone 5162 * intends to use a self-closing element in place of that element with an empty body. 5163 * For HTML foreign elements and custom elements the self-closing flag determines if 5164 * they self-close or not. 5165 * 5166 * This function does not determine if a tag is self-closing, 5167 * but only if the self-closing flag is present in the syntax. 5168 * 5169 * @since 6.6.0 Subclassed for the HTML Processor. 5170 * 5171 * @return bool Whether the currently matched tag contains the self-closing flag. 5172 */ 5173 public function has_self_closing_flag(): bool { 5174 return $this->is_virtual() ? false : parent::has_self_closing_flag(); 5175 } 5176 5177 /** 5178 * Returns the node name represented by the token. 5179 * 5180 * This matches the DOM API value `nodeName`. Some values 5181 * are static, such as `#text` for a text node, while others 5182 * are dynamically generated from the token itself. 5183 * 5184 * Dynamic names: 5185 * - Uppercase tag name for tag matches. 5186 * - `html` for DOCTYPE declarations. 5187 * 5188 * Note that if the Tag Processor is not matched on a token 5189 * then this function will return `null`, either because it 5190 * hasn't yet found a token or because it reached the end 5191 * of the document without matching a token. 5192 * 5193 * @since 6.6.0 Subclassed for the HTML Processor. 5194 * 5195 * @return string|null Name of the matched token. 5196 */ 5197 public function get_token_name(): ?string { 5198 return $this->is_virtual() 5199 ? $this->current_element->token->node_name 5200 : parent::get_token_name(); 5201 } 5202 5203 /** 5204 * Indicates the kind of matched token, if any. 5205 * 5206 * This differs from `get_token_name()` in that it always 5207 * returns a static string indicating the type, whereas 5208 * `get_token_name()` may return values derived from the 5209 * token itself, such as a tag name or processing 5210 * instruction tag. 5211 * 5212 * Possible values: 5213 * - `#tag` when matched on a tag. 5214 * - `#text` when matched on a text node. 5215 * - `#cdata-section` when matched on a CDATA node. 5216 * - `#comment` when matched on a comment. 5217 * - `#doctype` when matched on a DOCTYPE declaration. 5218 * - `#presumptuous-tag` when matched on an empty tag closer. 5219 * - `#funky-comment` when matched on a funky comment. 5220 * 5221 * @since 6.6.0 Subclassed for the HTML Processor. 5222 * 5223 * @return string|null What kind of token is matched, or null. 5224 */ 5225 public function get_token_type(): ?string { 5226 if ( $this->is_virtual() ) { 5227 /* 5228 * This logic comes from the Tag Processor. 5229 * 5230 * @todo It would be ideal not to repeat this here, but it's not clearly 5231 * better to allow passing a token name to `get_token_type()`. 5232 */ 5233 $node_name = $this->current_element->token->node_name; 5234 $starting_char = $node_name[0]; 5235 if ( 'A' <= $starting_char && 'Z' >= $starting_char ) { 5236 return '#tag'; 5237 } 5238 5239 if ( 'html' === $node_name ) { 5240 return '#doctype'; 5241 } 5242 5243 return $node_name; 5244 } 5245 5246 return parent::get_token_type(); 5247 } 5248 5249 /** 5250 * Returns the value of a requested attribute from a matched tag opener if that attribute exists. 5251 * 5252 * Example: 5253 * 5254 * $p = WP_HTML_Processor::create_fragment( '<div enabled class="test" data-test-id="14">Test</div>' ); 5255 * $p->next_token() === true; 5256 * $p->get_attribute( 'data-test-id' ) === '14'; 5257 * $p->get_attribute( 'enabled' ) === true; 5258 * $p->get_attribute( 'aria-label' ) === null; 5259 * 5260 * $p->next_tag() === false; 5261 * $p->get_attribute( 'class' ) === null; 5262 * 5263 * @since 6.6.0 Subclassed for HTML Processor. 5264 * 5265 * @param string $name Name of attribute whose value is requested. 5266 * @return string|true|null Value of attribute or `null` if not available. Boolean attributes return `true`. 5267 */ 5268 public function get_attribute( $name ) { 5269 return $this->is_virtual() ? null : parent::get_attribute( $name ); 5270 } 5271 5272 /** 5273 * Updates or creates a new attribute on the currently matched tag with the passed value. 5274 * 5275 * For boolean attributes special handling is provided: 5276 * - When `true` is passed as the value, then only the attribute name is added to the tag. 5277 * - When `false` is passed, the attribute gets removed if it existed before. 5278 * 5279 * For string attributes, the value is escaped using the `esc_attr` function. 5280 * 5281 * @since 6.6.0 Subclassed for the HTML Processor. 5282 * 5283 * @param string $name The attribute name to target. 5284 * @param string|bool $value The new attribute value. 5285 * @return bool Whether an attribute value was set. 5286 */ 5287 public function set_attribute( $name, $value ): bool { 5288 return $this->is_virtual() ? false : parent::set_attribute( $name, $value ); 5289 } 5290 5291 /** 5292 * Remove an attribute from the currently-matched tag. 5293 * 5294 * @since 6.6.0 Subclassed for HTML Processor. 5295 * 5296 * @param string $name The attribute name to remove. 5297 * @return bool Whether an attribute was removed. 5298 */ 5299 public function remove_attribute( $name ): bool { 5300 return $this->is_virtual() ? false : parent::remove_attribute( $name ); 5301 } 5302 5303 /** 5304 * Gets lowercase names of all attributes matching a given prefix in the current tag. 5305 * 5306 * Note that matching is case-insensitive. This is in accordance with the spec: 5307 * 5308 * > There must never be two or more attributes on 5309 * > the same start tag whose names are an ASCII 5310 * > case-insensitive match for each other. 5311 * - HTML 5 spec 5312 * 5313 * Example: 5314 * 5315 * $p = new WP_HTML_Tag_Processor( '<div data-ENABLED class="test" DATA-test-id="14">Test</div>' ); 5316 * $p->next_tag( array( 'class_name' => 'test' ) ) === true; 5317 * $p->get_attribute_names_with_prefix( 'data-' ) === array( 'data-enabled', 'data-test-id' ); 5318 * 5319 * $p->next_tag() === false; 5320 * $p->get_attribute_names_with_prefix( 'data-' ) === null; 5321 * 5322 * @since 6.6.0 Subclassed for the HTML Processor. 5323 * 5324 * @see https://html.spec.whatwg.org/multipage/syntax.html#attributes-2:ascii-case-insensitive 5325 * 5326 * @param string $prefix Prefix of requested attribute names. 5327 * @return array|null List of attribute names, or `null` when no tag opener is matched. 5328 */ 5329 public function get_attribute_names_with_prefix( $prefix ): ?array { 5330 return $this->is_virtual() ? null : parent::get_attribute_names_with_prefix( $prefix ); 5331 } 5332 5333 /** 5334 * Adds a new class name to the currently matched tag. 5335 * 5336 * @since 6.6.0 Subclassed for the HTML Processor. 5337 * 5338 * @param string $class_name The class name to add. 5339 * @return bool Whether the class was set to be added. 5340 */ 5341 public function add_class( $class_name ): bool { 5342 return $this->is_virtual() ? false : parent::add_class( $class_name ); 5343 } 5344 5345 /** 5346 * Removes a class name from the currently matched tag. 5347 * 5348 * @since 6.6.0 Subclassed for the HTML Processor. 5349 * 5350 * @param string $class_name The class name to remove. 5351 * @return bool Whether the class was set to be removed. 5352 */ 5353 public function remove_class( $class_name ): bool { 5354 return $this->is_virtual() ? false : parent::remove_class( $class_name ); 5355 } 5356 5357 /** 5358 * Returns if a matched tag contains the given ASCII case-insensitive class name. 5359 * 5360 * @since 6.6.0 Subclassed for the HTML Processor. 5361 * 5362 * @todo When reconstructing active formatting elements with attributes, find a way 5363 * to indicate if the virtually-reconstructed formatting elements contain the 5364 * wanted class name. 5365 * 5366 * @param string $wanted_class Look for this CSS class name, ASCII case-insensitive. 5367 * @return bool|null Whether the matched tag contains the given class name, or null if not matched. 5368 */ 5369 public function has_class( $wanted_class ): ?bool { 5370 return $this->is_virtual() ? null : parent::has_class( $wanted_class ); 5371 } 5372 5373 /** 5374 * Generator for a foreach loop to step through each class name for the matched tag. 5375 * 5376 * This generator function is designed to be used inside a "foreach" loop. 5377 * 5378 * Example: 5379 * 5380 * $p = WP_HTML_Processor::create_fragment( "<div class='free <egg<\tlang-en'>" ); 5381 * $p->next_tag(); 5382 * foreach ( $p->class_list() as $class_name ) { 5383 * echo "{$class_name} "; 5384 * } 5385 * // Outputs: "free <egg> lang-en " 5386 * 5387 * @since 6.6.0 Subclassed for the HTML Processor. 5388 */ 5389 public function class_list() { 5390 return $this->is_virtual() ? null : parent::class_list(); 5391 } 5392 5393 /** 5394 * Returns the modifiable text for a matched token, or an empty string. 5395 * 5396 * Modifiable text is text content that may be read and changed without 5397 * changing the HTML structure of the document around it. This includes 5398 * the contents of `#text` nodes in the HTML as well as the inner 5399 * contents of HTML comments, Processing Instructions, and others, even 5400 * though these nodes aren't part of a parsed DOM tree. They also contain 5401 * the contents of SCRIPT and STYLE tags, of TEXTAREA tags, and of any 5402 * other section in an HTML document which cannot contain HTML markup (DATA). 5403 * 5404 * If a token has no modifiable text then an empty string is returned to 5405 * avoid needless crashing or type errors. An empty string does not mean 5406 * that a token has modifiable text, and a token with modifiable text may 5407 * have an empty string (e.g. a comment with no contents). 5408 * 5409 * @since 6.6.0 Subclassed for the HTML Processor. 5410 * 5411 * @return string 5412 */ 5413 public function get_modifiable_text(): string { 5414 return $this->is_virtual() ? '' : parent::get_modifiable_text(); 5415 } 5416 5417 /** 5418 * Indicates what kind of comment produced the comment node. 5419 * 5420 * Because there are different kinds of HTML syntax which produce 5421 * comments, the Tag Processor tracks and exposes this as a type 5422 * for the comment. Nominally only regular HTML comments exist as 5423 * they are commonly known, but a number of unrelated syntax errors 5424 * also produce comments. 5425 * 5426 * @see self::COMMENT_AS_ABRUPTLY_CLOSED_COMMENT 5427 * @see self::COMMENT_AS_CDATA_LOOKALIKE 5428 * @see self::COMMENT_AS_INVALID_HTML 5429 * @see self::COMMENT_AS_HTML_COMMENT 5430 * @see self::COMMENT_AS_PI_NODE_LOOKALIKE 5431 * 5432 * @since 6.6.0 Subclassed for the HTML Processor. 5433 * 5434 * @return string|null 5435 */ 5436 public function get_comment_type(): ?string { 5437 return $this->is_virtual() ? null : parent::get_comment_type(); 5438 } 5439 5440 /** 5441 * Removes a bookmark that is no longer needed. 5442 * 5443 * Releasing a bookmark frees up the small 5444 * performance overhead it requires. 5445 * 5446 * @since 6.4.0 5447 * 5448 * @param string $bookmark_name Name of the bookmark to remove. 5449 * @return bool Whether the bookmark already existed before removal. 5450 */ 5451 public function release_bookmark( $bookmark_name ): bool { 5452 return parent::release_bookmark( "_{$bookmark_name}" ); 5453 } 5454 5455 /** 5456 * Moves the internal cursor in the HTML Processor to a given bookmark's location. 5457 * 5458 * Be careful! Seeking backwards to a previous location resets the parser to the 5459 * start of the document and reparses the entire contents up until it finds the 5460 * sought-after bookmarked location. 5461 * 5462 * In order to prevent accidental infinite loops, there's a 5463 * maximum limit on the number of times seek() can be called. 5464 * 5465 * @throws Exception When unable to allocate a bookmark for the next token in the input HTML document. 5466 * 5467 * @since 6.4.0 5468 * 5469 * @param string $bookmark_name Jump to the place in the document identified by this bookmark name. 5470 * @return bool Whether the internal cursor was successfully moved to the bookmark's location. 5471 */ 5472 public function seek( $bookmark_name ): bool { 5473 // Flush any pending updates to the document before beginning. 5474 $this->get_updated_html(); 5475 5476 $actual_bookmark_name = "_{$bookmark_name}"; 5477 $processor_started_at = $this->state->current_token 5478 ? $this->bookmarks[ $this->state->current_token->bookmark_name ]->start 5479 : 0; 5480 $bookmark_starts_at = $this->bookmarks[ $actual_bookmark_name ]->start; 5481 $direction = $bookmark_starts_at > $processor_started_at ? 'forward' : 'backward'; 5482 5483 /* 5484 * If seeking backwards, it's possible that the sought-after bookmark exists within an element 5485 * which has been closed before the current cursor; in other words, it has already been removed 5486 * from the stack of open elements. This means that it's insufficient to simply pop off elements 5487 * from the stack of open elements which appear after the bookmarked location and then jump to 5488 * that location, as the elements which were open before won't be re-opened. 5489 * 5490 * In order to maintain consistency, the HTML Processor rewinds to the start of the document 5491 * and reparses everything until it finds the sought-after bookmark. 5492 * 5493 * There are potentially better ways to do this: cache the parser state for each bookmark and 5494 * restore it when seeking; store an immutable and idempotent register of where elements open 5495 * and close. 5496 * 5497 * If caching the parser state it will be essential to properly maintain the cached stack of 5498 * open elements and active formatting elements when modifying the document. This could be a 5499 * tedious and time-consuming process as well, and so for now will not be performed. 5500 * 5501 * It may be possible to track bookmarks for where elements open and close, and in doing so 5502 * be able to quickly recalculate breadcrumbs for any element in the document. It may even 5503 * be possible to remove the stack of open elements and compute it on the fly this way. 5504 * If doing this, the parser would need to track the opening and closing locations for all 5505 * tokens in the breadcrumb path for any and all bookmarks. By utilizing bookmarks themselves 5506 * this list could be automatically maintained while modifying the document. Finding the 5507 * breadcrumbs would then amount to traversing that list from the start until the token 5508 * being inspected. Once an element closes, if there are no bookmarks pointing to locations 5509 * within that element, then all of these locations may be forgotten to save on memory use 5510 * and computation time. 5511 */ 5512 if ( 'backward' === $direction ) { 5513 5514 /* 5515 * When moving backward, stateful stacks should be cleared. 5516 */ 5517 foreach ( $this->state->stack_of_open_elements->walk_up() as $item ) { 5518 $this->state->stack_of_open_elements->remove_node( $item ); 5519 } 5520 5521 foreach ( $this->state->active_formatting_elements->walk_up() as $item ) { 5522 $this->state->active_formatting_elements->remove_node( $item ); 5523 } 5524 5525 /* 5526 * **After** clearing stacks, more processor state can be reset. 5527 * This must be done after clearing the stack because those stacks generate events that 5528 * would appear on a subsequent call to `next_token()`. 5529 */ 5530 $this->state->frameset_ok = true; 5531 $this->state->stack_of_template_insertion_modes = array(); 5532 $this->state->head_element = null; 5533 $this->state->form_element = null; 5534 $this->state->current_token = null; 5535 $this->current_element = null; 5536 $this->element_queue = array(); 5537 5538 /* 5539 * The absence of a context node indicates a full parse. 5540 * The presence of a context node indicates a fragment parser. 5541 */ 5542 if ( null === $this->context_node ) { 5543 $this->change_parsing_namespace( 'html' ); 5544 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_INITIAL; 5545 $this->breadcrumbs = array(); 5546 5547 $this->bookmarks['initial'] = new WP_HTML_Span( 0, 0 ); 5548 parent::seek( 'initial' ); 5549 unset( $this->bookmarks['initial'] ); 5550 } else { 5551 5552 /* 5553 * Push the root-node (HTML) back onto the stack of open elements. 5554 * 5555 * Fragment parsers require this extra bit of setup. 5556 * It's handled in full parsers by advancing the processor state. 5557 */ 5558 $this->state->stack_of_open_elements->push( 5559 new WP_HTML_Token( 5560 'root-node', 5561 'HTML', 5562 false 5563 ) 5564 ); 5565 5566 $this->change_parsing_namespace( 5567 $this->context_node->integration_node_type 5568 ? 'html' 5569 : $this->context_node->namespace 5570 ); 5571 5572 if ( 'TEMPLATE' === $this->context_node->node_name ) { 5573 $this->state->stack_of_template_insertion_modes[] = WP_HTML_Processor_State::INSERTION_MODE_IN_TEMPLATE; 5574 } 5575 5576 $this->reset_insertion_mode_appropriately(); 5577 $this->breadcrumbs = array_slice( $this->breadcrumbs, 0, 2 ); 5578 parent::seek( $this->context_node->bookmark_name ); 5579 } 5580 } 5581 5582 /* 5583 * Here, the processor moves forward through the document until it matches the bookmark. 5584 * do-while is used here because the processor is expected to already be stopped on 5585 * a token than may match the bookmarked location. 5586 */ 5587 do { 5588 /* 5589 * The processor will stop on virtual tokens, but bookmarks may not be set on them. 5590 * They should not be matched when seeking a bookmark, skip them. 5591 */ 5592 if ( $this->is_virtual() ) { 5593 continue; 5594 } 5595 if ( $bookmark_starts_at === $this->bookmarks[ $this->state->current_token->bookmark_name ]->start ) { 5596 return true; 5597 } 5598 } while ( $this->next_token() ); 5599 5600 return false; 5601 } 5602 5603 /** 5604 * Sets a bookmark in the HTML document. 5605 * 5606 * Bookmarks represent specific places or tokens in the HTML 5607 * document, such as a tag opener or closer. When applying 5608 * edits to a document, such as setting an attribute, the 5609 * text offsets of that token may shift; the bookmark is 5610 * kept updated with those shifts and remains stable unless 5611 * the entire span of text in which the token sits is removed. 5612 * 5613 * Release bookmarks when they are no longer needed. 5614 * 5615 * Example: 5616 * 5617 * <main><h2>Surprising fact you may not know!</h2></main> 5618 * ^ ^ 5619 * \-|-- this `H2` opener bookmark tracks the token 5620 * 5621 * <main class="clickbait"><h2>Surprising fact you may no… 5622 * ^ ^ 5623 * \-|-- it shifts with edits 5624 * 5625 * Bookmarks provide the ability to seek to a previously-scanned 5626 * place in the HTML document. This avoids the need to re-scan 5627 * the entire document. 5628 * 5629 * Example: 5630 * 5631 * <ul><li>One</li><li>Two</li><li>Three</li></ul> 5632 * ^^^^ 5633 * want to note this last item 5634 * 5635 * $p = new WP_HTML_Tag_Processor( $html ); 5636 * $in_list = false; 5637 * while ( $p->next_tag( array( 'tag_closers' => $in_list ? 'visit' : 'skip' ) ) ) { 5638 * if ( 'UL' === $p->get_tag() ) { 5639 * if ( $p->is_tag_closer() ) { 5640 * $in_list = false; 5641 * $p->set_bookmark( 'resume' ); 5642 * if ( $p->seek( 'last-li' ) ) { 5643 * $p->add_class( 'last-li' ); 5644 * } 5645 * $p->seek( 'resume' ); 5646 * $p->release_bookmark( 'last-li' ); 5647 * $p->release_bookmark( 'resume' ); 5648 * } else { 5649 * $in_list = true; 5650 * } 5651 * } 5652 * 5653 * if ( 'LI' === $p->get_tag() ) { 5654 * $p->set_bookmark( 'last-li' ); 5655 * } 5656 * } 5657 * 5658 * Bookmarks intentionally hide the internal string offsets 5659 * to which they refer. They are maintained internally as 5660 * updates are applied to the HTML document and therefore 5661 * retain their "position" - the location to which they 5662 * originally pointed. The inability to use bookmarks with 5663 * functions like `substr` is therefore intentional to guard 5664 * against accidentally breaking the HTML. 5665 * 5666 * Because bookmarks allocate memory and require processing 5667 * for every applied update, they are limited and require 5668 * a name. They should not be created with programmatically-made 5669 * names, such as "li_{$index}" with some loop. As a general 5670 * rule they should only be created with string-literal names 5671 * like "start-of-section" or "last-paragraph". 5672 * 5673 * Bookmarks are a powerful tool to enable complicated behavior. 5674 * Consider double-checking that you need this tool if you are 5675 * reaching for it, as inappropriate use could lead to broken 5676 * HTML structure or unwanted processing overhead. 5677 * 5678 * Bookmarks cannot be set on tokens that do no appear in the original 5679 * HTML text. For example, the HTML `<table><td>` stops at tags `TABLE`, 5680 * `TBODY`, `TR`, and `TD`. The `TBODY` and `TR` tags do not appear in 5681 * the original HTML and cannot be used as bookmarks. 5682 * 5683 * @since 6.4.0 5684 * 5685 * @param string $bookmark_name Identifies this particular bookmark. 5686 * @return bool Whether the bookmark was successfully created. 5687 */ 5688 public function set_bookmark( $bookmark_name ): bool { 5689 if ( $this->is_virtual() ) { 5690 _doing_it_wrong( 5691 __METHOD__, 5692 __( 'Cannot set bookmarks on tokens that do no appear in the original HTML text.' ), 5693 '6.8.0' 5694 ); 5695 return false; 5696 } 5697 return parent::set_bookmark( "_{$bookmark_name}" ); 5698 } 5699 5700 /** 5701 * Checks whether a bookmark with the given name exists. 5702 * 5703 * @since 6.5.0 5704 * 5705 * @param string $bookmark_name Name to identify a bookmark that potentially exists. 5706 * @return bool Whether that bookmark exists. 5707 */ 5708 public function has_bookmark( $bookmark_name ): bool { 5709 return parent::has_bookmark( "_{$bookmark_name}" ); 5710 } 5711 5712 /* 5713 * HTML Parsing Algorithms 5714 */ 5715 5716 /** 5717 * Closes a P element. 5718 * 5719 * @since 6.4.0 5720 * 5721 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 5722 * 5723 * @see https://html.spec.whatwg.org/#close-a-p-element 5724 */ 5725 private function close_a_p_element(): void { 5726 $this->generate_implied_end_tags( 'P' ); 5727 $this->state->stack_of_open_elements->pop_until( 'P' ); 5728 } 5729 5730 /** 5731 * Closes elements that have implied end tags. 5732 * 5733 * @since 6.4.0 5734 * @since 6.7.0 Full spec support. 5735 * 5736 * @see https://html.spec.whatwg.org/#generate-implied-end-tags 5737 * 5738 * @param string|null $except_for_this_element Perform as if this element doesn't exist in the stack of open elements. 5739 */ 5740 private function generate_implied_end_tags( ?string $except_for_this_element = null ): void { 5741 $elements_with_implied_end_tags = array( 5742 'DD', 5743 'DT', 5744 'LI', 5745 'OPTGROUP', 5746 'OPTION', 5747 'P', 5748 'RB', 5749 'RP', 5750 'RT', 5751 'RTC', 5752 ); 5753 5754 $no_exclusions = ! isset( $except_for_this_element ); 5755 5756 while ( 5757 ( $no_exclusions || ! $this->state->stack_of_open_elements->current_node_is( $except_for_this_element ) ) && 5758 in_array( $this->state->stack_of_open_elements->current_node()->node_name, $elements_with_implied_end_tags, true ) 5759 ) { 5760 $this->state->stack_of_open_elements->pop(); 5761 } 5762 } 5763 5764 /** 5765 * Closes elements that have implied end tags, thoroughly. 5766 * 5767 * See the HTML specification for an explanation why this is 5768 * different from generating end tags in the normal sense. 5769 * 5770 * @since 6.4.0 5771 * @since 6.7.0 Full spec support. 5772 * 5773 * @see WP_HTML_Processor::generate_implied_end_tags 5774 * @see https://html.spec.whatwg.org/#generate-implied-end-tags 5775 */ 5776 private function generate_implied_end_tags_thoroughly(): void { 5777 $elements_with_implied_end_tags = array( 5778 'CAPTION', 5779 'COLGROUP', 5780 'DD', 5781 'DT', 5782 'LI', 5783 'OPTGROUP', 5784 'OPTION', 5785 'P', 5786 'RB', 5787 'RP', 5788 'RT', 5789 'RTC', 5790 'TBODY', 5791 'TD', 5792 'TFOOT', 5793 'TH', 5794 'THEAD', 5795 'TR', 5796 ); 5797 5798 while ( in_array( $this->state->stack_of_open_elements->current_node()->node_name, $elements_with_implied_end_tags, true ) ) { 5799 $this->state->stack_of_open_elements->pop(); 5800 } 5801 } 5802 5803 /** 5804 * Returns the adjusted current node. 5805 * 5806 * > The adjusted current node is the context element if the parser was created as 5807 * > part of the HTML fragment parsing algorithm and the stack of open elements 5808 * > has only one element in it (fragment case); otherwise, the adjusted current 5809 * > node is the current node. 5810 * 5811 * @see https://html.spec.whatwg.org/#adjusted-current-node 5812 * 5813 * @since 6.7.0 5814 * 5815 * @return WP_HTML_Token|null The adjusted current node. 5816 */ 5817 private function get_adjusted_current_node(): ?WP_HTML_Token { 5818 if ( isset( $this->context_node ) && 1 === $this->state->stack_of_open_elements->count() ) { 5819 return $this->context_node; 5820 } 5821 5822 return $this->state->stack_of_open_elements->current_node(); 5823 } 5824 5825 /** 5826 * Reconstructs the active formatting elements. 5827 * 5828 * > This has the effect of reopening all the formatting elements that were opened 5829 * > in the current body, cell, or caption (whichever is youngest) that haven't 5830 * > been explicitly closed. 5831 * 5832 * @since 6.4.0 5833 * 5834 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 5835 * 5836 * @see https://html.spec.whatwg.org/#reconstruct-the-active-formatting-elements 5837 * 5838 * @return bool Whether any formatting elements needed to be reconstructed. 5839 */ 5840 private function reconstruct_active_formatting_elements(): bool { 5841 /* 5842 * > If there are no entries in the list of active formatting elements, then there is nothing 5843 * > to reconstruct; stop this algorithm. 5844 */ 5845 if ( 0 === $this->state->active_formatting_elements->count() ) { 5846 return false; 5847 } 5848 5849 $last_entry = $this->state->active_formatting_elements->current_node(); 5850 if ( 5851 5852 /* 5853 * > If the last (most recently added) entry in the list of active formatting elements is a marker; 5854 * > stop this algorithm. 5855 */ 5856 'marker' === $last_entry->node_name || 5857 5858 /* 5859 * > If the last (most recently added) entry in the list of active formatting elements is an 5860 * > element that is in the stack of open elements, then there is nothing to reconstruct; 5861 * > stop this algorithm. 5862 */ 5863 $this->state->stack_of_open_elements->contains_node( $last_entry ) 5864 ) { 5865 return false; 5866 } 5867 5868 $this->bail( 'Cannot reconstruct active formatting elements when advancing and rewinding is required.' ); 5869 } 5870 5871 /** 5872 * Runs the reset the insertion mode appropriately algorithm. 5873 * 5874 * @since 6.7.0 5875 * 5876 * @see https://html.spec.whatwg.org/multipage/parsing.html#reset-the-insertion-mode-appropriately 5877 */ 5878 private function reset_insertion_mode_appropriately(): void { 5879 // Set the first node. 5880 $first_node = null; 5881 foreach ( $this->state->stack_of_open_elements->walk_down() as $first_node ) { 5882 break; 5883 } 5884 5885 /* 5886 * > 1. Let _last_ be false. 5887 */ 5888 $last = false; 5889 foreach ( $this->state->stack_of_open_elements->walk_up() as $node ) { 5890 /* 5891 * > 2. Let _node_ be the last node in the stack of open elements. 5892 * > 3. _Loop_: If _node_ is the first node in the stack of open elements, then set _last_ 5893 * > to true, and, if the parser was created as part of the HTML fragment parsing 5894 * > algorithm (fragment case), set node to the context element passed to 5895 * > that algorithm. 5896 * > … 5897 */ 5898 if ( $node === $first_node ) { 5899 $last = true; 5900 if ( isset( $this->context_node ) ) { 5901 $node = $this->context_node; 5902 } 5903 } 5904 5905 // All of the following rules are for matching HTML elements. 5906 if ( 'html' !== $node->namespace ) { 5907 continue; 5908 } 5909 5910 switch ( $node->node_name ) { 5911 /* 5912 * > 4. If node is a `select` element, run these substeps: 5913 * > 1. If _last_ is true, jump to the step below labeled done. 5914 * > 2. Let _ancestor_ be _node_. 5915 * > 3. _Loop_: If _ancestor_ is the first node in the stack of open elements, 5916 * > jump to the step below labeled done. 5917 * > 4. Let ancestor be the node before ancestor in the stack of open elements. 5918 * > … 5919 * > 7. Jump back to the step labeled _loop_. 5920 * > 8. _Done_: Switch the insertion mode to "in select" and return. 5921 */ 5922 case 'SELECT': 5923 if ( ! $last ) { 5924 foreach ( $this->state->stack_of_open_elements->walk_up( $node ) as $ancestor ) { 5925 if ( 'html' !== $ancestor->namespace ) { 5926 continue; 5927 } 5928 5929 switch ( $ancestor->node_name ) { 5930 /* 5931 * > 5. If _ancestor_ is a `template` node, jump to the step below 5932 * > labeled _done_. 5933 */ 5934 case 'TEMPLATE': 5935 break 2; 5936 5937 /* 5938 * > 6. If _ancestor_ is a `table` node, switch the insertion mode to 5939 * > "in select in table" and return. 5940 */ 5941 case 'TABLE': 5942 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_SELECT_IN_TABLE; 5943 return; 5944 } 5945 } 5946 } 5947 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_SELECT; 5948 return; 5949 5950 /* 5951 * > 5. If _node_ is a `td` or `th` element and _last_ is false, then switch the 5952 * > insertion mode to "in cell" and return. 5953 */ 5954 case 'TD': 5955 case 'TH': 5956 if ( ! $last ) { 5957 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_CELL; 5958 return; 5959 } 5960 break; 5961 5962 /* 5963 * > 6. If _node_ is a `tr` element, then switch the insertion mode to "in row" 5964 * > and return. 5965 */ 5966 case 'TR': 5967 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_ROW; 5968 return; 5969 5970 /* 5971 * > 7. If _node_ is a `tbody`, `thead`, or `tfoot` element, then switch the 5972 * > insertion mode to "in table body" and return. 5973 */ 5974 case 'TBODY': 5975 case 'THEAD': 5976 case 'TFOOT': 5977 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY; 5978 return; 5979 5980 /* 5981 * > 8. If _node_ is a `caption` element, then switch the insertion mode to 5982 * > "in caption" and return. 5983 */ 5984 case 'CAPTION': 5985 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_CAPTION; 5986 return; 5987 5988 /* 5989 * > 9. If _node_ is a `colgroup` element, then switch the insertion mode to 5990 * > "in column group" and return. 5991 */ 5992 case 'COLGROUP': 5993 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_COLUMN_GROUP; 5994 return; 5995 5996 /* 5997 * > 10. If _node_ is a `table` element, then switch the insertion mode to 5998 * > "in table" and return. 5999 */ 6000 case 'TABLE': 6001 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE; 6002 return; 6003 6004 /* 6005 * > 11. If _node_ is a `template` element, then switch the insertion mode to the 6006 * > current template insertion mode and return. 6007 */ 6008 case 'TEMPLATE': 6009 $this->state->insertion_mode = end( $this->state->stack_of_template_insertion_modes ); 6010 return; 6011 6012 /* 6013 * > 12. If _node_ is a `head` element and _last_ is false, then switch the 6014 * > insertion mode to "in head" and return. 6015 */ 6016 case 'HEAD': 6017 if ( ! $last ) { 6018 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_HEAD; 6019 return; 6020 } 6021 break; 6022 6023 /* 6024 * > 13. If _node_ is a `body` element, then switch the insertion mode to "in body" 6025 * > and return. 6026 */ 6027 case 'BODY': 6028 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY; 6029 return; 6030 6031 /* 6032 * > 14. If _node_ is a `frameset` element, then switch the insertion mode to 6033 * > "in frameset" and return. (fragment case) 6034 */ 6035 case 'FRAMESET': 6036 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_FRAMESET; 6037 return; 6038 6039 /* 6040 * > 15. If _node_ is an `html` element, run these substeps: 6041 * > 1. If the head element pointer is null, switch the insertion mode to 6042 * > "before head" and return. (fragment case) 6043 * > 2. Otherwise, the head element pointer is not null, switch the insertion 6044 * > mode to "after head" and return. 6045 */ 6046 case 'HTML': 6047 $this->state->insertion_mode = isset( $this->state->head_element ) 6048 ? WP_HTML_Processor_State::INSERTION_MODE_AFTER_HEAD 6049 : WP_HTML_Processor_State::INSERTION_MODE_BEFORE_HEAD; 6050 return; 6051 } 6052 } 6053 6054 /* 6055 * > 16. If _last_ is true, then switch the insertion mode to "in body" 6056 * > and return. (fragment case) 6057 * 6058 * This is only reachable if `$last` is true, as per the fragment parsing case. 6059 */ 6060 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY; 6061 } 6062 6063 /** 6064 * Runs the adoption agency algorithm. 6065 * 6066 * @since 6.4.0 6067 * 6068 * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input. 6069 * 6070 * @see https://html.spec.whatwg.org/#adoption-agency-algorithm 6071 */ 6072 private function run_adoption_agency_algorithm(): void { 6073 $budget = 1000; 6074 $subject = $this->get_tag(); 6075 $current_node = $this->state->stack_of_open_elements->current_node(); 6076 6077 if ( 6078 // > If the current node is an HTML element whose tag name is subject 6079 $current_node && $subject === $current_node->node_name && 6080 // > the current node is not in the list of active formatting elements 6081 ! $this->state->active_formatting_elements->contains_node( $current_node ) 6082 ) { 6083 $this->state->stack_of_open_elements->pop(); 6084 return; 6085 } 6086 6087 $outer_loop_counter = 0; 6088 while ( $budget-- > 0 ) { 6089 if ( $outer_loop_counter++ >= 8 ) { 6090 return; 6091 } 6092 6093 /* 6094 * > Let formatting element be the last element in the list of active formatting elements that: 6095 * > - is between the end of the list and the last marker in the list, 6096 * > if any, or the start of the list otherwise, 6097 * > - and has the tag name subject. 6098 */ 6099 $formatting_element = null; 6100 foreach ( $this->state->active_formatting_elements->walk_up() as $item ) { 6101 if ( 'marker' === $item->node_name ) { 6102 break; 6103 } 6104 6105 if ( $subject === $item->node_name ) { 6106 $formatting_element = $item; 6107 break; 6108 } 6109 } 6110 6111 // > If there is no such element, then return and instead act as described in the "any other end tag" entry above. 6112 if ( null === $formatting_element ) { 6113 $this->bail( 'Cannot run adoption agency when "any other end tag" is required.' ); 6114 } 6115 6116 // > If formatting element is not in the stack of open elements, then this is a parse error; remove the element from the list, and return. 6117 if ( ! $this->state->stack_of_open_elements->contains_node( $formatting_element ) ) { 6118 $this->state->active_formatting_elements->remove_node( $formatting_element ); 6119 return; 6120 } 6121 6122 // > If formatting element is in the stack of open elements, but the element is not in scope, then this is a parse error; return. 6123 if ( ! $this->state->stack_of_open_elements->has_element_in_scope( $formatting_element->node_name ) ) { 6124 return; 6125 } 6126 6127 /* 6128 * > Let furthest block be the topmost node in the stack of open elements that is lower in the stack 6129 * > than formatting element, and is an element in the special category. There might not be one. 6130 */ 6131 $is_above_formatting_element = true; 6132 $furthest_block = null; 6133 foreach ( $this->state->stack_of_open_elements->walk_down() as $item ) { 6134 if ( $is_above_formatting_element && $formatting_element->bookmark_name !== $item->bookmark_name ) { 6135 continue; 6136 } 6137 6138 if ( $is_above_formatting_element ) { 6139 $is_above_formatting_element = false; 6140 continue; 6141 } 6142 6143 if ( self::is_special( $item ) ) { 6144 $furthest_block = $item; 6145 break; 6146 } 6147 } 6148 6149 /* 6150 * > If there is no furthest block, then the UA must first pop all the nodes from the bottom of the 6151 * > stack of open elements, from the current node up to and including formatting element, then 6152 * > remove formatting element from the list of active formatting elements, and finally return. 6153 */ 6154 if ( null === $furthest_block ) { 6155 foreach ( $this->state->stack_of_open_elements->walk_up() as $item ) { 6156 $this->state->stack_of_open_elements->pop(); 6157 6158 if ( $formatting_element->bookmark_name === $item->bookmark_name ) { 6159 $this->state->active_formatting_elements->remove_node( $formatting_element ); 6160 return; 6161 } 6162 } 6163 } 6164 6165 $this->bail( 'Cannot extract common ancestor in adoption agency algorithm.' ); 6166 } 6167 6168 $this->bail( 'Cannot run adoption agency when looping required.' ); 6169 } 6170 6171 /** 6172 * Runs the "close the cell" algorithm. 6173 * 6174 * > Where the steps above say to close the cell, they mean to run the following algorithm: 6175 * > 1. Generate implied end tags. 6176 * > 2. If the current node is not now a td element or a th element, then this is a parse error. 6177 * > 3. Pop elements from the stack of open elements stack until a td element or a th element has been popped from the stack. 6178 * > 4. Clear the list of active formatting elements up to the last marker. 6179 * > 5. Switch the insertion mode to "in row". 6180 * 6181 * @see https://html.spec.whatwg.org/multipage/parsing.html#close-the-cell 6182 * 6183 * @since 6.7.0 6184 */ 6185 private function close_cell(): void { 6186 $this->generate_implied_end_tags(); 6187 // @todo Parse error if the current node is a "td" or "th" element. 6188 foreach ( $this->state->stack_of_open_elements->walk_up() as $element ) { 6189 $this->state->stack_of_open_elements->pop(); 6190 if ( 'TD' === $element->node_name || 'TH' === $element->node_name ) { 6191 break; 6192 } 6193 } 6194 $this->state->active_formatting_elements->clear_up_to_last_marker(); 6195 $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_ROW; 6196 } 6197 6198 /** 6199 * Inserts an HTML element on the stack of open elements. 6200 * 6201 * @since 6.4.0 6202 * 6203 * @see https://html.spec.whatwg.org/#insert-a-foreign-element 6204 * 6205 * @param WP_HTML_Token $token Name of bookmark pointing to element in original input HTML. 6206 */ 6207 private function insert_html_element( WP_HTML_Token $token ): void { 6208 $this->state->stack_of_open_elements->push( $token ); 6209 } 6210 6211 /** 6212 * Inserts a foreign element on to the stack of open elements. 6213 * 6214 * @since 6.7.0 6215 * 6216 * @see https://html.spec.whatwg.org/#insert-a-foreign-element 6217 * 6218 * @param WP_HTML_Token $token Insert this token. The token's namespace and 6219 * insertion point will be updated correctly. 6220 * @param bool $only_add_to_element_stack Whether to skip the "insert an element at the adjusted 6221 * insertion location" algorithm when adding this element. 6222 */ 6223 private function insert_foreign_element( WP_HTML_Token $token, bool $only_add_to_element_stack ): void { 6224 $adjusted_current_node = $this->get_adjusted_current_node(); 6225 6226 $token->namespace = $adjusted_current_node ? $adjusted_current_node->namespace : 'html'; 6227 6228 if ( $this->is_mathml_integration_point() ) { 6229 $token->integration_node_type = 'math'; 6230 } elseif ( $this->is_html_integration_point() ) { 6231 $token->integration_node_type = 'html'; 6232 } 6233 6234 if ( false === $only_add_to_element_stack ) { 6235 /* 6236 * @todo Implement the "appropriate place for inserting a node" and the 6237 * "insert an element at the adjusted insertion location" algorithms. 6238 * 6239 * These algorithms mostly impacts DOM tree construction and not the HTML API. 6240 * Here, there's no DOM node onto which the element will be appended, so the 6241 * parser will skip this step. 6242 * 6243 * @see https://html.spec.whatwg.org/#insert-an-element-at-the-adjusted-insertion-location 6244 */ 6245 } 6246 6247 $this->insert_html_element( $token ); 6248 } 6249 6250 /** 6251 * Inserts a virtual element on the stack of open elements. 6252 * 6253 * @since 6.7.0 6254 * 6255 * @param string $token_name Name of token to create and insert into the stack of open elements. 6256 * @param string|null $bookmark_name Optional. Name to give bookmark for created virtual node. 6257 * Defaults to auto-creating a bookmark name. 6258 * @return WP_HTML_Token Newly-created virtual token. 6259 */ 6260 private function insert_virtual_node( $token_name, $bookmark_name = null ): WP_HTML_Token { 6261 $here = $this->bookmarks[ $this->state->current_token->bookmark_name ]; 6262 $name = $bookmark_name ?? $this->bookmark_token(); 6263 6264 $this->bookmarks[ $name ] = new WP_HTML_Span( $here->start, 0 ); 6265 6266 $token = new WP_HTML_Token( $name, $token_name, false ); 6267 $this->insert_html_element( $token ); 6268 return $token; 6269 } 6270 6271 /* 6272 * HTML Specification Helpers 6273 */ 6274 6275 /** 6276 * Indicates if the current token is a MathML integration point. 6277 * 6278 * @since 6.7.0 6279 * 6280 * @see https://html.spec.whatwg.org/#mathml-text-integration-point 6281 * 6282 * @return bool Whether the current token is a MathML integration point. 6283 */ 6284 private function is_mathml_integration_point(): bool { 6285 $current_token = $this->state->current_token; 6286 if ( ! isset( $current_token ) ) { 6287 return false; 6288 } 6289 6290 if ( 'math' !== $current_token->namespace || 'M' !== $current_token->node_name[0] ) { 6291 return false; 6292 } 6293 6294 $tag_name = $current_token->node_name; 6295 6296 return ( 6297 'MI' === $tag_name || 6298 'MO' === $tag_name || 6299 'MN' === $tag_name || 6300 'MS' === $tag_name || 6301 'MTEXT' === $tag_name 6302 ); 6303 } 6304 6305 /** 6306 * Indicates if the current token is an HTML integration point. 6307 * 6308 * Note that this method must be an instance method with access 6309 * to the current token, since it needs to examine the attributes 6310 * of the currently-matched tag, if it's in the MathML namespace. 6311 * Otherwise it would be required to scan the HTML and ensure that 6312 * no other accounting is overlooked. 6313 * 6314 * @since 6.7.0 6315 * 6316 * @see https://html.spec.whatwg.org/#html-integration-point 6317 * 6318 * @return bool Whether the current token is an HTML integration point. 6319 */ 6320 private function is_html_integration_point(): bool { 6321 $current_token = $this->state->current_token; 6322 if ( ! isset( $current_token ) ) { 6323 return false; 6324 } 6325 6326 if ( 'html' === $current_token->namespace ) { 6327 return false; 6328 } 6329 6330 $tag_name = $current_token->node_name; 6331 6332 if ( 'svg' === $current_token->namespace ) { 6333 return ( 6334 'DESC' === $tag_name || 6335 'FOREIGNOBJECT' === $tag_name || 6336 'TITLE' === $tag_name 6337 ); 6338 } 6339 6340 if ( 'math' === $current_token->namespace ) { 6341 if ( 'ANNOTATION-XML' !== $tag_name ) { 6342 return false; 6343 } 6344 6345 $encoding = $this->get_attribute( 'encoding' ); 6346 6347 return ( 6348 is_string( $encoding ) && 6349 ( 6350 0 === strcasecmp( $encoding, 'application/xhtml+xml' ) || 6351 0 === strcasecmp( $encoding, 'text/html' ) 6352 ) 6353 ); 6354 } 6355 6356 $this->bail( 'Should not have reached end of HTML Integration Point detection: check HTML API code.' ); 6357 // This unnecessary return prevents tools from inaccurately reporting type errors. 6358 return false; 6359 } 6360 6361 /** 6362 * Returns whether an element of a given name is in the HTML special category. 6363 * 6364 * @since 6.4.0 6365 * 6366 * @see https://html.spec.whatwg.org/#special 6367 * 6368 * @param WP_HTML_Token|string $tag_name Node to check, or only its name if in the HTML namespace. 6369 * @return bool Whether the element of the given name is in the special category. 6370 */ 6371 public static function is_special( $tag_name ): bool { 6372 if ( is_string( $tag_name ) ) { 6373 $tag_name = strtoupper( $tag_name ); 6374 } else { 6375 $tag_name = 'html' === $tag_name->namespace 6376 ? strtoupper( $tag_name->node_name ) 6377 : "{$tag_name->namespace} {$tag_name->node_name}"; 6378 } 6379 6380 return ( 6381 'ADDRESS' === $tag_name || 6382 'APPLET' === $tag_name || 6383 'AREA' === $tag_name || 6384 'ARTICLE' === $tag_name || 6385 'ASIDE' === $tag_name || 6386 'BASE' === $tag_name || 6387 'BASEFONT' === $tag_name || 6388 'BGSOUND' === $tag_name || 6389 'BLOCKQUOTE' === $tag_name || 6390 'BODY' === $tag_name || 6391 'BR' === $tag_name || 6392 'BUTTON' === $tag_name || 6393 'CAPTION' === $tag_name || 6394 'CENTER' === $tag_name || 6395 'COL' === $tag_name || 6396 'COLGROUP' === $tag_name || 6397 'DD' === $tag_name || 6398 'DETAILS' === $tag_name || 6399 'DIR' === $tag_name || 6400 'DIV' === $tag_name || 6401 'DL' === $tag_name || 6402 'DT' === $tag_name || 6403 'EMBED' === $tag_name || 6404 'FIELDSET' === $tag_name || 6405 'FIGCAPTION' === $tag_name || 6406 'FIGURE' === $tag_name || 6407 'FOOTER' === $tag_name || 6408 'FORM' === $tag_name || 6409 'FRAME' === $tag_name || 6410 'FRAMESET' === $tag_name || 6411 'H1' === $tag_name || 6412 'H2' === $tag_name || 6413 'H3' === $tag_name || 6414 'H4' === $tag_name || 6415 'H5' === $tag_name || 6416 'H6' === $tag_name || 6417 'HEAD' === $tag_name || 6418 'HEADER' === $tag_name || 6419 'HGROUP' === $tag_name || 6420 'HR' === $tag_name || 6421 'HTML' === $tag_name || 6422 'IFRAME' === $tag_name || 6423 'IMG' === $tag_name || 6424 'INPUT' === $tag_name || 6425 'KEYGEN' === $tag_name || 6426 'LI' === $tag_name || 6427 'LINK' === $tag_name || 6428 'LISTING' === $tag_name || 6429 'MAIN' === $tag_name || 6430 'MARQUEE' === $tag_name || 6431 'MENU' === $tag_name || 6432 'META' === $tag_name || 6433 'NAV' === $tag_name || 6434 'NOEMBED' === $tag_name || 6435 'NOFRAMES' === $tag_name || 6436 'NOSCRIPT' === $tag_name || 6437 'OBJECT' === $tag_name || 6438 'OL' === $tag_name || 6439 'P' === $tag_name || 6440 'PARAM' === $tag_name || 6441 'PLAINTEXT' === $tag_name || 6442 'PRE' === $tag_name || 6443 'SCRIPT' === $tag_name || 6444 'SEARCH' === $tag_name || 6445 'SECTION' === $tag_name || 6446 'SELECT' === $tag_name || 6447 'SOURCE' === $tag_name || 6448 'STYLE' === $tag_name || 6449 'SUMMARY' === $tag_name || 6450 'TABLE' === $tag_name || 6451 'TBODY' === $tag_name || 6452 'TD' === $tag_name || 6453 'TEMPLATE' === $tag_name || 6454 'TEXTAREA' === $tag_name || 6455 'TFOOT' === $tag_name || 6456 'TH' === $tag_name || 6457 'THEAD' === $tag_name || 6458 'TITLE' === $tag_name || 6459 'TR' === $tag_name || 6460 'TRACK' === $tag_name || 6461 'UL' === $tag_name || 6462 'WBR' === $tag_name || 6463 'XMP' === $tag_name || 6464 6465 // MathML. 6466 'math MI' === $tag_name || 6467 'math MO' === $tag_name || 6468 'math MN' === $tag_name || 6469 'math MS' === $tag_name || 6470 'math MTEXT' === $tag_name || 6471 'math ANNOTATION-XML' === $tag_name || 6472 6473 // SVG. 6474 'svg DESC' === $tag_name || 6475 'svg FOREIGNOBJECT' === $tag_name || 6476 'svg TITLE' === $tag_name 6477 ); 6478 } 6479 6480 /** 6481 * Returns whether a given element is an HTML Void Element 6482 * 6483 * > area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr 6484 * 6485 * @since 6.4.0 6486 * 6487 * @see https://html.spec.whatwg.org/#void-elements 6488 * 6489 * @param string $tag_name Name of HTML tag to check. 6490 * @return bool Whether the given tag is an HTML Void Element. 6491 */ 6492 public static function is_void( $tag_name ): bool { 6493 $tag_name = strtoupper( $tag_name ); 6494 6495 return ( 6496 'AREA' === $tag_name || 6497 'BASE' === $tag_name || 6498 'BASEFONT' === $tag_name || // Obsolete but still treated as void. 6499 'BGSOUND' === $tag_name || // Obsolete but still treated as void. 6500 'BR' === $tag_name || 6501 'COL' === $tag_name || 6502 'EMBED' === $tag_name || 6503 'FRAME' === $tag_name || 6504 'HR' === $tag_name || 6505 'IMG' === $tag_name || 6506 'INPUT' === $tag_name || 6507 'KEYGEN' === $tag_name || // Obsolete but still treated as void. 6508 'LINK' === $tag_name || 6509 'META' === $tag_name || 6510 'PARAM' === $tag_name || // Obsolete but still treated as void. 6511 'SOURCE' === $tag_name || 6512 'TRACK' === $tag_name || 6513 'WBR' === $tag_name 6514 ); 6515 } 6516 6517 /** 6518 * Gets an encoding from a given string. 6519 * 6520 * This is an algorithm defined in the WHAT-WG specification. 6521 * 6522 * Example: 6523 * 6524 * 'UTF-8' === self::get_encoding( 'utf8' ); 6525 * 'UTF-8' === self::get_encoding( " \tUTF-8 " ); 6526 * null === self::get_encoding( 'UTF-7' ); 6527 * null === self::get_encoding( 'utf8; charset=' ); 6528 * 6529 * @see https://encoding.spec.whatwg.org/#concept-encoding-get 6530 * 6531 * @todo As this parser only supports UTF-8, only the UTF-8 6532 * encodings are detected. Add more as desired, but the 6533 * parser will bail on non-UTF-8 encodings. 6534 * 6535 * @since 6.7.0 6536 * 6537 * @param string $label A string which may specify a known encoding. 6538 * @return string|null Known encoding if matched, otherwise null. 6539 */ 6540 protected static function get_encoding( string $label ): ?string { 6541 /* 6542 * > Remove any leading and trailing ASCII whitespace from label. 6543 */ 6544 $label = trim( $label, " \t\f\r\n" ); 6545 6546 /* 6547 * > If label is an ASCII case-insensitive match for any of the labels listed in the 6548 * > table below, then return the corresponding encoding; otherwise return failure. 6549 */ 6550 switch ( strtolower( $label ) ) { 6551 case 'unicode-1-1-utf-8': 6552 case 'unicode11utf8': 6553 case 'unicode20utf8': 6554 case 'utf-8': 6555 case 'utf8': 6556 case 'x-unicode20utf8': 6557 return 'UTF-8'; 6558 6559 default: 6560 return null; 6561 } 6562 } 6563 6564 /* 6565 * Constants that would pollute the top of the class if they were found there. 6566 */ 6567 6568 /** 6569 * Indicates that the next HTML token should be parsed and processed. 6570 * 6571 * @since 6.4.0 6572 * 6573 * @var string 6574 */ 6575 const PROCESS_NEXT_NODE = 'process-next-node'; 6576 6577 /** 6578 * Indicates that the current HTML token should be reprocessed in the newly-selected insertion mode. 6579 * 6580 * @since 6.4.0 6581 * 6582 * @var string 6583 */ 6584 const REPROCESS_CURRENT_NODE = 'reprocess-current-node'; 6585 6586 /** 6587 * Indicates that the current HTML token should be processed without advancing the parser. 6588 * 6589 * @since 6.5.0 6590 * 6591 * @var string 6592 */ 6593 const PROCESS_CURRENT_NODE = 'process-current-node'; 6594 6595 /** 6596 * Indicates that the parser encountered unsupported markup and has bailed. 6597 * 6598 * @since 6.4.0 6599 * 6600 * @var string 6601 */ 6602 const ERROR_UNSUPPORTED = 'unsupported'; 6603 6604 /** 6605 * Indicates that the parser encountered more HTML tokens than it 6606 * was able to process and has bailed. 6607 * 6608 * @since 6.4.0 6609 * 6610 * @var string 6611 */ 6612 const ERROR_EXCEEDED_MAX_BOOKMARKS = 'exceeded-max-bookmarks'; 6613 6614 /** 6615 * Unlock code that must be passed into the constructor to create this class. 6616 * 6617 * This class extends the WP_HTML_Tag_Processor, which has a public class 6618 * constructor. Therefore, it's not possible to have a private constructor here. 6619 * 6620 * This unlock code is used to ensure that anyone calling the constructor is 6621 * doing so with a full understanding that it's intended to be a private API. 6622 * 6623 * @access private 6624 */ 6625 const CONSTRUCTOR_UNLOCK_CODE = 'Use WP_HTML_Processor::create_fragment() instead of calling the class constructor directly.'; 6626 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Fri Jul 25 08:20:01 2025 | Cross-referenced by PHPXref |