Skip to content

Commit

Permalink
Work on table support
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Feb 2, 2024
1 parent 41bf1e4 commit e5d0d6c
Showing 1 changed file with 195 additions and 12 deletions.
207 changes: 195 additions & 12 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,9 @@ public function step( $node_to_process = self::PROCESS_NEXT_NODE ) {
case WP_HTML_Processor_State::INSERTION_MODE_IN_SELECT:
return $this->step_in_select();

case WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE:
return $this->step_in_table();

default:
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
Expand Down Expand Up @@ -968,6 +971,17 @@ private function step_in_body() {
$this->run_adoption_agency_algorithm();
return true;

/*
* > A start tag whose tag name is "table"
*/
case '+TABLE':
if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) {
$this->close_a_p_element();
}
$this->insert_html_element( $this->state->current_token );
$this->state->frameset_ok = false;
$this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_TABLE;

/*

Check failure on line 985 in src/wp-includes/html-api/class-wp-html-processor.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 4 tabs, found 3
* > An end tag whose tag name is "br"

Check failure on line 986 in src/wp-includes/html-api/class-wp-html-processor.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 4 tabs, found 3 tabs and 1 spaces
* > Parse error. Drop the attributes from the token, and act as described in the next

Check failure on line 987 in src/wp-includes/html-api/class-wp-html-processor.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 4 tabs, found 3 tabs and 1 spaces
Expand Down Expand Up @@ -1072,6 +1086,22 @@ private function step_in_body() {
$this->reconstruct_active_formatting_elements();
$this->insert_html_element( $this->state->current_token );
return true;

/*
* > A start tag whose tag name is one of: "caption", "col", "colgroup", "frame", "head", "tbody", "td", "tfoot", "th", "thead", "tr"
*/
case 'CAPTION':
case 'COL':
case 'COLGROUP':
case 'FRAME':
case 'HEAD':
case 'TBODY':
case 'TD':
case 'TFOOT':
case 'TH':
case 'THEAD':
case 'TR':
return $this->step();
}

/*
Expand All @@ -1096,13 +1126,8 @@ private function step_in_body() {
case 'BASEFONT':
case 'BGSOUND':
case 'BODY':
case 'CAPTION':
case 'COL':
case 'COLGROUP':
case 'FORM':
case 'FRAME':
case 'FRAMESET':
case 'HEAD':
case 'HTML':
case 'IFRAME':
case 'LINK':
Expand All @@ -1123,16 +1148,9 @@ private function step_in_body() {
case 'SCRIPT':
case 'STYLE':
case 'SVG':
case 'TABLE':
case 'TBODY':
case 'TD':
case 'TEMPLATE':
case 'TEXTAREA':
case 'TFOOT':
case 'TH':
case 'THEAD':
case 'TITLE':
case 'TR':
case 'XMP':
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "Cannot process {$tag_name} element." );
Expand Down Expand Up @@ -1293,6 +1311,148 @@ private function step_in_select() {
throw new WP_HTML_Unsupported_Exception( "Cannot process {$tag_name} element." );
}

/**
* Parses next element in the 'in table' insertion mode.
*
* This internal function performs the 'in table' insertion mode
* logic for the generalized WP_HTML_Processor::step() function.
*
* @since 6.5.0
*
* @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input.
*
* @see https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intable
* @see WP_HTML_Processor::step
*
* @return bool Whether an element was found.
*/
private function step_in_table() {
$tag_name = $this->get_tag();
$op_sigil = $this->is_tag_closer() ? '-' : '+';
$op = "{$op_sigil}{$tag_name}";

switch ( $op ) {
/*
* > A character token, if the current node is table, tbody, template, tfoot, thead, or tr element
*/
/*
* > A comment token
*/
/*
* > A DOCTYPE token
*/
/*
* > A start tag whose tag name is "caption"
*/
case "+CAPTION":

Check failure on line 1347 in src/wp-includes/html-api/class-wp-html-processor.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

String "+CAPTION" does not require double quotes; use single quotes instead
$this->clear_stack_to_table_context();
break;

/*
* > A start tag whose tag name is "colgroup"
*/
case "+COLGROUP":

Check failure on line 1354 in src/wp-includes/html-api/class-wp-html-processor.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

String "+COLGROUP" does not require double quotes; use single quotes instead
$this->clear_stack_to_table_context();
break;

/*
* > A start tag whose tag name is "col"
*/
case "+COL":

Check failure on line 1361 in src/wp-includes/html-api/class-wp-html-processor.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

String "+COL" does not require double quotes; use single quotes instead
$this->clear_stack_to_table_context();
break;

/*
* > A start tag whose tag name is one of: "tbody", "tfoot", "thead"
*/
case "+TBODY":

Check failure on line 1368 in src/wp-includes/html-api/class-wp-html-processor.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

String "+TBODY" does not require double quotes; use single quotes instead
case "+TFOOT":
case "+THEAD":
$this->clear_stack_to_table_context();
break;

/*
* > A start tag whose tag name is one of: "td", "th", "tr"
*/
case "+TD":
case "+TH":
case "+TR":
$this->clear_stack_to_table_context();
break;

/*
* > A start tag whose tag name is "table"
*/
case "+TABLE":
$this->clear_stack_to_table_context();
break;

/*
* > An end tag whose tag name is "table"
*/
case "-TABLE":
$this->clear_stack_to_table_context();
break;

/*
* > 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":
$this->clear_stack_to_table_context();
break;

/*
* > A start tag whose tag name is one of: "style", "script", "template"
*/
case "+STYLE":
case "+SCRIPT":
case "+TEMPLATE":
$this->clear_stack_to_table_context();
break;

/*
* > An end tag whose tag name is "template"
*/
case "-TEMPLATE":
$this->clear_stack_to_table_context();
break;

/*
* > A start tag whose tag name is "input"
*/
case "+INPUT":
$this->clear_stack_to_table_context();
break;

/*
* > A start tag whose tag name is "form"
*/
case "+FORM":
$this->clear_stack_to_table_context();
break;

/*
* > An end-of-file token
*/
/*
* > Anything else
*/
}

$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "Cannot process {$tag_name} element." );
}

/*
* Internal helpers
*/
Expand All @@ -1319,6 +1479,29 @@ private function bookmark_tag() {
return "{$this->bookmark_counter}";
}

/**
* 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
*/
private function clear_stack_to_table_context() {
// @todo we could add saftey here checking insertion modes…
foreach ( $this->state->stack_of_open_elements->walk_up() as $item ) {
if (
$item->node_name === 'TABLE' ||
$item->node_name === 'TEMPLATE' ||
$item->node_name === 'HTML'
) {
break;
}
$this->state->stack_of_open_elements->remove_node( $item );
}
}

/*
* HTML semantic overrides for Tag Processor
*/
Expand Down

0 comments on commit e5d0d6c

Please sign in to comment.