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