dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / parser / value_parsing_2.lucb
#==============================================================================================
#
# value_parsing_2 - parsing CSS values: numbers, percentages, anchor functions and the
# dimensions
#
# DESCRIPTION:
# Port of LibWeb/CSS/Parser/ValueParsing.cpp (lines 579-1191): <integer>, <number>,
# <percentage> and their combinations, anchor() and anchor-size(), and the dimension
# parsers (<angle>, <flex>, <frequency>, <length>, <resolution>, <time> and their
# -percentage forms) with the file's static parse_literal_*_value helpers. Each falls back
# to a math function through parse_calculated_value.
#
# Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
# (the full list of ValueParsing.cpp's copyright holders is in value_parsing_1)
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Calculation contexts ====================================================================
## The `CalculationContext { .percentages_resolve_as = ..., .resolve_numbers_as_integers = ...,
## .accepted_ranges_by_type = { { type, range } } }` designated initializers of this file.
func css_calculation_context_accepting(value_type: CssValueType, accepted_range: CssNumericRange, percentages_resolve_as: CssValueType? = none, resolve_numbers_as_integers: bool = false) -> CssCalculationContext:
var context = CssCalculationContext()
context.percentages_resolve_as = percentages_resolve_as
context.resolve_numbers_as_integers = resolve_numbers_as_integers
_ = ak.hash_map_set(&context.accepted_ranges_by_type, value_type, accepted_range)
return context
# mark: Numbers and percentages =================================================================
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:579 Web::CSS::Parser::Parser::parse_integer_value.
pub func css_parser_parse_integer_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_range: CssNumericRange) -> CssStyleValue*?:
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, .number):
let token = css_syntax.component_value_token(&peek_token)
if css_syntax.token_is_integer(&token) and css_numeric_range_contains(&accepted_range, f64(css_syntax.token_to_integer(&token))):
css_syntax.token_stream_discard_a_token(tokens) # integer
return css_integer_style_value_create(css_syntax.token_to_integer(&token)).css_style_value()
if let calc = css_parser_parse_calculated_value(this, peek_token, css_calculation_context_accepting(CssValueType.integer, accepted_range, resolve_numbers_as_integers = true)):
if css_calculated_style_value_resolves_to_number(calc):
css_syntax.token_stream_discard_a_token(tokens) # calc
return calc.css_style_value()
if let tree_counting_function = css_parser_parse_tree_counting_function(this, tokens, CssTreeCountingFunctionStyleValueComputedType.integer):
return tree_counting_function.css_style_value()
return none
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:600 Web::CSS::Parser::Parser::parse_number_value.
pub func css_parser_parse_number_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_range: CssNumericRange) -> CssStyleValue*?:
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, .number):
let token = css_syntax.component_value_token(&peek_token)
if css_numeric_range_contains(&accepted_range, css_syntax.token_number_value(&token)):
css_syntax.token_stream_discard_a_token(tokens) # number
return css_number_style_value_create(css_syntax.token_number_value(&token)).css_style_value()
if let calc = css_parser_parse_calculated_value(this, peek_token, css_calculation_context_accepting(CssValueType.number, accepted_range)):
if css_calculated_style_value_resolves_to_number(calc):
css_syntax.token_stream_discard_a_token(tokens) # calc
return calc.css_style_value()
if let tree_counting_function = css_parser_parse_tree_counting_function(this, tokens, CssTreeCountingFunctionStyleValueComputedType.number):
return tree_counting_function.css_style_value()
return none
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:621 Web::CSS::Parser::Parser::parse_number_percentage_value.
pub func css_parser_parse_number_percentage_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_number_range: CssNumericRange, accepted_percentage_range: CssNumericRange) -> CssStyleValue*?:
# Parses [<percentage> | <number>] (which is equivalent to [<alpha-value>])
if let value = css_parser_parse_number_value(this, tokens, accepted_number_range):
return value
if let value = css_parser_parse_percentage_value(this, tokens, accepted_percentage_range):
return value
return none
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:631 Web::CSS::Parser::Parser::parse_number_percentage_none_value.
pub func css_parser_parse_number_percentage_none_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# Parses [<percentage> | <number> | none] (which is equivalent to [<alpha-value> | none])
if let value = css_parser_parse_number_value(this, tokens, css_infinite_range):
return value
if let value = css_parser_parse_percentage_value(this, tokens, css_infinite_range):
return value
let next = css_syntax.token_stream_next_token(tokens)
if css_syntax.component_value_is_ident(&next, ak.sv("none")):
css_syntax.token_stream_discard_a_token(tokens) # keyword none
return css_keyword_style_value_create(css_data.Keyword.none_).css_style_value()
return none
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:647 Web::CSS::Parser::Parser::parse_percentage_value.
pub func css_parser_parse_percentage_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_range: CssNumericRange) -> CssStyleValue*?:
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, .percentage):
let token = css_syntax.component_value_token(&peek_token)
if css_numeric_range_contains(&accepted_range, css_syntax.token_percentage(&token)):
css_syntax.token_stream_discard_a_token(tokens) # percentage
return css_percentage_style_value_create(css_percentage_new(css_syntax.token_percentage(&token))).css_style_value()
if let calc = css_parser_parse_calculated_value(this, peek_token, css_calculation_context_accepting(CssValueType.percentage, accepted_range)):
if css_calculated_style_value_resolves_to_percentage(calc):
css_syntax.token_stream_discard_a_token(tokens) # calc
return calc.css_style_value()
return none
# mark: Anchor functions ========================================================================
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:666 Web::CSS::Parser::Parser::parse_anchor.
# https://drafts.csswg.org/css-anchor-position-1/#funcdef-anchor
pub func css_parser_parse_anchor(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# <anchor()> = anchor( <anchor-name>? && <anchor-side>, <length-percentage>? )
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
css_syntax.token_stream_discard_whitespace(tokens)
let function_token = css_syntax.token_stream_consume_a_token(tokens)
if not css_syntax.component_value_is_function_string_view(&function_token, ak.sv("anchor")):
return none
let function = css_syntax.component_value_function(&function_token)
var argument_tokens = css_syntax.token_stream_create(function.value)
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.css_function_context(CssFunctionContext(name = ak.fly_string_bytes_as_string_view(&function.name))))
defer css_parser_pop_temporary_value_parsing_context(this)
var anchor_name: ak.FlyString? = none
var anchor_side_value: CssStyleValue*? = none
var fallback_value: CssStyleValue*? = none
for i in 0..<2:
_ = i
css_syntax.token_stream_discard_whitespace(&argument_tokens)
# <anchor-name> = <dashed-ident>
if let dashed_ident = css_parser_parse_dashed_ident(this, &argument_tokens):
if anchor_name != none:
return none
anchor_name = dashed_ident
continue
if anchor_side_value != none:
break
# <anchor-side> = inside | outside
# | top | left | right | bottom
# | start | end | self-start | self-end
# | <percentage> | center
anchor_side_value = css_parser_parse_keyword_value(this, &argument_tokens)
if let keyword_value = anchor_side_value:
if css_data.keyword_to_anchor_side(css_style_value_to_keyword(keyword_value)) == none:
return none
else:
# FIXME: Only percentages are allowed here, but we parse a length-percentage so that calc values are handled.
anchor_side_value = css_parser_parse_length_percentage_value(this, &argument_tokens, css_infinite_range, css_infinite_range)
let length_percentage = anchor_side_value else return none
if css_style_value_is_length(length_percentage):
return none
let after_arguments = css_syntax.token_stream_next_token(&argument_tokens)
if css_syntax.component_value_is(&after_arguments, .comma):
css_syntax.token_stream_discard_a_token(&argument_tokens)
css_syntax.token_stream_discard_whitespace(&argument_tokens)
fallback_value = css_parser_parse_length_percentage_value(this, &argument_tokens, css_infinite_range, css_infinite_range)
if fallback_value == none:
fallback_value = css_parser_parse_anchor(this, &argument_tokens)
if fallback_value == none:
return none
css_syntax.token_stream_discard_whitespace(&argument_tokens)
if css_syntax.token_stream_has_next_token(&argument_tokens):
return none
let anchor_side = anchor_side_value else return none
css_syntax.state_transaction_commit(&transaction)
return css_anchor_style_value_create(anchor_name, anchor_side, fallback_value).css_style_value()
## The `static Array allowed_property_ids` of parse_anchor_size: the accepted @position-try properties.
let css_anchor_size_allowed_property_ids: css_data.PropertyId[37] = [
# inset properties
css_data.PropertyId.inset,
css_data.PropertyId.top,
css_data.PropertyId.right,
css_data.PropertyId.bottom,
css_data.PropertyId.left,
css_data.PropertyId.inset_block,
css_data.PropertyId.inset_block_start,
css_data.PropertyId.inset_block_end,
css_data.PropertyId.inset_inline,
css_data.PropertyId.inset_inline_start,
css_data.PropertyId.inset_inline_end,
# margin properties
css_data.PropertyId.margin,
css_data.PropertyId.margin_top,
css_data.PropertyId.margin_right,
css_data.PropertyId.margin_bottom,
css_data.PropertyId.margin_left,
css_data.PropertyId.margin_block,
css_data.PropertyId.margin_block_start,
css_data.PropertyId.margin_block_end,
css_data.PropertyId.margin_inline,
css_data.PropertyId.margin_inline_start,
css_data.PropertyId.margin_inline_end,
# sizing properties
css_data.PropertyId.width,
css_data.PropertyId.min_width,
css_data.PropertyId.max_width,
css_data.PropertyId.height,
css_data.PropertyId.min_height,
css_data.PropertyId.max_height,
css_data.PropertyId.block_size,
css_data.PropertyId.min_block_size,
css_data.PropertyId.max_block_size,
css_data.PropertyId.inline_size,
css_data.PropertyId.min_inline_size,
css_data.PropertyId.max_inline_size,
# self-alignment properties
css_data.PropertyId.align_self,
css_data.PropertyId.justify_self,
css_data.PropertyId.place_self,
# FIXME: position-anchor
# FIXME: position-area
]
## `allowed_property_ids.contains_slow(property_id)`.
func css_anchor_size_property_is_allowed(property_id: css_data.PropertyId) -> bool:
for allowed in css_anchor_size_allowed_property_ids:
if allowed == property_id:
return true
return false
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:736 Web::CSS::Parser::Parser::parse_anchor_size.
# https://drafts.csswg.org/css-anchor-position-1/#sizing
pub func css_parser_parse_anchor_size(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# anchor-size() = anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let function_token = css_syntax.token_stream_consume_a_token(tokens)
if not css_syntax.component_value_is_function_string_view(&function_token, ak.sv("anchor-size")):
return none
# It is only allowed in the accepted @position-try properties (and is otherwise invalid).
var valid_property_context = false
for value_context in ak.vector_span_const(&this.m_value_context):
match value_context:
.property_id(property_id):
if not css_anchor_size_property_is_allowed(property_id):
valid_property_context = false
break
valid_property_context = true
_:
continue
if not valid_property_context:
return none
let function = css_syntax.component_value_function(&function_token)
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.css_function_context(CssFunctionContext(name = ak.fly_string_bytes_as_string_view(&function.name))))
defer css_parser_pop_temporary_value_parsing_context(this)
var argument_tokens = css_syntax.token_stream_create(function.value)
var anchor_name: ak.FlyString? = none
var anchor_size: css_data.AnchorSize? = none
var fallback_value: CssStyleValue*? = none
# Parse optional anchor name and anchor size in arbitrary order.
for i in 0..<2:
_ = i
css_syntax.token_stream_discard_whitespace(&argument_tokens)
let peek_token = css_syntax.token_stream_next_token(&argument_tokens)
if not css_syntax.component_value_is(&peek_token, .ident):
break
# <anchor-name> = <dashed-ident>
if let dashed_ident = css_parser_parse_dashed_ident(this, &argument_tokens):
if anchor_name != none:
return none
anchor_name = dashed_ident
continue
# <anchor-size> = width | height | block | inline | self-block | self-inline
let ident = css_component_value_ident(&peek_token)
let keyword = css_data.keyword_from_string(ak.fly_string_bytes_as_string_view(&ident)) else return none
let maybe_anchor_size = css_data.keyword_to_anchor_size(keyword)
if maybe_anchor_size == none or anchor_size != none:
return none
css_syntax.token_stream_discard_a_token(&argument_tokens)
anchor_size = maybe_anchor_size
css_syntax.token_stream_discard_whitespace(&argument_tokens)
let has_name_or_size = anchor_name != none or anchor_size != none
var comma_present = false
let after_name_and_size = css_syntax.token_stream_next_token(&argument_tokens)
if css_syntax.component_value_is(&after_name_and_size, .comma):
if not has_name_or_size:
return none
comma_present = true
css_syntax.token_stream_discard_a_token(&argument_tokens)
css_syntax.token_stream_discard_whitespace(&argument_tokens)
# FIXME: Nested anchor sizes should actually be handled by parse_length_percentage()
if let nested_anchor_size = css_parser_parse_anchor_size(this, &argument_tokens):
fallback_value = nested_anchor_size
elif let length_percentage = css_parser_parse_length_percentage_value(this, &argument_tokens, css_infinite_range, css_infinite_range):
fallback_value = length_percentage
if fallback_value == none and comma_present:
return none
if fallback_value != none and not comma_present and has_name_or_size:
return none
if css_syntax.token_stream_has_next_token(&argument_tokens):
return none
css_syntax.state_transaction_commit(&transaction)
return css_anchor_size_style_value_create(anchor_name, anchor_size, fallback_value).css_style_value()
# mark: Dimensions ==============================================================================
## `tokens.consume_a_token().token()` of a component value known to be a token.
func css_consume_a_token_token(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> css_syntax.Token:
let component_value = css_syntax.token_stream_consume_a_token(tokens)
return css_syntax.component_value_token(&component_value)
## `tokens.next_token().is(type)`.
func css_next_token_is(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, type_: css_syntax.TokenType) -> bool:
let next = css_syntax.token_stream_next_token(tokens)
return css_syntax.component_value_is(&next, type_)
## `tokens.next_token().is_ident(ident)`.
func css_next_token_is_ident(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, ident: str) -> bool:
let next = css_syntax.token_stream_next_token(tokens)
return css_syntax.component_value_is_ident(&next, ak.sv(ident))
## `tokens.next_token().is_function(name)`.
func css_next_token_is_function(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, name: str) -> bool:
let next = css_syntax.token_stream_next_token(tokens)
return css_syntax.component_value_is_function_string_view(&next, ak.sv(name))
## `parse_calculated_value(tokens.consume_a_token(), context)` of a calc fallback in a transaction: the value if it
## resolves as `resolves` says, committing the transaction; none (and the transaction reverts) otherwise.
func css_parser_parse_calculated_fallback(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, context: CssCalculationContext, resolves: func(CssCalculatedStyleValue*) -> bool) -> CssStyleValue*?:
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
if let calc = css_parser_parse_calculated_value(this, css_syntax.token_stream_consume_a_token(tokens), context):
if resolves(calc):
css_syntax.state_transaction_commit(&transaction)
return calc.css_style_value()
return none
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:865 Web::CSS::Parser::parse_literal_angle_value.
func css_parse_literal_angle_value(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, is_parsing_svg_presentation_attribute: bool, accepted_range: CssNumericRange) -> CssAngleStyleValue*?:
css_syntax.token_stream_discard_whitespace(tokens)
if css_next_token_is(tokens, .dimension):
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let dimension_token = css_consume_a_token_token(tokens)
let unit_ = css_syntax.token_dimension_unit(&dimension_token)
if let angle_type = css_data.string_to_angle_unit(ak.fly_string_bytes_as_string_view(&unit_)):
let angle = css_angle_new(css_syntax.token_dimension_value(&dimension_token), angle_type)
if not css_numeric_range_contains(&accepted_range, css_angle_to_degrees(&angle)):
return none
css_syntax.state_transaction_commit(&transaction)
return css_angle_style_value_create(angle)
return none
# https://svgwg.org/svg2-draft/types.html#presentation-attribute-css-value
# When parsing an SVG attribute, an angle is allowed without a unit.
# FIXME: How should these numbers be interpreted? https://github.com/w3c/svgwg/issues/792
# For now: Convert to an angle in degrees.
if css_next_token_is(tokens, .number) and is_parsing_svg_presentation_attribute:
let number_token = css_consume_a_token_token(tokens)
let angle = css_angle_make_degrees(css_syntax.token_number_value(&number_token))
if not css_numeric_range_contains(&accepted_range, css_angle_to_degrees(&angle)):
return none
return css_angle_style_value_create(angle)
return none
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:900 Web::CSS::Parser::parse_literal_percentage_value.
func css_parse_literal_percentage_value(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_range: CssNumericRange) -> CssPercentageStyleValue*?:
css_syntax.token_stream_discard_whitespace(tokens)
if css_next_token_is(tokens, .percentage):
let next = css_syntax.token_stream_next_token(tokens)
let next_token = css_syntax.component_value_token(&next)
if css_numeric_range_contains(&accepted_range, css_syntax.token_percentage(&next_token)):
let percentage_token = css_consume_a_token_token(tokens)
return css_percentage_style_value_create(css_percentage_new(css_syntax.token_percentage(&percentage_token)))
return none
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:910 Web::CSS::Parser::Parser::parse_angle_value.
pub func css_parser_parse_angle_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_range: CssNumericRange) -> CssStyleValue*?:
if let literal_angle = css_parse_literal_angle_value(tokens, css_parser_is_parsing_svg_presentation_attribute(this), accepted_range):
return literal_angle.css_style_value()
return css_parser_parse_calculated_fallback(this, tokens, css_calculation_context_accepting(CssValueType.angle, accepted_range), css_calculated_style_value_resolves_to_angle)
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:923 Web::CSS::Parser::Parser::parse_angle_percentage_value.
pub func css_parser_parse_angle_percentage_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_angle_range: CssNumericRange, accepted_percentage_range: CssNumericRange) -> CssStyleValue*?:
if let literal_angle = css_parse_literal_angle_value(tokens, css_parser_is_parsing_svg_presentation_attribute(this), accepted_angle_range):
return literal_angle.css_style_value()
if let literal_percentage = css_parse_literal_percentage_value(tokens, accepted_percentage_range):
return literal_percentage.css_style_value()
return css_parser_parse_calculated_fallback(this, tokens, css_calculation_context_accepting(CssValueType.angle, accepted_angle_range, percentages_resolve_as = CssValueType.angle), css_calculated_style_value_resolves_to_angle_percentage)
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:939 Web::CSS::Parser::Parser::parse_flex_value.
pub func css_parser_parse_flex_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_range: CssNumericRange) -> CssStyleValue*?:
css_syntax.token_stream_discard_whitespace(tokens)
if css_next_token_is(tokens, .dimension):
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let dimension_token = css_consume_a_token_token(tokens)
let unit_ = css_syntax.token_dimension_unit(&dimension_token)
if let flex_type = css_data.string_to_flex_unit(ak.fly_string_bytes_as_string_view(&unit_)):
let flex = css_flex_new(css_syntax.token_dimension_value(&dimension_token), flex_type)
if not css_numeric_range_contains(&accepted_range, css_flex_to_fr(&flex)):
return none
css_syntax.state_transaction_commit(&transaction)
return css_flex_style_value_create(flex).css_style_value()
return none
return css_parser_parse_calculated_fallback(this, tokens, css_calculation_context_accepting(CssValueType.flex, accepted_range), css_calculated_style_value_resolves_to_flex)
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:966 Web::CSS::Parser::parse_literal_frequency_value.
func css_parse_literal_frequency_value(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_range: CssNumericRange) -> CssFrequencyStyleValue*?:
css_syntax.token_stream_discard_whitespace(tokens)
if css_next_token_is(tokens, .dimension):
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let dimension_token = css_consume_a_token_token(tokens)
let unit_ = css_syntax.token_dimension_unit(&dimension_token)
if let frequency_type = css_data.string_to_frequency_unit(ak.fly_string_bytes_as_string_view(&unit_)):
let frequency = css_frequency_new(css_syntax.token_dimension_value(&dimension_token), frequency_type)
if not css_numeric_range_contains(&accepted_range, css_frequency_to_hertz(&frequency)):
return none
css_syntax.state_transaction_commit(&transaction)
return css_frequency_style_value_create(frequency)
return none
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:987 Web::CSS::Parser::Parser::parse_frequency_value.
pub func css_parser_parse_frequency_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_range: CssNumericRange) -> CssStyleValue*?:
if let literal_frequency = css_parse_literal_frequency_value(tokens, accepted_range):
return literal_frequency.css_style_value()
return css_parser_parse_calculated_fallback(this, tokens, css_calculation_context_accepting(CssValueType.frequency, accepted_range), css_calculated_style_value_resolves_to_frequency)
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:1000 Web::CSS::Parser::Parser::parse_frequency_percentage_value.
pub func css_parser_parse_frequency_percentage_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_frequency_range: CssNumericRange, accepted_percentage_range: CssNumericRange) -> CssStyleValue*?:
if let literal_frequency = css_parse_literal_frequency_value(tokens, accepted_frequency_range):
return literal_frequency.css_style_value()
if let literal_percentage = css_parse_literal_percentage_value(tokens, accepted_percentage_range):
return literal_percentage.css_style_value()
return css_parser_parse_calculated_fallback(this, tokens, css_calculation_context_accepting(CssValueType.frequency, accepted_frequency_range, percentages_resolve_as = CssValueType.frequency), css_calculated_style_value_resolves_to_frequency_percentage)
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:1016 Web::CSS::Parser::parse_literal_length_value.
func css_parse_literal_length_value(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, context_allows_quirky_length: bool, is_parsing_svg_presentation_attribute: bool, accepted_range: CssNumericRange) -> CssLengthStyleValue*?:
css_syntax.token_stream_discard_whitespace(tokens)
if css_next_token_is(tokens, .dimension):
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let dimension_token = css_consume_a_token_token(tokens)
let unit_ = css_syntax.token_dimension_unit(&dimension_token)
if let length_type = css_data.string_to_length_unit(ak.fly_string_bytes_as_string_view(&unit_)):
let length = css_length_new(css_syntax.token_dimension_value(&dimension_token), length_type)
# NB: Since we can't convert font/viewport relative lengths to their canonical units at parse time it
# doesn't make sense to have non-zero/non-infinite bounds for lengths
assert(accepted_range.min == f64(ak.f32_lowest) or accepted_range.min == 0.0)
assert(accepted_range.max == f64(ak.f32_max) or accepted_range.max == 0.0)
if not css_numeric_range_contains(&accepted_range, css_length_raw_value(&length)):
return none
css_syntax.state_transaction_commit(&transaction)
return css_length_style_value_create(length)
return none
if css_next_token_is(tokens, .number):
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let number_token = css_consume_a_token_token(tokens)
let numeric_value = css_syntax.token_number_value(&number_token)
if numeric_value == 0.0:
if not css_numeric_range_contains(&accepted_range, 0.0):
return none
css_syntax.state_transaction_commit(&transaction)
return css_length_style_value_create(css_length_make_px_f64(0.0))
if context_allows_quirky_length:
let nearest_value = gfx.css_pixels_nearest_value_for_f64(numeric_value)
if not css_numeric_range_contains(&accepted_range, nearest_value.to_double()):
return none
css_syntax.state_transaction_commit(&transaction)
return css_length_style_value_create(css_length_make_px_css_pixels(nearest_value))
# https://svgwg.org/svg2-draft/types.html#presentation-attribute-css-value
# When parsing an SVG attribute, a length is allowed without a unit.
# FIXME: How should these numbers be interpreted? https://github.com/w3c/svgwg/issues/792
# For now: Convert to a length in pixels.
if is_parsing_svg_presentation_attribute:
let nearest_value = gfx.css_pixels_nearest_value_for_f64(numeric_value)
if not css_numeric_range_contains(&accepted_range, nearest_value.to_double()):
return none
css_syntax.state_transaction_commit(&transaction)
return css_length_style_value_create(css_length_make_px_css_pixels(nearest_value))
return none
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:1078 Web::CSS::Parser::Parser::parse_length_value.
pub func css_parser_parse_length_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_range: CssNumericRange) -> CssStyleValue*?:
if let literal_length = css_parse_literal_length_value(tokens, css_parser_context_allows_quirky_length(this), css_parser_is_parsing_svg_presentation_attribute(this), accepted_range):
return literal_length.css_style_value()
if css_next_token_is_function(tokens, "anchor-size"):
return css_parser_parse_anchor_size(this, tokens)
return css_parser_parse_calculated_fallback(this, tokens, css_calculation_context_accepting(CssValueType.length, accepted_range), css_calculated_style_value_resolves_to_length)
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:1094 Web::CSS::Parser::Parser::parse_length_percentage_value.
pub func css_parser_parse_length_percentage_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_length_range: CssNumericRange, accepted_percentage_range: CssNumericRange) -> CssStyleValue*?:
if let literal_length = css_parse_literal_length_value(tokens, css_parser_context_allows_quirky_length(this), css_parser_is_parsing_svg_presentation_attribute(this), accepted_length_range):
return literal_length.css_style_value()
if let literal_percentage = css_parse_literal_percentage_value(tokens, accepted_percentage_range):
return literal_percentage.css_style_value()
if css_next_token_is_function(tokens, "anchor-size"):
return css_parser_parse_anchor_size(this, tokens)
return css_parser_parse_calculated_fallback(this, tokens, css_calculation_context_accepting(CssValueType.length, accepted_length_range, percentages_resolve_as = CssValueType.length), css_calculated_style_value_resolves_to_length_percentage)
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:1113 Web::CSS::Parser::Parser::parse_resolution_value.
pub func css_parser_parse_resolution_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_range: CssNumericRange) -> CssStyleValue*?:
css_syntax.token_stream_discard_whitespace(tokens)
if css_next_token_is(tokens, .dimension):
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let dimension_token = css_consume_a_token_token(tokens)
let unit_ = css_syntax.token_dimension_unit(&dimension_token)
if let resolution_type = css_data.string_to_resolution_unit(ak.fly_string_bytes_as_string_view(&unit_)):
let resolution = css_resolution_new(css_syntax.token_dimension_value(&dimension_token), resolution_type)
# The allowed range of <resolution> values always excludes negative values, in addition to any explicit
# ranges that might be specified.
# https://drafts.csswg.org/css-values-4/#resolution
if css_syntax.token_dimension_value(&dimension_token) < 0.0 or not css_numeric_range_contains(&accepted_range, css_resolution_to_dots_per_pixel(&resolution)):
return none
css_syntax.state_transaction_commit(&transaction)
return css_resolution_style_value_create(resolution).css_style_value()
return none
return css_parser_parse_calculated_fallback(this, tokens, css_calculation_context_accepting(CssValueType.resolution, accepted_range), css_calculated_style_value_resolves_to_resolution)
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:1143 Web::CSS::Parser::parse_literal_time_value.
func css_parse_literal_time_value(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_range: CssNumericRange) -> CssTimeStyleValue*?:
css_syntax.token_stream_discard_whitespace(tokens)
if css_next_token_is(tokens, .dimension):
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let dimension_token = css_consume_a_token_token(tokens)
let unit_ = css_syntax.token_dimension_unit(&dimension_token)
if let time_type = css_data.string_to_time_unit(ak.fly_string_bytes_as_string_view(&unit_)):
let time = css_time_new(css_syntax.token_dimension_value(&dimension_token), time_type)
if not css_numeric_range_contains(&accepted_range, css_time_to_seconds(&time)):
return none
css_syntax.state_transaction_commit(&transaction)
return css_time_style_value_create(time)
return none
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:1164 Web::CSS::Parser::Parser::parse_time_value.
pub func css_parser_parse_time_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_range: CssNumericRange) -> CssStyleValue*?:
if let literal_time = css_parse_literal_time_value(tokens, accepted_range):
return literal_time.css_style_value()
return css_parser_parse_calculated_fallback(this, tokens, css_calculation_context_accepting(CssValueType.time, accepted_range), css_calculated_style_value_resolves_to_time)
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:1177 Web::CSS::Parser::Parser::parse_time_percentage_value.
pub func css_parser_parse_time_percentage_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, accepted_time_range: CssNumericRange, accepted_percentage_range: CssNumericRange) -> CssStyleValue*?:
if let literal_time = css_parse_literal_time_value(tokens, accepted_time_range):
return literal_time.css_style_value()
if let literal_percentage = css_parse_literal_percentage_value(tokens, accepted_percentage_range):
return literal_percentage.css_style_value()
return css_parser_parse_calculated_fallback(this, tokens, css_calculation_context_accepting(CssValueType.time, accepted_time_range, percentages_resolve_as = CssValueType.time), css_calculated_style_value_resolves_to_time_percentage)