OLD | NEW |
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, 2013 Appl
e 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 * Copyright (C) 2014 Samsung Electronics. All rights reserved. |
8 * | 8 * |
9 * This library is free software; you can redistribute it and/or | 9 * This library is free software; you can redistribute it and/or |
10 * modify it under the terms of the GNU Library General Public | 10 * modify it under the terms of the GNU Library General Public |
11 * License as published by the Free Software Foundation; either | 11 * License as published by the Free Software Foundation; either |
12 * 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. |
13 * | 13 * |
14 * 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, |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 * Library General Public License for more details. | 17 * Library General Public License for more details. |
18 * | 18 * |
19 * 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 |
20 * 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 |
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
22 * Boston, MA 02110-1301, USA. | 22 * Boston, MA 02110-1301, USA. |
23 * | 23 * |
24 */ | 24 */ |
25 | 25 |
26 #ifndef ElementTraversal_h | 26 #ifndef ElementTraversal_h |
27 #define ElementTraversal_h | 27 #define ElementTraversal_h |
28 | 28 |
29 #include "core/dom/Element.h" | 29 #include "core/dom/Element.h" |
30 #include "core/dom/NodeTraversal.h" | 30 #include "core/dom/NodeTraversal.h" |
31 | 31 |
32 namespace WebCore { | 32 namespace WebCore { |
33 | 33 |
34 class ElementTraversal { | 34 template <class ElementType> |
| 35 class Traversal { |
35 public: | 36 public: |
36 // First / Last element child of the node. | 37 // First or last ElementType child of the node. |
37 static Element* firstWithin(const ContainerNode& current) { return firstElem
entWithinTemplate(current); } | 38 static ElementType* firstChild(const ContainerNode& current) { return firstC
hildTemplate(current); } |
38 static Element* firstWithin(const Node& current) { return firstElementWithin
Template(current); } | 39 static ElementType* firstChild(const Node& current) { return firstChildTempl
ate(current); } |
39 static Element* lastWithin(const ContainerNode& current) { return lastWithin
Template(current); } | 40 static ElementType* lastChild(const ContainerNode& current) { return lastChi
ldTemplate(current); } |
40 static Element* lastWithin(const Node& current) { return lastWithinTemplate(
current); } | 41 static ElementType* lastChild(const Node& current) { return lastChildTemplat
e(current); } |
| 42 |
| 43 // First or last ElementType descendant of the node. |
| 44 // For Elements firstWithin() is always the same as firstChild(). |
| 45 static ElementType* firstWithin(const ContainerNode& current) { return first
WithinTemplate(current); } |
| 46 static ElementType* firstWithin(const Node& current) { return firstWithinTem
plate(current); } |
| 47 static ElementType* lastWithin(const ContainerNode& current) { return lastWi
thinTemplate(current); } |
| 48 static ElementType* lastWithin(const Node& current) { return lastWithinTempl
ate(current); } |
41 | 49 |
42 // Pre-order traversal skipping non-element nodes. | 50 // Pre-order traversal skipping non-element nodes. |
43 static Element* next(const ContainerNode& current) { return traverseNextElem
entTemplate(current); } | 51 static ElementType* next(const ContainerNode& current) { return nextTemplate
(current); } |
44 static Element* next(const Node& current) { return traverseNextElementTempla
te(current); } | 52 static ElementType* next(const Node& current) { return nextTemplate(current)
; } |
45 static Element* next(const ContainerNode& current, const Node* stayWithin) {
return traverseNextElementTemplate(current, stayWithin); } | 53 static ElementType* next(const ContainerNode& current, const Node* stayWithi
n) { return nextTemplate(current, stayWithin); } |
46 static Element* next(const Node& current, const Node* stayWithin) { return t
raverseNextElementTemplate(current, stayWithin); } | 54 static ElementType* next(const Node& current, const Node* stayWithin) { retu
rn nextTemplate(current, stayWithin); } |
| 55 static ElementType* previous(const ContainerNode& current) { return previous
Template(current); } |
| 56 static ElementType* previous(const Node& current) { return previousTemplate(
current); } |
| 57 static ElementType* previous(const ContainerNode& current, const Node* stayW
ithin) { return previousTemplate(current, stayWithin); } |
| 58 static ElementType* previous(const Node& current, const Node* stayWithin) {
return previousTemplate(current, stayWithin); } |
47 | 59 |
48 // Like next, but skips children. | 60 // Like next, but skips children. |
49 static Element* nextSkippingChildren(const ContainerNode& current) { return
traverseNextElementSkippingChildrenTemplate(current); } | 61 static ElementType* nextSkippingChildren(const ContainerNode& current) { ret
urn nextSkippingChildrenTemplate(current); } |
50 static Element* nextSkippingChildren(const Node& current) { return traverseN
extElementSkippingChildrenTemplate(current); } | 62 static ElementType* nextSkippingChildren(const Node& current) { return nextS
kippingChildrenTemplate(current); } |
51 static Element* nextSkippingChildren(const ContainerNode& current, const Nod
e* stayWithin) { return traverseNextElementSkippingChildrenTemplate(current, sta
yWithin); } | 63 static ElementType* nextSkippingChildren(const ContainerNode& current, const
Node* stayWithin) { return nextSkippingChildrenTemplate(current, stayWithin); } |
52 static Element* nextSkippingChildren(const Node& current, const Node* stayWi
thin) { return traverseNextElementSkippingChildrenTemplate(current, stayWithin);
} | 64 static ElementType* nextSkippingChildren(const Node& current, const Node* st
ayWithin) { return nextSkippingChildrenTemplate(current, stayWithin); } |
53 | 65 |
54 // Pre-order traversal including the pseudo-elements. | 66 // Pre-order traversal including the pseudo-elements. |
55 static Element* previousIncludingPseudo(const Node&, const Node* stayWithin
= 0); | 67 static ElementType* previousIncludingPseudo(const Node&, const Node* stayWit
hin = 0); |
56 static Element* nextIncludingPseudo(const Node&, const Node* stayWithin = 0)
; | 68 static ElementType* nextIncludingPseudo(const Node&, const Node* stayWithin
= 0); |
57 static Element* nextIncludingPseudoSkippingChildren(const Node&, const Node*
stayWithin = 0); | 69 static ElementType* nextIncludingPseudoSkippingChildren(const Node&, const N
ode* stayWithin = 0); |
58 | 70 |
59 // Utility function to traverse only the element and pseudo-element siblings
of a node. | 71 // Utility function to traverse only the element and pseudo-element siblings
of a node. |
60 static Element* pseudoAwarePreviousSibling(const Node&); | 72 static ElementType* pseudoAwarePreviousSibling(const Node&); |
61 | 73 |
62 // Previous / Next sibling. | 74 // Previous / Next sibling. |
63 static Element* previousSibling(const Node&); | 75 static ElementType* previousSibling(const Node&); |
64 static Element* nextSibling(const Node&); | 76 static ElementType* nextSibling(const Node&); |
65 | 77 |
66 private: | 78 private: |
67 template <class NodeType> | 79 template <class NodeType> |
68 static Element* firstElementWithinTemplate(NodeType&); | 80 static ElementType* firstChildTemplate(NodeType&); |
69 template <class NodeType> | 81 template <class NodeType> |
70 static Element* lastWithinTemplate(NodeType&); | 82 static ElementType* lastChildTemplate(NodeType&); |
71 template <class NodeType> | 83 template <class NodeType> |
72 static Element* traverseNextElementTemplate(NodeType&); | 84 static ElementType* firstWithinTemplate(NodeType&); |
73 template <class NodeType> | 85 template <class NodeType> |
74 static Element* traverseNextElementTemplate(NodeType&, const Node* stayWithi
n); | 86 static ElementType* lastWithinTemplate(NodeType&); |
75 template <class NodeType> | 87 template <class NodeType> |
76 static Element* traverseNextElementSkippingChildrenTemplate(NodeType&); | 88 static ElementType* nextTemplate(NodeType&); |
77 template <class NodeType> | 89 template <class NodeType> |
78 static Element* traverseNextElementSkippingChildrenTemplate(NodeType&, const
Node* stayWithin); | 90 static ElementType* nextTemplate(NodeType&, const Node* stayWithin); |
| 91 template <class NodeType> |
| 92 static ElementType* previousTemplate(NodeType&); |
| 93 template <class NodeType> |
| 94 static ElementType* previousTemplate(NodeType&, const Node* stayWithin); |
| 95 template <class NodeType> |
| 96 static ElementType* nextSkippingChildrenTemplate(NodeType&); |
| 97 template <class NodeType> |
| 98 static ElementType* nextSkippingChildrenTemplate(NodeType&, const Node* stay
Within); |
79 }; | 99 }; |
80 | 100 |
| 101 typedef Traversal<Element> ElementTraversal; |
| 102 |
| 103 // Specialized for pure Element to exploit the fact that Elements parent is alwa
ys either another Element or the root. |
| 104 template <> |
81 template <class NodeType> | 105 template <class NodeType> |
82 inline Element* ElementTraversal::firstElementWithinTemplate(NodeType& current) | 106 inline Element* Traversal<Element>::firstWithinTemplate(NodeType& current) |
83 { | 107 { |
84 // Except for the root containers, only elements can have element children. | 108 return firstChildTemplate(current); |
85 Node* node = current.firstChild(); | |
86 while (node && !node->isElementNode()) | |
87 node = node->nextSibling(); | |
88 return toElement(node); | |
89 } | 109 } |
90 | 110 |
| 111 template <> |
91 template <class NodeType> | 112 template <class NodeType> |
92 inline Element* ElementTraversal::lastWithinTemplate(NodeType& current) | 113 inline Element* Traversal<Element>::lastWithinTemplate(NodeType& current) |
93 { | 114 { |
94 Node* node = current.lastChild(); | 115 return lastChildTemplate(current); |
95 while (node && !node->isElementNode()) | |
96 node = node->previousSibling(); | |
97 return toElement(node); | |
98 } | 116 } |
99 | 117 |
| 118 template <> |
100 template <class NodeType> | 119 template <class NodeType> |
101 inline Element* ElementTraversal::traverseNextElementTemplate(NodeType& current) | 120 inline Element* Traversal<Element>::nextTemplate(NodeType& current) |
102 { | 121 { |
103 Node* node = NodeTraversal::next(current); | 122 Node* node = NodeTraversal::next(current); |
104 while (node && !node->isElementNode()) | 123 while (node && !node->isElementNode()) |
105 node = NodeTraversal::nextSkippingChildren(*node); | 124 node = NodeTraversal::nextSkippingChildren(*node); |
106 return toElement(node); | 125 return toElement(node); |
107 } | 126 } |
108 | 127 |
| 128 template <> |
109 template <class NodeType> | 129 template <class NodeType> |
110 inline Element* ElementTraversal::traverseNextElementTemplate(NodeType& current,
const Node* stayWithin) | 130 inline Element* Traversal<Element>::nextTemplate(NodeType& current, const Node*
stayWithin) |
111 { | 131 { |
112 Node* node = NodeTraversal::next(current, stayWithin); | 132 Node* node = NodeTraversal::next(current, stayWithin); |
113 while (node && !node->isElementNode()) | 133 while (node && !node->isElementNode()) |
114 node = NodeTraversal::nextSkippingChildren(*node, stayWithin); | 134 node = NodeTraversal::nextSkippingChildren(*node, stayWithin); |
115 return toElement(node); | 135 return toElement(node); |
116 } | 136 } |
117 | 137 |
| 138 template <> |
118 template <class NodeType> | 139 template <class NodeType> |
119 inline Element* ElementTraversal::traverseNextElementSkippingChildrenTemplate(No
deType& current) | 140 inline Element* Traversal<Element>::previousTemplate(NodeType& current) |
120 { | 141 { |
121 Node* node = NodeTraversal::nextSkippingChildren(current); | 142 Node* node = NodeTraversal::previous(current); |
122 while (node && !node->isElementNode()) | 143 while (node && !node->isElementNode()) |
123 node = NodeTraversal::nextSkippingChildren(*node); | 144 node = NodeTraversal::previous(*node); |
124 return toElement(node); | 145 return toElement(node); |
125 } | 146 } |
126 | 147 |
| 148 template <> |
127 template <class NodeType> | 149 template <class NodeType> |
128 inline Element* ElementTraversal::traverseNextElementSkippingChildrenTemplate(No
deType& current, const Node* stayWithin) | 150 inline Element* Traversal<Element>::previousTemplate(NodeType& current, const No
de* stayWithin) |
129 { | 151 { |
130 Node* node = NodeTraversal::nextSkippingChildren(current, stayWithin); | 152 Node* node = NodeTraversal::previous(current, stayWithin); |
131 while (node && !node->isElementNode()) | 153 while (node && !node->isElementNode()) |
132 node = NodeTraversal::nextSkippingChildren(*node, stayWithin); | 154 node = NodeTraversal::previous(*node, stayWithin); |
133 return toElement(node); | 155 return toElement(node); |
134 } | 156 } |
135 | 157 |
136 inline Element* ElementTraversal::previousIncludingPseudo(const Node& current, c
onst Node* stayWithin) | 158 // Generic versions. |
| 159 template <class ElementType> |
| 160 template <class NodeType> |
| 161 inline ElementType* Traversal<ElementType>::firstChildTemplate(NodeType& current
) |
| 162 { |
| 163 Node* node = current.firstChild(); |
| 164 while (node && !isElementOfType<const ElementType>(*node)) |
| 165 node = node->nextSibling(); |
| 166 return toElement<ElementType>(node); |
| 167 } |
| 168 |
| 169 template <class ElementType> |
| 170 template <class NodeType> |
| 171 inline ElementType* Traversal<ElementType>::lastChildTemplate(NodeType& current) |
| 172 { |
| 173 Node* node = current.lastChild(); |
| 174 while (node && !isElementOfType<const ElementType>(*node)) |
| 175 node = node->previousSibling(); |
| 176 return toElement<ElementType>(node); |
| 177 } |
| 178 |
| 179 template <class ElementType> |
| 180 template <class NodeType> |
| 181 inline ElementType* Traversal<ElementType>::firstWithinTemplate(NodeType& curren
t) |
| 182 { |
| 183 Element* element = Traversal<Element>::firstWithin(current); |
| 184 while (element && !isElementOfType<const ElementType>(*element)) |
| 185 element = Traversal<Element>::next(*element, ¤t); |
| 186 return toElement<ElementType>(element); |
| 187 } |
| 188 |
| 189 template <class ElementType> |
| 190 template <class NodeType> |
| 191 inline ElementType* Traversal<ElementType>::lastWithinTemplate(NodeType& current
) |
| 192 { |
| 193 Element* element = Traversal<Element>::lastWithin(current); |
| 194 while (element && !isElementOfType<const ElementType>(*element)) |
| 195 element = Traversal<Element>::previous(element, ¤t); |
| 196 return toElement<ElementType>(element); |
| 197 } |
| 198 |
| 199 template <class ElementType> |
| 200 template <class NodeType> |
| 201 inline ElementType* Traversal<ElementType>::nextTemplate(NodeType& current) |
| 202 { |
| 203 Element* element = Traversal<Element>::next(current); |
| 204 while (element && !isElementOfType<const ElementType>(*element)) |
| 205 element = Traversal<Element>::next(*element); |
| 206 return toElement<ElementType>(element); |
| 207 } |
| 208 |
| 209 template <class ElementType> |
| 210 template <class NodeType> |
| 211 inline ElementType* Traversal<ElementType>::nextTemplate(NodeType& current, cons
t Node* stayWithin) |
| 212 { |
| 213 Element* element = Traversal<Element>::next(current, stayWithin); |
| 214 while (element && !isElementOfType<const ElementType>(*element)) |
| 215 element = Traversal<Element>::next(*element, stayWithin); |
| 216 return toElement<ElementType>(element); |
| 217 } |
| 218 |
| 219 template <class ElementType> |
| 220 template <class NodeType> |
| 221 inline ElementType* Traversal<ElementType>::previousTemplate(NodeType& current) |
| 222 { |
| 223 Element* element = Traversal<Element>::previous(current); |
| 224 while (element && !isElementOfType<const ElementType>(*element)) |
| 225 element = Traversal<Element>::previous(*element); |
| 226 return toElement<ElementType>(element); |
| 227 } |
| 228 |
| 229 template <class ElementType> |
| 230 template <class NodeType> |
| 231 inline ElementType* Traversal<ElementType>::previousTemplate(NodeType& current,
const Node* stayWithin) |
| 232 { |
| 233 Element* element = Traversal<Element>::previous(current, stayWithin); |
| 234 while (element && !isElementOfType<const ElementType>(*element)) |
| 235 element = Traversal<Element>::previous(*element, stayWithin); |
| 236 return toElement<ElementType>(element); |
| 237 } |
| 238 |
| 239 template <class ElementType> |
| 240 template <class NodeType> |
| 241 inline ElementType* Traversal<ElementType>::nextSkippingChildrenTemplate(NodeTyp
e& current) |
| 242 { |
| 243 Node* node = NodeTraversal::nextSkippingChildren(current); |
| 244 while (node && !isElementOfType<const ElementType>(*node)) |
| 245 node = NodeTraversal::nextSkippingChildren(*node); |
| 246 return toElement<ElementType>(node); |
| 247 } |
| 248 |
| 249 template <class ElementType> |
| 250 template <class NodeType> |
| 251 inline ElementType* Traversal<ElementType>::nextSkippingChildrenTemplate(NodeTyp
e& current, const Node* stayWithin) |
| 252 { |
| 253 Node* node = NodeTraversal::nextSkippingChildren(current, stayWithin); |
| 254 while (node && !isElementOfType<const ElementType>(*node)) |
| 255 node = NodeTraversal::nextSkippingChildren(*node, stayWithin); |
| 256 return toElement<ElementType>(node); |
| 257 } |
| 258 |
| 259 template <class ElementType> |
| 260 inline ElementType* Traversal<ElementType>::previousIncludingPseudo(const Node&
current, const Node* stayWithin) |
137 { | 261 { |
138 Node* node = NodeTraversal::previousIncludingPseudo(current, stayWithin); | 262 Node* node = NodeTraversal::previousIncludingPseudo(current, stayWithin); |
139 while (node && !node->isElementNode()) | 263 while (node && !isElementOfType<const ElementType>(*node)) |
140 node = NodeTraversal::previousIncludingPseudo(*node, stayWithin); | 264 node = NodeTraversal::previousIncludingPseudo(*node, stayWithin); |
141 return toElement(node); | 265 return toElement<ElementType>(node); |
142 } | 266 } |
143 | 267 |
144 inline Element* ElementTraversal::nextIncludingPseudo(const Node& current, const
Node* stayWithin) | 268 template <class ElementType> |
| 269 inline ElementType* Traversal<ElementType>::nextIncludingPseudo(const Node& curr
ent, const Node* stayWithin) |
145 { | 270 { |
146 Node* node = NodeTraversal::nextIncludingPseudo(current, stayWithin); | 271 Node* node = NodeTraversal::nextIncludingPseudo(current, stayWithin); |
147 while (node && !node->isElementNode()) | 272 while (node && !isElementOfType<const ElementType>(*node)) |
148 node = NodeTraversal::nextIncludingPseudo(*node, stayWithin); | 273 node = NodeTraversal::nextIncludingPseudo(*node, stayWithin); |
149 return toElement(node); | 274 return toElement<ElementType>(node); |
150 } | 275 } |
151 | 276 |
152 inline Element* ElementTraversal::nextIncludingPseudoSkippingChildren(const Node
& current, const Node* stayWithin) | 277 template <class ElementType> |
| 278 inline ElementType* Traversal<ElementType>::nextIncludingPseudoSkippingChildren(
const Node& current, const Node* stayWithin) |
153 { | 279 { |
154 Node* node = NodeTraversal::nextIncludingPseudoSkippingChildren(current, sta
yWithin); | 280 Node* node = NodeTraversal::nextIncludingPseudoSkippingChildren(current, sta
yWithin); |
155 while (node && !node->isElementNode()) | 281 while (node && !isElementOfType<const ElementType>(*node)) |
156 node = NodeTraversal::nextIncludingPseudoSkippingChildren(*node, stayWit
hin); | 282 node = NodeTraversal::nextIncludingPseudoSkippingChildren(*node, stayWit
hin); |
157 return toElement(node); | 283 return toElement<ElementType>(node); |
158 } | 284 } |
159 | 285 |
160 inline Element* ElementTraversal::pseudoAwarePreviousSibling(const Node& current
) | 286 template <class ElementType> |
| 287 inline ElementType* Traversal<ElementType>::pseudoAwarePreviousSibling(const Nod
e& current) |
161 { | 288 { |
162 Node* node = current.pseudoAwarePreviousSibling(); | 289 Node* node = current.pseudoAwarePreviousSibling(); |
163 while (node && !node->isElementNode()) | 290 while (node && !isElementOfType<const ElementType>(*node)) |
164 node = node->pseudoAwarePreviousSibling(); | 291 node = node->pseudoAwarePreviousSibling(); |
165 return toElement(node); | 292 return toElement<ElementType>(node); |
166 } | 293 } |
167 | 294 |
168 inline Element* ElementTraversal::previousSibling(const Node& current) | 295 template <class ElementType> |
| 296 inline ElementType* Traversal<ElementType>::previousSibling(const Node& current) |
169 { | 297 { |
170 Node* node = current.previousSibling(); | 298 Node* node = current.previousSibling(); |
171 while (node && !node->isElementNode()) | 299 while (node && !isElementOfType<const ElementType>(*node)) |
172 node = node->previousSibling(); | 300 node = node->previousSibling(); |
173 return toElement(node); | 301 return toElement<ElementType>(node); |
174 } | 302 } |
175 | 303 |
176 inline Element* ElementTraversal::nextSibling(const Node& current) | 304 template <class ElementType> |
| 305 inline ElementType* Traversal<ElementType>::nextSibling(const Node& current) |
177 { | 306 { |
178 Node* node = current.nextSibling(); | 307 Node* node = current.nextSibling(); |
179 while (node && !node->isElementNode()) | 308 while (node && !isElementOfType<const ElementType>(*node)) |
180 node = node->nextSibling(); | 309 node = node->nextSibling(); |
181 return toElement(node); | 310 return toElement<ElementType>(node); |
182 } | 311 } |
183 | 312 |
184 } // namespace WebCore | 313 } // namespace WebCore |
185 | 314 |
186 #endif | 315 #endif |
OLD | NEW |