Skip to content

Truly independent web browser

LibWeb: Skip parser insertions that the DOM cannot hold

Andreas Kling78475ded69d6
diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp
index f845162f89e..d8e29b93718 100644
--- a/Libraries/LibWeb/DOM/Node.cpp
+++ b/Libraries/LibWeb/DOM/Node.cpp
@@ -3313,11 +3313,8 @@ bool Node::is_host_including_inclusive_ancestor_of(Node const& other) const
return true;
// or if B’s root has a non-null host and A is a host-including inclusive ancestor of B’s root’s host
- if (is<DocumentFragment>(other.root())
- && static_cast<DocumentFragment const&>(other.root()).host()
- && is_inclusive_ancestor_of(*static_cast<DocumentFragment const&>(other.root()).host())) {
- return true;
- }
+ if (auto const* fragment = as_if<DocumentFragment>(other.root()); fragment && fragment->host())
+ return is_host_including_inclusive_ancestor_of(*fragment->host());
return false;
}
diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
index d528f84f17b..1eef3eda258 100644
--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
@@ -195,6 +195,7 @@ extern "C" void ladybird_html_parser_set_script_source_line(void*, size_t, size_
extern "C" void ladybird_html_parser_mark_script_already_started(void*, size_t);
extern "C" void ladybird_html_parser_process_meta_element(void*, size_t);
extern "C" size_t ladybird_html_parser_parent_node(size_t);
+extern "C" bool ladybird_html_parser_element_cannot_be_inserted(size_t, size_t);
extern "C" size_t ladybird_html_parser_node_index(size_t);
extern "C" size_t ladybird_html_parser_create_element(void*, size_t, RustFfiHtmlNamespace, u16 const*, size_t, size_t, RustFfiHtmlParserAttribute const*, size_t, bool, size_t, bool);
extern "C" void ladybird_html_parser_append_child(size_t, size_t);
@@ -3126,6 +3127,28 @@ extern "C" size_t ladybird_html_parser_parent_node(size_t node)
return reinterpret_cast<size_t>(parent);
}
+// https://html.spec.whatwg.org/multipage/parsing.html#insert-an-element-at-the-adjusted-insertion-location
+// NB: The conditions of step 2.
+extern "C" bool ladybird_html_parser_element_cannot_be_inserted(size_t target_parent, size_t element)
+{
+ auto& target_parent_node = node_from_html_parser_ffi(target_parent);
+ auto& element_node = node_from_html_parser_ffi(element);
+
+ // - element's parent is non-null;
+ if (element_node.parent())
+ return true;
+
+ // - element is a host-including inclusive ancestor of targetParent; or
+ if (element_node.is_host_including_inclusive_ancestor_of(target_parent_node))
+ return true;
+
+ // - targetParent is a Document node that already has an element child,
+ if (is<DOM::Document>(target_parent_node) && target_parent_node.has_child_of_type<DOM::Element>())
+ return true;
+
+ return false;
+}
+
extern "C" size_t ladybird_html_parser_node_index(size_t node)
{
return node_from_html_parser_ffi(node).index();
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs
index 420187c9eb6..74f7b106704 100644
--- a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs
+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs
@@ -136,6 +136,7 @@ unsafe extern "C" {
fn ladybird_html_parser_mark_script_already_started(parser: *mut c_void, element: usize);
fn ladybird_html_parser_process_meta_element(parser: *mut c_void, element: usize);
fn ladybird_html_parser_parent_node(node: usize) -> usize;
+ fn ladybird_html_parser_element_cannot_be_inserted(target_parent: usize, element: usize) -> bool;
fn ladybird_html_parser_node_index(node: usize) -> usize;
fn ladybird_html_parser_create_element(
parser: *mut c_void,
@@ -3465,10 +3466,14 @@ impl TreeBuilder {
token.had_duplicate_attribute(),
);
self.recycle_attribute_buffers(attributes);
+ // NB: Creating element can run a custom element constructor, which can insert element or another element into
+ // the Document. Skip the insertion in that case, as step 2 of "insert an element at the adjusted insertion
+ // location" does.
+ let cannot_be_inserted = unsafe { ladybird_html_parser_element_cannot_be_inserted(document, element) };
// NB: When the parser's parser sanitizer configuration is not null, the element is inserted as described in
// "insert an element at the adjusted insertion location" with (the Document, null), so that a removed html
// element is not inserted.
- if self.sanitizer_action(element) == SanitizerAction::Keep {
+ if !cannot_be_inserted && self.sanitizer_action(element) == SanitizerAction::Keep {
self.append_child(document, element);
}
self.stack_of_open_elements.push(StackNode {
@@ -3787,11 +3792,15 @@ impl TreeBuilder {
// NB: The implementation names overrideLocation insertion_location.
let insertion_location = self.adjusted_insertion_location(insertion_location);
- // FIXME: 2. If any of the following are true:
- // - element's parent is non-null;
- // - element is a host-including inclusive ancestor of targetParent; or
- // - targetParent is a Document node that already has an element child,
- // then return.
+ // 2. If any of the following are true:
+ // - element's parent is non-null;
+ // - element is a host-including inclusive ancestor of targetParent; or
+ // - targetParent is a Document node that already has an element child,
+ // then return.
+ // NB: Creating element can run a custom element constructor, which can move element or targetParent.
+ if unsafe { ladybird_html_parser_element_cannot_be_inserted(insertion_location.parent, element) } {
+ return;
+ }
// 3. If alreadySanitized is false and the parser's parser sanitizer configuration is not null:
if !already_sanitized && self.sanitizing {
diff --git a/Tests/LibWeb/Crash/wpt-import/custom-elements/custom-button-abort-insertion-crash.html b/Tests/LibWeb/Crash/wpt-import/custom-elements/custom-button-abort-insertion-crash.html
new file mode 100644
index 00000000000..fc14c308772
--- /dev/null
+++ b/Tests/LibWeb/Crash/wpt-import/custom-elements/custom-button-abort-insertion-crash.html
@@ -0,0 +1,22 @@
+<!doctype html>
+<html class="test-wait">
+<title>Custom button element insertion is aborted after its form is set.</title>
+<script src="../common/gc.js"></script>
+<script>
+customElements.define("x-b", class extends HTMLButtonElement {
+ constructor() {
+ super();
+ window.button = this;
+ this.appendChild(document.querySelector("form"));
+ }
+}, { extends: "button" });
+
+addEventListener("load", async () => {
+ button.removeChild(button.firstChild);
+ await garbageCollect();
+ button.form?.append("test");
+ document.documentElement.classList.remove("test-wait");
+});
+</script>
+<form><button is="x-b"></button></form>
+</html>
diff --git a/Tests/LibWeb/Crash/wpt-import/custom-elements/custom-image-abort-insertion-crash.html b/Tests/LibWeb/Crash/wpt-import/custom-elements/custom-image-abort-insertion-crash.html
new file mode 100644
index 00000000000..e34706e996a
--- /dev/null
+++ b/Tests/LibWeb/Crash/wpt-import/custom-elements/custom-image-abort-insertion-crash.html
@@ -0,0 +1,22 @@
+<!doctype html>
+<html class="test-wait">
+<title>Custom image element insertion is aborted after its form is set.</title>
+<script src="../common/gc.js"></script>
+<script>
+customElements.define("x-i", class extends HTMLImageElement {
+ constructor() {
+ super();
+ window.image = this;
+ this.appendChild(document.querySelector("form"));
+ }
+}, { extends: "img" });
+
+addEventListener("load", async () => {
+ image.removeChild(image.firstChild);
+ await garbageCollect();
+ image.removeAttribute("id");
+ document.documentElement.classList.remove('test-wait');
+});
+</script>
+<form><img is="x-i" id="i"></form>
+</html>
diff --git a/Tests/LibWeb/Text/expected/HTML/custom-element-parser-insertion-into-nested-shadow-root.txt b/Tests/LibWeb/Text/expected/HTML/custom-element-parser-insertion-into-nested-shadow-root.txt
new file mode 100644
index 00000000000..3476c46e618
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/custom-element-parser-insertion-into-nested-shadow-root.txt
@@ -0,0 +1,5 @@
+element parent: null
+parent moved into inner shadow root: true
+parent child count: 0
+element text: child text
+appendChild threw HierarchyRequestError
diff --git a/Tests/LibWeb/Text/expected/HTML/custom-element-parser-root-insertion-after-constructor.txt b/Tests/LibWeb/Text/expected/HTML/custom-element-parser-root-insertion-after-constructor.txt
new file mode 100644
index 00000000000..46803d5e55e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/custom-element-parser-root-insertion-after-constructor.txt
@@ -0,0 +1,4 @@
+constructor ran: true
+document element is the constructor's element: true
+root element parent: null
+document element children: 1
diff --git a/Tests/LibWeb/Text/input/HTML/custom-element-parser-insertion-into-nested-shadow-root.html b/Tests/LibWeb/Text/input/HTML/custom-element-parser-insertion-into-nested-shadow-root.html
new file mode 100644
index 00000000000..0aff701eeea
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/custom-element-parser-insertion-into-nested-shadow-root.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ let element;
+ let innerHost;
+ customElements.define("x-nested-shadow-mover", class extends HTMLDivElement {
+ constructor() {
+ super();
+ element = this;
+ innerHost = document.createElement("div");
+ this.attachShadow({ mode: "open" }).appendChild(innerHost);
+ innerHost.attachShadow({ mode: "open" }).appendChild(document.getElementById("parent"));
+ }
+ }, { extends: "div" });
+</script>
+<div id="parent"><div is="x-nested-shadow-mover">child text</div></div>
+<script>
+ test(() => {
+ const parent = innerHost.shadowRoot.firstChild;
+ println(`element parent: ${element.parentNode}`);
+ println(`parent moved into inner shadow root: ${parent.id === "parent"}`);
+ println(`parent child count: ${parent.childNodes.length}`);
+ println(`element text: ${element.textContent}`);
+ try {
+ parent.appendChild(element);
+ println("appendChild did not throw");
+ } catch (e) {
+ println(`appendChild threw ${e.name}`);
+ }
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/custom-element-parser-root-insertion-after-constructor.html b/Tests/LibWeb/Text/input/HTML/custom-element-parser-root-insertion-after-constructor.html
new file mode 100644
index 00000000000..a94d467bb27
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/custom-element-parser-root-insertion-after-constructor.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(done => {
+ const iframe = document.createElement("iframe");
+ document.body.appendChild(iframe);
+ const win = iframe.contentWindow;
+ const doc = iframe.contentDocument;
+ let rootElement;
+ let otherElement;
+ win.customElements.define("x-root-inserter", class extends win.HTMLHtmlElement {
+ constructor() {
+ super();
+ rootElement = this;
+ otherElement = doc.createElement("div");
+ doc.appendChild(otherElement);
+ }
+ }, { extends: "html" });
+ doc.open();
+ doc.write('<html is="x-root-inserter"><body>text</body></html>');
+ doc.close();
+ println(`constructor ran: ${rootElement !== undefined}`);
+ println(`document element is the constructor's element: ${doc.documentElement === otherElement}`);
+ println(`root element parent: ${rootElement.parentNode}`);
+ println(`document element children: ${doc.children.length}`);
+ done();
+ });
+</script>