dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / css_style_properties_2.lucb
#==============================================================================================
#
# css_style_properties_2 - CSS::CSSStyleProperties: getPropertyValue() and its relatives
#
# DESCRIPTION:
# Port of LibWeb/CSS/CSSStyleProperties.cpp (lines 354-645): getPropertyValue(),
# getPropertyPriority(), has_property(), the style-value accessors, getting a property
# (shorthands assembled from their longhands, pending substitutions) and getting a
# declared or resolved (getComputedStyle()) property.
#
# 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: Getting properties ======================================================================
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:355 Web::CSS::CSSStyleProperties::get_property_value (override).
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue
pub func css_style_properties_get_property_value_impl(base: CssStyleDeclaration*, property_name: ak.FlyString) -> ak.String:
let this = (CssStyleProperties*)base
let property = css_property_name_and_id_from_name(property_name) else return ak.string_from_str("")
if let style_property = css_style_properties_get_property_internal(this, property):
let mode = css_syntax.SerializationMode.resolved_value if css_style_declaration_is_computed(base) else css_syntax.SerializationMode.normal
return css_style_value_to_string(style_property.value, mode)
return ak.string_from_str("")
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:369 Web::CSS::CSSStyleProperties::get_property_priority (override).
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertypriority
pub func css_style_properties_get_property_priority_impl(base: CssStyleDeclaration*, property_name: ak.FlyString) -> ak.StringView:
let this = (CssStyleProperties*)base
let property_id = css_data.property_id_from_string(ak.fly_string_bytes_as_string_view(&property_name)) else return ak.sv("")
if property_id == css_data.PropertyId.custom:
let maybe_custom_property = css_style_properties_custom_property(this, property_name) else return ak.sv("")
return ak.sv("important") if maybe_custom_property.important == css_syntax.Important.yes else ak.sv("")
let maybe_property = css_style_properties_get_property(this, property_id) else return ak.sv("")
return ak.sv("important") if maybe_property.important == css_syntax.Important.yes else ak.sv("")
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:386 Web::CSS::CSSStyleProperties::has_property (override).
pub func css_style_properties_has_property_impl(base: CssStyleDeclaration*, property: CssPropertyNameAndId) -> bool:
return css_style_properties_get_property_internal((CssStyleProperties*)base, property) != none
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:391 Web::CSS::CSSStyleProperties::has_property.
pub func css_style_properties_has_property_property_id(this: CssStyleProperties*, property_id: css_data.PropertyId) -> bool:
return css_style_declaration_has_property(this.css_style_declaration(), css_property_name_and_id_from_id(property_id))
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:396 Web::CSS::CSSStyleProperties::get_property_style_value (override).
pub func css_style_properties_get_property_style_value_impl(base: CssStyleDeclaration*, property: CssPropertyNameAndId) -> CssStyleValue*?:
if let style_property = css_style_properties_get_property_internal((CssStyleProperties*)base, property):
return style_property.value
return none
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:403 Web::CSS::CSSStyleProperties::get_property_style_value.
pub func css_style_properties_get_property_style_value_property_id(this: CssStyleProperties*, property_id: css_data.PropertyId) -> CssStyleValue*?:
return css_style_declaration_get_property_style_value(this.css_style_declaration(), css_property_name_and_id_from_id(property_id))
## The lambda of set_property_style_value's for_each_property_expanding_shorthands (captures: this).
func css_style_properties_set_property_style_value_longhand(context: void*?, longhand_id: css_data.PropertyId, longhand_value: CssStyleValue*):
let this = (CssStyleProperties*)(context else trap("no style properties"))
if let index = css_style_properties_index_of(&this.m_properties, longhand_id):
ak.vector_remove(&this.m_properties, index)
ak.vector_append_t(&this.m_properties, CssStyleProperty(important = css_syntax.Important.no, property_id = longhand_id, value = longhand_value))
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:408 Web::CSS::CSSStyleProperties::set_property_style_value (override).
pub func css_style_properties_set_property_style_value_impl(base: CssStyleDeclaration*, property: CssPropertyNameAndId, style_value: CssStyleValue*) -> !:
let this = (CssStyleProperties*)base
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()"))))
if css_property_name_and_id_is_custom_property(&property):
let name = css_property_name_and_id_name(&property)
_ = ak.ordered_hash_map_remove_k(&this.m_custom_properties, name)
_ = ak.ordered_hash_map_set(&this.m_custom_properties, name, CssStyleProperty(important = css_syntax.Important.no, property_id = css_data.PropertyId.custom, value = style_value))
css_style_declaration_update_style_attribute(base)
css_style_properties_invalidate_owners(this, DomStyleInvalidationReason.css_style_properties_set_property_style_value)
return
# FIXME: This should have been rejected earlier, but property_accepts_type() is too basic for what we need.
if (css_data.property_is_positional_value_list_shorthand(css_property_name_and_id_id(&property)) and not css_style_value_is_shorthand(style_value) and not css_style_value_is_unresolved(style_value) and not css_style_value_is_pending_substitution(style_value) and not css_style_value_is_guaranteed_invalid(style_value) and not css_style_value_is_css_wide_keyword(style_value)):
let name = css_property_name_and_id_name(&property)
let value_text = css_style_value_to_string(style_value, css_syntax.SerializationMode.normal)
let message = css_string_formatted("Setting {} to '{}' is not allowed.", [css_arg_fly_string(&name), css_arg_string(&value_text)])
try idl_throw_simple(IdlSimpleExceptionType.type_error, .string(message))
css_style_computer_for_each_property_expanding_shorthands(css_property_name_and_id_id(&property), style_value, ak.Function2[css_data.PropertyId, CssStyleValue*, unit](call = css_style_properties_set_property_style_value_longhand, context = this))
css_style_declaration_update_style_attribute(base)
css_style_properties_invalidate_owners(this, DomStyleInvalidationReason.css_style_properties_set_property_style_value)
## Whether every value of `list` is a pending-substitution value of `original_shorthand_value`
## (get_property_internal's all_of).
func css_style_properties_all_from_same_original(list: const ak.Vector[CssStyleValue*]*, original_shorthand_value: CssStyleValue*) -> bool:
for value in ak.vector_span_const(list):
if not (css_style_value_is_pending_substitution(value) and css_pending_substitution_style_value_original_shorthand_value(css_style_value_as_pending_substitution(value)) == original_shorthand_value):
return false
return true
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:457 Web::CSS::CSSStyleProperties::get_property_internal.
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue
func css_style_properties_get_property_internal(this: CssStyleProperties*, property: CssPropertyNameAndId) -> CssStyleProperty?:
# NB: This is our own method to get a StyleProperty, but following the algorithm for getPropertyValue() which
# returns a String. (This way, we can use the same logic in other places.) That's why the spec steps talk
# about strings and then we do something different.
# 1. If property is not a custom property, follow these substeps:
if not css_property_name_and_id_is_custom_property(&property):
# 1. Let property be property converted to ASCII lowercase.
# NB: Done already by PropertyNameAndID.
# 2. If property is a shorthand property, then follow these substeps:
if css_data.property_is_shorthand(css_property_name_and_id_id(&property)):
# 1. Let list be a new empty array.
var list: ak.Vector[CssStyleValue*]
var last_important_flag: css_syntax.Important? = none
# 2. For each longhand property longhand that property maps to, in canonical order, follow these substeps:
let longhand_ids = css_data.longhands_for_shorthand(css_property_name_and_id_id(&property))
for longhand_property_id in ak.vector_span_const(&longhand_ids):
# 1. If longhand is a case-sensitive match for a property name of a CSS declaration in the declarations,
# let declaration be that CSS declaration, or null otherwise.
# 2. If declaration is null, then return the empty string.
let declaration = css_style_properties_get_property_internal(this, css_property_name_and_id_from_id(longhand_property_id)) else return none
# 3. Append the declaration to list.
ak.vector_append_t(&list, declaration.value)
if let last = last_important_flag:
if declaration.important != last:
return none
last_important_flag = declaration.important
let important = last_important_flag else trap("VERIFY: last_important_flag")
# https://drafts.csswg.org/css-values-5/#pending-substitution-value
# If all of the component longhand properties for a given shorthand are pending-substitution values from
# the same original shorthand value, the shorthand property must serialize to that original
# (arbitrary substitution function-containing) value.
# Otherwise, if any of the component longhand properties for a given shorthand are pending-substitution
# values, or contain arbitrary substitution functions of their own that have not yet been substituted, the
# shorthand property must serialize to the empty string.
let first = *ak.vector_first(&list)
if css_style_value_is_pending_substitution(first):
let original_shorthand_value = css_pending_substitution_style_value_original_shorthand_value(css_style_value_as_pending_substitution(first))
if css_style_properties_all_from_same_original(&list, original_shorthand_value):
return CssStyleProperty(important = important, property_id = css_property_name_and_id_id(&property), value = original_shorthand_value)
for value in ak.vector_span(&list):
if css_style_value_is_pending_substitution(value) or css_style_value_is_unresolved(value):
return none
# 3. If important flags of all declarations in list are same, then return the serialization of list.
# NOTE: Currently we implement property-specific shorthand serialization in ShorthandStyleValue::to_string().
return CssStyleProperty(important = important, property_id = css_property_name_and_id_id(&property),
value = css_shorthand_style_value_create(css_property_name_and_id_id(&property), longhand_ids, list).css_style_value())
# 4. Return the empty string.
# NOTE: This is handled by the loop.
# 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.
# 3. Return the empty string.
return css_style_properties_get_direct_property(this, property)
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:536 Web::CSS::CSSStyleProperties::get_direct_property.
func css_style_properties_get_direct_property(this: CssStyleProperties*, property_name_and_id: CssPropertyNameAndId) -> CssStyleProperty?:
let property_id = css_property_name_and_id_id(&property_name_and_id)
let declaration = this.css_style_declaration()
if css_style_declaration_is_computed(declaration):
var abstract_element = css_style_declaration_owner_node(declaration) else return none
# https://drafts.csswg.org/cssom/#dom-window-getcomputedstyle
# NB: This is a partial enforcement of step 5:
# If [...] elt is connected, part of the flat tree, and its shadow-including root has a browsing context which
# either doesn't have a browsing context container, or whose browsing context container is being rendered.
let element = dom_abstract_element_element(&abstract_element)
if not dom_node_is_connected(element.dom_node()):
return none
if dom_document_browsing_context(dom_node_document(dom_node_shadow_including_root(element.dom_node()))) == none:
return none
# FIXME: Check if the element is part of the flat tree.
# FIXME: Check that the browsing context either doesn't have a browsing context container, or that its
# browsing context container is being rendered.
# NB: We grab the layout node before deciding whether update_layout() is needed.
# For properties that don't need layout or a layout node (the else branch below),
# we skip update_layout() entirely and use whatever layout node already exists.
# For the other paths, we call update_layout() and re-fetch below.
var layout_node = dom_abstract_element_unsafe_layout_node(&abstract_element)
# Determine what work is needed for this property:
# 1. Properties that need layout computation (used values) - always run update_layout()
# 2. Properties that need a layout node for special resolution - ensure layout node exists
# 3. Everything else - just update_style() and return computed value
let needs_layout = css_data.property_needs_layout_for_getcomputedstyle(property_id)
let needs_layout_node = css_data.property_needs_layout_node_for_resolved_value(property_id) or css_data.property_is_logical_alias(property_id) or css_data.property_is_shorthand(property_id)
let document = dom_abstract_element_document(&abstract_element)
if needs_layout or needs_layout_node:
# Properties that need layout computation or layout node for special resolution
# always need update_layout() to ensure both style and layout tree are up to date.
dom_document_update_layout(document, DomUpdateLayoutReason.resolved_css_style_declaration_property)
layout_node = dom_abstract_element_layout_node(&abstract_element)
elif dom_document_element_needs_style_update(document, abstract_element):
# Just ensure styles are up to date.
dom_document_update_style(document)
# FIXME: Somehow get custom properties if there's no layout node.
if css_property_name_and_id_is_custom_property(&property_name_and_id):
let name = css_property_name_and_id_name(&property_name_and_id)
if let maybe_value = dom_abstract_element_get_custom_property(&abstract_element, name):
return CssStyleProperty(property_id = property_id, value = maybe_value)
# FIXME: Currently, to get the initial value for a registered custom property we have to look at the document.
# These should be cascaded like other properties.
if let registration = dom_document_get_registered_custom_property(document, name):
if let initial_value = registration.initial_value:
return CssStyleProperty(property_id = property_id, value = initial_value)
return none
if layout_node == none:
# Seed the ancestor chain before this one-off style computation, so
# ancestor-dependent selectors still match for no `layout_node`
# queries (for example `.outer .inner .target`).
let style = css_style_computer_compute_style_with_seeded_ancestors(dom_document_style_computer(document), abstract_element)
return CssStyleProperty(property_id = property_id, value = css_computed_properties_property(style, property_id))
let node = layout_node else trap("unreachable")
let value = css_style_properties_style_value_for_computed_property(this, node, property_id) else return none
return CssStyleProperty(property_id = property_id, value = value)
if css_property_name_and_id_is_custom_property(&property_name_and_id):
let custom = css_style_properties_custom_property(this, css_property_name_and_id_name(&property_name_and_id)) else return none
return *custom
for property in ak.vector_span(&this.m_properties):
if property.property_id == property_id:
return property
return none
## Ported from Libraries/LibWeb/CSS/CSSStyleProperties.cpp:633 Web::CSS::resolve_color_style_value.
func css_resolve_color_style_value(style_value: CssStyleValue*, computed_color: gfx.Color) -> CssStyleValue*?:
if css_style_value_is_color_function(style_value) and css_color_function_style_value_serializes_as_color_function((CssColorFunctionStyleValue*)style_value):
return style_value
if css_style_value_is_color(style_value):
let color_style_value = css_style_value_as_color(style_value)
if let color_type = css_color_style_value_color_type(color_style_value):
if color_type == CssColorStyleValueColorType.lab or color_type == CssColorStyleValueColorType.ok_lab or color_type == CssColorStyleValueColorType.lch or color_type == CssColorStyleValueColorType.oklch:
return style_value
return css_color_style_value_create_from_color(computed_color, CssColorSyntax.modern).css_style_value()