XPathPath.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  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>
  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. */
  27. #include "config.h"
  28. #include "XPathPath.h"
  29. #include "Document.h"
  30. #include "XPathPredicate.h"
  31. #include "XPathStep.h"
  32. #include "XPathValue.h"
  33. namespace WebCore {
  34. namespace XPath {
  35. Filter::Filter(Expression* expr, const Vector<Predicate*>& predicates)
  36. : m_expr(expr), m_predicates(predicates)
  37. {
  38. setIsContextNodeSensitive(m_expr->isContextNodeSensitive());
  39. setIsContextPositionSensitive(m_expr->isContextPositionSensitive());
  40. setIsContextSizeSensitive(m_expr->isContextSizeSensitive());
  41. }
  42. Filter::~Filter()
  43. {
  44. delete m_expr;
  45. deleteAllValues(m_predicates);
  46. }
  47. Value Filter::evaluate() const
  48. {
  49. Value v = m_expr->evaluate();
  50. NodeSet& nodes = v.modifiableNodeSet();
  51. nodes.sort();
  52. EvaluationContext& evaluationContext = Expression::evaluationContext();
  53. for (unsigned i = 0; i < m_predicates.size(); i++) {
  54. NodeSet newNodes;
  55. evaluationContext.size = nodes.size();
  56. evaluationContext.position = 0;
  57. for (unsigned j = 0; j < nodes.size(); j++) {
  58. Node* node = nodes[j];
  59. evaluationContext.node = node;
  60. ++evaluationContext.position;
  61. if (m_predicates[i]->evaluate())
  62. newNodes.append(node);
  63. }
  64. nodes.swap(newNodes);
  65. }
  66. return v;
  67. }
  68. LocationPath::LocationPath()
  69. : m_absolute(false)
  70. {
  71. setIsContextNodeSensitive(true);
  72. }
  73. LocationPath::~LocationPath()
  74. {
  75. deleteAllValues(m_steps);
  76. }
  77. Value LocationPath::evaluate() const
  78. {
  79. EvaluationContext& evaluationContext = Expression::evaluationContext();
  80. EvaluationContext backupContext = evaluationContext;
  81. // http://www.w3.org/TR/xpath/
  82. // Section 2, Location Paths:
  83. // "/ selects the document root (which is always the parent of the document element)"
  84. // "A / by itself selects the root node of the document containing the context node."
  85. // In the case of a tree that is detached from the document, we violate
  86. // the spec and treat / as the root node of the detached tree.
  87. // This is for compatibility with Firefox, and also seems like a more
  88. // logical treatment of where you would expect the "root" to be.
  89. Node* context = evaluationContext.node.get();
  90. if (m_absolute && context->nodeType() != Node::DOCUMENT_NODE) {
  91. if (context->inDocument())
  92. context = context->ownerDocument();
  93. else
  94. context = context->highestAncestor();
  95. }
  96. NodeSet nodes;
  97. nodes.append(context);
  98. evaluate(nodes);
  99. evaluationContext = backupContext;
  100. return Value(nodes, Value::adopt);
  101. }
  102. void LocationPath::evaluate(NodeSet& nodes) const
  103. {
  104. bool resultIsSorted = nodes.isSorted();
  105. for (unsigned i = 0; i < m_steps.size(); i++) {
  106. Step* step = m_steps[i];
  107. NodeSet newNodes;
  108. HashSet<Node*> newNodesSet;
  109. bool needToCheckForDuplicateNodes = !nodes.subtreesAreDisjoint() || (step->axis() != Step::ChildAxis && step->axis() != Step::SelfAxis
  110. && step->axis() != Step::DescendantAxis && step->axis() != Step::DescendantOrSelfAxis && step->axis() != Step::AttributeAxis);
  111. if (needToCheckForDuplicateNodes)
  112. resultIsSorted = false;
  113. // This is a simplified check that can be improved to handle more cases.
  114. if (nodes.subtreesAreDisjoint() && (step->axis() == Step::ChildAxis || step->axis() == Step::SelfAxis))
  115. newNodes.markSubtreesDisjoint(true);
  116. for (unsigned j = 0; j < nodes.size(); j++) {
  117. NodeSet matches;
  118. step->evaluate(nodes[j], matches);
  119. if (!matches.isSorted())
  120. resultIsSorted = false;
  121. for (size_t nodeIndex = 0; nodeIndex < matches.size(); ++nodeIndex) {
  122. Node* node = matches[nodeIndex];
  123. if (!needToCheckForDuplicateNodes || newNodesSet.add(node).isNewEntry)
  124. newNodes.append(node);
  125. }
  126. }
  127. nodes.swap(newNodes);
  128. }
  129. nodes.markSorted(resultIsSorted);
  130. }
  131. void LocationPath::appendStep(Step* step)
  132. {
  133. unsigned stepCount = m_steps.size();
  134. if (stepCount) {
  135. bool dropSecondStep;
  136. optimizeStepPair(m_steps[stepCount - 1], step, dropSecondStep);
  137. if (dropSecondStep) {
  138. delete step;
  139. return;
  140. }
  141. }
  142. step->optimize();
  143. m_steps.append(step);
  144. }
  145. void LocationPath::insertFirstStep(Step* step)
  146. {
  147. if (m_steps.size()) {
  148. bool dropSecondStep;
  149. optimizeStepPair(step, m_steps[0], dropSecondStep);
  150. if (dropSecondStep) {
  151. delete m_steps[0];
  152. m_steps[0] = step;
  153. return;
  154. }
  155. }
  156. step->optimize();
  157. m_steps.insert(0, step);
  158. }
  159. Path::Path(Filter* filter, LocationPath* path)
  160. : m_filter(filter)
  161. , m_path(path)
  162. {
  163. setIsContextNodeSensitive(filter->isContextNodeSensitive());
  164. setIsContextPositionSensitive(filter->isContextPositionSensitive());
  165. setIsContextSizeSensitive(filter->isContextSizeSensitive());
  166. }
  167. Path::~Path()
  168. {
  169. delete m_filter;
  170. delete m_path;
  171. }
  172. Value Path::evaluate() const
  173. {
  174. Value v = m_filter->evaluate();
  175. NodeSet& nodes = v.modifiableNodeSet();
  176. m_path->evaluate(nodes);
  177. return v;
  178. }
  179. }
  180. }