Recursively iterating over documentfragment nodes and all child nodes
Recursively iterating over documentfragment nodes and all child nodes
I'm parsing an html string via createContextualFragment
:
createContextualFragment
let DocumentFragmentDom = IframeDocument.createRange().createContextualFragment(this.Html);
And i'm trying to append each node separately to my ifame like this:
while(DocumentFragmentDom.children.length > 0) {
if(DocumentFragmentDom.children[0].nodeName.toLowerCase() === "script" &&
DocumentFragmentDom.children[0].hasAttribute("src"))
{
DocumentFragmentDom.children[0].addEventListener("load",RecursiveAppending.bind(this));
IframeDocument.body.appendChild(DocumentFragmentDom.children[0]);
return;
}
IframeDocument.body.appendChild(DocumentFragmentDom.children[0]);
}
you can see that if the node is a script tag with the src
attribute i will add an eventlistener
for the load event, and the callback is the same function, the reason i'm doing this is because if i have got in my html code 2 script tags , one is using external resource and the other is inline , if i will not wait for the load event for the external tag the inline will run before it even if the inline tag is below the external.
src
eventlistener
With this code everything is working perfectly but I would like to be able to append individual nodes and when the node has children
, I would like to append the parent node first and than each children and if the children
have children
I would like to continue doing this.
children
children
children
I also need to take in consideration the eventlistener
for the script tag because if i encounter a script tag with src
I need to use a callback and stop appending until the event is fired and than I need to continue in the exact same point.
one more thing to consider is when you append a node from documentfragment
to another document the node will be moved from the documentfragment
to the new document so I'm not sure how exactly I should do that, maybe clone each node?
eventlistener
src
documentfragment
documentfragment
@CBroe where should i store them? and still how should i do the recursive function?
– avi dahan
Jul 3 at 6:56
You can either move them into a different parent, or use removeChild, which will returned the removed node, so that you could push it into an array or something.
– CBroe
Jul 3 at 6:59
@CBroe so yes i can use removeChild on the parent node and store it in a variable and than how should i proceed ? if the childrens have childrens? how should i wait until i append them and how should i continue to append from where i left of?
– avi dahan
Jul 3 at 7:02
I don’t know, this seems pretty much an X/Y problem right now to me … A more specific description/concrete example of what actual problem you are trying to solve here might help perhaps.
– CBroe
Jul 3 at 7:06
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Appending a node to a different location takes its children with it. If you wanted to avoid that and still use the original node and not a clone/newly created one, then you would have to move those children out of the node first.
– CBroe
Jul 3 at 6:54