dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / parser / descriptor_parsing_2.lucb
#==============================================================================================
#
# descriptor_parsing_2 - parsing descriptor values and converting declarations to descriptors
#
# DESCRIPTION:
# Port of LibWeb/CSS/Parser/DescriptorParsing.cpp (lines 1-81 and 389-448):
# parse_descriptor_value (support check, arbitrary substitution functions, then each
# syntax option of the descriptor's metadata in turn) and convert_to_descriptor. The
# value-type branch of the option visit is descriptor_parsing_1.
#
# Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## The `[&](PropertyID property_id) -> RefPtr<StyleValue const>` lambda of parse_descriptor_value.
func css_parser_parse_descriptor_value_as_property(this: CssParser*, property_id: css_data.PropertyId, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
let value_for_property = css_parser_parse_css_value(this, property_id, tokens) catch: return none
# Descriptors don't accept the CSS-wide keywords
if css_style_value_is_css_wide_keyword(value_for_property):
return none
return value_for_property
## Ported from Libraries/LibWeb/CSS/Parser/DescriptorParsing.cpp:25 Web::CSS::Parser::Parser::parse_descriptor_value.
pub func css_parser_parse_descriptor_value(this: CssParser*, at_rule_id: css_data.AtRuleId, descriptor_name_and_id: CssDescriptorNameAndId, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*!:
let descriptor_id = css_descriptor_name_and_id_id(&descriptor_name_and_id)
if not css_data.at_rule_supports_descriptor(at_rule_id, descriptor_id):
css_error_reporter_report(css_error_reporter_the(), CssParsingError.css_unknown_property_error(CssUnknownPropertyError(
rule_name = css_data.to_string_at_rule_id(at_rule_id),
property_name = css_descriptor_name_and_id_name(&descriptor_name_and_id),
)))
error(css_parser_parse_error_syntax_error, "")
css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.css_descriptor_context(CssDescriptorContext(at_rule = at_rule_id, descriptor = descriptor_id)))
defer css_parser_pop_temporary_value_parsing_context(this)
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let descriptor_value_start_index = css_syntax.token_stream_current_index(tokens)
var substitution_functions_presence = css_syntax.SubstitutionFunctionsPresence()
css_syntax.token_stream_mark(tokens)
while css_syntax.token_stream_has_next_token(tokens):
let token = css_syntax.token_stream_consume_a_token(tokens)
if css_syntax.component_value_is(&token, .semicolon):
error(css_parser_parse_error_syntax_error, "")
css_parser_collect_arbitrary_substitution_function_presence_component_value(&token, &substitution_functions_presence) catch:
error(css_parser_parse_error_syntax_error, "")
let metadata = css_data.get_descriptor_metadata(at_rule_id, descriptor_id)
if css_syntax.substitution_functions_presence_has_any(&substitution_functions_presence):
# https://drafts.csswg.org/css-values-5/#resolve-property
# Unless otherwise specified, arbitrary substitution functions can be used in place of any part of any
# property’s value (including within other functional notations); and are not valid in any other context.
# NB: Since we are not in a property value context we only allow ASFs if they are explicitly allowed in
# Descriptors.json
if not metadata.allow_arbitrary_substitution_functions:
let at_rule_name = css_data.to_string_at_rule_id(at_rule_id)
let descriptor_name = css_descriptor_name_and_id_name(&descriptor_name_and_id)
css_error_reporter_report(css_error_reporter_the(), CssParsingError.css_invalid_value_error(CssInvalidValueError(
value_type = ak.fly_string_from_string(css_string_formatted("{}/{}", [css_arg_fly_string(&at_rule_name), css_arg_fly_string(&descriptor_name)])),
value_string = css_syntax.token_stream_dump_string(tokens),
description = ak.string_from_str("ASFs are not supported in this descriptor"),
)))
error(css_parser_parse_error_syntax_error, "")
var values = ak.Vector[css_syntax.ComponentValue]()
ak.vector_construct_opaque_2(&values, css_syntax.token_stream_tokens_since(tokens, descriptor_value_start_index))
return css_unresolved_style_value_create(values, substitution_functions_presence).css_style_value()
css_syntax.token_stream_restore_a_mark(tokens)
var computation_context: CssComputationContext? = none
if let document = this.m_document:
computation_context = CssComputationContext(length_resolution_context = css_length_resolution_context_for_document(document))
for option in ak.vector_span_const(&metadata.syntax):
var syntax_transaction = css_syntax.state_transaction_create_child(&transaction)
defer css_syntax.state_transaction_destroy(&syntax_transaction)
let parsed_style_value = match option:
.keyword(keyword) => css_parser_parse_all_as_single_keyword_value(this, tokens, keyword)
.property_id(property_id) => css_parser_parse_descriptor_value_as_property(this, property_id, tokens)
.descriptor_metadata_value_type(value_type) => css_parser_parse_descriptor_value_of_value_type(this, value_type, tokens, computation_context)
if parsed_style_value == none or css_syntax.token_stream_has_next_token(tokens):
continue
css_syntax.state_transaction_commit(&syntax_transaction)
return parsed_style_value else trap("unreachable")
css_error_reporter_report(css_error_reporter_the(), CssParsingError.css_invalid_property_error(CssInvalidPropertyError(
rule_name = css_data.to_string_at_rule_id(at_rule_id),
property_name = css_descriptor_name_and_id_name(&descriptor_name_and_id),
value_string = css_syntax.token_stream_dump_string(tokens),
description = ak.string_from_str("Failed to parse."),
)))
error(css_parser_parse_error_syntax_error, "")
## Ported from Libraries/LibWeb/CSS/Parser/DescriptorParsing.cpp:434 Web::CSS::Parser::Parser::convert_to_descriptor.
pub func css_parser_convert_to_descriptor(this: CssParser*, at_rule_id: css_data.AtRuleId, declaration: css_syntax.Declaration) -> CssDescriptor?:
let descriptor_name_and_id = css_descriptor_name_and_id_from_name(at_rule_id, declaration.name) else return none
var value_token_stream = css_syntax.token_stream_create(declaration.value)
let value = css_parser_parse_descriptor_value(this, at_rule_id, descriptor_name_and_id, &value_token_stream) catch: return none
return CssDescriptor(descriptor_name_and_id = descriptor_name_and_id, value = value)