Skip to content

Commit 271b2a0

Browse files
Merge pull request #591 from lightpanda-io/element_matches
add Element.matches web api
2 parents a4f7393 + 8f851be commit 271b2a0

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/browser/dom/element.zig

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,12 @@ pub const Element = struct {
355355
return state.renderer.height();
356356
}
357357

358-
pub fn deinit(_: *parser.Element, _: std.mem.Allocator) void {}
358+
pub fn _matches(self: *parser.Element, selectors: []const u8, state: *SessionState) !bool {
359+
const cssParse = @import("../css/css.zig").parse;
360+
const CssNodeWrap = @import("../css/libdom.zig").Node;
361+
const s = try cssParse(state.call_arena, selectors, .{});
362+
return s.match(CssNodeWrap{ .node = parser.elementToNode(self) });
363+
}
359364
};
360365

361366
// Tests
@@ -518,4 +523,13 @@ test "Browser.DOM.Element" {
518523
.{ "document.getElementById('para').clientWidth", "2" },
519524
.{ "document.getElementById('para').clientHeight", "1" },
520525
}, .{});
526+
527+
try runner.testCases(&.{
528+
.{ "const el = document.createElement('div');", "undefined" },
529+
.{ "el.id = 'matches'; el.className = 'ok';", "ok" },
530+
.{ "el.matches('#matches')", "true" },
531+
.{ "el.matches('.ok')", "true" },
532+
.{ "el.matches('#9000')", "false" },
533+
.{ "el.matches('.notok')", "false" },
534+
}, .{});
521535
}

0 commit comments

Comments
 (0)