From 2f8f39e457c32c454c50545b0fdaa1d7a4a2f8bd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Micha=C5=82=20Go=C5=82=C4=99biowski-Owczarek?= Date: Mon, 19 Jul 2021 19:04:23 +0200 Subject: [PATCH] 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 `` 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 also used to strip CDATA sections. However, 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. We've preserved that behavior for backwards compatibility in 3.x but we're removing it for 4.0. Fixes gh-4904 Closes gh-4906 --- src/manipulation.js | 6 ++---- test/data/cleanScript.html | 4 ++-- test/unit/manipulation.js | 24 ++++++++++++++++++------ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/manipulation.js b/src/manipulation.js index f86bd9ab0..19c60fcbc 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -25,9 +25,7 @@ var // Support: IE <=10 - 11+ // In IE using regex groups here causes severe slowdowns. - rnoInnerhtml = /\s*$/g; + rnoInnerhtml = / diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 131109448..8262516a9 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -2233,19 +2233,31 @@ QUnit.test( "domManip executes scripts containing html comments or CDATA (trac-9 "" ].join( "\n" ) ).appendTo( "#qunit-fixture" ); + // This test requires XHTML mode as CDATA is not recognized in HTML. + // jQuery( [ + // "" + // ].join( "\n" ) ).appendTo( "#qunit-fixture" ); + jQuery( [ "" ].join( "\n" ) ).appendTo( "#qunit-fixture" ); + // ES2015 in Annex B requires HTML-style comment delimiters (``) to act as + // single-line comment delimiters; i.e. they should be treated as `//`. + // See gh-4904 jQuery( [ "" ].join( "\n" ) ).appendTo( "#qunit-fixture" ); } ); -- 2.39.5