srctree

Robin Linden parent 824c04d2 e9da7bc1
style: Add custom properties to the styled tree

style/style.cpp added: 28, removed: 5, total 23
@@ -152,6 +152,7 @@ bool is_match(style::StyledNode const &node, std::string_view selector) {
MatchingProperties matching_properties(
style::StyledNode const &node, css::StyleSheet const &stylesheet, css::MediaQuery::Context const &ctx) {
std::vector<std::pair<css::PropertyId, std::string>> matched_properties;
std::vector<std::pair<std::string, std::string>> matched_custom_properties;
 
for (auto const &rule : stylesheet.rules) {
if (rule.media_query.has_value() && !rule.media_query->evaluate(ctx)) {
@@ -160,6 +161,7 @@ MatchingProperties matching_properties(
 
if (std::ranges::any_of(rule.selectors, [&](auto const &selector) { return is_match(node, selector); })) {
std::ranges::copy(rule.declarations, std::back_inserter(matched_properties));
std::ranges::copy(rule.custom_properties, std::back_inserter(matched_custom_properties));
}
}
 
@@ -189,7 +191,7 @@ MatchingProperties matching_properties(
}
}
 
return {std::move(matched_properties)};
return {std::move(matched_properties), std::move(matched_custom_properties)};
}
 
namespace {
 
style/style.h added: 28, removed: 5, total 23
@@ -23,6 +23,7 @@ bool is_match(StyledNode const &, std::string_view selector);
 
struct MatchingProperties {
std::vector<std::pair<css::PropertyId, std::string>> normal;
std::vector<std::pair<std::string, std::string>> custom;
};
 
MatchingProperties matching_properties(StyledNode const &, css::StyleSheet const &, css::MediaQuery::Context const &);
 
style/style_test.cpp added: 28, removed: 5, total 23
@@ -284,6 +284,25 @@ int main() {
expect(check_parents(*style::style_tree(root, stylesheet), expected));
});
 
etest::test("matching rules: custom properties", [] {
css::StyleSheet stylesheet{{
css::Rule{.selectors{"p"}, .custom_properties{{"--hello", "very yes"}}},
css::Rule{.selectors{"a"}, .custom_properties{{"--goodbye", "very no"}}},
}};
 
auto res = style::matching_properties({.node = dom::Element{"p"}}, stylesheet, {});
expect_eq(res.custom, std::vector{std::pair{"--hello"s, "very yes"s}});
expect(res.normal.empty());
 
res = style::matching_properties({.node = dom::Element{"a"}}, stylesheet, {});
expect_eq(res.custom, std::vector{std::pair{"--goodbye"s, "very no"s}});
expect(res.normal.empty());
 
res = style::matching_properties({.node = dom::Element{"div"}}, stylesheet, {});
expect(res.custom.empty());
expect(res.normal.empty());
});
 
inline_css_tests();
important_declarations_tests();
return etest::run_all_tests();
 
style/styled_node.h added: 28, removed: 5, total 23
@@ -78,6 +78,7 @@ struct StyledNode {
std::vector<std::pair<css::PropertyId, std::string>> properties;
std::vector<StyledNode> children;
StyledNode const *parent{nullptr};
std::vector<std::pair<std::string, std::string>> custom_properties;
 
std::string_view get_raw_property(css::PropertyId) const;