| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2005 Frerich Raabe <raabe@kde.org> | 2 * Copyright (C) 2005 Frerich Raabe <raabe@kde.org> |
| 3 * Copyright (C) 2006, 2009 Apple Inc. | 3 * Copyright (C) 2006, 2009 Apple Inc. |
| 4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> | 4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> |
| 5 * | 5 * |
| 6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
| 8 * are met: | 8 * are met: |
| 9 * | 9 * |
| 10 * 1. Redistributions of source code must retain the above copyright | 10 * 1. Redistributions of source code must retain the above copyright |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 #include "core/dom/Document.h" | 31 #include "core/dom/Document.h" |
| 32 #include "core/dom/NodeTraversal.h" | 32 #include "core/dom/NodeTraversal.h" |
| 33 #include "core/xml/XPathPredicate.h" | 33 #include "core/xml/XPathPredicate.h" |
| 34 #include "core/xml/XPathStep.h" | 34 #include "core/xml/XPathStep.h" |
| 35 #include "core/xml/XPathValue.h" | 35 #include "core/xml/XPathValue.h" |
| 36 | 36 |
| 37 namespace blink { | 37 namespace blink { |
| 38 namespace XPath { | 38 namespace XPath { |
| 39 | 39 |
| 40 Filter::Filter(PassOwnPtrWillBeRawPtr<Expression> expr, WillBeHeapVector<OwnPtrW
illBeMember<Predicate> >& predicates) | 40 Filter::Filter(Expression* expr, HeapVector<Member<Predicate> >& predicates) |
| 41 : m_expr(expr) | 41 : m_expr(expr) |
| 42 { | 42 { |
| 43 m_predicates.swap(predicates); | 43 m_predicates.swap(predicates); |
| 44 setIsContextNodeSensitive(m_expr->isContextNodeSensitive()); | 44 setIsContextNodeSensitive(m_expr->isContextNodeSensitive()); |
| 45 setIsContextPositionSensitive(m_expr->isContextPositionSensitive()); | 45 setIsContextPositionSensitive(m_expr->isContextPositionSensitive()); |
| 46 setIsContextSizeSensitive(m_expr->isContextSizeSensitive()); | 46 setIsContextSizeSensitive(m_expr->isContextSizeSensitive()); |
| 47 } | 47 } |
| 48 | 48 |
| 49 Filter::~Filter() | 49 Filter::~Filter() |
| 50 { | 50 { |
| 51 } | 51 } |
| 52 | 52 |
| 53 void Filter::trace(Visitor* visitor) | 53 void Filter::trace(Visitor* visitor) |
| 54 { | 54 { |
| 55 visitor->trace(m_expr); | 55 visitor->trace(m_expr); |
| 56 visitor->trace(m_predicates); | 56 visitor->trace(m_predicates); |
| 57 Expression::trace(visitor); | 57 Expression::trace(visitor); |
| 58 } | 58 } |
| 59 | 59 |
| 60 Value Filter::evaluate(EvaluationContext& evaluationContext) const | 60 Value Filter::evaluate(EvaluationContext& evaluationContext) const |
| 61 { | 61 { |
| 62 Value v = m_expr->evaluate(evaluationContext); | 62 Value v = m_expr->evaluate(evaluationContext); |
| 63 | 63 |
| 64 NodeSet& nodes = v.modifiableNodeSet(evaluationContext); | 64 NodeSet& nodes = v.modifiableNodeSet(evaluationContext); |
| 65 nodes.sort(); | 65 nodes.sort(); |
| 66 | 66 |
| 67 for (unsigned i = 0; i < m_predicates.size(); i++) { | 67 for (unsigned i = 0; i < m_predicates.size(); i++) { |
| 68 OwnPtrWillBeRawPtr<NodeSet> newNodes(NodeSet::create()); | 68 NodeSet* newNodes = NodeSet::create(); |
| 69 evaluationContext.size = nodes.size(); | 69 evaluationContext.size = nodes.size(); |
| 70 evaluationContext.position = 0; | 70 evaluationContext.position = 0; |
| 71 | 71 |
| 72 for (unsigned j = 0; j < nodes.size(); j++) { | 72 for (unsigned j = 0; j < nodes.size(); j++) { |
| 73 Node* node = nodes[j]; | 73 Node* node = nodes[j]; |
| 74 | 74 |
| 75 evaluationContext.node = node; | 75 evaluationContext.node = node; |
| 76 ++evaluationContext.position; | 76 ++evaluationContext.position; |
| 77 | 77 |
| 78 if (m_predicates[i]->evaluate(evaluationContext)) | 78 if (m_predicates[i]->evaluate(evaluationContext)) |
| 79 newNodes->append(node); | 79 newNodes->append(node); |
| 80 } | 80 } |
| 81 nodes.swap(*newNodes); | 81 nodes.swap(*newNodes); |
| 82 } | 82 } |
| 83 | 83 |
| 84 return v; | 84 return v; |
| 85 } | 85 } |
| 86 | 86 |
| 87 LocationPath::LocationPath() | 87 LocationPath::LocationPath() |
| 88 : m_absolute(false) | 88 : m_absolute(false) |
| 89 { | 89 { |
| 90 setIsContextNodeSensitive(true); | 90 setIsContextNodeSensitive(true); |
| 91 } | 91 } |
| 92 | 92 |
| 93 LocationPath::~LocationPath() | 93 LocationPath::~LocationPath() |
| 94 { | 94 { |
| 95 #if !ENABLE(OILPAN) | |
| 96 deleteAllValues(m_steps); | |
| 97 #endif | |
| 98 } | 95 } |
| 99 | 96 |
| 100 void LocationPath::trace(Visitor* visitor) | 97 void LocationPath::trace(Visitor* visitor) |
| 101 { | 98 { |
| 102 #if ENABLE(OILPAN) | |
| 103 visitor->trace(m_steps); | 99 visitor->trace(m_steps); |
| 104 #endif | |
| 105 Expression::trace(visitor); | 100 Expression::trace(visitor); |
| 106 } | 101 } |
| 107 | 102 |
| 108 Value LocationPath::evaluate(EvaluationContext& evaluationContext) const | 103 Value LocationPath::evaluate(EvaluationContext& evaluationContext) const |
| 109 { | 104 { |
| 110 EvaluationContext clonedContext = evaluationContext; | 105 EvaluationContext clonedContext = evaluationContext; |
| 111 // http://www.w3.org/TR/xpath/ | 106 // http://www.w3.org/TR/xpath/ |
| 112 // Section 2, Location Paths: | 107 // Section 2, Location Paths: |
| 113 // "/ selects the document root (which is always the parent of the document
element)" | 108 // "/ selects the document root (which is always the parent of the document
element)" |
| 114 // "A / by itself selects the root node of the document containing the conte
xt node." | 109 // "A / by itself selects the root node of the document containing the conte
xt node." |
| 115 // In the case of a tree that is detached from the document, we violate | 110 // In the case of a tree that is detached from the document, we violate |
| 116 // the spec and treat / as the root node of the detached tree. | 111 // the spec and treat / as the root node of the detached tree. |
| 117 // This is for compatibility with Firefox, and also seems like a more | 112 // This is for compatibility with Firefox, and also seems like a more |
| 118 // logical treatment of where you would expect the "root" to be. | 113 // logical treatment of where you would expect the "root" to be. |
| 119 Node* context = evaluationContext.node.get(); | 114 Node* context = evaluationContext.node.get(); |
| 120 if (m_absolute && context->nodeType() != Node::DOCUMENT_NODE) { | 115 if (m_absolute && context->nodeType() != Node::DOCUMENT_NODE) { |
| 121 if (context->inDocument()) | 116 if (context->inDocument()) |
| 122 context = context->ownerDocument(); | 117 context = context->ownerDocument(); |
| 123 else | 118 else |
| 124 context = &NodeTraversal::highestAncestorOrSelf(*context); | 119 context = &NodeTraversal::highestAncestorOrSelf(*context); |
| 125 } | 120 } |
| 126 | 121 |
| 127 OwnPtrWillBeRawPtr<NodeSet> nodes(NodeSet::create()); | 122 NodeSet* nodes = NodeSet::create(); |
| 128 nodes->append(context); | 123 nodes->append(context); |
| 129 evaluate(clonedContext, *nodes); | 124 evaluate(clonedContext, *nodes); |
| 130 | 125 |
| 131 return Value(nodes.release(), Value::adopt); | 126 return Value(nodes, Value::adopt); |
| 132 } | 127 } |
| 133 | 128 |
| 134 void LocationPath::evaluate(EvaluationContext& context, NodeSet& nodes) const | 129 void LocationPath::evaluate(EvaluationContext& context, NodeSet& nodes) const |
| 135 { | 130 { |
| 136 bool resultIsSorted = nodes.isSorted(); | 131 bool resultIsSorted = nodes.isSorted(); |
| 137 | 132 |
| 138 for (unsigned i = 0; i < m_steps.size(); i++) { | 133 for (unsigned i = 0; i < m_steps.size(); i++) { |
| 139 Step* step = m_steps[i]; | 134 Step* step = m_steps[i]; |
| 140 OwnPtrWillBeRawPtr<NodeSet> newNodes(NodeSet::create()); | 135 NodeSet* newNodes = NodeSet::create(); |
| 141 WillBeHeapHashSet<RawPtrWillBeMember<Node> > newNodesSet; | 136 WillBeHeapHashSet<RawPtrWillBeMember<Node> > newNodesSet; |
| 142 | 137 |
| 143 bool needToCheckForDuplicateNodes = !nodes.subtreesAreDisjoint() || (ste
p->axis() != Step::ChildAxis && step->axis() != Step::SelfAxis | 138 bool needToCheckForDuplicateNodes = !nodes.subtreesAreDisjoint() || (ste
p->axis() != Step::ChildAxis && step->axis() != Step::SelfAxis |
| 144 && step->axis() != Step::DescendantAxis && step->axis() != Step::Des
cendantOrSelfAxis && step->axis() != Step::AttributeAxis); | 139 && step->axis() != Step::DescendantAxis && step->axis() != Step::Des
cendantOrSelfAxis && step->axis() != Step::AttributeAxis); |
| 145 | 140 |
| 146 if (needToCheckForDuplicateNodes) | 141 if (needToCheckForDuplicateNodes) |
| 147 resultIsSorted = false; | 142 resultIsSorted = false; |
| 148 | 143 |
| 149 // This is a simplified check that can be improved to handle more cases. | 144 // This is a simplified check that can be improved to handle more cases. |
| 150 if (nodes.subtreesAreDisjoint() && (step->axis() == Step::ChildAxis || s
tep->axis() == Step::SelfAxis)) | 145 if (nodes.subtreesAreDisjoint() && (step->axis() == Step::ChildAxis || s
tep->axis() == Step::SelfAxis)) |
| 151 newNodes->markSubtreesDisjoint(true); | 146 newNodes->markSubtreesDisjoint(true); |
| 152 | 147 |
| 153 for (unsigned j = 0; j < nodes.size(); j++) { | 148 for (unsigned j = 0; j < nodes.size(); j++) { |
| 154 OwnPtrWillBeRawPtr<NodeSet> matches(NodeSet::create()); | 149 NodeSet* matches = NodeSet::create(); |
| 155 step->evaluate(context, nodes[j], *matches); | 150 step->evaluate(context, nodes[j], *matches); |
| 156 | 151 |
| 157 if (!matches->isSorted()) | 152 if (!matches->isSorted()) |
| 158 resultIsSorted = false; | 153 resultIsSorted = false; |
| 159 | 154 |
| 160 for (size_t nodeIndex = 0; nodeIndex < matches->size(); ++nodeIndex)
{ | 155 for (size_t nodeIndex = 0; nodeIndex < matches->size(); ++nodeIndex)
{ |
| 161 Node* node = (*matches)[nodeIndex]; | 156 Node* node = (*matches)[nodeIndex]; |
| 162 if (!needToCheckForDuplicateNodes || newNodesSet.add(node).isNew
Entry) | 157 if (!needToCheckForDuplicateNodes || newNodesSet.add(node).isNew
Entry) |
| 163 newNodes->append(node); | 158 newNodes->append(node); |
| 164 } | 159 } |
| 165 } | 160 } |
| 166 | 161 |
| 167 nodes.swap(*newNodes); | 162 nodes.swap(*newNodes); |
| 168 } | 163 } |
| 169 | 164 |
| 170 nodes.markSorted(resultIsSorted); | 165 nodes.markSorted(resultIsSorted); |
| 171 } | 166 } |
| 172 | 167 |
| 173 void LocationPath::appendStep(Step* step) | 168 void LocationPath::appendStep(Step* step) |
| 174 { | 169 { |
| 175 unsigned stepCount = m_steps.size(); | 170 unsigned stepCount = m_steps.size(); |
| 176 if (stepCount) { | 171 if (stepCount && optimizeStepPair(m_steps[stepCount - 1], step)) |
| 177 bool dropSecondStep; | 172 return; |
| 178 optimizeStepPair(m_steps[stepCount - 1], step, dropSecondStep); | |
| 179 if (dropSecondStep) { | |
| 180 #if !ENABLE(OILPAN) | |
| 181 delete step; | |
| 182 #endif | |
| 183 return; | |
| 184 } | |
| 185 } | |
| 186 step->optimize(); | 173 step->optimize(); |
| 187 m_steps.append(step); | 174 m_steps.append(step); |
| 188 } | 175 } |
| 189 | 176 |
| 190 void LocationPath::insertFirstStep(Step* step) | 177 void LocationPath::insertFirstStep(Step* step) |
| 191 { | 178 { |
| 192 if (m_steps.size()) { | 179 if (m_steps.size() && optimizeStepPair(step, m_steps[0])) { |
| 193 bool dropSecondStep; | 180 m_steps[0] = step; |
| 194 optimizeStepPair(step, m_steps[0], dropSecondStep); | 181 return; |
| 195 if (dropSecondStep) { | |
| 196 #if !ENABLE(OILPAN) | |
| 197 delete m_steps[0]; | |
| 198 #endif | |
| 199 m_steps[0] = step; | |
| 200 return; | |
| 201 } | |
| 202 } | 182 } |
| 203 step->optimize(); | 183 step->optimize(); |
| 204 m_steps.insert(0, step); | 184 m_steps.insert(0, step); |
| 205 } | 185 } |
| 206 | 186 |
| 207 Path::Path(Expression* filter, LocationPath* path) | 187 Path::Path(Expression* filter, LocationPath* path) |
| 208 : m_filter(adoptPtrWillBeNoop(filter)) | 188 : m_filter(filter) |
| 209 , m_path(adoptPtrWillBeNoop(path)) | 189 , m_path(path) |
| 210 { | 190 { |
| 211 setIsContextNodeSensitive(filter->isContextNodeSensitive()); | 191 setIsContextNodeSensitive(filter->isContextNodeSensitive()); |
| 212 setIsContextPositionSensitive(filter->isContextPositionSensitive()); | 192 setIsContextPositionSensitive(filter->isContextPositionSensitive()); |
| 213 setIsContextSizeSensitive(filter->isContextSizeSensitive()); | 193 setIsContextSizeSensitive(filter->isContextSizeSensitive()); |
| 214 } | 194 } |
| 215 | 195 |
| 216 Path::~Path() | 196 Path::~Path() |
| 217 { | 197 { |
| 218 } | 198 } |
| 219 | 199 |
| 220 void Path::trace(Visitor* visitor) | 200 void Path::trace(Visitor* visitor) |
| 221 { | 201 { |
| 222 visitor->trace(m_filter); | 202 visitor->trace(m_filter); |
| 223 visitor->trace(m_path); | 203 visitor->trace(m_path); |
| 224 Expression::trace(visitor); | 204 Expression::trace(visitor); |
| 225 } | 205 } |
| 226 | 206 |
| 227 Value Path::evaluate(EvaluationContext& context) const | 207 Value Path::evaluate(EvaluationContext& context) const |
| 228 { | 208 { |
| 229 Value v = m_filter->evaluate(context); | 209 Value v = m_filter->evaluate(context); |
| 230 | 210 |
| 231 NodeSet& nodes = v.modifiableNodeSet(context); | 211 NodeSet& nodes = v.modifiableNodeSet(context); |
| 232 m_path->evaluate(context, nodes); | 212 m_path->evaluate(context, nodes); |
| 233 | 213 |
| 234 return v; | 214 return v; |
| 235 } | 215 } |
| 236 | 216 |
| 237 } | 217 } // namespace XPath |
| 238 } | 218 |
| 219 } // namespace blink |
| OLD | NEW |