зеркало из https://github.com/mozilla/gecko-dev.git
Bug 577438 Part 3: Make TreeWalker use new tree walking functions. r=sicking
This commit is contained in:
Родитель
e83208a31c
Коммит
07757a78aa
|
@ -1,6 +1,6 @@
|
||||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||||
*
|
/* vim: set ts=4 et sw=4 tw=80: */
|
||||||
* ***** BEGIN LICENSE BLOCK *****
|
/* ***** BEGIN LICENSE BLOCK *****
|
||||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||||
*
|
*
|
||||||
* The contents of this file are subject to the Mozilla Public License Version
|
* The contents of this file are subject to the Mozilla Public License Version
|
||||||
|
@ -22,6 +22,7 @@
|
||||||
*
|
*
|
||||||
* Contributor(s):
|
* Contributor(s):
|
||||||
* Jonas Sicking <sicking@bigfoot.com> (Original Author)
|
* Jonas Sicking <sicking@bigfoot.com> (Original Author)
|
||||||
|
* Craig Topper <craig.topper@gmail.com>
|
||||||
*
|
*
|
||||||
* Alternatively, the contents of this file may be used under the terms of
|
* Alternatively, the contents of this file may be used under the terms of
|
||||||
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
||||||
|
@ -47,9 +48,6 @@
|
||||||
#include "nsIDOMNodeFilter.h"
|
#include "nsIDOMNodeFilter.h"
|
||||||
#include "nsDOMError.h"
|
#include "nsDOMError.h"
|
||||||
|
|
||||||
#include "nsIContent.h"
|
|
||||||
#include "nsIDocument.h"
|
|
||||||
|
|
||||||
#include "nsContentUtils.h"
|
#include "nsContentUtils.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -61,8 +59,7 @@ nsTreeWalker::nsTreeWalker(nsINode *aRoot,
|
||||||
nsIDOMNodeFilter *aFilter,
|
nsIDOMNodeFilter *aFilter,
|
||||||
PRBool aExpandEntityReferences) :
|
PRBool aExpandEntityReferences) :
|
||||||
nsTraversal(aRoot, aWhatToShow, aFilter, aExpandEntityReferences),
|
nsTraversal(aRoot, aWhatToShow, aFilter, aExpandEntityReferences),
|
||||||
mCurrentNode(aRoot),
|
mCurrentNode(aRoot)
|
||||||
mPossibleIndexesPos(-1)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,8 +116,7 @@ NS_IMETHODIMP nsTreeWalker::GetFilter(nsIDOMNodeFilter * *aFilter)
|
||||||
{
|
{
|
||||||
NS_ENSURE_ARG_POINTER(aFilter);
|
NS_ENSURE_ARG_POINTER(aFilter);
|
||||||
|
|
||||||
nsCOMPtr<nsIDOMNodeFilter> filter = mFilter;
|
NS_IF_ADDREF(*aFilter = mFilter);
|
||||||
filter.swap((*aFilter = nsnull));
|
|
||||||
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
@ -152,8 +148,6 @@ NS_IMETHODIMP nsTreeWalker::SetCurrentNode(nsIDOMNode * aCurrentNode)
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
mCurrentNode = do_QueryInterface(aCurrentNode);
|
mCurrentNode = do_QueryInterface(aCurrentNode);
|
||||||
mPossibleIndexes.Clear();
|
|
||||||
mPossibleIndexesPos = -1;
|
|
||||||
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
@ -166,16 +160,13 @@ NS_IMETHODIMP nsTreeWalker::SetCurrentNode(nsIDOMNode * aCurrentNode)
|
||||||
NS_IMETHODIMP nsTreeWalker::ParentNode(nsIDOMNode **_retval)
|
NS_IMETHODIMP nsTreeWalker::ParentNode(nsIDOMNode **_retval)
|
||||||
{
|
{
|
||||||
*_retval = nsnull;
|
*_retval = nsnull;
|
||||||
|
|
||||||
nsresult rv;
|
nsresult rv;
|
||||||
|
|
||||||
PRInt32 indexPos = mPossibleIndexesPos;
|
|
||||||
nsCOMPtr<nsINode> node = mCurrentNode;
|
nsCOMPtr<nsINode> node = mCurrentNode;
|
||||||
|
|
||||||
while (node && node != mRoot) {
|
while (node && node != mRoot) {
|
||||||
node = node->GetNodeParent();
|
node = node->GetNodeParent();
|
||||||
|
|
||||||
indexPos--;
|
|
||||||
|
|
||||||
if (node) {
|
if (node) {
|
||||||
PRInt16 filtered;
|
PRInt16 filtered;
|
||||||
|
@ -183,8 +174,6 @@ NS_IMETHODIMP nsTreeWalker::ParentNode(nsIDOMNode **_retval)
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
|
if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
|
||||||
mCurrentNode = node;
|
mCurrentNode = node;
|
||||||
mPossibleIndexesPos = indexPos >= 0 ? indexPos : -1;
|
|
||||||
|
|
||||||
return CallQueryInterface(node, _retval);
|
return CallQueryInterface(node, _retval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -196,374 +185,260 @@ NS_IMETHODIMP nsTreeWalker::ParentNode(nsIDOMNode **_retval)
|
||||||
/* nsIDOMNode firstChild (); */
|
/* nsIDOMNode firstChild (); */
|
||||||
NS_IMETHODIMP nsTreeWalker::FirstChild(nsIDOMNode **_retval)
|
NS_IMETHODIMP nsTreeWalker::FirstChild(nsIDOMNode **_retval)
|
||||||
{
|
{
|
||||||
*_retval = nsnull;
|
return FirstChildInternal(PR_FALSE, _retval);
|
||||||
|
|
||||||
nsCOMPtr<nsINode> result;
|
|
||||||
nsresult rv = FirstChildOf(mCurrentNode,
|
|
||||||
PR_FALSE,
|
|
||||||
mPossibleIndexesPos + 1,
|
|
||||||
getter_AddRefs(result));
|
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
|
||||||
|
|
||||||
return result ? CallQueryInterface(result, _retval) : NS_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* nsIDOMNode lastChild (); */
|
/* nsIDOMNode lastChild (); */
|
||||||
NS_IMETHODIMP nsTreeWalker::LastChild(nsIDOMNode **_retval)
|
NS_IMETHODIMP nsTreeWalker::LastChild(nsIDOMNode **_retval)
|
||||||
{
|
{
|
||||||
*_retval = nsnull;
|
return FirstChildInternal(PR_TRUE, _retval);
|
||||||
|
|
||||||
nsCOMPtr<nsINode> result;
|
|
||||||
nsresult rv = FirstChildOf(mCurrentNode,
|
|
||||||
PR_TRUE,
|
|
||||||
mPossibleIndexesPos + 1,
|
|
||||||
getter_AddRefs(result));
|
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
|
||||||
|
|
||||||
return result ? CallQueryInterface(result, _retval) : NS_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* nsIDOMNode previousSibling (); */
|
/* nsIDOMNode previousSibling (); */
|
||||||
NS_IMETHODIMP nsTreeWalker::PreviousSibling(nsIDOMNode **_retval)
|
NS_IMETHODIMP nsTreeWalker::PreviousSibling(nsIDOMNode **_retval)
|
||||||
{
|
{
|
||||||
*_retval = nsnull;
|
return NextSiblingInternal(PR_TRUE, _retval);
|
||||||
|
|
||||||
nsCOMPtr<nsINode> result;
|
|
||||||
nsresult rv = NextSiblingOf(mCurrentNode,
|
|
||||||
PR_TRUE,
|
|
||||||
mPossibleIndexesPos,
|
|
||||||
getter_AddRefs(result));
|
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
|
||||||
|
|
||||||
return result ? CallQueryInterface(result, _retval) : NS_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* nsIDOMNode nextSibling (); */
|
/* nsIDOMNode nextSibling (); */
|
||||||
NS_IMETHODIMP nsTreeWalker::NextSibling(nsIDOMNode **_retval)
|
NS_IMETHODIMP nsTreeWalker::NextSibling(nsIDOMNode **_retval)
|
||||||
{
|
{
|
||||||
*_retval = nsnull;
|
return NextSiblingInternal(PR_FALSE, _retval);
|
||||||
|
|
||||||
nsCOMPtr<nsINode> result;
|
|
||||||
nsresult rv = NextSiblingOf(mCurrentNode,
|
|
||||||
PR_FALSE,
|
|
||||||
mPossibleIndexesPos,
|
|
||||||
getter_AddRefs(result));
|
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
|
||||||
|
|
||||||
return result ? CallQueryInterface(result, _retval) : NS_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* nsIDOMNode previousNode (); */
|
/* nsIDOMNode previousNode (); */
|
||||||
NS_IMETHODIMP nsTreeWalker::PreviousNode(nsIDOMNode **_retval)
|
NS_IMETHODIMP nsTreeWalker::PreviousNode(nsIDOMNode **_retval)
|
||||||
{
|
{
|
||||||
|
nsresult rv;
|
||||||
|
PRInt16 filtered;
|
||||||
|
|
||||||
*_retval = nsnull;
|
*_retval = nsnull;
|
||||||
|
|
||||||
nsCOMPtr<nsINode> result;
|
nsCOMPtr<nsINode> node = mCurrentNode;
|
||||||
nsresult rv = NextInDocumentOrderOf(mCurrentNode,
|
|
||||||
PR_TRUE,
|
|
||||||
mPossibleIndexesPos,
|
|
||||||
getter_AddRefs(result));
|
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
|
||||||
|
|
||||||
return result ? CallQueryInterface(result, _retval) : NS_OK;
|
while (node != mRoot) {
|
||||||
|
while (nsINode *previousSibling = node->GetPreviousSibling()) {
|
||||||
|
node = previousSibling;
|
||||||
|
|
||||||
|
rv = TestNode(node, &filtered);
|
||||||
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
|
nsINode *lastChild;
|
||||||
|
while (filtered != nsIDOMNodeFilter::FILTER_REJECT &&
|
||||||
|
(lastChild = node->GetLastChild())) {
|
||||||
|
node = lastChild;
|
||||||
|
rv = TestNode(node, &filtered);
|
||||||
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
|
||||||
|
mCurrentNode = node;
|
||||||
|
return CallQueryInterface(node, _retval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node == mRoot)
|
||||||
|
break;
|
||||||
|
|
||||||
|
node = node->GetNodeParent();
|
||||||
|
if (!node)
|
||||||
|
break;
|
||||||
|
|
||||||
|
rv = TestNode(node, &filtered);
|
||||||
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
|
if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
|
||||||
|
mCurrentNode = node;
|
||||||
|
return CallQueryInterface(node, _retval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* nsIDOMNode nextNode (); */
|
/* nsIDOMNode nextNode (); */
|
||||||
NS_IMETHODIMP nsTreeWalker::NextNode(nsIDOMNode **_retval)
|
NS_IMETHODIMP nsTreeWalker::NextNode(nsIDOMNode **_retval)
|
||||||
{
|
{
|
||||||
|
nsresult rv;
|
||||||
|
PRInt16 filtered = nsIDOMNodeFilter::FILTER_ACCEPT; // pre-init for inner loop
|
||||||
|
|
||||||
*_retval = nsnull;
|
*_retval = nsnull;
|
||||||
|
|
||||||
nsCOMPtr<nsINode> result;
|
nsCOMPtr<nsINode> node = mCurrentNode;
|
||||||
nsresult rv = NextInDocumentOrderOf(mCurrentNode,
|
|
||||||
PR_FALSE,
|
|
||||||
mPossibleIndexesPos,
|
|
||||||
getter_AddRefs(result));
|
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
|
||||||
|
|
||||||
return result ? CallQueryInterface(result, _retval) : NS_OK;
|
while (1) {
|
||||||
|
|
||||||
|
nsINode *firstChild;
|
||||||
|
while (filtered != nsIDOMNodeFilter::FILTER_REJECT &&
|
||||||
|
(firstChild = node->GetFirstChild())) {
|
||||||
|
node = firstChild;
|
||||||
|
|
||||||
|
rv = TestNode(node, &filtered);
|
||||||
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
|
if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
|
||||||
|
// Node found
|
||||||
|
mCurrentNode = node;
|
||||||
|
return CallQueryInterface(node, _retval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nsINode *sibling = nsnull;
|
||||||
|
nsINode *temp = node;
|
||||||
|
do {
|
||||||
|
if (temp == mRoot)
|
||||||
|
break;
|
||||||
|
|
||||||
|
sibling = temp->GetNextSibling();
|
||||||
|
if (sibling)
|
||||||
|
break;
|
||||||
|
|
||||||
|
temp = temp->GetNodeParent();
|
||||||
|
} while (temp);
|
||||||
|
|
||||||
|
if (!sibling)
|
||||||
|
break;
|
||||||
|
|
||||||
|
node = sibling;
|
||||||
|
|
||||||
|
// Found a sibling. Either ours or ancestor's
|
||||||
|
rv = TestNode(node, &filtered);
|
||||||
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
|
if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
|
||||||
|
// Node found
|
||||||
|
mCurrentNode = node;
|
||||||
|
return CallQueryInterface(node, _retval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* nsTreeWalker helper functions
|
* nsTreeWalker helper functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Finds the first child of aNode and returns it. If a child is
|
* Implements FirstChild and LastChild which only vary in which direction
|
||||||
* found, mCurrentNode is set to that child.
|
* they search.
|
||||||
* @param aNode Node to search for children.
|
* @param aReversed Controls whether we search forwards or backwards
|
||||||
* @param aReversed Reverses search to find the last child instead
|
|
||||||
* of first.
|
|
||||||
* @param aIndexPos Position of aNode in mPossibleIndexes.
|
|
||||||
* @param _retval Returned node. Null if no child is found
|
* @param _retval Returned node. Null if no child is found
|
||||||
* @returns Errorcode
|
* @returns Errorcode
|
||||||
*/
|
*/
|
||||||
nsresult
|
nsresult nsTreeWalker::FirstChildInternal(PRBool aReversed, nsIDOMNode **_retval)
|
||||||
nsTreeWalker::FirstChildOf(nsINode* aNode,
|
|
||||||
PRBool aReversed,
|
|
||||||
PRInt32 aIndexPos,
|
|
||||||
nsINode** _retval)
|
|
||||||
{
|
|
||||||
*_retval = nsnull;
|
|
||||||
PRInt32 start = aReversed ? (PRInt32)aNode->GetChildCount() : -1;
|
|
||||||
|
|
||||||
return ChildOf(aNode, start, aReversed, aIndexPos, _retval);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Finds the following sibling of aNode and returns it. If a sibling
|
|
||||||
* is found, mCurrentNode is set to that node.
|
|
||||||
* @param aNode Node to start search at.
|
|
||||||
* @param aReversed Reverses search to find the previous sibling
|
|
||||||
* instead of next.
|
|
||||||
* @param aIndexPos Position of aNode in mPossibleIndexes.
|
|
||||||
* @param _retval Returned node. Null if no sibling is found
|
|
||||||
* @returns Errorcode
|
|
||||||
*/
|
|
||||||
nsresult
|
|
||||||
nsTreeWalker::NextSiblingOf(nsINode* aNode,
|
|
||||||
PRBool aReversed,
|
|
||||||
PRInt32 aIndexPos,
|
|
||||||
nsINode** _retval)
|
|
||||||
{
|
{
|
||||||
nsresult rv;
|
nsresult rv;
|
||||||
nsCOMPtr<nsINode> node = aNode;
|
|
||||||
PRInt16 filtered;
|
PRInt16 filtered;
|
||||||
PRInt32 childNum;
|
|
||||||
|
|
||||||
if (node == mRoot) {
|
|
||||||
*_retval = nsnull;
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
nsCOMPtr<nsINode> parent = node->GetNodeParent();
|
|
||||||
|
|
||||||
if (!parent)
|
|
||||||
break;
|
|
||||||
|
|
||||||
childNum = IndexOf(parent, node, aIndexPos);
|
|
||||||
NS_ENSURE_TRUE(childNum >= 0, NS_ERROR_UNEXPECTED);
|
|
||||||
|
|
||||||
// Search siblings
|
|
||||||
rv = ChildOf(parent, childNum, aReversed, aIndexPos, _retval);
|
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
|
||||||
|
|
||||||
if (*_retval)
|
|
||||||
return NS_OK;
|
|
||||||
|
|
||||||
// Is parent the root?
|
|
||||||
if (parent == mRoot)
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Is parent transparent in filtered view?
|
|
||||||
rv = TestNode(parent, &filtered);
|
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
|
||||||
if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT)
|
|
||||||
break;
|
|
||||||
|
|
||||||
node = parent;
|
|
||||||
aIndexPos = aIndexPos < 0 ? -1 : aIndexPos-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
*_retval = nsnull;
|
*_retval = nsnull;
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
nsCOMPtr<nsINode> node = aReversed ? mCurrentNode->GetLastChild()
|
||||||
* Finds the next node in document order of aNode and returns it.
|
: mCurrentNode->GetFirstChild();
|
||||||
* If a node is found, mCurrentNode is set to that node.
|
|
||||||
* @param aNode Node to start search at.
|
|
||||||
* @param aReversed Reverses search to find the preceding node
|
|
||||||
* instead of next.
|
|
||||||
* @param aIndexPos Position of aNode in mPossibleIndexes.
|
|
||||||
* @param _retval Returned node. Null if no node is found
|
|
||||||
* @returns Errorcode
|
|
||||||
*/
|
|
||||||
nsresult
|
|
||||||
nsTreeWalker::NextInDocumentOrderOf(nsINode* aNode,
|
|
||||||
PRBool aReversed,
|
|
||||||
PRInt32 aIndexPos,
|
|
||||||
nsINode** _retval)
|
|
||||||
{
|
|
||||||
nsresult rv;
|
|
||||||
|
|
||||||
if (!aReversed) {
|
while (node) {
|
||||||
rv = FirstChildOf(aNode, aReversed, aIndexPos+1, _retval);
|
rv = TestNode(node, &filtered);
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
|
||||||
|
|
||||||
if (*_retval)
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (aNode == mRoot){
|
|
||||||
*_retval = nsnull;
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
nsCOMPtr<nsINode> node = aNode;
|
|
||||||
nsCOMPtr<nsINode> currentNodeBackup = mCurrentNode;
|
|
||||||
PRInt16 filtered;
|
|
||||||
PRInt32 childNum;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
// Get our index in the parent
|
|
||||||
nsCOMPtr<nsINode> parent = node->GetNodeParent();
|
|
||||||
if (!parent)
|
|
||||||
break;
|
|
||||||
|
|
||||||
childNum = IndexOf(parent, node, aIndexPos);
|
|
||||||
NS_ENSURE_TRUE(childNum >= 0, NS_ERROR_UNEXPECTED);
|
|
||||||
|
|
||||||
// Search siblings
|
|
||||||
nsCOMPtr<nsINode> sibling;
|
|
||||||
rv = ChildOf(parent, childNum, aReversed, aIndexPos,
|
|
||||||
getter_AddRefs(sibling));
|
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
|
||||||
|
|
||||||
if (sibling) {
|
|
||||||
if (aReversed) {
|
|
||||||
// in reversed walking we first test if there are
|
|
||||||
// any children. I don't like this piece of code :(
|
|
||||||
nsCOMPtr<nsINode> child = sibling;
|
|
||||||
while (child) {
|
|
||||||
sibling = child;
|
|
||||||
rv = FirstChildOf(sibling,
|
|
||||||
PR_TRUE,
|
|
||||||
aIndexPos,
|
|
||||||
getter_AddRefs(child));
|
|
||||||
if (NS_FAILED(rv)) {
|
|
||||||
// ChildOf set mCurrentNode and then something
|
|
||||||
// failed. Restore the old value before returning
|
|
||||||
mCurrentNode = currentNodeBackup;
|
|
||||||
mPossibleIndexesPos = -1;
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*_retval = sibling;
|
|
||||||
NS_ADDREF(*_retval);
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
aIndexPos = aIndexPos < 0 ? -1 : aIndexPos-1;
|
|
||||||
|
|
||||||
if (aReversed) {
|
|
||||||
// Is parent transparent in filtered view?
|
|
||||||
rv = TestNode(parent, &filtered);
|
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
|
||||||
if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
|
|
||||||
mCurrentNode = parent;
|
|
||||||
mPossibleIndexesPos = aIndexPos;
|
|
||||||
*_retval = parent;
|
|
||||||
NS_ADDREF(*_retval);
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Is parent the root?
|
|
||||||
if (parent == mRoot)
|
|
||||||
break;
|
|
||||||
|
|
||||||
node = parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
*_retval = nsnull;
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Finds the first child of aNode after child N and returns it. If a
|
|
||||||
* child is found, mCurrentNode is set to that child
|
|
||||||
* @param aNode Node to search for children
|
|
||||||
* @param childNum Child number to start search from. The child with
|
|
||||||
* this number is not searched
|
|
||||||
* @param aReversed Reverses search to find the last child instead
|
|
||||||
* of first
|
|
||||||
* @param aIndexPos Position of aNode in mPossibleIndexes
|
|
||||||
* @param _retval Returned node. Null if no child is found
|
|
||||||
* @returns Errorcode
|
|
||||||
*/
|
|
||||||
nsresult
|
|
||||||
nsTreeWalker::ChildOf(nsINode* aNode,
|
|
||||||
PRInt32 childNum,
|
|
||||||
PRBool aReversed,
|
|
||||||
PRInt32 aIndexPos,
|
|
||||||
nsINode** _retval)
|
|
||||||
{
|
|
||||||
PRInt16 filtered;
|
|
||||||
nsresult rv;
|
|
||||||
|
|
||||||
PRInt32 dir = aReversed ? -1 : 1;
|
|
||||||
|
|
||||||
// Step through all children
|
|
||||||
PRInt32 i = childNum;
|
|
||||||
while (1) {
|
|
||||||
i += dir;
|
|
||||||
nsCOMPtr<nsINode> child = aNode->GetChildAt(i);
|
|
||||||
if (!child) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
rv = TestNode(child, &filtered);
|
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
switch (filtered) {
|
switch (filtered) {
|
||||||
case nsIDOMNodeFilter::FILTER_ACCEPT:
|
case nsIDOMNodeFilter::FILTER_ACCEPT:
|
||||||
// Child found
|
// Node found
|
||||||
mCurrentNode = child;
|
mCurrentNode = node;
|
||||||
mPossibleIndexesPos = aIndexPos;
|
return CallQueryInterface(node, _retval);
|
||||||
*_retval = child;
|
case nsIDOMNodeFilter::FILTER_SKIP: {
|
||||||
NS_ADDREF(*_retval);
|
nsINode *child = aReversed ? node->GetLastChild()
|
||||||
|
: node->GetFirstChild();
|
||||||
SetChildIndex(aIndexPos, i);
|
if (child) {
|
||||||
|
node = child;
|
||||||
return NS_OK;
|
continue;
|
||||||
|
}
|
||||||
case nsIDOMNodeFilter::FILTER_SKIP:
|
break;
|
||||||
// Search children
|
|
||||||
rv = FirstChildOf(child, aReversed, aIndexPos+1, _retval);
|
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
|
||||||
|
|
||||||
if (*_retval) {
|
|
||||||
SetChildIndex(aIndexPos, i);
|
|
||||||
return NS_OK;
|
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
case nsIDOMNodeFilter::FILTER_REJECT:
|
case nsIDOMNodeFilter::FILTER_REJECT:
|
||||||
// Keep searching
|
// Keep searching
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
|
||||||
return NS_ERROR_UNEXPECTED;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
nsINode *sibling = aReversed ? node->GetPreviousSibling()
|
||||||
|
: node->GetNextSibling();
|
||||||
|
if (sibling) {
|
||||||
|
node = sibling;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
nsINode *parent = node->GetNodeParent();
|
||||||
|
|
||||||
|
if (!parent || parent == mRoot || parent == mCurrentNode) {
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (node);
|
||||||
}
|
}
|
||||||
|
|
||||||
*_retval = nsnull;
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Gets the child index of a node within its parent. Gets a possible index
|
* Implements NextSibling and PreviousSibling which only vary in which
|
||||||
* from mPossibleIndexes to gain speed. If the value in mPossibleIndexes
|
* direction they search.
|
||||||
* isn't correct it'll get the index the usual way
|
* @param aReversed Controls whether we search forwards or backwards
|
||||||
* @param aParent in which to get the index
|
* @param _retval Returned node. Null if no child is found
|
||||||
* @param aChild node to get the index of
|
* @returns Errorcode
|
||||||
* @param aIndexPos position in mPossibleIndexes that contains the possible.
|
|
||||||
* index
|
|
||||||
* @returns resulting index
|
|
||||||
*/
|
*/
|
||||||
PRInt32 nsTreeWalker::IndexOf(nsINode* aParent,
|
nsresult nsTreeWalker::NextSiblingInternal(PRBool aReversed, nsIDOMNode **_retval)
|
||||||
nsINode* aChild,
|
|
||||||
PRInt32 aIndexPos)
|
|
||||||
{
|
{
|
||||||
if (aIndexPos >= 0 && aIndexPos < PRInt32(mPossibleIndexes.Length())) {
|
nsresult rv;
|
||||||
PRInt32 possibleIndex = mPossibleIndexes.ElementAt(aIndexPos);
|
PRInt16 filtered;
|
||||||
if (aChild == aParent->GetChildAt(possibleIndex)) {
|
|
||||||
return possibleIndex;
|
*_retval = nsnull;
|
||||||
|
|
||||||
|
nsCOMPtr<nsINode> node = mCurrentNode;
|
||||||
|
|
||||||
|
if (node == mRoot)
|
||||||
|
return NS_OK;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
nsCOMPtr<nsINode> sibling = aReversed ? node->GetPreviousSibling()
|
||||||
|
: node->GetNextSibling();
|
||||||
|
|
||||||
|
while (sibling) {
|
||||||
|
rv = TestNode(sibling, &filtered);
|
||||||
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
|
switch (filtered) {
|
||||||
|
case nsIDOMNodeFilter::FILTER_ACCEPT:
|
||||||
|
// Node found
|
||||||
|
mCurrentNode = sibling;
|
||||||
|
return CallQueryInterface(sibling, _retval);
|
||||||
|
case nsIDOMNodeFilter::FILTER_SKIP: {
|
||||||
|
nsINode *firstChild = aReversed ? sibling->GetLastChild()
|
||||||
|
: sibling->GetFirstChild();
|
||||||
|
if (firstChild) {
|
||||||
|
sibling = firstChild;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case nsIDOMNodeFilter::FILTER_REJECT:
|
||||||
|
// Keep searching
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sibling = aReversed ? sibling->GetPreviousSibling()
|
||||||
|
: sibling->GetNextSibling();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
node = node->GetNodeParent();
|
||||||
|
|
||||||
|
if (!node || node == mRoot)
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Is parent transparent in filtered view?
|
||||||
|
rv = TestNode(node, &filtered);
|
||||||
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return aParent->IndexOf(aChild);
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||||
*
|
/* vim: set ts=4 et sw=4 tw=80: */
|
||||||
* ***** BEGIN LICENSE BLOCK *****
|
/* ***** BEGIN LICENSE BLOCK *****
|
||||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||||
*
|
*
|
||||||
* The contents of this file are subject to the Mozilla Public License Version
|
* The contents of this file are subject to the Mozilla Public License Version
|
||||||
|
@ -22,6 +22,7 @@
|
||||||
*
|
*
|
||||||
* Contributor(s):
|
* Contributor(s):
|
||||||
* Jonas Sicking <sicking@bigfoot.com> (Original Author)
|
* Jonas Sicking <sicking@bigfoot.com> (Original Author)
|
||||||
|
* Craig Topper <craig.topper@gmail.com>
|
||||||
*
|
*
|
||||||
* Alternatively, the contents of this file may be used under the terms of
|
* Alternatively, the contents of this file may be used under the terms of
|
||||||
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
||||||
|
@ -70,108 +71,24 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
nsCOMPtr<nsINode> mCurrentNode;
|
nsCOMPtr<nsINode> mCurrentNode;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Array with all child indexes up the tree. This should only be
|
* Implements FirstChild and LastChild which only vary in which direction
|
||||||
* considered a hint and the value could be wrong.
|
* they search.
|
||||||
*/
|
* @param aReversed Controls whether we search forwards or backwards
|
||||||
nsAutoTArray<PRInt32, 8> mPossibleIndexes;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Position of mCurrentNode in mPossibleIndexes
|
|
||||||
*/
|
|
||||||
PRInt32 mPossibleIndexesPos;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Finds the first child of aNode and returns it. If a child is
|
|
||||||
* found, mCurrentNode is set to that child.
|
|
||||||
* @param aNode Node to search for children.
|
|
||||||
* @param aReversed Reverses search to find the last child instead
|
|
||||||
* of first.
|
|
||||||
* @param aIndexPos Position of aNode in mPossibleIndexes.
|
|
||||||
* @param _retval Returned node. Null if no child is found
|
* @param _retval Returned node. Null if no child is found
|
||||||
* @returns Errorcode
|
* @returns Errorcode
|
||||||
*/
|
*/
|
||||||
nsresult FirstChildOf(nsINode* aNode,
|
nsresult FirstChildInternal(PRBool aReversed, nsIDOMNode **_retval);
|
||||||
PRBool aReversed,
|
|
||||||
PRInt32 aIndexPos,
|
|
||||||
nsINode** _retval);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Finds the following sibling of aNode and returns it. If a sibling
|
* Implements NextSibling and PreviousSibling which only vary in which
|
||||||
* is found, mCurrentNode is set to that node.
|
* direction they search.
|
||||||
* @param aNode Node to start search at.
|
* @param aReversed Controls whether we search forwards or backwards
|
||||||
* @param aReversed Reverses search to find the previous sibling
|
|
||||||
* instead of next.
|
|
||||||
* @param aIndexPos Position of aNode in mPossibleIndexes.
|
|
||||||
* @param _retval Returned node. Null if no sibling is found
|
|
||||||
* @returns Errorcode
|
|
||||||
*/
|
|
||||||
nsresult NextSiblingOf(nsINode* aNode,
|
|
||||||
PRBool aReversed,
|
|
||||||
PRInt32 aIndexPos,
|
|
||||||
nsINode** _retval);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Finds the next node in document order of aNode and returns it.
|
|
||||||
* If a node is found, mCurrentNode is set to that node.
|
|
||||||
* @param aNode Node to start search at.
|
|
||||||
* @param aReversed Reverses search to find the preceding node
|
|
||||||
* instead of next.
|
|
||||||
* @param aIndexPos Position of aNode in mPossibleIndexes.
|
|
||||||
* @param _retval Returned node. Null if no node is found
|
|
||||||
* @returns Errorcode
|
|
||||||
*/
|
|
||||||
nsresult NextInDocumentOrderOf(nsINode* aNode,
|
|
||||||
PRBool aReversed,
|
|
||||||
PRInt32 aIndexPos,
|
|
||||||
nsINode** _retval);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Finds the first child of aNode after child N and returns it. If a
|
|
||||||
* child is found, mCurrentNode is set to that child
|
|
||||||
* @param aNode Node to search for children
|
|
||||||
* @param childNum Child number to start search from. The child with
|
|
||||||
* this number is not searched
|
|
||||||
* @param aReversed Reverses search to find the last child instead
|
|
||||||
* of first
|
|
||||||
* @param aIndexPos Position of aNode in mPossibleIndexes
|
|
||||||
* @param _retval Returned node. Null if no child is found
|
* @param _retval Returned node. Null if no child is found
|
||||||
* @returns Errorcode
|
* @returns Errorcode
|
||||||
*/
|
*/
|
||||||
nsresult ChildOf(nsINode* aNode,
|
nsresult NextSiblingInternal(PRBool aReversed, nsIDOMNode **_retval);
|
||||||
PRInt32 childNum,
|
|
||||||
PRBool aReversed,
|
|
||||||
PRInt32 aIndexPos,
|
|
||||||
nsINode** _retval);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Gets the child index of a node within its parent. Gets a possible index
|
|
||||||
* from mPossibleIndexes to gain speed. If the value in mPossibleIndexes
|
|
||||||
* isn't correct it'll get the index the usual way.
|
|
||||||
* @param aParent in which to get the index
|
|
||||||
* @param aChild node to get the index of
|
|
||||||
* @param aIndexPos position in mPossibleIndexes that contains the possible.
|
|
||||||
* index
|
|
||||||
* @returns resulting index
|
|
||||||
*/
|
|
||||||
PRInt32 IndexOf(nsINode* aParent,
|
|
||||||
nsINode* aChild,
|
|
||||||
PRInt32 aIndexPos);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Sets the child index at the specified level. It doesn't matter if this
|
|
||||||
* fails since mPossibleIndexes should only be considered a hint
|
|
||||||
* @param aIndexPos position in mPossibleIndexes to set
|
|
||||||
* @param aChildIndex child index at specified position
|
|
||||||
*/
|
|
||||||
void SetChildIndex(PRInt32 aIndexPos, PRInt32 aChildIndex)
|
|
||||||
{
|
|
||||||
if (aIndexPos >= 0 &&
|
|
||||||
mPossibleIndexes.EnsureLengthAtLeast(aIndexPos+1)) {
|
|
||||||
mPossibleIndexes.ElementAt(aIndexPos) = aChildIndex;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Загрузка…
Ссылка в новой задаче