为什么流浪 </p>结束标签生成一个空段落? [英] Why does a stray </p> end tag generate an empty paragraph?

查看:26
本文介绍了为什么流浪 </p>结束标签生成一个空段落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,如果您有一个 </p> 结束标记,并且 body 元素中没有匹配的开始标记,那么大多数浏览器(如果不是全部)都会生成一个空段落取而代之:

Apparently, if you have a </p> end tag with no matching start tag within the body element, most if not all browsers will generate an empty paragraph in its place:

<!DOCTYPE html>
<title></title>
<body>
</p>
</body>

即使结束标记周围存在任何文本,它也不会成为此 p 元素的一部分——它始终为空,文本节点将始终独立存在:

Even if any text exists around the end tag, none of it is made part of this p element — it will always be empty and the text nodes will always exist on their own:

<!DOCTYPE html>
<title></title>
<body>
some text</p>more text
</body>

如果body的上述内容被包裹在<p></p>标签中......我会让你猜会发生什么:

If the above contents of body are wrapped in <p> and </p> tags... I'll leave you to guess what happens:

<!DOCTYPE html>
<title></title>
<body>
<p>some text</p>more text</p>
</body>

有趣的是,如果 </p> 标签前面没有 </body> 标签,除 IE9 及更早版本之外的所有浏览器都不会生成一个空段落(另一方面,IE ≤ 9 将始终创建一个,而 IE10 及更高版本的行为与所有其他浏览器相同):

Interestingly, if the </p> tag is not preceded by a <body> or </body> tag, all browsers except IE9 and older will not generate an empty paragraph (IE ≤ 9 on the other hand will always create one, while IE10 and later behave the same as all other browsers):

<!DOCTYPE html>
<title></title>
</p>

<!DOCTYPE html>
<title></title>
</p><body>

<!DOCTYPE html>
<title></title>
</p></body>

我找不到任何参考文献规定没有相应开始标记的结束标记应该生成一个空元素,但考虑到它甚至不是有效的 HTML,这不应该让人感到惊讶.事实上,我发现浏览器只使用 p 元素(在某种程度上也是 br 元素!),但没有任何解释为什么.

I can't find any references stipulating that an end tag with no corresponding start tag should generate an empty element, but that shouldn't come across as surprising considering that it's not even valid HTML in the first place. Indeed, I've only found browsers to do this with the p element (and to some extent the br element as well!), but not any explanation as to why.

它在使用传统 HTML 解析器和 HTML5 解析器的浏览器之间是相当一致的,但在 quirks 模式和标准模式下都适用.因此,推断这是为了与早期规范或遗留行为向后兼容可能是公平的.

It is rather consistent across browsers using both traditional HTML parsers and HTML5 parsers, though, applying both in quirks mode and in standards mode. So, it's probably fair to deduce that this is for backward compatibility with early specifications or legacy behavior.

事实上,我确实在对 一个有点相关的问题,基本上证实了这一点:

In fact, I did find this comment on an answer to a somewhat related question, which basically confirms it:

为什么<p>标签是有效的未封闭的,原来是 <p>被定义为新段落"标记,而不是 p 作为容器元素.相当于 <br>作为新行"标记.您可以在 1992 年的本文档中看到如此定义:http://www.w3.org/History/19921103-hypertext/hypertext/WWW/MarkUp/Tags.html 和这个来自 1993 年的:http://www.w3.org/MarkUp/draft-ietf-iiir-html-01.txt 因为有网页在更改之前,浏览器解析器始终尽可能向后兼容现有的 Web 内容,因此始终可以使用 <p>那样.

The reason why <p> tags are valid unclosed is that originally <p> was defined as a "new paragraph" marker, rather than p being a container element. Equivalent to <br> being a "new line" marker. You can see so defined in this document from 1992:http://www.w3.org/History/19921103-hypertext/hypertext/WWW/MarkUp/Tags.html and this one from 1993: http://www.w3.org/MarkUp/draft-ietf-iiir-html-01.txt Because there were web pages pre-dating the change and browser parsers have always been as backward compatible as possible with existing web content, it's always stayed possible to use <p> that way.

但它并没有完全解释为什么解析器将显式的 </p> 结束标记(带斜杠)视为简单的……一个标记,并在 DOM 中生成一个空元素.当语法没有像更多 最近或其他?如果是这样,它是否在任何地方都有记录?

But it doesn't quite explain why parsers treat an explicit </p> end tag (with the slash) as simply... a tag, and generate an empty element in the DOM. Is this part of some parser error handling convention from way back when the syntax wasn't as strictly defined as it was more recently or something? If so, is it documented anywhere at all?

推荐答案

HTML5 中记录了它是必需的.参见 http://w3c.github.io/html/syntax.html#the-in-body-insertion-mode 并向下搜索标签名称为p"的结束标签,它说:

如果打开元素的堆栈没有按钮范围内的元素具有与令牌相同的标签名称,那么这是一个解析错误;就好像看到了标签名称为p"的开始标签一样,然后重新处理当前令牌.

If the stack of open elements does not have an element in button scope with the same tag name as that of the token, then this is a parse error; act as if a start tag with the tag name "p" had been seen, then reprocess the current token.

翻译成英文意味着如果 </p> 标签不能与现有的 <p><匹配,则创建一个 p 元素/code> 标签.

Which translated into English means create a p element if the </p> tag can't be matched with an existing <p> tag.

为什么是这样,更难确定.通常,这是因为过去的某些浏览器将其作为错误发生,并且网页开始依赖该行为,因此其他浏览器也必须实现它.

Why it is so, is harder to ascertain. Usually, this is because some browser in the past caused this to happen as a bug, and web pages came to rely on the behaviour, so other browsers had to implement it too.

这篇关于为什么流浪 &lt;/p&gt;结束标签生成一个空段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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