XPathResult.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 mozilla_dom_XPathResult_h
  6. #define mozilla_dom_XPathResult_h
  7. #include "nsStubMutationObserver.h"
  8. #include "nsAutoPtr.h"
  9. #include "nsCOMPtr.h"
  10. #include "nsCOMArray.h"
  11. #include "nsWeakPtr.h"
  12. #include "nsCycleCollectionParticipant.h"
  13. #include "mozilla/Attributes.h"
  14. #include "mozilla/ErrorResult.h"
  15. #include "nsString.h"
  16. #include "nsWrapperCache.h"
  17. #include "nsINode.h"
  18. class nsIDocument;
  19. class txAExprResult;
  20. // {662f2c9a-c7cd-4cab-9349-e733df5a838c}
  21. #define NS_IXPATHRESULT_IID \
  22. { 0x662f2c9a, 0xc7cd, 0x4cab, {0x93, 0x49, 0xe7, 0x33, 0xdf, 0x5a, 0x83, 0x8c }}
  23. class nsIXPathResult : public nsISupports
  24. {
  25. public:
  26. NS_DECLARE_STATIC_IID_ACCESSOR(NS_IXPATHRESULT_IID)
  27. virtual nsresult SetExprResult(txAExprResult *aExprResult,
  28. uint16_t aResultType,
  29. nsINode* aContextNode) = 0;
  30. virtual nsresult GetExprResult(txAExprResult **aExprResult) = 0;
  31. virtual nsresult Clone(nsIXPathResult **aResult) = 0;
  32. };
  33. NS_DEFINE_STATIC_IID_ACCESSOR(nsIXPathResult, NS_IXPATHRESULT_IID)
  34. namespace mozilla {
  35. namespace dom {
  36. /**
  37. * A class for evaluating an XPath expression string
  38. */
  39. class XPathResult final : public nsIXPathResult,
  40. public nsStubMutationObserver,
  41. public nsWrapperCache
  42. {
  43. ~XPathResult();
  44. public:
  45. explicit XPathResult(nsINode* aParent);
  46. XPathResult(const XPathResult &aResult);
  47. enum {
  48. ANY_TYPE = 0U,
  49. NUMBER_TYPE = 1U,
  50. STRING_TYPE = 2U,
  51. BOOLEAN_TYPE = 3U,
  52. UNORDERED_NODE_ITERATOR_TYPE = 4U,
  53. ORDERED_NODE_ITERATOR_TYPE = 5U,
  54. UNORDERED_NODE_SNAPSHOT_TYPE = 6U,
  55. ORDERED_NODE_SNAPSHOT_TYPE = 7U,
  56. ANY_UNORDERED_NODE_TYPE = 8U,
  57. FIRST_ORDERED_NODE_TYPE = 9U
  58. };
  59. // nsISupports interface
  60. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  61. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(XPathResult,
  62. nsIXPathResult)
  63. static XPathResult* FromSupports(nsISupports* aSupports)
  64. {
  65. return static_cast<XPathResult*>(static_cast<nsIXPathResult*>(aSupports));
  66. }
  67. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  68. nsINode* GetParentObject() const
  69. {
  70. return mParent;
  71. }
  72. uint16_t ResultType() const
  73. {
  74. return mResultType;
  75. }
  76. double GetNumberValue(ErrorResult& aRv) const
  77. {
  78. if (mResultType != NUMBER_TYPE) {
  79. aRv.Throw(NS_ERROR_DOM_TYPE_ERR);
  80. return 0;
  81. }
  82. return mNumberResult;
  83. }
  84. void GetStringValue(nsAString &aStringValue, ErrorResult& aRv) const
  85. {
  86. if (mResultType != STRING_TYPE) {
  87. aRv.Throw(NS_ERROR_DOM_TYPE_ERR);
  88. return;
  89. }
  90. aStringValue = mStringResult;
  91. }
  92. bool GetBooleanValue(ErrorResult& aRv) const
  93. {
  94. if (mResultType != BOOLEAN_TYPE) {
  95. aRv.Throw(NS_ERROR_DOM_TYPE_ERR);
  96. return false;
  97. }
  98. return mBooleanResult;
  99. }
  100. nsINode* GetSingleNodeValue(ErrorResult& aRv) const
  101. {
  102. if (!isNode()) {
  103. aRv.Throw(NS_ERROR_DOM_TYPE_ERR);
  104. return nullptr;
  105. }
  106. return mResultNodes.SafeObjectAt(0);
  107. }
  108. bool InvalidIteratorState() const
  109. {
  110. return isIterator() && mInvalidIteratorState;
  111. }
  112. uint32_t GetSnapshotLength(ErrorResult& aRv) const
  113. {
  114. if (!isSnapshot()) {
  115. aRv.Throw(NS_ERROR_DOM_TYPE_ERR);
  116. return 0;
  117. }
  118. return (uint32_t)mResultNodes.Count();
  119. }
  120. nsINode* IterateNext(ErrorResult& aRv);
  121. nsINode* SnapshotItem(uint32_t aIndex, ErrorResult& aRv) const
  122. {
  123. if (!isSnapshot()) {
  124. aRv.Throw(NS_ERROR_DOM_TYPE_ERR);
  125. return nullptr;
  126. }
  127. return mResultNodes.SafeObjectAt(aIndex);
  128. }
  129. // nsIMutationObserver interface
  130. NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
  131. NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
  132. NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
  133. NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
  134. NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
  135. NS_DECL_NSIMUTATIONOBSERVER_NODEWILLBEDESTROYED
  136. nsresult SetExprResult(txAExprResult *aExprResult, uint16_t aResultType,
  137. nsINode* aContextNode) override;
  138. nsresult GetExprResult(txAExprResult **aExprResult) override;
  139. nsresult Clone(nsIXPathResult **aResult) override;
  140. void RemoveObserver();
  141. private:
  142. static bool isSnapshot(uint16_t aResultType)
  143. {
  144. return aResultType == UNORDERED_NODE_SNAPSHOT_TYPE ||
  145. aResultType == ORDERED_NODE_SNAPSHOT_TYPE;
  146. }
  147. static bool isIterator(uint16_t aResultType)
  148. {
  149. return aResultType == UNORDERED_NODE_ITERATOR_TYPE ||
  150. aResultType == ORDERED_NODE_ITERATOR_TYPE;
  151. }
  152. static bool isNode(uint16_t aResultType)
  153. {
  154. return aResultType == FIRST_ORDERED_NODE_TYPE ||
  155. aResultType == ANY_UNORDERED_NODE_TYPE;
  156. }
  157. bool isSnapshot() const
  158. {
  159. return isSnapshot(mResultType);
  160. }
  161. bool isIterator() const
  162. {
  163. return isIterator(mResultType);
  164. }
  165. bool isNode() const
  166. {
  167. return isNode(mResultType);
  168. }
  169. void Invalidate(const nsIContent* aChangeRoot);
  170. nsCOMPtr<nsINode> mParent;
  171. RefPtr<txAExprResult> mResult;
  172. nsCOMArray<nsINode> mResultNodes;
  173. nsCOMPtr<nsIDocument> mDocument;
  174. nsWeakPtr mContextNode;
  175. uint32_t mCurrentPos;
  176. uint16_t mResultType;
  177. bool mInvalidIteratorState;
  178. bool mBooleanResult;
  179. double mNumberResult;
  180. nsString mStringResult;
  181. };
  182. } // namespace dom
  183. } // namespace mozilla
  184. #endif /* mozilla_dom_XPathResult_h */