HTMLFormControlsCollection.cpp 3.3 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/HTMLFormControlsCollectionPrototype.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/DOM/Element.h>
  9. #include <LibWeb/DOM/HTMLCollection.h>
  10. #include <LibWeb/DOM/ParentNode.h>
  11. #include <LibWeb/HTML/HTMLFormControlsCollection.h>
  12. #include <LibWeb/HTML/RadioNodeList.h>
  13. namespace Web::HTML {
  14. GC_DEFINE_ALLOCATOR(HTMLFormControlsCollection);
  15. GC::Ref<HTMLFormControlsCollection> HTMLFormControlsCollection::create(DOM::ParentNode& root, Scope scope, Function<bool(DOM::Element const&)> filter)
  16. {
  17. return root.realm().create<HTMLFormControlsCollection>(root, scope, move(filter));
  18. }
  19. HTMLFormControlsCollection::HTMLFormControlsCollection(DOM::ParentNode& root, Scope scope, Function<bool(DOM::Element const&)> filter)
  20. : DOM::HTMLCollection(root, scope, move(filter))
  21. {
  22. }
  23. HTMLFormControlsCollection::~HTMLFormControlsCollection() = default;
  24. void HTMLFormControlsCollection::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLFormControlsCollection);
  28. }
  29. // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmlformcontrolscollection-nameditem
  30. Variant<Empty, DOM::Element*, GC::Root<RadioNodeList>> HTMLFormControlsCollection::named_item_or_radio_node_list(FlyString const& name) const
  31. {
  32. // 1. If name is the empty string, return null and stop the algorithm.
  33. if (name.is_empty())
  34. return {};
  35. // 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.
  36. // 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.
  37. DOM::Element* matching_element = nullptr;
  38. bool multiple_matching = false;
  39. auto collection = collect_matching_elements();
  40. for (auto const& element : collection) {
  41. if (element->id() != name && element->name() != name)
  42. continue;
  43. if (matching_element) {
  44. multiple_matching = true;
  45. break;
  46. }
  47. matching_element = element;
  48. }
  49. if (!matching_element)
  50. return {};
  51. if (!multiple_matching)
  52. return matching_element;
  53. // 4. Otherwise, create a new RadioNodeList object representing a live view of the HTMLFormControlsCollection object, further filtered so that the only nodes in the
  54. // 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
  55. // order. Return that RadioNodeList object.
  56. return GC::make_root(RadioNodeList::create(realm(), root(), DOM::LiveNodeList::Scope::Descendants, [name](auto const& node) {
  57. if (!is<DOM::Element>(node))
  58. return false;
  59. auto const& element = verify_cast<DOM::Element>(node);
  60. return element.id() == name || element.name() == name;
  61. }));
  62. }
  63. JS::Value HTMLFormControlsCollection::named_item_value(FlyString const& name) const
  64. {
  65. return named_item_or_radio_node_list(name).visit(
  66. [](Empty) -> JS::Value { return JS::js_undefined(); },
  67. [](auto const& value) -> JS::Value { return value; });
  68. }
  69. }