From cad326f3230620512e7b8a5e7c65af4827421092 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 6 Oct 2019 15:41:16 +0200 Subject: [PATCH] LibHTML: Implement immediate-child selectors (#foo > #bar) --- Base/home/anon/www/selectors.html | 17 +++++++++++++++++ Libraries/LibHTML/CSS/StyleResolver.cpp | 10 ++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Base/home/anon/www/selectors.html b/Base/home/anon/www/selectors.html index 2ad965f2c1..88c248e467 100644 --- a/Base/home/anon/www/selectors.html +++ b/Base/home/anon/www/selectors.html @@ -11,6 +11,10 @@ border-width: 1; border-color: #00ff00; } +div > .boo > .bee { + background-color: #000000; + color: #ff00ff; +} @@ -28,5 +32,18 @@
hello
+
+
+
Spooky!
+
+
+
+
+
+
+ Not spooky! +
+
+
diff --git a/Libraries/LibHTML/CSS/StyleResolver.cpp b/Libraries/LibHTML/CSS/StyleResolver.cpp index 95b7ab8cc2..3450cb8e12 100644 --- a/Libraries/LibHTML/CSS/StyleResolver.cpp +++ b/Libraries/LibHTML/CSS/StyleResolver.cpp @@ -34,9 +34,10 @@ static bool matches(const Selector& selector, int component_index, const Element auto& component = selector.components()[component_index]; if (!matches(component, element)) return false; - if (component.relation == Selector::Component::Relation::None) + switch (component.relation) { + case Selector::Component::Relation::None: return true; - if (component.relation == Selector::Component::Relation::Descendant) { + case Selector::Component::Relation::Descendant: ASSERT(component_index != 0); for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) { if (!ancestor->is_element()) @@ -45,6 +46,11 @@ static bool matches(const Selector& selector, int component_index, const Element return true; } return false; + case Selector::Component::Relation::ImmediateChild: + ASSERT(component_index != 0); + if (!element.parent() || !element.parent()->is_element()) + return false; + return matches(selector, component_index - 1, static_cast(*element.parent())); } ASSERT_NOT_REACHED(); }