diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2023-03-24 17:04:04 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-03-25 16:56:04 +0000 |
commit | 9a8415116948b4ad72f95deb217b8bc8f82b755e (patch) | |
tree | 5054d6bd2e3b705b5f11001f9009bbf9b91d4015 | |
parent | 1591352531afa22990066820bb16996ce0e9b0b8 (diff) | |
download | serenity-9a8415116948b4ad72f95deb217b8bc8f82b755e.zip |
LibWeb: Split LengthStyleValue out of StyleValue.{h,cpp}
9 files changed, 89 insertions, 53 deletions
diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 2cc8600601..b290750cbc 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -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 diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index eca8d6b85e..c16695e917 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -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> diff --git a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp index 1e281556e4..d126a21e41 100644 --- a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp +++ b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp @@ -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> diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 89f8868c47..ad047bbaf5 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -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> diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index 8fcf7414a0..f1118fc656 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -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); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index 500fcc5d13..4664a43036 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -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( diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.cpp new file mode 100644 index 0000000000..99fbe547de --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.cpp @@ -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; +} + +} diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.h new file mode 100644 index 0000000000..5f23c207b8 --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.h @@ -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; +}; + +} diff --git a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp index 3648116461..83d0827135 100644 --- a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp +++ b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp @@ -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> |