[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/html-api/ -> class-wp-html-open-elements.php (source)

   1  <?php
   2  /**
   3   * HTML API: WP_HTML_Open_Elements class
   4   *
   5   * @package WordPress
   6   * @subpackage HTML-API
   7   * @since 6.4.0
   8   */
   9  
  10  /**
  11   * Core class used by the HTML processor during HTML parsing
  12   * for managing the stack of open elements.
  13   *
  14   * This class is designed for internal use by the HTML processor.
  15   *
  16   * > Initially, the stack of open elements is empty. The stack grows
  17   * > downwards; the topmost node on the stack is the first one added
  18   * > to the stack, and the bottommost node of the stack is the most
  19   * > recently added node in the stack (notwithstanding when the stack
  20   * > is manipulated in a random access fashion as part of the handling
  21   * > for misnested tags).
  22   *
  23   * @since 6.4.0
  24   *
  25   * @access private
  26   * @ignore
  27   *
  28   * @see https://html.spec.whatwg.org/#stack-of-open-elements
  29   * @see WP_HTML_Processor
  30   */
  31  class WP_HTML_Open_Elements {
  32      /**
  33       * Holds the stack of open element references.
  34       *
  35       * @since 6.4.0
  36       *
  37       * @var WP_HTML_Token[]
  38       */
  39      public $stack = array();
  40  
  41      /**
  42       * Whether a P element is in button scope currently.
  43       *
  44       * This class optimizes scope lookup by pre-calculating
  45       * this value when elements are added and removed to the
  46       * stack of open elements which might change its value.
  47       * This avoids frequent iteration over the stack.
  48       *
  49       * @since 6.4.0
  50       *
  51       * @var bool
  52       */
  53      private $has_p_in_button_scope = false;
  54  
  55      /**
  56       * A function that will be called when an item is popped off the stack of open elements.
  57       *
  58       * The function will be called with the popped item as its argument.
  59       *
  60       * @since 6.6.0
  61       *
  62       * @var Closure|null
  63       */
  64      private $pop_handler = null;
  65  
  66      /**
  67       * A function that will be called when an item is pushed onto the stack of open elements.
  68       *
  69       * The function will be called with the pushed item as its argument.
  70       *
  71       * @since 6.6.0
  72       *
  73       * @var Closure|null
  74       */
  75      private $push_handler = null;
  76  
  77      /**
  78       * Sets a pop handler that will be called when an item is popped off the stack of
  79       * open elements.
  80       *
  81       * The function will be called with the popped item as its argument.
  82       *
  83       * @since 6.6.0
  84       *
  85       * @param Closure $handler The handler function.
  86       */
  87  	public function set_pop_handler( Closure $handler ): void {
  88          $this->pop_handler = $handler;
  89      }
  90  
  91      /**
  92       * Sets a push handler that will be called when an item is pushed onto the stack of
  93       * open elements.
  94       *
  95       * The function will be called with the pushed item as its argument.
  96       *
  97       * @since 6.6.0
  98       *
  99       * @param Closure $handler The handler function.
 100       */
 101  	public function set_push_handler( Closure $handler ): void {
 102          $this->push_handler = $handler;
 103      }
 104  
 105      /**
 106       * Returns the node at the nth position on the stack
 107       * of open elements, or `null` if no such position exists.
 108       *
 109       * Note that this uses a 1-based index, which represents the
 110       * "nth item" on the stack, counting from the top, where the
 111       * top-most element is the 1st, the second is the 2nd, etc...
 112       *
 113       * @since 6.7.0
 114       *
 115       * @param int $nth Retrieve the nth item on the stack, with 1 being
 116       *                 the top element, 2 being the second, etc...
 117       * @return WP_HTML_Token|null The node on the stack at the given location,
 118       *                            or `null` if the location isn't on the stack.
 119       */
 120      public function at( int $nth ): ?WP_HTML_Token {
 121          foreach ( $this->walk_down() as $item ) {
 122              if ( 0 === --$nth ) {
 123                  return $item;
 124              }
 125          }
 126  
 127          return null;
 128      }
 129  
 130      /**
 131       * Reports if an HTML element of a given name is on the stack of open elements.
 132       *
 133       * @since 6.7.0
 134       *
 135       * @param string $node_name Name of HTML element for which to check.
 136       * @return bool Whether a node of the given name is in the stack of open elements.
 137       */
 138  	public function contains( string $node_name ): bool {
 139          foreach ( $this->walk_up() as $item ) {
 140              if ( 'html' === $item->namespace && $node_name === $item->node_name ) {
 141                  return true;
 142              }
 143          }
 144  
 145          return false;
 146      }
 147  
 148      /**
 149       * Reports if a specific node is in the stack of open elements.
 150       *
 151       * @since 6.4.0
 152       *
 153       * @param WP_HTML_Token $token Look for this node in the stack.
 154       * @return bool Whether the referenced node is in the stack of open elements.
 155       */
 156  	public function contains_node( WP_HTML_Token $token ): bool {
 157          foreach ( $this->walk_up() as $item ) {
 158              if ( $token === $item ) {
 159                  return true;
 160              }
 161          }
 162  
 163          return false;
 164      }
 165  
 166      /**
 167       * Returns how many nodes are currently in the stack of open elements.
 168       *
 169       * @since 6.4.0
 170       *
 171       * @return int How many nodes are in the stack of open elements.
 172       */
 173  	public function count(): int {
 174          return count( $this->stack );
 175      }
 176  
 177      /**
 178       * Returns the node at the end of the stack of open elements,
 179       * if one exists. If the stack is empty, returns null.
 180       *
 181       * @since 6.4.0
 182       *
 183       * @return WP_HTML_Token|null Last node in the stack of open elements, if one exists, otherwise null.
 184       */
 185  	public function current_node(): ?WP_HTML_Token {
 186          $current_node = end( $this->stack );
 187  
 188          return $current_node ? $current_node : null;
 189      }
 190  
 191      /**
 192       * Indicates if the current node is of a given type or name.
 193       *
 194       * It's possible to pass either a node type or a node name to this function.
 195       * In the case there is no current element it will always return `false`.
 196       *
 197       * Example:
 198       *
 199       *     // Is the current node a text node?
 200       *     $stack->current_node_is( '#text' );
 201       *
 202       *     // Is the current node a DIV element?
 203       *     $stack->current_node_is( 'DIV' );
 204       *
 205       *     // Is the current node any element/tag?
 206       *     $stack->current_node_is( '#tag' );
 207       *
 208       * @see WP_HTML_Tag_Processor::get_token_type
 209       * @see WP_HTML_Tag_Processor::get_token_name
 210       *
 211       * @since 6.7.0
 212       *
 213       * @access private
 214       *
 215       * @param string $identity Check if the current node has this name or type (depending on what is provided).
 216       * @return bool Whether there is a current element that matches the given identity, whether a token name or type.
 217       */
 218  	public function current_node_is( string $identity ): bool {
 219          $current_node = end( $this->stack );
 220          if ( false === $current_node ) {
 221              return false;
 222          }
 223  
 224          $current_node_name = $current_node->node_name;
 225  
 226          return (
 227              $current_node_name === $identity ||
 228              ( '#doctype' === $identity && 'html' === $current_node_name ) ||
 229              ( '#tag' === $identity && ctype_upper( $current_node_name ) )
 230          );
 231      }
 232  
 233      /**
 234       * Returns whether an element is in a specific scope.
 235       *
 236       * @since 6.4.0
 237       *
 238       * @see https://html.spec.whatwg.org/#has-an-element-in-the-specific-scope
 239       *
 240       * @param string   $tag_name         Name of tag check.
 241       * @param string[] $termination_list List of elements that terminate the search.
 242       * @return bool Whether the element was found in a specific scope.
 243       */
 244  	public function has_element_in_specific_scope( string $tag_name, $termination_list ): bool {
 245          foreach ( $this->walk_up() as $node ) {
 246              $namespaced_name = 'html' === $node->namespace
 247                  ? $node->node_name
 248                  : "{$node->namespace} {$node->node_name}";
 249  
 250              if ( $namespaced_name === $tag_name ) {
 251                  return true;
 252              }
 253  
 254              if (
 255                  '(internal: H1 through H6 - do not use)' === $tag_name &&
 256                  in_array( $namespaced_name, array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true )
 257              ) {
 258                  return true;
 259              }
 260  
 261              if ( in_array( $namespaced_name, $termination_list, true ) ) {
 262                  return false;
 263              }
 264          }
 265  
 266          return false;
 267      }
 268  
 269      /**
 270       * Returns whether a particular element is in scope.
 271       *
 272       * > The stack of open elements is said to have a particular element in
 273       * > scope when it has that element in the specific scope consisting of
 274       * > the following element types:
 275       * >
 276       * >   - applet
 277       * >   - caption
 278       * >   - html
 279       * >   - table
 280       * >   - td
 281       * >   - th
 282       * >   - marquee
 283       * >   - object
 284       * >   - select
 285       * >   - template
 286       * >   - MathML mi
 287       * >   - MathML mo
 288       * >   - MathML mn
 289       * >   - MathML ms
 290       * >   - MathML mtext
 291       * >   - MathML annotation-xml
 292       * >   - SVG foreignObject
 293       * >   - SVG desc
 294       * >   - SVG title
 295       *
 296       * @since 6.4.0
 297       * @since 6.7.0 Full support.
 298       *
 299       * @see https://html.spec.whatwg.org/#has-an-element-in-scope
 300       *
 301       * @param string $tag_name Name of tag to check.
 302       * @return bool Whether given element is in scope.
 303       */
 304  	public function has_element_in_scope( string $tag_name ): bool {
 305          return $this->has_element_in_specific_scope(
 306              $tag_name,
 307              array(
 308                  'APPLET',
 309                  'CAPTION',
 310                  'HTML',
 311                  'TABLE',
 312                  'TD',
 313                  'TH',
 314                  'MARQUEE',
 315                  'OBJECT',
 316                  'SELECT',
 317                  'TEMPLATE',
 318  
 319                  'math MI',
 320                  'math MO',
 321                  'math MN',
 322                  'math MS',
 323                  'math MTEXT',
 324                  'math ANNOTATION-XML',
 325  
 326                  'svg FOREIGNOBJECT',
 327                  'svg DESC',
 328                  'svg TITLE',
 329              )
 330          );
 331      }
 332  
 333      /**
 334       * Returns whether a particular element is in list item scope.
 335       *
 336       * > The stack of open elements is said to have a particular element
 337       * > in list item scope when it has that element in the specific scope
 338       * > consisting of the following element types:
 339       * >
 340       * >   - All the element types listed above for the has an element in scope algorithm.
 341       * >   - ol in the HTML namespace
 342       * >   - ul in the HTML namespace
 343       *
 344       * @since 6.4.0
 345       * @since 6.5.0 Implemented: no longer throws on every invocation.
 346       * @since 6.7.0 Supports all required HTML elements.
 347       *
 348       * @see https://html.spec.whatwg.org/#has-an-element-in-list-item-scope
 349       *
 350       * @param string $tag_name Name of tag to check.
 351       * @return bool Whether given element is in scope.
 352       */
 353  	public function has_element_in_list_item_scope( string $tag_name ): bool {
 354          return $this->has_element_in_specific_scope(
 355              $tag_name,
 356              array(
 357                  'APPLET',
 358                  'BUTTON',
 359                  'CAPTION',
 360                  'HTML',
 361                  'TABLE',
 362                  'TD',
 363                  'TH',
 364                  'MARQUEE',
 365                  'OBJECT',
 366                  'OL',
 367                  'SELECT',
 368                  'TEMPLATE',
 369                  'UL',
 370  
 371                  'math MI',
 372                  'math MO',
 373                  'math MN',
 374                  'math MS',
 375                  'math MTEXT',
 376                  'math ANNOTATION-XML',
 377  
 378                  'svg FOREIGNOBJECT',
 379                  'svg DESC',
 380                  'svg TITLE',
 381              )
 382          );
 383      }
 384  
 385      /**
 386       * Returns whether a particular element is in button scope.
 387       *
 388       * > The stack of open elements is said to have a particular element
 389       * > in button scope when it has that element in the specific scope
 390       * > consisting of the following element types:
 391       * >
 392       * >   - All the element types listed above for the has an element in scope algorithm.
 393       * >   - button in the HTML namespace
 394       *
 395       * @since 6.4.0
 396       * @since 6.7.0 Supports all required HTML elements.
 397       *
 398       * @see https://html.spec.whatwg.org/#has-an-element-in-button-scope
 399       *
 400       * @param string $tag_name Name of tag to check.
 401       * @return bool Whether given element is in scope.
 402       */
 403  	public function has_element_in_button_scope( string $tag_name ): bool {
 404          return $this->has_element_in_specific_scope(
 405              $tag_name,
 406              array(
 407                  'APPLET',
 408                  'BUTTON',
 409                  'CAPTION',
 410                  'HTML',
 411                  'TABLE',
 412                  'TD',
 413                  'TH',
 414                  'MARQUEE',
 415                  'OBJECT',
 416                  'SELECT',
 417                  'TEMPLATE',
 418  
 419                  'math MI',
 420                  'math MO',
 421                  'math MN',
 422                  'math MS',
 423                  'math MTEXT',
 424                  'math ANNOTATION-XML',
 425  
 426                  'svg FOREIGNOBJECT',
 427                  'svg DESC',
 428                  'svg TITLE',
 429              )
 430          );
 431      }
 432  
 433      /**
 434       * Returns whether a particular element is in table scope.
 435       *
 436       * > The stack of open elements is said to have a particular element
 437       * > in table scope when it has that element in the specific scope
 438       * > consisting of the following element types:
 439       * >
 440       * >   - html in the HTML namespace
 441       * >   - table in the HTML namespace
 442       * >   - template in the HTML namespace
 443       *
 444       * @since 6.4.0
 445       * @since 6.7.0 Full implementation.
 446       *
 447       * @see https://html.spec.whatwg.org/#has-an-element-in-table-scope
 448       *
 449       * @param string $tag_name Name of tag to check.
 450       * @return bool Whether given element is in scope.
 451       */
 452  	public function has_element_in_table_scope( string $tag_name ): bool {
 453          return $this->has_element_in_specific_scope(
 454              $tag_name,
 455              array(
 456                  'HTML',
 457                  'TABLE',
 458                  'TEMPLATE',
 459              )
 460          );
 461      }
 462  
 463      /**
 464       * Returns whether a particular element is in select scope.
 465       *
 466       * The "select scope" concept was removed from the HTML standard along with the
 467       * customizable `<select>` changes, so nothing is ever in select scope.
 468       *
 469       * > The stack of open elements is said to have a particular element in select scope when it has
 470       * > that element in the specific scope consisting of all element types except the following:
 471       * >   - optgroup in the HTML namespace
 472       * >   - option in the HTML namespace
 473       *
 474       * @since 6.4.0 Stub implementation (throws).
 475       * @since 6.7.0 Full implementation.
 476       *
 477       * @deprecated 7.1.0 This method is no longer part of the HTML standard.
 478       * @ignore
 479       *
 480       * @param string $tag_name Name of tag to check.
 481       * @return bool Always false; select scope no longer exists.
 482       */
 483  	public function has_element_in_select_scope( string $tag_name ): bool {
 484          _deprecated_function( __METHOD__, '7.1.0' );
 485  
 486          return false;
 487      }
 488  
 489      /**
 490       * Returns whether a P is in BUTTON scope.
 491       *
 492       * @since 6.4.0
 493       *
 494       * @see https://html.spec.whatwg.org/#has-an-element-in-button-scope
 495       *
 496       * @return bool Whether a P is in BUTTON scope.
 497       */
 498  	public function has_p_in_button_scope(): bool {
 499          return $this->has_p_in_button_scope;
 500      }
 501  
 502      /**
 503       * Pops a node off of the stack of open elements.
 504       *
 505       * @since 6.4.0
 506       *
 507       * @see https://html.spec.whatwg.org/#stack-of-open-elements
 508       *
 509       * @return bool Whether a node was popped off of the stack.
 510       */
 511  	public function pop(): bool {
 512          $item = array_pop( $this->stack );
 513          if ( null === $item ) {
 514              return false;
 515          }
 516  
 517          $this->after_element_pop( $item );
 518          return true;
 519      }
 520  
 521      /**
 522       * Pops nodes off of the stack of open elements until an HTML tag with the given name has been popped.
 523       *
 524       * @since 6.4.0
 525       *
 526       * @see WP_HTML_Open_Elements::pop
 527       *
 528       * @param string $html_tag_name Name of tag that needs to be popped off of the stack of open elements.
 529       * @return bool Whether a tag of the given name was found and popped off of the stack of open elements.
 530       */
 531  	public function pop_until( string $html_tag_name ): bool {
 532          foreach ( $this->walk_up() as $item ) {
 533              $this->pop();
 534  
 535              if ( 'html' !== $item->namespace ) {
 536                  continue;
 537              }
 538  
 539              if (
 540                  '(internal: H1 through H6 - do not use)' === $html_tag_name &&
 541                  in_array( $item->node_name, array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true )
 542              ) {
 543                  return true;
 544              }
 545  
 546              if ( $html_tag_name === $item->node_name ) {
 547                  return true;
 548              }
 549          }
 550  
 551          return false;
 552      }
 553  
 554      /**
 555       * Pushes a node onto the stack of open elements.
 556       *
 557       * @since 6.4.0
 558       *
 559       * @see https://html.spec.whatwg.org/#stack-of-open-elements
 560       *
 561       * @param WP_HTML_Token $stack_item Item to add onto stack.
 562       */
 563  	public function push( WP_HTML_Token $stack_item ): void {
 564          $this->stack[] = $stack_item;
 565          $this->after_element_push( $stack_item );
 566      }
 567  
 568      /**
 569       * Removes a specific node from the stack of open elements.
 570       *
 571       * @since 6.4.0
 572       *
 573       * @param WP_HTML_Token $token The node to remove from the stack of open elements.
 574       * @return bool Whether the node was found and removed from the stack of open elements.
 575       */
 576  	public function remove_node( WP_HTML_Token $token ): bool {
 577          foreach ( $this->walk_up() as $position_from_end => $item ) {
 578              if ( $token->bookmark_name !== $item->bookmark_name ) {
 579                  continue;
 580              }
 581  
 582              $position_from_start = $this->count() - $position_from_end - 1;
 583              array_splice( $this->stack, $position_from_start, 1 );
 584              $this->after_element_pop( $item );
 585              return true;
 586          }
 587  
 588          return false;
 589      }
 590  
 591  
 592      /**
 593       * Steps through the stack of open elements, starting with the top element
 594       * (added first) and walking downwards to the one added last.
 595       *
 596       * This generator function is designed to be used inside a "foreach" loop.
 597       *
 598       * Example:
 599       *
 600       *     $html = '<em><strong><a>We are here';
 601       *     foreach ( $stack->walk_down() as $node ) {
 602       *         echo "{$node->node_name} -> ";
 603       *     }
 604       *     > EM -> STRONG -> A ->
 605       *
 606       * To start with the most-recently added element and walk towards the top,
 607       * see WP_HTML_Open_Elements::walk_up().
 608       *
 609       * @since 6.4.0
 610       */
 611  	public function walk_down() {
 612          $count = count( $this->stack );
 613  
 614          for ( $i = 0; $i < $count; $i++ ) {
 615              yield $this->stack[ $i ];
 616          }
 617      }
 618  
 619      /**
 620       * Steps through the stack of open elements, starting with the bottom element
 621       * (added last) and walking upwards to the one added first.
 622       *
 623       * This generator function is designed to be used inside a "foreach" loop.
 624       *
 625       * Example:
 626       *
 627       *     $html = '<em><strong><a>We are here';
 628       *     foreach ( $stack->walk_up() as $node ) {
 629       *         echo "{$node->node_name} -> ";
 630       *     }
 631       *     > A -> STRONG -> EM ->
 632       *
 633       * To start with the first added element and walk towards the bottom,
 634       * see WP_HTML_Open_Elements::walk_down().
 635       *
 636       * @since 6.4.0
 637       * @since 6.5.0 Accepts $above_this_node to start traversal above a given node, if it exists.
 638       *
 639       * @param WP_HTML_Token|null $above_this_node Optional. Start traversing above this node,
 640       *                                            if provided and if the node exists.
 641       */
 642  	public function walk_up( ?WP_HTML_Token $above_this_node = null ) {
 643          $has_found_node = null === $above_this_node;
 644  
 645          for ( $i = count( $this->stack ) - 1; $i >= 0; $i-- ) {
 646              $node = $this->stack[ $i ];
 647  
 648              if ( ! $has_found_node ) {
 649                  $has_found_node = $node === $above_this_node;
 650                  continue;
 651              }
 652  
 653              yield $node;
 654          }
 655      }
 656  
 657      /*
 658       * Internal helpers.
 659       */
 660  
 661      /**
 662       * Updates internal flags after adding an element.
 663       *
 664       * Certain conditions (such as "has_p_in_button_scope") are maintained here as
 665       * flags that are only modified when adding and removing elements. This allows
 666       * the HTML Processor to quickly check for these conditions instead of iterating
 667       * over the open stack elements upon each new tag it encounters. These flags,
 668       * however, need to be maintained as items are added and removed from the stack.
 669       *
 670       * @since 6.4.0
 671       *
 672       * @param WP_HTML_Token $item Element that was added to the stack of open elements.
 673       */
 674  	public function after_element_push( WP_HTML_Token $item ): void {
 675          $namespaced_name = 'html' === $item->namespace
 676              ? $item->node_name
 677              : "{$item->namespace} {$item->node_name}";
 678  
 679          /*
 680           * When adding support for new elements, expand this switch to trap
 681           * cases where the precalculated value needs to change.
 682           */
 683          switch ( $namespaced_name ) {
 684              case 'APPLET':
 685              case 'BUTTON':
 686              case 'CAPTION':
 687              case 'HTML':
 688              case 'TABLE':
 689              case 'TD':
 690              case 'TH':
 691              case 'MARQUEE':
 692              case 'OBJECT':
 693              case 'SELECT':
 694              case 'TEMPLATE':
 695              case 'math MI':
 696              case 'math MO':
 697              case 'math MN':
 698              case 'math MS':
 699              case 'math MTEXT':
 700              case 'math ANNOTATION-XML':
 701              case 'svg FOREIGNOBJECT':
 702              case 'svg DESC':
 703              case 'svg TITLE':
 704                  $this->has_p_in_button_scope = false;
 705                  break;
 706  
 707              case 'P':
 708                  $this->has_p_in_button_scope = true;
 709                  break;
 710          }
 711  
 712          if ( null !== $this->push_handler ) {
 713              call_user_func( $this->push_handler, $item );
 714          }
 715      }
 716  
 717      /**
 718       * Updates internal flags after removing an element.
 719       *
 720       * Certain conditions (such as "has_p_in_button_scope") are maintained here as
 721       * flags that are only modified when adding and removing elements. This allows
 722       * the HTML Processor to quickly check for these conditions instead of iterating
 723       * over the open stack elements upon each new tag it encounters. These flags,
 724       * however, need to be maintained as items are added and removed from the stack.
 725       *
 726       * @since 6.4.0
 727       *
 728       * @param WP_HTML_Token $item Element that was removed from the stack of open elements.
 729       */
 730  	public function after_element_pop( WP_HTML_Token $item ): void {
 731          /*
 732           * When adding support for new elements, expand this switch to trap
 733           * cases where the precalculated value needs to change.
 734           */
 735          $namespaced_name = 'html' === $item->namespace
 736              ? $item->node_name
 737              : "{$item->namespace} {$item->node_name}";
 738  
 739          switch ( $namespaced_name ) {
 740              case 'APPLET':
 741              case 'BUTTON':
 742              case 'CAPTION':
 743              case 'HTML':
 744              case 'P':
 745              case 'TABLE':
 746              case 'TD':
 747              case 'TH':
 748              case 'MARQUEE':
 749              case 'OBJECT':
 750              case 'SELECT':
 751              case 'TEMPLATE':
 752              case 'math MI':
 753              case 'math MO':
 754              case 'math MN':
 755              case 'math MS':
 756              case 'math MTEXT':
 757              case 'math ANNOTATION-XML':
 758              case 'svg FOREIGNOBJECT':
 759              case 'svg DESC':
 760              case 'svg TITLE':
 761                  $this->has_p_in_button_scope = $this->has_element_in_button_scope( 'P' );
 762                  break;
 763          }
 764  
 765          if ( null !== $this->pop_handler ) {
 766              call_user_func( $this->pop_handler, $item );
 767          }
 768      }
 769  
 770      /**
 771       * Clear the stack back to a table context.
 772       *
 773       * > When the steps above require the UA to clear the stack back to a table context, it means
 774       * > that the UA must, while the current node is not a table, template, or html element, pop
 775       * > elements from the stack of open elements.
 776       *
 777       * @see https://html.spec.whatwg.org/multipage/parsing.html#clear-the-stack-back-to-a-table-context
 778       *
 779       * @since 6.7.0
 780       */
 781  	public function clear_to_table_context(): void {
 782          foreach ( $this->walk_up() as $item ) {
 783              if (
 784                  'TABLE' === $item->node_name ||
 785                  'TEMPLATE' === $item->node_name ||
 786                  'HTML' === $item->node_name
 787              ) {
 788                  break;
 789              }
 790              $this->pop();
 791          }
 792      }
 793  
 794      /**
 795       * Clear the stack back to a table body context.
 796       *
 797       * > When the steps above require the UA to clear the stack back to a table body context, it
 798       * > means that the UA must, while the current node is not a tbody, tfoot, thead, template, or
 799       * > html element, pop elements from the stack of open elements.
 800       *
 801       * @see https://html.spec.whatwg.org/multipage/parsing.html#clear-the-stack-back-to-a-table-body-context
 802       *
 803       * @since 6.7.0
 804       */
 805  	public function clear_to_table_body_context(): void {
 806          foreach ( $this->walk_up() as $item ) {
 807              if (
 808                  'TBODY' === $item->node_name ||
 809                  'TFOOT' === $item->node_name ||
 810                  'THEAD' === $item->node_name ||
 811                  'TEMPLATE' === $item->node_name ||
 812                  'HTML' === $item->node_name
 813              ) {
 814                  break;
 815              }
 816              $this->pop();
 817          }
 818      }
 819  
 820      /**
 821       * Clear the stack back to a table row context.
 822       *
 823       * > When the steps above require the UA to clear the stack back to a table row context, it
 824       * > means that the UA must, while the current node is not a tr, template, or html element, pop
 825       * > elements from the stack of open elements.
 826       *
 827       * @see https://html.spec.whatwg.org/multipage/parsing.html#clear-the-stack-back-to-a-table-row-context
 828       *
 829       * @since 6.7.0
 830       */
 831  	public function clear_to_table_row_context(): void {
 832          foreach ( $this->walk_up() as $item ) {
 833              if (
 834                  'TR' === $item->node_name ||
 835                  'TEMPLATE' === $item->node_name ||
 836                  'HTML' === $item->node_name
 837              ) {
 838                  break;
 839              }
 840              $this->pop();
 841          }
 842      }
 843  
 844      /**
 845       * Wakeup magic method.
 846       *
 847       * @since 6.6.0
 848       */
 849  	public function __wakeup() {
 850          throw new \LogicException( __CLASS__ . ' should never be unserialized' );
 851      }
 852  }


Generated : Thu Jul 16 08:20:16 2026 Cross-referenced by PHPXref