I have a custom element <my-el>
structurally similar to this (purely static, server-rendered HTML):
<my-el>
<div>a</div>
<div>b</div>
<div>c</div>
<div>d</div>
</my-el>
Since Chrome does not guarantee child element availability in the connectedCallback
of custom elements, I'm using HTMLParsedElement
which basically delays custom element initialization using the following steps:
- Test if the element (or any ancestor element) has a
nextSibling
(in which case the parser assumably has passedmy-el
), or ifDOMContentLoaded
has been reached (a.k.a.document.readyState !== 'loading'
). - If none of the above, set up a
MutationObserver
on thechildList
that re-checks the above conditions.
The problem that currently remains with the outlined strategy is that the MutationObserver
might get fired when this is the HTML available:
<my-el>
<div>a</div>
<div>b</div>
</my-el>
Or even this
<my-el>
<div>a</div>
</my-el>
In those cases the MutationObserver gets fired several times, and the handler cannot know when the end of </my-el>
is actually reached.
Question: Does anyone know if wrapping all the inner div
elements like this would solve this problem:
<my-el>
<div>
<div>a</div>
<div>b</div>
<div>c</div>
<div>d</div>
</div>
</my-el>
Or in other words, when the mutation observer fires for this structure, can I reliably assume the wrapping child div being fully available including all its descendant nodes?