NamedNodeMap.cpp 13 KB

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