dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / parser / parser_1.lucb
#==============================================================================================
#
# parser_1 - the CSS parser: parsing parameters, construction, stylesheets, @supports and
# boolean expressions
#
# DESCRIPTION:
# Port of LibWeb/CSS/Parser/Parser.cpp (lines 1-556): log_parse_error, the
# ParsingParams constructors, Parser::create and its constructor, parsing a stylesheet
# and its contents, convert_rules, parse_as_css_stylesheet, @supports conditions,
# <boolean-expr>, container query conditions and <general-enclosed>.
#
# C++ explicitly instantiates the TokenStream<T> templates for T = Token and T =
# ComponentValue; here they are generics constrained by css_syntax.TokenStreamToken[T]
# (see parser_2 for the per-T steps).
#
# Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2020-2021, the SerenityOS developers.
# Copyright (c) 2021-2026, Sam Atkins <sam@ladybird.org>
# Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
# Copyright (c) 2022, MacDue <macdue@dueutil.tech>
# Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
# Copyright (c) 2024, Tommy van der Vorst <tommy@pixelspark.nl>
# Copyright (c) 2024, Matthew Olsson <mattco@serenityos.org>
# Copyright (c) 2024, Glenn Skrzypczak <glenn.skrzypczak@gmail.com>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:38 log_parse_error.
func log_parse_error(location: io.Location = luce.location):
if css_parser_debug:
ak.dbgln(f"Parse error (CSS) {location.function} @ {location.file}:{location.line}")
## The ParseError of Parser.h's `ParseErrorOr<T>`, as the code of the failure.
pub let css_parser_parse_error_includes_ignored_vendor_prefix: ErrorCode = ErrorCode.package(3)
## ParseError::SyntaxError.
pub let css_parser_parse_error_syntax_error: ErrorCode = ErrorCode.package(4)
# mark: Component value helpers =================================================================
## `value.is_block() && value.block().is_paren()`.
func css_component_value_is_paren_block(value: const css_syntax.ComponentValue*) -> bool:
if not css_syntax.component_value_is_block(value):
return false
let block = css_syntax.component_value_block(value)
return css_syntax.simple_block_is_paren(&block)
## `value.token().ident()`.
func css_component_value_ident(value: const css_syntax.ComponentValue*) -> ak.FlyString:
let token = css_syntax.component_value_token(value)
return css_syntax.token_ident(&token)
## `value.token().type()`.
func css_component_value_token_type(value: const css_syntax.ComponentValue*) -> css_syntax.TokenType:
let token = css_syntax.component_value_token(value)
return css_syntax.token_type(&token)
# mark: ParsingParams ===========================================================================
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:45 Web::CSS::Parser::ParsingParams::ParsingParams.
pub func css_parsing_params_construct_css_parsing_mode(this: CssParsingParams*, mode: CssParsingMode = CssParsingMode.normal):
*this = CssParsingParams(mode = mode, is_ua_style_sheet = CssIsUaStyleSheet.no)
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:50 Web::CSS::Parser::ParsingParams::ParsingParams.
pub func css_parsing_params_construct_css_value_parsing_context(this: CssParsingParams*, value_context: CssValueParsingContext):
var contexts = ak.Vector[CssValueParsingContext]()
ak.vector_append_t(&contexts, value_context)
*this = CssParsingParams(mode = CssParsingMode.normal, is_ua_style_sheet = CssIsUaStyleSheet.no, value_context = contexts)
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:55 Web::CSS::Parser::ParsingParams::ParsingParams.
pub func css_parsing_params_construct_realm_css_parsing_mode(this: CssParsingParams*, realm: Realm*, mode: CssParsingMode = CssParsingMode.normal):
*this = CssParsingParams(realm = realm, mode = mode, is_ua_style_sheet = CssIsUaStyleSheet.no)
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:61 Web::CSS::Parser::ParsingParams::ParsingParams.
pub func css_parsing_params_construct_realm_css_is_ua_style_sheet(this: CssParsingParams*, realm: Realm*, is_ua_style_sheet: CssIsUaStyleSheet):
*this = CssParsingParams(realm = realm, mode = CssParsingMode.normal, is_ua_style_sheet = is_ua_style_sheet)
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:67 Web::CSS::Parser::ParsingParams::ParsingParams.
pub func css_parsing_params_construct_dom_document(this: CssParsingParams*, document: DomDocument*, mode: CssParsingMode = CssParsingMode.normal):
*this = CssParsingParams(realm = bind_platform_object_realm(document.bind_platform_object()), document = document, mode = mode,
is_ua_style_sheet = CssIsUaStyleSheet.no)
# mark: Construction ============================================================================
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:74 Web::CSS::Parser::Parser::create.
pub func css_parser_create(context: CssParsingParams, input: ak.StringView, encoding: str = "utf-8") -> CssParser:
let tokens = css_syntax.tokenizer_tokenize_string_view(input, ak.sv(encoding))
var parser: CssParser = ---
css_parser_construct(&parser, context, tokens)
return parser
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:80 Web::CSS::Parser::Parser::Parser.
func css_parser_construct(this: CssParser*, context: CssParsingParams, tokens: ak.Vector[css_syntax.Token]):
# `move(context.value_context)` of a const& copies: the parser's stacks are its own.
*this = CssParser(
m_document = context.document,
m_realm = context.realm,
m_parsing_mode = context.mode,
m_is_ua_style_sheet = context.is_ua_style_sheet,
m_tokens = tokens,
m_token_stream = css_syntax.token_stream_create(tokens),
m_value_context = ak.vector_clone(&context.value_context),
m_rule_context = ak.vector_clone(&context.rule_context),
m_declared_namespaces = ak.hash_table_clone(&context.declared_namespaces),
)
# Addition (DESIGN.md §4.1.3): css_syntax's rule walkers report misplaced rules through this
# hook into the ErrorReporter; every rule they walk was made by a Parser.
css_syntax.invalid_rule_location_reporter = css_report_invalid_rule_location
# mark: Stylesheets =============================================================================
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:95 Web::CSS::Parser::Parser::parse_a_stylesheet.
# https://drafts.csswg.org/css-syntax/#parse-stylesheet
func css_parser_parse_a_stylesheet[T: css_syntax.TokenStreamToken[T]](this: CssParser*, input: css_syntax.TokenStream[T]*, location: web_url.Url?) -> CssParserParsedStyleSheet:
# To parse a stylesheet from an input given an optional url location:
# 1. If input is a byte stream for a stylesheet, decode bytes from input, and set input to the result.
# 2. Normalize input, and set input to the result.
# NOTE: These are done automatically when creating the Parser.
# 3. Create a new stylesheet, with its location set to location (or null, if location was not passed).
var style_sheet = CssParserParsedStyleSheet()
style_sheet.location = location
# 4. Consume a stylesheet’s contents from input, and set the stylesheet’s rules to the result.
style_sheet.rules = css_parser_consume_a_stylesheets_contents(this, input)
# 5. Return the stylesheet.
return style_sheet
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:116 Web::CSS::Parser::Parser::parse_a_stylesheets_contents.
# https://drafts.csswg.org/css-syntax/#parse-a-stylesheets-contents
func css_parser_parse_a_stylesheets_contents[T: css_syntax.TokenStreamToken[T]](this: CssParser*, input: css_syntax.TokenStream[T]*) -> ak.Vector[css_syntax.Rule]:
# To parse a stylesheet’s contents from input:
# 1. Normalize input, and set input to the result.
# NOTE: This is done automatically when creating the Parser.
# 2. Consume a stylesheet’s contents from input, and return the result.
return css_parser_consume_a_stylesheets_contents(this, input)
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:127 Web::CSS::Parser::Parser::convert_rules.
pub func css_parser_convert_rules(this: CssParser*, raw_rules: const ak.Vector[css_syntax.Rule]*) -> ak.Vector[CssRule*]:
var import_rules_valid = true
var namespace_rules_valid = true
# Interpret all of the resulting top-level qualified rules as style rules, defined below.
var rules = ak.Vector[CssRule*]()
for raw_rule in ak.vector_span_const(raw_rules):
# If any style rule is invalid, or any at-rule is not recognized or is invalid according to its grammar or context, it’s a parse error.
# Discard that rule.
let maybe_rule = css_parser_convert_to_rule[CssNestedDeclarations](this, raw_rule, CssParserNested.no)
if maybe_rule == none:
log_parse_error()
continue
let rule = maybe_rule else continue
# "Any @import rules must precede all other valid at-rules and style rules in a style sheet
# (ignoring @charset and @layer statement rules) and must not have any other valid at-rules
# or style rules between it and previous @import rules, or else the @import rule is invalid."
# https://drafts.csswg.org/css-cascade-5/#at-import
#
# "Any @namespace rules must follow all @charset and @import rules and precede all other
# non-ignored at-rules and style rules in a style sheet.
# ...
# A syntactically invalid @namespace rule (whether malformed or misplaced) must be ignored."
# https://drafts.csswg.org/css-namespaces/#syntax
let type_ = css_rule_type(rule)
match type_:
.layer_statement:
_ = type_
.import_:
if not import_rules_valid:
continue
.namespace:
import_rules_valid = false
if not namespace_rules_valid:
continue
_ = ak.hash_table_set(&this.m_declared_namespaces, css_namespace_rule_prefix(as_css_namespace_rule(rule)))
_:
import_rules_valid = false
namespace_rules_valid = false
ak.vector_append_t(&rules, rule)
return rules
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:180 Web::CSS::Parser::Parser::parse_as_stylesheet_contents.
pub func css_parser_parse_as_stylesheet_contents(this: CssParser*) -> ak.Vector[CssRule*]:
let raw_rules = css_parser_parse_a_stylesheets_contents(this, &this.m_token_stream)
return css_parser_convert_rules(this, &raw_rules)
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:186 Web::CSS::Parser::Parser::parse_as_css_stylesheet.
# https://drafts.csswg.org/css-syntax/#parse-a-css-stylesheet
pub func css_parser_parse_as_css_stylesheet(this: CssParser*, location: web_url.Url?, media_list: CssMediaList*? = none) -> CssCssStyleSheet*:
# To parse a CSS stylesheet, first parse a stylesheet.
let style_sheet = css_parser_parse_a_stylesheet(this, &this.m_token_stream, location)
let converted_rules = css_parser_convert_rules(this, &style_sheet.rules)
let rule_list = css_rule_list_create(css_parser_realm(this), ak.vector_span_const(&converted_rules))
var media = media_list
if media == none:
media = css_media_list_create(css_parser_realm(this), ak.Vector[CssMediaQuery*]())
return css_css_style_sheet_create(css_parser_realm(this), rule_list, media else trap("VERIFY: a media list"), location)
# mark: @supports ===============================================================================
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:197 Web::CSS::Parser::Parser::parse_as_supports.
pub func css_parser_parse_as_supports(this: CssParser*) -> CssSupports*?:
return css_parser_parse_a_supports(this, &this.m_token_stream)
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:203 Web::CSS::Parser::Parser::parse_a_supports.
func css_parser_parse_a_supports[T: css_syntax.TokenStreamToken[T]](this: CssParser*, tokens: css_syntax.TokenStream[T]*) -> CssSupports*?:
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
let component_values = css_parser_parse_a_list_of_component_values(this, tokens)
var token_stream = css_syntax.token_stream_create(component_values)
let maybe_condition = css_parser_parse_supports_condition(this, &token_stream)
css_syntax.token_stream_discard_whitespace(&token_stream)
if let condition = maybe_condition:
if not css_syntax.token_stream_has_next_token(&token_stream):
css_syntax.state_transaction_commit(&transaction)
return css_supports_create(condition)
return none
# mark: <boolean-expr> ==========================================================================
## Parse_boolean_expression's local `enum class Combinator`.
enum CssParserBooleanCombinator as u8:
and_ = 0
or_ = 1
## The `as_combinator` lambda of parse_boolean_expression.
func css_parser_as_combinator(token: const css_syntax.ComponentValue*) -> CssParserBooleanCombinator?:
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("and")):
return CssParserBooleanCombinator.and_
if ak.fly_string_equals_ignoring_ascii_case_string_view(&ident, ak.sv("or")):
return CssParserBooleanCombinator.or_
return none
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:219 Web::CSS::Parser::Parser::parse_boolean_expression.
# https://drafts.csswg.org/css-values-5/#typedef-boolean-expr
pub func css_parser_parse_boolean_expression(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, result_for_general_enclosed: CssMatchResult, parse_test: ak.Function1[css_syntax.TokenStream[css_syntax.ComponentValue]*, CssBooleanExpression*?]) -> CssBooleanExpression*?:
# <boolean-expr[ <test> ]> = not <boolean-expr-group> | <boolean-expr-group>
# [ [ and <boolean-expr-group> ]*
# | [ or <boolean-expr-group> ]* ]
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
css_syntax.token_stream_discard_whitespace(tokens)
let peeked_token = css_syntax.token_stream_next_token(tokens)
# `not <boolean-expr-group>`
if css_syntax.component_value_is_ident(&peeked_token, ak.sv("not")):
css_syntax.token_stream_discard_a_token(tokens)
css_syntax.token_stream_discard_whitespace(tokens)
if let child = css_parser_parse_boolean_expression_group(this, tokens, result_for_general_enclosed, parse_test):
css_syntax.token_stream_discard_whitespace(tokens)
css_syntax.state_transaction_commit(&transaction)
return css_boolean_not_expression_create(child).css_boolean_expression()
return none
# `<boolean-expr-group>
# [ [ and <boolean-expr-group> ]*
# | [ or <boolean-expr-group> ]* ]`
var children = ak.Vector[CssBooleanExpression*]()
var combinator: CssParserBooleanCombinator? = none
while css_syntax.token_stream_has_next_token(tokens):
if not ak.vector_is_empty(&children):
# Expect `and` or `or` here
let consumed = css_syntax.token_stream_consume_a_token(tokens)
let maybe_combinator = css_parser_as_combinator(&consumed) else return none
if combinator == none:
combinator = maybe_combinator
elif combinator != maybe_combinator:
return none
css_syntax.token_stream_discard_whitespace(tokens)
if let child = css_parser_parse_boolean_expression_group(this, tokens, result_for_general_enclosed, parse_test):
ak.vector_append_t(&children, child)
else:
return none
css_syntax.token_stream_discard_whitespace(tokens)
if ak.vector_is_empty(&children):
return none
css_syntax.state_transaction_commit(&transaction)
if ak.vector_size(&children) == 1:
return ak.vector_take_first(&children)
match (combinator else trap("VERIFY: a combinator")):
.and_: return css_boolean_and_expression_create(children).css_boolean_expression()
.or_: return css_boolean_or_expression_create(children).css_boolean_expression()
_: trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:303 Web::CSS::Parser::Parser::parse_boolean_expression_group.
pub func css_parser_parse_boolean_expression_group(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, result_for_general_enclosed: CssMatchResult, parse_test: ak.Function1[css_syntax.TokenStream[css_syntax.ComponentValue]*, CssBooleanExpression*?]) -> CssBooleanExpression*?:
# <boolean-expr-group> = <test> | ( <boolean-expr[ <test> ]> ) | <general-enclosed>
# `( <boolean-expr[ <test> ]> )`
let first_token = css_syntax.token_stream_next_token(tokens)
if css_component_value_is_paren_block(&first_token):
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
css_syntax.token_stream_discard_a_token(tokens)
css_syntax.token_stream_discard_whitespace(tokens)
let block = css_syntax.component_value_block(&first_token)
var child_tokens = css_syntax.token_stream_create(block.value)
if let expression = css_parser_parse_boolean_expression(this, &child_tokens, result_for_general_enclosed, parse_test):
if css_syntax.token_stream_has_next_token(&child_tokens):
return none
css_syntax.state_transaction_commit(&transaction)
return css_boolean_expression_in_parens_create(expression).css_boolean_expression()
# `<test>`
if let test_ = ak.function1_call(&parse_test, tokens):
return test_
# `<general-enclosed>`
if let general_enclosed = css_parser_parse_general_enclosed(this, tokens, result_for_general_enclosed):
return general_enclosed.css_boolean_expression()
return none
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:335 Web::CSS::Parser::Parser::parse_supports_condition.
# https://drafts.csswg.org/css-conditional-3/#typedef-supports-condition
pub func css_parser_parse_supports_condition(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssBooleanExpression*?:
ak.vector_append_t(&this.m_rule_context, CssRuleContext.supports_condition)
let maybe_condition = css_parser_parse_boolean_expression(this, tokens, CssMatchResult.false_, ak.Function1[css_syntax.TokenStream[css_syntax.ComponentValue]*, CssBooleanExpression*?](call = css_parser_parse_supports_condition_test, context = this))
_ = ak.vector_take_last(&this.m_rule_context)
return maybe_condition
## parse_supports_condition's `[this](auto& tokens) { return parse_supports_feature(tokens); }`.
func css_parser_parse_supports_condition_test(context: void*?, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssBooleanExpression*?:
return css_parser_parse_supports_feature((CssParser*)(context else trap("no parser")), tokens)
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:345 Web::CSS::Parser::Parser::parse_supports_feature.
# https://drafts.csswg.org/css-conditional-5/#typedef-supports-feature
pub func css_parser_parse_supports_feature(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssBooleanExpression*?:
# <supports-feature> = <supports-selector-fn> | <supports-font-tech-fn>
# | <supports-font-format-fn> | <supports-env-fn>
# | <supports-decl>
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
css_syntax.token_stream_discard_whitespace(tokens)
let first_token = css_syntax.token_stream_consume_a_token(tokens)
# `<supports-decl> = ( <declaration> )`
if css_component_value_is_paren_block(&first_token):
let block = css_syntax.component_value_block(&first_token)
var block_tokens = css_syntax.token_stream_create(block.value)
if let declaration = css_parser_parse_supports_declaration(this, &block_tokens):
css_syntax.state_transaction_commit(&transaction)
return css_boolean_expression_in_parens_create((CssBooleanExpression*)declaration).css_boolean_expression()
# `<supports-selector-fn> = selector( <complex-selector> )`
if css_syntax.component_value_is_function_string_view(&first_token, ak.sv("selector")):
# FIXME: Parsing and then converting back to a string is weird.
let function = css_syntax.component_value_function(&first_token)
var builder = ak.string_builder_new()
for item in ak.vector_span_const(&function.value):
let text = css_syntax.component_value_to_string(&item)
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&text))
css_syntax.state_transaction_commit(&transaction)
var selector_tokens = css_syntax.token_stream_create(function.value)
# A CSS processor is considered to support a CSS selector if it accepts that all aspects of that selector,
# recursively, (rather than considering any of its syntax to be unknown or invalid) and that selector doesn’t
# contain unknown -webkit- pseudo-elements.
# https://drafts.csswg.org/css-conditional-4/#dfn-support-selector
let maybe_selector: CssSelector*? = css_parser_parse_complex_selector(this, &selector_tokens, CssParserSelectorType.standalone) catch: recover none
var matches = false
if let selector = maybe_selector:
matches = not css_selector_contains_unknown_webkit_pseudo_element(selector)
return (CssBooleanExpression*)css_supports_selector_create(ak.string_builder_to_string_without_validation(&builder), matches)
# `<supports-font-tech-fn> = font-tech( <font-tech> )`
if css_syntax.component_value_is_function_string_view(&first_token, ak.sv("font-tech")):
let function = css_syntax.component_value_function(&first_token)
var tech_tokens = css_syntax.token_stream_create(function.value)
css_syntax.token_stream_discard_whitespace(&tech_tokens)
let tech_token = css_syntax.token_stream_consume_a_token(&tech_tokens)
css_syntax.token_stream_discard_whitespace(&tech_tokens)
if css_syntax.token_stream_has_next_token(&tech_tokens) or not css_syntax.component_value_is(&tech_token, .ident):
return none
css_syntax.state_transaction_commit(&transaction)
let tech_name = css_component_value_ident(&tech_token)
let matches = css_font_tech_is_supported_fly_string(tech_name)
return (CssBooleanExpression*)css_supports_font_tech_create(tech_name, matches)
# `<supports-font-format-fn> = font-format( <font-format> )`
if css_syntax.component_value_is_function_string_view(&first_token, ak.sv("font-format")):
let function = css_syntax.component_value_function(&first_token)
var format_tokens = css_syntax.token_stream_create(function.value)
css_syntax.token_stream_discard_whitespace(&format_tokens)
let format_token = css_syntax.token_stream_consume_a_token(&format_tokens)
css_syntax.token_stream_discard_whitespace(&format_tokens)
if css_syntax.token_stream_has_next_token(&format_tokens) or not css_syntax.component_value_is(&format_token, .ident):
return none
css_syntax.state_transaction_commit(&transaction)
let format_name = css_component_value_ident(&format_token)
let matches = css_font_format_is_supported(format_name)
return (CssBooleanExpression*)css_supports_font_format_create(format_name, matches)
# `<supports-env-fn> = env( <ident> )`
if css_syntax.component_value_is_function_string_view(&first_token, ak.sv("env")):
let function = css_syntax.component_value_function(&first_token)
var format_tokens = css_syntax.token_stream_create(function.value)
css_syntax.token_stream_discard_whitespace(&format_tokens)
let variable_token = css_syntax.token_stream_consume_a_token(&format_tokens)
css_syntax.token_stream_discard_whitespace(&format_tokens)
if css_syntax.token_stream_has_next_token(&format_tokens) or not css_syntax.component_value_is(&variable_token, .ident):
return none
css_syntax.state_transaction_commit(&transaction)
let variable_name = css_component_value_ident(&variable_token)
# https://drafts.csswg.org/css-conditional-5/#support-definition-env
# A CSS processor is considered to support an environment variable if the <ident> is a supported environment
# variable.
let matches = css_data.environment_variable_from_string(ak.fly_string_bytes_as_string_view(&variable_name)) != none
# Donor quirk: the C++ makes a Supports::FontFormat here, not a Supports::Env.
return (CssBooleanExpression*)css_supports_font_format_create(variable_name, matches)
return none
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:432 Web::CSS::Parser::Parser::parse_supports_declaration.
# https://drafts.csswg.org/css-conditional-5/#typedef-supports-decl
pub func css_parser_parse_supports_declaration(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssSupportsDeclaration*?:
# `<supports-decl> = ( <declaration> )`
# NB: Here, we only care about the <declaration> part.
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 declaration = css_parser_consume_a_declaration(this, tokens, CssParserNested.no, CssParserSaveOriginalText.yes):
css_syntax.token_stream_discard_whitespace(tokens)
if not css_syntax.token_stream_has_next_token(tokens):
css_syntax.state_transaction_commit(&transaction)
let original_full_text = declaration.original_full_text else trap("VERIFY: the declaration's original text")
return css_supports_declaration_create(original_full_text, css_parser_convert_to_style_property(this, &declaration) != none)
return none
# mark: Container queries =======================================================================
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:448 Web::CSS::Parser::Parser::parse_container_query_condition.
pub func css_parser_parse_container_query_condition(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssBooleanExpression*?:
return css_parser_parse_boolean_expression(this, tokens, CssMatchResult.false_, ak.Function1[css_syntax.TokenStream[css_syntax.ComponentValue]*, CssBooleanExpression*?](call = css_parser_parse_container_query_condition_test, context = this))
## parse_container_query_condition's `[this](auto& tokens) { return parse_container_query_feature(tokens); }`.
func css_parser_parse_container_query_condition_test(context: void*?, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssBooleanExpression*?:
return css_parser_parse_container_query_feature((CssParser*)(context else trap("no parser")), tokens)
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:455 Web::CSS::Parser::Parser::parse_container_query_feature.
pub func css_parser_parse_container_query_feature(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssBooleanExpression*?:
_ = this
_ = tokens
# https://drafts.csswg.org/css-conditional-5/#typedef-query-in-parens
# <query-in-parens> = ( <container-query> )
# | ( <size-feature> )
# | style( <style-query> )
# | scroll-state( <scroll-state-query> )
# | <general-enclosed>
# https://drafts.csswg.org/css-anchor-position-2/#container-rule-anchored
# <query-in-parens> = ...
# | anchored( <anchored-query> )
# NB: Spec isn't yet in terms of `<boolean-condition>`, so this is the closest definition to what we want.
# `( <container-query> )` and `<general-enclosed>` are handled by parse_boolean_expression() already.
# FIXME: `( <size-feature> )`
# FIXME: `style( <style-query> )`
# FIXME: `scroll-state( <scroll-state-query> )`
# FIXME: `anchored( <anchored-query> )`
return none
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:478 Web::CSS::Parser::Parser::parse_container_query.
pub func css_parser_parse_container_query(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssContainerQuery*?:
if let condition = css_parser_parse_container_query_condition(this, tokens):
return css_container_query_create(condition)
return none
# mark: <general-enclosed> ======================================================================
## The recursive `contains_only_any_value` lambda of parse_general_enclosed.
func css_parser_contains_only_any_value(values: const ak.Vector[css_syntax.ComponentValue]*) -> bool:
for value in ak.vector_span_const(values):
if css_syntax.component_value_is_function(&value):
let function = css_syntax.component_value_function(&value)
if not css_parser_contains_only_any_value(&function.value):
return false
continue
if css_syntax.component_value_is_block(&value):
let block = css_syntax.component_value_block(&value)
if not css_parser_contains_only_any_value(&block.value):
return false
continue
if not css_syntax.component_value_is_token(&value):
continue
# NB: Functions and blocks are emitted as component values, so any remaining bracket tokens are unmatched.
if css_token_type_is_one_of(css_component_value_token_type(&value),
[.invalid, .end_of_file, .bad_string, .bad_url, .function, .open_curly, .open_paren, .open_square, .close_curly, .close_paren, .close_square]):
return false
return true
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:486 Web::CSS::Parser::Parser::parse_general_enclosed.
# https://drafts.csswg.org/mediaqueries-5/#typedef-general-enclosed
pub func css_parser_parse_general_enclosed(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, result: CssMatchResult) -> CssGeneralEnclosed*?:
_ = this
# <general-enclosed> = [ <function-token> <any-value>? ) ] | [ ( <any-value>? ) ]
#
# https://drafts.csswg.org/css-syntax-3/#typedef-any-value
# "The <any-value> production is identical to <declaration-value>",
# and <declaration-value> does not contain "<<bad-string-token>>,
# <<bad-url-token>>, unmatched <<)-token>>, <<]-token>>, or
# <<}-token>>".
var transaction = css_syntax.token_stream_begin_transaction(tokens)
defer css_syntax.state_transaction_destroy(&transaction)
css_syntax.token_stream_discard_whitespace(tokens)
let first_token = css_syntax.token_stream_consume_a_token(tokens)
# `[ <function-token> <any-value>? ) ]`
if css_syntax.component_value_is_function(&first_token):
let function = css_syntax.component_value_function(&first_token)
if not css_parser_contains_only_any_value(&function.value):
return none
css_syntax.state_transaction_commit(&transaction)
return css_general_enclosed_create(css_syntax.component_value_to_string(&first_token), result)
# `( <any-value>? )`
if css_component_value_is_paren_block(&first_token):
let block = css_syntax.component_value_block(&first_token)
if not css_parser_contains_only_any_value(&block.value):
return none
css_syntax.state_transaction_commit(&transaction)
return css_general_enclosed_create(css_syntax.component_value_to_string(&first_token), result)
return none