dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / parser / value_parsing_6.lucb
#==============================================================================================
#
# value_parsing_6 - parsing CSS values: <image>, image-set(), <paint> and <position>
#
# DESCRIPTION:
# Port of LibWeb/CSS/Parser/ValueParsing.cpp (lines 2666-3132): <image> (url(), image-set()
# and the gradients), image-set() options, SVG's <paint> and <position> with its five
# alternatives (the C++ lambdas are the functions before parse_position_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: <image> =================================================================================
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2666 Web::CSS::Parser::Parser::parse_image_value.
pub func css_parser_parse_image_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssAbstractImageStyleValue*?:
return css_parser_parse_image_value_css_parser_allow_image_set(this, tokens, CssParserAllowImageSet.yes)
## `option_tokens_list.first_matching([](auto const& component_value) { return component_value.contains_attr_tainted_value(); }).has_value()`.
func css_component_values_contain_attr_tainted_value(component_values: const ak.Vector[css_syntax.ComponentValue]*) -> bool:
for component_value in ak.vector_span_const(component_values):
if css_syntax.component_value_contains_attr_tainted_value(&component_value):
return true
return false
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2671 Web::CSS::Parser::Parser::parse_image_set_function.
pub func css_parser_parse_image_set_function(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssImageSetStyleValue*?:
css_syntax.token_stream_discard_whitespace(tokens)
if not css_next_token_is_function(tokens, "image-set") and not css_next_token_is_function(tokens, "-webkit-image-set"):
return none
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)
let function = css_syntax.component_value_function(&function_token)
var function_tokens = css_syntax.token_stream_create(function.value)
let image_set_options_tokens = css_parser_parse_a_comma_separated_list_of_component_values(this, &function_tokens)
css_syntax.token_stream_discard_whitespace(&function_tokens)
if not css_syntax.token_stream_is_empty(&function_tokens):
return none
var options = ak.Vector[CssImageSetStyleValueOption]()
ak.vector_ensure_capacity(&options, ak.vector_size(&image_set_options_tokens))
for option_tokens_list in ak.vector_span_const(&image_set_options_tokens):
if css_component_values_contain_attr_tainted_value(&option_tokens_list):
return none
var option_tokens = css_syntax.token_stream_create(option_tokens_list)
css_syntax.token_stream_discard_whitespace(&option_tokens)
var image: CssAbstractImageStyleValue*? = none
if css_next_token_is(&option_tokens, .string):
let string_token = css_consume_a_token_token(&option_tokens)
let string = css_syntax.token_string(&string_token)
let url = css_url_new(ak.fly_string_to_string_const(&string))
image = css_image_style_value_create_css_url(url).css_abstract_image_style_value()
else:
image = css_parser_parse_image_value_css_parser_allow_image_set(this, &option_tokens, CssParserAllowImageSet.no)
let option_image = image else return none
var resolution: CssStyleValue*? = none
var type_: ak.String? = none
while true:
css_syntax.token_stream_discard_whitespace(&option_tokens)
if css_syntax.token_stream_is_empty(&option_tokens):
break
if resolution == none:
if let parsed_resolution = css_parser_parse_resolution_value(this, &option_tokens, css_infinite_range):
resolution = parsed_resolution
continue
if type_ == none and css_next_token_is_function(&option_tokens, "type"):
let type_function_token = css_syntax.token_stream_consume_a_token(&option_tokens)
let type_function = css_syntax.component_value_function(&type_function_token)
var type_tokens = css_syntax.token_stream_create(type_function.value)
css_syntax.token_stream_discard_whitespace(&type_tokens)
if not css_next_token_is(&type_tokens, .string):
return none
let type_string_token = css_consume_a_token_token(&type_tokens)
let type_string = css_syntax.token_string(&type_string_token)
type_ = ak.fly_string_to_string_const(&type_string)
css_syntax.token_stream_discard_whitespace(&type_tokens)
if not css_syntax.token_stream_is_empty(&type_tokens):
return none
continue
return none
if resolution == none:
resolution = css_resolution_style_value_create(css_resolution_new(1.0, css_data.ResolutionUnit.x)).css_style_value()
ak.vector_unchecked_append_u(&options, CssImageSetStyleValueOption(
image = option_image,
resolution = resolution else trap("VERIFY: a resolution"),
type_ = type_,
))
if ak.vector_is_empty(&options):
return none
css_syntax.state_transaction_commit(&transaction)
return css_image_set_style_value_create(options)
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2752 Web::CSS::Parser::Parser::parse_image_value.
pub func css_parser_parse_image_value_css_parser_allow_image_set(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, allow_image_set: CssParserAllowImageSet) -> CssAbstractImageStyleValue*?:
css_syntax.token_stream_mark(tokens)
if let url = css_parser_parse_url_function(this, tokens):
# If the value is a 'url(..)' parse as image, but if it is just a reference 'url(#xx)', leave it alone,
# so we can parse as URL further on. These URLs are used as references inside SVG documents for masks.
# FIXME: Remove this special case once mask-image accepts `<image>`.
let url_string = css_url_url(&url)
let url_view = ak.string_bytes_as_string_view(&url_string)
if not ak.string_view_starts_with_u8(&url_view, '#'):
css_syntax.token_stream_discard_a_mark(tokens)
return css_image_style_value_create_css_url(url).css_abstract_image_style_value()
css_syntax.token_stream_restore_a_mark(tokens)
return none
css_syntax.token_stream_discard_a_mark(tokens)
if allow_image_set == CssParserAllowImageSet.yes:
if let image_set = css_parser_parse_image_set_function(this, tokens):
return image_set.css_abstract_image_style_value()
if let linear_gradient = css_parser_parse_linear_gradient_function(this, tokens):
return linear_gradient.css_abstract_image_style_value()
if let conic_gradient = css_parser_parse_conic_gradient_function(this, tokens):
return conic_gradient.css_abstract_image_style_value()
if let radial_gradient = css_parser_parse_radial_gradient_function(this, tokens):
return radial_gradient.css_abstract_image_style_value()
return none
# mark: <paint> =================================================================================
## The result of parse_paint_value's `parse_color_or_none` lambda, an `Optional<RefPtr<StyleValue const>>`:
## nothing found (an empty Optional), an invalid value (a null RefPtr) or the value.
enum CssColorOrNone:
not_found
invalid
value(value: CssStyleValue*)
## The `parse_color_or_none` lambda of parse_paint_value.
func css_parser_parse_color_or_none(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssColorOrNone:
if let color = css_parser_parse_color_value(this, tokens):
return CssColorOrNone.value(color)
# NOTE: <color> also accepts identifiers, so we do this identifier check last.
if css_next_token_is(tokens, .ident):
let next = css_syntax.token_stream_next_token(tokens)
let ident = css_component_value_ident(&next)
if let maybe_keyword = css_data.keyword_from_string(ak.fly_string_bytes_as_string_view(&ident)):
# FIXME: Accept `context-fill` and `context-stroke`
match maybe_keyword:
.none_:
css_syntax.token_stream_discard_a_token(tokens)
return CssColorOrNone.value(css_keyword_style_value_create(maybe_keyword).css_style_value())
_:
return CssColorOrNone.invalid
return CssColorOrNone.not_found
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2787 Web::CSS::Parser::Parser::parse_paint_value.
# https://svgwg.org/svg2-draft/painting.html#SpecifyingPaint
pub func css_parser_parse_paint_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# `<paint> = none | <color> | <url> [none | <color>]? | context-fill | context-stroke`
# FIXME: Allow context-fill/context-stroke here
match css_parser_parse_color_or_none(this, tokens):
.value(value): return value
.invalid: return none
.not_found: _ = this
if let url = css_parser_parse_url_value(this, tokens):
css_syntax.token_stream_discard_whitespace(tokens)
match css_parser_parse_color_or_none(this, tokens):
.invalid:
# Fail to parse if the fallback is invalid, but otherwise ignore it.
return none
.value(color_or_none):
if css_style_value_has_color(color_or_none):
return css_url_style_value_create(css_url_style_value_url(css_style_value_as_url(url.css_style_value())), color_or_none).css_style_value()
.not_found:
_ = url
return url.css_style_value()
return none
# mark: <position> ==============================================================================
## The `parse_position_edge` lambda of parse_position_value.
func css_parse_position_edge(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> css_data.PositionEdge?:
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let token = css_syntax.token_stream_consume_a_token(tokens)
if not css_syntax.component_value_is(&token, .ident):
return none
let ident = css_component_value_ident(&token)
let keyword = css_data.keyword_from_string(ak.fly_string_bytes_as_string_view(&ident)) else return none
css_syntax.state_transaction_commit(&transaction)
return css_data.keyword_to_position_edge(keyword)
## The `is_horizontal` lambda of parse_position_value.
func css_position_edge_is_horizontal(edge: css_data.PositionEdge, accept_center: bool) -> bool:
match edge:
.left, .right: return true
.center: return accept_center
_: return false
## The `is_vertical` lambda of parse_position_value.
func css_position_edge_is_vertical(edge: css_data.PositionEdge, accept_center: bool) -> bool:
match edge:
.top, .bottom: return true
.center: return accept_center
_: return false
## `EdgeStyleValue::create(edge, {})`.
func css_edge_only(edge: css_data.PositionEdge) -> CssEdgeStyleValue*:
return css_edge_style_value_create(edge, none)
## The `alternative_1` lambda of parse_position_value: [ left | center | right | top | bottom | <length-percentage> ].
func css_parser_parse_position_alternative_1(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssPositionStyleValue*?:
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
css_syntax.token_stream_discard_whitespace(tokens)
# [ left | center | right | top | bottom ]
if let edge = css_parse_position_edge(tokens):
css_syntax.state_transaction_commit(&transaction)
# [ left | right ]
if css_position_edge_is_horizontal(edge, false):
return css_position_style_value_create(css_edge_only(edge), css_edge_only(css_data.PositionEdge.center))
# [ top | bottom ]
if css_position_edge_is_vertical(edge, false):
return css_position_style_value_create(css_edge_only(css_data.PositionEdge.center), css_edge_only(edge))
# [ center ]
assert(edge == css_data.PositionEdge.center)
return css_position_style_value_create(css_edge_only(css_data.PositionEdge.center), css_edge_only(css_data.PositionEdge.center))
# [ <length-percentage> ]
if let maybe_percentage = css_parser_parse_length_percentage_value(this, tokens, css_infinite_range, css_infinite_range):
css_syntax.state_transaction_commit(&transaction)
return css_position_style_value_create(css_edge_style_value_create(none, maybe_percentage), css_edge_only(css_data.PositionEdge.center))
return none
## The `alternative_2` lambda of parse_position_value: [ left | center | right ] && [ top | center | bottom ].
func css_parser_parse_position_alternative_2(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssPositionStyleValue*?:
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
css_syntax.token_stream_discard_whitespace(tokens)
# Parse out two position edges
var first_edge = css_parse_position_edge(tokens) else return none
css_syntax.token_stream_discard_whitespace(tokens)
var second_edge = css_parse_position_edge(tokens) else return none
# If 'left' or 'right' is given, that position is X and the other is Y.
# Conversely -
# If 'top' or 'bottom' is given, that position is Y and the other is X.
if css_position_edge_is_vertical(first_edge, false) or css_position_edge_is_horizontal(second_edge, false):
let swapped = first_edge
first_edge = second_edge
second_edge = swapped
# [ left | center | right ] [ top | bottom | center ]
if css_position_edge_is_horizontal(first_edge, true) and css_position_edge_is_vertical(second_edge, true):
css_syntax.state_transaction_commit(&transaction)
return css_position_style_value_create(css_edge_only(first_edge), css_edge_only(second_edge))
return none
## The `parse_position_or_length` lambda of alternative_3.
func css_parser_parse_position_or_length(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, as_horizontal: bool) -> CssEdgeStyleValue*?:
css_syntax.token_stream_discard_whitespace(tokens)
if let position = css_parse_position_edge(tokens):
let valid = css_position_edge_is_horizontal(position, true) if as_horizontal else css_position_edge_is_vertical(position, true)
if not valid:
return none
return css_edge_only(position)
let maybe_length = css_parser_parse_length_percentage_value(this, tokens, css_infinite_range, css_infinite_range) else return none
return css_edge_style_value_create(none, maybe_length)
## The `alternative_3` lambda of parse_position_value:
## [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ].
func css_parser_parse_position_alternative_3(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssPositionStyleValue*?:
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
# [ left | center | right | <length-percentage> ]
let horizontal_edge = css_parser_parse_position_or_length(this, tokens, true) else return none
# [ top | center | bottom | <length-percentage> ]
let vertical_edge = css_parser_parse_position_or_length(this, tokens, false) else return none
css_syntax.state_transaction_commit(&transaction)
return css_position_style_value_create(horizontal_edge, vertical_edge)
## The `PositionAndLength` struct of alternative_4.
struct CssPositionAndLength:
var position: css_data.PositionEdge
var length: CssStyleValue*
## The `parse_position_and_length` lambda of alternative_4.
func css_parser_parse_position_and_length(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssPositionAndLength?:
css_syntax.token_stream_discard_whitespace(tokens)
let maybe_position = css_parse_position_edge(tokens) else return none
css_syntax.token_stream_discard_whitespace(tokens)
let maybe_length = css_parser_parse_length_percentage_value(this, tokens, css_infinite_range, css_infinite_range) else return none
return CssPositionAndLength(
position = maybe_position,
length = maybe_length,
)
## The `alternative_4` lambda of parse_position_value:
## [ [ left | right ] <length-percentage> ] && [ [ top | bottom ] <length-percentage> ].
func css_parser_parse_position_alternative_4(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssPositionStyleValue*?:
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let group1 = css_parser_parse_position_and_length(this, tokens) else return none
let group2 = css_parser_parse_position_and_length(this, tokens) else return none
# [ [ left | right ] <length-percentage> ] [ [ top | bottom ] <length-percentage> ]
if css_position_edge_is_horizontal(group1.position, false) and css_position_edge_is_vertical(group2.position, false):
css_syntax.state_transaction_commit(&transaction)
return css_position_style_value_create(css_edge_style_value_create(group1.position, group1.length), css_edge_style_value_create(group2.position, group2.length))
# [ [ top | bottom ] <length-percentage> ] [ [ left | right ] <length-percentage> ]
if css_position_edge_is_vertical(group1.position, false) and css_position_edge_is_horizontal(group2.position, false):
css_syntax.state_transaction_commit(&transaction)
return css_position_style_value_create(css_edge_style_value_create(group2.position, group2.length), css_edge_style_value_create(group1.position, group1.length))
return none
## The `PositionAndMaybeLength` struct of alternative_5_for_background_position.
struct CssPositionAndMaybeLength:
var position: css_data.PositionEdge
var length: CssStyleValue*?
## The `parse_position_and_maybe_length` lambda of alternative_5_for_background_position: [ <position> <length-percentage>? ].
func css_parser_parse_position_and_maybe_length(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssPositionAndMaybeLength?:
var inner_transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&inner_transaction)
css_syntax.token_stream_discard_whitespace(tokens)
let maybe_position = css_parse_position_edge(tokens) else return none
css_syntax.token_stream_discard_whitespace(tokens)
let maybe_length = css_parser_parse_length_percentage_value(this, tokens, css_infinite_range, css_infinite_range)
if maybe_length != none:
# 'center' cannot be followed by a <length-percentage>
if maybe_position == css_data.PositionEdge.center and maybe_length != none:
return none
css_syntax.state_transaction_commit(&inner_transaction)
return CssPositionAndMaybeLength(
position = maybe_position,
length = maybe_length,
)
## The `to_style_value` lambda of alternative_5_for_background_position.
func css_position_and_maybe_length_to_style_value(group: const CssPositionAndMaybeLength*) -> CssEdgeStyleValue*:
if group.position == css_data.PositionEdge.center:
return css_edge_only(css_data.PositionEdge.center)
return css_edge_style_value_create(group.position, group.length)
## The `alternative_5_for_background_position` lambda of parse_position_value: the extra 3-value syntax that's allowed
## for background-position, [ center | [ left | right ] <length-percentage>? ] && [ center | [ top | bottom ] <length-percentage>? ].
func css_parser_parse_position_alternative_5_for_background_position(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssPositionStyleValue*?:
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
var group1 = css_parser_parse_position_and_maybe_length(this, tokens) else return none
var group2 = css_parser_parse_position_and_maybe_length(this, tokens) else return none
# 2-value or 4-value if both <length-percentage>s are present or missing.
if (group1.length != none and group2.length != none) or (group1.length == none and group2.length == none):
return none
# If 'left' or 'right' is given, that position is X and the other is Y.
# Conversely -
# If 'top' or 'bottom' is given, that position is Y and the other is X.
if css_position_edge_is_vertical(group1.position, false) or css_position_edge_is_horizontal(group2.position, false):
let swapped = group1
group1 = group2
group2 = swapped
# [ center | [ left | right ] ]
if not css_position_edge_is_horizontal(group1.position, true):
return none
# [ center | [ top | bottom ] ]
if not css_position_edge_is_vertical(group2.position, true):
return none
css_syntax.state_transaction_commit(&transaction)
return css_position_style_value_create(css_position_and_maybe_length_to_style_value(&group1), css_position_and_maybe_length_to_style_value(&group2))
## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2832 Web::CSS::Parser::Parser::parse_position_value.
# https://www.w3.org/TR/css-values-4/#position
pub func css_parser_parse_position_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, position_parsing_mode: CssParserPositionParsingMode = CssParserPositionParsingMode.normal) -> CssPositionStyleValue*?:
# <position> = [
# [ left | center | right | top | bottom | <length-percentage> ]
# |
# [ left | center | right ] && [ top | center | bottom ]
# |
# [ left | center | right | <length-percentage> ]
# [ top | center | bottom | <length-percentage> ]
# |
# [ [ left | right ] <length-percentage> ] &&
# [ [ top | bottom ] <length-percentage> ]
# ]
# Note: The alternatives must be attempted in this order since shorter alternatives can match a prefix of longer ones.
if let position = css_parser_parse_position_alternative_4(this, tokens):
return position
if position_parsing_mode == CssParserPositionParsingMode.background_position:
if let position = css_parser_parse_position_alternative_5_for_background_position(this, tokens):
return position
if let position = css_parser_parse_position_alternative_3(this, tokens):
return position
if let position = css_parser_parse_position_alternative_2(tokens):
return position
if let position = css_parser_parse_position_alternative_1(this, tokens):
return position
return none