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

Side by Side Diff: Source/core/dom/NodeTraversal.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
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 NodeTraversal_h 26 #ifndef NodeTraversal_h
26 #define NodeTraversal_h 27 #define NodeTraversal_h
27 28
28 #include "core/dom/Node.h" 29 #include "core/dom/Node.h"
29 30
30 namespace WebCore { 31 namespace WebCore {
31 32
32 namespace NodeTraversal { 33 class NodeTraversal {
34 public:
35 // Does a pre-order traversal of the tree to find the next node after this o ne.
36 // This uses the same order that tags appear in the source file. If the stay Within
37 // argument is non-null, the traversal will stop once the specified node is reached.
38 // This can be used to restrict traversal to a particular sub-tree.
39 static inline Node* next(const Node& current) { return traverseNextTemplate( current); }
adamk 2014/02/19 17:23:04 ditto on inline keywords
40 static inline Node* next(const ContainerNode& current) { return traverseNext Template(current); }
41 static inline Node* next(const Node& current, const Node* stayWithin) { retu rn traverseNextTemplate(current, stayWithin); }
42 static inline Node* next(const ContainerNode& current, const Node* stayWithi n) { return traverseNextTemplate(current, stayWithin); }
33 43
34 // Does a pre-order traversal of the tree to find the next node after this one. 44 // Like next, but skips children and starts with the next sibling.
35 // This uses the same order that tags appear in the source file. If the stayWith in 45 static inline Node* nextSkippingChildren(const Node& current) { return trave rseNextSkippingChildrenTemplate(current); }
36 // argument is non-null, the traversal will stop once the specified node is reac hed. 46 static inline Node* nextSkippingChildren(const ContainerNode& current) { ret urn traverseNextSkippingChildrenTemplate(current); }
37 // This can be used to restrict traversal to a particular sub-tree. 47 static inline Node* nextSkippingChildren(const Node& current, const Node* st ayWithin) { return traverseNextSkippingChildrenTemplate(current, stayWithin); }
38 Node* next(const Node&); 48 static inline Node* nextSkippingChildren(const ContainerNode& current, const Node* stayWithin) { return traverseNextSkippingChildrenTemplate(current, stayWi thin); }
39 Node* next(const Node&, const Node* stayWithin);
40 Node* next(const ContainerNode&);
41 Node* next(const ContainerNode&, const Node* stayWithin);
42 49
43 // Like next, but skips children and starts with the next sibling. 50 // Does a reverse pre-order traversal to find the node that comes before the current one in document order
44 Node* nextSkippingChildren(const Node&); 51 static Node* previous(const Node&, const Node* stayWithin = 0);
45 Node* nextSkippingChildren(const Node&, const Node* stayWithin);
46 Node* nextSkippingChildren(const ContainerNode&);
47 Node* nextSkippingChildren(const ContainerNode&, const Node* stayWithin);
48 52
49 // Does a reverse pre-order traversal to find the node that comes before the cur rent one in document order 53 // Like previous, but skips children and starts with the next sibling.
50 Node* previous(const Node&, const Node* stayWithin = 0); 54 static Node* previousSkippingChildren(const Node&, const Node* stayWithin = 0);
51 55
52 // Like previous, but skips children and starts with the next sibling. 56 // Like next, but visits parents after their children.
53 Node* previousSkippingChildren(const Node&, const Node* stayWithin = 0); 57 static Node* nextPostOrder(const Node&, const Node* stayWithin = 0);
54 58
55 // Like next, but visits parents after their children. 59 // Like previous, but visits parents before their children.
56 Node* nextPostOrder(const Node&, const Node* stayWithin = 0); 60 static Node* previousPostOrder(const Node&, const Node* stayWithin = 0);
57 61
58 // Like previous, but visits parents before their children. 62 // Pre-order traversal including the pseudo-elements.
59 Node* previousPostOrder(const Node&, const Node* stayWithin = 0); 63 static Node* previousIncludingPseudo(const Node&, const Node* stayWithin = 0 );
64 static Node* nextIncludingPseudo(const Node&, const Node* stayWithin = 0);
65 static Node* nextIncludingPseudoSkippingChildren(const Node&, const Node* st ayWithin = 0);
60 66
61 // Pre-order traversal including the pseudo-elements. 67 static Node* nextAncestorSibling(const Node&);
62 Node* previousIncludingPseudo(const Node&, const Node* stayWithin = 0); 68 static Node* nextAncestorSibling(const Node&, const Node* stayWithin);
63 Node* nextIncludingPseudo(const Node&, const Node* stayWithin = 0);
64 Node* nextIncludingPseudoSkippingChildren(const Node&, const Node* stayWithin = 0);
65 69
66 Node* nextAncestorSibling(const Node&); 70 private:
67 Node* nextAncestorSibling(const Node&, const Node* stayWithin); 71 template <class NodeType>
72 static Node* traverseNextTemplate(NodeType&);
73 template <class NodeType>
74 static Node* traverseNextTemplate(NodeType&, const Node* stayWithin);
75 template <class NodeType>
76 static Node* traverseNextSkippingChildrenTemplate(NodeType&);
77 template <class NodeType>
78 static Node* traverseNextSkippingChildrenTemplate(NodeType&, const Node* sta yWithin);
79 };
68 80
69 template <class NodeType> 81 template <class NodeType>
70 inline Node* traverseNextTemplate(NodeType& current) 82 inline Node* NodeTraversal::traverseNextTemplate(NodeType& current)
71 { 83 {
72 if (current.firstChild()) 84 if (current.firstChild())
73 return current.firstChild(); 85 return current.firstChild();
74 if (current.nextSibling()) 86 if (current.nextSibling())
75 return current.nextSibling(); 87 return current.nextSibling();
76 return nextAncestorSibling(current); 88 return nextAncestorSibling(current);
77 } 89 }
78 inline Node* next(const Node& current) { return traverseNextTemplate(current); }
79 inline Node* next(const ContainerNode& current) { return traverseNextTemplate(cu rrent); }
80 90
81 template <class NodeType> 91 template <class NodeType>
82 inline Node* traverseNextTemplate(NodeType& current, const Node* stayWithin) 92 inline Node* NodeTraversal::traverseNextTemplate(NodeType& current, const Node* stayWithin)
83 { 93 {
84 if (current.firstChild()) 94 if (current.firstChild())
85 return current.firstChild(); 95 return current.firstChild();
86 if (current == stayWithin) 96 if (current == stayWithin)
87 return 0; 97 return 0;
88 if (current.nextSibling()) 98 if (current.nextSibling())
89 return current.nextSibling(); 99 return current.nextSibling();
90 return nextAncestorSibling(current, stayWithin); 100 return nextAncestorSibling(current, stayWithin);
91 } 101 }
92 inline Node* next(const Node& current, const Node* stayWithin) { return traverse NextTemplate(current, stayWithin); }
93 inline Node* next(const ContainerNode& current, const Node* stayWithin) { return traverseNextTemplate(current, stayWithin); }
94 102
95 template <class NodeType> 103 template <class NodeType>
96 inline Node* traverseNextSkippingChildrenTemplate(NodeType& current) 104 inline Node* NodeTraversal::traverseNextSkippingChildrenTemplate(NodeType& curre nt)
97 { 105 {
98 if (current.nextSibling()) 106 if (current.nextSibling())
99 return current.nextSibling(); 107 return current.nextSibling();
100 return nextAncestorSibling(current); 108 return nextAncestorSibling(current);
101 } 109 }
102 inline Node* nextSkippingChildren(const Node& current) { return traverseNextSkip pingChildrenTemplate(current); }
103 inline Node* nextSkippingChildren(const ContainerNode& current) { return travers eNextSkippingChildrenTemplate(current); }
104 110
105 template <class NodeType> 111 template <class NodeType>
106 inline Node* traverseNextSkippingChildrenTemplate(NodeType& current, const Node* stayWithin) 112 inline Node* NodeTraversal::traverseNextSkippingChildrenTemplate(NodeType& curre nt, const Node* stayWithin)
107 { 113 {
108 if (current == stayWithin) 114 if (current == stayWithin)
109 return 0; 115 return 0;
110 if (current.nextSibling()) 116 if (current.nextSibling())
111 return current.nextSibling(); 117 return current.nextSibling();
112 return nextAncestorSibling(current, stayWithin); 118 return nextAncestorSibling(current, stayWithin);
113 } 119 }
114 inline Node* nextSkippingChildren(const Node& current, const Node* stayWithin) { return traverseNextSkippingChildrenTemplate(current, stayWithin); }
115 inline Node* nextSkippingChildren(const ContainerNode& current, const Node* stay Within) { return traverseNextSkippingChildrenTemplate(current, stayWithin); }
116 120
117 } 121 } // namespace WebCore
118
119 }
120 122
121 #endif 123 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698