NamedNodeMap.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  4. * Copyright (c) 2022, Alexander Narsudinov <a.narsudinov@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibWeb/Bindings/NamedNodeMapPrototype.h>
  9. #include <LibWeb/DOM/Attr.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/NamedNodeMap.h>
  12. #include <LibWeb/Infra/Strings.h>
  13. #include <LibWeb/Namespace.h>
  14. namespace Web::DOM {
  15. GC_DEFINE_ALLOCATOR(NamedNodeMap);
  16. GC::Ref<NamedNodeMap> NamedNodeMap::create(Element& element)
  17. {
  18. auto& realm = element.realm();
  19. return realm.create<NamedNodeMap>(element);
  20. }
  21. NamedNodeMap::NamedNodeMap(Element& element)
  22. : Bindings::PlatformObject(element.realm())
  23. , m_element(element)
  24. {
  25. m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
  26. .supports_indexed_properties = true,
  27. .supports_named_properties = true,
  28. .has_legacy_unenumerable_named_properties_interface_extended_attribute = true,
  29. };
  30. }
  31. void NamedNodeMap::initialize(JS::Realm& realm)
  32. {
  33. Base::initialize(realm);
  34. WEB_SET_PROTOTYPE_FOR_INTERFACE(NamedNodeMap);
  35. }
  36. void NamedNodeMap::visit_edges(Cell::Visitor& visitor)
  37. {
  38. Base::visit_edges(visitor);
  39. visitor.visit(m_element);
  40. visitor.visit(m_attributes);
  41. }
  42. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names%E2%91%A0
  43. Vector<FlyString> NamedNodeMap::supported_property_names() const
  44. {
  45. // 1. Let names be the qualified names of the attributes in this NamedNodeMap object’s attribute list, with duplicates omitted, in order.
  46. Vector<FlyString> names;
  47. names.ensure_capacity(m_attributes.size());
  48. for (auto const& attribute : m_attributes) {
  49. auto const attribute_name = attribute->name();
  50. if (!names.contains_slow(attribute_name))
  51. names.append(attribute_name.to_string());
  52. }
  53. // 2. If this NamedNodeMap object’s element is in the HTML namespace and its node document is an HTML document, then for each name in names:
  54. // FIXME: Handle the second condition, assume it is an HTML document for now.
  55. if (associated_element().namespace_uri() == Namespace::HTML) {
  56. // 1. Let lowercaseName be name, in ASCII lowercase.
  57. // 2. If lowercaseName is not equal to name, remove name from names.
  58. names.remove_all_matching([](auto const& name) { return name != name.to_ascii_lowercase(); });
  59. }
  60. // 3. Return names.
  61. return names;
  62. }
  63. // https://dom.spec.whatwg.org/#dom-namednodemap-item
  64. Attr const* NamedNodeMap::item(u32 index) const
  65. {
  66. // 1. If index is equal to or greater than this’s attribute list’s size, then return null.
  67. if (index >= m_attributes.size())
  68. return nullptr;
  69. // 2. Otherwise, return this’s attribute list[index].
  70. return m_attributes[index].ptr();
  71. }
  72. // https://dom.spec.whatwg.org/#dom-namednodemap-getnameditem
  73. Attr const* NamedNodeMap::get_named_item(FlyString const& qualified_name) const
  74. {
  75. return get_attribute(qualified_name);
  76. }
  77. // https://dom.spec.whatwg.org/#dom-namednodemap-getnameditemns
  78. Attr const* NamedNodeMap::get_named_item_ns(Optional<FlyString> const& namespace_, FlyString const& local_name) const
  79. {
  80. return get_attribute_ns(namespace_, local_name);
  81. }
  82. // https://dom.spec.whatwg.org/#dom-namednodemap-setnameditem
  83. WebIDL::ExceptionOr<GC::Ptr<Attr>> NamedNodeMap::set_named_item(Attr& attribute)
  84. {
  85. return set_attribute(attribute);
  86. }
  87. // https://dom.spec.whatwg.org/#dom-namednodemap-setnameditemns
  88. WebIDL::ExceptionOr<GC::Ptr<Attr>> NamedNodeMap::set_named_item_ns(Attr& attribute)
  89. {
  90. return set_attribute(attribute);
  91. }
  92. // https://dom.spec.whatwg.org/#dom-namednodemap-removenameditem
  93. WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item(FlyString const& qualified_name)
  94. {
  95. // 1. Let attr be the result of removing an attribute given qualifiedName and element.
  96. auto const* attribute = remove_attribute(qualified_name);
  97. // 2. If attr is null, then throw a "NotFoundError" DOMException.
  98. if (!attribute)
  99. return WebIDL::NotFoundError::create(realm(), MUST(String::formatted("Attribute with name '{}' not found", qualified_name)));
  100. // 3. Return attr.
  101. return attribute;
  102. }
  103. // https://dom.spec.whatwg.org/#dom-namednodemap-removenameditemns
  104. WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item_ns(Optional<FlyString> const& namespace_, FlyString const& local_name)
  105. {
  106. // 1. Let attr be the result of removing an attribute given namespace, localName, and element.
  107. auto const* attribute = remove_attribute_ns(namespace_, local_name);
  108. // 2. If attr is null, then throw a "NotFoundError" DOMException.
  109. if (!attribute)
  110. return WebIDL::NotFoundError::create(realm(), MUST(String::formatted("Attribute with namespace '{}' and local name '{}' not found", namespace_, local_name)));
  111. // 3. Return attr.
  112. return attribute;
  113. }
  114. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
  115. Attr* NamedNodeMap::get_attribute(FlyString const& qualified_name, size_t* item_index)
  116. {
  117. return const_cast<Attr*>(const_cast<NamedNodeMap const*>(this)->get_attribute(qualified_name, item_index));
  118. }
  119. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
  120. Attr const* NamedNodeMap::get_attribute(FlyString const& qualified_name, size_t* item_index) const
  121. {
  122. if (item_index)
  123. *item_index = 0;
  124. // 1. If element is in the HTML namespace and its node document is an HTML document, then set qualifiedName to qualifiedName in ASCII lowercase.
  125. bool compare_as_lowercase = associated_element().namespace_uri() == Namespace::HTML && associated_element().document().is_html_document();
  126. // 2. Return the first attribute in element’s attribute list whose qualified name is qualifiedName; otherwise null.
  127. for (auto const& attribute : m_attributes) {
  128. if (compare_as_lowercase) {
  129. if (attribute->name().equals_ignoring_ascii_case(qualified_name))
  130. return attribute;
  131. } else {
  132. if (attribute->name() == qualified_name)
  133. return attribute;
  134. }
  135. if (item_index)
  136. ++(*item_index);
  137. }
  138. return nullptr;
  139. }
  140. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
  141. Attr* NamedNodeMap::get_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name, size_t* item_index)
  142. {
  143. return const_cast<Attr*>(const_cast<NamedNodeMap const*>(this)->get_attribute_ns(namespace_, local_name, item_index));
  144. }
  145. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
  146. Attr const* NamedNodeMap::get_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name, size_t* item_index) const
  147. {
  148. if (item_index)
  149. *item_index = 0;
  150. // 1. If namespace is the empty string, then set it to null.
  151. Optional<FlyString> normalized_namespace;
  152. if (namespace_ != String {})
  153. normalized_namespace = namespace_;
  154. // 2. Return the attribute in element’s attribute list whose namespace is namespace and local name is localName, if any; otherwise null.
  155. for (auto const& attribute : m_attributes) {
  156. if (attribute->namespace_uri() == normalized_namespace && attribute->local_name() == local_name)
  157. return attribute.ptr();
  158. if (item_index)
  159. ++(*item_index);
  160. }
  161. return nullptr;
  162. }
  163. // https://dom.spec.whatwg.org/#concept-element-attributes-set
  164. WebIDL::ExceptionOr<GC::Ptr<Attr>> NamedNodeMap::set_attribute(Attr& attribute)
  165. {
  166. // 1. If attr’s element is neither null nor element, throw an "InUseAttributeError" DOMException.
  167. if ((attribute.owner_element() != nullptr) && (attribute.owner_element() != &associated_element()))
  168. return WebIDL::InUseAttributeError::create(realm(), "Attribute must not already be in use"_string);
  169. // 2. Let oldAttr be the result of getting an attribute given attr’s namespace, attr’s local name, and element.
  170. size_t old_attribute_index = 0;
  171. auto* old_attribute = get_attribute_ns(attribute.namespace_uri(), attribute.local_name(), &old_attribute_index);
  172. // 3. If oldAttr is attr, return attr.
  173. if (old_attribute == &attribute)
  174. return &attribute;
  175. // 4. If oldAttr is non-null, then replace oldAttr with attr.
  176. if (old_attribute) {
  177. replace_attribute(*old_attribute, attribute, old_attribute_index);
  178. }
  179. // 5. Otherwise, append attr to element.
  180. else {
  181. append_attribute(attribute);
  182. }
  183. // 6. Return oldAttr.
  184. return old_attribute;
  185. }
  186. // https://dom.spec.whatwg.org/#concept-element-attributes-replace
  187. void NamedNodeMap::replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index)
  188. {
  189. VERIFY(old_attribute.owner_element());
  190. // 1. Replace oldAttr by newAttr in oldAttr’s element’s attribute list.
  191. m_attributes.remove(old_attribute_index);
  192. m_attributes.insert(old_attribute_index, new_attribute);
  193. // 2. Set newAttr’s element to oldAttr’s element.
  194. new_attribute.set_owner_element(old_attribute.owner_element());
  195. // 3. Set oldAttr’s element to null.
  196. old_attribute.set_owner_element(nullptr);
  197. // 4. Handle attribute changes for oldAttr with newAttr’s element, oldAttr’s value, and newAttr’s value.
  198. old_attribute.handle_attribute_changes(*new_attribute.owner_element(), old_attribute.value(), new_attribute.value());
  199. }
  200. // https://dom.spec.whatwg.org/#concept-element-attributes-append
  201. void NamedNodeMap::append_attribute(Attr& attribute)
  202. {
  203. // 1. Append attribute to element’s attribute list.
  204. m_attributes.append(attribute);
  205. // 2. Set attribute’s element to element.
  206. attribute.set_owner_element(&associated_element());
  207. // 3. Handle attribute changes for attribute with element, null, and attribute’s value.
  208. attribute.handle_attribute_changes(associated_element(), {}, attribute.value());
  209. }
  210. // https://dom.spec.whatwg.org/#concept-element-attributes-remove
  211. void NamedNodeMap::remove_attribute_at_index(size_t attribute_index)
  212. {
  213. GC::Ref<Attr> attribute = m_attributes.at(attribute_index);
  214. // 1. Let element be attribute’s element.
  215. auto* element = attribute->owner_element();
  216. VERIFY(element);
  217. // 2. Remove attribute from element’s attribute list.
  218. m_attributes.remove(attribute_index);
  219. // 3. Set attribute’s element to null.
  220. attribute->set_owner_element(nullptr);
  221. // 4. Handle attribute changes for attribute with element, attribute’s value, and null.
  222. attribute->handle_attribute_changes(*element, attribute->value(), {});
  223. }
  224. // https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-name
  225. Attr const* NamedNodeMap::remove_attribute(FlyString const& qualified_name)
  226. {
  227. size_t item_index = 0;
  228. // 1. Let attr be the result of getting an attribute given qualifiedName and element.
  229. auto const* attribute = get_attribute(qualified_name, &item_index);
  230. // 2. If attr is non-null, then remove attr.
  231. if (attribute)
  232. remove_attribute_at_index(item_index);
  233. // 3. Return attr.
  234. return attribute;
  235. }
  236. // https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-namespace
  237. Attr const* NamedNodeMap::remove_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name)
  238. {
  239. size_t item_index = 0;
  240. // 1. Let attr be the result of getting an attribute given namespace, localName, and element.
  241. auto const* attribute = get_attribute_ns(namespace_, local_name, &item_index);
  242. // 2. If attr is non-null, then remove attr.
  243. if (attribute)
  244. remove_attribute_at_index(item_index);
  245. // 3. Return attr.
  246. return attribute;
  247. }
  248. Optional<JS::Value> NamedNodeMap::item_value(size_t index) const
  249. {
  250. auto const* node = item(index);
  251. if (!node)
  252. return {};
  253. return node;
  254. }
  255. JS::Value NamedNodeMap::named_item_value(FlyString const& name) const
  256. {
  257. auto const* node = get_named_item(name);
  258. if (!node)
  259. return JS::js_undefined();
  260. return node;
  261. }
  262. // https://dom.spec.whatwg.org/#dom-element-removeattributenode
  263. WebIDL::ExceptionOr<GC::Ref<Attr>> NamedNodeMap::remove_attribute_node(GC::Ref<Attr> attr)
  264. {
  265. // 1. If this’s attribute list does not contain attr, then throw a "NotFoundError" DOMException.
  266. auto index = m_attributes.find_first_index(attr);
  267. if (!index.has_value())
  268. return WebIDL::NotFoundError::create(realm(), "Attribute not found"_string);
  269. // 2. Remove attr.
  270. remove_attribute_at_index(index.value());
  271. // 3. Return attr.
  272. return attr;
  273. }
  274. }