srctree

Robin Linden parent 8f54f7ed 0b4dd877
style: Match classes attached to a specific element type

inlinesplit
style/style.cpp added: 17, removed: 5, total 12
@@ -136,9 +136,15 @@ bool is_match(style::StyledNode const &node, std::string_view selector) {
return true;
}
 
if (selector_.starts_with('.')) {
selector_.remove_prefix(1);
auto classes = util::split(selector_, ".");
auto class_position = selector_.find('.');
if (class_position != std::string_view::npos) {
auto class_string = selector_.substr(class_position);
if (class_position != 0 && selector_.substr(0, class_position) != element.name) {
return false;
}
 
class_string.remove_prefix(1);
auto classes = util::split(class_string, ".");
return std::ranges::all_of(classes, [&](auto const &c) { return has_class(element, c); });
}
 
 
style/style_test.cpp added: 17, removed: 5, total 12
@@ -112,6 +112,12 @@ int main() {
 
expect(is_match(dom::Element{"div", {{"class", "first second"}}}, ".first.second"sv));
expect(!is_match(dom::Element{"div", {{"class", "first second"}}}, ".first.third"sv));
 
expect(is_match(dom::Element{"div", {{"class", "first second"}}}, "div.first"sv));
expect(is_match(dom::Element{"div", {{"class", "first second"}}}, "div.second"sv));
expect(is_match(dom::Element{"div", {{"class", "first second"}}}, "div.second.first"sv));
expect(!is_match(dom::Element{"div", {{"class", "first second"}}}, "div.third"sv));
expect(!is_match(dom::Element{"div", {{"class", "first second"}}}, "p.first"sv));
});
 
etest::test("is_match: id", [] {