1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 15:47:42 +00:00

LibWeb: Make serializing selectors infallible

This commit is contained in:
Sam Atkins 2023-08-22 12:45:29 +01:00 committed by Sam Atkins
parent afa27bad19
commit 91114c157b
3 changed files with 60 additions and 60 deletions

View file

@ -49,7 +49,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()).release_value_but_fixme_should_propagate_errors()); builder.append(serialize_a_group_of_selectors(selectors()));
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.
@ -90,7 +90,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()).release_value_but_fixme_should_propagate_errors().to_deprecated_string(); return serialize_a_group_of_selectors(selectors()).to_deprecated_string();
} }
// https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext // https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext

View file

@ -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
ErrorOr<String> Selector::SimpleSelector::serialize() const String Selector::SimpleSelector::serialize() const
{ {
StringBuilder s; StringBuilder s;
switch (type) { switch (type) {
@ -131,13 +131,13 @@ ErrorOr<String> Selector::SimpleSelector::serialize() const
// followed by a "|" (U+007C) to s. // followed by a "|" (U+007C) to s.
if (qualified_name.namespace_type == QualifiedName::NamespaceType::Named) { if (qualified_name.namespace_type == QualifiedName::NamespaceType::Named) {
serialize_an_identifier(s, qualified_name.namespace_); serialize_an_identifier(s, qualified_name.namespace_);
TRY(s.try_append('|')); s.append('|');
} }
// 2. If the namespace prefix maps to a namespace that is the null namespace (not in a namespace) // 2. If the namespace prefix maps to a namespace that is the null namespace (not in a namespace)
// append "|" (U+007C) to s. // append "|" (U+007C) to s.
if (qualified_name.namespace_type == QualifiedName::NamespaceType::None) if (qualified_name.namespace_type == QualifiedName::NamespaceType::None)
TRY(s.try_append('|')); s.append('|');
// 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)
@ -145,7 +145,7 @@ ErrorOr<String> Selector::SimpleSelector::serialize() const
// 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)
TRY(s.try_append('*')); s.append('*');
break; break;
} }
@ -153,13 +153,13 @@ ErrorOr<String> Selector::SimpleSelector::serialize() const
auto& attribute = this->attribute(); auto& attribute = this->attribute();
// 1. Append "[" (U+005B) to s. // 1. Append "[" (U+005B) to s.
TRY(s.try_append('[')); s.append('[');
// 2. If the namespace prefix maps to a namespace that is not the null namespace (not in a namespace) // 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. // append the serialization of the namespace prefix as an identifier, followed by a "|" (U+007C) to s.
if (attribute.qualified_name.namespace_type == QualifiedName::NamespaceType::Named) { if (attribute.qualified_name.namespace_type == QualifiedName::NamespaceType::Named) {
serialize_an_identifier(s, attribute.qualified_name.namespace_); serialize_an_identifier(s, attribute.qualified_name.namespace_);
TRY(s.try_append('|')); s.append('|');
} }
// 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.
@ -170,22 +170,22 @@ ErrorOr<String> Selector::SimpleSelector::serialize() const
if (!attribute.value.is_empty()) { 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:
TRY(s.try_append("="sv)); s.append("="sv);
break; break;
case Selector::SimpleSelector::Attribute::MatchType::ContainsWord: case Selector::SimpleSelector::Attribute::MatchType::ContainsWord:
TRY(s.try_append("~="sv)); s.append("~="sv);
break; break;
case Selector::SimpleSelector::Attribute::MatchType::ContainsString: case Selector::SimpleSelector::Attribute::MatchType::ContainsString:
TRY(s.try_append("*="sv)); s.append("*="sv);
break; break;
case Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment: case Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment:
TRY(s.try_append("|="sv)); s.append("|="sv);
break; break;
case Selector::SimpleSelector::Attribute::MatchType::StartsWithString: case Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
TRY(s.try_append("^="sv)); s.append("^="sv);
break; break;
case Selector::SimpleSelector::Attribute::MatchType::EndsWithString: case Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
TRY(s.try_append("$="sv)); s.append("$="sv);
break; break;
default: default:
break; break;
@ -199,29 +199,29 @@ ErrorOr<String> 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:
TRY(s.try_append(" i"sv)); s.append(" i"sv);
break; break;
case Selector::SimpleSelector::Attribute::CaseType::CaseSensitiveMatch: case Selector::SimpleSelector::Attribute::CaseType::CaseSensitiveMatch:
TRY(s.try_append(" s"sv)); s.append(" s"sv);
break; break;
default: default:
break; break;
} }
// 6. Append "]" (U+005D) to s. // 6. Append "]" (U+005D) to s.
TRY(s.try_append(']')); s.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.
TRY(s.try_append('.')); s.append('.');
serialize_an_identifier(s, name()); 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.
TRY(s.try_append('#')); s.append('#');
serialize_an_identifier(s, name()); serialize_an_identifier(s, name());
break; break;
@ -233,44 +233,44 @@ ErrorOr<String> Selector::SimpleSelector::serialize() const
// It's also not in the spec. // It's also not in the spec.
if (pseudo_class.type == PseudoClass::Host) { if (pseudo_class.type == PseudoClass::Host) {
if (pseudo_class.argument_selector_list.is_empty()) { if (pseudo_class.argument_selector_list.is_empty()) {
TRY(s.try_append(':')); s.append(':');
TRY(s.try_append(pseudo_class_name(pseudo_class.type))); s.append(pseudo_class_name(pseudo_class.type));
} else { } else {
TRY(s.try_append(':')); s.append(':');
TRY(s.try_append(pseudo_class_name(pseudo_class.type))); s.append(pseudo_class_name(pseudo_class.type));
TRY(s.try_append('(')); s.append('(');
TRY(s.try_append(TRY(serialize_a_group_of_selectors(pseudo_class.argument_selector_list)))); s.append(serialize_a_group_of_selectors(pseudo_class.argument_selector_list));
TRY(s.try_append(')')); s.append(')');
} }
} }
// 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.
else if (metadata.is_valid_as_identifier) { else if (metadata.is_valid_as_identifier) {
TRY(s.try_append(':')); s.append(':');
TRY(s.try_append(pseudo_class_name(pseudo_class.type))); s.append(pseudo_class_name(pseudo_class.type));
} }
// 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.
else { else {
TRY(s.try_append(':')); s.append(':');
TRY(s.try_append(pseudo_class_name(pseudo_class.type))); s.append(pseudo_class_name(pseudo_class.type));
TRY(s.try_append('(')); s.append('(');
if (pseudo_class.type == PseudoClass::NthChild if (pseudo_class.type == PseudoClass::NthChild
|| pseudo_class.type == PseudoClass::NthLastChild || pseudo_class.type == PseudoClass::NthLastChild
|| pseudo_class.type == PseudoClass::NthOfType || pseudo_class.type == PseudoClass::NthOfType
|| pseudo_class.type == PseudoClass::NthLastOfType) { || pseudo_class.type == PseudoClass::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.
TRY(s.try_append(TRY(pseudo_class.nth_child_pattern.serialize()))); s.append(pseudo_class.nth_child_pattern.serialize());
} else if (pseudo_class.type == PseudoClass::Not } else if (pseudo_class.type == PseudoClass::Not
|| pseudo_class.type == PseudoClass::Is || pseudo_class.type == PseudoClass::Is
|| pseudo_class.type == PseudoClass::Where) { || pseudo_class.type == PseudoClass::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!
TRY(s.try_append(TRY(serialize_a_group_of_selectors(pseudo_class.argument_selector_list)))); s.append(serialize_a_group_of_selectors(pseudo_class.argument_selector_list));
} else if (pseudo_class.type == PseudoClass::Lang) { } else if (pseudo_class.type == PseudoClass::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);
} }
TRY(s.try_append(')')); s.append(')');
} }
break; break;
} }
@ -281,11 +281,11 @@ ErrorOr<String> 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_string(); return MUST(s.to_string());
} }
// https://www.w3.org/TR/cssom/#serialize-a-selector // https://www.w3.org/TR/cssom/#serialize-a-selector
ErrorOr<String> Selector::serialize() const String Selector::serialize() const
{ {
StringBuilder s; StringBuilder s;
@ -295,7 +295,7 @@ ErrorOr<String> 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) {
TRY(s.try_append(TRY(compound_selector.simple_selectors.first().serialize()))); s.append(compound_selector.simple_selectors.first().serialize());
} }
// 2. Otherwise, for each simple selector in the compound selectors that is not a universal selector // 2. Otherwise, for each simple selector in the compound selectors that is not a universal selector
// of which the namespace prefix maps to a namespace that is not the default namespace // of which the namespace prefix maps to a namespace that is not the default namespace
@ -314,7 +314,7 @@ ErrorOr<String> Selector::serialize() const
// foo|*.bar { } /* This would skip the `foo|*` when serializing. */ // foo|*.bar { } /* This would skip the `foo|*` when serializing. */
// </style> // </style>
} }
TRY(s.try_append(TRY(simple_selector.serialize()))); s.append(simple_selector.serialize());
} }
} }
@ -322,21 +322,21 @@ ErrorOr<String> 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) {
TRY(s.try_append(' ')); s.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:
TRY(s.try_append("> "sv)); s.append("> "sv);
break; break;
case Selector::Combinator::NextSibling: case Selector::Combinator::NextSibling:
TRY(s.try_append("+ "sv)); s.append("+ "sv);
break; break;
case Selector::Combinator::SubsequentSibling: case Selector::Combinator::SubsequentSibling:
TRY(s.try_append("~ "sv)); s.append("~ "sv);
break; break;
case Selector::Combinator::Column: case Selector::Combinator::Column:
TRY(s.try_append("|| "sv)); s.append("|| "sv);
break; break;
default: default:
break; break;
@ -345,20 +345,20 @@ ErrorOr<String> 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) {
TRY(s.try_append("::"sv)); s.append("::"sv);
TRY(s.try_append(pseudo_element_name(compound_selector.simple_selectors.last().pseudo_element()))); s.append(pseudo_element_name(compound_selector.simple_selectors.last().pseudo_element()));
} }
} }
} }
return s.to_string(); return MUST(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
ErrorOr<String> serialize_a_group_of_selectors(Vector<NonnullRefPtr<Selector>> const& selectors) String serialize_a_group_of_selectors(Vector<NonnullRefPtr<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.
return String::join(", "sv, selectors); return MUST(String::join(", "sv, selectors));
} }
Optional<Selector::PseudoElement> pseudo_element_from_string(StringView name) Optional<Selector::PseudoElement> pseudo_element_from_string(StringView name)

