1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Neither the name of Google Inc. nor the names of its
11 * contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_DOM_NODE_CHILD_REMOVAL_TRACKER_H_
28 #define THIRD_PARTY_BLINK_RENDERER_CORE_DOM_NODE_CHILD_REMOVAL_TRACKER_H_
29
30 #include "third_party/blink/renderer/core/dom/node.h"
31 #include "third_party/blink/renderer/platform/heap/handle.h"
32
33 namespace blink {
34
35 class NodeChildRemovalTracker {
36 STACK_ALLOCATED();
37
38 public:
39 explicit NodeChildRemovalTracker(const Node&);
40 ~NodeChildRemovalTracker();
41
42 static bool IsBeingRemoved(const Node&);
43
44 private:
GetNode()45 const Node& GetNode() const { return *node_; }
Previous()46 NodeChildRemovalTracker* Previous() { return previous_; }
47
48 const Node* node_;
49 // Using raw pointers are safe because these NodeChildRemovalTrackers are
50 // guaranteed to be on a stack.
51 NodeChildRemovalTracker* previous_;
52 static NodeChildRemovalTracker* last_;
53 };
54
NodeChildRemovalTracker(const Node & node)55 inline NodeChildRemovalTracker::NodeChildRemovalTracker(const Node& node)
56 : node_(&node), previous_(last_) {
57 last_ = this;
58 }
59
~NodeChildRemovalTracker()60 inline NodeChildRemovalTracker::~NodeChildRemovalTracker() {
61 last_ = previous_;
62 }
63
IsBeingRemoved(const Node & node)64 inline bool NodeChildRemovalTracker::IsBeingRemoved(const Node& node) {
65 for (NodeChildRemovalTracker* removal = last_; removal;
66 removal = removal->Previous()) {
67 if (removal->GetNode().IsShadowIncludingInclusiveAncestorOf(node))
68 return true;
69 }
70
71 return false;
72 }
73
74 } // namespace blink
75
76 #endif
77