script/dom/
staticrange.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 js::rust::HandleObject;
7
8use crate::dom::abstractrange::AbstractRange;
9use crate::dom::bindings::codegen::Bindings::StaticRangeBinding::{
10    StaticRangeInit, StaticRangeMethods,
11};
12use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
13use crate::dom::bindings::error::{Error, Fallible};
14use crate::dom::bindings::inheritance::NodeTypeId;
15use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::document::Document;
18use crate::dom::node::Node;
19use crate::dom::window::Window;
20use crate::script_runtime::CanGc;
21
22#[dom_struct]
23pub(crate) struct StaticRange {
24    abstract_range: AbstractRange,
25}
26
27impl StaticRange {
28    fn new_inherited(
29        start_container: &Node,
30        start_offset: u32,
31        end_container: &Node,
32        end_offset: u32,
33    ) -> StaticRange {
34        StaticRange {
35            abstract_range: AbstractRange::new_inherited(
36                start_container,
37                start_offset,
38                end_container,
39                end_offset,
40            ),
41        }
42    }
43    pub(crate) fn new_with_doc(
44        document: &Document,
45        proto: Option<HandleObject>,
46        init: &StaticRangeInit,
47        can_gc: CanGc,
48    ) -> DomRoot<StaticRange> {
49        StaticRange::new_with_proto(document, proto, init, can_gc)
50    }
51
52    pub(crate) fn new_with_proto(
53        document: &Document,
54        proto: Option<HandleObject>,
55        init: &StaticRangeInit,
56        can_gc: CanGc,
57    ) -> DomRoot<StaticRange> {
58        reflect_dom_object_with_proto(
59            Box::new(StaticRange::new_inherited(
60                &init.startContainer,
61                init.startOffset,
62                &init.endContainer,
63                init.endOffset,
64            )),
65            document.window(),
66            proto,
67            can_gc,
68        )
69    }
70}
71
72impl StaticRangeMethods<crate::DomTypeHolder> for StaticRange {
73    /// <https://dom.spec.whatwg.org/#dom-staticrange-staticrange>
74    #[allow(non_snake_case)]
75    fn Constructor(
76        window: &Window,
77        proto: Option<HandleObject>,
78        can_gc: CanGc,
79        init: &StaticRangeInit,
80    ) -> Fallible<DomRoot<StaticRange>> {
81        match init.startContainer.type_id() {
82            NodeTypeId::DocumentType | NodeTypeId::Attr => {
83                return Err(Error::InvalidNodeType);
84            },
85            _ => (),
86        }
87        match init.endContainer.type_id() {
88            NodeTypeId::DocumentType | NodeTypeId::Attr => {
89                return Err(Error::InvalidNodeType);
90            },
91            _ => (),
92        }
93        let document = window.Document();
94        Ok(StaticRange::new_with_doc(&document, proto, init, can_gc))
95    }
96}