Skip to content

Commit 1030db1

Browse files
committed
Fix d3.interpolate for built-in prototype properties.
1 parent 1e63be0 commit 1030db1

4 files changed

Lines changed: 15 additions & 9 deletions

File tree

d3.v2.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,14 +1001,14 @@ d3.interpolateObject = function(a, b) {
10011001
c = {},
10021002
k;
10031003
for (k in a) {
1004-
if (k in b) {
1004+
if (b.propertyIsEnumerable(k)) {
10051005
i[k] = d3_interpolateByName(k)(a[k], b[k]);
10061006
} else {
10071007
c[k] = a[k];
10081008
}
10091009
}
10101010
for (k in b) {
1011-
if (!(k in a)) {
1011+
if (!a.propertyIsEnumerable(k)) {
10121012
c[k] = b[k];
10131013
}
10141014
}
@@ -1030,7 +1030,7 @@ d3.interpolators = [
10301030
d3.interpolateObject,
10311031
function(a, b) { return (b instanceof Array) && d3.interpolateArray(a, b); },
10321032
function(a, b) { return (typeof a === "string" || typeof b === "string") && d3.interpolateString(a + "", b + ""); },
1033-
function(a, b) { return (typeof b === "string" ? b in d3_rgb_names || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a, b); },
1033+
function(a, b) { return (typeof b === "string" ? d3_rgb_names.hasOwnProperty(b) || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a, b); },
10341034
function(a, b) { return !isNaN(a = +a) && !isNaN(b = +b) && d3.interpolateNumber(a, b); }
10351035
];
10361036
function d3_uninterpolateNumber(a, b) {

d3.v2.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/interpolate.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,14 @@ d3.interpolateObject = function(a, b) {
196196
c = {},
197197
k;
198198
for (k in a) {
199-
if (k in b) {
199+
if (b.propertyIsEnumerable(k)) {
200200
i[k] = d3_interpolateByName(k)(a[k], b[k]);
201201
} else {
202202
c[k] = a[k];
203203
}
204204
}
205205
for (k in b) {
206-
if (!(k in a)) {
206+
if (!a.propertyIsEnumerable(k)) {
207207
c[k] = b[k];
208208
}
209209
}
@@ -225,6 +225,6 @@ d3.interpolators = [
225225
d3.interpolateObject,
226226
function(a, b) { return (b instanceof Array) && d3.interpolateArray(a, b); },
227227
function(a, b) { return (typeof a === "string" || typeof b === "string") && d3.interpolateString(a + "", b + ""); },
228-
function(a, b) { return (typeof b === "string" ? b in d3_rgb_names || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a, b); },
228+
function(a, b) { return (typeof b === "string" ? d3_rgb_names.hasOwnProperty(b) || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a, b); },
229229
function(a, b) { return !isNaN(a = +a) && !isNaN(b = +b) && d3.interpolateNumber(a, b); }
230230
];

test/core/interpolate-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ suite.addBatch({
2626
},
2727
"interpolates objects": function(interpolate) {
2828
assert.deepEqual(interpolate({foo: 2}, {foo: 12})(.4), {foo: 6});
29+
},
30+
"interpolates objects with default object prototype properties": function(interpolate) {
31+
assert.deepEqual(interpolate({foo: 2, hasOwnProperty: 1}, {foo: 12})(1), {foo: 12, hasOwnProperty: 1});
32+
},
33+
"doesn't interpret properties in the default object's prototype chain as RGB": function(interpolate) {
34+
assert.equal(interpolate("hasOwnProperty", "hasOwnProperty")(0), "hasOwnProperty");
2935
}
3036
}
3137
});

0 commit comments

Comments
 (0)