NamedNodeMap.cpp 13 KB

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