script/dom/
documenttype.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;
6
7use crate::dom::bindings::codegen::Bindings::DocumentTypeBinding::DocumentTypeMethods;
8use crate::dom::bindings::codegen::UnionTypes::NodeOrString;
9use crate::dom::bindings::error::ErrorResult;
10use crate::dom::bindings::inheritance::Castable;
11use crate::dom::bindings::root::DomRoot;
12use crate::dom::bindings::str::DOMString;
13use crate::dom::document::Document;
14use crate::dom::node::Node;
15use crate::script_runtime::CanGc;
16
17// https://dom.spec.whatwg.org/#documenttype
18/// The `DOCTYPE` tag.
19#[dom_struct]
20pub(crate) struct DocumentType {
21    node: Node,
22    name: DOMString,
23    public_id: DOMString,
24    system_id: DOMString,
25}
26
27impl DocumentType {
28    fn new_inherited(
29        name: DOMString,
30        public_id: Option<DOMString>,
31        system_id: Option<DOMString>,
32        document: &Document,
33    ) -> DocumentType {
34        DocumentType {
35            node: Node::new_inherited(document),
36            name,
37            public_id: public_id.unwrap_or_default(),
38            system_id: system_id.unwrap_or_default(),
39        }
40    }
41    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
42    pub(crate) fn new(
43        name: DOMString,
44        public_id: Option<DOMString>,
45        system_id: Option<DOMString>,
46        document: &Document,
47        can_gc: CanGc,
48    ) -> DomRoot<DocumentType> {
49        Node::reflect_node(
50            Box::new(DocumentType::new_inherited(
51                name, public_id, system_id, document,
52            )),
53            document,
54            can_gc,
55        )
56    }
57
58    #[inline]
59    pub(crate) fn name(&self) -> &DOMString {
60        &self.name
61    }
62
63    #[inline]
64    pub(crate) fn public_id(&self) -> &DOMString {
65        &self.public_id
66    }
67
68    #[inline]
69    pub(crate) fn system_id(&self) -> &DOMString {
70        &self.system_id
71    }
72}
73
74impl DocumentTypeMethods<crate::DomTypeHolder> for DocumentType {
75    // https://dom.spec.whatwg.org/#dom-documenttype-name
76    fn Name(&self) -> DOMString {
77        self.name.clone()
78    }
79
80    // https://dom.spec.whatwg.org/#dom-documenttype-publicid
81    fn PublicId(&self) -> DOMString {
82        self.public_id.clone()
83    }
84
85    // https://dom.spec.whatwg.org/#dom-documenttype-systemid
86    fn SystemId(&self) -> DOMString {
87        self.system_id.clone()
88    }
89
90    // https://dom.spec.whatwg.org/#dom-childnode-before
91    fn Before(&self, nodes: Vec<NodeOrString>, can_gc: CanGc) -> ErrorResult {
92        self.upcast::<Node>().before(nodes, can_gc)
93    }
94
95    // https://dom.spec.whatwg.org/#dom-childnode-after
96    fn After(&self, nodes: Vec<NodeOrString>, can_gc: CanGc) -> ErrorResult {
97        self.upcast::<Node>().after(nodes, can_gc)
98    }
99
100    // https://dom.spec.whatwg.org/#dom-childnode-replacewith
101    fn ReplaceWith(&self, nodes: Vec<NodeOrString>, can_gc: CanGc) -> ErrorResult {
102        self.upcast::<Node>().replace_with(nodes, can_gc)
103    }
104
105    // https://dom.spec.whatwg.org/#dom-childnode-remove
106    fn Remove(&self, can_gc: CanGc) {
107        self.upcast::<Node>().remove_self(can_gc);
108    }
109}