我应该使用“]]>”或“//]]>”用于将CDATA节关闭为xHTML [英] Should I use "]]>" or "//]]>" for closing a CDATA section into xHTML

查看:127
本文介绍了我应该使用“]]>”或“//]]>”用于将CDATA节关闭为xHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可以使用标有CDATA的部分来做到这一点。



根据 http://www.w3.org/ TR / xhtml1 /#h-4.8 CDATA区段可以定义为:

 < script type = text / javascript> 
<![CDATA [
...未转义的脚本内容...
]]>
< / script>

然后,根据 http://www.w3schools.com/TAGS/tag_script.asp ,CDATA可以如下所示:

 < script type =text / javascript><![CDATA [
// some code
//]]&脚本>

关闭CDATA部分的方法更好? ]]> //]]>

解决方案


根据www.w3.org/TR/xhtml1/#h-4.8,CDATA节可以定义为:[no //]


是的。在XHTML中,他们可以。正确的XHTML,由XML解析器读取,例如当您向不是IE的Web浏览器提供 application / xhtml + xml 时。



但是你可能实际上是作为 text / html ,这意味着你的浏览器不是一个'XML处理器'。这是一个旧版HTML4解析器,因此您必须遵守附录C指南并避免在HTML4中不起作用的任何XML功能。



