Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / parser / property_parsing_15.lucb

#==============================================================================================
#
#   property_parsing_15 - parsing property values: grid-template-areas, grid-auto-rows and
#                         -columns, grid-auto-flow, grid-template-rows and -columns
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Parser/PropertyParsing.cpp (lines 5086-5296).
#
#==============================================================================================

# mark: grid-template-areas =====================================================================

## The `is_full_stop` lambda of parse_grid_template_areas_value.
func css_is_full_stop(code_point: u32) -> bool:
    return code_point == (u32)'.'

## The `consume_while` lambda of parse_grid_template_areas_value: the code points from `code_points` while
## `predicate` holds, as a String.
func css_consume_while(code_points: ak.Utf8CodePointIterator*, predicate: func(u32) -> bool) -> ak.String:
    var builder = ak.string_builder_new()
    while not ak.utf8_code_point_iterator_done(code_points) and predicate(ak.utf8_code_point_iterator_deref(code_points)):
        ak.string_builder_append_code_point(&builder, ak.utf8_code_point_iterator_deref(code_points))
        _ = ak.utf8_code_point_iterator_inc(code_points)
    return ak.string_builder_to_string(&builder) else trap("MUST: StringBuilder::to_string")

## `string == "."sv`.
func css_is_full_stop_string(string: const ak.String*) -> bool:
    return ak.string_eq_string_view(string, ak.sv("."))

## `name_counts.get(name).value_or(0)`.
func css_name_count(name_counts: ak.HashMap[ak.String, usize, ak.StringTraits]*, name: ak.String) -> usize:
    if let count = ak.hash_map_get_k(name_counts, name):
        return *count
    return 0

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:5087 Web::CSS::Parser::Parser::parse_grid_template_areas_value.
# https://www.w3.org/TR/css-grid-1/#grid-template-areas-property
pub func css_parser_parse_grid_template_areas_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # none | <string>+
    if css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.none_) != none:
        return css_grid_template_area_style_value_create(ak.HashMap[ak.String, CssGridArea, ak.StringTraits](), 0, 0).css_style_value()

    # NB: The `is_full_stop` and `consume_while` lambdas are css_is_full_stop and css_consume_while.

    var grid_area_rows = ak.Vector[ak.Vector[ak.String]]()
    var column_count: usize? = none

    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) and css_next_token_is(tokens, .string):
        var grid_area_columns = ak.Vector[ak.String]()
        let string_token = css_syntax.token_stream_consume_a_token(tokens)
        let token = css_syntax.component_value_token(&string_token)
        let string_value = css_syntax.token_string(&token)
        let string = ak.fly_string_code_points(&string_value)
        var code_points = ak.utf8_view_begin(&string)

        while not ak.utf8_code_point_iterator_done(&code_points):
            let code_point = ak.utf8_code_point_iterator_deref(&code_points)
            if css_syntax.is_whitespace(code_point):
                _ = css_consume_while(&code_points, css_syntax.is_whitespace)
            elif css_is_full_stop(code_point):
                _ = css_consume_while(&code_points, css_is_full_stop)
                ak.vector_append_t(&grid_area_columns, ak.string_from_str("."))
            elif css_syntax.is_ident_code_point(code_point):
                let ident = css_consume_while(&code_points, css_syntax.is_ident_code_point)
                ak.vector_append_t(&grid_area_columns, ident)
            else:
                return none

        if ak.vector_is_empty(&grid_area_columns):
            return none

        if let count = column_count:
            if ak.vector_size(&grid_area_columns) != count:
                return none
        else:
            column_count = ak.vector_size(&grid_area_columns)

        ak.vector_append_t(&grid_area_rows, grid_area_columns)
        css_syntax.token_stream_discard_whitespace(tokens)

    # https://www.w3.org/TR/css-grid-2/#grid-template-areas-property
    # If a named grid area spans multiple grid cells, but those cells do not form a single
    # filled-in rectangle, the declaration is invalid.

    # Pre-compute occurrence counts for each named area.
    var name_counts = ak.HashMap[ak.String, usize, ak.StringTraits]()
    for row in ak.vector_span_const(&grid_area_rows):
        for cell in ak.vector_span_const(&row):
            if not css_is_full_stop_string(&cell):
                _ = ak.hash_map_set(&name_counts, cell, css_name_count(&name_counts, cell) + 1)

    var grid_areas = ak.HashMap[ak.String, CssGridArea, ak.StringTraits]()
    for y in 0..<ak.vector_size(&grid_area_rows):
        let row = ak.vector_at_const(&grid_area_rows, y)
        for x in 0..<ak.vector_size(row):
            let name = *ak.vector_at_const(row, x)
            if css_is_full_stop_string(&name):
                continue
            if ak.hash_map_contains_k(&grid_areas, name):
                continue

            var x_end = x
            while x_end < ak.vector_size(row) and ak.string_equals(ak.vector_at_const(row, x_end), &name):
                x_end += 1
            var y_end = y
            while y_end < ak.vector_size(&grid_area_rows) and ak.string_equals(ak.vector_at_const(ak.vector_at_const(&grid_area_rows, y_end), x), &name):
                y_end += 1

            # Verify the bounding rectangle is fully filled with this name.
            let expected_count = (x_end - x) * (y_end - y)
            for check_y in y..<y_end:
                for check_x in x..<x_end:
                    if not ak.string_equals(ak.vector_at_const(ak.vector_at_const(&grid_area_rows, check_y), check_x), &name):
                        return none

            # Verify there are no occurrences of this name outside the rectangle.
            if css_name_count(&name_counts, name) != expected_count:
                return none

            _ = ak.hash_map_set(&grid_areas, name, CssGridArea(row_start = y, row_end = y_end, column_start = x, column_end = x_end))

    css_syntax.state_transaction_commit(&transaction)
    return css_grid_template_area_style_value_create(grid_areas, ak.vector_size(&grid_area_rows), column_count else 0).css_style_value()

