NamedNodeMap.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. if (associated_element().namespace_uri() == Namespace::HTML && associated_element().document().is_html_document()) {
  55. // 1. Let lowercaseName be name, in ASCII lowercase.
  56. // 2. If lowercaseName is not equal to name, remove name from names.
  57. names.remove_all_matching([](auto const& name) { return name != name.to_ascii_lowercase(); });
  58. }
  59. // 3. Return names.
  60. return names;
  61. }
  62. // https://dom.spec.whatwg.org/#dom-namednodemap-item
  63. Attr const* NamedNodeMap::item(u32 index) const
  64. {
  65. // 1. If index is equal to or greater than this’s attribute list’s size, then return null.
  66. if (index >= m_attributes.size())
  67. return nullptr;
  68. // 2. Otherwise, return this’s attribute list[index].
  69. return m_attributes[index].ptr();
  70. }
  71. // https://dom.spec.whatwg.org/#dom-namednodemap-getnameditem
  72. Attr const* NamedNodeMap::get_named_item(FlyString const& qualified_name) const
  73. {
  74. return get_attribute(qualified_name);
  75. }
  76. // https://dom.spec.whatwg.org/#dom-namednodemap-getnameditemns
  77. Attr const* NamedNodeMap::get_named_item_ns(Optional<FlyString> const& namespace_, FlyString const& local_name) const
  78. {
  79. return get_attribute_ns(namespace_, local_name);
  80. }
  81. // https://dom.spec.whatwg.org/#dom-namednodemap-setnameditem
  82. WebIDL::ExceptionOr<GC::Ptr<Attr>> NamedNodeMap::set_named_item(Attr& attribute)
  83. {
  84. return set_attribute(attribute);
  85. }
  86. // https://dom.spec.whatwg.org/#dom-namednodemap-setnameditemns
  87. WebIDL::ExceptionOr<GC::Ptr<Attr>> NamedNodeMap::set_named_item_ns(Attr& attribute)
  88. {
  89. return set_attribute(attribute);
  90. }
  91. // https://dom.spec.whatwg.org/#dom-namednodemap-removenameditem
  92. WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item(FlyString const& qualified_name)
  93. {
  94. // 1. Let attr be the result of removing an attribute given qualifiedName and element.
  95. auto const* attribute = remove_attribute(qualified_name);
  96. // 2. If attr is null, then throw a "NotFoundError" DOMException.
  97. if (!attribute)
  98. return WebIDL::NotFoundError::create(realm(), MUST(String::formatted("Attribute with name '{}' not found", qualified_name)));
  99. // 3. Return attr.
  100. return attribute;
  101. }
  102. // https://dom.spec.whatwg.org/#dom-namednodemap-removenameditemns
  103. WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item_ns(Optional<FlyString> const& namespace_, FlyString const& local_name)
  104. {
  105. // 1. Let attr be the result of removing an attribute given namespace, localName, and element.
  106. auto const* attribute = remove_attribute_ns(namespace_, local_name);
  107. // 2. If attr is null, then throw a "NotFoundError" DOMException.
  108. if (!attribute)
  109. return WebIDL::NotFoundError::create(realm(), MUST(String::formatted("Attribute with namespace '{}' and local name '{}' not found", namespace_, local_name)));
  110. // 3. Return attr.
  111. return attribute;
  112. }
  113. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
  114. Attr* NamedNodeMap::get_attribute(FlyString const& qualified_name, size_t* item_index)
  115. {
  116. return const_cast<Attr*>(const_cast<NamedNodeMap const*>(this)->get_attribute(qualified_name, item_index));
  117. }
  118. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
  119. Attr const* NamedNodeMap::get_attribute(FlyString const& qualified_name, size_t* item_index) const
  120. {
  121. if (item_index)
  122. *item_index = 0;
  123. // 1. If element is in the HTML namespace and its node document is an HTML document, then set qualifiedName to qualifiedName in ASCII lowercase.
  124. bool compare_as_lowercase = associated_element().namespace_uri() == Namespace::HTML && associated_element().document().is_html_document();
  125. // 2. Return the first attribute in element’s attribute list whose qualified name is qualifiedName; otherwise null.
  126. for (auto const& attribute : m_attributes) {
  127. if (compare_as_lowercase) {
  128. if (attribute->name().equals_ignoring_ascii_case(qualified_name) && !AK::any_of(attribute->name().bytes(), is_ascii_upper_alpha))
  129. return attribute;
  130. } else {
  131. if (attribute->name() == qualified_name)
  132. return attribute;
  133. }
  134. if (item_index)
  135. ++(*item_index);
  136. }
  137. return nullptr;
  138. }
  139. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
  140. Attr* NamedNodeMap::get_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name, size_t* item_index)
  141. {
  142. return const_cast<Attr*>(const_cast<NamedNodeMap const*>(this)->get_attribute_ns(namespace_, local_name, item_index));
  143. }
  144. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
  145. Attr const* NamedNodeMap::get_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name, size_t* item_index) const
  146. {
  147. if (item_index)
  148. *item_index = 0;
  149. // 1. If namespace is the empty string, then set it to null.
  150. Optional<FlyString> normalized_namespace;
  151. if (namespace_ != String {})
  152. normalized_namespace = namespace_;
  153. // 2. Return the attribute in element’s attribute list whose namespace is namespace and local name is localName, if any; otherwise null.
  154. for (auto const& attribute : m_attributes) {
  155. if (attribute->namespace_uri() == normalized_namespace && attribute->local_name() == local_name)
  156. return attribute.ptr();
  157. if (item_index)
  158. ++(*item_index);
  159. }
  160. return nullptr;
  161. }
  162. // https://dom.spec.whatwg.org/#concept-element-attributes-set
  163. WebIDL::ExceptionOr<GC::Ptr<Attr>> NamedNodeMap::set_attribute(Attr& attribute)
  164. {
  165. // 1. If attr’s element is neither null nor element, throw an "InUseAttributeError" DOMException.
  166. if ((attribute.owner_element() != nullptr) && (attribute.owner_element() != &associated_element()))
  167. return WebIDL::InUseAttributeError::create(realm(), "Attribute must not already be in use"_string);
  168. // 2. Let oldAttr be the result of getting an attribute given attr’s namespace, attr’s local name, and element.
  169. size_t old_attribute_index = 0;
  170. auto* old_attribute = get_attribute_ns(attribute.namespace_uri(), attribute.local_name(), &old_attribute_index);
  171. // 3. If oldAttr is attr, return attr.
  172. if (old_attribute == &attribute)
  173. return &attribute;
  174. // 4. If oldAttr is non-null, then replace oldAttr with attr.
  175. if (old_attribute) {
  176. replace_attribute(*old_attribute, attribute, old_attribute_index);
  177. }
  178. // 5. Otherwise, append attr to element.
  179. else {
  180. append_attribute(attribute);
  181. }
  182. // 6. Return oldAttr.
  183. return old_attribute;
  184. }
  185. // https://dom.spec.whatwg.org/#concept-element-attributes-replace
  186. void NamedNodeMap::replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index)
  187. {
  188. VERIFY(old_attribute.owner_element());
  189. // 1. Replace oldAttr by newAttr in oldAttr’s element’s attribute list.
  190. m_attributes.remove(old_attribute_index);
  191. m_attributes.insert(old_attribute_index, new_attribute);
  192. // 2. Set newAttr’s element to oldAttr’s element.
  193. new_attribute.set_owner_element(old_attribute.owner_element());
  194. // 3. Set oldAttr’s element to null.
  195. old_attribute.set_owner_element(nullptr);
  196. // 4. Handle attribute changes for oldAttr with newAttr’s element, oldAttr’s value, and newAttr’s value.
  197. old_attribute.handle_attribute_changes(*new_attribute.owner_element(), old_attribute.value(), new_attribute.value());
  198. }
  199. // https://dom.spec.whatwg.org/#concept-element-attributes-append
  200. void NamedNodeMap::append_attribute(Attr& attribute)
  201. {
  202. // 1. Append attribute to element’s attribute list.
  203. m_attributes.append(attribute);
  204. // 2. Set attribute’s element to element.
  205. attribute.set_owner_element(&associated_element());
  206. // 3. Handle attribute changes for attribute with element, null, and attribute’s value.
  207. attribute.handle_attribute_changes(associated_element(), {}, attribute.value());
  208. }
  209. // https://dom.spec.whatwg.org/#concept-element-attributes-remove
  210. void NamedNodeMap::remove_attribute_at_index(size_t attribute_index)
  211. {
  212. GC::Ref<Attr> attribute = m_attributes.at(attribute_index);
  213. // 1. Let element be attribute’s element.
  214. auto* element = attribute->owner_element();
  215. VERIFY(element);
  216. // 2. Remove attribute from element’s attribute list.
  217. m_attributes.remove(attribute_index);
  218. // 3. Set attribute’s element to null.
  219. attribute->set_owner_element(nullptr);
  220. // 4. Handle attribute changes for attribute with element, attribute’s value, and null.
  221. attribute->handle_attribute_changes(*element, attribute->value(), {});
  222. }
  223. // https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-name
  224. Attr const* NamedNodeMap::remove_attribute(FlyString const& qualified_name)
  225. {
  226. size_t item_index = 0;
  227. // 1. Let attr be the result of getting an attribute given qualifiedName and element.
  228. auto const* attribute = get_attribute(qualified_name, &item_index);
  229. // 2. If attr is non-null, then remove attr.
  230. if (attribute)
  231. remove_attribute_at_index(item_index);
  232. // 3. Return attr.
  233. return attribute;
  234. }
  235. // https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-namespace
  236. Attr const* NamedNodeMap::remove_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name)
  237. {
  238. size_t item_index = 0;
  239. // 1. Let attr be the result of getting an attribute given namespace, localName, and element.
  240. auto const* attribute = get_attribute_ns(namespace_, local_name, &item_index);
  241. // 2. If attr is non-null, then remove attr.
  242. if (attribute)
  243. remove_attribute_at_index(item_index);
  244. // 3. Return attr.
  245. return attribute;
  246. }
  247. Optional<JS::Value> NamedNodeMap::item_value(size_t index) const
  248. {
  249. auto const* node = item(index);
  250. if (!node)
  251. return {};
  252. return node;
  253. }
  254. JS::Value NamedNodeMap::named_item_value(FlyString const& name) const
  255. {
  256. auto const* node = get_named_item(name);
  257. if (!node)
  258. return JS::js_undefined();
  259. return node;
  260. }
  261. // https://dom.spec.whatwg.org/#dom-element-removeattributenode
  262. WebIDL::ExceptionOr<GC::Ref<Attr>> NamedNodeMap::remove_attribute_node(GC::Ref<Attr> attr)
  263. {
  264. // 1. If this’s attribute list does not contain attr, then throw a "NotFoundError" DOMException.
  265. auto index = m_attributes.find_first_index(attr);
  266. if (!index.has_value())
  267. return WebIDL::NotFoundError::create(realm(), "Attribute not found"_string);
  268. // 2. Remove attr.
  269. remove_attribute_at_index(index.value());
  270. // 3. Return attr.
  271. return attr;
  272. }
  273. }