1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/xslt/xpath/txXPathTreeWalker.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,278 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef txXPathTreeWalker_h__ 1.10 +#define txXPathTreeWalker_h__ 1.11 + 1.12 +#include "txCore.h" 1.13 +#include "txXPathNode.h" 1.14 +#include "nsIContentInlines.h" 1.15 +#include "nsINodeInfo.h" 1.16 +#include "nsTArray.h" 1.17 + 1.18 +class nsIAtom; 1.19 +class nsIDOMDocument; 1.20 + 1.21 +class txUint32Array : public nsTArray<uint32_t> 1.22 +{ 1.23 +public: 1.24 + bool AppendValue(uint32_t aValue) 1.25 + { 1.26 + return AppendElement(aValue) != nullptr; 1.27 + } 1.28 + bool RemoveValueAt(uint32_t aIndex) 1.29 + { 1.30 + if (aIndex < Length()) { 1.31 + RemoveElementAt(aIndex); 1.32 + } 1.33 + return true; 1.34 + } 1.35 + uint32_t ValueAt(uint32_t aIndex) const 1.36 + { 1.37 + return (aIndex < Length()) ? ElementAt(aIndex) : 0; 1.38 + } 1.39 +}; 1.40 + 1.41 +class txXPathTreeWalker 1.42 +{ 1.43 +public: 1.44 + txXPathTreeWalker(const txXPathTreeWalker& aOther); 1.45 + explicit txXPathTreeWalker(const txXPathNode& aNode); 1.46 + 1.47 + bool getAttr(nsIAtom* aLocalName, int32_t aNSID, nsAString& aValue) const; 1.48 + int32_t getNamespaceID() const; 1.49 + uint16_t getNodeType() const; 1.50 + void appendNodeValue(nsAString& aResult) const; 1.51 + void getNodeName(nsAString& aName) const; 1.52 + 1.53 + void moveTo(const txXPathTreeWalker& aWalker); 1.54 + 1.55 + void moveToRoot(); 1.56 + bool moveToParent(); 1.57 + bool moveToElementById(const nsAString& aID); 1.58 + bool moveToFirstAttribute(); 1.59 + bool moveToNextAttribute(); 1.60 + bool moveToNamedAttribute(nsIAtom* aLocalName, int32_t aNSID); 1.61 + bool moveToFirstChild(); 1.62 + bool moveToLastChild(); 1.63 + bool moveToNextSibling(); 1.64 + bool moveToPreviousSibling(); 1.65 + 1.66 + bool isOnNode(const txXPathNode& aNode) const; 1.67 + 1.68 + const txXPathNode& getCurrentPosition() const; 1.69 + 1.70 +private: 1.71 + txXPathNode mPosition; 1.72 + 1.73 + bool moveToValidAttribute(uint32_t aStartIndex); 1.74 + bool moveToSibling(int32_t aDir); 1.75 + 1.76 + uint32_t mCurrentIndex; 1.77 + txUint32Array mDescendants; 1.78 +}; 1.79 + 1.80 +class txXPathNodeUtils 1.81 +{ 1.82 +public: 1.83 + static bool getAttr(const txXPathNode& aNode, nsIAtom* aLocalName, 1.84 + int32_t aNSID, nsAString& aValue); 1.85 + static already_AddRefed<nsIAtom> getLocalName(const txXPathNode& aNode); 1.86 + static nsIAtom* getPrefix(const txXPathNode& aNode); 1.87 + static void getLocalName(const txXPathNode& aNode, nsAString& aLocalName); 1.88 + static void getNodeName(const txXPathNode& aNode, 1.89 + nsAString& aName); 1.90 + static int32_t getNamespaceID(const txXPathNode& aNode); 1.91 + static void getNamespaceURI(const txXPathNode& aNode, nsAString& aURI); 1.92 + static uint16_t getNodeType(const txXPathNode& aNode); 1.93 + static void appendNodeValue(const txXPathNode& aNode, nsAString& aResult); 1.94 + static bool isWhitespace(const txXPathNode& aNode); 1.95 + static txXPathNode* getOwnerDocument(const txXPathNode& aNode); 1.96 + static int32_t getUniqueIdentifier(const txXPathNode& aNode); 1.97 + static nsresult getXSLTId(const txXPathNode& aNode, 1.98 + const txXPathNode& aBase, nsAString& aResult); 1.99 + static void release(txXPathNode* aNode); 1.100 + static void getBaseURI(const txXPathNode& aNode, nsAString& aURI); 1.101 + static int comparePosition(const txXPathNode& aNode, 1.102 + const txXPathNode& aOtherNode); 1.103 + static bool localNameEquals(const txXPathNode& aNode, 1.104 + nsIAtom* aLocalName); 1.105 + static bool isRoot(const txXPathNode& aNode); 1.106 + static bool isElement(const txXPathNode& aNode); 1.107 + static bool isAttribute(const txXPathNode& aNode); 1.108 + static bool isProcessingInstruction(const txXPathNode& aNode); 1.109 + static bool isComment(const txXPathNode& aNode); 1.110 + static bool isText(const txXPathNode& aNode); 1.111 + static inline bool isHTMLElementInHTMLDocument(const txXPathNode& aNode) 1.112 + { 1.113 + if (!aNode.isContent()) { 1.114 + return false; 1.115 + } 1.116 + nsIContent* content = aNode.Content(); 1.117 + return content->IsHTML() && content->IsInHTMLDocument(); 1.118 + } 1.119 +}; 1.120 + 1.121 +class txXPathNativeNode 1.122 +{ 1.123 +public: 1.124 + static txXPathNode* createXPathNode(nsIDOMNode* aNode, 1.125 + bool aKeepRootAlive = false); 1.126 + static txXPathNode* createXPathNode(nsIContent* aContent, 1.127 + bool aKeepRootAlive = false); 1.128 + static txXPathNode* createXPathNode(nsIDOMDocument* aDocument); 1.129 + static nsresult getNode(const txXPathNode& aNode, nsIDOMNode** aResult); 1.130 + static nsIContent* getContent(const txXPathNode& aNode); 1.131 + static nsIDocument* getDocument(const txXPathNode& aNode); 1.132 + static void addRef(const txXPathNode& aNode) 1.133 + { 1.134 + NS_ADDREF(aNode.mNode); 1.135 + } 1.136 + static void release(const txXPathNode& aNode) 1.137 + { 1.138 + nsINode *node = aNode.mNode; 1.139 + NS_RELEASE(node); 1.140 + } 1.141 +}; 1.142 + 1.143 +inline const txXPathNode& 1.144 +txXPathTreeWalker::getCurrentPosition() const 1.145 +{ 1.146 + return mPosition; 1.147 +} 1.148 + 1.149 +inline bool 1.150 +txXPathTreeWalker::getAttr(nsIAtom* aLocalName, int32_t aNSID, 1.151 + nsAString& aValue) const 1.152 +{ 1.153 + return txXPathNodeUtils::getAttr(mPosition, aLocalName, aNSID, aValue); 1.154 +} 1.155 + 1.156 +inline int32_t 1.157 +txXPathTreeWalker::getNamespaceID() const 1.158 +{ 1.159 + return txXPathNodeUtils::getNamespaceID(mPosition); 1.160 +} 1.161 + 1.162 +inline void 1.163 +txXPathTreeWalker::appendNodeValue(nsAString& aResult) const 1.164 +{ 1.165 + txXPathNodeUtils::appendNodeValue(mPosition, aResult); 1.166 +} 1.167 + 1.168 +inline void 1.169 +txXPathTreeWalker::getNodeName(nsAString& aName) const 1.170 +{ 1.171 + txXPathNodeUtils::getNodeName(mPosition, aName); 1.172 +} 1.173 + 1.174 +inline void 1.175 +txXPathTreeWalker::moveTo(const txXPathTreeWalker& aWalker) 1.176 +{ 1.177 + nsINode *root = nullptr; 1.178 + if (mPosition.mRefCountRoot) { 1.179 + root = mPosition.Root(); 1.180 + } 1.181 + mPosition.mIndex = aWalker.mPosition.mIndex; 1.182 + mPosition.mRefCountRoot = aWalker.mPosition.mRefCountRoot; 1.183 + mPosition.mNode = aWalker.mPosition.mNode; 1.184 + nsINode *newRoot = nullptr; 1.185 + if (mPosition.mRefCountRoot) { 1.186 + newRoot = mPosition.Root(); 1.187 + } 1.188 + if (root != newRoot) { 1.189 + NS_IF_ADDREF(newRoot); 1.190 + NS_IF_RELEASE(root); 1.191 + } 1.192 + 1.193 + mCurrentIndex = aWalker.mCurrentIndex; 1.194 + mDescendants.Clear(); 1.195 +} 1.196 + 1.197 +inline bool 1.198 +txXPathTreeWalker::isOnNode(const txXPathNode& aNode) const 1.199 +{ 1.200 + return (mPosition == aNode); 1.201 +} 1.202 + 1.203 +/* static */ 1.204 +inline int32_t 1.205 +txXPathNodeUtils::getUniqueIdentifier(const txXPathNode& aNode) 1.206 +{ 1.207 + NS_PRECONDITION(!aNode.isAttribute(), 1.208 + "Not implemented for attributes."); 1.209 + return NS_PTR_TO_INT32(aNode.mNode); 1.210 +} 1.211 + 1.212 +/* static */ 1.213 +inline void 1.214 +txXPathNodeUtils::release(txXPathNode* aNode) 1.215 +{ 1.216 + NS_RELEASE(aNode->mNode); 1.217 +} 1.218 + 1.219 +/* static */ 1.220 +inline bool 1.221 +txXPathNodeUtils::localNameEquals(const txXPathNode& aNode, 1.222 + nsIAtom* aLocalName) 1.223 +{ 1.224 + if (aNode.isContent() && 1.225 + aNode.Content()->IsElement()) { 1.226 + return aNode.Content()->NodeInfo()->Equals(aLocalName); 1.227 + } 1.228 + 1.229 + nsCOMPtr<nsIAtom> localName = txXPathNodeUtils::getLocalName(aNode); 1.230 + 1.231 + return localName == aLocalName; 1.232 +} 1.233 + 1.234 +/* static */ 1.235 +inline bool 1.236 +txXPathNodeUtils::isRoot(const txXPathNode& aNode) 1.237 +{ 1.238 + return !aNode.isAttribute() && !aNode.mNode->GetParentNode(); 1.239 +} 1.240 + 1.241 +/* static */ 1.242 +inline bool 1.243 +txXPathNodeUtils::isElement(const txXPathNode& aNode) 1.244 +{ 1.245 + return aNode.isContent() && 1.246 + aNode.Content()->IsElement(); 1.247 +} 1.248 + 1.249 + 1.250 +/* static */ 1.251 +inline bool 1.252 +txXPathNodeUtils::isAttribute(const txXPathNode& aNode) 1.253 +{ 1.254 + return aNode.isAttribute(); 1.255 +} 1.256 + 1.257 +/* static */ 1.258 +inline bool 1.259 +txXPathNodeUtils::isProcessingInstruction(const txXPathNode& aNode) 1.260 +{ 1.261 + return aNode.isContent() && 1.262 + aNode.Content()->IsNodeOfType(nsINode::ePROCESSING_INSTRUCTION); 1.263 +} 1.264 + 1.265 +/* static */ 1.266 +inline bool 1.267 +txXPathNodeUtils::isComment(const txXPathNode& aNode) 1.268 +{ 1.269 + return aNode.isContent() && 1.270 + aNode.Content()->IsNodeOfType(nsINode::eCOMMENT); 1.271 +} 1.272 + 1.273 +/* static */ 1.274 +inline bool 1.275 +txXPathNodeUtils::isText(const txXPathNode& aNode) 1.276 +{ 1.277 + return aNode.isContent() && 1.278 + aNode.Content()->IsNodeOfType(nsINode::eTEXT); 1.279 +} 1.280 + 1.281 +#endif /* txXPathTreeWalker_h__ */