Opened 3 months ago
Closed 3 months ago
#64500 closed enhancement (fixed)
Scripts: Use HTML API to generate script tags with improved safety
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 7.0 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Script Loader | Keywords: | has-patch has-unit-tests |
| Focuses: | javascript | Cc: |
Description
The HTML API can produce SCRIPT tags that are safe, or reject the script tags if no safe tag can be produced.
SCRIPT tags have complicated and unintuitive parsing rules that make them difficult to author correctly. Currently, authors need to provide safe JavaScript and/or correctly escaped JSON in order to produce safe HTML SCRIPT tags.
[61447] added the ability to automatically escape JSON and JavaScript content when problematic strings are discovered, making the HTML API especially well-suited to producing script tags.
This is similar to change [61418] where the HTML API is used to generate style tags.
This is well documented by issues like #40737, #62797, #63851, and #51159.
This is a follow up to #64419.
Change History (8)
This ticket was mentioned in PR #10639 on WordPress/wordpress-develop by @jonsurrell.
3 months ago
#1
- Keywords has-patch has-unit-tests added
@jonsurrell commented on PR #10639:
3 months ago
#2
[61477] has been merged and this PR has been updated. It's ready for review.
It includes tests updates in https://github.com/WordPress/wordpress-develop/pull/10727 which can be landed separately.
This ticket was mentioned in PR #10728 on WordPress/wordpress-develop by @jonsurrell.
3 months ago
#3
Add tests that verify the behavior of wp_get_inline_script_tag() wp_get_script_tag() attribute handling. This is relevant for the work in https://github.com/WordPress/wordpress-develop/pull/10639. See https://github.com/WordPress/wordpress-develop/pull/10639#discussion_r2687294023.
Tests are added using assertEqualHTML() assertions that verify the behavior when:
- Different attribute values and types are provided.
- Repeated attribute names are provided (correct order handling)
Trac ticket: https://core.trac.wordpress.org/ticket/64500
@jonsurrell commented on PR #10639:
3 months ago
#4
A nice improvement with this change:
echo wp_get_script_tag( array( "<" => "less-than" ) );
Warns:
Notice: Function WP_HTML_Tag_Processor::set_attribute was called <strong>incorrectly</strong>. Invalid attribute name.
And prints:
<script></script>
On trunk, there would be no warning and the following would be printed (because esc_attr(), although unsuitable, was used on the attribute name):
<script <="less-than"></script>
This SCRIPT tag has an attribute literally named <.
@jonsurrell commented on PR #10639:
3 months ago
#5
I'd like to land https://github.com/WordPress/wordpress-develop/pull/10728 first so it's clear that the attribute handling is unchanged from trunk in this change.
@jonsurrell commented on PR #10728:
3 months ago
#7
Merged in r61482.
✅ ~Includes #10635~ (merged in r61477).
Trac ticket: https://core.trac.wordpress.org/ticket/64500