TextTrackCueList.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/TextTrackCueListPrototype.h>
  9. #include <LibWeb/HTML/EventNames.h>
  10. #include <LibWeb/HTML/TextTrackCueList.h>
  11. namespace Web::HTML {
  12. GC_DEFINE_ALLOCATOR(TextTrackCueList);
  13. TextTrackCueList::TextTrackCueList(JS::Realm& realm)
  14. : DOM::EventTarget(realm, MayInterfereWithIndexedPropertyAccess::Yes)
  15. {
  16. }
  17. TextTrackCueList::~TextTrackCueList() = default;
  18. void TextTrackCueList::initialize(JS::Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. WEB_SET_PROTOTYPE_FOR_INTERFACE(TextTrackCueList);
  22. }
  23. void TextTrackCueList::visit_edges(JS::Cell::Visitor& visitor)
  24. {
  25. Base::visit_edges(visitor);
  26. visitor.visit(m_cues);
  27. }
  28. // https://html.spec.whatwg.org/multipage/media.html#dom-texttrackcuelist-item
  29. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> TextTrackCueList::internal_get_own_property(JS::PropertyKey const& property_name) const
  30. {
  31. // To determine the value of an indexed property for a given index index, the user agent must return the indexth text track cue in the list
  32. // represented by the TextTrackCueList object.
  33. if (property_name.is_number()) {
  34. if (auto index = property_name.as_number(); index < m_cues.size()) {
  35. JS::PropertyDescriptor descriptor;
  36. descriptor.value = m_cues.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-texttrackcuelist-length
  43. size_t TextTrackCueList::length() const
  44. {
  45. // The length attribute must return the number of cues in the list represented by the TextTrackCueList object.
  46. return m_cues.size();
  47. }
  48. // https://html.spec.whatwg.org/multipage/media.html#dom-texttrackcuelist-getcuebyid
  49. GC::Ptr<TextTrackCue> TextTrackCueList::get_cue_by_id(StringView id) const
  50. {
  51. // The getCueById(id) method, when called with an argument other than the empty string, must return the first text track cue in the list
  52. // represented by the TextTrackCueList object whose text track cue identifier is id, if any, or null otherwise. If the argument is the
  53. // empty string, then the method must return null.
  54. if (id.is_empty())
  55. return nullptr;
  56. auto it = m_cues.find_if([&](auto const& cue) {
  57. return cue->id() == id;
  58. });
  59. if (it == m_cues.end())
  60. return nullptr;
  61. return *it;
  62. }
  63. }