dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / css_descriptors.lucb
#==============================================================================================
#
# css_descriptors - CSS::CSSDescriptors: the descriptor block of an at-rule (@font-face, ...)
#
# DESCRIPTION:
# Port of LibWeb/CSS/CSSDescriptors.cpp (lines 1-313) and the inline members of
# CSSDescriptors.h (lines 18-49): a CSSStyleDeclaration over the descriptors of one
# at-rule, with setProperty(), removeProperty(), getPropertyValue(), serialization and
# cssText, and the descriptor shorthands (@page's margin).
#
# Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: CSSDescriptors.h ========================================================================
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.h:29 Web::CSS::CSSDescriptors::descriptors.
pub func css_descriptors_descriptors(this: CssDescriptors*) -> ak.Vector[CssDescriptor]:
return this.m_descriptors
# mark: CSSDescriptors.cpp ======================================================================
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:16 Web::CSS::CSSDescriptors::CSSDescriptors.
pub func css_descriptors_construct(this: CssDescriptors*, realm: Realm*, at_rule_id: css_data.AtRuleId, descriptors: ak.Vector[CssDescriptor]):
css_style_declaration_construct(this.css_style_declaration(), realm, CssStyleDeclarationComputed.no, CssStyleDeclarationReadonly.no)
this.m_at_rule_id = at_rule_id
this.m_descriptors = descriptors
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:26 Web::CSS::CSSDescriptors::length (override).
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-length
pub func css_descriptors_length_impl(base: CssStyleDeclaration*) -> usize:
let this = (CssDescriptors*)base
# The length attribute must return the number of CSS declarations in the declarations.
return ak.vector_size(&this.m_descriptors)
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:33 Web::CSS::CSSDescriptors::item (override).
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-item
pub func css_descriptors_item_impl(base: CssStyleDeclaration*, index: usize) -> ak.String:
let this = (CssDescriptors*)base
# The item(index) method must return the property name of the CSS declaration at position index.
# If there is no indexth object in the collection, then the method must return the empty string.
if index >= css_style_declaration_length(base):
return ak.string_from_str("")
let name = css_descriptor_name_and_id_name(&ak.vector_at(&this.m_descriptors, index).descriptor_name_and_id)
return ak.fly_string_to_string_const(&name)
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:44 Web::CSS::CSSDescriptors::set_a_css_declaration.
# https://drafts.csswg.org/cssom/#set-a-css-declaration
func css_descriptors_set_a_css_declaration(this: CssDescriptors*, descriptor_name_and_id: CssDescriptorNameAndId, value: CssStyleValue*, arg2: css_syntax.Important) -> bool:
_ = arg2
assert(not css_style_declaration_is_computed(this.css_style_declaration()))
for descriptor in &ak.vector_span(&this.m_descriptors):
if css_descriptor_name_and_id_eq(&descriptor.descriptor_name_and_id, descriptor_name_and_id):
if css_style_value_equals(descriptor.value, value):
return false
descriptor.value = value
return true
ak.vector_append_t(&this.m_descriptors, CssDescriptor(descriptor_name_and_id = descriptor_name_and_id, value = value))
return true
## The captures of set_property's per-longhand lambda.
struct CssDescriptorsSetPropertyLonghand:
var this: CssDescriptors*
var updated: bool*
var priority: ak.StringView
## set_property's per-longhand lambda.
func css_descriptors_set_property_longhand(context: void*?, longhand_name_and_id: const CssDescriptorNameAndId*, longhand_value: CssStyleValue*?):
let captures = (CssDescriptorsSetPropertyLonghand*)(context else trap("no captures"))
let value = longhand_value else trap("VERIFY: longhand_value")
# 1. Let longhand result be the result of set the CSS declaration longhand with the appropriate value(s)
# from component value list, with the important flag set if priority is not the empty string, and unset
# otherwise, and with the list of declarations being the declarations.
let important = css_syntax.Important.no if ak.string_view_is_empty(&captures.priority) else css_syntax.Important.yes
let longhand_result = css_descriptors_set_a_css_declaration(captures.this, *longhand_name_and_id, value, important)
# 2. If longhand result is true, let updated be true.
if longhand_result:
*captures.updated = true
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:65 Web::CSS::CSSDescriptors::set_property (override).
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
pub func css_descriptors_set_property_impl(base: CssStyleDeclaration*, property: ak.FlyString, value: ak.StringView, priority: ak.StringView) -> !:
let this = (CssDescriptors*)base
# 1. If the readonly flag is set, then throw a NoModificationAllowedError exception.
if css_style_declaration_is_readonly(base):
try idl_throw(.idl_dom_exception(idl_no_modification_allowed_error_create(bind_platform_object_realm(this.bind_platform_object()), ak.utf16_string_from_str("Cannot modify properties of readonly CSSStyleDeclaration"))))
# 2. If property is not a custom property, follow these substeps:
# 1. Let property be property converted to ASCII lowercase.
# 2. If property is not a case-sensitive match for a supported CSS property, then return.
let descriptor_name_and_id = css_descriptor_name_and_id_from_name(this.m_at_rule_id, property) else return
# 3. If value is the empty string, invoke removeProperty() with property as argument and return.
if ak.string_view_is_empty(&value):
_ = css_style_declaration_remove_property(base, property) else trap("MUST: remove_property")
return
# 4. If priority is not the empty string and is not an ASCII case-insensitive match for the string "important", then return.
if not ak.string_view_is_empty(&priority) and not ak.string_view_equals_ignoring_ascii_case(&priority, ak.sv("important")):
return
# 5. Let component value list be the result of parsing value for property property.
var parsing_params: CssParsingParams = ---
css_parsing_params_construct_css_parsing_mode(&parsing_params)
# 6. If component value list is null, then return.
let component_value_list = parse_css_descriptor(parsing_params, this.m_at_rule_id, descriptor_name_and_id, value) else return
# 7. Let updated be false.
var updated = false
# 8. If property is a shorthand property, then for each longhand property longhand that property maps to, in canonical order, follow these substeps:
if css_is_shorthand(this.m_at_rule_id, descriptor_name_and_id):
var captures = CssDescriptorsSetPropertyLonghand(this = this, updated = &updated, priority = priority)
css_for_each_expanded_longhand(this.m_at_rule_id, descriptor_name_and_id, component_value_list, ak.Function2[const CssDescriptorNameAndId*, CssStyleValue*?, unit](call = css_descriptors_set_property_longhand, context = &captures))
# 9. Otherwise, let updated be the result of set the CSS declaration property with value component value list,
# with the important flag set if priority is not the empty string, and unset otherwise, and with the list of
# declarations being the declarations.
else:
let important = css_syntax.Important.yes if not ak.string_view_is_empty(&priority) else css_syntax.Important.no
updated = css_descriptors_set_a_css_declaration(this, descriptor_name_and_id, component_value_list, important)
# 10. If updated is true, update style attribute for the CSS declaration block.
if updated:
css_style_declaration_update_style_attribute(base)
## The captures of remove_property's per-longhand lambda.
struct CssDescriptorsRemovePropertyLonghand:
var this: CssDescriptors*
var removed: bool*
## Remove the first descriptor named `name_and_id` (`m_descriptors.remove_first_matching(...)`).
func css_descriptors_remove_first(this: CssDescriptors*, name_and_id: CssDescriptorNameAndId) -> bool:
for i in 0..<ak.vector_size(&this.m_descriptors):
if css_descriptor_name_and_id_eq(&ak.vector_at(&this.m_descriptors, i).descriptor_name_and_id, name_and_id):
ak.vector_remove(&this.m_descriptors, i)
return true
return false
## remove_property's per-longhand lambda.
func css_descriptors_remove_property_longhand(context: void*?, longhand_name_and_id: const CssDescriptorNameAndId*, arg1: CssStyleValue*?):
_ = arg1
let captures = (CssDescriptorsRemovePropertyLonghand*)(context else trap("no captures"))
# 1. If longhand is not a property name of a CSS declaration in the declarations, continue.
# 2. Remove that CSS declaration and let removed be true.
if css_descriptors_remove_first(captures.this, *longhand_name_and_id):
*captures.removed = true
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:131 Web::CSS::CSSDescriptors::remove_property (override).
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty
pub func css_descriptors_remove_property_impl(base: CssStyleDeclaration*, property: ak.FlyString) -> ak.String!:
let this = (CssDescriptors*)base
# 1. If the readonly flag is set, then throw a NoModificationAllowedError exception.
if css_style_declaration_is_readonly(base):
try idl_throw(.idl_dom_exception(idl_no_modification_allowed_error_create(bind_platform_object_realm(this.bind_platform_object()), ak.utf16_string_from_str("Cannot modify properties of readonly CSSStyleDeclaration"))))
# 2. If property is not a custom property, let property be property converted to ASCII lowercase.
# AD-HOC: We compare names case-insensitively instead.
# 3. Let value be the return value of invoking getPropertyValue() with property as argument.
let value = css_style_declaration_get_property_value(base, property)
# 4. Let removed be false.
var removed = false
let descriptor_name_and_id = css_descriptor_name_and_id_from_name(this.m_at_rule_id, property)
if let name_and_id = descriptor_name_and_id:
# 5. If property is a shorthand property, for each longhand property longhand that property maps to:
if css_is_shorthand(this.m_at_rule_id, name_and_id):
var captures = CssDescriptorsRemovePropertyLonghand(this = this, removed = &removed)
css_for_each_expanded_longhand(this.m_at_rule_id, name_and_id, none, ak.Function2[const CssDescriptorNameAndId*, CssStyleValue*?, unit](call = css_descriptors_remove_property_longhand, context = &captures))
# 6. Otherwise, if property is a case-sensitive match for a property name of a CSS declaration in the
# declarations, remove that CSS declaration and let removed be true.
else:
removed = css_descriptors_remove_first(this, name_and_id)
# 7. If removed is true, Update style attribute for the CSS declaration block.
if removed:
css_style_declaration_update_style_attribute(base)
# 8. Return value.
return value
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:173 Web::CSS::CSSDescriptors::get_property_value (override).
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue
pub func css_descriptors_get_property_value_impl(base: CssStyleDeclaration*, property: ak.FlyString) -> ak.String:
let this = (CssDescriptors*)base
# 1. If property is not a custom property, follow these substeps: ...
# NB: These substeps only apply to shorthands, and descriptors cannot be shorthands.
# 2. If property is a case-sensitive match for a property name of a CSS declaration in the declarations, then
# return the result of invoking serialize a CSS value of that declaration.
if let descriptor_name_and_id = css_descriptor_name_and_id_from_name(this.m_at_rule_id, property):
if let match_ = css_descriptors_descriptor(this, descriptor_name_and_id):
return css_style_value_to_string(match_, css_syntax.SerializationMode.normal)
# 3. Return the empty string.
return ak.string_from_str("")
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:191 Web::CSS::CSSDescriptors::get_property_priority (override).
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertypriority
pub func css_descriptors_get_property_priority_impl(base: CssStyleDeclaration*, property: ak.FlyString) -> ak.StringView:
_ = base
_ = property
# AD-HOC: It's not valid for descriptors to be !important.
return ak.sv("")
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:198 Web::CSS::CSSDescriptors::serialized (override).
# https://drafts.csswg.org/cssom/#serialize-a-css-declaration-block
pub func css_descriptors_serialized_impl(base: CssStyleDeclaration*) -> ak.String:
let this = (CssDescriptors*)base
# 1. Let list be an empty array.
var list: ak.Vector[ak.String]
ak.vector_ensure_capacity(&list, ak.vector_size(&this.m_descriptors))
# 2. Let already serialized be an empty array.
# AD-HOC: Not needed as we don't have shorthands.
# 3. Declaration loop: For each CSS declaration declaration in declaration block’s declarations, follow these substeps:
for descriptor in ak.vector_span(&this.m_descriptors):
# 1. Let property be declaration’s property name.
let property = css_descriptor_name_and_id_name(&descriptor.descriptor_name_and_id)
# 2. If property is in already serialized, continue with the steps labeled declaration loop.
# AD-HOC: Not needed as we don't have shorthands.
# 3. If property maps to one or more shorthand properties, let shorthands be an array of those shorthand properties, in preferred order.
# 4. Shorthand loop: For each shorthand in shorthands, follow these substeps: ...
# NB: Descriptors can't be shorthands.
# 5. Let value be the result of invoking serialize a CSS value of declaration.
let value = css_style_value_to_string(descriptor.value, css_syntax.SerializationMode.normal)
# 6. Let serialized declaration be the result of invoking serialize a CSS declaration with property name property, value value, and the important flag set if declaration has its important flag set.
let serialized_declaration = css_serialize_a_css_declaration(ak.fly_string_bytes_as_string_view(&property), ak.string_bytes_as_string_view(&value), css_syntax.Important.no)
# 7. Append serialized declaration to list.
ak.vector_append_t(&list, serialized_declaration)
# 8. Append property to already serialized.
# AD-HOC: Not needed as we don't have shorthands.
# 4. Return list joined with " " (U+0020).
return ak.string_join_string(ak.sv(" "), ak.vector_span_const(&list)) else trap("MUST: String::join")
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:238 Web::CSS::CSSDescriptors::set_css_text (override).
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
pub func css_descriptors_set_css_text_impl(base: CssStyleDeclaration*, value: ak.StringView) -> !:
let this = (CssDescriptors*)base
# 1. If the readonly flag is set, then throw a NoModificationAllowedError exception.
if css_style_declaration_is_readonly(base):
try idl_throw(.idl_dom_exception(idl_no_modification_allowed_error_create(bind_platform_object_realm(this.bind_platform_object()), ak.utf16_string_from_str("Cannot modify properties of readonly CSSStyleDeclaration"))))
# 2. Empty the declarations.
ak.vector_clear(&this.m_descriptors)
# 3. Parse the given value and, if the return value is not the empty list, insert the items in the list into the
# declarations, in specified order.
var parsing_params: CssParsingParams = ---
css_parsing_params_construct_css_parsing_mode(&parsing_params)
let descriptors = parse_css_descriptor_declaration_block(parsing_params, this.m_at_rule_id, value)
if not ak.vector_is_empty(&descriptors):
this.m_descriptors = descriptors
# 4. Update style attribute for the CSS declaration block.
css_style_declaration_update_style_attribute(base)
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:259 Web::CSS::CSSDescriptors::visit_edges (override).
pub func css_descriptors_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (CssDescriptors*)base
css_style_declaration_visit_edges_impl(base, visitor)
for descriptor in ak.vector_span(&this.m_descriptors):
css_style_value_visit_edges(descriptor.value, visitor)
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:267 Web::CSS::CSSDescriptors::descriptor.
pub func css_descriptors_descriptor(this: CssDescriptors*, descriptor_name_and_id: CssDescriptorNameAndId) -> CssStyleValue*?:
for descriptor in ak.vector_span(&this.m_descriptors):
if css_descriptor_name_and_id_eq(&descriptor.descriptor_name_and_id, descriptor_name_and_id):
return descriptor.value
return none
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:277 Web::CSS::CSSDescriptors::descriptor_or_initial_value.
pub func css_descriptors_descriptor_or_initial_value(this: CssDescriptors*, descriptor_name_and_id: CssDescriptorNameAndId) -> CssStyleValue*?:
if let value = css_descriptors_descriptor(this, descriptor_name_and_id):
return value
return css_descriptor_initial_value(this.m_at_rule_id, css_descriptor_name_and_id_id(&descriptor_name_and_id))
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:285 Web::CSS::is_shorthand.
pub func css_is_shorthand(at_rule: css_data.AtRuleId, descriptor: CssDescriptorNameAndId) -> bool:
if at_rule == css_data.AtRuleId.page and css_descriptor_name_and_id_id(&descriptor) == css_data.DescriptorId.margin:
return true
return false
## Ported from Libraries/LibWeb/CSS/CSSDescriptors.cpp:293 Web::CSS::for_each_expanded_longhand.
pub func css_for_each_expanded_longhand(at_rule: css_data.AtRuleId, descriptor: CssDescriptorNameAndId, value: CssStyleValue*?, callback: ak.Function2[const CssDescriptorNameAndId*, CssStyleValue*?, unit]):
if at_rule == css_data.AtRuleId.page and css_descriptor_name_and_id_id(&descriptor) == css_data.DescriptorId.margin:
let margin_top = css_descriptor_name_and_id_from_id(css_data.DescriptorId.margin_top)
let margin_right = css_descriptor_name_and_id_from_id(css_data.DescriptorId.margin_right)
let margin_bottom = css_descriptor_name_and_id_from_id(css_data.DescriptorId.margin_bottom)
let margin_left = css_descriptor_name_and_id_from_id(css_data.DescriptorId.margin_left)
if value == none:
ak.function2_call(&callback, &margin_top, none)
ak.function2_call(&callback, &margin_right, none)
ak.function2_call(&callback, &margin_bottom, none)
ak.function2_call(&callback, &margin_left, none)
return
let style_value = value else trap("unreachable")
let shorthand_value = css_style_value_as_shorthand(style_value)
ak.function2_call(&callback, &margin_top, css_shorthand_style_value_longhand(shorthand_value, css_data.PropertyId.margin_top))
ak.function2_call(&callback, &margin_right, css_shorthand_style_value_longhand(shorthand_value, css_data.PropertyId.margin_right))
ak.function2_call(&callback, &margin_bottom, css_shorthand_style_value_longhand(shorthand_value, css_data.PropertyId.margin_bottom))
ak.function2_call(&callback, &margin_left, css_shorthand_style_value_longhand(shorthand_value, css_data.PropertyId.margin_left))