/* 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 dom_struct::dom_struct; use itertools::Itertools; use js::context::JSContext; use script_bindings::reflector::{Reflector, reflect_dom_object}; use crate::dom::bindings::codegen::Bindings::DOMStringListBinding::DOMStringListMethods; use crate::dom::bindings::root::DomRoot; use crate::dom::bindings::str::DOMString; use crate::dom::globalscope::GlobalScope; #[dom_struct] pub(crate) struct DOMStringList { reflector_: Reflector, strings: Vec, } impl DOMStringList { pub(crate) fn new_inherited(strings: Vec) -> DOMStringList { DOMStringList { reflector_: Reflector::new(), strings, } } pub(crate) fn new( cx: &mut JSContext, global: &GlobalScope, strings: Vec, ) -> DomRoot { reflect_dom_object(cx, Box::new(DOMStringList::new_inherited(strings)), global) } /// pub(crate) fn new_sorted<'a>( cx: &mut JSContext, global: &GlobalScope, strings: impl IntoIterator, ) -> DomRoot { let sorted = strings .into_iter() .map(|dom_string| dom_string.str().encode_utf16().collect::>()) .sorted_unstable() .map(|utf16_str| { String::from_utf16(utf16_str.as_slice()) .expect("can't convert object store name from utf16 back to utf8") .into() }) .collect(); Self::new(cx, global, sorted) } } // https://html.spec.whatwg.org/multipage/#domstringlist impl DOMStringListMethods for DOMStringList { /// fn Length(&self) -> u32 { self.strings.len() as u32 } /// fn Item(&self, index: u32) -> Option { self.strings.get(index as usize).cloned() } /// fn Contains(&self, string: DOMString) -> bool { self.strings.contains(&string) } // check-tidy: no specs after this line fn IndexedGetter(&self, index: u32) -> Option { self.Item(index) } }