Skip to content

Commit 924b515

Browse files
authored
Manipulation: Don't remove HTML comments from scripts
When evaluating scripts, jQuery strips out the possible wrapping HTML comment and a CDATA section. However, all supported browsers are already doing that when loading JS via appending a script tag to the DOM which is how we've been doing `jQuery.globalEval` since jQuery 3.0.0. jQuery logic was imperfect, e.g. it just stripped the `<!--` and `-->` markers, respectively at the beginning or the end of the script contents. However, browsers are also stripping everything following those markers in the same line, treating them as single-line comments delimiters; this is now also mandated by ECMAScript 2015 in Annex B. Instead of fixing the jQuery logic, just let the browser do its thing. We still need to strip CDATA sections for backwards compatibility. This shouldn't be needed as in XML documents they're already not visible when inspecting element contents and in HTML documents they have no meaning but we're preserving that logic for backwards compatibility. This will be removed completely in 4.0. Fixes gh-4904 Closes gh-4905 Ref gh-4906
1 parent f12cac6 commit 924b515

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/manipulation.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ var
4040

4141
// checked="checked" or checked
4242
rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i,
43-
rcleanScript = /^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g;
43+
44+
rcleanScript = /^\s*<!\[CDATA\[|\]\]>\s*$/g;
4445

4546
// Prefer a tbody over its parent table for containing new rows
4647
function manipulationTarget( elem, content ) {
@@ -195,6 +196,12 @@ function domManip( collection, args, callback, ignored ) {
195196
}, doc );
196197
}
197198
} else {
199+
200+
// Unwrap a CDATA section containing script contents. This shouldn't be
201+
// needed as in XML documents they're already not visible when
202+
// inspecting element contents and in HTML documents they have no
203+
// meaning but we're preserving that logic for backwards compatibility.
204+
// This will be removed completely in 4.0. See gh-4904.
198205
DOMEval( node.textContent.replace( rcleanScript, "" ), node, doc );
199206
}
200207
}

test/unit/manipulation.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,7 @@ QUnit.test( "domManip plain-text caching (trac-6779)", function( assert ) {
22682268

22692269
QUnit.test( "domManip executes scripts containing html comments or CDATA (trac-9221)", function( assert ) {
22702270

2271-
assert.expect( 3 );
2271+
assert.expect( 4 );
22722272

22732273
jQuery( [
22742274
"<script type='text/javascript'>",
@@ -2293,6 +2293,17 @@ QUnit.test( "domManip executes scripts containing html comments or CDATA (trac-9
22932293
"//--><!]]>",
22942294
"</script>"
22952295
].join( "\n" ) ).appendTo( "#qunit-fixture" );
2296+
2297+
// ES2015 in Annex B requires HTML-style comment delimiters (`<!--` & `-->`) to act as
2298+
// single-line comment delimiters; i.e. they should be treated as `//`.
2299+
// See gh-4904
2300+
jQuery( [
2301+
"<script type='text/javascript'>",
2302+
"<!-- Same-line HTML comment",
2303+
"QUnit.assert.ok( true, '<!-- Same-line HTML comment' );",
2304+
"-->",
2305+
"</script>"
2306+
].join( "\n" ) ).appendTo( "#qunit-fixture" );
22962307
} );
22972308

22982309
testIframe(

0 commit comments

Comments
 (0)