瀏覽代碼

LibWeb: Allow doing .to_color() on a StyleValue without a layout node

This will be needed to access the color of a stop from a SVG gradient
<stop> element (which does not participate in layout, so does not have
a layout node).
MacDue 2 年之前
父節點
當前提交
f099ee3d47

+ 1 - 1
Userland/Libraries/LibWeb/CSS/StyleValue.h

@@ -285,7 +285,7 @@ public:
 
     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;
 
-    virtual Color to_color(Layout::NodeWithStyle const&) const { return {}; }
+    virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const { return {}; }
     ValueID to_identifier() const;
     virtual Length to_length() const { VERIFY_NOT_REACHED(); }
     virtual float to_number() const { return 0; }

+ 1 - 1
Userland/Libraries/LibWeb/CSS/StyleValues/ColorStyleValue.h

@@ -22,7 +22,7 @@ public:
     Color color() const { return m_color; }
     virtual ErrorOr<String> to_string() const override;
     virtual bool has_color() const override { return true; }
-    virtual Color to_color(Layout::NodeWithStyle const&) const override { return m_color; }
+    virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const override { return m_color; }
 
     bool properties_equal(ColorStyleValue const& other) const { return m_color == other.m_color; };
 

+ 9 - 4
Userland/Libraries/LibWeb/CSS/StyleValues/IdentifierStyleValue.cpp

@@ -84,15 +84,20 @@ bool IdentifierStyleValue::has_color() const
     }
 }
 
-Color IdentifierStyleValue::to_color(Layout::NodeWithStyle const& node) const
+Color IdentifierStyleValue::to_color(Optional<Layout::NodeWithStyle const&> node) const
 {
     if (id() == CSS::ValueID::Currentcolor) {
-        if (!node.has_style())
+        if (!node.has_value() || !node->has_style())
             return Color::Black;
-        return node.computed_values().color();
+        return node->computed_values().color();
     }
 
-    auto& document = node.document();
+    if (!node.has_value()) {
+        // FIXME: Can't resolve palette colors without layout node.
+        return Color::Black;
+    }
+
+    auto& document = node->document();
     if (id() == CSS::ValueID::LibwebLink)
         return document.link_color();
 

+ 1 - 1
Userland/Libraries/LibWeb/CSS/StyleValues/IdentifierStyleValue.h

@@ -25,7 +25,7 @@ public:
     ValueID id() const { return m_id; }
 
     virtual bool has_color() const override;
-    virtual Color to_color(Layout::NodeWithStyle const& node) const override;
+    virtual Color to_color(Optional<Layout::NodeWithStyle const&> node) const override;
     virtual ErrorOr<String> to_string() const override;
 
     bool properties_equal(IdentifierStyleValue const& other) const { return m_id == other.m_id; }