HTMLOptionsCollection.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/DOM/ElementFactory.h>
  8. #include <LibWeb/HTML/HTMLOptGroupElement.h>
  9. #include <LibWeb/HTML/HTMLOptionElement.h>
  10. #include <LibWeb/HTML/HTMLOptionsCollection.h>
  11. #include <LibWeb/HTML/HTMLSelectElement.h>
  12. #include <LibWeb/Namespace.h>
  13. #include <LibWeb/WebIDL/DOMException.h>
  14. namespace Web::HTML {
  15. JS_DEFINE_ALLOCATOR(HTMLOptionsCollection);
  16. JS::NonnullGCPtr<HTMLOptionsCollection> HTMLOptionsCollection::create(DOM::ParentNode& root, Function<bool(DOM::Element const&)> filter)
  17. {
  18. return root.heap().allocate<HTMLOptionsCollection>(root.realm(), root, move(filter));
  19. }
  20. HTMLOptionsCollection::HTMLOptionsCollection(DOM::ParentNode& root, Function<bool(DOM::Element const&)> filter)
  21. : DOM::HTMLCollection(root, Scope::Descendants, move(filter))
  22. {
  23. }
  24. HTMLOptionsCollection::~HTMLOptionsCollection() = default;
  25. void HTMLOptionsCollection::initialize(JS::Realm& realm)
  26. {
  27. Base::initialize(realm);
  28. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLOptionsCollection);
  29. }
  30. // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmloptionscollection-length
  31. WebIDL::ExceptionOr<void> HTMLOptionsCollection::set_length(WebIDL::UnsignedLong value)
  32. {
  33. // 1. Let current be the number of nodes represented by the collection.
  34. auto current = static_cast<WebIDL::UnsignedLong>(length());
  35. // 2. If the given value is greater than current, then:
  36. if (value > current) {
  37. // 2.1. If the given value is greater than 100,000, then return.
  38. if (value > 100'000)
  39. return {};
  40. // 2.2. Let n be value − current.
  41. auto n = value - current;
  42. // 2.3. Append n new option elements with no attributes and no child nodes to the select element on which this is rooted.
  43. // Mutation events must be fired as if a DocumentFragment containing the new option elements had been inserted.
  44. auto root_element = root();
  45. for (WebIDL::UnsignedLong i = 0; i < n; i++)
  46. TRY(root_element->append_child(TRY(DOM::create_element(root_element->document(), HTML::TagNames::option, Namespace::HTML))));
  47. }
  48. // 3. If the given value is less than current, then:
  49. if (value < current) {
  50. // 3.1. Let n be current − value.
  51. auto n = current - value;
  52. // 3.2. Remove the last n nodes in the collection from their parent nodes.
  53. for (WebIDL::UnsignedLong i = current - 1; i >= current - n; i--)
  54. this->item(i)->remove();
  55. }
  56. return {};
  57. }
  58. // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmloptionscollection-add
  59. WebIDL::ExceptionOr<void> HTMLOptionsCollection::add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before)
  60. {
  61. auto resolved_element = element.visit(
  62. [](auto& e) -> JS::Handle<HTMLElement> {
  63. return JS::make_handle(static_cast<HTML::HTMLElement&>(*e));
  64. });
  65. JS::GCPtr<DOM::Node> before_element;
  66. if (before.has_value() && before->has<JS::Handle<HTMLElement>>())
  67. before_element = before->get<JS::Handle<HTMLElement>>().ptr();
  68. // 1. If element is an ancestor of the select element on which the HTMLOptionsCollection is rooted, then throw a "HierarchyRequestError" DOMException.
  69. if (resolved_element->is_ancestor_of(root()))
  70. return WebIDL::HierarchyRequestError::create(realm(), "The provided element is an ancestor of the root select element."_fly_string);
  71. // 2. If before is an element, but that element isn't a descendant of the select element on which the HTMLOptionsCollection is rooted, then throw a "NotFoundError" DOMException.
  72. if (before_element && !before_element->is_descendant_of(root()))
  73. return WebIDL::NotFoundError::create(realm(), "The 'before' element is not a descendant of the root select element."_fly_string);
  74. // 3. If element and before are the same element, then return.
  75. if (before_element && (resolved_element.ptr() == before_element.ptr()))
  76. return {};
  77. // 4. If before is a node, then let reference be that node. Otherwise, if before is an integer, and there is a beforeth node in the collection, let reference be that node. Otherwise, let reference be null.
  78. JS::GCPtr<DOM::Node> reference;
  79. if (before_element)
  80. reference = move(before_element);
  81. else if (before.has_value() && before->has<i32>())
  82. reference = item(before->get<i32>());
  83. // 5. If reference is not null, let parent be the parent node of reference. Otherwise, let parent be the select element on which the HTMLOptionsCollection is rooted.
  84. DOM::Node* parent = reference ? reference->parent() : root().ptr();
  85. // 6. Pre-insert element into parent node before reference.
  86. (void)TRY(parent->pre_insert(*resolved_element, reference));
  87. return {};
  88. }
  89. // https://html.spec.whatwg.org/#dom-htmloptionscollection-remove
  90. void HTMLOptionsCollection::remove(WebIDL::Long index)
  91. {
  92. // 1. If the number of nodes represented by the collection is zero, return.
  93. if (length() == 0)
  94. return;
  95. // 2. If index is not a number greater than or equal to 0 and less than the number of nodes represented by the collection, return.
  96. if (index < 0 || static_cast<WebIDL::UnsignedLong>(index) >= length())
  97. return;
  98. // 3. Let element be the indexth element in the collection.
  99. auto* element = this->item(index);
  100. // 4. Remove element from its parent node.
  101. element->remove();
  102. }
  103. // https://html.spec.whatwg.org/#dom-htmloptionscollection-selectedindex
  104. WebIDL::Long HTMLOptionsCollection::selected_index() const
  105. {
  106. // The selectedIndex IDL attribute must act like the identically named attribute
  107. // on the select element on which the HTMLOptionsCollection is rooted.
  108. return verify_cast<HTMLSelectElement>(*root()).selected_index();
  109. }
  110. void HTMLOptionsCollection::set_selected_index(WebIDL::Long index)
  111. {
  112. verify_cast<HTMLSelectElement>(*root()).set_selected_index(index);
  113. }
  114. }