Landing fix for bug 132824. Implement NodeIterator object of Traversal API. Patch by craig.topper@gmail.com, r+sr=jonas@sicking.cc

This commit is contained in:
Johnny Stenback 2008-07-21 17:35:38 -07:00
parent 30b2de5b9b
commit bd639e9ac3
7 changed files with 60 additions and 116 deletions

View File

@ -146,6 +146,7 @@ CPPSRCS = \
nsNoDataProtocolContentPolicy.cpp \
nsNodeInfo.cpp \
nsNodeInfoManager.cpp \
nsNodeIterator.cpp \
nsNodeUtils.cpp \
nsObjectLoadingContent.cpp \
nsParserUtils.cpp \
@ -164,6 +165,7 @@ CPPSRCS = \
nsSyncLoadService.cpp \
nsTextFragment.cpp \
nsTextNode.cpp \
nsTraversal.cpp \
nsTreeWalker.cpp \
nsXMLContentSerializer.cpp \
nsXMLHttpRequest.cpp \

View File

@ -85,6 +85,7 @@
#include "nsIDOMText.h"
#include "nsIDOMComment.h"
#include "nsDOMDocumentType.h"
#include "nsNodeIterator.h"
#include "nsTreeWalker.h"
#include "nsIServiceManager.h"
@ -4201,7 +4202,28 @@ nsDocument::CreateNodeIterator(nsIDOMNode *aRoot,
PRBool aEntityReferenceExpansion,
nsIDOMNodeIterator **_retval)
{
return NS_ERROR_NOT_IMPLEMENTED;
*_retval = nsnull;
if (!aRoot)
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
nsresult rv = nsContentUtils::CheckSameOrigin(this, aRoot);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG_POINTER(_retval);
nsCOMPtr<nsINode> root = do_QueryInterface(aRoot);
NS_ENSURE_TRUE(root, NS_ERROR_DOM_NOT_SUPPORTED_ERR);
nsNodeIterator *iterator = new nsNodeIterator(root,
aWhatToShow,
aFilter,
aEntityReferenceExpansion);
NS_ENSURE_TRUE(iterator, NS_ERROR_OUT_OF_MEMORY);
NS_ADDREF(*_retval = iterator);
return NS_OK;
}
NS_IMETHODIMP
@ -4213,17 +4235,26 @@ nsDocument::CreateTreeWalker(nsIDOMNode *aRoot,
{
*_retval = nsnull;
if (!aRoot) {
if (!aRoot)
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
}
nsresult rv = nsContentUtils::CheckSameOrigin(this, aRoot);
if (NS_FAILED(rv)) {
return rv;
}
NS_ENSURE_SUCCESS(rv, rv);
return NS_NewTreeWalker(aRoot, aWhatToShow, aFilter,
aEntityReferenceExpansion, _retval);
NS_ENSURE_ARG_POINTER(_retval);
nsCOMPtr<nsINode> root = do_QueryInterface(aRoot);
NS_ENSURE_TRUE(root, NS_ERROR_DOM_NOT_SUPPORTED_ERR);
nsTreeWalker* walker = new nsTreeWalker(root,
aWhatToShow,
aFilter,
aEntityReferenceExpansion);
NS_ENSURE_TRUE(walker, NS_ERROR_OUT_OF_MEMORY);
NS_ADDREF(*_retval = walker);
return NS_OK;
}

View File

@ -51,47 +51,19 @@
#include "nsIDocument.h"
#include "nsContentUtils.h"
#include "nsMemory.h"
#include "nsCOMArray.h"
#include "nsGkAtoms.h"
/*
* Factories, constructors and destructors
*/
nsresult
NS_NewTreeWalker(nsIDOMNode *aRoot,
PRUint32 aWhatToShow,
nsIDOMNodeFilter *aFilter,
PRBool aEntityReferenceExpansion,
nsIDOMTreeWalker **aInstancePtrResult)
{
NS_ENSURE_ARG_POINTER(aInstancePtrResult);
nsCOMPtr<nsINode> root = do_QueryInterface(aRoot);
NS_ENSURE_TRUE(root, NS_ERROR_DOM_NOT_SUPPORTED_ERR);
nsTreeWalker* walker = new nsTreeWalker(root,
aWhatToShow,
aFilter,
aEntityReferenceExpansion);
NS_ENSURE_TRUE(walker, NS_ERROR_OUT_OF_MEMORY);
return CallQueryInterface(walker, aInstancePtrResult);
}
nsTreeWalker::nsTreeWalker(nsINode *aRoot,
PRUint32 aWhatToShow,
nsIDOMNodeFilter *aFilter,
PRBool aExpandEntityReferences) :
mRoot(aRoot),
mWhatToShow(aWhatToShow),
mFilter(aFilter),
mExpandEntityReferences(aExpandEntityReferences),
nsTraversal(aRoot, aWhatToShow, aFilter, aExpandEntityReferences),
mCurrentNode(aRoot),
mPossibleIndexesPos(-1)
{
NS_ASSERTION(aRoot, "invalid root in call to nsTreeWalker constructor");
}
nsTreeWalker::~nsTreeWalker()
@ -572,63 +544,6 @@ nsTreeWalker::ChildOf(nsINode* aNode,
return NS_OK;
}
/*
* Tests if and how a node should be filtered. Uses mWhatToShow and
* mFilter to test the node.
* @param aNode Node to test
* @param _filtered Returned filtervalue. See nsIDOMNodeFilter.idl
* @returns Errorcode
*/
nsresult nsTreeWalker::TestNode(nsINode* aNode, PRInt16* _filtered)
{
nsresult rv;
*_filtered = nsIDOMNodeFilter::FILTER_SKIP;
PRUint16 nodeType = 0;
// Check the most common cases
if (aNode->IsNodeOfType(nsINode::eELEMENT)) {
nodeType = nsIDOMNode::ELEMENT_NODE;
}
else if (aNode->IsNodeOfType(nsINode::eCONTENT)) {
nsIAtom* tag = static_cast<nsIContent*>(aNode)->Tag();
if (tag == nsGkAtoms::textTagName) {
nodeType = nsIDOMNode::TEXT_NODE;
}
else if (tag == nsGkAtoms::cdataTagName) {
nodeType = nsIDOMNode::CDATA_SECTION_NODE;
}
else if (tag == nsGkAtoms::commentTagName) {
nodeType = nsIDOMNode::COMMENT_NODE;
}
else if (tag == nsGkAtoms::processingInstructionTagName) {
nodeType = nsIDOMNode::PROCESSING_INSTRUCTION_NODE;
}
}
nsCOMPtr<nsIDOMNode> domNode;
if (!nodeType) {
domNode = do_QueryInterface(aNode);
rv = domNode->GetNodeType(&nodeType);
NS_ENSURE_SUCCESS(rv, rv);
}
if (nodeType <= 12 && !((1 << (nodeType-1)) & mWhatToShow)) {
return NS_OK;
}
if (mFilter) {
if (!domNode) {
domNode = do_QueryInterface(aNode);
}
return mFilter->AcceptNode(domNode, _filtered);
}
*_filtered = nsIDOMNodeFilter::FILTER_ACCEPT;
return NS_OK;
}
/*
* Gets the child index of a node within it's parent. Gets a possible index
* from mPossibleIndexes to gain speed. If the value in mPossibleIndexes

View File

@ -45,16 +45,16 @@
#define nsTreeWalker_h___
#include "nsIDOMTreeWalker.h"
#include "nsTraversal.h"
#include "nsCOMPtr.h"
#include "nsVoidArray.h"
#include "nsJSUtils.h"
#include "nsCycleCollectionParticipant.h"
class nsINode;
class nsIDOMNode;
class nsIDOMNodeFilter;
class nsTreeWalker : public nsIDOMTreeWalker
class nsTreeWalker : public nsIDOMTreeWalker, public nsTraversal
{
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
@ -69,10 +69,6 @@ public:
NS_DECL_CYCLE_COLLECTION_CLASS(nsTreeWalker)
private:
nsCOMPtr<nsINode> mRoot;
PRUint32 mWhatToShow;
nsCOMPtr<nsIDOMNodeFilter> mFilter;
PRBool mExpandEntityReferences;
nsCOMPtr<nsINode> mCurrentNode;
/*
@ -150,15 +146,6 @@ private:
PRInt32 aIndexPos,
nsINode** _retval);
/*
* Tests if and how a node should be filtered. Uses mWhatToShow and
* mFilter to test the node.
* @param aNode Node to test
* @param _filtered Returned filtervalue. See nsIDOMNodeFilter.idl
* @returns Errorcode
*/
nsresult TestNode(nsINode* aNode, PRInt16* _filtered);
/*
* Gets the child index of a node within it's parent. Gets a possible index
* from mPossibleIndexes to gain speed. If the value in mPossibleIndexes
@ -187,12 +174,5 @@ private:
}
};
// Make a new nsIDOMTreeWalker object
nsresult NS_NewTreeWalker(nsIDOMNode *aRoot,
PRUint32 aWhatToShow,
nsIDOMNodeFilter *aFilter,
PRBool aEntityReferenceExpansion,
nsIDOMTreeWalker **aInstancePtrResult);
#endif

View File

@ -56,4 +56,8 @@ interface nsIDOMNodeIterator : nsISupports
nsIDOMNode previousNode()
raises(DOMException);
void detach();
// WebKit extensions, convenient for debugging.
readonly attribute nsIDOMNode referenceNode;
readonly attribute boolean pointerBeforeReferenceNode;
};

View File

@ -437,6 +437,9 @@ enum nsDOMClassInfoID {
eDOMClassInfo_HTMLAudioElement_id,
#endif
// DOM Traversal NodeIterator class
eDOMClassInfo_NodeIterator_id,
// This one better be the last one in this list
eDOMClassInfoIDCount
};

View File

@ -333,6 +333,7 @@
#include "nsIDOMRange.h"
#include "nsIDOMNSRange.h"
#include "nsIDOMRangeException.h"
#include "nsIDOMNodeIterator.h"
#include "nsIDOMTreeWalker.h"
#include "nsIDOMXULDocument.h"
#include "nsIDOMXULElement.h"
@ -1264,6 +1265,10 @@ static nsDOMClassInfoData sClassInfoData[] = {
NS_DEFINE_CLASSINFO_DATA(HTMLAudioElement, nsHTMLElementSH,
ELEMENT_SCRIPTABLE_FLAGS)
#endif
// DOM Traversal NodeIterator class
NS_DEFINE_CLASSINFO_DATA(NodeIterator, nsDOMGenericSH,
DOM_DEFAULT_SCRIPTABLE_FLAGS)
};
// Objects that shuld be constructable through |new Name();|
@ -2512,6 +2517,10 @@ nsDOMClassInfo::Init()
DOM_CLASSINFO_MAP_ENTRY(nsIDOMNSRange)
DOM_CLASSINFO_MAP_END
DOM_CLASSINFO_MAP_BEGIN(NodeIterator, nsIDOMNodeIterator)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMNodeIterator)
DOM_CLASSINFO_MAP_END
DOM_CLASSINFO_MAP_BEGIN(TreeWalker, nsIDOMTreeWalker)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMTreeWalker)
DOM_CLASSINFO_MAP_END