Skip to content

Truly independent web browser

LibWeb: Check parser insertion offsets in a Rust unit test

Aliaksandr Kalenik13e541681b99
diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
index efbdc44de32..63139d32588 100644
--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
@@ -2921,19 +2921,10 @@ static void insert_node_for_parser(DOM::Node& parent, DOM::Node& node, DOM::Node
}
}
-static u64 s_parser_non_append_insertions { 0 };
-
-u64 parser_non_append_insertions()
-{
- return s_parser_non_append_insertions;
-}
-
static NodeAndOffset node_and_offset_from_html_parser_ffi(size_t node, size_t offset)
{
auto& dom_node = node_from_html_parser_ffi(node);
VERIFY(offset == NodeAndOffset::append_child_offset || offset <= dom_node.child_count());
- if (offset != NodeAndOffset::append_child_offset)
- ++s_parser_non_append_insertions;
return { dom_node, offset };
}
diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.h b/Libraries/LibWeb/HTML/Parser/HTMLParser.h
index 151442ec30f..3c9756f8d59 100644
--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.h
+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.h
@@ -242,6 +242,4 @@ RefPtr<CSS::StyleValue const> parse_nonzero_dimension_value(Utf16View);
Optional<Color> parse_legacy_color_value(Utf16View);
RefPtr<CSS::StyleValue const> parse_table_child_element_align_value(Utf16View);
-u64 parser_non_append_insertions();
-
}
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs
index 7b5f95a6dd2..9137c2e4347 100644
--- a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs
+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs
@@ -6171,3 +6171,9 @@ pub unsafe extern "C" fn rust_html_parser_destroy(handle: *mut RustFfiHtmlParser
drop(unsafe { Box::from_raw(handle) });
}
}
+
+#[cfg(test)]
+mod test_host;
+
+#[cfg(test)]
+mod tests;
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/parser/test_host.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/parser/test_host.rs
new file mode 100644
index 00000000000..515f4c2a85a
--- /dev/null
+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/parser/test_host.rs
@@ -0,0 +1,461 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! A fake DOM host that defines the `ladybird_html_parser_*` callbacks so the tree builder can run in Rust tests.
+
+use super::APPEND_CHILD_OFFSET;
+use super::RustFfiHtmlNamespace;
+use super::RustFfiHtmlParserAttribute;
+use super::RustFfiHtmlQuirksMode;
+use super::RustFfiHtmlShadowRootMode;
+use super::RustFfiHtmlSlotAssignmentMode;
+use std::cell::RefCell;
+use std::ffi::c_void;
+use std::ptr::NonNull;
+
+pub(super) const DOCUMENT: usize = 1;
+
+#[derive(Clone, Debug, Eq, PartialEq)]
+pub(super) enum FakeNodeKind {
+ Document,
+ DocumentType,
+ DocumentFragment,
+ ShadowRoot,
+ Element(RustFfiHtmlNamespace),
+ Text(String),
+ Comment,
+}
+
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub(super) enum FakeInsertionCall {
+ InsertNode,
+ InsertText,
+ ReinsertLastNode,
+}
+
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub(super) struct FakeInsertion {
+ pub(super) call: FakeInsertionCall,
+ pub(super) parent: usize,
+ pub(super) explicit_offset: Option<usize>,
+}
+
+struct FakeNode {
+ kind: FakeNodeKind,
+ parent: usize,
+ children: Vec<usize>,
+ fragment_host: usize,
+ template_content: usize,
+ shadow_root: usize,
+}
+
+struct FakeDom {
+ nodes: Vec<FakeNode>,
+ insertions: Vec<FakeInsertion>,
+}
+
+impl FakeDom {
+ fn new() -> Self {
+ let mut dom = Self {
+ nodes: Vec::new(),
+ insertions: Vec::new(),
+ };
+ let document = dom.create_node(FakeNodeKind::Document);
+ assert_eq!(document, DOCUMENT);
+ dom
+ }
+
+ fn create_node(&mut self, kind: FakeNodeKind) -> usize {
+ self.nodes.push(FakeNode {
+ kind,
+ parent: 0,
+ children: Vec::new(),
+ fragment_host: 0,
+ template_content: 0,
+ shadow_root: 0,
+ });
+ self.nodes.len()
+ }
+
+ fn node(&self, handle: usize) -> &FakeNode {
+ &self.nodes[handle - 1]
+ }
+
+ fn node_mut(&mut self, handle: usize) -> &mut FakeNode {
+ &mut self.nodes[handle - 1]
+ }
+
+ fn index_in_parent(&self, node: usize) -> usize {
+ let parent = self.node(node).parent;
+ self.node(parent)
+ .children
+ .iter()
+ .position(|&child| child == node)
+ .unwrap()
+ }
+
+ fn has_element_child(&self, node: usize) -> bool {
+ self.node(node)
+ .children
+ .iter()
+ .any(|&child| matches!(self.node(child).kind, FakeNodeKind::Element(_)))
+ }
+
+ fn is_host_including_inclusive_ancestor(&self, ancestor: usize, node: usize) -> bool {
+ let mut current = node;
+ while current != 0 {
+ if current == ancestor {
+ return true;
+ }
+ let current_node = self.node(current);
+ current = if current_node.parent != 0 {
+ current_node.parent
+ } else {
+ current_node.fragment_host
+ };
+ }
+ false
+ }
+
+ fn record_insertion(&mut self, call: FakeInsertionCall, parent: usize, offset: usize) {
+ let explicit_offset = (offset != APPEND_CHILD_OFFSET).then_some(offset);
+ if let Some(offset) = explicit_offset {
+ assert!(offset <= self.node(parent).children.len());
+ }
+ self.insertions.push(FakeInsertion {
+ call,
+ parent,
+ explicit_offset,
+ });
+ }
+
+ fn child_at_offset(&self, parent: usize, offset: usize) -> Option<usize> {
+ if offset == APPEND_CHILD_OFFSET {
+ return None;
+ }
+ self.node(parent).children.get(offset).copied()
+ }
+
+ fn child_before_offset(&self, parent: usize, offset: usize) -> Option<usize> {
+ let children = &self.node(parent).children;
+ match offset {
+ APPEND_CHILD_OFFSET => children.last().copied(),
+ 0 => None,
+ _ => Some(children[offset - 1]),
+ }
+ }
+
+ fn remove(&mut self, node: usize) {
+ let parent = self.node(node).parent;
+ if parent == 0 {
+ return;
+ }
+ let index = self.index_in_parent(node);
+ self.node_mut(parent).children.remove(index);
+ self.node_mut(node).parent = 0;
+ }
+
+ fn insert_before(&mut self, parent: usize, node: usize, reference_child: Option<usize>) {
+ self.remove(node);
+ let index = match reference_child {
+ Some(reference_child) => self.index_in_parent(reference_child),
+ None => self.node(parent).children.len(),
+ };
+ self.node_mut(parent).children.insert(index, node);
+ self.node_mut(node).parent = parent;
+ }
+}
+
+thread_local! {
+ static FAKE_DOM: RefCell<FakeDom> = RefCell::new(FakeDom::new());
+}
+
+fn with_fake_dom<R>(callback: impl FnOnce(&mut FakeDom) -> R) -> R {
+ FAKE_DOM.with_borrow_mut(callback)
+}
+
+pub(super) fn fake_parser_host() -> *mut c_void {
+ NonNull::<c_void>::dangling().as_ptr()
+}
+
+pub(super) fn reset() {
+ with_fake_dom(|dom| *dom = FakeDom::new());
+}
+
+pub(super) fn insertions() -> Vec<FakeInsertion> {
+ with_fake_dom(|dom| dom.insertions.clone())
+}
+
+pub(super) fn children(node: usize) -> Vec<usize> {
+ with_fake_dom(|dom| dom.node(node).children.clone())
+}
+
+pub(super) fn kind(node: usize) -> FakeNodeKind {
+ with_fake_dom(|dom| dom.node(node).kind.clone())
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_document_node(_parser: *mut c_void) -> usize {
+ DOCUMENT
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_document_html_element(_parser: *mut c_void) -> usize {
+ with_fake_dom(|dom| {
+ let document_element = dom
+ .node(DOCUMENT)
+ .children
+ .iter()
+ .copied()
+ .find(|&child| matches!(dom.node(child).kind, FakeNodeKind::Element(_)));
+ match document_element {
+ Some(element) if dom.node(element).kind == FakeNodeKind::Element(RustFfiHtmlNamespace::Html) => element,
+ _ => 0,
+ }
+ })
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_set_document_quirks_mode(_parser: *mut c_void, _mode: RustFfiHtmlQuirksMode) {}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_document_is_iframe_srcdoc_document(_parser: *mut c_void) -> bool {
+ false
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_log_parse_error(_parser: *mut c_void, _message_ptr: *const u8, _message_len: usize) {
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_stop_parsing(_parser: *mut c_void) {}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_parse_errors_enabled() -> bool {
+ false
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_visit_node(_visitor: *mut c_void, _node: usize) {}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_create_document_type(
+ _parser: *mut c_void,
+ _name_ptr: *const u16,
+ _name_len: usize,
+ _public_id_ptr: *const u16,
+ _public_id_len: usize,
+ _system_id_ptr: *const u16,
+ _system_id_len: usize,
+) -> usize {
+ with_fake_dom(|dom| dom.create_node(FakeNodeKind::DocumentType))
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_create_comment(
+ _parser: *mut c_void,
+ _data_ptr: *const u16,
+ _data_len: usize,
+) -> usize {
+ with_fake_dom(|dom| dom.create_node(FakeNodeKind::Comment))
+}
+
+#[unsafe(no_mangle)]
+unsafe extern "C" fn ladybird_html_parser_insert_text(
+ parent: usize,
+ offset: usize,
+ data_ptr: *const u8,
+ data_len: usize,
+) {
+ let data = std::str::from_utf8(unsafe { ak::slice_from_raw_parts(data_ptr, data_len) }).unwrap();
+ with_fake_dom(|dom| {
+ dom.record_insertion(FakeInsertionCall::InsertText, parent, offset);
+ if dom.node(parent).kind == FakeNodeKind::Document {
+ return;
+ }
+ if let Some(previous_child) = dom.child_before_offset(parent, offset)
+ && let FakeNodeKind::Text(previous_text) = &mut dom.node_mut(previous_child).kind
+ {
+ previous_text.push_str(data);
+ return;
+ }
+ let reference_child = dom.child_at_offset(parent, offset);
+ let text = dom.create_node(FakeNodeKind::Text(data.to_string()));
+ dom.insert_before(parent, text, reference_child);
+ })
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_add_missing_attribute(
+ _parser: *mut c_void,
+ _element: usize,
+ _local_name: usize,
+ _value_ptr: *const u16,
+ _value_len: usize,
+) {
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_remove_node(node: usize) {
+ with_fake_dom(|dom| dom.remove(node))
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_handle_element_popped(_element: usize) {}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_set_script_source_line(
+ _parser: *mut c_void,
+ _element: usize,
+ _source_line_number: usize,
+) {
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_mark_script_already_started(_parser: *mut c_void, _element: usize) {}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_process_meta_element(_parser: *mut c_void, _element: usize) {}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_parent_node(node: usize) -> usize {
+ with_fake_dom(|dom| dom.node(node).parent)
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_element_cannot_be_inserted(target_parent: usize, element: usize) -> bool {
+ with_fake_dom(|dom| {
+ dom.node(element).parent != 0
+ || dom.is_host_including_inclusive_ancestor(element, target_parent)
+ || (dom.node(target_parent).kind == FakeNodeKind::Document && dom.has_element_child(target_parent))
+ })
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_node_index(node: usize) -> usize {
+ with_fake_dom(|dom| dom.index_in_parent(node))
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_create_element(
+ _parser: *mut c_void,
+ _intended_parent: usize,
+ namespace_: RustFfiHtmlNamespace,
+ _namespace_uri_ptr: *const u16,
+ _namespace_uri_len: usize,
+ _local_name: usize,
+ _attributes: *const RustFfiHtmlParserAttribute,
+ _attribute_count: usize,
+ _had_duplicate_attribute: bool,
+ _form_element: usize,
+ _has_template_element_on_stack: bool,
+) -> usize {
+ with_fake_dom(|dom| dom.create_node(FakeNodeKind::Element(namespace_)))
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_append_child(parent: usize, child: usize) {
+ with_fake_dom(|dom| dom.insert_before(parent, child, None))
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_insert_node(
+ parent: usize,
+ offset: usize,
+ child: usize,
+ _queue_custom_element_reactions: bool,
+) {
+ with_fake_dom(|dom| {
+ dom.record_insertion(FakeInsertionCall::InsertNode, parent, offset);
+ let reference_child = dom.child_at_offset(parent, offset);
+ dom.insert_before(parent, child, reference_child);
+ })
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_reinsert_last_node(parent: usize, offset: usize, last_node: usize) {
+ with_fake_dom(|dom| {
+ dom.record_insertion(FakeInsertionCall::ReinsertLastNode, parent, offset);
+ let reference_child = dom.child_at_offset(parent, offset);
+ dom.remove(last_node);
+ let can_be_inserted = !dom.is_host_including_inclusive_ancestor(last_node, parent)
+ && (dom.node(parent).kind != FakeNodeKind::Document || !dom.has_element_child(parent))
+ && reference_child.is_none_or(|reference_child| dom.node(reference_child).parent == parent);
+ if can_be_inserted {
+ dom.insert_before(parent, last_node, reference_child);
+ }
+ })
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_move_all_children(from: usize, to: usize) {
+ with_fake_dom(|dom| {
+ for child in dom.node(from).children.clone() {
+ dom.insert_before(to, child, None);
+ }
+ })
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_template_content(element: usize) -> usize {
+ with_fake_dom(|dom| {
+ if dom.node(element).template_content == 0 {
+ let content = dom.create_node(FakeNodeKind::DocumentFragment);
+ dom.node_mut(content).fragment_host = element;
+ dom.node_mut(element).template_content = content;
+ }
+ dom.node(element).template_content
+ })
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_attach_declarative_shadow_root(
+ host: usize,
+ _mode: RustFfiHtmlShadowRootMode,
+ _slot_assignment: RustFfiHtmlSlotAssignmentMode,
+ _clonable: bool,
+ _serializable: bool,
+ _delegates_focus: bool,
+ _keep_custom_element_registry_null: bool,
+) -> usize {
+ with_fake_dom(|dom| {
+ if dom.node(host).shadow_root != 0 {
+ return 0;
+ }
+ let shadow_root = dom.create_node(FakeNodeKind::ShadowRoot);
+ dom.node_mut(shadow_root).fragment_host = host;
+ dom.node_mut(host).shadow_root = shadow_root;
+ shadow_root
+ })
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_set_template_content(element: usize, content: usize) {
+ with_fake_dom(|dom| dom.node_mut(element).template_content = content)
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_is_shadow_host(node: usize) -> bool {
+ with_fake_dom(|dom| dom.node(node).shadow_root != 0)
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_sanitizer_action(_parser: *mut c_void, _element: usize) -> u8 {
+ 0
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_child_at_index(parent: usize, index: usize) -> usize {
+ with_fake_dom(|dom| dom.node(parent).children.get(index).copied().unwrap_or(0))
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_declarative_shadow_root_attributes(_element: usize) -> u32 {
+ 0
+}
+
+#[unsafe(no_mangle)]
+extern "C" fn ladybird_html_parser_set_script_text_from_parser(_element: usize) {}
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/parser/tests.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/parser/tests.rs
new file mode 100644
index 00000000000..2d38c2bbe04
--- /dev/null
+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/parser/tests.rs
@@ -0,0 +1,170 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+use super::RustFfiHtmlNamespace;
+use super::RustFfiHtmlParserHandle;
+use super::RustFfiHtmlParserRunResult;
+use super::rust_html_parser_create;
+use super::rust_html_parser_destroy;
+use super::rust_html_parser_pop_all_open_elements;
+use super::rust_html_parser_run_document;
+use super::test_host;
+use super::test_host::FakeInsertion;
+use super::test_host::FakeInsertionCall;
+use super::test_host::FakeNodeKind;
+use crate::RustFfiTokenizerHandle;
+use crate::make_handle;
+use crate::rust_html_tokenizer_destroy;
+use crate::tokenizer::HtmlTokenizer;
+
+const HTML_ELEMENT: FakeNodeKind = FakeNodeKind::Element(RustFfiHtmlNamespace::Html);
+
+struct ScriptCreatedParser {
+ parser: *mut RustFfiHtmlParserHandle,
+ tokenizer: *mut RustFfiTokenizerHandle,
+}
+
+impl ScriptCreatedParser {
+ fn open() -> Self {
+ test_host::reset();
+ let mut parser = Self {
+ parser: rust_html_parser_create(),
+ tokenizer: make_handle(HtmlTokenizer::new(Vec::new())),
+ };
+ parser.tokenizer().set_input_stream_closed(false);
+ parser.tokenizer().update_insertion_point();
+ parser
+ }
+
+ fn tokenizer(&mut self) -> &mut HtmlTokenizer {
+ unsafe { &mut (*self.tokenizer).tokenizer }
+ }
+
+ fn run(&mut self, stop_at_insertion_point: bool) {
+ let result = unsafe {
+ rust_html_parser_run_document(
+ self.parser,
+ self.tokenizer,
+ test_host::fake_parser_host(),
+ true,
+ false,
+ stop_at_insertion_point,
+ )
+ };
+ assert_eq!(result, RustFfiHtmlParserRunResult::Ok);
+ }
+
+ fn write(&mut self, markup: &str) {
+ let code_units: Vec<u16> = markup.encode_utf16().collect();
+ self.tokenizer().insert_input_at_insertion_point(&code_units);
+ self.run(true);
+ }
+
+ fn close(&mut self) {
+ self.tokenizer().insert_eof();
+ self.run(false);
+ unsafe { rust_html_parser_pop_all_open_elements(self.parser) };
+ }
+}
+
+impl Drop for ScriptCreatedParser {
+ fn drop(&mut self) {
+ unsafe {
+ rust_html_parser_destroy(self.parser);
+ rust_html_tokenizer_destroy(self.tokenizer);
+ }
+ }
+}
+
+fn body_element() -> usize {
+ let html_element = *test_host::children(test_host::DOCUMENT).last().unwrap();
+ test_host::children(html_element)[1]
+}
+
+fn kinds_of(nodes: &[usize]) -> Vec<FakeNodeKind> {
+ nodes.iter().map(|&node| test_host::kind(node)).collect()
+}
+
+fn insertions_with_explicit_offset() -> Vec<FakeInsertion> {
+ test_host::insertions()
+ .into_iter()
+ .filter(|insertion| insertion.explicit_offset.is_some())
+ .collect()
+}
+
+#[test]
+fn script_created_parser_appends_every_flat_sibling() {
+ const SIBLING_COUNT: usize = 5000;
+ let mut parser = ScriptCreatedParser::open();
+ parser.write(&format!("{}<span>PASS</span>", "<span></span>".repeat(SIBLING_COUNT)));
+ parser.close();
+
+ let body = body_element();
+ let insertions_into_body = test_host::insertions()
+ .into_iter()
+ .filter(|insertion| insertion.parent == body)
+ .count();
+ assert_eq!(insertions_into_body, SIBLING_COUNT + 1);
+ let explicit_offset_insertions = insertions_with_explicit_offset();
+ assert!(explicit_offset_insertions.is_empty(), "{explicit_offset_insertions:?}");
+
+ let body_children = test_host::children(body);
+ assert_eq!(kinds_of(&body_children), vec![HTML_ELEMENT; SIBLING_COUNT + 1]);
+ assert_eq!(
+ kinds_of(&test_host::children(body_children[SIBLING_COUNT])),
+ [FakeNodeKind::Text("PASS".to_string())]
+ );
+}
+
+#[test]
+fn foster_parented_element_is_inserted_before_the_table_at_an_explicit_offset() {
+ let mut parser = ScriptCreatedParser::open();
+ parser.write("<table><b>fostered</b></table>");
+ parser.close();
+
+ let body = body_element();
+ assert_eq!(
+ insertions_with_explicit_offset(),
+ [FakeInsertion {
+ call: FakeInsertionCall::InsertNode,
+ parent: body,
+ explicit_offset: Some(0),
+ }]
+ );
+
+ let [fostered_element, table] = test_host::children(body)[..] else {
+ panic!("expected the fostered element and the table in body");
+ };
+ assert_eq!(kinds_of(&[fostered_element, table]), [HTML_ELEMENT, HTML_ELEMENT]);
+ assert_eq!(
+ kinds_of(&test_host::children(fostered_element)),
+ [FakeNodeKind::Text("fostered".to_string())]
+ );
+ assert!(test_host::children(table).is_empty());
+}
+
+#[test]
+fn foster_parented_text_is_inserted_before_the_table_at_an_explicit_offset() {
+ let mut parser = ScriptCreatedParser::open();
+ parser.write("<table>fostered</table>");
+ parser.close();
+
+ let body = body_element();
+ let fostered_insertions = insertions_with_explicit_offset();
+ assert!(!fostered_insertions.is_empty());
+ assert!(
+ fostered_insertions
+ .iter()
+ .all(|insertion| insertion.call == FakeInsertionCall::InsertText && insertion.parent == body)
+ );
+
+ let body_children = test_host::children(body);
+ assert_eq!(
+ kinds_of(&body_children),
+ [FakeNodeKind::Text("fostered".to_string()), HTML_ELEMENT]
+ );
+ assert!(test_host::children(body_children[1]).is_empty());
+}
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index 2a576e89631..f3e60c79b5d 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -2627,11 +2627,6 @@ bool Internals::html_parser_body_is_exhausted(DOM::Document& document)
return parser && parser->streaming_body_is_exhausted();
}
-u64 Internals::parser_non_append_insertions()
-{
- return HTML::parser_non_append_insertions();
-}
-
bool Internals::media_element_is_fetching(HTML::HTMLMediaElement& element)
{
return element.is_fetching();
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index e7125de9dcc..bc06188ecf8 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -297,7 +297,6 @@ public:
void click_notification_by_id_for_testing(WebIDL::UnsignedLongLong browser_notification_id);
void dismiss_notification_for_testing(NotificationsAPI::Notification&);
- u64 parser_non_append_insertions();
void set_manual_rendering_opportunities(bool enabled);
void inject_rendering_opportunity(double frame_time_ms);
Utf16String frame_scheduler_state() const;
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index c3c42eb840a..c71f3e8f62b 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -341,8 +341,6 @@ interface Internals {
undefined clearElement(HTMLElement element);
- // Returns the number of insertions that computed an explicit child offset instead of using the append sentinel.
- unsigned long long parserNonAppendInsertions();
// Records what the rendering of the window agent does until takeRenderingTrace(), one event per line, a run of the
// same event as one line with how many times it ran: rendering update requests, opportunities, updates and paints,
diff --git a/Meta/Linters/check_rust_process_wide_state.py b/Meta/Linters/check_rust_process_wide_state.py
index 244fa5fec52..ee178f25675 100755
--- a/Meta/Linters/check_rust_process_wide_state.py
+++ b/Meta/Linters/check_rust_process_wide_state.py
@@ -37,6 +37,7 @@ ALLOWED = {
"Libraries/LibJS/JIT/Rust/src/asm/x86_64/sse.rs:FEATURES": "immutable CPU/OS feature cache; each copy computes the same supported instructions",
"Libraries/LibJS/JIT/Rust/src/codegen/tests.rs:REGION": "the fake heap region of JIT unit tests, in the cargo test binary that has no runtime",
"Libraries/LibRegex/Rust/src/vm.rs:INTERRUPT_FLAG": "per-thread, and only libjs_rust's copy sets it; the copy of LibURL's crate has no VM to interrupt, so it only ever sees null",
+ "Libraries/LibWeb/HTML/Parser/Rust/src/parser/test_host.rs:FAKE_DOM": "the fake DOM of tree builder unit tests, in the cargo test binary that has no C++ side",
"Libraries/LibWeb/HTML/Parser/Rust/src/token.rs:SPARE_ATTRIBUTE_LISTS": "per-thread allocation pool; a second copy costs memory, never an answer",
"Libraries/LibWeb/HTML/Parser/Rust/src/token.rs:SPARE_ATTRIBUTE_VALUES": "per-thread allocation pool; a second copy costs memory, never an answer",
}
diff --git a/Tests/LibWeb/Text/expected/html-parser-foster-parented-insertion-is-counted.txt b/Tests/LibWeb/Text/expected/html-parser-foster-parented-insertion-is-counted.txt
deleted file mode 100644
index b98d86d6c54..00000000000
--- a/Tests/LibWeb/Text/expected/html-parser-foster-parented-insertion-is-counted.txt
+++ /dev/null
@@ -1 +0,0 @@
-a foster-parented insertion computed an explicit offset
diff --git a/Tests/LibWeb/Text/expected/html-parser-script-created-parser-many-flat-siblings.txt b/Tests/LibWeb/Text/expected/html-parser-script-created-parser-many-flat-siblings.txt
deleted file mode 100644
index caee7855402..00000000000
--- a/Tests/LibWeb/Text/expected/html-parser-script-created-parser-many-flat-siblings.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-PASS
-parser appended every sibling using the append sentinel
diff --git a/Tests/LibWeb/Text/input/html-parser-foster-parented-insertion-is-counted.html b/Tests/LibWeb/Text/input/html-parser-foster-parented-insertion-is-counted.html
deleted file mode 100644
index 2d166363eb2..00000000000
--- a/Tests/LibWeb/Text/input/html-parser-foster-parented-insertion-is-counted.html
+++ /dev/null
@@ -1,15 +0,0 @@
-<!DOCTYPE html>
-<script src="include.js"></script>
-<script>
- // Liveness canary for internals.parserNonAppendInsertions().
- const before = internals.parserNonAppendInsertions();
- document.write("<table><b>fostered</b></table>");
- const nonAppendInsertions = internals.parserNonAppendInsertions() - before;
-
- test(() => {
- if (nonAppendInsertions > 0)
- println("a foster-parented insertion computed an explicit offset");
- else
- println(`FAIL: expected a foster-parented insertion to compute an explicit offset, got ${nonAppendInsertions}`);
- });
-</script>
diff --git a/Tests/LibWeb/Text/input/html-parser-script-created-parser-many-flat-siblings.html b/Tests/LibWeb/Text/input/html-parser-script-created-parser-many-flat-siblings.html
deleted file mode 100644
index 8c8a7220629..00000000000
--- a/Tests/LibWeb/Text/input/html-parser-script-created-parser-many-flat-siblings.html
+++ /dev/null
@@ -1,22 +0,0 @@
-<!DOCTYPE html>
-<script src="include.js"></script>
-<script>
- // Regression test for a problem where the parser computed every append-style insertion location by counting the
- // parent’s children. That's an O(children) sibling-list walk, so inserting N flat siblings performed O(N^2) sibling
- // hops overall. A previous test was racy due to trying to catch the regression through timing measurement. This
- // test instead doesn't rely on timing at all. Appends now resolve to an append sentinel, not an explicit child
- // index; internals.parserNonAppendInsertions() counts insertions that carried an explicit offset rather than the
- // sentinel — so appending flat siblings must add zero.
- const siblingCount = 5000;
- const before = internals.parserNonAppendInsertions();
- document.write("<span></span>".repeat(siblingCount) + "<span>PASS</span>");
- const nonAppendInsertions = internals.parserNonAppendInsertions() - before;
-
- test(() => {
- println(document.getElementsByTagName("span").item(siblingCount).textContent);
- if (nonAppendInsertions === 0)
- println("parser appended every sibling using the append sentinel");
- else
- println(`parser computed an explicit insertion offset ${nonAppendInsertions} time(s) while appending ${siblingCount} flat siblings`);
- });
-</script>