diff options
author | Dan Robertson <dan@dlrobertson.com> | 2018-12-08 04:00:38 +0000 |
---|---|---|
committer | Dan Robertson <dan@dlrobertson.com> | 2018-12-11 20:14:00 +0000 |
commit | 62a9bfa0c52c86f5ea35703529e6d23c4b06ade6 (patch) | |
tree | 4c349d27944a14727f14629b21d3f69506d2920f /components/script/dom/texttrackcuelist.rs | |
parent | 92962de76c3400e46ff329e66eff238ac076ed38 (diff) | |
download | servo-62a9bfa0c52c86f5ea35703529e6d23c4b06ade6.tar.gz servo-62a9bfa0c52c86f5ea35703529e6d23c4b06ade6.zip |
script: Create structures for TextTrack API
Fill out the basics for the WebIDLs for the following:
- TextTrack
- TextTrackCue
- TextTrackCueList
- TextTrackList
Diffstat (limited to 'components/script/dom/texttrackcuelist.rs')
-rw-r--r-- | components/script/dom/texttrackcuelist.rs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/components/script/dom/texttrackcuelist.rs b/components/script/dom/texttrackcuelist.rs new file mode 100644 index 00000000000..bd66027bb07 --- /dev/null +++ b/components/script/dom/texttrackcuelist.rs @@ -0,0 +1,91 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +use crate::dom::bindings::cell::DomRefCell; +use crate::dom::bindings::codegen::Bindings::TextTrackCueListBinding::{ + self, TextTrackCueListMethods, +}; +use crate::dom::bindings::reflector::{reflect_dom_object, Reflector}; +use crate::dom::bindings::root::{Dom, DomRoot}; +use crate::dom::bindings::str::DOMString; +use crate::dom::texttrackcue::TextTrackCue; +use crate::dom::window::Window; +use dom_struct::dom_struct; + +#[dom_struct] +pub struct TextTrackCueList { + reflector_: Reflector, + dom_cues: DomRefCell<Vec<Dom<TextTrackCue>>>, +} + +impl TextTrackCueList { + pub fn new_inherited(cues: &[&TextTrackCue]) -> TextTrackCueList { + TextTrackCueList { + reflector_: Reflector::new(), + dom_cues: DomRefCell::new(cues.iter().map(|g| Dom::from_ref(&**g)).collect()), + } + } + + pub fn new(window: &Window, cues: &[&TextTrackCue]) -> DomRoot<TextTrackCueList> { + reflect_dom_object( + Box::new(TextTrackCueList::new_inherited(cues)), + window, + TextTrackCueListBinding::Wrap, + ) + } + + pub fn item(&self, idx: usize) -> Option<DomRoot<TextTrackCue>> { + self.dom_cues + .borrow() + .get(idx) + .map(|t| DomRoot::from_ref(&**t)) + } + + pub fn find(&self, cue: &TextTrackCue) -> Option<usize> { + self.dom_cues + .borrow() + .iter() + .enumerate() + .filter(|(_, c)| **c == cue) + .next() + .map(|(i, _)| i) + } + + pub fn add(&self, cue: &TextTrackCue) { + // Only add a cue if it does not exist in the list + if self.find(cue).is_none() { + self.dom_cues.borrow_mut().push(Dom::from_ref(cue)); + } + } + + pub fn remove(&self, idx: usize) { + self.dom_cues.borrow_mut().remove(idx); + } +} + +impl TextTrackCueListMethods for TextTrackCueList { + // https://html.spec.whatwg.org/multipage/#dom-texttrackcuelist-length + fn Length(&self) -> u32 { + self.dom_cues.borrow().len() as u32 + } + + // https://html.spec.whatwg.org/multipage/#dom-texttrackcuelist-item + fn IndexedGetter(&self, idx: u32) -> Option<DomRoot<TextTrackCue>> { + self.item(idx as usize) + } + + // https://html.spec.whatwg.org/multipage/#dom-texttrackcuelist-getcuebyid + fn GetCueById(&self, id: DOMString) -> Option<DomRoot<TextTrackCue>> { + if id.is_empty() { + None + } else { + self.dom_cues + .borrow() + .iter() + .filter(|cue| cue.id() == id) + .next() + .map(|t| DomRoot::from_ref(&**t)) + } + } +} |