mirror of
https://github.com/servo/servo.git
synced 2025-08-05 21:50:18 +01:00
Share code between HTMLScriptElement:Text and Node:GetTextContent
Added a function in Node called collect_text_contents which is called from both places. Fixes #3157
This commit is contained in:
parent
8af758f591
commit
d12243df0d
2 changed files with 15 additions and 18 deletions
|
@ -6,7 +6,7 @@ use dom::bindings::codegen::Bindings::HTMLScriptElementBinding;
|
||||||
use dom::bindings::codegen::Bindings::HTMLScriptElementBinding::HTMLScriptElementMethods;
|
use dom::bindings::codegen::Bindings::HTMLScriptElementBinding::HTMLScriptElementMethods;
|
||||||
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
||||||
use dom::bindings::codegen::InheritTypes::HTMLScriptElementDerived;
|
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::js::{JSRef, Temporary};
|
||||||
use dom::bindings::utils::{Reflectable, Reflector};
|
use dom::bindings::utils::{Reflectable, Reflector};
|
||||||
use dom::document::Document;
|
use dom::document::Document;
|
||||||
|
@ -14,7 +14,6 @@ use dom::element::{HTMLScriptElementTypeId, Element, AttributeHandlers};
|
||||||
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
|
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
|
||||||
use dom::htmlelement::HTMLElement;
|
use dom::htmlelement::HTMLElement;
|
||||||
use dom::node::{Node, NodeHelpers, ElementNodeTypeId};
|
use dom::node::{Node, NodeHelpers, ElementNodeTypeId};
|
||||||
use dom::text::Text;
|
|
||||||
use servo_util::str::DOMString;
|
use servo_util::str::DOMString;
|
||||||
|
|
||||||
#[deriving(Encodable)]
|
#[deriving(Encodable)]
|
||||||
|
@ -50,15 +49,7 @@ impl<'a> HTMLScriptElementMethods for JSRef<'a, HTMLScriptElement> {
|
||||||
// http://www.whatwg.org/html/#dom-script-text
|
// http://www.whatwg.org/html/#dom-script-text
|
||||||
fn Text(&self) -> DOMString {
|
fn Text(&self) -> DOMString {
|
||||||
let node: &JSRef<Node> = NodeCast::from_ref(self);
|
let node: &JSRef<Node> = NodeCast::from_ref(self);
|
||||||
let mut content = String::new();
|
Node::collect_text_contents(node.children())
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// http://www.whatwg.org/html/#dom-script-text
|
// http://www.whatwg.org/html/#dom-script-text
|
||||||
|
|
|
@ -1408,6 +1408,18 @@ impl Node {
|
||||||
pub unsafe fn unsafe_get_flags(&self) -> *const NodeFlags {
|
pub unsafe fn unsafe_get_flags(&self) -> *const NodeFlags {
|
||||||
mem::transmute(&self.flags)
|
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> {
|
impl<'a> NodeMethods for JSRef<'a, Node> {
|
||||||
|
@ -1553,13 +1565,7 @@ impl<'a> NodeMethods for JSRef<'a, Node> {
|
||||||
match self.type_id {
|
match self.type_id {
|
||||||
DocumentFragmentNodeTypeId |
|
DocumentFragmentNodeTypeId |
|
||||||
ElementNodeTypeId(..) => {
|
ElementNodeTypeId(..) => {
|
||||||
let mut content = String::new();
|
let content = Node::collect_text_contents(self.traverse_preorder());
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(content)
|
Some(content)
|
||||||
}
|
}
|
||||||
CommentNodeTypeId |
|
CommentNodeTypeId |
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue