Permalink
Browse files

Fix #1664 - empty classed string.

The regex used for split was returning [""] for the input "", rather than [].
  • Loading branch information...
1 parent f46d8d2 commit 0a12e31fe2b95920e2bde82ea92c9fc8ee3fbbe9 @mbostock mbostock committed Dec 13, 2013
Showing with 28 additions and 9 deletions.
  1. +5 −2 d3.js
  2. +5 −5 d3.min.js
  3. +6 −2 src/selection/classed.js
  4. +12 −0 test/selection/classed-test.js
View
7 d3.js
@@ -607,7 +607,7 @@ d3 = function() {
d3_selectionPrototype.classed = function(name, value) {
if (arguments.length < 2) {
if (typeof name === "string") {
- var node = this.node(), n = (name = name.trim().split(/^|\s+/g)).length, i = -1;
+ var node = this.node(), n = (name = d3_selection_classes(name)).length, i = -1;
if (value = node.classList) {
while (++i < n) if (!value.contains(name[i])) return false;
} else {
@@ -624,8 +624,11 @@ d3 = function() {
function d3_selection_classedRe(name) {
return new RegExp("(?:^|\\s+)" + d3.requote(name) + "(?:\\s+|$)", "g");
}
+ function d3_selection_classes(name) {
+ return name.trim().split(/^|\s+/);
+ }
function d3_selection_classed(name, value) {
- name = name.trim().split(/\s+/).map(d3_selection_classedName);
+ name = d3_selection_classes(name).map(d3_selection_classedName);
var n = name.length;
function classedConstant() {
var i = -1;
View
10 d3.min.js
5 additions, 5 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
8 src/selection/classed.js
@@ -10,7 +10,7 @@ d3_selectionPrototype.classed = function(name, value) {
// probably doesn't support it on SVG elements (which can be animated).
if (typeof name === "string") {
var node = this.node(),
- n = (name = name.trim().split(/^|\s+/g)).length,
+ n = (name = d3_selection_classes(name)).length,
i = -1;
if (value = node.classList) {
while (++i < n) if (!value.contains(name[i])) return false;
@@ -35,9 +35,13 @@ function d3_selection_classedRe(name) {
return new RegExp("(?:^|\\s+)" + d3.requote(name) + "(?:\\s+|$)", "g");
}
+function d3_selection_classes(name) {
+ return name.trim().split(/^|\s+/);
+}
+
// Multiple class names are allowed (e.g., "foo bar").
function d3_selection_classed(name, value) {
- name = name.trim().split(/\s+/).map(d3_selection_classedName);
+ name = d3_selection_classes(name).map(d3_selection_classedName);
var n = name.length;
function classedConstant() {
View
12 test/selection/classed-test.js
@@ -89,6 +89,18 @@ suite.addBatch({
body.classed("\tfoo foo ", false);
assert.equal(body.node().className, "");
},
+ "accepts an empty name, doing nothing or returning true": function(body) {
+ body.attr("class", null);
+ body.classed("", true);
+ assert.equal(body.node().className, "");
+ assert.isTrue(body.classed(""));
+ body.classed(" \t ", true);
+ assert.equal(body.node().className, "");
+ assert.isTrue(body.classed(""));
+ body.classed("", false);
+ assert.equal(body.node().className, "");
+ assert.isTrue(body.classed(""));
+ },
"accepts a value function returning true or false": function(body) {
body.attr("class", null);
body.classed("foo", function() { return true; });

0 comments on commit 0a12e31

Please sign in to comment.