From 2a38f008bf95989e2d70cf7160b92d3fe5e2da71 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 8 Feb 2021 01:06:20 +0100 Subject: [PATCH] LibWeb: Make getElementsByClassName() case-insensitive in quirks mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Userland/Libraries/LibWeb/DOM/Document.cpp | 2 +- Userland/Libraries/LibWeb/DOM/Element.cpp | 8 ++++++-- Userland/Libraries/LibWeb/DOM/Element.h | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index d636e90d40..cb648297d2 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -505,7 +505,7 @@ NonnullRefPtrVector Document::get_elements_by_class_name(const FlyStrin { NonnullRefPtrVector elements; for_each_in_subtree_of_type([&](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; }); diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 8381a88585..a9896f386c 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -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 Element::create_layout_node() diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index ae5c53f16a..c442d183ae 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -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& class_names() const { return m_classes; } virtual void apply_presentational_hints(CSS::StyleProperties&) const { }