| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2005 Frerich Raabe <raabe@kde.org> | 2 * Copyright (C) 2005 Frerich Raabe <raabe@kde.org> |
| 3 * Copyright (C) 2006, 2009 Apple Inc. All rights reserved. | 3 * Copyright (C) 2006, 2009 Apple Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * | 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| 11 * 2. Redistributions in binary form must reproduce the above copyright | 11 * 2. Redistributions in binary form must reproduce the above copyright |
| 12 * notice, this list of conditions and the following disclaimer in the | 12 * notice, this list of conditions and the following disclaimer in the |
| 13 * documentation and/or other materials provided with the distribution. | 13 * documentation and/or other materials provided with the distribution. |
| 14 * | 14 * |
| 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #include "config.h" | 27 #include "config.h" |
| 28 #include "core/xml/XPathResult.h" | 28 #include "core/xml/XPathResult.h" |
| 29 | 29 |
| 30 #include "bindings/v8/ExceptionMessages.h" | |
| 31 #include "bindings/v8/ExceptionState.h" | 30 #include "bindings/v8/ExceptionState.h" |
| 32 #include "core/dom/Document.h" | 31 #include "core/dom/Document.h" |
| 33 #include "core/dom/ExceptionCode.h" | 32 #include "core/dom/ExceptionCode.h" |
| 34 #include "core/xml/XPathEvaluator.h" | 33 #include "core/xml/XPathEvaluator.h" |
| 35 | 34 |
| 36 namespace WebCore { | 35 namespace WebCore { |
| 37 | 36 |
| 38 using namespace XPath; | 37 using namespace XPath; |
| 39 | 38 |
| 40 XPathResult::XPathResult(Document* document, const Value& value) | 39 XPathResult::XPathResult(Document* document, const Value& value) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 break; | 82 break; |
| 84 case BOOLEAN_TYPE: | 83 case BOOLEAN_TYPE: |
| 85 m_resultType = type; | 84 m_resultType = type; |
| 86 m_value = m_value.toBoolean(); | 85 m_value = m_value.toBoolean(); |
| 87 break; | 86 break; |
| 88 case UNORDERED_NODE_ITERATOR_TYPE: | 87 case UNORDERED_NODE_ITERATOR_TYPE: |
| 89 case UNORDERED_NODE_SNAPSHOT_TYPE: | 88 case UNORDERED_NODE_SNAPSHOT_TYPE: |
| 90 case ANY_UNORDERED_NODE_TYPE: | 89 case ANY_UNORDERED_NODE_TYPE: |
| 91 case FIRST_ORDERED_NODE_TYPE: // This is correct - singleNodeValue() wil
l take care of ordering. | 90 case FIRST_ORDERED_NODE_TYPE: // This is correct - singleNodeValue() wil
l take care of ordering. |
| 92 if (!m_value.isNodeSet()) { | 91 if (!m_value.isNodeSet()) { |
| 93 exceptionState.throwTypeError(ExceptionMessages::failedToExecute
("evaluate", "XPathExpression", "The result is not a node set, and therefore can
not be converted to the desired type.")); | 92 exceptionState.throwTypeError("The result is not a node set, and
therefore cannot be converted to the desired type."); |
| 94 return; | 93 return; |
| 95 } | 94 } |
| 96 m_resultType = type; | 95 m_resultType = type; |
| 97 break; | 96 break; |
| 98 case ORDERED_NODE_ITERATOR_TYPE: | 97 case ORDERED_NODE_ITERATOR_TYPE: |
| 99 if (!m_value.isNodeSet()) { | 98 if (!m_value.isNodeSet()) { |
| 100 exceptionState.throwTypeError(ExceptionMessages::failedToExecute
("evaluate", "XPathExpression", "The result is not a node set, and therefore can
not be converted to the desired type.")); | 99 exceptionState.throwTypeError("The result is not a node set, and
therefore cannot be converted to the desired type."); |
| 101 return; | 100 return; |
| 102 } | 101 } |
| 103 m_nodeSet.sort(); | 102 m_nodeSet.sort(); |
| 104 m_resultType = type; | 103 m_resultType = type; |
| 105 break; | 104 break; |
| 106 case ORDERED_NODE_SNAPSHOT_TYPE: | 105 case ORDERED_NODE_SNAPSHOT_TYPE: |
| 107 if (!m_value.isNodeSet()) { | 106 if (!m_value.isNodeSet()) { |
| 108 exceptionState.throwTypeError(ExceptionMessages::failedToExecute
("evaluate", "XPathExpression", "The result is not a node set, and therefore can
not be converted to the desired type.")); | 107 exceptionState.throwTypeError("The result is not a node set, and
therefore cannot be converted to the desired type."); |
| 109 return; | 108 return; |
| 110 } | 109 } |
| 111 m_value.toNodeSet().sort(); | 110 m_value.toNodeSet().sort(); |
| 112 m_resultType = type; | 111 m_resultType = type; |
| 113 break; | 112 break; |
| 114 } | 113 } |
| 115 } | 114 } |
| 116 | 115 |
| 117 unsigned short XPathResult::resultType() const | 116 unsigned short XPathResult::resultType() const |
| 118 { | 117 { |
| 119 return m_resultType; | 118 return m_resultType; |
| 120 } | 119 } |
| 121 | 120 |
| 122 double XPathResult::numberValue(ExceptionState& exceptionState) const | 121 double XPathResult::numberValue(ExceptionState& exceptionState) const |
| 123 { | 122 { |
| 124 if (resultType() != NUMBER_TYPE) { | 123 if (resultType() != NUMBER_TYPE) { |
| 125 exceptionState.throwTypeError(ExceptionMessages::failedToGet("numberValu
e", "XPathResult", "The result type is not a number.")); | 124 exceptionState.throwTypeError("The result type is not a number."); |
| 126 return 0.0; | 125 return 0.0; |
| 127 } | 126 } |
| 128 return m_value.toNumber(); | 127 return m_value.toNumber(); |
| 129 } | 128 } |
| 130 | 129 |
| 131 String XPathResult::stringValue(ExceptionState& exceptionState) const | 130 String XPathResult::stringValue(ExceptionState& exceptionState) const |
| 132 { | 131 { |
| 133 if (resultType() != STRING_TYPE) { | 132 if (resultType() != STRING_TYPE) { |
| 134 exceptionState.throwTypeError(ExceptionMessages::failedToGet("stringValu
e", "XPathResult", "The result type is not a string.")); | 133 exceptionState.throwTypeError("The result type is not a string."); |
| 135 return String(); | 134 return String(); |
| 136 } | 135 } |
| 137 return m_value.toString(); | 136 return m_value.toString(); |
| 138 } | 137 } |
| 139 | 138 |
| 140 bool XPathResult::booleanValue(ExceptionState& exceptionState) const | 139 bool XPathResult::booleanValue(ExceptionState& exceptionState) const |
| 141 { | 140 { |
| 142 if (resultType() != BOOLEAN_TYPE) { | 141 if (resultType() != BOOLEAN_TYPE) { |
| 143 exceptionState.throwTypeError(ExceptionMessages::failedToGet("booleanVal
ue", "XPathResult", "The result type is not a boolean.")); | 142 exceptionState.throwTypeError("The result type is not a boolean."); |
| 144 return false; | 143 return false; |
| 145 } | 144 } |
| 146 return m_value.toBoolean(); | 145 return m_value.toBoolean(); |
| 147 } | 146 } |
| 148 | 147 |
| 149 Node* XPathResult::singleNodeValue(ExceptionState& exceptionState) const | 148 Node* XPathResult::singleNodeValue(ExceptionState& exceptionState) const |
| 150 { | 149 { |
| 151 if (resultType() != ANY_UNORDERED_NODE_TYPE && resultType() != FIRST_ORDERED
_NODE_TYPE) { | 150 if (resultType() != ANY_UNORDERED_NODE_TYPE && resultType() != FIRST_ORDERED
_NODE_TYPE) { |
| 152 exceptionState.throwTypeError(ExceptionMessages::failedToGet("singleNode
Value", "XPathResult", "The result type is not a single node.")); | 151 exceptionState.throwTypeError("The result type is not a single node."); |
| 153 return 0; | 152 return 0; |
| 154 } | 153 } |
| 155 | 154 |
| 156 const NodeSet& nodes = m_value.toNodeSet(); | 155 const NodeSet& nodes = m_value.toNodeSet(); |
| 157 if (resultType() == FIRST_ORDERED_NODE_TYPE) | 156 if (resultType() == FIRST_ORDERED_NODE_TYPE) |
| 158 return nodes.firstNode(); | 157 return nodes.firstNode(); |
| 159 else | 158 else |
| 160 return nodes.anyNode(); | 159 return nodes.anyNode(); |
| 161 } | 160 } |
| 162 | 161 |
| 163 bool XPathResult::invalidIteratorState() const | 162 bool XPathResult::invalidIteratorState() const |
| 164 { | 163 { |
| 165 if (resultType() != UNORDERED_NODE_ITERATOR_TYPE && resultType() != ORDERED_
NODE_ITERATOR_TYPE) | 164 if (resultType() != UNORDERED_NODE_ITERATOR_TYPE && resultType() != ORDERED_
NODE_ITERATOR_TYPE) |
| 166 return false; | 165 return false; |
| 167 | 166 |
| 168 ASSERT(m_document); | 167 ASSERT(m_document); |
| 169 return m_document->domTreeVersion() != m_domTreeVersion; | 168 return m_document->domTreeVersion() != m_domTreeVersion; |
| 170 } | 169 } |
| 171 | 170 |
| 172 unsigned long XPathResult::snapshotLength(ExceptionState& exceptionState) const | 171 unsigned long XPathResult::snapshotLength(ExceptionState& exceptionState) const |
| 173 { | 172 { |
| 174 if (resultType() != UNORDERED_NODE_SNAPSHOT_TYPE && resultType() != ORDERED_
NODE_SNAPSHOT_TYPE) { | 173 if (resultType() != UNORDERED_NODE_SNAPSHOT_TYPE && resultType() != ORDERED_
NODE_SNAPSHOT_TYPE) { |
| 175 exceptionState.throwTypeError(ExceptionMessages::failedToGet("snapshotLe
ngth", "XPathResult", "The result type is not a snapshot.")); | 174 exceptionState.throwTypeError("The result type is not a snapshot."); |
| 176 return 0; | 175 return 0; |
| 177 } | 176 } |
| 178 | 177 |
| 179 return m_value.toNodeSet().size(); | 178 return m_value.toNodeSet().size(); |
| 180 } | 179 } |
| 181 | 180 |
| 182 Node* XPathResult::iterateNext(ExceptionState& exceptionState) | 181 Node* XPathResult::iterateNext(ExceptionState& exceptionState) |
| 183 { | 182 { |
| 184 if (resultType() != UNORDERED_NODE_ITERATOR_TYPE && resultType() != ORDERED_
NODE_ITERATOR_TYPE) { | 183 if (resultType() != UNORDERED_NODE_ITERATOR_TYPE && resultType() != ORDERED_
NODE_ITERATOR_TYPE) { |
| 185 exceptionState.throwTypeError(ExceptionMessages::failedToExecute("iterat
eNext", "XPathResult", "The result type is not an iterator.")); | 184 exceptionState.throwTypeError("The result type is not an iterator."); |
| 186 return 0; | 185 return 0; |
| 187 } | 186 } |
| 188 | 187 |
| 189 if (invalidIteratorState()) { | 188 if (invalidIteratorState()) { |
| 190 exceptionState.throwDOMException(InvalidStateError, ExceptionMessages::f
ailedToExecute("iterateNext", "XPathResult", "The document has mutated since the
result was returned.")); | 189 exceptionState.throwDOMException(InvalidStateError, "The document has mu
tated since the result was returned."); |
| 191 return 0; | 190 return 0; |
| 192 } | 191 } |
| 193 | 192 |
| 194 if (m_nodeSetPosition + 1 > m_nodeSet.size()) | 193 if (m_nodeSetPosition + 1 > m_nodeSet.size()) |
| 195 return 0; | 194 return 0; |
| 196 | 195 |
| 197 Node* node = m_nodeSet[m_nodeSetPosition]; | 196 Node* node = m_nodeSet[m_nodeSetPosition]; |
| 198 | 197 |
| 199 m_nodeSetPosition++; | 198 m_nodeSetPosition++; |
| 200 | 199 |
| 201 return node; | 200 return node; |
| 202 } | 201 } |
| 203 | 202 |
| 204 Node* XPathResult::snapshotItem(unsigned long index, ExceptionState& exceptionSt
ate) | 203 Node* XPathResult::snapshotItem(unsigned long index, ExceptionState& exceptionSt
ate) |
| 205 { | 204 { |
| 206 if (resultType() != UNORDERED_NODE_SNAPSHOT_TYPE && resultType() != ORDERED_
NODE_SNAPSHOT_TYPE) { | 205 if (resultType() != UNORDERED_NODE_SNAPSHOT_TYPE && resultType() != ORDERED_
NODE_SNAPSHOT_TYPE) { |
| 207 exceptionState.throwTypeError(ExceptionMessages::failedToExecute("snapsh
otItem", "XPathResult", "The result type is not a snapshot.")); | 206 exceptionState.throwTypeError("The result type is not a snapshot."); |
| 208 return 0; | 207 return 0; |
| 209 } | 208 } |
| 210 | 209 |
| 211 const NodeSet& nodes = m_value.toNodeSet(); | 210 const NodeSet& nodes = m_value.toNodeSet(); |
| 212 if (index >= nodes.size()) | 211 if (index >= nodes.size()) |
| 213 return 0; | 212 return 0; |
| 214 | 213 |
| 215 return nodes[index]; | 214 return nodes[index]; |
| 216 } | 215 } |
| 217 | 216 |
| 218 } | 217 } |
| OLD | NEW |