1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-11 02:13:56 +09: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:
Sam Atkins 2023-02-18 15:42:55 +00:00 committed by Linus Groh
parent c2f0b20d6b
commit 13d2111b74
Notes: sideshowbarker 2024-07-17 07:16:27 +09:00
6 changed files with 105 additions and 105 deletions

View file

@ -51,7 +51,7 @@ DeprecatedString CSSStyleRule::serialized() const
// 1. Let s initially be the result of performing serialize a group of selectors on the rules associated selectors, // 1. Let s initially be the result of performing serialize a group of selectors on the rules associated selectors,
// followed by the string " {", i.e., a single SPACE (U+0020), followed by LEFT CURLY BRACKET (U+007B). // followed by the string " {", i.e., a single SPACE (U+0020), followed by LEFT CURLY BRACKET (U+007B).
builder.append(serialize_a_group_of_selectors(selectors())); builder.append(serialize_a_group_of_selectors(selectors()).release_value_but_fixme_should_propagate_errors());
builder.append(" {"sv); builder.append(" {"sv);
// 2. Let decls be the result of performing serialize a CSS declaration block on the rules associated declarations, or null if there are no such declarations. // 2. Let decls be the result of performing serialize a CSS declaration block on the rules associated declarations, or null if there are no such declarations.
@ -92,7 +92,7 @@ DeprecatedString CSSStyleRule::serialized() const
DeprecatedString CSSStyleRule::selector_text() const DeprecatedString CSSStyleRule::selector_text() const
{ {
// The selectorText attribute, on getting, must return the result of serializing the associated group of selectors. // The selectorText attribute, on getting, must return the result of serializing the associated group of selectors.
return serialize_a_group_of_selectors(selectors()); return serialize_a_group_of_selectors(selectors()).release_value_but_fixme_should_propagate_errors().to_deprecated_string();
} }
// https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext // https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext

View file