# mark: grid-auto-rows, grid-auto-columns and grid-auto-flow ====================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:5194 Web::CSS::Parser::Parser::parse_grid_auto_track_sizes.
pub func css_parser_parse_grid_auto_track_sizes(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # https://www.w3.org/TR/css-grid-2/#auto-tracks
    # <track-size>+
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    var track_list = CssGridTrackSizeList()
    while css_syntax.token_stream_has_next_token(tokens):
        css_syntax.token_stream_discard_whitespace(tokens)
        let track_size = css_parser_parse_grid_track_size(this, tokens) else break
        css_grid_track_size_list_append_css_explicit_grid_track(&track_list, track_size)
    if not css_grid_track_size_list_is_empty(&track_list):
        css_syntax.state_transaction_commit(&transaction)
    return css_grid_track_size_list_style_value_create(track_list).css_style_value()

## The `parse_axis` lambda of parse_grid_auto_flow_value.
func css_parse_grid_auto_flow_axis(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssGridAutoFlowStyleValueAxis?:
    var axis_transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&axis_transaction)
    css_syntax.token_stream_discard_whitespace(tokens)
    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)
    if ak.fly_string_equals_ignoring_ascii_case_string_view(&ident, ak.sv("row")):
        css_syntax.state_transaction_commit(&axis_transaction)
        return CssGridAutoFlowStyleValueAxis.row
    if ak.fly_string_equals_ignoring_ascii_case_string_view(&ident, ak.sv("column")):
        css_syntax.state_transaction_commit(&axis_transaction)
        return CssGridAutoFlowStyleValueAxis.column
    return none

## The `parse_dense` lambda of parse_grid_auto_flow_value.
func css_parse_grid_auto_flow_dense(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssGridAutoFlowStyleValueDense?:
    var dense_transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&dense_transaction)
    css_syntax.token_stream_discard_whitespace(tokens)
    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)
    if ak.fly_string_equals_ignoring_ascii_case_string_view(&ident, ak.sv("dense")):
        css_syntax.state_transaction_commit(&dense_transaction)
        return CssGridAutoFlowStyleValueDense.yes
    return none

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:5213 Web::CSS::Parser::Parser::parse_grid_auto_flow_value.
# https://www.w3.org/TR/css-grid-1/#grid-auto-flow-property
pub func css_parser_parse_grid_auto_flow_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssGridAutoFlowStyleValue*?:
    _ = this
    # [ row | column ] || dense
    if not css_syntax.token_stream_has_next_token(tokens):
        return none

    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)

    # NB: The `parse_axis` and `parse_dense` lambdas are css_parse_grid_auto_flow_axis and css_parse_grid_auto_flow_dense.

    var axis: CssGridAutoFlowStyleValueAxis? = none
    var dense: CssGridAutoFlowStyleValueDense? = none
    axis = css_parse_grid_auto_flow_axis(tokens)
    if axis != none:
        dense = css_parse_grid_auto_flow_dense(tokens)
    else:
        dense = css_parse_grid_auto_flow_dense(tokens)
        if dense != none:
            axis = css_parse_grid_auto_flow_axis(tokens)

    css_syntax.token_stream_discard_whitespace(tokens)
    if css_syntax.token_stream_has_next_token(tokens):
        return none

    css_syntax.state_transaction_commit(&transaction)
    return css_grid_auto_flow_style_value_create(axis else CssGridAutoFlowStyleValueAxis.row, dense else CssGridAutoFlowStyleValueDense.no)

# mark: grid-template-rows and grid-template-columns ============================================

## The `// none` block of parse_grid_track_size_list: `none`, in a transaction.
func css_parse_grid_track_size_list_none(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(tokens)
    if css_syntax.token_stream_has_next_token(tokens) and css_next_token_is_ident(tokens, "none"):
        css_syntax.token_stream_discard_a_token(tokens)  # none
        css_syntax.state_transaction_commit(&transaction)
        return css_grid_track_size_list_style_value_make_none().css_style_value()
    return none

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:5270 Web::CSS::Parser::Parser::parse_grid_track_size_list.
# https://www.w3.org/TR/css-grid-2/#track-sizing
pub func css_parser_parse_grid_track_size_list(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # none | <track-list> | <auto-track-list> | FIXME: subgrid <line-name-list>?

    # none
    if let none_ = css_parse_grid_track_size_list_none(tokens):
        return none_

    # <auto-track-list>
    let auto_track_list = css_parser_parse_grid_auto_track_list(this, tokens)
    if not css_grid_track_size_list_is_empty(&auto_track_list):
        return css_grid_track_size_list_style_value_create(auto_track_list).css_style_value()

    # <track-list>
    let track_list = css_parser_parse_grid_track_list(this, tokens)
    if not css_grid_track_size_list_is_empty(&track_list):
        return css_grid_track_size_list_style_value_create(track_list).css_style_value()

    return none