summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/DOM
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-02-07 23:44:01 +0100
committerAndreas Kling <kling@serenityos.org>2021-02-08 17:59:04 +0100
commit79bab28f5ef0665f00ad85ae959276338ad0f40c (patch)
treeb8e83f12884795de985590c456f7c994225afbb5 /Userland/Libraries/LibWeb/DOM
parent2a38f008bf95989e2d70cf7160b92d3fe5e2da71 (diff)
downloadserenity-79bab28f5ef0665f00ad85ae959276338ad0f40c.zip
LibWeb: Implement Element.getElementsBy{Tag,Class}Name()
Just like the Document variants, but using the given Element as for_each_in_subtree_of_type() root.
Diffstat (limited to 'Userland/Libraries/LibWeb/DOM')
-rw-r--r--Userland/Libraries/LibWeb/DOM/Element.cpp28
-rw-r--r--Userland/Libraries/LibWeb/DOM/Element.h3
-rw-r--r--Userland/Libraries/LibWeb/DOM/Element.idl3
3 files changed, 34 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp
index a9896f386c..5935272a6c 100644
--- a/Userland/Libraries/LibWeb/DOM/Element.cpp
+++ b/Userland/Libraries/LibWeb/DOM/Element.cpp
@@ -46,6 +46,7 @@
#include <LibWeb/Layout/TableRowGroupBox.h>
#include <LibWeb/Layout/TreeBuilder.h>
#include <LibWeb/Layout/WidgetBox.h>
+#include <LibWeb/Namespace.h>
namespace Web::DOM {
@@ -344,4 +345,31 @@ bool Element::is_focused() const
return document().focused_element() == this;
}
+NonnullRefPtrVector<Element> Element::get_elements_by_tag_name(const FlyString& tag_name) const
+{
+ // FIXME: Support "*" for tag_name
+ // https://dom.spec.whatwg.org/#concept-getelementsbytagname
+ NonnullRefPtrVector<Element> elements;
+ for_each_in_subtree_of_type<Element>([&](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> Element::get_elements_by_class_name(const FlyString& class_name) const
+{
+ NonnullRefPtrVector<Element> elements;
+ for_each_in_subtree_of_type<Element>([&](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 c442d183ae..1f914cf484 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<Element> get_elements_by_tag_name(const FlyString&) const;
+ NonnullRefPtrVector<Element> get_elements_by_class_name(const FlyString&) const;
+
protected:
RefPtr<Layout::Node> create_layout_node() override;
diff --git a/Userland/Libraries/LibWeb/DOM/Element.idl b/Userland/Libraries/LibWeb/DOM/Element.idl
index e5de36e75b..2067594321 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;