From 98c45f3f038c1e8eeb8aefa6d5514186afa25260 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 10 May 2025 10:31:08 +1200 Subject: [PATCH] Tests/LibWeb: Import a bunch of slots WPT tests --- .../shadow-dom/slots-fallback-in-document.txt | 7 + .../wpt-import/shadow-dom/slots-fallback.txt | 18 + .../shadow-dom/slots-outside-shadow-dom.txt | 6 + .../expected/wpt-import/shadow-dom/slots.txt | 32 ++ .../shadow-dom/resources/shadow-dom.js | 163 ++++++ .../slots-fallback-in-document.html | 55 ++ .../wpt-import/shadow-dom/slots-fallback.html | 253 +++++++++ .../shadow-dom/slots-outside-shadow-dom.html | 16 + .../input/wpt-import/shadow-dom/slots.html | 526 ++++++++++++++++++ 9 files changed, 1076 insertions(+) create mode 100644 Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots-fallback-in-document.txt create mode 100644 Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots-fallback.txt create mode 100644 Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots-outside-shadow-dom.txt create mode 100644 Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots.txt create mode 100644 Tests/LibWeb/Text/input/wpt-import/shadow-dom/resources/shadow-dom.js create mode 100644 Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots-fallback-in-document.html create mode 100644 Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots-fallback.html create mode 100644 Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots-outside-shadow-dom.html create mode 100644 Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots.html diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots-fallback-in-document.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots-fallback-in-document.txt new file mode 100644 index 00000000000..9fed588d4df --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots-fallback-in-document.txt @@ -0,0 +1,7 @@ +Harness status: OK + +Found 2 tests + +2 Fail +Fail Children of a slot in a document tree should not be counted in flattened assigned nodes. +Fail Slot fallback content in shadow tree should be counted in flattened assigned nodes. \ No newline at end of file diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots-fallback.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots-fallback.txt new file mode 100644 index 00000000000..ea2bc3bac9b --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots-fallback.txt @@ -0,0 +1,18 @@ +Harness status: OK + +Found 13 tests + +13 Fail +Fail Slots fallback: Basic. +Fail Slots fallback: Basic, elements only. +Fail Slots fallback: Slots in Slots. +Fail Slots fallback: Slots in Slots, elements only. +Fail Slots fallback: Fallback contents should not be used if a node is assigned. +Fail Slots fallback: Slots in Slots: Assigned nodes should be used as fallback contents of another slot +Fail Slots fallback: Complex case. +Fail Slots fallback: Complex case, elements only. +Fail Slots fallback: Mutation. Append fallback contents. +Fail Slots fallback: Mutation. Remove fallback contents. +Fail Slots fallback: Mutation. Assign a node to a slot so that fallback contens are no longer used. +Fail Slots fallback: Mutation. Remove an assigned node from a slot so that fallback contens will be used. +Fail Slots fallback: Mutation. Remove a slot which is a fallback content of another slot. \ No newline at end of file diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots-outside-shadow-dom.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots-outside-shadow-dom.txt new file mode 100644 index 00000000000..e79de39d284 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots-outside-shadow-dom.txt @@ -0,0 +1,6 @@ +Harness status: OK + +Found 1 tests + +1 Fail +Fail Light DOM slot element should be in flattened assignedNodes \ No newline at end of file diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots.txt new file mode 100644 index 00000000000..499e7f31e2d --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/slots.txt @@ -0,0 +1,32 @@ +Harness status: OK + +Found 26 tests + +12 Pass +14 Fail +Pass Slots: Basic. +Pass Slots: Basic, elements only. +Pass Slots: Slots in closed. +Pass Slots: Slots in closed, elements only. +Pass Slots: Slots not in a shadow tree. +Pass Slots: Slots not in a shadow tree, elements only. +Pass Slots: Distributed nodes for Slots not in a shadow tree. +Pass Slots: Name matching +Pass Slots: No direct host child. +Pass Slots: Default Slot. +Pass Slots: Slot in Slot does not matter in assignment. +Fail Slots: Slot is assigned to another slot +Fail Slots: Open > Closed. +Fail Slots: Closed > Closed. +Fail Slots: Closed > Open. +Fail Slots: Complex case: Basi line. +Fail Slots: Mutation: appendChild. +Fail Slots: Mutation: Change slot= attribute 1. +Fail Slots: Mutation: Change slot= attribute 2. +Fail Slots: Mutation: Change slot= attribute 3. +Fail Slots: Mutation: Remove a child. +Pass Slots: Mutation: Add a slot: after. +Fail Slots: Mutation: Add a slot: before. +Fail Slots: Mutation: Remove a slot. +Fail Slots: Mutation: Change slot name= attribute. +Fail Slots: Mutation: Change slot slot= attribute. \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/resources/shadow-dom.js b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/resources/shadow-dom.js new file mode 100644 index 00000000000..3a4ad39d42a --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/resources/shadow-dom.js @@ -0,0 +1,163 @@ +function removeWhiteSpaceOnlyTextNodes(node) +{ + for (var i = 0; i < node.childNodes.length; i++) { + var child = node.childNodes[i]; + if (child.nodeType === Node.TEXT_NODE && child.nodeValue.trim().length == 0) { + node.removeChild(child); + i--; + } else if (child.nodeType === Node.ELEMENT_NODE || child.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { + removeWhiteSpaceOnlyTextNodes(child); + } + } + if (node.shadowRoot) { + removeWhiteSpaceOnlyTextNodes(node.shadowRoot); + } +} + +function createTestTree(node) { + + let ids = {}; + + function attachShadowFromTemplate(template) { + let parent = template.parentNode; + parent.removeChild(template); + let shadowRoot; + if (template.getAttribute('data-slot-assignment') === 'manual') { + shadowRoot = + parent.attachShadow({mode: template.getAttribute('data-mode'), + slotAssignment: 'manual'}); + } else { + shadowRoot = parent.attachShadow( + {mode: template.getAttribute('data-mode')}); + } + let id = template.id; + if (id) { + shadowRoot.id = id; + ids[id] = shadowRoot; + } + shadowRoot.appendChild(document.importNode(template.content, true)); + return shadowRoot; + } + + function walk(root) { + if (root.id) { + ids[root.id] = root; + } + for (let e of Array.from(root.querySelectorAll('[id]'))) { + ids[e.id] = e; + } + for (let e of Array.from(root.querySelectorAll('template'))) { + walk(attachShadowFromTemplate(e)); + } + } + + walk(node.cloneNode(true)); + return ids; +} + +// TODO: Refactor this so that only interested results are recorded. +// Callers of this function would not be interested in every results. +function dispatchEventWithLog(nodes, target, event, options) { + + function labelFor(e) { + return e.id || e.tagName; + } + + let log = []; + let attachedNodes = []; + for (let label in nodes) { + let startingNode = nodes[label]; + for (let node = startingNode; node; node = node.parentNode) { + if (attachedNodes.indexOf(node) >= 0) + continue; + let id = node.id; + if (!id) + continue; + attachedNodes.push(node); + if (options && options.capture) { + // Record [currentTarget, target, relatedTarget, composedPath(), 'capture' | 'non-capture'] + // TODO: Support registering listeners in different orders. + // e.g. Register a non-capture listener at first, then register a capture listener. + node.addEventListener(event.type, (e) => { + log.push([id, + labelFor(e.target), + e.relatedTarget ? labelFor(e.relatedTarget) : null, + e.composedPath().map((n) => { + return labelFor(n); + }), + 'capture']); + }, true); + node.addEventListener(event.type, (e) => { + log.push([id, + labelFor(e.target), + e.relatedTarget ? labelFor(e.relatedTarget) : null, + e.composedPath().map((n) => { + return labelFor(n); + }), + 'non-capture']); + }); + } else { + // Record [currentTarget, target, relatedTarget, composedPath()] + node.addEventListener(event.type, (e) => { + log.push([id, + labelFor(e.target), + e.relatedTarget ? labelFor(e.relatedTarget) : null, + e.composedPath().map((n) => { + return labelFor(n); + })] + ); + }); + } + } + } + target.dispatchEvent(event); + return log; +} + +// TODO(hayato): Merge this into dispatchEventWithLog +function dispatchUAEventWithLog(nodes, target, eventType, callback) { + + function labelFor(e) { + return e.id || e.tagName; + } + + let log = []; + let attachedNodes = []; + for (let label in nodes) { + let startingNode = nodes[label]; + for (let node = startingNode; node; node = node.parentNode) { + if (attachedNodes.indexOf(node) >= 0) + continue; + let id = node.id; + if (!id) + continue; + attachedNodes.push(node); + node.addEventListener(eventType, (e) => { + // Record [currentTarget, target, relatedTarget, composedPath()] + log.push([id, + labelFor(e.target), + e.relatedTarget ? labelFor(e.relatedTarget) : null, + e.composedPath().map((n) => { + return labelFor(n); + })]); + }); + } + } + callback(target); + return log; +} + +// This function assumes that testharness.js is available. +function assert_event_path_equals(actual, expected) { + assert_equals(actual.length, expected.length); + for (let i = 0; i < actual.length; ++i) { + assert_equals(actual[i].length, expected[i].length); + assert_equals(actual[i][0], expected[i][0], 'currentTarget at ' + i + ' should be same'); + assert_equals(actual[i][1], expected[i][1], 'target at ' + i + ' should be same'); + assert_equals(actual[i][2], expected[i][2], 'relatedTarget at ' + i + ' should be same'); + assert_array_equals(actual[i][3], expected[i][3], 'composedPath at ' + i + ' should be same'); + if (actual[i][4]) { + assert_equals(actual[i][4], expected[i][4], 'listener type should be same at ' + i); + } + } +} diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots-fallback-in-document.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots-fallback-in-document.html new file mode 100644 index 00000000000..5d2e130fb4f --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots-fallback-in-document.html @@ -0,0 +1,55 @@ + +Shadow DOM: Slots and fallback contents in Document tree + + + + + +
+
+ +
This is fallback content
+
+
+ + + +
+
+ +
+
+ + diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots-fallback.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots-fallback.html new file mode 100644 index 00000000000..8c9f9dfe9b2 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots-fallback.html @@ -0,0 +1,253 @@ + +Shadow DOM: Slots and fallback contents + + + + + +
+
+ +
+
+ + + +
+
+ +
+
+ + + +
+
+ +
+
+
+ + + +
+
+ +
+
+
+ + + +
+
+ +
+
+
+ + diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots-outside-shadow-dom.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots-outside-shadow-dom.html new file mode 100644 index 00000000000..677c88b8ebf --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots-outside-shadow-dom.html @@ -0,0 +1,16 @@ + + + + + +
foo
+ diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots.html new file mode 100644 index 00000000000..a98626b6f71 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/slots.html @@ -0,0 +1,526 @@ + +Shadow DOM: Slots and assignments + + + + + +
+
+ +
+
+
+ + + +
+
+ +
+
+
+ + + +
+ +
+ + + +
+ +
+
+ +
+ +
+
+
+
+
+ + + +
+
+ +
+
+
+
+
+ + + +
+
+ +
+
+
+
+
+
+
+ + + +
+
+ +
+
+
+
+
+ + + +
+
+ +
+
+
+
+ + + +
+
+ +
+
+
+ + + +
+
+ +
+
+
+ + + +
+
+ +
+
+
+ + + +
+
+ +
+
+
+ + + +
+
+ +
+
+
+
+
+
+ +