从 HTML 加载 DOM:HTML 解析器如何知道空元素何时结束

Loading DOM from HTML: How does a HTML parser know when empty element ends

在XML中,空元素有一个对应的标记为/>。但这在 HTML 中不存在。 HTML 解析器也有一个有限的可以为空的元素列表。如果这样的元素有结束标记怎么办?

So do a HTML parser have a finite list of elements that can be empty.

是的。 HTML 解析器知道 HTML 的规则,这些规则说明哪些元素没有结束标记(或可选的结束标记)。

例如:

A start tag whose tag name is one of: "area", "br", "embed", "img", "keygen", "wbr"
...
Insert an HTML element for the token. Immediately pop the current node off the stack of open elements.


What if such an element has an end tag?

那么它是无效的,并且与任何其他未打开的标记的结束标记一样,当解析器到达它时标记将被丢弃。

If node is not the current node, then this is a parse error.

html 中有些标签有结束标签,有些标签没有 one.And 引入 HTML5 后更加混乱。经过大量研究,这是我到目前为止的发现。我希望你能理解:)

do a HTML parser have a finite list of elements that can be empty.

Answer : Yes, HTML parsers have finite list of empty elements. The parser have certain rules for parsing and will ignore the empty tags.

这些是可以为空的元素。 (来源:- Mozilla documentation

<link>
<track>
<param>
<area>
<command>
<col>
<base>
<meta>
<hr>
<source>
<img>
<keygen>
<br>
<wbr>
<colgroup> when the span is present
<input>

在 HTML 中,在空元素上使用结束标记是无效的。例如,<input type="text"></input> 是无效的 HTML,解析器将忽略这些标签。

"Empty elements (void elements)" were introduced to HTML by mistake: presentational markup crept into the language, contrary to the spirit of SGML, and with some strange syntactic implications. This fundamental error has caused some technical problems like an unintended discrepancy between HTML and XHTML, causing surprises in validation. More importantly, it illustrates the implications of the decision to make HTML formally, and only formally, an "SGML application". "Empty elements" are more than they look like.

来源(值得一读):cs.tut.fi empty elements research paper )

What if such an element has an end tag?

The parser will ignore the element which has an end tag and it will consume the next element or character for parsing. And the parser will throw an ignored syntax error

阅读这篇 w3c 文章,它是关于 HTML 空元素(void elements)W3C Link

456bereastreet

关于空元素的文章

关于空元素的颜色眩光文章Colorglare link