Html 我应该在 HTML5 中使用 <![CDATA[...]]> 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3302648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should I use <![CDATA[...]]> in HTML5?
提问by Darryl Hein
I'm pretty sure <![CDATA[...]]>
sections can be used in XHTML5, but what about HTML5?
我很确定<![CDATA[...]]>
可以在 XHTML5 中使用部分,但是 HTML5 呢?
采纳答案by hollsk
The CDATA
structure isn't really for HTML at all, it's for XML.
该CDATA
结构根本不是用于 HTML,而是用于 XML。
People sometimes use them in HTML inside script
tags because it removes the need for them to escape certain special characters. It's by no means a requirement, though (for either HTML 4 or 5).
人们有时在script
标签内的 HTML 中使用它们,因为它消除了对某些特殊字符进行转义的需要。但是(对于 HTML 4 或 5),这绝不是必需的。
Edit:This is where we open that really mouldy old can of worms from 2002 over whether you're sending XHTML as text/html
or as application/xhtml+xml
like you're “supposed” to :-)
编辑:这是我们从 2002 年开始打开那个真正发霉的旧蠕虫罐头的地方,无论您是按照“应该”发送 XHTMLtext/html
还是application/xhtml+xml
“应该”发送给 :-)
回答by Joel Purra
From the same page @pst linked to:
从同一页面@pst 链接到:
Element-specific parsing for
script
andstyle
tags, Guidance for XHTML-HTML compatibility: "The following code with escaping can ensure script and style elements will work in both XHTML and HTML, including older browsers."
script
和style
标签的元素特定解析,XHTML-HTML 兼容性指南:“以下带有转义的代码可以确保脚本和样式元素在 XHTML 和 HTML 中都可以使用,包括旧版浏览器。”
Maximum backwards compatibility:
最大的向后兼容性:
<script type="text/javascript"><!--//--><![CDATA[//><!--
...
//--><!]]></script>
Simpler version, sort of incompatible with "much older browsers":
更简单的版本,有点与“更旧的浏览器”不兼容:
<script>//<![CDATA[
...
//]]></script>
So, CDATA
can be used in HTML5, and it's recommended in the Guidance for XHTML-HTML compatibility. This useful for polyglotHTML/XML/XHTML pages, which are created and parsed as XML during development, but served as HTML5 for better cross-browser compatibility. Polyglot pages has their benefits, and I've used this myself, as it's much easier to debug XML/XHTML5. Google Chrome, for example, will throw an error for invalid XML/XHTML5 (including for example character escaping), whereas the same page served as HTML5 will "just work" aka "probably work".
因此,CDATA
可以在 HTML5 中使用,并且在XHTML-HTML 兼容性指南中推荐使用。这对于多语言HTML/XML/XHTML 页面很有用,这些页面在开发过程中被创建和解析为 XML,但作为 HTML5 提供更好的跨浏览器兼容性。多语言页面有其优点,我自己也使用过它,因为它更容易调试 XML/XHTML5。例如,谷歌浏览器会因无效的 XML/XHTML5 引发错误(包括例如字符转义),而作为 HTML5 的同一页面将“正常工作”,也就是“可能工作”。
回答by rmarscher
The spec seems to clear up this issue. script and style tags are considered to be "raw text elements." CDATA is not needed or allowed for them. CDATA is only used with "foreign content" - i.e. MathML and SVG. Note that there are some restrictions to what can go in the script tag -- basically you can't put something like var x = '</script>'
in there because it will close the tag and needs to be split like pst noted in his answer. http://www.w3.org/TR/html5/syntax.html#cdata-rcdata-restrictions
规范似乎澄清了这个问题。脚本和样式标签被认为是“原始文本元素”。他们不需要或不允许使用 CDATA。CDATA 仅用于“外国内容”——即 MathML 和 SVG。请注意,脚本标签中可以包含的内容存在一些限制——基本上你不能var x = '</script>'
在那里放置类似的东西,因为它会关闭标签并且需要像他的回答中提到的 pst 那样拆分。http://www.w3.org/TR/html5/syntax.html#cdata-rcdata-restrictions
回答by rmarscher
Perhaps see: http://wiki.whatwg.org/wiki/HTML_vs._XHTML
也许见:http: //wiki.whatwg.org/wiki/HTML_vs._XHTML
<![CDATA[...]]> is a a bogus comment.
<![CDATA[...]]> 是虚假评论。
In HTML, <script>
is already protected -- this is why sometimes it must be written as a = "<" + "/script>"
, to avoid confusing the browser. Note that the code is valid outsidea CDATA in HTML.
在 HTML 中,<script>
已经受到保护——这就是为什么有时必须将其写为a = "<" + "/script>"
,以避免混淆浏览器。请注意,该代码在 HTML 中的 CDATA之外是有效的。