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

HTML API: Fix CDATA section null and whitespace handling #7230

Conversation

sirreal
Copy link
Member

@sirreal sirreal commented Aug 22, 2024

CDATA sections should behave like text. This means applying the rules
for inserting text in foreign content:

  • Null bytes should not change the frameset-ok flag.
  • Whitespace should not change the frameset-ok flag.
  • Other characters set frameset-ok flag to false.

Fix this behavior.

See the problematic behavior here.

The HTML input looks like this (null byte has been replaced with visual character):

<svg><![CDATA[ ␀]]></svg><frameset>

In this case, frameset-ok should not be changed. The HTML API should fail with WP_HTML_Unsupported_Exception: Cannot process non-ignored FRAMESET tags.

The current behavior (fixed in this PR) is to add the CDATA token without any inspection. This is incorrect because, depending on the contents of the CDATA section, the frameset-ok flag should be set to false.

Trac ticket: https://core.trac.wordpress.org/ticket/61576
Follow up to: [58868]


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

CDATA sections should behave like text. This means applying the rules
for inserting text in foreign content:

- Null bytes should not change the frameset-ok flag.
- Whitespace should not change the frameset-ok flag.
- Other characters set frameset-ok flag to false.

Fix this behavior.
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

$current_token = $this->bookmarks[ $this->state->current_token->bookmark_name ];
$cdata_content_start = $current_token->start + 9;
$cdata_content_length = $current_token->length - 12;
if ( $cdata_content_length !== strspn( $this->html, "\0 \t\n\f\r", $cdata_content_start, $cdata_content_length ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The yoda condition PHPCS flagged here seems to be a false positive. I suspect it's confusing a string literal on the line with a literal comparison, but the string literal is not being compared. It's fine to have it on the other side…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't define Yoda conditions the way most of us do, I think. by flipping this conditional it's happy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect it was finding a literal in the RHS of the comparison and thinking that literal are supposed to go on the left (yoda), but it ignores the fact that the literal is in a function call. But that's just my intuition.

Switching the order is fine if it satisfies the linter 🤷

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's the variable on the left hand side that it doesn't like, unless both are variables, in which case it's happy and doesn't care.

joda condition checks like these aren't about literals, but accidental assignment to a variable.

@sirreal sirreal marked this pull request as ready for review August 22, 2024 20:40
Copy link

github-actions bot commented Aug 22, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jonsurrell, dmsnell.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@sirreal sirreal force-pushed the html-api/fix-cdata-whitespace-frameset-handling branch from d5a528c to 47ccb34 Compare August 22, 2024 20:49
Copy link
Member

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sirreal with the merge of #7236 we should be able to add a short-circuit to skip the additional processing if the ->text_node_classification is WP_HTML_Tag_Processor::TEXT_IS_NULL_SEQUENCE || WP_HTML_Tag_Processor::TEXT_IS_WHITESPACE.

that may not make much of a difference, but it should have already been checked in those cases. maybe it's good enough to leave this in since we don't have to perform any character reference decoding.

what do you think?

@sirreal
Copy link
Member Author

sirreal commented Sep 3, 2024

After reviewing this and text subdivision, my preference is to go the other direction. I've pushed a change to stop running subdivision on CDATA and let this change handle CDATA on its own.

@@ -3368,70 +3368,55 @@ public function get_comment_type(): ?string {
* @return bool Whether the text node was subdivided.
*/
public function subdivide_text_appropriately(): bool {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When viewing with whitespace changes, the diff here is confusing. I've adjusted this method so that it only operates on text nodes.

@sirreal sirreal requested a review from dmsnell September 3, 2024 08:23
Copy link
Member

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fitting. It seemed fitting to join them together since text nodes and CDATA sections both represent text content. But computationally, CDATA sections are easier and don't need the abstraction.

I think I prefer classifying CDATA sections, but it also seems rather unlikely to find fully NULL byte or fully whitespace content inside of a CDATA section, so in practice it may not matter.

pento pushed a commit that referenced this pull request Sep 3, 2024
…orbid FRAMESET.

When CDATA sections (which can only occur inside SVG and MathML content) consist only of NULL bytes or whitespace characters they should not clear the "frameset ok" flag. Previously they have always been clearing this flag, but in this patch the logic is updated to detect these sequences properly.

Developed in #7230
Discussed in https://core.trac.wordpress.org/ticket/61576

Follow-up to [58867].

Props dmsnell, jonsurrell.
See #61576.


git-svn-id: https://develop.svn.wordpress.org/trunk@58977 602fd350-edb4-49c9-b593-d223f7449a82
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Sep 3, 2024
…orbid FRAMESET.

When CDATA sections (which can only occur inside SVG and MathML content) consist only of NULL bytes or whitespace characters they should not clear the "frameset ok" flag. Previously they have always been clearing this flag, but in this patch the logic is updated to detect these sequences properly.

Developed in WordPress/wordpress-develop#7230
Discussed in https://core.trac.wordpress.org/ticket/61576

Follow-up to [58867].

Props dmsnell, jonsurrell.
See #61576.

Built from https://develop.svn.wordpress.org/trunk@58977


git-svn-id: http://core.svn.wordpress.org/trunk@58373 1a063a9b-81f0-0310-95a4-ce76da25c4cd
@dmsnell
Copy link
Member

dmsnell commented Sep 3, 2024

Merged in [58977]
79c1047

@dmsnell dmsnell closed this Sep 3, 2024
github-actions bot pushed a commit to platformsh/wordpress-performance that referenced this pull request Sep 3, 2024
…orbid FRAMESET.

When CDATA sections (which can only occur inside SVG and MathML content) consist only of NULL bytes or whitespace characters they should not clear the "frameset ok" flag. Previously they have always been clearing this flag, but in this patch the logic is updated to detect these sequences properly.

Developed in WordPress/wordpress-develop#7230
Discussed in https://core.trac.wordpress.org/ticket/61576

Follow-up to [58867].

Props dmsnell, jonsurrell.
See #61576.

Built from https://develop.svn.wordpress.org/trunk@58977


git-svn-id: https://core.svn.wordpress.org/trunk@58373 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants