HTMLFormControlsCollection.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/HTMLFormControlsCollection.h>
  11. #include <LibWeb/DOM/ParentNode.h>
  12. namespace Web::DOM {
  13. JS_DEFINE_ALLOCATOR(HTMLFormControlsCollection);
  14. JS::NonnullGCPtr<HTMLFormControlsCollection> HTMLFormControlsCollection::create(ParentNode& root, Scope scope, Function<bool(Element const&)> filter)
  15. {
  16. return root.heap().allocate<HTMLFormControlsCollection>(root.realm(), root, scope, move(filter));
  17. }
  18. HTMLFormControlsCollection::HTMLFormControlsCollection(ParentNode& root, Scope scope, Function<bool(Element const&)> filter)
  19. : HTMLCollection(root, scope, move(filter))
  20. {
  21. }
  22. HTMLFormControlsCollection::~HTMLFormControlsCollection() = default;
  23. void HTMLFormControlsCollection::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLFormControlsCollection);
  27. }
  28. // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmlformcontrolscollection-nameditem
  29. Variant<Empty, Element*, JS::Handle<RadioNodeList>> HTMLFormControlsCollection::named_item_or_radio_node_list(FlyString const& name) const
  30. {
  31. // 1. If name is the empty string, return null and stop the algorithm.
  32. if (name.is_empty())
  33. return {};
  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() != 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](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() == name;
  60. }));
  61. }
  62. 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. }