特别是,字符串<![CDATA [< script> < style>中的 block对于HTML4解析器来说不是特殊的,因为在HTML4中,这两个元素是CDATA元素,其中标记不适用(除了< / ETAGO序列来结束元素本身)。因此,HTML4解析器会将这些字符串直接发送到CSS或JavaScript引擎。



因为<![CDATA [不是有效的JS,你会得到一个JavaScript语法错误。 (其他答案在这里是错误的:不仅仅是旧的浏览器,但所有HTML4浏览器,这将导致一个未注释CDATA部分在脚本中的错误。)



您可以使用 // / * 注释标记来隐藏JavaScript或CSS中的内容发动机。所以:

 < script type =text / javascript> //<![CDATA [
警报('a& b');
//]]>< / script>

(请注意领导 // 在W3Schools示例代码中被省略,并且使示例代码不起作用。失败。不信任W3Schools:它们与W3C无关,他们的材料通常是垃圾。)



这是由HTML解析器读取的:




  • 开放标签脚本在下一个ETAGO之前建立CDATA内容

  • 文本 //<![CDATA [\\\
    alert('a& b'); \\ n //]]>

  • ETAGO和关闭标签脚本

  • - >发送到JavaScript引擎的结果内容: // <![CDATA [\\\
    alert('a& b'); \\\
    //]]& / code>



但由XML解析器:




  • 打开标签脚本(无需解释特殊解释)

  • 文本内容 //

  • 打开CDATA节,建立CDATA内容,直到下一个]]> 序列

  • 文字 \\\
    alert('a& b'); \\\
    //

  • 关闭CDATA区段

  • 关闭标签脚本

  • - > : // \\\
    alert('a& b'); \\\
    //



虽然解析过程是完全不同的,JS引擎最终得到相同的有效代码在每种情况下,因为感谢 //



注意,这是一个非常不同的情况下老学校:

 < script type =text / javascript><! -  
alert('a& b');
// - >< / script>

这是为了隐藏脚本/样式内容,使其不会被写入浏览器的页面不理解< script> < style> 标记。这不会生成JavaScript / CSS错误,因为一个黑客被置于不同的层次:它是CSS和JavaScript语言本身的语法特征,< ;!



这些浏览器是古代历史;你绝对今天不应该使用这种技术。特别是在XHTML中,作为一个XML解析器,你会把你的话,把整个脚本块转换成一个XML注释,而不是可执行代码。



避免这样做,你会更快乐。

您需要< & 才能 / code>中的字符?/ code> < style>不,几乎没有。你真的需要他们在< script>



但是说实话,XHTML兼容性指南C.4是适用的到HTML4,因为它是XHTML1:任何不重要的应该是外​​部脚本,然后你不必担心任何这一点。


I want to inline scripts or CSSs into XHTML without escaping special characters.

I can do that using a CDATA marked section.

According to http://www.w3.org/TR/xhtml1/#h-4.8 the CDATA section can be defined as:

   <script type="text/javascript">
      <![CDATA[
         ... unescaped script content ...
      ]]>
   </script>

Then, according to http://www.w3schools.com/TAGS/tag_script.asp, the CDATA can look like:

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

Which method for closing the CDATA section is better? ]]> or //]]> ?

解决方案

According to www.w3.org/TR/xhtml1/#h-4.8 the CDATA section can be defined as: [no //]

Yeah. In XHTML, they can. Proper XHTML, as read by an XML parser like when you serve application/xhtml+xml to a web browser that isn't IE.

But probably you're actually serving as text/html, which means your browser isn't an ‘XML processor’ as referenced in that section. It's a legacy-HTML4 parser, so you have to abide by the appendix C guidelines and avoid any XML features that don't work in HTML4.

In particular, the strings <![CDATA[ and ]]> in a <script> or <style> block are not special to an HTML4 parser, because in HTML4 those two elements are ‘CDATA elements’ where markup doesn't apply (except for the </ ETAGO sequence to end the element itself). So an HTML4 parser will send those strings straight to the CSS or JavaScript engine.

Because <![CDATA[ is not valid JS, you'll get a JavaScript syntax error. (The other answers are wrong here: it's not just very old browsers, but all HTML4 browsers, that will give errors for an uncommented CDATA section in script.)

You use the // or /* comment markup to hide the content from the JavaScript or CSS engine. So:

<script type="text/javascript">//<![CDATA[
    alert('a&b');
//]]></script>

(Note the leading //; this was omitted in the W3Schools example code, and makes that example code not work at all. Fail. Don't trust W3Schools: they are nothing to do with W3C and their material is often rubbish.)

This is read by an HTML parser as:

  • Open-tag script establishing CDATA content until the next ETAGO
  • Text //<![CDATA[\n alert('a&b');\n//]]>
  • ETAGO and close-tag script
  • -> resultant content sent to JavaScript engine: //<![CDATA[\nalert('a&b');\n//]]>

But by an XML parser as:

  • Open-tag script (no special parsing implications)
  • Text content //
  • Open CDATA section establishing CDATA content until the next ]]> sequence
  • Text \n alert('a&b');\n//
  • Close CDATA section
  • Close-tag script
  • -> resultant content sent to JavaScript engine: //\nalert('a&b');\n//

Whilst the parsing process is quite different, the JS engine ends up with the same effective code in each case, as thanks to the //​s the only difference is in the comments.

Note this is a very different case to the old-school:

<script type="text/javascript"><!--
    alert('a&b');
//--></script>

which was to hide script/style content so that it didn't get written onto the page in browsers that didn't understand <script> and <style> tags. This will not generate a JavaScript/CSS error, because a hack was put it at a different level: it is a syntactical feature of the CSS and JavaScript languages themselves that <!-- is defined to do nothing, allowing this hack to work.

Those browsers are ancient history; you absolutely should not use this technique today. Especially in XHTML, as an XML parser would take you at your word, turning the whole script block into an XML comment instead of executable code.

I want to inline Scripts or CSSs into xHTML without escaping special characters.

Avoid doing this and you will be much happier.

Do you really need the < and & characters in a <style>? No, almost never. Do you really need them in <script>? Well... sometimes, yeah, and in that case the commented-CDATA-section is acceptable.

But to be honest, XHTML compatibility guideline C.4 is as applicable to HTML4 as it is to XHTML1: anything non-trivial should be an in external script, and then you don't have to worry about any of this.

这篇关于我应该使用“]]>”或“//]]&gt;”用于将CDATA节关闭为xHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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