Skip to main content

script/dom/attributes/
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;
6use itertools::Itertools;
7use js::context::JSContext;
8use script_bindings::domstring::TracedDOMString;
9use script_bindings::reflector::{Reflector, reflect_dom_object};
10
11use crate::dom::bindings::codegen::Bindings::DOMStringListBinding::DOMStringListMethods;
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::bindings::str::DOMString;
14use crate::dom::globalscope::GlobalScope;
15
16#[dom_struct]
17pub(crate) struct DOMStringList {
18    reflector_: Reflector,
19    strings: Vec<TracedDOMString>,
20}
21
22impl DOMStringList {
23    pub(crate) fn new_inherited(strings: Vec<DOMString>) -> DOMStringList {
24        DOMStringList {
25            reflector_: Reflector::new(),
26            strings: strings
27                .into_iter()
28                .map(|domstring| domstring.traced())
29                .collect(),
30        }
31    }
32
33    pub(crate) fn new(
34        cx: &mut JSContext,
35        global: &GlobalScope,
36        strings: Vec<DOMString>,
37    ) -> DomRoot<DOMStringList> {
38        reflect_dom_object(cx, Box::new(DOMStringList::new_inherited(strings)), global)
39    }
40
41    /// <https://www.w3.org/TR/IndexedDB-3/#sorted-name-list>
42    pub(crate) fn new_sorted<'a>(
43        cx: &mut JSContext,
44        global: &GlobalScope,
45        strings: impl IntoIterator<Item = &'a TracedDOMString>,
46    ) -> DomRoot<DOMStringList> {
47        let sorted = strings
48            .into_iter()
49            .map(|dom_string| dom_string.str().encode_utf16().collect::<Vec<u16>>())
50            .sorted_unstable()
51            .map(|utf16_str| {
52                String::from_utf16(utf16_str.as_slice())
53                    .expect("can't convert object store name from utf16 back to utf8")
54                    .into()
55            })
56            .collect();
57        Self::new(cx, global, sorted)
58    }
59}
60
61// https://html.spec.whatwg.org/multipage/#domstringlist
62impl DOMStringListMethods<crate::DomTypeHolder> for DOMStringList {
63    /// <https://html.spec.whatwg.org/multipage/#dom-domstringlist-length>
64    fn Length(&self) -> u32 {
65        self.strings.len() as u32
66    }
67
68    /// <https://html.spec.whatwg.org/multipage/#dom-domstringlist-item>
69    fn Item(&self, index: u32) -> Option<DOMString> {
70        self.strings
71            .get(index as usize)
72            .cloned()
73            .map(|domstring| domstring.root())
74    }
75
76    /// <https://html.spec.whatwg.org/multipage/#dom-domstringlist-contains>
77    fn Contains(&self, string: DOMString) -> bool {
78        self.strings.contains(&string)
79    }
80
81    // check-tidy: no specs after this line
82    fn IndexedGetter(&self, index: u32) -> Option<DOMString> {
83        self.Item(index)
84    }
85}