Bladeren bron

LibWeb: Serialize StringStyleValue with quotes

In order to access the string's contents, use the new
`StringStyleValue::string_value()` method.

I think I found all the existing places that relied on
`StringStyleValue::to_string()` returning an unquoted string, but it's
hard to know for sure until things break.
Sam Atkins 1 jaar geleden
bovenliggende
commit
ff02de4ad0

+ 1 - 0
Tests/LibWeb/Text/expected/css/string-serializes-with-quotes.txt

@@ -0,0 +1 @@
+"wheeee"

+ 8 - 0
Tests/LibWeb/Text/input/css/string-serializes-with-quotes.html

@@ -0,0 +1,8 @@
+<script src="../include.js"></script>
+<script>
+    test(() => {
+        const e = document.createElement("div");
+        e.style.content = '"wheeee"';
+        println(e.style.content);
+    });
+</script>

+ 14 - 5
Userland/Libraries/LibWeb/CSS/StyleComputer.cpp

@@ -60,6 +60,7 @@
 #include <LibWeb/CSS/StyleValues/PlaceSelfStyleValue.h>
 #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
 #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
+#include <LibWeb/CSS/StyleValues/StringStyleValue.h>
 #include <LibWeb/CSS/StyleValues/StyleValueList.h>
 #include <LibWeb/CSS/StyleValues/TextDecorationStyleValue.h>
 #include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
@@ -1543,7 +1544,15 @@ ErrorOr<void> StyleComputer::compute_cascaded_values(StyleProperties& style, DOM
     cascade_declarations(style, element, pseudo_element, matching_rule_set.author_rules, CascadeOrigin::Author, Important::No);
 
     // Animation declarations [css-animations-2]
-    if (auto animation_name = style.maybe_null_property(PropertyID::AnimationName)) {
+    auto get_animation_name = [&]() -> Optional<String> {
+        auto animation_name = style.maybe_null_property(PropertyID::AnimationName);
+        if (animation_name.is_null())
+            return OptionalNone {};
+        if (animation_name->is_string())
+            return animation_name->as_string().string_value();
+        return animation_name->to_string();
+    };
+    if (auto animation_name = get_animation_name(); animation_name.has_value()) {
         if (auto source_declaration = style.property_source_declaration(PropertyID::AnimationName)) {
             AnimationKey animation_key {
                 .source_declaration = source_declaration,
@@ -1561,7 +1570,7 @@ ErrorOr<void> StyleComputer::compute_cascaded_values(StyleProperties& style, DOM
                             style.set_property(static_cast<PropertyID>(property_id_value), *property_value);
                     }
                 }
-            } else if (auto name = animation_name->to_string(); !name.is_empty()) {
+            } else if (!animation_name->is_empty()) {
                 auto active_animation = m_active_animations.get(animation_key);
                 if (!active_animation.has_value()) {
                     // New animation!
@@ -1682,7 +1691,7 @@ ErrorOr<void> StyleComputer::compute_cascaded_values(StyleProperties& style, DOM
                     }
 
                     auto animation = make<Animation>(Animation {
-                        .name = move(name),
+                        .name = animation_name.release_value(),
                         .duration = duration,
                         .delay = delay,
                         .iteration_count = iteration_count,
@@ -2127,7 +2136,7 @@ RefPtr<Gfx::Font const> StyleComputer::compute_font_for_style_values(DOM::Elemen
             if (family->is_identifier()) {
                 found_font = find_generic_font(family->to_identifier());
             } else if (family->is_string()) {
-                found_font = find_font(family->to_string());
+                found_font = find_font(family->as_string().string_value());
             }
             if (found_font)
                 break;
@@ -2135,7 +2144,7 @@ RefPtr<Gfx::Font const> StyleComputer::compute_font_for_style_values(DOM::Elemen
     } else if (font_family.is_identifier()) {
         found_font = find_generic_font(font_family.to_identifier());
     } else if (font_family.is_string()) {
-        found_font = find_font(font_family.to_string());
+        found_font = find_font(font_family.as_string().string_value());
     }
 
     if (!found_font) {

+ 3 - 3
Userland/Libraries/LibWeb/CSS/StyleProperties.cpp

@@ -660,7 +660,7 @@ CSS::ContentData StyleProperties::content() const
         StringBuilder builder;
         for (auto const& item : content_style_value.content().values()) {
             if (item->is_string()) {
-                builder.append(item->to_string());
+                builder.append(item->as_string().string_value());
             } else {
                 // TODO: Implement quotes, counters, images, and other things.
             }
@@ -672,7 +672,7 @@ CSS::ContentData StyleProperties::content() const
             StringBuilder alt_text_builder;
             for (auto const& item : content_style_value.alt_text()->values()) {
                 if (item->is_string()) {
-                    alt_text_builder.append(item->to_string());
+                    alt_text_builder.append(item->as_string().string_value());
                 } else {
                     // TODO: Implement counters
                 }
@@ -951,7 +951,7 @@ Vector<Vector<String>> StyleProperties::grid_template_areas() const
 String StyleProperties::grid_area() const
 {
     auto value = property(CSS::PropertyID::GridArea);
-    return value->as_string().to_string();
+    return value->as_string().string_value();
 }
 
 Optional<CSS::ObjectFit> StyleProperties::object_fit() const

+ 2 - 1
Userland/Libraries/LibWeb/CSS/StyleValues/StringStyleValue.h

@@ -19,7 +19,8 @@ public:
     }
     virtual ~StringStyleValue() override = default;
 
-    String to_string() const override { return m_string; }
+    String string_value() const { return m_string; }
+    String to_string() const override { return serialize_a_string(m_string); }
 
     bool properties_equal(StringStyleValue const& other) const { return m_string == other.m_string; }