txXPathTreeWalker.h (6060B)
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/. */ 5 6 #ifndef txXPathTreeWalker_h__ 7 #define txXPathTreeWalker_h__ 8 9 #include "nsIContentInlines.h" 10 #include "nsTArray.h" 11 #include "txCore.h" 12 #include "txXPathNode.h" 13 14 class nsAtom; 15 16 class txXPathTreeWalker { 17 public: 18 txXPathTreeWalker(const txXPathTreeWalker& aOther); 19 explicit txXPathTreeWalker(const txXPathNode& aNode); 20 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; 26 27 void moveTo(const txXPathTreeWalker& aWalker); 28 29 void moveToRoot(); 30 bool moveToParent(); 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(); 39 40 bool isOnNode(const txXPathNode& aNode) const; 41 42 const txXPathNode& getCurrentPosition() const; 43 44 private: 45 txXPathNode mPosition; 46 47 bool moveToValidAttribute(uint32_t aStartIndex); 48 }; 49 50 class txXPathNodeUtils { 51 public: 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, 66 nsAString& aResult); 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()) { 80 return false; 81 } 82 nsIContent* content = aNode.Content(); 83 return content->IsHTMLElement() && content->IsInHTMLDocument(); 84 } 85 }; 86 87 class txXPathNativeNode { 88 public: 89 static mozilla::Maybe<txXPathNode> createXPathNode(nsINode* aNode); 90 static nsINode* getNode(const txXPathNode& aNode); 91 static nsIContent* getContent(const txXPathNode& aNode); 92 static mozilla::dom::Document* getDocument(const txXPathNode& aNode); 93 }; 94 95 inline const txXPathNode& txXPathTreeWalker::getCurrentPosition() const { 96 return mPosition; 97 } 98 99 inline bool txXPathTreeWalker::getAttr(nsAtom* aLocalName, int32_t aNSID, 100 nsAString& aValue) const { 101 return txXPathNodeUtils::getAttr(mPosition, aLocalName, aNSID, aValue); 102 } 103 104 inline int32_t txXPathTreeWalker::getNamespaceID() const { 105 return txXPathNodeUtils::getNamespaceID(mPosition); 106 } 107 108 inline void txXPathTreeWalker::appendNodeValue(nsAString& aResult) const { 109 txXPathNodeUtils::appendNodeValue(mPosition, aResult); 110 } 111 112 inline void txXPathTreeWalker::getNodeName(nsAString& aName) const { 113 txXPathNodeUtils::getNodeName(mPosition, aName); 114 } 115 116 inline void txXPathTreeWalker::moveTo(const txXPathTreeWalker& aWalker) { 117 mPosition.mIndex = aWalker.mPosition.mIndex; 118 mPosition.mNode = aWalker.mPosition.mNode; 119 } 120 121 inline bool txXPathTreeWalker::isOnNode(const txXPathNode& aNode) const { 122 return (mPosition == aNode); 123 } 124 125 /* static */ 126 inline int32_t txXPathNodeUtils::getUniqueIdentifier(const txXPathNode& aNode) { 127 MOZ_ASSERT(!aNode.isAttribute(), "Not implemented for attributes."); 128 return NS_PTR_TO_INT32(aNode.mNode.get()); 129 } 130 131 /* static */ 132 inline bool txXPathNodeUtils::localNameEquals(const txXPathNode& aNode, 133 nsAtom* aLocalName) { 134 if (aNode.isContent() && aNode.Content()->IsElement()) { 135 return aNode.Content()->NodeInfo()->Equals(aLocalName); 136 } 137 138 RefPtr<nsAtom> localName = txXPathNodeUtils::getLocalName(aNode); 139 140 return localName == aLocalName; 141 } 142 143 /* static */ 144 inline bool txXPathNodeUtils::isRoot(const txXPathNode& aNode) { 145 return !aNode.isAttribute() && !aNode.mNode->GetParentNode(); 146 } 147 148 /* static */ 149 inline bool txXPathNodeUtils::isElement(const txXPathNode& aNode) { 150 return aNode.isContent() && aNode.Content()->IsElement(); 151 } 152 153 /* static */ 154 inline bool txXPathNodeUtils::isAttribute(const txXPathNode& aNode) { 155 return aNode.isAttribute(); 156 } 157 158 /* static */ 159 inline bool txXPathNodeUtils::isProcessingInstruction( 160 const txXPathNode& aNode) { 161 return aNode.isContent() && aNode.Content()->IsProcessingInstruction(); 162 } 163 164 /* static */ 165 inline bool txXPathNodeUtils::isComment(const txXPathNode& aNode) { 166 return aNode.isContent() && aNode.Content()->IsComment(); 167 } 168 169 /* static */ 170 inline bool txXPathNodeUtils::isText(const txXPathNode& aNode) { 171 return aNode.isContent() && aNode.Content()->IsText(); 172 } 173 174 #endif /* txXPathTreeWalker_h__ */