Bläddra i källkod

LibWeb: Cache and reuse some very common StyleValue objects

Length values: auto, 0px, 1px
Color values: black, white, transparent
Andreas Kling 3 år sedan
förälder
incheckning
bfe69e9d0d

+ 1 - 0
Userland/Libraries/LibWeb/CSS/Length.h

@@ -48,6 +48,7 @@ public:
 
     bool is_auto() const { return m_type == Type::Auto; }
     bool is_calculated() const { return m_type == Type::Calculated; }
+    bool is_px() const { return m_type == Type::Px; }
 
     bool is_absolute() const
     {

+ 39 - 0
Userland/Libraries/LibWeb/CSS/StyleValue.cpp

@@ -1193,4 +1193,43 @@ String StyleValueList::to_string() const
     return String::join(separator, m_values);
 }
 
+NonnullRefPtr<ColorStyleValue> ColorStyleValue::create(Color color)
+{
+    if (color.value() == 0) {
+        static auto transparent = adopt_ref(*new ColorStyleValue(color));
+        return transparent;
+    }
+
+    if (color == Color::from_rgb(0x000000)) {
+        static auto black = adopt_ref(*new ColorStyleValue(color));
+        return black;
+    }
+
+    if (color == Color::from_rgb(0xffffff)) {
+        static auto white = adopt_ref(*new ColorStyleValue(color));
+        return white;
+    }
+
+    return adopt_ref(*new ColorStyleValue(color));
+}
+
+NonnullRefPtr<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));
+}
+
 }

+ 2 - 8
Userland/Libraries/LibWeb/CSS/StyleValue.h

@@ -856,10 +856,7 @@ private:
 
 class ColorStyleValue : public StyleValue {
 public:
-    static NonnullRefPtr<ColorStyleValue> create(Color color)
-    {
-        return adopt_ref(*new ColorStyleValue(color));
-    }
+    static NonnullRefPtr<ColorStyleValue> create(Color color);
     virtual ~ColorStyleValue() override { }
 
     Color color() const { return m_color; }
@@ -1101,10 +1098,7 @@ private:
 
 class LengthStyleValue : public StyleValue {
 public:
-    static NonnullRefPtr<LengthStyleValue> create(Length const& length)
-    {
-        return adopt_ref(*new LengthStyleValue(length));
-    }
+    static NonnullRefPtr<LengthStyleValue> create(Length const&);
     virtual ~LengthStyleValue() override { }
 
     Length const& length() const { return m_length; }