HTMLTrackElement.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/HTMLTrackElementPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/HTML/HTMLTrackElement.h>
  11. #include <LibWeb/HTML/TextTrack.h>
  12. namespace Web::HTML {
  13. GC_DEFINE_ALLOCATOR(HTMLTrackElement);
  14. HTMLTrackElement::HTMLTrackElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  15. : HTMLElement(document, move(qualified_name))
  16. {
  17. m_track = TextTrack::create(document.realm());
  18. }
  19. HTMLTrackElement::~HTMLTrackElement() = default;
  20. void HTMLTrackElement::initialize(JS::Realm& realm)
  21. {
  22. Base::initialize(realm);
  23. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLTrackElement);
  24. }
  25. void HTMLTrackElement::visit_edges(Cell::Visitor& visitor)
  26. {
  27. Base::visit_edges(visitor);
  28. visitor.visit(m_track);
  29. }
  30. void HTMLTrackElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
  31. {
  32. Base::attribute_changed(name, old_value, value, namespace_);
  33. // https://html.spec.whatwg.org/multipage/media.html#sourcing-out-of-band-text-tracks
  34. // As the kind, label, and srclang attributes are set, changed, or removed, the text track must update accordingly, as per the definitions above.
  35. if (name.equals_ignoring_ascii_case("kind"sv)) {
  36. m_track->set_kind(text_track_kind_from_string(value.value_or({})));
  37. } else if (name.equals_ignoring_ascii_case("label"sv)) {
  38. m_track->set_label(value.value_or({}));
  39. } else if (name.equals_ignoring_ascii_case("srclang"sv)) {
  40. m_track->set_language(value.value_or({}));
  41. }
  42. // https://html.spec.whatwg.org/multipage/media.html#dom-texttrack-id
  43. // For tracks that correspond to track elements, the track's identifier is the value of the element's id attribute, if any.
  44. if (name.equals_ignoring_ascii_case("id"sv)) {
  45. m_track->set_id(value.value_or({}));
  46. }
  47. }
  48. // https://html.spec.whatwg.org/multipage/media.html#dom-track-readystate
  49. WebIDL::UnsignedShort HTMLTrackElement::ready_state()
  50. {
  51. // The readyState attribute must return the numeric value corresponding to the text track readiness state of the track element's text track, as defined by the following list:
  52. switch (m_track->readiness_state()) {
  53. case TextTrack::ReadinessState::NotLoaded:
  54. // NONE (numeric value 0)
  55. // The text track not loaded state.
  56. return 0;
  57. case TextTrack::ReadinessState::Loading:
  58. // LOADING (numeric value 1)
  59. // The text track loading state.
  60. return 1;
  61. case TextTrack::ReadinessState::Loaded:
  62. // LOADED (numeric value 2)
  63. // The text track loaded state.
  64. return 2;
  65. case TextTrack::ReadinessState::FailedToLoad:
  66. // ERROR (numeric value 3)
  67. // The text track failed to load state.
  68. return 3;
  69. }
  70. VERIFY_NOT_REACHED();
  71. }
  72. }