aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/devtools.rs
blob: c7a81ff67a60d9de26475dae85036c22185da59a (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
99
100
101
102
103
104
105
106
107
108
109
110
/* 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 devtools_traits;
use devtools_traits::{EvaluateJSReply, NodeInfo, Modification};
use dom::bindings::conversions::FromJSValConvertible;
use dom::bindings::conversions::StringificationBehavior;
use dom::bindings::js::{JSRef, Temporary, OptionalRootable};
use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast};
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
use dom::bindings::codegen::Bindings::DOMRectBinding::{DOMRectMethods};
use dom::bindings::codegen::Bindings::ElementBinding::{ElementMethods};
use dom::node::{Node, NodeHelpers};
use dom::window::{ScriptHelpers};
use dom::element::Element;
use dom::document::DocumentHelpers;
use page::Page;
use servo_msg::constellation_msg::PipelineId;
use script_task::get_page;

use std::sync::mpsc::Sender;
use std::rc::Rc;


pub fn handle_evaluate_js(page: &Rc<Page>, pipeline: PipelineId, eval: String, reply: Sender<EvaluateJSReply>){
    let page = get_page(&*page, pipeline);
    let frame = page.frame();
    let window = frame.as_ref().unwrap().window.root();
    let cx = window.r().get_cx();
    let rval = window.r().evaluate_js_on_global_with_result(eval.as_slice());

    reply.send(if rval.is_undefined() {
        devtools_traits::VoidValue
    } else if rval.is_boolean() {
        devtools_traits::BooleanValue(rval.to_boolean())
    } else if rval.is_double() {
        devtools_traits::NumberValue(FromJSValConvertible::from_jsval(cx, rval, ()).unwrap())
    } else if rval.is_string() {
        //FIXME: use jsstring_to_str when jsval grows to_jsstring
        devtools_traits::StringValue(FromJSValConvertible::from_jsval(cx, rval, StringificationBehavior::Default).unwrap())
    } else if rval.is_null() {
        devtools_traits::NullValue
    } else {
        //FIXME: jsvals don't have an is_int32/is_number yet
        assert!(rval.is_object());
        panic!("object values unimplemented")
    }).unwrap();
}

pub fn handle_get_root_node(page: &Rc<Page>, pipeline: PipelineId, reply: Sender<NodeInfo>) {
    let page = get_page(&*page, pipeline);
    let frame = page.frame();
    let document = frame.as_ref().unwrap().document.root();

    let node: JSRef<Node> = NodeCast::from_ref(document.r());
    reply.send(node.summarize()).unwrap();
}

pub fn handle_get_document_element(page: &Rc<Page>, pipeline: PipelineId, reply: Sender<NodeInfo>) {
    let page = get_page(&*page, pipeline);
    let frame = page.frame();
    let document = frame.as_ref().unwrap().document.root();
    let document_element = document.r().GetDocumentElement().root().unwrap();

    let node: JSRef<Node> = NodeCast::from_ref(document_element.r());
    reply.send(node.summarize()).unwrap();
}

fn find_node_by_unique_id(page: &Rc<Page>, pipeline: PipelineId, node_id: String) -> Temporary<Node> {
    let page = get_page(&*page, pipeline);
    let frame = page.frame();
    let document = frame.as_ref().unwrap().document.root();
    let node: JSRef<Node> = NodeCast::from_ref(document.r());

    for candidate in node.traverse_preorder() {
        if candidate.get_unique_id().as_slice() == node_id.as_slice() {
            return Temporary::from_rooted(candidate);
        }
    }

    panic!("couldn't find node with unique id {}", node_id)
}

pub fn handle_get_children(page: &Rc<Page>, pipeline: PipelineId, node_id: String, reply: Sender<Vec<NodeInfo>>) {
    let parent = find_node_by_unique_id(&*page, pipeline, node_id).root();
    let children = parent.r().children().map(|child| child.summarize()).collect();
    reply.send(children).unwrap();
}

pub fn handle_get_layout(page: &Rc<Page>, pipeline: PipelineId, node_id: String, reply: Sender<(f32, f32)>) {
    let node = find_node_by_unique_id(&*page, pipeline, node_id).root();
    let elem: JSRef<Element> = ElementCast::to_ref(node.r()).expect("should be getting layout of element");
    let rect = elem.GetBoundingClientRect().root();
    reply.send((rect.r().Width(), rect.r().Height())).unwrap();
}

pub fn handle_modify_attribute(page: &Rc<Page>, pipeline: PipelineId, node_id: String, modifications: Vec<Modification>) {
    let node = find_node_by_unique_id(&*page, pipeline, node_id).root();
    let elem: JSRef<Element> = ElementCast::to_ref(node.r()).expect("should be getting layout of element");

    for modification in modifications.iter(){
        match modification.newValue {
            Some(ref string) => {
                let _ = elem.SetAttribute(modification.attributeName.clone(), string.clone());
            },
            None => elem.RemoveAttribute(modification.attributeName.clone()),
        }
    }
}