HTML 中的 CDATA 是什么?

发布于 2024-11-29 22:13:21 字数 150 浏览 0 评论 0原文

JavaScript 标签和 HTML 中的 CDATA 有什么用?

<script type="text/javascript"> 
// <![CDATA[

// ]]>
</script> 

What is the use of CDATA inside JavaScript tags and HTML?

<script type="text/javascript"> 
// <![CDATA[

// ]]>
</script> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

口干舌燥 2024-12-06 22:13:21

XML 文档中的所有文本都将由解析器进行解析。

但是 CDATA 部分内的文本将被解析器忽略。

CDATA - (未解析的)字符数据

术语 CDATA 用于描述不应由 XML 解析器解析的文本数据。

像“<”这样的字符和“&” XML 元素中是非法的。

“<”将生成错误,因为解析器将其解释为新元素的开始。

“&”将生成错误,因为解析器将其解释为字符实体的开头。

某些文本(例如 JavaScript 代码)包含大量“<”或“&”人物。为了避免错误,脚本代码可以定义为 CDATA。

解析器会忽略 CDATA 部分内的所有内容。

CDATA 部分以“”开头,以“]]>”结尾


在程序输出中使用 CDATA

如果 Web 浏览器将文档呈现为 HTML,XHTML 文档中的 CDATA 部分可能会以不同的方式进行解析,因为 HTML 解析器无法识别 CDATA 开始和结束标记,也无法识别 HTML 实体引用,例如

简短的 SGML 教程

另请参阅有关 CDATA 的维基百科条目

All text in an XML document will be parsed by the parser.

But text inside a CDATA section will be ignored by the parser.

CDATA - (Unparsed) Character Data

The term CDATA is used about text data that should not be parsed by the XML parser.

Characters like "<" and "&" are illegal in XML elements.

"<" will generate an error because the parser interprets it as the start of a new element.

"&" will generate an error because the parser interprets it as the start of an character entity.

Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script code can be defined as CDATA.

Everything inside a CDATA section is ignored by the parser.

A CDATA section starts with "<![CDATA[" and ends with "]]>"

Use of CDATA in program output

CDATA sections in XHTML documents are liable to be parsed differently by web browsers if they render the document as HTML, since HTML parsers do not recognise the CDATA start and end markers, nor do they recognise HTML entity references such as < within <script> tags. This can cause rendering problems in web browsers and can lead to cross-site scripting vulnerabilities if used to display data from untrusted sources, since the two kinds of parsers will disagree on where the CDATA section ends.

A brief SGML tutorial.

Also, see the Wikipedia entry on CDATA.

私藏温柔 2024-12-06 22:13:21

CDATA 在 HTML 中没有任何意义。

CDATA 是一个 XML 构造,它将通常为 #PCDATA(已解析的字符数据)的标记内容设置为 #CDATA(即未解析的字符数据)。它仅在 XHTML 中相关且有效。

它用在 script 标记中以避免解析 <&。在 HTML 中,这是不需要的,因为在 HTML 中,script 已经是#CDATA。

CDATA has no meaning at all in HTML.

CDATA is an XML construct which sets a tag's contents that is normally #PCDATA - parsed character data, to be instead taken as #CDATA, that is, non-parsed character data. It is only relevant and valid in XHTML.

It is used in script tags to avoid parsing < and &. In HTML, this is not needed, because in HTML, script is already #CDATA.

塔塔猫 2024-12-06 22:13:21

CDATA 已已过时

请注意,CDATA 部分不应在 HTML 中使用;它们仅适用于 XML。

因此,请勿在 HTML 5 中使用它。

https://developer.mozilla.org/en-US/docs/Web/API/CDATASection#Specifications

来自 MDN 的屏幕截图

CDATA is Obsolete.

Note that CDATA sections should not be used within HTML; they only work in XML.

So do not use it in HTML 5.

https://developer.mozilla.org/en-US/docs/Web/API/CDATASection#Specifications

