Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag Processor: Remove the shorthand next_tag( $tag_name ) syntax #45082

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 29 additions & 39 deletions lib/experimental/html/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,32 @@
* Example:
* ```php
* $tags = new WP_HTML_Tag_Processor( $html );
* if ( $tags->next_tag( [ 'tag_name' => 'option' ] ) ) {
* if ( $tags->next( [ 'tag' => 'option' ] ) ) {
* $tags->set_attribute( 'selected', true );
* }
* ```
*
* ### Finding tags
*
* The `next_tag()` function moves the internal cursor through
* The `next()` function moves the internal cursor through
* your input HTML document until it finds a tag meeting any of
* the supplied restrictions in the optional query argument. If
* no argument is provided then it will find the next HTML tag,
* regardless of what kind it is.
*
* If you want to _find whatever the next tag is_:
* ```php
* $tags->next_tag();
* $tags->next();
* ```
*
* | Goal | Query |
* |-----------------------------------------------------------|----------------------------------------------------------------------------|
* | Find any tag. | `$tags->next_tag();` |
* | Find next image tag. | `$tags->next_tag( [ 'tag_name' => 'img' ] );` |
* | Find next tag containing the `fullwidth` CSS class. | `$tags->next_tag( [ 'class_name' => 'fullwidth' ] );` |
* | Find next image tag containing the `fullwidth` CSS class. | `$tags->next_tag( [ 'tag_name' => 'img', 'class_name' => 'fullwidth' ] );` |
* | Goal | Query |
* |-----------------------------------------------------------|--------------------------------------------------------------|
* | Find any tag. | `$tags->next();` |
* | Find next image tag. | `$tags->next( [ 'tag' => 'img' ] );` |
* | Find next tag containing the `fullwidth` CSS class. | `$tags->next( [ 'class' => 'fullwidth' ] );` |
* | Find next image tag containing the `fullwidth` CSS class. | `$tags->next( [ 'tag' => 'img', 'class' => 'fullwidth' ] );` |
*
* If a tag was found meeting your criteria then `next_tag()`
* If a tag was found meeting your criteria then `next()`
* will return `true` and you can proceed to modify it. If it
* returns `false`, however, it failed to find the tag and
* moved the cursor to the end of the file.
Expand All @@ -88,7 +88,7 @@
* ```php
* // Paint up to the first five DIV or SPAN tags marked with the "jazzy" style.
* $remaining_count = 5;
* while ( $remaining_count > 0 && $tags->next_tag() ) {
* while ( $remaining_count > 0 && $tags->next() ) {
* if (
* ( 'DIV' === $tags->get_tag() || 'SPAN' === $tags->get_tag() ) &&
* 'jazzy' === $tags->get_attribute( 'data-style' )
Expand All @@ -115,7 +115,7 @@
*
* Example:
* ```php
* if ( $tags->next_tag( [ 'class' => 'wp-group-block' ] ) ) {
* if ( $tags->next( [ 'class' => 'wp-group-block' ] ) ) {
* $tags->set_attribute( 'title', 'This groups the contained content.' );
* $tags->remove_attribute( 'data-test-id' );
* }
Expand Down Expand Up @@ -190,7 +190,7 @@ class WP_HTML_Tag_Processor {
private $html;

/**
* The last query passed to next_tag().
* The last query passed to next().
*
* @since 6.2.0
* @var array|null
Expand Down Expand Up @@ -392,7 +392,7 @@ public function __construct( $html ) {
* }
* @return boolean Whether a tag was matched.
*/
public function next_tag( $query = null ) {
public function next( $query = null ) {
$this->parse_query( $query );
$already_found = 0;

Expand Down Expand Up @@ -644,12 +644,13 @@ private function parse_next_tag() {
* * https://html.spec.whatwg.org/multipage/parsing.html#data-state
* * https://html.spec.whatwg.org/multipage/parsing.html#tag-open-state
*/
$tag_name_prefix_length = strspn( $html, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', $at + 1 );
if ( $tag_name_prefix_length > 0 ) {
$tag_prefix_length = strspn( $html, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', $at + 1 );
if ( $tag_prefix_length > 0 ) {
++$at;
$this->tag_name_length = $tag_name_prefix_length + strcspn( $html, " \t\f\r\n/>", $at + $tag_name_prefix_length );
$this->tag_name_starts_at = $at;
$this->parsed_bytes = $at + $this->tag_name_length;
$this->tag_length = $tag_prefix_length + strcspn( $html, " \t\f\r\n/>", $at + $tag_prefix_length );
$this->tag_starts_at = $at;
$this->parsed_bytes = $at + $this->tag_length;

return true;
}

Expand Down Expand Up @@ -1027,12 +1028,12 @@ private static function sort_start_ascending( $a, $b ) {
* Example:
* <code>
* $p = new WP_HTML_Tag_Processor( '<div enabled class="test" data-test-id="14">Test</div>' );
* $p->next_tag( [ 'class_name' => 'test' ] ) === true;
* $p->next( [ 'class' => 'test' ] ) === true;
* $p->get_attribute( 'data-test-id' ) === '14';
* $p->get_attribute( 'enabled' ) === true;
* $p->get_attribute( 'aria-label' ) === null;
*
* $p->next_tag( [] ) === false;
* $p->next( [] ) === false;
* $p->get_attribute( 'class' ) === null;
* </code>
*
Expand Down Expand Up @@ -1069,10 +1070,10 @@ public function get_attribute( $name ) {
* Example:
* <code>
* $p = new WP_HTML_Tag_Processor( '<DIV CLASS="test">Test</DIV>' );
* $p->next_tag( [] ) === true;
* $p->next( [] ) === true;
* $p->get_tag() === 'DIV';
*
* $p->next_tag( [] ) === false;
* $p->next( [] ) === false;
* $p->get_tag() === null;
* </code>
*
Expand Down Expand Up @@ -1333,7 +1334,7 @@ public function get_updated_html() {
*
* @since 6.2.0
*
* @param array|string $query {
* @param array $query {
* Which tag name to find, having which class.
*
* @type string|null $tag_name Which tag to find, or `null` for "any tag."
Expand All @@ -1350,23 +1351,12 @@ private function parse_query( $query ) {
$this->sought_class_name = null;
$this->sought_match_offset = 1;

// A single string value means "find the tag of this name".
if ( is_string( $query ) ) {
$this->sought_tag_name = $query;
return;
}

// If not using the string interface we have to pass an associative array.
if ( ! is_array( $query ) ) {
return;
}

if ( isset( $query['tag_name'] ) && is_string( $query['tag_name'] ) ) {
$this->sought_tag_name = $query['tag_name'];
if ( isset( $query['tag'] ) && is_string( $query['tag'] ) ) {
$this->sought_tag_name = $query['tag'];
}

if ( isset( $query['class_name'] ) && is_string( $query['class_name'] ) ) {
$this->sought_class_name = $query['class_name'];
if ( isset( $query['class'] ) && is_string( $query['class'] ) ) {
$this->sought_class_name = $query['class'];
}

if ( isset( $query['match_offset'] ) && is_int( $query['match_offset'] ) && 0 < $query['match_offset'] ) {
Expand Down
Loading