dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / css_style_properties_1.lucb
#==============================================================================================
#
# css_style_properties_1 - CSS::CSSStyleProperties: creation, length/item, setProperty()
#
# DESCRIPTION:
# Port of LibWeb/CSS/CSSStyleProperties.cpp (lines 1-353) and the inline members of
# CSSStyleProperties.h (lines 18-92): the three creation paths (rule declarations,
# getComputedStyle()'s resolved style, an element's inline style), the specified order of
# declarations, length and item(), custom properties, setProperty(), and the helpers that
# turn computed values back into style values. The rest of the file is
# css_style_properties_2 to _4.
#
# Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2023-2025, Sam Atkins <sam@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: CSSStyleProperties.h ====================================================================
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.h:48 Web::CSS::CSSStyleProperties::properties.
pub func css_style_properties_properties(this: CssStyleProperties*) -> ak.Vector[CssStyleProperty]:
return this.m_properties
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.h:49 Web::CSS::CSSStyleProperties::custom_properties.
pub func css_style_properties_custom_properties(this: CssStyleProperties*) -> ak.OrderedHashMap[ak.FlyString, CssStyleProperty, ak.FlyStringTraits]:
return this.m_custom_properties
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.h:51 Web::CSS::CSSStyleProperties::custom_property_count.
pub func css_style_properties_custom_property_count(this: CssStyleProperties*) -> usize:
return ak.ordered_hash_map_size(&this.m_custom_properties)
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.h:71 Web::CSS::CSSStyleProperties::generated_style_properties_to_css_style_properties (override).
pub func css_style_properties_generated_style_properties_to_css_style_properties_impl(base: BindGeneratedCssStyleProperties*) -> CssStyleProperties*:
# `return *this;`: the mixin's CSSStyleProperties.
return (CssStyleProperties*)((u8*)base - memory.offset_of(CssStyleProperties, generated_css_style_properties))
# mark: Creation ================================================================================
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:43 Web::CSS::CSSStyleProperties::create.
pub func css_style_properties_create(realm: Realm*, properties: ak.Vector[CssStyleProperty], custom_properties: ak.OrderedHashMap[ak.FlyString, CssStyleProperty, ak.FlyStringTraits]) -> CssStyleProperties*:
# https://drafts.csswg.org/cssom/#dom-cssstylerule-style
# The style attribute must return a CSSStyleProperties object for the style rule, with the following properties:
# computed flag: Unset.
# readonly flag: Unset.
# declarations: The declared declarations in the rule, in specified order.
# parent CSS rule: The context object.
# owner node: Null.
var declarations = ak.vector_clone(&properties) # by-value copy in C++
return realm_create_css_style_properties(realm, realm, CssStyleDeclarationComputed.no, CssStyleDeclarationReadonly.no, css_style_properties_convert_declarations_to_specified_order(&declarations), custom_properties, none)
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:55 Web::CSS::CSSStyleProperties::create_resolved_style.
pub func css_style_properties_create_resolved_style(realm: Realm*, element_reference: DomAbstractElement?) -> CssStyleProperties*:
# https://drafts.csswg.org/cssom/#dom-window-getcomputedstyle
# 6. Return a live CSSStyleProperties object with the following properties:
# computed flag: Set.
# readonly flag: Set.
# declarations: decls.
# parent CSS rule: Null.
# owner node: obj.
# AD-HOC: Rather than instantiate with a list of decls, they're generated on demand.
return realm_create_css_style_properties(realm, realm, CssStyleDeclarationComputed.yes, CssStyleDeclarationReadonly.yes, ak.Vector[CssStyleProperty](), ak.OrderedHashMap[ak.FlyString, CssStyleProperty, ak.FlyStringTraits](), element_reference)
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:68 Web::CSS::CSSStyleProperties::create_element_inline_style.
pub func css_style_properties_create_element_inline_style(element_reference: DomAbstractElement, properties: ak.Vector[CssStyleProperty], custom_properties: ak.OrderedHashMap[ak.FlyString, CssStyleProperty, ak.FlyStringTraits]) -> CssStyleProperties*:
# https://drafts.csswg.org/cssom/#dom-elementcssinlinestyle-style
# The style attribute must return a CSS declaration block object whose readonly flag is unset, whose parent CSS
# rule is null, and whose owner node is the context object.
let realm = bind_platform_object_realm(dom_abstract_element_element_const(&element_reference).bind_platform_object())
var declarations = ak.vector_clone(&properties) # by-value copy in C++
return realm_create_css_style_properties(realm, realm, CssStyleDeclarationComputed.no, CssStyleDeclarationReadonly.no, css_style_properties_convert_declarations_to_specified_order(&declarations), custom_properties, element_reference)
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:77 Web::CSS::CSSStyleProperties::CSSStyleProperties.
pub func css_style_properties_construct(this: CssStyleProperties*, realm: Realm*, computed: CssStyleDeclarationComputed, readonly: CssStyleDeclarationReadonly, properties: ak.Vector[CssStyleProperty], custom_properties: ak.OrderedHashMap[ak.FlyString, CssStyleProperty, ak.FlyStringTraits], owner_node: DomAbstractElement?):
css_style_declaration_construct(this.css_style_declaration(), realm, computed, readonly)
this.generated_css_style_properties.vtable = &css_style_properties_generated_css_style_properties_vtable
this.m_properties = properties
this.m_custom_properties = custom_properties
css_style_declaration_set_owner_node(this.css_style_declaration(), owner_node)
## The index of the declaration of `property_id` in `declarations` (C++'s
## `find_first_index_if([&](auto declaration) { return declaration.property_id == property_id; })`).
func css_style_properties_index_of(declarations: const ak.Vector[CssStyleProperty]*, property_id: css_data.PropertyId) -> usize?:
for i in 0..<ak.vector_size(declarations):
if ak.vector_at_const(declarations, i).property_id == property_id:
return i
return none
## The captures of convert_declarations_to_specified_order's per-longhand lambda.
struct CssStylePropertiesSpecifiedOrderLonghand:
var specified_order_declarations: ak.Vector[CssStyleProperty]*
var declaration: CssStyleProperty
## convert_declarations_to_specified_order's per-longhand lambda.
func css_style_properties_specified_order_longhand(context: void*?, longhand_id: css_data.PropertyId, longhand_property_value: CssStyleValue*):
let captures = (CssStylePropertiesSpecifiedOrderLonghand*)(context else trap("no captures"))
let specified_order_declarations = captures.specified_order_declarations
let declaration = captures.declaration
if let existing_entry_index = css_style_properties_index_of(specified_order_declarations, longhand_id):
# If there is an existing entry for this property and it is a higher cascading order than the current entry, skip the current entry.
if ak.vector_at(specified_order_declarations, existing_entry_index).important == css_syntax.Important.yes and declaration.important == css_syntax.Important.no:
return
# Otherwise the existing entry has a lower cascading order and is removed.
ak.vector_remove(specified_order_declarations, existing_entry_index)
ak.vector_append_t(specified_order_declarations, CssStyleProperty(important = declaration.important, property_id = longhand_id, value = longhand_property_value))
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:86 Web::CSS::CSSStyleProperties::convert_declarations_to_specified_order.
# https://drafts.csswg.org/cssom/#concept-declarations-specified-order
func css_style_properties_convert_declarations_to_specified_order(declarations: ak.Vector[CssStyleProperty]*) -> ak.Vector[CssStyleProperty]:
# The specified order for declarations is the same as specified, but with shorthand properties expanded into their
# longhand properties, in canonical order. If a property is specified more than once (after shorthand expansion), only
# the one with greatest cascading order must be represented, at the same relative position as it was specified.
var specified_order_declarations: ak.Vector[CssStyleProperty]
for declaration in ak.vector_span(declarations):
var captures = CssStylePropertiesSpecifiedOrderLonghand(specified_order_declarations = &specified_order_declarations, declaration = declaration)
css_style_computer_for_each_property_expanding_shorthands(declaration.property_id, declaration.value, ak.Function2[css_data.PropertyId, CssStyleValue*, unit](call = css_style_properties_specified_order_longhand, context = &captures))
return specified_order_declarations
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:115 Web::CSS::CSSStyleProperties::initialize (override).
pub func css_style_properties_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.css_style_properties)
css_style_declaration_initialize_impl(base, realm)
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:121 Web::CSS::CSSStyleProperties::visit_edges (override).
pub func css_style_properties_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (CssStyleProperties*)base
css_style_declaration_visit_edges_impl(base, visitor)
for property in ak.vector_span(&this.m_properties):
css_style_value_visit_edges(property.value, visitor)
# mark: length, item and custom properties ======================================================
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:130 Web::CSS::CSSStyleProperties::length (override).
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-length
pub func css_style_properties_length_impl(base: CssStyleDeclaration*) -> usize:
let this = (CssStyleProperties*)base
# The length attribute must return the number of CSS declarations in the declarations.
if css_style_declaration_is_computed(base):
if css_style_declaration_owner_node(base) == none:
return 0
return css_data.number_of_longhand_properties
return ak.vector_size(&this.m_properties) + ak.ordered_hash_map_size(&this.m_custom_properties)
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:142 Web::CSS::CSSStyleProperties::item (override).
pub func css_style_properties_item_impl(base: CssStyleDeclaration*, index: usize) -> ak.String:
let this = (CssStyleProperties*)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.
let custom_properties_count = ak.ordered_hash_map_size(&this.m_custom_properties)
if index >= css_style_declaration_length(base):
return ak.string_from_str("")
if css_style_declaration_is_computed(base):
let property_id = (css_data.PropertyId)(index + (usize)css_data.first_longhand_property_id)
let name = css_data.string_from_property_id(property_id)
return ak.fly_string_to_string_const(&name)
if index < custom_properties_count:
let keys = ak.ordered_hash_map_keys(&this.m_custom_properties)
return ak.fly_string_to_string_const(ak.vector_at_const(&keys, index))
let name = css_data.string_from_property_id(ak.vector_at(&this.m_properties, index - custom_properties_count).property_id)
return ak.fly_string_to_string_const(&name)
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:165 Web::CSS::CSSStyleProperties::get_property.
pub func css_style_properties_get_property(this: CssStyleProperties*, property_id: css_data.PropertyId) -> CssStyleProperty?:
return css_style_properties_get_property_internal(this, css_property_name_and_id_from_id(property_id))
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:170 Web::CSS::CSSStyleProperties::custom_property.
pub func css_style_properties_custom_property(this: CssStyleProperties*, custom_property_name: ak.FlyString) -> const CssStyleProperty*?:
let declaration = this.css_style_declaration()
if css_style_declaration_is_computed(declaration):
var owner_node = css_style_declaration_owner_node(declaration) else return none
let element = dom_abstract_element_element(&owner_node)
let pseudo_element = dom_abstract_element_pseudo_element(&owner_node)
dom_document_update_style(dom_node_document(element.dom_node()))
let data = dom_element_custom_property_data(element, pseudo_element) else return none
if let property = css_custom_property_data_get(data, custom_property_name):
return property
return none
return ak.ordered_hash_map_get_k_const(&this.m_custom_properties, custom_property_name)
# mark: setProperty() ===========================================================================
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:195 Web::CSS::CSSStyleProperties::set_property (override).
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
pub func css_style_properties_set_property_impl(base: CssStyleDeclaration*, property_name: ak.FlyString, value: ak.StringView, priority: ak.StringView) -> !:
let this = (CssStyleProperties*)base
# 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
if css_style_declaration_is_computed(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 in result of getComputedStyle()"))))
# 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.
# NB: This is handled inside PropertyNameAndID::from_string().
let property = css_property_name_and_id_from_name(property_name) else return
# NB: The remaining steps are implemented in set_property_internal().
try css_style_properties_set_property_internal(this, property, value, priority)
## The captures of set_property_internal's per-longhand lambda.
struct CssStylePropertiesSetPropertyLonghand:
var this: CssStyleProperties*
var updated: bool*
var priority: ak.StringView
## set_property_internal's per-longhand lambda.
func css_style_properties_set_property_longhand(context: void*?, longhand_property_id: css_data.PropertyId, longhand_value: CssStyleValue*):
let captures = (CssStylePropertiesSetPropertyLonghand*)(context else trap("no captures"))
# 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.
# 2. If longhand result is true, let updated be true.
let important = css_syntax.Important.yes if not ak.string_view_is_empty(&captures.priority) else css_syntax.Important.no
let longhand_result = css_style_properties_set_a_css_declaration(captures.this, longhand_property_id, longhand_value, important)
*captures.updated = *captures.updated or longhand_result
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:214 Web::CSS::CSSStyleProperties::set_property_internal.
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
func css_style_properties_set_property_internal(this: CssStyleProperties*, property: CssPropertyNameAndId, value: ak.StringView, priority: ak.StringView) -> !:
# NB: Steps 1 and 2 only apply to the IDL method that invokes this.
# 3. If value is the empty string, invoke removeProperty() with property as argument and return.
if ak.string_view_is_empty(&value):
_ = css_style_properties_remove_property_internal(this, property) else trap("MUST: remove_property_internal")
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 = ---
if let owner_node = css_style_declaration_owner_node(this.css_style_declaration()):
css_parsing_params_construct_dom_document(&parsing_params, dom_node_document(dom_abstract_element_element_const(&owner_node).dom_node()))
else:
css_parsing_params_construct_css_parsing_mode(&parsing_params)
# 6. If component value list is null, then return.
let component_value_list = parse_css_value(parsing_params, value, css_property_name_and_id_id(&property)) else return
# 7. Let updated be false.
var updated = false
let important = css_syntax.Important.yes if not ak.string_view_is_empty(&priority) else css_syntax.Important.no
# 8. If property is a shorthand property,
if css_data.property_is_shorthand(css_property_name_and_id_id(&property)):
# then for each longhand property longhand that property maps to, in canonical order, follow these substeps:
var captures = CssStylePropertiesSetPropertyLonghand(this = this, updated = &updated, priority = priority)
css_style_computer_for_each_property_expanding_shorthands(css_property_name_and_id_id(&property), component_value_list, ak.Function2[css_data.PropertyId, CssStyleValue*, unit](call = css_style_properties_set_property_longhand, context = &captures))
# 9. Otherwise,
elif css_property_name_and_id_is_custom_property(&property):
let style_property = CssStyleProperty(important = important, property_id = css_property_name_and_id_id(&property), value = component_value_list)
_ = ak.ordered_hash_map_set(&this.m_custom_properties, css_property_name_and_id_name(&property), style_property)
updated = true
else:
# 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.
updated = css_style_properties_set_a_css_declaration(this, css_property_name_and_id_id(&property), 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(this.css_style_declaration())
# Non-standard: Invalidate style for the owners of our containing sheet, if any.
css_style_properties_invalidate_owners(this, DomStyleInvalidationReason.css_style_properties_set_property)
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:279 Web::CSS::CSSStyleProperties::set_property.
pub func css_style_properties_set_property_property_id(this: CssStyleProperties*, property_id: css_data.PropertyId, css_text: ak.StringView, priority: ak.StringView) -> !:
try css_style_properties_set_property_internal(this, css_property_name_and_id_from_id(property_id), css_text, priority)
# mark: Style values from computed values =======================================================
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:284 Web::CSS::style_value_for_length_percentage.
func css_style_value_for_length_percentage(length_percentage: CssLengthPercentage) -> CssStyleValue*:
if css_percentage_or_is_percentage(&length_percentage.percentage_or):
return css_percentage_style_value_create(css_percentage_or_percentage(&length_percentage.percentage_or)).css_style_value()
if css_length_percentage_is_length(&length_percentage):
return css_length_style_value_create(css_length_percentage_length(&length_percentage)).css_style_value()
return css_percentage_or_calculated(&length_percentage.percentage_or).css_style_value()
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:293 Web::CSS::style_value_for_length_percentage_or_auto.
func css_style_value_for_length_percentage_or_auto(length_percentage: CssLengthPercentageOrAuto) -> CssStyleValue*:
if css_length_percentage_or_auto_is_auto(&length_percentage):
return css_keyword_style_value_create(css_data.Keyword.auto).css_style_value()
if css_length_percentage_or_auto_is_percentage(&length_percentage):
return css_percentage_style_value_create(css_length_percentage_or_auto_percentage(&length_percentage)).css_style_value()
if css_length_percentage_or_auto_is_length(&length_percentage):
return css_length_style_value_create(css_length_percentage_or_auto_length(&length_percentage)).css_style_value()
return css_length_percentage_or_auto_calculated(&length_percentage).css_style_value()
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:304 Web::CSS::style_value_for_size.
func css_style_value_for_size(size: CssSize) -> CssStyleValue*:
if css_size_is_none(&size):
return css_keyword_style_value_create(css_data.Keyword.none_).css_style_value()
if css_size_is_percentage(&size):
return css_percentage_style_value_create(css_size_percentage(&size)).css_style_value()
if css_size_is_length(&size):
return css_length_style_value_create(css_size_length(&size)).css_style_value()
if css_size_is_auto(&size):
return css_keyword_style_value_create(css_data.Keyword.auto).css_style_value()
if css_size_is_calculated(&size):
return css_size_calculated(&size).css_style_value()
if css_size_is_min_content(&size):
return css_keyword_style_value_create(css_data.Keyword.min_content).css_style_value()
if css_size_is_max_content(&size):
return css_keyword_style_value_create(css_data.Keyword.max_content).css_style_value()
if css_size_is_fit_content(&size):
if let available_space = css_size_fit_content_available_space(&size):
return css_function_style_value_create(ak.fly_string_from_str("fit-content"), css_style_value_for_length_percentage(available_space)).css_style_value()
return css_keyword_style_value_create(css_data.Keyword.fit_content).css_style_value()
trap("TODO")
## make_shadow_style_value, the lambda of style_value_for_shadow.
func css_style_value_for_shadow_make(shadow_type: CssShadowStyleValueShadowType, shadow: const CssShadowData*) -> CssStyleValue*:
return css_shadow_style_value_create(
shadow_type,
css_color_style_value_create_from_color(shadow.color, CssColorSyntax.modern).css_style_value(),
css_style_value_for_length_percentage(css_length_percentage_new_css_length(shadow.offset_x)),
css_style_value_for_length_percentage(css_length_percentage_new_css_length(shadow.offset_y)),
css_style_value_for_length_percentage(css_length_percentage_new_css_length(shadow.blur_radius)),
css_style_value_for_length_percentage(css_length_percentage_new_css_length(shadow.spread_distance)),
shadow.placement).css_style_value()
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:328 Web::CSS::style_value_for_shadow.
func css_style_value_for_shadow(shadow_type: CssShadowStyleValueShadowType, shadow_data: ak.Vector[CssShadowData]) -> CssStyleValue*?:
if ak.vector_is_empty(&shadow_data):
return css_keyword_style_value_create(css_data.Keyword.none_).css_style_value()
if ak.vector_size(&shadow_data) == 1:
return css_style_value_for_shadow_make(shadow_type, ak.vector_first_const(&shadow_data))
var style_values: ak.Vector[CssStyleValue*]
ak.vector_ensure_capacity(&style_values, ak.vector_size(&shadow_data))
for shadow in ak.vector_span_const(&shadow_data):
ak.vector_unchecked_append_u(&style_values, css_style_value_for_shadow_make(shadow_type, &shadow))
return css_style_value_list_create(style_values, CssStyleValueListSeparator.comma).css_style_value()