Skip to content

Commit fd61888

Browse files
committed
Improve select_ method arguments, docs, implementation
1 parent b389584 commit fd61888

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

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

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -636,37 +636,64 @@ public function get_unsupported_exception() {
636636
}
637637

638638
/**
639-
* Use a selector to advance.
639+
* Progress through a document pausing on tags matching the provided CSS selector string.
640+
*
641+
* @example
642+
*
643+
* $processor = WP_HTML_Processor::create_fragment(
644+
* '<meta charset="utf-8"><title>Example</title><meta property="og:type" content="website"><meta property="og:description" content="An example.">'
645+
* );
646+
* foreach ( $processor->select_all( 'meta[property^="og:" i]' ) as $_ ) {
647+
* // Loop is entered twice.
648+
* var_dump(
649+
* $processor->get_tag(), // string(4) "META"
650+
* $processor->get_attribute( 'property' ), // string(7) "og:type" / string(14) "og:description"
651+
* $processor->get_attribute( 'content' ), // string(7) "website" / string(11) "An example."
652+
* );
653+
* }
640654
*
641-
* @todo _doing_it_wrong on null selector?
655+
* @since TBD
642656
*
643-
* @param string $selectors
644-
* @return Generator<void>
657+
* @param string $selector_string Selector string.
658+
* @return Generator<void> A generator pausing on each tag matching the selector.
645659
*/
646-
public function select_all( string $selectors ): ?Generator {
647-
$select = WP_CSS_Selector::from_selectors( $selectors );
648-
if ( null === $select ) {
660+
public function select_all( string $selector_string ): ?Generator {
661+
$selector = WP_CSS_Selector::from_selectors( $selector_string );
662+
if ( null === $selector ) {
649663
return;
650664
}
651665

652666
while ( $this->next_tag() ) {
653-
if ( $select->matches( $this ) ) {
667+
if ( $selector->matches( $this ) ) {
654668
yield;
655669
}
656670
}
657671
}
658672

659673
/**
660-
* Select the next matching element.
674+
* Move to the next tag matching the provided CSS selector string.
661675
*
662-
* If iterating through matching elements, use `select_all` instead.
676+
* This method will stop at the next match. To progress through all matches, use
677+
* the `select_all` method.
663678
*
664-
* @param string $selectors
665-
* @return bool
679+
* @example
680+
*
681+
* $processor = WP_HTML_Processor::create_fragment(
682+
* '<meta charset="utf-8"><title>Example</title><meta property="og:title" content="Example">'
683+
* );
684+
* $processor->select( 'meta[charset]' );
685+
* var_dump(
686+
* $processor->get_tag(), // string(4) "META"
687+
* $processor->get_attribute( 'charset' ), // string(5) "utf-8"
688+
* );
689+
*
690+
* @since TBD
691+
*
692+
* @param string $selector_string
693+
* @return bool True if a matching tag was found, otherwise false.
666694
*/
667-
public function select( string $selectors ) {
668-
$selection = $this->select_all( $selectors );
669-
foreach ( $selection as $_ ) {
695+
public function select( string $selector_string ) {
696+
foreach ( $this->select_all( $selector_string ) as $_ ) {
670697
return true;
671698
}
672699
return false;

0 commit comments

Comments
 (0)