Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / parser / property_parsing_12.lucb

#==============================================================================================
#
#   property_parsing_12 - parsing property values: text-decoration-line, text-indent,
#                         text-underline-position, timeline-scope, touch-action,
#                         transform-origin
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Parser/PropertyParsing.cpp (lines 4089-4428).
#
#==============================================================================================

# mark: text-decoration-line ====================================================================

## The comparator of parse_text_decoration_line_value's quick_sort: by TextDecorationLine.
func css_text_decoration_line_less_than(context: void*?, left: const (CssStyleValue*)*, right: const (CssStyleValue*)*) -> bool:
    _ = context
    let left_line = css_data.keyword_to_text_decoration_line(css_style_value_to_keyword(*left)) else trap("VERIFY: a text-decoration-line")
    let right_line = css_data.keyword_to_text_decoration_line(css_style_value_to_keyword(*right)) else trap("VERIFY: a text-decoration-line")
    return (u8)left_line < (u8)right_line

## `style_values.contains_slow(value)` over ValueComparingNonnullRefPtrs: by value.
func css_style_values_contains_slow(style_values: const ak.Vector[CssStyleValue*]*, value: CssStyleValue*) -> bool:
    for existing in ak.vector_span_const(style_values):
        if css_style_values_equal(existing, value):
            return true
    return false

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4089 Web::CSS::Parser::Parser::parse_text_decoration_line_value.
pub func css_parser_parse_text_decoration_line_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    var style_values = ak.Vector[CssStyleValue*]()

    var includes_spelling_or_grammar_error_value = false

    while css_syntax.token_stream_has_next_token(tokens):
        let value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.text_decoration_line, tokens) else break

        if let maybe_line = css_data.keyword_to_text_decoration_line(css_style_value_to_keyword(value)):
            if maybe_line == css_data.TextDecorationLine.none_:
                if not ak.vector_is_empty(&style_values):
                    return none
                return value
            if maybe_line == css_data.TextDecorationLine.spelling_error or maybe_line == css_data.TextDecorationLine.grammar_error:
                includes_spelling_or_grammar_error_value = true
            if css_style_values_contains_slow(&style_values, value):
                return none
            ak.vector_append_t(&style_values, value)
            continue

        trap("VERIFY_NOT_REACHED")

    if ak.vector_is_empty(&style_values):
        return none

    # These can only appear on their own.
    if ak.vector_size(&style_values) > 1 and includes_spelling_or_grammar_error_value:
        return none

    ak.quick_sort_t_collection_t_less_than(ak.vector_span(&style_values), ak.Function2[const (CssStyleValue*)*, const (CssStyleValue*)*, bool](call = css_text_decoration_line_less_than, context = none))

    return css_style_value_list_of(style_values, CssStyleValueListSeparator.space)

# mark: text-indent and text-underline-position =================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4134 Web::CSS::Parser::Parser::parse_text_indent_value.
# https://drafts.csswg.org/css-text-3/#text-indent-property
pub func css_parser_parse_text_indent_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # [ <length-percentage> ] && hanging? && each-line?
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)

    var length_percentage: CssStyleValue*? = none
    var has_hanging = false
    var has_each_line = false

    css_syntax.token_stream_discard_whitespace(tokens)

    while css_syntax.token_stream_has_next_token(tokens):
        if length_percentage == none:
            if let parsed = css_parser_parse_length_percentage_value(this, tokens, css_infinite_range, css_infinite_range):
                length_percentage = parsed
                css_syntax.token_stream_discard_whitespace(tokens)
                continue

        if let keyword = css_parser_parse_keyword_value(this, tokens):
            if not has_hanging and css_style_value_to_keyword(keyword) == css_data.Keyword.hanging:
                has_hanging = true
                continue
            if not has_each_line and css_style_value_to_keyword(keyword) == css_data.Keyword.each_line:
                has_each_line = true
                continue
            return none

        return none

    let length_percentage_value = length_percentage else return none

    css_syntax.state_transaction_commit(&transaction)
    return css_text_indent_style_value_create(length_percentage_value,
                                              CssTextIndentStyleValueHanging.yes if has_hanging else CssTextIndentStyleValueHanging.no,
                                              CssTextIndentStyleValueEachLine.yes if has_each_line else CssTextIndentStyleValueEachLine.no).css_style_value()

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4179 Web::CSS::Parser::Parser::parse_text_underline_position_value.
# https://drafts.csswg.org/css-text-decor-4/#text-underline-position-property
pub func css_parser_parse_text_underline_position_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # auto | [ from-font | under ] || [ left | right ]
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)

    if css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.auto) != none:
        css_syntax.state_transaction_commit(&transaction)
        return css_text_underline_position_style_value_create(css_data.TextUnderlinePositionHorizontal.auto, css_data.TextUnderlinePositionVertical.auto).css_style_value()

    var horizontal_value: css_data.TextUnderlinePositionHorizontal? = none
    var vertical_value: css_data.TextUnderlinePositionVertical? = none

    while css_syntax.token_stream_has_next_token(tokens):
        let keyword_value = css_parser_parse_keyword_value(this, tokens) else return none

        if let maybe_horizontal_value = css_data.keyword_to_text_underline_position_horizontal(css_style_value_to_keyword(keyword_value)):
            if maybe_horizontal_value == css_data.TextUnderlinePositionHorizontal.auto or horizontal_value != none:
                return none

            horizontal_value = maybe_horizontal_value

            continue

        if let maybe_vertical_value = css_data.keyword_to_text_underline_position_vertical(css_style_value_to_keyword(keyword_value)):
            if maybe_vertical_value == css_data.TextUnderlinePositionVertical.auto or vertical_value != none:
                return none

            vertical_value = maybe_vertical_value

            continue

        return none

    css_syntax.state_transaction_commit(&transaction)
    return css_text_underline_position_style_value_create(horizontal_value else css_data.TextUnderlinePositionHorizontal.auto,
                                                          vertical_value else css_data.TextUnderlinePositionVertical.auto).css_style_value()

