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

LibWeb: Convert CSS parse_{color,length}() lambdas into methods

This lets us get the Length and Color values directly, without having to
create a StyleValue object and then throw it away again, when parsing
the box-shadow property in the next commit.
This commit is contained in:
Sam Atkins 2021-07-28 14:10:03 +01:00 committed by Andreas Kling
parent 1b72766e4e
commit 697bffa3bd
2 changed files with 222 additions and 218 deletions

View file

@ -1414,9 +1414,8 @@ RefPtr<StyleValue> Parser::parse_keyword_or_custom_value(ParsingContext const&,
return {}; return {};
} }
RefPtr<StyleValue> Parser::parse_length_value(ParsingContext const& context, StyleComponentValueRule const& component_value) Optional<Length> Parser::parse_length(ParsingContext const& context, StyleComponentValueRule const& component_value)
{ {
auto parse_length = [&]() -> Optional<Length> {
Length::Type type = Length::Type::Undefined; Length::Type type = Length::Type::Undefined;
Optional<float> numeric_value; Optional<float> numeric_value;
@ -1478,10 +1477,12 @@ RefPtr<StyleValue> Parser::parse_length_value(ParsingContext const& context, Sty
return {}; return {};
return Length(numeric_value.value(), type); return Length(numeric_value.value(), type);
}; }
RefPtr<StyleValue> Parser::parse_length_value(ParsingContext const& context, StyleComponentValueRule const& component_value)
{
if (component_value.is(Token::Type::Dimension) || component_value.is(Token::Type::Number) || component_value.is(Token::Type::Percentage)) { if (component_value.is(Token::Type::Dimension) || component_value.is(Token::Type::Number) || component_value.is(Token::Type::Percentage)) {
auto length = parse_length(); auto length = parse_length(context, component_value);
if (length.has_value()) if (length.has_value())
return LengthStyleValue::create(length.value()); return LengthStyleValue::create(length.value());
} }
@ -1516,10 +1517,9 @@ RefPtr<StyleValue> Parser::parse_identifier_value(ParsingContext const&, StyleCo
return {}; return {};
} }
RefPtr<StyleValue> Parser::parse_color_value(ParsingContext const&, StyleComponentValueRule const& component_value) Optional<Color> Parser::parse_color(ParsingContext const&, StyleComponentValueRule const& component_value)
{ {
// https://www.w3.org/TR/css-color-3/ // https://www.w3.org/TR/css-color-3/
auto parse_css_color = [&]() -> Optional<Color> {
if (component_value.is(Token::Type::Ident)) { if (component_value.is(Token::Type::Ident)) {
auto ident = component_value.token().ident(); auto ident = component_value.token().ident();
if (ident.equals_ignoring_case("transparent")) if (ident.equals_ignoring_case("transparent"))
@ -1685,9 +1685,11 @@ RefPtr<StyleValue> Parser::parse_color_value(ParsingContext const&, StyleCompone
} }
return {}; return {};
}; }
auto color = parse_css_color(); RefPtr<StyleValue> Parser::parse_color_value(ParsingContext const& context, StyleComponentValueRule const& component_value)
{
auto color = parse_color(context, component_value);
if (color.has_value()) if (color.has_value())
return ColorStyleValue::create(color.value()); return ColorStyleValue::create(color.value());

View file

@ -165,6 +165,8 @@ private:
[[nodiscard]] Optional<StyleProperty> convert_to_style_property(StyleDeclarationRule&); [[nodiscard]] Optional<StyleProperty> convert_to_style_property(StyleDeclarationRule&);
static Optional<float> try_parse_float(StringView string); static Optional<float> try_parse_float(StringView string);
static Optional<Color> parse_color(ParsingContext const&, StyleComponentValueRule const&);
static Optional<Length> parse_length(ParsingContext const&, StyleComponentValueRule const&);
static Optional<URL> parse_url_function(ParsingContext const&, StyleComponentValueRule const&); static Optional<URL> parse_url_function(ParsingContext const&, StyleComponentValueRule const&);
static RefPtr<StyleValue> parse_keyword_or_custom_value(ParsingContext const&, StyleComponentValueRule const&); static RefPtr<StyleValue> parse_keyword_or_custom_value(ParsingContext const&, StyleComponentValueRule const&);