diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index a9896f386c7..5935272a6c8 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -46,6 +46,7 @@ #include #include #include +#include namespace Web::DOM { @@ -344,4 +345,31 @@ bool Element::is_focused() const return document().focused_element() == this; } +NonnullRefPtrVector Element::get_elements_by_tag_name(const FlyString& tag_name) const +{ + // FIXME: Support "*" for tag_name + // https://dom.spec.whatwg.org/#concept-getelementsbytagname + NonnullRefPtrVector elements; + for_each_in_subtree_of_type([&](auto& element) { + if (element.namespace_() == Namespace::HTML + ? element.local_name().to_lowercase() == tag_name.to_lowercase() + : element.local_name() == tag_name) { + elements.append(element); + } + return IterationDecision::Continue; + }); + return elements; +} + +NonnullRefPtrVector Element::get_elements_by_class_name(const FlyString& class_name) const +{ + NonnullRefPtrVector elements; + for_each_in_subtree_of_type([&](auto& element) { + if (element.has_class(class_name, m_document->in_quirks_mode() ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive)) + elements.append(element); + return IterationDecision::Continue; + }); + return elements; +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index c442d183ae3..1f914cf484e 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -98,6 +98,9 @@ public: bool is_focused() const; virtual bool is_focusable() const { return false; } + NonnullRefPtrVector get_elements_by_tag_name(const FlyString&) const; + NonnullRefPtrVector get_elements_by_class_name(const FlyString&) const; + protected: RefPtr create_layout_node() override; diff --git a/Userland/Libraries/LibWeb/DOM/Element.idl b/Userland/Libraries/LibWeb/DOM/Element.idl index e5de36e75b6..20675943210 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.idl +++ b/Userland/Libraries/LibWeb/DOM/Element.idl @@ -8,6 +8,9 @@ interface Element : Node { boolean hasAttribute(DOMString qualifiedName); boolean hasAttributes(); + ArrayFromVector getElementsByTagName(DOMString tagName); + ArrayFromVector getElementsByClassName(DOMString className); + readonly attribute Element? firstElementChild; readonly attribute Element? lastElementChild;