Permalink
Browse files

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.)
  • Loading branch information...
1 parent c31590e commit 8da80dcc994ef660198b5cf2b88343c429296847 @mbostock mbostock committed Feb 17, 2011
Showing with 8,590 additions and 124 deletions.
  1. +92 −29 d3.js
  2. +67 −66 d3.min.js
  3. +27 −0 examples/sizzle/sizzle.html
  4. +1 −0 examples/stream/stack.html
  5. +6,883 −0 lib/jquery/jquery.js
  6. +24 −0 lib/sizzle/LICENSE
  7. +1,376 −0 lib/sizzle/sizzle.js
  8. +28 −0 lib/sizzle/sizzle.min.js
  9. +1 −1 src/core/core.js
  10. +91 −28 src/core/selection.js
View
@@ -1,4 +1,4 @@
-(function(){d3 = {version: "0.30.8"}; // semver
+(function(){d3 = {version: "1.0.0"}; // semver
if (!Date.now) Date.now = function() {
return +new Date();
};
@@ -923,20 +923,29 @@ function d3_hsl_rgb(h, s, l) {
return d3_rgb(vv(h + 120), vv(h), vv(h - 120));
}
+var d3_select = function(s, n) { return n.querySelector(s); },
+ d3_selectAll = function(s, n) { return d3_array(n.querySelectorAll(s)); };
+
+// Use Sizzle, if available.
+if (typeof Sizzle == "function") {
+ d3_select = function(s, n) { return Sizzle(s, n)[0]; };
+ d3_selectAll = Sizzle;
+}
+
var d3_root = d3_selection([[document]]);
d3_root[0].parentNode = document.documentElement;
// TODO fast singleton implementation!
-d3.select = function(query) {
- return typeof query == "string"
- ? d3_root.select(query)
- : d3_selection([[query]]); // assume node
+d3.select = function(selector) {
+ return typeof selector == "string"
+ ? d3_root.select(selector)
+ : d3_selection([[selector]]); // assume node
};
-d3.selectAll = function(query) {
- return typeof query == "string"
- ? d3_root.selectAll(query)
- : d3_selection([d3_array(query)]); // assume node[]
+d3.selectAll = function(selector) {
+ return typeof selector == "string"
+ ? d3_root.selectAll(selector)
+ : d3_selection([d3_array(selector)]); // assume node[]
};
function d3_selection(groups) {
@@ -983,16 +992,16 @@ function d3_selection(groups) {
}
// TODO select(function)?
- groups.select = function(query) {
+ groups.select = function(selector) {
return select(function(node) {
- return node.querySelector(query);
+ return d3_select(selector, node);
});
};
// TODO selectAll(function)?
- groups.selectAll = function(query) {
+ groups.selectAll = function(selector) {
return selectAll(function(node) {
- return d3_array(node.querySelectorAll(query));
+ return d3_selectAll(selector, node);
});
};
@@ -1035,18 +1044,7 @@ function d3_selection(groups) {
nodeData;
function enterNode(data) {
- return {
- __data__: data,
- appendChild: function(a) {
- return group.parentNode.appendChild(a);
- },
- insertBefore: function(a, b) {
- return group.parentNode.insertBefore(a, b);
- },
- querySelector: function(a) {
- return group.parentNode.querySelector(a);
- }
- };
+ return {__data__: data};
}
if (join) {
@@ -1137,7 +1135,7 @@ function d3_selection(groups) {
var selection = d3_selection(update);
selection.enter = function() {
- return d3_selection(enter);
+ return d3_selectionEnter(enter);
};
selection.exit = function() {
return d3_selection(exit);
@@ -1409,19 +1407,19 @@ function d3_selection(groups) {
function insert(node) {
return node.insertBefore(
document.createElement(name),
- node.querySelector(before));
+ d3_select(before, node));
}
function insertNS(node) {
return node.insertBefore(
document.createElementNS(name.space, name.local),
- node.querySelector(before));
+ d3_select(before, node));
}
return select(name.local ? insertNS : insert);
};
- // TODO remove(query)?
+ // TODO remove(selector)?
// TODO remove(node)?
// TODO remove(function)?
groups.remove = function() {
@@ -1486,6 +1484,71 @@ function d3_selection(groups) {
return groups;
}
+function d3_selectionEnter(groups) {
+
+ function select(select) {
+ var subgroups = [],
+ subgroup,
+ subnode,
+ group,
+ node;
+ for (var j = 0, m = groups.length; j < m; j++) {
+ group = groups[j];
+ subgroups.push(subgroup = []);
+ subgroup.parentNode = group.parentNode;
+ subgroup.parentData = group.parentData;
+ for (var i = 0, n = group.length; i < n; i++) {
+ if (node = group[i]) {
+ subgroup.push(subnode = select(group.parentNode));
+ subnode.__data__ = node.__data__;
+ } else {
+ subgroup.push(null);
+ }
+ }
+ }
+ return d3_selection(subgroups);
+ }
+
+ // TODO append(node)?
+ // TODO append(function)?
+ groups.append = function(name) {
+ name = d3.ns.qualify(name);
+
+ function append(node) {
+ return node.appendChild(document.createElement(name));
+ }
+
+ function appendNS(node) {
+ return node.appendChild(document.createElementNS(name.space, name.local));
+ }
+
+ return select(name.local ? appendNS : append);
+ };
+
+ // TODO insert(node, function)?
+ // TODO insert(function, string)?
+ // TODO insert(function, function)?
+ groups.insert = function(name, before) {
+ name = d3.ns.qualify(name);
+
+ function insert(node) {
+ return node.insertBefore(
+ document.createElement(name),
+ d3_select(before, node));
+ }
+
+ function insertNS(node) {
+ return node.insertBefore(
+ document.createElementNS(name.space, name.local),
+ d3_select(before, node));
+ }
+
+ return select(name.local ? insertNS : insert);
+ };
+
+ return groups;
+}
+
function d3_selection_comparator(comparator) {
if (!arguments.length) comparator = d3.ascending;
return function(a, b) {
Oops, something went wrong.

0 comments on commit 8da80dc

Please sign in to comment.