NamedNodeMap.cpp 12 KB

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