Skip to main content

script/dom/testing/
testbindingiterable.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
5// check-tidy: no specs after this line
6
7use dom_struct::dom_struct;
8use js::context::JSContext;
9use js::rust::HandleObject;
10use script_bindings::cell::DomRefCell;
11use script_bindings::domstring::TracedDOMString;
12use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto};
13
14use crate::dom::bindings::codegen::Bindings::TestBindingIterableBinding::TestBindingIterableMethods;
15use crate::dom::bindings::error::Fallible;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::globalscope::GlobalScope;
19
20#[dom_struct]
21pub(crate) struct TestBindingIterable {
22    reflector: Reflector,
23    vals: DomRefCell<Vec<TracedDOMString>>,
24}
25
26impl TestBindingIterable {
27    fn new(
28        cx: &mut JSContext,
29        global: &GlobalScope,
30        proto: Option<HandleObject>,
31    ) -> DomRoot<TestBindingIterable> {
32        reflect_dom_object_with_proto(
33            cx,
34            Box::new(TestBindingIterable {
35                reflector: Reflector::new(),
36                vals: DomRefCell::new(vec![]),
37            }),
38            global,
39            proto,
40        )
41    }
42}
43
44impl TestBindingIterableMethods<crate::DomTypeHolder> for TestBindingIterable {
45    fn Constructor(
46        cx: &mut JSContext,
47        global: &GlobalScope,
48        proto: Option<HandleObject>,
49    ) -> Fallible<DomRoot<TestBindingIterable>> {
50        Ok(TestBindingIterable::new(cx, global, proto))
51    }
52
53    fn Add(&self, v: DOMString) {
54        self.vals.borrow_mut().push(v.traced());
55    }
56    fn Length(&self) -> u32 {
57        self.vals.borrow().len() as u32
58    }
59    fn GetItem(&self, n: u32) -> DOMString {
60        self.IndexedGetter(n).unwrap_or_default()
61    }
62    fn IndexedGetter(&self, n: u32) -> Option<DOMString> {
63        self.vals
64            .borrow()
65            .get(n as usize)
66            .cloned()
67            .map(|domstring| domstring.root())
68    }
69}