NamedNodeMap.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/DOM/Attr.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/NamedNodeMap.h>
  10. #include <LibWeb/Namespace.h>
  11. namespace Web::DOM {
  12. JS::NonnullGCPtr<NamedNodeMap> NamedNodeMap::create(Element& element)
  13. {
  14. auto& realm = element.realm();
  15. return *realm.heap().allocate<NamedNodeMap>(realm, element);
  16. }
  17. NamedNodeMap::NamedNodeMap(Element& element)
  18. : Bindings::LegacyPlatformObject(element.window().cached_web_prototype("NamedNodeMap"))
  19. , m_element(element)
  20. {
  21. }
  22. void NamedNodeMap::visit_edges(Cell::Visitor& visitor)
  23. {
  24. Base::visit_edges(visitor);
  25. visitor.visit(m_element.ptr());
  26. for (auto& attribute : m_attributes)
  27. visitor.visit(attribute.ptr());
  28. }
  29. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices%E2%91%A3
  30. bool NamedNodeMap::is_supported_property_index(u32 index) const
  31. {
  32. return index < m_attributes.size();
  33. }
  34. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names%E2%91%A0
  35. Vector<String> NamedNodeMap::supported_property_names() const
  36. {
  37. // 1. Let names be the qualified names of the attributes in this NamedNodeMap object’s attribute list, with duplicates omitted, in order.
  38. Vector<String> names;
  39. names.ensure_capacity(m_attributes.size());
  40. for (auto const& attribute : m_attributes) {
  41. if (!names.contains_slow(attribute->name()))
  42. names.append(attribute->name());
  43. }
  44. // 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:
  45. // FIXME: Handle the second condition, assume it is an HTML document for now.
  46. if (associated_element().namespace_uri() == Namespace::HTML) {
  47. // 1. Let lowercaseName be name, in ASCII lowercase.
  48. // 2. If lowercaseName is not equal to name, remove name from names.
  49. names.remove_all_matching([](auto const& name) { return name != name.to_lowercase(); });
  50. }
  51. // 3. Return names.
  52. return names;
  53. }
  54. // https://dom.spec.whatwg.org/#dom-namednodemap-item
  55. Attr const* NamedNodeMap::item(u32 index) const
  56. {
  57. // 1. If index is equal to or greater than this’s attribute list’s size, then return null.
  58. if (index >= m_attributes.size())
  59. return nullptr;
  60. // 2. Otherwise, return this’s attribute list[index].
  61. return m_attributes[index].ptr();
  62. }
  63. // https://dom.spec.whatwg.org/#dom-namednodemap-getnameditem
  64. Attr const* NamedNodeMap::get_named_item(StringView qualified_name) const
  65. {
  66. return get_attribute(qualified_name);
  67. }
  68. // https://dom.spec.whatwg.org/#dom-namednodemap-setnameditem
  69. WebIDL::ExceptionOr<Attr const*> NamedNodeMap::set_named_item(Attr& attribute)
  70. {
  71. return set_attribute(attribute);
  72. }
  73. // https://dom.spec.whatwg.org/#dom-namednodemap-removenameditem
  74. WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item(StringView qualified_name)
  75. {
  76. // 1. Let attr be the result of removing an attribute given qualifiedName and element.
  77. auto const* attribute = remove_attribute(qualified_name);
  78. // 2. If attr is null, then throw a "NotFoundError" DOMException.
  79. if (!attribute)
  80. return NotFoundError::create(global_object(), String::formatted("Attribute with name '{}' not found", qualified_name));
  81. // 3. Return attr.
  82. return nullptr;
  83. }
  84. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
  85. Attr* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_index)
  86. {
  87. return const_cast<Attr*>(const_cast<NamedNodeMap const*>(this)->get_attribute(qualified_name, item_index));
  88. }
  89. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
  90. Attr const* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_index) const
  91. {
  92. if (item_index)
  93. *item_index = 0;
  94. // 1. If element is in the HTML namespace and its node document is an HTML document, then set qualifiedName to qualifiedName in ASCII lowercase.
  95. // FIXME: Handle the second condition, assume it is an HTML document for now.
  96. bool compare_as_lowercase = associated_element().namespace_uri() == Namespace::HTML;
  97. // 2. Return the first attribute in element’s attribute list whose qualified name is qualifiedName; otherwise null.
  98. for (auto const& attribute : m_attributes) {
  99. if (compare_as_lowercase) {
  100. if (attribute->name().equals_ignoring_case(qualified_name))
  101. return attribute.ptr();
  102. } else {
  103. if (attribute->name() == qualified_name)
  104. return attribute.ptr();
  105. }
  106. if (item_index)
  107. ++(*item_index);
  108. }
  109. return nullptr;
  110. }
  111. // https://dom.spec.whatwg.org/#concept-element-attributes-set
  112. WebIDL::ExceptionOr<Attr const*> NamedNodeMap::set_attribute(Attr& attribute)
  113. {
  114. // 1. If attr’s element is neither null nor element, throw an "InUseAttributeError" DOMException.
  115. if ((attribute.owner_element() != nullptr) && (attribute.owner_element() != &associated_element()))
  116. return InUseAttributeError::create(global_object(), "Attribute must not already be in use"sv);
  117. // 2. Let oldAttr be the result of getting an attribute given attr’s namespace, attr’s local name, and element.
  118. // FIXME: When getNamedItemNS is implemented, use that instead.
  119. size_t old_attribute_index = 0;
  120. auto* old_attribute = get_attribute(attribute.local_name(), &old_attribute_index);
  121. // 3. If oldAttr is attr, return attr.
  122. if (old_attribute == &attribute)
  123. return &attribute;
  124. // 4. If oldAttr is non-null, then replace oldAttr with attr.
  125. if (old_attribute) {
  126. replace_attribute(*old_attribute, attribute, old_attribute_index);
  127. }
  128. // 5. Otherwise, append attr to element.
  129. else {
  130. append_attribute(attribute);
  131. }
  132. // 6. Return oldAttr.
  133. return old_attribute;
  134. }
  135. // https://dom.spec.whatwg.org/#concept-element-attributes-replace
  136. void NamedNodeMap::replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index)
  137. {
  138. // 1. Handle attribute changes for oldAttr with oldAttr’s element, oldAttr’s value, and newAttr’s value.
  139. VERIFY(old_attribute.owner_element());
  140. old_attribute.handle_attribute_changes(*old_attribute.owner_element(), old_attribute.value(), new_attribute.value());
  141. // 2. Replace oldAttr by newAttr in oldAttr’s element’s attribute list.
  142. m_attributes.remove(old_attribute_index);
  143. m_attributes.insert(old_attribute_index, new_attribute);
  144. // 3. Set newAttr’s element to oldAttr’s element.
  145. new_attribute.set_owner_element(old_attribute.owner_element());
  146. // 4 .Set oldAttr’s element to null.
  147. old_attribute.set_owner_element(nullptr);
  148. }
  149. // https://dom.spec.whatwg.org/#concept-element-attributes-append
  150. void NamedNodeMap::append_attribute(Attr& attribute)
  151. {
  152. // 1. Handle attribute changes for attribute with element, null, and attribute’s value.
  153. attribute.handle_attribute_changes(associated_element(), {}, attribute.value());
  154. // 2. Append attribute to element’s attribute list.
  155. m_attributes.append(attribute);
  156. // 3. Set attribute’s element to element.
  157. attribute.set_owner_element(&associated_element());
  158. }
  159. // https://dom.spec.whatwg.org/#concept-element-attributes-remove
  160. void NamedNodeMap::remove_attribute_at_index(size_t attribute_index)
  161. {
  162. JS::NonnullGCPtr<Attr> attribute = m_attributes.at(attribute_index);
  163. // 1. Handle attribute changes for attribute with attribute’s element, attribute’s value, and null.
  164. VERIFY(attribute->owner_element());
  165. attribute->handle_attribute_changes(*attribute->owner_element(), attribute->value(), {});
  166. // 2. Remove attribute from attribute’s element’s attribute list.
  167. m_attributes.remove(attribute_index);
  168. // 3. Set attribute’s element to null.
  169. attribute->set_owner_element(nullptr);
  170. }
  171. // https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-name
  172. Attr const* NamedNodeMap::remove_attribute(StringView qualified_name)
  173. {
  174. size_t item_index = 0;
  175. // 1. Let attr be the result of getting an attribute given qualifiedName and element.
  176. auto const* attribute = get_attribute(qualified_name, &item_index);
  177. // 2. If attr is non-null, then remove attr.
  178. if (attribute)
  179. remove_attribute_at_index(item_index);
  180. // 3. Return attr.
  181. return attribute;
  182. }
  183. JS::Value NamedNodeMap::item_value(size_t index) const
  184. {
  185. auto const* node = item(index);
  186. if (!node)
  187. return JS::js_undefined();
  188. return node;
  189. }
  190. JS::Value NamedNodeMap::named_item_value(FlyString const& name) const
  191. {
  192. auto const* node = get_named_item(name);
  193. if (!node)
  194. return JS::js_undefined();
  195. return node;
  196. }
  197. }