Skip to content

Commit 3e80556

Browse files
stragerzbjornson
authored andcommitted
Fix .complete for errored images
According to the HTML specification [1], HTMLImageElement#complete indicates whether or not an image is loading. It does not indicate whether or not the image successfully loaded. When an Image fails to load, we call the onerror callback. However, Image#complete is false, despite the current request's state being 'broken' (according to the spec). This is not spec-compliant. Because our Image implementation loads images synchronously (as soon as the src property is set, which is not spec-compliant), then the current request's state is only ever 'completely available' or 'broken' [2] (if src is set). This means that, according to the spec, Image#complete must always be true. Fix Image#complete to return true unconditionally. [1] https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-complete [2] https://html.spec.whatwg.org/multipage/images.html#img-req-state
1 parent f13efc7 commit 3e80556

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1818
* Fix signed/unsigned comparison warning introduced in 2.6.0, and function cast warnings with GCC8+
1919
* Fix to compile without JPEG support (#1593).
2020
* Fix compile errors with cairo
21+
* Fix Image#complete if the image failed to load.
2122

2223
2.6.1
2324
==================

src/Image.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ NAN_METHOD(Image::New) {
100100
*/
101101

102102
NAN_GETTER(Image::GetComplete) {
103-
Image *img = Nan::ObjectWrap::Unwrap<Image>(info.This());
104-
info.GetReturnValue().Set(Nan::New<Boolean>(Image::COMPLETE == img->state));
103+
info.GetReturnValue().Set(Nan::New<Boolean>(true));
105104
}
106105

107106
/*

test/image.test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ describe('Image', function () {
8888
it('detects invalid PNG', function (done) {
8989
if (process.platform === 'win32') this.skip(); // TODO
9090
const img = new Image()
91-
img.onerror = () => done()
91+
img.onerror = () => {
92+
assert.strictEqual(img.complete, true)
93+
done()
94+
}
9295
img.src = Buffer.from('89504E470D', 'hex')
9396
})
9497

@@ -156,6 +159,7 @@ describe('Image', function () {
156159
assert.equal(err.code, 'ENOENT')
157160
assert.equal(err.path, 'path/to/nothing')
158161
assert.equal(err.syscall, 'fopen')
162+
assert.strictEqual(img.complete, true)
159163
done()
160164
}
161165
img.src = 'path/to/nothing'
@@ -165,6 +169,7 @@ describe('Image', function () {
165169
const img = new Image()
166170
img.onerror = err => {
167171
assert.equal(err.message, "JPEG datastream contains no image")
172+
assert.strictEqual(img.complete, true)
168173
done()
169174
}
170175
img.src = `${__dirname}/fixtures/159-crash1.jpg`
@@ -218,6 +223,7 @@ describe('Image', function () {
218223
img.src = Buffer.alloc(0)
219224
assert.strictEqual(img.width, 0)
220225
assert.strictEqual(img.height, 0)
226+
assert.strictEqual(img.complete, true)
221227

222228
assert.strictEqual(onerrorCalled, 1)
223229
})

0 commit comments

Comments
 (0)