Skip to content

Commit c41b639

Browse files
committed
Merge branch 'html-api/add-select-api' into html-api/work-on-select-api
2 parents 27239d6 + 176d787 commit c41b639

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/wp-includes/html-api/class-wp-html-processor.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,44 @@ public function get_unsupported_exception() {
635635
return $this->unsupported_exception;
636636
}
637637

638+
/**
639+
* Use a selector to advance.
640+
*
641+
* @param string $selectors
642+
* @return Generator<void>|null
643+
*/
644+
public function select_all( string $selectors ): ?Generator {
645+
$select = WP_CSS_Selector_List::from_selectors( $selectors );
646+
if ( null === $select ) {
647+
return null;
648+
}
649+
650+
while ( $this->next_tag() ) {
651+
if ( $select->matches( $this ) ) {
652+
yield;
653+
}
654+
}
655+
}
656+
657+
/**
658+
* Select the next matching element.
659+
*
660+
* If iterating through matching elements, use `select_all` instead.
661+
*
662+
* @param string $selectors
663+
* @return bool|null
664+
*/
665+
public function select( string $selectors ) {
666+
$selection = $this->select_all( $selectors );
667+
if ( null === $selection ) {
668+
return null;
669+
}
670+
foreach ( $selection as $_ ) {
671+
return true;
672+
}
673+
return false;
674+
}
675+
638676
/**
639677
* Finds the next tag matching the $query.
640678
*

0 commit comments

Comments
 (0)