# mark: timeline-scope and touch-action =========================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4224 Web::CSS::Parser::Parser::parse_timeline_scope_value.
# https://drafts.csswg.org/scroll-animations-1/#propdef-timeline-scope
pub func css_parser_parse_timeline_scope_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # none | all | <dashed-ident>#
    if let none_ = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.none_):
        return none_

    if let all = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.all):
        return all

    return css_parser_parse_comma_separated_values(this, tokens, css_parser_parse_dashed_ident_callback)

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4239 Web::CSS::Parser::Parser::parse_touch_action_value.
# https://www.w3.org/TR/pointerevents/#the-touch-action-css-property
pub func css_parser_parse_touch_action_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # auto | none | [ [ pan-x | pan-left | pan-right ] || [ pan-y | pan-up | pan-down ] ] | manipulation

    if let value = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.auto):
        return value
    if let value = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.none_):
        return value
    if let value = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.manipulation):
        return value

    var parsed_values = ak.Vector[CssStyleValue*]()
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)

    # We will verify that we have up to one vertical and one horizontal value
    var has_horizontal = false
    var has_vertical = false

    # Were the values specified in y/x order? (we need to store them in canonical x/y order)
    var swap_order = false

    while let parsed_value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.touch_action, tokens):
        match css_keyword_style_value_keyword(css_style_value_as_keyword(parsed_value)):
            .pan_x, .pan_left, .pan_right:
                if has_horizontal:
                    return none
                if has_vertical:
                    swap_order = true
                has_horizontal = true
            .pan_y, .pan_up, .pan_down:
                if has_vertical:
                    return none
                has_vertical = true
            .auto, .none_, .manipulation:
                # Not valid as part of a list
                return none
            _:
                trap("VERIFY_NOT_REACHED")

        ak.vector_append_t(&parsed_values, parsed_value)
        if not css_syntax.token_stream_has_next_token(tokens):
            break

    if swap_order:
        let first = *ak.vector_at(&parsed_values, 0)
        *ak.vector_at(&parsed_values, 0) = *ak.vector_at(&parsed_values, 1)
        *ak.vector_at(&parsed_values, 1) = first

    css_syntax.state_transaction_commit(&transaction)
    return css_style_value_list_of(parsed_values, CssStyleValueListSeparator.space)

# mark: transform-origin ========================================================================

## The local `enum class Axis` of parse_transform_origin_value.
enum CssParseTransformOriginValueAxis:
    none_
    x
    y

## The local `struct AxisOffset` of parse_transform_origin_value.
struct CssParseTransformOriginValueAxisOffset:
    var axis: CssParseTransformOriginValueAxis
    var offset: CssStyleValue*

## The `to_axis_offset` lambda of parse_transform_origin_value.
func css_transform_origin_to_axis_offset(value: CssStyleValue*?) -> CssParseTransformOriginValueAxisOffset?:
    let present = value else return none
    if css_style_value_is_percentage(present):
        return CssParseTransformOriginValueAxisOffset(axis = .none_, offset = css_style_value_as_percentage(present).css_style_value())
    if css_style_value_is_length(present):
        return CssParseTransformOriginValueAxisOffset(axis = .none_, offset = css_style_value_as_length(present).css_style_value())
    if css_style_value_is_keyword(present):
        match css_style_value_to_keyword(present):
            .top:
                return CssParseTransformOriginValueAxisOffset(axis = .y, offset = present)
            .left:
                return CssParseTransformOriginValueAxisOffset(axis = .x, offset = present)
            .center:
                return CssParseTransformOriginValueAxisOffset(axis = .none_, offset = present)
            .bottom:
                return CssParseTransformOriginValueAxisOffset(axis = .y, offset = present)
            .right:
                return CssParseTransformOriginValueAxisOffset(axis = .x, offset = present)
            _:
                return none
    if css_style_value_is_calculated(present):
        return CssParseTransformOriginValueAxisOffset(axis = .none_, offset = css_style_value_as_calculated(present).css_style_value())
    return none

