Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
4a8ca98
Auto-escape JavaScript and JSON script tags when necessary
sirreal Dec 15, 2025
4a28ef2
Update ticket number
sirreal Dec 15, 2025
0bef687
Fix those lints
sirreal Dec 15, 2025
cdba027
No trailing function commas
sirreal Dec 15, 2025
ba54ae4
Remove JSON_THROW_ON_ERROR constant
sirreal Dec 15, 2025
a697e9e
fixup! Remove JSON_THROW_ON_ERROR constant
sirreal Dec 15, 2025
2b3d0d0
Merge branch 'trunk' into html-api/auto-escape-javascript-json
sirreal Dec 16, 2025
8246439
Remove JSON +json subtype handling
sirreal Dec 16, 2025
1b6b4fd
Merge branch 'html-api/auto-escape-javascript-json' into scripts/use-…
sirreal Dec 16, 2025
b3e88e8
Add test for scripts containing script tags
sirreal Dec 16, 2025
27c6371
Update wp_add_inline_script to use HTML API
sirreal Dec 16, 2025
deebd54
Use the tag processor for all the inline script tags
sirreal Dec 16, 2025
d78cd35
Remove assertions
sirreal Dec 16, 2025
c29c3d9
PICKME: Update Script Modules tests to use assertEqualHTML
sirreal Dec 16, 2025
abeebd6
Revert "Remove assertions"
sirreal Dec 16, 2025
aaacd6f
Merge branch 'trunk' into scripts/use-html-api-for-script-tags
sirreal Dec 19, 2025
3f7f227
Merge branch 'trunk' into html-api/auto-escape-javascript-json
sirreal Dec 19, 2025
1362451
Add JS and JSON script tag tests
Copilot Dec 19, 2025
8d30680
Fix typo in comment
sirreal Dec 19, 2025
3b5ef4e
Improve comment
sirreal Dec 19, 2025
f8cfdf9
Clean up and fix tests
sirreal Dec 19, 2025
501d201
Clean up and improve type/language logic
sirreal Dec 19, 2025
253b971
Merge branch 'html-api/auto-escape-javascript-json' into scripts/use-…
sirreal Dec 19, 2025
bcc02ae
Fix svg SCRIPT tag tests
sirreal Dec 19, 2025
ea03441
Add todo on MIME type parsing
sirreal Dec 19, 2025
c7d1827
Update since tags 🤞
sirreal Dec 19, 2025
a134e82
Make is_{javascript,json}_script_tag methods private
sirreal Dec 19, 2025
d4bd4b3
Add language whitespace test
sirreal Dec 19, 2025
edae8d5
How'd that extra space get there, remove it!
sirreal Dec 19, 2025
02ca3c0
Name search parts
sirreal Dec 19, 2025
d058a78
Clean up escaping tests
sirreal Dec 19, 2025
dfb63af
Add ignore and todo tags to new private is_*_script_tag functions
sirreal Dec 19, 2025
11f51c9
Improve example comment
sirreal Dec 19, 2025
a3e0e27
Update regex comment with named groups
sirreal Dec 19, 2025
288b952
Add details to "other" failure to escape comment
sirreal Dec 19, 2025
a7495dd
Improve consistency of comment quoting and differentiate types of dou…
sirreal Dec 19, 2025
614916d
Describe JavaScript escaping strategy in detail
sirreal Dec 19, 2025
5828382
Merge branch 'html-api/auto-escape-javascript-json' into scripts/use-…
sirreal Dec 19, 2025
d8c320c
Describe JavaScript escaping strategy in detail
sirreal Dec 19, 2025
016a29f
Merge branch 'html-api/auto-escape-javascript-json' into scripts/use-…
sirreal Dec 19, 2025
369eefc
Use the updated_html value in round-trip test
sirreal Dec 19, 2025
d67749d
Merge branch 'html-api/auto-escape-javascript-json' into scripts/use-…
sirreal Dec 19, 2025
2869880
Revert changes to wp_add_inline_script
sirreal Dec 19, 2025
d6bfdca
Remove debugging assertions
sirreal Dec 19, 2025
b7099e4
fixup! Revert changes to wp_add_inline_script
sirreal Dec 19, 2025
010a2a2
Switch to assertEqualHTML assertions
sirreal Dec 19, 2025
ab53486
Update defer/async boolean attributes
sirreal Dec 19, 2025
3ba2267
Use Tag Processor in wp_get_script_Tag
sirreal Dec 19, 2025
64a23b7
Add utility for semantically comparing a SCRIPT tag within HTML
sirreal Dec 19, 2025
c920821
Use script tag comparison in tests
sirreal Dec 19, 2025
dbd9f55
Remove more async=async and defer=defer
sirreal Dec 19, 2025
7bef55a
fixup! Use script tag comparison in tests
sirreal Dec 19, 2025
4333300
fixup! Add utility for semantically comparing a SCRIPT tag within HTML
sirreal Dec 19, 2025
504a928
fix lint
sirreal Dec 19, 2025
55d4e47
Merge branch 'trunk' into html-api/auto-escape-javascript-json
sirreal Dec 22, 2025
1c84037
Merge branch 'html-api/auto-escape-javascript-json' into scripts/use-…
sirreal Dec 22, 2025
2ef0bf0
Fix demonstration comment
sirreal Dec 22, 2025
cb6b990
Extract and document escaping functions
sirreal Dec 22, 2025
da28eec
Merge branch 'html-api/auto-escape-javascript-json' into scripts/use-…
sirreal Dec 22, 2025
e399bf6
Tweak workaround documentation
sirreal Dec 22, 2025
83c1fab
Add ASCII chart about HTML script tags with original source
sirreal Dec 22, 2025
1872681
Improve linking between escapes
sirreal Dec 22, 2025
83ff62f
Fix comments, typos, lints
sirreal Dec 22, 2025
5979782
fixup! Fix comments, typos, lints
sirreal Dec 22, 2025
d469ae4
fixup! fixup! Fix comments, typos, lints
sirreal Dec 22, 2025
402ae9f
Fix \c -> \r (carriage return) typo
sirreal Dec 22, 2025
6b2c9ba
Add note about not parsing MIME types for JS script tags
sirreal Dec 22, 2025
eef0ccb
Add todo comment to is_json_script_tag
sirreal Dec 22, 2025
71d2686
Re-order tag name termination chars to match elsewhere
sirreal Dec 22, 2025
d4693a2
Fix typo
sirreal Dec 22, 2025
eb4e091
Merge branch 'trunk' into html-api/auto-escape-javascript-json
sirreal Dec 29, 2025
4c3b0b2
Update comments on tag prefixes matching search pattern
sirreal Dec 29, 2025
40015c9
Merge branch 'html-api/auto-escape-javascript-json' into scripts/use-…
sirreal Dec 29, 2025
bfa60cf
Use content-type identification and escape without PCRE
dmsnell Dec 30, 2025
3252160
fixup! Use content-type identification and escape without PCRE
dmsnell Dec 30, 2025
0b9b2bd
fixup! Use content-type identification and escape without PCRE
dmsnell Dec 30, 2025
b2533e1
fixup! Use content-type identification and escape without PCRE
dmsnell Dec 31, 2025
ddeb301
fixup! Use content-type identification and escape without PCRE
dmsnell Dec 31, 2025
b36fc32
fixup! Use content-type identification and escape without PCRE
dmsnell Dec 31, 2025
1249af0
fixup! Use content-type identification and escape without PCRE
dmsnell Dec 31, 2025
645cb45
Update escaping comment
sirreal Jan 8, 2026
f23b35b
Fix off-by-one repeat processing after script
sirreal Jan 8, 2026
31cefb7
Restructure escaping logic
sirreal Jan 8, 2026
25279e3
Merge and remove duplicate script tag content type tests
sirreal Jan 8, 2026
6660196
Merge branch 'trunk' into html-api/auto-escape-javascript-json
sirreal Jan 8, 2026
b436e71
Editing review of the escaping method docblock.
dmsnell Jan 9, 2026
95259d2
Fixup: Missed a couple of box drawing connections.
dmsnell Jan 9, 2026
91c019d
Fixup: Missed a curved corner
dmsnell Jan 9, 2026
09e3aa2
Tweak language in JS escaping comments
sirreal Jan 9, 2026
30d7747
Merge branch 'trunk' into html-api/auto-escape-javascript-json
sirreal Jan 12, 2026
c1c6067
Merge branch 'trunk' into html-api/auto-escape-javascript-json
sirreal Jan 12, 2026
292614d
Merge branch 'html-api/auto-escape-javascript-json' into scripts/use-…
sirreal Jan 12, 2026
6a6e1cc
Append trailing newline to output HTML not input
sirreal Jan 12, 2026
3f1ba32
Update dot script to clean up fonts, align labels, etc...
dmsnell Jan 12, 2026
f7715fd
Update note in `set_modifiable_text()` now that `</script` is allowed
dmsnell Jan 12, 2026
1015ace
Inline escaping to avoid WPCS-mandated misalignment.
dmsnell Jan 12, 2026
59ccdc1
Expand the explanation for why other strings are rejected
dmsnell Jan 12, 2026
ef34aa1
Explain that there are non-HTML SCRIPT tags.
dmsnell Jan 12, 2026
d018cdf
Wording changes to comment.
dmsnell Jan 12, 2026
c59028e
Make escape_javascript_script_contents private
sirreal Jan 13, 2026
ae14a44
Merge branch 'trunk' into html-api/auto-escape-javascript-json
sirreal Jan 13, 2026
6a5bc56
Tweak some langauge in "other" escaping
sirreal Jan 13, 2026
df2f663
fixup! Tweak some langauge in "other" escaping
sirreal Jan 13, 2026
adeb155
Merge branch 'html-api/auto-escape-javascript-json' into scripts/use-…
sirreal Jan 13, 2026
e884759
Merge branch 'trunk' into scripts/use-html-api-for-script-tags
sirreal Jan 13, 2026
8b11027
Merge branch 'trunk' into scripts/use-html-api-for-script-tags
sirreal Jan 13, 2026
1af9b2f
Add test for behavior of wp_get_inline_script_tag()
sirreal Jan 13, 2026
00c49a5
Add matching tests for wp_get_script_tag()
sirreal Jan 13, 2026
13141bc
Merge branch 'script-test-attribute-behavior' into HEAD
sirreal Jan 13, 2026
8789ace
Fix script attribute order and type handling
sirreal Jan 13, 2026
3858d8e
Use the force, Jon??
dmsnell Jan 13, 2026
0197abc
Document suprprising de-duplication
dmsnell Jan 13, 2026
8047003
Merge branch 'trunk' into script-test-attribute-behavior
sirreal Jan 14, 2026
f2ea738
Merge branch 'script-test-attribute-behavior' into scripts/use-html-a…
sirreal Jan 14, 2026
1be946e
Merge branch 'trunk' into scripts/use-html-api-for-script-tags
sirreal Jan 14, 2026
cea5f04
Add _doing_it_wrong in case script contents are unsafe
sirreal Jan 14, 2026
67ba19e
Improve function documentation
sirreal Jan 14, 2026
ed73a44
Return empty string in case the content is unsafe and cannot be escaped
sirreal Jan 14, 2026
faf77e3
Apply return type description suggestion
sirreal Jan 14, 2026
36537d9
Fix language in comments
sirreal Jan 14, 2026
049332a
Merge branch 'trunk' into scripts/use-html-api-for-script-tags
sirreal Jan 15, 2026
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
80 changes: 76 additions & 4 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2880,7 +2880,31 @@ function wp_get_script_tag( $attributes ) {
*/
$attributes = apply_filters( 'wp_script_attributes', $attributes );

return sprintf( "<script%s></script>\n", wp_sanitize_script_attributes( $attributes ) );
$processor = new WP_HTML_Tag_Processor( '<script></script>' );
$processor->next_tag();
foreach ( $attributes as $name => $value ) {
/*
* Lexical variations of an attribute name may represent the
* same attribute in HTML, therefore it’s possible that the
* input array might contain duplicate attributes even though
* it’s keyed on their name. Calling code should rewrite an
* attribute’s value rather than sending a duplicate attribute.
*
* Example:
*
* array( 'id' => 'main', 'ID' => 'nav' )
*
* In this example, there are two keys both describing the `id`
* attribute. PHP array iteration is in key-insertion order so
* the 'id' value will be set in the SCRIPT tag.
*/
if ( null !== $processor->get_attribute( $name ) ) {
continue;
}

$processor->set_attribute( $name, $value ?? true );
}
return "{$processor->get_updated_html()}\n";
}

