mirror of
https://github.com/servo/servo.git
synced 2025-08-04 05:00:08 +01:00
Use StrBuf more efficiently in htmlserializer.
This commit is contained in:
parent
2717ab65ec
commit
d1ca380482
1 changed files with 74 additions and 60 deletions
|
@ -25,51 +25,53 @@ pub fn serialize(iterator: &mut NodeIterator) -> ~str {
|
||||||
|
|
||||||
for node in *iterator {
|
for node in *iterator {
|
||||||
while open_elements.len() > iterator.depth {
|
while open_elements.len() > iterator.depth {
|
||||||
html.push_str("</".to_owned() + open_elements.pop().unwrap().as_slice() + ">");
|
html.push_str("</");
|
||||||
|
html.push_str(open_elements.pop().unwrap().as_slice());
|
||||||
|
html.push_str(">");
|
||||||
}
|
}
|
||||||
html.push_str(
|
|
||||||
match node.type_id() {
|
match node.type_id() {
|
||||||
ElementNodeTypeId(..) => {
|
ElementNodeTypeId(..) => {
|
||||||
let elem: &JSRef<Element> = ElementCast::to_ref(&node).unwrap();
|
let elem: &JSRef<Element> = ElementCast::to_ref(&node).unwrap();
|
||||||
serialize_elem(elem, &mut open_elements)
|
serialize_elem(elem, &mut open_elements, &mut html)
|
||||||
}
|
}
|
||||||
CommentNodeTypeId => {
|
CommentNodeTypeId => {
|
||||||
let comment: &JSRef<Comment> = CommentCast::to_ref(&node).unwrap();
|
let comment: &JSRef<Comment> = CommentCast::to_ref(&node).unwrap();
|
||||||
serialize_comment(comment)
|
serialize_comment(comment, &mut html)
|
||||||
}
|
}
|
||||||
TextNodeTypeId => {
|
TextNodeTypeId => {
|
||||||
let text: &JSRef<Text> = TextCast::to_ref(&node).unwrap();
|
let text: &JSRef<Text> = TextCast::to_ref(&node).unwrap();
|
||||||
serialize_text(text)
|
serialize_text(text, &mut html)
|
||||||
}
|
}
|
||||||
DoctypeNodeTypeId => {
|
DoctypeNodeTypeId => {
|
||||||
let doctype: &JSRef<DocumentType> = DocumentTypeCast::to_ref(&node).unwrap();
|
let doctype: &JSRef<DocumentType> = DocumentTypeCast::to_ref(&node).unwrap();
|
||||||
serialize_doctype(doctype)
|
serialize_doctype(doctype, &mut html)
|
||||||
}
|
}
|
||||||
ProcessingInstructionNodeTypeId => {
|
ProcessingInstructionNodeTypeId => {
|
||||||
let processing_instruction: &JSRef<ProcessingInstruction> =
|
let processing_instruction: &JSRef<ProcessingInstruction> =
|
||||||
ProcessingInstructionCast::to_ref(&node).unwrap();
|
ProcessingInstructionCast::to_ref(&node).unwrap();
|
||||||
serialize_processing_instruction(processing_instruction)
|
serialize_processing_instruction(processing_instruction, &mut html)
|
||||||
}
|
|
||||||
DocumentFragmentNodeTypeId => {
|
|
||||||
"".to_owned()
|
|
||||||
}
|
}
|
||||||
|
DocumentFragmentNodeTypeId => {}
|
||||||
DocumentNodeTypeId => {
|
DocumentNodeTypeId => {
|
||||||
fail!("It shouldn't be possible to serialize a document node")
|
fail!("It shouldn't be possible to serialize a document node")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
|
||||||
}
|
}
|
||||||
while open_elements.len() > 0 {
|
while open_elements.len() > 0 {
|
||||||
html.push_str("</".to_owned() + open_elements.pop().unwrap().as_slice() + ">");
|
html.push_str("</");
|
||||||
|
html.push_str(open_elements.pop().unwrap().as_slice());
|
||||||
|
html.push_str(">");
|
||||||
}
|
}
|
||||||
html.into_owned()
|
html.into_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_comment(comment: &JSRef<Comment>) -> ~str {
|
fn serialize_comment(comment: &JSRef<Comment>, html: &mut StrBuf) {
|
||||||
"<!--".to_owned() + comment.deref().characterdata.data + "-->"
|
html.push_str("<!--");
|
||||||
|
html.push_str(comment.deref().characterdata.data);
|
||||||
|
html.push_str("-->");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_text(text: &JSRef<Text>) -> ~str {
|
fn serialize_text(text: &JSRef<Text>, html: &mut StrBuf) {
|
||||||
let text_node: &JSRef<Node> = NodeCast::from_ref(text);
|
let text_node: &JSRef<Node> = NodeCast::from_ref(text);
|
||||||
match text_node.parent_node().map(|node| node.root()) {
|
match text_node.parent_node().map(|node| node.root()) {
|
||||||
Some(ref parent) if parent.is_element() => {
|
Some(ref parent) if parent.is_element() => {
|
||||||
|
@ -77,33 +79,39 @@ fn serialize_text(text: &JSRef<Text>) -> ~str {
|
||||||
match elem.deref().local_name.as_slice() {
|
match elem.deref().local_name.as_slice() {
|
||||||
"style" | "script" | "xmp" | "iframe" |
|
"style" | "script" | "xmp" | "iframe" |
|
||||||
"noembed" | "noframes" | "plaintext" |
|
"noembed" | "noframes" | "plaintext" |
|
||||||
"noscript" if elem.deref().namespace == namespace::HTML => {
|
"noscript" if elem.deref().namespace == namespace::HTML
|
||||||
text.deref().characterdata.data.clone()
|
=> html.push_str(text.deref().characterdata.data),
|
||||||
},
|
_ => html.push_str(escape(text.deref().characterdata.data, false))
|
||||||
_ => escape(text.deref().characterdata.data, false)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => escape(text.deref().characterdata.data, false)
|
_ => html.push_str(escape(text.deref().characterdata.data, false))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_processing_instruction(processing_instruction: &JSRef<ProcessingInstruction>) -> ~str {
|
fn serialize_processing_instruction(processing_instruction: &JSRef<ProcessingInstruction>,
|
||||||
"<?".to_owned() + processing_instruction.deref().target + " " + processing_instruction.deref().characterdata.data + "?>"
|
html: &mut StrBuf) {
|
||||||
|
html.push_str("<?");
|
||||||
|
html.push_str(processing_instruction.deref().target);
|
||||||
|
html.push_char(' ');
|
||||||
|
html.push_str(processing_instruction.deref().characterdata.data);
|
||||||
|
html.push_str("?>");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_doctype(doctype: &JSRef<DocumentType>) -> ~str {
|
fn serialize_doctype(doctype: &JSRef<DocumentType>, html: &mut StrBuf) {
|
||||||
"<!DOCTYPE".to_owned() + doctype.deref().name + ">"
|
html.push_str("<!DOCTYPE");
|
||||||
|
html.push_str(doctype.deref().name);
|
||||||
|
html.push_char('>');
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_elem(elem: &JSRef<Element>, open_elements: &mut Vec<~str>) -> ~str {
|
fn serialize_elem(elem: &JSRef<Element>, open_elements: &mut Vec<~str>, html: &mut StrBuf) {
|
||||||
let mut rv = StrBuf::new();
|
html.push_char('<');
|
||||||
rv.push_str("<");
|
html.push_str(elem.deref().local_name);
|
||||||
rv.push_str(elem.deref().local_name);
|
|
||||||
for attr in elem.deref().attrs.iter() {
|
for attr in elem.deref().attrs.iter() {
|
||||||
let attr = attr.root();
|
let attr = attr.root();
|
||||||
rv.push_str(serialize_attr(&*attr));
|
serialize_attr(&*attr, html);
|
||||||
};
|
};
|
||||||
rv.push_str(">");
|
html.push_char('>');
|
||||||
|
|
||||||
match elem.deref().local_name.as_slice() {
|
match elem.deref().local_name.as_slice() {
|
||||||
"pre" | "listing" | "textarea" if elem.deref().namespace == namespace::HTML => {
|
"pre" | "listing" | "textarea" if elem.deref().namespace == namespace::HTML => {
|
||||||
let node: &JSRef<Node> = NodeCast::from_ref(elem);
|
let node: &JSRef<Node> = NodeCast::from_ref(elem);
|
||||||
|
@ -111,7 +119,7 @@ fn serialize_elem(elem: &JSRef<Element>, open_elements: &mut Vec<~str>) -> ~str
|
||||||
Some(ref child) if child.is_text() => {
|
Some(ref child) if child.is_text() => {
|
||||||
let text: &JSRef<CharacterData> = CharacterDataCast::to_ref(&**child).unwrap();
|
let text: &JSRef<CharacterData> = CharacterDataCast::to_ref(&**child).unwrap();
|
||||||
if text.deref().data.len() > 0 && text.deref().data[0] == 0x0A as u8 {
|
if text.deref().data.len() > 0 && text.deref().data[0] == 0x0A as u8 {
|
||||||
rv.push_str("\x0A");
|
html.push_char('\x0A');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -119,26 +127,32 @@ fn serialize_elem(elem: &JSRef<Element>, open_elements: &mut Vec<~str>) -> ~str
|
||||||
},
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !elem.deref().is_void() {
|
if !elem.deref().is_void() {
|
||||||
open_elements.push(elem.deref().local_name.clone());
|
open_elements.push(elem.deref().local_name.clone());
|
||||||
}
|
}
|
||||||
rv.into_owned()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_attr(attr: &JSRef<Attr>) -> ~str {
|
fn serialize_attr(attr: &JSRef<Attr>, html: &mut StrBuf) {
|
||||||
let attr_name = if attr.deref().namespace == namespace::XML {
|
html.push_char(' ');
|
||||||
"xml:".to_owned() + attr.deref().local_name.clone()
|
if attr.deref().namespace == namespace::XML {
|
||||||
|
html.push_str("xml:");
|
||||||
|
html.push_str(attr.deref().local_name);
|
||||||
} else if attr.deref().namespace == namespace::XMLNS &&
|
} else if attr.deref().namespace == namespace::XMLNS &&
|
||||||
attr.deref().local_name.as_slice() == "xmlns" {
|
attr.deref().local_name.as_slice() == "xmlns" {
|
||||||
"xmlns".to_owned()
|
html.push_str("xmlns");
|
||||||
} else if attr.deref().namespace == namespace::XMLNS {
|
} else if attr.deref().namespace == namespace::XMLNS {
|
||||||
"xmlns:".to_owned() + attr.deref().local_name.clone()
|
html.push_str("xmlns:");
|
||||||
|
html.push_str(attr.deref().local_name);
|
||||||
} else if attr.deref().namespace == namespace::XLink {
|
} else if attr.deref().namespace == namespace::XLink {
|
||||||
"xlink:".to_owned() + attr.deref().local_name.clone()
|
html.push_str("xlink:");
|
||||||
|
html.push_str(attr.deref().local_name);
|
||||||
} else {
|
} else {
|
||||||
attr.deref().name.clone()
|
html.push_str(attr.deref().name);
|
||||||
};
|
};
|
||||||
" ".to_owned() + attr_name + "=\"" + escape(attr.deref().value, true) + "\""
|
html.push_str("=\"");
|
||||||
|
html.push_str(escape(attr.deref().value, true));
|
||||||
|
html.push_char('"');
|
||||||
}
|
}
|
||||||
|
|
||||||
fn escape(string: &str, attr_mode: bool) -> ~str {
|
fn escape(string: &str, attr_mode: bool) -> ~str {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue