diff options
author | Andreas Kling <kling@serenityos.org> | 2020-12-14 18:38:02 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-12-14 20:43:25 +0100 |
commit | 3247ea358124fd3cdaef17933adf1d0f3bf9927e (patch) | |
tree | 6db25c627ed8cd85fbc594b51ef5bcaba83f0d4f | |
parent | 08517daa5ab0093538e913205328786966e440bd (diff) | |
download | serenity-3247ea358124fd3cdaef17933adf1d0f3bf9927e.zip |
LibWeb: Use CSS::ValueID for 'text-align' values
Let's start moving away from using raw strings for CSS identifiers.
The idea here is to use IdentifierStyleValue with a CSS::ValueID inside
for all CSS identifier values.
-rw-r--r-- | Libraries/LibWeb/CSS/Parser/CSSParser.cpp | 10 | ||||
-rw-r--r-- | Libraries/LibWeb/CSS/StyleProperties.cpp | 23 | ||||
-rw-r--r-- | Libraries/LibWeb/CSS/StyleProperties.h | 2 | ||||
-rw-r--r-- | Libraries/LibWeb/Layout/LayoutStyle.h | 3 | ||||
-rw-r--r-- | Libraries/LibWeb/Layout/Node.cpp | 5 |
5 files changed, 32 insertions, 11 deletions
diff --git a/Libraries/LibWeb/CSS/Parser/CSSParser.cpp b/Libraries/LibWeb/CSS/Parser/CSSParser.cpp index b633974ecd..e2e4f9a158 100644 --- a/Libraries/LibWeb/CSS/Parser/CSSParser.cpp +++ b/Libraries/LibWeb/CSS/Parser/CSSParser.cpp @@ -350,10 +350,16 @@ static Optional<CSS::ValueID> value_id_from_string(const String& string) return CSS::ValueID::Bold; if (string.equals_ignoring_case("bolder")) return CSS::ValueID::Bolder; + if (string.equals_ignoring_case("center")) + return CSS::ValueID::Center; + if (string.equals_ignoring_case("justify")) + return CSS::ValueID::Justify; if (string.equals_ignoring_case("large")) return CSS::ValueID::Large; if (string.equals_ignoring_case("larger")) return CSS::ValueID::Larger; + if (string.equals_ignoring_case("left")) + return CSS::ValueID::Left; if (string.equals_ignoring_case("lighter")) return CSS::ValueID::Lighter; if (string.equals_ignoring_case("medium")) @@ -362,6 +368,8 @@ static Optional<CSS::ValueID> value_id_from_string(const String& string) return CSS::ValueID::Normal; if (string.equals_ignoring_case("small")) return CSS::ValueID::Small; + if (string.equals_ignoring_case("right")) + return CSS::ValueID::Right; if (string.equals_ignoring_case("smaller")) return CSS::ValueID::Smaller; if (string.equals_ignoring_case("x-large")) @@ -374,6 +382,8 @@ static Optional<CSS::ValueID> value_id_from_string(const String& string) return CSS::ValueID::XxSmall; if (string.equals_ignoring_case("xxx-large")) return CSS::ValueID::XxxLarge; + if (string.equals_ignoring_case("-libweb-center")) + return CSS::ValueID::VendorSpecificCenter; if (string.equals_ignoring_case("-libweb-link")) return CSS::ValueID::VendorSpecificLink; if (string.starts_with("-libweb-palette-", CaseSensitivity::CaseInsensitive)) diff --git a/Libraries/LibWeb/CSS/StyleProperties.cpp b/Libraries/LibWeb/CSS/StyleProperties.cpp index a9f1b94149..0ce6f5e119 100644 --- a/Libraries/LibWeb/CSS/StyleProperties.cpp +++ b/Libraries/LibWeb/CSS/StyleProperties.cpp @@ -261,19 +261,26 @@ bool StyleProperties::operator==(const StyleProperties& other) const return true; } -CSS::TextAlign StyleProperties::text_align() const +Optional<CSS::TextAlign> StyleProperties::text_align() const { - auto string = string_or_fallback(CSS::PropertyID::TextAlign, "left"); - if (string == "center") + auto value = property(CSS::PropertyID::TextAlign); + if (!value.has_value() || !value.value()->is_identifier()) + return {}; + + switch (static_cast<const IdentifierStyleValue&>(*value.value()).id()) { + case CSS::ValueID::Left: + return CSS::TextAlign::Left; + case CSS::ValueID::Center: return CSS::TextAlign::Center; - if (string == "right") + case CSS::ValueID::Right: return CSS::TextAlign::Right; - if (string == "justify") + case CSS::ValueID::Justify: return CSS::TextAlign::Justify; - if (string == "-libweb-center") + case CSS::ValueID::VendorSpecificCenter: return CSS::TextAlign::VendorSpecificCenter; - // Otherwise, just assume "left".. - return CSS::TextAlign::Left; + default: + return {}; + } } Optional<CSS::WhiteSpace> StyleProperties::white_space() const diff --git a/Libraries/LibWeb/CSS/StyleProperties.h b/Libraries/LibWeb/CSS/StyleProperties.h index f8fd7cc2c5..282098986e 100644 --- a/Libraries/LibWeb/CSS/StyleProperties.h +++ b/Libraries/LibWeb/CSS/StyleProperties.h @@ -60,7 +60,7 @@ public: LengthBox length_box(CSS::PropertyID left_id, CSS::PropertyID top_id, CSS::PropertyID right_id, CSS::PropertyID bottom_id) const; String string_or_fallback(CSS::PropertyID, const StringView& fallback) const; Color color_or_fallback(CSS::PropertyID, const DOM::Document&, Color fallback) const; - CSS::TextAlign text_align() const; + Optional<CSS::TextAlign> text_align() const; CSS::Display display() const; Optional<CSS::Float> float_() const; Optional<CSS::Clear> clear() const; diff --git a/Libraries/LibWeb/Layout/LayoutStyle.h b/Libraries/LibWeb/Layout/LayoutStyle.h index 1dcf796889..7073f9a8cc 100644 --- a/Libraries/LibWeb/Layout/LayoutStyle.h +++ b/Libraries/LibWeb/Layout/LayoutStyle.h @@ -37,6 +37,7 @@ public: static CSS::Float float_() { return CSS::Float::None; } static CSS::Clear clear() { return CSS::Clear::None; } static CSS::WhiteSpace white_space() { return CSS::WhiteSpace::Normal; } + static CSS::TextAlign text_align() { return CSS::TextAlign::Left; } }; struct BorderData { @@ -74,7 +75,7 @@ protected: CSS::Float m_float { InitialValues::float_() }; CSS::Clear m_clear { InitialValues::clear() }; Optional<int> m_z_index; - CSS::TextAlign m_text_align; + CSS::TextAlign m_text_align { InitialValues::text_align() }; CSS::Position m_position; CSS::WhiteSpace m_white_space { InitialValues::white_space() }; CSS::Length m_width; diff --git a/Libraries/LibWeb/Layout/Node.cpp b/Libraries/LibWeb/Layout/Node.cpp index b1147bd5c6..afb600bfca 100644 --- a/Libraries/LibWeb/Layout/Node.cpp +++ b/Libraries/LibWeb/Layout/Node.cpp @@ -220,7 +220,10 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style) auto& style = static_cast<MutableLayoutStyle&>(m_style); style.set_position(specified_style.position()); - style.set_text_align(specified_style.text_align()); + + auto text_align = specified_style.text_align(); + if (text_align.has_value()) + style.set_text_align(text_align.value()); auto white_space = specified_style.white_space(); if (white_space.has_value()) |