NamedNodeMap.cpp 8.8 KB

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