-
Notifications
You must be signed in to change notification settings - Fork 3.2k
wp.sanitize.stripTags could rely on the browser for HTML parsing - [Core - 64274] #10536
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
Changes from 5 commits
826369c
0612d03
84c2bd9
3142ead
32c72ce
36c9856
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -23,22 +23,26 @@ | |||||
| * @return {string} Stripped text. | ||||||
| */ | ||||||
| stripTags: function( text ) { | ||||||
| let _text = text || ''; | ||||||
|
|
||||||
| // Do the search-replace until there is nothing to be replaced. | ||||||
| do { | ||||||
| // Keep pre-replace text for comparison. | ||||||
| text = _text; | ||||||
| const domParser = new DOMParser(); | ||||||
| const htmlDocument = domParser.parseFromString( | ||||||
| text, | ||||||
| 'text/html' | ||||||
| ); | ||||||
|
|
||||||
| // Do the replacement. | ||||||
| _text = text | ||||||
| .replace( /<!--[\s\S]*?(-->|$)/g, '' ) | ||||||
| .replace( /<(script|style)[^>]*>[\s\S]*?(<\/\1>|$)/ig, '' ) | ||||||
| .replace( /<\/?[a-z][\s\S]*?(>|$)/ig, '' ); | ||||||
| } while ( _text !== text ); | ||||||
| /* | ||||||
| * This looks funny and appears to be a no-op, but it | ||||||
| * enforces the escaping. How? when _read_ the `innerText` | ||||||
| * property decodes character references, returning a raw | ||||||
| * string. When _written_, however, it re-encodes to ensure | ||||||
| * that the rendered text replicates what it’s given. | ||||||
| * | ||||||
| * See: https://github.com/WordPress/wordpress-develop/pull/10536#discussion_r2550615378 | ||||||
| */ | ||||||
| htmlDocument.body.innerText = htmlDocument.body.innerText; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A way to get around the PhpStorm warning:
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if we just ignore it or file a bug report with Jetbrains? PhpStorm is wrong here because it doesn’t understand the code. Seems silly to me to modify our code to make up for that. it’s just a warning, and hopefully any reviewer will not the comment explaining why it’s there, as a guard against breaking this by “fixing” it.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we could go either way. I just don't have a lot of faith in Jetbrains fixing this issue (or others I've filed) in a timely manner.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've filed a ticket with Jetbrains: https://youtrack.jetbrains.com/issue/WI-83287/False-positive-inspection-for-Variable-is-assigned-to-itself-for-getting-setting-innerText Feel free to disregard this suggestion.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. before we merge, do we need to check if any minifiers might also make this mistake and remove the line? if that’s a hard question to answer, we can merge and see. it’s early in the release cycle so we should have ample time to test
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #10536 (comment) |
||||||
|
|
||||||
| // Return the text with stripped tags. | ||||||
| return _text; | ||||||
| return htmlDocument.body.innerHTML; | ||||||
| }, | ||||||
|
|
||||||
| /** | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.