Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
define([ | ||
"mout/math/clamp", | ||
"./append", | ||
"../utils/allNodes" | ||
], function(clamp, append, allNodes) { | ||
|
||
function insertAt(parent, nodes, index) { | ||
|
||
index = Math.max(index, 0); | ||
|
||
if (index >= parent.children.length) { | ||
append(parent, nodes); | ||
} else { | ||
var sibling; | ||
allNodes(nodes, function(node) { | ||
sibling = parent.children[ index ]; | ||
parent.insertBefore(node, sibling); | ||
index++; | ||
}); | ||
} | ||
} | ||
|
||
return insertAt; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
define(["cane/dom/insertAt"], function(insertAt) { | ||
|
||
describe("dom/insertAt", function() { | ||
|
||
it("should insert node at index", function() { | ||
var parent = document.createElement("div"), | ||
first = document.createElement("span"), | ||
second = document.createElement("span"), | ||
third = document.createElement("span"), | ||
div = document.createElement("div"); | ||
|
||
parent.appendChild(first); | ||
parent.appendChild(second); | ||
parent.appendChild(third); | ||
|
||
insertAt(parent, div, 1); | ||
|
||
expect(parent.children[1]).to.be(div); | ||
}); | ||
|
||
it("should append if index is higher then children", function() { | ||
var parent = document.createElement("div"), | ||
first = document.createElement("span"), | ||
second = document.createElement("span"), | ||
third = document.createElement("span"), | ||
div = document.createElement("div"); | ||
|
||
parent.appendChild(first); | ||
parent.appendChild(second); | ||
parent.appendChild(third); | ||
|
||
insertAt(parent, div, 10); | ||
|
||
expect(parent.children[3]).to.be(div); | ||
}); | ||
|
||
it("should prepend node if index small than 1", function() { | ||
var parent = document.createElement("div"), | ||
first = document.createElement("span"), | ||
second = document.createElement("span"), | ||
third = document.createElement("span"), | ||
div = document.createElement("div"); | ||
|
||
parent.appendChild(first); | ||
parent.appendChild(second); | ||
parent.appendChild(third); | ||
|
||
insertAt(parent, div, -25); | ||
|
||
expect(parent.children[0]).to.be(div); | ||
}); | ||
|
||
it("should insert multiple nodes", function() { | ||
var parent = document.createElement("div"), | ||
first = document.createElement("span"), | ||
second = document.createElement("span"), | ||
third = document.createElement("span"), | ||
foo = document.createElement("div"), | ||
bar = document.createElement("div"); | ||
|
||
parent.appendChild(first); | ||
parent.appendChild(second); | ||
parent.appendChild(third); | ||
|
||
insertAt(parent, [foo, bar], 1); | ||
|
||
expect(parent.children[1]).to.be(foo); | ||
expect(parent.children[2]).to.be(bar); | ||
}); | ||
|
||
}); | ||
|
||
}); |