Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(style): invalidate validity containers on child changes

ldm0ad757730ba3e
diff --git a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
index e8dd2865a..6814d781d 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
@@ -2226,10 +2226,33 @@ impl JsContextHost {
.collect()
}
+ fn validity_containers_for_child_list_mutations(
+ &self,
+ effects: &[StyleMutationEffect],
+ ) -> Vec<DomHandle> {
+ let mut containers = Vec::new();
+ for effect in effects {
+ let StyleMutationEffect::ChildList { parent, .. } = effect else {
+ continue;
+ };
+ let mut current = Some(*parent);
+ while let Some(handle) = current {
+ if self.dom_host().is_html_element_named(handle, "form")
+ || self.dom_host().is_html_element_named(handle, "fieldset")
+ {
+ push_unique_handle(&mut containers, handle);
+ }
+ current = self.dom_host().parent_node(handle);
+ }
+ }
+ containers
+ }
+
pub(crate) fn note_style_mutation_effects(&mut self, effects: &[StyleMutationEffect]) {
if style_mutation_effects_affect_layout_metric(effects) {
self.clear_layout_rect_cache();
}
+ let validity_containers = self.validity_containers_for_child_list_mutations(effects);
let dom_host = self.dom_host() as *const _;
let emulated_media = self.emulated_media().clone();
let viewport = self.style_viewport();
@@ -2239,6 +2262,16 @@ impl JsContextHost {
&emulated_media,
viewport,
);
+ for container in validity_containers {
+ // Child-list effects are produced after the tree splice, so the
+ // previous aggregate validity is unavailable. An absent old state
+ // deliberately selects the conservative state invalidation path.
+ self.note_element_state_style_activity_with_old_state(
+ container,
+ StyloElementState::VALIDITY_STATES,
+ None,
+ );
+ }
}
pub(crate) fn note_element_inline_style_subtree_activity(&mut self, root: DomHandle) {
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
index 3eadb7ae2..9499bd04c 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
@@ -4073,6 +4073,66 @@ fn text_value_change_invalidates_validity_computed_style() {
"rgb(0, 128, 0),rgb(255, 0, 0)|rgb(255, 0, 0),rgb(0, 0, 255)"
);
}
+
+#[test]
+fn child_list_change_invalidates_form_and_fieldset_validity_computed_style() {
+ let mut vm = new_parsed_test_vm(
+ "https://computed-style-validity-child-list-invalidation.test/",
+ r#"<!doctype html><html><head><style>
+ form, fieldset { background-color: rgb(0, 128, 0); }
+ form:invalid, fieldset:invalid { background-color: rgb(0, 255, 0); }
+ .target { color: rgb(255, 0, 0); }
+ #form:invalid + #form-target { color: rgb(0, 0, 255); }
+ #fieldset:invalid + #fieldset-target { color: rgb(1, 2, 3); }
+ </style></head><body>
+ <form id="form"></form><span id="form-target" class="target"></span>
+ <fieldset id="fieldset"></fieldset><span id="fieldset-target" class="target"></span>
+ </body></html>"#,
+ );
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const form = document.getElementById('form');
+ const fieldset = document.getElementById('fieldset');
+ const formTarget = document.getElementById('form-target');
+ const fieldsetTarget = document.getElementById('fieldset-target');
+ const invalid = document.createElement('input');
+ invalid.type = 'number';
+ invalid.min = '8';
+ invalid.value = '4';
+ const state = () => [
+ getComputedStyle(form).backgroundColor,
+ getComputedStyle(formTarget).color,
+ getComputedStyle(fieldset).backgroundColor,
+ getComputedStyle(fieldsetTarget).color
+ ].join(',');
+
+ const initial = state();
+ form.append(invalid);
+ const inForm = state();
+ fieldset.append(invalid);
+ const inFieldset = state();
+ invalid.remove();
+ const removed = state();
+ return [initial, inForm, inFieldset, removed].join('|');
+})()
+"#,
+ )
+ .expect("child-list validity invalidation should evaluate");
+
+ assert_eq!(
+ result,
+ concat!(
+ "rgb(0, 128, 0),rgb(255, 0, 0),rgb(0, 128, 0),rgb(255, 0, 0)|",
+ "rgb(0, 255, 0),rgb(0, 0, 255),rgb(0, 128, 0),rgb(255, 0, 0)|",
+ "rgb(0, 128, 0),rgb(255, 0, 0),rgb(0, 255, 0),rgb(1, 2, 3)|",
+ "rgb(0, 128, 0),rgb(255, 0, 0),rgb(0, 128, 0),rgb(255, 0, 0)"
+ )
+ );
+}
+
#[test]
fn text_value_change_invalidates_range_computed_style() {
let mut vm = new_storage_test_vm("https://computed-style-range-invalidation.test/");