[DOM Mock] Fix insertBefore when the reference node follows the moving child in a nodeList

This commit is contained in:
Pierre-Yves Gérardy 2018-04-20 23:00:50 +02:00 committed by Pierre-Yves Gérardy
parent c6693aa361
commit 9136585fe4
2 changed files with 23 additions and 0 deletions

View file

@ -101,6 +101,7 @@ module.exports = function(options) {
if (index > -1) this.childNodes.splice(index, 1)
if (reference === null) this.appendChild(child)
else {
if (index !== -1 && refIndex > index) refIndex--
if (child.nodeType === 11) {
this.childNodes.splice.apply(this.childNodes, [refIndex, 0].concat(child.childNodes))
while (child.firstChild) {

View file

@ -242,6 +242,28 @@ o.spec("domMock", function() {
o(a.parentNode).equals(parent)
o(b.parentNode).equals(parent)
})
o("moves existing node forward but not at the end", function() {
var parent = $document.createElement("div")
var a = $document.createElement("a")
var b = $document.createElement("b")
var c = $document.createElement("c")
parent.appendChild(a)
parent.appendChild(b)
parent.appendChild(c)
parent.insertBefore(a, c)
o(parent.childNodes.length).equals(3)
o(parent.childNodes[0]).equals(b)
o(parent.childNodes[1]).equals(a)
o(parent.childNodes[2]).equals(c)
o(parent.firstChild).equals(b)
o(parent.firstChild.nextSibling).equals(a)
o(parent.firstChild.nextSibling.nextSibling).equals(c)
o(a.parentNode).equals(parent)
o(b.parentNode).equals(parent)
o(c.parentNode).equals(parent)
})
o("removes from old parent", function() {
var parent = $document.createElement("div")
var source = $document.createElement("span")