aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/script/dom/domparser.rs
blob: 1dec0025e8af804d15fa05bf2f92c963e200d506 (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
/* 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::DOMParserBinding;
use dom::bindings::codegen::DOMParserBinding::SupportedTypeValues::{Text_html, Text_xml};
use dom::bindings::utils::{Reflector, Reflectable, reflect_dom_object};
use dom::bindings::utils::Fallible;
use dom::bindings::utils::FailureUnknown;
use dom::document::{AbstractDocument, Document};
use dom::htmldocument::HTMLDocument;
use dom::window::Window;
use servo_util::str::DOMString;

pub struct DOMParser {
    owner: @mut Window, //XXXjdm Document instead?
    reflector_: Reflector
}

impl DOMParser {
    pub fn new_inherited(owner: @mut Window) -> DOMParser {
        DOMParser {
            owner: owner,
            reflector_: Reflector::new()
        }
    }

    pub fn new(owner: @mut Window) -> @mut DOMParser {
        reflect_dom_object(@mut DOMParser::new_inherited(owner), owner,
                           DOMParserBinding::Wrap)
    }

    pub fn Constructor(owner: @mut Window) -> Fallible<@mut DOMParser> {
        Ok(DOMParser::new(owner))
    }

    pub fn ParseFromString(&self,
                           _s: DOMString,
                           ty: DOMParserBinding::SupportedType)
                           -> Fallible<AbstractDocument> {
        match ty {
            Text_html => {
                Ok(HTMLDocument::new(self.owner, None))
            }
            Text_xml => {
                Document::Constructor(self.owner)
            }
            _ => {
                Err(FailureUnknown)
            }
        }
    }
}

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

    fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector {
        &mut self.reflector_
    }
}