This source file includes following definitions.
- m_nodeTest
- m_nodeTest
- optimize
- optimizeStepPair
- predicatesAreContextListInsensitive
- evaluate
- primaryNodeType
- nodeMatchesBasicTest
- nodeMatches
- nodesInAxis
#include "config.h"
#include "core/xml/XPathStep.h"
#include "XMLNSNames.h"
#include "core/dom/Attr.h"
#include "core/dom/Document.h"
#include "core/dom/Element.h"
#include "core/dom/NodeTraversal.h"
#include "core/xml/XPathParser.h"
#include "core/xml/XPathUtil.h"
namespace WebCore {
namespace XPath {
Step::Step(Axis axis, const NodeTest& nodeTest)
: m_axis(axis)
, m_nodeTest(nodeTest)
{
}
Step::Step(Axis axis, const NodeTest& nodeTest, Vector<OwnPtr<Predicate> >& predicates)
: m_axis(axis)
, m_nodeTest(nodeTest)
{
m_predicates.swap(predicates);
}
Step::~Step()
{
}
void Step::optimize()
{
Vector<OwnPtr<Predicate> > remainingPredicates;
for (size_t i = 0; i < m_predicates.size(); ++i) {
OwnPtr<Predicate> predicate(m_predicates[i].release());
if ((!predicate->isContextPositionSensitive() || m_nodeTest.mergedPredicates().isEmpty()) && !predicate->isContextSizeSensitive() && remainingPredicates.isEmpty()) {
m_nodeTest.mergedPredicates().append(predicate.release());
} else {
remainingPredicates.append(predicate.release());
}
}
swap(remainingPredicates, m_predicates);
}
void optimizeStepPair(Step* first, Step* second, bool& dropSecondStep)
{
dropSecondStep = false;
if (first->m_axis == Step::DescendantOrSelfAxis
&& first->m_nodeTest.kind() == Step::NodeTest::AnyNodeTest
&& !first->m_predicates.size()
&& !first->m_nodeTest.mergedPredicates().size()) {
ASSERT(first->m_nodeTest.data().isEmpty());
ASSERT(first->m_nodeTest.namespaceURI().isEmpty());
if (second->m_axis == Step::ChildAxis && second->predicatesAreContextListInsensitive()) {
first->m_axis = Step::DescendantAxis;
first->m_nodeTest = Step::NodeTest(second->m_nodeTest.kind(), second->m_nodeTest.data(), second->m_nodeTest.namespaceURI());
swap(second->m_nodeTest.mergedPredicates(), first->m_nodeTest.mergedPredicates());
swap(second->m_predicates, first->m_predicates);
first->optimize();
dropSecondStep = true;
}
}
}
bool Step::predicatesAreContextListInsensitive() const
{
for (size_t i = 0; i < m_predicates.size(); ++i) {
Predicate* predicate = m_predicates[i].get();
if (predicate->isContextPositionSensitive() || predicate->isContextSizeSensitive())
return false;
}
for (size_t i = 0; i < m_nodeTest.mergedPredicates().size(); ++i) {
Predicate* predicate = m_nodeTest.mergedPredicates()[i].get();
if (predicate->isContextPositionSensitive() || predicate->isContextSizeSensitive())
return false;
}
return true;
}
void Step::evaluate(Node* context, NodeSet& nodes) const
{
EvaluationContext& evaluationContext = Expression::evaluationContext();
evaluationContext.position = 0;
nodesInAxis(context, nodes);
for (unsigned i = 0; i < m_predicates.size(); i++) {
Predicate* predicate = m_predicates[i].get();
NodeSet newNodes;
if (!nodes.isSorted())
newNodes.markSorted(false);
for (unsigned j = 0; j < nodes.size(); j++) {
Node* node = nodes[j];
evaluationContext.node = node;
evaluationContext.size = nodes.size();
evaluationContext.position = j + 1;
if (predicate->evaluate())
newNodes.append(node);
}
nodes.swap(newNodes);
}
}
#if !ASSERT_DISABLED
static inline Node::NodeType primaryNodeType(Step::Axis axis)
{
switch (axis) {
case Step::AttributeAxis:
return Node::ATTRIBUTE_NODE;
default:
return Node::ELEMENT_NODE;
}
}
#endif
static inline bool nodeMatchesBasicTest(Node* node, Step::Axis axis, const Step::NodeTest& nodeTest)
{
switch (nodeTest.kind()) {
case Step::NodeTest::TextNodeTest:
return node->nodeType() == Node::TEXT_NODE || node->nodeType() == Node::CDATA_SECTION_NODE;
case Step::NodeTest::CommentNodeTest:
return node->nodeType() == Node::COMMENT_NODE;
case Step::NodeTest::ProcessingInstructionNodeTest: {
const AtomicString& name = nodeTest.data();
return node->nodeType() == Node::PROCESSING_INSTRUCTION_NODE && (name.isEmpty() || node->nodeName() == name);
}
case Step::NodeTest::AnyNodeTest:
return true;
case Step::NodeTest::NameTest: {
const AtomicString& name = nodeTest.data();
const AtomicString& namespaceURI = nodeTest.namespaceURI();
if (axis == Step::AttributeAxis) {
ASSERT(node->isAttributeNode());
if (node->namespaceURI() == XMLNSNames::xmlnsNamespaceURI)
return false;
if (name == starAtom)
return namespaceURI.isEmpty() || node->namespaceURI() == namespaceURI;
return node->localName() == name && node->namespaceURI() == namespaceURI;
}
ASSERT(axis != Step::NamespaceAxis);
ASSERT(primaryNodeType(axis) == Node::ELEMENT_NODE);
if (node->nodeType() != Node::ELEMENT_NODE)
return false;
if (name == starAtom)
return namespaceURI.isEmpty() || namespaceURI == node->namespaceURI();
if (node->document().isHTMLDocument()) {
if (node->isHTMLElement()) {
return equalIgnoringCase(toElement(node)->localName(), name) && (namespaceURI.isNull() || namespaceURI == node->namespaceURI());
}
return toElement(node)->hasLocalName(name) && namespaceURI == node->namespaceURI() && !namespaceURI.isNull();
}
return toElement(node)->hasLocalName(name) && namespaceURI == node->namespaceURI();
}
}
ASSERT_NOT_REACHED();
return false;
}
static inline bool nodeMatches(Node* node, Step::Axis axis, const Step::NodeTest& nodeTest)
{
if (!nodeMatchesBasicTest(node, axis, nodeTest))
return false;
EvaluationContext& evaluationContext = Expression::evaluationContext();
++evaluationContext.position;
const Vector<OwnPtr<Predicate> >& mergedPredicates = nodeTest.mergedPredicates();
for (unsigned i = 0; i < mergedPredicates.size(); i++) {
Predicate* predicate = mergedPredicates[i].get();
evaluationContext.node = node;
if (!predicate->evaluate())
return false;
}
return true;
}
void Step::nodesInAxis(Node* context, NodeSet& nodes) const
{
ASSERT(nodes.isEmpty());
switch (m_axis) {
case ChildAxis:
if (context->isAttributeNode())
return;
for (Node* n = context->firstChild(); n; n = n->nextSibling())
if (nodeMatches(n, ChildAxis, m_nodeTest))
nodes.append(n);
return;
case DescendantAxis:
if (context->isAttributeNode())
return;
for (Node* n = context->firstChild(); n; n = NodeTraversal::next(*n, context))
if (nodeMatches(n, DescendantAxis, m_nodeTest))
nodes.append(n);
return;
case ParentAxis:
if (context->isAttributeNode()) {
Element* n = toAttr(context)->ownerElement();
if (nodeMatches(n, ParentAxis, m_nodeTest))
nodes.append(n);
} else {
ContainerNode* n = context->parentNode();
if (n && nodeMatches(n, ParentAxis, m_nodeTest))
nodes.append(n);
}
return;
case AncestorAxis: {
Node* n = context;
if (context->isAttributeNode()) {
n = toAttr(context)->ownerElement();
if (nodeMatches(n, AncestorAxis, m_nodeTest))
nodes.append(n);
}
for (n = n->parentNode(); n; n = n->parentNode())
if (nodeMatches(n, AncestorAxis, m_nodeTest))
nodes.append(n);
nodes.markSorted(false);
return;
}
case FollowingSiblingAxis:
if (context->nodeType() == Node::ATTRIBUTE_NODE)
return;
for (Node* n = context->nextSibling(); n; n = n->nextSibling())
if (nodeMatches(n, FollowingSiblingAxis, m_nodeTest))
nodes.append(n);
return;
case PrecedingSiblingAxis:
if (context->nodeType() == Node::ATTRIBUTE_NODE)
return;
for (Node* n = context->previousSibling(); n; n = n->previousSibling())
if (nodeMatches(n, PrecedingSiblingAxis, m_nodeTest))
nodes.append(n);
nodes.markSorted(false);
return;
case FollowingAxis:
if (context->isAttributeNode()) {
Node* p = toAttr(context)->ownerElement();
while ((p = NodeTraversal::next(*p))) {
if (nodeMatches(p, FollowingAxis, m_nodeTest))
nodes.append(p);
}
} else {
for (Node* p = context; !isRootDomNode(p); p = p->parentNode()) {
for (Node* n = p->nextSibling(); n; n = n->nextSibling()) {
if (nodeMatches(n, FollowingAxis, m_nodeTest))
nodes.append(n);
for (Node* c = n->firstChild(); c; c = NodeTraversal::next(*c, n))
if (nodeMatches(c, FollowingAxis, m_nodeTest))
nodes.append(c);
}
}
}
return;
case PrecedingAxis: {
if (context->isAttributeNode())
context = toAttr(context)->ownerElement();
Node* n = context;
while (ContainerNode* parent = n->parentNode()) {
for (n = NodeTraversal::previous(*n); n != parent; n = NodeTraversal::previous(*n))
if (nodeMatches(n, PrecedingAxis, m_nodeTest))
nodes.append(n);
n = parent;
}
nodes.markSorted(false);
return;
}
case AttributeAxis: {
if (!context->isElementNode())
return;
Element* contextElement = toElement(context);
if (m_nodeTest.kind() == NodeTest::NameTest && m_nodeTest.data() != starAtom) {
RefPtr<Node> n = contextElement->getAttributeNodeNS(m_nodeTest.namespaceURI(), m_nodeTest.data());
if (n && n->namespaceURI() != XMLNSNames::xmlnsNamespaceURI) {
if (nodeMatches(n.get(), AttributeAxis, m_nodeTest))
nodes.append(n.release());
}
return;
}
if (!contextElement->hasAttributes())
return;
unsigned attributeCount = contextElement->attributeCount();
for (unsigned i = 0; i < attributeCount; ++i) {
RefPtr<Attr> attr = contextElement->ensureAttr(contextElement->attributeItem(i).name());
if (nodeMatches(attr.get(), AttributeAxis, m_nodeTest))
nodes.append(attr.release());
}
return;
}
case NamespaceAxis:
return;
case SelfAxis:
if (nodeMatches(context, SelfAxis, m_nodeTest))
nodes.append(context);
return;
case DescendantOrSelfAxis:
if (nodeMatches(context, DescendantOrSelfAxis, m_nodeTest))
nodes.append(context);
if (context->isAttributeNode())
return;
for (Node* n = context->firstChild(); n; n = NodeTraversal::next(*n, context))
if (nodeMatches(n, DescendantOrSelfAxis, m_nodeTest))
nodes.append(n);
return;
case AncestorOrSelfAxis: {
if (nodeMatches(context, AncestorOrSelfAxis, m_nodeTest))
nodes.append(context);
Node* n = context;
if (context->isAttributeNode()) {
n = toAttr(context)->ownerElement();
if (nodeMatches(n, AncestorOrSelfAxis, m_nodeTest))
nodes.append(n);
}
for (n = n->parentNode(); n; n = n->parentNode())
if (nodeMatches(n, AncestorOrSelfAxis, m_nodeTest))
nodes.append(n);
nodes.markSorted(false);
return;
}
}
ASSERT_NOT_REACHED();
}
}
}