HTMLFormControlsCollection.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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::NonnullGCPtr<HTMLFormControlsCollection> HTMLFormControlsCollection::create(ParentNode& root, Scope scope, Function<bool(Element const&)> filter)
  13. {
  14. return root.heap().allocate<HTMLFormControlsCollection>(root.realm(), root, scope, move(filter));
  15. }
  16. HTMLFormControlsCollection::HTMLFormControlsCollection(ParentNode& root, Scope scope, Function<bool(Element const&)> filter)
  17. : HTMLCollection(root, scope, move(filter))
  18. {
  19. }
  20. HTMLFormControlsCollection::~HTMLFormControlsCollection() = default;
  21. void HTMLFormControlsCollection::initialize(JS::Realm& realm)
  22. {
  23. Base::initialize(realm);
  24. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLFormControlsCollectionPrototype>(realm, "HTMLFormControlsCollection"));
  25. }
  26. // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmlformcontrolscollection-nameditem
  27. Variant<Empty, Element*, JS::Handle<RadioNodeList>> HTMLFormControlsCollection::named_item_or_radio_node_list(FlyString const& name)
  28. {
  29. // 1. If name is the empty string, return null and stop the algorithm.
  30. if (name.is_empty())
  31. return {};
  32. auto const deprecated_name = name.to_deprecated_fly_string();
  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->attribute(HTML::AttributeNames::id) != deprecated_name && element->name() != deprecated_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, [deprecated_name](Node const& node) {
  55. if (!is<Element>(node))
  56. return false;
  57. auto const& element = verify_cast<Element>(node);
  58. return element.attribute(HTML::AttributeNames::id) == deprecated_name || element.name() == deprecated_name;
  59. }));
  60. }
  61. }