Merge pull request #3189 from gilles-leblanc/issue-3157

Share code between HTMLScriptElement:Text and Node:GetTextContent; r=Manishearth
This commit is contained in:
Manish Goregaokar 2014-08-31 20:11:05 +05:30
commit b79432d98e
2 changed files with 15 additions and 18 deletions

View file

@ -6,7 +6,7 @@ use dom::bindings::codegen::Bindings::HTMLScriptElementBinding;
use dom::bindings::codegen::Bindings::HTMLScriptElementBinding::HTMLScriptElementMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::codegen::InheritTypes::HTMLScriptElementDerived;
use dom::bindings::codegen::InheritTypes::{ElementCast, NodeCast, TextCast};
use dom::bindings::codegen::InheritTypes::{ElementCast, NodeCast};
use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector};
use dom::document::Document;
@ -14,7 +14,6 @@ use dom::element::{HTMLScriptElementTypeId, Element, AttributeHandlers};
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement;
use dom::node::{Node, NodeHelpers, ElementNodeTypeId};
use dom::text::Text;
use servo_util::str::DOMString;
#[deriving(Encodable)]
@ -50,15 +49,7 @@ impl<'a> HTMLScriptElementMethods for JSRef<'a, HTMLScriptElement> {
// http://www.whatwg.org/html/#dom-script-text
fn Text(&self) -> DOMString {
let node: &JSRef<Node> = NodeCast::from_ref(self);
let mut content = String::new();
for child in node.children() {
let text: Option<&JSRef<Text>> = TextCast::to_ref(&child);
match text {
Some(text) => content.push_str(text.characterdata.data.borrow().as_slice()),
None => (),
}
}
content
Node::collect_text_contents(node.children())
}
// http://www.whatwg.org/html/#dom-script-text

View file

@ -1408,6 +1408,18 @@ impl Node {
pub unsafe fn unsafe_get_flags(&self) -> *const NodeFlags {
mem::transmute(&self.flags)
}
pub fn collect_text_contents<'a, T: Iterator<JSRef<'a, Node>>>(mut iterator: T) -> String {
let mut content = String::new();
for node in iterator {
let text: Option<&JSRef<Text>> = TextCast::to_ref(&node);
match text {
Some(text) => content.push_str(text.characterdata.data.borrow().as_slice()),
None => (),
}
}
content
}
}
impl<'a> NodeMethods for JSRef<'a, Node> {
@ -1553,13 +1565,7 @@ impl<'a> NodeMethods for JSRef<'a, Node> {
match self.type_id {
DocumentFragmentNodeTypeId |
ElementNodeTypeId(..) => {
let mut content = String::new();
for node in self.traverse_preorder() {
if node.is_text() {
let text: &JSRef<Text> = TextCast::to_ref(&node).unwrap();
content.push_str(text.deref().characterdata.data.deref().borrow().as_slice());
}
}
let content = Node::collect_text_contents(self.traverse_preorder());
Some(content)
}
CommentNodeTypeId |