diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index ded522b47a..2fed59cbe9 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -247,6 +248,21 @@ NonnullRefPtr Element::computed_style() return properties; } +// https://dom.spec.whatwg.org/#dom-element-matches +DOM::ExceptionOr Element::matches(StringView selectors) const +{ + auto maybe_selectors = parse_selector(CSS::ParsingContext(static_cast(const_cast(*this))), selectors); + if (!maybe_selectors.has_value()) + return DOM::SyntaxError::create("Failed to parse selector"); + + auto sel = maybe_selectors.value(); + for (auto& s : sel) { + if (SelectorEngine::matches(s, *this)) + return true; + } + return false; +} + ExceptionOr Element::set_inner_html(String const& markup) { auto result = DOMParsing::InnerHTML::inner_html_setter(*this, markup); diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index ca45079646..43931a9028 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -56,6 +56,8 @@ public: void remove_attribute(const FlyString& name); size_t attribute_list_size() const { return m_attributes.size(); } + DOM::ExceptionOr matches(StringView selectors) const; + template void for_each_attribute(Callback callback) const { diff --git a/Userland/Libraries/LibWeb/DOM/Element.idl b/Userland/Libraries/LibWeb/DOM/Element.idl index 8b99a0cc72..64472afe9f 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.idl +++ b/Userland/Libraries/LibWeb/DOM/Element.idl @@ -20,6 +20,8 @@ interface Element : Node { [Reflect] attribute DOMString id; [Reflect=class] attribute DOMString className; + boolean matches(DOMString selectors); + readonly attribute Element? nextElementSibling; readonly attribute Element? previousElementSibling;