Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(parser): notify mutations when coalescing text
ldm0f645752894d4
diff --git a/moli-benchmark/wpt-cross-current/failed-cases.txt b/moli-benchmark/wpt-cross-current/failed-cases.txtindex 537f6628a..cb3c0249e 100644--- a/moli-benchmark/wpt-cross-current/failed-cases.txt+++ b/moli-benchmark/wpt-cross-current/failed-cases.txt@@ -2394,7 +2394,6 @@ css/cssom-view/elementFromPoint-003.htmlcss/cssom-view/elementFromPoint-dynamic-anon-box.htmlcss/cssom-view/elementFromPoint.htmlcss/cssom-view/elementFromPosition.html-css/cssom-view/elementsFromPoint-iframes.htmlcss/cssom-view/elementsFromPoint-inline-htb-rtl.htmlcss/cssom-view/elementsFromPoint-inline-vlr-rtl.htmlcss/cssom-view/elementsFromPoint-inline-vrl-ltr.htmldiff --git a/moli-benchmark/wpt-cross-current/passed-cases.txt b/moli-benchmark/wpt-cross-current/passed-cases.txtindex 504415680..755fa5cf9 100644--- a/moli-benchmark/wpt-cross-current/passed-cases.txt+++ b/moli-benchmark/wpt-cross-current/passed-cases.txt@@ -2896,6 +2896,7 @@ css/cssom-view/elementFromPoint-subpixel.htmlcss/cssom-view/elementFromPoint-visibility-hidden-resizer.htmlcss/cssom-view/elementScroll-002.htmlcss/cssom-view/elementScroll.html+css/cssom-view/elementsFromPoint-iframes.htmlcss/cssom-view/elementsFromPoint-inline-htb-ltr.htmlcss/cssom-view/elementsFromPoint-inline-nested.htmlcss/cssom-view/elementsFromPoint-inline-vlr-ltr.htmldiff --git a/moli-parser/src/live_target.rs b/moli-parser/src/live_target.rsindex a425a7a27..99f1620d9 100644--- a/moli-parser/src/live_target.rs+++ b/moli-parser/src/live_target.rs@@ -604,9 +604,19 @@ pub trait ParserDomMutationConsumer {system_id: String,) -> NativeNodeId;- fn prepend_text_to_text_node(&mut self, node_id: NativeNodeId, text: String);+ /// Return notifications for text coalescing just as for node insertion.+ /// The sink delivers them after releasing its structural borrow.+ fn prepend_text_to_text_node(+ &mut self,+ node_id: NativeNodeId,+ text: String,+ ) -> DomMutationEffects;- fn append_text_to_text_node(&mut self, node_id: NativeNodeId, text: String);+ fn append_text_to_text_node(+ &mut self,+ node_id: NativeNodeId,+ text: String,+ ) -> DomMutationEffects;fn push_parse_error(&mut self, error: String);@@ -642,8 +652,8 @@ struct ParserDomMutationSink {create_cdata_section: unsafe fn(NonNull<()>, NativeNodeId, String) -> NativeNodeId,create_document_type:unsafe fn(NonNull<()>, NativeNodeId, String, String, String) -> NativeNodeId,- prepend_text_to_text_node: unsafe fn(NonNull<()>, NativeNodeId, String),- append_text_to_text_node: unsafe fn(NonNull<()>, NativeNodeId, String),+ prepend_text_to_text_node: unsafe fn(NonNull<()>, NativeNodeId, String) -> DomMutationEffects,+ append_text_to_text_node: unsafe fn(NonNull<()>, NativeNodeId, String) -> DomMutationEffects,push_parse_error: unsafe fn(NonNull<()>, String),set_html_quirks_mode_for_parser: unsafe fn(NonNull<()>, QuirksMode),mark_script_already_started_for_parser: unsafe fn(NonNull<()>, NativeNodeId),@@ -753,19 +763,19 @@ impl ParserDomMutationSink {data: NonNull<()>,node_id: NativeNodeId,text: String,- ) {+ ) -> DomMutationEffects {// SAFETY: ParserDomMutationSink::from_consumer_unchecked requires the// pointed-to consumer to remain live and exclusive for the pump step.- unsafe { data.cast::<T>().as_mut() }.prepend_text_to_text_node(node_id, text);+ unsafe { data.cast::<T>().as_mut() }.prepend_text_to_text_node(node_id, text)}unsafe fn append_text_to_text_node_impl<T: ParserDomMutationConsumer>(data: NonNull<()>,node_id: NativeNodeId,text: String,- ) {+ ) -> DomMutationEffects {// SAFETY: ParserDomMutationSink::from_consumer_unchecked requires the// pointed-to consumer to remain live and exclusive for the pump step.- unsafe { data.cast::<T>().as_mut() }.append_text_to_text_node(node_id, text);+ unsafe { data.cast::<T>().as_mut() }.append_text_to_text_node(node_id, text)}unsafe fn push_parse_error_impl<T: ParserDomMutationConsumer>(data: NonNull<()>,@@ -929,16 +939,16 @@ impl ParserDomMutationSink {}}- fn prepend_text_to_text_node(self, node_id: NativeNodeId, text: String) {+ fn prepend_text_to_text_node(self, node_id: NativeNodeId, text: String) -> DomMutationEffects {// SAFETY: construction ties the raw pointer and callback to the same// consumer remains live for the current runtime-DOM sink step.- unsafe { (self.prepend_text_to_text_node)(self.data, node_id, text) };+ unsafe { (self.prepend_text_to_text_node)(self.data, node_id, text) }}- fn append_text_to_text_node(self, node_id: NativeNodeId, text: String) {+ fn append_text_to_text_node(self, node_id: NativeNodeId, text: String) -> DomMutationEffects {// SAFETY: construction ties the raw pointer and callback to the same// consumer remains live for the current runtime-DOM sink step.- unsafe { (self.append_text_to_text_node)(self.data, node_id, text) };+ unsafe { (self.append_text_to_text_node)(self.data, node_id, text) }}fn push_parse_error(self, error: String) {@@ -1487,16 +1497,24 @@ impl ParserDomMutationConsumer for TestMutationEffectCollector<'_> {)}- fn prepend_text_to_text_node(&mut self, node_id: NativeNodeId, text: String) {+ fn prepend_text_to_text_node(+ &mut self,+ node_id: NativeNodeId,+ text: String,+ ) -> DomMutationEffects {// SAFETY: tests keep the borrowed DomHost pointer alive and route the// parser pump through this collector for the duration of the step.- prepend_text_to_text_node_in_host(unsafe { &mut *self.host }, node_id, text);+ prepend_text_to_text_node_in_host(unsafe { &mut *self.host }, node_id, text)}- fn append_text_to_text_node(&mut self, node_id: NativeNodeId, text: String) {+ fn append_text_to_text_node(+ &mut self,+ node_id: NativeNodeId,+ text: String,+ ) -> DomMutationEffects {// SAFETY: tests keep the borrowed DomHost pointer alive and route the// parser pump through this collector for the duration of the step.- append_text_to_text_node_in_host(unsafe { &mut *self.host }, node_id, text);+ append_text_to_text_node_in_host(unsafe { &mut *self.host }, node_id, text)}fn push_parse_error(&mut self, error: String) {@@ -1558,26 +1576,30 @@ impl ParserMutationEffectConsumer for TestMutationEffectCollector<'_> {}}-fn prepend_text_to_text_node_in_host(host: &mut DomHost, node_id: NativeNodeId, text: String) {- if let Some(text_node) = host- .node_mut(node_id)- .and_then(|node| node.data_mut().as_text_mut())- {- let mut merged = text;- merged.push_str(text_node.data());- text_node.set_data(merged);- }+fn prepend_text_to_text_node_in_host(+ host: &mut DomHost,+ node_id: NativeNodeId,+ text: String,+) -> DomMutationEffects {+ let Some(previous) = host.node(node_id).and_then(Node::as_text) else {+ return DomMutationEffects::default();+ };+ let mut merged = text;+ merged.push_str(previous.data());+ host.set_text_content_effects(node_id, &merged)}-fn append_text_to_text_node_in_host(host: &mut DomHost, node_id: NativeNodeId, text: String) {- if let Some(text_node) = host- .node_mut(node_id)- .and_then(|node| node.data_mut().as_text_mut())- {- let mut merged = text_node.data().to_owned();- merged.push_str(&text);- text_node.set_data(merged);- }+fn append_text_to_text_node_in_host(+ host: &mut DomHost,+ node_id: NativeNodeId,+ text: String,+) -> DomMutationEffects {+ let Some(previous) = host.node(node_id).and_then(Node::as_text) else {+ return DomMutationEffects::default();+ };+ let mut merged = previous.data().to_owned();+ merged.push_str(&text);+ host.set_text_content_effects(node_id, &merged)}#[cfg(test)]@@ -1846,16 +1868,24 @@ impl ParserDomMutationConsumer for TestReadTrackingCollector<'_> {)}- fn prepend_text_to_text_node(&mut self, node_id: NativeNodeId, text: String) {+ fn prepend_text_to_text_node(+ &mut self,+ node_id: NativeNodeId,+ text: String,+ ) -> DomMutationEffects {// SAFETY: tests keep the borrowed DomHost pointer alive and route the// parser pump through this collector for the duration of the step.- prepend_text_to_text_node_in_host(unsafe { &mut *self.host }, node_id, text);+ prepend_text_to_text_node_in_host(unsafe { &mut *self.host }, node_id, text)}- fn append_text_to_text_node(&mut self, node_id: NativeNodeId, text: String) {+ fn append_text_to_text_node(+ &mut self,+ node_id: NativeNodeId,+ text: String,+ ) -> DomMutationEffects {// SAFETY: tests keep the borrowed DomHost pointer alive and route the// parser pump through this collector for the duration of the step.- append_text_to_text_node_in_host(unsafe { &mut *self.host }, node_id, text);+ append_text_to_text_node_in_host(unsafe { &mut *self.host }, node_id, text)}fn push_parse_error(&mut self, error: String) {@@ -2755,23 +2785,31 @@ impl ParserStreamHtmlTreeSinkTarget {}}- fn prepend_text_to_text_node(&mut self, node_id: NativeNodeId, text: String) {+ fn prepend_text_to_text_node(+ &mut self,+ node_id: NativeNodeId,+ text: String,+ ) -> DomMutationEffects {if let Some(owner) = &self.runtime_dom_sinks {owner.dom_mutation_sink()- .prepend_text_to_text_node(node_id, text);+ .prepend_text_to_text_node(node_id, text)} else {- prepend_text_to_text_node_in_host(self.dom_host_mut(), node_id, text);+ prepend_text_to_text_node_in_host(self.dom_host_mut(), node_id, text)}}- fn append_text_to_text_node(&mut self, node_id: NativeNodeId, text: String) {+ fn append_text_to_text_node(+ &mut self,+ node_id: NativeNodeId,+ text: String,+ ) -> DomMutationEffects {if let Some(owner) = &self.runtime_dom_sinks {owner.dom_mutation_sink()- .append_text_to_text_node(node_id, text);+ .append_text_to_text_node(node_id, text)} else {- append_text_to_text_node_in_host(self.dom_host_mut(), node_id, text);+ append_text_to_text_node_in_host(self.dom_host_mut(), node_id, text)}}@@ -2999,21 +3037,21 @@ impl ParserStreamHtmlTreeSinkTarget {if let Some(reference_child) = reference_child {if self.read_is_text_node(reference_child) {- self.prepend_text_to_text_node(reference_child, text);- return ParserMutationEffectDelivery::none();+ let effects = self.prepend_text_to_text_node(reference_child, text);+ return self.mutation_effect_delivery(effects);}if let Some(previous) = self.read_previous_sibling(reference_child)&& self.read_is_text_node(previous){- self.append_text_to_text_node(previous, text);- return ParserMutationEffectDelivery::none();+ let effects = self.append_text_to_text_node(previous, text);+ return self.mutation_effect_delivery(effects);}} else if let Some(last_child) = self.read_last_child(parent_id)&& self.read_is_text_node(last_child){- self.append_text_to_text_node(last_child, text);- return ParserMutationEffectDelivery::none();+ let effects = self.append_text_to_text_node(last_child, text);+ return self.mutation_effect_delivery(effects);}let document_handle = selfdiff --git a/moli-parser/src/stream.rs b/moli-parser/src/stream.rsindex 61fa5876d..fa14f3b82 100644--- a/moli-parser/src/stream.rs+++ b/moli-parser/src/stream.rs@@ -1114,30 +1114,34 @@ mod tests {)}- fn prepend_text_to_text_node(&mut self, node_id: NativeNodeId, text: String) {+ fn prepend_text_to_text_node(+ &mut self,+ node_id: NativeNodeId,+ text: String,+ ) -> DomMutationEffects {// SAFETY: the test keeps the DomHost alive for this parser pump step.let host = unsafe { &mut *self.host };- if let Some(text_node) = host- .node_mut(node_id)- .and_then(|node| node.data_mut().as_text_mut())- {- let mut merged = text;- merged.push_str(text_node.data());- text_node.set_data(merged);- }+ let Some(previous) = host.node(node_id).and_then(Node::as_text) else {+ return DomMutationEffects::default();+ };+ let mut merged = text;+ merged.push_str(previous.data());+ host.set_text_content_effects(node_id, &merged)}- fn append_text_to_text_node(&mut self, node_id: NativeNodeId, text: String) {+ fn append_text_to_text_node(+ &mut self,+ node_id: NativeNodeId,+ text: String,+ ) -> DomMutationEffects {// SAFETY: the test keeps the DomHost alive for this parser pump step.let host = unsafe { &mut *self.host };- if let Some(text_node) = host- .node_mut(node_id)- .and_then(|node| node.data_mut().as_text_mut())- {- let mut merged = text_node.data().to_owned();- merged.push_str(&text);- text_node.set_data(merged);- }+ let Some(previous) = host.node(node_id).and_then(Node::as_text) else {+ return DomMutationEffects::default();+ };+ let mut merged = previous.data().to_owned();+ merged.push_str(&text);+ host.set_text_content_effects(node_id, &merged)}fn push_parse_error(&mut self, error: String) {diff --git a/moli-renderer-v8/src/document_runtime/document_write.rs b/moli-renderer-v8/src/document_runtime/document_write.rsindex daa76683a..151e3f09d 100644--- a/moli-renderer-v8/src/document_runtime/document_write.rs+++ b/moli-renderer-v8/src/document_runtime/document_write.rs@@ -276,14 +276,18 @@ impl ParserDomMutationConsumer for DocumentWriteParserMutationOwner<'_, '_, '_>.create_document_type_for_document(document_handle, &name, &public_id, &system_id)}- fn prepend_text_to_text_node(&mut self, node_id: DomHandle, text: String) {+ fn prepend_text_to_text_node(+ &mut self,+ node_id: DomHandle,+ text: String,+ ) -> DomMutationEffects {self.runtime- .prepend_text_to_text_node_in_live_dom_host(node_id, text);+ .prepend_text_to_text_node_in_live_dom_host(node_id, text)}- fn append_text_to_text_node(&mut self, node_id: DomHandle, text: String) {+ fn append_text_to_text_node(&mut self, node_id: DomHandle, text: String) -> DomMutationEffects {self.runtime- .append_text_to_text_node_in_live_dom_host(node_id, text);+ .append_text_to_text_node_in_live_dom_host(node_id, text)}fn push_parse_error(&mut self, error: String) {diff --git a/moli-renderer-v8/src/document_runtime/dom_facade.rs b/moli-renderer-v8/src/document_runtime/dom_facade.rsindex 1d7263033..ab71b42c4 100644--- a/moli-renderer-v8/src/document_runtime/dom_facade.rs+++ b/moli-renderer-v8/src/document_runtime/dom_facade.rs@@ -364,32 +364,28 @@ impl DocumentRuntime {&mut self,node_id: DomHandle,text: String,- ) {+ ) -> DomMutationEffects {let host = self.dom_host_mut_for_active_parser_step();- if let Some(text_node) = host- .node_mut(node_id)- .and_then(|node| node.data_mut().as_text_mut())- {- let mut merged = text;- merged.push_str(text_node.data());- text_node.set_data(merged);- }+ let Some(previous) = host.node(node_id).and_then(Node::as_text) else {+ return DomMutationEffects::default();+ };+ let mut merged = text;+ merged.push_str(previous.data());+ host.set_text_content_effects(node_id, &merged)}pub(crate) fn append_text_to_text_node_in_live_dom_host(&mut self,node_id: DomHandle,text: String,- ) {+ ) -> DomMutationEffects {let host = self.dom_host_mut_for_active_parser_step();- if let Some(text_node) = host- .node_mut(node_id)- .and_then(|node| node.data_mut().as_text_mut())- {- let mut merged = text_node.data().to_owned();- merged.push_str(&text);- text_node.set_data(merged);- }+ let Some(previous) = host.node(node_id).and_then(Node::as_text) else {+ return DomMutationEffects::default();+ };+ let mut merged = previous.data().to_owned();+ merged.push_str(&text);+ host.set_text_content_effects(node_id, &merged)}pub(crate) fn push_parse_error_in_live_dom_host(&mut self, error: String) {diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_documents/live_parser.rs b/moli-renderer-v8/src/native_bridge/context_host/child_documents/live_parser.rsindex d0aad8674..642b5100f 100644--- a/moli-renderer-v8/src/native_bridge/context_host/child_documents/live_parser.rs+++ b/moli-renderer-v8/src/native_bridge/context_host/child_documents/live_parser.rs@@ -413,30 +413,28 @@ impl ParserDomMutationConsumer for ChildFrameLiveParserOwner<'_, '_, '_> {)}- fn prepend_text_to_text_node(&mut self, node_id: DomHandle, text: String) {- if let Some(text_node) = self- .host- .dom_host_mut()- .node_mut(node_id)- .and_then(|node| node.data_mut().as_text_mut())- {- let mut merged = text;- merged.push_str(text_node.data());- text_node.set_data(merged);- }+ fn prepend_text_to_text_node(+ &mut self,+ node_id: DomHandle,+ text: String,+ ) -> DomMutationEffects {+ let host = self.host.dom_host_mut();+ let Some(previous) = host.node(node_id).and_then(Node::as_text) else {+ return DomMutationEffects::default();+ };+ let mut merged = text;+ merged.push_str(previous.data());+ host.set_text_content_effects(node_id, &merged)}- fn append_text_to_text_node(&mut self, node_id: DomHandle, text: String) {- if let Some(text_node) = self- .host- .dom_host_mut()- .node_mut(node_id)- .and_then(|node| node.data_mut().as_text_mut())- {- let mut merged = text_node.data().to_owned();- merged.push_str(&text);- text_node.set_data(merged);- }+ fn append_text_to_text_node(&mut self, node_id: DomHandle, text: String) -> DomMutationEffects {+ let host = self.host.dom_host_mut();+ let Some(previous) = host.node(node_id).and_then(Node::as_text) else {+ return DomMutationEffects::default();+ };+ let mut merged = previous.data().to_owned();+ merged.push_str(&text);+ host.set_text_content_effects(node_id, &merged)}fn push_parse_error(&mut self, error: String) {diff --git a/moli-renderer-v8/src/runtime/phase_one/parser_turn.rs b/moli-renderer-v8/src/runtime/phase_one/parser_turn.rsindex d3fac9339..b17f0f10e 100644--- a/moli-renderer-v8/src/runtime/phase_one/parser_turn.rs+++ b/moli-renderer-v8/src/runtime/phase_one/parser_turn.rs@@ -292,16 +292,24 @@ impl ParserDomMutationConsumer for PhaseOneParserOwner<'_> {)}- fn prepend_text_to_text_node(&mut self, node_id: NativeNodeId, text: String) {+ fn prepend_text_to_text_node(+ &mut self,+ node_id: NativeNodeId,+ text: String,+ ) -> DomMutationEffects {self.vm.document_runtime- .prepend_text_to_text_node_in_live_dom_host(node_id, text);+ .prepend_text_to_text_node_in_live_dom_host(node_id, text)}- fn append_text_to_text_node(&mut self, node_id: NativeNodeId, text: String) {+ fn append_text_to_text_node(+ &mut self,+ node_id: NativeNodeId,+ text: String,+ ) -> DomMutationEffects {self.vm.document_runtime- .append_text_to_text_node_in_live_dom_host(node_id, text);+ .append_text_to_text_node_in_live_dom_host(node_id, text)}fn push_parse_error(&mut self, error: String) {diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom.rsindex 2686a2f2a..6f94be9ee 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom.rs@@ -2350,6 +2350,77 @@ fn document_point_queries_use_real_paint_order_geometry() {r#"{"element":"div","elements":["div","body","html"]}"#);}++#[test]+fn parser_coalesced_style_text_updates_main_and_child_hit_tests() {+ let mut vm = new_parsed_test_vm(+ "https://parser-style-hit-test.test/",+ "<!doctype html><body></body>",+ );+ let result = vm+ .eval(+ r#"+(() => {+ function exercise(w) {+ const d = w.document;+ d.open();+ d.write('<!doctype html><style>\n');+ d.write('html, body {margin:0;padding:0} #target {width:100px;height:100px}');+ d.write('</style><body><div id=target></div>');+ const rect = d.getElementById('target').getBoundingClientRect();+ const result = {rect:[rect.x,rect.y,rect.width,rect.height],+ hits:d.elementsFromPoint(1,1).map(e => e.id || e.localName)};+ d.close();+ return result;+ }+ const main = exercise(window);+ const frame = document.body.appendChild(document.createElement('iframe'));+ const child = exercise(frame.contentWindow);+ return JSON.stringify([main,child]);+})()+"#,+ )+ .expect("coalesced parser text must update its stylesheet before layout queries");+ let expected = serde_json::json!({"rect":[0,0,100,100], "hits":["target","body","html"]});+ assert_eq!(+ serde_json::from_str::<serde_json::Value>(&result).unwrap(),+ serde_json::json!([expected, expected]),+ );+}++#[test]+fn parser_coalesced_text_notifies_main_and_child_mutation_observers() {+ let mut vm = new_parsed_test_vm(+ "https://parser-text-observer.test/",+ "<!doctype html><body></body>",+ );+ let result = vm.eval(r#"+(() => {+ function exercise(w) {+ const d = w.document;+ d.open(); d.write('<!doctype html><body><p>a');+ const p = d.querySelector('p'), text = p.firstChild;+ const observer = new w.MutationObserver(() => {});+ observer.observe(p, {subtree:true,childList:true,characterData:true,characterDataOldValue:true});+ d.write('bc');+ const result = {sameText:p.firstChild === text, children:p.childNodes.length,+ records:observer.takeRecords().map(r => [r.type,r.target === text,r.oldValue,r.target.data])};+ observer.disconnect();+ d.write('</p>'); d.close();+ return result;+ }+ const main = exercise(window);+ const frame = document.body.appendChild(document.createElement('iframe'));+ return JSON.stringify([main,exercise(frame.contentWindow)]);+})()+"#).expect("parser text coalescing must report characterData without replacing the Text node");+ let expected = serde_json::json!({"sameText":true,"children":1,+ "records":[["characterData",true,"a","abc"]]});+ assert_eq!(+ serde_json::from_str::<serde_json::Value>(&result).unwrap(),+ serde_json::json!([expected, expected]),+ );+}#[test]fn document_point_queries_parse_webidl_coordinates() {let mut vm = new_parsed_test_vm(