Sfoglia il codice sorgente

LibWeb: Move non-trivial StyleValue to_string() methods to cpp file

Many of these will need to change in the future in order to include
features we don't yet support, and touching StyleValue.h is a great way
to have to wait for all of LibWeb to rebuild. I'm hoping this saves me
time in the long run. :^)
Sam Atkins 3 anni fa
parent
commit
f22d8c8f77

+ 106 - 6
Userland/Libraries/LibWeb/CSS/StyleValue.cpp

@@ -1,12 +1,13 @@
 /*
  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
+ * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
 #include <AK/ByteBuffer.h>
 #include <LibGfx/Palette.h>
+#include <LibWeb/CSS/Serialize.h>
 #include <LibWeb/CSS/StyleValue.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/HTML/BrowsingContext.h>
@@ -247,6 +248,59 @@ String BackgroundStyleValue::to_string() const
     return builder.to_string();
 }
 
+String BackgroundRepeatStyleValue::to_string() const
+{
+    return String::formatted("{} {}", CSS::to_string(m_repeat_x), CSS::to_string(m_repeat_y));
+}
+
+String BackgroundSizeStyleValue::to_string() const
+{
+    return String::formatted("{} {}", m_size_x.to_string(), m_size_y.to_string());
+}
+
+String BorderStyleValue::to_string() const
+{
+    return String::formatted("Border border_width: {}, border_style: {}, border_color: {}", m_border_width->to_string(), m_border_style->to_string(), m_border_color->to_string());
+}
+
+String BorderRadiusStyleValue::to_string() const
+{
+    return String::formatted("{} / {}", m_horizontal_radius.to_string(), m_vertical_radius.to_string());
+}
+
+String BoxShadowStyleValue::to_string() const
+{
+    return String::formatted("BoxShadow offset_x: {}, offset_y: {}, blur_radius: {}, color: {}", m_offset_x.to_string(), m_offset_y.to_string(), m_blur_radius.to_string(), m_color.to_string());
+}
+
+// https://www.w3.org/TR/css-color-4/#serializing-sRGB-values
+String ColorStyleValue::to_string() const
+{
+    if (m_color.alpha() == 1)
+        return String::formatted("rgb({}, {}, {})", m_color.red(), m_color.green(), m_color.blue());
+    return String::formatted("rgba({}, {}, {}, {})", m_color.red(), m_color.green(), m_color.blue(), (float)(m_color.alpha()) / 255.0f);
+}
+
+String CombinedBorderRadiusStyleValue::to_string() const
+{
+    return String::formatted("{} {} {} {} / {} {} {} {}", m_top_left->horizontal_radius().to_string(), m_top_right->horizontal_radius().to_string(), m_bottom_right->horizontal_radius().to_string(), m_bottom_left->horizontal_radius().to_string(), m_top_left->vertical_radius().to_string(), m_top_right->vertical_radius().to_string(), m_bottom_right->vertical_radius().to_string(), m_bottom_left->vertical_radius().to_string());
+}
+
+String FlexStyleValue::to_string() const
+{
+    return String::formatted("Flex grow: {}, shrink: {}, basis: {}", m_grow->to_string(), m_shrink->to_string(), m_basis->to_string());
+}
+
+String FlexFlowStyleValue::to_string() const
+{
+    return String::formatted("FlexFlow flex_direction: {}, flex_wrap: {}", m_flex_direction->to_string(), m_flex_wrap->to_string());
+}
+
+String FontStyleValue::to_string() const
+{
+    return String::formatted("Font style: {}, weight: {}, size: {}, line_height: {}, families: {}", m_font_style->to_string(), m_font_weight->to_string(), m_font_size->to_string(), m_line_height->to_string(), m_font_families->to_string());
+}
+
 String IdentifierStyleValue::to_string() const
 {
     return CSS::string_from_value_id(m_id);
@@ -471,12 +525,35 @@ void ImageStyleValue::resource_did_load()
         m_document->browsing_context()->set_needs_display({});
 }
 
-// https://www.w3.org/TR/css-color-4/#serializing-sRGB-values
-String ColorStyleValue::to_string() const
+String ImageStyleValue::to_string() const
 {
-    if (m_color.alpha() == 1)
-        return String::formatted("rgb({}, {}, {})", m_color.red(), m_color.green(), m_color.blue());
-    return String::formatted("rgba({}, {}, {}, {})", m_color.red(), m_color.green(), m_color.blue(), (float)(m_color.alpha()) / 255.0f);
+    return String::formatted("Image({})", m_url.to_string());
+}
+
+String ListStyleStyleValue::to_string() const
+{
+    return String::formatted("ListStyle position: {}, image: {}, style_type: {}", m_position->to_string(), m_image->to_string(), m_style_type->to_string());
+}
+
+String NumericStyleValue::to_string() const
+{
+    return m_value.visit(
+        [](float value) {
+            return String::formatted("{}", value);
+        },
+        [](i64 value) {
+            return String::formatted("{}", value);
+        });
+}
+
+String OverflowStyleValue::to_string() const
+{
+    return String::formatted("{} {}", m_overflow_x->to_string(), m_overflow_y->to_string());
+}
+
+String PercentageStyleValue::to_string() const
+{
+    return m_percentage.to_string();
 }
 
 String PositionStyleValue::to_string() const
@@ -498,6 +575,16 @@ String PositionStyleValue::to_string() const
     return String::formatted("{} {} {} {}", to_string(m_edge_x), m_offset_x.to_string(), to_string(m_edge_y), m_offset_y.to_string());
 }
 
+String TextDecorationStyleValue::to_string() const
+{
+    return String::formatted("TextDecoration line: {}, style: {}, color: {}", m_line->to_string(), m_style->to_string(), m_color->to_string());
+}
+
+String TransformationStyleValue::to_string() const
+{
+    return "TransformationStyleValue";
+}
+
 String UnresolvedStyleValue::to_string() const
 {
     StringBuilder builder;
@@ -506,4 +593,17 @@ String UnresolvedStyleValue::to_string() const
     return builder.to_string();
 }
 
+String StyleValueList::to_string() const
+{
+    StringBuilder builder;
+    builder.appendff("List[{}](", m_values.size());
+    for (size_t i = 0; i < m_values.size(); ++i) {
+        if (i)
+            builder.append(',');
+        builder.append(m_values[i].to_string());
+    }
+    builder.append(')');
+    return builder.to_string();
+}
+
 }

+ 19 - 79
Userland/Libraries/LibWeb/CSS/StyleValue.h

@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
- * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
+ * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -487,10 +487,7 @@ public:
     Repeat repeat_x() const { return m_repeat_x; }
     Repeat repeat_y() const { return m_repeat_y; }
 
-    virtual String to_string() const override
-    {
-        return String::formatted("{} {}", CSS::to_string(m_repeat_x), CSS::to_string(m_repeat_y));
-    }
+    virtual String to_string() const override;
 
 private:
     BackgroundRepeatStyleValue(Repeat repeat_x, Repeat repeat_y)
@@ -516,10 +513,7 @@ public:
     LengthPercentage size_x() const { return m_size_x; }
     LengthPercentage size_y() const { return m_size_y; }
 
-    virtual String to_string() const override
-    {
-        return String::formatted("{} {}", m_size_x.to_string(), m_size_y.to_string());
-    }
+    virtual String to_string() const override;
 
 private:
     BackgroundSizeStyleValue(LengthPercentage size_x, LengthPercentage size_y)
@@ -548,10 +542,7 @@ public:
     NonnullRefPtr<StyleValue> border_style() const { return m_border_style; }
     NonnullRefPtr<StyleValue> border_color() const { return m_border_color; }
 
-    virtual String to_string() const override
-    {
-        return String::formatted("Border border_width: {}, border_style: {}, border_color: {}", m_border_width->to_string(), m_border_style->to_string(), m_border_color->to_string());
-    }
+    virtual String to_string() const override;
 
 private:
     BorderStyleValue(
@@ -582,10 +573,7 @@ public:
     LengthPercentage const& vertical_radius() const { return m_vertical_radius; }
     bool is_elliptical() const { return m_is_elliptical; }
 
-    virtual String to_string() const override
-    {
-        return String::formatted("{} / {}", m_horizontal_radius.to_string(), m_vertical_radius.to_string());
-    }
+    virtual String to_string() const override;
 
     virtual bool equals(StyleValue const& other) const override
     {
@@ -633,13 +621,13 @@ public:
     }
     virtual ~BoxShadowStyleValue() override { }
 
+    // FIXME: Spread-distance and "inset" flag
     Length const& offset_x() const { return m_offset_x; }
     Length const& offset_y() const { return m_offset_y; }
     Length const& blur_radius() const { return m_blur_radius; }
     Color const& color() const { return m_color; }
 
-    String to_string() const override { return String::formatted("BoxShadow offset_x: {}, offset_y: {}, blur_radius: {}, color: {}",
-        m_offset_x.to_string(), m_offset_y.to_string(), m_blur_radius.to_string(), m_color.to_string()); }
+    virtual String to_string() const override;
 
 private:
     explicit BoxShadowStyleValue(Length const& offset_x, Length const& offset_y, Length const& blur_radius, Color const& color)
@@ -814,10 +802,7 @@ public:
     NonnullRefPtr<BorderRadiusStyleValue> bottom_right() const { return m_bottom_right; }
     NonnullRefPtr<BorderRadiusStyleValue> bottom_left() const { return m_bottom_left; }
 
-    virtual String to_string() const override
-    {
-        return String::formatted("{} {} {} {} / {} {} {} {}", m_top_left->horizontal_radius().to_string(), m_top_right->horizontal_radius().to_string(), m_bottom_right->horizontal_radius().to_string(), m_bottom_left->horizontal_radius().to_string(), m_top_left->vertical_radius().to_string(), m_top_right->vertical_radius().to_string(), m_bottom_right->vertical_radius().to_string(), m_bottom_left->vertical_radius().to_string());
-    }
+    virtual String to_string() const override;
 
 private:
     CombinedBorderRadiusStyleValue(NonnullRefPtr<BorderRadiusStyleValue> top_left, NonnullRefPtr<BorderRadiusStyleValue> top_right, NonnullRefPtr<BorderRadiusStyleValue> bottom_right, NonnullRefPtr<BorderRadiusStyleValue> bottom_left)
@@ -850,10 +835,7 @@ public:
     NonnullRefPtr<StyleValue> shrink() const { return m_shrink; }
     NonnullRefPtr<StyleValue> basis() const { return m_basis; }
 
-    virtual String to_string() const override
-    {
-        return String::formatted("Flex grow: {}, shrink: {}, basis: {}", m_grow->to_string(), m_shrink->to_string(), m_basis->to_string());
-    }
+    virtual String to_string() const override;
 
 private:
     FlexStyleValue(
@@ -883,10 +865,7 @@ public:
     NonnullRefPtr<StyleValue> flex_direction() const { return m_flex_direction; }
     NonnullRefPtr<StyleValue> flex_wrap() const { return m_flex_wrap; }
 
-    virtual String to_string() const override
-    {
-        return String::formatted("FlexFlow flex_direction: {}, flex_wrap: {}", m_flex_direction->to_string(), m_flex_wrap->to_string());
-    }
+    virtual String to_string() const override;
 
 private:
     FlexFlowStyleValue(NonnullRefPtr<StyleValue> flex_direction, NonnullRefPtr<StyleValue> flex_wrap)
@@ -911,11 +890,7 @@ public:
     NonnullRefPtr<StyleValue> line_height() const { return m_line_height; }
     NonnullRefPtr<StyleValue> font_families() const { return m_font_families; }
 
-    virtual String to_string() const override
-    {
-        return String::formatted("Font style: {}, weight: {}, size: {}, line_height: {}, families: {}",
-            m_font_style->to_string(), m_font_weight->to_string(), m_font_size->to_string(), m_line_height->to_string(), m_font_families->to_string());
-    }
+    virtual String to_string() const override;
 
 private:
     FontStyleValue(NonnullRefPtr<StyleValue> font_style, NonnullRefPtr<StyleValue> font_weight, NonnullRefPtr<StyleValue> font_size, NonnullRefPtr<StyleValue> line_height, NonnullRefPtr<StyleValue> font_families)
@@ -977,7 +952,7 @@ public:
     static NonnullRefPtr<ImageStyleValue> create(AK::URL const& url) { return adopt_ref(*new ImageStyleValue(url)); }
     virtual ~ImageStyleValue() override { }
 
-    String to_string() const override { return String::formatted("Image({})", m_url.to_string()); }
+    virtual String to_string() const override;
 
     void load_bitmap(DOM::Document& document);
     Gfx::Bitmap const* bitmap() const { return m_bitmap; }
@@ -1083,10 +1058,7 @@ public:
     NonnullRefPtr<StyleValue> image() const { return m_image; }
     NonnullRefPtr<StyleValue> style_type() const { return m_style_type; }
 
-    virtual String to_string() const override
-    {
-        return String::formatted("ListStyle position: {}, image: {}, style_type: {}", m_position->to_string(), m_image->to_string(), m_style_type->to_string());
-    }
+    virtual String to_string() const override;
 
 private:
     ListStyleStyleValue(
@@ -1131,16 +1103,7 @@ public:
     virtual bool has_integer() const override { return m_value.has<i64>(); }
     virtual float to_integer() const override { return m_value.get<i64>(); }
 
-    String to_string() const override
-    {
-        return m_value.visit(
-            [](float value) {
-                return String::formatted("{}", value);
-            },
-            [](i64 value) {
-                return String::formatted("{}", value);
-            });
-    }
+    virtual String to_string() const override;
 
     virtual bool equals(StyleValue const& other) const override
     {
@@ -1174,10 +1137,7 @@ public:
     NonnullRefPtr<StyleValue> overflow_x() const { return m_overflow_x; }
     NonnullRefPtr<StyleValue> overflow_y() const { return m_overflow_y; }
 
-    virtual String to_string() const override
-    {
-        return String::formatted("{} {}", m_overflow_x->to_string(), m_overflow_y->to_string());
-    }
+    virtual String to_string() const override;
 
 private:
     OverflowStyleValue(NonnullRefPtr<StyleValue> overflow_x, NonnullRefPtr<StyleValue> overflow_y)
@@ -1202,10 +1162,7 @@ public:
     Percentage const& percentage() const { return m_percentage; }
     Percentage& percentage() { return m_percentage; }
 
-    virtual String to_string() const override
-    {
-        return m_percentage.to_string();
-    }
+    virtual String to_string() const override;
 
 private:
     PercentageStyleValue(Percentage&& percentage)
@@ -1283,10 +1240,7 @@ public:
     NonnullRefPtr<StyleValue> style() const { return m_style; }
     NonnullRefPtr<StyleValue> color() const { return m_color; }
 
-    virtual String to_string() const override
-    {
-        return String::formatted("TextDecoration line: {}, style: {}, color: {}", m_line->to_string(), m_style->to_string(), m_color->to_string());
-    }
+    virtual String to_string() const override;
 
 private:
     TextDecorationStyleValue(
@@ -1316,10 +1270,7 @@ public:
     CSS::TransformFunction transform_function() const { return m_transform_function; }
     NonnullRefPtrVector<StyleValue> values() const { return m_values; }
 
-    virtual String to_string() const override
-    {
-        return String::formatted("TransformationStyleValue");
-    }
+    virtual String to_string() const override;
 
 private:
     TransformationStyleValue(CSS::TransformFunction transform_function, NonnullRefPtrVector<StyleValue>&& values)
@@ -1389,18 +1340,7 @@ public:
         return m_values[i];
     }
 
-    virtual String to_string() const override
-    {
-        StringBuilder builder;
-        builder.appendff("List[{}](", m_values.size());
-        for (size_t i = 0; i < m_values.size(); ++i) {
-            if (i)
-                builder.append(',');
-            builder.append(m_values[i].to_string());
-        }
-        builder.append(')');
-        return builder.to_string();
-    }
+    virtual String to_string() const override;
 
 private:
     StyleValueList(NonnullRefPtrVector<StyleValue>&& values)