From a9fee9be072165b05e7e92bc53652517c9ae1979 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Wed, 13 Dec 2023 16:16:57 +0100 Subject: [PATCH 1/2] dom: implement NonDocumentTypeChildNode mixin for element --- src/dom/element.zig | 25 +++++++++++++++++ .../nodes/Element-nextElementSibling-svg.svg | 23 +++++++++++++++ .../Element-nextElementSibling-xhtml.xhtml | 23 +++++++++++++++ .../dom/nodes/Element-nextElementSibling.html | 18 ++++++++++++ .../Element-previousElementSibling-svg.svg | 28 +++++++++++++++++++ ...Element-previousElementSibling-xhtml.xhtml | 28 +++++++++++++++++++ .../nodes/Element-previousElementSibling.html | 23 +++++++++++++++ 7 files changed, 168 insertions(+) create mode 100644 tests/wpt/dom/nodes/Element-nextElementSibling-svg.svg create mode 100644 tests/wpt/dom/nodes/Element-nextElementSibling-xhtml.xhtml create mode 100644 tests/wpt/dom/nodes/Element-nextElementSibling.html create mode 100644 tests/wpt/dom/nodes/Element-previousElementSibling-svg.svg create mode 100644 tests/wpt/dom/nodes/Element-previousElementSibling-xhtml.xhtml create mode 100644 tests/wpt/dom/nodes/Element-previousElementSibling.html diff --git a/src/dom/element.zig b/src/dom/element.zig index 1a506681..97b6f129 100644 --- a/src/dom/element.zig +++ b/src/dom/element.zig @@ -177,6 +177,24 @@ pub const Element = struct { return try children.get_length(); } + // NonDocumentTypeChildNode + // https://dom.spec.whatwg.org/#interface-nondocumenttypechildnode + pub fn get_previousElementSibling(self: *parser.Element) !?Union { + const res = try parser.nodePreviousElementSibling(parser.elementToNode(self)); + if (res == null) { + return null; + } + return try HTMLElem.toInterface(HTMLElem.Union, res.?); + } + + pub fn get_nextElementSibling(self: *parser.Element) !?Union { + const res = try parser.nodeNextElementSibling(parser.elementToNode(self)); + if (res == null) { + return null; + } + return try HTMLElem.toInterface(HTMLElem.Union, res.?); + } + pub fn deinit(_: *parser.Element, _: std.mem.Allocator) void {} }; @@ -257,4 +275,11 @@ pub fn testExecFn( .{ .src = "c.childElementCount", .ex = "3" }, }; try checkCases(js_env, &parentNode); + + var elementSibling = [_]Case{ + .{ .src = "let d = document.getElementById('para')", .ex = "undefined" }, + .{ .src = "d.previousElementSibling.nodeName", .ex = "P" }, + .{ .src = "d.nextElementSibling", .ex = "null" }, + }; + try checkCases(js_env, &elementSibling); } diff --git a/tests/wpt/dom/nodes/Element-nextElementSibling-svg.svg b/tests/wpt/dom/nodes/Element-nextElementSibling-svg.svg new file mode 100644 index 00000000..3e17cad2 --- /dev/null +++ b/tests/wpt/dom/nodes/Element-nextElementSibling-svg.svg @@ -0,0 +1,23 @@ + + +nextElementSibling + + + +Test of nextElementSibling +The result of this test is unknown. + + +test(function() { + var parentEl = document.getElementById("parentEl"); + var fec = document.getElementById("first_element_child"); + var nes = fec.nextElementSibling; + assert_true(!!nes) + assert_equals(nes.nodeType, 1) + assert_equals(nes.getAttribute("id"), "last_element_child") +}) + + diff --git a/tests/wpt/dom/nodes/Element-nextElementSibling-xhtml.xhtml b/tests/wpt/dom/nodes/Element-nextElementSibling-xhtml.xhtml new file mode 100644 index 00000000..915209bd --- /dev/null +++ b/tests/wpt/dom/nodes/Element-nextElementSibling-xhtml.xhtml @@ -0,0 +1,23 @@ + + + +nextElementSibling + + + + +

Test of nextElementSibling

+
+

The result of this test is unknown.

+ + + diff --git a/tests/wpt/dom/nodes/Element-nextElementSibling.html b/tests/wpt/dom/nodes/Element-nextElementSibling.html new file mode 100644 index 00000000..985c602f --- /dev/null +++ b/tests/wpt/dom/nodes/Element-nextElementSibling.html @@ -0,0 +1,18 @@ + + +nextElementSibling + + +

Test of nextElementSibling

+
+

The result of this test is unknown.

+ diff --git a/tests/wpt/dom/nodes/Element-previousElementSibling-svg.svg b/tests/wpt/dom/nodes/Element-previousElementSibling-svg.svg new file mode 100644 index 00000000..671d2c87 --- /dev/null +++ b/tests/wpt/dom/nodes/Element-previousElementSibling-svg.svg @@ -0,0 +1,28 @@ + + +previousElementSibling + + + +Test of previousElementSibling +The result of this test is +unknown. + + + +fnord + + +test(function() { + var parentEl = document.getElementById("parentEl"); + var lec = document.getElementById("last_element_child"); + var pes = lec.previousElementSibling; + assert_true(!!pes) + assert_equals(pes.nodeType, 1) + assert_equals(pes.getAttribute("id"), "middle_element_child") +}) + + diff --git a/tests/wpt/dom/nodes/Element-previousElementSibling-xhtml.xhtml b/tests/wpt/dom/nodes/Element-previousElementSibling-xhtml.xhtml new file mode 100644 index 00000000..7fbbc6d3 --- /dev/null +++ b/tests/wpt/dom/nodes/Element-previousElementSibling-xhtml.xhtml @@ -0,0 +1,28 @@ + + + +previousElementSibling + + + + +

Test of previousElementSibling

+
+

The result of this test is +unknown. + + + +

+ + + diff --git a/tests/wpt/dom/nodes/Element-previousElementSibling.html b/tests/wpt/dom/nodes/Element-previousElementSibling.html new file mode 100644 index 00000000..02c7b16d --- /dev/null +++ b/tests/wpt/dom/nodes/Element-previousElementSibling.html @@ -0,0 +1,23 @@ + + +previousElementSibling + + +

Test of previousElementSibling

+
+

The result of this test is +unknown. + + + +

+ From 455136df17c7feeb7e30fd6b356a23b2ea648b3b Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Fri, 15 Dec 2023 14:06:26 +0100 Subject: [PATCH 2/2] code style --- src/dom/element.zig | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/dom/element.zig b/src/dom/element.zig index 97b6f129..8ab9d3c7 100644 --- a/src/dom/element.zig +++ b/src/dom/element.zig @@ -181,17 +181,13 @@ pub const Element = struct { // https://dom.spec.whatwg.org/#interface-nondocumenttypechildnode pub fn get_previousElementSibling(self: *parser.Element) !?Union { const res = try parser.nodePreviousElementSibling(parser.elementToNode(self)); - if (res == null) { - return null; - } + if (res == null) return null; return try HTMLElem.toInterface(HTMLElem.Union, res.?); } pub fn get_nextElementSibling(self: *parser.Element) !?Union { const res = try parser.nodeNextElementSibling(parser.elementToNode(self)); - if (res == null) { - return null; - } + if (res == null) return null; return try HTMLElem.toInterface(HTMLElem.Union, res.?); }