dom
dom copied to clipboard
Should post-connected steps be able to run multiple times during insertion?
What is the issue with the DOM Standard?
See https://github.com/whatwg/dom/pull/1261#discussion_r1594562487 for context. In short, after https://github.com/whatwg/dom/pull/1261, it is possible to run the post-connected steps multiple times/reentrantly for a single element during an insertion. Should that be allowed? Copied from the aforementioned comment:
Imagine a case where you insert two scripts at the same time, i.e., document.body.append(script1, script2). script2 has an invalid type and therefore is not executable upon insertion as-is. script1 does two things:
- Moves
script2around in the DOM (thus reentrantly invoking the DOM insertion algorithm, and runningscript2's post-connected steps for the first time) - Removes
script2's invalidtypeattribute, making it valid/executable (but not immediately executing it, becausetypeattribute mutations do not trigger the script processing model).
At least in Chromium, when script2's post-connected steps run for the second time (as a result of the outermost document.body.append(script1, script2) call finally invoking script2's post-connected steps), script2 will finally execute. Here's the code snippet:
const script1 = document.createElement('script');
script1.innerText = `
const div = document.querySelector('div');
div.append(script2);
script2.removeAttribute('type');
`;
const script2 = document.createElement('script');
script2.id = 'script2';
// Makes the script invalid so that it does not run upon insertion.
script2.type = 'foo';
script2.innerText = 'console.log("grazie");';
document.body.append(script1, script2);
Despite being generally structured like Blink here, WebKit does not run script2 in this scenario, which I think indicates that the post-connected steps do not run the second time for script2. I would love to know why / how WebKit does this, @rniwa do you know? And WDYT @smaug----?