Skip to content

Commit

Permalink
Share code between HTMLScriptElement:Text and Node:GetTextContent
Browse files Browse the repository at this point in the history
Added a function in Node called collect_text_contents which is called
from both places.

Fixes #3157
  • Loading branch information
gilles-leblanc committed Aug 31, 2014
1 parent 8af758f commit d12243d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
13 changes: 2 additions & 11 deletions src/components/script/dom/htmlscriptelement.rs
Expand Up @@ -6,15 +6,14 @@ 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;
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)]
Expand Down Expand Up @@ -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
Expand Down
20 changes: 13 additions & 7 deletions src/components/script/dom/node.rs
Expand Up @@ -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> {
Expand Down Expand Up @@ -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 |
Expand Down

0 comments on commit d12243d

Please sign in to comment.