dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / parser / property_parsing_1.lucb
#==============================================================================================
#
# property_parsing_1 - parsing property values: lists, shorthands and values for properties
#
# DESCRIPTION:
# Port of LibWeb/CSS/Parser/PropertyParsing.cpp (lines 1-368): a keyword as the whole
# value, simple and coordinating comma-separated value lists, and the value of one of
# several properties (keywords, custom idents, then each value type the properties
# accept, in the C++ order).
#
# Copyright (c) 2018-2025, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2020-2021, the SerenityOS developers.
# Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
# Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
# Copyright (c) 2022, MacDue <macdue@dueutil.tech>
# Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
# Copyright (c) 2024, Tommy van der Vorst <tommy@pixelspark.nl>
# Copyright (c) 2024, Matthew Olsson <mattco@serenityos.org>
# Copyright (c) 2024, Glenn Skrzypczak <glenn.skrzypczak@gmail.com>
# Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Helpers =================================================================================
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:73 Web::CSS::Parser::remove_property.
func css_remove_property(properties: ak.Vector[css_data.PropertyId]*, property_to_remove: css_data.PropertyId):
# `properties.remove_first_matching([&](auto it) { return it == property_to_remove; })`
for index in 0..<ak.vector_size(properties):
if *ak.vector_at(properties, index) == property_to_remove:
ak.vector_remove(properties, index)
return
## `PropertyAndValue { property, value }`.
func css_property_and_value(property: css_data.PropertyId, value: CssStyleValue*) -> CssParserPropertyAndValue:
return CssParserPropertyAndValue(property = property, style_value = value)
## A StyleValueList of `values` with `separator`, as a StyleValue.
func css_style_value_list_of(values: ak.Vector[CssStyleValue*], separator: CssStyleValueListSeparator) -> CssStyleValue*:
return css_style_value_list_create(values, separator).css_style_value()
## `ShorthandStyleValue::create(...)` as a StyleValue.
func css_shorthand_of(shorthand: css_data.PropertyId, sub_properties: ak.Vector[css_data.PropertyId], values: ak.Vector[CssStyleValue*]) -> CssStyleValue*:
return css_shorthand_style_value_create(shorthand, sub_properties, values).css_style_value()
## `KeywordStyleValue::create(keyword)` as a StyleValue.
func css_keyword_value_of(keyword: css_data.Keyword) -> CssStyleValue*:
return css_keyword_style_value_create(keyword).css_style_value()
## A Vector of the given property ids (`{ PropertyID::A, PropertyID::B, ... }`).
func css_property_ids_of(ids: const css_data.PropertyId[]) -> ak.Vector[css_data.PropertyId]:
var vector = ak.Vector[css_data.PropertyId]()
for id in ids:
ak.vector_append_t(&vector, id)
return vector
## A Vector of the given style values (`{ a, b, ... }`).
func css_style_values_of(values: const (CssStyleValue*)[]) -> ak.Vector[CssStyleValue*]:
var vector = ak.Vector[CssStyleValue*]()
for value in values:
ak.vector_append_t(&vector, value)
return vector
# mark: Keywords and lists ======================================================================
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:78 Web::CSS::Parser::Parser::parse_all_as_single_keyword_value.
pub func css_parser_parse_all_as_single_keyword_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, keyword: css_data.Keyword) -> CssStyleValue*?:
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
css_syntax.token_stream_discard_whitespace(tokens)
let keyword_value = css_parser_parse_specific_keyword_value(this, tokens, keyword)
css_syntax.token_stream_discard_whitespace(tokens)
if css_syntax.token_stream_has_next_token(tokens) or keyword_value == none:
return none
css_syntax.state_transaction_commit(&transaction)
return keyword_value
## The captures of the per-value lambdas that need the parser and a property id.
struct CssParserPropertyCaptures:
var this: CssParser*
var property_id: css_data.PropertyId
## The lambda of parse_simple_comma_separated_value_list: one value of the property, in a transaction.
func css_parser_parse_simple_comma_separated_value(context: void*?, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
let captures = (CssParserPropertyCaptures*)(context else trap("no captures"))
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
if let value = css_parser_parse_css_value_for_property(captures.this, captures.property_id, tokens):
css_syntax.state_transaction_commit(&transaction)
return value
return none
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:92 Web::CSS::Parser::Parser::parse_simple_comma_separated_value_list.
pub func css_parser_parse_simple_comma_separated_value_list(this: CssParser*, property_id: css_data.PropertyId, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValueList*?:
var captures = CssParserPropertyCaptures(this = this, property_id = property_id)
return css_parser_parse_comma_separated_value_list(this, tokens, ak.Function1[css_syntax.TokenStream[css_syntax.ComponentValue]*, CssStyleValue*?](
call = css_parser_parse_simple_comma_separated_value, context = &captures))
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:104 Web::CSS::Parser::Parser::parse_coordinating_value_list_shorthand.
pub func css_parser_parse_coordinating_value_list_shorthand(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, shorthand_id: css_data.PropertyId,
longhand_ids: ak.Vector[css_data.PropertyId],
reset_only_longhand_ids: ak.Vector[css_data.PropertyId] = ak.Vector[css_data.PropertyId]()) -> CssStyleValue*?:
var longhand_vectors = ak.HashMap[css_data.PropertyId, ak.Vector[CssStyleValue*], css_data.PropertyIdTraits]()
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
while true:
var remaining_longhands = ak.Vector[css_data.PropertyId]()
ak.vector_extend_2(&remaining_longhands, &longhand_ids)
var parsed_values = ak.HashMap[css_data.PropertyId, CssStyleValue*, css_data.PropertyIdTraits]()
while css_syntax.token_stream_has_next_token(tokens) and not css_next_token_is(tokens, .comma):
let property_and_value = css_parser_parse_css_value_for_properties(this, ak.vector_span_const(&remaining_longhands), tokens) else return none
css_remove_property(&remaining_longhands, property_and_value.property)
ak.hash_map_set(&parsed_values, property_and_value.property, property_and_value.style_value else trap("release_nonnull"))
if ak.hash_map_is_empty(&parsed_values):
return none
for longhand_id in ak.vector_span_const(&longhand_ids):
var value: CssStyleValue*? = none
if let parsed_value = ak.hash_map_get_k(&parsed_values, longhand_id):
value = *parsed_value
else:
let initial_values = css_style_value_list_values(css_style_value_as_value_list(css_property_initial_value(longhand_id)))
value = *ak.vector_at_const(&initial_values, 0)
ak.vector_append_t(ak.hash_map_ensure(&longhand_vectors, longhand_id), value else trap("unreachable"))
if css_syntax.token_stream_has_next_token(tokens):
if css_next_token_is(tokens, .comma):
css_syntax.token_stream_discard_a_token(tokens)
else:
return none
if not css_syntax.token_stream_has_next_token(tokens):
break
css_syntax.state_transaction_commit(&transaction)
var longhand_ids_including_reset_only_longhands = ak.Vector[css_data.PropertyId]()
ak.vector_extend_2(&longhand_ids_including_reset_only_longhands, &longhand_ids)
ak.vector_extend_2(&longhand_ids_including_reset_only_longhands, &reset_only_longhand_ids)
var longhand_values = ak.Vector[CssStyleValue*]()
for longhand_id in ak.vector_span_const(&longhand_ids):
let values = ak.hash_map_get_k(&longhand_vectors, longhand_id) else trap("VERIFY: a vector for every longhand")
ak.vector_append_t(&longhand_values, css_style_value_list_of(*values, CssStyleValueListSeparator.comma))
for reset_only_longhand_id in ak.vector_span_const(&reset_only_longhand_ids):
ak.vector_append_t(&longhand_values, css_property_initial_value(reset_only_longhand_id))
return css_shorthand_of(shorthand_id, longhand_ids_including_reset_only_longhands, longhand_values)
# mark: The value of one of several properties ==================================================
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:159 Web::CSS::Parser::Parser::parse_css_value_for_property.
pub func css_parser_parse_css_value_for_property(this: CssParser*, property_id: css_data.PropertyId, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
let property_ids: css_data.PropertyId[1] = [property_id]
let property_and_value = css_parser_parse_css_value_for_properties(this, property_ids, tokens) else return none
return property_and_value.style_value
## The `any_property_accepts_type` lambda of parse_css_value_for_properties.
func css_any_property_accepts_type(property_ids: const css_data.PropertyId[], value_type: CssValueType) -> css_data.PropertyId?:
for property in property_ids:
if css_property_accepts_type(property, value_type):
return property
return none
## The `any_property_accepts_keyword` lambda of parse_css_value_for_properties.
func css_any_property_accepts_keyword(property_ids: const css_data.PropertyId[], keyword: css_data.Keyword) -> css_data.PropertyId?:
for property in property_ids:
if css_data.property_accepts_keyword(property, keyword):
return property
return none
## The `parse_for_type` lambda of parse_css_value_for_properties: a value of `type_` for the first property
## that accepts it, in that property's value-parsing context.
func css_parser_parse_for_type(this: CssParser*, property_ids: const css_data.PropertyId[], tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*,
type_: CssValueType) -> CssParserPropertyAndValue?:
if let property = css_any_property_accepts_type(property_ids, type_):
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.property_id(value = property))
defer css_parser_pop_temporary_value_parsing_context(this)
if let maybe_parsed_value = css_parser_parse_value(this, type_, tokens):
return css_property_and_value(property, maybe_parsed_value)
return none
## `property_accepted_ranges_by_value_type(property).get(type).value()`.
func css_accepted_range(ranges: const ak.HashMap[CssValueType, CssNumericRange, CssValueTypeTraits]*, type_: CssValueType) -> CssNumericRange:
return *(ak.hash_map_get_k_const(ranges, type_) else trap("VERIFY: an accepted range for the type"))
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:166 Web::CSS::Parser::Parser::parse_css_value_for_properties.
pub func css_parser_parse_css_value_for_properties(this: CssParser*, property_ids: const css_data.PropertyId[], tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssParserPropertyAndValue?:
# NB: The `any_property_accepts_type`, `any_property_accepts_keyword` and `parse_for_type` lambdas are
# css_any_property_accepts_type, css_any_property_accepts_keyword and css_parser_parse_for_type.
css_syntax.token_stream_discard_whitespace(tokens)
let peek_token = css_syntax.token_stream_next_token(tokens)
if css_syntax.component_value_is(&peek_token, .ident):
# NOTE: We do not try to parse "CSS-wide keywords" here. https://www.w3.org/TR/css-values-4/#common-keywords
# These are only valid on their own, and so should be parsed directly in `parse_css_value()`.
let ident = css_component_value_ident(&peek_token)
if let keyword = css_data.keyword_from_string(ak.fly_string_bytes_as_string_view(&ident)):
if let property = css_any_property_accepts_keyword(property_ids, keyword):
css_syntax.token_stream_discard_a_token(tokens)
if let resolved_keyword = css_data.resolve_legacy_value_alias(property, keyword):
return css_property_and_value(property, css_keyword_value_of(resolved_keyword))
return css_property_and_value(property, css_keyword_value_of(keyword))
# Custom idents
if let property = css_any_property_accepts_type(property_ids, CssValueType.custom_ident):
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.property_id(value = property))
defer css_parser_pop_temporary_value_parsing_context(this)
let blacklist = css_data.property_custom_ident_blacklist(property)
if let custom_ident = css_parser_parse_custom_ident_value(this, tokens, ak.vector_span_const(&blacklist)):
return css_property_and_value(property, custom_ident.css_style_value())
let types_before_numbers: CssValueType[26] = [
CssValueType.color,
CssValueType.corner_shape,
CssValueType.counter,
CssValueType.counter_style,
CssValueType.dashed_ident,
CssValueType.easing_function,
CssValueType.font_style,
CssValueType.font_variant_alternates,
CssValueType.font_variant_east_asian,
CssValueType.font_variant_ligatures,
CssValueType.font_variant_numeric,
CssValueType.image,
CssValueType.position,
CssValueType.background_position,
CssValueType.basic_shape,
CssValueType.ratio,
CssValueType.opacity_value,
CssValueType.opentype_tag,
CssValueType.rect,
CssValueType.scroll_function,
CssValueType.string,
CssValueType.transform_function,
CssValueType.transform_list,
CssValueType.url,
CssValueType.view_function,
CssValueType.view_timeline_inset,
]
for type_ in types_before_numbers:
if let parsed = css_parser_parse_for_type(this, property_ids, tokens, type_):
return parsed
# <integer>/<number> come before <length>, so that 0 is not interpreted as a <length> in case both are allowed.
if let property = css_any_property_accepts_type(property_ids, CssValueType.integer):
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.property_id(value = property))
defer css_parser_pop_temporary_value_parsing_context(this)
let ranges = css_property_accepted_ranges_by_value_type(property)
if let value = css_parser_parse_integer_value(this, tokens, css_accepted_range(&ranges, CssValueType.integer)):
return css_property_and_value(property, value)
if let property = css_any_property_accepts_type(property_ids, CssValueType.number):
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.property_id(value = property))
defer css_parser_pop_temporary_value_parsing_context(this)
let ranges = css_property_accepted_ranges_by_value_type(property)
if let value = css_parser_parse_number_value(this, tokens, css_accepted_range(&ranges, CssValueType.number)):
return css_property_and_value(property, value)
if let property = css_any_property_accepts_type(property_ids, CssValueType.angle):
let valid_ranges = css_property_accepted_ranges_by_value_type(property)
let angle_range = css_accepted_range(&valid_ranges, CssValueType.angle)
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.property_id(value = property))
defer css_parser_pop_temporary_value_parsing_context(this)
if css_property_resolves_percentages_relative_to(property) == CssValueType.angle:
if let value = css_parser_parse_angle_percentage_value(this, tokens, angle_range, css_accepted_range(&valid_ranges, CssValueType.percentage)):
return css_property_and_value(property, value)
if let value = css_parser_parse_angle_value(this, tokens, angle_range):
return css_property_and_value(property, value)
if let property = css_any_property_accepts_type(property_ids, CssValueType.flex):
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.property_id(value = property))
defer css_parser_pop_temporary_value_parsing_context(this)
let ranges = css_property_accepted_ranges_by_value_type(property)
if let value = css_parser_parse_flex_value(this, tokens, css_accepted_range(&ranges, CssValueType.flex)):
return css_property_and_value(property, value)
if let property = css_any_property_accepts_type(property_ids, CssValueType.frequency):
let valid_ranges = css_property_accepted_ranges_by_value_type(property)
let frequency_range = css_accepted_range(&valid_ranges, CssValueType.frequency)
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.property_id(value = property))
defer css_parser_pop_temporary_value_parsing_context(this)
if css_property_resolves_percentages_relative_to(property) == CssValueType.frequency:
if let value = css_parser_parse_frequency_percentage_value(this, tokens, frequency_range, css_accepted_range(&valid_ranges, CssValueType.percentage)):
return css_property_and_value(property, value)
if let value = css_parser_parse_frequency_value(this, tokens, frequency_range):
return css_property_and_value(property, value)
if let parsed = css_parser_parse_for_type(this, property_ids, tokens, CssValueType.fit_content):
return parsed
if let property = css_any_property_accepts_type(property_ids, CssValueType.length):
let valid_ranges = css_property_accepted_ranges_by_value_type(property)
let length_range = css_accepted_range(&valid_ranges, CssValueType.length)
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.property_id(value = property))
defer css_parser_pop_temporary_value_parsing_context(this)
if css_property_resolves_percentages_relative_to(property) == CssValueType.length:
if let value = css_parser_parse_length_percentage_value(this, tokens, length_range, css_accepted_range(&valid_ranges, CssValueType.percentage)):
return css_property_and_value(property, value)
if let value = css_parser_parse_length_value(this, tokens, length_range):
return css_property_and_value(property, value)
if let property = css_any_property_accepts_type(property_ids, CssValueType.resolution):
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.property_id(value = property))
defer css_parser_pop_temporary_value_parsing_context(this)
let ranges = css_property_accepted_ranges_by_value_type(property)
if let value = css_parser_parse_resolution_value(this, tokens, css_accepted_range(&ranges, CssValueType.resolution)):
return css_property_and_value(property, value)
if let property = css_any_property_accepts_type(property_ids, CssValueType.time):
let valid_ranges = css_property_accepted_ranges_by_value_type(property)
let time_range = css_accepted_range(&valid_ranges, CssValueType.time)
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.property_id(value = property))
defer css_parser_pop_temporary_value_parsing_context(this)
if css_property_resolves_percentages_relative_to(property) == CssValueType.time:
if let value = css_parser_parse_time_percentage_value(this, tokens, time_range, css_accepted_range(&valid_ranges, CssValueType.percentage)):
return css_property_and_value(property, value)
if let value = css_parser_parse_time_value(this, tokens, time_range):
return css_property_and_value(property, value)
# <percentage> is checked after the <foo-percentage> types.
if let property = css_any_property_accepts_type(property_ids, CssValueType.percentage):
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.property_id(value = property))
defer css_parser_pop_temporary_value_parsing_context(this)
let ranges = css_property_accepted_ranges_by_value_type(property)
if let value = css_parser_parse_percentage_value(this, tokens, css_accepted_range(&ranges, CssValueType.percentage)):
return css_property_and_value(property, value)
if let parsed = css_parser_parse_for_type(this, property_ids, tokens, CssValueType.paint):
return parsed
if let parsed = css_parser_parse_for_type(this, property_ids, tokens, CssValueType.anchor):
return parsed
return none