Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML API: Expand Unsupported class and make it available for debugging. #6985

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 95 additions & 56 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
*/
private $last_error = null;

/**
* Stores context for why the parser bailed on unsupported HTML, if it did.
*
* @see self::get_unsupported_exception
*
* @since 6.7.0
*
* @var WP_HTML_Unsupported_Exception|null
*/
private $unsupported_exception = null;

/**
* Releases a bookmark when PHP garbage-collects its wrapping WP_HTML_Token instance.
*
Expand Down Expand Up @@ -375,6 +386,45 @@ function ( WP_HTML_Token $token ) {
};
}

/**
* Stops the parser and terminates its execution when encountering unsupported markup.
*
* @throws WP_HTML_Unsupported_Exception Halts execution of the parser.
*
* @since 6.7.0
*
* @param string $message Explains support is missing in order to parse the current node.
*
* @return mixed
*/
private function bail( string $message ) {
$here = $this->bookmarks[ $this->state->current_token->bookmark_name ];
$token = substr( $this->html, $here->start, $here->length );

$open_elements = array();
foreach ( $this->state->stack_of_open_elements->stack as $item ) {
$open_elements[] = $item->node_name;
}

$active_formats = array();
foreach ( $this->state->active_formatting_elements->walk_down() as $item ) {
$active_formats[] = $item->node_name;
}

$this->last_error = self::ERROR_UNSUPPORTED;

$this->unsupported_exception = new WP_HTML_Unsupported_Exception(
$message,
$this->state->current_token->node_name,
$here->start,
$token,
$open_elements,
$active_formats
);

throw $this->unsupported_exception;
}

/**
* Returns the last error, if any.
*
Expand Down Expand Up @@ -402,6 +452,21 @@ public function get_last_error() {
return $this->last_error;
}

/**
* Returns context for why the parser aborted due to unsupported HTML, if it did.
*
* This is meant for debugging purposes, not for production use.
*
* @since 6.7.0
*
* @see self::$unsupported_exception
*
* @return WP_HTML_Unsupported_Exception|null
*/
public function get_unsupported_exception() {
return $this->unsupported_exception;
}