@ -280,7 +280,7 @@ Parser::ParseErrorOr<Selector::SimpleSelector> Parser::parse_attribute_simple_se
// they are converted to lowercase, so we do that here too. If we want to be // they are converted to lowercase, so we do that here too. If we want to be
// correct with XML later, we'll need to keep the original case and then do // correct with XML later, we'll need to keep the original case and then do
// a case-insensitive compare later. // a case-insensitive compare later.
.name = attribute_part.token().ident().to_lowercase_string(), .name = FlyString::from_utf8(attribute_part.token().ident().to_lowercase_string()).release_value_but_fixme_should_propagate_errors(),
.case_type = Selector::SimpleSelector::Attribute::CaseType::DefaultMatch, .case_type = Selector::SimpleSelector::Attribute::CaseType::DefaultMatch,
} }
}; };
@ -340,7 +340,8 @@ Parser::ParseErrorOr<Selector::SimpleSelector> Parser::parse_attribute_simple_se
dbgln_if(CSS_PARSER_DEBUG, "Expected a string or ident for the value to match attribute against, got: '{}'", value_part.to_debug_string()); dbgln_if(CSS_PARSER_DEBUG, "Expected a string or ident for the value to match attribute against, got: '{}'", value_part.to_debug_string());
return ParseError::SyntaxError; return ParseError::SyntaxError;
} }
simple_selector.attribute().value = value_part.token().is(Token::Type::Ident) ? value_part.token().ident() : value_part.token().string(); auto value_string_view = value_part.token().is(Token::Type::Ident) ? value_part.token().ident() : value_part.token().string();
simple_selector.attribute().value = String::from_utf8(value_string_view).release_value_but_fixme_should_propagate_errors();
attribute_tokens.skip_whitespace(); attribute_tokens.skip_whitespace();
// Handle case-sensitivity suffixes. https://www.w3.org/TR/selectors-4/#attribute-case // Handle case-sensitivity suffixes. https://www.w3.org/TR/selectors-4/#attribute-case
@ -563,8 +564,8 @@ Parser::ParseErrorOr<Selector::SimpleSelector> Parser::parse_pseudo_simple_selec
return ParseError::SyntaxError; return ParseError::SyntaxError;
} }
// FIXME: Support multiple, comma-separated, language ranges. // FIXME: Support multiple, comma-separated, language ranges.
Vector<DeprecatedFlyString> languages; Vector<FlyString> languages;
languages.append(pseudo_function.values().first().token().to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string()); languages.append(pseudo_function.values().first().token().to_string().release_value_but_fixme_should_propagate_errors());
return Selector::SimpleSelector { return Selector::SimpleSelector {
.type = Selector::SimpleSelector::Type::PseudoClass, .type = Selector::SimpleSelector::Type::PseudoClass,
.value = Selector::SimpleSelector::PseudoClass { .value = Selector::SimpleSelector::PseudoClass {
@ -618,7 +619,7 @@ Parser::ParseErrorOr<Optional<Selector::SimpleSelector>> Parser::parse_simple_se
} }
return Selector::SimpleSelector { return Selector::SimpleSelector {
.type = Selector::SimpleSelector::Type::Class, .type = Selector::SimpleSelector::Type::Class,
.value = Selector::SimpleSelector::Name { class_name_value.token().ident() } .value = Selector::SimpleSelector::Name { FlyString::from_utf8(class_name_value.token().ident()).release_value_but_fixme_should_propagate_errors() }
}; };
} }
case '>': case '>':
@ -642,13 +643,13 @@ Parser::ParseErrorOr<Optional<Selector::SimpleSelector>> Parser::parse_simple_se
} }
return Selector::SimpleSelector { return Selector::SimpleSelector {
.type = Selector::SimpleSelector::Type::Id, .type = Selector::SimpleSelector::Type::Id,
.value = Selector::SimpleSelector::Name { first_value.token().hash_value() } .value = Selector::SimpleSelector::Name { FlyString::from_utf8(first_value.token().hash_value()).release_value_but_fixme_should_propagate_errors() }
}; };
} }
if (first_value.is(Token::Type::Ident)) { if (first_value.is(Token::Type::Ident)) {
return Selector::SimpleSelector { return Selector::SimpleSelector {
.type = Selector::SimpleSelector::Type::TagName, .type = Selector::SimpleSelector::Type::TagName,
.value = Selector::SimpleSelector::Name { first_value.token().ident() } .value = Selector::SimpleSelector::Name { FlyString::from_utf8(first_value.token().ident()).release_value_but_fixme_should_propagate_errors() }
}; };
} }
if (first_value.is_block() && first_value.block().is_square()) if (first_value.is_block() && first_value.block().is_square())

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -119,7 +119,7 @@ u32 Selector::specificity() const
} }
// https://www.w3.org/TR/cssom/#serialize-a-simple-selector // https://www.w3.org/TR/cssom/#serialize-a-simple-selector
DeprecatedString Selector::SimpleSelector::serialize() const ErrorOr<String> Selector::SimpleSelector::serialize() const
{ {
StringBuilder s; StringBuilder s;
switch (type) { switch (type) {
@ -129,50 +129,50 @@ DeprecatedString Selector::SimpleSelector::serialize() const
// FIXME: 2. If the namespace prefix maps to a namespace that is the null namespace (not in a namespace) append "|" (U+007C) to s. // FIXME: 2. If the namespace prefix maps to a namespace that is the null namespace (not in a namespace) append "|" (U+007C) to s.
// 3. If this is a type selector append the serialization of the element name as an identifier to s. // 3. If this is a type selector append the serialization of the element name as an identifier to s.
if (type == Selector::SimpleSelector::Type::TagName) { if (type == Selector::SimpleSelector::Type::TagName) {
serialize_an_identifier(s, name()).release_value_but_fixme_should_propagate_errors(); TRY(serialize_an_identifier(s, name()));
} }
// 4. If this is a universal selector append "*" (U+002A) to s. // 4. If this is a universal selector append "*" (U+002A) to s.
if (type == Selector::SimpleSelector::Type::Universal) if (type == Selector::SimpleSelector::Type::Universal)
s.append('*'); TRY(s.try_append('*'));
break; break;
case Selector::SimpleSelector::Type::Attribute: { case Selector::SimpleSelector::Type::Attribute: {
auto& attribute = this->attribute(); auto& attribute = this->attribute();
// 1. Append "[" (U+005B) to s. // 1. Append "[" (U+005B) to s.
s.append('['); TRY(s.try_append('['));
// FIXME: 2. If the namespace prefix maps to a namespace that is not the null namespace (not in a namespace) append the serialization of the namespace prefix as an identifier, followed by a "|" (U+007C) to s. // FIXME: 2. If the namespace prefix maps to a namespace that is not the null namespace (not in a namespace) append the serialization of the namespace prefix as an identifier, followed by a "|" (U+007C) to s.
// 3. Append the serialization of the attribute name as an identifier to s. // 3. Append the serialization of the attribute name as an identifier to s.
serialize_an_identifier(s, attribute.name).release_value_but_fixme_should_propagate_errors(); TRY(serialize_an_identifier(s, attribute.name));
// 4. If there is an attribute value specified, append "=", "~=", "|=", "^=", "$=", or "*=" as appropriate (depending on the type of attribute selector), // 4. If there is an attribute value specified, append "=", "~=", "|=", "^=", "$=", or "*=" as appropriate (depending on the type of attribute selector),
// followed by the serialization of the attribute value as a string, to s. // followed by the serialization of the attribute value as a string, to s.
if (!attribute.value.is_null()) { if (!attribute.value.is_empty()) {
switch (attribute.match_type) { switch (attribute.match_type) {
case Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch: case Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch:
s.append("="sv); TRY(s.try_append("="sv));
break; break;
case Selector::SimpleSelector::Attribute::MatchType::ContainsWord: case Selector::SimpleSelector::Attribute::MatchType::ContainsWord:
s.append("~="sv); TRY(s.try_append("~="sv));
break; break;
case Selector::SimpleSelector::Attribute::MatchType::ContainsString: case Selector::SimpleSelector::Attribute::MatchType::ContainsString:
s.append("*="sv); TRY(s.try_append("*="sv));
break; break;
case Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment: case Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment:
s.append("|="sv); TRY(s.try_append("|="sv));
break; break;
case Selector::SimpleSelector::Attribute::MatchType::StartsWithString: case Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
s.append("^="sv); TRY(s.try_append("^="sv));
break; break;
case Selector::SimpleSelector::Attribute::MatchType::EndsWithString: case Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
s.append("$="sv); TRY(s.try_append("$="sv));
break; break;
default: default:
break; break;
} }
serialize_a_string(s, attribute.value).release_value_but_fixme_should_propagate_errors(); TRY(serialize_a_string(s, attribute.value));
} }
// 5. If the attribute selector has the case-insensitivity flag present, append " i" (U+0020 U+0069) to s. // 5. If the attribute selector has the case-insensitivity flag present, append " i" (U+0020 U+0069) to s.
@ -180,30 +180,30 @@ DeprecatedString Selector::SimpleSelector::serialize() const
// (the line just above is an addition to CSS OM to match Selectors Level 4 last draft) // (the line just above is an addition to CSS OM to match Selectors Level 4 last draft)
switch (attribute.case_type) { switch (attribute.case_type) {
case Selector::SimpleSelector::Attribute::CaseType::CaseInsensitiveMatch: case Selector::SimpleSelector::Attribute::CaseType::CaseInsensitiveMatch:
s.append(" i"sv); TRY(s.try_append(" i"sv));
break; break;
case Selector::SimpleSelector::Attribute::CaseType::CaseSensitiveMatch: case Selector::SimpleSelector::Attribute::CaseType::CaseSensitiveMatch:
s.append(" s"sv); TRY(s.try_append(" s"sv));
break; break;
default: default:
break; break;
} }
// 6. Append "]" (U+005D) to s. // 6. Append "]" (U+005D) to s.
s.append(']'); TRY(s.try_append(']'));
break; break;
} }
case Selector::SimpleSelector::Type::Class: case Selector::SimpleSelector::Type::Class:
// Append a "." (U+002E), followed by the serialization of the class name as an identifier to s. // Append a "." (U+002E), followed by the serialization of the class name as an identifier to s.
s.append('.'); TRY(s.try_append('.'));
serialize_an_identifier(s, name()).release_value_but_fixme_should_propagate_errors(); TRY(serialize_an_identifier(s, name()));
break; break;
case Selector::SimpleSelector::Type::Id: case Selector::SimpleSelector::Type::Id:
// Append a "#" (U+0023), followed by the serialization of the ID as an identifier to s. // Append a "#" (U+0023), followed by the serialization of the ID as an identifier to s.
s.append('#'); TRY(s.try_append('#'));
serialize_an_identifier(s, name()).release_value_but_fixme_should_propagate_errors(); TRY(serialize_an_identifier(s, name()));
break; break;
case Selector::SimpleSelector::Type::PseudoClass: { case Selector::SimpleSelector::Type::PseudoClass: {
@ -228,8 +228,8 @@ DeprecatedString Selector::SimpleSelector::serialize() const
case Selector::SimpleSelector::PseudoClass::Type::Checked: case Selector::SimpleSelector::PseudoClass::Type::Checked:
case Selector::SimpleSelector::PseudoClass::Type::Active: case Selector::SimpleSelector::PseudoClass::Type::Active:
// If the pseudo-class does not accept arguments append ":" (U+003A), followed by the name of the pseudo-class, to s. // If the pseudo-class does not accept arguments append ":" (U+003A), followed by the name of the pseudo-class, to s.
s.append(':'); TRY(s.try_append(':'));
s.append(pseudo_class_name(pseudo_class.type)); TRY(s.try_append(pseudo_class_name(pseudo_class.type)));
break; break;
case Selector::SimpleSelector::PseudoClass::Type::NthChild: case Selector::SimpleSelector::PseudoClass::Type::NthChild:
case Selector::SimpleSelector::PseudoClass::Type::NthLastChild: case Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
@ -241,26 +241,26 @@ DeprecatedString Selector::SimpleSelector::serialize() const
case Selector::SimpleSelector::PseudoClass::Type::Lang: case Selector::SimpleSelector::PseudoClass::Type::Lang:
// Otherwise, append ":" (U+003A), followed by the name of the pseudo-class, followed by "(" (U+0028), // Otherwise, append ":" (U+003A), followed by the name of the pseudo-class, followed by "(" (U+0028),
// followed by the value of the pseudo-class argument(s) determined as per below, followed by ")" (U+0029), to s. // followed by the value of the pseudo-class argument(s) determined as per below, followed by ")" (U+0029), to s.
s.append(':'); TRY(s.try_append(':'));
s.append(pseudo_class_name(pseudo_class.type)); TRY(s.try_append(pseudo_class_name(pseudo_class.type)));
s.append('('); TRY(s.try_append('('));
if (pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthChild if (pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthChild
|| pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthLastChild || pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthLastChild
|| pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthOfType || pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthOfType
|| pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthLastOfType) { || pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthLastOfType) {
// The result of serializing the value using the rules to serialize an <an+b> value. // The result of serializing the value using the rules to serialize an <an+b> value.
s.append(pseudo_class.nth_child_pattern.serialize()); TRY(s.try_append(TRY(pseudo_class.nth_child_pattern.serialize())));
} else if (pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Not } else if (pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Not
|| pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Is || pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Is
|| pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Where) { || pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Where) {
// The result of serializing the value using the rules for serializing a group of selectors. // The result of serializing the value using the rules for serializing a group of selectors.
// NOTE: `:is()` and `:where()` aren't in the spec for this yet, but it should be! // NOTE: `:is()` and `:where()` aren't in the spec for this yet, but it should be!
s.append(serialize_a_group_of_selectors(pseudo_class.argument_selector_list)); TRY(s.try_append(TRY(serialize_a_group_of_selectors(pseudo_class.argument_selector_list))));
} else if (pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Lang) { } else if (pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Lang) {
// The serialization of a comma-separated list of each arguments serialization as a string, preserving relative order. // The serialization of a comma-separated list of each arguments serialization as a string, preserving relative order.
s.join(", "sv, pseudo_class.languages); s.join(", "sv, pseudo_class.languages);
} }
s.append(')'); TRY(s.try_append(')'));
break; break;
default: default:
dbgln("FIXME: Unknown pseudo class type for serialization: {}", to_underlying(pseudo_class.type)); dbgln("FIXME: Unknown pseudo class type for serialization: {}", to_underlying(pseudo_class.type));
@ -275,11 +275,11 @@ DeprecatedString Selector::SimpleSelector::serialize() const
dbgln("FIXME: Unsupported simple selector serialization for type {}", to_underlying(type)); dbgln("FIXME: Unsupported simple selector serialization for type {}", to_underlying(type));
break; break;
} }
return s.to_deprecated_string(); return s.to_string();
} }
// https://www.w3.org/TR/cssom/#serialize-a-selector // https://www.w3.org/TR/cssom/#serialize-a-selector
DeprecatedString Selector::serialize() const ErrorOr<String> Selector::serialize() const
{ {
StringBuilder s; StringBuilder s;
@ -289,14 +289,14 @@ DeprecatedString Selector::serialize() const
// 1. If there is only one simple selector in the compound selectors which is a universal selector, append the result of serializing the universal selector to s. // 1. If there is only one simple selector in the compound selectors which is a universal selector, append the result of serializing the universal selector to s.
if (compound_selector.simple_selectors.size() == 1 if (compound_selector.simple_selectors.size() == 1
&& compound_selector.simple_selectors.first().type == Selector::SimpleSelector::Type::Universal) { && compound_selector.simple_selectors.first().type == Selector::SimpleSelector::Type::Universal) {
s.append(compound_selector.simple_selectors.first().serialize()); TRY(s.try_append(TRY(compound_selector.simple_selectors.first().serialize())));
} }
// 2. Otherwise, for each simple selector in the compound selectors... // 2. Otherwise, for each simple selector in the compound selectors...
// FIXME: ...that is not a universal selector of which the namespace prefix maps to a namespace that is not the default namespace... // FIXME: ...that is not a universal selector of which the namespace prefix maps to a namespace that is not the default namespace...
// ...serialize the simple selector and append the result to s. // ...serialize the simple selector and append the result to s.
else { else {
for (auto& simple_selector : compound_selector.simple_selectors) { for (auto& simple_selector : compound_selector.simple_selectors) {
s.append(simple_selector.serialize()); TRY(s.try_append(TRY(simple_selector.serialize())));
} }
} }
@ -304,21 +304,21 @@ DeprecatedString Selector::serialize() const
// followed by the combinator ">", "+", "~", ">>", "||", as appropriate, followed by another // followed by the combinator ">", "+", "~", ">>", "||", as appropriate, followed by another
// single SPACE (U+0020) if the combinator was not whitespace, to s. // single SPACE (U+0020) if the combinator was not whitespace, to s.
if (i != compound_selectors().size() - 1) { if (i != compound_selectors().size() - 1) {
s.append(' '); TRY(s.try_append(' '));
// Note: The combinator that appears between parts `i` and `i+1` appears with the `i+1` selector, // Note: The combinator that appears between parts `i` and `i+1` appears with the `i+1` selector,
// so we have to check that one. // so we have to check that one.
switch (compound_selectors()[i + 1].combinator) { switch (compound_selectors()[i + 1].combinator) {
case Selector::Combinator::ImmediateChild: case Selector::Combinator::ImmediateChild:
s.append("> "sv); TRY(s.try_append("> "sv));
break; break;
case Selector::Combinator::NextSibling: case Selector::Combinator::NextSibling:
s.append("+ "sv); TRY(s.try_append("+ "sv));
break; break;
case Selector::Combinator::SubsequentSibling: case Selector::Combinator::SubsequentSibling:
s.append("~ "sv); TRY(s.try_append("~ "sv));
break; break;
case Selector::Combinator::Column: case Selector::Combinator::Column:
s.append("|| "sv); TRY(s.try_append("|| "sv));
break; break;
default: default:
break; break;
@ -327,22 +327,20 @@ DeprecatedString Selector::serialize() const
// 4. If this is the last part of the chain of the selector and there is a pseudo-element, // 4. If this is the last part of the chain of the selector and there is a pseudo-element,
// append "::" followed by the name of the pseudo-element, to s. // append "::" followed by the name of the pseudo-element, to s.
if (compound_selector.simple_selectors.last().type == Selector::SimpleSelector::Type::PseudoElement) { if (compound_selector.simple_selectors.last().type == Selector::SimpleSelector::Type::PseudoElement) {
s.append("::"sv); TRY(s.try_append("::"sv));
s.append(pseudo_element_name(compound_selector.simple_selectors.last().pseudo_element())); TRY(s.try_append(pseudo_element_name(compound_selector.simple_selectors.last().pseudo_element())));
} }
} }
} }
return s.to_deprecated_string(); return s.to_string();
} }
// https://www.w3.org/TR/cssom/#serialize-a-group-of-selectors // https://www.w3.org/TR/cssom/#serialize-a-group-of-selectors
DeprecatedString serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const& selectors) ErrorOr<String> serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const& selectors)
{ {
// To serialize a group of selectors serialize each selector in the group of selectors and then serialize a comma-separated list of these serializations. // To serialize a group of selectors serialize each selector in the group of selectors and then serialize a comma-separated list of these serializations.
StringBuilder builder; return String::join(", "sv, selectors);
builder.join(", "sv, selectors);
return builder.to_deprecated_string();
} }
Optional<Selector::PseudoElement> pseudo_element_from_string(StringView name) Optional<Selector::PseudoElement> pseudo_element_from_string(StringView name)

