servo: Merge #8964 - Fix Node::replace_with (from nox:replacewith); r=frewsxcv
authorAnthony Ramine <n.oxyde@gmail.com>
Mon, 14 Dec 2015 10:14:51 +0500
changeset 337833 7cc474579138bfcb9ccb59702ba48ef26e5cd838
parent 337832 846e889bdf69ac8415dc0254fe49945908c26140
child 337834 cd57d79c5d79ece52d7d6459c03f2c929e3f1744
push id86548
push userkwierso@gmail.com
push dateSat, 04 Feb 2017 01:35:21 +0000
treeherdermozilla-inbound@e7b96d015d03 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfrewsxcv
servo: Merge #8964 - Fix Node::replace_with (from nox:replacewith); r=frewsxcv Source-Repo: https://github.com/servo/servo Source-Revision: cbd2c9560babbad96eaeb3608bada012103d691b
servo/components/script/dom/node.rs
--- a/servo/components/script/dom/node.rs
+++ b/servo/components/script/dom/node.rs
@@ -661,29 +661,35 @@ impl Node {
         // Step 5.
         try!(Node::pre_insert(&node, &parent, viable_next_sibling.r()));
 
         Ok(())
     }
 
     // https://dom.spec.whatwg.org/#dom-childnode-replacewith
     pub fn replace_with(&self, nodes: Vec<NodeOrString>) -> ErrorResult {
-        match self.parent_node.get() {
-            None => {
-                // Step 1.
-                Ok(())
-            },
-            Some(ref parent_node) => {
-                // Step 2.
-                let doc = self.owner_doc();
-                let node = try!(doc.node_from_nodes_and_strings(nodes));
-                // Step 3.
-                parent_node.ReplaceChild(node.r(), self).map(|_| ())
-            },
+        // Step 1.
+        let parent = if let Some(parent) = self.GetParentNode() {
+            parent
+        } else {
+            // Step 2.
+            return Ok(());
+        };
+        // Step 3.
+        let viable_next_sibling = first_node_not_in(self.following_siblings(), &nodes);
+        // Step 4.
+        let node = try!(self.owner_doc().node_from_nodes_and_strings(nodes));
+        if self.parent_node == Some(&*parent) {
+            // Step 5.
+            try!(parent.ReplaceChild(&node, self));
+        } else {
+            // Step 6.
+            try!(Node::pre_insert(&node, &parent, viable_next_sibling.r()));
         }
+        Ok(())
     }
 
     // https://dom.spec.whatwg.org/#dom-parentnode-prepend
     pub fn prepend(&self, nodes: Vec<NodeOrString>) -> ErrorResult {
         // Step 1.
         let doc = self.owner_doc();
         let node = try!(doc.node_from_nodes_and_strings(nodes));
         // Step 2.