Bug 1369063 - Remove mCurrentIndex from txMozillaXPathTreeWalker and use (Next/Previous)Sibling instead. r=peterv

This commit is contained in:
Catalin Badea 2017-05-31 13:05:43 +01:00
parent ef121ca0aa
commit 8d614681bb
2 changed files with 22 additions and 103 deletions

View File

@ -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),

View File

@ -14,26 +14,6 @@
class nsIAtom;
class nsIDOMDocument;
class txUint32Array : public nsTArray<uint32_t>
{
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