2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef txXPathTreeWalker_h__
|
|
|
|
#define txXPathTreeWalker_h__
|
|
|
|
|
|
|
|
#include "txCore.h"
|
|
|
|
#include "txXPathNode.h"
|
2013-10-02 04:40:07 -07:00
|
|
|
#include "nsIContentInlines.h"
|
2009-03-20 01:15:35 -07:00
|
|
|
#include "nsTArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-01 09:49:25 -07:00
|
|
|
class nsIAtom;
|
|
|
|
class nsIDOMDocument;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
class txUint32Array : public nsTArray<uint32_t>
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2012-08-22 08:56:38 -07:00
|
|
|
bool AppendValue(uint32_t aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
return AppendElement(aValue) != nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
bool RemoveValueAt(uint32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-03-20 01:15:35 -07:00
|
|
|
if (aIndex < Length()) {
|
|
|
|
RemoveElementAt(aIndex);
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t ValueAt(uint32_t aIndex) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-03-20 01:15:35 -07:00
|
|
|
return (aIndex < Length()) ? ElementAt(aIndex) : 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class txXPathTreeWalker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
txXPathTreeWalker(const txXPathTreeWalker& aOther);
|
|
|
|
explicit txXPathTreeWalker(const txXPathNode& aNode);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool getAttr(nsIAtom* aLocalName, int32_t aNSID, nsAString& aValue) const;
|
|
|
|
int32_t getNamespaceID() const;
|
|
|
|
uint16_t getNodeType() const;
|
2007-03-22 10:30:00 -07:00
|
|
|
void appendNodeValue(nsAString& aResult) const;
|
|
|
|
void getNodeName(nsAString& aName) const;
|
|
|
|
|
|
|
|
void moveTo(const txXPathTreeWalker& aWalker);
|
|
|
|
|
|
|
|
void moveToRoot();
|
2011-09-28 23:19:26 -07:00
|
|
|
bool moveToParent();
|
|
|
|
bool moveToElementById(const nsAString& aID);
|
|
|
|
bool moveToFirstAttribute();
|
|
|
|
bool moveToNextAttribute();
|
2012-08-22 08:56:38 -07:00
|
|
|
bool moveToNamedAttribute(nsIAtom* aLocalName, int32_t aNSID);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool moveToFirstChild();
|
|
|
|
bool moveToLastChild();
|
|
|
|
bool moveToNextSibling();
|
|
|
|
bool moveToPreviousSibling();
|
|
|
|
|
|
|
|
bool isOnNode(const txXPathNode& aNode) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
const txXPathNode& getCurrentPosition() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
txXPathNode mPosition;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool moveToValidAttribute(uint32_t aStartIndex);
|
|
|
|
bool moveToSibling(int32_t aDir);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mCurrentIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
txUint32Array mDescendants;
|
|
|
|
};
|
|
|
|
|
|
|
|
class txXPathNodeUtils
|
|
|
|
{
|
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool getAttr(const txXPathNode& aNode, nsIAtom* aLocalName,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNSID, nsAString& aValue);
|
2007-03-22 10:30:00 -07:00
|
|
|
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);
|
2012-08-22 08:56:38 -07:00
|
|
|
static int32_t getNamespaceID(const txXPathNode& aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
static void getNamespaceURI(const txXPathNode& aNode, nsAString& aURI);
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint16_t getNodeType(const txXPathNode& aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
static void appendNodeValue(const txXPathNode& aNode, nsAString& aResult);
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool isWhitespace(const txXPathNode& aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
static txXPathNode* getOwnerDocument(const txXPathNode& aNode);
|
2012-08-22 08:56:38 -07:00
|
|
|
static int32_t getUniqueIdentifier(const txXPathNode& aNode);
|
2011-04-11 18:28:28 -07:00
|
|
|
static nsresult getXSLTId(const txXPathNode& aNode,
|
|
|
|
const txXPathNode& aBase, nsAString& aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
static void release(txXPathNode* aNode);
|
|
|
|
static void getBaseURI(const txXPathNode& aNode, nsAString& aURI);
|
2012-08-09 00:09:40 -07:00
|
|
|
static int comparePosition(const txXPathNode& aNode,
|
|
|
|
const txXPathNode& aOtherNode);
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool localNameEquals(const txXPathNode& aNode,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIAtom* aLocalName);
|
2011-09-28 23:19:26 -07:00
|
|
|
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)
|
2009-06-09 00:41:19 -07:00
|
|
|
{
|
|
|
|
if (!aNode.isContent()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-06-09 00:41:19 -07:00
|
|
|
}
|
|
|
|
nsIContent* content = aNode.Content();
|
2009-08-24 13:02:07 -07:00
|
|
|
return content->IsHTML() && content->IsInHTMLDocument();
|
2009-06-09 00:41:19 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class txXPathNativeNode
|
|
|
|
{
|
|
|
|
public:
|
2013-07-04 08:40:06 -07:00
|
|
|
static txXPathNode* createXPathNode(nsINode* aNode,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aKeepRootAlive = false);
|
2013-07-04 08:40:06 -07:00
|
|
|
static txXPathNode* createXPathNode(nsIDOMNode* aNode,
|
|
|
|
bool aKeepRootAlive = false)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
return createXPathNode(node, aKeepRootAlive);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
static txXPathNode* createXPathNode(nsIContent* aContent,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aKeepRootAlive = false);
|
2007-03-22 10:30:00 -07:00
|
|
|
static txXPathNode* createXPathNode(nsIDOMDocument* aDocument);
|
2013-07-04 08:40:06 -07:00
|
|
|
static nsINode* getNode(const txXPathNode& aNode);
|
|
|
|
static nsresult getNode(const txXPathNode& aNode, nsIDOMNode** aResult)
|
|
|
|
{
|
|
|
|
return CallQueryInterface(getNode(aNode), aResult);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool
|
2012-08-22 08:56:38 -07:00
|
|
|
txXPathTreeWalker::getAttr(nsIAtom* aLocalName, int32_t aNSID,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aValue) const
|
|
|
|
{
|
|
|
|
return txXPathNodeUtils::getAttr(mPosition, aLocalName, aNSID, aValue);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
inline int32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
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)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
nsINode *root = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mPosition.mRefCountRoot) {
|
|
|
|
root = mPosition.Root();
|
|
|
|
}
|
|
|
|
mPosition.mIndex = aWalker.mPosition.mIndex;
|
|
|
|
mPosition.mRefCountRoot = aWalker.mPosition.mRefCountRoot;
|
|
|
|
mPosition.mNode = aWalker.mPosition.mNode;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsINode *newRoot = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mPosition.mRefCountRoot) {
|
|
|
|
newRoot = mPosition.Root();
|
|
|
|
}
|
|
|
|
if (root != newRoot) {
|
|
|
|
NS_IF_ADDREF(newRoot);
|
|
|
|
NS_IF_RELEASE(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
mCurrentIndex = aWalker.mCurrentIndex;
|
|
|
|
mDescendants.Clear();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool
|
2007-03-22 10:30:00 -07:00
|
|
|
txXPathTreeWalker::isOnNode(const txXPathNode& aNode) const
|
|
|
|
{
|
|
|
|
return (mPosition == aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
2012-08-22 08:56:38 -07:00
|
|
|
inline int32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
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 */
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool
|
2007-03-22 10:30:00 -07:00
|
|
|
txXPathNodeUtils::localNameEquals(const txXPathNode& aNode,
|
|
|
|
nsIAtom* aLocalName)
|
|
|
|
{
|
|
|
|
if (aNode.isContent() &&
|
2010-04-30 06:12:06 -07:00
|
|
|
aNode.Content()->IsElement()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return aNode.Content()->NodeInfo()->Equals(aLocalName);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> localName = txXPathNodeUtils::getLocalName(aNode);
|
|
|
|
|
|
|
|
return localName == aLocalName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool
|
2007-03-22 10:30:00 -07:00
|
|
|
txXPathNodeUtils::isRoot(const txXPathNode& aNode)
|
|
|
|
{
|
2012-10-09 05:31:24 -07:00
|
|
|
return !aNode.isAttribute() && !aNode.mNode->GetParentNode();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool
|
2007-03-22 10:30:00 -07:00
|
|
|
txXPathNodeUtils::isElement(const txXPathNode& aNode)
|
|
|
|
{
|
|
|
|
return aNode.isContent() &&
|
2010-04-30 06:12:06 -07:00
|
|
|
aNode.Content()->IsElement();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* static */
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool
|
2007-03-22 10:30:00 -07:00
|
|
|
txXPathNodeUtils::isAttribute(const txXPathNode& aNode)
|
|
|
|
{
|
|
|
|
return aNode.isAttribute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool
|
2007-03-22 10:30:00 -07:00
|
|
|
txXPathNodeUtils::isProcessingInstruction(const txXPathNode& aNode)
|
|
|
|
{
|
|
|
|
return aNode.isContent() &&
|
|
|
|
aNode.Content()->IsNodeOfType(nsINode::ePROCESSING_INSTRUCTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool
|
2007-03-22 10:30:00 -07:00
|
|
|
txXPathNodeUtils::isComment(const txXPathNode& aNode)
|
|
|
|
{
|
|
|
|
return aNode.isContent() &&
|
|
|
|
aNode.Content()->IsNodeOfType(nsINode::eCOMMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool
|
2007-03-22 10:30:00 -07:00
|
|
|
txXPathNodeUtils::isText(const txXPathNode& aNode)
|
|
|
|
{
|
|
|
|
return aNode.isContent() &&
|
|
|
|
aNode.Content()->IsNodeOfType(nsINode::eTEXT);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* txXPathTreeWalker_h__ */
|