dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / parser / property_parsing_9.lucb
#==============================================================================================
#
# property_parsing_9 - parsing property values: font-variant, list-style, mask
#
# DESCRIPTION:
# Port of LibWeb/CSS/Parser/PropertyParsing.cpp (lines 2814-3285): the font-variant and
# list-style shorthands, and the mask shorthand (its per-layer state and lambdas).
#
#==============================================================================================
# mark: font-variant ============================================================================
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:2814 Web::CSS::Parser::Parser::parse_font_variant.
pub func css_parser_parse_font_variant(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# 6.11 https://drafts.csswg.org/css-fonts/#propdef-font-variant
# normal |
# none |
# [
# [ <common-lig-values> || <discretionary-lig-values> || <historical-lig-values> || <contextual-alt-values> ] ||
# [ small-caps | all-small-caps | petite-caps | all-petite-caps | unicase | titling-caps ] ||
# [ stylistic(<feature-value-name>) || historical-forms || styleset(<feature-value-name>#) || character-variant(<feature-value-name>#) || swash(<feature-value-name>) || ornaments(<feature-value-name>) || annotation(<feature-value-name>) ] ||
# [ <numeric-figure-values> || <numeric-spacing-values> || <numeric-fraction-values> || ordinal || slashed-zero ] ||
# [ <east-asian-variant-values> || <east-asian-width-values> || ruby ] ||
# [ sub | super ] ||
# [ text | emoji | unicode ]
# ]
var alternates_value: CssStyleValue*? = none
var caps_value: CssStyleValue*? = none
var emoji_value: CssStyleValue*? = none
var position_value: CssStyleValue*? = none
var east_asian_value: CssStyleValue*? = none
var ligatures_value: CssStyleValue*? = none
var numeric_value: CssStyleValue*? = none
if let parsed_normal = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.normal):
# normal, do nothing
_ = parsed_normal
elif let parsed_value = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.none_):
# none
ligatures_value = parsed_value
else:
while css_syntax.token_stream_has_next_token(tokens):
# [ <common-lig-values> || <discretionary-lig-values> || <historical-lig-values> || <contextual-alt-values> ]
if let maybe_ligature_value = css_parser_parse_font_variant_ligatures_value(this, tokens):
if ligatures_value != none:
return none
ligatures_value = maybe_ligature_value
continue
# [ stylistic(<feature-value-name>) || historical-forms || styleset(<feature-value-name>#) || character-variant(<feature-value-name>#) || swash(<feature-value-name>) || ornaments(<feature-value-name>) || annotation(<feature-value-name>) ]
if let maybe_alternates_value = css_parser_parse_font_variant_alternates_value(this, tokens):
if alternates_value != none:
return none
alternates_value = maybe_alternates_value
continue
# [ <numeric-figure-values> || <numeric-spacing-values> || <numeric-fraction-values> || ordinal || slashed-zero ]
if let maybe_numeric_value = css_parser_parse_font_variant_numeric_value(this, tokens):
if numeric_value != none:
return none
numeric_value = maybe_numeric_value
continue
# [ <east-asian-variant-values> || <east-asian-width-values> || ruby ]
if let maybe_east_asian_value = css_parser_parse_font_variant_east_asian_value(this, tokens):
if east_asian_value != none:
return none
east_asian_value = maybe_east_asian_value
continue
let value = css_parser_parse_keyword_value(this, tokens) else break
if not css_style_value_is_keyword(value):
return none
let keyword = css_style_value_to_keyword(value)
match keyword:
# [ small-caps | all-small-caps | petite-caps | all-petite-caps | unicase | titling-caps ]
.small_caps, .all_small_caps, .petite_caps, .all_petite_caps, .unicase, .titling_caps:
if caps_value != none:
return none
caps_value = value
# text | emoji | unicode
.text, .emoji, .unicode:
if emoji_value != none:
return none
emoji_value = value
# sub | super
.sub, .super:
if position_value != none:
return none
position_value = value
_:
return none
let normal_value = css_keyword_value_of(css_data.Keyword.normal)
if alternates_value == none:
alternates_value = normal_value
if caps_value == none:
caps_value = normal_value
if emoji_value == none:
emoji_value = normal_value
if position_value == none:
position_value = normal_value
if east_asian_value == none:
east_asian_value = normal_value
if ligatures_value == none:
ligatures_value = normal_value
if numeric_value == none:
numeric_value = normal_value
return css_shorthand_of(css_data.PropertyId.font_variant,
css_property_ids_of([css_data.PropertyId.font_variant_alternates,
css_data.PropertyId.font_variant_caps,
css_data.PropertyId.font_variant_east_asian,
css_data.PropertyId.font_variant_emoji,
css_data.PropertyId.font_variant_ligatures,
css_data.PropertyId.font_variant_numeric,
css_data.PropertyId.font_variant_position]),
css_style_values_of([
alternates_value else trap("release_nonnull"),
caps_value else trap("release_nonnull"),
east_asian_value else trap("release_nonnull"),
emoji_value else trap("release_nonnull"),
ligatures_value else trap("release_nonnull"),
numeric_value else trap("release_nonnull"),
position_value else trap("release_nonnull"),
]))
# mark: list-style ==============================================================================
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:2954 Web::CSS::Parser::Parser::parse_list_style_value.
pub func css_parser_parse_list_style_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
var list_position: CssStyleValue*? = none
var list_image: CssStyleValue*? = none
var list_type: CssStyleValue*? = none
var found_nones: i32 = 0
var remaining_longhands = css_property_ids_of([css_data.PropertyId.list_style_image, css_data.PropertyId.list_style_position, css_data.PropertyId.list_style_type])
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
css_syntax.token_stream_discard_whitespace(tokens)
while css_syntax.token_stream_has_next_token(tokens):
if css_next_token_is_ident(tokens, "none"):
css_syntax.token_stream_discard_a_token(tokens) # none
found_nones += 1
continue
let property_and_value = css_parser_parse_css_value_for_properties(this, ak.vector_span_const(&remaining_longhands), tokens)
css_syntax.token_stream_discard_whitespace(tokens)
let parsed = property_and_value else return none
let value = parsed.style_value else trap("release_nonnull")
css_remove_property(&remaining_longhands, parsed.property)
match parsed.property:
.list_style_position:
assert(list_position == none)
list_position = value
continue
.list_style_image:
assert(list_image == none)
list_image = value
continue
.list_style_type:
assert(list_type == none)
list_type = value
continue
_:
trap("VERIFY_NOT_REACHED")
if found_nones > 2:
return none
if found_nones == 2:
if list_image != none or list_type != none:
return none
let none_ = css_keyword_value_of(css_data.Keyword.none_)
list_image = none_
list_type = none_
elif found_nones == 1:
if list_image != none and list_type != none:
return none
let none_ = css_keyword_value_of(css_data.Keyword.none_)
if list_image == none:
list_image = none_
if list_type == none:
list_type = none_
if list_position == none:
list_position = css_property_initial_value(css_data.PropertyId.list_style_position)
if list_image == none:
list_image = css_property_initial_value(css_data.PropertyId.list_style_image)
if list_type == none:
list_type = css_property_initial_value(css_data.PropertyId.list_style_type)
css_syntax.state_transaction_commit(&transaction)
return css_shorthand_of(css_data.PropertyId.list_style,
css_property_ids_of([css_data.PropertyId.list_style_position, css_data.PropertyId.list_style_image, css_data.PropertyId.list_style_type]),
css_style_values_of([list_position else trap("release_nonnull"), list_image else trap("release_nonnull"), list_type else trap("release_nonnull")]))
# mark: mask ====================================================================================
## The locals of parse_mask_value that its lambdas capture by reference: the per-property lists of all layers,
## the initial values, the per-layer values and the properties the current layer may still set.
struct CssParserParseMaskValueState:
var mask_images: ak.Vector[CssStyleValue*]
var mask_positions: ak.Vector[CssStyleValue*]
var mask_sizes: ak.Vector[CssStyleValue*]
var mask_repeats: ak.Vector[CssStyleValue*]
var mask_origins: ak.Vector[CssStyleValue*]
var mask_clips: ak.Vector[CssStyleValue*]
var mask_composites: ak.Vector[CssStyleValue*]
var mask_modes: ak.Vector[CssStyleValue*]
var initial_mask_image: CssStyleValue*?
var initial_mask_position: CssStyleValue*?
var initial_mask_size: CssStyleValue*?
var initial_mask_repeat: CssStyleValue*?
var initial_mask_origin: CssStyleValue*?
var initial_mask_clip: CssStyleValue*?
var initial_mask_composite: CssStyleValue*?
var initial_mask_mode: CssStyleValue*?
# Per-layer values
var mask_image: CssStyleValue*?
var mask_position: CssStyleValue*?
var mask_size: CssStyleValue*?
var mask_repeat: CssStyleValue*?
var mask_origin: CssStyleValue*?
var mask_clip: CssStyleValue*?
var mask_composite: CssStyleValue*?
var mask_mode: CssStyleValue*?
var remaining_layer_properties: ak.Vector[css_data.PropertyId]
## The `make_mask_shorthand` lambda of parse_mask_value.
func css_make_mask_shorthand(mask_image: CssStyleValue*, mask_position: CssStyleValue*, mask_size: CssStyleValue*, mask_repeat: CssStyleValue*, mask_origin: CssStyleValue*,
mask_clip: CssStyleValue*, mask_composite: CssStyleValue*, mask_mode: CssStyleValue*) -> CssStyleValue*:
return css_shorthand_of(css_data.PropertyId.mask,
css_property_ids_of([css_data.PropertyId.mask_image, css_data.PropertyId.mask_position, css_data.PropertyId.mask_size, css_data.PropertyId.mask_repeat,
css_data.PropertyId.mask_origin, css_data.PropertyId.mask_clip, css_data.PropertyId.mask_composite, css_data.PropertyId.mask_mode]),
css_style_values_of([mask_image, mask_position, mask_size, mask_repeat, mask_origin, mask_clip, mask_composite, mask_mode]))
## The `mask_layer_is_valid` lambda of parse_mask_value.
func css_mask_layer_is_valid(state: const CssParserParseMaskValueState*) -> bool:
return (state.mask_image != none or state.mask_position != none or state.mask_size != none or state.mask_repeat != none or state.mask_origin != none or state.mask_clip != none or state.mask_composite != none or state.mask_mode != none)
## Sets `properties` to the properties a mask layer may set (mask-size is parsed with mask-position).
func css_set_mask_layer_properties(properties: ak.Vector[css_data.PropertyId]*):
ak.vector_unchecked_append_u(properties, css_data.PropertyId.mask_image)
ak.vector_unchecked_append_u(properties, css_data.PropertyId.mask_position)
ak.vector_unchecked_append_u(properties, css_data.PropertyId.mask_repeat)
ak.vector_unchecked_append_u(properties, css_data.PropertyId.mask_origin)
ak.vector_unchecked_append_u(properties, css_data.PropertyId.mask_clip)
ak.vector_unchecked_append_u(properties, css_data.PropertyId.mask_composite)
ak.vector_unchecked_append_u(properties, css_data.PropertyId.mask_mode)
## The `complete_mask_layer` lambda of parse_mask_value.
func css_complete_mask_layer(state: CssParserParseMaskValueState*):
ak.vector_append_t(&state.mask_images, css_value_or_initial(state.mask_image, state.initial_mask_image))
ak.vector_append_t(&state.mask_positions, css_value_or_initial(state.mask_position, state.initial_mask_position))
ak.vector_append_t(&state.mask_sizes, css_value_or_initial(state.mask_size, state.initial_mask_size))
ak.vector_append_t(&state.mask_repeats, css_value_or_initial(state.mask_repeat, state.initial_mask_repeat))
ak.vector_append_t(&state.mask_composites, css_value_or_initial(state.mask_composite, state.initial_mask_composite))
ak.vector_append_t(&state.mask_modes, css_value_or_initial(state.mask_mode, state.initial_mask_mode))
if state.mask_origin == none:
state.mask_origin = state.initial_mask_origin
if state.mask_clip == none:
state.mask_clip = state.mask_origin
ak.vector_append_t(&state.mask_origins, state.mask_origin else trap("release_nonnull"))
ak.vector_append_t(&state.mask_clips, state.mask_clip else trap("release_nonnull"))
state.mask_image = none
state.mask_position = none
state.mask_size = none
state.mask_repeat = none
state.mask_origin = none
state.mask_clip = none
state.mask_composite = none
state.mask_mode = none
ak.vector_clear_with_capacity(&state.remaining_layer_properties)
css_set_mask_layer_properties(&state.remaining_layer_properties)
## The block of parse_mask_value's loop that parses the next value for one of the remaining layer properties,
## committing its transaction unless the value is a mask-repeat (parsed again in full below).
func css_parser_parse_mask_layer_property(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*,
remaining_layer_properties: const ak.Vector[css_data.PropertyId]*) -> CssParserPropertyAndValue?:
var value_transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&value_transaction)
let property_and_value = css_parser_parse_css_value_for_properties(this, ak.vector_span_const(remaining_layer_properties), tokens) else return none
# FIXME: mask-repeat can't be parsed fully with parse_css_value_for_properties(), so don't commit
# the transaction, and then parse it manually below.
if property_and_value.property != css_data.PropertyId.mask_repeat:
css_syntax.state_transaction_commit(&value_transaction)
return property_and_value
## The `case PropertyID::MaskPosition` block of parse_mask_value after the position: an optional `/ <bg-size>`, in a
## transaction. False when a `/` is not followed by a size (the C++ returns nullptr).
func css_parser_parse_mask_size_after_position(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, state: CssParserParseMaskValueState*) -> bool:
# Attempt to parse `/ <bg-size>`
var mask_size_transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&mask_size_transaction)
css_syntax.token_stream_discard_whitespace(tokens)
let maybe_slash = css_syntax.token_stream_consume_a_token(tokens)
if css_syntax.component_value_is_delim(&maybe_slash, (u32)'/'):
css_syntax.token_stream_discard_whitespace(tokens)
if let maybe_mask_size = css_parser_parse_single_background_size_value(this, css_data.PropertyId.mask_size, tokens):
css_syntax.state_transaction_commit(&mask_size_transaction)
css_syntax.token_stream_discard_whitespace(tokens)
state.mask_size = maybe_mask_size
return true
return false
css_syntax.token_stream_discard_whitespace(tokens)
return true
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3033 Web::CSS::Parser::Parser::parse_mask_value.
pub func css_parser_parse_mask_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# https://drafts.fxtf.org/css-masking-1/#the-mask
# <mask-layer>#
#
# <mask-layer> =
# <mask-reference> ||
# <position> [ / <bg-size> ]? ||
# <repeat-style> ||
# <geometry-box> ||
# [ <geometry-box> | no-clip ] ||
# <compositing-operator> ||
# <masking-mode>
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
# NB: The `make_mask_shorthand`, `mask_layer_is_valid` and `complete_mask_layer` lambdas are
# css_make_mask_shorthand, css_mask_layer_is_valid and css_complete_mask_layer, over the locals they capture
# (CssParserParseMaskValueState).
var state = CssParserParseMaskValueState()
state.initial_mask_image = css_first_initial_value(css_data.PropertyId.mask_image)
state.initial_mask_position = css_first_initial_value(css_data.PropertyId.mask_position)
state.initial_mask_size = css_first_initial_value(css_data.PropertyId.mask_size)
state.initial_mask_repeat = css_first_initial_value(css_data.PropertyId.mask_repeat)
state.initial_mask_origin = css_first_initial_value(css_data.PropertyId.mask_origin)
state.initial_mask_clip = css_first_initial_value(css_data.PropertyId.mask_clip)
state.initial_mask_composite = css_first_initial_value(css_data.PropertyId.mask_composite)
state.initial_mask_mode = css_first_initial_value(css_data.PropertyId.mask_mode)
var has_multiple_layers = false
# MaskSize is always parsed as part of MaskPosition, so we don't include it here.
ak.vector_ensure_capacity(&state.remaining_layer_properties, 7)
css_set_mask_layer_properties(&state.remaining_layer_properties)
css_syntax.token_stream_discard_whitespace(tokens)
while css_syntax.token_stream_has_next_token(tokens):
if css_next_token_is(tokens, .comma):
has_multiple_layers = true
if not css_mask_layer_is_valid(&state):
return none
css_complete_mask_layer(&state)
css_syntax.token_stream_discard_a_token(tokens) # ,
css_syntax.token_stream_discard_whitespace(tokens)
continue
let property_and_value = css_parser_parse_mask_layer_property(this, tokens, &state.remaining_layer_properties) else return none
let property = property_and_value.property
let value = property_and_value.style_value
css_remove_property(&state.remaining_layer_properties, property)
match property:
# <mask-reference>
.mask_image:
assert(state.mask_image == none)
state.mask_image = value else trap("release_nonnull")
css_syntax.token_stream_discard_whitespace(tokens)
continue
# <position> [ / <bg-size> ]?
.mask_position:
assert(state.mask_position == none)
state.mask_position = value else trap("release_nonnull")
if not css_parser_parse_mask_size_after_position(this, tokens, &state):
return none
continue
# <repeat-style>
.mask_repeat:
assert(state.mask_repeat == none)
# NB: The tokens for this didn't get consumed, see above. So we parse it fully now.
if let maybe_repeat = css_parser_parse_single_repeat_style_value(this, property, tokens):
state.mask_repeat = maybe_repeat
css_syntax.token_stream_discard_whitespace(tokens)
continue
return none
# <geometry-box> || [ <geometry-box> | no-clip ]
# If one <geometry-box> value and the no-clip keyword are present then <geometry-box> sets mask-origin and
# no-clip sets mask-clip to that value.
# If one <geometry-box> value and no no-clip keyword are present then <geometry-box> sets both mask-origin and
# mask-clip to that value.
# If two <geometry-box> values are present, then the first sets mask-origin and the second mask-clip.
.mask_origin, .mask_clip:
let keyword_value = value else trap("release_nonnull")
assert(css_style_value_is_keyword(keyword_value))
if css_keyword_style_value_keyword(css_style_value_as_keyword(keyword_value)) == css_data.Keyword.no_clip:
assert(state.mask_clip == none)
state.mask_clip = keyword_value
elif state.mask_origin == none:
state.mask_origin = keyword_value
elif state.mask_clip == none:
state.mask_clip = keyword_value
else:
trap("VERIFY_NOT_REACHED")
css_syntax.token_stream_discard_whitespace(tokens)
continue
# <compositing-operator>
.mask_composite:
assert(state.mask_composite == none)
state.mask_composite = value else trap("release_nonnull")
css_syntax.token_stream_discard_whitespace(tokens)
continue
# <masking-mode>
.mask_mode:
assert(state.mask_mode == none)
state.mask_mode = value else trap("release_nonnull")
css_syntax.token_stream_discard_whitespace(tokens)
continue
_:
trap("VERIFY_NOT_REACHED")
if not css_mask_layer_is_valid(&state):
return none
# We only need to create StyleValueLists if there are multiple layers.
# Otherwise, we can pass the single StyleValue directly
if has_multiple_layers:
css_complete_mask_layer(&state)
css_syntax.state_transaction_commit(&transaction)
return css_make_mask_shorthand(
css_style_value_list_of(state.mask_images, CssStyleValueListSeparator.comma),
css_style_value_list_of(state.mask_positions, CssStyleValueListSeparator.comma),
css_style_value_list_of(state.mask_sizes, CssStyleValueListSeparator.comma),
css_style_value_list_of(state.mask_repeats, CssStyleValueListSeparator.comma),
css_style_value_list_of(state.mask_origins, CssStyleValueListSeparator.comma),
css_style_value_list_of(state.mask_clips, CssStyleValueListSeparator.comma),
css_style_value_list_of(state.mask_composites, CssStyleValueListSeparator.comma),
css_style_value_list_of(state.mask_modes, CssStyleValueListSeparator.comma))
if state.mask_image == none:
state.mask_image = state.initial_mask_image
if state.mask_position == none:
state.mask_position = state.initial_mask_position
if state.mask_size == none:
state.mask_size = state.initial_mask_size
if state.mask_repeat == none:
state.mask_repeat = state.initial_mask_repeat
if state.mask_origin == none:
state.mask_origin = state.initial_mask_origin
if state.mask_clip == none:
state.mask_clip = state.mask_origin # intentionally not initial value
if state.mask_composite == none:
state.mask_composite = state.initial_mask_composite
if state.mask_mode == none:
state.mask_mode = state.initial_mask_mode
css_syntax.state_transaction_commit(&transaction)
return css_make_mask_shorthand(
state.mask_image else trap("release_nonnull"),
state.mask_position else trap("release_nonnull"),
state.mask_size else trap("release_nonnull"),
state.mask_repeat else trap("release_nonnull"),
state.mask_origin else trap("release_nonnull"),
state.mask_clip else trap("release_nonnull"),
state.mask_composite else trap("release_nonnull"),
state.mask_mode else trap("release_nonnull"))