Truly independent web browser
/*
* Copyright (c) 2018-2025, Andreas Kling <[email protected]>
* Copyright (c) 2021-2025, Sam Atkins <[email protected]>
* Copyright (c) 2021, Tobias Christiansen <[email protected]>
* Copyright (c) 2022-2023, MacDue <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/StringBuilder.h>
#include <AK/Utf16StringBuilder.h>
#include <LibGfx/Font/Font.h>
#include <LibGfx/Font/FontStyleMapping.h>
#include <LibWeb/CSS/ComputedValues.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/StyleComputeFFI.h>
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
#include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
#include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
#include <LibWeb/CSS/StyleValues/ColorFunctionStyleValue.h>
#include <LibWeb/CSS/StyleValues/ColorSchemeStyleValue.h>
#include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
#include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
#include <LibWeb/CSS/StyleValues/CounterDefinitionsStyleValue.h>
#include <LibWeb/CSS/StyleValues/CounterStyleStyleValue.h>
#include <LibWeb/CSS/StyleValues/CursorStyleValue.h>
#include <LibWeb/CSS/StyleValues/CustomIdentStyleValue.h>
#include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
#include <LibWeb/CSS/StyleValues/FilterStyleValue.h>
#include <LibWeb/CSS/StyleValues/FlexStyleValue.h>
#include <LibWeb/CSS/StyleValues/FontSourceStyleValue.h>
#include <LibWeb/CSS/StyleValues/FontStyleStyleValue.h>
#include <LibWeb/CSS/StyleValues/FrequencyStyleValue.h>
#include <LibWeb/CSS/StyleValues/FunctionStyleValue.h>
#include <LibWeb/CSS/StyleValues/ImageSetStyleValue.h>
#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
#include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
#include <LibWeb/CSS/StyleValues/KeywordStyleValue.h>
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
#include <LibWeb/CSS/StyleValues/OpacityValueStyleValue.h>
#include <LibWeb/CSS/StyleValues/PendingSubstitutionStyleValue.h>
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
#include <LibWeb/CSS/StyleValues/RatioStyleValue.h>
#include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>
#include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
#include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
#include <LibWeb/CSS/StyleValues/StringStyleValue.h>
#include <LibWeb/CSS/StyleValues/StyleValue.h>
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
#include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
#include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
#include <LibWeb/CSS/StyleValues/URLStyleValue.h>
#include <LibWeb/CSS/StyleValues/UnicodeRangeStyleValue.h>
#include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
#include <LibWeb/ComputedValuesRustFFI.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/HTML/LocalNavigable.h>
#include <LibWeb/Layout/Node.h>
extern "C" void ladybird_utf16_fly_string_unref(size_t);
static_assert(sizeof(Web::CSS::ComputedValuesFFI::RetainedUtf16FlyString) == sizeof(Utf16FlyString));
static_assert(alignof(Web::CSS::ComputedValuesFFI::RetainedUtf16FlyString) == alignof(Utf16FlyString));
static_assert(offsetof(Web::CSS::ComputedValuesFFI::RetainedUtf16FlyString, raw) == 0);
namespace Web::CSS {
Utf16FlyString css_string_from_rust(StyleValueFFI::CssString const* string)
{
auto view = StyleValueFFI::rust_css_string_view(string);
return Utf16FlyString::from_utf16(Utf16View { reinterpret_cast<char16_t const*>(view.data), view.length });
}
ColorResolutionStyle ColorResolutionStyle::for_element(DOM::AbstractElement const& element)
{
return {
.inherited_ui_values = element.style_group<ComputedValues::InheritedUIValues>(),
.inherited_text_values = element.style_group<ComputedValues::InheritedTextValues>(),
};
}
ColorResolutionStyle ColorResolutionStyle::for_layout_node(Layout::NodeWithStyle const& layout_node)
{
return {
.inherited_ui_values = &layout_node.style_group<ComputedValues::InheritedUIValues>(),
.inherited_text_values = &layout_node.style_group<ComputedValues::InheritedTextValues>(),
};
}
StyleValue::StyleValue(Type type, StyleValueFFI::StyleValueData const* value)
: m_value(value)
, m_type(type)
{
}
ValueComparingNonnullRefPtr<StyleValue const> StyleValue::adopt_rust_style_value_data(StyleValueFFI::StyleValueData const* data)
{
VERIFY(data);
switch (data->tag) {
case StyleValueFFI::StyleValueData::Tag::Anchor:
return adopt_ref(*new (nothrow) StyleValue(Type::Anchor, data));
case StyleValueFFI::StyleValueData::Tag::AnchorSize:
return adopt_ref(*new (nothrow) StyleValue(Type::AnchorSize, data));
case StyleValueFFI::StyleValueData::Tag::BackgroundSize:
return adopt_ref(*new (nothrow) BackgroundSizeStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::BasicShape:
return adopt_ref(*new (nothrow) StyleValue(Type::BasicShape, data));
case StyleValueFFI::StyleValueData::Tag::BorderRadius:
return adopt_ref(*new (nothrow) StyleValue(Type::BorderRadius, data));
case StyleValueFFI::StyleValueData::Tag::BorderRadiusRect:
return adopt_ref(*new (nothrow) StyleValue(Type::BorderRadiusRect, data));
case StyleValueFFI::StyleValueData::Tag::BorderImageSlice:
return adopt_ref(*new (nothrow) StyleValue(Type::BorderImageSlice, data));
case StyleValueFFI::StyleValueData::Tag::Calculated:
return adopt_ref(*new (nothrow) CalculatedStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::ColorFunction:
return adopt_ref(*new (nothrow) ColorFunctionStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::ColorInterpolationMethod:
return adopt_ref(*new (nothrow) StyleValue(Type::ColorInterpolationMethod, data));
case StyleValueFFI::StyleValueData::Tag::ColorMix:
return adopt_ref(*new (nothrow) ColorStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::ColorScheme:
return adopt_ref(*new (nothrow) ColorSchemeStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::ConicGradient:
return adopt_ref(*new (nothrow) AbstractImageStyleValue(Type::ConicGradient, data));
case StyleValueFFI::StyleValueData::Tag::CustomIdent:
return adopt_ref(*new (nothrow) CustomIdentStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Display:
return adopt_ref(*new (nothrow) DisplayStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Cursor:
return adopt_ref(*new (nothrow) CursorStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::CounterStyle:
return adopt_ref(*new (nothrow) CounterStyleStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::CounterStyleSystem:
return adopt_ref(*new (nothrow) StyleValue(Type::CounterStyleSystem, data));
case StyleValueFFI::StyleValueData::Tag::CounterDefinitions:
return adopt_ref(*new (nothrow) CounterDefinitionsStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Counter:
return adopt_ref(*new (nothrow) StyleValue(Type::Counter, data));
case StyleValueFFI::StyleValueData::Tag::ContrastColor:
return adopt_ref(*new (nothrow) ColorStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Content:
return adopt_ref(*new (nothrow) ContentStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Edge:
return adopt_ref(*new (nothrow) StyleValue(Type::Edge, data));
case StyleValueFFI::StyleValueData::Tag::EmptyOptional:
return adopt_ref(*new (nothrow) StyleValue(Type::EmptyOptional, data));
case StyleValueFFI::StyleValueData::Tag::Easing:
return adopt_ref(*new (nothrow) StyleValue(Type::Easing, data));
case StyleValueFFI::StyleValueData::Tag::Keyword: {
auto keyword = static_cast<Keyword>(data->keyword.keyword);
StyleValueFFI::rust_style_value_release(data);
return KeywordStyleValue::create(keyword);
}
case StyleValueFFI::StyleValueData::Tag::Number:
return adopt_ref(*new (nothrow) NumberStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Integer:
return adopt_ref(*new (nothrow) IntegerStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Image:
return ImageStyleValue::adopt_rust_style_value_data(data);
case StyleValueFFI::StyleValueData::Tag::ImageSet:
return adopt_ref(*new (nothrow) ImageSetStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::LightDark:
return adopt_ref(*new (nothrow) ColorStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::LinearGradient:
return adopt_ref(*new (nothrow) AbstractImageStyleValue(Type::LinearGradient, data));
case StyleValueFFI::StyleValueData::Tag::Angle:
return adopt_ref(*new (nothrow) AngleStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Flex:
return adopt_ref(*new (nothrow) FlexStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Filter:
switch (static_cast<FilterStyleValue::Kind>(data->filter.kind)) {
case FilterStyleValue::Kind::Blur:
return adopt_ref(*new (nothrow) BlurFilterStyleValue(data));
case FilterStyleValue::Kind::DropShadow:
return adopt_ref(*new (nothrow) DropShadowFilterStyleValue(data));
case FilterStyleValue::Kind::HueRotate:
return adopt_ref(*new (nothrow) HueRotateFilterStyleValue(data));
case FilterStyleValue::Kind::Color:
return adopt_ref(*new (nothrow) ColorFilterStyleValue(data));
}
VERIFY_NOT_REACHED();
case StyleValueFFI::StyleValueData::Tag::FontSource:
return adopt_ref(*new (nothrow) FontSourceStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::FontStyle:
return adopt_ref(*new (nothrow) FontStyleStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Frequency:
return adopt_ref(*new (nothrow) FrequencyStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Function:
return adopt_ref(*new (nothrow) FunctionStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::GridAutoFlow:
return adopt_ref(*new (nothrow) StyleValue(Type::GridAutoFlow, data));
case StyleValueFFI::StyleValueData::Tag::GridTemplateArea:
return adopt_ref(*new (nothrow) StyleValue(Type::GridTemplateArea, data));
case StyleValueFFI::StyleValueData::Tag::GridTrackPlacement:
return adopt_ref(*new (nothrow) StyleValue(Type::GridTrackPlacement, data));
case StyleValueFFI::StyleValueData::Tag::GridTrackSizeList:
return adopt_ref(*new (nothrow) StyleValue(Type::GridTrackSizeList, data));
case StyleValueFFI::StyleValueData::Tag::GuaranteedInvalid:
return adopt_ref(*new (nothrow) StyleValue(Type::GuaranteedInvalid, data));
case StyleValueFFI::StyleValueData::Tag::Length:
return adopt_ref(*new (nothrow) LengthStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Percentage:
return adopt_ref(*new (nothrow) PercentageStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Resolution:
return adopt_ref(*new (nothrow) ResolutionStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Time:
return adopt_ref(*new (nothrow) TimeStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::OpacityValue:
return adopt_ref(*new (nothrow) OpacityValueStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::PendingSubstitution:
return adopt_ref(*new (nothrow) PendingSubstitutionStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::OpenTypeTagged:
return adopt_ref(*new (nothrow) StyleValue(Type::OpenTypeTagged, data));
case StyleValueFFI::StyleValueData::Tag::OverflowClipMargin:
return adopt_ref(*new (nothrow) StyleValue(Type::OverflowClipMargin, data));
case StyleValueFFI::StyleValueData::Tag::Position:
return adopt_ref(*new (nothrow) StyleValue(Type::Position, data));
case StyleValueFFI::StyleValueData::Tag::Ratio:
return adopt_ref(*new (nothrow) RatioStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Ray:
return adopt_ref(*new (nothrow) StyleValue(Type::Ray, data));
case StyleValueFFI::StyleValueData::Tag::Rect:
return adopt_ref(*new (nothrow) StyleValue(Type::Rect, data));
case StyleValueFFI::StyleValueData::Tag::RepeatStyle:
return adopt_ref(*new (nothrow) StyleValue(Type::RepeatStyle, data));
case StyleValueFFI::StyleValueData::Tag::RandomValueSharing:
return adopt_ref(*new (nothrow) StyleValue(Type::RandomValueSharing, data));
case StyleValueFFI::StyleValueData::Tag::RadialSize:
return adopt_ref(*new (nothrow) StyleValue(Type::RadialSize, data));
case StyleValueFFI::StyleValueData::Tag::RadialGradient:
return adopt_ref(*new (nothrow) AbstractImageStyleValue(Type::RadialGradient, data));
case StyleValueFFI::StyleValueData::Tag::ScrollbarColor:
return adopt_ref(*new (nothrow) StyleValue(Type::ScrollbarColor, data));
case StyleValueFFI::StyleValueData::Tag::ScrollbarGutter:
return adopt_ref(*new (nothrow) StyleValue(Type::ScrollbarGutter, data));
case StyleValueFFI::StyleValueData::Tag::Shadow:
return adopt_ref(*new (nothrow) ShadowStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Shorthand:
return adopt_ref(*new (nothrow) ShorthandStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::String:
return adopt_ref(*new (nothrow) StringStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Superellipse:
return adopt_ref(*new (nothrow) StyleValue(Type::Superellipse, data));
case StyleValueFFI::StyleValueData::Tag::TextIndent:
return adopt_ref(*new (nothrow) StyleValue(Type::TextIndent, data));
case StyleValueFFI::StyleValueData::Tag::TextUnderlinePosition:
return adopt_ref(*new (nothrow) StyleValue(Type::TextUnderlinePosition, data));
case StyleValueFFI::StyleValueData::Tag::TreeCountingFunction:
return adopt_ref(*new (nothrow) StyleValue(Type::TreeCountingFunction, data));
case StyleValueFFI::StyleValueData::Tag::Transformation:
return adopt_ref(*new (nothrow) TransformationStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::ValueList:
return adopt_ref(*new (nothrow) StyleValueList(data));
case StyleValueFFI::StyleValueData::Tag::Tuple:
return adopt_ref(*new (nothrow) StyleValue(Type::Tuple, data));
case StyleValueFFI::StyleValueData::Tag::UnicodeRange:
return adopt_ref(*new (nothrow) UnicodeRangeStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Unresolved:
return adopt_ref(*new (nothrow) UnresolvedStyleValue(data));
case StyleValueFFI::StyleValueData::Tag::Url:
return adopt_ref(*new (nothrow) URLStyleValue(data));
default:
VERIFY_NOT_REACHED();
}
}
ValueComparingNonnullRefPtr<StyleValue const> StyleValue::create_guaranteed_invalid()
{
static auto& instance = *make_immortal(new (nothrow) StyleValue(Type::GuaranteedInvalid, StyleValueFFI::rust_style_value_create_guaranteed_invalid()));
return instance;
}
NonnullRefPtr<StyleValue const> StyleValue::intern(Atomic<StyleValue const*>& slot, NonnullRefPtr<StyleValue const> value)
{
// NB: Two threads can get here for the same slot. The one that loses has built a value that nothing will free.
auto const* candidate = make_immortal(value.ptr());
StyleValue const* winner = nullptr;
if (slot.compare_exchange_strong(winner, candidate, AK::MemoryOrder::memory_order_acq_rel))
return *candidate;
return *winner;
}
void StyleValue::set_style_sheet(StyleSheetState* style_sheet)
{
// Only the types holding nested values with document-associated state care.
switch (type()) {
case Type::Content:
return as_content().set_style_sheet(style_sheet);
case Type::Image:
return as_image().set_style_sheet(style_sheet);
case Type::ImageSet:
return as_image_set().set_style_sheet(style_sheet);
case Type::Shorthand:
return as_shorthand().set_style_sheet(style_sheet);
case Type::ValueList:
return as_value_list().set_style_sheet(style_sheet);
default:
return;
}
}
bool StyleValue::is_computationally_independent() const
{
return ComputedValuesFFI::rust_style_value_is_computationally_independent(m_value.operator->());
}
bool StyleValue::contains_anchor_function() const
{
return StyleValueFFI::rust_style_value_contains_anchor_function(m_value.operator->());
}
void StyleValue::serialize(StringBuilder& builder, SerializationMode mode) const
{
auto string = to_utf16_string(mode);
if (string.has_ascii_storage())
builder.append(string.ascii_view());
else
builder.append(string.utf16_view());
}
bool StyleValue::equals(StyleValue const& other) const
{
// Structural equality runs over the shared Rust value data. The exceptions are the types
// whose C++ comparison semantics deliberately differ from the data representation:
switch (type()) {
case Type::PendingSubstitution:
// The value of a pending-substitution value is unknown, so they never compare equal
// (not even to themselves).
return false;
case Type::Calculated:
// Calculations compare by tree structure alone; the data also carries parse-context
// fields (resolved type, accepted ranges) that must not affect equality.
return as_calculated().equals(other);
default:
return StyleValueFFI::rust_style_value_equals(m_value.data(), other.m_value.data());
}
}
bool StyleValue::has_color() const
{
if (type() == Type::Color)
return true;
if (type() == Type::Keyword)
return as_keyword().has_color();
return false;
}
Optional<Color> StyleValue::to_color(ColorResolutionStyle color_resolution_style) const
{
if (type() != Type::Color && type() != Type::Keyword)
return {};
auto ffi_color_resolution_style = color_resolution_style.to_ffi();
auto resolved = StyleValueFFI::rust_style_value_to_color(m_value.operator->(), &ffi_color_resolution_style);
if (!resolved.resolved)
return {};
return Color(resolved.rgba[0], resolved.rgba[1], resolved.rgba[2], resolved.rgba[3]);
}
String StyleValue::to_string(SerializationMode mode) const
{
StringBuilder builder;
serialize(builder, mode);
return builder.to_string_without_validation();
}
Utf16String StyleValue::to_utf16_string(SerializationMode mode) const
{
auto text = StyleValueFFI::rust_style_value_serialize(m_value.operator->(), to_underlying(mode));
VERIFY(text);
return Utf16String::adopt_raw(text);
}
void StyleValue::serialize(Utf16StringBuilder& builder, SerializationMode mode) const
{
builder.append(to_utf16_string(mode));
}
AbstractImageStyleValue const& StyleValue::as_abstract_image() const
{
VERIFY(is_abstract_image());
return static_cast<AbstractImageStyleValue const&>(*this);
}
DimensionStyleValue const& StyleValue::as_dimension() const
{
VERIFY(is_dimension());
return static_cast<DimensionStyleValue const&>(*this);
}
#define __ENUMERATE_CSS_STYLE_VALUE_TYPE(title_case, snake_case, style_value_class_name) \
style_value_class_name const& StyleValue::as_##snake_case() const \
{ \
VERIFY(is_##snake_case()); \
return static_cast<style_value_class_name const&>(*this); \
}
ENUMERATE_CSS_STYLE_VALUE_TYPES
#undef __ENUMERATE_CSS_STYLE_VALUE_TYPE
StyleValueFFI::FfiElementFacts ElementFactsForRust::to_ffi() const
{
return {
.has_element = abstract_element.has_value(),
.callback_context = this,
.fill_container_bases = [](void const* opaque_facts, u8 unit_mask, void* length) {
auto const& facts = *static_cast<ElementFactsForRust const*>(opaque_facts);
*static_cast<ComputedValuesFFI::FfiLengthResolutionContext*>(length) = to_ffi_length_resolution_context_with_container_bases(*facts.length_resolution_context, unit_mask); },
.tree_counting = [](void const* opaque_facts, u64* sibling_count, u64* sibling_index) {
auto const& facts = *static_cast<ElementFactsForRust const*>(opaque_facts);
if (!facts.abstract_element.has_value())
return false;
const_cast<DOM::Element&>(facts.abstract_element->element()).set_style_uses_tree_counting_function();
auto tree_counting = facts.abstract_element->tree_counting_function_resolution_context();
*sibling_count = tree_counting.sibling_count;
*sibling_index = tree_counting.sibling_index;
return true; },
.random_base_value = [](void const* opaque_facts, u16 const* name, size_t name_length, bool element_shared, double* value) {
auto const& facts = *static_cast<ElementFactsForRust const*>(opaque_facts);
if (!facts.abstract_element.has_value())
return false;
// https://drafts.csswg.org/css-values-5/#random-caching
// NB: The style engine keeps the base values, one engine per document. A key names the element by its style
// node, and a pseudo-element's by its element's: the name, and the element unless the sharing is
// element-shared.
auto const& element = facts.abstract_element->element();
Layout::ForcedReadScope read { element.document() };
*value = bit_cast<double>(StyleEngineFFI::style_engine_ensure_random_base_value(element.document().style_computer().style_engine().host(), read, element.style_node_id(), { name, name_length }, element_shared));
return true; },
};
}
ValueComparingNonnullRefPtr<StyleValue const> StyleValue::absolutized(ComputationContext const& context) const
{
auto length_context = to_ffi_length_resolution_context(context.length_resolution_context);
auto document_base_url = context.abstract_element.has_value() ? context.abstract_element->document().serialized_base_url() : String {};
ElementFactsForRust element_facts { &context.length_resolution_context, context.abstract_element };
StyleValueFFI::FfiAbsolutizationContext ffi_context {
.length = &length_context,
.has_scheme = context.color_scheme.has_value(),
.scheme = static_cast<u8>(context.color_scheme.has_value() ? to_underlying(*context.color_scheme) : 0),
.document_base_url = document_base_url.bytes().data(),
.document_base_url_length = document_base_url.bytes().size(),
.element_facts = element_facts.to_ffi(),
};
if (auto const* absolutized = StyleValueFFI::rust_style_value_absolutize(m_value.operator->(), &ffi_context))
return adopt_rust_style_value_data(absolutized);
return *this;
}
bool StyleValue::has_auto() const
{
return is_keyword() && as_keyword().keyword() == Keyword::Auto;
}
// https://drafts.css-houdini.org/css-typed-om-1/#subdivide-into-iterations
StyleValueVector StyleValue::subdivide_into_iterations(PropertyNameAndID const& property) const
{
// To subdivide into iterations a CSS value whole value for a property property, execute the following steps:
// 1. If property is a single-valued property, return a list containing whole value.
// 2. Otherwise, divide whole value into individual iterations, as appropriate for property, and return a list
// containing the iterations in order.
// NB: We do this by type. By default, we assume step 1 applies. Step 2 applies to value lists.
if (type() == Type::ValueList)
return as_value_list().subdivide_into_iterations(property);
return StyleValueVector { *this };
}
double number_from_style_value(NonnullRefPtr<StyleValue const> const& style_value, Optional<double> percentage_basis)
{
if (style_value->is_number())
return style_value->as_number().number();
if (style_value->is_calculated()) {
auto const& calculated_style_value = style_value->as_calculated();
if (calculated_style_value.resolves_to_number())
return calculated_style_value.resolve_number({}).value();
if (calculated_style_value.resolves_to_percentage()) {
VERIFY(percentage_basis.has_value());
return calculated_style_value.resolve_percentage({}).value().as_fraction() * percentage_basis.value();
}
VERIFY_NOT_REACHED();
}
if (style_value->is_percentage()) {
VERIFY(percentage_basis.has_value());
return percentage_basis.value() * style_value->as_percentage().percentage().as_fraction();
}
VERIFY_NOT_REACHED();
}
Utf16FlyString string_from_style_value(NonnullRefPtr<StyleValue const> const& style_value)
{
if (style_value->is_string())
return style_value->as_string().string_value();
if (style_value->is_custom_ident())
return style_value->as_custom_ident().custom_ident();
VERIFY_NOT_REACHED();
}
Keyword StyleValue::to_keyword() const
{
if (is_keyword())
return static_cast<KeywordStyleValue const&>(*this).keyword();
return Keyword::Invalid;
}
}
// Called when Rust-owned style value data drops a retained Utf16FlyString.
extern "C" void ladybird_utf16_fly_string_unref(size_t raw)
{
Utf16FlyString::unref_raw(raw);
}