OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Neither the name of Google Inc. nor the names of its | 10 * * Neither the name of Google Inc. nor the names of its |
11 * contributors may be used to endorse or promote products derived from | 11 * contributors may be used to endorse or promote products derived from |
12 * this software without specific prior written permission. | 12 * this software without specific prior written permission. |
13 * | 13 * |
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 */ | 25 */ |
26 | 26 |
27 #ifndef ComposedTreeTraversal_h | 27 #ifndef FlatTreeTraversal_h |
28 #define ComposedTreeTraversal_h | 28 #define FlatTreeTraversal_h |
29 | 29 |
30 #include "core/CoreExport.h" | 30 #include "core/CoreExport.h" |
31 #include "core/dom/Document.h" | 31 #include "core/dom/Document.h" |
32 #include "core/dom/LayoutTreeBuilderTraversal.h" | 32 #include "core/dom/LayoutTreeBuilderTraversal.h" |
33 #include "core/dom/shadow/InsertionPoint.h" | 33 #include "core/dom/shadow/InsertionPoint.h" |
34 #include "core/dom/shadow/ShadowRoot.h" | 34 #include "core/dom/shadow/ShadowRoot.h" |
35 #include "wtf/Allocator.h" | 35 #include "wtf/Allocator.h" |
36 | 36 |
37 namespace blink { | 37 namespace blink { |
38 | 38 |
39 class ContainerNode; | 39 class ContainerNode; |
40 class HTMLSlotElement; | 40 class HTMLSlotElement; |
41 class Node; | 41 class Node; |
42 | 42 |
43 // Composed tree version of |NodeTraversal|. | 43 // Flat tree version of |NodeTraversal|. |
44 // | 44 // |
45 // None of member functions takes a |ShadowRoot| or an active insertion point, | 45 // None of member functions takes a |ShadowRoot| or an active insertion point, |
46 // e.g. roughly speaking <content> and <shadow> in the shadow tree, see | 46 // e.g. roughly speaking <content> and <shadow> in the shadow tree, see |
47 // |InsertionPoint::isActive()| for details of active insertion points, since | 47 // |InsertionPoint::isActive()| for details of active insertion points, since |
48 // they aren't appeared in the composed tree. |assertPrecondition()| and | 48 // they aren't appeared in the flat tree. |assertPrecondition()| and |
49 // |assertPostCondition()| check this condition. | 49 // |assertPostCondition()| check this condition. |
50 // | 50 // |
51 // FIXME: Make some functions inline to optimise the performance. | 51 // FIXME: Make some functions inline to optimise the performance. |
52 // https://bugs.webkit.org/show_bug.cgi?id=82702 | 52 // https://bugs.webkit.org/show_bug.cgi?id=82702 |
53 class CORE_EXPORT ComposedTreeTraversal { | 53 class CORE_EXPORT FlatTreeTraversal { |
54 STATIC_ONLY(ComposedTreeTraversal); | 54 STATIC_ONLY(FlatTreeTraversal); |
55 public: | 55 public: |
56 typedef LayoutTreeBuilderTraversal::ParentDetails ParentTraversalDetails; | 56 typedef LayoutTreeBuilderTraversal::ParentDetails ParentTraversalDetails; |
57 | 57 |
58 static Node* next(const Node&); | 58 static Node* next(const Node&); |
59 static Node* next(const Node&, const Node* stayWithin); | 59 static Node* next(const Node&, const Node* stayWithin); |
60 static Node* previous(const Node&); | 60 static Node* previous(const Node&); |
61 | 61 |
62 static Node* firstChild(const Node&); | 62 static Node* firstChild(const Node&); |
63 static Node* lastChild(const Node&); | 63 static Node* lastChild(const Node&); |
64 static bool hasChildren(const Node&); | 64 static bool hasChildren(const Node&); |
65 | 65 |
66 static ContainerNode* parent(const Node&, ParentTraversalDetails* = 0); | 66 static ContainerNode* parent(const Node&, ParentTraversalDetails* = 0); |
67 static Element* parentElement(const Node&); | 67 static Element* parentElement(const Node&); |
68 | 68 |
69 static Node* nextSibling(const Node&); | 69 static Node* nextSibling(const Node&); |
70 static Node* previousSibling(const Node&); | 70 static Node* previousSibling(const Node&); |
71 | 71 |
72 // Returns a child node at |index|. If |index| is greater than or equal to | 72 // Returns a child node at |index|. If |index| is greater than or equal to |
73 // the children, this function returns |nullptr|. | 73 // the children, this function returns |nullptr|. |
74 static Node* childAt(const Node&, unsigned index); | 74 static Node* childAt(const Node&, unsigned index); |
75 | 75 |
76 // Composed tree version of |NodeTraversal::nextSkippingChildren()|. This | 76 // Flat tree version of |NodeTraversal::nextSkippingChildren()|. This |
77 // function is similar to |next()| but skips child nodes of a specified | 77 // function is similar to |next()| but skips child nodes of a specified |
78 // node. | 78 // node. |
79 static Node* nextSkippingChildren(const Node&); | 79 static Node* nextSkippingChildren(const Node&); |
80 static Node* nextSkippingChildren(const Node&, const Node* stayWithin); | 80 static Node* nextSkippingChildren(const Node&, const Node* stayWithin); |
81 | 81 |
82 // Composed tree version of |NodeTraversal::previousSkippingChildren()| | 82 // Flat tree version of |NodeTraversal::previousSkippingChildren()| |
83 // similar to |previous()| but skipping child nodes of the specified node. | 83 // similar to |previous()| but skipping child nodes of the specified node. |
84 static Node* previousSkippingChildren(const Node&); | 84 static Node* previousSkippingChildren(const Node&); |
85 | 85 |
86 // Like previous, but visits parents before their children. | 86 // Like previous, but visits parents before their children. |
87 static Node* previousPostOrder(const Node&, const Node* stayWithin = nullptr
); | 87 static Node* previousPostOrder(const Node&, const Node* stayWithin = nullptr
); |
88 | 88 |
89 // Composed tree version of |Node::isDescendantOf(other)|. This function | 89 // Flat tree version of |Node::isDescendantOf(other)|. This function |
90 // returns true if |other| contains |node|, otherwise returns | 90 // returns true if |other| contains |node|, otherwise returns |
91 // false. If |other| is |node|, this function returns false. | 91 // false. If |other| is |node|, this function returns false. |
92 static bool isDescendantOf(const Node& /*node*/, const Node& other); | 92 static bool isDescendantOf(const Node& /*node*/, const Node& other); |
93 | 93 |
94 static bool contains(const ContainerNode& container, const Node& node) | 94 static bool contains(const ContainerNode& container, const Node& node) |
95 { | 95 { |
96 assertPrecondition(container); | 96 assertPrecondition(container); |
97 assertPrecondition(node); | 97 assertPrecondition(node); |
98 return container == node || isDescendantOf(node, container); | 98 return container == node || isDescendantOf(node, container); |
99 } | 99 } |
100 | 100 |
101 static bool containsIncludingPseudoElement(const ContainerNode&, const Node&
); | 101 static bool containsIncludingPseudoElement(const ContainerNode&, const Node&
); |
102 | 102 |
103 // Returns a common ancestor of |nodeA| and |nodeB| if exists, otherwise | 103 // Returns a common ancestor of |nodeA| and |nodeB| if exists, otherwise |
104 // returns |nullptr|. | 104 // returns |nullptr|. |
105 static Node* commonAncestor(const Node& nodeA, const Node& nodeB); | 105 static Node* commonAncestor(const Node& nodeA, const Node& nodeB); |
106 | 106 |
107 // Composed tree version of |Node::nodeIndex()|. This function returns a | 107 // Flat tree version of |Node::nodeIndex()|. This function returns a |
108 // zero base position number of the specified node in child nodes list, or | 108 // zero base position number of the specified node in child nodes list, or |
109 // zero if the specified node has no parent. | 109 // zero if the specified node has no parent. |
110 static unsigned index(const Node&); | 110 static unsigned index(const Node&); |
111 | 111 |
112 // Composed tree version of |ContainerNode::countChildren()|. This function | 112 // Flat tree version of |ContainerNode::countChildren()|. This function |
113 // returns the number of the child nodes of the specified node in the | 113 // returns the number of the child nodes of the specified node in the |
114 // composed tree. | 114 // flat tree. |
115 static unsigned countChildren(const Node&); | 115 static unsigned countChildren(const Node&); |
116 | 116 |
117 static Node* lastWithin(const Node&); | 117 static Node* lastWithin(const Node&); |
118 static Node& lastWithinOrSelf(const Node&); | 118 static Node& lastWithinOrSelf(const Node&); |
119 | 119 |
120 private: | 120 private: |
121 enum TraversalDirection { | 121 enum TraversalDirection { |
122 TraversalDirectionForward, | 122 TraversalDirectionForward, |
123 TraversalDirectionBackward | 123 TraversalDirectionBackward |
124 }; | 124 }; |
125 | 125 |
126 static void assertPrecondition(const Node& node) | 126 static void assertPrecondition(const Node& node) |
127 { | 127 { |
128 #if ENABLE(ASSERT) | 128 #if ENABLE(ASSERT) |
129 ASSERT(!node.needsDistributionRecalc()); | 129 ASSERT(!node.needsDistributionRecalc()); |
130 ASSERT(node.canParticipateInComposedTree()); | 130 ASSERT(node.canParticipateInFlatTree()); |
131 #endif | 131 #endif |
132 } | 132 } |
133 | 133 |
134 static void assertPostcondition(const Node* node) | 134 static void assertPostcondition(const Node* node) |
135 { | 135 { |
136 #if ENABLE(ASSERT) | 136 #if ENABLE(ASSERT) |
137 if (node) | 137 if (node) |
138 assertPrecondition(*node); | 138 assertPrecondition(*node); |
139 #endif | 139 #endif |
140 } | 140 } |
(...skipping 19 matching lines...) Expand all Loading... |
160 static Node* traversePreviousSibling(const Node&); | 160 static Node* traversePreviousSibling(const Node&); |
161 | 161 |
162 static Node* traverseSiblings(const Node&, TraversalDirection); | 162 static Node* traverseSiblings(const Node&, TraversalDirection); |
163 static Node* traverseSiblingsForV1HostChild(const Node&, TraversalDirection)
; | 163 static Node* traverseSiblingsForV1HostChild(const Node&, TraversalDirection)
; |
164 static Node* traverseSiblingsForV0Distribution(const Node&, TraversalDirecti
on); | 164 static Node* traverseSiblingsForV0Distribution(const Node&, TraversalDirecti
on); |
165 | 165 |
166 static Node* traverseNextAncestorSibling(const Node&); | 166 static Node* traverseNextAncestorSibling(const Node&); |
167 static Node* traversePreviousAncestorSibling(const Node&); | 167 static Node* traversePreviousAncestorSibling(const Node&); |
168 }; | 168 }; |
169 | 169 |
170 inline ContainerNode* ComposedTreeTraversal::parent(const Node& node, ParentTrav
ersalDetails* details) | 170 inline ContainerNode* FlatTreeTraversal::parent(const Node& node, ParentTraversa
lDetails* details) |
171 { | 171 { |
172 assertPrecondition(node); | 172 assertPrecondition(node); |
173 ContainerNode* result = traverseParent(node, details); | 173 ContainerNode* result = traverseParent(node, details); |
174 assertPostcondition(result); | 174 assertPostcondition(result); |
175 return result; | 175 return result; |
176 } | 176 } |
177 | 177 |
178 inline Element* ComposedTreeTraversal::parentElement(const Node& node) | 178 inline Element* FlatTreeTraversal::parentElement(const Node& node) |
179 { | 179 { |
180 ContainerNode* parent = ComposedTreeTraversal::parent(node); | 180 ContainerNode* parent = FlatTreeTraversal::parent(node); |
181 return parent && parent->isElementNode() ? toElement(parent) : nullptr; | 181 return parent && parent->isElementNode() ? toElement(parent) : nullptr; |
182 } | 182 } |
183 | 183 |
184 inline Node* ComposedTreeTraversal::nextSibling(const Node& node) | 184 inline Node* FlatTreeTraversal::nextSibling(const Node& node) |
185 { | 185 { |
186 assertPrecondition(node); | 186 assertPrecondition(node); |
187 Node* result = traverseSiblings(node, TraversalDirectionForward); | 187 Node* result = traverseSiblings(node, TraversalDirectionForward); |
188 assertPostcondition(result); | 188 assertPostcondition(result); |
189 return result; | 189 return result; |
190 } | 190 } |
191 | 191 |
192 inline Node* ComposedTreeTraversal::previousSibling(const Node& node) | 192 inline Node* FlatTreeTraversal::previousSibling(const Node& node) |
193 { | 193 { |
194 assertPrecondition(node); | 194 assertPrecondition(node); |
195 Node* result = traverseSiblings(node, TraversalDirectionBackward); | 195 Node* result = traverseSiblings(node, TraversalDirectionBackward); |
196 assertPostcondition(result); | 196 assertPostcondition(result); |
197 return result; | 197 return result; |
198 } | 198 } |
199 | 199 |
200 inline Node* ComposedTreeTraversal::next(const Node& node) | 200 inline Node* FlatTreeTraversal::next(const Node& node) |
201 { | 201 { |
202 assertPrecondition(node); | 202 assertPrecondition(node); |
203 Node* result = traverseNext(node); | 203 Node* result = traverseNext(node); |
204 assertPostcondition(result); | 204 assertPostcondition(result); |
205 return result; | 205 return result; |
206 } | 206 } |
207 | 207 |
208 inline Node* ComposedTreeTraversal::next(const Node& node, const Node* stayWithi
n) | 208 inline Node* FlatTreeTraversal::next(const Node& node, const Node* stayWithin) |
209 { | 209 { |
210 assertPrecondition(node); | 210 assertPrecondition(node); |
211 Node* result = traverseNext(node, stayWithin); | 211 Node* result = traverseNext(node, stayWithin); |
212 assertPostcondition(result); | 212 assertPostcondition(result); |
213 return result; | 213 return result; |
214 } | 214 } |
215 | 215 |
216 inline Node* ComposedTreeTraversal::nextSkippingChildren(const Node& node, const
Node* stayWithin) | 216 inline Node* FlatTreeTraversal::nextSkippingChildren(const Node& node, const Nod
e* stayWithin) |
217 { | 217 { |
218 assertPrecondition(node); | 218 assertPrecondition(node); |
219 Node* result = traverseNextSkippingChildren(node, stayWithin); | 219 Node* result = traverseNextSkippingChildren(node, stayWithin); |
220 assertPostcondition(result); | 220 assertPostcondition(result); |
221 return result; | 221 return result; |
222 } | 222 } |
223 | 223 |
224 inline Node* ComposedTreeTraversal::traverseNext(const Node& node) | 224 inline Node* FlatTreeTraversal::traverseNext(const Node& node) |
225 { | 225 { |
226 if (Node* next = traverseFirstChild(node)) | 226 if (Node* next = traverseFirstChild(node)) |
227 return next; | 227 return next; |
228 for (const Node* next = &node; next; next = traverseParent(*next)) { | 228 for (const Node* next = &node; next; next = traverseParent(*next)) { |
229 if (Node* sibling = traverseNextSibling(*next)) | 229 if (Node* sibling = traverseNextSibling(*next)) |
230 return sibling; | 230 return sibling; |
231 } | 231 } |
232 return nullptr; | 232 return nullptr; |
233 } | 233 } |
234 | 234 |
235 inline Node* ComposedTreeTraversal::traverseNext(const Node& node, const Node* s
tayWithin) | 235 inline Node* FlatTreeTraversal::traverseNext(const Node& node, const Node* stayW
ithin) |
236 { | 236 { |
237 if (Node* next = traverseFirstChild(node)) | 237 if (Node* next = traverseFirstChild(node)) |
238 return next; | 238 return next; |
239 return traverseNextSkippingChildren(node, stayWithin); | 239 return traverseNextSkippingChildren(node, stayWithin); |
240 } | 240 } |
241 | 241 |
242 inline Node* ComposedTreeTraversal::traverseNextSkippingChildren(const Node& nod
e, const Node* stayWithin) | 242 inline Node* FlatTreeTraversal::traverseNextSkippingChildren(const Node& node, c
onst Node* stayWithin) |
243 { | 243 { |
244 for (const Node* next = &node; next; next = traverseParent(*next)) { | 244 for (const Node* next = &node; next; next = traverseParent(*next)) { |
245 if (next == stayWithin) | 245 if (next == stayWithin) |
246 return nullptr; | 246 return nullptr; |
247 if (Node* sibling = traverseNextSibling(*next)) | 247 if (Node* sibling = traverseNextSibling(*next)) |
248 return sibling; | 248 return sibling; |
249 } | 249 } |
250 return nullptr; | 250 return nullptr; |
251 } | 251 } |
252 | 252 |
253 inline Node* ComposedTreeTraversal::previous(const Node& node) | 253 inline Node* FlatTreeTraversal::previous(const Node& node) |
254 { | 254 { |
255 assertPrecondition(node); | 255 assertPrecondition(node); |
256 Node* result = traversePrevious(node); | 256 Node* result = traversePrevious(node); |
257 assertPostcondition(result); | 257 assertPostcondition(result); |
258 return result; | 258 return result; |
259 } | 259 } |
260 | 260 |
261 inline Node* ComposedTreeTraversal::traversePrevious(const Node& node) | 261 inline Node* FlatTreeTraversal::traversePrevious(const Node& node) |
262 { | 262 { |
263 if (Node* previous = traversePreviousSibling(node)) { | 263 if (Node* previous = traversePreviousSibling(node)) { |
264 while (Node* child = traverseLastChild(*previous)) | 264 while (Node* child = traverseLastChild(*previous)) |
265 previous = child; | 265 previous = child; |
266 return previous; | 266 return previous; |
267 } | 267 } |
268 return traverseParent(node); | 268 return traverseParent(node); |
269 } | 269 } |
270 | 270 |
271 inline Node* ComposedTreeTraversal::firstChild(const Node& node) | 271 inline Node* FlatTreeTraversal::firstChild(const Node& node) |
272 { | 272 { |
273 assertPrecondition(node); | 273 assertPrecondition(node); |
274 Node* result = traverseChild(node, TraversalDirectionForward); | 274 Node* result = traverseChild(node, TraversalDirectionForward); |
275 assertPostcondition(result); | 275 assertPostcondition(result); |
276 return result; | 276 return result; |
277 } | 277 } |
278 | 278 |
279 inline Node* ComposedTreeTraversal::lastChild(const Node& node) | 279 inline Node* FlatTreeTraversal::lastChild(const Node& node) |
280 { | 280 { |
281 assertPrecondition(node); | 281 assertPrecondition(node); |
282 Node* result = traverseLastChild(node); | 282 Node* result = traverseLastChild(node); |
283 assertPostcondition(result); | 283 assertPostcondition(result); |
284 return result; | 284 return result; |
285 } | 285 } |
286 | 286 |
287 inline bool ComposedTreeTraversal::hasChildren(const Node& node) | 287 inline bool FlatTreeTraversal::hasChildren(const Node& node) |
288 { | 288 { |
289 return firstChild(node); | 289 return firstChild(node); |
290 } | 290 } |
291 | 291 |
292 inline Node* ComposedTreeTraversal::traverseNextSibling(const Node& node) | 292 inline Node* FlatTreeTraversal::traverseNextSibling(const Node& node) |
293 { | 293 { |
294 return traverseSiblings(node, TraversalDirectionForward); | 294 return traverseSiblings(node, TraversalDirectionForward); |
295 } | 295 } |
296 | 296 |
297 inline Node* ComposedTreeTraversal::traversePreviousSibling(const Node& node) | 297 inline Node* FlatTreeTraversal::traversePreviousSibling(const Node& node) |
298 { | 298 { |
299 return traverseSiblings(node, TraversalDirectionBackward); | 299 return traverseSiblings(node, TraversalDirectionBackward); |
300 } | 300 } |
301 | 301 |
302 inline Node* ComposedTreeTraversal::traverseFirstChild(const Node& node) | 302 inline Node* FlatTreeTraversal::traverseFirstChild(const Node& node) |
303 { | 303 { |
304 return traverseChild(node, TraversalDirectionForward); | 304 return traverseChild(node, TraversalDirectionForward); |
305 } | 305 } |
306 | 306 |
307 inline Node* ComposedTreeTraversal::traverseLastChild(const Node& node) | 307 inline Node* FlatTreeTraversal::traverseLastChild(const Node& node) |
308 { | 308 { |
309 return traverseChild(node, TraversalDirectionBackward); | 309 return traverseChild(node, TraversalDirectionBackward); |
310 } | 310 } |
311 | 311 |
312 } // namespace blink | 312 } // namespace blink |
313 | 313 |
314 #endif | 314 #endif |
OLD | NEW |