HTMLOptionsCollection.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 "mozilla/dom/HTMLOptionsCollection.h"
  6. #include "HTMLOptGroupElement.h"
  7. #include "mozAutoDocUpdate.h"
  8. #include "mozilla/dom/BindingUtils.h"
  9. #include "mozilla/dom/Element.h"
  10. #include "mozilla/dom/HTMLFormSubmission.h"
  11. #include "mozilla/dom/HTMLOptionElement.h"
  12. #include "mozilla/dom/HTMLOptionsCollectionBinding.h"
  13. #include "mozilla/dom/HTMLSelectElement.h"
  14. #include "nsContentCreatorFunctions.h"
  15. #include "nsError.h"
  16. #include "nsGkAtoms.h"
  17. #include "nsIComboboxControlFrame.h"
  18. #include "nsIDocument.h"
  19. #include "nsIDOMHTMLOptGroupElement.h"
  20. #include "nsIFormControlFrame.h"
  21. #include "nsIForm.h"
  22. #include "nsIFormProcessor.h"
  23. #include "nsIListControlFrame.h"
  24. #include "nsLayoutUtils.h"
  25. #include "nsMappedAttributes.h"
  26. #include "nsRuleData.h"
  27. #include "nsServiceManagerUtils.h"
  28. #include "nsStyleConsts.h"
  29. #include "jsfriendapi.h"
  30. namespace mozilla {
  31. namespace dom {
  32. HTMLOptionsCollection::HTMLOptionsCollection(HTMLSelectElement* aSelect)
  33. : mSelect(aSelect)
  34. {}
  35. nsresult
  36. HTMLOptionsCollection::GetOptionIndex(Element* aOption,
  37. int32_t aStartIndex,
  38. bool aForward,
  39. int32_t* aIndex)
  40. {
  41. // NOTE: aIndex shouldn't be set if the returned value isn't NS_OK.
  42. int32_t index;
  43. // Make the common case fast
  44. if (aStartIndex == 0 && aForward) {
  45. index = mElements.IndexOf(aOption);
  46. if (index == -1) {
  47. return NS_ERROR_FAILURE;
  48. }
  49. *aIndex = index;
  50. return NS_OK;
  51. }
  52. int32_t high = mElements.Length();
  53. int32_t step = aForward ? 1 : -1;
  54. for (index = aStartIndex; index < high && index > -1; index += step) {
  55. if (mElements[index] == aOption) {
  56. *aIndex = index;
  57. return NS_OK;
  58. }
  59. }
  60. return NS_ERROR_FAILURE;
  61. }
  62. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(HTMLOptionsCollection,
  63. mElements,
  64. mSelect)
  65. // nsISupports
  66. // QueryInterface implementation for HTMLOptionsCollection
  67. NS_INTERFACE_TABLE_HEAD(HTMLOptionsCollection)
  68. NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
  69. NS_INTERFACE_TABLE(HTMLOptionsCollection,
  70. nsIHTMLCollection,
  71. nsIDOMHTMLOptionsCollection,
  72. nsIDOMHTMLCollection)
  73. NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(HTMLOptionsCollection)
  74. NS_INTERFACE_MAP_END
  75. NS_IMPL_CYCLE_COLLECTING_ADDREF(HTMLOptionsCollection)
  76. NS_IMPL_CYCLE_COLLECTING_RELEASE(HTMLOptionsCollection)
  77. JSObject*
  78. HTMLOptionsCollection::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  79. {
  80. return HTMLOptionsCollectionBinding::Wrap(aCx, this, aGivenProto);
  81. }
  82. NS_IMETHODIMP
  83. HTMLOptionsCollection::GetLength(uint32_t* aLength)
  84. {
  85. *aLength = mElements.Length();
  86. return NS_OK;
  87. }
  88. NS_IMETHODIMP
  89. HTMLOptionsCollection::SetLength(uint32_t aLength)
  90. {
  91. return mSelect->SetLength(aLength);
  92. }
  93. NS_IMETHODIMP
  94. HTMLOptionsCollection::SetOption(uint32_t aIndex,
  95. nsIDOMHTMLOptionElement* aOption)
  96. {
  97. // if the new option is null, just remove this option. Note that it's safe
  98. // to pass a too-large aIndex in here.
  99. if (!aOption) {
  100. mSelect->Remove(aIndex);
  101. // We're done.
  102. return NS_OK;
  103. }
  104. nsresult rv = NS_OK;
  105. uint32_t index = uint32_t(aIndex);
  106. // Now we're going to be setting an option in our collection
  107. if (index > mElements.Length()) {
  108. // Fill our array with blank options up to (but not including, since we're
  109. // about to change it) aIndex, for compat with other browsers.
  110. rv = SetLength(index);
  111. NS_ENSURE_SUCCESS(rv, rv);
  112. }
  113. NS_ASSERTION(index <= mElements.Length(), "SetLength lied");
  114. nsCOMPtr<nsIDOMNode> ret;
  115. if (index == mElements.Length()) {
  116. nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aOption);
  117. rv = mSelect->AppendChild(node, getter_AddRefs(ret));
  118. } else {
  119. // Find the option they're talking about and replace it
  120. // hold a strong reference to follow COM rules.
  121. RefPtr<HTMLOptionElement> refChild = ItemAsOption(index);
  122. NS_ENSURE_TRUE(refChild, NS_ERROR_UNEXPECTED);
  123. nsCOMPtr<nsINode> parent = refChild->GetParent();
  124. if (parent) {
  125. nsCOMPtr<nsINode> node = do_QueryInterface(aOption);
  126. ErrorResult res;
  127. parent->ReplaceChild(*node, *refChild, res);
  128. rv = res.StealNSResult();
  129. }
  130. }
  131. return rv;
  132. }
  133. int32_t
  134. HTMLOptionsCollection::GetSelectedIndex(ErrorResult& aError)
  135. {
  136. int32_t selectedIndex;
  137. aError = mSelect->GetSelectedIndex(&selectedIndex);
  138. return selectedIndex;
  139. }
  140. NS_IMETHODIMP
  141. HTMLOptionsCollection::GetSelectedIndex(int32_t* aSelectedIndex)
  142. {
  143. ErrorResult rv;
  144. *aSelectedIndex = GetSelectedIndex(rv);
  145. return rv.StealNSResult();
  146. }
  147. void
  148. HTMLOptionsCollection::SetSelectedIndex(int32_t aSelectedIndex,
  149. ErrorResult& aError)
  150. {
  151. aError = mSelect->SetSelectedIndex(aSelectedIndex);
  152. }
  153. NS_IMETHODIMP
  154. HTMLOptionsCollection::SetSelectedIndex(int32_t aSelectedIndex)
  155. {
  156. ErrorResult rv;
  157. SetSelectedIndex(aSelectedIndex, rv);
  158. return rv.StealNSResult();
  159. }
  160. NS_IMETHODIMP
  161. HTMLOptionsCollection::Item(uint32_t aIndex, nsIDOMNode** aReturn)
  162. {
  163. nsISupports* item = GetElementAt(aIndex);
  164. if (!item) {
  165. *aReturn = nullptr;
  166. return NS_OK;
  167. }
  168. return CallQueryInterface(item, aReturn);
  169. }
  170. Element*
  171. HTMLOptionsCollection::GetElementAt(uint32_t aIndex)
  172. {
  173. return ItemAsOption(aIndex);
  174. }
  175. HTMLOptionElement*
  176. HTMLOptionsCollection::NamedGetter(const nsAString& aName, bool& aFound)
  177. {
  178. uint32_t count = mElements.Length();
  179. for (uint32_t i = 0; i < count; i++) {
  180. HTMLOptionElement* content = mElements.ElementAt(i);
  181. if (content &&
  182. (content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name, aName,
  183. eCaseMatters) ||
  184. content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::id, aName,
  185. eCaseMatters))) {
  186. aFound = true;
  187. return content;
  188. }
  189. }
  190. aFound = false;
  191. return nullptr;
  192. }
  193. nsINode*
  194. HTMLOptionsCollection::GetParentObject()
  195. {
  196. return mSelect;
  197. }
  198. DocGroup*
  199. HTMLOptionsCollection::GetDocGroup() const
  200. {
  201. return mSelect ? mSelect->GetDocGroup() : nullptr;
  202. }
  203. NS_IMETHODIMP
  204. HTMLOptionsCollection::NamedItem(const nsAString& aName,
  205. nsIDOMNode** aReturn)
  206. {
  207. NS_IF_ADDREF(*aReturn = GetNamedItem(aName));
  208. return NS_OK;
  209. }
  210. void
  211. HTMLOptionsCollection::GetSupportedNames(nsTArray<nsString>& aNames)
  212. {
  213. AutoTArray<nsIAtom*, 8> atoms;
  214. for (uint32_t i = 0; i < mElements.Length(); ++i) {
  215. HTMLOptionElement* content = mElements.ElementAt(i);
  216. if (content) {
  217. // Note: HasName means the names is exposed on the document,
  218. // which is false for options, so we don't check it here.
  219. const nsAttrValue* val = content->GetParsedAttr(nsGkAtoms::name);
  220. if (val && val->Type() == nsAttrValue::eAtom) {
  221. nsIAtom* name = val->GetAtomValue();
  222. if (!atoms.Contains(name)) {
  223. atoms.AppendElement(name);
  224. }
  225. }
  226. if (content->HasID()) {
  227. nsIAtom* id = content->GetID();
  228. if (!atoms.Contains(id)) {
  229. atoms.AppendElement(id);
  230. }
  231. }
  232. }
  233. }
  234. uint32_t atomsLen = atoms.Length();
  235. nsString* names = aNames.AppendElements(atomsLen);
  236. for (uint32_t i = 0; i < atomsLen; ++i) {
  237. atoms[i]->ToString(names[i]);
  238. }
  239. }
  240. NS_IMETHODIMP
  241. HTMLOptionsCollection::GetSelect(nsIDOMHTMLSelectElement** aReturn)
  242. {
  243. NS_IF_ADDREF(*aReturn = mSelect);
  244. return NS_OK;
  245. }
  246. NS_IMETHODIMP
  247. HTMLOptionsCollection::Add(nsIDOMHTMLOptionElement* aOption,
  248. nsIVariant* aBefore)
  249. {
  250. if (!aOption) {
  251. return NS_ERROR_INVALID_ARG;
  252. }
  253. if (!mSelect) {
  254. return NS_ERROR_NOT_INITIALIZED;
  255. }
  256. nsCOMPtr<nsIDOMHTMLElement> elem = do_QueryInterface(aOption);
  257. return mSelect->Add(elem, aBefore);
  258. }
  259. void
  260. HTMLOptionsCollection::Add(const HTMLOptionOrOptGroupElement& aElement,
  261. const Nullable<HTMLElementOrLong>& aBefore,
  262. ErrorResult& aError)
  263. {
  264. mSelect->Add(aElement, aBefore, aError);
  265. }
  266. void
  267. HTMLOptionsCollection::Remove(int32_t aIndex, ErrorResult& aError)
  268. {
  269. uint32_t len = 0;
  270. mSelect->GetLength(&len);
  271. if (aIndex < 0 || (uint32_t)aIndex >= len)
  272. aIndex = 0;
  273. aError = mSelect->Remove(aIndex);
  274. }
  275. NS_IMETHODIMP
  276. HTMLOptionsCollection::Remove(int32_t aIndex)
  277. {
  278. ErrorResult rv;
  279. Remove(aIndex, rv);
  280. return rv.StealNSResult();
  281. }
  282. } // namespace dom
  283. } // namespace mozilla