Skip to content

Commit a99d02e

Browse files
AtkinsSJlinusg
authored andcommitted
LibWeb: Add an enum for !important
1 parent c08a52d commit a99d02e

File tree

8 files changed

+21
-15
lines changed

8 files changed

+21
-15
lines changed

Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ bool PropertyOwningCSSStyleDeclaration::set_property(PropertyID property_id, Str
8787
}
8888

8989
m_properties.append(CSS::StyleProperty {
90-
.important = false,
90+
.important = Important::No,
9191
.property_id = property_id,
9292
.value = new_value.release_nonnull(),
9393
});
@@ -125,7 +125,7 @@ void CSSStyleDeclaration::set_css_text(StringView)
125125
}
126126

127127
// https://www.w3.org/TR/cssom/#serialize-a-css-declaration
128-
static String serialize_a_css_declaration(CSS::PropertyID property, String value, bool important)
128+
static String serialize_a_css_declaration(CSS::PropertyID property, String value, Important important)
129129
{
130130
StringBuilder builder;
131131

@@ -140,7 +140,7 @@ static String serialize_a_css_declaration(CSS::PropertyID property, String value
140140
builder.append(value);
141141

142142
// 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.
143-
if (important)
143+
if (important == Important::Yes)
144144
builder.append(" !important"sv);
145145

146146
// 6. Append ";" (U+003B) to s.

Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313

1414
namespace Web::CSS {
1515

16+
enum class Important {
17+
No,
18+
Yes,
19+
};
20+
1621
struct StyleProperty {
17-
bool important { false };
22+
Important important { Important::No };
1823
CSS::PropertyID property_id;
1924
NonnullRefPtr<StyleValue> value;
2025
String custom_name {};

Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,7 @@ Optional<StyleDeclarationRule> Parser::consume_a_declaration(TokenStream<T>& tok
16111611
if (bang_index.has_value()) {
16121612
declaration.m_values.remove(important_index.value());
16131613
declaration.m_values.remove(bang_index.value());
1614-
declaration.m_important = true;
1614+
declaration.m_important = Important::Yes;
16151615
}
16161616
}
16171617
}

Userland/Libraries/LibWeb/CSS/Parser/StyleDeclarationRule.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <AK/String.h>
1010
#include <AK/Vector.h>
11+
#include <LibWeb/CSS/CSSStyleDeclaration.h>
1112
#include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
1213

1314
namespace Web::CSS {
@@ -24,7 +25,7 @@ class StyleDeclarationRule {
2425
private:
2526
String m_name;
2627
Vector<StyleComponentValueRule> m_values;
27-
bool m_important { false };
28+
Important m_important { Important::No };
2829
};
2930

3031
}

Userland/Libraries/LibWeb/CSS/Parser/StyleRules.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ String StyleDeclarationRule::to_string() const
171171
builder.append(": ");
172172
append_with_to_string(builder, " ", m_values);
173173

174-
if (m_important)
174+
if (m_important == Important::Yes)
175175
builder.append(" !important");
176176

177177
return builder.to_string();

Userland/Libraries/LibWeb/CSS/StyleComputer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ RefPtr<StyleValue> StyleComputer::resolve_unresolved_style_value(DOM::Element& e
543543
return {};
544544
}
545545

546-
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
546+
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
547547
{
548548
for (auto const& match : matching_rules) {
549549
for (auto const& property : verify_cast<PropertyOwningCSSStyleDeclaration>(match.rule->declaration()).properties()) {
@@ -607,25 +607,25 @@ void StyleComputer::compute_cascaded_values(StyleProperties& style, DOM::Element
607607
// Then we apply the declarations from the matched rules in cascade order:
608608

609609
// Normal user agent declarations
610-
cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, false, custom_properties);
610+
cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, Important::No, custom_properties);
611611

612612
// FIXME: Normal user declarations
613613

614614
// Normal author declarations
615-
cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, false, custom_properties);
615+
cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, Important::No, custom_properties);
616616

617617
// Author presentational hints (NOTE: The spec doesn't say exactly how to prioritize these.)
618618
element.apply_presentational_hints(style);
619619

620620
// FIXME: Animation declarations [css-animations-1]
621621

622622
// Important author declarations
623-
cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, true, custom_properties);
623+
cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, Important::Yes, custom_properties);
624624

625625
// FIXME: Important user declarations
626626

627627
// Important user agent declarations
628-
cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, true, custom_properties);
628+
cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, Important::Yes, custom_properties);
629629

630630
// FIXME: Transition declarations [css-transitions-1]
631631
}

Userland/Libraries/LibWeb/CSS/StyleComputer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class StyleComputer {
8787
Vector<MatchingRule> author_rules;
8888
};
8989

90-
void cascade_declarations(StyleProperties&, DOM::Element&, Vector<MatchingRule> const&, CascadeOrigin, bool important, HashMap<String, StyleProperty const*> const&) const;
90+
void cascade_declarations(StyleProperties&, DOM::Element&, Vector<MatchingRule> const&, CascadeOrigin, Important important, HashMap<String, StyleProperty const*> const&) const;
9191

9292
void build_rule_cache();
9393
void build_rule_cache_if_needed() const;

Userland/Libraries/LibWeb/Dump.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,14 +562,14 @@ void dump_style_rule(StringBuilder& builder, CSS::CSSStyleRule const& rule, int
562562
for (auto& property : style_declaration.properties()) {
563563
indent(builder, indent_levels);
564564
builder.appendff(" {}: '{}'", CSS::string_from_property_id(property.property_id), property.value->to_string());
565-
if (property.important)
565+
if (property.important == CSS::Important::Yes)
566566
builder.append(" \033[31;1m!important\033[0m");
567567
builder.append('\n');
568568
}
569569
for (auto& property : style_declaration.custom_properties()) {
570570
indent(builder, indent_levels);
571571
builder.appendff(" {}: '{}'", property.key, property.value.value->to_string());
572-
if (property.value.important)
572+
if (property.value.important == CSS::Important::Yes)
573573
builder.append(" \033[31;1m!important\033[0m");
574574
builder.append('\n');
575575
}

0 commit comments

Comments
 (0)