View file

@ -7,10 +7,10 @@
#pragma once #pragma once
#include <AK/DeprecatedFlyString.h> #include <AK/FlyString.h>
#include <AK/DeprecatedString.h>
#include <AK/NonnullRefPtrVector.h> #include <AK/NonnullRefPtrVector.h>
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <AK/String.h>
#include <AK/Vector.h> #include <AK/Vector.h>
namespace Web::CSS { namespace Web::CSS {
@ -50,11 +50,11 @@ public:
int offset = { 0 }; // "B" int offset = { 0 }; // "B"
// https://www.w3.org/TR/css-syntax-3/#serializing-anb // https://www.w3.org/TR/css-syntax-3/#serializing-anb
DeprecatedString serialize() const ErrorOr<String> serialize() const
{ {
// 1. If A is zero, return the serialization of B. // 1. If A is zero, return the serialization of B.
if (step_size == 0) { if (step_size == 0) {
return DeprecatedString::formatted("{}", offset); return String::formatted("{}", offset);
} }
// 2. Otherwise, let result initially be an empty string. // 2. Otherwise, let result initially be an empty string.
@ -63,24 +63,24 @@ public:
// 3. // 3.
// - A is 1: Append "n" to result. // - A is 1: Append "n" to result.
if (step_size == 1) if (step_size == 1)
result.append('n'); TRY(result.try_append('n'));
// - A is -1: Append "-n" to result. // - A is -1: Append "-n" to result.
else if (step_size == -1) else if (step_size == -1)
result.append("-n"sv); TRY(result.try_append("-n"sv));
// - A is non-zero: Serialize A and append it to result, then append "n" to result. // - A is non-zero: Serialize A and append it to result, then append "n" to result.
else if (step_size != 0) else if (step_size != 0)
result.appendff("{}n", step_size); TRY(result.try_appendff("{}n", step_size));
// 4. // 4.
// - B is greater than zero: Append "+" to result, then append the serialization of B to result. // - B is greater than zero: Append "+" to result, then append the serialization of B to result.
if (offset > 0) if (offset > 0)
result.appendff("+{}", offset); TRY(result.try_appendff("+{}", offset));
// - B is less than zero: Append the serialization of B to result. // - B is less than zero: Append the serialization of B to result.
if (offset < 0) if (offset < 0)
result.appendff("{}", offset); TRY(result.try_appendff("{}", offset));
// 5. Return result. // 5. Return result.
return result.to_deprecated_string(); return result.to_string();
} }
}; };
@ -121,7 +121,7 @@ public:
SelectorList argument_selector_list {}; SelectorList argument_selector_list {};
// Used for :lang(en-gb,dk) // Used for :lang(en-gb,dk)
Vector<DeprecatedFlyString> languages {}; Vector<FlyString> languages {};
}; };
struct Attribute { struct Attribute {
@ -140,20 +140,20 @@ public:
CaseInsensitiveMatch, CaseInsensitiveMatch,
}; };
MatchType match_type; MatchType match_type;
DeprecatedFlyString name {}; FlyString name {};
DeprecatedString value {}; String value {};
CaseType case_type; CaseType case_type;
}; };
struct Name { struct Name {
Name(DeprecatedFlyString n) Name(FlyString n)
: name(move(n)) : name(move(n))
, lowercase_name(name.to_lowercase()) , lowercase_name(name.to_string().to_lowercase().release_value_but_fixme_should_propagate_errors())
{ {
} }
DeprecatedFlyString name; FlyString name;
DeprecatedFlyString lowercase_name; FlyString lowercase_name;
}; };
Type type; Type type;
@ -166,12 +166,12 @@ public:
PseudoElement const& pseudo_element() const { return value.get<PseudoElement>(); } PseudoElement const& pseudo_element() const { return value.get<PseudoElement>(); }
PseudoElement& pseudo_element() { return value.get<PseudoElement>(); } PseudoElement& pseudo_element() { return value.get<PseudoElement>(); }
DeprecatedFlyString const& name() const { return value.get<Name>().name; } FlyString const& name() const { return value.get<Name>().name; }
DeprecatedFlyString& name() { return value.get<Name>().name; } FlyString& name() { return value.get<Name>().name; }
DeprecatedFlyString const& lowercase_name() const { return value.get<Name>().lowercase_name; } FlyString const& lowercase_name() const { return value.get<Name>().lowercase_name; }
DeprecatedFlyString& lowercase_name() { return value.get<Name>().lowercase_name; } FlyString& lowercase_name() { return value.get<Name>().lowercase_name; }
DeprecatedString serialize() const; ErrorOr<String> serialize() const;
}; };
enum class Combinator { enum class Combinator {
@ -200,7 +200,7 @@ public:
Vector<CompoundSelector> const& compound_selectors() const { return m_compound_selectors; } Vector<CompoundSelector> const& compound_selectors() const { return m_compound_selectors; }
Optional<PseudoElement> pseudo_element() const { return m_pseudo_element; } Optional<PseudoElement> pseudo_element() const { return m_pseudo_element; }
u32 specificity() const; u32 specificity() const;
DeprecatedString serialize() const; ErrorOr<String> serialize() const;
private: private:
explicit Selector(Vector<CompoundSelector>&&); explicit Selector(Vector<CompoundSelector>&&);
@ -294,7 +294,7 @@ constexpr StringView pseudo_class_name(Selector::SimpleSelector::PseudoClass::Ty
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
DeprecatedString serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const& selectors); ErrorOr<String> serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const& selectors);
} }
@ -304,7 +304,7 @@ template<>
struct Formatter<Web::CSS::Selector> : Formatter<StringView> { struct Formatter<Web::CSS::Selector> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Selector const& selector) ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Selector const& selector)
{ {
return Formatter<StringView>::format(builder, selector.serialize()); return Formatter<StringView>::format(builder, TRY(selector.serialize()));
} }
}; };

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org> * 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 * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -21,21 +21,22 @@
#include <LibWeb/HTML/HTMLOptionElement.h> #include <LibWeb/HTML/HTMLOptionElement.h>
#include <LibWeb/HTML/HTMLSelectElement.h> #include <LibWeb/HTML/HTMLSelectElement.h>
#include <LibWeb/HTML/HTMLTextAreaElement.h> #include <LibWeb/HTML/HTMLTextAreaElement.h>
#include <LibWeb/Infra/Strings.h>
namespace Web::SelectorEngine { namespace Web::SelectorEngine {
// https://drafts.csswg.org/selectors-4/#the-lang-pseudo // 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()) { for (auto const* e = &element; e; e = e->parent_element()) {
auto lang = e->attribute(HTML::AttributeNames::lang); auto lang = e->attribute(HTML::AttributeNames::lang);
if (!lang.is_null()) { if (!lang.is_null()) {
element_language = lang; element_language = FlyString::from_utf8(lang).release_value_but_fixme_should_propagate_errors();
break; break;
} }
} }
if (element_language.is_null()) if (element_language.is_empty())
return false; return false;
// FIXME: This is ad-hoc. Implement a proper language range matching algorithm as recommended by BCP47. // 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; return false;
if (language == "*"sv) if (language == "*"sv)
return true; return true;
if (!element_language.view().contains('-')) if (!element_language.to_string().contains('-'))
return element_language.equals_ignoring_case(language); return Infra::is_ascii_case_insensitive_match(element_language, language);
auto parts = element_language.view().split_view('-'); auto parts = element_language.to_string().split_limit('-', 2).release_value_but_fixme_should_propagate_errors();
return parts[0].equals_ignoring_case(language); return Infra::is_ascii_case_insensitive_match(parts[0], language);
} }
return false; 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) { if (attribute.match_type == CSS::Selector::SimpleSelector::Attribute::MatchType::HasAttribute) {
// Early way out in case of an attribute existence selector. // 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); 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) { switch (attribute.match_type) {
case CSS::Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch: case CSS::Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch:
return case_insensitive_match return case_insensitive_match
? element.attribute(attribute.name).equals_ignoring_case(attribute.value) ? Infra::is_ascii_case_insensitive_match(element.attribute(attribute.name.to_string().to_deprecated_string()), attribute.value)
: element.attribute(attribute.name) == attribute.value; : element.attribute(attribute.name.to_string().to_deprecated_string()) == attribute.value.to_deprecated_string();
case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsWord: { case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsWord: {
if (attribute.value.is_empty()) { if (attribute.value.is_empty()) {
// This selector is always false is match value is empty. // This selector is always false is match value is empty.
return false; 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(); auto const size = view.size();
for (size_t i = 0; i < size; ++i) { for (size_t i = 0; i < size; ++i) {
auto const value = view.at(i); auto const value = view.at(i);
if (case_insensitive_match if (case_insensitive_match
? value.equals_ignoring_case(attribute.value) ? Infra::is_ascii_case_insensitive_match(value, attribute.value)
: value == attribute.value) { : value == attribute.value) {
return true; return true;
} }
@ -129,9 +130,9 @@ static inline bool matches_attribute(CSS::Selector::SimpleSelector::Attribute co
} }
case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsString: case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsString:
return !attribute.value.is_empty() 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: { 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 (element_attr_value.is_empty()) {
// If the attribute value on element is empty, the selector is true // If the attribute value on element is empty, the selector is true
// if the match value is also empty and false otherwise. // 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('-'); auto segments = element_attr_value.split_view('-');
return case_insensitive_match 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; : segments.first() == attribute.value;
} }
case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithString: case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
return !attribute.value.is_empty() 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: case CSS::Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
return !attribute.value.is_empty() 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: default:
break; break;
} }
@ -351,14 +352,14 @@ static inline bool matches(CSS::Selector::SimpleSelector const& component, DOM::
case CSS::Selector::SimpleSelector::Type::Universal: case CSS::Selector::SimpleSelector::Type::Universal:
return true; return true;
case CSS::Selector::SimpleSelector::Type::Id: 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: 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: case CSS::Selector::SimpleSelector::Type::TagName:
// See https://html.spec.whatwg.org/multipage/semantics-other.html#case-sensitivity-of-selectors // See https://html.spec.whatwg.org/multipage/semantics-other.html#case-sensitivity-of-selectors
if (element.document().document_type() == DOM::Document::Type::HTML) if (element.document().document_type() == DOM::Document::Type::HTML)
return component.lowercase_name() == element.local_name(); return component.lowercase_name() == element.local_name().view();
return component.name().equals_ignoring_case(element.local_name()); return Infra::is_ascii_case_insensitive_match(component.name(), element.local_name());
case CSS::Selector::SimpleSelector::Type::Attribute: case CSS::Selector::SimpleSelector::Type::Attribute:
return matches_attribute(component.attribute(), element); return matches_attribute(component.attribute(), element);
case CSS::Selector::SimpleSelector::Type::PseudoClass: case CSS::Selector::SimpleSelector::Type::PseudoClass:

View file

@ -1469,19 +1469,19 @@ void StyleComputer::build_rule_cache()
if (!added_to_bucket) { if (!added_to_bucket) {
for (auto const& simple_selector : selector.compound_selectors().last().simple_selectors) { for (auto const& simple_selector : selector.compound_selectors().last().simple_selectors) {
if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Id) { if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Id) {
m_rule_cache->rules_by_id.ensure(simple_selector.name()).append(move(matching_rule)); m_rule_cache->rules_by_id.ensure(simple_selector.name().to_string().to_deprecated_string()).append(move(matching_rule));
++num_id_rules; ++num_id_rules;
added_to_bucket = true; added_to_bucket = true;
break; break;
} }
if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Class) { if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Class) {
m_rule_cache->rules_by_class.ensure(simple_selector.name()).append(move(matching_rule)); m_rule_cache->rules_by_class.ensure(simple_selector.name().to_string().to_deprecated_string()).append(move(matching_rule));
++num_class_rules; ++num_class_rules;
added_to_bucket = true; added_to_bucket = true;
break; break;
} }
if (simple_selector.type == CSS::Selector::SimpleSelector::Type::TagName) { if (simple_selector.type == CSS::Selector::SimpleSelector::Type::TagName) {
m_rule_cache->rules_by_tag_name.ensure(simple_selector.name()).append(move(matching_rule)); m_rule_cache->rules_by_tag_name.ensure(simple_selector.name().to_string().to_deprecated_string()).append(move(matching_rule));
++num_tag_name_rules; ++num_tag_name_rules;
added_to_bucket = true; added_to_bucket = true;
break; break;