NamedNodeMap.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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(Optional<String> const& namespace_, StringView local_name) const
  76. {
  77. // FIXME: This conversion is quite ugly.
  78. StringView namespace_view;
  79. if (namespace_.has_value())
  80. namespace_view = namespace_->bytes_as_string_view();
  81. return get_attribute_ns(namespace_view, local_name);
  82. }
  83. // https://dom.spec.whatwg.org/#dom-namednodemap-setnameditem
  84. WebIDL::ExceptionOr<JS::GCPtr<Attr>> NamedNodeMap::set_named_item(Attr& attribute)
  85. {
  86. return set_attribute(attribute);
  87. }
  88. // https://dom.spec.whatwg.org/#dom-namednodemap-setnameditemns
  89. WebIDL::ExceptionOr<JS::GCPtr<Attr>> NamedNodeMap::set_named_item_ns(Attr& attribute)
  90. {
  91. return set_attribute(attribute);
  92. }
  93. // https://dom.spec.whatwg.org/#dom-namednodemap-removenameditem
  94. WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item(StringView qualified_name)
  95. {
  96. // 1. Let attr be the result of removing an attribute given qualifiedName and element.
  97. auto const* attribute = remove_attribute(qualified_name);
  98. // 2. If attr is null, then throw a "NotFoundError" DOMException.
  99. if (!attribute)
  100. return WebIDL::NotFoundError::create(realm(), MUST(String::formatted("Attribute with name '{}' not found", qualified_name)));
  101. // 3. Return attr.
  102. return attribute;
  103. }
  104. // https://dom.spec.whatwg.org/#dom-namednodemap-removenameditemns
  105. WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item_ns(Optional<String> const& namespace_, StringView local_name)
  106. {
  107. // FIXME: This conversion is quite ugly.
  108. StringView namespace_view;
  109. if (namespace_.has_value())
  110. namespace_view = namespace_->bytes_as_string_view();
  111. // 1. Let attr be the result of removing an attribute given namespace, localName, and element.
  112. auto const* attribute = remove_attribute_ns(namespace_view, 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(StringView 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(StringView 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.ptr();
  137. } else {
  138. if (attribute->name() == qualified_name)
  139. return attribute.ptr();
  140. }
  141. if (item_index)
  142. ++(*item_index);
  143. }
  144. return nullptr;
  145. }
  146. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
  147. Attr* NamedNodeMap::get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index)
  148. {
  149. return const_cast<Attr*>(const_cast<NamedNodeMap const*>(this)->get_attribute_ns(namespace_, local_name, item_index));
  150. }
  151. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
  152. Attr const* NamedNodeMap::get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index) const
  153. {
  154. if (item_index)
  155. *item_index = 0;
  156. // 1. If namespace is the empty string, then set it to null.
  157. if (namespace_.is_empty())
  158. namespace_ = {};
  159. // 2. Return the attribute in element’s attribute list whose namespace is namespace and local name is localName, if any; otherwise null.
  160. for (auto const& attribute : m_attributes) {
  161. if (attribute->namespace_uri() == namespace_ && attribute->local_name() == local_name)
  162. return attribute.ptr();
  163. if (item_index)
  164. ++(*item_index);
  165. }
  166. return nullptr;
  167. }
  168. // https://dom.spec.whatwg.org/#concept-element-attributes-set
  169. WebIDL::ExceptionOr<JS::GCPtr<Attr>> NamedNodeMap::set_attribute(Attr& attribute)
  170. {
  171. // 1. If attr’s element is neither null nor element, throw an "InUseAttributeError" DOMException.
  172. if ((attribute.owner_element() != nullptr) && (attribute.owner_element() != &associated_element()))
  173. return WebIDL::InUseAttributeError::create(realm(), "Attribute must not already be in use"_fly_string);
  174. // 2. Let oldAttr be the result of getting an attribute given attr’s namespace, attr’s local name, and element.
  175. size_t old_attribute_index = 0;
  176. auto* old_attribute = get_attribute_ns(attribute.namespace_uri(), attribute.local_name(), &old_attribute_index);
  177. // 3. If oldAttr is attr, return attr.
  178. if (old_attribute == &attribute)
  179. return &attribute;
  180. // 4. If oldAttr is non-null, then replace oldAttr with attr.
  181. if (old_attribute) {
  182. replace_attribute(*old_attribute, attribute, old_attribute_index);
  183. }
  184. // 5. Otherwise, append attr to element.
  185. else {
  186. append_attribute(attribute);
  187. }
  188. // 6. Return oldAttr.
  189. return old_attribute;
  190. }
  191. // https://dom.spec.whatwg.org/#concept-element-attributes-replace
  192. void NamedNodeMap::replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index)
  193. {
  194. VERIFY(old_attribute.owner_element());
  195. // 1. Replace oldAttr by newAttr in oldAttr’s element’s attribute list.
  196. m_attributes.remove(old_attribute_index);
  197. m_attributes.insert(old_attribute_index, new_attribute);
  198. // 2. Set newAttr’s element to oldAttr’s element.
  199. new_attribute.set_owner_element(old_attribute.owner_element());
  200. // 3. Set oldAttr’s element to null.
  201. old_attribute.set_owner_element(nullptr);
  202. // 4. Handle attribute changes for oldAttr with newAttr’s element, oldAttr’s value, and newAttr’s value.
  203. old_attribute.handle_attribute_changes(*new_attribute.owner_element(), old_attribute.value(), new_attribute.value());
  204. }
  205. // https://dom.spec.whatwg.org/#concept-element-attributes-append
  206. void NamedNodeMap::append_attribute(Attr& attribute)
  207. {
  208. // 1. Append attribute to element’s attribute list.
  209. m_attributes.append(attribute);
  210. // 2. Set attribute’s element to element.
  211. attribute.set_owner_element(&associated_element());
  212. // 3. Handle attribute changes for attribute with element, null, and attribute’s value.
  213. attribute.handle_attribute_changes(associated_element(), {}, attribute.value());
  214. }
  215. // https://dom.spec.whatwg.org/#concept-element-attributes-remove
  216. void NamedNodeMap::remove_attribute_at_index(size_t attribute_index)
  217. {
  218. JS::NonnullGCPtr<Attr> attribute = m_attributes.at(attribute_index);
  219. // 1. Let element be attribute’s element.
  220. auto* element = attribute->owner_element();
  221. VERIFY(element);
  222. // 2. Remove attribute from element’s attribute list.
  223. m_attributes.remove(attribute_index);
  224. // 3. Set attribute’s element to null.
  225. attribute->set_owner_element(nullptr);
  226. // 4. Handle attribute changes for attribute with element, attribute’s value, and null.
  227. attribute->handle_attribute_changes(*element, attribute->value(), {});
  228. }
  229. // https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-name
  230. Attr const* NamedNodeMap::remove_attribute(StringView qualified_name)
  231. {
  232. size_t item_index = 0;
  233. // 1. Let attr be the result of getting an attribute given qualifiedName and element.
  234. auto const* attribute = get_attribute(qualified_name, &item_index);
  235. // 2. If attr is non-null, then remove attr.
  236. if (attribute)
  237. remove_attribute_at_index(item_index);
  238. // 3. Return attr.
  239. return attribute;
  240. }
  241. // https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-namespace
  242. Attr const* NamedNodeMap::remove_attribute_ns(StringView namespace_, StringView local_name)
  243. {
  244. size_t item_index = 0;
  245. // 1. Let attr be the result of getting an attribute given namespace, localName, and element.
  246. auto const* attribute = get_attribute_ns(namespace_, local_name, &item_index);
  247. // 2. If attr is non-null, then remove attr.
  248. if (attribute)
  249. remove_attribute_at_index(item_index);
  250. // 3. Return attr.
  251. return attribute;
  252. }
  253. WebIDL::ExceptionOr<JS::Value> NamedNodeMap::item_value(size_t index) const
  254. {
  255. auto const* node = item(index);
  256. if (!node)
  257. return JS::js_undefined();
  258. return node;
  259. }
  260. WebIDL::ExceptionOr<JS::Value> NamedNodeMap::named_item_value(DeprecatedFlyString const& name) const
  261. {
  262. auto const* node = get_named_item(name);
  263. if (!node)
  264. return JS::js_undefined();
  265. return node;
  266. }
  267. }