简体   繁体   English

巢状 <p> 标签自动关闭/打开

[英]Nested <p> tag auto-closing/opening

I'm seeing some weird behavior around nested <p> tags. 我看到嵌套<p>标记周围有一些奇怪的行为。 See this for the test case. 请参阅测试用例。

Given: 鉴于:

<p>
  <p>
    <p>
      <p>
        <p>
          <p>
          </p>
        </p>
      </p>
    </p>
  </p>
</p>

In Chrome, this results in 在Chrome中,这导致

<p>
  </p><p>
    </p><p>
      </p><p>
        </p><p>
          </p><p>
          </p>
        <p></p>
      <p></p>
    <p></p>
  <p></p>
<p></p>

That is, it closes opening tags and opens closing tags. 即,它关闭开始标签并打开结束标签。 I've heard (of course) of "self-closing tags"; 我(当然)听说过“自动关闭标签”; I've never heard of "self-opening tags". 我从未听说过“自动打开标签”。

IE11 does it a different (and arguably, even wronger) way: IE11采取了不同的方式(甚至可以说是错误的):

<p>
  <p>
    <p>
      <p>
        <p>
          <p>
          </p>
        <p></p>
      <p></p>
    <p></p>
  <p></p>
<p></p>

So it does not auto-close opening tags, but it does auto-open close tags, resulting in an unbalanced DOM tree. 因此,它不会自动关闭打开的标签,但是会自动打开关闭的标签,从而导致DOM树不平衡。

Anyway, I'm curious what the proper path to address this; 无论如何,我很好奇解决这个问题的正确方法。 should I open issues on Chrome and IE bug trackers? 我应该在Chrome和IE错误跟踪器上打开问题吗? I'm not particularly well versed in the W3C specs, where would I go to try and find the "correct" behavior (I'm not aware of any tags apart from <p> that exhibit this behavior, although I haven't done extensive testing)? 我对W3C规范并不特别精通,在哪里可以尝试找到“正确的”行为(除了<p>之外,我还没有其他标签可以显示这种行为,尽管我还没有做过广泛的测试)?

For what it's worth, I realize nesting <p> tags is invalid; 对于它的价值,我意识到嵌套<p>标签是无效的; this is a result of a third party library that I am working to address with them, I just found this to be bizarre behavior). 这是由于我正在与第三方图书馆打交道,我只是发现这是一种奇怪的行为)。

EDIT: I was wrong about IE: see https://jsbin.com/ripaxe/5/edit?html,js,output . 编辑:我对IE的看法是错误的:请参阅https://jsbin.com/ripaxe/5/edit?html,js,output Turns out innerHTML is browser dependent. 事实证明innerHTML是依赖于浏览器的。 Didn't realize that. 没意识到。

This isn't a bug. 这不是错误。 <p> elements cannot be nested within other <p> elements as you've said. <p>元素不能嵌套在其他<p>元素中。

Chrome is attempting to fix your code by closing your <p> tags for you, and is thus generating this markup. Chrome正在尝试通过为您关闭<p>标签来修复您的代码,从而生成此标记。 I'd suggest raising a bug report with the third-party library you're using instead as this isn't a browser-specific problem. 我建议使用您正在使用的第三方库提出一个错误报告,因为这不是特定于浏览器的问题。

Your test case uses innerHTML to display the serializations of your markup. 您的测试用例使用innerHTML来显示标记的序列化。 Unsurprisingly IE tends to do things differently to other browsers, but neither browser's representation of your markup is wrong per se . 毫不奇怪,IE往往在做事上与其他浏览器有所不同,但是浏览器对您标记的表示本身都不对。

Chrome (and Firefox) inserts explicit </p> end tags to show exactly where each <p> start tag terminates. Chrome浏览器(和Firefox)插入明确的</p>结束标记,以准确显示每个<p>起始标记的终止位置。 Each start tag terminates immediately before the next start tag (except the last one, which is terminated by its end tag). 每个开始标记都在下一个开始标记之前终止(最后一个标记除外,后者由其结束标记终止)。 Notice that the whitespace is enclosed within each pair. 请注意,空格包含每对中。

But the explicit end tags are not needed, and IE simply chooses not to insert end tags to match any of the start tags for that reason. 但是不需要显式的结束标记,因此IE只是选择不插入结束标记以匹配任何开始标记。 That doesn't mean that there is any nesting going on — all of the p elements are still siblings, and you can verify this with the fact that document.querySelector('p p') returns null and document.querySelector('p:nth-child(11):last-child') returns the element represented by the very last </p> end tag in all browsers. 这并不意味着正在进行任何嵌套-所有p元素仍然是兄弟姐妹,您可以通过document.querySelector('p p')返回null和document.querySelector('p:nth-child(11):last-child')返回所有浏览器中最后一个</p>结束标记表示的元素。

The end tags that are in your original markup are preserved, of course, but the reason all but the first end tag are matched with their own start tags ( without enclosing the whitespace this time) is described in Why does a stray </p> end tag generate an empty paragraph? 当然,保留了原始标记中的结束标记,但是,除了第一个结束标记之外,所有其他标记都与它们自己的开始标记(这次包含空格)相匹配的原因在为何会迷路</ p>中进行了描述。 结束标记会生成一个空段? — in short, it's specced by HTML5 for consistency with legacy browser behavior. —简而言之,HTML5规定它与旧版浏览器行为保持一致。

The end tag for the p element being optional is also the reason why both representations are equally valid (and, in fact, functionally equivalent) HTML fragments. p元素的end标记是可选的,这也是两个表示形式均有效(并且实际上在功能上等效)HTML片段的原因。 So, there is nothing wrong with what IE is doing, but it does create headaches for authors relying on the innerHTML property simply because it does things differently. 因此,IE所做的事情没有错,但是它确实使依赖innerHTML属性的作者感到头痛,仅仅是因为它的行为有所不同。

But this is why you shouldn't rely on innerHTML. 但是,这就是为什么您不应该依赖innerHTML的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM