1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef txXPathTreeWalker_h__
#define txXPathTreeWalker_h__
#include "txCore.h"
#include "txXPathNode.h"
#include "nsIContentInlines.h"
#include "nsTArray.h"
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:
txXPathTreeWalker(const txXPathTreeWalker& aOther);
explicit txXPathTreeWalker(const txXPathNode& aNode);
bool getAttr(nsIAtom* aLocalName, int32_t aNSID, nsAString& aValue) const;
int32_t getNamespaceID() const;
uint16_t getNodeType() const;
void appendNodeValue(nsAString& aResult) const;
void getNodeName(nsAString& aName) const;
void moveTo(const txXPathTreeWalker& aWalker);
void moveToRoot();
bool moveToParent();
bool moveToElementById(const nsAString& aID);
bool moveToFirstAttribute();
bool moveToNextAttribute();
bool moveToNamedAttribute(nsIAtom* aLocalName, int32_t aNSID);
bool moveToFirstChild();
bool moveToLastChild();
bool moveToNextSibling();
bool moveToPreviousSibling();
bool isOnNode(const txXPathNode& aNode) const;
const txXPathNode& getCurrentPosition() const;
private:
txXPathNode mPosition;
bool moveToValidAttribute(uint32_t aStartIndex);
bool moveToSibling(int32_t aDir);
uint32_t mCurrentIndex;
txUint32Array mDescendants;
};
class txXPathNodeUtils
{
public:
static bool getAttr(const txXPathNode& aNode, nsIAtom* aLocalName,
int32_t aNSID, nsAString& aValue);
static already_AddRefed<nsIAtom> getLocalName(const txXPathNode& aNode);
static nsIAtom* getPrefix(const txXPathNode& aNode);
static void getLocalName(const txXPathNode& aNode, nsAString& aLocalName);
static void getNodeName(const txXPathNode& aNode,
nsAString& aName);
static int32_t getNamespaceID(const txXPathNode& aNode);
static void getNamespaceURI(const txXPathNode& aNode, nsAString& aURI);
static uint16_t getNodeType(const txXPathNode& aNode);
static void appendNodeValue(const txXPathNode& aNode, nsAString& aResult);
static bool isWhitespace(const txXPathNode& aNode);
static txXPathNode* getOwnerDocument(const txXPathNode& aNode);
static int32_t getUniqueIdentifier(const txXPathNode& aNode);
static nsresult getXSLTId(const txXPathNode& aNode,
const txXPathNode& aBase, nsAString& aResult);
static void release(txXPathNode* aNode);
static nsresult getBaseURI(const txXPathNode& aNode, nsAString& aURI);
static int comparePosition(const txXPathNode& aNode,
const txXPathNode& aOtherNode);
static bool localNameEquals(const txXPathNode& aNode,
nsIAtom* aLocalName);
static bool isRoot(const txXPathNode& aNode);
static bool isElement(const txXPathNode& aNode);
static bool isAttribute(const txXPathNode& aNode);
static bool isProcessingInstruction(const txXPathNode& aNode);
static bool isComment(const txXPathNode& aNode);
static bool isText(const txXPathNode& aNode);
static inline bool isHTMLElementInHTMLDocument(const txXPathNode& aNode)
{
if (!aNode.isContent()) {
return false;
}
nsIContent* content = aNode.Content();
return content->IsHTMLElement() && content->IsInHTMLDocument();
}
};
class txXPathNativeNode
{
public:
static txXPathNode* createXPathNode(nsINode* aNode,
bool aKeepRootAlive = false);
static txXPathNode* createXPathNode(nsIDOMNode* aNode,
bool aKeepRootAlive = false)
{
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
return createXPathNode(node, aKeepRootAlive);
}
static txXPathNode* createXPathNode(nsIContent* aContent,
bool aKeepRootAlive = false);
static txXPathNode* createXPathNode(nsIDOMDocument* aDocument);
static nsINode* getNode(const txXPathNode& aNode);
static nsresult getNode(const txXPathNode& aNode, nsIDOMNode** aResult)
{
return CallQueryInterface(getNode(aNode), aResult);
}
static nsIContent* getContent(const txXPathNode& aNode);
static nsIDocument* getDocument(const txXPathNode& aNode);
static void addRef(const txXPathNode& aNode)
{
NS_ADDREF(aNode.mNode);
}
static void release(const txXPathNode& aNode)
{
nsINode *node = aNode.mNode;
NS_RELEASE(node);
}
};
inline const txXPathNode&
txXPathTreeWalker::getCurrentPosition() const
{
return mPosition;
}
inline bool
txXPathTreeWalker::getAttr(nsIAtom* aLocalName, int32_t aNSID,
nsAString& aValue) const
{
return txXPathNodeUtils::getAttr(mPosition, aLocalName, aNSID, aValue);
}
inline int32_t
txXPathTreeWalker::getNamespaceID() const
{
return txXPathNodeUtils::getNamespaceID(mPosition);
}
inline void
txXPathTreeWalker::appendNodeValue(nsAString& aResult) const
{
txXPathNodeUtils::appendNodeValue(mPosition, aResult);
}
inline void
txXPathTreeWalker::getNodeName(nsAString& aName) const
{
txXPathNodeUtils::getNodeName(mPosition, aName);
}
inline void
txXPathTreeWalker::moveTo(const txXPathTreeWalker& aWalker)
{
nsINode *root = nullptr;
if (mPosition.mRefCountRoot) {
root = mPosition.Root();
}
mPosition.mIndex = aWalker.mPosition.mIndex;
mPosition.mRefCountRoot = aWalker.mPosition.mRefCountRoot;
mPosition.mNode = aWalker.mPosition.mNode;
nsINode *newRoot = nullptr;
if (mPosition.mRefCountRoot) {
newRoot = mPosition.Root();
}
if (root != newRoot) {
NS_IF_ADDREF(newRoot);
NS_IF_RELEASE(root);
}
mCurrentIndex = aWalker.mCurrentIndex;
mDescendants.Clear();
}
inline bool
txXPathTreeWalker::isOnNode(const txXPathNode& aNode) const
{
return (mPosition == aNode);
}
/* static */
inline int32_t
txXPathNodeUtils::getUniqueIdentifier(const txXPathNode& aNode)
{
NS_PRECONDITION(!aNode.isAttribute(),
"Not implemented for attributes.");
return NS_PTR_TO_INT32(aNode.mNode);
}
/* static */
inline void
txXPathNodeUtils::release(txXPathNode* aNode)
{
NS_RELEASE(aNode->mNode);
}
/* static */
inline bool
txXPathNodeUtils::localNameEquals(const txXPathNode& aNode,
nsIAtom* aLocalName)
{
if (aNode.isContent() &&
aNode.Content()->IsElement()) {
return aNode.Content()->NodeInfo()->Equals(aLocalName);
}
nsCOMPtr<nsIAtom> localName = txXPathNodeUtils::getLocalName(aNode);
return localName == aLocalName;
}
/* static */
inline bool
txXPathNodeUtils::isRoot(const txXPathNode& aNode)
{
return !aNode.isAttribute() && !aNode.mNode->GetParentNode();
}
/* static */
inline bool
txXPathNodeUtils::isElement(const txXPathNode& aNode)
{
return aNode.isContent() &&
aNode.Content()->IsElement();
}
/* static */
inline bool
txXPathNodeUtils::isAttribute(const txXPathNode& aNode)
{
return aNode.isAttribute();
}
/* static */
inline bool
txXPathNodeUtils::isProcessingInstruction(const txXPathNode& aNode)
{
return aNode.isContent() &&
aNode.Content()->IsNodeOfType(nsINode::ePROCESSING_INSTRUCTION);
}
/* static */
inline bool
txXPathNodeUtils::isComment(const txXPathNode& aNode)
{
return aNode.isContent() &&
aNode.Content()->IsNodeOfType(nsINode::eCOMMENT);
}
/* static */
inline bool
txXPathNodeUtils::isText(const txXPathNode& aNode)
{
return aNode.isContent() &&
aNode.Content()->IsNodeOfType(nsINode::eTEXT);
}
#endif /* txXPathTreeWalker_h__ */
|