HTMLOptionsCollection.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/HTML/HTMLOptGroupElement.h>
  8. #include <LibWeb/HTML/HTMLOptionElement.h>
  9. #include <LibWeb/HTML/HTMLOptionsCollection.h>
  10. #include <LibWeb/HTML/HTMLSelectElement.h>
  11. #include <LibWeb/WebIDL/DOMException.h>
  12. namespace Web::HTML {
  13. JS::NonnullGCPtr<HTMLOptionsCollection> HTMLOptionsCollection::create(DOM::ParentNode& root, Function<bool(DOM::Element const&)> filter)
  14. {
  15. return root.heap().allocate<HTMLOptionsCollection>(root.realm(), root, move(filter));
  16. }
  17. HTMLOptionsCollection::HTMLOptionsCollection(DOM::ParentNode& root, Function<bool(DOM::Element const&)> filter)
  18. : DOM::HTMLCollection(root, Scope::Descendants, move(filter))
  19. {
  20. }
  21. HTMLOptionsCollection::~HTMLOptionsCollection() = default;
  22. void HTMLOptionsCollection::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLOptionsCollectionPrototype>(realm, "HTMLOptionsCollection"));
  26. }
  27. // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmloptionscollection-add
  28. WebIDL::ExceptionOr<void> HTMLOptionsCollection::add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before)
  29. {
  30. auto resolved_element = element.visit(
  31. [](auto& e) -> JS::Handle<HTMLElement> {
  32. return JS::make_handle(static_cast<HTML::HTMLElement&>(*e));
  33. });
  34. JS::GCPtr<DOM::Node> before_element;
  35. if (before.has_value() && before->has<JS::Handle<HTMLElement>>())
  36. before_element = before->get<JS::Handle<HTMLElement>>().ptr();
  37. // 1. If element is an ancestor of the select element on which the HTMLOptionsCollection is rooted, then throw a "HierarchyRequestError" DOMException.
  38. if (resolved_element->is_ancestor_of(root()))
  39. return WebIDL::HierarchyRequestError::create(realm(), "The provided element is an ancestor of the root select element."_fly_string);
  40. // 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.
  41. if (before_element && !before_element->is_descendant_of(root()))
  42. return WebIDL::NotFoundError::create(realm(), "The 'before' element is not a descendant of the root select element."_fly_string);
  43. // 3. If element and before are the same element, then return.
  44. if (before_element && (resolved_element.ptr() == before_element.ptr()))
  45. return {};
  46. // 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.
  47. JS::GCPtr<DOM::Node> reference;
  48. if (before_element)
  49. reference = move(before_element);
  50. else if (before.has_value() && before->has<i32>())
  51. reference = item(before->get<i32>());
  52. // 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.
  53. DOM::Node* parent = reference ? reference->parent() : root().ptr();
  54. // 6. Pre-insert element into parent node before reference.
  55. (void)TRY(parent->pre_insert(*resolved_element, reference));
  56. return {};
  57. }
  58. }