NamedNodeMap.cpp 12 KB

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