txXPathNode.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifndef txXPathNode_h__
  6. #define txXPathNode_h__
  7. #include "nsAutoPtr.h"
  8. #include "nsIContent.h"
  9. #include "nsIDocument.h"
  10. #include "nsIDOMNode.h"
  11. #include "nsNameSpaceManager.h"
  12. #include "nsContentUtils.h" // For NameSpaceManager().
  13. typedef nsIDOMNode txXPathNodeType;
  14. class txXPathNode
  15. {
  16. public:
  17. bool operator==(const txXPathNode& aNode) const;
  18. bool operator!=(const txXPathNode& aNode) const
  19. {
  20. return !(*this == aNode);
  21. }
  22. ~txXPathNode();
  23. private:
  24. friend class txNodeSet;
  25. friend class txXPathNativeNode;
  26. friend class txXPathNodeUtils;
  27. friend class txXPathTreeWalker;
  28. txXPathNode(const txXPathNode& aNode);
  29. explicit txXPathNode(nsIDocument* aDocument) : mNode(aDocument),
  30. mRefCountRoot(0),
  31. mIndex(eDocument)
  32. {
  33. MOZ_COUNT_CTOR(txXPathNode);
  34. }
  35. txXPathNode(nsINode *aNode, uint32_t aIndex, nsINode *aRoot)
  36. : mNode(aNode),
  37. mRefCountRoot(aRoot ? 1 : 0),
  38. mIndex(aIndex)
  39. {
  40. MOZ_COUNT_CTOR(txXPathNode);
  41. if (aRoot) {
  42. NS_ADDREF(aRoot);
  43. }
  44. }
  45. static nsINode *RootOf(nsINode *aNode)
  46. {
  47. nsINode *ancestor, *root = aNode;
  48. while ((ancestor = root->GetParentNode())) {
  49. root = ancestor;
  50. }
  51. return root;
  52. }
  53. nsINode *Root() const
  54. {
  55. return RootOf(mNode);
  56. }
  57. nsINode *GetRootToAddRef() const
  58. {
  59. return mRefCountRoot ? Root() : nullptr;
  60. }
  61. bool isDocument() const
  62. {
  63. return mIndex == eDocument;
  64. }
  65. bool isContent() const
  66. {
  67. return mIndex == eContent;
  68. }
  69. bool isAttribute() const
  70. {
  71. return mIndex != eDocument && mIndex != eContent;
  72. }
  73. nsIContent* Content() const
  74. {
  75. NS_ASSERTION(isContent() || isAttribute(), "wrong type");
  76. return static_cast<nsIContent*>(mNode);
  77. }
  78. nsIDocument* Document() const
  79. {
  80. NS_ASSERTION(isDocument(), "wrong type");
  81. return static_cast<nsIDocument*>(mNode);
  82. }
  83. enum PositionType
  84. {
  85. eDocument = (1 << 30),
  86. eContent = eDocument - 1
  87. };
  88. nsINode* mNode;
  89. uint32_t mRefCountRoot : 1;
  90. uint32_t mIndex : 31;
  91. };
  92. class txNamespaceManager
  93. {
  94. public:
  95. static int32_t getNamespaceID(const nsAString& aNamespaceURI);
  96. static nsresult getNamespaceURI(const int32_t aID, nsAString& aResult);
  97. };
  98. /* static */
  99. inline int32_t
  100. txNamespaceManager::getNamespaceID(const nsAString& aNamespaceURI)
  101. {
  102. int32_t namespaceID = kNameSpaceID_Unknown;
  103. nsContentUtils::NameSpaceManager()->
  104. RegisterNameSpace(aNamespaceURI, namespaceID);
  105. return namespaceID;
  106. }
  107. /* static */
  108. inline nsresult
  109. txNamespaceManager::getNamespaceURI(const int32_t aID, nsAString& aResult)
  110. {
  111. return nsContentUtils::NameSpaceManager()->
  112. GetNameSpaceURI(aID, aResult);
  113. }
  114. inline bool
  115. txXPathNode::operator==(const txXPathNode& aNode) const
  116. {
  117. return mIndex == aNode.mIndex && mNode == aNode.mNode;
  118. }
  119. #endif /* txXPathNode_h__ */