Don't run scripts while DOM tree is undergoing mutations (#34505)

* script: Implement node insertion post-connection hook. Ensure script elements only run scripts when the DOM has stabilized.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Make iframe element use post-connection steps when handling initial document insertion.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Use a delayed task when running post-connection steps.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Add explanatory comment.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Tidy.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2024-12-26 01:06:09 -05:00 committed by GitHub
parent 20d67bdc44
commit 981616f918
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 117 additions and 51 deletions

View file

@ -93,6 +93,15 @@ pub trait VirtualMethods {
}
}
/// Invoked during a DOM tree mutation after a node becomes connected, once all
/// related DOM tree mutations have been applied.
/// <https://dom.spec.whatwg.org/#concept-node-post-connection-ext>
fn post_connection_steps(&self) {
if let Some(s) = self.super_type() {
s.post_connection_steps();
}
}
/// Called when a Node is appended to a tree, where 'tree_connected' indicates
/// whether the tree is part of a Document.
fn bind_to_tree(&self, context: &BindContext) {