角度括号是否需要在嵌套样式元素中进行HTML转义? [英] Do Angle Brackets Need to be HTML-Escaped in Nested Style Elements?

查看:134
本文介绍了角度括号是否需要在嵌套样式元素中进行HTML转义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您在HTML文档中有以下内容:

 < style> 
ul.foo> li {
/ * etc * /
}
< / style>

是否有效的HTML语法 - 将大于孩子的选择符作为 $ gt;

 < style> 
ul.foo& gt; li {
/ * etc * /
}
< / style>

我明白这是不必要的,但它是好的形式吗?

HTML,但不是有效的CSS,因为嵌入的样式表根本不会被解析为HTML;它被解析为CSS,这意味着& gt; 可以从字面上看出来。 & gt; 不是有效的选择器,因此它不是有效的CSS。 HTML;在XHTML中, 有效,浏览器将将& gt; 解码为> ; 在将样式表解析为CSS之前。以下数据URI演示了这一点:

  data:application / xhtml + xml,< html xmlns = http://www.w3.org/1999/xhtml >< HEAD><风格> ul.foo&安培; GT;李{颜色:红色}< /风格>< /头>< body>< ul class =foo>< li> Foo< / li>< / ul>< / body>< / html> 

然而,就像在HTML中一样,在这个特定的上下文中没有必要,因为在您链接到的问题,但它仍被认为是格式良好的XHTML。但通常情况下,将嵌入式脚本和样式表放入CDATA部分比编码每个< > &

 < style> 
/ *<![CDATA [* /
ul.foo> li {
/ * etc * /
}
/ *]]> * /
< / style>


Suppose you have the following in your HTML document:

<style>
  ul.foo > li {
    /* etc */
  }
</style>

Is it valid syntax to HTML-escape the greater-than child selector as $gt;?

<style>
  ul.foo &gt; li {
    /* etc */
  }
</style>

I understand it's not necessary, but is it good form?

解决方案

It's valid HTML, but not valid CSS, because the embedded stylesheet isn't parsed as HTML at all; it's parsed as CSS, meaning that the &gt; is seen literally. &gt; is not a valid selector, and therefore it's not valid CSS.

This is only true of HTML; in XHTML, it is valid and browsers will decode the &gt; into > before parsing the stylesheet as CSS. The following data URI demonstrates this:

data:application/xhtml+xml,<html xmlns="http://www.w3.org/1999/xhtml"><head><style>ul.foo&gt;li{color:red}</style></head><body><ul class="foo"><li>Foo</li></ul></body></html>

Just like in HTML, though, it's not necessary in this specific context, for the reason given in the question you link to, but it's still considered well-formed XHTML. But generally, putting embedded scripts and stylesheets into CDATA sections is preferred over encoding every single <, > and &:

<style>
/* <![CDATA[ */
  ul.foo > li {
    /* etc */
  }
/* ]]> */
</style>

这篇关于角度括号是否需要在嵌套样式元素中进行HTML转义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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