-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Only emit CDATA wrappers for inline scripts for JavaScript #5925
Conversation
str_contains( $attributes['type'], 'javascript' ) || | ||
str_contains( $attributes['type'], 'ecmascript' ) || | ||
str_contains( $attributes['type'], 'jscript' ) || | ||
str_contains( $attributes['type'], 'livescript' ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will account for a range of possible legacy types beyond the current text/javascript
, such as: application/javascript
, text/x-javascript
, text/javascript1.5
, application/javascript
, and so on.
For example, see this list from WPRocket: https://github.com/wp-media/wp-rocket/blob/8d510d7b160011ff175488e17d9d6d1254ed16f9/inc/Engine/Optimization/DelayJS/HTML.php#L43-L59
See also a query on Bard: https://g.co/bard/share/eb1358920977
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is type="module"
relevant here? I guess it may not be mutually exclusive, but in which world would someone not use HTML5 but JS modules? 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is relevant. A theme may not "support" HTML5 but a plugin may still add modules to the page.
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@westonruter Looks great! Only a few minor points.
str_contains( $attributes['type'], 'javascript' ) || | ||
str_contains( $attributes['type'], 'ecmascript' ) || | ||
str_contains( $attributes['type'], 'jscript' ) || | ||
str_contains( $attributes['type'], 'livescript' ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is type="module"
relevant here? I guess it may not be mutually exclusive, but in which world would someone not use HTML5 but JS modules? 😆
The only situation where we could break out of a We would only need it I think in XML circumstances, meaning inside RSS feeds, which don't get Since these pages are truly delivering HTML5 even without theme support, this seems a bit of a practical choice. We could reject JavaScript code containing |
@dmsnell AFAIK the only purpose of the CDATA wrappers was to prevent scripts from breaking XML when they use So I think more protection against accidentally (or not) breaking out of a |
@westonruter "AFAIK the only purpose of the CDATA wrappers was to prevent scripts from breaking XML when they use <, for example in an if statement." this was my point exactly. I'm suggesting that given how near-impossible it is to trigger the XML parsing mode within WordPress, this code isn't practically necessary and the breakage it introduces more than outweighs a hypothetical breakage we could see without any of it. In fact, on the pages this serves, the Thus my question is should we rip it all out since it's not fulfilling the purpose for which it's there? (Okay technically it's fulfilling the purpose if and only if the page is properly sent as XML, which you confirmed "it is not.") In those cases the burden is on the extender to properly escape their own JavaScript code. This affects a fraction of the less-than 0.0001% of sites serving XML that also contain script data that would break without the CDATA. On the other hand, it seems like it's breaking a noticeable number of normative sites where the |
I think you're right that the CDATA wrappers should be removed. But maybe we should do it in a follow-up ticket so there is more visibility? |
Something else we should do is make it clear that these script functions are not exclusively for JavaScript. Currently it explicitly is mentioning "JavaScript" in the function description and in the initial argument. |
+1 to discussing removal of CDATA wrappers separately. This PR is about a bug with the CDATA wrappers rather than removing them. |
See Core-60331. |
Trac ticket: https://core.trac.wordpress.org/ticket/60320
Commit message
Script Loader: Only emit CDATA wrapper comments in
wp_get_inline_script_tag()
for JavaScript.This avoids erroneously adding CDATA wrapper comments for non-JavaScript scripts, including those for JSON such as the
importmap
for script modules in #56313.Props westonruter, flixos90, mukesh27, dmsnell.
Fixes #60320.
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.