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

LibWeb: Parse content property using TokenStream

This commit is contained in:
Sam Atkins 2023-12-07 12:34:51 +00:00 committed by Sam Atkins
parent 4d6e8d8f37
commit 3d6eb36664
2 changed files with 12 additions and 7 deletions

View file

@ -3430,10 +3430,12 @@ RefPtr<StyleValue> Parser::parse_single_shadow_value(TokenStream<ComponentValue>
return ShadowStyleValue::create(color.release_value(), offset_x.release_nonnull(), offset_y.release_nonnull(), blur_radius.release_nonnull(), spread_distance.release_nonnull(), placement.release_value()); return ShadowStyleValue::create(color.release_value(), offset_x.release_nonnull(), offset_y.release_nonnull(), blur_radius.release_nonnull(), spread_distance.release_nonnull(), placement.release_value());
} }
RefPtr<StyleValue> Parser::parse_content_value(Vector<ComponentValue> const& component_values) RefPtr<StyleValue> Parser::parse_content_value(TokenStream<ComponentValue>& tokens)
{ {
// FIXME: `content` accepts several kinds of function() type, which we don't handle in property_accepts_value() yet. // FIXME: `content` accepts several kinds of function() type, which we don't handle in property_accepts_value() yet.
auto transaction = tokens.begin_transaction();
auto is_single_value_identifier = [](ValueID identifier) -> bool { auto is_single_value_identifier = [](ValueID identifier) -> bool {
switch (identifier) { switch (identifier) {
case ValueID::None: case ValueID::None:
@ -3444,10 +3446,13 @@ RefPtr<StyleValue> Parser::parse_content_value(Vector<ComponentValue> const& com
} }
}; };
if (component_values.size() == 1) { if (tokens.remaining_token_count() == 1) {
if (auto identifier = parse_identifier_value(component_values.first())) { if (auto identifier = parse_identifier_value(tokens.peek_token())) {
if (is_single_value_identifier(identifier->to_identifier())) if (is_single_value_identifier(identifier->to_identifier())) {
(void)tokens.next_token();
transaction.commit();
return identifier; return identifier;
}
} }
} }
@ -3455,7 +3460,6 @@ RefPtr<StyleValue> Parser::parse_content_value(Vector<ComponentValue> const& com
StyleValueVector alt_text_values; StyleValueVector alt_text_values;
bool in_alt_text = false; bool in_alt_text = false;
auto tokens = TokenStream { component_values };
while (tokens.has_next_token()) { while (tokens.has_next_token()) {
auto& next = tokens.peek_token(); auto& next = tokens.peek_token();
if (next.is_delim('/')) { if (next.is_delim('/')) {
@ -3490,6 +3494,7 @@ RefPtr<StyleValue> Parser::parse_content_value(Vector<ComponentValue> const& com
if (!alt_text_values.is_empty()) if (!alt_text_values.is_empty())
alt_text = StyleValueList::create(move(alt_text_values), StyleValueList::Separator::Space); alt_text = StyleValueList::create(move(alt_text_values), StyleValueList::Separator::Space);
transaction.commit();
return ContentStyleValue::create(StyleValueList::create(move(content_values), StyleValueList::Separator::Space), move(alt_text)); return ContentStyleValue::create(StyleValueList::create(move(content_values), StyleValueList::Separator::Space), move(alt_text));
} }
@ -5768,7 +5773,7 @@ Parser::ParseErrorOr<NonnullRefPtr<StyleValue>> Parser::parse_css_value(Property
return parsed_value.release_nonnull(); return parsed_value.release_nonnull();
return ParseError::SyntaxError; return ParseError::SyntaxError;
case PropertyID::Content: case PropertyID::Content:
if (auto parsed_value = parse_content_value(component_values)) if (auto parsed_value = parse_content_value(tokens); parsed_value && !tokens.has_next_token())
return parsed_value.release_nonnull(); return parsed_value.release_nonnull();
return ParseError::SyntaxError; return ParseError::SyntaxError;
case PropertyID::Display: case PropertyID::Display:

View file

@ -233,7 +233,7 @@ private:
RefPtr<StyleValue> parse_border_value(PropertyID, Vector<ComponentValue> const&); RefPtr<StyleValue> parse_border_value(PropertyID, Vector<ComponentValue> const&);
RefPtr<StyleValue> parse_border_radius_value(TokenStream<ComponentValue>&); RefPtr<StyleValue> parse_border_radius_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue> parse_border_radius_shorthand_value(TokenStream<ComponentValue>&); RefPtr<StyleValue> parse_border_radius_shorthand_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue> parse_content_value(Vector<ComponentValue> const&); RefPtr<StyleValue> parse_content_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue> parse_display_value(Vector<ComponentValue> const&); RefPtr<StyleValue> parse_display_value(Vector<ComponentValue> const&);
RefPtr<StyleValue> parse_flex_value(Vector<ComponentValue> const&); RefPtr<StyleValue> parse_flex_value(Vector<ComponentValue> const&);
RefPtr<StyleValue> parse_flex_flow_value(Vector<ComponentValue> const&); RefPtr<StyleValue> parse_flex_flow_value(Vector<ComponentValue> const&);