Browse Source

LibWeb: Add shadow including ancestor/descendant checks

Luke Wilde 3 years ago
parent
commit
bd55f0ad64
2 changed files with 38 additions and 0 deletions
  1. 33 0
      Userland/Libraries/LibWeb/DOM/Node.cpp
  2. 5 0
      Userland/Libraries/LibWeb/DOM/Node.h

+ 33 - 0
Userland/Libraries/LibWeb/DOM/Node.cpp

@@ -655,4 +655,37 @@ bool Node::contains(RefPtr<Node> 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<ShadowRoot>(root()))
+        return false;
+
+    auto shadow_root = verify_cast<ShadowRoot>(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);
+}
+
 }

+ 5 - 0
Userland/Libraries/LibWeb/DOM/Node.h

@@ -174,6 +174,11 @@ public:
     // Used for dumping the DOM Tree
     void serialize_tree_as_json(JsonObjectSerializer<StringBuilder>&) 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);