HTMLFormControlsCollection.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/DOM/Element.h>
  8. #include <LibWeb/DOM/HTMLCollection.h>
  9. #include <LibWeb/DOM/HTMLFormControlsCollection.h>
  10. #include <LibWeb/DOM/ParentNode.h>
  11. namespace Web::DOM {
  12. JS_DEFINE_ALLOCATOR(HTMLFormControlsCollection);
  13. JS::NonnullGCPtr<HTMLFormControlsCollection> HTMLFormControlsCollection::create(ParentNode& root, Scope scope, Function<bool(Element const&)> filter)
  14. {
  15. return root.heap().allocate<HTMLFormControlsCollection>(root.realm(), root, scope, move(filter));
  16. }
  17. HTMLFormControlsCollection::HTMLFormControlsCollection(ParentNode& root, Scope scope, Function<bool(Element const&)> filter)
  18. : HTMLCollection(root, scope, move(filter))
  19. {
  20. }
  21. HTMLFormControlsCollection::~HTMLFormControlsCollection() = default;
  22. void HTMLFormControlsCollection::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLFormControlsCollectionPrototype>(realm, "HTMLFormControlsCollection"_fly_string));
  26. }
  27. // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmlformcontrolscollection-nameditem
  28. Variant<Empty, Element*, JS::Handle<RadioNodeList>> HTMLFormControlsCollection::named_item_or_radio_node_list(FlyString const& name) const
  29. {
  30. // 1. If name is the empty string, return null and stop the algorithm.
  31. if (name.is_empty())
  32. return {};
  33. auto const deprecated_name = name.to_deprecated_fly_string();
  34. // 2. If, at the time the method is called, there is exactly one node in the collection that has either an id attribute or a name attribute equal to name, then return that node and stop the algorithm.
  35. // 3. Otherwise, if there are no nodes in the collection that have either an id attribute or a name attribute equal to name, then return null and stop the algorithm.
  36. Element* matching_element = nullptr;
  37. bool multiple_matching = false;
  38. auto collection = collect_matching_elements();
  39. for (auto const& element : collection) {
  40. if (element->id() != name && element->name() != deprecated_name)
  41. continue;
  42. if (matching_element) {
  43. multiple_matching = true;
  44. break;
  45. }
  46. matching_element = element;
  47. }
  48. if (!matching_element)
  49. return {};
  50. if (!multiple_matching)
  51. return matching_element;
  52. // 4. Otherwise, create a new RadioNodeList object representing a live view of the HTMLFormControlsCollection object, further filtered so that the only nodes in the
  53. // RadioNodeList object are those that have either an id attribute or a name attribute equal to name. The nodes in the RadioNodeList object must be sorted in tree
  54. // order. Return that RadioNodeList object.
  55. return JS::make_handle(RadioNodeList::create(realm(), root(), LiveNodeList::Scope::Descendants, [name, deprecated_name](Node const& node) {
  56. if (!is<Element>(node))
  57. return false;
  58. auto const& element = verify_cast<Element>(node);
  59. return element.id() == name || element.name() == deprecated_name;
  60. }));
  61. }
  62. WebIDL::ExceptionOr<JS::Value> HTMLFormControlsCollection::named_item_value(FlyString const& name) const
  63. {
  64. return named_item_or_radio_node_list(name).visit(
  65. [](Empty) -> JS::Value { return JS::js_undefined(); },
  66. [](auto const& value) -> JS::Value { return value; });
  67. }
  68. }