auto merge of #1864 : saneyuki/servo/split_cast_to, r=jdm

fix #1836
This commit is contained in:
bors-servo 2014-03-20 10:58:36 -04:00
commit 8317122068
10 changed files with 71 additions and 64 deletions

View file

@ -795,7 +795,7 @@ impl<'ln> NodeUtils for ThreadSafeLayoutNode<'ln> {
match self.type_id() {
TextNodeTypeId => {
unsafe {
let text: JS<Text> = TextCast::to(self.get_jsmanaged());
let text: JS<Text> = TextCast::to(self.get_jsmanaged()).unwrap();
if !is_whitespace(text.get().characterdata.data) {
return false
}

View file

@ -5793,9 +5793,16 @@ class GlobalGenRoots():
unsafe { derived.clone().transmute() }
}
fn to<T: ${toBound}>(base: &JS<T>) -> JS<Self> {
fn to<T: ${toBound}>(base: &JS<T>) -> Option<JS<Self>> {
match base.get().${checkFn}() {
true => unsafe { Some(base.clone().transmute()) },
false => None
}
}
unsafe fn to_unchecked<T: ${toBound}>(base: &JS<T>) -> JS<Self> {
assert!(base.get().${checkFn}());
unsafe { base.clone().transmute() }
base.clone().transmute()
}
}
''').substitute({'checkFn': 'is_' + name.lower(),

View file

@ -213,7 +213,7 @@ impl Document {
// http://dom.spec.whatwg.org/#dom-document-doctype
pub fn GetDoctype(&self) -> Option<JS<DocumentType>> {
self.node.children().find(|child| child.is_doctype())
.map(|node| DocumentTypeCast::to(&node))
.map(|node| DocumentTypeCast::to(&node).unwrap())
}
// http://dom.spec.whatwg.org/#dom-document-documentelement
@ -314,7 +314,7 @@ impl Document {
.map(|title_elem| {
for child in title_elem.children() {
if child.is_text() {
let text: JS<Text> = TextCast::to(&child);
let text: JS<Text> = TextCast::to(&child).unwrap();
title.push_str(text.get().characterdata.data.as_slice());
}
}
@ -362,7 +362,7 @@ impl Document {
fn get_html_element(&self) -> Option<JS<HTMLHtmlElement>> {
self.GetDocumentElement().filtered(|root| {
root.get().node.type_id == ElementNodeTypeId(HTMLHtmlElementTypeId)
}).map(|elem| HTMLHtmlElementCast::to(&elem))
}).map(|elem| HTMLHtmlElementCast::to(&elem).unwrap())
}
// http://www.whatwg.org/specs/web-apps/current-work/#dom-document-head
@ -371,7 +371,7 @@ impl Document {
let node: JS<Node> = NodeCast::from(&root);
node.children().find(|child| {
child.type_id() == ElementNodeTypeId(HTMLHeadElementTypeId)
}).map(|node| HTMLHeadElementCast::to(&node))
}).map(|node| HTMLHeadElementCast::to(&node).unwrap())
})
}
@ -385,7 +385,7 @@ impl Document {
ElementNodeTypeId(HTMLFrameSetElementTypeId) => true,
_ => false
}
}).map(|node| HTMLElementCast::to(&node))
}).map(|node| HTMLElementCast::to(&node).unwrap())
})
}
@ -434,7 +434,7 @@ impl Document {
return false;
}
let element: JS<Element> = ElementCast::to(node);
let element: JS<Element> = ElementCast::to(node).unwrap();
element.get().get_attribute(Null, "name").map_or(false, |attr| {
attr.get().value_ref() == name
})

View file

@ -272,15 +272,15 @@ impl Element {
// This hardcoding is awful.
match abstract_self.get().node.type_id {
ElementNodeTypeId(HTMLImageElementTypeId) => {
let mut elem: JS<HTMLImageElement> = HTMLImageElementCast::to(abstract_self);
let mut elem: JS<HTMLImageElement> = HTMLImageElementCast::to(abstract_self).unwrap();
elem.get_mut().AfterSetAttr(local_name.clone(), value.clone());
}
ElementNodeTypeId(HTMLIFrameElementTypeId) => {
let mut elem: JS<HTMLIFrameElement> = HTMLIFrameElementCast::to(abstract_self);
let mut elem: JS<HTMLIFrameElement> = HTMLIFrameElementCast::to(abstract_self).unwrap();
elem.get_mut().AfterSetAttr(local_name.clone(), value.clone());
}
ElementNodeTypeId(HTMLObjectElementTypeId) => {
let mut elem: JS<HTMLObjectElement> = HTMLObjectElementCast::to(abstract_self);
let mut elem: JS<HTMLObjectElement> = HTMLObjectElementCast::to(abstract_self).unwrap();
elem.get_mut().AfterSetAttr(local_name.clone(), value.clone());
}
_ => ()
@ -341,11 +341,11 @@ impl Element {
// This hardcoding is awful.
match abstract_self.get().node.type_id {
ElementNodeTypeId(HTMLImageElementTypeId) => {
let mut elem: JS<HTMLImageElement> = HTMLImageElementCast::to(abstract_self);
let mut elem: JS<HTMLImageElement> = HTMLImageElementCast::to(abstract_self).unwrap();
elem.get_mut().BeforeRemoveAttr(local_name.clone());
}
ElementNodeTypeId(HTMLIFrameElementTypeId) => {
let mut elem: JS<HTMLIFrameElement> = HTMLIFrameElementCast::to(abstract_self);
let mut elem: JS<HTMLIFrameElement> = HTMLIFrameElementCast::to(abstract_self).unwrap();
elem.get_mut().BeforeRemoveAttr(local_name.clone());
}
_ => ()

View file

@ -28,7 +28,7 @@ pub fn dispatch_event(target: &JS<EventTarget>,
//TODO: no chain if not participating in a tree
if target.get().is_node() {
let target_node: JS<Node> = NodeCast::to(target);
let target_node: JS<Node> = NodeCast::to(target).unwrap();
for ancestor in target_node.ancestors() {
let ancestor_target: JS<EventTarget> = EventTargetCast::from(&ancestor);
chain.push(ancestor_target);

View file

@ -39,7 +39,7 @@ impl HTMLCollection {
let mut elements = ~[];
for child in root.traverse_preorder() {
if child.is_element() {
let elem: JS<Element> = ElementCast::to(&child);
let elem: JS<Element> = ElementCast::to(&child).unwrap();
if predicate(&elem) {
elements.push(elem);
}

View file

@ -30,23 +30,23 @@ pub fn serialize(iterator: &mut NodeIterator) -> ~str {
html.push_str(
match node.type_id() {
ElementNodeTypeId(..) => {
let elem: JS<Element> = ElementCast::to(&node);
let elem: JS<Element> = ElementCast::to(&node).unwrap();
serialize_elem(&elem, &mut open_elements)
}
CommentNodeTypeId => {
let comment: JS<Comment> = CommentCast::to(&node);
let comment: JS<Comment> = CommentCast::to(&node).unwrap();
serialize_comment(&comment)
}
TextNodeTypeId => {
let text: JS<Text> = TextCast::to(&node);
let text: JS<Text> = TextCast::to(&node).unwrap();
serialize_text(&text)
}
DoctypeNodeTypeId => {
let doctype: JS<DocumentType> = DocumentTypeCast::to(&node);
let doctype: JS<DocumentType> = DocumentTypeCast::to(&node).unwrap();
serialize_doctype(&doctype)
}
ProcessingInstructionNodeTypeId => {
let processing_instruction: JS<ProcessingInstruction> = ProcessingInstructionCast::to(&node);
let processing_instruction: JS<ProcessingInstruction> = ProcessingInstructionCast::to(&node).unwrap();
serialize_processing_instruction(&processing_instruction)
}
DocumentFragmentNodeTypeId => {
@ -71,7 +71,7 @@ fn serialize_comment(comment: &JS<Comment>) -> ~str {
fn serialize_text(text: &JS<Text>) -> ~str {
match text.get().characterdata.node.parent_node {
Some(ref parent) if parent.is_element() => {
let elem: JS<Element> = ElementCast::to(parent);
let elem: JS<Element> = ElementCast::to(parent).unwrap();
match elem.get().tag_name.as_slice() {
"style" | "script" | "xmp" | "iframe" |
"noembed" | "noframes" | "plaintext" |
@ -103,7 +103,7 @@ fn serialize_elem(elem: &JS<Element>, open_elements: &mut ~[~str]) -> ~str {
"pre" | "listing" | "textarea" if elem.get().namespace == namespace::HTML => {
match elem.get().node.first_child {
Some(ref child) if child.is_text() => {
let text: JS<CharacterData> = CharacterDataCast::to(child);
let text: JS<CharacterData> = CharacterDataCast::to(child).unwrap();
if text.get().data.len() > 0 && text.get().data[0] == 0x0A as u8 {
rv.push_str("\x0A");
}

View file

@ -407,7 +407,7 @@ impl NodeHelpers for JS<Node> {
if self.is_in_doc() {
for node in self.traverse_preorder() {
if node.is_element() {
let element: JS<Element> = ElementCast::to(&node);
let element: JS<Element> = ElementCast::to(&node).unwrap();
element.bind_to_tree_impl();
}
}
@ -423,7 +423,7 @@ impl NodeHelpers for JS<Node> {
for node in self.traverse_preorder() {
if node.is_element() {
let element: JS<Element> = ElementCast::to(&node);
let element: JS<Element> = ElementCast::to(&node).unwrap();
element.unbind_from_tree_impl();
}
}
@ -660,7 +660,7 @@ impl NodeIterator {
fn next_child(&self, node: &JS<Node>) -> Option<JS<Node>> {
if !self.include_descendants_of_void && node.is_element() {
let elem: JS<Element> = ElementCast::to(node);
let elem: JS<Element> = ElementCast::to(node).unwrap();
if elem.get().is_void() {
None
} else {
@ -761,7 +761,7 @@ impl Node {
self.children()
.filter(|node| node.is_element())
.map(|node| {
let elem: JS<Element> = ElementCast::to(&node);
let elem: JS<Element> = ElementCast::to(&node).unwrap();
elem
})
}
@ -837,18 +837,18 @@ impl Node {
pub fn NodeName(&self, abstract_self: &JS<Node>) -> DOMString {
match self.type_id {
ElementNodeTypeId(..) => {
let elem: JS<Element> = ElementCast::to(abstract_self);
let elem: JS<Element> = ElementCast::to(abstract_self).unwrap();
elem.get().TagName()
}
TextNodeTypeId => ~"#text",
ProcessingInstructionNodeTypeId => {
let processing_instruction: JS<ProcessingInstruction> =
ProcessingInstructionCast::to(abstract_self);
ProcessingInstructionCast::to(abstract_self).unwrap();
processing_instruction.get().Target()
}
CommentNodeTypeId => ~"#comment",
DoctypeNodeTypeId => {
let doctype: JS<DocumentType> = DocumentTypeCast::to(abstract_self);
let doctype: JS<DocumentType> = DocumentTypeCast::to(abstract_self).unwrap();
doctype.get().name.clone()
},
DocumentFragmentNodeTypeId => ~"#document-fragment",
@ -884,7 +884,7 @@ impl Node {
pub fn GetParentElement(&self) -> Option<JS<Element>> {
self.parent_node.clone()
.filtered(|parent| parent.is_element())
.map(|node| ElementCast::to(&node))
.map(|node| ElementCast::to(&node).unwrap())
}
// http://dom.spec.whatwg.org/#dom-node-haschildnodes
@ -932,7 +932,7 @@ impl Node {
CommentNodeTypeId |
TextNodeTypeId |
ProcessingInstructionNodeTypeId => {
let chardata: JS<CharacterData> = CharacterDataCast::to(abstract_self);
let chardata: JS<CharacterData> = CharacterDataCast::to(abstract_self).unwrap();
Some(chardata.get().Data())
}
_ => {
@ -962,7 +962,7 @@ impl Node {
let mut content = ~"";
for node in abstract_self.traverse_preorder() {
if node.is_text() {
let text: JS<Text> = TextCast::to(&node);
let text: JS<Text> = TextCast::to(&node).unwrap();
content.push_str(text.get().characterdata.data.as_slice());
}
}
@ -971,7 +971,7 @@ impl Node {
CommentNodeTypeId |
TextNodeTypeId |
ProcessingInstructionNodeTypeId => {
let characterdata: JS<CharacterData> = CharacterDataCast::to(abstract_self);
let characterdata: JS<CharacterData> = CharacterDataCast::to(abstract_self).unwrap();
Some(characterdata.get().Data())
}
DoctypeNodeTypeId |
@ -1003,7 +1003,7 @@ impl Node {
ProcessingInstructionNodeTypeId => {
self.wait_until_safe_to_modify_dom();
let mut characterdata: JS<CharacterData> = CharacterDataCast::to(abstract_self);
let mut characterdata: JS<CharacterData> = CharacterDataCast::to(abstract_self).unwrap();
characterdata.get_mut().data = value.clone();
// Notify the document that the content of this node is different
@ -1310,7 +1310,7 @@ impl Node {
// XXXabinader: clone() for each node as trait?
let mut copy: JS<Node> = match node.type_id() {
DoctypeNodeTypeId => {
let doctype: JS<DocumentType> = DocumentTypeCast::to(node);
let doctype: JS<DocumentType> = DocumentTypeCast::to(node).unwrap();
let doctype = doctype.get();
let doctype = DocumentType::new(doctype.name.clone(),
Some(doctype.public_id.clone()),
@ -1322,13 +1322,13 @@ impl Node {
NodeCast::from(&doc_fragment)
},
CommentNodeTypeId => {
let comment: JS<Comment> = CommentCast::to(node);
let comment: JS<Comment> = CommentCast::to(node).unwrap();
let comment = comment.get();
let comment = Comment::new(comment.characterdata.data.clone(), &document);
NodeCast::from(&comment)
},
DocumentNodeTypeId => {
let document: JS<Document> = DocumentCast::to(node);
let document: JS<Document> = DocumentCast::to(node).unwrap();
let document = document.get();
let is_html_doc = match document.is_html_document {
true => HTMLDocument,
@ -1339,19 +1339,19 @@ impl Node {
NodeCast::from(&document)
},
ElementNodeTypeId(..) => {
let element: JS<Element> = ElementCast::to(node);
let element: JS<Element> = ElementCast::to(node).unwrap();
let element = element.get();
let element = build_element_from_tag(element.tag_name.clone(), &document);
NodeCast::from(&element)
},
TextNodeTypeId => {
let text: JS<Text> = TextCast::to(node);
let text: JS<Text> = TextCast::to(node).unwrap();
let text = text.get();
let text = Text::new(text.characterdata.data.clone(), &document);
NodeCast::from(&text)
},
ProcessingInstructionNodeTypeId => {
let pi: JS<ProcessingInstruction> = ProcessingInstructionCast::to(node);
let pi: JS<ProcessingInstruction> = ProcessingInstructionCast::to(node).unwrap();
let pi = pi.get();
let pi = ProcessingInstruction::new(pi.target.clone(),
pi.characterdata.data.clone(), &document);
@ -1361,24 +1361,24 @@ impl Node {
// Step 3.
if copy.is_document() {
document = DocumentCast::to(&copy);
document = DocumentCast::to(&copy).unwrap();
}
assert_eq!(copy.get().owner_doc(), &document);
// Step 4 (some data already copied in step 2).
match node.type_id() {
DocumentNodeTypeId => {
let node_doc: JS<Document> = DocumentCast::to(node);
let node_doc: JS<Document> = DocumentCast::to(node).unwrap();
let node_doc = node_doc.get();
let mut copy_doc: JS<Document> = DocumentCast::to(&copy);
let mut copy_doc: JS<Document> = DocumentCast::to(&copy).unwrap();
let copy_doc = copy_doc.get_mut();
copy_doc.set_encoding_name(node_doc.encoding_name.clone());
copy_doc.set_quirks_mode(node_doc.quirks_mode());
},
ElementNodeTypeId(..) => {
let node_elem: JS<Element> = ElementCast::to(node);
let node_elem: JS<Element> = ElementCast::to(node).unwrap();
let node_elem = node_elem.get();
let mut copy_elem: JS<Element> = ElementCast::to(&copy);
let mut copy_elem: JS<Element> = ElementCast::to(&copy).unwrap();
let copy_elem = copy_elem.get_mut();
// FIXME: https://github.com/mozilla/servo/issues/1737
copy_elem.namespace = node_elem.namespace.clone();
@ -1561,13 +1561,13 @@ impl Node {
let mut prev_text = None;
for mut child in self.children() {
if child.is_text() {
let characterdata: JS<CharacterData> = CharacterDataCast::to(&child);
let characterdata: JS<CharacterData> = CharacterDataCast::to(&child).unwrap();
if characterdata.get().Length() == 0 {
abstract_self.remove_child(&mut child);
} else {
match prev_text {
Some(ref text_node) => {
let mut prev_characterdata: JS<CharacterData> = CharacterDataCast::to(text_node);
let mut prev_characterdata: JS<CharacterData> = CharacterDataCast::to(text_node).unwrap();
let _ = prev_characterdata.get_mut().AppendData(characterdata.get().Data());
abstract_self.remove_child(&mut child);
},
@ -1592,34 +1592,34 @@ impl Node {
// http://dom.spec.whatwg.org/#dom-node-isequalnode
pub fn IsEqualNode(&self, abstract_self: &JS<Node>, maybe_node: Option<JS<Node>>) -> bool {
fn is_equal_doctype(node: &JS<Node>, other: &JS<Node>) -> bool {
let doctype: JS<DocumentType> = DocumentTypeCast::to(node);
let other_doctype: JS<DocumentType> = DocumentTypeCast::to(other);
let doctype: JS<DocumentType> = DocumentTypeCast::to(node).unwrap();
let other_doctype: JS<DocumentType> = DocumentTypeCast::to(other).unwrap();
(doctype.get().name == other_doctype.get().name) &&
(doctype.get().public_id == other_doctype.get().public_id) &&
(doctype.get().system_id == other_doctype.get().system_id)
}
fn is_equal_element(node: &JS<Node>, other: &JS<Node>) -> bool {
let element: JS<Element> = ElementCast::to(node);
let other_element: JS<Element> = ElementCast::to(other);
let element: JS<Element> = ElementCast::to(node).unwrap();
let other_element: JS<Element> = ElementCast::to(other).unwrap();
// FIXME: namespace prefix
(element.get().namespace == other_element.get().namespace) &&
(element.get().tag_name == other_element.get().tag_name) &&
(element.get().attrs.len() == other_element.get().attrs.len())
}
fn is_equal_processinginstruction(node: &JS<Node>, other: &JS<Node>) -> bool {
let pi: JS<ProcessingInstruction> = ProcessingInstructionCast::to(node);
let other_pi: JS<ProcessingInstruction> = ProcessingInstructionCast::to(other);
let pi: JS<ProcessingInstruction> = ProcessingInstructionCast::to(node).unwrap();
let other_pi: JS<ProcessingInstruction> = ProcessingInstructionCast::to(other).unwrap();
(pi.get().target == other_pi.get().target) &&
(pi.get().characterdata.data == other_pi.get().characterdata.data)
}
fn is_equal_characterdata(node: &JS<Node>, other: &JS<Node>) -> bool {
let characterdata: JS<CharacterData> = CharacterDataCast::to(node);
let other_characterdata: JS<CharacterData> = CharacterDataCast::to(other);
let characterdata: JS<CharacterData> = CharacterDataCast::to(node).unwrap();
let other_characterdata: JS<CharacterData> = CharacterDataCast::to(other).unwrap();
characterdata.get().data == other_characterdata.get().data
}
fn is_equal_element_attrs(node: &JS<Node>, other: &JS<Node>) -> bool {
let element: JS<Element> = ElementCast::to(node);
let other_element: JS<Element> = ElementCast::to(other);
let element: JS<Element> = ElementCast::to(node).unwrap();
let other_element: JS<Element> = ElementCast::to(other).unwrap();
assert!(element.get().attrs.len() == other_element.get().attrs.len());
element.get().attrs.iter().all(|attr| {
other_element.get().attrs.iter().any(|other_attr| {

View file

@ -369,7 +369,7 @@ pub fn parse_html(page: &Page,
ElementNodeTypeId(HTMLIFrameElementTypeId) => {
let iframe_chan = discovery_chan.clone();
let mut iframe_element: JS<HTMLIFrameElement> =
HTMLIFrameElementCast::to(&element);
HTMLIFrameElementCast::to(&element).unwrap();
let sandboxed = iframe_element.get().is_sandboxed();
let elem: JS<Element> = ElementCast::from(&iframe_element);
let src_opt = elem.get().get_attribute(Null, "src").map(|x| x.get().Value());
@ -475,7 +475,7 @@ pub fn parse_html(page: &Page,
debug!("iterating over children {:?}", scriptnode.first_child());
for child in scriptnode.children() {
debug!("child = {:?}", child);
let text: JS<Text> = TextCast::to(&child);
let text: JS<Text> = TextCast::to(&child).unwrap();
data.push(text.get().characterdata.data.to_str()); // FIXME: Bad copy.
}
@ -494,7 +494,7 @@ pub fn parse_html(page: &Page,
debug!("iterating over children {:?}", style.first_child());
for child in style.children() {
debug!("child = {:?}", child);
let text: JS<Text> = TextCast::to(&child);
let text: JS<Text> = TextCast::to(&child).unwrap();
data.push(text.get().characterdata.data.to_str()); // FIXME: Bad copy.
}

View file

@ -915,11 +915,11 @@ impl ScriptTask {
let doc_node: JS<Node> = NodeCast::from(&document);
let mut anchors = doc_node.traverse_preorder().filter(|node| node.is_anchor_element());
anchors.find(|node| {
let elem: JS<Element> = ElementCast::to(node);
let elem: JS<Element> = ElementCast::to(node).unwrap();
elem.get().get_attribute(Null, "name").map_or(false, |attr| {
attr.get().value_ref() == fragid
})
}).map(|node| ElementCast::to(&node))
}).map(|node| ElementCast::to(&node).unwrap())
}
}
}
@ -1027,7 +1027,7 @@ impl ScriptTask {
}
if node.is_element() {
let element: JS<Element> = ElementCast::to(&node);
let element: JS<Element> = ElementCast::to(&node).unwrap();
if "a" == element.get().tag_name {
self.load_url_from_element(page, element.get())
}