## The `make_list` lambda of parse_transform_origin_value (it commits the function's transaction).
func css_transform_origin_make_list(transaction: css_syntax.StateTransaction*, x_value: CssStyleValue*, y_value: CssStyleValue*, z_value: CssStyleValue*) -> CssStyleValue*:
    css_syntax.state_transaction_commit(transaction)
    return css_style_value_list_of(css_style_values_of([x_value, y_value, z_value]), CssStyleValueListSeparator.space)

## `value->is_length() || value->is_percentage()`.
func css_is_length_or_percentage(value: CssStyleValue*) -> bool:
    return css_style_value_is_length(value) or css_style_value_is_percentage(value)

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4300 Web::CSS::Parser::Parser::parse_transform_origin_value.
# https://www.w3.org/TR/css-transforms-1/#propdef-transform-origin
pub func css_parser_parse_transform_origin_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    #   [ left | center | right | top | bottom | <length-percentage> ]
    # |
    #   [ left | center | right | <length-percentage> ]
    #   [ top | center | bottom | <length-percentage> ] <length>?
    # |
    #   [[ center | left | right ] && [ center | top | bottom ]] <length>?

    # NB: `Axis`, `AxisOffset` and the `to_axis_offset` lambda are CssParseTransformOriginValueAxis,
    #     CssParseTransformOriginValueAxisOffset and css_transform_origin_to_axis_offset.

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

    # NB: The `make_list` lambda is css_transform_origin_make_list.

    let zero_value = css_length_style_value_create(css_length_make_px_f64(0.0)).css_style_value()

    let first_value = css_transform_origin_to_axis_offset(css_parser_parse_css_value_for_property(this, css_data.PropertyId.transform_origin, tokens)) else return none
    css_syntax.token_stream_discard_whitespace(tokens)
    if not css_syntax.token_stream_has_next_token(tokens):
        # If only one value is specified, the second value is assumed to be center.
        match first_value.axis:
            .none_, .x:
                return css_transform_origin_make_list(&transaction, first_value.offset, css_keyword_value_of(css_data.Keyword.center), zero_value)
            .y:
                return css_transform_origin_make_list(&transaction, css_keyword_value_of(css_data.Keyword.center), first_value.offset, zero_value)

    let maybe_second_value = css_transform_origin_to_axis_offset(css_parser_parse_css_value_for_property(this, css_data.PropertyId.transform_origin, tokens))
    var third_value = css_parser_parse_length_value(this, tokens, css_infinite_range)

    let second_value = maybe_second_value else return none

    if css_is_length_or_percentage(first_value.offset) and second_value.axis == CssParseTransformOriginValueAxis.x:
        return none
    if css_is_length_or_percentage(second_value.offset) and first_value.axis == CssParseTransformOriginValueAxis.y:
        return none

    if third_value == none:
        third_value = zero_value

    var x_value: CssStyleValue*? = none
    var y_value: CssStyleValue*? = none

    if first_value.axis == CssParseTransformOriginValueAxis.x:
        x_value = first_value.offset
    elif first_value.axis == CssParseTransformOriginValueAxis.y:
        y_value = first_value.offset

    if second_value.axis == CssParseTransformOriginValueAxis.x:
        if x_value != none:
            return none
        x_value = second_value.offset
        # Put the other in Y since its axis can't have been X
        y_value = first_value.offset
    elif second_value.axis == CssParseTransformOriginValueAxis.y:
        if y_value != none:
            return none
        y_value = second_value.offset
        # Put the other in X since its axis can't have been Y
        x_value = first_value.offset
    else:
        if x_value != none:
            assert(y_value == none)
            y_value = second_value.offset
        else:
            assert(x_value == none)
            x_value = second_value.offset
    # If two or more values are defined and either no value is a keyword, or the only used keyword is center,
    # then the first value represents the horizontal position (or offset) and the second represents the vertical position (or offset).
    # A third value always represents the Z position (or offset) and must be of type <length>.
    if first_value.axis == CssParseTransformOriginValueAxis.none_ and second_value.axis == CssParseTransformOriginValueAxis.none_:
        x_value = first_value.offset
        y_value = second_value.offset

    return css_transform_origin_make_list(&transaction, x_value else trap("release_nonnull"), y_value else trap("release_nonnull"), third_value else trap("release_nonnull"))