HTMLFormControlsCollection.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLFormControlsCollection);
  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. // 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.
  34. // 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.
  35. Element* matching_element = nullptr;
  36. bool multiple_matching = false;
  37. auto collection = collect_matching_elements();
  38. for (auto const& element : collection) {
  39. if (element->id() != name && element->name() != name)
  40. continue;
  41. if (matching_element) {
  42. multiple_matching = true;
  43. break;
  44. }
  45. matching_element = element;
  46. }
  47. if (!matching_element)
  48. return {};
  49. if (!multiple_matching)
  50. return matching_element;
  51. // 4. Otherwise, create a new RadioNodeList object representing a live view of the HTMLFormControlsCollection object, further filtered so that the only nodes in the
  52. // 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
  53. // order. Return that RadioNodeList object.
  54. return JS::make_handle(RadioNodeList::create(realm(), root(), LiveNodeList::Scope::Descendants, [name](Node const& node) {
  55. if (!is<Element>(node))
  56. return false;
  57. auto const& element = verify_cast<Element>(node);
  58. return element.id() == name || element.name() == name;
  59. }));
  60. }
  61. WebIDL::ExceptionOr<JS::Value> HTMLFormControlsCollection::named_item_value(FlyString const& name) const
  62. {
  63. return named_item_or_radio_node_list(name).visit(
  64. [](Empty) -> JS::Value { return JS::js_undefined(); },
  65. [](auto const& value) -> JS::Value { return value; });
  66. }
  67. }