Skip to content

Commit

Permalink
Implement in_select_scope via …in_specific_scope
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Jul 4, 2024
1 parent dc0bbb9 commit 9d99844
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/wp-includes/html-api/class-wp-html-open-elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,15 @@ public function current_node() {
*
* @see https://html.spec.whatwg.org/#has-an-element-in-the-specific-scope
*
* @param string $tag_name Name of tag check.
* @param string[] $termination_list List of elements that terminate the search.
* @param string $tag_name Name of tag check.
* @param string[] $termination_list List of elements that terminate the search.
* @param string $termination_behavior Optional. "include" or "exclude". If "include"
* (default), the terminate when any tag in
* `$termination_list` is reached. Otherwise,
* terminate when any _other_ tag is reached.
* @return bool Whether the element was found in a specific scope.
*/
public function has_element_in_specific_scope( $tag_name, $termination_list ) {
public function has_element_in_specific_scope( $tag_name, $termination_list, $termination_behavior = 'include' ) {
foreach ( $this->walk_up() as $node ) {
if ( $node->node_name === $tag_name ) {
return true;
Expand All @@ -178,7 +182,10 @@ public function has_element_in_specific_scope( $tag_name, $termination_list ) {
return false;
}

if ( in_array( $node->node_name, $termination_list, true ) ) {
$terminate = 'include' === $termination_behavior
? in_array( $node->node_name, $termination_list, true )
: ! in_array( $node->node_name, $termination_list, true );
if ( $terminate ) {
return false;
}
}
Expand Down Expand Up @@ -283,20 +290,11 @@ public function has_element_in_table_scope( $tag_name ) {
* @return bool Whether given element is in scope.
*/
public function has_element_in_select_scope( $tag_name ) {
foreach ( $this->walk_up() as $node ) {
if ( $node->node_name === $tag_name ) {
return true;
}

if (
'OPTION' !== $node->node_name &&
'OPTGROUP' !== $node->node_name
) {
return false;
}
}

return false;
return $this->has_element_in_specific_scope(
$tag_name,
array( 'OPTION', 'OPTGROUP' ),
'exclude'
);
}

/**
Expand Down

0 comments on commit 9d99844

Please sign in to comment.