1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:58:11 +00:00

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.
This commit is contained in:
Andreas Kling 2020-12-14 18:38:02 +01:00
parent 08517daa5a
commit 3247ea3581
5 changed files with 32 additions and 11 deletions

View file

@ -350,10 +350,16 @@ static Optional<CSS::ValueID> value_id_from_string(const String& string)
return CSS::ValueID::Bold; return CSS::ValueID::Bold;
if (string.equals_ignoring_case("bolder")) if (string.equals_ignoring_case("bolder"))
return CSS::ValueID::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")) if (string.equals_ignoring_case("large"))
return CSS::ValueID::Large; return CSS::ValueID::Large;
if (string.equals_ignoring_case("larger")) if (string.equals_ignoring_case("larger"))
return CSS::ValueID::Larger; return CSS::ValueID::Larger;
if (string.equals_ignoring_case("left"))
return CSS::ValueID::Left;
if (string.equals_ignoring_case("lighter")) if (string.equals_ignoring_case("lighter"))
return CSS::ValueID::Lighter; return CSS::ValueID::Lighter;
if (string.equals_ignoring_case("medium")) 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; return CSS::ValueID::Normal;
if (string.equals_ignoring_case("small")) if (string.equals_ignoring_case("small"))
return CSS::ValueID::Small; return CSS::ValueID::Small;
if (string.equals_ignoring_case("right"))
return CSS::ValueID::Right;
if (string.equals_ignoring_case("smaller")) if (string.equals_ignoring_case("smaller"))
return CSS::ValueID::Smaller; return CSS::ValueID::Smaller;
if (string.equals_ignoring_case("x-large")) 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; return CSS::ValueID::XxSmall;
if (string.equals_ignoring_case("xxx-large")) if (string.equals_ignoring_case("xxx-large"))
return CSS::ValueID::XxxLarge; return CSS::ValueID::XxxLarge;
if (string.equals_ignoring_case("-libweb-center"))
return CSS::ValueID::VendorSpecificCenter;
if (string.equals_ignoring_case("-libweb-link")) if (string.equals_ignoring_case("-libweb-link"))
return CSS::ValueID::VendorSpecificLink; return CSS::ValueID::VendorSpecificLink;
if (string.starts_with("-libweb-palette-", CaseSensitivity::CaseInsensitive)) if (string.starts_with("-libweb-palette-", CaseSensitivity::CaseInsensitive))

View file

@ -261,19 +261,26 @@ bool StyleProperties::operator==(const StyleProperties& other) const
return true; return true;
} }
CSS::TextAlign StyleProperties::text_align() const Optional<CSS::TextAlign> StyleProperties::text_align() const
{ {
auto string = string_or_fallback(CSS::PropertyID::TextAlign, "left"); auto value = property(CSS::PropertyID::TextAlign);
if (string == "center") 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; return CSS::TextAlign::Center;
if (string == "right") case CSS::ValueID::Right:
return CSS::TextAlign::Right; return CSS::TextAlign::Right;
if (string == "justify") case CSS::ValueID::Justify:
return CSS::TextAlign::Justify; return CSS::TextAlign::Justify;
if (string == "-libweb-center") case CSS::ValueID::VendorSpecificCenter:
return CSS::TextAlign::VendorSpecificCenter; return CSS::TextAlign::VendorSpecificCenter;
// Otherwise, just assume "left".. default:
return CSS::TextAlign::Left; return {};
}
} }
Optional<CSS::WhiteSpace> StyleProperties::white_space() const Optional<CSS::WhiteSpace> StyleProperties::white_space() const

View file

@ -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; 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; String string_or_fallback(CSS::PropertyID, const StringView& fallback) const;
Color color_or_fallback(CSS::PropertyID, const DOM::Document&, Color 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; CSS::Display display() const;
Optional<CSS::Float> float_() const; Optional<CSS::Float> float_() const;
Optional<CSS::Clear> clear() const; Optional<CSS::Clear> clear() const;

View file

@ -37,6 +37,7 @@ public:
static CSS::Float float_() { return CSS::Float::None; } static CSS::Float float_() { return CSS::Float::None; }
static CSS::Clear clear() { return CSS::Clear::None; } static CSS::Clear clear() { return CSS::Clear::None; }
static CSS::WhiteSpace white_space() { return CSS::WhiteSpace::Normal; } static CSS::WhiteSpace white_space() { return CSS::WhiteSpace::Normal; }
static CSS::TextAlign text_align() { return CSS::TextAlign::Left; }
}; };
struct BorderData { struct BorderData {
@ -74,7 +75,7 @@ protected:
CSS::Float m_float { InitialValues::float_() }; CSS::Float m_float { InitialValues::float_() };
CSS::Clear m_clear { InitialValues::clear() }; CSS::Clear m_clear { InitialValues::clear() };
Optional<int> m_z_index; Optional<int> m_z_index;
CSS::TextAlign m_text_align; CSS::TextAlign m_text_align { InitialValues::text_align() };
CSS::Position m_position; CSS::Position m_position;
CSS::WhiteSpace m_white_space { InitialValues::white_space() }; CSS::WhiteSpace m_white_space { InitialValues::white_space() };
CSS::Length m_width; CSS::Length m_width;

View file

@ -220,7 +220,10 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
auto& style = static_cast<MutableLayoutStyle&>(m_style); auto& style = static_cast<MutableLayoutStyle&>(m_style);
style.set_position(specified_style.position()); 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(); auto white_space = specified_style.white_space();
if (white_space.has_value()) if (white_space.has_value())