aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/documenttype.rs
blob: 683b1c56ae05a4d1eccf3f0f3bc31bb470ec9b20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::Bindings::DocumentTypeBinding;
use dom::bindings::codegen::Bindings::DocumentTypeBinding::DocumentTypeMethods;
use dom::bindings::codegen::InheritTypes::{DocumentTypeDerived, NodeCast};
use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector};
use dom::document::Document;
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::node::{Node, DoctypeNodeTypeId, NodeHelpers};
use servo_util::str::DOMString;

/// The `DOCTYPE` tag.
#[jstraceable]
#[must_root]
#[privatize]
pub struct DocumentType {
    node: Node,
    name: DOMString,
    public_id: DOMString,
    system_id: DOMString,
}

impl DocumentTypeDerived for EventTarget {
    fn is_documenttype(&self) -> bool {
        *self.type_id() == NodeTargetTypeId(DoctypeNodeTypeId)
    }
}

impl DocumentType {
    fn new_inherited(name: DOMString,
                         public_id: Option<DOMString>,
                         system_id: Option<DOMString>,
                         document: JSRef<Document>)
            -> DocumentType {
        DocumentType {
            node: Node::new_inherited(DoctypeNodeTypeId, document),
            name: name,
            public_id: public_id.unwrap_or("".to_string()),
            system_id: system_id.unwrap_or("".to_string())
        }
    }
    #[allow(unrooted_must_root)]
    pub fn new(name: DOMString,
               public_id: Option<DOMString>,
               system_id: Option<DOMString>,
               document: JSRef<Document>)
               -> Temporary<DocumentType> {
        let documenttype = DocumentType::new_inherited(name,
                                                       public_id,
                                                       system_id,
                                                       document);
        Node::reflect_node(box documenttype, document, DocumentTypeBinding::Wrap)
    }

    #[inline]
    pub fn name<'a>(&'a self) -> &'a DOMString {
        &self.name
    }

    #[inline]
    pub fn public_id<'a>(&'a self) -> &'a DOMString {
        &self.public_id
    }

    #[inline]
    pub fn system_id<'a>(&'a self) -> &'a DOMString {
        &self.system_id
    }
}

impl<'a> DocumentTypeMethods for JSRef<'a, DocumentType> {
    fn Name(self) -> DOMString {
        self.name.clone()
    }

    fn PublicId(self) -> DOMString {
        self.public_id.clone()
    }

    fn SystemId(self) -> DOMString {
        self.system_id.clone()
    }

    // http://dom.spec.whatwg.org/#dom-childnode-remove
    fn Remove(self) {
        let node: JSRef<Node> = NodeCast::from_ref(self);
        node.remove_self();
    }
}

impl Reflectable for DocumentType {
    fn reflector<'a>(&'a self) -> &'a Reflector {
        self.node.reflector()
    }
}