servo: Merge #14928 - Unify text insertion when parsing HTML and XML (from nox:insert); r=SimonSapin

Source-Repo: https://github.com/servo/servo
Source-Revision: f54dd0112bd9e51b21e9a5ee38bb2cfc0326e071
This commit is contained in:
Anthony Ramine 2017-01-10 03:51:47 -08:00
Родитель acccadb9f2
Коммит 5754480f7d
3 изменённых файлов: 26 добавлений и 29 удалений

Просмотреть файл

@ -19,7 +19,6 @@ use dom::htmlscriptelement::HTMLScriptElement;
use dom::htmltemplateelement::HTMLTemplateElement;
use dom::node::Node;
use dom::processinginstruction::ProcessingInstruction;
use dom::text::Text;
use dom::virtualmethods::vtable_for;
use html5ever::Attribute;
use html5ever::serialize::{AttrRef, Serializable, Serializer};
@ -180,7 +179,7 @@ impl TreeSink for Sink {
None => return Err(new_node),
};
insert(&parent, Some(&*sibling), new_node);
super::insert(&parent, Some(&*sibling), new_node);
Ok(())
}
@ -198,8 +197,7 @@ impl TreeSink for Sink {
}
fn append(&mut self, parent: JS<Node>, child: NodeOrText<JS<Node>>) {
// FIXME(#3701): Use a simpler algorithm and merge adjacent text nodes
insert(&parent, None, child);
super::insert(&parent, None, child);
}
fn append_doctype_to_document(&mut self, name: StrTendril, public_id: StrTendril,
@ -242,23 +240,6 @@ impl TreeSink for Sink {
}
}
fn insert(parent: &Node, reference_child: Option<&Node>, child: NodeOrText<JS<Node>>) {
match child {
NodeOrText::AppendNode(n) => {
assert!(parent.InsertBefore(&n, reference_child).is_ok());
},
NodeOrText::AppendText(t) => {
if let Some(text) = parent.GetLastChild().and_then(Root::downcast::<Text>) {
text.upcast::<CharacterData>().append_data(&t);
} else {
let s: String = t.into();
let text = Text::new(DOMString::from(s), &parent.owner_doc());
parent.InsertBefore(text.upcast(), reference_child).unwrap();
}
}
}
}
impl<'a> Serializable for &'a Node {
fn serialize<'wr, Wr: Write>(&self, serializer: &mut Serializer<'wr, Wr>,
traversal_scope: TraversalScope) -> io::Result<()> {

Просмотреть файл

@ -13,6 +13,7 @@ use dom::bindings::js::{JS, Root, RootedReference};
use dom::bindings::refcounted::Trusted;
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::str::DOMString;
use dom::characterdata::CharacterData;
use dom::document::{Document, DocumentSource, IsHTMLDocument};
use dom::element::Element;
use dom::globalscope::GlobalScope;
@ -20,9 +21,11 @@ use dom::htmlformelement::HTMLFormElement;
use dom::htmlimageelement::HTMLImageElement;
use dom::htmlscriptelement::HTMLScriptElement;
use dom::node::{Node, NodeSiblingIterator};
use dom::text::Text;
use encoding::all::UTF_8;
use encoding::types::{DecoderTrap, Encoding};
use html5ever::tokenizer::buffer_queue::BufferQueue;
use html5ever::tree_builder::NodeOrText;
use hyper::header::ContentType;
use hyper::mime::{Mime, SubLevel, TopLevel};
use hyper_serde::Serde;
@ -559,3 +562,20 @@ pub struct FragmentContext<'a> {
pub context_elem: &'a Node,
pub form_elem: Option<&'a Node>,
}
#[allow(unrooted_must_root)]
fn insert(parent: &Node, reference_child: Option<&Node>, child: NodeOrText<JS<Node>>) {
match child {
NodeOrText::AppendNode(n) => {
parent.InsertBefore(&n, reference_child).unwrap();
},
NodeOrText::AppendText(t) => {
if let Some(text) = parent.GetLastChild().and_then(Root::downcast::<Text>) {
text.upcast::<CharacterData>().append_data(&t);
} else {
let text = Text::new(String::from(t).into(), &parent.owner_doc());
parent.InsertBefore(text.upcast(), reference_child).unwrap();
}
}
}
}

Просмотреть файл

@ -16,9 +16,9 @@ use dom::element::{Element, ElementCreator};
use dom::htmlscriptelement::HTMLScriptElement;
use dom::node::Node;
use dom::processinginstruction::ProcessingInstruction;
use dom::text::Text;
use dom::virtualmethods::vtable_for;
use html5ever::tokenizer::buffer_queue::BufferQueue;
use html5ever::tree_builder::{NodeOrText as H5eNodeOrText};
use html5ever_atoms::{Prefix, QualName};
use js::jsapi::JSTracer;
use servo_url::ServoUrl;
@ -155,14 +155,10 @@ impl TreeSink for Sink {
fn append(&mut self, parent: JS<Node>, child: NodeOrText<JS<Node>>) {
let child = match child {
NodeOrText::AppendNode(n) => Root::from_ref(&*n),
NodeOrText::AppendText(t) => {
let s: String = t.into();
let text = Text::new(DOMString::from(s), &self.document);
Root::upcast(text)
}
NodeOrText::AppendNode(n) => H5eNodeOrText::AppendNode(n),
NodeOrText::AppendText(s) => H5eNodeOrText::AppendText(s),
};
assert!(parent.AppendChild(&child).is_ok());
super::insert(&*parent, None, child);
}
fn append_doctype_to_document(&mut self, name: StrTendril, public_id: StrTendril,