<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[58806] trunk: HTML API: Add TABLE support in HTML Processor.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/58806">58806</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/58806","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dmsnell</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-07-24 18:39:46 +0000 (Wed, 24 Jul 2024)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>HTML API: Add TABLE support in HTML Processor.

As part of work to add more spec support to the HTML API, this patch adds
support for various table-related insertion modes. This includes support
for tables, table rows, table cells, table column groups, etc...

Developed in https://github.com/wordpress/wordpress-develop/pull/6040
Discussed in https://core.trac.wordpress.org/ticket/61576

Props: dmsnell, jonsurrell.
See <a href="https://core.trac.wordpress.org/ticket/61576">#61576</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludeshtmlapiclasswphtmlopenelementsphp">trunk/src/wp-includes/html-api/class-wp-html-open-elements.php</a></li>
<li><a href="#trunksrcwpincludeshtmlapiclasswphtmlprocessorphp">trunk/src/wp-includes/html-api/class-wp-html-processor.php</a></li>
<li><a href="#trunktestsphpunittestshtmlapiwpHtmlProcessorBreadcrumbsphp">trunk/tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php</a></li>
<li><a href="#trunktestsphpunittestshtmlapiwpHtmlProcessorSemanticRulesphp">trunk/tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludeshtmlapiclasswphtmlopenelementsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/html-api/class-wp-html-open-elements.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/html-api/class-wp-html-open-elements.php    2024-07-24 17:30:22 UTC (rev 58805)
+++ trunk/src/wp-includes/html-api/class-wp-html-open-elements.php      2024-07-24 18:39:46 UTC (rev 58806)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -721,6 +721,80 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Clear the stack back to a table context.
+        *
+        * > When the steps above require the UA to clear the stack back to a table context, it means
+        * > that the UA must, while the current node is not a table, template, or html element, pop
+        * > elements from the stack of open elements.
+        *
+        * @see https://html.spec.whatwg.org/multipage/parsing.html#clear-the-stack-back-to-a-table-context
+        *
+        * @since 6.7.0
+        */
+       public function clear_to_table_context(): void {
+               foreach ( $this->walk_up() as $item ) {
+                       if (
+                               'TABLE' === $item->node_name ||
+                               'TEMPLATE' === $item->node_name ||
+                               'HTML' === $item->node_name
+                       ) {
+                               break;
+                       }
+                       $this->pop();
+               }
+       }
+
+       /**
+        * Clear the stack back to a table body context.
+        *
+        * > When the steps above require the UA to clear the stack back to a table body context, it
+        * > means that the UA must, while the current node is not a tbody, tfoot, thead, template, or
+        * > html element, pop elements from the stack of open elements.
+        *
+        * @see https://html.spec.whatwg.org/multipage/parsing.html#clear-the-stack-back-to-a-table-body-context
+        *
+        * @since 6.7.0
+        */
+       public function clear_to_table_body_context(): void {
+               foreach ( $this->walk_up() as $item ) {
+                       if (
+                               'TBODY' === $item->node_name ||
+                               'TFOOT' === $item->node_name ||
+                               'THEAD' === $item->node_name ||
+                               'TEMPLATE' === $item->node_name ||
+                               'HTML' === $item->node_name
+                       ) {
+                               break;
+                       }
+                       $this->pop();
+               }
+       }
+
+       /**
+        * Clear the stack back to a table row context.
+        *
+        * > When the steps above require the UA to clear the stack back to a table row context, it
+        * > means that the UA must, while the current node is not a tr, template, or html element, pop
+        * > elements from the stack of open elements.
+        *
+        * @see https://html.spec.whatwg.org/multipage/parsing.html#clear-the-stack-back-to-a-table-row-context
+        *
+        * @since 6.7.0
+        */
+       public function clear_to_table_row_context(): void {
+               foreach ( $this->walk_up() as $item ) {
+                       if (
+                               'TR' === $item->node_name ||
+                               'TEMPLATE' === $item->node_name ||
+                               'HTML' === $item->node_name
+                       ) {
+                               break;
+                       }
+                       $this->pop();
+               }
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Wakeup magic method.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.6.0
</span></span></pre></div>
<a id="trunksrcwpincludeshtmlapiclasswphtmlprocessorphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/html-api/class-wp-html-processor.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/html-api/class-wp-html-processor.php        2024-07-24 17:30:22 UTC (rev 58805)
+++ trunk/src/wp-includes/html-api/class-wp-html-processor.php  2024-07-24 18:39:46 UTC (rev 58806)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1786,6 +1786,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         * > A start tag whose tag name is "table"
</span><span class="cx" style="display: block; padding: 0 10px">                         */
</span><span class="cx" style="display: block; padding: 0 10px">                        case '+TABLE':
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                /*
+                                * > If the Document is not set to quirks mode, and the stack of open elements
+                                * > has a p element in button scope, then close a p element.
+                                */
</ins><span class="cx" style="display: block; padding: 0 10px">                                 if (
</span><span class="cx" style="display: block; padding: 0 10px">                                        WP_HTML_Processor_State::QUIRKS_MODE !== $this->state->document_mode &&
</span><span class="cx" style="display: block; padding: 0 10px">                                        $this->state->stack_of_open_elements->has_p_in_button_scope()
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2117,7 +2121,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * This internal function performs the 'in table' insertion mode
</span><span class="cx" style="display: block; padding: 0 10px">         * logic for the generalized WP_HTML_Processor::step() function.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @since 6.7.0 Stub implementation.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @since 6.7.0
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2127,7 +2131,245 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return bool Whether an element was found.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        private function step_in_table(): bool {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->bail( 'No support for parsing in the ' . WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE . ' state.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $token_name = $this->get_token_name();
+               $token_type = $this->get_token_type();
+               $op_sigil   = '#tag' === $token_type ? ( parent::is_tag_closer() ? '-' : '+' ) : '';
+               $op         = "{$op_sigil}{$token_name}";
+
+               switch ( $op ) {
+                       /*
+                        * > A character token, if the current node is table,
+                        * > tbody, template, tfoot, thead, or tr element
+                        */
+                       case '#text':
+                               $current_node      = $this->state->stack_of_open_elements->current_node();
+                               $current_node_name = $current_node ? $current_node->node_name : null;
+                               if (
+                                       $current_node_name && (
+                                               'TABLE' === $current_node_name ||
+                                               'TBODY' === $current_node_name ||
+                                               'TEMPLATE' === $current_node_name ||
+                                               'TFOOT' === $current_node_name ||
+                                               'THEAD' === $current_node_name ||
+                                               'TR' === $current_node_name
+                                       )
+                               ) {
+                                       $text = $this->get_modifiable_text();
+                                       /*
+                                        * If the text is empty after processing HTML entities and stripping
+                                        * U+0000 NULL bytes then ignore the token.
+                                        */
+                                       if ( '' === $text ) {
+                                               return $this->step();
+                                       }
+
+                                       /*
+                                        * This follows the rules for "in table text" insertion mode.
+                                        *
+                                        * Whitespace-only text nodes are inserted in-place. Otherwise
+                                        * foster parenting is enabled and the nodes would be
+                                        * inserted out-of-place.
+                                        *
+                                        * > If any of the tokens in the pending table character tokens
+                                        * > list are character tokens that are not ASCII whitespace,
+                                        * > then this is a parse error: reprocess the character tokens
+                                        * > in the pending table character tokens list using the rules
+                                        * > given in the "anything else" entry in the "in table"
+                                        * > insertion mode.
+                                        * >
+                                        * > Otherwise, insert the characters given by the pending table
+                                        * > character tokens list.
+                                        *
+                                        * @see https://html.spec.whatwg.org/#parsing-main-intabletext
+                                        */
+                                       if ( strlen( $text ) === strspn( $text, " \t\f\r\n" ) ) {
+                                               $this->insert_html_element( $this->state->current_token );
+                                               return true;
+                                       }
+
+                                       // Non-whitespace would trigger fostering, unsupported at this time.
+                                       $this->bail( 'Foster parenting is not supported.' );
+                                       break;
+                               }
+                               break;
+
+                       /*
+                        * > A comment token
+                        */
+                       case '#comment':
+                       case '#funky-comment':
+                       case '#presumptuous-tag':
+                               $this->insert_html_element( $this->state->current_token );
+                               return true;
+
+                       /*
+                        * > A DOCTYPE token
+                        */
+                       case 'html':
+                               // Parse error: ignore the token.
+                               return $this->step();
+
+                       /*
+                        * > A start tag whose tag name is "caption"
+                        */
+                       case '+CAPTION':
+                               $this->state->stack_of_open_elements->clear_to_table_context();
+                               $this->state->active_formatting_elements->insert_marker();
+                               $this->insert_html_element( $this->state->current_token );
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_CAPTION;
+                               return true;
+
+                       /*
+                        * > A start tag whose tag name is "colgroup"
+                        */
+                       case '+COLGROUP':
+                               $this->state->stack_of_open_elements->clear_to_table_context();
+                               $this->insert_html_element( $this->state->current_token );
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_COLUMN_GROUP;
+                               return true;
+
+                       /*
+                        * > A start tag whose tag name is "col"
+                        */
+                       case '+COL':
+                               $this->state->stack_of_open_elements->clear_to_table_context();
+
+                               /*
+                                * > Insert an HTML element for a "colgroup" start tag token with no attributes,
+                                * > then switch the insertion mode to "in column group".
+                                */
+                               $this->insert_virtual_node( 'COLGROUP' );
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_COLUMN_GROUP;
+                               return $this->step( self::REPROCESS_CURRENT_NODE );
+
+                       /*
+                        * > A start tag whose tag name is one of: "tbody", "tfoot", "thead"
+                        */
+                       case '+TBODY':
+                       case '+TFOOT':
+                       case '+THEAD':
+                               $this->state->stack_of_open_elements->clear_to_table_context();
+                               $this->insert_html_element( $this->state->current_token );
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY;
+                               return true;
+
+                       /*
+                        * > A start tag whose tag name is one of: "td", "th", "tr"
+                        */
+                       case '+TD':
+                       case '+TH':
+                       case '+TR':
+                               $this->state->stack_of_open_elements->clear_to_table_context();
+                               /*
+                                * > Insert an HTML element for a "tbody" start tag token with no attributes,
+                                * > then switch the insertion mode to "in table body".
+                                */
+                               $this->insert_virtual_node( 'TBODY' );
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY;
+                               return $this->step( self::REPROCESS_CURRENT_NODE );
+
+                       /*
+                        * > A start tag whose tag name is "table"
+                        *
+                        * This tag in the IN TABLE insertion mode is a parse error.
+                        */
+                       case '+TABLE':
+                               if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TABLE' ) ) {
+                                       return $this->step();
+                               }
+
+                               $this->state->stack_of_open_elements->pop_until( 'TABLE' );
+                               $this->reset_insertion_mode();
+                               return $this->step( self::REPROCESS_CURRENT_NODE );
+
+                       /*
+                        * > An end tag whose tag name is "table"
+                        */
+                       case '-TABLE':
+                               if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TABLE' ) ) {
+                                       // @todo Indicate a parse error once it's possible.
+                                       return $this->step();
+                               }
+
+                               $this->state->stack_of_open_elements->pop_until( 'TABLE' );
+                               $this->reset_insertion_mode();
+                               return true;
+
+                       /*
+                        * > An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr"
+                        */
+                       case '-BODY':
+                       case '-CAPTION':
+                       case '-COL':
+                       case '-COLGROUP':
+                       case '-HTML':
+                       case '-TBODY':
+                       case '-TD':
+                       case '-TFOOT':
+                       case '-TH':
+                       case '-THEAD':
+                       case '-TR':
+                               // Parse error: ignore the token.
+                               return $this->step();
+
+                       /*
+                        * > A start tag whose tag name is one of: "style", "script", "template"
+                        * > An end tag whose tag name is "template"
+                        */
+                       case '+STYLE':
+                       case '+SCRIPT':
+                       case '+TEMPLATE':
+                       case '-TEMPLATE':
+                               /*
+                                * > Process the token using the rules for the "in head" insertion mode.
+                                */
+                               return $this->step_in_head();
+
+                       /*
+                        * > A start tag whose tag name is "input"
+                        *
+                        * > If the token does not have an attribute with the name "type", or if it does, but
+                        * > that attribute's value is not an ASCII case-insensitive match for the string
+                        * > "hidden", then: act as described in the "anything else" entry below.
+                        */
+                       case '+INPUT':
+                               $type_attribute = $this->get_attribute( 'type' );
+                               if ( ! is_string( $type_attribute ) || 'hidden' !== strtolower( $type_attribute ) ) {
+                                       goto anything_else;
+                               }
+                               // @todo Indicate a parse error once it's possible.
+                               $this->insert_html_element( $this->state->current_token );
+                               return true;
+
+                       /*
+                        * > A start tag whose tag name is "form"
+                        *
+                        * This tag in the IN TABLE insertion mode is a parse error.
+                        */
+                       case '+FORM':
+                               if (
+                                       $this->state->stack_of_open_elements->has_element_in_scope( 'TEMPLATE' ) ||
+                                       isset( $this->state->form_element )
+                               ) {
+                                       return $this->step();
+                               }
+
+                               // This FORM is special because it immediately closes and cannot have other children.
+                               $this->insert_html_element( $this->state->current_token );
+                               $this->state->form_element = $this->state->current_token;
+                               $this->state->stack_of_open_elements->pop();
+                               return true;
+               }
+
+               /*
+                * > Anything else
+                * > Parse error. Enable foster parenting, process the token using the rules for the
+                * > "in body" insertion mode, and then disable foster parenting.
+                *
+                * @todo Indicate a parse error once it's possible.
+                */
+               anything_else:
+               $this->bail( 'Foster parenting is not supported.' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2193,7 +2435,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * This internal function performs the 'in table body' insertion mode
</span><span class="cx" style="display: block; padding: 0 10px">         * logic for the generalized WP_HTML_Processor::step() function.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @since 6.7.0 Stub implementation.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @since 6.7.0
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2203,7 +2445,97 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return bool Whether an element was found.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        private function step_in_table_body(): bool {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->bail( 'No support for parsing in the ' . WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY . ' state.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $tag_name = $this->get_tag();
+               $op_sigil = $this->is_tag_closer() ? '-' : '+';
+               $op       = "{$op_sigil}{$tag_name}";
+
+               switch ( $op ) {
+                       /*
+                        * > A start tag whose tag name is "tr"
+                        */
+                       case '+TR':
+                               $this->state->stack_of_open_elements->clear_to_table_body_context();
+                               $this->insert_html_element( $this->state->current_token );
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_ROW;
+                               return true;
+
+                       /*
+                        * > A start tag whose tag name is one of: "th", "td"
+                        */
+                       case '+TH':
+                       case '+TD':
+                               // @todo Indicate a parse error once it's possible.
+                               $this->state->stack_of_open_elements->clear_to_table_body_context();
+                               $this->insert_virtual_node( 'TR' );
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_ROW;
+                               return $this->step( self::REPROCESS_CURRENT_NODE );
+
+                       /*
+                        * > An end tag whose tag name is one of: "tbody", "tfoot", "thead"
+                        */
+                       case '-TBODY':
+                       case '-TFOOT':
+                       case '-THEAD':
+                               /*
+                                * @todo This needs to check if the element in scope is an HTML element, meaning that
+                                *       when SVG and MathML support is added, this needs to differentiate between an
+                                *       HTML element of the given name, such as `<center>`, and a foreign element of
+                                *       the same given name.
+                                */
+                               if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( $tag_name ) ) {
+                                       // Parse error: ignore the token.
+                                       return $this->step();
+                               }
+
+                               $this->state->stack_of_open_elements->clear_to_table_body_context();
+                               $this->state->stack_of_open_elements->pop();
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE;
+                               return true;
+
+                       /*
+                        * > A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "tfoot", "thead"
+                        * > An end tag whose tag name is "table"
+                        */
+                       case '+CAPTION':
+                       case '+COL':
+                       case '+COLGROUP':
+                       case '+TBODY':
+                       case '+TFOOT':
+                       case '+THEAD':
+                       case '-TABLE':
+                               if (
+                                       ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TBODY' ) &&
+                                       ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'THEAD' ) &&
+                                       ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TFOOT' )
+                               ) {
+                                       // Parse error: ignore the token.
+                                       return $this->step();
+                               }
+                               $this->state->stack_of_open_elements->clear_to_table_body_context();
+                               $this->state->stack_of_open_elements->pop();
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE;
+                               return $this->step( self::REPROCESS_CURRENT_NODE );
+
+                       /*
+                        * > An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html", "td", "th", "tr"
+                        */
+                       case '-BODY':
+                       case '-CAPTION':
+                       case '-COL':
+                       case '-COLGROUP':
+                       case '-HTML':
+                       case '-TD':
+                       case '-TH':
+                       case '-TR':
+                               // Parse error: ignore the token.
+                               return $this->step();
+               }
+
+               /*
+                * > Anything else
+                * > Process the token using the rules for the "in table" insertion mode.
+                */
+               return $this->step_in_table();
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2212,7 +2544,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * This internal function performs the 'in row' insertion mode
</span><span class="cx" style="display: block; padding: 0 10px">         * logic for the generalized WP_HTML_Processor::step() function.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @since 6.7.0 Stub implementation.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @since 6.7.0
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2222,7 +2554,104 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return bool Whether an element was found.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        private function step_in_row(): bool {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->bail( 'No support for parsing in the ' . WP_HTML_Processor_State::INSERTION_MODE_IN_ROW . ' state.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $tag_name = $this->get_tag();
+               $op_sigil = $this->is_tag_closer() ? '-' : '+';
+               $op       = "{$op_sigil}{$tag_name}";
+
+               switch ( $op ) {
+                       /*
+                        * > A start tag whose tag name is one of: "th", "td"
+                        */
+                       case '+TH':
+                       case '+TD':
+                               $this->state->stack_of_open_elements->clear_to_table_row_context();
+                               $this->insert_html_element( $this->state->current_token );
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_CELL;
+                               $this->state->active_formatting_elements->insert_marker();
+                               return true;
+
+                       /*
+                        * > An end tag whose tag name is "tr"
+                        */
+                       case '-TR':
+                               if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TR' ) ) {
+                                       // Parse error: ignore the token.
+                                       return $this->step();
+                               }
+
+                               $this->state->stack_of_open_elements->clear_to_table_row_context();
+                               $this->state->stack_of_open_elements->pop();
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY;
+                               return true;
+
+                       /*
+                        * > A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "tfoot", "thead", "tr"
+                        * > An end tag whose tag name is "table"
+                        */
+                       case '+CAPTION':
+                       case '+COL':
+                       case '+COLGROUP':
+                       case '+TBODY':
+                       case '+TFOOT':
+                       case '+THEAD':
+                       case '+TR':
+                       case '-TABLE':
+                               if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TR' ) ) {
+                                       // Parse error: ignore the token.
+                                       return $this->step();
+                               }
+
+                               $this->state->stack_of_open_elements->clear_to_table_row_context();
+                               $this->state->stack_of_open_elements->pop();
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY;
+                               return $this->step( self::REPROCESS_CURRENT_NODE );
+
+                       /*
+                        * > An end tag whose tag name is one of: "tbody", "tfoot", "thead"
+                        */
+                       case '-TBODY':
+                       case '-TFOOT':
+                       case '-THEAD':
+                               /*
+                                * @todo This needs to check if the element in scope is an HTML element, meaning that
+                                *       when SVG and MathML support is added, this needs to differentiate between an
+                                *       HTML element of the given name, such as `<center>`, and a foreign element of
+                                *       the same given name.
+                                */
+                               if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( $tag_name ) ) {
+                                       // Parse error: ignore the token.
+                                       return $this->step();
+                               }
+
+                               if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( 'TR' ) ) {
+                                       // Ignore the token.
+                                       return $this->step();
+                               }
+
+                               $this->state->stack_of_open_elements->clear_to_table_row_context();
+                               $this->state->stack_of_open_elements->pop();
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE_BODY;
+                               return $this->step( self::REPROCESS_CURRENT_NODE );
+
+                       /*
+                        * > An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html", "td", "th"
+                        */
+                       case '-BODY':
+                       case '-CAPTION':
+                       case '-COL':
+                       case '-COLGROUP':
+                       case '-HTML':
+                       case '-TD':
+                       case '-TH':
+                               // Parse error: ignore the token.
+                               return $this->step();
+               }
+
+               /*
+                * > Anything else
+                * >   Process the token using the rules for the "in table" insertion mode.
+                */
+               return $this->step_in_table();
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2231,7 +2660,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * This internal function performs the 'in cell' insertion mode
</span><span class="cx" style="display: block; padding: 0 10px">         * logic for the generalized WP_HTML_Processor::step() function.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @since 6.7.0 Stub implementation.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @since 6.7.0
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2241,7 +2670,104 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return bool Whether an element was found.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        private function step_in_cell(): bool {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->bail( 'No support for parsing in the ' . WP_HTML_Processor_State::INSERTION_MODE_IN_CELL . ' state.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $tag_name = $this->get_tag();
+               $op_sigil = $this->is_tag_closer() ? '-' : '+';
+               $op       = "{$op_sigil}{$tag_name}";
+
+               switch ( $op ) {
+                       /*
+                        * > An end tag whose tag name is one of: "td", "th"
+                        */
+                       case '-TD':
+                       case '-TH':
+                               /*
+                                * @todo This needs to check if the element in scope is an HTML element, meaning that
+                                *       when SVG and MathML support is added, this needs to differentiate between an
+                                *       HTML element of the given name, such as `<center>`, and a foreign element of
+                                *       the same given name.
+                                */
+                               if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( $tag_name ) ) {
+                                       // Parse error: ignore the token.
+                                       return $this->step();
+                               }
+
+                               $this->generate_implied_end_tags();
+
+                               /*
+                                * @todo This needs to check if the current node is an HTML element, meaning that
+                                *       when SVG and MathML support is added, this needs to differentiate between an
+                                *       HTML element of the given name, such as `<center>`, and a foreign element of
+                                *       the same given name.
+                                */
+                               if ( ! $this->state->stack_of_open_elements->current_node_is( $tag_name ) ) {
+                                       // @todo Indicate a parse error once it's possible.
+                               }
+
+                               $this->state->stack_of_open_elements->pop_until( $tag_name );
+                               $this->state->active_formatting_elements->clear_up_to_last_marker();
+                               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_ROW;
+                               return true;
+
+                       /*
+                        * > A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "td",
+                        * > "tfoot", "th", "thead", "tr"
+                        */
+                       case '+CAPTION':
+                       case '+COL':
+                       case '+COLGROUP':
+                       case '+TBODY':
+                       case '+TD':
+                       case '+TFOOT':
+                       case '+TH':
+                       case '+THEAD':
+                       case '+TR':
+                               /*
+                                * > Assert: The stack of open elements has a td or th element in table scope.
+                                *
+                                * Nothing to do here, except to verify in tests that this never appears.
+                                */
+
+                               $this->close_cell();
+                               return $this->step( self::REPROCESS_CURRENT_NODE );
+
+                       /*
+                        * > An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html"
+                        */
+                       case '-BODY':
+                       case '-CAPTION':
+                       case '-COL':
+                       case '-COLGROUP':
+                       case '-HTML':
+                               // Parse error: ignore the token.
+                               return $this->step();
+
+                       /*
+                        * > An end tag whose tag name is one of: "table", "tbody", "tfoot", "thead", "tr"
+                        */
+                       case '-TABLE':
+                       case '-TBODY':
+                       case '-TFOOT':
+                       case '-THEAD':
+                       case '-TR':
+                               /*
+                                * @todo This needs to check if the element in scope is an HTML element, meaning that
+                                *       when SVG and MathML support is added, this needs to differentiate between an
+                                *       HTML element of the given name, such as `<center>`, and a foreign element of
+                                *       the same given name.
+                                */
+                               if ( ! $this->state->stack_of_open_elements->has_element_in_table_scope( $tag_name ) ) {
+                                       // Parse error: ignore the token.
+                                       return $this->step();
+                               }
+                               $this->close_cell();
+                               return $this->step( self::REPROCESS_CURRENT_NODE );
+               }
+
+               /*
+                * > Anything else
+                * >   Process the token using the rules for the "in body" insertion mode.
+                */
+               return $this->step_in_body();
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3577,6 +4103,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Runs the "close the cell" algorithm.
+        *
+        * > Where the steps above say to close the cell, they mean to run the following algorithm:
+        * >   1. Generate implied end tags.
+        * >   2. If the current node is not now a td element or a th element, then this is a parse error.
+        * >   3. Pop elements from the stack of open elements stack until a td element or a th element has been popped from the stack.
+        * >   4. Clear the list of active formatting elements up to the last marker.
+        * >   5. Switch the insertion mode to "in row".
+        *
+        * @see https://html.spec.whatwg.org/multipage/parsing.html#close-the-cell
+        *
+        * @since 6.7.0
+        */
+       private function close_cell(): void {
+               $this->generate_implied_end_tags();
+               // @todo Parse error if the current node is a "td" or "th" element.
+               foreach ( $this->state->stack_of_open_elements->walk_up() as $element ) {
+                       $this->state->stack_of_open_elements->pop();
+                       if ( 'TD' === $element->node_name || 'TH' === $element->node_name ) {
+                               break;
+                       }
+               }
+               $this->state->active_formatting_elements->clear_up_to_last_marker();
+               $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_ROW;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Inserts an HTML element on the stack of open elements.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.4.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3589,6 +4142,24 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->state->stack_of_open_elements->push( $token );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Inserts a virtual element on the stack of open elements.
+        *
+        * @since 6.7.0
+        *
+        * @param string      $token_name    Name of token to create and insert into the stack of open elements.
+        * @param string|null $bookmark_name Optional. Name to give bookmark for created virtual node.
+        *                                   Defaults to auto-creating a bookmark name.
+        */
+       private function insert_virtual_node( $token_name, $bookmark_name = null ): void {
+               $here = $this->bookmarks[ $this->state->current_token->bookmark_name ];
+               $name = $bookmark_name ?? $this->bookmark_token();
+
+               $this->bookmarks[ $name ] = new WP_HTML_Span( $here->start, 0 );
+
+               $this->insert_html_element( new WP_HTML_Token( $name, $token_name, false ) );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /*
</span><span class="cx" style="display: block; padding: 0 10px">         * HTML Specification Helpers
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span></span></pre></div>
<a id="trunktestsphpunittestshtmlapiwpHtmlProcessorBreadcrumbsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php 2024-07-24 17:30:22 UTC (rev 58805)
+++ trunk/tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php   2024-07-24 18:39:46 UTC (rev 58806)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -181,9 +181,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'BASE',
</span><span class="cx" style="display: block; padding: 0 10px">                        'BGSOUND', // Deprecated; self-closing if self-closing flag provided, otherwise normal.
</span><span class="cx" style="display: block; padding: 0 10px">                        'BODY',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'CAPTION',
-                       'COL',
-                       'COLGROUP',
</del><span class="cx" style="display: block; padding: 0 10px">                         'FRAME',
</span><span class="cx" style="display: block; padding: 0 10px">                        'FRAMESET',
</span><span class="cx" style="display: block; padding: 0 10px">                        'HEAD',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -198,15 +195,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'SCRIPT',
</span><span class="cx" style="display: block; padding: 0 10px">                        'STYLE',
</span><span class="cx" style="display: block; padding: 0 10px">                        'SVG',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'TBODY',
-                       'TD',
</del><span class="cx" style="display: block; padding: 0 10px">                         'TEMPLATE',
</span><span class="cx" style="display: block; padding: 0 10px">                        'TEXTAREA',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'TFOOT',
-                       'TH',
-                       'THEAD',
</del><span class="cx" style="display: block; padding: 0 10px">                         'TITLE',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'TR',
</del><span class="cx" style="display: block; padding: 0 10px">                         'XMP', // Deprecated, use PRE instead.
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunittestshtmlapiwpHtmlProcessorSemanticRulesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php       2024-07-24 17:30:22 UTC (rev 58805)
+++ trunk/tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php 2024-07-24 18:39:46 UTC (rev 58806)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -424,4 +424,28 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertFalse( $processor->is_tag_closer(), 'Should have treated the tag as an opening tag.' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNull( $processor->get_attribute_names_with_prefix( '' ), 'Should have ignored any attributes on the tag.' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /*******************************************************************
+        * RULES FOR "IN TABLE" MODE
+        *******************************************************************/
+
+       /**
+        * Ensure that form elements in tables (but not cells) are immediately popped off the stack.
+        *
+        * @ticket 61576
+        */
+       public function test_table_form_element_immediately_popped() {
+               $processor = WP_HTML_Processor::create_fragment( '<table><form><!--comment-->' );
+
+               // There should be a FORM opener and a (virtual) FORM closer.
+               $this->assertTrue( $processor->next_tag( 'FORM' ) );
+               $this->assertTrue( $processor->next_token() );
+               $this->assertSame( 'FORM', $processor->get_token_name() );
+               $this->assertTrue( $processor->is_tag_closer() );
+
+               // Followed by the comment token.
+               $this->assertTrue( $processor->next_token() );
+               $this->assertSame( '#comment', $processor->get_token_name() );
+               $this->assertsame( array( 'HTML', 'BODY', 'TABLE', '#comment' ), $processor->get_breadcrumbs() );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>