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    pub(crate) fn new(
42        name: DOMString,
43        public_id: Option<DOMString>,
44        system_id: Option<DOMString>,
45        document: &Document,
46        can_gc: CanGc,
47    ) -> DomRoot<DocumentType> {
48        Node::reflect_node(
49            Box::new(DocumentType::new_inherited(
50                name, public_id, system_id, document,
51            )),
52            document,
53            can_gc,
54        )
55    }
56
57    #[inline]
58    pub(crate) fn name(&self) -> &DOMString {
59        &self.name
60    }
61
62    #[inline]
63    pub(crate) fn public_id(&self) -> &DOMString {
64        &self.public_id
65    }
66
67    #[inline]
68    pub(crate) fn system_id(&self) -> &DOMString {
69        &self.system_id
70    }
71}
72
73impl DocumentTypeMethods<crate::DomTypeHolder> for DocumentType {
74    /// <https://dom.spec.whatwg.org/#dom-documenttype-name>
75    fn Name(&self) -> DOMString {
76        self.name.clone()
77    }
78
79    /// <https://dom.spec.whatwg.org/#dom-documenttype-publicid>
80    fn PublicId(&self) -> DOMString {
81        self.public_id.clone()
82    }
83
84    /// <https://dom.spec.whatwg.org/#dom-documenttype-systemid>
85    fn SystemId(&self) -> DOMString {
86        self.system_id.clone()
87    }
88
89    /// <https://dom.spec.whatwg.org/#dom-childnode-before>
90    fn Before(&self, nodes: Vec<NodeOrString>, can_gc: CanGc) -> ErrorResult {
91        self.upcast::<Node>().before(nodes, can_gc)
92    }
93
94    /// <https://dom.spec.whatwg.org/#dom-childnode-after>
95    fn After(&self, nodes: Vec<NodeOrString>, can_gc: CanGc) -> ErrorResult {
96        self.upcast::<Node>().after(nodes, can_gc)
97    }
98
99    /// <https://dom.spec.whatwg.org/#dom-childnode-replacewith>
100    fn ReplaceWith(&self, nodes: Vec<NodeOrString>, can_gc: CanGc) -> ErrorResult {
101        self.upcast::<Node>().replace_with(nodes, can_gc)
102    }
103
104    /// <https://dom.spec.whatwg.org/#dom-childnode-remove>
105    fn Remove(&self, can_gc: CanGc) {
106        self.upcast::<Node>().remove_self(can_gc);
107    }
108}