andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 1 | /* |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 2 | * Copyright (C) 2005 Frerich Raabe <raabe@kde.org> |
ap@webkit.org | 68a5bc6 | 2009-06-02 17:59:09 +0000 | [diff] [blame] | 3 | * Copyright (C) 2006, 2009 Apple Inc. All rights reserved. |
weinig | e2750b3 | 2007-02-17 22:09:10 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 27 | |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 28 | #include "config.h" |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 29 | #include "XPathStep.h" |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 30 | |
darin@apple.com | 9a925fa | 2009-05-04 18:00:34 +0000 | [diff] [blame] | 31 | #include "Attr.h" |
ap | 6a2f488 | 2007-02-11 09:05:04 +0000 | [diff] [blame] | 32 | #include "Document.h" |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 33 | #include "HTMLElement.h" |
antti@apple.com | 5d47b58 | 2012-12-11 00:13:29 +0000 | [diff] [blame] | 34 | #include "NodeTraversal.h" |
ap@apple.com | 38221c6 | 2010-01-18 22:05:55 +0000 | [diff] [blame] | 35 | #include "XMLNSNames.h" |
andersca | 0315a98 | 2006-06-04 08:08:31 +0000 | [diff] [blame] | 36 | #include "XPathParser.h" |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 37 | #include "XPathUtil.h" |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 38 | |
| 39 | namespace WebCore { |
| 40 | namespace XPath { |
| 41 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 42 | Step::Step(Axis axis, NodeTest nodeTest) |
weinig | e2750b3 | 2007-02-17 22:09:10 +0000 | [diff] [blame] | 43 | : m_axis(axis) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 44 | , m_nodeTest(std::move(nodeTest)) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | Step::Step(Axis axis, NodeTest nodeTest, Vector<std::unique_ptr<Expression>> predicates) |
| 49 | : m_axis(axis) |
| 50 | , m_nodeTest(std::move(nodeTest)) |
| 51 | , m_predicates(std::move(predicates)) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 52 | { |
weinig | e2750b3 | 2007-02-17 22:09:10 +0000 | [diff] [blame] | 53 | } |
| 54 | |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 55 | Step::~Step() |
| 56 | { |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void Step::optimize() |
| 60 | { |
| 61 | // Evaluate predicates as part of node test if possible to avoid building unnecessary NodeSets. |
| 62 | // E.g., there is no need to build a set of all "foo" nodes to evaluate "foo[@bar]", we can check the predicate while enumerating. |
| 63 | // This optimization can be applied to predicates that are not context node list sensitive, or to first predicate that is only context position sensitive, e.g. foo[position() mod 2 = 0]. |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 64 | Vector<std::unique_ptr<Expression>> remainingPredicates; |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 65 | for (size_t i = 0; i < m_predicates.size(); ++i) { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 66 | auto& predicate = m_predicates[i]; |
| 67 | if ((!predicateIsContextPositionSensitive(*predicate) || m_nodeTest.m_mergedPredicates.isEmpty()) && !predicate->isContextSizeSensitive() && remainingPredicates.isEmpty()) |
| 68 | m_nodeTest.m_mergedPredicates.append(std::move(predicate)); |
| 69 | else |
| 70 | remainingPredicates.append(std::move(predicate)); |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 71 | } |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 72 | m_predicates = std::move(remainingPredicates); |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 73 | } |
| 74 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 75 | void optimizeStepPair(Step& first, Step& second, bool& dropSecondStep) |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 76 | { |
| 77 | dropSecondStep = false; |
| 78 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 79 | if (first.m_axis != Step::DescendantOrSelfAxis) |
| 80 | return; |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 81 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 82 | if (first.m_nodeTest.m_kind != Step::NodeTest::AnyNodeTest) |
| 83 | return; |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 84 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 85 | if (!first.m_predicates.isEmpty()) |
| 86 | return; |
| 87 | |
| 88 | if (!first.m_nodeTest.m_mergedPredicates.isEmpty()) |
| 89 | return; |
| 90 | |
| 91 | ASSERT(first.m_nodeTest.m_data.isEmpty()); |
| 92 | ASSERT(first.m_nodeTest.m_namespaceURI.isEmpty()); |
| 93 | |
| 94 | // Optimize the common case of "//" AKA /descendant-or-self::node()/child::NodeTest to /descendant::NodeTest. |
| 95 | if (second.m_axis != Step::ChildAxis) |
| 96 | return; |
| 97 | |
| 98 | if (!second.predicatesAreContextListInsensitive()) |
| 99 | return; |
| 100 | |
| 101 | first.m_axis = Step::DescendantAxis; |
| 102 | first.m_nodeTest = std::move(second.m_nodeTest); |
| 103 | first.m_predicates = std::move(second.m_predicates); |
| 104 | first.optimize(); |
| 105 | dropSecondStep = true; |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | bool Step::predicatesAreContextListInsensitive() const |
| 109 | { |
| 110 | for (size_t i = 0; i < m_predicates.size(); ++i) { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 111 | auto& predicate = *m_predicates[i]; |
| 112 | if (predicateIsContextPositionSensitive(predicate) || predicate.isContextSizeSensitive()) |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 113 | return false; |
| 114 | } |
| 115 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 116 | for (size_t i = 0; i < m_nodeTest.m_mergedPredicates.size(); ++i) { |
| 117 | auto& predicate = *m_nodeTest.m_mergedPredicates[i]; |
| 118 | if (predicateIsContextPositionSensitive(predicate) || predicate.isContextSizeSensitive()) |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
| 122 | return true; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 123 | } |
| 124 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 125 | void Step::evaluate(Node& context, NodeSet& nodes) const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 126 | { |
ap | 94457f1 | 2007-03-11 08:21:13 +0000 | [diff] [blame] | 127 | EvaluationContext& evaluationContext = Expression::evaluationContext(); |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 128 | evaluationContext.position = 0; |
| 129 | |
| 130 | nodesInAxis(context, nodes); |
| 131 | |
| 132 | // Check predicates that couldn't be merged into node test. |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 133 | for (unsigned i = 0; i < m_predicates.size(); i++) { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 134 | auto& predicate = *m_predicates[i]; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 135 | |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 136 | NodeSet newNodes; |
| 137 | if (!nodes.isSorted()) |
| 138 | newNodes.markSorted(false); |
| 139 | |
ap | 94457f1 | 2007-03-11 08:21:13 +0000 | [diff] [blame] | 140 | for (unsigned j = 0; j < nodes.size(); j++) { |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 141 | Node* node = nodes[j]; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 142 | |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 143 | evaluationContext.node = node; |
ap@webkit.org | 891af4c | 2007-11-16 06:04:45 +0000 | [diff] [blame] | 144 | evaluationContext.size = nodes.size(); |
| 145 | evaluationContext.position = j + 1; |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 146 | if (evaluatePredicate(predicate)) |
ap | 94457f1 | 2007-03-11 08:21:13 +0000 | [diff] [blame] | 147 | newNodes.append(node); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 148 | } |
| 149 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 150 | nodes = std::move(newNodes); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 151 | } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 152 | } |
| 153 | |
mrowe@apple.com | 00e249e | 2013-09-28 10:26:17 +0000 | [diff] [blame] | 154 | #if !ASSERT_DISABLED |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 155 | static inline Node::NodeType primaryNodeType(Step::Axis axis) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 156 | { |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 157 | switch (axis) { |
| 158 | case Step::AttributeAxis: |
| 159 | return Node::ATTRIBUTE_NODE; |
| 160 | case Step::NamespaceAxis: |
| 161 | return Node::XPATH_NAMESPACE_NODE; |
| 162 | default: |
| 163 | return Node::ELEMENT_NODE; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 164 | } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 165 | } |
mrowe@apple.com | 00e249e | 2013-09-28 10:26:17 +0000 | [diff] [blame] | 166 | #endif |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 167 | |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 168 | // Evaluate NodeTest without considering merged predicates. |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 169 | inline bool nodeMatchesBasicTest(Node& node, Step::Axis axis, const Step::NodeTest& nodeTest) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 170 | { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 171 | switch (nodeTest.m_kind) { |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 172 | case Step::NodeTest::TextNodeTest: |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 173 | return node.nodeType() == Node::TEXT_NODE || node.nodeType() == Node::CDATA_SECTION_NODE; |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 174 | case Step::NodeTest::CommentNodeTest: |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 175 | return node.nodeType() == Node::COMMENT_NODE; |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 176 | case Step::NodeTest::ProcessingInstructionNodeTest: { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 177 | const AtomicString& name = nodeTest.m_data; |
| 178 | return node.nodeType() == Node::PROCESSING_INSTRUCTION_NODE && (name.isEmpty() || node.nodeName() == name); |
ap | b437182 | 2007-03-25 08:39:27 +0000 | [diff] [blame] | 179 | } |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 180 | case Step::NodeTest::AnyNodeTest: |
ap | b437182 | 2007-03-25 08:39:27 +0000 | [diff] [blame] | 181 | return true; |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 182 | case Step::NodeTest::NameTest: { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 183 | const AtomicString& name = nodeTest.m_data; |
| 184 | const AtomicString& namespaceURI = nodeTest.m_namespaceURI; |
ap | b437182 | 2007-03-25 08:39:27 +0000 | [diff] [blame] | 185 | |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 186 | if (axis == Step::AttributeAxis) { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 187 | ASSERT(node.isAttributeNode()); |
ap | 16c9b5f | 2007-03-28 16:48:16 +0000 | [diff] [blame] | 188 | |
ap | b437182 | 2007-03-25 08:39:27 +0000 | [diff] [blame] | 189 | // In XPath land, namespace nodes are not accessible on the attribute axis. |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 190 | if (node.namespaceURI() == XMLNSNames::xmlnsNamespaceURI) |
ap | b437182 | 2007-03-25 08:39:27 +0000 | [diff] [blame] | 191 | return false; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 192 | |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 193 | if (name == starAtom) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 194 | return namespaceURI.isEmpty() || node.namespaceURI() == namespaceURI; |
ap | 16c9b5f | 2007-03-28 16:48:16 +0000 | [diff] [blame] | 195 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 196 | return node.localName() == name && node.namespaceURI() == namespaceURI; |
ap | 94457f1 | 2007-03-11 08:21:13 +0000 | [diff] [blame] | 197 | } |
ap | 16c9b5f | 2007-03-28 16:48:16 +0000 | [diff] [blame] | 198 | |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 199 | // Node test on the namespace axis is not implemented yet, the caller has a check for it. |
| 200 | ASSERT(axis != Step::NamespaceAxis); |
ap | 16c9b5f | 2007-03-28 16:48:16 +0000 | [diff] [blame] | 201 | |
ap@webkit.org | 43495fb | 2009-05-27 17:21:02 +0000 | [diff] [blame] | 202 | // For other axes, the principal node type is element. |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 203 | ASSERT(primaryNodeType(axis) == Node::ELEMENT_NODE); |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 204 | if (!node.isElementNode()) |
ap@webkit.org | 43495fb | 2009-05-27 17:21:02 +0000 | [diff] [blame] | 205 | return false; |
| 206 | |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 207 | if (name == starAtom) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 208 | return namespaceURI.isEmpty() || namespaceURI == node.namespaceURI(); |
ap@webkit.org | 43495fb | 2009-05-27 17:21:02 +0000 | [diff] [blame] | 209 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 210 | if (node.document().isHTMLDocument()) { |
| 211 | if (node.isHTMLElement()) { |
ap@apple.com | 84ab119 | 2009-11-02 18:41:55 +0000 | [diff] [blame] | 212 | // Paths without namespaces should match HTML elements in HTML documents despite those having an XHTML namespace. Names are compared case-insensitively. |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 213 | return equalIgnoringCase(toHTMLElement(node).localName(), name) && (namespaceURI.isNull() || namespaceURI == node.namespaceURI()); |
ap@apple.com | 84ab119 | 2009-11-02 18:41:55 +0000 | [diff] [blame] | 214 | } |
| 215 | // An expression without any prefix shouldn't match no-namespace nodes (because HTML5 says so). |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 216 | return toElement(node).hasLocalName(name) && namespaceURI == node.namespaceURI() && !namespaceURI.isNull(); |
ap@webkit.org | 43495fb | 2009-05-27 17:21:02 +0000 | [diff] [blame] | 217 | } |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 218 | return toElement(node).hasLocalName(name) && namespaceURI == node.namespaceURI(); |
ap | 94457f1 | 2007-03-11 08:21:13 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | ASSERT_NOT_REACHED(); |
ap | b437182 | 2007-03-25 08:39:27 +0000 | [diff] [blame] | 222 | return false; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 223 | } |
| 224 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 225 | inline bool nodeMatches(Node& node, Step::Axis axis, const Step::NodeTest& nodeTest) |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 226 | { |
| 227 | if (!nodeMatchesBasicTest(node, axis, nodeTest)) |
| 228 | return false; |
| 229 | |
| 230 | EvaluationContext& evaluationContext = Expression::evaluationContext(); |
| 231 | |
| 232 | // Only the first merged predicate may depend on position. |
| 233 | ++evaluationContext.position; |
| 234 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 235 | auto& mergedPredicates = nodeTest.m_mergedPredicates; |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 236 | for (unsigned i = 0; i < mergedPredicates.size(); i++) { |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 237 | // No need to set context size - we only get here when evaluating predicates that do not depend on it. |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 238 | evaluationContext.node = &node; |
| 239 | if (!evaluatePredicate(*mergedPredicates[i])) |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 240 | return false; |
| 241 | } |
| 242 | |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | // Result nodes are ordered in axis order. Node test (including merged predicates) is applied. |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 247 | void Step::nodesInAxis(Node& context, NodeSet& nodes) const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 248 | { |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 249 | ASSERT(nodes.isEmpty()); |
| 250 | switch (m_axis) { |
| 251 | case ChildAxis: |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 252 | if (context.isAttributeNode()) // In XPath model, attribute nodes do not have children. |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 253 | return; |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 254 | for (Node* node = context.firstChild(); node; node = node->nextSibling()) { |
| 255 | if (nodeMatches(*node, ChildAxis, m_nodeTest)) |
| 256 | nodes.append(node); |
| 257 | } |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 258 | return; |
| 259 | case DescendantAxis: |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 260 | if (context.isAttributeNode()) // In XPath model, attribute nodes do not have children. |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 261 | return; |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 262 | for (Node* node = context.firstChild(); node; node = NodeTraversal::next(node, &context)) { |
| 263 | if (nodeMatches(*node, DescendantAxis, m_nodeTest)) |
| 264 | nodes.append(node); |
| 265 | } |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 266 | return; |
| 267 | case ParentAxis: |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 268 | if (context.isAttributeNode()) { |
| 269 | Element* node = static_cast<Attr&>(context).ownerElement(); |
| 270 | if (nodeMatches(*node, ParentAxis, m_nodeTest)) |
| 271 | nodes.append(node); |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 272 | } else { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 273 | ContainerNode* node = context.parentNode(); |
| 274 | if (node && nodeMatches(*node, ParentAxis, m_nodeTest)) |
| 275 | nodes.append(node); |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 276 | } |
| 277 | return; |
| 278 | case AncestorAxis: { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 279 | Node* node = &context; |
| 280 | if (context.isAttributeNode()) { |
| 281 | node = static_cast<Attr&>(context).ownerElement(); |
| 282 | if (nodeMatches(*node, AncestorAxis, m_nodeTest)) |
| 283 | nodes.append(node); |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 284 | } |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 285 | for (node = node->parentNode(); node; node = node->parentNode()) { |
| 286 | if (nodeMatches(*node, AncestorAxis, m_nodeTest)) |
| 287 | nodes.append(node); |
| 288 | } |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 289 | nodes.markSorted(false); |
| 290 | return; |
| 291 | } |
| 292 | case FollowingSiblingAxis: |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 293 | if (context.nodeType() == Node::ATTRIBUTE_NODE || context.nodeType() == Node::XPATH_NAMESPACE_NODE) |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 294 | return; |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 295 | for (Node* node = context.nextSibling(); node; node = node->nextSibling()) { |
| 296 | if (nodeMatches(*node, FollowingSiblingAxis, m_nodeTest)) |
| 297 | nodes.append(node); |
| 298 | } |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 299 | return; |
| 300 | case PrecedingSiblingAxis: |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 301 | if (context.nodeType() == Node::ATTRIBUTE_NODE || context.nodeType() == Node::XPATH_NAMESPACE_NODE) |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 302 | return; |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 303 | for (Node* node = context.previousSibling(); node; node = node->previousSibling()) { |
| 304 | if (nodeMatches(*node, PrecedingSiblingAxis, m_nodeTest)) |
| 305 | nodes.append(node); |
| 306 | } |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 307 | nodes.markSorted(false); |
| 308 | return; |
| 309 | case FollowingAxis: |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 310 | if (context.isAttributeNode()) { |
| 311 | Node* node = static_cast<Attr&>(context).ownerElement(); |
| 312 | while ((node = NodeTraversal::next(node))) { |
| 313 | if (nodeMatches(*node, FollowingAxis, m_nodeTest)) |
| 314 | nodes.append(node); |
| 315 | } |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 316 | } else { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 317 | for (Node* parent = &context; !isRootDomNode(parent); parent = parent->parentNode()) { |
| 318 | for (Node* node = parent->nextSibling(); node; node = node->nextSibling()) { |
| 319 | if (nodeMatches(*node, FollowingAxis, m_nodeTest)) |
| 320 | nodes.append(node); |
| 321 | for (Node* child = node->firstChild(); child; child = NodeTraversal::next(child, node)) { |
| 322 | if (nodeMatches(*child, FollowingAxis, m_nodeTest)) |
| 323 | nodes.append(child); |
| 324 | } |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | } |
| 328 | return; |
| 329 | case PrecedingAxis: { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 330 | Node* node; |
| 331 | if (context.isAttributeNode()) |
| 332 | node = static_cast<Attr&>(context).ownerElement(); |
| 333 | else |
| 334 | node = &context; |
| 335 | while (ContainerNode* parent = node->parentNode()) { |
| 336 | for (node = NodeTraversal::previous(node); node != parent; node = NodeTraversal::previous(node)) { |
| 337 | if (nodeMatches(*node, PrecedingAxis, m_nodeTest)) |
| 338 | nodes.append(node); |
| 339 | } |
| 340 | node = parent; |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 341 | } |
| 342 | nodes.markSorted(false); |
| 343 | return; |
| 344 | } |
| 345 | case AttributeAxis: { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 346 | if (!context.isElementNode()) |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 347 | return; |
| 348 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 349 | Element& contextElement = toElement(context); |
caio.oliveira@openbossa.org | 062edfb | 2012-02-02 00:20:36 +0000 | [diff] [blame] | 350 | |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 351 | // Avoid lazily creating attribute nodes for attributes that we do not need anyway. |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 352 | if (m_nodeTest.m_kind == NodeTest::NameTest && m_nodeTest.m_data != starAtom) { |
| 353 | RefPtr<Attr> attr = contextElement.getAttributeNodeNS(m_nodeTest.m_namespaceURI, m_nodeTest.m_data); |
| 354 | if (attr && attr->namespaceURI() != XMLNSNames::xmlnsNamespaceURI) { // In XPath land, namespace nodes are not accessible on the attribute axis. |
| 355 | if (nodeMatches(*attr, AttributeAxis, m_nodeTest)) // Still need to check merged predicates. |
| 356 | nodes.append(attr.release()); |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 357 | } |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 361 | if (!contextElement.hasAttributes()) |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 362 | return; |
| 363 | |
benjamin@webkit.org | 64c9533 | 2014-01-21 01:31:37 +0000 | [diff] [blame] | 364 | for (const Attribute& attribute : contextElement.attributesIterator()) { |
| 365 | RefPtr<Attr> attr = contextElement.ensureAttr(attribute.name()); |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 366 | if (nodeMatches(*attr, AttributeAxis, m_nodeTest)) |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 367 | nodes.append(attr.release()); |
| 368 | } |
| 369 | return; |
| 370 | } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 371 | case NamespaceAxis: |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 372 | // XPath namespace nodes are not implemented yet. |
| 373 | return; |
| 374 | case SelfAxis: |
| 375 | if (nodeMatches(context, SelfAxis, m_nodeTest)) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 376 | nodes.append(&context); |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 377 | return; |
| 378 | case DescendantOrSelfAxis: |
| 379 | if (nodeMatches(context, DescendantOrSelfAxis, m_nodeTest)) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 380 | nodes.append(&context); |
| 381 | if (context.isAttributeNode()) // In XPath model, attribute nodes do not have children. |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 382 | return; |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 383 | for (Node* node = context.firstChild(); node; node = NodeTraversal::next(node, &context)) { |
| 384 | if (nodeMatches(*node, DescendantOrSelfAxis, m_nodeTest)) |
| 385 | nodes.append(node); |
| 386 | } |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 387 | return; |
| 388 | case AncestorOrSelfAxis: { |
| 389 | if (nodeMatches(context, AncestorOrSelfAxis, m_nodeTest)) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 390 | nodes.append(&context); |
| 391 | Node* node = &context; |
| 392 | if (context.isAttributeNode()) { |
| 393 | node = static_cast<Attr&>(context).ownerElement(); |
| 394 | if (nodeMatches(*node, AncestorOrSelfAxis, m_nodeTest)) |
| 395 | nodes.append(node); |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 396 | } |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 397 | for (node = node->parentNode(); node; node = node->parentNode()) { |
| 398 | if (nodeMatches(*node, AncestorOrSelfAxis, m_nodeTest)) |
| 399 | nodes.append(node); |
| 400 | } |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 401 | nodes.markSorted(false); |
| 402 | return; |
| 403 | } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 404 | } |
ap@webkit.org | 66fb855 | 2009-05-28 16:23:32 +0000 | [diff] [blame] | 405 | ASSERT_NOT_REACHED(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | } |
| 409 | } |