Skip to content

Commit 8da80dc

Browse files
committed
Support for Sizzle.
D3 now supports Sizzle, preferring Sizzle to the native Selectors API if Sizzle is available. Sizzle internally uses the native Selectors API and thus this should have minimal performance implications; also, it allows you to use Sizzle proprietary extensions such as ":first". This commit also restricts the definition of the enter selection so that only append and insert operations are defined. The other operations were generally unsupported anyway, and it cleans up the code to have separate implementations for insert and append. (I might enable additional operations in the future, such as `filter`, `sort` and `each`, but this seems like a reasonable first pass.)
1 parent c31590e commit 8da80dc

10 files changed

Lines changed: 8590 additions & 124 deletions

File tree

d3.js

Lines changed: 92 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function(){d3 = {version: "0.30.8"}; // semver
1+
(function(){d3 = {version: "1.0.0"}; // semver
22
if (!Date.now) Date.now = function() {
33
return +new Date();
44
};
@@ -923,20 +923,29 @@ function d3_hsl_rgb(h, s, l) {
923923

924924
return d3_rgb(vv(h + 120), vv(h), vv(h - 120));
925925
}
926+
var d3_select = function(s, n) { return n.querySelector(s); },
927+
d3_selectAll = function(s, n) { return d3_array(n.querySelectorAll(s)); };
928+
929+
// Use Sizzle, if available.
930+
if (typeof Sizzle == "function") {
931+
d3_select = function(s, n) { return Sizzle(s, n)[0]; };
932+
d3_selectAll = Sizzle;
933+
}
934+
926935
var d3_root = d3_selection([[document]]);
927936
d3_root[0].parentNode = document.documentElement;
928937

929938
// TODO fast singleton implementation!
930-
d3.select = function(query) {
931-
return typeof query == "string"
932-
? d3_root.select(query)
933-
: d3_selection([[query]]); // assume node
939+
d3.select = function(selector) {
940+
return typeof selector == "string"
941+
? d3_root.select(selector)
942+
: d3_selection([[selector]]); // assume node
934943
};
935944

936-
d3.selectAll = function(query) {
937-
return typeof query == "string"
938-
? d3_root.selectAll(query)
939-
: d3_selection([d3_array(query)]); // assume node[]
945+
d3.selectAll = function(selector) {
946+
return typeof selector == "string"
947+
? d3_root.selectAll(selector)
948+
: d3_selection([d3_array(selector)]); // assume node[]
940949
};
941950

942951
function d3_selection(groups) {
@@ -983,16 +992,16 @@ function d3_selection(groups) {
983992
}
984993

985994
// TODO select(function)?
986-
groups.select = function(query) {
995+
groups.select = function(selector) {
987996
return select(function(node) {
988-
return node.querySelector(query);
997+
return d3_select(selector, node);
989998
});
990999
};
9911000

9921001
// TODO selectAll(function)?
993-
groups.selectAll = function(query) {
1002+
groups.selectAll = function(selector) {
9941003
return selectAll(function(node) {
995-
return d3_array(node.querySelectorAll(query));
1004+
return d3_selectAll(selector, node);
9961005
});
9971006
};
9981007

@@ -1035,18 +1044,7 @@ function d3_selection(groups) {
10351044
nodeData;
10361045

10371046
function enterNode(data) {
1038-
return {
1039-
__data__: data,
1040-
appendChild: function(a) {
1041-
return group.parentNode.appendChild(a);
1042-
},
1043-
insertBefore: function(a, b) {
1044-
return group.parentNode.insertBefore(a, b);
1045-
},
1046-
querySelector: function(a) {
1047-
return group.parentNode.querySelector(a);
1048-
}
1049-
};
1047+
return {__data__: data};
10501048
}
10511049

10521050
if (join) {
@@ -1137,7 +1135,7 @@ function d3_selection(groups) {
11371135

11381136
var selection = d3_selection(update);
11391137
selection.enter = function() {
1140-
return d3_selection(enter);
1138+
return d3_selectionEnter(enter);
11411139
};
11421140
selection.exit = function() {
11431141
return d3_selection(exit);
@@ -1409,19 +1407,19 @@ function d3_selection(groups) {
14091407
function insert(node) {
14101408
return node.insertBefore(
14111409
document.createElement(name),
1412-
node.querySelector(before));
1410+
d3_select(before, node));
14131411
}
14141412

14151413
function insertNS(node) {
14161414
return node.insertBefore(
14171415
document.createElementNS(name.space, name.local),
1418-
node.querySelector(before));
1416+
d3_select(before, node));
14191417
}
14201418

14211419
return select(name.local ? insertNS : insert);
14221420
};
14231421

1424-
// TODO remove(query)?
1422+
// TODO remove(selector)?
14251423
// TODO remove(node)?
14261424
// TODO remove(function)?
14271425
groups.remove = function() {
@@ -1486,6 +1484,71 @@ function d3_selection(groups) {
14861484
return groups;
14871485
}
14881486

1487+
function d3_selectionEnter(groups) {
1488+
1489+
function select(select) {
1490+
var subgroups = [],
1491+
subgroup,
1492+
subnode,
1493+
group,
1494+
node;
1495+
for (var j = 0, m = groups.length; j < m; j++) {
1496+
group = groups[j];
1497+
subgroups.push(subgroup = []);
1498+
subgroup.parentNode = group.parentNode;
1499+
subgroup.parentData = group.parentData;
1500+
for (var i = 0, n = group.length; i < n; i++) {
1501+
if (node = group[i]) {
1502+
subgroup.push(subnode = select(group.parentNode));
1503+
subnode.__data__ = node.__data__;
1504+
} else {
1505+
subgroup.push(null);
1506+
}
1507+
}
1508+
}
1509+
return d3_selection(subgroups);
1510+
}
1511+
1512+
// TODO append(node)?
1513+
// TODO append(function)?
1514+
groups.append = function(name) {
1515+
name = d3.ns.qualify(name);
1516+
1517+
function append(node) {
1518+
return node.appendChild(document.createElement(name));
1519+
}
1520+
1521+
function appendNS(node) {
1522+
return node.appendChild(document.createElementNS(name.space, name.local));
1523+
}
1524+
1525+
return select(name.local ? appendNS : append);
1526+
};
1527+
1528+
// TODO insert(node, function)?
1529+
// TODO insert(function, string)?
1530+
// TODO insert(function, function)?
1531+
groups.insert = function(name, before) {
1532+
name = d3.ns.qualify(name);
1533+
1534+
function insert(node) {
1535+
return node.insertBefore(
1536+
document.createElement(name),
1537+
d3_select(before, node));
1538+
}
1539+
1540+
function insertNS(node) {
1541+
return node.insertBefore(
1542+
document.createElementNS(name.space, name.local),
1543+
d3_select(before, node));
1544+
}
1545+
1546+
return select(name.local ? insertNS : insert);
1547+
};
1548+
1549+
return groups;
1550+
}
1551+
14891552
function d3_selection_comparator(comparator) {
14901553
if (!arguments.length) comparator = d3.ascending;
14911554
return function(a, b) {

0 commit comments

Comments
 (0)