1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
6 #ifndef txXPathTreeWalker_h__
7 #define txXPathTreeWalker_h__
10 #include "txXPathNode.h"
11 #include "nsIContentInlines.h"
16 class txXPathTreeWalker
{
18 txXPathTreeWalker(const txXPathTreeWalker
& aOther
);
19 explicit txXPathTreeWalker(const txXPathNode
& aNode
);
21 bool getAttr(nsAtom
* aLocalName
, int32_t aNSID
, nsAString
& aValue
) const;
22 int32_t getNamespaceID() const;
23 uint16_t getNodeType() const;
24 void appendNodeValue(nsAString
& aResult
) const;
25 void getNodeName(nsAString
& aName
) const;
27 void moveTo(const txXPathTreeWalker
& aWalker
);
31 bool moveToElementById(const nsAString
& aID
);
32 bool moveToFirstAttribute();
33 bool moveToNextAttribute();
34 bool moveToNamedAttribute(nsAtom
* aLocalName
, int32_t aNSID
);
35 bool moveToFirstChild();
36 bool moveToLastChild();
37 bool moveToNextSibling();
38 bool moveToPreviousSibling();
40 bool isOnNode(const txXPathNode
& aNode
) const;
42 const txXPathNode
& getCurrentPosition() const;
45 txXPathNode mPosition
;
47 bool moveToValidAttribute(uint32_t aStartIndex
);
50 class txXPathNodeUtils
{
52 static bool getAttr(const txXPathNode
& aNode
, nsAtom
* aLocalName
,
53 int32_t aNSID
, nsAString
& aValue
);
54 static already_AddRefed
<nsAtom
> getLocalName(const txXPathNode
& aNode
);
55 static nsAtom
* getPrefix(const txXPathNode
& aNode
);
56 static void getLocalName(const txXPathNode
& aNode
, nsAString
& aLocalName
);
57 static void getNodeName(const txXPathNode
& aNode
, nsAString
& aName
);
58 static int32_t getNamespaceID(const txXPathNode
& aNode
);
59 static void getNamespaceURI(const txXPathNode
& aNode
, nsAString
& aURI
);
60 static uint16_t getNodeType(const txXPathNode
& aNode
);
61 static void appendNodeValue(const txXPathNode
& aNode
, nsAString
& aResult
);
62 static bool isWhitespace(const txXPathNode
& aNode
);
63 static txXPathNode
* getOwnerDocument(const txXPathNode
& aNode
);
64 static int32_t getUniqueIdentifier(const txXPathNode
& aNode
);
65 static nsresult
getXSLTId(const txXPathNode
& aNode
, const txXPathNode
& aBase
,
67 static void release(txXPathNode
* aNode
);
68 static nsresult
getBaseURI(const txXPathNode
& aNode
, nsAString
& aURI
);
69 static int comparePosition(const txXPathNode
& aNode
,
70 const txXPathNode
& aOtherNode
);
71 static bool localNameEquals(const txXPathNode
& aNode
, nsAtom
* aLocalName
);
72 static bool isRoot(const txXPathNode
& aNode
);
73 static bool isElement(const txXPathNode
& aNode
);
74 static bool isAttribute(const txXPathNode
& aNode
);
75 static bool isProcessingInstruction(const txXPathNode
& aNode
);
76 static bool isComment(const txXPathNode
& aNode
);
77 static bool isText(const txXPathNode
& aNode
);
78 static inline bool isHTMLElementInHTMLDocument(const txXPathNode
& aNode
) {
79 if (!aNode
.isContent()) {
82 nsIContent
* content
= aNode
.Content();
83 return content
->IsHTMLElement() && content
->IsInHTMLDocument();
87 class txXPathNativeNode
{
89 static txXPathNode
* createXPathNode(nsINode
* aNode
,
90 bool aKeepRootAlive
= false);
91 static txXPathNode
* createXPathNode(nsIContent
* aContent
,
92 bool aKeepRootAlive
= false);
93 static txXPathNode
* createXPathNode(mozilla::dom::Document
* aDocument
);
94 static nsINode
* getNode(const txXPathNode
& aNode
);
95 static nsIContent
* getContent(const txXPathNode
& aNode
);
96 static mozilla::dom::Document
* getDocument(const txXPathNode
& aNode
);
97 static void addRef(const txXPathNode
& aNode
) { NS_ADDREF(aNode
.mNode
); }
98 static void release(const txXPathNode
& aNode
) {
99 nsINode
* node
= aNode
.mNode
;
104 inline const txXPathNode
& txXPathTreeWalker::getCurrentPosition() const {
108 inline bool txXPathTreeWalker::getAttr(nsAtom
* aLocalName
, int32_t aNSID
,
109 nsAString
& aValue
) const {
110 return txXPathNodeUtils::getAttr(mPosition
, aLocalName
, aNSID
, aValue
);
113 inline int32_t txXPathTreeWalker::getNamespaceID() const {
114 return txXPathNodeUtils::getNamespaceID(mPosition
);
117 inline void txXPathTreeWalker::appendNodeValue(nsAString
& aResult
) const {
118 txXPathNodeUtils::appendNodeValue(mPosition
, aResult
);
121 inline void txXPathTreeWalker::getNodeName(nsAString
& aName
) const {
122 txXPathNodeUtils::getNodeName(mPosition
, aName
);
125 inline void txXPathTreeWalker::moveTo(const txXPathTreeWalker
& aWalker
) {
126 nsINode
* root
= nullptr;
127 if (mPosition
.mRefCountRoot
) {
128 root
= mPosition
.Root();
130 mPosition
.mIndex
= aWalker
.mPosition
.mIndex
;
131 mPosition
.mRefCountRoot
= aWalker
.mPosition
.mRefCountRoot
;
132 mPosition
.mNode
= aWalker
.mPosition
.mNode
;
133 nsINode
* newRoot
= nullptr;
134 if (mPosition
.mRefCountRoot
) {
135 newRoot
= mPosition
.Root();
137 if (root
!= newRoot
) {
138 NS_IF_ADDREF(newRoot
);
143 inline bool txXPathTreeWalker::isOnNode(const txXPathNode
& aNode
) const {
144 return (mPosition
== aNode
);
148 inline int32_t txXPathNodeUtils::getUniqueIdentifier(const txXPathNode
& aNode
) {
149 MOZ_ASSERT(!aNode
.isAttribute(), "Not implemented for attributes.");
150 return NS_PTR_TO_INT32(aNode
.mNode
);
154 inline void txXPathNodeUtils::release(txXPathNode
* aNode
) {
155 NS_RELEASE(aNode
->mNode
);
159 inline bool txXPathNodeUtils::localNameEquals(const txXPathNode
& aNode
,
160 nsAtom
* aLocalName
) {
161 if (aNode
.isContent() && aNode
.Content()->IsElement()) {
162 return aNode
.Content()->NodeInfo()->Equals(aLocalName
);
165 RefPtr
<nsAtom
> localName
= txXPathNodeUtils::getLocalName(aNode
);
167 return localName
== aLocalName
;
171 inline bool txXPathNodeUtils::isRoot(const txXPathNode
& aNode
) {
172 return !aNode
.isAttribute() && !aNode
.mNode
->GetParentNode();
176 inline bool txXPathNodeUtils::isElement(const txXPathNode
& aNode
) {
177 return aNode
.isContent() && aNode
.Content()->IsElement();
181 inline bool txXPathNodeUtils::isAttribute(const txXPathNode
& aNode
) {
182 return aNode
.isAttribute();
186 inline bool txXPathNodeUtils::isProcessingInstruction(
187 const txXPathNode
& aNode
) {
188 return aNode
.isContent() && aNode
.Content()->IsProcessingInstruction();
192 inline bool txXPathNodeUtils::isComment(const txXPathNode
& aNode
) {
193 return aNode
.isContent() && aNode
.Content()->IsComment();
197 inline bool txXPathNodeUtils::isText(const txXPathNode
& aNode
) {
198 return aNode
.isContent() && aNode
.Content()->IsText();
201 #endif /* txXPathTreeWalker_h__ */