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