Skip to content

Truly independent web browser

LibWeb: Cover foreign namespaces in HTML attribute reflection

Andreas Kling5ffd1a5435cb
diff --git a/Tests/LibWeb/Text/expected/DOM/reflected-attributes-namespaces.txt b/Tests/LibWeb/Text/expected/DOM/reflected-attributes-namespaces.txt
new file mode 100644
index 00000000000..be5b36fe5e9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/DOM/reflected-attributes-namespaces.txt
@@ -0,0 +1,26 @@
+Foreign boolean (false): false
+Boolean setter preserves foreign attribute (false): false
+Foreign integer (false): false
+Integer setter uses null namespace (false): false
+Foreign unsigned integer (false): false
+Unsigned setter uses null namespace (false): false
+Foreign enumerated reflection (false): false
+Nullable setter preserves foreign attribute (false): false
+Foreign URL (false): false
+URL setter uses null namespace (false): false
+Foreign enumerated attribute (false): false
+Foreign element reflection (false): false
+Foreign selected attribute (false): false
+Foreign boolean (true): true
+Boolean setter preserves foreign attribute (true): true
+Foreign integer (true): true
+Integer setter uses null namespace (true): true
+Foreign unsigned integer (true): true
+Unsigned setter uses null namespace (true): true
+Foreign enumerated reflection (true): true
+Nullable setter preserves foreign attribute (true): true
+Foreign URL (true): true
+URL setter uses null namespace (true): true
+Foreign enumerated attribute (true): false
+Foreign element reflection (true): true
+Foreign selected attribute (true): false
diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/namespaced-attribute-mutations.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/namespaced-attribute-mutations.txt
new file mode 100644
index 00000000000..cd7436970cd
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/namespaced-attribute-mutations.txt
@@ -0,0 +1,30 @@
+Harness status: OK
+
+Found 24 tests
+
+20 Pass
+4 Fail
+Pass append a namespaced style attribute preserves HTML inline style
+Pass append a namespaced event handler attribute preserves an HTML event handler
+Pass change a namespaced style attribute preserves HTML inline style
+Pass change a namespaced event handler attribute preserves an HTML event handler
+Pass remove a namespaced style attribute preserves HTML inline style
+Pass remove a namespaced event handler attribute preserves an HTML event handler
+Pass replace a namespaced style attribute preserves HTML inline style
+Pass replace a namespaced event handler attribute preserves an HTML event handler
+Pass append a namespaced style attribute preserves SVG inline style
+Pass append a namespaced event handler attribute preserves an SVG event handler
+Pass change a namespaced style attribute preserves SVG inline style
+Pass change a namespaced event handler attribute preserves an SVG event handler
+Pass remove a namespaced style attribute preserves SVG inline style
+Pass remove a namespaced event handler attribute preserves an SVG event handler
+Pass replace a namespaced style attribute preserves SVG inline style
+Pass replace a namespaced event handler attribute preserves an SVG event handler
+Pass A namespaced slot attribute does not assign a slot
+Fail A namespaced open attribute does not fire a toggle event
+Fail A namespaced src attribute does not navigate an iframe
+Pass A namespaced src attribute does not prepare a script
+Fail A namespaced type attribute does not change an input's state
+Fail A namespaced selected attribute does not change an option's selectedness
+Pass A namespaced multiple attribute does not reset a select
+Pass A namespaced size attribute does not reset a select
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/DOM/reflected-attributes-namespaces.html b/Tests/LibWeb/Text/input/DOM/reflected-attributes-namespaces.html
new file mode 100644
index 00000000000..6d952c03f37
--- /dev/null
+++ b/Tests/LibWeb/Text/input/DOM/reflected-attributes-namespaces.html
@@ -0,0 +1,59 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const namespace = "https://example.com/attributes";
+ for (const qualified of [false, true]) {
+ const name = local => qualified ? `foreign:${local}` : local;
+ const details = document.createElement("details");
+ details.setAttributeNS(namespace, name("open"), "");
+ println(`Foreign boolean (${qualified}): ${details.open === false}`);
+ details.open = true;
+ details.open = false;
+ println(`Boolean setter preserves foreign attribute (${qualified}): ${details.hasAttributeNS(namespace, "open") && !details.hasAttributeNS(null, "open")}`);
+
+ const pre = document.createElement("pre");
+ pre.setAttributeNS(namespace, name("width"), "17");
+ println(`Foreign integer (${qualified}): ${pre.width === 0}`);
+ pre.width = 5;
+ println(`Integer setter uses null namespace (${qualified}): ${pre.width === 5 && pre.getAttributeNS(namespace, "width") === "17"}`);
+
+ const source = document.createElement("source");
+ source.setAttributeNS(namespace, name("width"), "17");
+ println(`Foreign unsigned integer (${qualified}): ${source.width === 0}`);
+ source.width = 5;
+ println(`Unsigned setter uses null namespace (${qualified}): ${source.width === 5 && source.getAttributeNS(namespace, "width") === "17"}`);
+
+ const image = document.createElement("img");
+ image.setAttributeNS(namespace, name("decoding"), "sync");
+ image.setAttributeNS(namespace, name("crossorigin"), "anonymous");
+ println(`Foreign enumerated reflection (${qualified}): ${image.decoding === "auto" && image.crossOrigin === null}`);
+ image.crossOrigin = null;
+ println(`Nullable setter preserves foreign attribute (${qualified}): ${image.hasAttributeNS(namespace, "crossorigin") && !image.hasAttributeNS(null, "crossorigin")}`);
+
+ const iframe = document.createElement("iframe");
+ iframe.setAttributeNS(namespace, name("src"), "https://example.com/foreign");
+ println(`Foreign URL (${qualified}): ${iframe.src === ""}`);
+ iframe.src = "https://example.com/real";
+ println(`URL setter uses null namespace (${qualified}): ${iframe.src === "https://example.com/real" && iframe.getAttributeNS(namespace, "src") === "https://example.com/foreign"}`);
+
+ const input = document.createElement("input");
+ input.value = "retained";
+ input.setAttributeNS(namespace, name("type"), "file");
+ println(`Foreign enumerated attribute (${qualified}): ${input.type === "text" && input.value === "retained"}`);
+
+ const root = document.createElement("div");
+ const target = document.createElement("span");
+ target.id = "reference";
+ const controller = document.createElement("button");
+ root.append(target, controller);
+ controller.setAttributeNS(namespace, name("aria-activedescendant"), "reference");
+ controller.setAttributeNS(namespace, name("aria-controls"), "reference");
+ println(`Foreign element reflection (${qualified}): ${controller.ariaActiveDescendantElement === null && controller.ariaControlsElements === null}`);
+
+ const option = document.createElement("option");
+ option.setAttributeNS(namespace, name("selected"), "");
+ println(`Foreign selected attribute (${qualified}): ${option.selected === false}`);
+ }
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/namespaced-attribute-mutations.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/namespaced-attribute-mutations.html
new file mode 100644
index 00000000000..6b73ddbbc3f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/namespaced-attribute-mutations.html
@@ -0,0 +1,154 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Namespaced attributes do not trigger null-namespace attribute behavior</title>
+<link rel="help" href="https://dom.spec.whatwg.org/#dom-element-setattributens">
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<body>
+<script>
+"use strict";
+
+const namespace = "https://example.com/attributes";
+const svgNamespace = "http://www.w3.org/2000/svg";
+
+function mutateNamespacedAttribute(element, name, operation, beforeMutation) {
+ if (operation !== "append") {
+ element.setAttributeNS(namespace, name, "before");
+ }
+
+ beforeMutation();
+
+ if (operation === "append" || operation === "change") {
+ element.setAttributeNS(namespace, name, "after");
+ } else if (operation === "remove") {
+ element.removeAttributeNS(namespace, name);
+ } else {
+ const attribute = document.createAttributeNS(namespace, name);
+ attribute.value = "after";
+ element.setAttributeNodeNS(attribute);
+ }
+}
+
+for (const elementNamespace of ["HTML", "SVG"]) {
+ for (const operation of ["append", "change", "remove", "replace"]) {
+ test(() => {
+ const element = elementNamespace === "HTML" ?
+ document.createElement("div") :
+ document.createElementNS(svgNamespace, "g");
+
+ mutateNamespacedAttribute(element, "style", operation, () => {
+ element.setAttributeNS(null, "style", "color: green");
+ });
+
+ assert_equals(element.getAttributeNS(null, "style"), "color: green");
+ assert_equals(element.style.color, "green");
+ }, `${operation} a namespaced style attribute preserves ${elementNamespace} inline style`);
+
+ test(() => {
+ const element = elementNamespace === "HTML" ?
+ document.createElement("div") :
+ document.createElementNS(svgNamespace, "g");
+ let callCount = 0;
+ function handler() {
+ ++callCount;
+ }
+
+ mutateNamespacedAttribute(element, "onclick", operation, () => {
+ element.onclick = handler;
+ });
+
+ assert_equals(element.onclick, handler);
+ element.dispatchEvent(new Event("click"));
+ assert_equals(callCount, 1);
+ }, `${operation} a namespaced event handler attribute preserves an ${elementNamespace} event handler`);
+ }
+}
+
+test(() => {
+ const host = document.createElement("div");
+ const slot = document.createElement("slot");
+ slot.name = "target";
+ host.attachShadow({ mode: "open" }).append(slot);
+
+ const child = document.createElement("span");
+ child.setAttributeNS(namespace, "slot", "target");
+ host.append(child);
+
+ assert_equals(child.slot, "");
+ assert_equals(child.assignedSlot, null);
+ assert_array_equals(slot.assignedNodes(), []);
+}, "A namespaced slot attribute does not assign a slot");
+
+promise_test(async t => {
+ const details = document.createElement("details");
+ let toggleCount = 0;
+ details.addEventListener("toggle", () => {
+ ++toggleCount;
+ });
+
+ details.setAttributeNS(namespace, "open", "");
+ await new Promise(resolve => {
+ t.step_timeout(resolve, 0);
+ });
+
+ assert_false(details.open);
+ assert_equals(toggleCount, 0);
+}, "A namespaced open attribute does not fire a toggle event");
+
+test(t => {
+ const iframe = document.createElement("iframe");
+ t.add_cleanup(() => iframe.remove());
+ document.body.append(iframe);
+ const { contentDocument } = iframe;
+
+ iframe.setAttributeNS(namespace, "src", "data:text/html,namespaced");
+
+ assert_equals(iframe.src, "");
+ assert_equals(iframe.contentDocument, contentDocument);
+}, "A namespaced src attribute does not navigate an iframe");
+
+test(t => {
+ const script = document.createElement("script");
+ t.add_cleanup(() => {
+ script.remove();
+ delete window.namespacedScriptRan;
+ });
+ document.body.append(script);
+
+ script.setAttributeNS(namespace, "src", "data:text/javascript,window.namespacedScriptRan=true");
+
+ assert_false("namespacedScriptRan" in window);
+}, "A namespaced src attribute does not prepare a script");
+
+test(() => {
+ const input = document.createElement("input");
+ input.value = "value";
+
+ input.setAttributeNS(namespace, "type", "file");
+
+ assert_equals(input.type, "text");
+ assert_equals(input.value, "value");
+}, "A namespaced type attribute does not change an input's state");
+
+test(() => {
+ const select = document.createElement("select");
+ select.innerHTML = "<option selected>one</option><option>two</option>";
+ select.selectedIndex = -1;
+
+ select.options[0].setAttributeNS(namespace, "selected", "");
+
+ assert_equals(select.selectedIndex, -1);
+}, "A namespaced selected attribute does not change an option's selectedness");
+
+for (const name of ["multiple", "size"]) {
+ test(() => {
+ const select = document.createElement("select");
+ select.innerHTML = "<option>one</option><option>two</option>";
+ select.selectedIndex = -1;
+
+ select.setAttributeNS(namespace, name, "");
+
+ assert_equals(select.selectedIndex, -1);
+ }, `A namespaced ${name} attribute does not reset a select`);
+}
+</script>