Coverage Report

Created: 2025-03-04 07:22

/src/serenity/Userland/Libraries/LibWeb/WebVTT/VTTCue.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <LibWeb/Bindings/Intrinsics.h>
8
#include <LibWeb/HTML/Window.h>
9
#include <LibWeb/WebVTT/VTTCue.h>
10
11
namespace Web::WebVTT {
12
13
JS_DEFINE_ALLOCATOR(VTTCue);
14
15
// https://w3c.github.io/webvtt/#dom-vttcue-vttcue
16
WebIDL::ExceptionOr<JS::NonnullGCPtr<VTTCue>> VTTCue::construct_impl(JS::Realm& realm, double start_time, double end_time, String const& text)
17
0
{
18
    // 1. Create a new WebVTT cue. Let cue be that WebVTT cue.
19
0
    auto cue = realm.heap().allocate<VTTCue>(realm, realm, nullptr);
20
21
    // 2. Let cue’s text track cue start time be the value of the startTime argument.
22
0
    cue->m_start_time = start_time;
23
24
    // 3. If the value of the endTime argument is negative Infinity or a Not-a-Number (NaN) value, then throw a TypeError exception.
25
    //    Otherwise, let cue’s text track cue end time be the value of the endTime argument.
26
0
    if (end_time == -AK::Infinity<double> || isnan(end_time))
27
0
        return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "End time is negative infinity or NaN"_string };
28
0
    cue->m_end_time = end_time;
29
30
    // 4. Let cue’s cue text be the value of the text argument, and let the rules for extracting the chapter title be the WebVTT rules
31
    //    for extracting the chapter title.
32
0
    cue->m_text = text;
33
    // FIXME: let the rules for extracting the chapter title be the WebVTT rules for extracting the chapter title.
34
35
    // 5. Let cue’s text track cue identifier be the empty string.
36
0
    cue->m_identifier = ""_string;
37
38
    // 6. Let cue’s text track cue pause-on-exit flag be false.
39
0
    cue->m_pause_on_exit = false;
40
41
    // 7. Let cue’s WebVTT cue region be null.
42
0
    cue->m_region = nullptr;
43
44
    // 8. Let cue’s WebVTT cue writing direction be horizontal.
45
0
    cue->m_writing_direction = WritingDirection::Horizontal;
46
47
    // 9. Let cue’s WebVTT cue snap-to-lines flag be true.
48
0
    cue->m_snap_to_lines = true;
49
50
    // FIXME: 10. Let cue’s WebVTT cue line be auto.
51
52
    // 11. Let cue’s WebVTT cue line alignment be start alignment.
53
0
    cue->m_line_alignment = Bindings::LineAlignSetting::Start;
54
55
    // FIXME: 12. Let cue’s WebVTT cue position be auto.
56
57
    // 13. Let cue’s WebVTT cue position alignment be auto.
58
0
    cue->m_position_alignment = Bindings::PositionAlignSetting::Auto;
59
60
    // 14. Let cue’s WebVTT cue size be 100.
61
0
    cue->m_size = 100;
62
63
    // 15. Let cue’s WebVTT cue text alignment be center alignment.
64
0
    cue->m_text_alignment = Bindings::AlignSetting::Center;
65
66
    // 16. Return the VTTCue object representing cue.
67
0
    return cue;
68
0
}
69
70
VTTCue::VTTCue(JS::Realm& realm, JS::GCPtr<HTML::TextTrack> track)
71
0
    : HTML::TextTrackCue(realm, track)
72
0
{
73
0
}
74
75
void VTTCue::initialize(JS::Realm& realm)
76
0
{
77
0
    Base::initialize(realm);
78
0
    WEB_SET_PROTOTYPE_FOR_INTERFACE(VTTCue);
79
0
}
80
81
void VTTCue::visit_edges(Visitor& visitor)
82
0
{
83
0
    Base::visit_edges(visitor);
84
0
    visitor.visit(m_region);
85
0
}
86
87
// https://w3c.github.io/webvtt/#dom-vttcue-vertical
88
Bindings::DirectionSetting VTTCue::vertical() const
89
0
{
90
0
    switch (m_writing_direction) {
91
0
    case WritingDirection::Horizontal:
92
0
        return Bindings::DirectionSetting::Empty;
93
0
    case WritingDirection::VerticalGrowingLeft:
94
0
        return Bindings::DirectionSetting::Rl;
95
0
    case WritingDirection::VerticalGrowingRight:
96
0
        return Bindings::DirectionSetting::Lr;
97
0
    }
98
0
    VERIFY_NOT_REACHED();
99
0
}
100
101
// https://w3c.github.io/webvtt/#dom-vttcue-vertical
102
void VTTCue::set_vertical(Bindings::DirectionSetting vertical)
103
0
{
104
0
    switch (vertical) {
105
0
    case Bindings::DirectionSetting::Empty:
106
0
        m_writing_direction = WritingDirection::Horizontal;
107
0
        break;
108
0
    case Bindings::DirectionSetting::Rl:
109
0
        m_writing_direction = WritingDirection::VerticalGrowingLeft;
110
0
        break;
111
0
    case Bindings::DirectionSetting::Lr:
112
0
        m_writing_direction = WritingDirection::VerticalGrowingRight;
113
0
        break;
114
0
    }
115
0
}
116
117
// https://w3c.github.io/webvtt/#cue-computed-position-alignment
118
Bindings::PositionAlignSetting VTTCue::computed_position_alignment()
119
0
{
120
    // 1. If the WebVTT cue position alignment is not auto, then return the value of the WebVTT cue position alignment and abort these
121
    //    steps.
122
0
    if (m_position_alignment != Bindings::PositionAlignSetting::Auto)
123
0
        return m_position_alignment;
124
125
    // 2. If the WebVTT cue text alignment is left, return line-left and abort these steps.
126
0
    if (m_text_alignment == Bindings::AlignSetting::Left)
127
0
        return Bindings::PositionAlignSetting::LineLeft;
128
129
    // 3. If the WebVTT cue text alignment is right, return line-right and abort these steps.
130
0
    if (m_text_alignment == Bindings::AlignSetting::Right)
131
0
        return Bindings::PositionAlignSetting::LineRight;
132
133
    // FIXME: 4. If the WebVTT cue text alignment is start, return line-left if the base direction of the cue text is left-to-right, line-right
134
    //    otherwise.
135
136
    // FIXME: 5. If the WebVTT cue text alignment is end, return line-right if the base direction of the cue text is left-to-right, line-left
137
    //    otherwise.
138
139
    // 6. Otherwise, return center.
140
0
    return Bindings::PositionAlignSetting::Center;
141
0
}
142
143
}