diff options
author | George Roman <george.roman.99@gmail.com> | 2019-08-12 00:51:25 +0300 |
---|---|---|
committer | George Roman <george.roman.99@gmail.com> | 2019-08-18 15:30:55 +0300 |
commit | d7b9fede99eaaf854e78a2e544595ea8439baecf (patch) | |
tree | 242c6ebcd7bbbc280953fe6e5b46c29bec308cba /components/script/webdriver_handlers.rs | |
parent | 6a637ceffbe34474ea4b657b08acc0966dc8a088 (diff) | |
download | servo-d7b9fede99eaaf854e78a2e544595ea8439baecf.tar.gz servo-d7b9fede99eaaf854e78a2e544595ea8439baecf.zip |
Return ErrorStatus from webdriver_handlers
Diffstat (limited to 'components/script/webdriver_handlers.rs')
-rw-r--r-- | components/script/webdriver_handlers.rs | 696 |
1 files changed, 375 insertions, 321 deletions
diff --git a/components/script/webdriver_handlers.rs b/components/script/webdriver_handlers.rs index 182f110c0c8..22d3e56fcaf 100644 --- a/components/script/webdriver_handlers.rs +++ b/components/script/webdriver_handlers.rs @@ -33,7 +33,7 @@ use crate::dom::nodelist::NodeList; use crate::dom::window::Window; use crate::dom::xmlserializer::XMLSerializer; use crate::script_runtime::JSContext as SafeJSContext; -use crate::script_thread::Documents; +use crate::script_thread::{Documents, ScriptThread}; use cookie::Cookie; use euclid::default::{Point2D, Rect, Size2D}; use hyper_serde::Serde; @@ -52,18 +52,28 @@ use script_traits::webdriver_msg::{ }; use servo_url::ServoUrl; use webdriver::common::WebElement; +use webdriver::error::ErrorStatus; fn find_node_by_unique_id( documents: &Documents, pipeline: PipelineId, node_id: String, -) -> Option<DomRoot<Node>> { - documents.find_document(pipeline).and_then(|document| { +) -> Result<DomRoot<Node>, ErrorStatus> { + match documents.find_document(pipeline).and_then(|document| { document .upcast::<Node>() .traverse_preorder(ShadowIncluding::Yes) - .find(|candidate| candidate.unique_id() == node_id) - }) + .find(|node| node.unique_id() == node_id) + }) { + Some(node) => Ok(node), + None => { + if ScriptThread::has_node_id(&node_id) { + Err(ErrorStatus::StaleElementReference) + } else { + Err(ErrorStatus::NoSuchElement) + } + }, + } } fn matching_links<'a>( @@ -92,10 +102,10 @@ fn all_matching_links( root_node: &Node, link_text: String, partial: bool, -) -> Result<Vec<String>, ()> { +) -> Result<Vec<String>, ErrorStatus> { root_node .query_selector_all(DOMString::from("a")) - .map_err(|_| ()) + .map_err(|_| ErrorStatus::UnknownError) .map(|nodes| matching_links(&nodes, link_text, partial).collect()) } @@ -103,10 +113,10 @@ fn first_matching_link( root_node: &Node, link_text: String, partial: bool, -) -> Result<Option<String>, ()> { +) -> Result<Option<String>, ErrorStatus> { root_node .query_selector_all(DOMString::from("a")) - .map_err(|_| ()) + .map_err(|_| ErrorStatus::UnknownError) .map(|nodes| matching_links(&nodes, link_text, partial).take(1).next()) } @@ -243,45 +253,53 @@ pub fn handle_get_browsing_context_id( documents: &Documents, pipeline: PipelineId, webdriver_frame_id: WebDriverFrameId, - reply: IpcSender<Result<BrowsingContextId, ()>>, + reply: IpcSender<Result<BrowsingContextId, ErrorStatus>>, ) { - let result = match webdriver_frame_id { - WebDriverFrameId::Short(_) => { - // This isn't supported yet - Err(()) - }, - WebDriverFrameId::Element(x) => find_node_by_unique_id(documents, pipeline, x) - .and_then(|node| { - node.downcast::<HTMLIFrameElement>() - .and_then(|elem| elem.browsing_context_id()) - }) - .ok_or(()), - WebDriverFrameId::Parent => documents - .find_window(pipeline) - .and_then(|window| { - window - .window_proxy() - .parent() - .map(|parent| parent.browsing_context_id()) - }) - .ok_or(()), - }; - - reply.send(result).unwrap() + reply + .send(match webdriver_frame_id { + WebDriverFrameId::Short(_) => { + // This isn't supported yet + Err(ErrorStatus::UnsupportedOperation) + }, + WebDriverFrameId::Element(element_id) => { + find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| { + node.downcast::<HTMLIFrameElement>() + .and_then(|element| element.browsing_context_id()) + .ok_or(ErrorStatus::NoSuchFrame) + }) + }, + WebDriverFrameId::Parent => documents + .find_window(pipeline) + .and_then(|window| { + window + .window_proxy() + .parent() + .map(|parent| parent.browsing_context_id()) + }) + .ok_or(ErrorStatus::NoSuchFrame), + }) + .unwrap(); } pub fn handle_find_element_css( documents: &Documents, pipeline: PipelineId, selector: String, - reply: IpcSender<Result<Option<String>, ()>>, + reply: IpcSender<Result<Option<String>, ErrorStatus>>, ) { - let node_id = documents - .find_document(pipeline) - .ok_or(()) - .and_then(|doc| doc.QuerySelector(DOMString::from(selector)).map_err(|_| ())) - .map(|node| node.map(|x| x.upcast::<Node>().unique_id())); - reply.send(node_id).unwrap(); + reply + .send( + documents + .find_document(pipeline) + .ok_or(ErrorStatus::UnknownError) + .and_then(|document| { + document + .QuerySelector(DOMString::from(selector)) + .map_err(|_| ErrorStatus::InvalidSelector) + }) + .map(|node| node.map(|x| x.upcast::<Node>().unique_id())), + ) + .unwrap(); } pub fn handle_find_element_link_text( @@ -289,54 +307,66 @@ pub fn handle_find_element_link_text( pipeline: PipelineId, selector: String, partial: bool, - reply: IpcSender<Result<Option<String>, ()>>, + reply: IpcSender<Result<Option<String>, ErrorStatus>>, ) { - let node_id = documents - .find_document(pipeline) - .ok_or(()) - .and_then(|doc| first_matching_link(&doc.upcast::<Node>(), selector.clone(), partial)); - reply.send(node_id).unwrap(); + reply + .send( + documents + .find_document(pipeline) + .ok_or(ErrorStatus::UnknownError) + .and_then(|document| { + first_matching_link(&document.upcast::<Node>(), selector.clone(), partial) + }), + ) + .unwrap(); } pub fn handle_find_element_tag_name( documents: &Documents, pipeline: PipelineId, selector: String, - reply: IpcSender<Result<Option<String>, ()>>, + reply: IpcSender<Result<Option<String>, ErrorStatus>>, ) { - let node_id = documents - .find_document(pipeline) - .ok_or(()) - .and_then(|doc| { - Ok(doc - .GetElementsByTagName(DOMString::from(selector)) - .elements_iter() - .next()) - }) - .map(|node| node.map(|x| x.upcast::<Node>().unique_id())); - reply.send(node_id).unwrap(); + reply + .send( + documents + .find_document(pipeline) + .ok_or(ErrorStatus::UnknownError) + .and_then(|document| { + Ok(document + .GetElementsByTagName(DOMString::from(selector)) + .elements_iter() + .next()) + }) + .map(|node| node.map(|x| x.upcast::<Node>().unique_id())), + ) + .unwrap(); } pub fn handle_find_elements_css( documents: &Documents, pipeline: PipelineId, selector: String, - reply: IpcSender<Result<Vec<String>, ()>>, + reply: IpcSender<Result<Vec<String>, ErrorStatus>>, ) { - let node_ids = documents - .find_document(pipeline) - .ok_or(()) - .and_then(|doc| { - doc.QuerySelectorAll(DOMString::from(selector)) - .map_err(|_| ()) - }) - .map(|nodes| { - nodes - .iter() - .map(|x| x.upcast::<Node>().unique_id()) - .collect() - }); - reply.send(node_ids).unwrap(); + reply + .send( + documents + .find_document(pipeline) + .ok_or(ErrorStatus::UnknownError) + .and_then(|document| { + document + .QuerySelectorAll(DOMString::from(selector)) + .map_err(|_| ErrorStatus::InvalidSelector) + }) + .map(|nodes| { + nodes + .iter() + .map(|x| x.upcast::<Node>().unique_id()) + .collect() + }), + ) + .unwrap(); } pub fn handle_find_elements_link_text( @@ -344,32 +374,40 @@ pub fn handle_find_elements_link_text( pipeline: PipelineId, selector: String, partial: bool, - reply: IpcSender<Result<Vec<String>, ()>>, + reply: IpcSender<Result<Vec<String>, ErrorStatus>>, ) { - let node_ids = documents - .find_document(pipeline) - .ok_or(()) - .and_then(|doc| all_matching_links(&doc.upcast::<Node>(), selector.clone(), partial)); - reply.send(node_ids).unwrap(); + reply + .send( + documents + .find_document(pipeline) + .ok_or(ErrorStatus::UnknownError) + .and_then(|document| { + all_matching_links(&document.upcast::<Node>(), selector.clone(), partial) + }), + ) + .unwrap(); } pub fn handle_find_elements_tag_name( documents: &Documents, pipeline: PipelineId, selector: String, - reply: IpcSender<Result<Vec<String>, ()>>, + reply: IpcSender<Result<Vec<String>, ErrorStatus>>, ) { - let node_ids = documents - .find_document(pipeline) - .ok_or(()) - .and_then(|doc| Ok(doc.GetElementsByTagName(DOMString::from(selector)))) - .map(|nodes| { - nodes - .elements_iter() - .map(|x| x.upcast::<Node>().unique_id()) - .collect::<Vec<String>>() - }); - reply.send(node_ids).unwrap(); + reply + .send( + documents + .find_document(pipeline) + .ok_or(ErrorStatus::UnknownError) + .and_then(|document| Ok(document.GetElementsByTagName(DOMString::from(selector)))) + .map(|nodes| { + nodes + .elements_iter() + .map(|x| x.upcast::<Node>().unique_id()) + .collect::<Vec<String>>() + }), + ) + .unwrap(); } pub fn handle_find_element_element_css( @@ -377,16 +415,17 @@ pub fn handle_find_element_element_css( pipeline: PipelineId, element_id: String, selector: String, - reply: IpcSender<Result<Option<String>, ()>>, + reply: IpcSender<Result<Option<String>, ErrorStatus>>, ) { - let node_id = find_node_by_unique_id(documents, pipeline, element_id) - .ok_or(()) - .and_then(|node| { - node.query_selector(DOMString::from(selector)) - .map_err(|_| ()) - }) - .map(|node| node.map(|x| x.upcast::<Node>().unique_id())); - reply.send(node_id).unwrap(); + reply + .send( + find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| { + node.query_selector(DOMString::from(selector)) + .map_err(|_| ErrorStatus::InvalidSelector) + .map(|node| node.map(|x| x.upcast::<Node>().unique_id())) + }), + ) + .unwrap(); } pub fn handle_find_element_element_link_text( @@ -395,12 +434,14 @@ pub fn handle_find_element_element_link_text( element_id: String, selector: String, partial: bool, - reply: IpcSender<Result<Option<String>, ()>>, + reply: IpcSender<Result<Option<String>, ErrorStatus>>, ) { - let node_id = find_node_by_unique_id(documents, pipeline, element_id) - .ok_or(()) - .and_then(|node| first_matching_link(&node, selector.clone(), partial)); - reply.send(node_id).unwrap(); + reply + .send( + find_node_by_unique_id(documents, pipeline, element_id) + .and_then(|node| first_matching_link(&node, selector.clone(), partial)), + ) + .unwrap(); } pub fn handle_find_element_element_tag_name( @@ -408,19 +449,22 @@ pub fn handle_find_element_element_tag_name( pipeline: PipelineId, element_id: String, selector: String, - reply: IpcSender<Result<Option<String>, ()>>, + reply: IpcSender<Result<Option<String>, ErrorStatus>>, ) { - let node_id = find_node_by_unique_id(documents, pipeline, element_id) - .ok_or(()) - .and_then(|node| match node.downcast::<Element>() { - Some(elem) => Ok(elem - .GetElementsByTagName(DOMString::from(selector)) - .elements_iter() - .next()), - None => Err(()), - }) - .map(|node| node.map(|x| x.upcast::<Node>().unique_id())); - reply.send(node_id).unwrap(); + reply + .send( + find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| match node + .downcast::<Element>( + ) { + Some(element) => Ok(element + .GetElementsByTagName(DOMString::from(selector)) + .elements_iter() + .next() + .map(|x| x.upcast::<Node>().unique_id())), + None => Err(ErrorStatus::UnknownError), + }), + ) + .unwrap(); } pub fn handle_find_element_elements_css( @@ -428,21 +472,22 @@ pub fn handle_find_element_elements_css( pipeline: PipelineId, element_id: String, selector: String, - reply: IpcSender<Result<Vec<String>, ()>>, + reply: IpcSender<Result<Vec<String>, ErrorStatus>>, ) { - let node_ids = find_node_by_unique_id(documents, pipeline, element_id) - .ok_or(()) - .and_then(|node| { - node.query_selector_all(DOMString::from(selector)) - .map_err(|_| ()) - }) - .map(|nodes| { - nodes - .iter() - .map(|x| x.upcast::<Node>().unique_id()) - .collect() - }); - reply.send(node_ids).unwrap(); + reply + .send( + find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| { + node.query_selector_all(DOMString::from(selector)) + .map_err(|_| ErrorStatus::InvalidSelector) + .map(|nodes| { + nodes + .iter() + .map(|x| x.upcast::<Node>().unique_id()) + .collect() + }) + }), + ) + .unwrap(); } pub fn handle_find_element_elements_link_text( @@ -451,12 +496,14 @@ pub fn handle_find_element_elements_link_text( element_id: String, selector: String, partial: bool, - reply: IpcSender<Result<Vec<String>, ()>>, + reply: IpcSender<Result<Vec<String>, ErrorStatus>>, ) { - let node_ids = find_node_by_unique_id(documents, pipeline, element_id) - .ok_or(()) - .and_then(|node| all_matching_links(&node, selector.clone(), partial)); - reply.send(node_ids).unwrap(); + reply + .send( + find_node_by_unique_id(documents, pipeline, element_id) + .and_then(|node| all_matching_links(&node, selector.clone(), partial)), + ) + .unwrap(); } pub fn handle_find_element_elements_tag_name( @@ -464,44 +511,42 @@ pub fn handle_find_element_elements_tag_name( pipeline: PipelineId, element_id: String, selector: String, - reply: IpcSender<Result<Vec<String>, ()>>, + reply: IpcSender<Result<Vec<String>, ErrorStatus>>, ) { - let node_ids = find_node_by_unique_id(documents, pipeline, element_id) - .ok_or(()) - .and_then(|node| match node.downcast::<Element>() { - Some(elem) => Ok(elem.GetElementsByTagName(DOMString::from(selector))), - None => Err(()), - }) - .map(|nodes| { - nodes - .elements_iter() - .map(|x| x.upcast::<Node>().unique_id()) - .collect::<Vec<String>>() - }); - reply.send(node_ids).unwrap(); + reply + .send( + find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| match node + .downcast::<Element>( + ) { + Some(element) => Ok(element + .GetElementsByTagName(DOMString::from(selector)) + .elements_iter() + .map(|x| x.upcast::<Node>().unique_id()) + .collect::<Vec<String>>()), + None => Err(ErrorStatus::UnknownError), + }), + ) + .unwrap(); } pub fn handle_focus_element( documents: &Documents, pipeline: PipelineId, element_id: String, - reply: IpcSender<Result<(), ()>>, + reply: IpcSender<Result<(), ErrorStatus>>, ) { reply .send( - match find_node_by_unique_id(documents, pipeline, element_id) { - Some(ref node) => { - match node.downcast::<HTMLElement>() { - Some(ref elem) => { - // Need a way to find if this actually succeeded - elem.Focus(); - Ok(()) - }, - None => Err(()), - } - }, - None => Err(()), - }, + find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| { + match node.downcast::<HTMLElement>() { + Some(element) => { + // Need a way to find if this actually succeeded + element.Focus(); + Ok(()) + }, + None => Err(ErrorStatus::UnknownError), + } + }), ) .unwrap(); } @@ -515,8 +560,8 @@ pub fn handle_get_active_element( .send( documents .find_document(pipeline) - .and_then(|doc| doc.GetActiveElement()) - .map(|elem| elem.upcast::<Node>().unique_id()), + .and_then(|document| document.GetActiveElement()) + .map(|element| element.upcast::<Node>().unique_id()), ) .unwrap(); } @@ -524,25 +569,28 @@ pub fn handle_get_active_element( pub fn handle_get_page_source( documents: &Documents, pipeline: PipelineId, - reply: IpcSender<Result<String, ()>>, + reply: IpcSender<Result<String, ErrorStatus>>, ) { reply - .send(documents.find_document(pipeline).ok_or(()).and_then(|doc| { - match doc.GetDocumentElement() { - Some(elem) => match elem.GetOuterHTML() { - Ok(source) => Ok(source.to_string()), - Err(_) => { - match XMLSerializer::new(doc.window()) - .SerializeToString(elem.upcast::<Node>()) - { - Ok(source) => Ok(source.to_string()), - Err(_) => Err(()), - } + .send( + documents + .find_document(pipeline) + .ok_or(ErrorStatus::UnknownError) + .and_then(|document| match document.GetDocumentElement() { + Some(element) => match element.GetOuterHTML() { + Ok(source) => Ok(source.to_string()), + Err(_) => { + match XMLSerializer::new(document.window()) + .SerializeToString(element.upcast::<Node>()) + { + Ok(source) => Ok(source.to_string()), + Err(_) => Err(ErrorStatus::UnknownError), + } + }, }, - }, - None => Err(()), - } - })) + None => Err(ErrorStatus::UnknownError), + }), + ) .unwrap(); } @@ -551,21 +599,24 @@ pub fn handle_get_cookies( pipeline: PipelineId, reply: IpcSender<Vec<Serde<Cookie<'static>>>>, ) { - // TODO: Return an error if the pipeline doesn't exist? - let cookies = match documents.find_document(pipeline) { - None => Vec::new(), - Some(document) => { - let url = document.url(); - let (sender, receiver) = ipc::channel().unwrap(); - let _ = document - .window() - .upcast::<GlobalScope>() - .resource_threads() - .send(GetCookiesDataForUrl(url, sender, NonHTTP)); - receiver.recv().unwrap() - }, - }; - reply.send(cookies).unwrap(); + reply + .send( + // TODO: Return an error if the pipeline doesn't exist + match documents.find_document(pipeline) { + Some(document) => { + let url = document.url(); + let (sender, receiver) = ipc::channel().unwrap(); + let _ = document + .window() + .upcast::<GlobalScope>() + .resource_threads() + .send(GetCookiesDataForUrl(url, sender, NonHTTP)); + receiver.recv().unwrap() + }, + None => Vec::new(), + }, + ) + .unwrap(); } // https://w3c.github.io/webdriver/webdriver-spec.html#get-cookie @@ -575,22 +626,27 @@ pub fn handle_get_cookie( name: String, reply: IpcSender<Vec<Serde<Cookie<'static>>>>, ) { - // TODO: Return an error if the pipeline doesn't exist? - let cookies = match documents.find_document(pipeline) { - None => Vec::new(), - Some(document) => { - let url = document.url(); - let (sender, receiver) = ipc::channel().unwrap(); - let _ = document - .window() - .upcast::<GlobalScope>() - .resource_threads() - .send(GetCookiesDataForUrl(url, sender, NonHTTP)); - receiver.recv().unwrap() - }, - }; reply - .send(cookies.into_iter().filter(|c| c.name() == &*name).collect()) + .send( + // TODO: Return an error if the pipeline doesn't exist + match documents.find_document(pipeline) { + Some(document) => { + let url = document.url(); + let (sender, receiver) = ipc::channel().unwrap(); + let _ = document + .window() + .upcast::<GlobalScope>() + .resource_threads() + .send(GetCookiesDataForUrl(url, sender, NonHTTP)); + let cookies = receiver.recv().unwrap(); + cookies + .into_iter() + .filter(|cookie| cookie.name() == &*name) + .collect() + }, + None => Vec::new(), + }, + ) .unwrap(); } @@ -601,7 +657,7 @@ pub fn handle_add_cookie( cookie: Cookie<'static>, reply: IpcSender<Result<(), WebDriverCookieError>>, ) { - // TODO: Return a different error if the pipeline doesn't exist? + // TODO: Return a different error if the pipeline doesn't exist let document = match documents.find_document(pipeline) { Some(document) => document, None => { @@ -645,12 +701,12 @@ pub fn handle_add_cookie( pub fn handle_delete_cookies( documents: &Documents, pipeline: PipelineId, - reply: IpcSender<Result<(), ()>>, + reply: IpcSender<Result<(), ErrorStatus>>, ) { let document = match documents.find_document(pipeline) { Some(document) => document, None => { - return reply.send(Err(())).unwrap(); + return reply.send(Err(ErrorStatus::UnknownError)).unwrap(); }, }; let url = document.url(); @@ -660,62 +716,62 @@ pub fn handle_delete_cookies( .resource_threads() .send(DeleteCookies(url)) .unwrap(); - let _ = reply.send(Ok(())); + reply.send(Ok(())).unwrap(); } pub fn handle_get_title(documents: &Documents, pipeline: PipelineId, reply: IpcSender<String>) { - // TODO: Return an error if the pipeline doesn't exist. - let title = documents - .find_document(pipeline) - .map(|doc| String::from(doc.Title())) - .unwrap_or_default(); - reply.send(title).unwrap(); + reply + .send( + // TODO: Return an error if the pipeline doesn't exist + documents + .find_document(pipeline) + .map(|document| String::from(document.Title())) + .unwrap_or_default(), + ) + .unwrap(); } pub fn handle_get_rect( documents: &Documents, pipeline: PipelineId, element_id: String, - reply: IpcSender<Result<Rect<f64>, ()>>, + reply: IpcSender<Result<Rect<f64>, ErrorStatus>>, ) { reply .send( - match find_node_by_unique_id(documents, pipeline, element_id) { - Some(elem) => { - // https://w3c.github.io/webdriver/webdriver-spec.html#dfn-calculate-the-absolute-position - match elem.downcast::<HTMLElement>() { - Some(html_elem) => { - // Step 1 - let mut x = 0; - let mut y = 0; - - let mut offset_parent = html_elem.GetOffsetParent(); - - // Step 2 - while let Some(element) = offset_parent { - offset_parent = match element.downcast::<HTMLElement>() { - Some(elem) => { - x += elem.OffsetLeft(); - y += elem.OffsetTop(); - elem.GetOffsetParent() - }, - None => None, - }; - } - // Step 3 - Ok(Rect::new( - Point2D::new(x as f64, y as f64), - Size2D::new( - html_elem.OffsetWidth() as f64, - html_elem.OffsetHeight() as f64, - ), - )) - }, - None => Err(()), - } - }, - None => Err(()), - }, + find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| { + // https://w3c.github.io/webdriver/webdriver-spec.html#dfn-calculate-the-absolute-position + match node.downcast::<HTMLElement>() { + Some(html_element) => { + // Step 1 + let mut x = 0; + let mut y = 0; + + let mut offset_parent = html_element.GetOffsetParent(); + + // Step 2 + while let Some(element) = offset_parent { + offset_parent = match element.downcast::<HTMLElement>() { + Some(elem) => { + x += elem.OffsetLeft(); + y += elem.OffsetTop(); + elem.GetOffsetParent() + }, + None => None, + }; + } + // Step 3 + Ok(Rect::new( + Point2D::new(x as f64, y as f64), + Size2D::new( + html_element.OffsetWidth() as f64, + html_element.OffsetHeight() as f64, + ), + )) + }, + None => Err(ErrorStatus::UnknownError), + } + }), ) .unwrap(); } @@ -724,13 +780,13 @@ pub fn handle_get_text( documents: &Documents, pipeline: PipelineId, node_id: String, - reply: IpcSender<Result<String, ()>>, + reply: IpcSender<Result<String, ErrorStatus>>, ) { reply - .send(match find_node_by_unique_id(documents, pipeline, node_id) { - Some(ref node) => Ok(node.GetTextContent().map_or("".to_owned(), String::from)), - None => Err(()), - }) + .send( + find_node_by_unique_id(documents, pipeline, node_id) + .and_then(|node| Ok(node.GetTextContent().map_or("".to_owned(), String::from))), + ) .unwrap(); } @@ -738,13 +794,13 @@ pub fn handle_get_name( documents: &Documents, pipeline: PipelineId, node_id: String, - reply: IpcSender<Result<String, ()>>, + reply: IpcSender<Result<String, ErrorStatus>>, ) { reply - .send(match find_node_by_unique_id(documents, pipeline, node_id) { - Some(node) => Ok(String::from(node.downcast::<Element>().unwrap().TagName())), - None => Err(()), - }) + .send( + find_node_by_unique_id(documents, pipeline, node_id) + .and_then(|node| Ok(String::from(node.downcast::<Element>().unwrap().TagName()))), + ) .unwrap(); } @@ -753,17 +809,18 @@ pub fn handle_get_attribute( pipeline: PipelineId, node_id: String, name: String, - reply: IpcSender<Result<Option<String>, ()>>, + reply: IpcSender<Result<Option<String>, ErrorStatus>>, ) { reply - .send(match find_node_by_unique_id(documents, pipeline, node_id) { - Some(node) => Ok(node - .downcast::<Element>() - .unwrap() - .GetAttribute(DOMString::from(name)) - .map(String::from)), - None => Err(()), - }) + .send( + find_node_by_unique_id(documents, pipeline, node_id).and_then(|node| { + Ok(node + .downcast::<Element>() + .unwrap() + .GetAttribute(DOMString::from(name)) + .map(String::from)) + }), + ) .unwrap(); } @@ -773,11 +830,11 @@ pub fn handle_get_property( pipeline: PipelineId, node_id: String, name: String, - reply: IpcSender<Result<WebDriverJSValue, ()>>, + reply: IpcSender<Result<WebDriverJSValue, ErrorStatus>>, ) { reply - .send(match find_node_by_unique_id(documents, pipeline, node_id) { - Some(node) => { + .send( + find_node_by_unique_id(documents, pipeline, node_id).and_then(|node| { let cx = documents.find_document(pipeline).unwrap().window().get_cx(); rooted!(in(*cx) let mut property = UndefinedValue()); @@ -800,9 +857,8 @@ pub fn handle_get_property( Ok(WebDriverJSValue::Undefined) }, } - }, - None => Err(()), - }) + }), + ) .unwrap(); } @@ -811,48 +867,49 @@ pub fn handle_get_css( pipeline: PipelineId, node_id: String, name: String, - reply: IpcSender<Result<String, ()>>, + reply: IpcSender<Result<String, ErrorStatus>>, ) { reply - .send(match find_node_by_unique_id(documents, pipeline, node_id) { - Some(node) => { + .send( + find_node_by_unique_id(documents, pipeline, node_id).and_then(|node| { let window = window_from_node(&*node); - let elem = node.downcast::<Element>().unwrap(); + let element = node.downcast::<Element>().unwrap(); Ok(String::from( window - .GetComputedStyle(&elem, None) + .GetComputedStyle(&element, None) .GetPropertyValue(DOMString::from(name)), )) - }, - None => Err(()), - }) + }), + ) .unwrap(); } pub fn handle_get_url(documents: &Documents, pipeline: PipelineId, reply: IpcSender<ServoUrl>) { - // TODO: Return an error if the pipeline doesn't exist. - let url = documents - .find_document(pipeline) - .map(|document| document.url()) - .unwrap_or_else(|| ServoUrl::parse("about:blank").expect("infallible")); - reply.send(url).unwrap(); + reply + .send( + // TODO: Return an error if the pipeline doesn't exist. + documents + .find_document(pipeline) + .map(|document| document.url()) + .unwrap_or_else(|| ServoUrl::parse("about:blank").expect("infallible")), + ) + .unwrap(); } pub fn handle_is_enabled( documents: &Documents, pipeline: PipelineId, element_id: String, - reply: IpcSender<Result<bool, ()>>, + reply: IpcSender<Result<bool, ErrorStatus>>, ) { reply .send( - match find_node_by_unique_id(&documents, pipeline, element_id) { - Some(ref node) => match node.downcast::<Element>() { - Some(elem) => Ok(elem.enabled_state()), - None => Err(()), - }, - None => Err(()), - }, + find_node_by_unique_id(&documents, pipeline, element_id).and_then(|node| match node + .downcast::<Element>( + ) { + Some(element) => Ok(element.enabled_state()), + None => Err(ErrorStatus::UnknownError), + }), ) .unwrap(); } @@ -861,24 +918,21 @@ pub fn handle_is_selected( documents: &Documents, pipeline: PipelineId, element_id: String, - reply: IpcSender<Result<bool, ()>>, + reply: IpcSender<Result<bool, ErrorStatus>>, ) { reply .send( - match find_node_by_unique_id(documents, pipeline, element_id) { - Some(ref node) => { - if let Some(input_element) = node.downcast::<HTMLInputElement>() { - Ok(input_element.Checked()) - } else if let Some(option_element) = node.downcast::<HTMLOptionElement>() { - Ok(option_element.Selected()) - } else if node.is::<HTMLElement>() { - Ok(false) // regular elements are not selectable - } else { - Err(()) - } - }, - None => Err(()), - }, + find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| { + if let Some(input_element) = node.downcast::<HTMLInputElement>() { + Ok(input_element.Checked()) + } else if let Some(option_element) = node.downcast::<HTMLOptionElement>() { + Ok(option_element.Selected()) + } else if node.is::<HTMLElement>() { + Ok(false) // regular elements are not selectable + } else { + Err(ErrorStatus::UnknownError) + } + }), ) .unwrap(); } |