script/dom/
domstringlist.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::codegen::Bindings::DOMStringListBinding::DOMStringListMethods;
8use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
9use crate::dom::bindings::root::DomRoot;
10use crate::dom::bindings::str::DOMString;
11use crate::dom::globalscope::GlobalScope;
12use crate::script_runtime::CanGc;
13
14#[dom_struct]
15pub(crate) struct DOMStringList {
16    reflector_: Reflector,
17    strings: Vec<DOMString>,
18}
19
20impl DOMStringList {
21    pub(crate) fn new_inherited(strings: Vec<DOMString>) -> DOMStringList {
22        DOMStringList {
23            reflector_: Reflector::new(),
24            strings,
25        }
26    }
27
28    pub(crate) fn new(
29        global: &GlobalScope,
30        strings: Vec<DOMString>,
31        can_gc: CanGc,
32    ) -> DomRoot<DOMStringList> {
33        reflect_dom_object(
34            Box::new(DOMStringList::new_inherited(strings)),
35            global,
36            can_gc,
37        )
38    }
39}
40
41// https://html.spec.whatwg.org/multipage/#domstringlist
42impl DOMStringListMethods<crate::DomTypeHolder> for DOMStringList {
43    // https://html.spec.whatwg.org/multipage/#dom-domstringlist-length
44    fn Length(&self) -> u32 {
45        self.strings.len() as u32
46    }
47
48    // https://html.spec.whatwg.org/multipage/#dom-domstringlist-item
49    fn Item(&self, index: u32) -> Option<DOMString> {
50        self.strings.get(index as usize).cloned()
51    }
52
53    // https://html.spec.whatwg.org/multipage/#dom-domstringlist-contains
54    fn Contains(&self, string: DOMString) -> bool {
55        self.strings.contains(&string)
56    }
57
58    // check-tidy: no specs after this line
59    fn IndexedGetter(&self, index: u32) -> Option<DOMString> {
60        self.Item(index)
61    }
62}