TextTrackList.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Realm.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Bindings/TextTrackListPrototype.h>
  9. #include <LibWeb/HTML/EventNames.h>
  10. #include <LibWeb/HTML/TextTrackList.h>
  11. namespace Web::HTML {
  12. GC_DEFINE_ALLOCATOR(TextTrackList);
  13. TextTrackList::TextTrackList(JS::Realm& realm)
  14. : DOM::EventTarget(realm, MayInterfereWithIndexedPropertyAccess::Yes)
  15. {
  16. }
  17. TextTrackList::~TextTrackList() = default;
  18. void TextTrackList::initialize(JS::Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. WEB_SET_PROTOTYPE_FOR_INTERFACE(TextTrackList);
  22. }
  23. void TextTrackList::visit_edges(JS::Cell::Visitor& visitor)
  24. {
  25. Base::visit_edges(visitor);
  26. visitor.visit(m_text_tracks);
  27. }
  28. // https://html.spec.whatwg.org/multipage/media.html#dom-texttracklist-item
  29. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> TextTrackList::internal_get_own_property(const JS::PropertyKey& property_name) const
  30. {
  31. // To determine the value of an indexed property of a TextTrackList object for a given index index, the user
  32. // agent must return the indexth text track in the list represented by the TextTrackList object.
  33. if (property_name.is_number()) {
  34. if (auto index = property_name.as_number(); index < m_text_tracks.size()) {
  35. JS::PropertyDescriptor descriptor;
  36. descriptor.value = m_text_tracks.at(index);
  37. return descriptor;
  38. }
  39. }
  40. return Base::internal_get_own_property(property_name);
  41. }
  42. // https://html.spec.whatwg.org/multipage/media.html#dom-texttracklist-length
  43. size_t TextTrackList::length() const
  44. {
  45. return m_text_tracks.size();
  46. }
  47. // https://html.spec.whatwg.org/multipage/media.html#dom-texttracklist-gettrackbyid
  48. GC::Ptr<TextTrack> TextTrackList::get_track_by_id(StringView id) const
  49. {
  50. // The getTrackById(id) method must return the first TextTrack in the TextTrackList object whose id
  51. // IDL attribute would return a value equal to the value of the id argument.
  52. auto it = m_text_tracks.find_if([&](auto const& text_track) {
  53. return text_track->id() == id;
  54. });
  55. // When no tracks match the given argument, the method must return null.
  56. if (it == m_text_tracks.end())
  57. return nullptr;
  58. return *it;
  59. }
  60. // https://html.spec.whatwg.org/multipage/media.html#handler-texttracklist-onchange
  61. void TextTrackList::set_onchange(WebIDL::CallbackType* event_handler)
  62. {
  63. set_event_handler_attribute(HTML::EventNames::change, event_handler);
  64. }
  65. // https://html.spec.whatwg.org/multipage/media.html#handler-texttracklist-onchange
  66. WebIDL::CallbackType* TextTrackList::onchange()
  67. {
  68. return event_handler_attribute(HTML::EventNames::change);
  69. }
  70. // https://html.spec.whatwg.org/multipage/media.html#handler-texttracklist-onaddtrack
  71. void TextTrackList::set_onaddtrack(WebIDL::CallbackType* event_handler)
  72. {
  73. set_event_handler_attribute(HTML::EventNames::addtrack, event_handler);
  74. }
  75. // https://html.spec.whatwg.org/multipage/media.html#handler-texttracklist-onaddtrack
  76. WebIDL::CallbackType* TextTrackList::onaddtrack()
  77. {
  78. return event_handler_attribute(HTML::EventNames::addtrack);
  79. }
  80. // https://html.spec.whatwg.org/multipage/media.html#handler-texttracklist-onremovetrack
  81. void TextTrackList::set_onremovetrack(WebIDL::CallbackType* event_handler)
  82. {
  83. set_event_handler_attribute(HTML::EventNames::removetrack, event_handler);
  84. }
  85. // https://html.spec.whatwg.org/multipage/media.html#handler-texttracklist-onremovetrack
  86. WebIDL::CallbackType* TextTrackList::onremovetrack()
  87. {
  88. return event_handler_attribute(HTML::EventNames::removetrack);
  89. }
  90. }