Screenshot from MDN

南冥有猫 2024-12-06 22:13:21

来自http://en.wikipedia.org/wiki/CDATA

因为能够使用小于号 (<) 和
网页脚本中的与号 (&),以及较小程度的样式,
不必记住转义它们,通常使用 CDATA
内联文本和元素周围的标记
XHTML 文档。但这样文档也可以被HTML解析
解析器,无法识别 CDATA 标记,CDATA 标记
通常被注释掉,如以下 JavaScript 示例所示:

<script type="text/javascript">
//<![CDATA[
document.write("<");
//]]>
</script>

From http://en.wikipedia.org/wiki/CDATA:

Since it is useful to be able to use less-than signs (<) and
ampersands (&) in web page scripts, and to a lesser extent styles,
without having to remember to escape them, it is common to use CDATA
markers around the text of inline and elements in
XHTML documents. But so that the document can also be parsed by HTML
parsers, which do not recognise the CDATA markers, the CDATA markers
are usually commented-out, as in this JavaScript example:

<script type="text/javascript">
//<![CDATA[
document.write("<");
//]]>
</script>
猫九 2024-12-06 22:13:21

一种编写 HTML 和 XHTML 公共子集的方法

希望获得更大的可移植性。

在 HTML 中, 出现。

所以你可以写:

<script>x = '<br/>';

并且
不会被视为标签。

这就是为什么诸如:之类的字符串

x = '</scripts>'

必须像这样进行转义:

x = '</scri' + 'pts>'

请参阅: 为什么要拆分

但是 XML(以及 XHTML,它是 XML 的“子集”,与 HTML 不同),没有那种魔力:
将被视为标签。

是 XHTML 的表达方式:

在下一个]]>之前不要解析任何标签,将其视为字符串

添加 // 是为了使 CDATA 在 HTML 中也能正常工作。

在 HTML 中 并不神奇,因此它将由 JavaScript 运行。所以用//来注释掉它。

XHTML 也可以看到 //,但会将其视为空注释行,这不是问题:

//

也就是说:

  • 兼容的浏览器应该从初始文档类型
  • 兼容的网站可以依赖于兼容的浏览器,并使用单个有效的脚本协调文档类型 :

但这违反了互联网的黄金法则

不要信任第三方,否则你的产品将会损坏

A way to write a common subset of HTML and XHTML

In the hope of greater portability.

In HTML, <script> is magic escapes everything until </script> appears.

So you can write:

<script>x = '<br/>';

and <br/> won't be considered a tag.

This is why strings such as:

x = '</scripts>'

must be escaped like:

x = '</scri' + 'pts>'

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

But XML (and thus XHTML, which is a "subset" of XML, unlike HTML), doesn't have that magic: <br/> would be seen as a tag.

<![CDATA[ is the XHTML way to say:

don't parse any tags until the next ]]>, consider it all a string

The // is added to make the CDATA work well in HTML as well.

In HTML <![CDATA[ is not magic, so it would be run by JavaScript. So // is used to comment it out.

The XHTML also sees the //, but will observe it as an empty comment line which is not a problem:

//

That said:

  • compliant browsers should recognize if the document is HTML or XHTML from the initial doctype <!DOCTYPE html> vs <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  • compliant websites could rely on compliant browsers, and coordinate doctype with a single valid script syntax

But that violates the golden rule of the Internet:

don't trust third parties, or your product will break

谷夏 2024-12-06 22:13:21

CDATA 是文档字符集中的字符序列,并且可能包括字符实体。用户代理应按如下方式解释属性值:

  • 用字符替换字符实体,

  • 忽略换行符,

  • 将每个回车符或制表符替换为一个空格。

CDATA is a sequence of characters from the document character set and may include character entities. User agents should interpret attribute values as follows:

  • Replace character entities with characters,

  • Ignore line feeds,

  • Replace each carriage return or tab with a single space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文