Parcourir la source

LibWeb: Add an enum for !important

Sam Atkins il y a 3 ans
Parent
commit
a99d02e14d

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

@@ -87,7 +87,7 @@ bool PropertyOwningCSSStyleDeclaration::set_property(PropertyID property_id, Str
     }
 
     m_properties.append(CSS::StyleProperty {
-        .important = false,
+        .important = Important::No,
         .property_id = property_id,
         .value = new_value.release_nonnull(),
     });
@@ -125,7 +125,7 @@ void CSSStyleDeclaration::set_css_text(StringView)
 }
 
 // https://www.w3.org/TR/cssom/#serialize-a-css-declaration
-static String serialize_a_css_declaration(CSS::PropertyID property, String value, bool important)
+static String serialize_a_css_declaration(CSS::PropertyID property, String value, Important important)
 {
     StringBuilder builder;
 
@@ -140,7 +140,7 @@ static String serialize_a_css_declaration(CSS::PropertyID property, String value
     builder.append(value);
 
     // 5. If the important flag is set, append " !important" (U+0020 U+0021 U+0069 U+006D U+0070 U+006F U+0072 U+0074 U+0061 U+006E U+0074) to s.
-    if (important)
+    if (important == Important::Yes)
         builder.append(" !important"sv);
 
     // 6. Append ";" (U+003B) to s.

+ 6 - 1
Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h

@@ -13,8 +13,13 @@
 
 namespace Web::CSS {
 
+enum class Important {
+    No,
+    Yes,
+};
+
 struct StyleProperty {
-    bool important { false };
+    Important important { Important::No };
     CSS::PropertyID property_id;
     NonnullRefPtr<StyleValue> value;
     String custom_name {};

+ 1 - 1
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -1611,7 +1611,7 @@ Optional<StyleDeclarationRule> Parser::consume_a_declaration(TokenStream<T>& tok
             if (bang_index.has_value()) {
                 declaration.m_values.remove(important_index.value());
                 declaration.m_values.remove(bang_index.value());
-                declaration.m_important = true;
+                declaration.m_important = Important::Yes;
             }
         }
     }

+ 2 - 1
Userland/Libraries/LibWeb/CSS/Parser/StyleDeclarationRule.h

@@ -8,6 +8,7 @@
 
 #include <AK/String.h>
 #include <AK/Vector.h>
+#include <LibWeb/CSS/CSSStyleDeclaration.h>
 #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
 
 namespace Web::CSS {
@@ -24,7 +25,7 @@ public:
 private:
     String m_name;
     Vector<StyleComponentValueRule> m_values;
-    bool m_important { false };
+    Important m_important { Important::No };
 };
 
 }

+ 1 - 1
Userland/Libraries/LibWeb/CSS/Parser/StyleRules.cpp

@@ -171,7 +171,7 @@ String StyleDeclarationRule::to_string() const
     builder.append(": ");
     append_with_to_string(builder, " ", m_values);
 
-    if (m_important)
+    if (m_important == Important::Yes)
         builder.append(" !important");
 
     return builder.to_string();

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

@@ -543,7 +543,7 @@ RefPtr<StyleValue> StyleComputer::resolve_unresolved_style_value(DOM::Element& e
     return {};
 }
 
-void StyleComputer::cascade_declarations(StyleProperties& style, DOM::Element& element, Vector<MatchingRule> const& matching_rules, CascadeOrigin cascade_origin, bool important, HashMap<String, StyleProperty const*> const& custom_properties) const
+void StyleComputer::cascade_declarations(StyleProperties& style, DOM::Element& element, Vector<MatchingRule> const& matching_rules, CascadeOrigin cascade_origin, Important important, HashMap<String, StyleProperty const*> const& custom_properties) const
 {
     for (auto const& match : matching_rules) {
         for (auto const& property : verify_cast<PropertyOwningCSSStyleDeclaration>(match.rule->declaration()).properties()) {
@@ -607,12 +607,12 @@ void StyleComputer::compute_cascaded_values(StyleProperties& style, DOM::Element
     // Then we apply the declarations from the matched rules in cascade order:
 
     // Normal user agent declarations
-    cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, false, custom_properties);
+    cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, Important::No, custom_properties);
 
     // FIXME: Normal user declarations
 
     // Normal author declarations
-    cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, false, custom_properties);
+    cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, Important::No, custom_properties);
 
     // Author presentational hints (NOTE: The spec doesn't say exactly how to prioritize these.)
     element.apply_presentational_hints(style);
@@ -620,12 +620,12 @@ void StyleComputer::compute_cascaded_values(StyleProperties& style, DOM::Element
     // FIXME: Animation declarations [css-animations-1]
 
     // Important author declarations
-    cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, true, custom_properties);
+    cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, Important::Yes, custom_properties);
 
     // FIXME: Important user declarations
 
     // Important user agent declarations
-    cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, true, custom_properties);
+    cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, Important::Yes, custom_properties);
 
     // FIXME: Transition declarations [css-transitions-1]
 }

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

@@ -87,7 +87,7 @@ private:
         Vector<MatchingRule> author_rules;
     };
 
-    void cascade_declarations(StyleProperties&, DOM::Element&, Vector<MatchingRule> const&, CascadeOrigin, bool important, HashMap<String, StyleProperty const*> const&) const;
+    void cascade_declarations(StyleProperties&, DOM::Element&, Vector<MatchingRule> const&, CascadeOrigin, Important important, HashMap<String, StyleProperty const*> const&) const;
 
     void build_rule_cache();
     void build_rule_cache_if_needed() const;

+ 2 - 2
Userland/Libraries/LibWeb/Dump.cpp

@@ -562,14 +562,14 @@ void dump_style_rule(StringBuilder& builder, CSS::CSSStyleRule const& rule, int
     for (auto& property : style_declaration.properties()) {
         indent(builder, indent_levels);
         builder.appendff("    {}: '{}'", CSS::string_from_property_id(property.property_id), property.value->to_string());
-        if (property.important)
+        if (property.important == CSS::Important::Yes)
             builder.append(" \033[31;1m!important\033[0m");
         builder.append('\n');
     }
     for (auto& property : style_declaration.custom_properties()) {
         indent(builder, indent_levels);
         builder.appendff("    {}: '{}'", property.key, property.value.value->to_string());
-        if (property.value.important)
+        if (property.value.important == CSS::Important::Yes)
             builder.append(" \033[31;1m!important\033[0m");
         builder.append('\n');
     }