| 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 "core/dom/TreeWalker.h" | 26 #include "core/dom/TreeWalker.h" |
| 27 | 27 |
| 28 #include "bindings/v8/ExceptionMessages.h" | 28 #include "bindings/v8/ExceptionMessages.h" |
| 29 #include "bindings/v8/ExceptionState.h" | 29 #include "bindings/v8/ExceptionState.h" |
| 30 #include "bindings/v8/ScriptState.h" | |
| 31 #include "core/dom/ContainerNode.h" | 30 #include "core/dom/ContainerNode.h" |
| 32 #include "core/dom/ExceptionCode.h" | 31 #include "core/dom/ExceptionCode.h" |
| 33 #include "core/dom/NodeTraversal.h" | 32 #include "core/dom/NodeTraversal.h" |
| 34 | 33 |
| 35 namespace WebCore { | 34 namespace WebCore { |
| 36 | 35 |
| 37 TreeWalker::TreeWalker(PassRefPtr<Node> rootNode, unsigned whatToShow, PassRefPt
r<NodeFilter> filter) | 36 TreeWalker::TreeWalker(PassRefPtr<Node> rootNode, unsigned whatToShow, PassRefPt
r<NodeFilter> filter) |
| 38 : NodeIteratorBase(rootNode, whatToShow, filter) | 37 : NodeIteratorBase(rootNode, whatToShow, filter) |
| 39 , m_current(root()) | 38 , m_current(root()) |
| 40 { | 39 { |
| 41 ScriptWrappable::init(this); | 40 ScriptWrappable::init(this); |
| 42 } | 41 } |
| 43 | 42 |
| 44 void TreeWalker::setCurrentNode(PassRefPtr<Node> node, ExceptionState& exception
State) | 43 void TreeWalker::setCurrentNode(PassRefPtr<Node> node, ExceptionState& exception
State) |
| 45 { | 44 { |
| 46 if (!node) { | 45 if (!node) { |
| 47 exceptionState.throwDOMException(NotSupportedError, ExceptionMessages::a
rgumentNullOrIncorrectType(1, "Node")); | 46 exceptionState.throwDOMException(NotSupportedError, ExceptionMessages::a
rgumentNullOrIncorrectType(1, "Node")); |
| 48 return; | 47 return; |
| 49 } | 48 } |
| 50 m_current = node; | 49 m_current = node; |
| 51 } | 50 } |
| 52 | 51 |
| 53 inline Node* TreeWalker::setCurrent(PassRefPtr<Node> node) | 52 inline Node* TreeWalker::setCurrent(PassRefPtr<Node> node) |
| 54 { | 53 { |
| 55 m_current = node; | 54 m_current = node; |
| 56 return m_current.get(); | 55 return m_current.get(); |
| 57 } | 56 } |
| 58 | 57 |
| 59 Node* TreeWalker::parentNode(ScriptState* state) | 58 Node* TreeWalker::parentNode(ExceptionState& exceptionState) |
| 60 { | 59 { |
| 61 RefPtr<Node> node = m_current; | 60 RefPtr<Node> node = m_current; |
| 62 while (node != root()) { | 61 while (node != root()) { |
| 63 node = node->parentNode(); | 62 node = node->parentNode(); |
| 64 if (!node) | 63 if (!node) |
| 65 return 0; | 64 return 0; |
| 66 short acceptNodeResult = acceptNode(state, node.get()); | 65 short acceptNodeResult = acceptNode(node.get(), exceptionState); |
| 67 if (state && state->hadException()) | 66 if (exceptionState.hadException()) |
| 68 return 0; | 67 return 0; |
| 69 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) | 68 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) |
| 70 return setCurrent(node.release()); | 69 return setCurrent(node.release()); |
| 71 } | 70 } |
| 72 return 0; | 71 return 0; |
| 73 } | 72 } |
| 74 | 73 |
| 75 Node* TreeWalker::firstChild(ScriptState* state) | 74 Node* TreeWalker::firstChild(ExceptionState& exceptionState) |
| 76 { | 75 { |
| 77 for (RefPtr<Node> node = m_current->firstChild(); node; ) { | 76 for (RefPtr<Node> node = m_current->firstChild(); node; ) { |
| 78 short acceptNodeResult = acceptNode(state, node.get()); | 77 short acceptNodeResult = acceptNode(node.get(), exceptionState); |
| 79 if (state && state->hadException()) | 78 if (exceptionState.hadException()) |
| 80 return 0; | 79 return 0; |
| 81 switch (acceptNodeResult) { | 80 switch (acceptNodeResult) { |
| 82 case NodeFilter::FILTER_ACCEPT: | 81 case NodeFilter::FILTER_ACCEPT: |
| 83 m_current = node.release(); | 82 m_current = node.release(); |
| 84 return m_current.get(); | 83 return m_current.get(); |
| 85 case NodeFilter::FILTER_SKIP: | 84 case NodeFilter::FILTER_SKIP: |
| 86 if (node->firstChild()) { | 85 if (node->firstChild()) { |
| 87 node = node->firstChild(); | 86 node = node->firstChild(); |
| 88 continue; | 87 continue; |
| 89 } | 88 } |
| 90 break; | 89 break; |
| 91 case NodeFilter::FILTER_REJECT: | 90 case NodeFilter::FILTER_REJECT: |
| 92 break; | 91 break; |
| 93 } | 92 } |
| 94 do { | 93 do { |
| 95 if (node->nextSibling()) { | 94 if (node->nextSibling()) { |
| 96 node = node->nextSibling(); | 95 node = node->nextSibling(); |
| 97 break; | 96 break; |
| 98 } | 97 } |
| 99 ContainerNode* parent = node->parentNode(); | 98 ContainerNode* parent = node->parentNode(); |
| 100 if (!parent || parent == root() || parent == m_current) | 99 if (!parent || parent == root() || parent == m_current) |
| 101 return 0; | 100 return 0; |
| 102 node = parent; | 101 node = parent; |
| 103 } while (node); | 102 } while (node); |
| 104 } | 103 } |
| 105 return 0; | 104 return 0; |
| 106 } | 105 } |
| 107 | 106 |
| 108 Node* TreeWalker::lastChild(ScriptState* state) | 107 Node* TreeWalker::lastChild(ExceptionState& exceptionState) |
| 109 { | 108 { |
| 110 for (RefPtr<Node> node = m_current->lastChild(); node; ) { | 109 for (RefPtr<Node> node = m_current->lastChild(); node; ) { |
| 111 short acceptNodeResult = acceptNode(state, node.get()); | 110 short acceptNodeResult = acceptNode(node.get(), exceptionState); |
| 112 if (state && state->hadException()) | 111 if (exceptionState.hadException()) |
| 113 return 0; | 112 return 0; |
| 114 switch (acceptNodeResult) { | 113 switch (acceptNodeResult) { |
| 115 case NodeFilter::FILTER_ACCEPT: | 114 case NodeFilter::FILTER_ACCEPT: |
| 116 m_current = node.release(); | 115 m_current = node.release(); |
| 117 return m_current.get(); | 116 return m_current.get(); |
| 118 case NodeFilter::FILTER_SKIP: | 117 case NodeFilter::FILTER_SKIP: |
| 119 if (node->lastChild()) { | 118 if (node->lastChild()) { |
| 120 node = node->lastChild(); | 119 node = node->lastChild(); |
| 121 continue; | 120 continue; |
| 122 } | 121 } |
| 123 break; | 122 break; |
| 124 case NodeFilter::FILTER_REJECT: | 123 case NodeFilter::FILTER_REJECT: |
| 125 break; | 124 break; |
| 126 } | 125 } |
| 127 do { | 126 do { |
| 128 if (node->previousSibling()) { | 127 if (node->previousSibling()) { |
| 129 node = node->previousSibling(); | 128 node = node->previousSibling(); |
| 130 break; | 129 break; |
| 131 } | 130 } |
| 132 ContainerNode* parent = node->parentNode(); | 131 ContainerNode* parent = node->parentNode(); |
| 133 if (!parent || parent == root() || parent == m_current) | 132 if (!parent || parent == root() || parent == m_current) |
| 134 return 0; | 133 return 0; |
| 135 node = parent; | 134 node = parent; |
| 136 } while (node); | 135 } while (node); |
| 137 } | 136 } |
| 138 return 0; | 137 return 0; |
| 139 } | 138 } |
| 140 | 139 |
| 141 Node* TreeWalker::previousSibling(ScriptState* state) | 140 Node* TreeWalker::previousSibling(ExceptionState& exceptionState) |
| 142 { | 141 { |
| 143 RefPtr<Node> node = m_current; | 142 RefPtr<Node> node = m_current; |
| 144 if (node == root()) | 143 if (node == root()) |
| 145 return 0; | 144 return 0; |
| 146 while (1) { | 145 while (1) { |
| 147 for (RefPtr<Node> sibling = node->previousSibling(); sibling; ) { | 146 for (RefPtr<Node> sibling = node->previousSibling(); sibling; ) { |
| 148 short acceptNodeResult = acceptNode(state, sibling.get()); | 147 short acceptNodeResult = acceptNode(sibling.get(), exceptionState); |
| 149 if (state && state->hadException()) | 148 if (exceptionState.hadException()) |
| 150 return 0; | 149 return 0; |
| 151 switch (acceptNodeResult) { | 150 switch (acceptNodeResult) { |
| 152 case NodeFilter::FILTER_ACCEPT: | 151 case NodeFilter::FILTER_ACCEPT: |
| 153 m_current = sibling.release(); | 152 m_current = sibling.release(); |
| 154 return m_current.get(); | 153 return m_current.get(); |
| 155 case NodeFilter::FILTER_SKIP: | 154 case NodeFilter::FILTER_SKIP: |
| 156 if (sibling->lastChild()) { | 155 if (sibling->lastChild()) { |
| 157 sibling = sibling->lastChild(); | 156 sibling = sibling->lastChild(); |
| 158 node = sibling; | 157 node = sibling; |
| 159 continue; | 158 continue; |
| 160 } | 159 } |
| 161 break; | 160 break; |
| 162 case NodeFilter::FILTER_REJECT: | 161 case NodeFilter::FILTER_REJECT: |
| 163 break; | 162 break; |
| 164 } | 163 } |
| 165 sibling = sibling->previousSibling(); | 164 sibling = sibling->previousSibling(); |
| 166 } | 165 } |
| 167 node = node->parentNode(); | 166 node = node->parentNode(); |
| 168 if (!node || node == root()) | 167 if (!node || node == root()) |
| 169 return 0; | 168 return 0; |
| 170 short acceptNodeResult = acceptNode(state, node.get()); | 169 short acceptNodeResult = acceptNode(node.get(), exceptionState); |
| 171 if (state && state->hadException()) | 170 if (exceptionState.hadException()) |
| 172 return 0; | 171 return 0; |
| 173 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) | 172 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) |
| 174 return 0; | 173 return 0; |
| 175 } | 174 } |
| 176 } | 175 } |
| 177 | 176 |
| 178 Node* TreeWalker::nextSibling(ScriptState* state) | 177 Node* TreeWalker::nextSibling(ExceptionState& exceptionState) |
| 179 { | 178 { |
| 180 RefPtr<Node> node = m_current; | 179 RefPtr<Node> node = m_current; |
| 181 if (node == root()) | 180 if (node == root()) |
| 182 return 0; | 181 return 0; |
| 183 while (1) { | 182 while (1) { |
| 184 for (RefPtr<Node> sibling = node->nextSibling(); sibling; ) { | 183 for (RefPtr<Node> sibling = node->nextSibling(); sibling; ) { |
| 185 short acceptNodeResult = acceptNode(state, sibling.get()); | 184 short acceptNodeResult = acceptNode(sibling.get(), exceptionState); |
| 186 if (state && state->hadException()) | 185 if (exceptionState.hadException()) |
| 187 return 0; | 186 return 0; |
| 188 switch (acceptNodeResult) { | 187 switch (acceptNodeResult) { |
| 189 case NodeFilter::FILTER_ACCEPT: | 188 case NodeFilter::FILTER_ACCEPT: |
| 190 m_current = sibling.release(); | 189 m_current = sibling.release(); |
| 191 return m_current.get(); | 190 return m_current.get(); |
| 192 case NodeFilter::FILTER_SKIP: | 191 case NodeFilter::FILTER_SKIP: |
| 193 if (sibling->firstChild()) { | 192 if (sibling->firstChild()) { |
| 194 sibling = sibling->firstChild(); | 193 sibling = sibling->firstChild(); |
| 195 node = sibling; | 194 node = sibling; |
| 196 continue; | 195 continue; |
| 197 } | 196 } |
| 198 break; | 197 break; |
| 199 case NodeFilter::FILTER_REJECT: | 198 case NodeFilter::FILTER_REJECT: |
| 200 break; | 199 break; |
| 201 } | 200 } |
| 202 sibling = sibling->nextSibling(); | 201 sibling = sibling->nextSibling(); |
| 203 } | 202 } |
| 204 node = node->parentNode(); | 203 node = node->parentNode(); |
| 205 if (!node || node == root()) | 204 if (!node || node == root()) |
| 206 return 0; | 205 return 0; |
| 207 short acceptNodeResult = acceptNode(state, node.get()); | 206 short acceptNodeResult = acceptNode(node.get(), exceptionState); |
| 208 if (state && state->hadException()) | 207 if (exceptionState.hadException()) |
| 209 return 0; | 208 return 0; |
| 210 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) | 209 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) |
| 211 return 0; | 210 return 0; |
| 212 } | 211 } |
| 213 } | 212 } |
| 214 | 213 |
| 215 Node* TreeWalker::previousNode(ScriptState* state) | 214 Node* TreeWalker::previousNode(ExceptionState& exceptionState) |
| 216 { | 215 { |
| 217 RefPtr<Node> node = m_current; | 216 RefPtr<Node> node = m_current; |
| 218 while (node != root()) { | 217 while (node != root()) { |
| 219 while (Node* previousSibling = node->previousSibling()) { | 218 while (Node* previousSibling = node->previousSibling()) { |
| 220 node = previousSibling; | 219 node = previousSibling; |
| 221 short acceptNodeResult = acceptNode(state, node.get()); | 220 short acceptNodeResult = acceptNode(node.get(), exceptionState); |
| 222 if (state && state->hadException()) | 221 if (exceptionState.hadException()) |
| 223 return 0; | 222 return 0; |
| 224 if (acceptNodeResult == NodeFilter::FILTER_REJECT) | 223 if (acceptNodeResult == NodeFilter::FILTER_REJECT) |
| 225 continue; | 224 continue; |
| 226 while (Node* lastChild = node->lastChild()) { | 225 while (Node* lastChild = node->lastChild()) { |
| 227 node = lastChild; | 226 node = lastChild; |
| 228 acceptNodeResult = acceptNode(state, node.get()); | 227 acceptNodeResult = acceptNode(node.get(), exceptionState); |
| 229 if (state && state->hadException()) | 228 if (exceptionState.hadException()) |
| 230 return 0; | 229 return 0; |
| 231 if (acceptNodeResult == NodeFilter::FILTER_REJECT) | 230 if (acceptNodeResult == NodeFilter::FILTER_REJECT) |
| 232 break; | 231 break; |
| 233 } | 232 } |
| 234 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) { | 233 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) { |
| 235 m_current = node.release(); | 234 m_current = node.release(); |
| 236 return m_current.get(); | 235 return m_current.get(); |
| 237 } | 236 } |
| 238 } | 237 } |
| 239 if (node == root()) | 238 if (node == root()) |
| 240 return 0; | 239 return 0; |
| 241 ContainerNode* parent = node->parentNode(); | 240 ContainerNode* parent = node->parentNode(); |
| 242 if (!parent) | 241 if (!parent) |
| 243 return 0; | 242 return 0; |
| 244 node = parent; | 243 node = parent; |
| 245 short acceptNodeResult = acceptNode(state, node.get()); | 244 short acceptNodeResult = acceptNode(node.get(), exceptionState); |
| 246 if (state && state->hadException()) | 245 if (exceptionState.hadException()) |
| 247 return 0; | 246 return 0; |
| 248 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) | 247 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) |
| 249 return setCurrent(node.release()); | 248 return setCurrent(node.release()); |
| 250 } | 249 } |
| 251 return 0; | 250 return 0; |
| 252 } | 251 } |
| 253 | 252 |
| 254 Node* TreeWalker::nextNode(ScriptState* state) | 253 Node* TreeWalker::nextNode(ExceptionState& exceptionState) |
| 255 { | 254 { |
| 256 RefPtr<Node> node = m_current; | 255 RefPtr<Node> node = m_current; |
| 257 Children: | 256 Children: |
| 258 while (Node* firstChild = node->firstChild()) { | 257 while (Node* firstChild = node->firstChild()) { |
| 259 node = firstChild; | 258 node = firstChild; |
| 260 short acceptNodeResult = acceptNode(state, node.get()); | 259 short acceptNodeResult = acceptNode(node.get(), exceptionState); |
| 261 if (state && state->hadException()) | 260 if (exceptionState.hadException()) |
| 262 return 0; | 261 return 0; |
| 263 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) | 262 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) |
| 264 return setCurrent(node.release()); | 263 return setCurrent(node.release()); |
| 265 if (acceptNodeResult == NodeFilter::FILTER_REJECT) | 264 if (acceptNodeResult == NodeFilter::FILTER_REJECT) |
| 266 break; | 265 break; |
| 267 } | 266 } |
| 268 while (Node* nextSibling = NodeTraversal::nextSkippingChildren(*node, root()
)) { | 267 while (Node* nextSibling = NodeTraversal::nextSkippingChildren(*node, root()
)) { |
| 269 node = nextSibling; | 268 node = nextSibling; |
| 270 short acceptNodeResult = acceptNode(state, node.get()); | 269 short acceptNodeResult = acceptNode(node.get(), exceptionState); |
| 271 if (state && state->hadException()) | 270 if (exceptionState.hadException()) |
| 272 return 0; | 271 return 0; |
| 273 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) | 272 if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) |
| 274 return setCurrent(node.release()); | 273 return setCurrent(node.release()); |
| 275 if (acceptNodeResult == NodeFilter::FILTER_SKIP) | 274 if (acceptNodeResult == NodeFilter::FILTER_SKIP) |
| 276 goto Children; | 275 goto Children; |
| 277 } | 276 } |
| 278 return 0; | 277 return 0; |
| 279 } | 278 } |
| 280 | 279 |
| 281 } // namespace WebCore | 280 } // namespace WebCore |
| OLD | NEW |