Coverage Report

Created: 2026-09-14 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/HTML/HTMLTrackElement.cpp
Line
Count
Source
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
8
#include <LibWeb/Bindings/HTMLTrackElementPrototype.h>
9
#include <LibWeb/Bindings/Intrinsics.h>
10
#include <LibWeb/DOM/Document.h>
11
#include <LibWeb/HTML/HTMLTrackElement.h>
12
#include <LibWeb/HTML/TextTrack.h>
13
14
namespace Web::HTML {
15
16
JS_DEFINE_ALLOCATOR(HTMLTrackElement);
17
18
HTMLTrackElement::HTMLTrackElement(DOM::Document& document, DOM::QualifiedName qualified_name)
19
0
    : HTMLElement(document, move(qualified_name))
20
0
{
21
0
    m_track = TextTrack::create(document.realm());
22
0
}
23
24
0
HTMLTrackElement::~HTMLTrackElement() = default;
25
26
void HTMLTrackElement::initialize(JS::Realm& realm)
27
0
{
28
0
    Base::initialize(realm);
29
0
    WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLTrackElement);
30
0
}
31
32
void HTMLTrackElement::visit_edges(Cell::Visitor& visitor)
33
0
{
34
0
    Base::visit_edges(visitor);
35
0
    visitor.visit(m_track);
36
0
}
37
38
void HTMLTrackElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value)
39
0
{
40
0
    HTMLElement::attribute_changed(name, old_value, value);
41
42
    // https://html.spec.whatwg.org/multipage/media.html#sourcing-out-of-band-text-tracks
43
    // As the kind, label, and srclang attributes are set, changed, or removed, the text track must update accordingly, as per the definitions above.
44
0
    if (name.equals_ignoring_ascii_case("kind"sv)) {
45
0
        m_track->set_kind(text_track_kind_from_string(value.value_or({})));
46
0
    } else if (name.equals_ignoring_ascii_case("label"sv)) {
47
0
        m_track->set_label(value.value_or({}));
48
0
    } else if (name.equals_ignoring_ascii_case("srclang"sv)) {
49
0
        m_track->set_language(value.value_or({}));
50
0
    }
51
52
    // https://html.spec.whatwg.org/multipage/media.html#dom-texttrack-id
53
    // For tracks that correspond to track elements, the track's identifier is the value of the element's id attribute, if any.
54
0
    if (name.equals_ignoring_ascii_case("id"sv)) {
55
0
        m_track->set_id(value.value_or({}));
56
0
    }
57
0
}
58
59
// https://html.spec.whatwg.org/multipage/media.html#dom-track-readystate
60
WebIDL::UnsignedShort HTMLTrackElement::ready_state()
61
0
{
62
    // 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:
63
0
    switch (m_track->readiness_state()) {
64
0
    case TextTrack::ReadinessState::NotLoaded:
65
        // NONE (numeric value 0)
66
        //    The text track not loaded state.
67
0
        return 0;
68
0
    case TextTrack::ReadinessState::Loading:
69
        // LOADING (numeric value 1)
70
        //    The text track loading state.
71
0
        return 1;
72
0
    case TextTrack::ReadinessState::Loaded:
73
        // LOADED (numeric value 2)
74
        //    The text track loaded state.
75
0
        return 2;
76
0
    case TextTrack::ReadinessState::FailedToLoad:
77
        // ERROR (numeric value 3)
78
        //    The text track failed to load state.
79
0
        return 3;
80
0
    }
81
82
0
    VERIFY_NOT_REACHED();
83
0
}
84
85
}