diff --git a/src/wp-includes/html-api/class-wp-html-open-elements.php b/src/wp-includes/html-api/class-wp-html-open-elements.php
index 117bfb4a04eac..0fd8ae43b0c42 100644
--- a/src/wp-includes/html-api/class-wp-html-open-elements.php
+++ b/src/wp-includes/html-api/class-wp-html-open-elements.php
@@ -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;
@@ -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;
}
}
@@ -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'
+ );
}
/**