diff options
author | Konstantin Tokarev <annulen@yandex.ru> | 2016-08-25 19:20:41 +0300 |
---|---|---|
committer | Konstantin Tokarev <annulen@yandex.ru> | 2017-02-02 12:30:55 +0000 |
commit | 6882a04fb36642862b11efe514251d32070c3d65 (patch) | |
tree | b7959826000b061fd5ccc7512035c7478742f7b0 /Source/WebCore/xml/XPathNodeSet.cpp | |
parent | ab6df191029eeeb0b0f16f127d553265659f739e (diff) | |
download | qtwebkit-6882a04fb36642862b11efe514251d32070c3d65.tar.gz |
Imported QtWebKit TP3 (git b57bc6801f1876c3220d5a4bfea33d620d477443)
Change-Id: I3b1d8a2808782c9f34d50240000e20cb38d3680f
Reviewed-by: Konstantin Tokarev <annulen@yandex.ru>
Diffstat (limited to 'Source/WebCore/xml/XPathNodeSet.cpp')
-rw-r--r-- | Source/WebCore/xml/XPathNodeSet.cpp | 81 |
1 files changed, 33 insertions, 48 deletions
diff --git a/Source/WebCore/xml/XPathNodeSet.cpp b/Source/WebCore/xml/XPathNodeSet.cpp index 5d94b000d..0949c4d61 100644 --- a/Source/WebCore/xml/XPathNodeSet.cpp +++ b/Source/WebCore/xml/XPathNodeSet.cpp @@ -28,7 +28,6 @@ #include "Attr.h" #include "Element.h" -#include "Node.h" #include "NodeTraversal.h" namespace WebCore { @@ -44,7 +43,7 @@ static inline Node* parentWithDepth(unsigned depth, const Vector<Node*>& parents return parents[parents.size() - 1 - depth]; } -static void sortBlock(unsigned from, unsigned to, Vector<Vector<Node*> >& parentMatrix, bool mayContainAttributeNodes) +static void sortBlock(unsigned from, unsigned to, Vector<Vector<Node*>>& parentMatrix, bool mayContainAttributeNodes) { ASSERT(from + 1 < to); // Should not call this function with less that two nodes to sort. unsigned minDepth = UINT_MAX; @@ -95,8 +94,8 @@ static void sortBlock(unsigned from, unsigned to, Vector<Vector<Node*> >& parent unsigned sortedEnd = from; // FIXME: namespace nodes are not implemented. for (unsigned i = sortedEnd; i < to; ++i) { - Node* n = parentMatrix[i][0]; - if (n->isAttributeNode() && static_cast<Attr*>(n)->ownerElement() == commonAncestor) + Node* node = parentMatrix[i][0]; + if (is<Attr>(*node) && downcast<Attr>(*node).ownerElement() == commonAncestor) parentMatrix[i].swap(parentMatrix[sortedEnd++]); } if (sortedEnd != from) { @@ -142,7 +141,7 @@ void NodeSet::sort() const unsigned nodeCount = m_nodes.size(); if (nodeCount < 2) { - const_cast<bool&>(m_isSorted) = true; + m_isSorted = true; return; } @@ -153,36 +152,37 @@ void NodeSet::sort() const bool containsAttributeNodes = false; - Vector<Vector<Node*> > parentMatrix(nodeCount); + Vector<Vector<Node*>> parentMatrix(nodeCount); for (unsigned i = 0; i < nodeCount; ++i) { Vector<Node*>& parentsVector = parentMatrix[i]; - Node* n = m_nodes[i].get(); - parentsVector.append(n); - if (n->isAttributeNode()) { - n = static_cast<Attr*>(n)->ownerElement(); - parentsVector.append(n); + Node* node = m_nodes[i].get(); + parentsVector.append(node); + if (is<Attr>(*node)) { + node = downcast<Attr>(*node).ownerElement(); + parentsVector.append(node); containsAttributeNodes = true; } - while ((n = n->parentNode())) - parentsVector.append(n); + while ((node = node->parentNode())) + parentsVector.append(node); } sortBlock(0, nodeCount, parentMatrix, containsAttributeNodes); // It is not possible to just assign the result to m_nodes, because some nodes may get dereferenced and destroyed. - Vector<RefPtr<Node> > sortedNodes; + Vector<RefPtr<Node>> sortedNodes; sortedNodes.reserveInitialCapacity(nodeCount); for (unsigned i = 0; i < nodeCount; ++i) sortedNodes.append(parentMatrix[i][0]); - const_cast<Vector<RefPtr<Node> >&>(m_nodes).swap(sortedNodes); + m_nodes = WTFMove(sortedNodes); + m_isSorted = true; } static Node* findRootNode(Node* node) { - if (node->isAttributeNode()) - node = static_cast<Attr*>(node)->ownerElement(); + if (is<Attr>(*node)) + node = downcast<Attr>(*node).ownerElement(); if (node->inDocument()) - node = node->document(); + node = &node->document(); else { while (Node* parent = node->parentNode()) node = parent; @@ -197,57 +197,42 @@ void NodeSet::traversalSort() const unsigned nodeCount = m_nodes.size(); ASSERT(nodeCount > 1); - for (unsigned i = 0; i < nodeCount; ++i) { - Node* node = m_nodes[i].get(); - nodes.add(node); + for (auto& node : m_nodes) { + nodes.add(node.get()); if (node->isAttributeNode()) containsAttributeNodes = true; } - Vector<RefPtr<Node> > sortedNodes; + Vector<RefPtr<Node>> sortedNodes; sortedNodes.reserveInitialCapacity(nodeCount); - for (Node* n = findRootNode(m_nodes.first().get()); n; n = NodeTraversal::next(n)) { - if (nodes.contains(n)) - sortedNodes.append(n); + for (Node* node = findRootNode(m_nodes.first().get()); node; node = NodeTraversal::next(*node)) { + if (nodes.contains(node)) + sortedNodes.append(node); - if (!containsAttributeNodes || !n->isElementNode()) + if (!containsAttributeNodes || !is<Element>(*node)) continue; - Element* element = toElement(n); - if (!element->hasAttributes()) + Element& element = downcast<Element>(*node); + if (!element.hasAttributes()) continue; - unsigned attributeCount = element->attributeCount(); - for (unsigned i = 0; i < attributeCount; ++i) { - RefPtr<Attr> attr = element->attrIfExists(element->attributeItem(i)->name()); + for (const Attribute& attribute : element.attributesIterator()) { + RefPtr<Attr> attr = element.attrIfExists(attribute.name()); if (attr && nodes.contains(attr.get())) sortedNodes.append(attr); } } ASSERT(sortedNodes.size() == nodeCount); - const_cast<Vector<RefPtr<Node> >&>(m_nodes).swap(sortedNodes); -} - -void NodeSet::reverse() -{ - if (m_nodes.isEmpty()) - return; - - unsigned from = 0; - unsigned to = m_nodes.size() - 1; - while (from < to) { - m_nodes[from].swap(m_nodes[to]); - ++from; - --to; - } + m_nodes = WTFMove(sortedNodes); + m_isSorted = true; } Node* NodeSet::firstNode() const { if (isEmpty()) - return 0; + return nullptr; sort(); // FIXME: fully sorting the node-set just to find its first node is wasteful. return m_nodes.at(0).get(); @@ -256,7 +241,7 @@ Node* NodeSet::firstNode() const Node* NodeSet::anyNode() const { if (isEmpty()) - return 0; + return nullptr; return m_nodes.at(0).get(); } |