mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:27:35 +00:00
LibWeb: Port Selector to new Strings
Also use `Infra::is_ascii_case_insensitive_match()` in some appropriate places, after checking the specs.
This commit is contained in:
parent
c2f0b20d6b
commit
13d2111b74
6 changed files with 105 additions and 105 deletions
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
||||
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -21,21 +21,22 @@
|
|||
#include <LibWeb/HTML/HTMLOptionElement.h>
|
||||
#include <LibWeb/HTML/HTMLSelectElement.h>
|
||||
#include <LibWeb/HTML/HTMLTextAreaElement.h>
|
||||
#include <LibWeb/Infra/Strings.h>
|
||||
|
||||
namespace Web::SelectorEngine {
|
||||
|
||||
// https://drafts.csswg.org/selectors-4/#the-lang-pseudo
|
||||
static inline bool matches_lang_pseudo_class(DOM::Element const& element, Vector<DeprecatedFlyString> const& languages)
|
||||
static inline bool matches_lang_pseudo_class(DOM::Element const& element, Vector<FlyString> const& languages)
|
||||
{
|
||||
DeprecatedFlyString element_language;
|
||||
FlyString element_language;
|
||||
for (auto const* e = &element; e; e = e->parent_element()) {
|
||||
auto lang = e->attribute(HTML::AttributeNames::lang);
|
||||
if (!lang.is_null()) {
|
||||
element_language = lang;
|
||||
element_language = FlyString::from_utf8(lang).release_value_but_fixme_should_propagate_errors();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (element_language.is_null())
|
||||
if (element_language.is_empty())
|
||||
return false;
|
||||
|
||||
// FIXME: This is ad-hoc. Implement a proper language range matching algorithm as recommended by BCP47.
|
||||
|
@ -44,10 +45,10 @@ static inline bool matches_lang_pseudo_class(DOM::Element const& element, Vector
|
|||
return false;
|
||||
if (language == "*"sv)
|
||||
return true;
|
||||
if (!element_language.view().contains('-'))
|
||||
return element_language.equals_ignoring_case(language);
|
||||
auto parts = element_language.view().split_view('-');
|
||||
return parts[0].equals_ignoring_case(language);
|
||||
if (!element_language.to_string().contains('-'))
|
||||
return Infra::is_ascii_case_insensitive_match(element_language, language);
|
||||
auto parts = element_language.to_string().split_limit('-', 2).release_value_but_fixme_should_propagate_errors();
|
||||
return Infra::is_ascii_case_insensitive_match(parts[0], language);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -97,7 +98,7 @@ static inline bool matches_attribute(CSS::Selector::SimpleSelector::Attribute co
|
|||
{
|
||||
if (attribute.match_type == CSS::Selector::SimpleSelector::Attribute::MatchType::HasAttribute) {
|
||||
// Early way out in case of an attribute existence selector.
|
||||
return element.has_attribute(attribute.name);
|
||||
return element.has_attribute(attribute.name.to_string().to_deprecated_string());
|
||||
}
|
||||
|
||||
auto const case_insensitive_match = (attribute.case_type == CSS::Selector::SimpleSelector::Attribute::CaseType::CaseInsensitiveMatch);
|
||||
|
@ -108,19 +109,19 @@ static inline bool matches_attribute(CSS::Selector::SimpleSelector::Attribute co
|
|||
switch (attribute.match_type) {
|
||||
case CSS::Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch:
|
||||
return case_insensitive_match
|
||||
? element.attribute(attribute.name).equals_ignoring_case(attribute.value)
|
||||
: element.attribute(attribute.name) == attribute.value;
|
||||
? Infra::is_ascii_case_insensitive_match(element.attribute(attribute.name.to_string().to_deprecated_string()), attribute.value)
|
||||
: element.attribute(attribute.name.to_string().to_deprecated_string()) == attribute.value.to_deprecated_string();
|
||||
case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsWord: {
|
||||
if (attribute.value.is_empty()) {
|
||||
// This selector is always false is match value is empty.
|
||||
return false;
|
||||
}
|
||||
auto const view = element.attribute(attribute.name).split_view(' ');
|
||||
auto const view = element.attribute(attribute.name.to_string().to_deprecated_string()).split_view(' ');
|
||||
auto const size = view.size();
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
auto const value = view.at(i);
|
||||
if (case_insensitive_match
|
||||
? value.equals_ignoring_case(attribute.value)
|
||||
? Infra::is_ascii_case_insensitive_match(value, attribute.value)
|
||||
: value == attribute.value) {
|
||||
return true;
|
||||
}
|
||||
|
@ -129,9 +130,9 @@ static inline bool matches_attribute(CSS::Selector::SimpleSelector::Attribute co
|
|||
}
|
||||
case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsString:
|
||||
return !attribute.value.is_empty()
|
||||
&& element.attribute(attribute.name).contains(attribute.value, case_sensitivity);
|
||||
&& element.attribute(attribute.name.to_string().to_deprecated_string()).contains(attribute.value, case_sensitivity);
|
||||
case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment: {
|
||||
auto const element_attr_value = element.attribute(attribute.name);
|
||||
auto const element_attr_value = element.attribute(attribute.name.to_string().to_deprecated_string());
|
||||
if (element_attr_value.is_empty()) {
|
||||
// If the attribute value on element is empty, the selector is true
|
||||
// if the match value is also empty and false otherwise.
|
||||
|
@ -142,15 +143,15 @@ static inline bool matches_attribute(CSS::Selector::SimpleSelector::Attribute co
|
|||
}
|
||||
auto segments = element_attr_value.split_view('-');
|
||||
return case_insensitive_match
|
||||
? segments.first().equals_ignoring_case(attribute.value)
|
||||
? Infra::is_ascii_case_insensitive_match(segments.first(), attribute.value)
|
||||
: segments.first() == attribute.value;
|
||||
}
|
||||
case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
|
||||
return !attribute.value.is_empty()
|
||||
&& element.attribute(attribute.name).starts_with(attribute.value, case_sensitivity);
|
||||
&& element.attribute(attribute.name.to_string().to_deprecated_string()).starts_with(attribute.value, case_sensitivity);
|
||||
case CSS::Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
|
||||
return !attribute.value.is_empty()
|
||||
&& element.attribute(attribute.name).ends_with(attribute.value, case_sensitivity);
|
||||
&& element.attribute(attribute.name.to_string().to_deprecated_string()).ends_with(attribute.value, case_sensitivity);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -351,14 +352,14 @@ static inline bool matches(CSS::Selector::SimpleSelector const& component, DOM::
|
|||
case CSS::Selector::SimpleSelector::Type::Universal:
|
||||
return true;
|
||||
case CSS::Selector::SimpleSelector::Type::Id:
|
||||
return component.name() == element.attribute(HTML::AttributeNames::id);
|
||||
return component.name() == element.attribute(HTML::AttributeNames::id).view();
|
||||
case CSS::Selector::SimpleSelector::Type::Class:
|
||||
return element.has_class(component.name());
|
||||
return element.has_class(component.name().bytes_as_string_view());
|
||||
case CSS::Selector::SimpleSelector::Type::TagName:
|
||||
// See https://html.spec.whatwg.org/multipage/semantics-other.html#case-sensitivity-of-selectors
|
||||
if (element.document().document_type() == DOM::Document::Type::HTML)
|
||||
return component.lowercase_name() == element.local_name();
|
||||
return component.name().equals_ignoring_case(element.local_name());
|
||||
return component.lowercase_name() == element.local_name().view();
|
||||
return Infra::is_ascii_case_insensitive_match(component.name(), element.local_name());
|
||||
case CSS::Selector::SimpleSelector::Type::Attribute:
|
||||
return matches_attribute(component.attribute(), element);
|
||||
case CSS::Selector::SimpleSelector::Type::PseudoClass:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue