NamedNodeMap.cpp 13 KB

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