script/dom/
filelist.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::slice::Iter;
6
7use dom_struct::dom_struct;
8
9use crate::dom::bindings::codegen::Bindings::FileListBinding::FileListMethods;
10use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
11use crate::dom::bindings::root::{Dom, DomRoot};
12use crate::dom::file::File;
13use crate::dom::window::Window;
14use crate::script_runtime::CanGc;
15
16// https://w3c.github.io/FileAPI/#dfn-filelist
17#[dom_struct]
18pub(crate) struct FileList {
19    reflector_: Reflector,
20    list: Vec<Dom<File>>,
21}
22
23impl FileList {
24    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
25    fn new_inherited(files: Vec<Dom<File>>) -> FileList {
26        FileList {
27            reflector_: Reflector::new(),
28            list: files,
29        }
30    }
31
32    pub(crate) fn new(
33        window: &Window,
34        files: Vec<DomRoot<File>>,
35        can_gc: CanGc,
36    ) -> DomRoot<FileList> {
37        reflect_dom_object(
38            Box::new(FileList::new_inherited(
39                files.iter().map(|r| Dom::from_ref(&**r)).collect(),
40            )),
41            window,
42            can_gc,
43        )
44    }
45
46    pub(crate) fn iter_files(&self) -> Iter<'_, Dom<File>> {
47        self.list.iter()
48    }
49}
50
51impl FileListMethods<crate::DomTypeHolder> for FileList {
52    /// <https://w3c.github.io/FileAPI/#dfn-length>
53    fn Length(&self) -> u32 {
54        self.list.len() as u32
55    }
56
57    /// <https://w3c.github.io/FileAPI/#dfn-item>
58    fn Item(&self, index: u32) -> Option<DomRoot<File>> {
59        if (index as usize) < self.list.len() {
60            Some(DomRoot::from_ref(&*(self.list[index as usize])))
61        } else {
62            None
63        }
64    }
65
66    // check-tidy: no specs after this line
67    fn IndexedGetter(&self, index: u32) -> Option<DomRoot<File>> {
68        self.Item(index)
69    }
70}