1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3
* The contents of this file are subject to the Mozilla Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is Mozilla.
15
* The Initial Developer of the Original Code is Netscape
16
* Communications. Portions created by Netscape Communications are
17
* Copyright (C) 2001 by Netscape Communications. All
21
* Vidur Apparao <vidur@netscape.com> (original author)
24
#ifndef _nsDOMUtils_h__
25
#define _nsDOMUtils_h__
28
#include "nsIContent.h"
29
#include "nsINodeInfo.h"
30
#include "nsIDOMElement.h"
31
#include "nsIDOMNode.h"
32
#include "nsIDOMNodeList.h"
36
#include "nsReadableUtils.h"
38
// collection includes
39
#include "nsVoidArray.h"
41
////////////////////////////////////////////////////////////
43
// nsChildElementIterator
45
////////////////////////////////////////////////////////////
47
class nsChildElementIterator {
49
nsCOMPtr<nsIDOMNodeList> mNodeList;
53
const char** mNamespaceArray;
54
PRUint32 mNumNamespaces;
57
nsChildElementIterator(nsIDOMElement* aParent) :
58
mIndex(0), mLength(0), mNumNamespaces(0)
63
nsChildElementIterator(nsIDOMElement* aParent,
64
const nsAString& aNamespace) :
65
mIndex(0), mLength(0), mNamespace(aNamespace), mNumNamespaces(0)
70
nsChildElementIterator(nsIDOMElement* aParent,
71
const char** aNamespaceArray,
72
PRUint32 aNumNamespaces) :
73
mIndex(0), mLength(0), mNamespaceArray(aNamespaceArray),
74
mNumNamespaces(aNumNamespaces)
79
~nsChildElementIterator() {}
81
void SetElement(nsIDOMElement* aParent)
83
aParent->GetChildNodes(getter_AddRefs(mNodeList));
85
mNodeList->GetLength(&mLength);
89
PRUint32 GetCurrentIndex() { return mIndex; }
91
void Reset(PRUint32 aIndex=0) { mIndex = aIndex; }
93
PRBool HasChildNodes() { return mLength; }
95
nsresult GetNextChild(nsIDOMElement** aChildElement,
96
nsIAtom** aElementName)
98
*aChildElement = nsnull;
101
return NS_ERROR_FAILURE;
104
nsCOMPtr<nsIDOMNode> child;
105
while (mIndex < mLength) {
106
mNodeList->Item(mIndex++, getter_AddRefs(child));
107
nsCOMPtr<nsIDOMElement> childElement(do_QueryInterface(child));
112
// Confirm that the element is an element of the specified namespace
113
nsAutoString namespaceURI;
114
childElement->GetNamespaceURI(namespaceURI);
115
if (!mNamespace.IsEmpty()) {
116
if (!namespaceURI.Equals(mNamespace)) {
120
else if (mNumNamespaces) {
122
for (i = 0; i < mNumNamespaces; i++) {
123
if (!namespaceURI.Equals(NS_ConvertASCIItoUCS2(mNamespaceArray[i]))) {
129
nsCOMPtr<nsIContent> content(do_QueryInterface(childElement));
130
NS_ASSERTION(content, "Element is not content");
132
return NS_ERROR_FAILURE;
135
nsINodeInfo *nodeInfo = content->GetNodeInfo();
137
return NS_ERROR_FAILURE;
140
NS_ADDREF(*aElementName = nodeInfo->NameAtom());
142
*aChildElement = childElement;
143
NS_ADDREF(*aChildElement);
151
#endif // _nsDOMUtils_h__