NamedNodeMap.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. JS_DEFINE_ALLOCATOR(NamedNodeMap);
  16. JS::NonnullGCPtr<NamedNodeMap> NamedNodeMap::create(Element& element)
  17. {
  18. auto& realm = element.realm();
  19. return realm.heap().allocate<NamedNodeMap>(realm, 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<JS::GCPtr<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<JS::GCPtr<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. // FIXME: Handle the second condition, assume it is an HTML document for now.
  126. bool compare_as_lowercase = associated_element().namespace_uri() == Namespace::HTML;
  127. // 2. Return the first attribute in element’s attribute list whose qualified name is qualifiedName; otherwise null.
  128. for (auto const& attribute : m_attributes) {
  129. if (compare_as_lowercase) {
  130. if (attribute->name().equals_ignoring_ascii_case(qualified_name))
  131. return attribute;
  132. } else {
  133. if (attribute->name() == qualified_name)
  134. return attribute;
  135. }
  136. if (item_index)
  137. ++(*item_index);
  138. }
  139. return nullptr;
  140. }
  141. Attr const* NamedNodeMap::get_attribute_with_lowercase_qualified_name(FlyString const& lowercase_qualified_name) const
  142. {
  143. bool compare_as_lowercase = associated_element().namespace_uri() == Namespace::HTML;
  144. VERIFY(compare_as_lowercase);
  145. for (auto const& attribute : m_attributes) {
  146. if (attribute->lowercase_name() == lowercase_qualified_name)
  147. return attribute;
  148. }
  149. return nullptr;
  150. }
  151. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
  152. Attr* NamedNodeMap::get_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name, size_t* item_index)
  153. {
  154. return const_cast<Attr*>(const_cast<NamedNodeMap const*>(this)->get_attribute_ns(namespace_, local_name, item_index));
  155. }
  156. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
  157. Attr const* NamedNodeMap::get_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name, size_t* item_index) const
  158. {
  159. if (item_index)
  160. *item_index = 0;
  161. // 1. If namespace is the empty string, then set it to null.
  162. Optional<FlyString> normalized_namespace;
  163. if (namespace_ != String {})
  164. normalized_namespace = namespace_;
  165. // 2. Return the attribute in element’s attribute list whose namespace is namespace and local name is localName, if any; otherwise null.
  166. for (auto const& attribute : m_attributes) {
  167. if (attribute->namespace_uri() == normalized_namespace && attribute->local_name() == local_name)
  168. return attribute.ptr();
  169. if (item_index)
  170. ++(*item_index);
  171. }
  172. return nullptr;
  173. }
  174. // https://dom.spec.whatwg.org/#concept-element-attributes-set
  175. WebIDL::ExceptionOr<JS::GCPtr<Attr>> NamedNodeMap::set_attribute(Attr& attribute)
  176. {
  177. // 1. If attr’s element is neither null nor element, throw an "InUseAttributeError" DOMException.
  178. if ((attribute.owner_element() != nullptr) && (attribute.owner_element() != &associated_element()))
  179. return WebIDL::InUseAttributeError::create(realm(), "Attribute must not already be in use"_string);
  180. // 2. Let oldAttr be the result of getting an attribute given attr’s namespace, attr’s local name, and element.
  181. size_t old_attribute_index = 0;
  182. auto* old_attribute = get_attribute_ns(attribute.namespace_uri(), attribute.local_name(), &old_attribute_index);
  183. // 3. If oldAttr is attr, return attr.
  184. if (old_attribute == &attribute)
  185. return &attribute;
  186. // 4. If oldAttr is non-null, then replace oldAttr with attr.
  187. if (old_attribute) {
  188. replace_attribute(*old_attribute, attribute, old_attribute_index);
  189. }
  190. // 5. Otherwise, append attr to element.
  191. else {
  192. append_attribute(attribute);
  193. }
  194. // 6. Return oldAttr.
  195. return old_attribute;
  196. }
  197. // https://dom.spec.whatwg.org/#concept-element-attributes-replace
  198. void NamedNodeMap::replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index)
  199. {
  200. VERIFY(old_attribute.owner_element());
  201. // 1. Replace oldAttr by newAttr in oldAttr’s element’s attribute list.
  202. m_attributes.remove(old_attribute_index);
  203. m_attributes.insert(old_attribute_index, new_attribute);
  204. // 2. Set newAttr’s element to oldAttr’s element.
  205. new_attribute.set_owner_element(old_attribute.owner_element());
  206. // 3. Set oldAttr’s element to null.
  207. old_attribute.set_owner_element(nullptr);
  208. // 4. Handle attribute changes for oldAttr with newAttr’s element, oldAttr’s value, and newAttr’s value.
  209. old_attribute.handle_attribute_changes(*new_attribute.owner_element(), old_attribute.value(), new_attribute.value());
  210. }
  211. // https://dom.spec.whatwg.org/#concept-element-attributes-append
  212. void NamedNodeMap::append_attribute(Attr& attribute)
  213. {
  214. // 1. Append attribute to element’s attribute list.
  215. m_attributes.append(attribute);
  216. // 2. Set attribute’s element to element.
  217. attribute.set_owner_element(&associated_element());
  218. // 3. Handle attribute changes for attribute with element, null, and attribute’s value.
  219. attribute.handle_attribute_changes(associated_element(), {}, attribute.value());
  220. }
  221. // https://dom.spec.whatwg.org/#concept-element-attributes-remove
  222. void NamedNodeMap::remove_attribute_at_index(size_t attribute_index)
  223. {
  224. JS::NonnullGCPtr<Attr> attribute = m_attributes.at(attribute_index);
  225. // 1. Let element be attribute’s element.
  226. auto* element = attribute->owner_element();
  227. VERIFY(element);
  228. // 2. Remove attribute from element’s attribute list.
  229. m_attributes.remove(attribute_index);
  230. // 3. Set attribute’s element to null.
  231. attribute->set_owner_element(nullptr);
  232. // 4. Handle attribute changes for attribute with element, attribute’s value, and null.
  233. attribute->handle_attribute_changes(*element, attribute->value(), {});
  234. }
  235. // https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-name
  236. Attr const* NamedNodeMap::remove_attribute(FlyString const& qualified_name)
  237. {
  238. size_t item_index = 0;
  239. // 1. Let attr be the result of getting an attribute given qualifiedName and element.
  240. auto const* attribute = get_attribute(qualified_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. // https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-namespace
  248. Attr const* NamedNodeMap::remove_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name)
  249. {
  250. size_t item_index = 0;
  251. // 1. Let attr be the result of getting an attribute given namespace, localName, and element.
  252. auto const* attribute = get_attribute_ns(namespace_, local_name, &item_index);
  253. // 2. If attr is non-null, then remove attr.
  254. if (attribute)
  255. remove_attribute_at_index(item_index);
  256. // 3. Return attr.
  257. return attribute;
  258. }
  259. Optional<JS::Value> NamedNodeMap::item_value(size_t index) const
  260. {
  261. auto const* node = item(index);
  262. if (!node)
  263. return {};
  264. return node;
  265. }
  266. JS::Value NamedNodeMap::named_item_value(FlyString const& name) const
  267. {
  268. auto const* node = get_named_item(name);
  269. if (!node)
  270. return JS::js_undefined();
  271. return node;
  272. }
  273. // https://dom.spec.whatwg.org/#dom-element-removeattributenode
  274. WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> NamedNodeMap::remove_attribute_node(JS::NonnullGCPtr<Attr> attr)
  275. {
  276. // 1. If this’s attribute list does not contain attr, then throw a "NotFoundError" DOMException.
  277. auto index = m_attributes.find_first_index(attr);
  278. if (!index.has_value())
  279. return WebIDL::NotFoundError::create(realm(), "Attribute not found"_string);
  280. // 2. Remove attr.
  281. remove_attribute_at_index(index.value());
  282. // 3. Return attr.
  283. return attr;
  284. }
  285. }