2 * Copyright (C) 2005 Frerich Raabe <raabe@kde.org>
3 * Copyright (C) 2006, 2009 Apple Inc.
4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 #include "XPathPath.h"
32 #include "XPathPredicate.h"
33 #include "XPathStep.h"
34 #include "XPathValue.h"
39 Filter::Filter(Expression* expr, const Vector<Predicate*>& predicates)
40 : m_expr(expr), m_predicates(predicates)
42 setIsContextNodeSensitive(m_expr->isContextNodeSensitive());
43 setIsContextPositionSensitive(m_expr->isContextPositionSensitive());
44 setIsContextSizeSensitive(m_expr->isContextSizeSensitive());
50 deleteAllValues(m_predicates);
53 Value Filter::evaluate() const
55 Value v = m_expr->evaluate();
57 NodeSet& nodes = v.modifiableNodeSet();
60 EvaluationContext& evaluationContext = Expression::evaluationContext();
61 for (unsigned i = 0; i < m_predicates.size(); i++) {
63 evaluationContext.size = nodes.size();
64 evaluationContext.position = 0;
66 for (unsigned j = 0; j < nodes.size(); j++) {
67 Node* node = nodes[j];
69 evaluationContext.node = node;
70 ++evaluationContext.position;
72 if (m_predicates[i]->evaluate())
73 newNodes.append(node);
81 LocationPath::LocationPath()
84 setIsContextNodeSensitive(true);
87 LocationPath::~LocationPath()
89 deleteAllValues(m_steps);
92 Value LocationPath::evaluate() const
94 EvaluationContext& evaluationContext = Expression::evaluationContext();
95 EvaluationContext backupContext = evaluationContext;
96 // For absolute location paths, the context node is ignored - the
97 // document's root node is used instead.
98 Node* context = evaluationContext.node.get();
99 if (m_absolute && context->nodeType() != Node::DOCUMENT_NODE)
100 context = context->ownerDocument();
103 nodes.append(context);
106 evaluationContext = backupContext;
107 return Value(nodes, Value::adopt);
110 void LocationPath::evaluate(NodeSet& nodes) const
112 bool resultIsSorted = nodes.isSorted();
114 for (unsigned i = 0; i < m_steps.size(); i++) {
115 Step* step = m_steps[i];
117 HashSet<Node*> newNodesSet;
119 bool needToCheckForDuplicateNodes = !nodes.subtreesAreDisjoint() || (step->axis() != Step::ChildAxis && step->axis() != Step::SelfAxis
120 && step->axis() != Step::DescendantAxis && step->axis() != Step::DescendantOrSelfAxis && step->axis() != Step::AttributeAxis);
122 if (needToCheckForDuplicateNodes)
123 resultIsSorted = false;
125 // This is a simplified check that can be improved to handle more cases.
126 if (nodes.subtreesAreDisjoint() && (step->axis() == Step::ChildAxis || step->axis() == Step::SelfAxis))
127 newNodes.markSubtreesDisjoint(true);
129 for (unsigned j = 0; j < nodes.size(); j++) {
131 step->evaluate(nodes[j], matches);
133 if (!matches.isSorted())
134 resultIsSorted = false;
136 for (size_t nodeIndex = 0; nodeIndex < matches.size(); ++nodeIndex) {
137 Node* node = matches[nodeIndex];
138 if (!needToCheckForDuplicateNodes || newNodesSet.add(node).second)
139 newNodes.append(node);
143 nodes.swap(newNodes);
146 nodes.markSorted(resultIsSorted);
149 void LocationPath::appendStep(Step* step)
151 unsigned stepCount = m_steps.size();
154 optimizeStepPair(m_steps[stepCount - 1], step, dropSecondStep);
155 if (dropSecondStep) {
161 m_steps.append(step);
164 void LocationPath::insertFirstStep(Step* step)
166 if (m_steps.size()) {
168 optimizeStepPair(step, m_steps[0], dropSecondStep);
169 if (dropSecondStep) {
176 m_steps.insert(0, step);
179 Path::Path(Filter* filter, LocationPath* path)
183 setIsContextNodeSensitive(filter->isContextNodeSensitive());
184 setIsContextPositionSensitive(filter->isContextPositionSensitive());
185 setIsContextSizeSensitive(filter->isContextSizeSensitive());
194 Value Path::evaluate() const
196 Value v = m_filter->evaluate();
198 NodeSet& nodes = v.modifiableNodeSet();
199 m_path->evaluate(nodes);