NamedNodeMap.cpp 12 KB

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