Skip to content

Commit

Permalink
Remove support for functions that return maps.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Aug 5, 2012
1 parent f2b45c1 commit 74596a9
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 141 deletions.
38 changes: 7 additions & 31 deletions d3.v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -1390,15 +1390,10 @@
}
d3_selectionPrototype.attr = function(name, value) {
if (arguments.length < 2) {
if ((value = typeof name) === "function") {
return this.each(function() {
var x = name.apply(this, arguments);
for (value in x) d3_selection_attr(value, x[value]).apply(this, arguments);
});
}
if (value === "string") {
value = this.node();
return (name = d3.ns.qualify(name)).local ? value.getAttributeNS(name.space, name.local) : value.getAttribute(name);
if (typeof name === "string") {
var node = this.node();
name = d3.ns.qualify(name);
return name.local ? node.getAttributeNS(name.space, name.local) : node.getAttribute(name);
}
for (value in name) this.each(d3_selection_attr(value, name[value]));
return this;
Expand Down Expand Up @@ -1431,13 +1426,7 @@
}
d3_selectionPrototype.classed = function(name, value) {
if (arguments.length < 2) {
if ((value = typeof name) === "function") {
return this.each(function() {
var x = name.apply(this, arguments);
for (value in x) d3_selection_classed(value, x[value]).apply(this, arguments);
});
}
if (value === "string") {
if (typeof name === "string") {
var node = this.node(), n = (name = name.trim().split(/^|\s+/g)).length, i = -1;
if (value = node.classList) {
while (++i < n) if (!value.contains(name[i])) return false;
Expand Down Expand Up @@ -1489,14 +1478,7 @@
d3_selectionPrototype.style = function(name, value, priority) {
var n = arguments.length;
if (n < 3) {
if ((priority = typeof name) === "function") {
if (n < 2) value = "";
return this.each(function() {
var x = name.apply(this, arguments);
for (priority in x) d3_selection_style(priority, x[priority], value).apply(this, arguments);
});
}
if (priority !== "string") {
if (typeof name !== "string") {
if (n < 2) value = "";
for (priority in name) this.each(d3_selection_style(priority, name[priority], value));
return this;
Expand All @@ -1521,13 +1503,7 @@
}
d3_selectionPrototype.property = function(name, value) {
if (arguments.length < 2) {
if ((value = typeof name) === "function") {
return this.each(function() {
var x = name.apply(this, arguments);
for (value in x) d3_selection_property(value, x[value]).apply(this, arguments);
});
}
if (value === "string") return this.node()[name];
if (typeof name === "string") return this.node()[name];
for (value in name) this.each(d3_selection_property(value, name[value]));
return this;
}
Expand Down
6 changes: 3 additions & 3 deletions d3.v2.min.js

Large diffs are not rendered by default.

30 changes: 10 additions & 20 deletions src/core/selection-attr.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,45 @@
d3_selectionPrototype.attr = function(name, value) {
if (arguments.length < 2) {

// For attr(function), the function must return an object for each element,
// specifying the names and values of the attributes to set or remove. The
// values must be constants, not functions.
if ((value = typeof name) === "function") {
return this.each(function() {
var x = name.apply(this, arguments);
for (value in x) d3_selection_attr(value, x[value]).apply(this, arguments);
});
}

// For attr(string), return the attribute value for the first node.
if (value === "string") {
value = this.node();
return (name = d3.ns.qualify(name)).local
? value.getAttributeNS(name.space, name.local)
: value.getAttribute(name);
if (typeof name === "string") {
var node = this.node();
name = d3.ns.qualify(name);
return name.local
? node.getAttributeNS(name.space, name.local)
: node.getAttribute(name);
}

// For attr(object), the object specifies the names and values of the
// attributes to set or remove. The values may be functions that are
// evaluated for each element.
// evaluated for each element, or nulls, or strings.
for (value in name) this.each(d3_selection_attr(value, name[value]));
return this;
}

// Otherwise, both a name and a value are specified, and are handled as below.
return this.each(d3_selection_attr(name, value));
};

function d3_selection_attr(name, value) {
name = d3.ns.qualify(name);

// For attr(name, null), remove the attribute with the specified name.
// For attr(string, null), remove the attribute with the specified name.
function attrNull() {
this.removeAttribute(name);
}
function attrNullNS() {
this.removeAttributeNS(name.space, name.local);
}

// For attr(name, string), set the attribute with the specified name.
// For attr(string, string), set the attribute with the specified name.
function attrConstant() {
this.setAttribute(name, value);
}
function attrConstantNS() {
this.setAttributeNS(name.space, name.local, value);
}

// For attr(name, function), evaluate the function for each element, and set
// For attr(string, function), evaluate the function for each element, and set
// or remove the attribute as appropriate.
function attrFunction() {
var x = value.apply(this, arguments);
Expand Down
12 changes: 1 addition & 11 deletions src/core/selection-classed.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
d3_selectionPrototype.classed = function(name, value) {
if (arguments.length < 2) {

// For classed(function), the function must return an object for each
// element, specifying the names of classes to add or remove. The values
// must be constants, not functions.
if ((value = typeof name) === "function") {
return this.each(function() {
var x = name.apply(this, arguments);
for (value in x) d3_selection_classed(value, x[value]).apply(this, arguments);
});
}

// For classed(string), return true only if the first node has the specified
// class or classes. Note that even if the browser supports DOMTokenList, it
// probably doesn't support it on SVG elements (which can be animated).
if (value === "string") {
if (typeof name === "string") {
var node = this.node(),
n = (name = name.trim().split(/^|\s+/g)).length,
i = -1;
Expand Down
12 changes: 1 addition & 11 deletions src/core/selection-property.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
d3_selectionPrototype.property = function(name, value) {
if (arguments.length < 2) {

// For property(function), the function must return an object for each
// element, specifying the names and values of the properties to set or
// remove. The values must be constants, not functions.
if ((value = typeof name) === "function") {
return this.each(function() {
var x = name.apply(this, arguments);
for (value in x) d3_selection_property(value, x[value]).apply(this, arguments);
});
}

// For property(string), return the property value for the first node.
if (value === "string") return this.node()[name];
if (typeof name === "string") return this.node()[name];

// For property(object), the object specifies the names and values of the
// properties to set or remove. The values may be functions that are
Expand Down
18 changes: 4 additions & 14 deletions src/core/selection-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,10 @@ d3_selectionPrototype.style = function(name, value, priority) {
var n = arguments.length;
if (n < 3) {

// For style(function) or style(function, priority), the function must
// return an object for each element, specifying the names and values of the
// styles to set or remove. The values must be constants, not functions.
if ((priority = typeof name) === "function") {
if (n < 2) value = "";
return this.each(function() {
var x = name.apply(this, arguments);
for (priority in x) d3_selection_style(priority, x[priority], value).apply(this, arguments);
});
}

// For style(object) or style(object, priority), the object specifies the
// For style(object) or style(object, string), the object specifies the
// names and values of the attributes to set or remove. The values may be
// functions that are evaluated for each element.
if (priority !== "string") {
if (typeof name !== "string") {
if (n < 2) value = "";
for (priority in name) this.each(d3_selection_style(priority, name[priority], value));
return this;
Expand All @@ -27,7 +16,8 @@ d3_selectionPrototype.style = function(name, value, priority) {
.getComputedStyle(this.node(), null)
.getPropertyValue(name);

// For style(name, value), use the default priority.
// For style(string, string) or style(string, function), use the default
// priority. The priority is ignored for style(string, null).
priority = "";
}

Expand Down
11 changes: 0 additions & 11 deletions test/core/selection-attr-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ suite.addBatch({
body.data(["orange"]).attr({"xlink:href": function(d, i) { return d + "-" + i + ".png"; }});
assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "orange-0.png");
},
"sets attributes as a function that returns a map": function(body) {
body.data(["red"]).attr(function(d, i) { return {"xlink:href": d + "-" + i + ".png"}; });
assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "red-0.png");
},
"gets an attribute value": function(body) {
document.body.setAttribute("bgcolor", "yellow");
assert.equal(body.attr("bgcolor"), "yellow");
Expand Down Expand Up @@ -89,13 +85,6 @@ suite.addBatch({
assert.equal(document.body.getAttribute("bgcolor"), "");
assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "");
},
"removes attributes as a function that returns a map of null": function(body) {
document.body.setAttribute("bgcolor", "white");
document.body.setAttributeNS("http://www.w3.org/1999/xlink", "href", "foo.png");
body.attr(function() { return {bgcolor: null, "xlink:href": null}; });
assert.equal(document.body.getAttribute("bgcolor"), "");
assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "");
},
"returns the current selection": function(body) {
assert.isTrue(body.attr("foo", "bar") === body);
}
Expand Down
19 changes: 0 additions & 19 deletions test/core/selection-classed-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,6 @@ suite.addBatch({
body.classed("foo", function() { return false; });
assert.equal(document.body.className, "bar");
},
"accepts a value function returning returning an object containing true or false": function(body) {
body.attr("class", null);
body.classed(function() { return {foo: true}; });
assert.equal(document.body.className, "foo");
body.classed(function() { return {foo: true, bar: true}; });
assert.equal(document.body.className, "foo bar");
body.classed(function() { return {bar: false, foo: false}; });
assert.equal(document.body.className, "");
},
"accepts a name object containing true or false": function(body) {
body.attr("class", null);
body.classed({foo: true});
Expand Down Expand Up @@ -133,35 +124,25 @@ suite.addBatch({
body.classed({" foo\t": function() { return true; }});
assert.equal(document.body.className, "foo");
body.attr("class", null);
body.classed(function() { return {"\tfoo ": true}; });
assert.equal(document.body.className, "foo");
},
"keys in the name object may reference multiple classes": function(body) {
body.attr("class", null);
body.classed({"foo bar": function() { return true; }});
assert.equal(document.body.className, "foo bar");
body.attr("class", null);
body.classed(function() { return {"foo bar": true}; });
assert.equal(document.body.className, "foo bar");
},
"keys in the name object may contain duplicates": function(body) {
body.attr("class", null);
body.classed({"foo foo": function() { return true; }});
assert.equal(document.body.className, "foo");
body.attr("class", null);
body.classed(function() { return {"foo foo": true}; });
assert.equal(document.body.className, "foo");
},
"value functions are only evaluated once when used for multiple classes": function(body) {
var count = 0;
body.attr("class", null);
body.classed({"foo bar": function() { return ++count; }});
assert.equal(document.body.className, "foo bar");
assert.equal(count, 1);
body.attr("class", null);
body.classed(function() { return {"foo bar": count--}; });
assert.equal(document.body.className, "foo bar");
assert.equal(count, 0);
},
"returns the current selection": function(body) {
assert.isTrue(body.classed("foo", true) === body);
Expand Down
10 changes: 0 additions & 10 deletions test/core/selection-property-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ suite.addBatch({
assert.equal(document.body.bgcolor, "cyan");
assert.equal(document.body.opacity, 0);
},
"sets properties as a function that returns a map": function(body) {
body.data([.14]).property(function(d, i) { return {bgcolor: "black", opacity: d}; });
assert.equal(document.body.bgcolor, "black");
assert.equal(document.body.opacity, .14);
},
"gets a property value": function(body) {
document.body.bgcolor = "yellow";
assert.equal(body.property("bgcolor"), "yellow");
Expand All @@ -59,11 +54,6 @@ suite.addBatch({
body.property({bgcolor: function() {}});
assert.isFalse("bgcolor" in document.body);
},
"removes properties as a function that returns a map of nulls": function(body) {
document.body.bgcolor = "red";
body.property(function() { return {bgcolor: null}; });
assert.isFalse("bgcolor" in document.body);
},
"returns the current selection": function(body) {
assert.isTrue(body.property("bgcolor", "yellow") === body);
}
Expand Down
12 changes: 1 addition & 11 deletions test/core/selection-style-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ suite.addBatch({
assert.equal(document.body.style["background-color"], "orange");
assert.equal(document.body.style["opacity"], "0");
},
"sets properties as a function that returns a map": function(body) {
body.data([.5]).style(function(d, i) { return {"background-color": "green", opacity: d}; });
assert.equal(document.body.style["background-color"], "green");
assert.equal(document.body.style["opacity"], "0.5");
},
"gets a property value": function(body) {
document.body.style.setProperty("background-color", "yellow", "");
assert.equal(body.style("background-color"), "yellow");
Expand All @@ -46,7 +41,7 @@ suite.addBatch({
assert.equal(document.body.style.getPropertyPriority("background-color"), "important");
body.style({opacity: .52}, "important");
assert.equal(document.body.style.getPropertyPriority("opacity"), "important");
body.style(function() { return {visibility: "visible"}; }, "important");
body.style({visibility: function() { return "visible"; }}, "important");
assert.equal(document.body.style.getPropertyPriority("visibility"), "important");
},
"removes a property as null": function(body) {
Expand All @@ -67,11 +62,6 @@ suite.addBatch({
body.style({"background-color": function() {}});
assert.equal(body.style("background-color"), "");
},
"removes properties as a function that returns a map of nulls": function(body) {
document.body.style.setProperty("background-color", "purple");
body.style(function() { return {"background-color": null}; });
assert.equal(body.style("background-color"), "");
},
"returns the current selection": function(body) {
assert.isTrue(body.style("background-color", "green") === body);
}
Expand Down

0 comments on commit 74596a9

Please sign in to comment.