NamedNodeMap.cpp 11 KB

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