2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2023-05-24 16:28:09 +03:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
|
2021-08-09 22:28:56 +03:00
|
|
|
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
|
2022-11-08 19:31:01 +03:00
|
|
|
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
|
2023-01-14 02:44:44 +03:00
|
|
|
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-06-21 00:25:25 +03:00
|
|
|
#pragma once
|
|
|
|
|
2023-02-20 03:41:51 +03:00
|
|
|
#include <AK/Concepts.h>
|
2022-07-31 03:11:59 +03:00
|
|
|
#include <AK/GenericShorthands.h>
|
2021-07-23 00:01:39 +03:00
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2019-06-21 19:58:45 +03:00
|
|
|
#include <AK/RefCounted.h>
|
2019-06-22 22:48:21 +03:00
|
|
|
#include <AK/RefPtr.h>
|
2023-01-06 21:02:26 +03:00
|
|
|
#include <AK/String.h>
|
2019-06-22 22:48:21 +03:00
|
|
|
#include <AK/StringView.h>
|
2019-10-19 12:49:46 +03:00
|
|
|
#include <AK/URL.h>
|
2021-07-23 00:01:39 +03:00
|
|
|
#include <AK/Variant.h>
|
|
|
|
#include <AK/Vector.h>
|
2019-10-19 12:49:46 +03:00
|
|
|
#include <AK/WeakPtr.h>
|
2023-07-09 11:10:17 +03:00
|
|
|
#include <LibGfx/Color.h>
|
2022-04-13 19:29:01 +03:00
|
|
|
#include <LibWeb/CSS/Enums.h>
|
2020-03-07 12:32:51 +03:00
|
|
|
#include <LibWeb/CSS/Length.h>
|
2020-12-15 22:39:09 +03:00
|
|
|
#include <LibWeb/CSS/ValueID.h>
|
2020-07-21 17:23:08 +03:00
|
|
|
#include <LibWeb/Forward.h>
|
2019-06-21 00:25:25 +03:00
|
|
|
|
2020-07-21 17:23:08 +03:00
|
|
|
namespace Web::CSS {
|
2019-10-06 11:25:08 +03:00
|
|
|
|
2023-02-20 03:41:51 +03:00
|
|
|
template<typename T>
|
|
|
|
struct ValueComparingNonnullRefPtr : public NonnullRefPtr<T> {
|
|
|
|
using NonnullRefPtr<T>::NonnullRefPtr;
|
|
|
|
|
|
|
|
ValueComparingNonnullRefPtr(NonnullRefPtr<T> const& other)
|
|
|
|
: NonnullRefPtr<T>(other)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueComparingNonnullRefPtr(NonnullRefPtr<T>&& other)
|
|
|
|
: NonnullRefPtr<T>(move(other))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(ValueComparingNonnullRefPtr const& other) const
|
|
|
|
{
|
|
|
|
return this->ptr() == other.ptr() || this->ptr()->equals(*other);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
using NonnullRefPtr<T>::operator==;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct ValueComparingRefPtr : public RefPtr<T> {
|
|
|
|
using RefPtr<T>::RefPtr;
|
|
|
|
|
|
|
|
ValueComparingRefPtr(RefPtr<T> const& other)
|
|
|
|
: RefPtr<T>(other)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueComparingRefPtr(RefPtr<T>&& other)
|
|
|
|
: RefPtr<T>(move(other))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
bool operator==(ValueComparingNonnullRefPtr<U> const& other) const
|
|
|
|
{
|
|
|
|
return this->ptr() == other.ptr() || (this->ptr() && this->ptr()->equals(*other));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(ValueComparingRefPtr const& other) const
|
|
|
|
{
|
|
|
|
return this->ptr() == other.ptr() || (this->ptr() && other.ptr() && this->ptr()->equals(*other));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
using RefPtr<T>::operator==;
|
|
|
|
};
|
|
|
|
|
2023-03-06 16:33:11 +03:00
|
|
|
using StyleValueVector = Vector<ValueComparingNonnullRefPtr<StyleValue const>>;
|
2023-02-20 03:41:51 +03:00
|
|
|
|
2023-08-20 15:48:13 +03:00
|
|
|
#define ENUMERATE_STYLE_VALUE_TYPES \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Angle, angle) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Background, background) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(BackgroundRepeat, background_repeat) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(BackgroundSize, background_size) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Border, border) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(BorderRadius, border_radius) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(BorderRadiusShorthand, border_radius_shorthand) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Calculated, calculated) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Color, color) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Composite, composite) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(ConicGradient, conic_gradient) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Content, content) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(CustomIdent, custom_ident) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Display, display) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Easing, easing) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Edge, edge) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(FilterValueList, filter_value_list) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Flex, flex) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(FlexFlow, flex_flow) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Font, font) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Frequency, frequency) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(GridAreaShorthand, grid_area_shorthand) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(GridAutoFlow, grid_auto_flow) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(GridTemplateArea, grid_template_area) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(GridTrackPlacement, grid_track_placement) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(GridTrackPlacementShorthand, grid_track_placement_shorthand) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(GridTrackSizeList, grid_track_size_list) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(GridTrackSizeListShorthand, grid_track_size_list_shorthand) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Identifier, identifier) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Image, image) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Inherit, inherit) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Initial, initial) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Integer, integer) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Length, length) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(LinearGradient, linear_gradient) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(ListStyle, list_style) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Number, number) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Overflow, overflow) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Percentage, percentage) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(PlaceContent, place_content) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(PlaceItems, place_items) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(PlaceSelf, place_self) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Position, position) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(RadialGradient, radial_gradient) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Ratio, ratio) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Rect, rect) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Resolution, resolution) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Revert, revert) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Shadow, shadow) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(String, string) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(TextDecoration, text_decoration) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Time, time) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Transformation, transformation) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Unresolved, unresolved) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(Unset, unset) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(URL, url) \
|
|
|
|
__ENUMERATE_STYLE_VALUE_TYPE(ValueList, value_list)
|
|
|
|
|
|
|
|
// NOTE:
|
|
|
|
using ValueListStyleValue = StyleValueList;
|
|
|
|
|
2019-06-21 16:29:31 +03:00
|
|
|
class StyleValue : public RefCounted<StyleValue> {
|
2019-06-21 00:25:25 +03:00
|
|
|
public:
|
2022-03-14 22:21:51 +03:00
|
|
|
virtual ~StyleValue() = default;
|
2019-06-21 00:25:25 +03:00
|
|
|
|
2019-07-03 08:55:22 +03:00
|
|
|
enum class Type {
|
2023-08-20 15:48:13 +03:00
|
|
|
#define __ENUMERATE_STYLE_VALUE_TYPE(TitleCaseName, SnakeCaseName) \
|
|
|
|
TitleCaseName,
|
|
|
|
ENUMERATE_STYLE_VALUE_TYPES
|
|
|
|
#undef __ENUMERATE_STYLE_VALUE_TYPE
|
2019-06-21 00:25:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Type type() const { return m_type; }
|
|
|
|
|
2023-08-20 15:48:13 +03:00
|
|
|
#define __ENUMERATE_STYLE_VALUE_TYPE(TitleCaseName, SnakeCaseName) \
|
|
|
|
bool is_##SnakeCaseName() const { return type() == Type::TitleCaseName; } \
|
|
|
|
TitleCaseName##StyleValue const& as_##SnakeCaseName() const; \
|
|
|
|
TitleCaseName##StyleValue& as_##SnakeCaseName() { return const_cast<TitleCaseName##StyleValue&>(const_cast<StyleValue const&>(*this).as_##SnakeCaseName()); }
|
|
|
|
ENUMERATE_STYLE_VALUE_TYPES
|
|
|
|
#undef __ENUMERATE_STYLE_VALUE_TYPE
|
2021-08-10 14:23:27 +03:00
|
|
|
|
2023-08-20 15:48:13 +03:00
|
|
|
bool is_abstract_image() const
|
|
|
|
{
|
|
|
|
return AK::first_is_one_of(type(), Type::Image, Type::LinearGradient, Type::ConicGradient, Type::RadialGradient);
|
|
|
|
}
|
2022-07-31 03:11:59 +03:00
|
|
|
AbstractImageStyleValue const& as_abstract_image() const;
|
|
|
|
AbstractImageStyleValue& as_abstract_image() { return const_cast<AbstractImageStyleValue&>(const_cast<StyleValue const&>(*this).as_abstract_image()); }
|
2023-08-20 15:48:13 +03:00
|
|
|
|
|
|
|
bool is_builtin() const { return is_inherit() || is_initial() || is_unset(); }
|
2021-09-23 21:54:19 +03:00
|
|
|
|
2023-04-19 14:00:38 +03:00
|
|
|
bool has_auto() const;
|
2021-09-23 21:54:19 +03:00
|
|
|
virtual bool has_color() const { return false; }
|
2021-09-23 14:45:08 +03:00
|
|
|
|
2023-08-19 17:33:21 +03:00
|
|
|
virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const;
|
2022-02-26 03:35:25 +03:00
|
|
|
|
2023-04-19 20:31:00 +03:00
|
|
|
virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const { return {}; }
|
2023-04-19 14:00:38 +03:00
|
|
|
ValueID to_identifier() const;
|
2023-01-06 21:02:26 +03:00
|
|
|
virtual ErrorOr<String> to_string() const = 0;
|
2019-08-18 09:09:56 +03:00
|
|
|
|
2023-05-24 16:28:09 +03:00
|
|
|
[[nodiscard]] int to_font_weight() const;
|
|
|
|
[[nodiscard]] int to_font_slope() const;
|
2023-08-07 22:44:10 +03:00
|
|
|
[[nodiscard]] int to_font_stretch_width() const;
|
2023-05-24 16:28:09 +03:00
|
|
|
|
2023-02-20 03:41:51 +03:00
|
|
|
virtual bool equals(StyleValue const& other) const = 0;
|
|
|
|
|
|
|
|
bool operator==(StyleValue const& other) const
|
|
|
|
{
|
|
|
|
return this->equals(other);
|
|
|
|
}
|
2020-12-14 17:04:13 +03:00
|
|
|
|
2019-06-21 00:25:25 +03:00
|
|
|
protected:
|
|
|
|
explicit StyleValue(Type);
|
|
|
|
|
|
|
|
private:
|
2023-05-06 14:43:29 +03:00
|
|
|
Type m_type;
|
2019-06-21 00:25:25 +03:00
|
|
|
};
|
2019-06-22 22:48:21 +03:00
|
|
|
|
2023-02-11 21:12:00 +03:00
|
|
|
template<typename T>
|
|
|
|
struct StyleValueWithDefaultOperators : public StyleValue {
|
|
|
|
using StyleValue::StyleValue;
|
|
|
|
|
2023-02-20 03:41:51 +03:00
|
|
|
virtual bool equals(StyleValue const& other) const override
|
2023-02-11 21:12:00 +03:00
|
|
|
{
|
|
|
|
if (type() != other.type())
|
|
|
|
return false;
|
|
|
|
auto const& typed_other = static_cast<T const&>(other);
|
2023-02-20 03:41:51 +03:00
|
|
|
return static_cast<T const&>(*this).properties_equal(typed_other);
|
2023-02-11 21:12:00 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-07 12:27:02 +03:00
|
|
|
}
|
2022-02-02 23:53:55 +03:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct AK::Formatter<Web::CSS::StyleValue> : Formatter<StringView> {
|
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::StyleValue const& style_value)
|
|
|
|
{
|
2023-01-06 21:02:26 +03:00
|
|
|
return Formatter<StringView>::format(builder, TRY(style_value.to_string()));
|
2022-02-02 23:53:55 +03:00
|
|
|
}
|
|
|
|
};
|