DOMSVGStringList.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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 "DOMSVGStringList.h"
  6. #include "mozilla/dom/SVGStringListBinding.h"
  7. #include "mozilla/dom/SVGTests.h"
  8. #include "nsError.h"
  9. #include "nsCOMPtr.h"
  10. #include "nsSVGAttrTearoffTable.h"
  11. #include "nsQueryObject.h"
  12. #include <algorithm>
  13. // See the architecture comment in this file's header.
  14. namespace mozilla {
  15. using namespace dom;
  16. static inline
  17. nsSVGAttrTearoffTable<SVGStringList, DOMSVGStringList>&
  18. SVGStringListTearoffTable()
  19. {
  20. static nsSVGAttrTearoffTable<SVGStringList, DOMSVGStringList>
  21. sSVGStringListTearoffTable;
  22. return sSVGStringListTearoffTable;
  23. }
  24. NS_SVG_VAL_IMPL_CYCLE_COLLECTION_WRAPPERCACHED(DOMSVGStringList, mElement)
  25. NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGStringList)
  26. NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGStringList)
  27. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGStringList)
  28. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  29. NS_INTERFACE_MAP_ENTRY(nsISupports)
  30. NS_INTERFACE_MAP_END
  31. //----------------------------------------------------------------------
  32. // Helper class: AutoChangeStringListNotifier
  33. // Stack-based helper class to pair calls to WillChangeStringListList and
  34. // DidChangeStringListList.
  35. class MOZ_RAII AutoChangeStringListNotifier
  36. {
  37. public:
  38. explicit AutoChangeStringListNotifier(DOMSVGStringList* aStringList MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
  39. : mStringList(aStringList)
  40. {
  41. MOZ_GUARD_OBJECT_NOTIFIER_INIT;
  42. MOZ_ASSERT(mStringList, "Expecting non-null stringList");
  43. mEmptyOrOldValue =
  44. mStringList->mElement->WillChangeStringList(mStringList->mIsConditionalProcessingAttribute,
  45. mStringList->mAttrEnum);
  46. }
  47. ~AutoChangeStringListNotifier()
  48. {
  49. mStringList->mElement->DidChangeStringList(mStringList->mIsConditionalProcessingAttribute,
  50. mStringList->mAttrEnum, mEmptyOrOldValue);
  51. }
  52. private:
  53. DOMSVGStringList* const mStringList;
  54. nsAttrValue mEmptyOrOldValue;
  55. MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
  56. };
  57. /* static */ already_AddRefed<DOMSVGStringList>
  58. DOMSVGStringList::GetDOMWrapper(SVGStringList *aList,
  59. nsSVGElement *aElement,
  60. bool aIsConditionalProcessingAttribute,
  61. uint8_t aAttrEnum)
  62. {
  63. RefPtr<DOMSVGStringList> wrapper =
  64. SVGStringListTearoffTable().GetTearoff(aList);
  65. if (!wrapper) {
  66. wrapper = new DOMSVGStringList(aElement,
  67. aIsConditionalProcessingAttribute,
  68. aAttrEnum);
  69. SVGStringListTearoffTable().AddTearoff(aList, wrapper);
  70. }
  71. return wrapper.forget();
  72. }
  73. DOMSVGStringList::~DOMSVGStringList()
  74. {
  75. // Script no longer has any references to us.
  76. SVGStringListTearoffTable().RemoveTearoff(&InternalList());
  77. }
  78. /* virtual */ JSObject*
  79. DOMSVGStringList::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  80. {
  81. return SVGStringListBinding::Wrap(aCx, this, aGivenProto);
  82. }
  83. // ----------------------------------------------------------------------------
  84. // SVGStringList implementation:
  85. uint32_t
  86. DOMSVGStringList::NumberOfItems() const
  87. {
  88. return InternalList().Length();
  89. }
  90. uint32_t
  91. DOMSVGStringList::Length() const
  92. {
  93. return NumberOfItems();
  94. }
  95. void
  96. DOMSVGStringList::Clear()
  97. {
  98. if (InternalList().IsExplicitlySet()) {
  99. AutoChangeStringListNotifier notifier(this);
  100. InternalList().Clear();
  101. }
  102. }
  103. void
  104. DOMSVGStringList::Initialize(const nsAString& aNewItem, nsAString& aRetval,
  105. ErrorResult& aRv)
  106. {
  107. if (InternalList().IsExplicitlySet()) {
  108. InternalList().Clear();
  109. }
  110. InsertItemBefore(aNewItem, 0, aRetval, aRv);
  111. }
  112. void
  113. DOMSVGStringList::GetItem(uint32_t aIndex, nsAString& aRetval, ErrorResult& aRv)
  114. {
  115. bool found;
  116. IndexedGetter(aIndex, found, aRetval);
  117. if (!found) {
  118. aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
  119. }
  120. }
  121. void
  122. DOMSVGStringList::IndexedGetter(uint32_t aIndex, bool& aFound,
  123. nsAString& aRetval)
  124. {
  125. aFound = aIndex < InternalList().Length();
  126. if (aFound) {
  127. aRetval = InternalList()[aIndex];
  128. }
  129. }
  130. void
  131. DOMSVGStringList::InsertItemBefore(const nsAString& aNewItem, uint32_t aIndex,
  132. nsAString& aRetval, ErrorResult& aRv)
  133. {
  134. if (aNewItem.IsEmpty()) {
  135. aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
  136. return;
  137. }
  138. aIndex = std::min(aIndex, InternalList().Length());
  139. // Ensure we have enough memory so we can avoid complex error handling below:
  140. if (!InternalList().SetCapacity(InternalList().Length() + 1)) {
  141. aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
  142. return;
  143. }
  144. AutoChangeStringListNotifier notifier(this);
  145. InternalList().InsertItem(aIndex, aNewItem);
  146. aRetval = aNewItem;
  147. }
  148. void
  149. DOMSVGStringList::ReplaceItem(const nsAString& aNewItem, uint32_t aIndex,
  150. nsAString& aRetval, ErrorResult& aRv)
  151. {
  152. if (aNewItem.IsEmpty()) {
  153. aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
  154. return;
  155. }
  156. if (aIndex >= InternalList().Length()) {
  157. aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
  158. return;
  159. }
  160. aRetval = InternalList()[aIndex];
  161. AutoChangeStringListNotifier notifier(this);
  162. InternalList().ReplaceItem(aIndex, aNewItem);
  163. }
  164. void
  165. DOMSVGStringList::RemoveItem(uint32_t aIndex, nsAString& aRetval,
  166. ErrorResult& aRv)
  167. {
  168. if (aIndex >= InternalList().Length()) {
  169. aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
  170. return;
  171. }
  172. AutoChangeStringListNotifier notifier(this);
  173. InternalList().RemoveItem(aIndex);
  174. }
  175. void
  176. DOMSVGStringList::AppendItem(const nsAString& aNewItem, nsAString& aRetval,
  177. ErrorResult& aRv)
  178. {
  179. InsertItemBefore(aNewItem, InternalList().Length(), aRetval, aRv);
  180. }
  181. SVGStringList &
  182. DOMSVGStringList::InternalList() const
  183. {
  184. if (mIsConditionalProcessingAttribute) {
  185. nsCOMPtr<dom::SVGTests> tests = do_QueryObject(mElement.get());
  186. return tests->mStringListAttributes[mAttrEnum];
  187. }
  188. return mElement->GetStringListInfo().mStringLists[mAttrEnum];
  189. }
  190. } // namespace mozilla