dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / parser / parser_5.lucb
#==============================================================================================
#
# parser_5 - the CSS parser: the sizes attribute, substitution functions, accessors
#
# DESCRIPTION:
# Port of LibWeb/CSS/Parser/Parser.cpp (lines 2000-2253): parsing a sizes attribute,
# collecting the arbitrary substitution functions a value uses, ignored vendor
# prefixes, and the document / window / realm / quirks / SVG-attribute accessors. (The
# C++ explicit template instantiations of lines 2174-2223 have no Luce counterpart: the
# generics are instantiated where they are used.)
#
# Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2021-2026, Sam Atkins <sam@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: sizes="" ================================================================================
## The `remove_all_consecutive_whitespace_tokens_from_the_end_of` lambda of parse_as_sizes_attribute.
func css_parser_remove_trailing_whitespace_tokens(tokens: ak.Vector[css_syntax.ComponentValue]*):
while (not ak.vector_is_empty(tokens) and css_syntax.component_value_is_token(ak.vector_last(tokens)) and css_syntax.component_value_is(ak.vector_last(tokens), .whitespace)):
_ = ak.vector_take_last(tokens)
## `LengthStyleValue::create(Length(100, LengthUnit::Vw))`.
func css_parser_one_hundred_vw() -> CssStyleValue*:
var length: CssLength = ---
css_length_construct(&length, 100.0, css_data.LengthUnit.vw)
return css_length_style_value_create(length).css_style_value()
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:2001 Web::CSS::Parser::Parser::parse_as_sizes_attribute.
# https://html.spec.whatwg.org/multipage/images.html#parsing-a-sizes-attribute
pub func css_parser_parse_as_sizes_attribute(this: CssParser*, element: DomElement*, img: HtmlImageElement*? = none) -> CssStyleValue*:
# When asked to parse a sizes attribute from an element element, with an img element or null img:
# AD-HOC: If element has no sizes attribute, this algorithm always logs a parse error and then returns 100vw.
# The attribute is optional, so avoid spamming the debug log with false positives by just returning early.
if not dom_element_has_attribute(element, html_attribute_names_sizes):
return css_parser_one_hundred_vw()
# 1. Let unparsed sizes list be the result of parsing a comma-separated list of component values
# from the value of element's sizes attribute (or the empty string, if the attribute is absent).
# NOTE: The sizes attribute has already been tokenized into m_token_stream by this point.
var unparsed_sizes_list = css_parser_parse_a_comma_separated_list_of_component_values(this, &this.m_token_stream)
# 2. Let size be null.
var size: CssStyleValue*? = none
# 3. For each unparsed size in unparsed sizes list:
for i in 0..<ak.vector_size(&unparsed_sizes_list):
let unparsed_size = ak.vector_at(&unparsed_sizes_list, i)
# 1. Remove all consecutive <whitespace-token>s from the end of unparsed size.
# If unparsed size is now empty, that is a parse error; continue.
css_parser_remove_trailing_whitespace_tokens(unparsed_size)
if ak.vector_is_empty(unparsed_size):
log_parse_error()
css_error_reporter_report(css_error_reporter_the(), CssParsingError.css_invalid_value_error(CssInvalidValueError(
value_type = ak.fly_string_from_str("sizes attribute"),
value_string = css_syntax.token_stream_dump_string(&this.m_token_stream),
description = ak.string_from_str("Failed in step 3.1; all whitespace"),
)))
continue
# 2. If the last component value in unparsed size is a valid non-negative <source-size-value>,
# then set size to its value and remove the component value from unparsed size.
# Any CSS function other than the math functions is invalid.
# Otherwise, there is a parse error; continue.
var last_value_stream = css_syntax.token_stream_of_single_token(ak.vector_last(unparsed_size))
if let source_size_value = css_parser_parse_source_size_value(this, &last_value_stream):
size = source_size_value
_ = ak.vector_take_last(unparsed_size)
else:
log_parse_error()
css_error_reporter_report(css_error_reporter_the(), CssParsingError.css_invalid_value_error(CssInvalidValueError(
value_type = ak.fly_string_from_str("sizes attribute"),
value_string = css_syntax.token_stream_dump_string(&this.m_token_stream),
description = ak.string_from_str("Failed in step 3.2; couldn't parse {} as a <source-size-value>"),
)))
continue
# 3. If size is auto, and img is not null, and img is being rendered, and img allows auto-sizes,
# then set size to the concrete object size width of img, in CSS pixels.
# FIXME: "img is being rendered" - we just see if it has a bitmap for now
if css_style_value_has_auto(size else trap("VERIFY: a size")):
if let image = img:
if html_image_element_immutable_bitmap(image) != none and html_image_element_allows_auto_sizes(image):
# FIXME: The spec doesn't seem to tell us how to determine the concrete size of an <img>, so use the default sizing algorithm.
# Should this use some of the methods from FormattingContext?
# NOTE (on the default size 300x150): https://html.spec.whatwg.org/multipage/rendering.html#img-contain-size
let concrete_size = css_run_default_sizing_algorithm(
gfx.CssPixels.from_int((i64)html_image_element_width(image)), gfx.CssPixels.from_int((i64)html_image_element_height(image)),
CssSizeWithAspectRatio(
width = gfx.CssPixels.from_int((i64)html_image_element_natural_width(image)),
height = gfx.CssPixels.from_int((i64)html_image_element_natural_height(image)),
aspect_ratio = layout_image_provider_intrinsic_aspect_ratio(&image.image_provider),
), gfx.CssPixelSize(m_width = gfx.CssPixels.from_int(300), m_height = gfx.CssPixels.from_int(150)))
size = css_length_style_value_create(css_length_make_px_css_pixels(concrete_size.m_width)).css_style_value()
# 4. Remove all consecutive <whitespace-token>s from the end of unparsed size.
# If unparsed size is now empty:
css_parser_remove_trailing_whitespace_tokens(unparsed_size)
if ak.vector_is_empty(unparsed_size):
# 1. If this was not the last item in unparsed sizes list, that is a parse error.
if i != ak.vector_size(&unparsed_sizes_list) - 1:
log_parse_error()
css_error_reporter_report(css_error_reporter_the(), CssParsingError.css_invalid_value_error(CssInvalidValueError(
value_type = ak.fly_string_from_str("sizes attribute"),
value_string = css_syntax.token_stream_dump_string(&this.m_token_stream),
description = css_string_formatted("Failed in step 3.4.1; is unparsed size #{}, count {}",
[ak.arg_u64((u64)i), ak.arg_u64((u64)ak.vector_size(&unparsed_sizes_list))]),
)))
# 2. If size is not auto, then return size. Otherwise, continue.
let current_size = size else trap("VERIFY: a size")
if not css_style_value_has_auto(current_size):
return current_size
continue
# 5. Parse the remaining component values in unparsed size as a <media-condition>.
# If it does not parse correctly, or it does parse correctly but the <media-condition> evaluates to false, continue.
var token_stream = css_syntax.token_stream_create(*unparsed_size)
let media_condition = css_parser_parse_media_condition(this, &token_stream) else continue
# https://drafts.csswg.org/mediaqueries-5/#evaluating
# "If the result of any of the above productions is used in any
# context that expects a two-valued boolean, 'unknown' must be
# converted to 'false'."
if this.m_document != none and not css_boolean_expression_evaluate_to_boolean(media_condition, this.m_document):
continue
# 5. If size is not auto, then return size. Otherwise, continue.
let current_size = size else trap("VERIFY: a size")
if not css_style_value_has_auto(current_size):
return current_size
# 4. Return 100vw.
return css_parser_one_hundred_vw()
# mark: Arbitrary substitution functions ========================================================
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:2115 Web::CSS::Parser::Parser::collect_arbitrary_substitution_function_presence.
pub func css_parser_collect_arbitrary_substitution_function_presence_vector(component_values: const ak.Vector[css_syntax.ComponentValue]*, presence: css_syntax.SubstitutionFunctionsPresence*) -> !:
let list = ak.vector_span_const(component_values)
for component_value in &list:
css_parser_collect_arbitrary_substitution_function_presence_component_value(component_value, presence) catch:
error(css_parser_parse_error_syntax_error, "")
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:2125 Web::CSS::Parser::Parser::collect_arbitrary_substitution_function_presence.
pub func css_parser_collect_arbitrary_substitution_function_presence_component_value(component_value: const css_syntax.ComponentValue*, presence: css_syntax.SubstitutionFunctionsPresence*) -> !:
if css_syntax.component_value_is_function(component_value):
let function = css_syntax.component_value_function(component_value)
if let arbitrary_substitution_function = css_to_arbitrary_substitution_function(function.name):
if css_parse_according_to_argument_grammar(arbitrary_substitution_function, function.value) == none:
error(css_parser_parse_error_syntax_error, "")
match arbitrary_substitution_function:
.attr: presence.attr = true
.env: presence.env = true
.if_: presence.if_ = true
.inherit: presence.inherit = true
.var_: presence.var_ = true
_: trap("VERIFY_NOT_REACHED")
try css_parser_collect_arbitrary_substitution_function_presence_vector(&function.value, presence)
return
if css_syntax.component_value_is_block(component_value):
let block = css_syntax.component_value_block(component_value)
try css_parser_collect_arbitrary_substitution_function_presence_vector(&block.value, presence)
return
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:2161 Web::CSS::Parser::Parser::has_ignored_vendor_prefix.
pub func css_parser_has_ignored_vendor_prefix(string: ak.StringView) -> bool:
if not ak.string_view_starts_with_u8(&string, '-'):
return false
if ak.string_view_starts_with_string_view(&string, ak.sv("--")):
return false
if ak.string_view_starts_with_string_view(&string, ak.sv("-libweb-")):
return false
if ak.string_view_count_u8(&string, '-') == 1:
return false
return true
# mark: Accessors ===============================================================================
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:2225 Web::CSS::Parser::Parser::document.
pub func css_parser_document(this: const CssParser*) -> DomDocument*?:
return this.m_document
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:2230 Web::CSS::Parser::Parser::window.
pub func css_parser_window(this: const CssParser*) -> HtmlWindow*?:
let document = this.m_document else return none
return dom_document_window(document)
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:2237 Web::CSS::Parser::Parser::realm.
pub func css_parser_realm(this: const CssParser*) -> Realm*:
return this.m_realm else trap("VERIFY: the parser has a realm")
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:2243 Web::CSS::Parser::Parser::in_quirks_mode.
pub func css_parser_in_quirks_mode(this: const CssParser*) -> bool:
if let document = this.m_document:
return dom_document_in_quirks_mode(document)
return false
## Ported from Libraries/LibWeb/CSS/Parser/Parser.cpp:2248 Web::CSS::Parser::Parser::is_parsing_svg_presentation_attribute.
pub func css_parser_is_parsing_svg_presentation_attribute(this: const CssParser*) -> bool:
return this.m_parsing_mode == CssParsingMode.svg_presentation_attribute