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 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 return false; | 184 return false; |
185 } | 185 } |
186 | 186 |
187 Node* ComposedTreeTraversal::previousSkippingChildren(const Node& node) | 187 Node* ComposedTreeTraversal::previousSkippingChildren(const Node& node) |
188 { | 188 { |
189 if (Node* previousSibling = traversePreviousSibling(node)) | 189 if (Node* previousSibling = traversePreviousSibling(node)) |
190 return previousSibling; | 190 return previousSibling; |
191 return traversePreviousAncestorSibling(node); | 191 return traversePreviousAncestorSibling(node); |
192 } | 192 } |
193 | 193 |
| 194 static Node* previousAncestorSiblingPostOrder(const Node& current, const Node* s
tayWithin) |
| 195 { |
| 196 ASSERT(!ComposedTreeTraversal::previousSibling(current)); |
| 197 for (Node* parent = ComposedTreeTraversal::parent(current); parent; parent =
ComposedTreeTraversal::parent(*parent)) { |
| 198 if (parent == stayWithin) |
| 199 return nullptr; |
| 200 if (Node* previousSibling = ComposedTreeTraversal::previousSibling(*pare
nt)) |
| 201 return previousSibling; |
| 202 } |
| 203 return nullptr; |
| 204 } |
| 205 |
| 206 // TODO(yosin) We should consider introducing template class to share code |
| 207 // between DOM tree traversal and composed tree tarversal. |
| 208 Node* ComposedTreeTraversal::previousPostOrder(const Node& current, const Node*
stayWithin) |
| 209 { |
| 210 assertPrecondition(current); |
| 211 if (stayWithin) |
| 212 assertPrecondition(*stayWithin); |
| 213 if (Node* lastChild = traverseLastChild(current)) { |
| 214 assertPostcondition(lastChild); |
| 215 return lastChild; |
| 216 } |
| 217 if (current == stayWithin) |
| 218 return nullptr; |
| 219 if (Node* previousSibling = traversePreviousSibling(current)) { |
| 220 assertPostcondition(previousSibling); |
| 221 return previousSibling; |
| 222 } |
| 223 return previousAncestorSiblingPostOrder(current, stayWithin); |
| 224 } |
| 225 |
194 bool ComposedTreeTraversal::isDescendantOf(const Node& node, const Node& other) | 226 bool ComposedTreeTraversal::isDescendantOf(const Node& node, const Node& other) |
195 { | 227 { |
196 assertPrecondition(node); | 228 assertPrecondition(node); |
197 assertPrecondition(other); | 229 assertPrecondition(other); |
198 if (!hasChildren(other) || node.inDocument() != other.inDocument()) | 230 if (!hasChildren(other) || node.inDocument() != other.inDocument()) |
199 return false; | 231 return false; |
200 for (const ContainerNode* n = traverseParent(node); n; n = traverseParent(*n
)) { | 232 for (const ContainerNode* n = traverseParent(node); n; n = traverseParent(*n
)) { |
201 if (n == other) | 233 if (n == other) |
202 return true; | 234 return true; |
203 } | 235 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 Node& ComposedTreeTraversal::lastWithinOrSelf(const Node& node) | 300 Node& ComposedTreeTraversal::lastWithinOrSelf(const Node& node) |
269 { | 301 { |
270 assertPrecondition(node); | 302 assertPrecondition(node); |
271 Node* lastDescendant = lastWithin(node); | 303 Node* lastDescendant = lastWithin(node); |
272 Node& result = lastDescendant ? *lastDescendant : const_cast<Node&>(node); | 304 Node& result = lastDescendant ? *lastDescendant : const_cast<Node&>(node); |
273 assertPostcondition(&result); | 305 assertPostcondition(&result); |
274 return result; | 306 return result; |
275 } | 307 } |
276 | 308 |
277 } // namespace | 309 } // namespace |
OLD | NEW |