Skip to content

HTML API: Add text whitespace checking method #7051

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

Closed
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
22 changes: 20 additions & 2 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1141,8 +1141,7 @@
* It is probably inter-element whitespace, but it may also
* contain character references which decode only to whitespace.
*/
$text = $this->get_modifiable_text();
if ( strlen( $text ) !== strspn( $text, " \t\n\f\r" ) ) {
if ( ! $this->is_text_whitespace() ) {
$this->state->frameset_ok = false;
}

Expand Down Expand Up @@ -2503,6 +2502,25 @@
return $this->is_virtual() ? '' : parent::get_modifiable_text();
}

/**
* Checks whether a text node or atomic element contains only whitespace.
*
* HTML entities will be decoded as necessary before the check is performed.
*
* The whitespace check is performed according to the HTML specification:
* > ASCII whitespace is U+0009 TAB, U+000A LF, U+000C FF, U+000D CR, or U+0020 SPACE.
* @see https://infra.spec.whatwg.org/#ascii-whitespace
*
* @since 6.7.0
*
* @return bool True if the text only contains whitespace.
*/
public function is_text_whitespace() {
$text = $this->get_modifiable_text();
$length = strlen( $text );
return $length === strspn( $text, "\x09\x0A\x0C\x0D\x20", 0, $length );

Check failure on line 2521 in src/wp-includes/html-api/class-wp-html-processor.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Use Yoda Condition checks, you must.
}

/**
* Indicates what kind of comment produced the comment node.
*
Expand Down
Loading