LibWeb: Split LengthStyleValue out of StyleValue.{h,cpp}

This commit is contained in:
Sam Atkins 2023-03-24 17:04:04 +00:00 committed by Linus Groh
parent 1591352531
commit 9a84151169
Notes: sideshowbarker 2024-07-17 12:02:22 +09:00
9 changed files with 89 additions and 53 deletions

View file

@ -85,6 +85,7 @@ set(SOURCES
CSS/StyleValues/GridTrackSizeStyleValue.cpp
CSS/StyleValues/IdentifierStyleValue.cpp
CSS/StyleValues/ImageStyleValue.cpp
CSS/StyleValues/LengthStyleValue.cpp
CSS/StyleValues/LinearGradientStyleValue.cpp
CSS/StyleValues/RadialGradientStyleValue.cpp
CSS/Supports.cpp

View file

@ -53,6 +53,7 @@
#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
#include <LibWeb/CSS/StyleValues/InheritStyleValue.h>
#include <LibWeb/CSS/StyleValues/InitialStyleValue.h>
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/CSS/StyleValues/LinearGradientStyleValue.h>
#include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h>
#include <LibWeb/DOM/Document.h>

View file

@ -24,6 +24,7 @@
#include <LibWeb/CSS/StyleValues/GridTrackSizeStyleValue.h>
#include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
#include <LibWeb/CSS/StyleValues/InitialStyleValue.h>
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/Layout/Viewport.h>

View file

@ -38,6 +38,7 @@
#include <LibWeb/CSS/StyleValues/GridTrackPlacementShorthandStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
#include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/FontCache.h>

View file

@ -36,6 +36,7 @@
#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
#include <LibWeb/CSS/StyleValues/InheritStyleValue.h>
#include <LibWeb/CSS/StyleValues/InitialStyleValue.h>
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/CSS/StyleValues/LinearGradientStyleValue.h>
#include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h>
#include <LibWeb/DOM/Document.h>
@ -1265,25 +1266,6 @@ ValueComparingNonnullRefPtr<RectStyleValue> RectStyleValue::create(EdgeRect rect
return adopt_ref(*new RectStyleValue(rect));
}
ValueComparingNonnullRefPtr<LengthStyleValue> LengthStyleValue::create(Length const& length)
{
if (length.is_auto()) {
static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_auto()));
return value;
}
if (length.is_px()) {
if (length.raw_value() == 0) {
static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(0)));
return value;
}
if (length.raw_value() == 1) {
static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(1)));
return value;
}
}
return adopt_ref(*new LengthStyleValue(length));
}
Optional<CSS::Length> absolutized_length(CSS::Length const& length, CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height)
{
if (length.is_px())
@ -1300,13 +1282,6 @@ ValueComparingNonnullRefPtr<StyleValue const> StyleValue::absolutized(CSSPixelRe
return *this;
}
ValueComparingNonnullRefPtr<StyleValue const> LengthStyleValue::absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const
{
if (auto length = absolutized_length(m_length, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height); length.has_value())
return LengthStyleValue::create(length.release_value());
return *this;
}
ValueComparingNonnullRefPtr<StyleValue const> ShadowStyleValue::absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const
{
auto absolutized_offset_x = absolutized_length(m_properties.offset_x, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height).value_or(m_properties.offset_x);

View file

@ -627,33 +627,6 @@ private:
NonnullOwnPtr<CalcSum> m_expression;
};
class LengthStyleValue : public StyleValueWithDefaultOperators<LengthStyleValue> {
public:
static ValueComparingNonnullRefPtr<LengthStyleValue> create(Length const&);
virtual ~LengthStyleValue() override = default;
Length const& length() const { return m_length; }
virtual bool has_auto() const override { return m_length.is_auto(); }
virtual bool has_length() const override { return true; }
virtual bool has_identifier() const override { return has_auto(); }
virtual ErrorOr<String> to_string() const override { return m_length.to_string(); }
virtual Length to_length() const override { return m_length; }
virtual ValueID to_identifier() const override { return has_auto() ? ValueID::Auto : ValueID::Invalid; }
virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const override;
bool properties_equal(LengthStyleValue const& other) const { return m_length == other.m_length; }
private:
explicit LengthStyleValue(Length const& length)
: StyleValueWithDefaultOperators(Type::Length)
, m_length(length)
{
}
Length m_length;
};
class ListStyleStyleValue final : public StyleValueWithDefaultOperators<ListStyleStyleValue> {
public:
static ValueComparingNonnullRefPtr<ListStyleStyleValue> create(

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "LengthStyleValue.h"
namespace Web::CSS {
ValueComparingNonnullRefPtr<LengthStyleValue> LengthStyleValue::create(Length const& length)
{
if (length.is_auto()) {
static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_auto()));
return value;
}
if (length.is_px()) {
if (length.raw_value() == 0) {
static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(0)));
return value;
}
if (length.raw_value() == 1) {
static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(1)));
return value;
}
}
return adopt_ref(*new LengthStyleValue(length));
}
ValueComparingNonnullRefPtr<StyleValue const> LengthStyleValue::absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const
{
if (auto length = absolutized_length(m_length, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height); length.has_value())
return LengthStyleValue::create(length.release_value());
return *this;
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/StyleValue.h>
namespace Web::CSS {
class LengthStyleValue : public StyleValueWithDefaultOperators<LengthStyleValue> {
public:
static ValueComparingNonnullRefPtr<LengthStyleValue> create(Length const&);
virtual ~LengthStyleValue() override = default;
Length const& length() const { return m_length; }
virtual bool has_auto() const override { return m_length.is_auto(); }
virtual bool has_length() const override { return true; }
virtual bool has_identifier() const override { return has_auto(); }
virtual ErrorOr<String> to_string() const override { return m_length.to_string(); }
virtual Length to_length() const override { return m_length; }
virtual ValueID to_identifier() const override { return has_auto() ? ValueID::Auto : ValueID::Invalid; }
virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const override;
bool properties_equal(LengthStyleValue const& other) const { return m_length == other.m_length; }
private:
explicit LengthStyleValue(Length const& length)
: StyleValueWithDefaultOperators(Type::Length)
, m_length(length)
{
}
Length m_length;
};
}

View file

@ -10,6 +10,7 @@
#include <AK/Utf32View.h>
#include <LibTextCodec/Decoder.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/DOM/Comment.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/DocumentType.h>