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