View file

@ -52,11 +52,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
ErrorOr<String> serialize() const 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 String::formatted("{}", offset); return MUST(String::number(offset));
} }
// 2. Otherwise, let result initially be an empty string. // 2. Otherwise, let result initially be an empty string.
@ -65,24 +65,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)
TRY(result.try_append('n')); result.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)
TRY(result.try_append("-n"sv)); result.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)
TRY(result.try_appendff("{}n", step_size)); result.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)
TRY(result.try_appendff("+{}", offset)); result.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)
TRY(result.try_appendff("{}", offset)); result.appendff("{}", offset);
// 5. Return result. // 5. Return result.
return result.to_string(); return MUST(result.to_string());
} }
}; };
@ -165,7 +165,7 @@ public:
QualifiedName const& qualified_name() const { return value.get<QualifiedName>(); } QualifiedName const& qualified_name() const { return value.get<QualifiedName>(); }
QualifiedName& qualified_name() { return value.get<QualifiedName>(); } QualifiedName& qualified_name() { return value.get<QualifiedName>(); }
ErrorOr<String> serialize() const; String serialize() const;
}; };
enum class Combinator { enum class Combinator {
@ -194,7 +194,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;
ErrorOr<String> serialize() const; String serialize() const;
private: private:
explicit Selector(Vector<CompoundSelector>&&); explicit Selector(Vector<CompoundSelector>&&);
@ -233,7 +233,7 @@ constexpr StringView pseudo_element_name(Selector::PseudoElement pseudo_element)
Optional<Selector::PseudoElement> pseudo_element_from_string(StringView); Optional<Selector::PseudoElement> pseudo_element_from_string(StringView);
ErrorOr<String> serialize_a_group_of_selectors(Vector<NonnullRefPtr<Selector>> const& selectors); String serialize_a_group_of_selectors(Vector<NonnullRefPtr<Selector>> const& selectors);
} }
@ -243,7 +243,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, TRY(selector.serialize())); return Formatter<StringView>::format(builder, selector.serialize());
} }
}; };