andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 1 | /* |
ap@webkit.org | 35c006d | 2009-06-01 08:27:25 +0000 | [diff] [blame] | 2 | * Copyright (C) 2005 Frerich Raabe <raabe@kde.org> |
| 3 | * Copyright (C) 2006, 2009 Apple Inc. All rights reserved. |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 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 |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 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 |
| 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 |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 26 | |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 27 | #include "config.h" |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 28 | #include "XPathResult.h" |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 29 | |
ap@webkit.org | 35c006d | 2009-06-01 08:27:25 +0000 | [diff] [blame] | 30 | #include "Document.h" |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 31 | #include "ExceptionCode.h" |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 32 | #include "XPathEvaluator.h" |
weinig@apple.com | 97b2d1e | 2008-01-02 05:46:41 +0000 | [diff] [blame] | 33 | #include "XPathException.h" |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | using namespace XPath; |
| 38 | |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 39 | XPathResult::XPathResult(Document& document, const Value& value) |
andersca@apple.com | c442c15 | 2008-02-20 01:55:42 +0000 | [diff] [blame] | 40 | : m_value(value) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 41 | { |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 42 | switch (m_value.type()) { |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 43 | case Value::BooleanValue: |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 44 | m_resultType = BOOLEAN_TYPE; |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 45 | return; |
| 46 | case Value::NumberValue: |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 47 | m_resultType = NUMBER_TYPE; |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 48 | return; |
| 49 | case Value::StringValue: |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 50 | m_resultType = STRING_TYPE; |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 51 | return; |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 52 | case Value::NodeSetValue: |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 53 | m_resultType = UNORDERED_NODE_ITERATOR_TYPE; |
| 54 | m_nodeSetPosition = 0; |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 55 | m_nodeSet = m_value.toNodeSet(); |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 56 | m_document = &document; |
| 57 | m_domTreeVersion = document.domTreeVersion(); |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 58 | return; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 59 | } |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 60 | ASSERT_NOT_REACHED(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | XPathResult::~XPathResult() |
| 64 | { |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 65 | } |
| 66 | |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 67 | ExceptionOr<void> XPathResult::convertTo(unsigned short type) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 68 | { |
| 69 | switch (type) { |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 70 | case ANY_TYPE: |
| 71 | break; |
| 72 | case NUMBER_TYPE: |
| 73 | m_resultType = type; |
| 74 | m_value = m_value.toNumber(); |
| 75 | break; |
| 76 | case STRING_TYPE: |
| 77 | m_resultType = type; |
| 78 | m_value = m_value.toString(); |
| 79 | break; |
| 80 | case BOOLEAN_TYPE: |
| 81 | m_resultType = type; |
| 82 | m_value = m_value.toBoolean(); |
| 83 | break; |
| 84 | case UNORDERED_NODE_ITERATOR_TYPE: |
| 85 | case UNORDERED_NODE_SNAPSHOT_TYPE: |
| 86 | case ANY_UNORDERED_NODE_TYPE: |
| 87 | case FIRST_ORDERED_NODE_TYPE: // This is correct - singleNodeValue() will take care of ordering. |
| 88 | if (!m_value.isNodeSet()) |
| 89 | return Exception { XPathException::TYPE_ERR }; |
| 90 | m_resultType = type; |
| 91 | break; |
| 92 | case ORDERED_NODE_ITERATOR_TYPE: |
| 93 | if (!m_value.isNodeSet()) |
| 94 | return Exception { XPathException::TYPE_ERR }; |
| 95 | m_nodeSet.sort(); |
| 96 | m_resultType = type; |
| 97 | break; |
| 98 | case ORDERED_NODE_SNAPSHOT_TYPE: |
| 99 | if (!m_value.isNodeSet()) |
| 100 | return Exception { XPathException::TYPE_ERR }; |
| 101 | m_value.toNodeSet().sort(); |
| 102 | m_resultType = type; |
| 103 | break; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 104 | } |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 105 | return { }; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | unsigned short XPathResult::resultType() const |
| 109 | { |
| 110 | return m_resultType; |
| 111 | } |
| 112 | |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 113 | ExceptionOr<double> XPathResult::numberValue() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 114 | { |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 115 | if (resultType() != NUMBER_TYPE) |
| 116 | return Exception { XPathException::TYPE_ERR }; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 117 | return m_value.toNumber(); |
| 118 | } |
| 119 | |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 120 | ExceptionOr<String> XPathResult::stringValue() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 121 | { |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 122 | if (resultType() != STRING_TYPE) |
| 123 | return Exception { XPathException::TYPE_ERR }; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 124 | return m_value.toString(); |
| 125 | } |
| 126 | |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 127 | ExceptionOr<bool> XPathResult::booleanValue() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 128 | { |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 129 | if (resultType() != BOOLEAN_TYPE) |
| 130 | return Exception { XPathException::TYPE_ERR }; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 131 | return m_value.toBoolean(); |
| 132 | } |
| 133 | |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 134 | ExceptionOr<Node*> XPathResult::singleNodeValue() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 135 | { |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 136 | if (resultType() != ANY_UNORDERED_NODE_TYPE && resultType() != FIRST_ORDERED_NODE_TYPE) |
| 137 | return Exception { XPathException::TYPE_ERR }; |
| 138 | |
| 139 | auto& nodes = m_value.toNodeSet(); |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 140 | if (resultType() == FIRST_ORDERED_NODE_TYPE) |
| 141 | return nodes.firstNode(); |
| 142 | else |
| 143 | return nodes.anyNode(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 144 | } |
| 145 | |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 146 | bool XPathResult::invalidIteratorState() const |
| 147 | { |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 148 | if (resultType() != UNORDERED_NODE_ITERATOR_TYPE && resultType() != ORDERED_NODE_ITERATOR_TYPE) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 149 | return false; |
ap@webkit.org | 35c006d | 2009-06-01 08:27:25 +0000 | [diff] [blame] | 150 | |
| 151 | ASSERT(m_document); |
| 152 | return m_document->domTreeVersion() != m_domTreeVersion; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 153 | } |
| 154 | |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 155 | ExceptionOr<unsigned> XPathResult::snapshotLength() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 156 | { |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 157 | if (resultType() != UNORDERED_NODE_SNAPSHOT_TYPE && resultType() != ORDERED_NODE_SNAPSHOT_TYPE) |
| 158 | return Exception { XPathException::TYPE_ERR }; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 159 | |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 160 | return m_value.toNodeSet().size(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 161 | } |
| 162 | |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 163 | ExceptionOr<Node*> XPathResult::iterateNext() |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 164 | { |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 165 | if (resultType() != UNORDERED_NODE_ITERATOR_TYPE && resultType() != ORDERED_NODE_ITERATOR_TYPE) |
| 166 | return Exception { XPathException::TYPE_ERR }; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 167 | |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 168 | if (invalidIteratorState()) |
| 169 | return Exception { INVALID_STATE_ERR }; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 170 | |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 171 | if (m_nodeSetPosition >= m_nodeSet.size()) |
| 172 | return nullptr; |
| 173 | |
| 174 | return m_nodeSet[m_nodeSetPosition++]; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 175 | } |
| 176 | |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 177 | ExceptionOr<Node*> XPathResult::snapshotItem(unsigned index) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 178 | { |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 179 | if (resultType() != UNORDERED_NODE_SNAPSHOT_TYPE && resultType() != ORDERED_NODE_SNAPSHOT_TYPE) |
| 180 | return Exception { XPathException::TYPE_ERR }; |
| 181 | |
| 182 | auto& nodes = m_value.toNodeSet(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 183 | if (index >= nodes.size()) |
darin@apple.com | 4b47a4d | 2016-10-19 16:57:31 +0000 | [diff] [blame] | 184 | return nullptr; |
| 185 | |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 186 | return nodes[index]; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | } |