srctree

Robin Linden parent bb21e048 beef3be6
style: Make the white-space property strongly typed

inlinesplit
style/styled_node.cpp added: 59, removed: 3, total 56
@@ -99,6 +99,9 @@ std::map<css::PropertyId, std::string_view> const initial_values{
{css::PropertyId::Width, "auto"sv},
{css::PropertyId::MaxWidth, "none"sv},
{css::PropertyId::MinWidth, "auto"sv},
 
// https://developer.mozilla.org/en-US/docs/Web/CSS/white-space
{css::PropertyId::WhiteSpace, "normal"sv},
};
 
std::optional<gfx::Color> try_from_hex_chars(std::string_view hex_chars) {
@@ -481,4 +484,34 @@ int StyledNode::get_font_size_property() const {
return 0;
}
 
std::optional<WhiteSpace> StyledNode::get_white_space_property() const {
auto raw = get_raw_property(css::PropertyId::WhiteSpace);
if (raw == "normal") {
return WhiteSpace::Normal;
}
 
if (raw == "pre") {
return WhiteSpace::Pre;
}
 
if (raw == "nowrap") {
return WhiteSpace::Nowrap;
}
 
if (raw == "pre-wrap") {
return WhiteSpace::PreWrap;
}
 
if (raw == "break-spaces") {
return WhiteSpace::BreakSpaces;
}
 
if (raw == "pre-line") {
return WhiteSpace::PreLine;
}
 
spdlog::warn("Unhandled white-space '{}'", raw);
return std::nullopt;
}
 
} // namespace style
 
style/styled_node.h added: 59, removed: 3, total 56
@@ -52,6 +52,15 @@ enum class TextDecorationLine {
Blink,
};
 
enum class WhiteSpace {
Normal,
Pre,
Nowrap,
PreWrap,
BreakSpaces,
PreLine,
};
 
struct StyledNode {
dom::Node const &node;
std::vector<std::pair<css::PropertyId, std::string>> properties;
@@ -89,6 +98,8 @@ struct StyledNode {
return get_font_style_property();
} else if constexpr (T == css::PropertyId::TextDecorationLine) {
return get_text_decoration_line_property();
} else if constexpr (T == css::PropertyId::WhiteSpace) {
return get_white_space_property();
} else {
return get_raw_property(T);
}
@@ -101,6 +112,7 @@ private:
FontStyle get_font_style_property() const;
int get_font_size_property() const;
std::vector<TextDecorationLine> get_text_decoration_line_property() const;
std::optional<WhiteSpace> get_white_space_property() const;
};
 
[[nodiscard]] inline bool operator==(style::StyledNode const &a, style::StyledNode const &b) noexcept {
 
style/styled_node_test.cpp added: 59, removed: 3, total 56
@@ -298,6 +298,17 @@ int main() {
expect_property_eq<css::PropertyId::TextDecorationLine>("unhandled!", std::vector<style::TextDecorationLine>{});
});
 
etest::test("get_property, white-space", [] {
expect_property_eq<css::PropertyId::WhiteSpace>("normal", style::WhiteSpace::Normal);
expect_property_eq<css::PropertyId::WhiteSpace>("pre", style::WhiteSpace::Pre);
expect_property_eq<css::PropertyId::WhiteSpace>("nowrap", style::WhiteSpace::Nowrap);
expect_property_eq<css::PropertyId::WhiteSpace>("pre-wrap", style::WhiteSpace::PreWrap);
expect_property_eq<css::PropertyId::WhiteSpace>("break-spaces", style::WhiteSpace::BreakSpaces);
expect_property_eq<css::PropertyId::WhiteSpace>("pre-line", style::WhiteSpace::PreLine);
 
expect_property_eq<css::PropertyId::WhiteSpace>("unhandled!", std::nullopt);
});
 
etest::test("get_property, non-inherited property for a text node", [] {
dom::Node dom = dom::Element{"hello"};
dom::Node text = dom::Text{"world"};