Skip to main content

script/dom/webvtt/
texttrackcuelist.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 dom_struct::dom_struct;
6use js::context::JSContext;
7use script_bindings::cell::DomRefCell;
8use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
9
10use crate::dom::bindings::codegen::Bindings::TextTrackCueListBinding::TextTrackCueListMethods;
11use crate::dom::bindings::root::{Dom, DomRoot, MutDom};
12use crate::dom::bindings::str::DOMString;
13use crate::dom::texttrack::TextTrack;
14use crate::dom::texttrackcue::TextTrackCue;
15use crate::dom::window::Window;
16
17#[dom_struct]
18pub(crate) struct TextTrackCueList {
19    reflector_: Reflector,
20    text_track: MutDom<TextTrack>,
21    dom_cues: DomRefCell<Vec<Dom<TextTrackCue>>>,
22}
23
24impl TextTrackCueList {
25    pub(crate) fn new_inherited(
26        text_track: &TextTrack,
27        cues: &[&TextTrackCue],
28    ) -> TextTrackCueList {
29        TextTrackCueList {
30            reflector_: Reflector::new(),
31            text_track: MutDom::new(text_track),
32            dom_cues: DomRefCell::new(cues.iter().map(|g| Dom::from_ref(&**g)).collect()),
33        }
34    }
35
36    pub(crate) fn new(
37        cx: &mut JSContext,
38        text_track: &TextTrack,
39        window: &Window,
40        cues: &[&TextTrackCue],
41    ) -> DomRoot<TextTrackCueList> {
42        reflect_dom_object_with_cx(
43            Box::new(TextTrackCueList::new_inherited(text_track, cues)),
44            window,
45            cx,
46        )
47    }
48
49    pub(crate) fn item(&self, idx: usize) -> Option<DomRoot<TextTrackCue>> {
50        self.dom_cues
51            .borrow()
52            .get(idx)
53            .map(|t| DomRoot::from_ref(&**t))
54    }
55
56    pub(crate) fn find(&self, cue: &TextTrackCue) -> Option<usize> {
57        self.dom_cues
58            .borrow()
59            .iter()
60            .enumerate()
61            .find(|(_, c)| **c == cue)
62            .map(|(i, _)| i)
63    }
64
65    pub(crate) fn add(&self, cx: &mut JSContext, cue: &TextTrackCue) {
66        // Only add a cue if it does not exist in the list
67        if self.find(cue).is_none() {
68            self.dom_cues.borrow_mut().push(Dom::from_ref(cue));
69            if let Some(track_list) = self.text_track.get().track_list() {
70                track_list.notify_media_element_for_added_cue(cx, cue);
71            }
72        }
73    }
74
75    pub(crate) fn remove(&self, idx: usize) {
76        self.dom_cues.borrow_mut().remove(idx);
77    }
78
79    pub(crate) fn empty(&self) {
80        self.dom_cues.borrow_mut().clear();
81    }
82
83    pub(crate) fn cues(&self) -> Vec<DomRoot<TextTrackCue>> {
84        self.dom_cues
85            .borrow()
86            .clone()
87            .into_iter()
88            .map(|track| track.as_rooted())
89            .collect()
90    }
91}
92
93impl TextTrackCueListMethods<crate::DomTypeHolder> for TextTrackCueList {
94    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcuelist-length>
95    fn Length(&self) -> u32 {
96        self.dom_cues.borrow().len() as u32
97    }
98
99    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcuelist-item>
100    fn IndexedGetter(&self, idx: u32) -> Option<DomRoot<TextTrackCue>> {
101        self.item(idx as usize)
102    }
103
104    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcuelist-getcuebyid>
105    fn GetCueById(&self, id: DOMString) -> Option<DomRoot<TextTrackCue>> {
106        if id.is_empty() {
107            None
108        } else {
109            self.dom_cues
110                .borrow()
111                .iter()
112                .find(|cue| cue.id() == id)
113                .map(|t| DomRoot::from_ref(&**t))
114        }
115    }
116}