blob: 6e60952f3c61b5b23f11a3baa5a52238b9465686 [file] [log] [blame]
andersca75fd42c2006-05-08 21:27:25 +00001/*
ap@webkit.org66fb8552009-05-28 16:23:32 +00002 * Copyright (C) 2005 Frerich Raabe <raabe@kde.org>
ap@webkit.org68a5bc62009-06-02 17:59:09 +00003 * Copyright (C) 2006, 2009 Apple Inc. All rights reserved.
weinige2750b32007-02-17 22:09:10 +00004 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
andersca75fd42c2006-05-08 21:27:25 +00005 *
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 */
darina9406af2006-06-04 23:03:41 +000027
andersca75fd42c2006-05-08 21:27:25 +000028#include "config.h"
darina9406af2006-06-04 23:03:41 +000029#include "XPathStep.h"
andersca75fd42c2006-05-08 21:27:25 +000030
mjsd2948ef2007-02-26 19:29:04 +000031#if ENABLE(XPATH)
andersca75fd42c2006-05-08 21:27:25 +000032
darin@apple.com9a925fa2009-05-04 18:00:34 +000033#include "Attr.h"
ap6a2f4882007-02-11 09:05:04 +000034#include "Document.h"
darina51de912007-04-29 20:32:51 +000035#include "Element.h"
darin@apple.comc91332e2009-03-30 17:22:35 +000036#include "NamedNodeMap.h"
ap@apple.com38221c62010-01-18 22:05:55 +000037#include "XMLNSNames.h"
andersca0315a982006-06-04 08:08:31 +000038#include "XPathParser.h"
apce79f362007-03-20 17:21:07 +000039#include "XPathUtil.h"
andersca75fd42c2006-05-08 21:27:25 +000040
41namespace WebCore {
42namespace XPath {
43
ap94457f12007-03-11 08:21:13 +000044Step::Step(Axis axis, const NodeTest& nodeTest, const Vector<Predicate*>& predicates)
weinige2750b32007-02-17 22:09:10 +000045 : m_axis(axis)
46 , m_nodeTest(nodeTest)
47 , m_predicates(predicates)
andersca75fd42c2006-05-08 21:27:25 +000048{
weinige2750b32007-02-17 22:09:10 +000049}
50
andersca75fd42c2006-05-08 21:27:25 +000051Step::~Step()
52{
53 deleteAllValues(m_predicates);
ap@webkit.org84b68192009-06-01 04:18:09 +000054 deleteAllValues(m_nodeTest.mergedPredicates());
55}
56
57void Step::optimize()
58{
59 // Evaluate predicates as part of node test if possible to avoid building unnecessary NodeSets.
60 // 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.
61 // 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].
62 Vector<Predicate*> remainingPredicates;
63 for (size_t i = 0; i < m_predicates.size(); ++i) {
64 Predicate* predicate = m_predicates[i];
65 if ((!predicate->isContextPositionSensitive() || m_nodeTest.mergedPredicates().isEmpty()) && !predicate->isContextSizeSensitive() && remainingPredicates.isEmpty()) {
66 m_nodeTest.mergedPredicates().append(predicate);
67 } else
68 remainingPredicates.append(predicate);
69 }
70 swap(remainingPredicates, m_predicates);
71}
72
73void optimizeStepPair(Step* first, Step* second, bool& dropSecondStep)
74{
75 dropSecondStep = false;
76
77 if (first->m_axis == Step::DescendantOrSelfAxis
78 && first->m_nodeTest.kind() == Step::NodeTest::AnyNodeTest
ap@webkit.orgb9722a62009-06-01 05:23:08 +000079 && !first->m_predicates.size()
80 && !first->m_nodeTest.mergedPredicates().size()) {
ap@webkit.org84b68192009-06-01 04:18:09 +000081
82 ASSERT(first->m_nodeTest.data().isEmpty());
83 ASSERT(first->m_nodeTest.namespaceURI().isEmpty());
84
85 // Optimize the common case of "//" AKA /descendant-or-self::node()/child::NodeTest to /descendant::NodeTest.
86 if (second->m_axis == Step::ChildAxis && second->predicatesAreContextListInsensitive()) {
87 first->m_axis = Step::DescendantAxis;
88 first->m_nodeTest = Step::NodeTest(second->m_nodeTest.kind(), second->m_nodeTest.data(), second->m_nodeTest.namespaceURI());
89 swap(second->m_nodeTest.mergedPredicates(), first->m_nodeTest.mergedPredicates());
90 swap(second->m_predicates, first->m_predicates);
91 first->optimize();
92 dropSecondStep = true;
93 }
94 }
95}
96
97bool Step::predicatesAreContextListInsensitive() const
98{
99 for (size_t i = 0; i < m_predicates.size(); ++i) {
100 Predicate* predicate = m_predicates[i];
101 if (predicate->isContextPositionSensitive() || predicate->isContextSizeSensitive())
102 return false;
103 }
104
105 for (size_t i = 0; i < m_nodeTest.mergedPredicates().size(); ++i) {
106 Predicate* predicate = m_nodeTest.mergedPredicates()[i];
107 if (predicate->isContextPositionSensitive() || predicate->isContextSizeSensitive())
108 return false;
109 }
110
111 return true;
andersca75fd42c2006-05-08 21:27:25 +0000112}
113
apf0b81e02007-03-30 20:31:26 +0000114void Step::evaluate(Node* context, NodeSet& nodes) const
andersca75fd42c2006-05-08 21:27:25 +0000115{
ap94457f12007-03-11 08:21:13 +0000116 EvaluationContext& evaluationContext = Expression::evaluationContext();
ap@webkit.org84b68192009-06-01 04:18:09 +0000117 evaluationContext.position = 0;
118
119 nodesInAxis(context, nodes);
120
121 // Check predicates that couldn't be merged into node test.
andersca75fd42c2006-05-08 21:27:25 +0000122 for (unsigned i = 0; i < m_predicates.size(); i++) {
123 Predicate* predicate = m_predicates[i];
124
apce79f362007-03-20 17:21:07 +0000125 NodeSet newNodes;
126 if (!nodes.isSorted())
127 newNodes.markSorted(false);
128
ap94457f12007-03-11 08:21:13 +0000129 for (unsigned j = 0; j < nodes.size(); j++) {
apce79f362007-03-20 17:21:07 +0000130 Node* node = nodes[j];
andersca75fd42c2006-05-08 21:27:25 +0000131
ap@webkit.org84b68192009-06-01 04:18:09 +0000132 evaluationContext.node = node;
ap@webkit.org891af4c2007-11-16 06:04:45 +0000133 evaluationContext.size = nodes.size();
134 evaluationContext.position = j + 1;
andersca75fd42c2006-05-08 21:27:25 +0000135 if (predicate->evaluate())
ap94457f12007-03-11 08:21:13 +0000136 newNodes.append(node);
andersca75fd42c2006-05-08 21:27:25 +0000137 }
138
ap94457f12007-03-11 08:21:13 +0000139 nodes.swap(newNodes);
andersca75fd42c2006-05-08 21:27:25 +0000140 }
andersca75fd42c2006-05-08 21:27:25 +0000141}
142
ap@webkit.org66fb8552009-05-28 16:23:32 +0000143static inline Node::NodeType primaryNodeType(Step::Axis axis)
andersca75fd42c2006-05-08 21:27:25 +0000144{
ap@webkit.org66fb8552009-05-28 16:23:32 +0000145 switch (axis) {
146 case Step::AttributeAxis:
147 return Node::ATTRIBUTE_NODE;
148 case Step::NamespaceAxis:
149 return Node::XPATH_NAMESPACE_NODE;
150 default:
151 return Node::ELEMENT_NODE;
andersca75fd42c2006-05-08 21:27:25 +0000152 }
andersca75fd42c2006-05-08 21:27:25 +0000153}
154
ap@webkit.org84b68192009-06-01 04:18:09 +0000155// Evaluate NodeTest without considering merged predicates.
156static inline bool nodeMatchesBasicTest(Node* node, Step::Axis axis, const Step::NodeTest& nodeTest)
andersca75fd42c2006-05-08 21:27:25 +0000157{
ap@webkit.org66fb8552009-05-28 16:23:32 +0000158 switch (nodeTest.kind()) {
159 case Step::NodeTest::TextNodeTest:
apb4371822007-03-25 08:39:27 +0000160 return node->nodeType() == Node::TEXT_NODE || node->nodeType() == Node::CDATA_SECTION_NODE;
ap@webkit.org66fb8552009-05-28 16:23:32 +0000161 case Step::NodeTest::CommentNodeTest:
apb4371822007-03-25 08:39:27 +0000162 return node->nodeType() == Node::COMMENT_NODE;
ap@webkit.org66fb8552009-05-28 16:23:32 +0000163 case Step::NodeTest::ProcessingInstructionNodeTest: {
164 const AtomicString& name = nodeTest.data();
apb4371822007-03-25 08:39:27 +0000165 return node->nodeType() == Node::PROCESSING_INSTRUCTION_NODE && (name.isEmpty() || node->nodeName() == name);
166 }
ap@webkit.org66fb8552009-05-28 16:23:32 +0000167 case Step::NodeTest::AnyNodeTest:
apb4371822007-03-25 08:39:27 +0000168 return true;
ap@webkit.org66fb8552009-05-28 16:23:32 +0000169 case Step::NodeTest::NameTest: {
170 const AtomicString& name = nodeTest.data();
171 const AtomicString& namespaceURI = nodeTest.namespaceURI();
apb4371822007-03-25 08:39:27 +0000172
ap@webkit.org66fb8552009-05-28 16:23:32 +0000173 if (axis == Step::AttributeAxis) {
ap16c9b5f2007-03-28 16:48:16 +0000174 ASSERT(node->isAttributeNode());
175
apb4371822007-03-25 08:39:27 +0000176 // In XPath land, namespace nodes are not accessible on the attribute axis.
ap@apple.com38221c62010-01-18 22:05:55 +0000177 if (node->namespaceURI() == XMLNSNames::xmlnsNamespaceURI)
apb4371822007-03-25 08:39:27 +0000178 return false;
andersca75fd42c2006-05-08 21:27:25 +0000179
ap@webkit.org66fb8552009-05-28 16:23:32 +0000180 if (name == starAtom)
ap16c9b5f2007-03-28 16:48:16 +0000181 return namespaceURI.isEmpty() || node->namespaceURI() == namespaceURI;
182
183 return node->localName() == name && node->namespaceURI() == namespaceURI;
ap94457f12007-03-11 08:21:13 +0000184 }
ap16c9b5f2007-03-28 16:48:16 +0000185
ap@webkit.org66fb8552009-05-28 16:23:32 +0000186 // Node test on the namespace axis is not implemented yet, the caller has a check for it.
187 ASSERT(axis != Step::NamespaceAxis);
ap16c9b5f2007-03-28 16:48:16 +0000188
ap@webkit.org43495fb2009-05-27 17:21:02 +0000189 // For other axes, the principal node type is element.
ap@webkit.org66fb8552009-05-28 16:23:32 +0000190 ASSERT(primaryNodeType(axis) == Node::ELEMENT_NODE);
ap@webkit.org43495fb2009-05-27 17:21:02 +0000191 if (node->nodeType() != Node::ELEMENT_NODE)
192 return false;
193
ap@webkit.org66fb8552009-05-28 16:23:32 +0000194 if (name == starAtom)
ap@webkit.org43495fb2009-05-27 17:21:02 +0000195 return namespaceURI.isEmpty() || namespaceURI == node->namespaceURI();
196
ap@apple.com84ab1192009-11-02 18:41:55 +0000197 if (node->document()->isHTMLDocument()) {
198 if (node->isHTMLElement()) {
199 // Paths without namespaces should match HTML elements in HTML documents despite those having an XHTML namespace. Names are compared case-insensitively.
200 return equalIgnoringCase(static_cast<Element*>(node)->localName(), name) && (namespaceURI.isNull() || namespaceURI == node->namespaceURI());
201 }
202 // An expression without any prefix shouldn't match no-namespace nodes (because HTML5 says so).
203 return static_cast<Element*>(node)->hasLocalName(name) && namespaceURI == node->namespaceURI() && !namespaceURI.isNull();
ap@webkit.org43495fb2009-05-27 17:21:02 +0000204 }
205 return static_cast<Element*>(node)->hasLocalName(name) && namespaceURI == node->namespaceURI();
ap94457f12007-03-11 08:21:13 +0000206 }
207 }
208 ASSERT_NOT_REACHED();
apb4371822007-03-25 08:39:27 +0000209 return false;
andersca75fd42c2006-05-08 21:27:25 +0000210}
211
ap@webkit.org84b68192009-06-01 04:18:09 +0000212static inline bool nodeMatches(Node* node, Step::Axis axis, const Step::NodeTest& nodeTest)
213{
214 if (!nodeMatchesBasicTest(node, axis, nodeTest))
215 return false;
216
217 EvaluationContext& evaluationContext = Expression::evaluationContext();
218
219 // Only the first merged predicate may depend on position.
220 ++evaluationContext.position;
221
222 const Vector<Predicate*>& mergedPredicates = nodeTest.mergedPredicates();
223 for (unsigned i = 0; i < mergedPredicates.size(); i++) {
224 Predicate* predicate = mergedPredicates[i];
225
226 evaluationContext.node = node;
227 // No need to set context size - we only get here when evaluating predicates that do not depend on it.
228 if (!predicate->evaluate())
229 return false;
230 }
231
232 return true;
233}
234
235// Result nodes are ordered in axis order. Node test (including merged predicates) is applied.
ap@webkit.org66fb8552009-05-28 16:23:32 +0000236void Step::nodesInAxis(Node* context, NodeSet& nodes) const
andersca75fd42c2006-05-08 21:27:25 +0000237{
ap@webkit.org66fb8552009-05-28 16:23:32 +0000238 ASSERT(nodes.isEmpty());
239 switch (m_axis) {
240 case ChildAxis:
241 if (context->isAttributeNode()) // In XPath model, attribute nodes do not have children.
242 return;
243
244 for (Node* n = context->firstChild(); n; n = n->nextSibling())
245 if (nodeMatches(n, ChildAxis, m_nodeTest))
246 nodes.append(n);
247 return;
248 case DescendantAxis:
249 if (context->isAttributeNode()) // In XPath model, attribute nodes do not have children.
250 return;
251
252 for (Node* n = context->firstChild(); n; n = n->traverseNextNode(context))
253 if (nodeMatches(n, DescendantAxis, m_nodeTest))
254 nodes.append(n);
255 return;
256 case ParentAxis:
257 if (context->isAttributeNode()) {
258 Node* n = static_cast<Attr*>(context)->ownerElement();
259 if (nodeMatches(n, ParentAxis, m_nodeTest))
260 nodes.append(n);
261 } else {
262 Node* n = context->parentNode();
263 if (n && nodeMatches(n, ParentAxis, m_nodeTest))
264 nodes.append(n);
265 }
266 return;
267 case AncestorAxis: {
268 Node* n = context;
269 if (context->isAttributeNode()) {
270 n = static_cast<Attr*>(context)->ownerElement();
271 if (nodeMatches(n, AncestorAxis, m_nodeTest))
272 nodes.append(n);
273 }
274 for (n = n->parentNode(); n; n = n->parentNode())
275 if (nodeMatches(n, AncestorAxis, m_nodeTest))
276 nodes.append(n);
277 nodes.markSorted(false);
278 return;
279 }
280 case FollowingSiblingAxis:
281 if (context->nodeType() == Node::ATTRIBUTE_NODE ||
282 context->nodeType() == Node::XPATH_NAMESPACE_NODE)
283 return;
284
285 for (Node* n = context->nextSibling(); n; n = n->nextSibling())
286 if (nodeMatches(n, FollowingSiblingAxis, m_nodeTest))
287 nodes.append(n);
288 return;
289 case PrecedingSiblingAxis:
290 if (context->nodeType() == Node::ATTRIBUTE_NODE ||
291 context->nodeType() == Node::XPATH_NAMESPACE_NODE)
292 return;
293
294 for (Node* n = context->previousSibling(); n; n = n->previousSibling())
295 if (nodeMatches(n, PrecedingSiblingAxis, m_nodeTest))
296 nodes.append(n);
297
298 nodes.markSorted(false);
299 return;
300 case FollowingAxis:
301 if (context->isAttributeNode()) {
302 Node* p = static_cast<Attr*>(context)->ownerElement();
303 while ((p = p->traverseNextNode()))
304 if (nodeMatches(p, FollowingAxis, m_nodeTest))
305 nodes.append(p);
306 } else {
307 for (Node* p = context; !isRootDomNode(p); p = p->parentNode()) {
308 for (Node* n = p->nextSibling(); n; n = n->nextSibling()) {
309 if (nodeMatches(n, FollowingAxis, m_nodeTest))
310 nodes.append(n);
311 for (Node* c = n->firstChild(); c; c = c->traverseNextNode(n))
312 if (nodeMatches(c, FollowingAxis, m_nodeTest))
313 nodes.append(c);
314 }
315 }
316 }
317 return;
318 case PrecedingAxis: {
319 if (context->isAttributeNode())
320 context = static_cast<Attr*>(context)->ownerElement();
321
322 Node* n = context;
323 while (Node* parent = n->parent()) {
324 for (n = n->traversePreviousNode(); n != parent; n = n->traversePreviousNode())
325 if (nodeMatches(n, PrecedingAxis, m_nodeTest))
326 nodes.append(n);
327 n = parent;
328 }
329 nodes.markSorted(false);
330 return;
331 }
332 case AttributeAxis: {
333 if (context->nodeType() != Node::ELEMENT_NODE)
334 return;
335
336 // Avoid lazily creating attribute nodes for attributes that we do not need anyway.
337 if (m_nodeTest.kind() == NodeTest::NameTest && m_nodeTest.data() != starAtom) {
338 RefPtr<Node> n = static_cast<Element*>(context)->getAttributeNodeNS(m_nodeTest.namespaceURI(), m_nodeTest.data());
ap@apple.com38221c62010-01-18 22:05:55 +0000339 if (n && n->namespaceURI() != XMLNSNames::xmlnsNamespaceURI) { // In XPath land, namespace nodes are not accessible on the attribute axis.
ap@webkit.org84b68192009-06-01 04:18:09 +0000340 if (nodeMatches(n.get(), AttributeAxis, m_nodeTest)) // Still need to check merged predicates.
341 nodes.append(n.release());
342 }
ap@webkit.org66fb8552009-05-28 16:23:32 +0000343 return;
344 }
345
346 NamedNodeMap* attrs = context->attributes();
347 if (!attrs)
348 return;
349
350 for (unsigned i = 0; i < attrs->length(); ++i) {
351 RefPtr<Attr> attr = attrs->attributeItem(i)->createAttrIfNeeded(static_cast<Element*>(context));
352 if (nodeMatches(attr.get(), AttributeAxis, m_nodeTest))
353 nodes.append(attr.release());
354 }
355 return;
356 }
andersca75fd42c2006-05-08 21:27:25 +0000357 case NamespaceAxis:
ap@webkit.org66fb8552009-05-28 16:23:32 +0000358 // XPath namespace nodes are not implemented yet.
359 return;
360 case SelfAxis:
361 if (nodeMatches(context, SelfAxis, m_nodeTest))
362 nodes.append(context);
363 return;
364 case DescendantOrSelfAxis:
365 if (nodeMatches(context, DescendantOrSelfAxis, m_nodeTest))
366 nodes.append(context);
367 if (context->isAttributeNode()) // In XPath model, attribute nodes do not have children.
368 return;
369
370 for (Node* n = context->firstChild(); n; n = n->traverseNextNode(context))
371 if (nodeMatches(n, DescendantOrSelfAxis, m_nodeTest))
372 nodes.append(n);
373 return;
374 case AncestorOrSelfAxis: {
375 if (nodeMatches(context, AncestorOrSelfAxis, m_nodeTest))
376 nodes.append(context);
377 Node* n = context;
378 if (context->isAttributeNode()) {
379 n = static_cast<Attr*>(context)->ownerElement();
380 if (nodeMatches(n, AncestorOrSelfAxis, m_nodeTest))
381 nodes.append(n);
382 }
383 for (n = n->parentNode(); n; n = n->parentNode())
384 if (nodeMatches(n, AncestorOrSelfAxis, m_nodeTest))
385 nodes.append(n);
386
387 nodes.markSorted(false);
388 return;
389 }
andersca75fd42c2006-05-08 21:27:25 +0000390 }
ap@webkit.org66fb8552009-05-28 16:23:32 +0000391 ASSERT_NOT_REACHED();
andersca75fd42c2006-05-08 21:27:25 +0000392}
393
ap@webkit.org66fb8552009-05-28 16:23:32 +0000394
andersca75fd42c2006-05-08 21:27:25 +0000395}
396}
397
mjsd2948ef2007-02-26 19:29:04 +0000398#endif // ENABLE(XPATH)