From bd55f0ad64b4bff802bacf24d82a94f11a822274 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Thu, 2 Sep 2021 02:17:13 +0100 Subject: [PATCH] LibWeb: Add shadow including ancestor/descendant checks --- Userland/Libraries/LibWeb/DOM/Node.cpp | 33 ++++++++++++++++++++++++++ Userland/Libraries/LibWeb/DOM/Node.h | 5 ++++ 2 files changed, 38 insertions(+) diff --git a/Userland/Libraries/LibWeb/DOM/Node.cpp b/Userland/Libraries/LibWeb/DOM/Node.cpp index 4bfe4dadad..10fc69c0eb 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.cpp +++ b/Userland/Libraries/LibWeb/DOM/Node.cpp @@ -655,4 +655,37 @@ bool Node::contains(RefPtr other) const return other && other->is_inclusive_descendant_of(*this); } +// https://dom.spec.whatwg.org/#concept-shadow-including-descendant +bool Node::is_shadow_including_descendant_of(Node const& other) const +{ + if (is_descendant_of(other)) + return true; + + if (!is(root())) + return false; + + auto shadow_root = verify_cast(root()); + + // NOTE: While host is nullable because of inheriting from DocumentFragment, shadow roots always have a host. + return shadow_root->host()->is_shadow_including_inclusive_descendant_of(other); +} + +// https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-descendant +bool Node::is_shadow_including_inclusive_descendant_of(Node const& other) const +{ + return &other == this || is_shadow_including_descendant_of(other); +} + +// https://dom.spec.whatwg.org/#concept-shadow-including-ancestor +bool Node::is_shadow_including_ancestor_of(Node const& other) const +{ + return other.is_shadow_including_descendant_of(*this); +} + +// https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-ancestor +bool Node::is_shadow_including_inclusive_ancestor_of(Node const& other) const +{ + return other.is_shadow_including_inclusive_descendant_of(*this); +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Node.h b/Userland/Libraries/LibWeb/DOM/Node.h index 2238fcedf6..5907e061b7 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.h +++ b/Userland/Libraries/LibWeb/DOM/Node.h @@ -174,6 +174,11 @@ public: // Used for dumping the DOM Tree void serialize_tree_as_json(JsonObjectSerializer&) const; + bool is_shadow_including_descendant_of(Node const&) const; + bool is_shadow_including_inclusive_descendant_of(Node const&) const; + bool is_shadow_including_ancestor_of(Node const&) const; + bool is_shadow_including_inclusive_ancestor_of(Node const&) const; + protected: Node(Document&, NodeType);