Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(custom-elements): allow pending upgrades in nested reactions

ldm06f5759a0c374
diff --git a/moli-renderer-v8/src/custom_elements.rs b/moli-renderer-v8/src/custom_elements.rs
index 4372d1565..2832dd66a 100644
--- a/moli-renderer-v8/src/custom_elements.rs
+++ b/moli-renderer-v8/src/custom_elements.rs
@@ -49,8 +49,7 @@ pub(crate) use element_state::{
};
mod existing_upgrade;
pub(crate) use existing_upgrade::{
- has_pending_upgrade_reaction, upgrade_element_with_wrapper_if_defined,
- upgrade_handle_if_defined,
+ upgrade_element_with_wrapper_if_defined, upgrade_handle_if_defined,
};
mod existing_upgrade_candidate;
use existing_upgrade_candidate::custom_element_wrapper_for_existing_upgrade;
diff --git a/moli-renderer-v8/src/custom_elements/connected_lifecycle.rs b/moli-renderer-v8/src/custom_elements/connected_lifecycle.rs
index 4987e591b..73eda9707 100644
--- a/moli-renderer-v8/src/custom_elements/connected_lifecycle.rs
+++ b/moli-renderer-v8/src/custom_elements/connected_lifecycle.rs
@@ -1,5 +1,5 @@
+use super::CustomElementReaction;
use super::reactions::{enqueue_custom_element_reaction, with_custom_element_reaction_scope};
-use super::{CustomElementReaction, has_pending_upgrade_reaction};
use super::super::{document_runtime::DomHandle, native_bridge::JsContextHost};
@@ -34,9 +34,6 @@ pub(crate) fn enqueue_connected_callback(
host_ptr: *mut JsContextHost,
handle: DomHandle,
) -> bool {
- if has_pending_upgrade_reaction(host_ptr, handle) {
- return false;
- }
enqueue_lifecycle_callback(
scope,
host_ptr,
@@ -51,9 +48,6 @@ pub(crate) fn enqueue_disconnected_callback(
host_ptr: *mut JsContextHost,
handle: DomHandle,
) -> bool {
- if has_pending_upgrade_reaction(host_ptr, handle) {
- return false;
- }
enqueue_lifecycle_callback(
scope,
host_ptr,
diff --git a/moli-renderer-v8/src/custom_elements/existing_upgrade.rs b/moli-renderer-v8/src/custom_elements/existing_upgrade.rs
index c3e896805..b39f5ea02 100644
--- a/moli-renderer-v8/src/custom_elements/existing_upgrade.rs
+++ b/moli-renderer-v8/src/custom_elements/existing_upgrade.rs
@@ -7,7 +7,6 @@ use super::existing_upgrade_candidate::{
};
use super::existing_upgrade_failure::fail_existing_custom_element_construction;
use super::existing_upgrade_invocation::upgrade_existing_custom_element_with_constructor;
-use super::reactions::CustomElementReaction;
use super::{
dispatch_form_association_callback_if_needed, dispatch_form_disabled_callback_if_needed,
};
@@ -17,9 +16,6 @@ pub(crate) fn upgrade_handle_if_defined(
host_ptr: *mut JsContextHost,
handle: DomHandle,
) -> bool {
- if has_pending_upgrade_reaction(host_ptr, handle) {
- return true;
- }
let already_handled = unsafe { &*host_ptr }
.custom_elements_for_node_handle(handle)
.is_some_and(|store| {
@@ -91,9 +87,6 @@ pub(crate) fn upgrade_element_with_wrapper_if_defined<'s>(
wrapper: v8::Local<'s, v8::Object>,
handle: DomHandle,
) -> bool {
- if has_pending_upgrade_reaction(host_ptr, handle) {
- return true;
- }
let already_handled = unsafe { &*host_ptr }
.custom_elements_for_node_handle(handle)
.is_some_and(|store| {
@@ -139,12 +132,3 @@ pub(crate) fn upgrade_element_with_wrapper_if_defined<'s>(
}
upgraded
}
-
-pub(crate) fn has_pending_upgrade_reaction(
- host_ptr: *mut JsContextHost,
- handle: DomHandle,
-) -> bool {
- unsafe { &*host_ptr }
- .custom_element_reactions()
- .pending_reactions_contain(handle, &CustomElementReaction::Upgrade)
-}
diff --git a/moli-renderer-v8/src/custom_elements/form_lifecycle.rs b/moli-renderer-v8/src/custom_elements/form_lifecycle.rs
index 56feb5a72..56d029cf0 100644
--- a/moli-renderer-v8/src/custom_elements/form_lifecycle.rs
+++ b/moli-renderer-v8/src/custom_elements/form_lifecycle.rs
@@ -1,5 +1,4 @@
use super::element_state::is_form_associated_custom_element_handle;
-use super::existing_upgrade::has_pending_upgrade_reaction;
use super::reaction_types::FormStateRestoreMode;
use super::reactions::{
CustomElementReaction, enqueue_custom_element_reaction, with_custom_element_reaction_scope,
@@ -18,9 +17,6 @@ pub(crate) fn enqueue_form_association_callback_if_needed(
host_ptr: *mut JsContextHost,
handle: DomHandle,
) -> bool {
- if has_pending_upgrade_reaction(host_ptr, handle) {
- return false;
- }
if !unsafe { &*host_ptr }
.custom_elements_for_node_handle(handle)
.is_some_and(|store| store.is_upgraded_handle(handle))
@@ -66,9 +62,6 @@ pub(super) fn enqueue_form_disabled_callback_if_needed(
host_ptr: *mut JsContextHost,
handle: DomHandle,
) -> bool {
- if has_pending_upgrade_reaction(host_ptr, handle) {
- return false;
- }
if !unsafe { &*host_ptr }
.custom_elements_for_node_handle(handle)
.is_some_and(|store| store.is_upgraded_handle(handle))
diff --git a/moli-renderer-v8/src/custom_elements/reaction_queue.rs b/moli-renderer-v8/src/custom_elements/reaction_queue.rs
index 93de3d14f..348c8a8dd 100644
--- a/moli-renderer-v8/src/custom_elements/reaction_queue.rs
+++ b/moli-renderer-v8/src/custom_elements/reaction_queue.rs
@@ -65,16 +65,6 @@ impl CustomElementReactionCoordinator {
.is_some_and(|queue| queue.pending_reactions_end_with(reaction))
}
- pub(super) fn pending_reactions_contain(
- &self,
- handle: DomHandle,
- reaction: &CustomElementReaction,
- ) -> bool {
- self.element_reactions
- .get(&handle)
- .is_some_and(|queue| queue.pending_reactions_contain(reaction))
- }
-
pub(super) fn next_current_element(&mut self) -> Option<DomHandle> {
self.stack.last_mut()?.next()
}
diff --git a/moli-renderer-v8/src/custom_elements/reaction_queue_storage.rs b/moli-renderer-v8/src/custom_elements/reaction_queue_storage.rs
index 1019cd1c1..a388cc923 100644
--- a/moli-renderer-v8/src/custom_elements/reaction_queue_storage.rs
+++ b/moli-renderer-v8/src/custom_elements/reaction_queue_storage.rs
@@ -70,12 +70,4 @@ impl ElementReactionQueue {
.find_map(Option::as_ref)
== Some(reaction)
}
-
- pub(super) fn pending_reactions_contain(&self, reaction: &CustomElementReaction) -> bool {
- self.reactions
- .iter()
- .skip(self.index)
- .flatten()
- .any(|pending| pending == reaction)
- }
}
diff --git a/moli-renderer-v8/src/custom_elements/upgrade_eligibility.rs b/moli-renderer-v8/src/custom_elements/upgrade_eligibility.rs
index 2fc13c39c..0a5ed8482 100644
--- a/moli-renderer-v8/src/custom_elements/upgrade_eligibility.rs
+++ b/moli-renderer-v8/src/custom_elements/upgrade_eligibility.rs
@@ -1,10 +1,9 @@
use super::super::{document_runtime::DomHandle, native_bridge::JsContextHost};
-use super::{definition_name_for_handle, has_pending_upgrade_reaction};
+use super::definition_name_for_handle;
pub(super) fn can_upgrade_handle(host_ptr: *mut JsContextHost, handle: DomHandle) -> bool {
- if has_pending_upgrade_reaction(host_ptr, handle) {
- return false;
- }
+ // A pending reaction does not change the element's state. A nested reaction
+ // scope must be able to enqueue it again and perform the upgrade immediately.
let host = unsafe { &*host_ptr };
if host
.custom_elements_for_node_handle(handle)
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements.rs
index 313298375..238cd2a3c 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements.rs
@@ -3,6 +3,7 @@ use crate::custom_elements::CustomElementRegistryKey;
mod insertion;
mod parser;
+mod upgrade_reentry;
#[tokio::test]
async fn popup_classic_script_custom_element_microtasks_wait_for_outer_javascript() {
@@ -8104,9 +8105,11 @@ fn html_fragment_parser_upgrade_reaction_runs_before_descendant_connected_callba
)
.expect("HTML fragment custom element upgrade reaction probe should evaluate");
+ // The child is still awaiting upgrade when the parent reconnects it. The
+ // nested appendChild reaction scope must upgrade it before returning.
assert_eq!(
result,
- "1,parent:connected|parent:removed|parent:appended|child:connected:1,true"
+ "1,parent:connected|parent:removed|child:connected:1|parent:appended,true"
);
}
@@ -8158,7 +8161,7 @@ fn html_fragment_parser_upgrade_reaction_survives_move_to_new_document() {
assert_eq!(
result,
- "1,parent:connected|parent:removed|parent:appended|child:connected:1"
+ "1,parent:connected|parent:removed|child:connected:1|parent:appended"
);
}
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements/upgrade_reentry.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements/upgrade_reentry.rs
new file mode 100644
index 000000000..4cd73984d
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements/upgrade_reentry.rs
@@ -0,0 +1,105 @@
+use super::*;
+
+#[test]
+fn pending_upgrade_reenters_through_append_child_and_explicit_upgrade() {
+ let mut vm = new_storage_test_vm("https://custom-element-upgrade-reentry.test/");
+ let result = vm.eval(r#"
+ (() => {
+ const results = [];
+ for (const operation of ['append', 'upgrade']) {
+ const frame = document.createElement('iframe');
+ (document.body || document.documentElement || document).appendChild(frame);
+ const w = frame.contentWindow, d = w.document;
+ d.body.innerHTML = '<reentry-element id="a"></reentry-element><reentry-element id="b"></reentry-element>';
+ const log = [];
+ class Reentry extends w.HTMLElement {
+ constructor() {
+ super(); log.push(this.id + ':begin');
+ const b = d.getElementById('b');
+ if (operation === 'append') { b.remove(); d.body.appendChild(b); }
+ else w.customElements.upgrade(b);
+ log.push(this.id + ':end');
+ }
+ }
+ w.customElements.define('reentry-element', Reentry);
+ results.push(log);
+ frame.remove();
+ }
+ return JSON.stringify(results);
+ })()
+ "#).expect("pending upgrades should participate in nested reaction scopes");
+ assert_eq!(
+ result,
+ r#"[["a:begin","b:begin","b:end","a:end"],["a:begin","b:begin","b:end","a:end"]]"#
+ );
+}
+
+#[test]
+fn pending_duplicate_upgrade_preserves_initial_form_association_reaction() {
+ let mut vm = new_storage_test_vm("https://custom-element-upgrade-form-reentry.test/");
+ let result = vm
+ .eval(
+ r#"
+ (() => {
+ const form = document.createElement('form');
+ form.id = 'owner';
+ form.innerHTML = '<reentry-face id="a"></reentry-face><reentry-face id="b"></reentry-face>';
+ (document.body || document.documentElement || document).appendChild(form);
+ const log = [];
+ class Reentry extends HTMLElement {
+ static formAssociated = true;
+ constructor() {
+ super(); this.attachInternals(); log.push(this.id + ':begin');
+ if (this.id === 'a') {
+ const b = document.getElementById('b'); b.remove(); form.appendChild(b);
+ }
+ log.push(this.id + ':end');
+ }
+ connectedCallback() { log.push(this.id + ':connected'); }
+ formAssociatedCallback(form) { log.push(this.id + ':form:' + form.id); }
+ }
+ customElements.define('reentry-face', Reentry);
+ return JSON.stringify(log);
+ })()
+ "#,
+ )
+ .expect(
+ "a remaining upgrade reaction must not suppress a completed upgrade's form callback",
+ );
+ assert_eq!(
+ result,
+ r#"["a:begin","b:begin","b:end","b:connected","b:form:owner","a:end","a:connected","a:form:owner"]"#
+ );
+}
+
+#[test]
+fn pending_duplicate_upgrade_clears_reactions_after_constructor_failure() {
+ let mut vm = new_storage_test_vm("https://custom-element-upgrade-failure-reentry.test/");
+ let result = vm.eval(r#"
+ (() => {
+ const parent = document.createElement('div');
+ parent.innerHTML = '<reentry-failure id="a"></reentry-failure><reentry-failure id="b"></reentry-failure>';
+ (document.body || document.documentElement || document).appendChild(parent);
+ const constructors = [], connections = [];
+ let errors = 0;
+ addEventListener('error', event => { ++errors; event.preventDefault(); });
+ class Reentry extends HTMLElement {
+ constructor() {
+ super(); constructors.push(this.id);
+ if (this.id === 'b') throw new Error('failed upgrade');
+ const b = document.getElementById('b'); b.remove(); parent.appendChild(b);
+ }
+ connectedCallback() { connections.push(this.id); }
+ }
+ customElements.define('reentry-failure', Reentry);
+ const b = document.getElementById('b');
+ customElements.upgrade(b);
+ b.remove(); parent.appendChild(b);
+ return JSON.stringify({constructors, connections, errors, defined:b.matches(':defined')});
+ })()
+ "#).expect("failed upgrades should discard duplicate upgrades and initial callbacks");
+ assert_eq!(
+ result,
+ r#"{"constructors":["a","b"],"connections":["a"],"errors":1,"defined":false}"#
+ );
+}