diff options
author | Andreas Kling <kling@serenityos.org> | 2022-09-17 16:12:04 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-09-17 18:53:26 +0200 |
commit | df7e64d1036212cc0653895b22384c9601c98f89 (patch) | |
tree | d489434b1b84a14a4cc5c287ba3dc2eb39217d33 /Userland/Libraries | |
parent | 23714469521be852f6e816091e05a3fd35485d79 (diff) | |
download | serenity-df7e64d1036212cc0653895b22384c9601c98f89.zip |
LibWeb: Handle multiple class names in getElementsByClassName()
The input string is actually a space-separated list of class names,
not a single class name.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.cpp | 14 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Element.cpp | 14 |
2 files changed, 22 insertions, 6 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 0d21541a4d..af4430c3ca 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -933,10 +933,18 @@ JS::NonnullGCPtr<HTMLCollection> Document::get_elements_by_name(String const& na }); } -JS::NonnullGCPtr<HTMLCollection> Document::get_elements_by_class_name(FlyString const& class_name) +JS::NonnullGCPtr<HTMLCollection> Document::get_elements_by_class_name(FlyString const& class_names) { - return HTMLCollection::create(*this, [class_name, quirks_mode = document().in_quirks_mode()](Element const& element) { - return element.has_class(class_name, quirks_mode ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive); + Vector<FlyString> list_of_class_names; + for (auto& name : class_names.view().split_view(' ')) { + list_of_class_names.append(name); + } + return HTMLCollection::create(*this, [list_of_class_names = move(list_of_class_names), quirks_mode = document().in_quirks_mode()](Element const& element) { + for (auto& name : list_of_class_names) { + if (!element.has_class(name, quirks_mode ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive)) + return false; + } + return true; }); } diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index e5963cb29b..37219fb37d 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -507,10 +507,18 @@ bool Element::is_active() const return document().active_element() == this; } -JS::NonnullGCPtr<HTMLCollection> Element::get_elements_by_class_name(FlyString const& class_name) +JS::NonnullGCPtr<HTMLCollection> Element::get_elements_by_class_name(FlyString const& class_names) { - return HTMLCollection::create(*this, [class_name, quirks_mode = document().in_quirks_mode()](Element const& element) { - return element.has_class(class_name, quirks_mode ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive); + Vector<FlyString> list_of_class_names; + for (auto& name : class_names.view().split_view(' ')) { + list_of_class_names.append(name); + } + return HTMLCollection::create(*this, [list_of_class_names = move(list_of_class_names), quirks_mode = document().in_quirks_mode()](Element const& element) { + for (auto& name : list_of_class_names) { + if (!element.has_class(name, quirks_mode ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive)) + return false; + } + return true; }); } |