aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/script/dom/bindings/text.rs
blob: 9b231d7087de6844578603f914e6caf754b873d4 (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
/* 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::element;
use dom::bindings::node::unwrap;
use dom::bindings::utils;
use dom::bindings::utils::{DOM_OBJECT_SLOT, CacheableWrapper};
use dom::node::{AbstractNode, Comment, Doctype, TextNodeTypeId, CommentNodeTypeId};
use dom::node::{DoctypeNodeTypeId, ScriptView};
use dom::text::Text;

use js::jsapi::{JSFreeOp, JSObject, JSContext};
use js::jsapi::{JS_SetReservedSlot};
use js::glue::{RUST_PRIVATE_TO_JSVAL};
use js::rust::{Compartment, jsobj};

use std::cast;
use std::libc;

extern fn finalize_text(_fop: *JSFreeOp, obj: *JSObject) {
    debug!("text finalize: %?!", obj as uint);
    unsafe {
        let node: AbstractNode<ScriptView> = unwrap(obj);
        let _elem: ~Text = cast::transmute(node.raw_object());
    }
}

extern fn finalize_comment(_fop: *JSFreeOp, obj: *JSObject) {
    debug!("comment finalize: %?!", obj as uint);
    unsafe {
        let node: AbstractNode<ScriptView> = unwrap(obj);
        let _elem: ~Comment = cast::transmute(node.raw_object());
    }
}

extern fn finalize_doctype(_fop: *JSFreeOp, obj: *JSObject) {
    debug!("doctype finalize: %?!", obj as uint);
    unsafe {
        let node: AbstractNode<ScriptView> = unwrap(obj);
        let _elem: ~Doctype<ScriptView> = cast::transmute(node.raw_object());
    }
}

pub fn init(compartment: @mut Compartment) {
    let _ = utils::define_empty_prototype(~"CharacterData", Some(~"Node"), compartment);
    
    let _ = utils::define_empty_prototype(~"TextPrototype",
                                          Some(~"CharacterData"),
                                          compartment);
    let _ = utils::define_empty_prototype(~"CommentPrototype",
                                          Some(~"CharacterData"),
                                          compartment);
    let _ = utils::define_empty_prototype(~"DocumentTypePrototype",
                                          Some(~"Node"),
                                          compartment);

    compartment.register_class(utils::instance_jsclass(~"Text",
                                                       finalize_text,
                                                       element::trace));
    compartment.register_class(utils::instance_jsclass(~"Comment",
                                                       finalize_comment,
                                                       element::trace));
    compartment.register_class(utils::instance_jsclass(~"DocumentType",
                                                       finalize_doctype,
                                                       element::trace));

    
}

pub fn create(cx: *JSContext, node: &mut AbstractNode<ScriptView>) -> jsobj {
    let (proto, instance) = match node.type_id() {
      TextNodeTypeId => (~"TextPrototype", ~"Text"),
      CommentNodeTypeId => (~"CommentPrototype", ~"Comment"),
      DoctypeNodeTypeId => (~"DocumentTypePrototype", ~"DocumentType"),
      _ => fail!(~"text::create only handles textual nodes")
    };

    //XXXjdm the parent should probably be the node parent instead of the global
    //TODO error checking
    let compartment = utils::get_compartment(cx);
    let obj = compartment.new_object_with_proto(instance,
                                                proto,
                                                compartment.global_obj.ptr).unwrap();

    let cache = node.get_wrappercache();
    assert!(cache.get_wrapper().is_null());
    cache.set_wrapper(obj.ptr);

    unsafe {
        let raw_ptr = node.raw_object() as *libc::c_void;
        JS_SetReservedSlot(obj.ptr, DOM_OBJECT_SLOT as u32, RUST_PRIVATE_TO_JSVAL(raw_ptr));
    }

    return obj;
}