HTMLAllCollection.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/HTMLAllCollection.h"
  6. #include "mozilla/dom/HTMLAllCollectionBinding.h"
  7. #include "mozilla/dom/Nullable.h"
  8. #include "mozilla/dom/Element.h"
  9. #include "nsHTMLDocument.h"
  10. namespace mozilla {
  11. namespace dom {
  12. HTMLAllCollection::HTMLAllCollection(nsHTMLDocument* aDocument)
  13. : mDocument(aDocument)
  14. {
  15. MOZ_ASSERT(mDocument);
  16. }
  17. HTMLAllCollection::~HTMLAllCollection()
  18. {
  19. }
  20. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(HTMLAllCollection,
  21. mDocument,
  22. mCollection,
  23. mNamedMap)
  24. NS_IMPL_CYCLE_COLLECTING_ADDREF(HTMLAllCollection)
  25. NS_IMPL_CYCLE_COLLECTING_RELEASE(HTMLAllCollection)
  26. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(HTMLAllCollection)
  27. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  28. NS_INTERFACE_MAP_ENTRY(nsISupports)
  29. NS_INTERFACE_MAP_END
  30. nsINode*
  31. HTMLAllCollection::GetParentObject() const
  32. {
  33. return mDocument;
  34. }
  35. uint32_t
  36. HTMLAllCollection::Length()
  37. {
  38. return Collection()->Length(true);
  39. }
  40. nsIContent*
  41. HTMLAllCollection::Item(uint32_t aIndex)
  42. {
  43. return Collection()->Item(aIndex);
  44. }
  45. nsContentList*
  46. HTMLAllCollection::Collection()
  47. {
  48. if (!mCollection) {
  49. nsIDocument* document = mDocument;
  50. mCollection = document->GetElementsByTagName(NS_LITERAL_STRING("*"));
  51. MOZ_ASSERT(mCollection);
  52. }
  53. return mCollection;
  54. }
  55. static bool
  56. IsAllNamedElement(nsIContent* aContent)
  57. {
  58. return aContent->IsAnyOfHTMLElements(nsGkAtoms::a,
  59. nsGkAtoms::applet,
  60. nsGkAtoms::button,
  61. nsGkAtoms::embed,
  62. nsGkAtoms::form,
  63. nsGkAtoms::iframe,
  64. nsGkAtoms::img,
  65. nsGkAtoms::input,
  66. nsGkAtoms::map,
  67. nsGkAtoms::meta,
  68. nsGkAtoms::object,
  69. nsGkAtoms::select,
  70. nsGkAtoms::textarea,
  71. nsGkAtoms::frame,
  72. nsGkAtoms::frameset);
  73. }
  74. static bool
  75. DocAllResultMatch(Element* aElement, int32_t aNamespaceID, nsIAtom* aAtom,
  76. void* aData)
  77. {
  78. if (aElement->GetID() == aAtom) {
  79. return true;
  80. }
  81. nsGenericHTMLElement* elm = nsGenericHTMLElement::FromContent(aElement);
  82. if (!elm) {
  83. return false;
  84. }
  85. if (!IsAllNamedElement(elm)) {
  86. return false;
  87. }
  88. const nsAttrValue* val = elm->GetParsedAttr(nsGkAtoms::name);
  89. return val && val->Type() == nsAttrValue::eAtom &&
  90. val->GetAtomValue() == aAtom;
  91. }
  92. nsContentList*
  93. HTMLAllCollection::GetDocumentAllList(const nsAString& aID)
  94. {
  95. if (nsContentList* docAllList = mNamedMap.GetWeak(aID)) {
  96. return docAllList;
  97. }
  98. nsCOMPtr<nsIAtom> id = NS_Atomize(aID);
  99. RefPtr<nsContentList> docAllList =
  100. new nsContentList(mDocument, DocAllResultMatch, nullptr, nullptr, true, id);
  101. mNamedMap.Put(aID, docAllList);
  102. return docAllList;
  103. }
  104. void
  105. HTMLAllCollection::NamedGetter(const nsAString& aID,
  106. bool& aFound,
  107. Nullable<OwningNodeOrHTMLCollection>& aResult)
  108. {
  109. if (aID.IsEmpty()) {
  110. aFound = false;
  111. aResult.SetNull();
  112. return;
  113. }
  114. nsContentList* docAllList = GetDocumentAllList(aID);
  115. if (!docAllList) {
  116. aFound = false;
  117. aResult.SetNull();
  118. return;
  119. }
  120. // Check if there are more than 1 entries. Do this by getting the second one
  121. // rather than the length since getting the length always requires walking
  122. // the entire document.
  123. if (docAllList->Item(1, true)) {
  124. aFound = true;
  125. aResult.SetValue().SetAsHTMLCollection() = docAllList;
  126. return;
  127. }
  128. // There's only 0 or 1 items. Return the first one or null.
  129. if (nsIContent* node = docAllList->Item(0, true)) {
  130. aFound = true;
  131. aResult.SetValue().SetAsNode() = node;
  132. return;
  133. }
  134. aFound = false;
  135. aResult.SetNull();
  136. }
  137. void
  138. HTMLAllCollection::GetSupportedNames(nsTArray<nsString>& aNames)
  139. {
  140. // XXXbz this is very similar to nsContentList::GetSupportedNames,
  141. // but has to check IsAllNamedElement for the name case.
  142. AutoTArray<nsIAtom*, 8> atoms;
  143. for (uint32_t i = 0; i < Length(); ++i) {
  144. nsIContent *content = Item(i);
  145. if (content->HasID()) {
  146. nsIAtom* id = content->GetID();
  147. MOZ_ASSERT(id != nsGkAtoms::_empty,
  148. "Empty ids don't get atomized");
  149. if (!atoms.Contains(id)) {
  150. atoms.AppendElement(id);
  151. }
  152. }
  153. nsGenericHTMLElement* el = nsGenericHTMLElement::FromContent(content);
  154. if (el) {
  155. // Note: nsINode::HasName means the name is exposed on the document,
  156. // which is false for options, so we don't check it here.
  157. const nsAttrValue* val = el->GetParsedAttr(nsGkAtoms::name);
  158. if (val && val->Type() == nsAttrValue::eAtom &&
  159. IsAllNamedElement(content)) {
  160. nsIAtom* name = val->GetAtomValue();
  161. MOZ_ASSERT(name != nsGkAtoms::_empty,
  162. "Empty names don't get atomized");
  163. if (!atoms.Contains(name)) {
  164. atoms.AppendElement(name);
  165. }
  166. }
  167. }
  168. }
  169. uint32_t atomsLen = atoms.Length();
  170. nsString* names = aNames.AppendElements(atomsLen);
  171. for (uint32_t i = 0; i < atomsLen; ++i) {
  172. atoms[i]->ToString(names[i]);
  173. }
  174. }
  175. JSObject*
  176. HTMLAllCollection::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  177. {
  178. return HTMLAllCollectionBinding::Wrap(aCx, this, aGivenProto);
  179. }
  180. } // namespace dom
  181. } // namespace mozilla