2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2025-02-26 14:16:36 -03:00
|
|
|
* Copyright (c) 2018-2025, Andreas Kling <andreas@ladybird.org>
|
2025-02-21 10:04:20 -03:00
|
|
|
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
|
2022-01-24 14:38:29 -03:00
|
|
|
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
|
2023-01-13 20:44:44 -03:00
|
|
|
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2026-01-08 09:02:18 -03:00
|
|
|
#include <AK/StringBuilder.h>
|
2025-02-02 11:55:26 -03:00
|
|
|
#include <LibGfx/Font/Font.h>
|
2023-05-24 10:28:09 -03:00
|
|
|
#include <LibGfx/Font/FontStyleMapping.h>
|
2025-08-13 10:46:21 -03:00
|
|
|
#include <LibWeb/CSS/CSSStyleValue.h>
|
2025-10-23 22:27:15 -03:00
|
|
|
#include <LibWeb/CSS/ComputedProperties.h>
|
2025-07-10 08:17:27 -03:00
|
|
|
#include <LibWeb/CSS/Parser/Parser.h>
|
2023-03-24 13:42:50 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
|
2025-07-30 09:14:53 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/AnchorSizeStyleValue.h>
|
2025-08-03 09:58:13 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/AnchorStyleValue.h>
|
2023-03-23 14:26:13 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
|
2023-03-23 14:54:05 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
|
2024-05-25 19:06:47 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/BasicShapeStyleValue.h>
|
2025-06-10 12:58:54 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/BorderImageSliceStyleValue.h>
|
2026-01-05 04:55:26 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/BorderRadiusRectStyleValue.h>
|
2023-03-24 21:33:20 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
|
2024-12-11 12:05:56 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
|
2026-03-18 05:45:10 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ColorInterpolationMethodStyleValue.h>
|
2025-01-01 22:59:09 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ColorSchemeStyleValue.h>
|
2025-08-08 07:24:15 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
|
2023-03-24 13:09:41 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ConicGradientStyleValue.h>
|
2023-03-23 18:17:43 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
|
2024-07-24 11:47:11 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/CounterDefinitionsStyleValue.h>
|
2026-02-03 20:24:04 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/CounterStyleStyleValue.h>
|
2026-01-31 05:09:11 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/CounterStyleSystemStyleValue.h>
|
2024-07-24 10:56:16 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/CounterStyleValue.h>
|
2025-02-21 10:04:20 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/CursorStyleValue.h>
|
2023-05-25 08:43:52 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/CustomIdentStyleValue.h>
|
2023-04-26 16:05:38 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
|
2023-07-05 19:59:36 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/EasingStyleValue.h>
|
2023-04-02 18:56:45 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
|
2023-03-23 18:26:03 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
|
2023-09-28 11:18:14 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/FlexStyleValue.h>
|
2025-04-02 13:01:16 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/FontSourceStyleValue.h>
|
2025-05-02 09:55:58 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/FontStyleStyleValue.h>
|
2023-03-23 18:50:24 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/FrequencyStyleValue.h>
|
2026-04-22 08:56:54 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/FunctionStyleValue.h>
|
2023-08-17 15:25:18 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/GridAutoFlowStyleValue.h>
|
2023-03-24 11:25:00 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
|
2023-03-24 11:53:08 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
|
2023-04-29 12:59:07 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
|
2025-03-12 12:29:12 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/GuaranteedInvalidStyleValue.h>
|
2023-03-24 12:17:11 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
|
2023-06-01 13:01:09 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
|
2025-08-08 06:28:41 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/KeywordStyleValue.h>
|
2023-03-24 14:04:04 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
|
2023-03-24 13:17:50 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/LinearGradientStyleValue.h>
|
2023-06-01 12:16:15 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
|
2026-04-13 10:35:53 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/OpacityValueStyleValue.h>
|
2024-09-30 13:05:52 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/OpenTypeTaggedStyleValue.h>
|
2025-03-12 16:24:59 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/PendingSubstitutionStyleValue.h>
|
2023-03-24 14:28:43 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
|
2023-03-24 14:35:31 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
|
2023-03-24 13:35:18 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h>
|
2025-12-10 09:15:17 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/RadialSizeStyleValue.h>
|
2025-11-03 21:35:17 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/RandomValueSharingStyleValue.h>
|
2023-06-06 11:42:43 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/RatioStyleValue.h>
|
2023-03-24 21:02:50 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/RectStyleValue.h>
|
2025-07-18 15:36:09 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/RepeatStyleStyleValue.h>
|
2023-03-24 14:40:28 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>
|
2025-05-26 18:36:12 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ScrollbarColorStyleValue.h>
|
2024-06-26 19:19:38 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ScrollbarGutterStyleValue.h>
|
2023-03-24 14:45:25 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
|
2023-09-19 08:21:15 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
|
2023-03-24 14:48:42 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/StringStyleValue.h>
|
2025-08-08 06:11:51 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/StyleValue.h>
|
2023-03-24 21:12:21 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
|
2025-10-08 06:43:27 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/SuperellipseStyleValue.h>
|
2025-11-18 07:53:25 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/TextIndentStyleValue.h>
|
2025-09-13 04:27:52 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/TextUnderlinePositionStyleValue.h>
|
2023-03-24 20:47:56 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
|
2023-03-24 14:59:33 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
|
2025-10-01 04:02:33 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/TreeCountingFunctionStyleValue.h>
|
2026-02-17 06:15:22 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/TupleStyleValue.h>
|
2023-04-20 14:31:00 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/URLStyleValue.h>
|
2025-03-27 14:35:06 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/UnicodeRangeStyleValue.h>
|
2023-03-24 20:53:41 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
|
2026-01-26 13:25:41 -03:00
|
|
|
#include <LibWeb/CSS/SystemColor.h>
|
2026-02-08 12:42:02 -03:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2025-10-16 10:07:09 -03:00
|
|
|
#include <LibWeb/HTML/Navigable.h>
|
2025-07-18 20:31:07 -03:00
|
|
|
#include <LibWeb/Layout/Node.h>
|
2019-06-22 16:48:21 -03:00
|
|
|
|
2020-07-26 15:01:35 -03:00
|
|
|
namespace Web::CSS {
|
2020-03-07 06:27:02 -03:00
|
|
|
|
2025-07-19 00:38:16 -03:00
|
|
|
ColorResolutionContext ColorResolutionContext::for_element(DOM::AbstractElement const& element)
|
|
|
|
|
{
|
|
|
|
|
auto color_scheme = element.computed_properties()->color_scheme(element.document().page().preferred_color_scheme(), element.document().supported_color_schemes());
|
|
|
|
|
|
|
|
|
|
CalculationResolutionContext calculation_resolution_context { .length_resolution_context = Length::ResolutionContext::for_element(element) };
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
.color_scheme = color_scheme,
|
2026-03-24 08:48:45 -03:00
|
|
|
.current_color = element.computed_properties()->color(PropertyID::Color, { color_scheme, CSS::InitialValues::color(), CSS::SystemColor::accent_color(color_scheme), element.document(), calculation_resolution_context }),
|
2026-03-24 08:35:33 -03:00
|
|
|
.accent_color = element.computed_properties()->accent_color({ color_scheme, CSS::InitialValues::color(), CSS::SystemColor::accent_color(color_scheme), element.document(), calculation_resolution_context }),
|
2025-07-19 00:38:16 -03:00
|
|
|
.document = element.document(),
|
|
|
|
|
.calculation_resolution_context = calculation_resolution_context
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-18 20:31:07 -03:00
|
|
|
ColorResolutionContext ColorResolutionContext::for_layout_node_with_style(Layout::NodeWithStyle const& layout_node)
|
|
|
|
|
{
|
|
|
|
|
return {
|
|
|
|
|
.color_scheme = layout_node.computed_values().color_scheme(),
|
|
|
|
|
.current_color = layout_node.computed_values().color(),
|
2026-01-26 13:25:41 -03:00
|
|
|
.accent_color = layout_node.computed_values().accent_color(),
|
2025-07-18 20:31:07 -03:00
|
|
|
.document = layout_node.document(),
|
|
|
|
|
.calculation_resolution_context = { .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node) },
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-08 06:11:51 -03:00
|
|
|
StyleValue::StyleValue(Type type)
|
2019-06-22 16:48:21 -03:00
|
|
|
: m_type(type)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-08 09:02:18 -03:00
|
|
|
String StyleValue::to_string(SerializationMode mode) const
|
|
|
|
|
{
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
serialize(builder, mode);
|
|
|
|
|
return builder.to_string_without_validation();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-08 06:11:51 -03:00
|
|
|
AbstractImageStyleValue const& StyleValue::as_abstract_image() const
|
2022-07-30 21:11:59 -03:00
|
|
|
{
|
|
|
|
|
VERIFY(is_abstract_image());
|
|
|
|
|
return static_cast<AbstractImageStyleValue const&>(*this);
|
2025-07-30 09:14:53 -03:00
|
|
|
}
|
|
|
|
|
|
2025-08-08 07:08:54 -03:00
|
|
|
DimensionStyleValue const& StyleValue::as_dimension() const
|
|
|
|
|
{
|
|
|
|
|
VERIFY(is_dimension());
|
|
|
|
|
return static_cast<DimensionStyleValue const&>(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-08 04:09:51 -03:00
|
|
|
#define __ENUMERATE_CSS_STYLE_VALUE_TYPE(title_case, snake_case, style_value_class_name) \
|
2025-08-08 06:11:51 -03:00
|
|
|
style_value_class_name const& StyleValue::as_##snake_case() const \
|
2025-08-08 04:09:51 -03:00
|
|
|
{ \
|
|
|
|
|
VERIFY(is_##snake_case()); \
|
|
|
|
|
return static_cast<style_value_class_name const&>(*this); \
|
|
|
|
|
}
|
|
|
|
|
ENUMERATE_CSS_STYLE_VALUE_TYPES
|
|
|
|
|
#undef __ENUMERATE_CSS_STYLE_VALUE_TYPE
|
2021-09-23 15:54:19 -03:00
|
|
|
|
2025-10-21 08:48:32 -03:00
|
|
|
ValueComparingNonnullRefPtr<StyleValue const> StyleValue::absolutized(ComputationContext const&) const
|
2023-03-31 10:25:38 -03:00
|
|
|
{
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-08 06:11:51 -03:00
|
|
|
bool StyleValue::has_auto() const
|
2023-04-19 08:00:38 -03:00
|
|
|
{
|
2024-08-14 10:06:03 -03:00
|
|
|
return is_keyword() && as_keyword().keyword() == Keyword::Auto;
|
2023-04-19 08:00:38 -03:00
|
|
|
}
|
|
|
|
|
|
2025-08-08 06:11:51 -03:00
|
|
|
Vector<Parser::ComponentValue> StyleValue::tokenize() const
|
2025-07-10 08:17:27 -03:00
|
|
|
{
|
|
|
|
|
// This is an inefficient way of producing ComponentValues, but it's guaranteed to work for types that round-trip.
|
|
|
|
|
// FIXME: Implement better versions in the subclasses.
|
|
|
|
|
return Parser::Parser::create(Parser::ParsingParams {}, to_string(SerializationMode::Normal)).parse_as_list_of_component_values();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-13 10:46:21 -03:00
|
|
|
// https://drafts.css-houdini.org/css-typed-om-1/#reify-as-a-cssstylevalue
|
2025-09-25 09:00:43 -03:00
|
|
|
GC::Ref<CSSStyleValue> StyleValue::reify(JS::Realm& realm, FlyString const& associated_property) const
|
2025-08-13 10:46:21 -03:00
|
|
|
{
|
|
|
|
|
// 1. Return a new CSSStyleValue object representing value whose [[associatedProperty]] internal slot is set to property.
|
2025-09-19 10:54:23 -03:00
|
|
|
return CSSStyleValue::create(realm, associated_property, *this);
|
2025-08-13 10:46:21 -03:00
|
|
|
}
|
|
|
|
|
|
2025-11-03 14:52:08 -03:00
|
|
|
// https://drafts.css-houdini.org/css-typed-om-1/#subdivide-into-iterations
|
|
|
|
|
StyleValueVector StyleValue::subdivide_into_iterations(PropertyNameAndID const&) 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. For step 2, override this method.
|
|
|
|
|
return StyleValueVector { *this };
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 05:33:07 -03:00
|
|
|
i32 int_from_style_value(NonnullRefPtr<StyleValue const> const& style_value)
|
2026-02-04 03:34:33 -03:00
|
|
|
{
|
|
|
|
|
if (style_value->is_integer())
|
|
|
|
|
return style_value->as_integer().integer();
|
|
|
|
|
|
|
|
|
|
if (style_value->is_calculated())
|
|
|
|
|
return style_value->as_calculated().resolve_integer({}).value();
|
|
|
|
|
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 00:36:07 -03:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-04 03:59:10 -03:00
|
|
|
FlyString const& 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();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 06:27:02 -03:00
|
|
|
}
|