LibWeb: Make getElementsByClassName() case-insensitive in quirks mode

From https://dom.spec.whatwg.org/#concept-getelementsbyclassname:

    The comparisons for the classes must be done in an ASCII case-
    insensitive manner if root’s node document’s mode is "quirks", and
    in an identical to manner otherwise.
This commit is contained in:
Linus Groh 2021-02-08 01:06:20 +01:00 committed by Andreas Kling
parent 5a9094a70a
commit 2a38f008bf
Notes: sideshowbarker 2024-07-18 22:31:37 +09:00
3 changed files with 8 additions and 4 deletions

View file

@ -505,7 +505,7 @@ NonnullRefPtrVector<Element> Document::get_elements_by_class_name(const FlyStrin
{
NonnullRefPtrVector<Element> elements;
for_each_in_subtree_of_type<Element>([&](auto& element) {
if (element.has_class(class_name))
if (element.has_class(class_name, in_quirks_mode() ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive))
elements.append(element);
return IterationDecision::Continue;
});

View file

@ -103,9 +103,13 @@ void Element::remove_attribute(const FlyString& name)
m_attributes.remove_first_matching([&](auto& attribute) { return attribute.name() == name; });
}
bool Element::has_class(const FlyString& class_name) const
bool Element::has_class(const FlyString& class_name, CaseSensitivity case_sensitivity) const
{
return any_of(m_classes.begin(), m_classes.end(), [&](auto& it) { return it == class_name; });
return any_of(m_classes.begin(), m_classes.end(), [&](auto& it) {
return case_sensitivity == CaseSensitivity::CaseSensitive
? it == class_name
: it.to_lowercase() == class_name.to_lowercase();
});
}
RefPtr<Layout::Node> Element::create_layout_node()

View file

@ -73,7 +73,7 @@ public:
callback(attribute.name(), attribute.value());
}
bool has_class(const FlyString&) const;
bool has_class(const FlyString&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
const Vector<FlyString>& class_names() const { return m_classes; }
virtual void apply_presentational_hints(CSS::StyleProperties&) const { }