1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 20:27:35 +00:00

LibWeb: Add an enum for !important

This commit is contained in:
Sam Atkins 2022-02-12 14:53:59 +00:00 committed by Linus Groh
parent c08a52dd97
commit a99d02e14d
8 changed files with 21 additions and 15 deletions

View file

@ -87,7 +87,7 @@ bool PropertyOwningCSSStyleDeclaration::set_property(PropertyID property_id, Str
} }
m_properties.append(CSS::StyleProperty { m_properties.append(CSS::StyleProperty {
.important = false, .important = Important::No,
.property_id = property_id, .property_id = property_id,
.value = new_value.release_nonnull(), .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 // 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; StringBuilder builder;
@ -140,7 +140,7 @@ static String serialize_a_css_declaration(CSS::PropertyID property, String value
builder.append(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. // 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); builder.append(" !important"sv);
// 6. Append ";" (U+003B) to s. // 6. Append ";" (U+003B) to s.

View file

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

View file

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

View file

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

View file

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

View file

@ -543,7 +543,7 @@ RefPtr<StyleValue> StyleComputer::resolve_unresolved_style_value(DOM::Element& e
return {}; 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& match : matching_rules) {
for (auto const& property : verify_cast<PropertyOwningCSSStyleDeclaration>(match.rule->declaration()).properties()) { 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: // Then we apply the declarations from the matched rules in cascade order:
// Normal user agent declarations // 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 // FIXME: Normal user declarations
// Normal author 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.) // Author presentational hints (NOTE: The spec doesn't say exactly how to prioritize these.)
element.apply_presentational_hints(style); element.apply_presentational_hints(style);
@ -620,12 +620,12 @@ void StyleComputer::compute_cascaded_values(StyleProperties& style, DOM::Element
// FIXME: Animation declarations [css-animations-1] // FIXME: Animation declarations [css-animations-1]
// Important author declarations // 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 // FIXME: Important user declarations
// Important user agent 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] // FIXME: Transition declarations [css-transitions-1]
} }

View file

@ -87,7 +87,7 @@ private:
Vector<MatchingRule> author_rules; 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();
void build_rule_cache_if_needed() const; void build_rule_cache_if_needed() const;

View file

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