Skip to main content

script/dom/webvtt/
texttrackcue.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::Cell;
6use std::cmp::Ordering;
7
8use dom_struct::dom_struct;
9use script_bindings::cell::DomRefCell;
10
11use crate::dom::bindings::codegen::Bindings::TextTrackCueBinding::TextTrackCueMethods;
12use crate::dom::bindings::num::Finite;
13use crate::dom::bindings::root::{DomRoot, MutNullableDom};
14use crate::dom::bindings::str::DOMString;
15use crate::dom::eventtarget::EventTarget;
16use crate::dom::texttrack::TextTrack;
17use crate::dom::webvtt::rules_for_rendering::RulesForUpdatingTheTextTrackRendering;
18
19#[dom_struct]
20pub(crate) struct TextTrackCue {
21    eventtarget: EventTarget,
22    /// <https://html.spec.whatwg.org/multipage/#text-track-cue-identifier>
23    id: DomRefCell<DOMString>,
24    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-track>
25    text_track: MutNullableDom<TextTrack>,
26    /// <https://html.spec.whatwg.org/multipage/#text-track-cue-start-time>
27    start_time: Cell<f64>,
28    /// <https://html.spec.whatwg.org/multipage/#text-track-cue-end-time>
29    end_time: Cell<f64>,
30    /// <https://html.spec.whatwg.org/multipage/#text-track-cue-pause-on-exit-flag>
31    pause_on_exit: Cell<bool>,
32    /// <https://html.spec.whatwg.org/multipage/#text-track-cue-active-flag>
33    active: Cell<bool>,
34    /// <https://html.spec.whatwg.org/multipage/#text-track-cue-order>
35    /// > in the order they were last added to their respective
36    /// > text track list of cues, oldest first
37    initial_index_in_list: Cell<usize>,
38    /// <https://html.spec.whatwg.org/multipage/#rules-for-updating-the-text-track-rendering>
39    rules_for_updating_the_text_track_rendering: RulesForUpdatingTheTextTrackRendering,
40}
41
42impl TextTrackCue {
43    pub(crate) fn new_inherited(
44        id: DOMString,
45        start_time: f64,
46        end_time: f64,
47        text_track: Option<&TextTrack>,
48        rules_for_updating_the_text_track_rendering: RulesForUpdatingTheTextTrackRendering,
49    ) -> TextTrackCue {
50        TextTrackCue {
51            eventtarget: EventTarget::new_inherited(),
52            id: DomRefCell::new(id),
53            text_track: MutNullableDom::new(text_track),
54            start_time: Cell::new(start_time),
55            end_time: Cell::new(end_time),
56            pause_on_exit: Cell::new(false),
57            active: Default::default(),
58            initial_index_in_list: Default::default(),
59            rules_for_updating_the_text_track_rendering,
60        }
61    }
62
63    pub(crate) fn id(&self) -> DOMString {
64        self.id.borrow().clone()
65    }
66
67    pub(crate) fn get_text_track(&self) -> Option<DomRoot<TextTrack>> {
68        self.text_track.get()
69    }
70
71    pub(crate) fn set_text_track(&self, text_track: Option<&TextTrack>) {
72        self.text_track.set(text_track);
73    }
74
75    pub(crate) fn start_time(&self) -> f64 {
76        self.start_time.get()
77    }
78
79    pub(crate) fn end_time(&self) -> f64 {
80        self.end_time.get()
81    }
82
83    pub(crate) fn is_active(&self) -> bool {
84        self.active.get()
85    }
86
87    pub(crate) fn set_active(&self, active: bool) {
88        self.active.set(active)
89    }
90
91    pub(crate) fn set_initial_index_in_list(&self, initial_index_in_list: usize) {
92        self.initial_index_in_list.set(initial_index_in_list);
93    }
94
95    /// <https://html.spec.whatwg.org/multipage/#rules-for-updating-the-text-track-rendering>
96    pub(crate) fn rules_for_updating_the_text_track_rendering(
97        &self,
98    ) -> RulesForUpdatingTheTextTrackRendering {
99        // https://html.spec.whatwg.org/multipage/#text-track-model:rules-for-updating-the-text-track-rendering
100        // > A text track cue is associated with rules for updating the text track rendering,
101        // > as defined by the specification for the specific kind of text track cue.
102        // > These rules are used specifically when the object representing the cue
103        // > is added to a TextTrack object using the addCue() method.
104        self.rules_for_updating_the_text_track_rendering
105    }
106}
107
108impl TextTrackCueMethods<crate::DomTypeHolder> for TextTrackCue {
109    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-id>
110    fn Id(&self) -> DOMString {
111        self.id()
112    }
113
114    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-id>
115    fn SetId(&self, value: DOMString) {
116        *self.id.borrow_mut() = value;
117    }
118
119    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-track>
120    fn GetTrack(&self) -> Option<DomRoot<TextTrack>> {
121        self.get_text_track()
122    }
123
124    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-starttime>
125    fn StartTime(&self) -> Finite<f64> {
126        Finite::wrap(self.start_time.get())
127    }
128
129    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-starttime>
130    fn SetStartTime(&self, value: Finite<f64>) {
131        self.start_time.set(*value);
132        if let Some(text_track) = self.text_track.get() {
133            text_track.sort_cue_list();
134        }
135    }
136
137    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-endtime>
138    fn EndTime(&self) -> Finite<f64> {
139        Finite::wrap(self.end_time.get())
140    }
141
142    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-endtime>
143    fn SetEndTime(&self, value: Finite<f64>) {
144        self.end_time.set(*value);
145        if let Some(text_track) = self.text_track.get() {
146            text_track.sort_cue_list();
147        }
148    }
149
150    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-pauseonexit>
151    fn PauseOnExit(&self) -> bool {
152        self.pause_on_exit.get()
153    }
154
155    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-pauseonexit>
156    fn SetPauseOnExit(&self, value: bool) {
157        self.pause_on_exit.set(value);
158    }
159
160    // https://html.spec.whatwg.org/multipage/#handler-texttrackcue-onenter
161    event_handler!(enter, GetOnenter, SetOnenter);
162
163    // https://html.spec.whatwg.org/multipage/#handler-texttrackcue-onexit
164    event_handler!(exit, GetOnexit, SetOnexit);
165}
166
167impl PartialOrd for TextTrackCue {
168    #[inline]
169    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
170        Some(self.cmp(other))
171    }
172}
173
174impl Ord for TextTrackCue {
175    /// <https://html.spec.whatwg.org/multipage/#text-track-cue-order>
176    fn cmp(&self, other: &TextTrackCue) -> Ordering {
177        // > cues must be sorted by their start time, earliest first;
178        self.start_time
179            .get()
180            .total_cmp(&other.start_time.get())
181            .then_with(|| {
182                // > then, any cues with the same start time must be sorted by their end time, latest first;
183                self.end_time
184                    .get()
185                    .total_cmp(&other.end_time.get())
186                    .reverse()
187                    .then_with(|| {
188                        // > and finally, any cues with identical end times must be sorted in the order
189                        // > they were last added to their respective text track list of cues,
190                        // > oldest first (so e.g. for cues from a WebVTT file,
191                        // > that would initially be the order in which the cues were listed in the file).
192                        self.initial_index_in_list
193                            .get()
194                            .cmp(&other.initial_index_in_list.get())
195                    })
196            })
197    }
198}