Skip to content

Commit 3bd33a5

Browse files
committed
Merge branch 'append-or-insert-function' into 3.2.3
2 parents 2db1fe8 + a27d2d3 commit 3bd33a5

9 files changed

Lines changed: 54 additions & 66 deletions

File tree

d3.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ d3 = function() {
484484
var d3_selectionPrototype = d3.selection.prototype = [];
485485
d3_selectionPrototype.select = function(selector) {
486486
var subgroups = [], subgroup, subnode, group, node;
487-
if (typeof selector !== "function") selector = d3_selection_selector(selector);
487+
selector = d3_selection_selector(selector);
488488
for (var j = -1, m = this.length; ++j < m; ) {
489489
subgroups.push(subgroup = []);
490490
subgroup.parentNode = (group = this[j]).parentNode;
@@ -500,13 +500,13 @@ d3 = function() {
500500
return d3_selection(subgroups);
501501
};
502502
function d3_selection_selector(selector) {
503-
return function() {
503+
return typeof selector === "function" ? selector : function() {
504504
return d3_select(selector, this);
505505
};
506506
}
507507
d3_selectionPrototype.selectAll = function(selector) {
508508
var subgroups = [], subgroup, node;
509-
if (typeof selector !== "function") selector = d3_selection_selectorAll(selector);
509+
selector = d3_selection_selectorAll(selector);
510510
for (var j = -1, m = this.length; ++j < m; ) {
511511
for (var group = this[j], i = -1, n = group.length; ++i < n; ) {
512512
if (node = group[i]) {
@@ -518,7 +518,7 @@ d3 = function() {
518518
return d3_selection(subgroups);
519519
};
520520
function d3_selection_selectorAll(selector) {
521-
return function() {
521+
return typeof selector === "function" ? selector : function() {
522522
return d3_selectAll(selector, this);
523523
};
524524
}
@@ -696,25 +696,24 @@ d3 = function() {
696696
}) : this.node().innerHTML;
697697
};
698698
d3_selectionPrototype.append = function(name) {
699-
name = d3.ns.qualify(name);
700-
function append() {
701-
return this.appendChild(d3_document.createElementNS(this.namespaceURI, name));
702-
}
703-
function appendNS() {
704-
return this.appendChild(d3_document.createElementNS(name.space, name.local));
705-
}
706-
return this.select(name.local ? appendNS : append);
699+
name = d3_selection_creator(name);
700+
return this.select(function() {
701+
return this.appendChild(name.apply(this, arguments));
702+
});
707703
};
704+
function d3_selection_creator(name) {
705+
return typeof name === "function" ? name : (name = d3.ns.qualify(name)).local ? function() {
706+
return d3_document.createElementNS(name.space, name.local);
707+
} : function() {
708+
return d3_document.createElementNS(this.namespaceURI, name);
709+
};
710+
}
708711
d3_selectionPrototype.insert = function(name, before) {
709-
name = d3.ns.qualify(name);
710-
if (typeof before !== "function") before = d3_selection_selector(before);
711-
function insert(d, i) {
712-
return this.insertBefore(d3_document.createElementNS(this.namespaceURI, name), before.call(this, d, i));
713-
}
714-
function insertNS(d, i) {
715-
return this.insertBefore(d3_document.createElementNS(name.space, name.local), before.call(this, d, i));
716-
}
717-
return this.select(name.local ? insertNS : insert);
712+
name = d3_selection_creator(name);
713+
before = d3_selection_selector(before);
714+
return this.select(function() {
715+
return this.insertBefore(name.apply(this, arguments), before.apply(this, arguments));
716+
});
718717
};
719718
d3_selectionPrototype.remove = function() {
720719
return this.each(function() {
@@ -7444,7 +7443,7 @@ d3 = function() {
74447443
d3.transition.prototype = d3_transitionPrototype;
74457444
d3_transitionPrototype.select = function(selector) {
74467445
var id = this.id, subgroups = [], subgroup, subnode, node;
7447-
if (typeof selector !== "function") selector = d3_selection_selector(selector);
7446+
selector = d3_selection_selector(selector);
74487447
for (var j = -1, m = this.length; ++j < m; ) {
74497448
subgroups.push(subgroup = []);
74507449
for (var group = this[j], i = -1, n = group.length; ++i < n; ) {
@@ -7461,7 +7460,7 @@ d3 = function() {
74617460
};
74627461
d3_transitionPrototype.selectAll = function(selector) {
74637462
var id = this.id, subgroups = [], subgroup, subnodes, node, subnode, transition;
7464-
if (typeof selector !== "function") selector = d3_selection_selectorAll(selector);
7463+
selector = d3_selection_selectorAll(selector);
74657464
for (var j = -1, m = this.length; ++j < m; ) {
74667465
for (var group = this[j], i = -1, n = group.length; ++i < n; ) {
74677466
if (node = group[i]) {

d3.min.js

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

src/selection/append.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ import "../core/document";
22
import "../core/ns";
33
import "selection";
44

5-
// TODO append(node)?
6-
// TODO append(function)?
75
d3_selectionPrototype.append = function(name) {
8-
name = d3.ns.qualify(name);
9-
10-
function append() {
11-
return this.appendChild(d3_document.createElementNS(this.namespaceURI, name));
12-
}
13-
14-
function appendNS() {
15-
return this.appendChild(d3_document.createElementNS(name.space, name.local));
16-
}
17-
18-
return this.select(name.local ? appendNS : append);
6+
name = d3_selection_creator(name);
7+
return this.select(function() {
8+
return this.appendChild(name.apply(this, arguments));
9+
});
1910
};
11+
12+
function d3_selection_creator(name) {
13+
return typeof name === "function" ? name
14+
: (name = d3.ns.qualify(name)).local ? function() { return d3_document.createElementNS(name.space, name.local); }
15+
: function() { return d3_document.createElementNS(this.namespaceURI, name); };
16+
}

src/selection/insert.js

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1-
import "../core/document";
2-
import "../core/ns";
31
import "selection";
42

53
d3_selectionPrototype.insert = function(name, before) {
6-
name = d3.ns.qualify(name);
7-
8-
if (typeof before !== "function") before = d3_selection_selector(before);
9-
10-
function insert(d, i) {
11-
return this.insertBefore(
12-
d3_document.createElementNS(this.namespaceURI, name),
13-
before.call(this, d, i));
14-
}
15-
16-
function insertNS(d, i) {
17-
return this.insertBefore(
18-
d3_document.createElementNS(name.space, name.local),
19-
before.call(this, d, i));
20-
}
21-
22-
return this.select(name.local ? insertNS : insert);
4+
name = d3_selection_creator(name);
5+
before = d3_selection_selector(before);
6+
return this.select(function() {
7+
return this.insertBefore(name.apply(this, arguments), before.apply(this, arguments));
8+
});
239
};

src/selection/select.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ d3_selectionPrototype.select = function(selector) {
77
group,
88
node;
99

10-
if (typeof selector !== "function") selector = d3_selection_selector(selector);
10+
selector = d3_selection_selector(selector);
1111

1212
for (var j = -1, m = this.length; ++j < m;) {
1313
subgroups.push(subgroup = []);
@@ -26,7 +26,7 @@ d3_selectionPrototype.select = function(selector) {
2626
};
2727

2828
function d3_selection_selector(selector) {
29-
return function() {
29+
return typeof selector === "function" ? selector : function() {
3030
return d3_select(selector, this);
3131
};
3232
}

src/selection/selectAll.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ d3_selectionPrototype.selectAll = function(selector) {
66
subgroup,
77
node;
88

9-
if (typeof selector !== "function") selector = d3_selection_selectorAll(selector);
9+
selector = d3_selection_selectorAll(selector);
1010

1111
for (var j = -1, m = this.length; ++j < m;) {
1212
for (var group = this[j], i = -1, n = group.length; ++i < n;) {
@@ -21,7 +21,7 @@ d3_selectionPrototype.selectAll = function(selector) {
2121
};
2222

2323
function d3_selection_selectorAll(selector) {
24-
return function() {
24+
return typeof selector === "function" ? selector : function() {
2525
return d3_selectAll(selector, this);
2626
};
2727
}

src/transition/select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ d3_transitionPrototype.select = function(selector) {
88
subnode,
99
node;
1010

11-
if (typeof selector !== "function") selector = d3_selection_selector(selector);
11+
selector = d3_selection_selector(selector);
1212

1313
for (var j = -1, m = this.length; ++j < m;) {
1414
subgroups.push(subgroup = []);

src/transition/selectAll.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ d3_transitionPrototype.selectAll = function(selector) {
1010
subnode,
1111
transition;
1212

13-
if (typeof selector !== "function") selector = d3_selection_selectorAll(selector);
13+
selector = d3_selection_selectorAll(selector);
1414

1515
for (var j = -1, m = this.length; ++j < m;) {
1616
for (var group = this[j], i = -1, n = group.length; ++i < n;) {

test/selection/append-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ suite.addBatch({
2525
assert.isTrue(svg[0][0].parentNode === body.node());
2626
assert.isTrue(svg[0][0] === body.node().lastChild);
2727
},
28+
"appends an element specified as a function": function(body) {
29+
var svg = body.select("svg").remove().node();
30+
assert.isFalse(svg === body.node().lastChild);
31+
body.append(function() { return svg; });
32+
assert.isTrue(svg === body.node().lastChild);
33+
},
2834
"propagates data to new element": function(body) {
2935
var data = new Object(), div = body.data([data]).append("div");
3036
assert.strictEqual(div[0][0].__data__, data);

0 commit comments

Comments
 (0)