1 | /*
|
---|
2 | * Copyright (C) 2007 Alexey Proskuryakov <[email protected]>
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
7 | *
|
---|
8 | * 1. Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer in the
|
---|
12 | * documentation and/or other materials provided with the distribution.
|
---|
13 | *
|
---|
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "config.h"
|
---|
27 | #include "XPathNodeSet.h"
|
---|
28 |
|
---|
29 | #include "Attr.h"
|
---|
30 | #include "Attribute.h"
|
---|
31 | #include "Document.h"
|
---|
32 | #include "ElementInlines.h"
|
---|
33 | #include "NodeTraversal.h"
|
---|
34 |
|
---|
35 | namespace WebCore {
|
---|
36 | namespace XPath {
|
---|
37 |
|
---|
38 | // When a node set is large, sorting it by traversing the whole document is better (we can
|
---|
39 | // assume that we aren't dealing with documents that we cannot even traverse in reasonable time).
|
---|
40 | const unsigned traversalSortCutoff = 10000;
|
---|
41 |
|
---|
42 | static inline Node* parentWithDepth(unsigned depth, const Vector<Node*>& parents)
|
---|
43 | {
|
---|
44 | ASSERT(parents.size() >= depth + 1);
|
---|
45 | return parents[parents.size() - 1 - depth];
|
---|
46 | }
|
---|
47 |
|
---|
48 | static void sortBlock(unsigned from, unsigned to, Vector<Vector<Node*>>& parentMatrix, bool mayContainAttributeNodes)
|
---|
49 | {
|
---|
50 | ASSERT(from + 1 < to); // Should not call this function with less that two nodes to sort.
|
---|
51 | unsigned minDepth = UINT_MAX;
|
---|
52 | for (unsigned i = from; i < to; ++i) {
|
---|
53 | unsigned depth = parentMatrix[i].size() - 1;
|
---|
54 | if (minDepth > depth)
|
---|
55 | minDepth = depth;
|
---|
56 | }
|
---|
57 |
|
---|
58 | // Find the common ancestor.
|
---|
59 | unsigned commonAncestorDepth = minDepth;
|
---|
60 | Node* commonAncestor;
|
---|
61 | while (true) {
|
---|
62 | commonAncestor = parentWithDepth(commonAncestorDepth, parentMatrix[from]);
|
---|
63 | if (commonAncestorDepth == 0)
|
---|
64 | break;
|
---|
65 |
|
---|
66 | bool allEqual = true;
|
---|
67 | for (unsigned i = from + 1; i < to; ++i) {
|
---|
68 | if (commonAncestor != parentWithDepth(commonAncestorDepth, parentMatrix[i])) {
|
---|
69 | allEqual = false;
|
---|
70 | break;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | if (allEqual)
|
---|
74 | break;
|
---|
75 |
|
---|
76 | --commonAncestorDepth;
|
---|
77 | }
|
---|
78 |
|
---|
79 | if (commonAncestorDepth == minDepth) {
|
---|
80 | // One of the nodes is the common ancestor => it is the first in document order.
|
---|
81 | // Find it and move it to the beginning.
|
---|
82 | for (unsigned i = from; i < to; ++i)
|
---|
83 | if (commonAncestor == parentMatrix[i][0]) {
|
---|
84 | parentMatrix[i].swap(parentMatrix[from]);
|
---|
85 | if (from + 2 < to)
|
---|
86 | sortBlock(from + 1, to, parentMatrix, mayContainAttributeNodes);
|
---|
87 | return;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (mayContainAttributeNodes && commonAncestor->isElementNode()) {
|
---|
92 | // The attribute nodes and namespace nodes of an element occur before the children of the element.
|
---|
93 | // The namespace nodes are defined to occur before the attribute nodes.
|
---|
94 | // The relative order of namespace nodes is implementation-dependent.
|
---|
95 | // The relative order of attribute nodes is implementation-dependent.
|
---|
96 | unsigned sortedEnd = from;
|
---|
97 | // FIXME: namespace nodes are not implemented.
|
---|
98 | for (unsigned i = sortedEnd; i < to; ++i) {
|
---|
99 | Node* node = parentMatrix[i][0];
|
---|
100 | if (is<Attr>(*node) && downcast<Attr>(*node).ownerElement() == commonAncestor)
|
---|
101 | parentMatrix[i].swap(parentMatrix[sortedEnd++]);
|
---|
102 | }
|
---|
103 | if (sortedEnd != from) {
|
---|
104 | if (to - sortedEnd > 1)
|
---|
105 | sortBlock(sortedEnd, to, parentMatrix, mayContainAttributeNodes);
|
---|
106 | return;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | // Children nodes of the common ancestor induce a subdivision of our node-set.
|
---|
111 | // Sort it according to this subdivision, and recursively sort each group.
|
---|
112 | HashSet<RefPtr<Node>> parentNodes;
|
---|
113 | for (unsigned i = from; i < to; ++i)
|
---|
114 | parentNodes.add(parentWithDepth(commonAncestorDepth + 1, parentMatrix[i]));
|
---|
115 |
|
---|
116 | unsigned previousGroupEnd = from;
|
---|
117 | unsigned groupEnd = from;
|
---|
118 | for (Node* n = commonAncestor->firstChild(); n; n = n->nextSibling()) {
|
---|
119 | // If parentNodes contains the node, perform a linear search to move its children in the node-set to the beginning.
|
---|
120 | if (parentNodes.contains(n)) {
|
---|
121 | for (unsigned i = groupEnd; i < to; ++i)
|
---|
122 | if (parentWithDepth(commonAncestorDepth + 1, parentMatrix[i]) == n)
|
---|
123 | parentMatrix[i].swap(parentMatrix[groupEnd++]);
|
---|
124 |
|
---|
125 | if (groupEnd - previousGroupEnd > 1)
|
---|
126 | sortBlock(previousGroupEnd, groupEnd, parentMatrix, mayContainAttributeNodes);
|
---|
127 |
|
---|
128 | ASSERT(previousGroupEnd != groupEnd);
|
---|
129 | previousGroupEnd = groupEnd;
|
---|
130 | #ifndef NDEBUG
|
---|
131 | parentNodes.remove(n);
|
---|
132 | #endif
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | ASSERT(parentNodes.isEmpty());
|
---|
137 | }
|
---|
138 |
|
---|
139 | void NodeSet::sort() const
|
---|
140 | {
|
---|
141 | if (m_isSorted)
|
---|
142 | return;
|
---|
143 |
|
---|
144 | unsigned nodeCount = m_nodes.size();
|
---|
145 | if (nodeCount < 2) {
|
---|
146 | m_isSorted = true;
|
---|
147 | return;
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (nodeCount > traversalSortCutoff) {
|
---|
151 | traversalSort();
|
---|
152 | return;
|
---|
153 | }
|
---|
154 |
|
---|
155 | bool containsAttributeNodes = false;
|
---|
156 |
|
---|
157 | Vector<Vector<Node*>> parentMatrix(nodeCount);
|
---|
158 | for (unsigned i = 0; i < nodeCount; ++i) {
|
---|
159 | Vector<Node*>& parentsVector = parentMatrix[i];
|
---|
160 | Node* node = m_nodes[i].get();
|
---|
161 | parentsVector.append(node);
|
---|
162 | if (is<Attr>(*node)) {
|
---|
163 | node = downcast<Attr>(*node).ownerElement();
|
---|
164 | parentsVector.append(node);
|
---|
165 | containsAttributeNodes = true;
|
---|
166 | }
|
---|
167 | while ((node = node->parentNode()))
|
---|
168 | parentsVector.append(node);
|
---|
169 | }
|
---|
170 | sortBlock(0, nodeCount, parentMatrix, containsAttributeNodes);
|
---|
171 |
|
---|
172 | // It is not possible to just assign the result to m_nodes, because some nodes may get dereferenced and destroyed.
|
---|
173 | Vector<RefPtr<Node>> sortedNodes;
|
---|
174 | sortedNodes.reserveInitialCapacity(nodeCount);
|
---|
175 | for (unsigned i = 0; i < nodeCount; ++i)
|
---|
176 | sortedNodes.append(parentMatrix[i][0]);
|
---|
177 |
|
---|
178 | m_nodes = WTFMove(sortedNodes);
|
---|
179 | m_isSorted = true;
|
---|
180 | }
|
---|
181 |
|
---|
182 | static Node* findRootNode(Node* node)
|
---|
183 | {
|
---|
184 | if (is<Attr>(*node))
|
---|
185 | node = downcast<Attr>(*node).ownerElement();
|
---|
186 | if (node->isConnected())
|
---|
187 | node = &node->document();
|
---|
188 | else {
|
---|
189 | while (Node* parent = node->parentNode())
|
---|
190 | node = parent;
|
---|
191 | }
|
---|
192 | return node;
|
---|
193 | }
|
---|
194 |
|
---|
195 | void NodeSet::traversalSort() const
|
---|
196 | {
|
---|
197 | HashSet<RefPtr<Node>> nodes;
|
---|
198 | bool containsAttributeNodes = false;
|
---|
199 |
|
---|
200 | unsigned nodeCount = m_nodes.size();
|
---|
201 | ASSERT(nodeCount > 1);
|
---|
202 | for (auto& node : m_nodes) {
|
---|
203 | nodes.add(node.get());
|
---|
204 | if (node->isAttributeNode())
|
---|
205 | containsAttributeNodes = true;
|
---|
206 | }
|
---|
207 |
|
---|
208 | Vector<RefPtr<Node>> sortedNodes;
|
---|
209 | sortedNodes.reserveInitialCapacity(nodeCount);
|
---|
210 |
|
---|
211 | for (Node* node = findRootNode(m_nodes.first().get()); node; node = NodeTraversal::next(*node)) {
|
---|
212 | if (nodes.contains(node))
|
---|
213 | sortedNodes.append(node);
|
---|
214 |
|
---|
215 | if (!containsAttributeNodes || !is<Element>(*node))
|
---|
216 | continue;
|
---|
217 |
|
---|
218 | Element& element = downcast<Element>(*node);
|
---|
219 | if (!element.hasAttributes())
|
---|
220 | continue;
|
---|
221 |
|
---|
222 | for (const Attribute& attribute : element.attributesIterator()) {
|
---|
223 | RefPtr<Attr> attr = element.attrIfExists(attribute.name());
|
---|
224 | if (attr && nodes.contains(attr.get()))
|
---|
225 | sortedNodes.append(attr);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | ASSERT(sortedNodes.size() == nodeCount);
|
---|
230 | m_nodes = WTFMove(sortedNodes);
|
---|
231 | m_isSorted = true;
|
---|
232 | }
|
---|
233 |
|
---|
234 | Node* NodeSet::firstNode() const
|
---|
235 | {
|
---|
236 | if (isEmpty())
|
---|
237 | return nullptr;
|
---|
238 |
|
---|
239 | sort(); // FIXME: fully sorting the node-set just to find its first node is wasteful.
|
---|
240 | return m_nodes.at(0).get();
|
---|
241 | }
|
---|
242 |
|
---|
243 | Node* NodeSet::anyNode() const
|
---|
244 | {
|
---|
245 | if (isEmpty())
|
---|
246 | return nullptr;
|
---|
247 |
|
---|
248 | return m_nodes.at(0).get();
|
---|
249 | }
|
---|
250 |
|
---|
251 | }
|
---|
252 | }
|
---|