🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@mgol
Copy link
Member

@mgol mgol commented Jul 15, 2021

Summary

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

-8 bytes

4.0 version of this PR: #4906

Checklist

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 jquerygh-4904
@mgol mgol added this to the 3.6.1 milestone Jul 15, 2021
@mgol mgol self-assigned this Jul 15, 2021
@mgol mgol changed the title Manipulation: Don't remove HTML comments from scripts Manipulation: Don't remove HTML comments from scripts (3.x version) Jul 15, 2021
Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
@mgol mgol removed the Needs review label Jul 19, 2021
@mgol mgol changed the title Manipulation: Don't remove HTML comments from scripts (3.x version) Manipulation: Don't remove HTML comments from scripts Jul 19, 2021
@mgol mgol merged commit 924b515 into jquery:3.x-stable Jul 19, 2021
@mgol mgol deleted the 3.x-html-comments-scripts branch July 19, 2021 17:15
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Development

Successfully merging this pull request may close these issues.

3 participants