Skip to content

Commit aad4969

Browse files
committed
Merge branch 'insert-function' into 3.1.0
2 parents d80cc86 + 873db8d commit aad4969

4 files changed

Lines changed: 22 additions & 14 deletions

File tree

d3.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,11 +1695,12 @@ d3 = function() {
16951695
};
16961696
d3_selectionPrototype.insert = function(name, before) {
16971697
name = d3.ns.qualify(name);
1698-
function insert() {
1699-
return this.insertBefore(d3_document.createElementNS(this.namespaceURI, name), d3_select(before, this));
1698+
if (typeof before !== "function") before = d3_selection_selector(before);
1699+
function insert(d, i) {
1700+
return this.insertBefore(d3_document.createElementNS(this.namespaceURI, name), before.call(this, d, i));
17001701
}
1701-
function insertNS() {
1702-
return this.insertBefore(d3_document.createElementNS(name.space, name.local), d3_select(before, this));
1702+
function insertNS(d, i) {
1703+
return this.insertBefore(d3_document.createElementNS(name.space, name.local), before.call(this, d, i));
17031704
}
17041705
return this.select(name.local ? insertNS : insert);
17051706
};

d3.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/selection-insert.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
// TODO insert(node, function)?
2-
// TODO insert(function, string)?
3-
// TODO insert(function, function)?
41
d3_selectionPrototype.insert = function(name, before) {
52
name = d3.ns.qualify(name);
63

7-
function insert() {
4+
if (typeof before !== "function") before = d3_selection_selector(before);
5+
6+
function insert(d, i) {
87
return this.insertBefore(
98
d3_document.createElementNS(this.namespaceURI, name),
10-
d3_select(before, this));
9+
before.call(this, d, i));
1110
}
1211

13-
function insertNS() {
12+
function insertNS(d, i) {
1413
return this.insertBefore(
1514
d3_document.createElementNS(name.space, name.local),
16-
d3_select(before, this));
15+
before.call(this, d, i));
1716
}
1817

1918
return this.select(name.local ? insertNS : insert);

test/core/selection-insert-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ suite.addBatch({
1818
assert.domEqual(div[0][0], document.body.firstChild);
1919
assert.domEqual(div[0][0].nextSibling, span[0][0]);
2020
},
21+
"inserts before the specified node": function(body) {
22+
var span = body.html("").append("span");
23+
var div = body.insert("div", function() { return span.node(); });
24+
assert.equal(div[0][0].tagName, "DIV");
25+
assert.isNull(div[0][0].namespaceURI);
26+
assert.domEqual(div[0][0], document.body.firstChild);
27+
assert.domEqual(div[0][0].nextSibling, span[0][0]);
28+
},
2129
"appends an HTML element": function(body) {
2230
var div = body.insert("div");
2331
assert.equal(div[0][0].tagName, "DIV");

0 commit comments

Comments
 (0)