NamedNodeMap.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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-removenameditem
  80. WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item(StringView qualified_name)
  81. {
  82. // 1. Let attr be the result of removing an attribute given qualifiedName and element.
  83. auto const* attribute = remove_attribute(qualified_name);
  84. // 2. If attr is null, then throw a "NotFoundError" DOMException.
  85. if (!attribute)
  86. return WebIDL::NotFoundError::create(realm(), DeprecatedString::formatted("Attribute with name '{}' not found", qualified_name));
  87. // 3. Return attr.
  88. return attribute;
  89. }
  90. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
  91. Attr* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_index)
  92. {
  93. return const_cast<Attr*>(const_cast<NamedNodeMap const*>(this)->get_attribute(qualified_name, item_index));
  94. }
  95. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
  96. Attr const* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_index) const
  97. {
  98. if (item_index)
  99. *item_index = 0;
  100. // 1. If element is in the HTML namespace and its node document is an HTML document, then set qualifiedName to qualifiedName in ASCII lowercase.
  101. // FIXME: Handle the second condition, assume it is an HTML document for now.
  102. bool compare_as_lowercase = associated_element().namespace_uri() == Namespace::HTML;
  103. // 2. Return the first attribute in element’s attribute list whose qualified name is qualifiedName; otherwise null.
  104. for (auto const& attribute : m_attributes) {
  105. if (compare_as_lowercase) {
  106. if (attribute->name().equals_ignoring_case(qualified_name))
  107. return attribute.ptr();
  108. } else {
  109. if (attribute->name() == qualified_name)
  110. return attribute.ptr();
  111. }
  112. if (item_index)
  113. ++(*item_index);
  114. }
  115. return nullptr;
  116. }
  117. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
  118. Attr* NamedNodeMap::get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index)
  119. {
  120. return const_cast<Attr*>(const_cast<NamedNodeMap const*>(this)->get_attribute_ns(namespace_, local_name, item_index));
  121. }
  122. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
  123. Attr const* NamedNodeMap::get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index) const
  124. {
  125. if (item_index)
  126. *item_index = 0;
  127. // 1. If namespace is the empty string, then set it to null.
  128. if (namespace_.is_empty())
  129. namespace_ = {};
  130. // 2. Return the attribute in element’s attribute list whose namespace is namespace and local name is localName, if any; otherwise null.
  131. for (auto const& attribute : m_attributes) {
  132. if (attribute->namespace_uri() == namespace_ && attribute->local_name() == local_name)
  133. return attribute.ptr();
  134. if (item_index)
  135. ++(*item_index);
  136. }
  137. return nullptr;
  138. }
  139. // https://dom.spec.whatwg.org/#concept-element-attributes-set
  140. WebIDL::ExceptionOr<Attr const*> NamedNodeMap::set_attribute(Attr& attribute)
  141. {
  142. // 1. If attr’s element is neither null nor element, throw an "InUseAttributeError" DOMException.
  143. if ((attribute.owner_element() != nullptr) && (attribute.owner_element() != &associated_element()))
  144. return WebIDL::InUseAttributeError::create(realm(), "Attribute must not already be in use"sv);
  145. // 2. Let oldAttr be the result of getting an attribute given attr’s namespace, attr’s local name, and element.
  146. size_t old_attribute_index = 0;
  147. auto* old_attribute = get_attribute_ns(attribute.namespace_uri(), attribute.local_name(), &old_attribute_index);
  148. // 3. If oldAttr is attr, return attr.
  149. if (old_attribute == &attribute)
  150. return &attribute;
  151. // 4. If oldAttr is non-null, then replace oldAttr with attr.
  152. if (old_attribute) {
  153. replace_attribute(*old_attribute, attribute, old_attribute_index);
  154. }
  155. // 5. Otherwise, append attr to element.
  156. else {
  157. append_attribute(attribute);
  158. }
  159. // 6. Return oldAttr.
  160. return old_attribute;
  161. }
  162. // https://dom.spec.whatwg.org/#concept-element-attributes-replace
  163. void NamedNodeMap::replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index)
  164. {
  165. // 1. Handle attribute changes for oldAttr with oldAttr’s element, oldAttr’s value, and newAttr’s value.
  166. VERIFY(old_attribute.owner_element());
  167. old_attribute.handle_attribute_changes(*old_attribute.owner_element(), old_attribute.value(), new_attribute.value());
  168. // 2. Replace oldAttr by newAttr in oldAttr’s element’s attribute list.
  169. m_attributes.remove(old_attribute_index);
  170. m_attributes.insert(old_attribute_index, new_attribute);
  171. // 3. Set newAttr’s element to oldAttr’s element.
  172. new_attribute.set_owner_element(old_attribute.owner_element());
  173. // 4 .Set oldAttr’s element to null.
  174. old_attribute.set_owner_element(nullptr);
  175. }
  176. // https://dom.spec.whatwg.org/#concept-element-attributes-append
  177. void NamedNodeMap::append_attribute(Attr& attribute)
  178. {
  179. // 1. Handle attribute changes for attribute with element, null, and attribute’s value.
  180. attribute.handle_attribute_changes(associated_element(), {}, attribute.value());
  181. // 2. Append attribute to element’s attribute list.
  182. m_attributes.append(attribute);
  183. // 3. Set attribute’s element to element.
  184. attribute.set_owner_element(&associated_element());
  185. }
  186. // https://dom.spec.whatwg.org/#concept-element-attributes-remove
  187. void NamedNodeMap::remove_attribute_at_index(size_t attribute_index)
  188. {
  189. JS::NonnullGCPtr<Attr> attribute = m_attributes.at(attribute_index);
  190. // 1. Handle attribute changes for attribute with attribute’s element, attribute’s value, and null.
  191. VERIFY(attribute->owner_element());
  192. attribute->handle_attribute_changes(*attribute->owner_element(), attribute->value(), {});
  193. // 2. Remove attribute from attribute’s element’s attribute list.
  194. m_attributes.remove(attribute_index);
  195. // 3. Set attribute’s element to null.
  196. attribute->set_owner_element(nullptr);
  197. }
  198. // https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-name
  199. Attr const* NamedNodeMap::remove_attribute(StringView qualified_name)
  200. {
  201. size_t item_index = 0;
  202. // 1. Let attr be the result of getting an attribute given qualifiedName and element.
  203. auto const* attribute = get_attribute(qualified_name, &item_index);
  204. // 2. If attr is non-null, then remove attr.
  205. if (attribute)
  206. remove_attribute_at_index(item_index);
  207. // 3. Return attr.
  208. return attribute;
  209. }
  210. JS::Value NamedNodeMap::item_value(size_t index) const
  211. {
  212. auto const* node = item(index);
  213. if (!node)
  214. return JS::js_undefined();
  215. return node;
  216. }
  217. JS::Value NamedNodeMap::named_item_value(FlyString const& name) const
  218. {
  219. auto const* node = get_named_item(name);
  220. if (!node)
  221. return JS::js_undefined();
  222. return node;
  223. }
  224. }