DOMStringList.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/DOMStringListPrototype.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/HTML/DOMStringList.h>
  9. #include <LibWeb/WebIDL/ExceptionOr.h>
  10. namespace Web::HTML {
  11. GC_DEFINE_ALLOCATOR(DOMStringList);
  12. GC::Ref<DOMStringList> DOMStringList::create(JS::Realm& realm, Vector<String> list)
  13. {
  14. return realm.create<DOMStringList>(realm, list);
  15. }
  16. DOMStringList::DOMStringList(JS::Realm& realm, Vector<String> list)
  17. : Bindings::PlatformObject(realm)
  18. , m_list(move(list))
  19. {
  20. m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = true };
  21. }
  22. void DOMStringList::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. WEB_SET_PROTOTYPE_FOR_INTERFACE(DOMStringList);
  26. }
  27. // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-domstringlist-length
  28. u32 DOMStringList::length() const
  29. {
  30. // The length getter steps are to return this's associated list's size.
  31. return m_list.size();
  32. }
  33. // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-domstringlist-item
  34. Optional<String> DOMStringList::item(u32 index) const
  35. {
  36. // The item(index) method steps are to return the indexth item in this's associated list, or null if index plus one
  37. // is greater than this's associated list's size.
  38. if (index + 1 > m_list.size())
  39. return {};
  40. return m_list.at(index);
  41. }
  42. // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-domstringlist-contains
  43. bool DOMStringList::contains(StringView string)
  44. {
  45. // The contains(string) method steps are to return true if this's associated list contains string, and false otherwise.
  46. return m_list.contains_slow(string);
  47. }
  48. Optional<JS::Value> DOMStringList::item_value(size_t index) const
  49. {
  50. if (index + 1 > m_list.size())
  51. return {};
  52. return JS::PrimitiveString::create(vm(), m_list.at(index));
  53. }
  54. }