HTML5规范是否要忽略HTML注释中的CSS?

有人能告诉我
HTML5规范中的以下段落是什么意思吗?关于< style>的处理元素内容:

https://www.w3.org/TR/html5/document-metadata.html#the-style-element

All descendant elements must be processed, according to their
semantics, before the style element itself is evaluated. For styling
languages that consist of pure text (as opposed to XML), user agents
must evaluate style elements by passing the concatenation of the
contents of all the Text nodes that are children of the style element
(not any other nodes such as comments or elements), in tree order, to
the style system. For XML-based styling languages, user agents must
pass all the child nodes of the style element to the style system.

对我来说,这听起来像HTML解析器应删除< style>中的所有HTML元素和注释.在将结果文本发送到样式系统之前的元素.

HTML注释中的内容也是Text节点,但它不是style元素的直接子节点,因此不应包含在发送到样式系统的文本中.

现代浏览器似乎没有对样式元素中的注释或元素进行任何处理,而是将样式内容视为与HTML 4一致的CDATA.但HTML5规范中的这一段说明这是不正确的行为不是吗?如果不是我错过了什么?

最佳答案 将注释节点或元素节点添加到样式元素的唯一方法是通过DOM操作 – 在HTML解析器已经解析文档之后将注释或元素放入DOM中的样式元素.

所以规范并不是说HTML解析器应该删除< style> …< / style>中的所有HTML元素和注释.标记.如果规范打算明确说明它.

HTML解析器解析< style> …< / style>中的所有内容标记为文本 – 包括任何看起来像注释或看起来像元素的内容.

因此,HTML解析器没有任何注释或元素可以删除它 – 它只是文本.

Where in the spec does it say that the content is pure text?

html.spec.whatwg.org/multipage/syntax.html#raw-text-elements
说样式内容是“原始文本”.

The HTML 4 spec states clearly that the content of style elements is CDATA. That is what I am looking for but I can’t find it in the HTML5 spec.

当前HTML规范所称的“原始文本”与HTML4规范中的CDATA基本相同.

Where does it say that it is terminated by the string “</style“?

请参阅解析算法的以下步骤:

> https://html.spec.whatwg.org/multipage/syntax.html#rawtext-state
> https://html.spec.whatwg.org/multipage/syntax.html#rawtext-less-than-sign-state
> https://html.spec.whatwg.org/multipage/syntax.html#rawtext-end-tag-open-state
> https://html.spec.whatwg.org/multipage/syntax.html#rawtext-end-tag-name-state

最后一步参考the definition of “appropriate end tag token”

An appropriate end tag token is an end tag token whose tag name matches the tag name of the last start tag to have been emitted from this tokenizer, if any.

因此,在解析脚本内容的原始文本时,要发出的最后一个开始标记是< script>开始标记,因此“适当的结束标记令牌”是< / script>.

点赞