JSON中的结束脚本标记会导致Chrome解析器错误 [英] Ending script tag in JSON causing chrome parser error

查看:50
本文介绍了JSON中的结束脚本标记会导致Chrome解析器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

给出Chrome中的解析器错误.转义工作...</SCRIPT>

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

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

这真的是chrome漏洞吗?

Is it really chrome bug?

编辑我认为论点是不正确的.HTML解析器应该与JavaScript解析器无关.首先,其中包含的所有内容都应解析为JavaScript,而不是HTML.这绝对是HTML解析器的错误实现.

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.

出于好奇:

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

在JSFiddle中产生sam错误...这肯定是格式正确的.

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

再次有人认为,SCRIPT标记不能出现在外部SCRIPT标记内,但是SCRIPT标记中包含的任何内容都应解析为脚本,而不是HTML格式,尤其是在以常规字符串文字形式进行转义的情况下.

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.

显然,当使用序列化为json并保留在SCRIPT标签中的自定义内容时,此行为会产生错误.

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

只是好奇您删除了Felix哪些标签,为什么?

Just curious what tags you removed Felix and why?

推荐答案

否,这不是Chrome错误.HTML解析器对JavaScript一无所知,它将在找到的</script> 首次出现时关闭< script> 标记.如果那是在JavaScript程序的中间,那么您将得到一个无效的程序.

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.

另请参见为什么拆分<脚本>使用document.write()编写标签时会使用该标签?

这篇关于JSON中的结束脚本标记会导致Chrome解析器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