NamedNodeMap.cpp 9.8 KB

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