From 8d614681bb905979598b8cbb4734d75d836d505f Mon Sep 17 00:00:00 2001 From: Catalin Badea Date: Wed, 31 May 2017 13:05:43 +0100 Subject: [PATCH] Bug 1369063 - Remove mCurrentIndex from txMozillaXPathTreeWalker and use (Next/Previous)Sibling instead. r=peterv --- dom/xslt/xpath/txMozillaXPathTreeWalker.cpp | 98 +++++---------------- dom/xslt/xpath/txXPathTreeWalker.h | 27 ------ 2 files changed, 22 insertions(+), 103 deletions(-) diff --git a/dom/xslt/xpath/txMozillaXPathTreeWalker.cpp b/dom/xslt/xpath/txMozillaXPathTreeWalker.cpp index 0bb17451be77..adf7074dc193 100644 --- a/dom/xslt/xpath/txMozillaXPathTreeWalker.cpp +++ b/dom/xslt/xpath/txMozillaXPathTreeWalker.cpp @@ -27,17 +27,13 @@ using namespace mozilla::dom; -const uint32_t kUnknownIndex = uint32_t(-1); - txXPathTreeWalker::txXPathTreeWalker(const txXPathTreeWalker& aOther) - : mPosition(aOther.mPosition), - mCurrentIndex(aOther.mCurrentIndex) + : mPosition(aOther.mPosition) { } txXPathTreeWalker::txXPathTreeWalker(const txXPathNode& aNode) - : mPosition(aNode), - mCurrentIndex(kUnknownIndex) + : mPosition(aNode) { } @@ -62,9 +58,6 @@ txXPathTreeWalker::moveToRoot() mPosition.mIndex = txXPathNode::eContent; mPosition.mNode = rootNode; } - - mCurrentIndex = kUnknownIndex; - mDescendants.Clear(); } bool @@ -97,8 +90,6 @@ txXPathTreeWalker::moveToElementById(const nsAString& aID) mPosition.mIndex = txXPathNode::eContent; mPosition.mNode = content; - mCurrentIndex = kUnknownIndex; - mDescendants.Clear(); return true; } @@ -174,12 +165,6 @@ txXPathTreeWalker::moveToFirstChild() return false; } - NS_ASSERTION(!mPosition.isDocument() || - (mCurrentIndex == kUnknownIndex && mDescendants.IsEmpty()), - "we shouldn't have any position info at the document"); - NS_ASSERTION(mCurrentIndex != kUnknownIndex || mDescendants.IsEmpty(), - "Index should be known if parents index are"); - nsIContent* child = mPosition.mNode->GetFirstChild(); if (!child) { return false; @@ -187,12 +172,6 @@ txXPathTreeWalker::moveToFirstChild() mPosition.mIndex = txXPathNode::eContent; mPosition.mNode = child; - if (mCurrentIndex != kUnknownIndex && - !mDescendants.AppendValue(mCurrentIndex)) { - mDescendants.Clear(); - } - mCurrentIndex = 0; - return true; } @@ -203,23 +182,13 @@ txXPathTreeWalker::moveToLastChild() return false; } - NS_ASSERTION(!mPosition.isDocument() || - (mCurrentIndex == kUnknownIndex && mDescendants.IsEmpty()), - "we shouldn't have any position info at the document"); - NS_ASSERTION(mCurrentIndex != kUnknownIndex || mDescendants.IsEmpty(), - "Index should be known if parents index are"); - - uint32_t total = mPosition.mNode->GetChildCount(); - if (!total) { + nsIContent* child = mPosition.mNode->GetLastChild(); + if (!child) { return false; } - mPosition.mNode = mPosition.mNode->GetLastChild(); - if (mCurrentIndex != kUnknownIndex && - !mDescendants.AppendValue(mCurrentIndex)) { - mDescendants.Clear(); - } - mCurrentIndex = total - 1; + mPosition.mIndex = txXPathNode::eContent; + mPosition.mNode = child; return true; } @@ -231,7 +200,14 @@ txXPathTreeWalker::moveToNextSibling() return false; } - return moveToSibling(1); + nsINode* sibling = mPosition.mNode->GetNextSibling(); + if (!sibling) { + return false; + } + + mPosition.mNode = sibling; + + return true; } bool @@ -241,7 +217,14 @@ txXPathTreeWalker::moveToPreviousSibling() return false; } - return moveToSibling(-1); + nsINode* sibling = mPosition.mNode->GetPreviousSibling(); + if (!sibling) { + return false; + } + + mPosition.mNode = sibling; + + return true; } bool @@ -262,15 +245,6 @@ txXPathTreeWalker::moveToParent() return false; } - uint32_t count = mDescendants.Length(); - if (count) { - mCurrentIndex = mDescendants.ValueAt(--count); - mDescendants.RemoveValueAt(count); - } - else { - mCurrentIndex = kUnknownIndex; - } - mPosition.mIndex = mPosition.mNode->GetParent() ? txXPathNode::eContent : txXPathNode::eDocument; mPosition.mNode = parent; @@ -278,34 +252,6 @@ txXPathTreeWalker::moveToParent() return true; } -bool -txXPathTreeWalker::moveToSibling(int32_t aDir) -{ - NS_ASSERTION(mPosition.isContent(), - "moveToSibling should only be called for content"); - - nsINode* parent = mPosition.mNode->GetParentNode(); - if (!parent) { - return false; - } - if (mCurrentIndex == kUnknownIndex) { - mCurrentIndex = parent->IndexOf(mPosition.mNode); - } - - // if mCurrentIndex is 0 we rely on GetChildAt returning null for an - // index of uint32_t(-1). - uint32_t newIndex = mCurrentIndex + aDir; - nsIContent* newChild = parent->GetChildAt(newIndex); - if (!newChild) { - return false; - } - - mPosition.mNode = newChild; - mCurrentIndex = newIndex; - - return true; -} - txXPathNode::txXPathNode(const txXPathNode& aNode) : mNode(aNode.mNode), mRefCountRoot(aNode.mRefCountRoot), diff --git a/dom/xslt/xpath/txXPathTreeWalker.h b/dom/xslt/xpath/txXPathTreeWalker.h index 99a07ffe7170..fc6dff3249db 100644 --- a/dom/xslt/xpath/txXPathTreeWalker.h +++ b/dom/xslt/xpath/txXPathTreeWalker.h @@ -14,26 +14,6 @@ class nsIAtom; class nsIDOMDocument; -class txUint32Array : public nsTArray -{ -public: - bool AppendValue(uint32_t aValue) - { - return AppendElement(aValue) != nullptr; - } - bool RemoveValueAt(uint32_t aIndex) - { - if (aIndex < Length()) { - RemoveElementAt(aIndex); - } - return true; - } - uint32_t ValueAt(uint32_t aIndex) const - { - return (aIndex < Length()) ? ElementAt(aIndex) : 0; - } -}; - class txXPathTreeWalker { public: @@ -67,10 +47,6 @@ private: txXPathNode mPosition; bool moveToValidAttribute(uint32_t aStartIndex); - bool moveToSibling(int32_t aDir); - - uint32_t mCurrentIndex; - txUint32Array mDescendants; }; class txXPathNodeUtils @@ -195,9 +171,6 @@ txXPathTreeWalker::moveTo(const txXPathTreeWalker& aWalker) NS_IF_ADDREF(newRoot); NS_IF_RELEASE(root); } - - mCurrentIndex = aWalker.mCurrentIndex; - mDescendants.Clear(); } inline bool