Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(344)

Side by Side Diff: third_party/WebKit/Source/core/dom/NodeTraversal.cpp

Issue 1932523003: Introduce NodeTraversal::ancestorsOf() and inclusiveAncestors() for range-based for loop (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 2016-04-28T18:38:12 Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved. 5 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
6 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/) 6 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/)
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public 9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 } 42 }
43 43
44 Node* NodeTraversal::nextIncludingPseudo(const Node& current, const Node* stayWi thin) 44 Node* NodeTraversal::nextIncludingPseudo(const Node& current, const Node* stayWi thin)
45 { 45 {
46 if (Node* next = current.pseudoAwareFirstChild()) 46 if (Node* next = current.pseudoAwareFirstChild())
47 return next; 47 return next;
48 if (current == stayWithin) 48 if (current == stayWithin)
49 return 0; 49 return 0;
50 if (Node* next = current.pseudoAwareNextSibling()) 50 if (Node* next = current.pseudoAwareNextSibling())
51 return next; 51 return next;
52 for (Node* parent = current.parentNode(); parent; parent = parent->parentNod e()) { 52 for (Node& parent : ancestorsOf(current)) {
53 if (parent == stayWithin) 53 if (parent == stayWithin)
54 return 0; 54 return 0;
55 if (Node* next = parent->pseudoAwareNextSibling()) 55 if (Node* next = parent.pseudoAwareNextSibling())
56 return next; 56 return next;
57 } 57 }
58 return 0; 58 return 0;
59 } 59 }
60 60
61 Node* NodeTraversal::nextIncludingPseudoSkippingChildren(const Node& current, co nst Node* stayWithin) 61 Node* NodeTraversal::nextIncludingPseudoSkippingChildren(const Node& current, co nst Node* stayWithin)
62 { 62 {
63 if (current == stayWithin) 63 if (current == stayWithin)
64 return 0; 64 return 0;
65 if (Node* next = current.pseudoAwareNextSibling()) 65 if (Node* next = current.pseudoAwareNextSibling())
66 return next; 66 return next;
67 for (Node* parent = current.parentNode(); parent; parent = parent->parentNod e()) { 67 for (Node& parent : ancestorsOf(current)) {
68 if (parent == stayWithin) 68 if (parent == stayWithin)
69 return 0; 69 return 0;
70 if (Node* next = parent->pseudoAwareNextSibling()) 70 if (Node* next = parent.pseudoAwareNextSibling())
71 return next; 71 return next;
72 } 72 }
73 return 0; 73 return 0;
74 } 74 }
75 75
76 Node* NodeTraversal::nextAncestorSibling(const Node& current) 76 Node* NodeTraversal::nextAncestorSibling(const Node& current)
77 { 77 {
78 DCHECK(!current.nextSibling()); 78 DCHECK(!current.nextSibling());
79 for (Node* parent = current.parentNode(); parent; parent = parent->parentNod e()) { 79 for (Node& parent : ancestorsOf(current)) {
80 if (parent->nextSibling()) 80 if (parent.nextSibling())
81 return parent->nextSibling(); 81 return parent.nextSibling();
82 } 82 }
83 return 0; 83 return 0;
84 } 84 }
85 85
86 Node* NodeTraversal::nextAncestorSibling(const Node& current, const Node* stayWi thin) 86 Node* NodeTraversal::nextAncestorSibling(const Node& current, const Node* stayWi thin)
87 { 87 {
88 DCHECK(!current.nextSibling()); 88 DCHECK(!current.nextSibling());
89 DCHECK_NE(current, stayWithin); 89 DCHECK_NE(current, stayWithin);
90 for (Node* parent = current.parentNode(); parent; parent = parent->parentNod e()) { 90 for (Node& parent : ancestorsOf(current)) {
91 if (parent == stayWithin) 91 if (parent == stayWithin)
92 return 0; 92 return 0;
93 if (parent->nextSibling()) 93 if (parent.nextSibling())
94 return parent->nextSibling(); 94 return parent.nextSibling();
95 } 95 }
96 return 0; 96 return 0;
97 } 97 }
98 98
99 Node* NodeTraversal::lastWithin(const ContainerNode& current) 99 Node* NodeTraversal::lastWithin(const ContainerNode& current)
100 { 100 {
101 Node* descendant = current.lastChild(); 101 Node* descendant = current.lastChild();
102 for (Node* child = descendant; child; child = child->lastChild()) 102 for (Node* child = descendant; child; child = child->lastChild())
103 descendant = child; 103 descendant = child;
104 return descendant; 104 return descendant;
(...skipping 17 matching lines...) Expand all
122 } 122 }
123 return current.parentNode(); 123 return current.parentNode();
124 } 124 }
125 125
126 Node* NodeTraversal::previousSkippingChildren(const Node& current, const Node* s tayWithin) 126 Node* NodeTraversal::previousSkippingChildren(const Node& current, const Node* s tayWithin)
127 { 127 {
128 if (current == stayWithin) 128 if (current == stayWithin)
129 return 0; 129 return 0;
130 if (current.previousSibling()) 130 if (current.previousSibling())
131 return current.previousSibling(); 131 return current.previousSibling();
132 for (Node* parent = current.parentNode(); parent; parent = parent->parentNod e()) { 132 for (Node& parent : ancestorsOf(current)) {
133 if (parent == stayWithin) 133 if (parent == stayWithin)
134 return 0; 134 return 0;
135 if (parent->previousSibling()) 135 if (parent.previousSibling())
136 return parent->previousSibling(); 136 return parent.previousSibling();
137 } 137 }
138 return 0; 138 return 0;
139 } 139 }
140 140
141 Node* NodeTraversal::nextPostOrder(const Node& current, const Node* stayWithin) 141 Node* NodeTraversal::nextPostOrder(const Node& current, const Node* stayWithin)
142 { 142 {
143 if (current == stayWithin) 143 if (current == stayWithin)
144 return 0; 144 return 0;
145 if (!current.nextSibling()) 145 if (!current.nextSibling())
146 return current.parentNode(); 146 return current.parentNode();
147 Node* next = current.nextSibling(); 147 Node* next = current.nextSibling();
148 while (Node* child = next->firstChild()) 148 while (Node* child = next->firstChild())
149 next = child; 149 next = child;
150 return next; 150 return next;
151 } 151 }
152 152
153 static Node* previousAncestorSiblingPostOrder(const Node& current, const Node* s tayWithin) 153 static Node* previousAncestorSiblingPostOrder(const Node& current, const Node* s tayWithin)
154 { 154 {
155 DCHECK(!current.previousSibling()); 155 DCHECK(!current.previousSibling());
156 for (Node* parent = current.parentNode(); parent; parent = parent->parentNod e()) { 156 for (Node& parent : NodeTraversal::ancestorsOf(current)) {
157 if (parent == stayWithin) 157 if (parent == stayWithin)
158 return 0; 158 return 0;
159 if (parent->previousSibling()) 159 if (parent.previousSibling())
160 return parent->previousSibling(); 160 return parent.previousSibling();
161 } 161 }
162 return 0; 162 return 0;
163 } 163 }
164 164
165 Node* NodeTraversal::previousPostOrder(const Node& current, const Node* stayWith in) 165 Node* NodeTraversal::previousPostOrder(const Node& current, const Node* stayWith in)
166 { 166 {
167 if (Node* lastChild = current.lastChild()) 167 if (Node* lastChild = current.lastChild())
168 return lastChild; 168 return lastChild;
169 if (current == stayWithin) 169 if (current == stayWithin)
170 return 0; 170 return 0;
171 if (current.previousSibling()) 171 if (current.previousSibling())
172 return current.previousSibling(); 172 return current.previousSibling();
173 return previousAncestorSiblingPostOrder(current, stayWithin); 173 return previousAncestorSiblingPostOrder(current, stayWithin);
174 } 174 }
175 175
176 Node* NodeTraversal::commonAncestor(const Node& nodeA, const Node& nodeB) 176 Node* NodeTraversal::commonAncestor(const Node& nodeA, const Node& nodeB)
177 { 177 {
178 return Range::commonAncestorContainer(&nodeA, &nodeB); 178 return Range::commonAncestorContainer(&nodeA, &nodeB);
179 } 179 }
180 180
181 } // namespace blink 181 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/dom/NodeTraversal.h ('k') | third_party/WebKit/Source/core/dom/Range.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698