srctree

Robin Linden parent 5a5db6ec 3be735fa
style: Support matching the :root psuedo-class

inlinesplit
style/style.cpp added: 25, removed: 7, total 18
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2023 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -95,11 +95,11 @@ bool is_match(style::StyledNode const &node, std::string_view selector) {
}
 
if (!psuedo_class.empty()) {
// https://developer.mozilla.org/en-US/docs/Web/CSS/:any-link
// https://developer.mozilla.org/en-US/docs/Web/CSS/:link
// https://developer.mozilla.org/en-US/docs/Web/CSS/:visited
// Ignoring :visited for now as we treat all links as unvisited.
if (psuedo_class == "link" || psuedo_class == "any-link") {
// https://developer.mozilla.org/en-US/docs/Web/CSS/:any-link
// https://developer.mozilla.org/en-US/docs/Web/CSS/:link
// https://developer.mozilla.org/en-US/docs/Web/CSS/:visited
// Ignoring :visited for now as we treat all links as unvisited.
if (!element.attributes.contains("href")) {
return false;
}
@@ -108,6 +108,15 @@ bool is_match(style::StyledNode const &node, std::string_view selector) {
return false;
}
 
if (selector_.empty()) {
return true;
}
} else if (psuedo_class == "root") {
// https://developer.mozilla.org/en-US/docs/Web/CSS/:root
if (node.parent != nullptr) {
return false;
}
 
if (selector_.empty()) {
return true;
}
 
style/style_test.cpp added: 25, removed: 7, total 18
@@ -136,6 +136,15 @@ int main() {
});
}
 
etest::test("is_match: :root", [] {
dom::Element dom = dom::Element{"html", {}, {dom::Element{"body"}}};
style::StyledNode node{dom, {}, {style::StyledNode{dom.children[0]}}};
node.children[0].parent = &node;
 
expect(style::is_match(node, ":root"));
expect(!style::is_match(node.children[0], ":root"));
});
 
etest::test("is_match: child", [] {
dom::Element dom = dom::Element{"div", {{"class", "logo"}}, {dom::Element{"span"}}};
style::StyledNode node{dom, {}, {style::StyledNode{dom.children[0]}}};