/**
* Finds the next tag matching the $query.
*
Expand Down Expand Up @@ -829,8 +894,7 @@ public function step( $node_to_process = self::PROCESS_NEXT_NODE ) {

// This should be unreachable but PHP doesn't have total type checking on switch.
default:
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "Found unrecognized insertion mode '{$this->state->insertion_mode}'." );
$this->bail( "Unaware of the requested parsing mode: '{$this->state->insertion_mode}'." );
}
} catch ( WP_HTML_Unsupported_Exception $e ) {
/*
Expand Down Expand Up @@ -951,8 +1015,7 @@ public function get_current_depth() {
* @return bool Whether an element was found.
*/
private function step_initial() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -971,8 +1034,7 @@ private function step_initial() {
* @return bool Whether an element was found.
*/
private function step_before_html() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -991,8 +1053,7 @@ private function step_before_html() {
* @return bool Whether an element was found.
*/
private function step_before_head() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -1011,8 +1072,7 @@ private function step_before_head() {
* @return bool Whether an element was found.
*/
private function step_in_head() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -1031,8 +1091,7 @@ private function step_in_head() {
* @return bool Whether an element was found.
*/
private function step_in_head_noscript() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -1051,8 +1110,7 @@ private function step_in_head_noscript() {
* @return bool Whether an element was found.
*/
private function step_after_head() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand Down Expand Up @@ -1474,8 +1532,9 @@ private function step_in_body() {
* > than the end tag token that it actually is.
*/
case '-BR':
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( 'Closing BR tags require unimplemented special handling.' );
$this->bail( 'Closing BR tags require unimplemented special handling.' );
// This return required because PHPCS can't determine that the call to bail() throws.
return false;

/*
* > A start tag whose tag name is one of: "area", "br", "embed", "img", "keygen", "wbr"
Expand Down Expand Up @@ -1631,8 +1690,7 @@ private function step_in_body() {
case 'TITLE':
case 'TR':
case 'XMP':
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "Cannot process {$token_name} element." );
$this->bail( "Cannot process {$token_name} element." );
}

if ( ! parent::is_tag_closer() ) {
Expand Down Expand Up @@ -1694,8 +1752,7 @@ private function step_in_body() {
* @return bool Whether an element was found.
*/
private function step_in_table() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -1714,8 +1771,7 @@ private function step_in_table() {
* @return bool Whether an element was found.
*/
private function step_in_table_text() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -1734,8 +1790,7 @@ private function step_in_table_text() {
* @return bool Whether an element was found.
*/
private function step_in_caption() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -1754,8 +1809,7 @@ private function step_in_caption() {
* @return bool Whether an element was found.
*/
private function step_in_column_group() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -1774,8 +1828,7 @@ private function step_in_column_group() {
* @return bool Whether an element was found.
*/
private function step_in_table_body() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -1794,8 +1847,7 @@ private function step_in_table_body() {
* @return bool Whether an element was found.
*/
private function step_in_row() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -1814,8 +1866,7 @@ private function step_in_row() {
* @return bool Whether an element was found.
*/
private function step_in_cell() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand Down Expand Up @@ -2015,8 +2066,7 @@ private function step_in_select() {
* @return bool Whether an element was found.
*/
private function step_in_select_in_table() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -2035,8 +2085,7 @@ private function step_in_select_in_table() {
* @return bool Whether an element was found.
*/
private function step_in_template() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -2055,8 +2104,7 @@ private function step_in_template() {
* @return bool Whether an element was found.
*/
private function step_after_body() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -2075,8 +2123,7 @@ private function step_after_body() {
* @return bool Whether an element was found.
*/
private function step_in_frameset() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -2095,8 +2142,7 @@ private function step_in_frameset() {
* @return bool Whether an element was found.
*/
private function step_after_frameset() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -2115,8 +2161,7 @@ private function step_after_frameset() {
* @return bool Whether an element was found.
*/
private function step_after_after_body() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -2135,8 +2180,7 @@ private function step_after_after_body() {
* @return bool Whether an element was found.
*/
private function step_after_after_frameset() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/**
Expand All @@ -2155,8 +2199,7 @@ private function step_after_after_frameset() {
* @return bool Whether an element was found.
*/
private function step_in_foreign_content() {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
$this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
}

/*
Expand Down Expand Up @@ -2859,8 +2902,7 @@ private function reconstruct_active_formatting_elements() {
return false;
}

$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( 'Cannot reconstruct active formatting elements when advancing and rewinding is required.' );
$this->bail( 'Cannot reconstruct active formatting elements when advancing and rewinding is required.' );
}

/**
Expand Down Expand Up @@ -3096,8 +3138,7 @@ private function run_adoption_agency_algorithm() {

// > If there is no such element, then return and instead act as described in the "any other end tag" entry above.
if ( null === $formatting_element ) {
$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( 'Cannot run adoption agency when "any other end tag" is required.' );
$this->bail( 'Cannot run adoption agency when "any other end tag" is required.' );
}

// > If formatting element is not in the stack of open elements, then this is a parse error; remove the element from the list, and return.
Expand Down Expand Up @@ -3149,12 +3190,10 @@ private function run_adoption_agency_algorithm() {
}
}

$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( 'Cannot extract common ancestor in adoption agency algorithm.' );
$this->bail( 'Cannot extract common ancestor in adoption agency algorithm.' );
}

$this->last_error = self::ERROR_UNSUPPORTED;
throw new WP_HTML_Unsupported_Exception( 'Cannot run adoption agency when looping required.' );
$this->bail( 'Cannot run adoption agency when looping required.' );
}

/**
Expand Down
Loading
Loading