Skip to content
This repository was archived by the owner on Feb 9, 2018. It is now read-only.

Commit 1d90195

Browse files
committed
refactor css-image-value to copy src, not use passed Image
1 parent ba796b9 commit 1d90195

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/css-image-value.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,20 @@
4040

4141
(function() {
4242
function CSSImageValue(image) {
43-
if (!(image instanceof Image)) {
44-
throw new TypeError("image must be an Image object");
43+
if (!(image instanceof Image) && typeof image != 'string') {
44+
throw new TypeError("image must be an Image object or string URL");
4545
}
46+
var src = image instanceof Image ? image.src : image;
4647

47-
this._image = image;
4848
this.state = "unloaded";
4949
this.intrinsicWidth = null;
5050
this.intrinsicHeight = null;
5151
this.intrinsicRatio = null;
52+
this._image = new Image();
5253
this._image.onload = onLoad.bind(this);
53-
this._image.onerror = onError.bind(this);
54+
this._image.onerror = onError.bind(this); // nb. loading blank src calls onerror
5455
this._image.onprogess = onProgress.bind(this);
56+
this._image.src = src; // load last, to force callbacks
5557
}
5658
CSSImageValue.prototype = Object.create(scope.CSSImageValue.prototype);
5759

test/js/css-image-value.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,28 @@ suite('CSSImageValue', function() {
2424
assert.instanceOf(new typedOM.internal.CSSImageValue(new Image()), CSSStyleValue);
2525
});
2626

27-
test('CSSImageValue only accepts Image objects', function() {
28-
var imageErr = /image must be an Image object/;
27+
test('CSSImageValue only accepts Image object or string URL', function() {
28+
var imageErr = /^image must be an Image object or string URL$/;
2929
assert.throws(function() { new typedOM.internal.CSSImageValue(); }, TypeError, imageErr);
3030
assert.throws(function() { new typedOM.internal.CSSImageValue(1); }, TypeError, imageErr);
31-
assert.throws(function() { new typedOM.internal.CSSImageValue("abc"); }, TypeError, imageErr);
3231
assert.throws(function() { new typedOM.internal.CSSImageValue([]); }, TypeError, imageErr);
3332
assert.throws(function() { new typedOM.internal.CSSImageValue({ x: 1, y: 2 }); }, TypeError, imageErr);
3433
});
3534

36-
test('State and dimensions are correct before and after loading', function(done) {
37-
var image = new typedOM.internal.CSSImageValue(new Image());
38-
assert.strictEqual(image.state, "unloaded");
39-
assert.strictEqual(image.intrinsicWidth, null);
40-
assert.strictEqual(image.intrinsicHeight, null);
41-
assert.strictEqual(image.intrinsicRatio, null);
42-
image._image.src = "resources/1x1-green.png";
43-
var oldOnload = image._image.onload;
44-
image._image.onload = function() {
45-
oldOnload();
46-
assert.strictEqual(image.state, "loaded");
47-
assert.strictEqual(image.intrinsicWidth, 1);
48-
assert.strictEqual(image.intrinsicHeight, 1);
49-
assert.strictEqual(image.intrinsicRatio, 1);
35+
36+
test('CSSImageValue\'s state and dimensions are correct before and after loaded', function(done) {
37+
var iv = new typedOM.internal.CSSImageValue('resources/1x1-green.png');
38+
assert.strictEqual(iv.state, "unloaded");
39+
assert.strictEqual(iv.intrinsicWidth, null);
40+
assert.strictEqual(iv.intrinsicHeight, null);
41+
assert.strictEqual(iv.intrinsicRatio, null);
42+
43+
iv._image.addEventListener('load', function() {
44+
assert.strictEqual(iv.state, "loaded");
45+
assert.strictEqual(iv.intrinsicWidth, 1);
46+
assert.strictEqual(iv.intrinsicHeight, 1);
47+
assert.strictEqual(iv.intrinsicRatio, 1);
5048
done();
51-
};
49+
});
5250
});
5351
});

0 commit comments

Comments
 (0)