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};
12use crate::dom::bindings::str::DOMString;
13use crate::dom::texttrackcue::TextTrackCue;
14use crate::dom::window::Window;
15
16#[dom_struct]
17pub(crate) struct TextTrackCueList {
18    reflector_: Reflector,
19    dom_cues: DomRefCell<Vec<Dom<TextTrackCue>>>,
20}
21
22impl TextTrackCueList {
23    pub(crate) fn new_inherited(cues: &[&TextTrackCue]) -> TextTrackCueList {
24        TextTrackCueList {
25            reflector_: Reflector::new(),
26            dom_cues: DomRefCell::new(cues.iter().map(|g| Dom::from_ref(&**g)).collect()),
27        }
28    }
29
30    pub(crate) fn new(
31        cx: &mut JSContext,
32        window: &Window,
33        cues: &[&TextTrackCue],
34    ) -> DomRoot<TextTrackCueList> {
35        reflect_dom_object_with_cx(Box::new(TextTrackCueList::new_inherited(cues)), window, cx)
36    }
37
38    pub(crate) fn item(&self, idx: usize) -> Option<DomRoot<TextTrackCue>> {
39        self.dom_cues
40            .borrow()
41            .get(idx)
42            .map(|t| DomRoot::from_ref(&**t))
43    }
44
45    pub(crate) fn find(&self, cue: &TextTrackCue) -> Option<usize> {
46        self.dom_cues
47            .borrow()
48            .iter()
49            .enumerate()
50            .find(|(_, c)| **c == cue)
51            .map(|(i, _)| i)
52    }
53
54    pub(crate) fn add(&self, cue: &TextTrackCue) {
55        // Only add a cue if it does not exist in the list
56        if self.find(cue).is_none() {
57            self.dom_cues.borrow_mut().push(Dom::from_ref(cue));
58        }
59    }
60
61    pub(crate) fn remove(&self, idx: usize) {
62        self.dom_cues.borrow_mut().remove(idx);
63    }
64
65    pub(crate) fn empty(&self) {
66        self.dom_cues.borrow_mut().clear();
67    }
68}
69
70impl TextTrackCueListMethods<crate::DomTypeHolder> for TextTrackCueList {
71    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcuelist-length>
72    fn Length(&self) -> u32 {
73        self.dom_cues.borrow().len() as u32
74    }
75
76    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcuelist-item>
77    fn IndexedGetter(&self, idx: u32) -> Option<DomRoot<TextTrackCue>> {
78        self.item(idx as usize)
79    }
80
81    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcuelist-getcuebyid>
82    fn GetCueById(&self, id: DOMString) -> Option<DomRoot<TextTrackCue>> {
83        if id.is_empty() {
84            None
85        } else {
86            self.dom_cues
87                .borrow()
88                .iter()
89                .find(|cue| cue.id() == id)
90                .map(|t| DomRoot::from_ref(&**t))
91        }
92    }
93}