diff options
author | Andreas Kling <kling@serenityos.org> | 2020-12-01 15:47:50 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-12-01 16:53:10 +0100 |
commit | 7c4c706ebe568661d828537bbe5863c9c5f4511f (patch) | |
tree | 927a265c3746814a22add771a6a62dc289464f69 /Libraries/LibWeb | |
parent | 09da5f7263e1b8fd2a141e3e2f31c8aec9d7b295 (diff) | |
download | serenity-7c4c706ebe568661d828537bbe5863c9c5f4511f.zip |
LibWeb: Implement Document.getElementsByClassName()
Note that we're taking a shortcut here and returning the elements as an
Array instead of HTMLCollection. One day we'll have to bite the bullet
and deal with HTMLCollection, but not today.
Diffstat (limited to 'Libraries/LibWeb')
-rw-r--r-- | Libraries/LibWeb/DOM/Document.cpp | 11 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/Document.h | 1 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/Document.idl | 1 |
3 files changed, 13 insertions, 0 deletions
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 8016a40225..c57d7e06c7 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -458,6 +458,17 @@ NonnullRefPtrVector<Element> Document::get_elements_by_tag_name(const FlyString& return elements; } +NonnullRefPtrVector<Element> Document::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)) + elements.append(element); + return IterationDecision::Continue; + }); + return elements; +} + Color Document::link_color() const { if (m_link_color.has_value()) diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h index 77e9b12039..5c4d82b094 100644 --- a/Libraries/LibWeb/DOM/Document.h +++ b/Libraries/LibWeb/DOM/Document.h @@ -132,6 +132,7 @@ public: NonnullRefPtrVector<Element> get_elements_by_name(const String&) const; NonnullRefPtrVector<Element> get_elements_by_tag_name(const FlyString&) const; + NonnullRefPtrVector<Element> get_elements_by_class_name(const FlyString&) const; const String& source() const { return m_source; } void set_source(const String& source) { m_source = source; } diff --git a/Libraries/LibWeb/DOM/Document.idl b/Libraries/LibWeb/DOM/Document.idl index a6610cd8d1..ce50d7d7b2 100644 --- a/Libraries/LibWeb/DOM/Document.idl +++ b/Libraries/LibWeb/DOM/Document.idl @@ -9,6 +9,7 @@ interface Document : Node { Element? getElementById(DOMString id); ArrayFromVector getElementsByTagName(DOMString tagName); + ArrayFromVector getElementsByClassName(DOMString className); readonly attribute Element? firstElementChild; readonly attribute Element? lastElementChild; |