/**
Expand All @@ -2901,13 +2925,27 @@ function wp_print_script_tag( $attributes ) {
* Constructs an inline script tag.
*
* It is possible to inject attributes in the `<script>` tag via the {@see 'wp_inline_script_attributes'} filter.
* Automatically injects type attribute if needed.
*
* If the `$data` is unsafe to embed in a `<script>` tag, an empty script tag with the provided
* attributes will be returned. JavaScript and JSON contents can be escaped, so this is only likely
* to be a problem with unusual content types.
*
* Example:
*
* // The dangerous JavaScript in this example will be safely escaped.
* // A string with the script tag and the desired contents will be returned.
* wp_get_inline_script_tag( 'console.log( "</script>" );' );
*
* // This data is unsafe and `text/plain` cannot be escaped.
* // The following will return `""` to indicate failure:
* wp_get_inline_script_tag( '</script>', array( 'type' => 'text/plain' ) );
*
* @since 5.7.0
* @since 7.0.0 Returns an empty string if the data cannot be safely embedded in a script tag.
*
* @param string $data Data for script tag: JavaScript, importmap, speculationrules, etc.
* @param array<string, string|bool> $attributes Optional. Key-value pairs representing `<script>` tag attributes.
* @return string String containing inline JavaScript code wrapped around `<script>` tag.
* @return string HTML script tag containing the provided $data or the empty string `""` if the data cannot be safely embedded in a script tag.
*/
function wp_get_inline_script_tag( $data, $attributes = array() ) {
$data = "\n" . trim( $data, "\n\r " ) . "\n";
Expand All @@ -2924,7 +2962,41 @@ function wp_get_inline_script_tag( $data, $attributes = array() ) {
*/
$attributes = apply_filters( 'wp_inline_script_attributes', $attributes, $data );

return sprintf( "<script%s>%s</script>\n", wp_sanitize_script_attributes( $attributes ), $data );
$processor = new WP_HTML_Tag_Processor( '<script></script>' );
$processor->next_tag();
foreach ( $attributes as $name => $value ) {
/*
* Lexical variations of an attribute name may represent the
* same attribute in HTML, therefore it’s possible that the
* input array might contain duplicate attributes even though
* it’s keyed on their name. Calling code should rewrite an
* attribute’s value rather than sending a duplicate attribute.
*
* Example:
*
* array( 'id' => 'main', 'ID' => 'nav' )
*
* In this example, there are two keys both describing the `id`
* attribute. PHP array iteration is in key-insertion order so
* the 'id' value will be set in the SCRIPT tag.
*/
if ( null !== $processor->get_attribute( $name ) ) {
continue;
}

$processor->set_attribute( $name, $value ?? true );
}

if ( ! $processor->set_modifiable_text( $data ) ) {
_doing_it_wrong(
__FUNCTION__,
__( 'Unable to set inline script data.' ),
'7.0.0'
);
return '';
}

return "{$processor->get_updated_html()}\n";
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/phpunit/tests/dependencies/wpInlineScriptTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,23 @@ public function test_script_tag_repeat_attributes() {
),
);
}

/**
* Test failure conditions setting inline script tag contents.
*
* @ticket 64500
*/
public function test_script_tag_dangerous_unescapeable_contents() {
$this->setExpectedIncorrectUsage( 'wp_get_inline_script_tag' );
/*
* </script> cannot be printed inside a script tag
* the `example/example` type is an unknown type with no known escaping rules.
* The only choice is to abort.
*/
$result = wp_get_inline_script_tag(
'</script>',
array( 'type' => 'example/example' )
);
$this->assertSame( '', $result );
}
}
Loading