dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / style_values / random_value_sharing_style_value.lucb
#==============================================================================================
#
# random_value_sharing_style_value - RandomValueSharingStyleValue: random()'s <random-value-sharing>, and the random caching key
#
# DESCRIPTION:
# Port of LibWeb/CSS/StyleValues/RandomValueSharingStyleValue.cpp (lines 1-79) and the
# inline members of RandomValueSharingStyleValue.h.
#
# Copyright (c) 2025, Callum Law <callumlaw1709@outlook.com>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: RandomCachingKey ========================================================================
## AK::Traits<RandomCachingKey>::hash (RandomValueSharingStyleValue.h:76).
pub func css_random_caching_key_hash(key: const CssRandomCachingKey*) -> u32:
let element_id = key.element_id else return ak.fly_string_hash(&key.name)
return ak.pair_int_hash(ak.fly_string_hash(&key.name), ak.u64_hash((u64)element_id))
## AK::Traits<RandomCachingKey>::equals (RandomValueSharingStyleValue.h:83).
pub func css_random_caching_key_equals(a: const CssRandomCachingKey*, b: const CssRandomCachingKey*) -> bool:
return a.element_id == b.element_id and ak.fly_string_eq_fly_string(&a.name, &b.name)
# mark: RandomValueSharingStyleValue ============================================================
## Ported from Libraries/LibWeb/CSS/StyleValues/RandomValueSharingStyleValue.h:20 Web::CSS::RandomValueSharingStyleValue::create_fixed.
pub func css_random_value_sharing_style_value_create_fixed(fixed_value: CssStyleValue*) -> CssRandomValueSharingStyleValue*:
let this = &(new CssRandomValueSharingStyleValue[1] --- else trap("out of memory"))[0]
css_random_value_sharing_style_value_construct(this, fixed_value, false, none, false)
return this
## Ported from Libraries/LibWeb/CSS/StyleValues/RandomValueSharingStyleValue.h:25 Web::CSS::RandomValueSharingStyleValue::create_auto.
pub func css_random_value_sharing_style_value_create_auto(name: ak.FlyString, element_shared: bool) -> CssRandomValueSharingStyleValue*:
let this = &(new CssRandomValueSharingStyleValue[1] --- else trap("out of memory"))[0]
css_random_value_sharing_style_value_construct(this, none, true, name, element_shared)
return this
## Ported from Libraries/LibWeb/CSS/StyleValues/RandomValueSharingStyleValue.h:30 Web::CSS::RandomValueSharingStyleValue::create_dashed_ident.
pub func css_random_value_sharing_style_value_create_dashed_ident(name: ak.FlyString, element_shared: bool) -> CssRandomValueSharingStyleValue*:
let this = &(new CssRandomValueSharingStyleValue[1] --- else trap("out of memory"))[0]
css_random_value_sharing_style_value_construct(this, none, false, name, element_shared)
return this
## Ported from Libraries/LibWeb/CSS/StyleValues/RandomValueSharingStyleValue.h:54 Web::CSS::RandomValueSharingStyleValue::RandomValueSharingStyleValue.
pub func css_random_value_sharing_style_value_construct(this: CssRandomValueSharingStyleValue*, fixed_value: CssStyleValue*?, is_auto: bool, name: ak.FlyString?, element_shared: bool):
css_style_value_construct(this.css_style_value(), CssStyleValueType.random_value_sharing)
css_style_value_set_class(this.css_style_value(), (const CssStyleValueVTable*)&css_random_value_sharing_style_value_vtable, &css_random_value_sharing_style_value_class)
this.m_fixed_value = fixed_value
this.m_is_auto = is_auto
this.m_name = name
this.m_element_shared = element_shared
## Ported from Libraries/LibWeb/CSS/StyleValues/RandomValueSharingStyleValue.cpp:14 Web::CSS::RandomValueSharingStyleValue::absolutized (override).
pub func css_random_value_sharing_style_value_absolutized_impl(base: CssStyleValue*, computation_context: CssComputationContext) -> CssStyleValue*:
let this = (CssRandomValueSharingStyleValue*)base
# https://drafts.csswg.org/css-values-5/#random-caching
# Each instance of a random function in styles has an associated random base value.
# If the random function’s <random-value-sharing> is fixed <number>, the random base value is that number.
if let fixed_value = this.m_fixed_value:
let absolutized_fixed_value = css_style_value_absolutized(fixed_value, computation_context)
if css_style_values_equal(fixed_value, absolutized_fixed_value):
return base
return css_random_value_sharing_style_value_create_fixed(absolutized_fixed_value).css_style_value()
# Otherwise, the random base value is a pseudo-random real number in the range `[0, 1)` (greater than or equal to 0
# and less than 1), generated from a uniform distribution, and influenced by the function’s random caching key.
# A random caching key is a tuple of:
var abstract_element = computation_context.abstract_element else trap("VERIFY: an abstract element")
var element_id: i64? = none
if not this.m_element_shared:
element_id = dom_node_unique_id((DomNode*)dom_abstract_element_element(&abstract_element))
let random_caching_key = CssRandomCachingKey(name = this.m_name else trap("VERIFY: a random caching key name"), element_id = element_id)
# 1. A string name: the value of the <dashed-ident>, if specified in <random-value-sharing>; or else a string
# of the form "PROPERTY N", where PROPERTY is the name of the property the random function is used in
# (before shorthand expansion, if relevant), and N is the index of the random function among other random
# functions in the same property value.
# 2. An element ID identifying the element the style is being applied to, or null if element-shared is
# specified in <random-value-sharing>.
# FIXME: Use the pseudo element's unique_id() when that's accessible
# 3. A document ID identifying the Document the styles are from.
# NB: This is implicit since the cache is stored on the document or the element (which is a child of the document).
let random_base_value = dom_element_ensure_css_random_base_value(dom_abstract_element_element(&abstract_element), random_caching_key)
return css_random_value_sharing_style_value_create_fixed(css_number_style_value_create(random_base_value).css_style_value()).css_style_value()
## Ported from Libraries/LibWeb/CSS/StyleValues/RandomValueSharingStyleValue.cpp:53 Web::CSS::RandomValueSharingStyleValue::random_base_value.
pub func css_random_value_sharing_style_value_random_base_value(this: CssRandomValueSharingStyleValue*) -> f64:
return css_number_from_style_value(this.m_fixed_value else trap("VERIFY: a fixed value"), none)
## Ported from Libraries/LibWeb/CSS/StyleValues/RandomValueSharingStyleValue.cpp:58 Web::CSS::RandomValueSharingStyleValue::serialize (override).
pub func css_random_value_sharing_style_value_serialize_impl(base: CssStyleValue*, builder: ak.StringBuilder*, serialization_mode: css_syntax.SerializationMode):
let this = (CssRandomValueSharingStyleValue*)base
if let fixed_value = this.m_fixed_value:
ak.string_builder_append(builder, "fixed ")
css_style_value_serialize(fixed_value, builder, serialization_mode)
return
var first = true
if not this.m_is_auto:
let name = this.m_name else trap("VERIFY: a name")
css_appendff(builder, "{}", [css_arg_fly_string(&name)])
first = false
if this.m_element_shared:
if not first:
ak.string_builder_append_u8(builder, ' ')
ak.string_builder_append(builder, "element-shared")
## Ported from Libraries/LibWeb/CSS/StyleValues/RandomValueSharingStyleValue.h:43 Web::CSS::RandomValueSharingStyleValue::properties_equal.
pub func css_random_value_sharing_style_value_properties_equal(this: CssRandomValueSharingStyleValue*, other: CssRandomValueSharingStyleValue*) -> bool:
return css_optional_style_values_equal(this.m_fixed_value, other.m_fixed_value) and this.m_is_auto == other.m_is_auto and css_optional_fly_strings_equal(this.m_name, other.m_name) and this.m_element_shared == other.m_element_shared
## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:202 Web::CSS::StyleValueWithDefaultOperators::equals for Web::CSS::RandomValueSharingStyleValue (CRTP helper) (override).
pub func css_random_value_sharing_style_value_equals_impl(base: CssStyleValue*, other: CssStyleValue*) -> bool:
if css_style_value_type(base) != css_style_value_type(other):
return false
return css_random_value_sharing_style_value_properties_equal((CssRandomValueSharingStyleValue*)base, (CssRandomValueSharingStyleValue*)other)
## Ported from Libraries/LibWeb/CSS/StyleValues/RandomValueSharingStyleValue.h:51 Web::CSS::RandomValueSharingStyleValue::is_computationally_independent (override).
pub func css_random_value_sharing_style_value_is_computationally_independent_impl(base: CssStyleValue*) -> bool:
let this = (CssRandomValueSharingStyleValue*)base
return css_is_null_or_computationally_independent(this.m_fixed_value)