Skip to content

Commit

Permalink
LibWeb: Ensure ParentNode.getElementsByClassName() matches all classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tcl3 committed Aug 3, 2024
1 parent 82ed253 commit c2429ee
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
document.getElementsByClassName("te st").length: 3
<DIV id="1" >
<DIV id="2" >
<DIV id="3" >
20 changes: 20 additions & 0 deletions Tests/LibWeb/Text/input/DOM/getElementsByClassName-multiple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<div id="1" class="te st"></div>
<div id="2" class="st te"></div>
<div id="3" class="te te st"></div>
<div id="4" class="te te"></div>
<div id="5" class="st st"></div>
<div id="6" class="test"></div>
<div id="6" class=" test"></div>
<div id="7" class="test "></div>
<div id="8" class="te st"></div>
<script>
test(() => {
let elements = document.getElementsByClassName("te st");
println(`document.getElementsByClassName("te st").length: ${elements.length}`);
for (let element of elements) {
printElement(element);
}
});
</script>
7 changes: 4 additions & 3 deletions Userland/Libraries/LibWeb/DOM/ParentNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ WebIDL::ExceptionOr<void> ParentNode::replace_children(Vector<Variant<JS::Handle
return {};
}

// https://dom.spec.whatwg.org/#dom-document-getelementsbyclassname
JS::NonnullGCPtr<HTMLCollection> ParentNode::get_elements_by_class_name(StringView class_names)
{
Vector<FlyString> list_of_class_names;
Expand All @@ -235,10 +236,10 @@ JS::NonnullGCPtr<HTMLCollection> ParentNode::get_elements_by_class_name(StringVi
}
return HTMLCollection::create(*this, HTMLCollection::Scope::Descendants, [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 true;
if (!element.has_class(name, quirks_mode ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive))
return false;
}
return false;
return !list_of_class_names.is_empty();
});
}

Expand Down

0 comments on commit c2429ee

Please sign in to comment.