简体   繁体   English

由于某种原因,页面上的自定义html标记呈现跳过HTML解析

[英]Custom html tags on page render skip HTML parsing for some reason

I don't know, why it's happening, but looks like custom html tags cannot parse it's content properly on page load if there's really a lot of such elements. 我不知道,为什么会发生这种情况,但看起来自定义html标签无法在页面加载时正确解析它的内容,如果真的有很多这样的元素。

document.registerElement('x-tag', 
  {
    prototype: Object.create(HTMLElement.prototype, {
      attachedCallback: { value: function() {
        console.log(this.innerHTML, this.childNodes); // wrong innerHTML and childNodes once in n-occurrences 
      } 
    }})
  }
);

Here's an example 这是一个例子

My hypothesis is that there's some kind of stack, and sometimes this stack just overflows :) 我的假设是有某种堆栈,有时这个堆栈只是溢出:)

Do you have any ideas on how to fix it? 你对如何解决它有任何想法吗? (I'm already looking under the hood of react fiber.. to get the scheduling from there). (我已经在反应纤维的引擎盖下看了......从那里得到调度)。

It's because the elements are added to the DOM tree as they are parsed. 这是因为元素在解析时会添加到DOM树中。

Here the document is very large, so elements are not added in a single pass but in several chunks. 这里的文档非常大,因此元素不会在一次传递中添加,而是以几个块的形式添加。 Sometimes only 1 or 2 elements are added (at the end of the chunk) and then the Custom Element is created and attached whith a piece of its definitive child nodes only. 有时只添加1或2个元素(在块的末尾),然后创建自定义元素并仅附加一个其确定的子节点。

To fix it, you can define the custom element only after all the document is parsed. 要修复它,只有在解析完所有文档后才能定义自定义元素。 Put the <script> after the <x-tag>s , or use the onload event. <script>放在<x-tag>s ,或使用onload事件。

document.onload = function ()
{
    document.registerElement('x-tag', { prototype: proto } )
}

Else if for some reasons the Custom Element is already defined, put the numerous tags in a <template> element, then insert its content in a single operation: 否则,如果由于某些原因定义了自定义元素,则将众多标记放在<template>元素中,然后将其content插入到单个操作中:

<template id=tpl>
  <x-tag></x-tag><x-tag></x-tag><x-tag></x-tag><x-tag></x-tag><x-tag></x-tag><x-tag></x-tag><x-tag></x-tag><x-tag></x-tag><x-tag></x-tag>...
</template> 
<script>
    target.appendChild( document.importNode( tpl.content, true )
</script>

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

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