dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / parser / property_parsing_8.lucb
#==============================================================================================
#
# property_parsing_8 - parsing property values: font, font-family, font-language-override,
# font-feature-settings, font-variation-settings
#
# DESCRIPTION:
# Port of LibWeb/CSS/Parser/PropertyParsing.cpp (lines 2450-2813).
#
#==============================================================================================
# mark: font ====================================================================================
## The `<font-width-css3>` block of parse_font_value: a font-width keyword, in a transaction committed only
## when the keyword is one.
func css_parser_parse_font_width_keyword(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
var font_width_transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&font_width_transaction)
if let keyword = css_parser_parse_keyword_value(this, tokens):
if css_data.keyword_to_font_width(css_style_value_to_keyword(keyword)) != none:
css_syntax.state_transaction_commit(&font_width_transaction)
return keyword
return none
## `(value ? 0 : 1)`.
func css_unset_count(value: CssStyleValue*?) -> i32:
if value != none:
return 0
return 1
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:2451 Web::CSS::Parser::Parser::parse_font_value.
# https://drafts.csswg.org/css-fonts-4/#font-prop
pub func css_parser_parse_font_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# [ [ <'font-style'> || <font-variant-css2> || <'font-weight'> || <font-width-css3> ]? <'font-size'> [ / <'line-height'> ]? <'font-family'># ] | <system-family-name>
var font_style: CssStyleValue*? = none
var font_variant: CssStyleValue*? = none
var font_weight: CssStyleValue*? = none
var font_width: CssStyleValue*? = none
var font_size: CssStyleValue*? = none
var line_height: CssStyleValue*? = none
var font_families: CssStyleValue*? = none
# FIXME: Handle <system-family-name>. (caption, icon, menu, message-box, small-caption, status-bar)
# Several sub-properties can be "normal", and appear in any order: style, variant, weight, stretch
# So, we have to handle that separately.
var normal_count: i32 = 0
# font-variant and font-width aren't included because we have special parsing rules for them in font.
var remaining_longhands = css_property_ids_of([css_data.PropertyId.font_size, css_data.PropertyId.font_style, css_data.PropertyId.font_weight])
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, "normal"):
normal_count += 1
css_syntax.token_stream_discard_a_token(tokens) # normal
css_syntax.token_stream_discard_whitespace(tokens)
continue
# <font-variant-css2> = normal | small-caps
# So, we handle that manually instead of trying to parse the font-variant property.
if font_variant == none and css_next_token_is_ident(tokens, "small-caps"):
css_syntax.token_stream_discard_a_token(tokens) # small-caps
font_variant = 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([
css_property_initial_value(css_data.PropertyId.font_variant_alternates),
css_keyword_value_of(css_data.Keyword.small_caps),
css_property_initial_value(css_data.PropertyId.font_variant_east_asian),
css_property_initial_value(css_data.PropertyId.font_variant_emoji),
css_property_initial_value(css_data.PropertyId.font_variant_ligatures),
css_property_initial_value(css_data.PropertyId.font_variant_numeric),
css_property_initial_value(css_data.PropertyId.font_variant_position),
]))
css_syntax.token_stream_discard_whitespace(tokens)
continue
# <font-width-css3> = normal | ultra-condensed | extra-condensed | condensed | semi-condensed | semi-expanded | expanded | extra-expanded | ultra-expanded
# So again, we do this manually.
if font_width == none and css_next_token_is(tokens, .ident):
if let keyword = css_parser_parse_font_width_keyword(this, tokens):
font_width = keyword
css_syntax.token_stream_discard_whitespace(tokens)
continue
let property_and_value = css_parser_parse_css_value_for_properties(this, ak.vector_span_const(&remaining_longhands), tokens) else return none
let value = property_and_value.style_value else trap("release_nonnull")
css_remove_property(&remaining_longhands, property_and_value.property)
match property_and_value.property:
.font_size:
assert(font_size == none)
font_size = value
# Consume `/ line-height` if present
css_syntax.token_stream_discard_whitespace(tokens)
if css_next_token_is_delim(tokens, (u32)'/'):
css_syntax.token_stream_discard_a_token(tokens) # /
let maybe_line_height = css_parser_parse_css_value_for_property(this, css_data.PropertyId.line_height, tokens) else return none
line_height = maybe_line_height
css_syntax.token_stream_discard_whitespace(tokens)
# Consume font-families
let maybe_font_families = css_parser_parse_font_family_value(this, tokens)
# font-family comes last, so we must not have any tokens left over.
if maybe_font_families == none or css_syntax.token_stream_has_next_token(tokens):
return none
font_families = maybe_font_families
css_syntax.token_stream_discard_whitespace(tokens)
continue
.font_style:
assert(font_style == none)
font_style = value
css_syntax.token_stream_discard_whitespace(tokens)
continue
.font_weight:
assert(font_weight == none)
font_weight = value
css_syntax.token_stream_discard_whitespace(tokens)
continue
_:
trap("VERIFY_NOT_REACHED")
# Since normal is the default value for all the properties that can have it, we don't have to actually
# set anything to normal here. It'll be set when we create the ShorthandStyleValue below.
# We just need to make sure we were not given more normals than will fit.
let unset_value_count = css_unset_count(font_style) + css_unset_count(font_weight) + css_unset_count(font_variant) + css_unset_count(font_width)
if unset_value_count < normal_count:
return none
if font_size == none or font_families == none:
return none
if font_style == none:
font_style = css_property_initial_value(css_data.PropertyId.font_style)
if font_variant == none:
font_variant = css_property_initial_value(css_data.PropertyId.font_variant)
if font_weight == none:
font_weight = css_property_initial_value(css_data.PropertyId.font_weight)
if font_width == none:
font_width = css_property_initial_value(css_data.PropertyId.font_width)
if line_height == none:
line_height = css_property_initial_value(css_data.PropertyId.line_height)
css_syntax.state_transaction_commit(&transaction)
return css_shorthand_of(css_data.PropertyId.font,
css_property_ids_of([
# Set explicitly https://drafts.csswg.org/css-fonts/#set-explicitly
css_data.PropertyId.font_family,
css_data.PropertyId.font_size,
css_data.PropertyId.font_width,
css_data.PropertyId.font_style,
css_data.PropertyId.font_variant,
css_data.PropertyId.font_weight,
css_data.PropertyId.line_height,
# Reset implicitly https://drafts.csswg.org/css-fonts/#reset-implicitly
css_data.PropertyId.font_feature_settings,
css_data.PropertyId.font_kerning,
css_data.PropertyId.font_language_override,
css_data.PropertyId.font_optical_sizing,
# FIXME: PropertyID::FontSizeAdjust,
css_data.PropertyId.font_variation_settings,
]),
css_style_values_of([
# Set explicitly
font_families else trap("release_nonnull"),
font_size else trap("release_nonnull"),
font_width else trap("release_nonnull"),
font_style else trap("release_nonnull"),
font_variant else trap("release_nonnull"),
font_weight else trap("release_nonnull"),
line_height else trap("release_nonnull"),
# Reset implicitly
css_property_initial_value(css_data.PropertyId.font_feature_settings), # font-feature-settings
css_property_initial_value(css_data.PropertyId.font_kerning), # font-kerning,
css_property_initial_value(css_data.PropertyId.font_language_override), # font-language-override
css_property_initial_value(css_data.PropertyId.font_optical_sizing), # font-optical-sizing,
# FIXME: font-size-adjust,
css_property_initial_value(css_data.PropertyId.font_variation_settings), # font-variation-settings
]))
# mark: font-family =============================================================================
## The per-family lambda of parse_font_family_value (the context is the parser).
func css_parser_parse_font_family_callback(context: void*?, inner_tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
let this = (CssParser*)(context else trap("no parser"))
css_syntax.token_stream_discard_whitespace(inner_tokens)
# <generic-family>
if css_next_token_is(inner_tokens, .ident):
let next = css_syntax.token_stream_next_token(inner_tokens)
let ident = css_component_value_ident(&next)
if let keyword = css_data.keyword_from_string(ak.fly_string_bytes_as_string_view(&ident)):
if css_data.keyword_to_generic_font_family(keyword) != none:
css_syntax.token_stream_discard_a_token(inner_tokens) # Ident
css_syntax.token_stream_discard_whitespace(inner_tokens)
return css_keyword_value_of(keyword)
# <family-name>
return css_parser_parse_family_name_value(this, inner_tokens)
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:2633 Web::CSS::Parser::Parser::parse_font_family_value.
# https://drafts.csswg.org/css-fonts-4/#font-family-prop
pub func css_parser_parse_font_family_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# [ <family-name> | <generic-family> ]#
return css_parser_parse_comma_separated_values(this, tokens, css_parser_parse_font_family_callback)
# mark: font-language-override ==================================================================
## `ErrorReporter::the().report(InvalidPropertyError { "style", "font-language-override", tokens.dump_string(), description })`.
func css_report_invalid_font_language_override(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, description: ak.String):
css_error_reporter_report(css_error_reporter_the(), CssParsingError.css_invalid_property_error(CssInvalidPropertyError(
rule_name = ak.fly_string_from_str("style"),
property_name = ak.fly_string_from_str("font-language-override"),
value_string = css_syntax.token_stream_dump_string(tokens),
description = description,
)))
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:2654 Web::CSS::Parser::Parser::parse_font_language_override_value.
pub func css_parser_parse_font_language_override_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# https://drafts.csswg.org/css-fonts/#propdef-font-language-override
# This is `normal | <string>` but with the constraint that the string has to be 4 characters long:
# Shorter strings are right-padded with spaces before use, and longer strings are invalid.
if let normal = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.normal):
return normal
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 string = css_parser_parse_string_value(this, tokens):
let string_value = css_string_style_value_string_value(string)
css_syntax.token_stream_discard_whitespace(tokens)
if css_syntax.token_stream_has_next_token(tokens):
css_report_invalid_font_language_override(tokens, ak.string_from_str("Unexpected trailing tokens"))
return none
let length = ak.fly_string_bytes(&string_value).length
if length == 0:
css_report_invalid_font_language_override(tokens, ak.string_from_str("<string> value is empty"))
return none
if not ak.fly_string_is_ascii(&string_value):
css_report_invalid_font_language_override(tokens, css_string_formatted("<string> value \"{}\" contains non-ascii characters", [css_arg_fly_string(&string_value)]))
return none
if length > 4:
css_report_invalid_font_language_override(tokens, css_string_formatted("<string> value \"{}\" is too long", [css_arg_fly_string(&string_value)]))
return none
# We're expected to always serialize without any trailing spaces, so remove them now for convenience.
let string_view = ak.fly_string_bytes_as_string_view(&string_value)
let trimmed = ak.string_view_trim_whitespace(&string_view, ak.TrimMode.right)
if ak.string_view_is_empty(&trimmed):
css_report_invalid_font_language_override(tokens, css_string_formatted("<string> value \"{}\" is only whitespace", [css_arg_fly_string(&string_value)]))
return none
css_syntax.state_transaction_commit(&transaction)
if ak.string_view_ne(&trimmed, string_view):
return css_string_style_value_create(ak.fly_string_from_utf8_without_validation(ak.string_view_bytes(&trimmed))).css_style_value()
return string.css_style_value()
return none
# mark: font-feature-settings and font-variation-settings =======================================
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:2725 Web::CSS::Parser::Parser::parse_font_feature_settings_value.
pub func css_parser_parse_font_feature_settings_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# https://drafts.csswg.org/css-fonts/#propdef-font-feature-settings
# normal | <feature-tag-value>#
# normal
if let normal = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.normal):
return normal
# <feature-tag-value>#
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let tag_values = css_parser_parse_a_comma_separated_list_of_component_values(this, tokens)
var feature_tags = ak.Vector[CssStyleValue*]()
ak.vector_ensure_capacity(&feature_tags, ak.vector_size(&tag_values))
for values in ak.vector_span_const(&tag_values):
# <feature-tag-value> = <opentype-tag> [ <integer [0,∞]> | on | off ]?
var tag_tokens = css_syntax.token_stream_create(values)
css_syntax.token_stream_discard_whitespace(&tag_tokens)
let opentype_tag = css_parser_parse_opentype_tag_value(this, &tag_tokens)
css_syntax.token_stream_discard_whitespace(&tag_tokens)
var value: CssStyleValue*? = none
if css_syntax.token_stream_has_next_token(&tag_tokens):
if let integer = css_parser_parse_integer_value(this, &tag_tokens, css_non_negative_integer_range):
value = integer
else:
# A value of on is synonymous with 1 and off is synonymous with 0.
let keyword = css_parser_parse_keyword_value(this, &tag_tokens) else return none
match css_style_value_to_keyword(keyword):
.on:
value = css_integer_style_value_create(1).css_style_value()
.off:
value = css_integer_style_value_create(0).css_style_value()
_:
return none
css_syntax.token_stream_discard_whitespace(&tag_tokens)
else:
# "If the value is omitted, a value of 1 is assumed."
value = css_integer_style_value_create(1).css_style_value()
if opentype_tag == none or value == none or css_syntax.token_stream_has_next_token(&tag_tokens):
return none
let tag = opentype_tag else trap("unreachable")
ak.vector_append_t(&feature_tags, css_open_type_tagged_style_value_create(CssOpenTypeTaggedStyleValueMode.font_feature_settings, css_string_style_value_string_value(tag),
value else trap("release_nonnull")).css_style_value())
css_syntax.state_transaction_commit(&transaction)
return css_style_value_list_of(feature_tags, CssStyleValueListSeparator.comma)
## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:2782 Web::CSS::Parser::Parser::parse_font_variation_settings_value.
pub func css_parser_parse_font_variation_settings_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
# https://drafts.csswg.org/css-fonts/#propdef-font-variation-settings
# normal | [ <opentype-tag> <number> ]#
# normal
if let normal = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.normal):
return normal
# [ <opentype-tag> <number>]#
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let tag_values = css_parser_parse_a_comma_separated_list_of_component_values(this, tokens)
var axis_tags = ak.Vector[CssStyleValue*]()
for values in ak.vector_span_const(&tag_values):
var tag_tokens = css_syntax.token_stream_create(values)
css_syntax.token_stream_discard_whitespace(&tag_tokens)
let opentype_tag = css_parser_parse_opentype_tag_value(this, &tag_tokens)
css_syntax.token_stream_discard_whitespace(&tag_tokens)
let number = css_parser_parse_number_value(this, &tag_tokens, css_infinite_range)
css_syntax.token_stream_discard_whitespace(&tag_tokens)
if opentype_tag == none or number == none or css_syntax.token_stream_has_next_token(&tag_tokens):
return none
let tag = opentype_tag else trap("unreachable")
ak.vector_append_t(&axis_tags, css_open_type_tagged_style_value_create(CssOpenTypeTaggedStyleValueMode.font_variation_settings, css_string_style_value_string_value(tag),
number else trap("release_nonnull")).css_style_value())
css_syntax.state_transaction_commit(&transaction)
return css_style_value_list_of(axis_tags, CssStyleValueListSeparator.comma)