HTML API: Add `current_node_is()` helper method to stack of open elements.
As part of work to add more spec support to the HTML API, this new method will make it easier to implement the logic when in the SELECT and TABLE insertion modes. Developed in https://github.com/WordPress/wordpress-develop/pull/6968 Discussed in https://core.trac.wordpress.org/ticket/51576 Props dmsnell, jonsurrell. See #61576. Built from https://develop.svn.wordpress.org/trunk@58676 git-svn-id: http://core.svn.wordpress.org/trunk@58078 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
8269c3fcd6
commit
bb1da1f82e
|
@ -144,6 +144,48 @@ class WP_HTML_Open_Elements {
|
||||||
return $current_node ? $current_node : null;
|
return $current_node ? $current_node : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates if the current node is of a given type or name.
|
||||||
|
*
|
||||||
|
* It's possible to pass either a node type or a node name to this function.
|
||||||
|
* In the case there is no current element it will always return `false`.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
*
|
||||||
|
* // Is the current node a text node?
|
||||||
|
* $stack->current_node_is( '#text' );
|
||||||
|
*
|
||||||
|
* // Is the current node a DIV element?
|
||||||
|
* $stack->current_node_is( 'DIV' );
|
||||||
|
*
|
||||||
|
* // Is the current node any element/tag?
|
||||||
|
* $stack->current_node_is( '#tag' );
|
||||||
|
*
|
||||||
|
* @see WP_HTML_Tag_Processor::get_token_type
|
||||||
|
* @see WP_HTML_Tag_Processor::get_token_name
|
||||||
|
*
|
||||||
|
* @since 6.7.0
|
||||||
|
*
|
||||||
|
* @access private
|
||||||
|
*
|
||||||
|
* @param string $identity Check if the current node has this name or type (depending on what is provided).
|
||||||
|
* @return bool Whether there is a current element that matches the given identity, whether a token name or type.
|
||||||
|
*/
|
||||||
|
public function current_node_is( string $identity ): bool {
|
||||||
|
$current_node = end( $this->stack );
|
||||||
|
if ( false === $current_node ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$current_node_name = $current_node->node_name;
|
||||||
|
|
||||||
|
return (
|
||||||
|
$current_node_name === $identity ||
|
||||||
|
( '#doctype' === $identity && 'html' === $current_node_name ) ||
|
||||||
|
( '#tag' === $identity && ctype_upper( $current_node_name ) )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether an element is in a specific scope.
|
* Returns whether an element is in a specific scope.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1029,7 +1029,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->generate_implied_end_tags();
|
$this->generate_implied_end_tags();
|
||||||
if ( $this->state->stack_of_open_elements->current_node()->node_name !== $token_name ) {
|
if ( ! $this->state->stack_of_open_elements->current_node_is( $token_name ) ) {
|
||||||
// @todo Record parse error: this error doesn't impact parsing.
|
// @todo Record parse error: this error doesn't impact parsing.
|
||||||
}
|
}
|
||||||
$this->state->stack_of_open_elements->pop_until( $token_name );
|
$this->state->stack_of_open_elements->pop_until( $token_name );
|
||||||
|
@ -1094,7 +1094,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
|
||||||
|
|
||||||
$this->generate_implied_end_tags();
|
$this->generate_implied_end_tags();
|
||||||
|
|
||||||
if ( $this->state->stack_of_open_elements->current_node()->node_name !== $token_name ) {
|
if ( ! $this->state->stack_of_open_elements->current_node_is( $token_name ) ) {
|
||||||
// @todo Record parse error: this error doesn't impact parsing.
|
// @todo Record parse error: this error doesn't impact parsing.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1120,7 +1120,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
|
||||||
if ( $is_li ? 'LI' === $node->node_name : ( 'DD' === $node->node_name || 'DT' === $node->node_name ) ) {
|
if ( $is_li ? 'LI' === $node->node_name : ( 'DD' === $node->node_name || 'DT' === $node->node_name ) ) {
|
||||||
$node_name = $is_li ? 'LI' : $node->node_name;
|
$node_name = $is_li ? 'LI' : $node->node_name;
|
||||||
$this->generate_implied_end_tags( $node_name );
|
$this->generate_implied_end_tags( $node_name );
|
||||||
if ( $node_name !== $this->state->stack_of_open_elements->current_node()->node_name ) {
|
if ( ! $this->state->stack_of_open_elements->current_node_is( $node_name ) ) {
|
||||||
// @todo Indicate a parse error once it's possible. This error does not impact the logic here.
|
// @todo Indicate a parse error once it's possible. This error does not impact the logic here.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1197,7 +1197,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
|
||||||
|
|
||||||
$this->generate_implied_end_tags( $token_name );
|
$this->generate_implied_end_tags( $token_name );
|
||||||
|
|
||||||
if ( $token_name !== $this->state->stack_of_open_elements->current_node()->node_name ) {
|
if ( ! $this->state->stack_of_open_elements->current_node_is( $token_name ) ) {
|
||||||
// @todo Indicate a parse error once it's possible. This error does not impact the logic here.
|
// @todo Indicate a parse error once it's possible. This error does not impact the logic here.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '6.7-alpha-58674';
|
$wp_version = '6.7-alpha-58676';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue