LibWeb: Ensure document.getElementsByClassName("") returns no elements

Previously, `document.getElementsByClassName("")` would return a
collection containing all elements in the given document.
This commit is contained in:
Tim Ledbetter 2024-07-22 21:17:17 +01:00 committed by Andreas Kling
parent faf64bfb41
commit 0fceede029
Notes: github-actions[bot] 2024-07-23 06:59:20 +00:00
4 changed files with 23 additions and 10 deletions

View File

@ -0,0 +1 @@
document.getElementsByClassName("").length: 0

View File

@ -0,0 +1,8 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<div class=""></div>
<script>
test(() => {
println(`document.getElementsByClassName("").length: ${document.getElementsByClassName("").length}`);
});
</script>

View File

@ -471,14 +471,18 @@ void Element::attribute_changed(FlyString const& name, Optional<String> const&,
document().element_name_changed({}, *this);
} else if (name == HTML::AttributeNames::class_) {
auto new_classes = value_or_empty.bytes_as_string_view().split_view_if(Infra::is_ascii_whitespace);
m_classes.clear();
m_classes.ensure_capacity(new_classes.size());
for (auto& new_class : new_classes) {
m_classes.unchecked_append(FlyString::from_utf8(new_class).release_value_but_fixme_should_propagate_errors());
if (value_or_empty.is_empty()) {
m_classes.clear();
} else {
auto new_classes = value_or_empty.bytes_as_string_view().split_view_if(Infra::is_ascii_whitespace);
m_classes.clear();
m_classes.ensure_capacity(new_classes.size());
for (auto& new_class : new_classes) {
m_classes.unchecked_append(FlyString::from_utf8(new_class).release_value_but_fixme_should_propagate_errors());
}
if (m_class_list)
m_class_list->associated_attribute_changed(value_or_empty);
}
if (m_class_list)
m_class_list->associated_attribute_changed(value_or_empty);
} else if (name == HTML::AttributeNames::style) {
if (!value.has_value()) {
if (m_inline_style) {

View File

@ -234,10 +234,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 false;
if (element.has_class(name, quirks_mode ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive))
return true;
}
return true;
return false;
});
}