Skip to content

Commit c2e3735

Browse files
committed
Fix for IE9's style.setProperty.
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.
1 parent cd135d5 commit c2e3735

8 files changed

Lines changed: 28 additions & 19 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ d3.custom.js: \
3636
src/end.js
3737

3838
d3.core.js: \
39+
src/compat/date.js \
40+
src/compat/style.js \
3941
src/core/core.js \
40-
src/core/date.js \
41-
src/core/object.js \
4242
src/core/array.js \
4343
src/core/this.js \
4444
src/core/functor.js \

d3.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
(function(){d3 = {version: "2.0.4"}; // semver
2-
if (!Date.now) Date.now = function() {
1+
(function(){if (!Date.now) Date.now = function() {
32
return +new Date;
43
};
5-
if (!Object.create) Object.create = function(o) {
6-
/** @constructor */ function f() {}
7-
f.prototype = o;
8-
return new f;
9-
};
4+
try {
5+
document.createElement("div").style.setProperty("opacity", 0, "");
6+
} catch (error) {
7+
var d3_style_prototype = CSSStyleDeclaration.prototype,
8+
d3_style_setProperty = d3_style_prototype.setProperty;
9+
d3_style_prototype.setProperty = function(name, value, priority) {
10+
d3_style_setProperty.call(this, name, value + "", priority);
11+
};
12+
}
13+
d3 = {version: "2.0.4"}; // semver
1014
var d3_arraySubclass = [].__proto__?
1115

1216
// Until ECMAScript supports array subclassing, prototype injection works well.

d3.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

src/compat/style.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
try {
2+
document.createElement("div").style.setProperty("opacity", 0, "");
3+
} catch (error) {
4+
var d3_style_prototype = CSSStyleDeclaration.prototype,
5+
d3_style_setProperty = d3_style_prototype.setProperty;
6+
d3_style_prototype.setProperty = function(name, value, priority) {
7+
d3_style_setProperty.call(this, name, value + "", priority);
8+
};
9+
}

src/core/object.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/core/selection-style-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ suite.addBatch({
1717
},
1818
"sets a property as a number": function(body) {
1919
body.style("opacity", .3);
20-
assert.equal(document.body.style["opacity"], ".3");
20+
assert.equal(document.body.style["opacity"], "0.3");
2121
},
2222
"sets a property as a function": function(body) {
2323
body.style("background-color", function() { return "orange"; });
@@ -57,8 +57,8 @@ suite.addBatch({
5757
},
5858
"sets a property as a number": function(div) {
5959
div.style("opacity", .5);
60-
assert.equal(div[0][0].style["opacity"], ".5");
61-
assert.equal(div[0][1].style["opacity"], ".5");
60+
assert.equal(div[0][0].style["opacity"], "0.5");
61+
assert.equal(div[0][1].style["opacity"], "0.5");
6262
},
6363
"sets a property as a function": function(div) {
6464
div.style("background-color", d3.interpolateRgb("orange", "yellow"));

test/env.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
document = require("jsdom").jsdom("<html><head></head><body></body></html>");
22
window = document.createWindow();
33
navigator = window.navigator;
4+
CSSStyleDeclaration = window.CSSStyleDeclaration;
45

56
require("../lib/sizzle/sizzle");
67
Sizzle = window.Sizzle;

0 commit comments

Comments
 (0)