srctree

Robin Linden parent 0d9c3baa 77a85ca3
style: Return a stronger type for the display property

inlinesplit
layout/layout.cpp added: 38, removed: 8, total 30
@@ -39,11 +39,11 @@ std::optional<LayoutBox> create_tree(style::StyledNode const &node) {
auto visitor = util::Overloaded{
[&node](dom::Element const &) -> std::optional<LayoutBox> {
auto display = node.get_property<css::PropertyId::Display>();
if (display == "none") {
if (display == style::DisplayValue::None) {
return std::nullopt;
}
 
LayoutBox box{&node, display == "inline" ? LayoutType::Inline : LayoutType::Block};
LayoutBox box{&node, display == style::DisplayValue::Inline ? LayoutType::Inline : LayoutType::Block};
 
for (auto const &child : node.children) {
auto child_box = create_tree(child);
 
style/BUILD added: 38, removed: 8, total 30
@@ -15,6 +15,7 @@ cc_library(
"//css",
"//dom",
"//util:string",
"@spdlog",
],
)
 
 
style/styled_node.cpp added: 38, removed: 8, total 30
@@ -4,6 +4,8 @@
 
#include "style/styled_node.h"
 
#include <spdlog/spdlog.h>
 
#include <algorithm>
#include <string_view>
 
@@ -113,4 +115,18 @@ std::string_view StyledNode::get_raw_property(css::PropertyId property) const {
return it->second;
}
 
DisplayValue StyledNode::get_display_property(css::PropertyId id) const {
auto raw = get_raw_property(id);
if (raw == "none") {
return DisplayValue::None;
} else if (raw == "inline") {
return DisplayValue::Inline;
} else if (raw == "block") {
return DisplayValue::Block;
}
 
spdlog::warn("Unhandled display value {} for property {}", raw, static_cast<int>(id));
return DisplayValue::None;
}
 
} // namespace style
 
style/styled_node.h added: 38, removed: 8, total 30
@@ -15,6 +15,12 @@
 
namespace style {
 
enum class DisplayValue {
None,
Inline,
Block,
};
 
struct StyledNode {
dom::Node const &node;
std::vector<std::pair<css::PropertyId, std::string>> properties;
@@ -24,9 +30,16 @@ struct StyledNode {
std::string_view get_raw_property(css::PropertyId) const;
 
template<css::PropertyId T>
std::string_view get_property() const {
return get_raw_property(T);
auto get_property() const {
if constexpr (T == css::PropertyId::Display) {
return get_display_property(T);
} else {
return get_raw_property(T);
}
}
 
private:
DisplayValue get_display_property(css::PropertyId) const;
};
 
[[nodiscard]] inline bool operator==(style::StyledNode const &a, style::StyledNode const &b) noexcept {