Internet Explorer的HTML解析器会添加非结束标记吗? [英] Will Internet Explorer's HTML Parser ever add non-closing tags?

查看:98
本文介绍了Internet Explorer的HTML解析器会添加非结束标记吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我遇到一个奇怪的错误时,我正在为IE8和IE9中的错误修复一些错误。在HTML中有一个< section class =create> 标签,但是在页面加载之后,页面上会有更多的标签。起初,我认为有一些不好的JavaScript以某种方式克隆了部分,但我禁用了JavaScript,并且在页面加载后它仍然存在。这必须是创建此标签的HTML解析器,对吧?我知道HTML解析器将会添加< / div> / code>标签或< / html> 标签等,但它们会丢失,但会重新打开 或<因某种原因添加标签?如果是这样,这是什么原因?如果情况并非如此,那么当javascript被禁用时,HTML中不存在的标记可能会被添加到DOM中



<我正在处理的HTML页面超过20000行,并且整个地方肯定存在格式错误的HTML,其中任何一个都可能导致DOM呈现不正确。如果有一个片段可以添加到这个问题中,这会有所帮助,请在评论中告诉我。



以下是复制此问题的最小HTML。请注意,它只发生在表格标记中,而不是 div 标记。在表单元素关闭后,部分标记在IE8和IE9中重新打开,因此创建2 部分元素。

 <!DOCTYPE html> 
< html>
< head>< / head>
< body>
< form>
< section class =create>
CREATE SECTION
< / form>
< / body>
< / html>

以下是一个小提琴: http://jsfiddle.net/VurE6/



据记录,IE10,Chrome,Firefox和Safari不是显示这种行为(我知道)。

解决方案

在某种程度上,所有浏览器都会这样做。例如,试试这个

 < b> 1< i class =create> 2< / b> 3< / I> 

在任何浏览器中都可以看到两个使用create类创建的i元素。 p>

关于何时发生这种情况的规则非常复杂,但是当关闭标签不会终止其元素后代的效果时会发生这种情况。在IE9中,表单结束标记并未关闭该部分,因此为了保持该部分进行,浏览器必须创建第二个部分元素。



虽然这使得在b和i元素的情况下,对于section来说是没有意义的,所以它不会在任何实现HTML5解析算法(如IE10和现代Firefox和Chrome)的浏览器中发生,但显然在IE9中会发生。

请参阅 http://www.w3.org/html/wg/drafts/html/master/syntax.html#misnested-tags:-bi-/b-/i 关于HTML5如何处理misnested b和i标签问题。


I'm doing some bug fixing for an error in IE8 and IE9 when I came across a strange error. There is a <section class="create"> tag in the HTML, but after the page loads, there is the exact same tag a little farther down the page. At first, I thought there was some bad javascript cloning the section somehow, but I disabled javascript, and it was still there after the page loaded. This has to be the HTML parser creating this tag then, right? The rendered HTML does not match the HTML file at all.

I know that HTML parsers will add in </div> tags or </html> tags, etc., when they are missing, but will IE ever reopen or add a tag for some reason? If so, what causes this? If this isn't the case, what could be causing a tag that doesn't exist in the HTML to be added to the DOM when javascript is disabled?

The HTML page I'm working on is over 20,000 lines long, and there's surely malformed HTML all over the place, any of which could be causing the DOM to be rendered improperly. If there's a snippet I could add to this question that would help, let me know in the comments.

Here is the minimal HTML that replicates this problem. Note that it only happens within a form tag, and not a div tag. The section tag reopens itself in IE8 and IE9 after the form element is closed, therefore creating 2 section elements.

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <form>
            <section class="create">
                CREATE SECTION
        </form>
    </body>
</html>

And here's a fiddle: http://jsfiddle.net/VurE6/

For the record, IE10, Chrome, Firefox, and Safari aren't displaying this behavior (that I know of).

解决方案

To an extent, all browsers do this sort of thing. For example, try this

<b>1<i class="create">2</b>3</i>

in any browser and you should see two i elements created both with the "create" class.

The rules about when this happens are quite complex, but it happens when close tags don't end the effect of their element's descendants. In IE9, the form end tag is not closing the section, so to keep the section going, the browser has to create a second section element.

While this makes a certain sense in the case of the b and i elements, it makes no sense for section, and so it won't happen in any browser that implements the HTML5 parsing algorithm like IE10 and modern Firefox and Chrome, but does, apparently, in IE9.

See http://www.w3.org/html/wg/drafts/html/master/syntax.html#misnested-tags:-b-i-/b-/i for how HTML5 deals with the misnested b and i tags issue.

这篇关于Internet Explorer的HTML解析器会添加非结束标记吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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