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

Side by Side Diff: Source/core/dom/ElementTraversal.h

Issue 172313003: Make ElementTraversal / NodeTraversal classes for clarity (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | Source/core/dom/NodeTraversal.h » ('j') | Source/core/dom/NodeTraversal.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
7 * 8 *
8 * This library is free software; you can redistribute it and/or 9 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public 10 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either 11 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version. 12 * version 2 of the License, or (at your option) any later version.
12 * 13 *
13 * This library is distributed in the hope that it will be useful, 14 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details. 17 * Library General Public License for more details.
17 * 18 *
18 * You should have received a copy of the GNU Library General Public License 19 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to 20 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA. 22 * Boston, MA 02110-1301, USA.
22 * 23 *
23 */ 24 */
24 25
25 #ifndef ElementTraversal_h 26 #ifndef ElementTraversal_h
26 #define ElementTraversal_h 27 #define ElementTraversal_h
27 28
28 #include "core/dom/Element.h" 29 #include "core/dom/Element.h"
29 #include "core/dom/NodeTraversal.h" 30 #include "core/dom/NodeTraversal.h"
30 31
31 namespace WebCore { 32 namespace WebCore {
32 33
33 namespace ElementTraversal { 34 class ElementTraversal {
35 public:
36 // First / Last element child of the node.
37 static inline Element* firstWithin(const ContainerNode& current) { return fi rstElementWithinTemplate(current); }
adamk 2014/02/19 17:23:04 Aren't these 'inline' keywords redundant? My under
38 static inline Element* firstWithin(const Node& current) { return firstElemen tWithinTemplate(current); }
39 static inline Element* lastWithin(const ContainerNode& current) { return las tWithinTemplate(current); }
40 static inline Element* lastWithin(const Node& current) { return lastWithinTe mplate(current); }
34 41
35 // First / Last element child of the node. 42 // Pre-order traversal skipping non-element nodes.
36 Element* firstWithin(const Node&); 43 static inline Element* next(const ContainerNode& current) { return traverseN extElementTemplate(current); }
37 Element* firstWithin(const ContainerNode&); 44 static inline Element* next(const Node& current) { return traverseNextElemen tTemplate(current); }
38 Element* lastWithin(const Node&); 45 static inline Element* next(const ContainerNode& current, const Node* stayWi thin) { return traverseNextElementTemplate(current, stayWithin); }
39 Element* lastWithin(const ContainerNode&); 46 static inline Element* next(const Node& current, const Node* stayWithin) { r eturn traverseNextElementTemplate(current, stayWithin); }
40 47
41 // Pre-order traversal skipping non-element nodes. 48 // Like next, but skips children.
42 Element* next(const Node&); 49 static inline Element* nextSkippingChildren(const ContainerNode& current) { return traverseNextElementSkippingChildrenTemplate(current); }
43 Element* next(const Node&, const Node* stayWithin); 50 static inline Element* nextSkippingChildren(const Node& current) { return tr averseNextElementSkippingChildrenTemplate(current); }
44 Element* next(const ContainerNode&); 51 static inline Element* nextSkippingChildren(const ContainerNode& current, co nst Node* stayWithin) { return traverseNextElementSkippingChildrenTemplate(curre nt, stayWithin); }
45 Element* next(const ContainerNode&, const Node* stayWithin); 52 static inline Element* nextSkippingChildren(const Node& current, const Node* stayWithin) { return traverseNextElementSkippingChildrenTemplate(current, stayW ithin); }
46 53
47 // Like next, but skips children. 54 // Pre-order traversal including the pseudo-elements.
48 Element* nextSkippingChildren(const Node&); 55 static Element* previousIncludingPseudo(const Node&, const Node* stayWithin = 0);
49 Element* nextSkippingChildren(const Node&, const Node* stayWithin); 56 static Element* nextIncludingPseudo(const Node&, const Node* stayWithin = 0) ;
50 Element* nextSkippingChildren(const ContainerNode&); 57 static Element* nextIncludingPseudoSkippingChildren(const Node&, const Node* stayWithin = 0);
51 Element* nextSkippingChildren(const ContainerNode&, const Node* stayWithin);
52 58
53 // Pre-order traversal including the pseudo-elements. 59 // Utility function to traverse only the element and pseudo-element siblings of a node.
54 Element* previousIncludingPseudo(const Node&, const Node* stayWithin = 0); 60 static Element* pseudoAwarePreviousSibling(const Node&);
55 Element* nextIncludingPseudo(const Node&, const Node* stayWithin = 0);
56 Element* nextIncludingPseudoSkippingChildren(const Node&, const Node* stayWithin = 0);
57 61
58 // Utility function to traverse only the element and pseudo-element siblings of a node. 62 // Previous / Next sibling.
59 Element* pseudoAwarePreviousSibling(const Node&); 63 static Element* previousSibling(const Node&);
64 static Element* nextSibling(const Node&);
60 65
61 // Previous / Next sibling. 66 private:
62 Element* previousSibling(const Node&); 67 template <class NodeType>
63 Element* nextSibling(const Node&); 68 static Element* firstElementWithinTemplate(NodeType&);
69 template <class NodeType>
70 static Element* lastWithinTemplate(NodeType&);
71 template <class NodeType>
72 static Element* traverseNextElementTemplate(NodeType&);
73 template <class NodeType>
74 static Element* traverseNextElementTemplate(NodeType&, const Node* stayWithi n);
75 template <class NodeType>
76 static Element* traverseNextElementSkippingChildrenTemplate(NodeType&);
77 template <class NodeType>
78 static Element* traverseNextElementSkippingChildrenTemplate(NodeType&, const Node* stayWithin);
79 };
64 80
65 template <class NodeType> 81 template <class NodeType>
66 inline Element* firstElementWithinTemplate(NodeType& current) 82 inline Element* ElementTraversal::firstElementWithinTemplate(NodeType& current)
67 { 83 {
68 // Except for the root containers, only elements can have element children. 84 // Except for the root containers, only elements can have element children.
69 Node* node = current.firstChild(); 85 Node* node = current.firstChild();
70 while (node && !node->isElementNode()) 86 while (node && !node->isElementNode())
71 node = node->nextSibling(); 87 node = node->nextSibling();
72 return toElement(node); 88 return toElement(node);
73 } 89 }
74 inline Element* firstWithin(const ContainerNode& current) { return firstElementW ithinTemplate(current); }
75 inline Element* firstWithin(const Node& current) { return firstElementWithinTemp late(current); }
76 90
77 template <class NodeType> 91 template <class NodeType>
78 inline Element* lastWithinTemplate(NodeType& current) 92 inline Element* ElementTraversal::lastWithinTemplate(NodeType& current)
79 { 93 {
80 Node* node = current.lastChild(); 94 Node* node = current.lastChild();
81 while (node && !node->isElementNode()) 95 while (node && !node->isElementNode())
82 node = node->previousSibling(); 96 node = node->previousSibling();
83 return toElement(node); 97 return toElement(node);
84 } 98 }
85 inline Element* lastWithin(const ContainerNode& current) { return lastWithinTemp late(current); }
86 inline Element* lastWithin(const Node& current) { return lastWithinTemplate(curr ent); }
87 99
88 template <class NodeType> 100 template <class NodeType>
89 inline Element* traverseNextElementTemplate(NodeType& current) 101 inline Element* ElementTraversal::traverseNextElementTemplate(NodeType& current)
90 { 102 {
91 Node* node = NodeTraversal::next(current); 103 Node* node = NodeTraversal::next(current);
92 while (node && !node->isElementNode()) 104 while (node && !node->isElementNode())
93 node = NodeTraversal::nextSkippingChildren(*node); 105 node = NodeTraversal::nextSkippingChildren(*node);
94 return toElement(node); 106 return toElement(node);
95 } 107 }
96 inline Element* next(const ContainerNode& current) { return traverseNextElementT emplate(current); }
97 inline Element* next(const Node& current) { return traverseNextElementTemplate(c urrent); }
98 108
99 template <class NodeType> 109 template <class NodeType>
100 inline Element* traverseNextElementTemplate(NodeType& current, const Node* stayW ithin) 110 inline Element* ElementTraversal::traverseNextElementTemplate(NodeType& current, const Node* stayWithin)
101 { 111 {
102 Node* node = NodeTraversal::next(current, stayWithin); 112 Node* node = NodeTraversal::next(current, stayWithin);
103 while (node && !node->isElementNode()) 113 while (node && !node->isElementNode())
104 node = NodeTraversal::nextSkippingChildren(*node, stayWithin); 114 node = NodeTraversal::nextSkippingChildren(*node, stayWithin);
105 return toElement(node); 115 return toElement(node);
106 } 116 }
107 inline Element* next(const ContainerNode& current, const Node* stayWithin) { ret urn traverseNextElementTemplate(current, stayWithin); }
108 inline Element* next(const Node& current, const Node* stayWithin) { return trave rseNextElementTemplate(current, stayWithin); }
109 117
110 template <class NodeType> 118 template <class NodeType>
111 inline Element* traverseNextElementSkippingChildrenTemplate(NodeType& current) 119 inline Element* ElementTraversal::traverseNextElementSkippingChildrenTemplate(No deType& current)
112 { 120 {
113 Node* node = NodeTraversal::nextSkippingChildren(current); 121 Node* node = NodeTraversal::nextSkippingChildren(current);
114 while (node && !node->isElementNode()) 122 while (node && !node->isElementNode())
115 node = NodeTraversal::nextSkippingChildren(*node); 123 node = NodeTraversal::nextSkippingChildren(*node);
116 return toElement(node); 124 return toElement(node);
117 } 125 }
118 inline Element* nextSkippingChildren(const ContainerNode& current) { return trav erseNextElementSkippingChildrenTemplate(current); }
119 inline Element* nextSkippingChildren(const Node& current) { return traverseNextE lementSkippingChildrenTemplate(current); }
120 126
121 template <class NodeType> 127 template <class NodeType>
122 inline Element* traverseNextElementSkippingChildrenTemplate(NodeType& current, c onst Node* stayWithin) 128 inline Element* ElementTraversal::traverseNextElementSkippingChildrenTemplate(No deType& current, const Node* stayWithin)
123 { 129 {
124 Node* node = NodeTraversal::nextSkippingChildren(current, stayWithin); 130 Node* node = NodeTraversal::nextSkippingChildren(current, stayWithin);
125 while (node && !node->isElementNode()) 131 while (node && !node->isElementNode())
126 node = NodeTraversal::nextSkippingChildren(*node, stayWithin); 132 node = NodeTraversal::nextSkippingChildren(*node, stayWithin);
127 return toElement(node); 133 return toElement(node);
128 } 134 }
129 inline Element* nextSkippingChildren(const ContainerNode& current, const Node* s tayWithin) { return traverseNextElementSkippingChildrenTemplate(current, stayWit hin); }
130 inline Element* nextSkippingChildren(const Node& current, const Node* stayWithin ) { return traverseNextElementSkippingChildrenTemplate(current, stayWithin); }
131 135
132 inline Element* previousIncludingPseudo(const Node& current, const Node* stayWit hin) 136 inline Element* ElementTraversal::previousIncludingPseudo(const Node& current, c onst Node* stayWithin)
133 { 137 {
134 Node* node = NodeTraversal::previousIncludingPseudo(current, stayWithin); 138 Node* node = NodeTraversal::previousIncludingPseudo(current, stayWithin);
135 while (node && !node->isElementNode()) 139 while (node && !node->isElementNode())
136 node = NodeTraversal::previousIncludingPseudo(*node, stayWithin); 140 node = NodeTraversal::previousIncludingPseudo(*node, stayWithin);
137 return toElement(node); 141 return toElement(node);
138 } 142 }
139 143
140 inline Element* nextIncludingPseudo(const Node& current, const Node* stayWithin) 144 inline Element* ElementTraversal::nextIncludingPseudo(const Node& current, const Node* stayWithin)
141 { 145 {
142 Node* node = NodeTraversal::nextIncludingPseudo(current, stayWithin); 146 Node* node = NodeTraversal::nextIncludingPseudo(current, stayWithin);
143 while (node && !node->isElementNode()) 147 while (node && !node->isElementNode())
144 node = NodeTraversal::nextIncludingPseudo(*node, stayWithin); 148 node = NodeTraversal::nextIncludingPseudo(*node, stayWithin);
145 return toElement(node); 149 return toElement(node);
146 } 150 }
147 151
148 inline Element* nextIncludingPseudoSkippingChildren(const Node& current, const N ode* stayWithin) 152 inline Element* ElementTraversal::nextIncludingPseudoSkippingChildren(const Node & current, const Node* stayWithin)
149 { 153 {
150 Node* node = NodeTraversal::nextIncludingPseudoSkippingChildren(current, sta yWithin); 154 Node* node = NodeTraversal::nextIncludingPseudoSkippingChildren(current, sta yWithin);
151 while (node && !node->isElementNode()) 155 while (node && !node->isElementNode())
152 node = NodeTraversal::nextIncludingPseudoSkippingChildren(*node, stayWit hin); 156 node = NodeTraversal::nextIncludingPseudoSkippingChildren(*node, stayWit hin);
153 return toElement(node); 157 return toElement(node);
154 } 158 }
155 159
156 inline Element* pseudoAwarePreviousSibling(const Node& current) 160 inline Element* ElementTraversal::pseudoAwarePreviousSibling(const Node& current )
157 { 161 {
158 Node* node = current.pseudoAwarePreviousSibling(); 162 Node* node = current.pseudoAwarePreviousSibling();
159 while (node && !node->isElementNode()) 163 while (node && !node->isElementNode())
160 node = node->pseudoAwarePreviousSibling(); 164 node = node->pseudoAwarePreviousSibling();
161 return toElement(node); 165 return toElement(node);
162 } 166 }
163 167
164 inline Element* previousSibling(const Node& current) 168 inline Element* ElementTraversal::previousSibling(const Node& current)
165 { 169 {
166 Node* node = current.previousSibling(); 170 Node* node = current.previousSibling();
167 while (node && !node->isElementNode()) 171 while (node && !node->isElementNode())
168 node = node->previousSibling(); 172 node = node->previousSibling();
169 return toElement(node); 173 return toElement(node);
170 } 174 }
171 175
172 inline Element* nextSibling(const Node& current) 176 inline Element* ElementTraversal::nextSibling(const Node& current)
173 { 177 {
174 Node* node = current.nextSibling(); 178 Node* node = current.nextSibling();
175 while (node && !node->isElementNode()) 179 while (node && !node->isElementNode())
176 node = node->nextSibling(); 180 node = node->nextSibling();
177 return toElement(node); 181 return toElement(node);
178 } 182 }
179 183
180 } 184 } // namespace WebCore
181
182 }
183 185
184 #endif 186 #endif
OLDNEW
« no previous file with comments | « no previous file | Source/core/dom/NodeTraversal.h » ('j') | Source/core/dom/NodeTraversal.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698