dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / parser / property_parsing_5.lucb
#==============================================================================================
#
# property_parsing_5 - parsing property values: border-image, border-radius, columns
#
# DESCRIPTION:
# Port of LibWeb/CSS/Parser/PropertyParsing.cpp (lines 1480-1820): the border-image
# shorthand (with its lambdas), border-image-slice, a border radius, the border-radius
# shorthand and the columns shorthand.
#
#==============================================================================================
# mark: border-image ============================================================================
## The locals of parse_border_image_value that its lambdas capture by reference.
struct CssParserParseBorderImageValueState:
var source_value: CssStyleValue*?
var slice_value: CssStyleValue*?
var width_value: CssStyleValue*?
var outset_value: CssStyleValue*?
var repeat_value: CssStyleValue*?
## The `make_border_image_shorthand` lambda of parse_border_image_value (it commits the function's transaction).
func css_make_border_image_shorthand(state: CssParserParseBorderImageValueState*, transaction: css_syntax.StateTransaction*) -> CssStyleValue*:
css_syntax.state_transaction_commit(transaction)
if state.source_value == none:
state.source_value = css_property_initial_value(css_data.PropertyId.border_image_source)
if state.slice_value == none:
state.slice_value = css_property_initial_value(css_data.PropertyId.border_image_slice)
if state.width_value == none:
state.width_value = css_property_initial_value(css_data.PropertyId.border_image_width)
if state.outset_value == none:
state.outset_value = css_property_initial_value(css_data.PropertyId.border_image_outset)
if state.repeat_value == none:
state.repeat_value = css_property_initial_value(css_data.PropertyId.border_image_repeat)
return css_shorthand_of(css_data.PropertyId.border_image,
css_property_ids_of([css_data.PropertyId.border_image_source, css_data.PropertyId.border_image_slice, css_data.PropertyId.border_image_width,
css_data.PropertyId.border_image_outset, css_data.PropertyId.border_image_repeat]),
css_style_values_of([state.source_value else trap("release_nonnull"), state.slice_value else trap("release_nonnull"),
state.width_value else trap("release_nonnull"), state.outset_value else trap("release_nonnull"),
state.repeat_value else trap("release_nonnull")]))
## The `parse_value_list` lambda of parse_border_image_value: up to the property's maximum number of values.
func css_parser_parse_border_image_value_list(this: CssParser*, property_id: css_data.PropertyId, inner_tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*,
delimiter: u8? = none) -> CssStyleValue*?:
var inner_transaction = css_syntax.token_stream_begin_transaction(inner_tokens)
defer css_syntax.state_transaction_destroy(&inner_transaction)
var remaining_values = css_data.property_maximum_value_count(property_id)
var values = ak.Vector[CssStyleValue*]()
while css_syntax.token_stream_has_next_token(inner_tokens) and remaining_values > 0:
if let delimiter_value = delimiter:
if css_next_token_is_delim(inner_tokens, (u32)delimiter_value):
break
let value = css_parser_parse_css_value_for_property(this, property_id, inner_tokens) else break
ak.vector_append_t(&values, value)
remaining_values -= 1
if ak.vector_is_empty(&values):
return none
css_syntax.state_transaction_commit(&inner_transaction)
if ak.vector_size(&values) == 1:
return *ak.vector_at(&values, 0)
return css_style_value_list_of(values, CssStyleValueListSeparator.space)
## The `parse_slice_portion` lambda of parse_border_image_value.
func css_parser_parse_border_image_slice_portion(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, state: CssParserParseBorderImageValueState*) -> bool:
# <'border-image-slice'> [ / <'border-image-width'> | / <'border-image-width'>? / <'border-image-outset'> ]?
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
css_syntax.token_stream_discard_whitespace(tokens)
if let value = css_parser_parse_border_image_slice_value(this, tokens):
state.slice_value = value
else:
return false
css_syntax.token_stream_discard_whitespace(tokens)
if css_next_token_is_delim(tokens, (u32)'/'):
css_syntax.token_stream_discard_a_token(tokens)
css_syntax.token_stream_discard_whitespace(tokens)
if let width = css_parser_parse_border_image_value_list(this, css_data.PropertyId.border_image_width, tokens):
state.width_value = width
css_syntax.token_stream_discard_whitespace(tokens)
if not css_next_token_is_delim(tokens, (u32)'/'):
css_syntax.state_transaction_commit(&transaction)
return true
if not css_next_token_is_delim(tokens, (u32)'/'):
return false
css_syntax.token_stream_discard_a_token(tokens)
css_syntax.token_stream_discard_whitespace(tokens)
if let outset = css_parser_parse_border_image_value_list(this, css_data.PropertyId.border_image_outset, tokens):
state.outset_value = outset
css_syntax.token_stream_discard_whitespace(tokens)
else:
return false
css_syntax.token_stream_discard_whitespace(tokens)
css_syntax.state_transaction_commit(&transaction)
return true
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:1481 Web::CSS::Parser::Parser::parse_border_image_value.
# https://drafts.csswg.org/css-backgrounds/#border-image
pub func css_parser_parse_border_image_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# <'border-image-source'> || <'border-image-slice'> [ / <'border-image-width'> | / <'border-image-width'>? / <'border-image-outset'> ]? || <'border-image-repeat'>
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
var state = CssParserParseBorderImageValueState()
# NB: The `make_border_image_shorthand`, `parse_value_list` and `parse_slice_portion` lambdas are
# css_make_border_image_shorthand, css_parser_parse_border_image_value_list and
# css_parser_parse_border_image_slice_portion.
var has_source = false
var has_repeat = false
var has_slice_portion = false
while css_syntax.token_stream_has_next_token(tokens):
if let source = css_parser_parse_css_value_for_property(this, css_data.PropertyId.border_image_source, tokens):
if has_source:
return none
has_source = true
state.source_value = source
continue
if let repeat = css_parser_parse_border_image_value_list(this, css_data.PropertyId.border_image_repeat, tokens):
if has_repeat:
return none
has_repeat = true
state.repeat_value = repeat
continue
if css_parser_parse_border_image_slice_portion(this, tokens, &state):
if has_slice_portion:
return none
has_slice_portion = true
continue
return none
return css_make_border_image_shorthand(&state, &transaction)
## The `parse_fill` lambda of parse_border_image_slice_value.
func css_parse_border_image_slice_fill(fill_tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> bool:
if css_next_token_is_ident(fill_tokens, "fill"):
css_syntax.token_stream_discard_a_token(fill_tokens)
return true
return false
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:1608 Web::CSS::Parser::Parser::parse_border_image_slice_value.
# https://drafts.csswg.org/css-backgrounds/#border-image-slice
pub func css_parser_parse_border_image_slice_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# [<number [0,∞]> | <percentage [0,∞]>]{1,4} && fill?
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
var fill = false
var top: CssStyleValue*? = none
var right: CssStyleValue*? = none
var bottom: CssStyleValue*? = none
var left: CssStyleValue*? = none
# NB: The `parse_fill` lambda is css_parse_border_image_slice_fill.
css_syntax.token_stream_discard_whitespace(tokens)
if css_parse_border_image_slice_fill(tokens):
fill = true
css_syntax.token_stream_discard_whitespace(tokens)
var number_percentages = ak.Vector[CssStyleValue*]()
while ak.vector_size(&number_percentages) <= 4 and css_syntax.token_stream_has_next_token(tokens):
let number_percentage = css_parser_parse_number_percentage_value(this, tokens, css_non_negative_range, css_non_negative_range) else break
ak.vector_append_t(&number_percentages, number_percentage)
css_syntax.token_stream_discard_whitespace(tokens)
match ak.vector_size(&number_percentages):
1:
top = *ak.vector_at(&number_percentages, 0)
right = *ak.vector_at(&number_percentages, 0)
bottom = *ak.vector_at(&number_percentages, 0)
left = *ak.vector_at(&number_percentages, 0)
2:
top = *ak.vector_at(&number_percentages, 0)
bottom = *ak.vector_at(&number_percentages, 0)
right = *ak.vector_at(&number_percentages, 1)
left = *ak.vector_at(&number_percentages, 1)
3:
top = *ak.vector_at(&number_percentages, 0)
right = *ak.vector_at(&number_percentages, 1)
left = *ak.vector_at(&number_percentages, 1)
bottom = *ak.vector_at(&number_percentages, 2)
4:
top = *ak.vector_at(&number_percentages, 0)
right = *ak.vector_at(&number_percentages, 1)
bottom = *ak.vector_at(&number_percentages, 2)
left = *ak.vector_at(&number_percentages, 3)
_:
return none
css_syntax.token_stream_discard_whitespace(tokens)
if css_syntax.token_stream_has_next_token(tokens) and css_parse_border_image_slice_fill(tokens):
if fill:
return none
fill = true
css_syntax.token_stream_discard_whitespace(tokens)
css_syntax.state_transaction_commit(&transaction)
return css_border_image_slice_style_value_create(
top else trap("release_nonnull"),
right else trap("release_nonnull"),
bottom else trap("release_nonnull"),
left else trap("release_nonnull"),
fill).css_style_value()
# mark: border-radius ===========================================================================
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:1689 Web::CSS::Parser::Parser::parse_border_radius_value.
# https://drafts.csswg.org/css-borders-4/#typedef-border-radius
pub func css_parser_parse_border_radius_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# <border-radius> = <slash-separated-border-radius-syntax> | <legacy-border-radius-syntax>
# <slash-separated-border-radius-syntax> = <length-percentage [0,∞]> [ / <length-percentage [0,∞]> ]?
# <legacy-border-radius-syntax> = <length-percentage [0,∞]>{1,2}
# NB: So, 1 or 2 `<length-percentage>`s, optionally separated with a `/`.
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
css_syntax.token_stream_discard_whitespace(tokens)
let horizontal = css_parser_parse_length_percentage_value(this, tokens, css_non_negative_range, css_non_negative_range)
css_syntax.token_stream_discard_whitespace(tokens)
if css_next_token_is_delim(tokens, (u32)'/'):
css_syntax.token_stream_discard_a_token(tokens) # '/'
css_syntax.token_stream_discard_whitespace(tokens)
let vertical = css_parser_parse_length_percentage_value(this, tokens, css_non_negative_range, css_non_negative_range)
if let horizontal_value = horizontal:
if let vertical_value = vertical:
css_syntax.state_transaction_commit(&transaction)
return css_border_radius_style_value_create(horizontal_value, vertical_value).css_style_value()
css_syntax.state_transaction_commit(&transaction)
return css_border_radius_style_value_create(horizontal_value, horizontal_value).css_style_value()
return none
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:1718 Web::CSS::Parser::Parser::parse_border_radius_shorthand_value.
pub func css_parser_parse_border_radius_shorthand_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let border_radius_rect = css_parser_parse_border_radius_rect_value(this, tokens) else return none
css_syntax.state_transaction_commit(&transaction)
return css_shorthand_of(css_data.PropertyId.border_radius,
css_property_ids_of([css_data.PropertyId.border_top_left_radius, css_data.PropertyId.border_top_right_radius,
css_data.PropertyId.border_bottom_right_radius, css_data.PropertyId.border_bottom_left_radius]),
css_style_values_of([css_border_radius_rect_style_value_top_left(border_radius_rect), css_border_radius_rect_style_value_top_right(border_radius_rect),
css_border_radius_rect_style_value_bottom_right(border_radius_rect), css_border_radius_rect_style_value_bottom_left(border_radius_rect)]))
# mark: columns =================================================================================
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:1733 Web::CSS::Parser::Parser::parse_columns_value.
pub func css_parser_parse_columns_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
var column_count: CssStyleValue*? = none
var column_width: CssStyleValue*? = none
var column_height: CssStyleValue*? = none
var remaining_longhands = css_property_ids_of([css_data.PropertyId.column_count, css_data.PropertyId.column_width])
var found_autos: i32 = 0
var values_read: i32 = 0
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_delim(tokens, (u32)'/'):
if values_read == 0:
return none
css_syntax.token_stream_discard_a_token(tokens) # /
css_syntax.token_stream_discard_whitespace(tokens)
let value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.column_height, tokens) else return none
column_height = value
css_syntax.token_stream_discard_whitespace(tokens)
if css_syntax.token_stream_has_next_token(tokens):
return none
break
if values_read == 2:
return none
values_read += 1
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("null style value")
# since the values can be in either order, we want to skip over autos
if css_style_value_has_auto(value):
found_autos += 1
continue
css_remove_property(&remaining_longhands, parsed.property)
match parsed.property:
.column_count:
assert(column_count == none)
column_count = value
continue
.column_width:
assert(column_width == none)
column_width = value
continue
_:
trap("VERIFY_NOT_REACHED")
if found_autos == 2:
column_count = css_keyword_value_of(css_data.Keyword.auto)
column_width = css_keyword_value_of(css_data.Keyword.auto)
if found_autos == 1:
if column_count == none:
column_count = css_keyword_value_of(css_data.Keyword.auto)
if column_width == none:
column_width = css_keyword_value_of(css_data.Keyword.auto)
if column_count == none:
column_count = css_property_initial_value(css_data.PropertyId.column_count)
if column_width == none:
column_width = css_property_initial_value(css_data.PropertyId.column_width)
if column_height == none:
column_height = css_property_initial_value(css_data.PropertyId.column_height)
css_syntax.state_transaction_commit(&transaction)
return css_shorthand_of(css_data.PropertyId.columns,
css_property_ids_of([css_data.PropertyId.column_count, css_data.PropertyId.column_width, css_data.PropertyId.column_height]),
css_style_values_of([column_count else trap("release_nonnull"), column_width else trap("release_nonnull"), column_height else trap("release_nonnull")]))