diff --git a/servo/components/script/dom/servoparser/mod.rs b/servo/components/script/dom/servoparser/mod.rs index 531f13274e632..3904910d3a30e 100644 --- a/servo/components/script/dom/servoparser/mod.rs +++ b/servo/components/script/dom/servoparser/mod.rs @@ -657,7 +657,12 @@ fn insert(parent: &Node, reference_child: Option<&Node>, child: NodeOrText { - if let Some(text) = parent.GetLastChild().and_then(Root::downcast::) { + let text = reference_child + .and_then(Node::GetPreviousSibling) + .or_else(|| parent.GetLastChild()) + .and_then(Root::downcast::); + + if let Some(text) = text { text.upcast::().append_data(&t); } else { let text = Text::new(String::from(t).into(), &parent.owner_doc());