OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
3 * Copyright (C) 2000 Frederik Holljen (frederik.holljen@hig.no) | 3 * Copyright (C) 2000 Frederik Holljen (frederik.holljen@hig.no) |
4 * Copyright (C) 2001 Peter Kelly (pmk@post.com) | 4 * Copyright (C) 2001 Peter Kelly (pmk@post.com) |
5 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) | 5 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) |
6 * Copyright (C) 2004, 2008 Apple Inc. All rights reserved. | 6 * Copyright (C) 2004, 2008 Apple Inc. All rights reserved. |
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 |
11 * version 2 of the License, or (at your option) any later version. | 11 * version 2 of the License, or (at your option) any later version. |
12 * | 12 * |
13 * This library is distributed in the hope that it will be useful, | 13 * This library is distributed in the hope that it will be useful, |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 * Library General Public License for more details. | 16 * Library General Public License for more details. |
17 * | 17 * |
18 * You should have received a copy of the GNU Library General Public License | 18 * 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 | 19 * along with this library; see the file COPYING.LIB. If not, write to |
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 * Boston, MA 02110-1301, USA. | 21 * Boston, MA 02110-1301, USA. |
22 * | 22 * |
23 */ | 23 */ |
24 | 24 |
25 #include "config.h" | 25 #include "config.h" |
26 #include "TreeWalker.h" | 26 #include "TreeWalker.h" |
27 | 27 |
28 #include <kjs/ExecState.h> | |
29 #include "ExceptionCode.h" | 28 #include "ExceptionCode.h" |
| 29 #include "ExceptionContext.h" |
30 #include "Node.h" | 30 #include "Node.h" |
31 #include "NodeFilter.h" | 31 #include "NodeFilter.h" |
32 #include <wtf/PassRefPtr.h> | 32 #include <wtf/PassRefPtr.h> |
33 | 33 |
34 using namespace KJS; | |
35 | |
36 namespace WebCore { | 34 namespace WebCore { |
37 | 35 |
38 TreeWalker::TreeWalker(PassRefPtr<Node> rootNode, unsigned whatToShow, PassRefPt
r<NodeFilter> filter, bool expandEntityReferences) | 36 TreeWalker::TreeWalker(PassRefPtr<Node> rootNode, unsigned whatToShow, PassRefPt
r<NodeFilter> filter, bool expandEntityReferences) |
39 : Traversal(rootNode, whatToShow, filter, expandEntityReferences) | 37 : Traversal(rootNode, whatToShow, filter, expandEntityReferences) |
40 , m_current(root()) | 38 , m_current(root()) |
41 { | 39 { |
42 } | 40 } |
43 | 41 |
44 void TreeWalker::setCurrentNode(PassRefPtr<Node> node, ExceptionCode& ec) | 42 void TreeWalker::setCurrentNode(PassRefPtr<Node> node, ExceptionCode& ec) |
45 { | 43 { |
46 if (!node) { | 44 if (!node) { |
47 ec = NOT_SUPPORTED_ERR; | 45 ec = NOT_SUPPORTED_ERR; |
48 return; | 46 return; |
49 } | 47 } |
50 m_current = node; | 48 m_current = node; |
51 } | 49 } |
52 | 50 |
53 inline Node* TreeWalker::setCurrent(PassRefPtr<Node> node) | 51 inline Node* TreeWalker::setCurrent(PassRefPtr<Node> node) |
54 { | 52 { |
55 m_current = node; | 53 m_current = node; |
56 return m_current.get(); | 54 return m_current.get(); |
57 } | 55 } |
58 | 56 |
59 #if USE(JSC) | 57 Node* TreeWalker::parentNode(ExceptionContext* exec) |
60 Node* TreeWalker::parentNode(ExecState* exec) | |
61 { | 58 { |
62 RefPtr<Node> node = m_current; | 59 RefPtr<Node> node = m_current; |
63 while (node != root()) { | 60 while (node != root()) { |
64 node = node->parentNode(); | 61 node = node->parentNode(); |
65 if (!node) | 62 if (!node) |
66 return 0; | 63 return 0; |
67 short acceptNodeResult = acceptNode(exec, node.get()); | 64 short acceptNodeResult = acceptNode(exec, node.get()); |
68 if (exec && exec->hadException()) | 65 if (exec && exec->hadException()) |
69 return 0; | 66 return 0; |
70 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) | 67 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) |
71 return setCurrent(node.release()); | 68 return setCurrent(node.release()); |
72 } | 69 } |
73 return 0; | 70 return 0; |
74 } | 71 } |
75 | 72 |
76 Node* TreeWalker::firstChild(ExecState* exec) | 73 Node* TreeWalker::firstChild(ExceptionContext* exec) |
77 { | 74 { |
78 for (RefPtr<Node> node = m_current->firstChild(); node; ) { | 75 for (RefPtr<Node> node = m_current->firstChild(); node; ) { |
79 short acceptNodeResult = acceptNode(exec, node.get()); | 76 short acceptNodeResult = acceptNode(exec, node.get()); |
80 if (exec && exec->hadException()) | 77 if (exec && exec->hadException()) |
81 return 0; | 78 return 0; |
82 switch (acceptNodeResult) { | 79 switch (acceptNodeResult) { |
83 case NodeFilter::FILTER_ACCEPT: | 80 case NodeFilter::FILTER_ACCEPT: |
84 m_current = node.release(); | 81 m_current = node.release(); |
85 return m_current.get(); | 82 return m_current.get(); |
86 case NodeFilter::FILTER_SKIP: | 83 case NodeFilter::FILTER_SKIP: |
(...skipping 12 matching lines...) Expand all Loading... |
99 } | 96 } |
100 Node* parent = node->parentNode(); | 97 Node* parent = node->parentNode(); |
101 if (!parent || parent == root() || parent == m_current) | 98 if (!parent || parent == root() || parent == m_current) |
102 return 0; | 99 return 0; |
103 node = parent; | 100 node = parent; |
104 } while (node); | 101 } while (node); |
105 } | 102 } |
106 return 0; | 103 return 0; |
107 } | 104 } |
108 | 105 |
109 Node* TreeWalker::lastChild(ExecState* exec) | 106 Node* TreeWalker::lastChild(ExceptionContext* exec) |
110 { | 107 { |
111 for (RefPtr<Node> node = m_current->lastChild(); node; ) { | 108 for (RefPtr<Node> node = m_current->lastChild(); node; ) { |
112 short acceptNodeResult = acceptNode(exec, node.get()); | 109 short acceptNodeResult = acceptNode(exec, node.get()); |
113 if (exec && exec->hadException()) | 110 if (exec && exec->hadException()) |
114 return 0; | 111 return 0; |
115 switch (acceptNodeResult) { | 112 switch (acceptNodeResult) { |
116 case NodeFilter::FILTER_ACCEPT: | 113 case NodeFilter::FILTER_ACCEPT: |
117 m_current = node.release(); | 114 m_current = node.release(); |
118 return m_current.get(); | 115 return m_current.get(); |
119 case NodeFilter::FILTER_SKIP: | 116 case NodeFilter::FILTER_SKIP: |
(...skipping 12 matching lines...) Expand all Loading... |
132 } | 129 } |
133 Node* parent = node->parentNode(); | 130 Node* parent = node->parentNode(); |
134 if (!parent || parent == root() || parent == m_current) | 131 if (!parent || parent == root() || parent == m_current) |
135 return 0; | 132 return 0; |
136 node = parent; | 133 node = parent; |
137 } while (node); | 134 } while (node); |
138 } | 135 } |
139 return 0; | 136 return 0; |
140 } | 137 } |
141 | 138 |
142 Node* TreeWalker::previousSibling(ExecState* exec) | 139 Node* TreeWalker::previousSibling(ExceptionContext* exec) |
143 { | 140 { |
144 RefPtr<Node> node = m_current; | 141 RefPtr<Node> node = m_current; |
145 if (node == root()) | 142 if (node == root()) |
146 return 0; | 143 return 0; |
147 while (1) { | 144 while (1) { |
148 for (RefPtr<Node> sibling = node->previousSibling(); sibling; ) { | 145 for (RefPtr<Node> sibling = node->previousSibling(); sibling; ) { |
149 short acceptNodeResult = acceptNode(exec, sibling.get()); | 146 short acceptNodeResult = acceptNode(exec, sibling.get()); |
150 if (exec && exec->hadException()) | 147 if (exec && exec->hadException()) |
151 return 0; | 148 return 0; |
152 switch (acceptNodeResult) { | 149 switch (acceptNodeResult) { |
(...skipping 15 matching lines...) Expand all Loading... |
168 if (!node || node == root()) | 165 if (!node || node == root()) |
169 return 0; | 166 return 0; |
170 short acceptNodeResult = acceptNode(exec, node.get()); | 167 short acceptNodeResult = acceptNode(exec, node.get()); |
171 if (exec && exec->hadException()) | 168 if (exec && exec->hadException()) |
172 return 0; | 169 return 0; |
173 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) | 170 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) |
174 return 0; | 171 return 0; |
175 } | 172 } |
176 } | 173 } |
177 | 174 |
178 Node* TreeWalker::nextSibling(ExecState* exec) | 175 Node* TreeWalker::nextSibling(ExceptionContext* exec) |
179 { | 176 { |
180 RefPtr<Node> node = m_current; | 177 RefPtr<Node> node = m_current; |
181 if (node == root()) | 178 if (node == root()) |
182 return 0; | 179 return 0; |
183 while (1) { | 180 while (1) { |
184 for (RefPtr<Node> sibling = node->nextSibling(); sibling; ) { | 181 for (RefPtr<Node> sibling = node->nextSibling(); sibling; ) { |
185 short acceptNodeResult = acceptNode(exec, sibling.get()); | 182 short acceptNodeResult = acceptNode(exec, sibling.get()); |
186 if (exec && exec->hadException()) | 183 if (exec && exec->hadException()) |
187 return 0; | 184 return 0; |
188 switch (acceptNodeResult) { | 185 switch (acceptNodeResult) { |
(...skipping 15 matching lines...) Expand all Loading... |
204 if (!node || node == root()) | 201 if (!node || node == root()) |
205 return 0; | 202 return 0; |
206 short acceptNodeResult = acceptNode(exec, node.get()); | 203 short acceptNodeResult = acceptNode(exec, node.get()); |
207 if (exec && exec->hadException()) | 204 if (exec && exec->hadException()) |
208 return 0; | 205 return 0; |
209 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) | 206 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) |
210 return 0; | 207 return 0; |
211 } | 208 } |
212 } | 209 } |
213 | 210 |
214 Node* TreeWalker::previousNode(ExecState* exec) | 211 Node* TreeWalker::previousNode(ExceptionContext* exec) |
215 { | 212 { |
216 RefPtr<Node> node = m_current; | 213 RefPtr<Node> node = m_current; |
217 while (node != root()) { | 214 while (node != root()) { |
218 while (Node* previousSibling = node->previousSibling()) { | 215 while (Node* previousSibling = node->previousSibling()) { |
219 node = previousSibling; | 216 node = previousSibling; |
220 short acceptNodeResult = acceptNode(exec, node.get()); | 217 short acceptNodeResult = acceptNode(exec, node.get()); |
221 if (exec && exec->hadException()) | 218 if (exec && exec->hadException()) |
222 return 0; | 219 return 0; |
223 if (acceptNodeResult == NodeFilter::FILTER_REJECT) | 220 if (acceptNodeResult == NodeFilter::FILTER_REJECT) |
224 continue; | 221 continue; |
(...skipping 18 matching lines...) Expand all Loading... |
243 node = parent; | 240 node = parent; |
244 short acceptNodeResult = acceptNode(exec, node.get()); | 241 short acceptNodeResult = acceptNode(exec, node.get()); |
245 if (exec && exec->hadException()) | 242 if (exec && exec->hadException()) |
246 return 0; | 243 return 0; |
247 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) | 244 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) |
248 return setCurrent(node.release()); | 245 return setCurrent(node.release()); |
249 } | 246 } |
250 return 0; | 247 return 0; |
251 } | 248 } |
252 | 249 |
253 Node* TreeWalker::nextNode(ExecState* exec) | 250 Node* TreeWalker::nextNode(ExceptionContext* exec) |
254 { | 251 { |
255 RefPtr<Node> node = m_current; | 252 RefPtr<Node> node = m_current; |
256 Children: | 253 Children: |
257 while (Node* firstChild = node->firstChild()) { | 254 while (Node* firstChild = node->firstChild()) { |
258 node = firstChild; | 255 node = firstChild; |
259 short acceptNodeResult = acceptNode(exec, node.get()); | 256 short acceptNodeResult = acceptNode(exec, node.get()); |
260 if (exec && exec->hadException()) | 257 if (exec && exec->hadException()) |
261 return 0; | 258 return 0; |
262 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) | 259 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) |
263 return setCurrent(node.release()); | 260 return setCurrent(node.release()); |
264 if (acceptNodeResult == NodeFilter::FILTER_REJECT) | 261 if (acceptNodeResult == NodeFilter::FILTER_REJECT) |
265 break; | 262 break; |
266 } | 263 } |
267 while (Node* nextSibling = node->traverseNextSibling(root())) { | 264 while (Node* nextSibling = node->traverseNextSibling(root())) { |
268 node = nextSibling; | 265 node = nextSibling; |
269 short acceptNodeResult = acceptNode(exec, node.get()); | 266 short acceptNodeResult = acceptNode(exec, node.get()); |
270 if (exec && exec->hadException()) | 267 if (exec && exec->hadException()) |
271 return 0; | 268 return 0; |
272 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) | 269 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) |
273 return setCurrent(node.release()); | 270 return setCurrent(node.release()); |
274 if (acceptNodeResult == NodeFilter::FILTER_SKIP) | 271 if (acceptNodeResult == NodeFilter::FILTER_SKIP) |
275 goto Children; | 272 goto Children; |
276 } | 273 } |
277 return 0; | 274 return 0; |
278 } | 275 } |
279 #endif // USE(JSC) | |
280 | 276 |
281 } // namespace WebCore | 277 } // namespace WebCore |
OLD | NEW |