Skip to content

HTML style comment breaks script #4904

@ovarn

Description

@ovarn

Description

$.append fails when I try to append script element with HTML style comment.

There is no errors when I try to do the same thing using vanila JS:

var script = document.createElement('script');
script.text = '<!-- html style comment in javascript -->';
document.querySelector('html').append(script); // no errors

When I do it using jQuery:

var script = document.createElement('script');
script.text = '<!--html style comment in javascript -->';
$('html').append(script); // Uncaught SyntaxError: Unexpected identifier

The root cause:
The domManip function does RegExp replacement before calling DOMEval and removes <!-- and -->:

rcleanScript = /^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g;

DOMEval( node.textContent.replace( rcleanScript, "" ), node, doc );

Link to test case

https://codepen.io/ovarn/pen/vYmgjzP (open browser console to see the error)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions