mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-01 21:59:07 +00:00
LibWeb: Parse color values using TokenStream
This commit is contained in:
parent
710e5c24d3
commit
e4e048f278
Notes:
sideshowbarker
2024-07-18 04:46:35 +09:00
Author: https://github.com/AtkinsSJ
Commit: e4e048f278
Pull-request: https://github.com/SerenityOS/serenity/pull/24062
Reviewed-by: https://github.com/Hendiadyoin1
3 changed files with 18 additions and 18 deletions
|
@ -30,15 +30,14 @@ Optional<Vector<TElement>> Parser::parse_color_stop_list(TokenStream<ComponentVa
|
||||||
tokens.skip_whitespace();
|
tokens.skip_whitespace();
|
||||||
if (!tokens.has_next_token())
|
if (!tokens.has_next_token())
|
||||||
return ElementType::Garbage;
|
return ElementType::Garbage;
|
||||||
auto const& token = tokens.next_token();
|
|
||||||
|
|
||||||
RefPtr<StyleValue> color;
|
RefPtr<StyleValue> color;
|
||||||
Optional<typename TElement::PositionType> position;
|
Optional<typename TElement::PositionType> position;
|
||||||
Optional<typename TElement::PositionType> second_position;
|
Optional<typename TElement::PositionType> second_position;
|
||||||
auto dimension = parse_dimension(token);
|
if (auto dimension = parse_dimension(tokens.peek_token()); dimension.has_value() && is_position(*dimension)) {
|
||||||
if (dimension.has_value() && is_position(*dimension)) {
|
|
||||||
// [<T-percentage> <color>] or [<T-percentage>]
|
// [<T-percentage> <color>] or [<T-percentage>]
|
||||||
position = get_position(*dimension);
|
position = get_position(*dimension);
|
||||||
|
(void)tokens.next_token(); // dimension
|
||||||
tokens.skip_whitespace();
|
tokens.skip_whitespace();
|
||||||
// <T-percentage>
|
// <T-percentage>
|
||||||
if (!tokens.has_next_token() || tokens.peek_token().is(Token::Type::Comma)) {
|
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;
|
return ElementType::ColorHint;
|
||||||
}
|
}
|
||||||
// <T-percentage> <color>
|
// <T-percentage> <color>
|
||||||
auto maybe_color = parse_color_value(tokens.next_token());
|
auto maybe_color = parse_color_value(tokens);
|
||||||
if (!maybe_color)
|
if (!maybe_color)
|
||||||
return ElementType::Garbage;
|
return ElementType::Garbage;
|
||||||
color = maybe_color.release_nonnull();
|
color = maybe_color.release_nonnull();
|
||||||
} else {
|
} else {
|
||||||
// [<color> <T-percentage>?]
|
// [<color> <T-percentage>?]
|
||||||
auto maybe_color = parse_color_value(token);
|
auto maybe_color = parse_color_value(tokens);
|
||||||
if (!maybe_color)
|
if (!maybe_color)
|
||||||
return ElementType::Garbage;
|
return ElementType::Garbage;
|
||||||
color = maybe_color.release_nonnull();
|
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.
|
// Note: Double-position color stops only appear to be valid in this order.
|
||||||
for (auto stop_position : Array { &position, &second_position }) {
|
for (auto stop_position : Array { &position, &second_position }) {
|
||||||
if (tokens.has_next_token() && !tokens.peek_token().is(Token::Type::Comma)) {
|
if (tokens.has_next_token() && !tokens.peek_token().is(Token::Type::Comma)) {
|
||||||
auto token = tokens.next_token();
|
auto dimension = parse_dimension(tokens.next_token());
|
||||||
auto dimension = parse_dimension(token);
|
|
||||||
if (!dimension.has_value() || !is_position(*dimension))
|
if (!dimension.has_value() || !is_position(*dimension))
|
||||||
return ElementType::Garbage;
|
return ElementType::Garbage;
|
||||||
*stop_position = get_position(*dimension);
|
*stop_position = get_position(*dimension);
|
||||||
|
|
|
@ -2502,16 +2502,22 @@ Optional<Color> Parser::parse_color(ComponentValue const& component_value)
|
||||||
return {};
|
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);
|
auto transaction = tokens.begin_transaction();
|
||||||
if (color.has_value())
|
auto component_value = tokens.next_token();
|
||||||
|
|
||||||
|
if (auto color = parse_color(component_value); color.has_value()) {
|
||||||
|
transaction.commit();
|
||||||
return ColorStyleValue::create(color.value());
|
return ColorStyleValue::create(color.value());
|
||||||
|
}
|
||||||
|
|
||||||
if (component_value.is(Token::Type::Ident)) {
|
if (component_value.is(Token::Type::Ident)) {
|
||||||
auto ident = value_id_from_string(component_value.token().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 IdentifierStyleValue::create(ident.value());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
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`
|
// `<paint> = none | <color> | <url> [none | <color>]? | context-fill | context-stroke`
|
||||||
|
|
||||||
auto parse_color_or_none = [&]() -> Optional<RefPtr<StyleValue>> {
|
auto parse_color_or_none = [&]() -> Optional<RefPtr<StyleValue>> {
|
||||||
if (auto color = parse_color_value(tokens.peek_token())) {
|
if (auto color = parse_color_value(tokens))
|
||||||
(void)tokens.next_token();
|
|
||||||
return color;
|
return color;
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE: <color> also accepts identifiers, so we do this identifier check last.
|
// NOTE: <color> also accepts identifiers, so we do this identifier check last.
|
||||||
if (tokens.peek_token().is(Token::Type::Ident)) {
|
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 property = any_property_accepts_type(property_ids, ValueType::Color); property.has_value()) {
|
||||||
if (auto maybe_color = parse_color_value(peek_token)) {
|
if (auto maybe_color = parse_color_value(tokens))
|
||||||
(void)tokens.next_token();
|
|
||||||
return PropertyAndValue { *property, maybe_color };
|
return PropertyAndValue { *property, maybe_color };
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto property = any_property_accepts_type(property_ids, ValueType::Image); property.has_value()) {
|
if (auto property = any_property_accepts_type(property_ids, ValueType::Image); property.has_value()) {
|
||||||
|
|
|
@ -224,7 +224,7 @@ private:
|
||||||
RefPtr<StyleValue> parse_number_value(TokenStream<ComponentValue>&);
|
RefPtr<StyleValue> parse_number_value(TokenStream<ComponentValue>&);
|
||||||
RefPtr<StyleValue> parse_number_or_percentage_value(TokenStream<ComponentValue>&);
|
RefPtr<StyleValue> parse_number_or_percentage_value(TokenStream<ComponentValue>&);
|
||||||
RefPtr<StyleValue> parse_identifier_value(ComponentValue const&);
|
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_rect_value(TokenStream<ComponentValue>&);
|
||||||
RefPtr<StyleValue> parse_ratio_value(TokenStream<ComponentValue>&);
|
RefPtr<StyleValue> parse_ratio_value(TokenStream<ComponentValue>&);
|
||||||
RefPtr<StyleValue> parse_string_value(TokenStream<ComponentValue>&);
|
RefPtr<StyleValue> parse_string_value(TokenStream<ComponentValue>&);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue