srctree

Robin Linden parent 393ed034 9fa0779e
style: Support lookup of inherited css variables

inlinesplit
style/styled_node.cpp added: 22, removed: 12, total 10
@@ -241,18 +241,31 @@ std::string_view StyledNode::get_raw_property(css::PropertyId property) const {
if (it->second.starts_with("var(") && (it->second.find(')') != std::string::npos)) {
// Remove "var(" from the start and ")" from the end. 5 characters in total.
auto var_name = it->second.substr(4, it->second.size() - 5);
auto prop = std::ranges::find(custom_properties, var_name, &std::pair<std::string, std::string>::first);
if (prop == end(custom_properties)) {
spdlog::info("No matching variable for custom property '{}'", var_name);
auto prop = resolve_variable(var_name);
if (!prop) {
return it->second;
}
 
return prop->second;
return *prop;
}
 
return it->second;
}
 
std::optional<std::string_view> StyledNode::resolve_variable(std::string_view name) const {
auto prop = std::ranges::find(custom_properties, name, &std::pair<std::string, std::string>::first);
if (prop == end(custom_properties)) {
if (parent != nullptr) {
return parent->resolve_variable(name);
}
 
spdlog::info("No matching variable for custom property '{}'", name);
return std::nullopt;
}
 
return prop->second;
}
 
BorderStyle StyledNode::get_border_style_property(css::PropertyId property) const {
auto raw = get_raw_property(property);
 
 
style/styled_node.h added: 22, removed: 12, total 10
@@ -140,6 +140,8 @@ struct StyledNode {
}
 
private:
std::optional<std::string_view> resolve_variable(std::string_view) const;
 
BorderStyle get_border_style_property(css::PropertyId) const;
gfx::Color get_color_property(css::PropertyId) const;
DisplayValue get_display_property() const;
 
style/styled_node_test.cpp added: 22, removed: 12, total 10
@@ -504,13 +504,8 @@ int main() {
auto &child = styled_node.children[0];
child.parent = &styled_node;
 
// TODO(robinlinden)
#if 0
expect_eq(child.get_property<css::PropertyId::FontWeight>(), //
style::FontWeight::bold());
#endif
expect_eq(child.get_property<css::PropertyId::FontWeight>(), //
std::nullopt);
});
 
return etest::run_all_tests();