script/dom/
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;
6
7use crate::dom::bindings::cell::DomRefCell;
8use crate::dom::bindings::codegen::Bindings::TextTrackCueListBinding::TextTrackCueListMethods;
9use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
10use crate::dom::bindings::root::{Dom, DomRoot};
11use crate::dom::bindings::str::DOMString;
12use crate::dom::texttrackcue::TextTrackCue;
13use crate::dom::window::Window;
14use crate::script_runtime::CanGc;
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        window: &Window,
32        cues: &[&TextTrackCue],
33        can_gc: CanGc,
34    ) -> DomRoot<TextTrackCueList> {
35        reflect_dom_object(
36            Box::new(TextTrackCueList::new_inherited(cues)),
37            window,
38            can_gc,
39        )
40    }
41
42    pub(crate) fn item(&self, idx: usize) -> Option<DomRoot<TextTrackCue>> {
43        self.dom_cues
44            .borrow()
45            .get(idx)
46            .map(|t| DomRoot::from_ref(&**t))
47    }
48
49    pub(crate) fn find(&self, cue: &TextTrackCue) -> Option<usize> {
50        self.dom_cues
51            .borrow()
52            .iter()
53            .enumerate()
54            .find(|(_, c)| **c == cue)
55            .map(|(i, _)| i)
56    }
57
58    pub(crate) fn add(&self, cue: &TextTrackCue) {
59        // Only add a cue if it does not exist in the list
60        if self.find(cue).is_none() {
61            self.dom_cues.borrow_mut().push(Dom::from_ref(cue));
62        }
63    }
64
65    pub(crate) fn remove(&self, idx: usize) {
66        self.dom_cues.borrow_mut().remove(idx);
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}