aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/devtools.rs
diff options
context:
space:
mode:
authorShanil Puri <shanil.puri@gmail.com>2014-11-11 22:36:52 -0500
committerJosh Matthews <josh@joshmatthews.net>2014-12-03 18:58:44 -0800
commit72a5ae72107493410e447e5ff9cb3bb21d8fe39f (patch)
tree8a85c79881415dbb66b601e7f6a9c739c5dce2e6 /components/script/devtools.rs
parentad9aedac77d094b8524b6170b857499bdc9cd762 (diff)
downloadservo-72a5ae72107493410e447e5ff9cb3bb21d8fe39f.tar.gz
servo-72a5ae72107493410e447e5ff9cb3bb21d8fe39f.zip
Implemeneted ModifyAttribute handler to update DOM elements.
Diffstat (limited to 'components/script/devtools.rs')
-rw-r--r--components/script/devtools.rs106
1 files changed, 106 insertions, 0 deletions
diff --git a/components/script/devtools.rs b/components/script/devtools.rs
new file mode 100644
index 00000000000..8d7a5d35bad
--- /dev/null
+++ b/components/script/devtools.rs
@@ -0,0 +1,106 @@
+/* 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;
+use dom::bindings::conversions::FromJSValConvertible;
+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::{WindowHelpers};
+use dom::element::Element;
+use dom::document::DocumentHelpers;
+use page::Page;
+use servo_msg::constellation_msg::PipelineId;
+use script_task::get_page;
+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.get_cx();
+ let rval = window.evaluate_js_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, conversions::Default).unwrap())
+ } else {
+ //FIXME: jsvals don't have an is_int32/is_number yet
+ assert!(rval.is_object_or_null());
+ panic!("object values unimplemented")
+ });
+}
+
+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);
+ reply.send(node.summarize());
+}
+
+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.GetDocumentElement().root().unwrap();
+
+ let node: JSRef<Node> = NodeCast::from_ref(*document_element);
+ reply.send(node.summarize());
+}
+
+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);
+
+ 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 {:s}", 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.children().map(|child| child.summarize()).collect();
+ reply.send(children);
+}
+
+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).expect("should be getting layout of element");
+ let rect = elem.GetBoundingClientRect().root();
+ reply.send((rect.Width(), rect.Height()));
+}
+
+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).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()),
+ }
+ }
+}