1

var c= {"Content":"</SCRIPT>"}

Gives parser error in Chrome. Escaping works ... </SCRIPT>

https://jsfiddle.net/OndrejSpilka327/banr9836/

Is it really chrome bug?

EDIT I don't think the argumentation is correct. HTML parser should have nothing to do with JavaScript parser. First of all, whatever is enclosed in should be parsed as JavaScript, not HTML. This is definitely wrong implementation of HTML parser.

For your curiosity:

var c= {"Content":"<SCRIPT></SCRIPT>"}
console.log(c.Content);

Produces the sam error in JSFiddle...and this is definitely well formed.

Again one can argue that SCRIPT tag can't occur inside outer SCRIPT tag, however whatever is enclosed in SCRIPT tags should be parsed as script not as HTML and especially if escaped in a regular string literal.

Such an argumentation only advocates bad implementation.

Obviously the behaviour produces errors when working with custom content serialized to json and persisted in SCRIPT tag.

Just curious what tags you removed Felix and why?

4
  • An object literal is not the same as JSON. You don’t have JSON here. Commented Oct 17, 2017 at 14:17
  • Well this object literal is JSON compatible. But to be precise, yes, this is object literal. Makes no difference. Commented Oct 18, 2017 at 18:37
  • 1
    "First of all, whatever is enclosed in <SCRIPT></SCRIPT> should be parsed as JavaScript, not HTML. This is definitely wrong implementation of HTML parser." — No. The JavaScript is inside the HTML. You either need to parse the HTML (here is the start tag, here is a text node, here is the end tag, now pass the text node to the JS parser to be treated as JS) or you need to recognise the start tag and then switch to parsing JS until you reach the end of the JS which has no way to mark "the end of the JS" so that's impossible and HTML+JS are consequently not designed to be parsed like that. Commented Oct 18, 2017 at 18:50
  • I disagree with the marking of duplicate on this. Commented Oct 31, 2017 at 12:53

1 Answer 1

2

No it’s not a Chrome bug. The HTML parser doesn’t know anything about JavaScript, it will close the <script> tag at the first occurrence of </script> that it finds. If that’s in the middle of a JavaScript program you end up with an invalid program.

Any character sequence that has a special meaning but should not interpreted with that special meaning needs to be escaped or split up.

See also Why split the <script> tag when writing it with document.write()?

Sign up to request clarification or add additional context in comments.

4 Comments

Well I don't think your explanation is correct. First of all, whatever is enclosed in <SCRIPT></SCRIPT> should be parsed as JavaScript, not HTML. This is definitely wrong implementation. Secondly, this code produces the same error:
"whatever is enclosed in <SCRIPT></SCRIPT> should be parsed as JavaScript, not HTML" It's not an HTML parsers job to parse JavaScript. In order to get the JavaScript code, the HTML document has to parsed first. That's the HTML parsers job. It then hands off the content of <script> tag to the JavaScript engine. If you this seems "wrong" to you then don't embed JavaScript into HTML but load an external JS file.
Maybe this simplified example helps: Consider string literals in JavaScript and this example: var str = "He said: "Get out"";. JavaScript is not going to be able to parse this because it thinks the string literal ends at the second ". JavaScript doesn't understand the content of string, similar to how the HTML parser doesn't understand the content of the <script> tag. In order to make the string literal valid you have to explicitly tell JavaScript to "ignore" the inner " by escaping them: var str = "He said: \"Get out\"";. You have to do something similar for the HTML parser.
ok, acceptable answer ;) What works is escaping with \. So <\/SCRIPT> is parsed well and works ok as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.