LibWeb: Parse color values using TokenStream

This commit is contained in:
Sam Atkins 2024-04-21 18:35:50 +01:00 committed by Andreas Kling
commit e4e048f278
Notes: sideshowbarker 2024-07-18 04:46:35 +09:00
3 changed files with 18 additions and 18 deletions

View file

@ -30,15 +30,14 @@ Optional<Vector<TElement>> Parser::parse_color_stop_list(TokenStream<ComponentVa
tokens.skip_whitespace();
if (!tokens.has_next_token())
return ElementType::Garbage;
auto const& token = tokens.next_token();
RefPtr<StyleValue> color;
Optional<typename TElement::PositionType> position;
Optional<typename TElement::PositionType> second_position;
auto dimension = parse_dimension(token);
if (dimension.has_value() && is_position(*dimension)) {
if (auto dimension = parse_dimension(tokens.peek_token()); dimension.has_value() && is_position(*dimension)) {
// [<T-percentage> <color>] or [<T-percentage>]
position = get_position(*dimension);
(void)tokens.next_token(); // dimension
tokens.skip_whitespace();
// <T-percentage>
if (!tokens.has_next_token() || tokens.peek_token().is(Token::Type::Comma)) {
@ -46,13 +45,13 @@ Optional<Vector<TElement>> Parser::parse_color_stop_list(TokenStream<ComponentVa
return ElementType::ColorHint;
}
// <T-percentage> <color>
auto maybe_color = parse_color_value(tokens.next_token());
auto maybe_color = parse_color_value(tokens);
if (!maybe_color)
return ElementType::Garbage;
color = maybe_color.release_nonnull();
} else {
// [<color> <T-percentage>?]
auto maybe_color = parse_color_value(token);
auto maybe_color = parse_color_value(tokens);
if (!maybe_color)
return ElementType::Garbage;
color = maybe_color.release_nonnull();
@ -61,8 +60,7 @@ Optional<Vector<TElement>> Parser::parse_color_stop_list(TokenStream<ComponentVa
// Note: Double-position color stops only appear to be valid in this order.
for (auto stop_position : Array { &position, &second_position }) {
if (tokens.has_next_token() && !tokens.peek_token().is(Token::Type::Comma)) {
auto token = tokens.next_token();
auto dimension = parse_dimension(token);
auto dimension = parse_dimension(tokens.next_token());
if (!dimension.has_value() || !is_position(*dimension))
return ElementType::Garbage;
*stop_position = get_position(*dimension);

View file

@ -2502,16 +2502,22 @@ Optional<Color> Parser::parse_color(ComponentValue const& component_value)
return {};
}
RefPtr<StyleValue> Parser::parse_color_value(ComponentValue const& component_value)
RefPtr<StyleValue> Parser::parse_color_value(TokenStream<ComponentValue>& tokens)
{
auto color = parse_color(component_value);
if (color.has_value())
auto transaction = tokens.begin_transaction();
auto component_value = tokens.next_token();
if (auto color = parse_color(component_value); color.has_value()) {
transaction.commit();
return ColorStyleValue::create(color.value());
}
if (component_value.is(Token::Type::Ident)) {
auto ident = value_id_from_string(component_value.token().ident());
if (ident.has_value() && IdentifierStyleValue::is_color(ident.value()))
if (ident.has_value() && IdentifierStyleValue::is_color(ident.value())) {
transaction.commit();
return IdentifierStyleValue::create(ident.value());
}
}
return nullptr;
@ -2565,10 +2571,8 @@ RefPtr<StyleValue> Parser::parse_paint_value(TokenStream<ComponentValue>& tokens
// `<paint> = none | <color> | <url> [none | <color>]? | context-fill | context-stroke`
auto parse_color_or_none = [&]() -> Optional<RefPtr<StyleValue>> {
if (auto color = parse_color_value(tokens.peek_token())) {
(void)tokens.next_token();
if (auto color = parse_color_value(tokens))
return color;
}
// NOTE: <color> also accepts identifiers, so we do this identifier check last.
if (tokens.peek_token().is(Token::Type::Ident)) {
@ -6499,10 +6503,8 @@ Optional<Parser::PropertyAndValue> Parser::parse_css_value_for_properties(Readon
}
if (auto property = any_property_accepts_type(property_ids, ValueType::Color); property.has_value()) {
if (auto maybe_color = parse_color_value(peek_token)) {
(void)tokens.next_token();
if (auto maybe_color = parse_color_value(tokens))
return PropertyAndValue { *property, maybe_color };
}
}
if (auto property = any_property_accepts_type(property_ids, ValueType::Image); property.has_value()) {

View file

@ -224,7 +224,7 @@ private:
RefPtr<StyleValue> parse_number_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue> parse_number_or_percentage_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue> parse_identifier_value(ComponentValue const&);
RefPtr<StyleValue> parse_color_value(ComponentValue const&);
RefPtr<StyleValue> parse_color_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue> parse_rect_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue> parse_ratio_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue> parse_string_value(TokenStream<ComponentValue>&);