NamedNodeMap.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Attribute.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/NamedNodeMap.h>
  9. #include <LibWeb/Namespace.h>
  10. namespace Web::DOM {
  11. NonnullRefPtr<NamedNodeMap> NamedNodeMap::create(Element const& associated_element)
  12. {
  13. return adopt_ref(*new NamedNodeMap(associated_element));
  14. }
  15. NamedNodeMap::NamedNodeMap(Element const& associated_element)
  16. : m_associated_element(associated_element)
  17. {
  18. // Note: To avoid a reference cycle between Element, NamedNodeMap, and Attribute, do not store a
  19. // strong reference to the associated element.
  20. }
  21. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices%E2%91%A3
  22. bool NamedNodeMap::is_supported_property_index(u32 index) const
  23. {
  24. return index < m_attributes.size();
  25. }
  26. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names%E2%91%A0
  27. Vector<String> NamedNodeMap::supported_property_names() const
  28. {
  29. auto associated_element = m_associated_element.strong_ref();
  30. if (!associated_element)
  31. return {};
  32. // 1. Let names be the qualified names of the attributes in this NamedNodeMap object’s attribute list, with duplicates omitted, in order.
  33. Vector<String> names;
  34. names.ensure_capacity(m_attributes.size());
  35. for (auto const& attribute : m_attributes) {
  36. if (!names.contains_slow(attribute.name()))
  37. names.append(attribute.name());
  38. }
  39. // 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:
  40. // FIXME: Handle the second condition, assume it is an HTML document for now.
  41. if (associated_element->namespace_uri() == Namespace::HTML) {
  42. // 1. Let lowercaseName be name, in ASCII lowercase.
  43. // 2. If lowercaseName is not equal to name, remove name from names.
  44. names.remove_all_matching([](auto const& name) { return name != name.to_lowercase(); });
  45. }
  46. // 3. Return names.
  47. return names;
  48. }
  49. // https://dom.spec.whatwg.org/#dom-namednodemap-item
  50. Attribute const* NamedNodeMap::item(u32 index) const
  51. {
  52. // 1. If index is equal to or greater than this’s attribute list’s size, then return null.
  53. if (index >= m_attributes.size())
  54. return nullptr;
  55. // 2. Otherwise, return this’s attribute list[index].
  56. return &m_attributes[index];
  57. }
  58. // https://dom.spec.whatwg.org/#dom-namednodemap-getnameditem
  59. Attribute const* NamedNodeMap::get_named_item(StringView qualified_name) const
  60. {
  61. return get_attribute(qualified_name);
  62. }
  63. // https://dom.spec.whatwg.org/#dom-namednodemap-setnameditem
  64. ExceptionOr<Attribute const*> NamedNodeMap::set_named_item(Attribute& attribute)
  65. {
  66. return set_attribute(attribute);
  67. }
  68. // https://dom.spec.whatwg.org/#dom-namednodemap-removenameditem
  69. ExceptionOr<Attribute const*> NamedNodeMap::remove_named_item(StringView qualified_name)
  70. {
  71. // 1. Let attr be the result of removing an attribute given qualifiedName and element.
  72. auto const* attribute = remove_attribute(qualified_name);
  73. // 2. If attr is null, then throw a "NotFoundError" DOMException.
  74. if (!attribute)
  75. return NotFoundError::create(String::formatted("Attribute with name '{}' not found", qualified_name));
  76. // 3. Return attr.
  77. return nullptr;
  78. }
  79. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
  80. Attribute* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_index)
  81. {
  82. return const_cast<Attribute*>(const_cast<NamedNodeMap const*>(this)->get_attribute(qualified_name, item_index));
  83. }
  84. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
  85. Attribute const* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_index) const
  86. {
  87. auto associated_element = m_associated_element.strong_ref();
  88. if (!associated_element)
  89. return nullptr;
  90. if (item_index)
  91. *item_index = 0;
  92. // 1. If element is in the HTML namespace and its node document is an HTML document, then set qualifiedName to qualifiedName in ASCII lowercase.
  93. // FIXME: Handle the second condition, assume it is an HTML document for now.
  94. Optional<String> qualified_name_lowercase;
  95. if (associated_element->namespace_uri() == Namespace::HTML) {
  96. qualified_name_lowercase = qualified_name.to_lowercase_string();
  97. qualified_name = qualified_name_lowercase->view();
  98. }
  99. // 2. Return the first attribute in element’s attribute list whose qualified name is qualifiedName; otherwise null.
  100. for (auto const& attribute : m_attributes) {
  101. if (attribute.name() == qualified_name)
  102. return &attribute;
  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. auto associated_element = m_associated_element.strong_ref();
  112. if (!associated_element)
  113. return nullptr;
  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("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(Attribute& old_attribute, Attribute& 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. // FIXME: The steps to handle an attribute change deal with mutation records and custom element states.
  140. // Once those are supported, implement these steps: https://dom.spec.whatwg.org/#handle-attribute-changes
  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(Attribute& attribute)
  151. {
  152. // 1. Handle attribute changes for attribute with element, null, and attribute’s value.
  153. // FIXME: The steps to handle an attribute change deal with mutation records and custom element states.
  154. // Once those are supported, implement these steps: https://dom.spec.whatwg.org/#handle-attribute-changes
  155. // 2. Append attribute to element’s attribute list.
  156. m_attributes.append(attribute);
  157. // 3. Set attribute’s element to element.
  158. attribute.set_owner_element(m_associated_element);
  159. }
  160. // https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-name
  161. Attribute const* NamedNodeMap::remove_attribute(StringView qualified_name)
  162. {
  163. size_t item_index = 0;
  164. // 1. Let attr be the result of getting an attribute given qualifiedName and element.
  165. auto const* attribute = get_attribute(qualified_name, &item_index);
  166. // 2. If attr is non-null, then remove attr.
  167. if (attribute)
  168. m_attributes.remove(item_index);
  169. // 3. Return attr.
  170. return attribute;
  171. }
  172. }