NamedNodeMap.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  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& associated_element)
  12. {
  13. return adopt_ref(*new NamedNodeMap(associated_element));
  14. }
  15. NamedNodeMap::NamedNodeMap(Element& associated_element)
  16. : RefCountForwarder(associated_element)
  17. {
  18. }
  19. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices%E2%91%A3
  20. bool NamedNodeMap::is_supported_property_index(u32 index) const
  21. {
  22. return index < m_attributes.size();
  23. }
  24. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names%E2%91%A0
  25. Vector<String> NamedNodeMap::supported_property_names() const
  26. {
  27. // 1. Let names be the qualified names of the attributes in this NamedNodeMap object’s attribute list, with duplicates omitted, in order.
  28. Vector<String> names;
  29. names.ensure_capacity(m_attributes.size());
  30. for (auto const& attribute : m_attributes) {
  31. if (!names.contains_slow(attribute.name()))
  32. names.append(attribute.name());
  33. }
  34. // 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:
  35. // FIXME: Handle the second condition, assume it is an HTML document for now.
  36. if (associated_element().namespace_uri() == Namespace::HTML) {
  37. // 1. Let lowercaseName be name, in ASCII lowercase.
  38. // 2. If lowercaseName is not equal to name, remove name from names.
  39. names.remove_all_matching([](auto const& name) { return name != name.to_lowercase(); });
  40. }
  41. // 3. Return names.
  42. return names;
  43. }
  44. // https://dom.spec.whatwg.org/#dom-namednodemap-item
  45. Attribute const* NamedNodeMap::item(u32 index) const
  46. {
  47. // 1. If index is equal to or greater than this’s attribute list’s size, then return null.
  48. if (index >= m_attributes.size())
  49. return nullptr;
  50. // 2. Otherwise, return this’s attribute list[index].
  51. return &m_attributes[index];
  52. }
  53. // https://dom.spec.whatwg.org/#dom-namednodemap-getnameditem
  54. Attribute const* NamedNodeMap::get_named_item(StringView qualified_name) const
  55. {
  56. return get_attribute(qualified_name);
  57. }
  58. // https://dom.spec.whatwg.org/#dom-namednodemap-setnameditem
  59. ExceptionOr<Attribute const*> NamedNodeMap::set_named_item(Attribute& attribute)
  60. {
  61. return set_attribute(attribute);
  62. }
  63. // https://dom.spec.whatwg.org/#dom-namednodemap-removenameditem
  64. ExceptionOr<Attribute const*> NamedNodeMap::remove_named_item(StringView qualified_name)
  65. {
  66. // 1. Let attr be the result of removing an attribute given qualifiedName and element.
  67. auto const* attribute = remove_attribute(qualified_name);
  68. // 2. If attr is null, then throw a "NotFoundError" DOMException.
  69. if (!attribute)
  70. return NotFoundError::create(String::formatted("Attribute with name '{}' not found", qualified_name));
  71. // 3. Return attr.
  72. return nullptr;
  73. }
  74. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
  75. Attribute* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_index)
  76. {
  77. return const_cast<Attribute*>(const_cast<NamedNodeMap const*>(this)->get_attribute(qualified_name, item_index));
  78. }
  79. // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
  80. Attribute const* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_index) const
  81. {
  82. if (item_index)
  83. *item_index = 0;
  84. // 1. If element is in the HTML namespace and its node document is an HTML document, then set qualifiedName to qualifiedName in ASCII lowercase.
  85. // FIXME: Handle the second condition, assume it is an HTML document for now.
  86. bool compare_as_lowercase = associated_element().namespace_uri() == Namespace::HTML;
  87. // 2. Return the first attribute in element’s attribute list whose qualified name is qualifiedName; otherwise null.
  88. for (auto const& attribute : m_attributes) {
  89. if (compare_as_lowercase) {
  90. if (attribute.name().equals_ignoring_case(qualified_name))
  91. return &attribute;
  92. } else {
  93. if (attribute.name() == qualified_name)
  94. return &attribute;
  95. }
  96. if (item_index)
  97. ++(*item_index);
  98. }
  99. return nullptr;
  100. }
  101. // https://dom.spec.whatwg.org/#concept-element-attributes-set
  102. ExceptionOr<Attribute const*> NamedNodeMap::set_attribute(Attribute& attribute)
  103. {
  104. // 1. If attr’s element is neither null nor element, throw an "InUseAttributeError" DOMException.
  105. if ((attribute.owner_element() != nullptr) && (attribute.owner_element() != &associated_element()))
  106. return InUseAttributeError::create("Attribute must not already be in use"sv);
  107. // 2. Let oldAttr be the result of getting an attribute given attr’s namespace, attr’s local name, and element.
  108. // FIXME: When getNamedItemNS is implemented, use that instead.
  109. size_t old_attribute_index = 0;
  110. auto* old_attribute = get_attribute(attribute.local_name(), &old_attribute_index);
  111. // 3. If oldAttr is attr, return attr.
  112. if (old_attribute == &attribute)
  113. return &attribute;
  114. // 4. If oldAttr is non-null, then replace oldAttr with attr.
  115. if (old_attribute) {
  116. replace_attribute(*old_attribute, attribute, old_attribute_index);
  117. }
  118. // 5. Otherwise, append attr to element.
  119. else {
  120. append_attribute(attribute);
  121. }
  122. // 6. Return oldAttr.
  123. return old_attribute;
  124. }
  125. // https://dom.spec.whatwg.org/#concept-element-attributes-replace
  126. void NamedNodeMap::replace_attribute(Attribute& old_attribute, Attribute& new_attribute, size_t old_attribute_index)
  127. {
  128. // 1. Handle attribute changes for oldAttr with oldAttr’s element, oldAttr’s value, and newAttr’s value.
  129. // FIXME: The steps to handle an attribute change deal with mutation records and custom element states.
  130. // Once those are supported, implement these steps: https://dom.spec.whatwg.org/#handle-attribute-changes
  131. // 2. Replace oldAttr by newAttr in oldAttr’s element’s attribute list.
  132. m_attributes.remove(old_attribute_index);
  133. m_attributes.insert(old_attribute_index, new_attribute);
  134. // 3. Set newAttr’s element to oldAttr’s element.
  135. new_attribute.set_owner_element(old_attribute.owner_element());
  136. // 4 .Set oldAttr’s element to null.
  137. old_attribute.set_owner_element(nullptr);
  138. }
  139. // https://dom.spec.whatwg.org/#concept-element-attributes-append
  140. void NamedNodeMap::append_attribute(Attribute& attribute)
  141. {
  142. // 1. Handle attribute changes for attribute with element, null, and attribute’s value.
  143. // FIXME: The steps to handle an attribute change deal with mutation records and custom element states.
  144. // Once those are supported, implement these steps: https://dom.spec.whatwg.org/#handle-attribute-changes
  145. // 2. Append attribute to element’s attribute list.
  146. m_attributes.append(attribute);
  147. // 3. Set attribute’s element to element.
  148. attribute.set_owner_element(&associated_element());
  149. }
  150. // https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-name
  151. Attribute const* NamedNodeMap::remove_attribute(StringView qualified_name)
  152. {
  153. size_t item_index = 0;
  154. // 1. Let attr be the result of getting an attribute given qualifiedName and element.
  155. auto const* attribute = get_attribute(qualified_name, &item_index);
  156. // 2. If attr is non-null, then remove attr.
  157. if (attribute)
  158. m_attributes.remove(item_index);
  159. // 3. Return attr.
  160. return attribute;
  161. }
  162. }