XPathResult.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. #include "XPathResult.h"
  6. #include "txExprResult.h"
  7. #include "txNodeSet.h"
  8. #include "nsError.h"
  9. #include "mozilla/dom/Attr.h"
  10. #include "mozilla/dom/Element.h"
  11. #include "nsDOMClassInfoID.h"
  12. #include "nsIDOMNode.h"
  13. #include "nsIDOMDocument.h"
  14. #include "nsDOMString.h"
  15. #include "txXPathTreeWalker.h"
  16. #include "nsCycleCollectionParticipant.h"
  17. #include "mozilla/dom/XPathResultBinding.h"
  18. namespace mozilla {
  19. namespace dom {
  20. XPathResult::XPathResult(nsINode* aParent)
  21. : mParent(aParent),
  22. mDocument(nullptr),
  23. mCurrentPos(0),
  24. mResultType(ANY_TYPE),
  25. mInvalidIteratorState(true),
  26. mBooleanResult(false),
  27. mNumberResult(0)
  28. {
  29. }
  30. XPathResult::XPathResult(const XPathResult &aResult)
  31. : mParent(aResult.mParent),
  32. mResult(aResult.mResult),
  33. mResultNodes(aResult.mResultNodes),
  34. mDocument(aResult.mDocument),
  35. mContextNode(aResult.mContextNode),
  36. mCurrentPos(0),
  37. mResultType(aResult.mResultType),
  38. mInvalidIteratorState(aResult.mInvalidIteratorState)
  39. {
  40. if (mDocument) {
  41. mDocument->AddMutationObserver(this);
  42. }
  43. }
  44. XPathResult::~XPathResult()
  45. {
  46. RemoveObserver();
  47. }
  48. NS_IMPL_CYCLE_COLLECTION_CLASS(XPathResult)
  49. NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(XPathResult)
  50. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(XPathResult)
  51. NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
  52. NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
  53. {
  54. tmp->RemoveObserver();
  55. }
  56. NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument)
  57. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  58. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(XPathResult)
  59. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
  60. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument)
  61. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResultNodes)
  62. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  63. NS_IMPL_CYCLE_COLLECTING_ADDREF(XPathResult)
  64. NS_IMPL_CYCLE_COLLECTING_RELEASE(XPathResult)
  65. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(XPathResult)
  66. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  67. NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
  68. NS_INTERFACE_MAP_ENTRY(nsIXPathResult)
  69. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIXPathResult)
  70. NS_INTERFACE_MAP_END
  71. JSObject*
  72. XPathResult::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  73. {
  74. return XPathResultBinding::Wrap(aCx, this, aGivenProto);
  75. }
  76. void
  77. XPathResult::RemoveObserver()
  78. {
  79. if (mDocument) {
  80. mDocument->RemoveMutationObserver(this);
  81. }
  82. }
  83. nsINode*
  84. XPathResult::IterateNext(ErrorResult& aRv)
  85. {
  86. if (!isIterator()) {
  87. aRv.Throw(NS_ERROR_DOM_TYPE_ERR);
  88. return nullptr;
  89. }
  90. if (mDocument) {
  91. mDocument->FlushPendingNotifications(Flush_Content);
  92. }
  93. if (mInvalidIteratorState) {
  94. aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
  95. return nullptr;
  96. }
  97. return mResultNodes.SafeObjectAt(mCurrentPos++);
  98. }
  99. void
  100. XPathResult::NodeWillBeDestroyed(const nsINode* aNode)
  101. {
  102. nsCOMPtr<nsIMutationObserver> kungFuDeathGrip(this);
  103. // Set to null to avoid unregistring unnecessarily
  104. mDocument = nullptr;
  105. Invalidate(aNode->IsNodeOfType(nsINode::eCONTENT) ?
  106. static_cast<const nsIContent*>(aNode) : nullptr);
  107. }
  108. void
  109. XPathResult::CharacterDataChanged(nsIDocument* aDocument,
  110. nsIContent *aContent,
  111. CharacterDataChangeInfo* aInfo)
  112. {
  113. Invalidate(aContent);
  114. }
  115. void
  116. XPathResult::AttributeChanged(nsIDocument* aDocument,
  117. Element* aElement,
  118. int32_t aNameSpaceID,
  119. nsIAtom* aAttribute,
  120. int32_t aModType,
  121. const nsAttrValue* aOldValue)
  122. {
  123. Invalidate(aElement);
  124. }
  125. void
  126. XPathResult::ContentAppended(nsIDocument* aDocument,
  127. nsIContent* aContainer,
  128. nsIContent* aFirstNewContent,
  129. int32_t aNewIndexInContainer)
  130. {
  131. Invalidate(aContainer);
  132. }
  133. void
  134. XPathResult::ContentInserted(nsIDocument* aDocument,
  135. nsIContent* aContainer,
  136. nsIContent* aChild,
  137. int32_t aIndexInContainer)
  138. {
  139. Invalidate(aContainer);
  140. }
  141. void
  142. XPathResult::ContentRemoved(nsIDocument* aDocument,
  143. nsIContent* aContainer,
  144. nsIContent* aChild,
  145. int32_t aIndexInContainer,
  146. nsIContent* aPreviousSibling)
  147. {
  148. Invalidate(aContainer);
  149. }
  150. nsresult
  151. XPathResult::SetExprResult(txAExprResult* aExprResult, uint16_t aResultType,
  152. nsINode* aContextNode)
  153. {
  154. MOZ_ASSERT(aExprResult);
  155. if ((isSnapshot(aResultType) || isIterator(aResultType) ||
  156. isNode(aResultType)) &&
  157. aExprResult->getResultType() != txAExprResult::NODESET) {
  158. // The DOM spec doesn't really say what should happen when reusing an
  159. // XPathResult and an error is thrown. Let's not touch the XPathResult
  160. // in that case.
  161. return NS_ERROR_DOM_TYPE_ERR;
  162. }
  163. mResultType = aResultType;
  164. mContextNode = do_GetWeakReference(aContextNode);
  165. if (mDocument) {
  166. mDocument->RemoveMutationObserver(this);
  167. mDocument = nullptr;
  168. }
  169. mResultNodes.Clear();
  170. // XXX This will keep the recycler alive, should we clear it?
  171. mResult = aExprResult;
  172. switch (mResultType) {
  173. case BOOLEAN_TYPE:
  174. {
  175. mBooleanResult = mResult->booleanValue();
  176. break;
  177. }
  178. case NUMBER_TYPE:
  179. {
  180. mNumberResult = mResult->numberValue();
  181. break;
  182. }
  183. case STRING_TYPE:
  184. {
  185. mResult->stringValue(mStringResult);
  186. break;
  187. }
  188. default:
  189. {
  190. MOZ_ASSERT(isNode() || isIterator() || isSnapshot());
  191. }
  192. }
  193. if (aExprResult->getResultType() == txAExprResult::NODESET) {
  194. txNodeSet *nodeSet = static_cast<txNodeSet*>(aExprResult);
  195. int32_t i, count = nodeSet->size();
  196. for (i = 0; i < count; ++i) {
  197. nsINode* node = txXPathNativeNode::getNode(nodeSet->get(i));
  198. mResultNodes.AppendObject(node);
  199. }
  200. if (count > 0) {
  201. mResult = nullptr;
  202. }
  203. }
  204. if (!isIterator()) {
  205. return NS_OK;
  206. }
  207. mInvalidIteratorState = false;
  208. if (mResultNodes.Count() > 0) {
  209. // If we support the document() function in DOM-XPath we need to
  210. // observe all documents that we have resultnodes in.
  211. mDocument = mResultNodes[0]->OwnerDoc();
  212. NS_ASSERTION(mDocument, "We need a document!");
  213. if (mDocument) {
  214. mDocument->AddMutationObserver(this);
  215. }
  216. }
  217. return NS_OK;
  218. }
  219. void
  220. XPathResult::Invalidate(const nsIContent* aChangeRoot)
  221. {
  222. nsCOMPtr<nsINode> contextNode = do_QueryReferent(mContextNode);
  223. if (contextNode && aChangeRoot && aChangeRoot->GetBindingParent()) {
  224. // If context node is in anonymous content, changes to
  225. // non-anonymous content need to invalidate the XPathResult. If
  226. // the changes are happening in a different anonymous trees, no
  227. // invalidation should happen.
  228. nsIContent* ctxBindingParent = nullptr;
  229. if (contextNode->IsNodeOfType(nsINode::eCONTENT)) {
  230. ctxBindingParent =
  231. static_cast<nsIContent*>(contextNode.get())
  232. ->GetBindingParent();
  233. } else if (contextNode->IsNodeOfType(nsINode::eATTRIBUTE)) {
  234. Element* parent =
  235. static_cast<Attr*>(contextNode.get())->GetElement();
  236. if (parent) {
  237. ctxBindingParent = parent->GetBindingParent();
  238. }
  239. }
  240. if (ctxBindingParent != aChangeRoot->GetBindingParent()) {
  241. return;
  242. }
  243. }
  244. mInvalidIteratorState = true;
  245. // Make sure nulling out mDocument is the last thing we do.
  246. if (mDocument) {
  247. mDocument->RemoveMutationObserver(this);
  248. mDocument = nullptr;
  249. }
  250. }
  251. nsresult
  252. XPathResult::GetExprResult(txAExprResult** aExprResult)
  253. {
  254. if (isIterator() && mInvalidIteratorState) {
  255. return NS_ERROR_DOM_INVALID_STATE_ERR;
  256. }
  257. if (mResult) {
  258. NS_ADDREF(*aExprResult = mResult);
  259. return NS_OK;
  260. }
  261. if (mResultNodes.Count() == 0) {
  262. return NS_ERROR_DOM_INVALID_STATE_ERR;
  263. }
  264. RefPtr<txNodeSet> nodeSet = new txNodeSet(nullptr);
  265. if (!nodeSet) {
  266. return NS_ERROR_OUT_OF_MEMORY;
  267. }
  268. uint32_t i, count = mResultNodes.Count();
  269. for (i = 0; i < count; ++i) {
  270. nsAutoPtr<txXPathNode> node(txXPathNativeNode::createXPathNode(mResultNodes[i]));
  271. if (!node) {
  272. return NS_ERROR_OUT_OF_MEMORY;
  273. }
  274. nodeSet->append(*node);
  275. }
  276. NS_ADDREF(*aExprResult = nodeSet);
  277. return NS_OK;
  278. }
  279. nsresult
  280. XPathResult::Clone(nsIXPathResult **aResult)
  281. {
  282. *aResult = nullptr;
  283. if (isIterator() && mInvalidIteratorState) {
  284. return NS_ERROR_DOM_INVALID_STATE_ERR;
  285. }
  286. NS_ADDREF(*aResult = new XPathResult(*this));
  287. return NS_OK;
  288. }
  289. } // namespace dom
  290. } // namespace mozilla