aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/script/dom/documentfragment.rs
blob: 55e782406b3f3de51e34eb5b1c98f7f21e63dd04 (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
/* 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::InheritTypes::{DocumentFragmentDerived, NodeCast};
use dom::bindings::codegen::BindingDeclarations::DocumentFragmentBinding;
use dom::bindings::js::{JS, JSRef, RootCollection, Unrooted};
use dom::bindings::error::Fallible;
use dom::document::Document;
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlcollection::HTMLCollection;
use dom::node::{DocumentFragmentNodeTypeId, Node, window_from_node};
use dom::window::{Window, WindowMethods};

#[deriving(Encodable)]
pub struct DocumentFragment {
    pub node: Node,
}

impl DocumentFragmentDerived for EventTarget {
    fn is_documentfragment(&self) -> bool {
        match self.type_id {
            NodeTargetTypeId(DocumentFragmentNodeTypeId) => true,
            _ => false
        }
    }
}

impl DocumentFragment {
    /// Creates a new DocumentFragment.
    pub fn new_inherited(document: JS<Document>) -> DocumentFragment {
        DocumentFragment {
            node: Node::new_inherited(DocumentFragmentNodeTypeId, document),
        }
    }

    pub fn new(document: &JSRef<Document>) -> Unrooted<DocumentFragment> {
        let node = DocumentFragment::new_inherited(document.unrooted());
        Node::reflect_node(~node, document, DocumentFragmentBinding::Wrap)
    }

    pub fn Constructor(owner: &JSRef<Window>) -> Fallible<Unrooted<DocumentFragment>> {
        let roots = RootCollection::new();
        let document = owner.Document();
        let document = document.root(&roots);

        Ok(DocumentFragment::new(&document.root_ref()))
    }
}

pub trait DocumentFragmentMethods {
    fn Children(&self) -> Unrooted<HTMLCollection>;
}

impl<'a> DocumentFragmentMethods for JSRef<'a, DocumentFragment> {
    fn Children(&self) -> Unrooted<HTMLCollection> {
        let roots = RootCollection::new();
        let window = window_from_node(self).root(&roots);
        HTMLCollection::children(&window.root_ref(), NodeCast::from_ref(self))
    }
}