Skip to content

Commit

Permalink
Fix for IE9's style.setProperty.
Browse files Browse the repository at this point in the history
IE9 does not string-coerce values, instead throwing an error. We now wrap IE9's
implementation to force string coercion. While it would be simpler to turn on
string-coercion for all browsers inside D3's style operator, this approach
avoids penalizing standards-compliant browsers.

This commit also moves language-compatibility code to a separate directory, and
deletes the obsolete Object.create polyfill, which is no longer needed by D3.
  • Loading branch information
mbostock committed Aug 29, 2011
1 parent cd135d5 commit c2e3735
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ d3.custom.js: \
src/end.js

d3.core.js: \
src/compat/date.js \
src/compat/style.js \
src/core/core.js \
src/core/date.js \
src/core/object.js \
src/core/array.js \
src/core/this.js \
src/core/functor.js \
Expand Down
18 changes: 11 additions & 7 deletions d3.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
(function(){d3 = {version: "2.0.4"}; // semver
if (!Date.now) Date.now = function() {
(function(){if (!Date.now) Date.now = function() {
return +new Date;
};
if (!Object.create) Object.create = function(o) {
/** @constructor */ function f() {}
f.prototype = o;
return new f;
};
try {
document.createElement("div").style.setProperty("opacity", 0, "");
} catch (error) {
var d3_style_prototype = CSSStyleDeclaration.prototype,
d3_style_setProperty = d3_style_prototype.setProperty;
d3_style_prototype.setProperty = function(name, value, priority) {
d3_style_setProperty.call(this, name, value + "", priority);
};
}
d3 = {version: "2.0.4"}; // semver
var d3_arraySubclass = [].__proto__?

// Until ECMAScript supports array subclassing, prototype injection works well.
Expand Down
4 changes: 2 additions & 2 deletions d3.min.js

Large diffs are not rendered by default.

File renamed without changes.
9 changes: 9 additions & 0 deletions src/compat/style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
try {
document.createElement("div").style.setProperty("opacity", 0, "");
} catch (error) {
var d3_style_prototype = CSSStyleDeclaration.prototype,
d3_style_setProperty = d3_style_prototype.setProperty;
d3_style_prototype.setProperty = function(name, value, priority) {
d3_style_setProperty.call(this, name, value + "", priority);
};
}
5 changes: 0 additions & 5 deletions src/core/object.js

This file was deleted.

6 changes: 3 additions & 3 deletions test/core/selection-style-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ suite.addBatch({
},
"sets a property as a number": function(body) {
body.style("opacity", .3);
assert.equal(document.body.style["opacity"], ".3");
assert.equal(document.body.style["opacity"], "0.3");
},
"sets a property as a function": function(body) {
body.style("background-color", function() { return "orange"; });
Expand Down Expand Up @@ -57,8 +57,8 @@ suite.addBatch({
},
"sets a property as a number": function(div) {
div.style("opacity", .5);
assert.equal(div[0][0].style["opacity"], ".5");
assert.equal(div[0][1].style["opacity"], ".5");
assert.equal(div[0][0].style["opacity"], "0.5");
assert.equal(div[0][1].style["opacity"], "0.5");
},
"sets a property as a function": function(div) {
div.style("background-color", d3.interpolateRgb("orange", "yellow"));
Expand Down
1 change: 1 addition & 0 deletions test/env.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
document = require("jsdom").jsdom("<html><head></head><body></body></html>");
window = document.createWindow();
navigator = window.navigator;
CSSStyleDeclaration = window.CSSStyleDeclaration;

require("../lib/sizzle/sizzle");
Sizzle = window.Sizzle;
Expand Down

0 comments on commit c2e3735

Please sign in to comment.