Skip to content

Commit

Permalink
Compat: Update HTML API with changes from 6.6 (WordPress#63089)
Browse files Browse the repository at this point in the history
Updates the HTML API components in the compatability layer with updates
from each of the 6.4, 6.5, and 6.6 branches from Core.

Notably, because of the breaking change in 6.5 to the `WP_HTML_Span` and
`WP_HTML_Text_Replacement` classes, which changed the range format from
`(start, end)` to `(start, length)`, the 6.6 classes still reference the
`Gutenberg_HTML_Span_6_5` and `Gutenberg_HTML_Text_Replacement_6_5`
substitutions.

While updating it was noticed that the 6.4 compatability code
accidentally pulled in updates from the 6.5 branch for the HTML
Processor. These additions have been removed for the sake of closer
correspondance with the Core code.

Co-authored-by: ockham <[email protected]>
  • Loading branch information
2 people authored and carstingaxion committed Jul 18, 2024
1 parent 9611ab8 commit 05fbb3a
Show file tree
Hide file tree
Showing 17 changed files with 9,823 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2031,8 +2031,8 @@ public function set_attribute( $name, $value ) {
*
* @see https://html.spec.whatwg.org/#attributes-2
*
* @todo As the only regex pattern maybe we should take it out?
* Are Unicode patterns available broadly in Core?
* @TODO as the only regex pattern maybe we should take it out? are
* Unicode patterns available broadly in Core?
*/
if ( preg_match(
'~[' .
Expand Down Expand Up @@ -2071,7 +2071,14 @@ public function set_attribute( $name, $value ) {
if ( true === $value ) {
$updated_attribute = $name;
} else {
$escaped_new_value = esc_attr( $value );
$comparable_name = strtolower( $name );

/*
* Escape URL attributes.
*
* @see https://html.spec.whatwg.org/#attributes-3
*/
$escaped_new_value = in_array( $comparable_name, wp_kses_uri_attributes() ) ? esc_url( $value ) : esc_attr( $value );
$updated_attribute = "{$name}=\"{$escaped_new_value}\"";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ public function current_node() {
*/
public function push( $token ) {
/*
* > If there are already three elements in the list of active formatting elements after the last marker,
* > if any, or anywhere in the list if there are no markers, that have the same tag name, namespace, and
* > attributes as element, then remove the earliest such element from the list of active formatting
* > elements. For these purposes, the attributes must be compared as they were when the elements were
* > created by the parser; two elements have the same attributes if all their parsed attributes can be
* > paired such that the two attributes in each pair have identical names, namespaces, and values
* > (the order of the attributes does not matter).
*
* @todo Implement the "Noah's Ark clause" to only add up to three of any given kind of formatting elements to the stack.
*/
* > If there are already three elements in the list of active formatting elements after the last marker,
* > if any, or anywhere in the list if there are no markers, that have the same tag name, namespace, and
* > attributes as element, then remove the earliest such element from the list of active formatting
* > elements. For these purposes, the attributes must be compared as they were when the elements were
* > created by the parser; two elements have the same attributes if all their parsed attributes can be
* > paired such that the two attributes in each pair have identical names, namespaces, and values
* > (the order of the attributes does not matter).
*
* @TODO: Implement the "Noah's Ark clause" to only add up to three of any given kind of formatting elements to the stack.
*/
// > Add element to the list of active formatting elements.
$this->stack[] = $token;
}
Expand Down
36 changes: 18 additions & 18 deletions lib/compat/wordpress-6.4/html-api/class-wp-html-open-elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function current_node() {
* @param string[] $termination_list List of elements that terminate the search.
* @return bool Whether the element was found in a specific scope.
*/
public function has_element_in_specific_scope( $tag_name, $termination_list ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
public function has_element_in_specific_scope( $tag_name, $termination_list ) {
foreach ( $this->walk_up() as $node ) {
if ( $node->node_name === $tag_name ) {
return true;
Expand Down Expand Up @@ -147,12 +147,12 @@ public function has_element_in_scope( $tag_name ) {
array(

/*
* Because it's not currently possible to encounter
* one of the termination elements, they don't need
* to be listed here. If they were, they would be
* unreachable and only waste CPU cycles while
* scanning through HTML.
*/
* Because it's not currently possible to encounter
* one of the termination elements, they don't need
* to be listed here. If they were, they would be
* unreachable and only waste CPU cycles while
* scanning through HTML.
*/
)
);
}
Expand All @@ -169,7 +169,7 @@ public function has_element_in_scope( $tag_name ) {
* @param string $tag_name Name of tag to check.
* @return bool Whether given element is in scope.
*/
public function has_element_in_list_item_scope( $tag_name ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
public function has_element_in_list_item_scope( $tag_name ) {
throw new WP_HTML_Unsupported_Exception( 'Cannot process elements depending on list item scope.' );

return false; // The linter requires this unreachable code until the function is implemented and can return.
Expand Down Expand Up @@ -201,7 +201,7 @@ public function has_element_in_button_scope( $tag_name ) {
* @param string $tag_name Name of tag to check.
* @return bool Whether given element is in scope.
*/
public function has_element_in_table_scope( $tag_name ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
public function has_element_in_table_scope( $tag_name ) {
throw new WP_HTML_Unsupported_Exception( 'Cannot process elements depending on table scope.' );

return false; // The linter requires this unreachable code until the function is implemented and can return.
Expand All @@ -219,7 +219,7 @@ public function has_element_in_table_scope( $tag_name ) { // phpcs:ignore Variab
* @param string $tag_name Name of tag to check.
* @return bool Whether given element is in scope.
*/
public function has_element_in_select_scope( $tag_name ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
public function has_element_in_select_scope( $tag_name ) {
throw new WP_HTML_Unsupported_Exception( 'Cannot process elements depending on select scope.' );

return false; // The linter requires this unreachable code until the function is implemented and can return.
Expand Down Expand Up @@ -371,8 +371,8 @@ public function walk_up() {
}

/*
* Internal helpers.
*/
* Internal helpers.
*/

/**
* Updates internal flags after adding an element.
Expand All @@ -389,9 +389,9 @@ public function walk_up() {
*/
public function after_element_push( $item ) {
/*
* When adding support for new elements, expand this switch to trap
* cases where the precalculated value needs to change.
*/
* When adding support for new elements, expand this switch to trap
* cases where the precalculated value needs to change.
*/
switch ( $item->node_name ) {
case 'BUTTON':
$this->has_p_in_button_scope = false;
Expand All @@ -418,9 +418,9 @@ public function after_element_push( $item ) {
*/
public function after_element_pop( $item ) {
/*
* When adding support for new elements, expand this switch to trap
* cases where the precalculated value needs to change.
*/
* When adding support for new elements, expand this switch to trap
* cases where the precalculated value needs to change.
*/
switch ( $item->node_name ) {
case 'BUTTON':
$this->has_p_in_button_scope = $this->has_element_in_button_scope( 'P' );
Expand Down
35 changes: 22 additions & 13 deletions lib/compat/wordpress-6.4/html-api/class-wp-html-processor-state.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
*/
class WP_HTML_Processor_State {
/*
* Insertion mode constants.
*
* These constants exist and are named to make it easier to
* discover and recognize the supported insertion modes in
* the parser.
*
* Out of all the possible insertion modes, only those
* supported by the parser are listed here. As support
* is added to the parser for more modes, add them here
* following the same naming and value pattern.
*
* @see https://html.spec.whatwg.org/#the-insertion-mode
*/
* Insertion mode constants.
*
* These constants exist and are named to make it easier to
* discover and recognize the supported insertion modes in
* the parser.
*
* Out of all the possible insertion modes, only those
* supported by the parser are listed here. As support
* is added to the parser for more modes, add them here
* following the same naming and value pattern.
*
* @see https://html.spec.whatwg.org/#the-insertion-mode
*/

/**
* Initial insertion mode for full HTML parser.
Expand Down Expand Up @@ -87,6 +87,15 @@ class WP_HTML_Processor_State {
*/
public $active_formatting_elements = null;

/**
* Refers to the currently-matched tag, if any.
*
* @since 6.4.0
*
* @var WP_HTML_Token|null
*/
public $current_token = null;

/**
* Tree construction insertion mode.
*
Expand Down
Loading

0 comments on commit 05fbb3a

Please sign in to comment.