Skip to content

Commit

Permalink
Fix removing an <img>'s src="" attribute
Browse files Browse the repository at this point in the history
This used to raise an exception under certain circumstances. Closes #2150.
  • Loading branch information
atsikov authored and domenic committed Feb 19, 2018
1 parent d563965 commit dc672a2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
26 changes: 15 additions & 11 deletions lib/jsdom/living/nodes/HTMLImageElement-impl.js
Expand Up @@ -19,17 +19,21 @@ class HTMLImageElementImpl extends HTMLElementImpl {
};
}
this._currentSrc = null;
resourceLoader.load(this, this.src, {}, (data, url, response) => {
if (response && response.statusCode !== undefined && response.statusCode !== 200) {
throw new Error("Status code: " + response.statusCode);
}
error = null;
this._image.source = data;
if (error) {
throw new Error(error);
}
this._currentSrc = value;
});
if (this.hasAttribute("src")) {
resourceLoader.load(this, this.src, {}, (data, url, response) => {
if (response && response.statusCode !== undefined && response.statusCode !== 200) {
throw new Error("Status code: " + response.statusCode);
}
error = null;
this._image.source = data;
if (error) {
throw new Error(error);
}
this._currentSrc = value;
});
} else {
this._image.source = undefined;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/web-platform-tests/run-single-wpt.js
Expand Up @@ -51,7 +51,7 @@ function createJSDOM(urlPrefix, testPath) {
pool: globalPool,
strictSSL: false,
features: {
FetchExternalResources: ["script", "frame", "iframe", "link"],
FetchExternalResources: ["script", "frame", "iframe", "link", "img"],
ProcessExternalResources: ["script"]
},
virtualConsole,
Expand Down
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Removing src attribute from img after it loads</title>
<link rel="author" title="atsikov" href="mailto:alexey.tsikov@gmail.com">
<link rel="help" href="https://html.spec.whatwg.org/#the-img-element">

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script>
"use strict";

async_test(t => {
const image = new window.Image();
const runTest = t.step_func(() => {
image.onload = t.unreached_func("onload should not be called with removed src");
image.onerror = t.unreached_func("onerror should not be called with removed src");
image.removeAttribute("src");

assert_equals(image.width, 0);
assert_equals(image.height, 0);
assert_equals(image.complete, false);
assert_equals(image.src, "");
assert_equals(image.currentSrc, "");

// Give it a second to make sure onload/onerror don't fire within that time frame.
t.step_timeout(() => {
t.done();
}, 1000);
});

image.src = "data:image/gif;base64,R0lGODlhAQABAIAAAMLCwgAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==";

image.onload = runTest;

// JSDOM specific: when image loading is not supported (i.e. when node-canvas is not installed), run the test anyway.
t.step_timeout(runTest, 1000);
}, "img is cleared when src attribute is removed");
</script>

0 comments on commit dc672a2

Please sign in to comment.