Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Safari bug which mistakenly requested WebP images (#7817) #7819

Merged
merged 1 commit into from
Jan 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/util/webp_supported.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ export default exported;
let glForTesting;
let webpCheckComplete = false;
let webpImgTest;
let webpImgTestOnloadComplete = false;

if (window.document) {
webpImgTest = window.document.createElement('img');
webpImgTest.onload = function() {
if (glForTesting) testWebpTextureUpload(glForTesting);
glForTesting = null;
webpImgTestOnloadComplete = true;
};
webpImgTest.onerror = function() {
webpCheckComplete = true;
Expand All @@ -29,12 +31,18 @@ if (window.document) {
function testSupport(gl: WebGLRenderingContext) {
if (webpCheckComplete || !webpImgTest) return;

if (!webpImgTest.complete) {
// HTMLImageElement.complete is set when an image is done loading it's source
// regardless of whether the load was successful or not.
// It's possible for an error to set HTMLImageElement.complete to true which would trigger
// testWebpTextureUpload and mistakenly set exported.supported to true in browsers which don't support webp
// To avoid this, we set a flag in the image's onload handler and only call testWebpTextureUpload
// after a successful image load event.
if (webpImgTestOnloadComplete) {
testWebpTextureUpload(gl);
} else {
glForTesting = gl;
return;
}

testWebpTextureUpload(gl);
}
}

function testWebpTextureUpload(gl: WebGLRenderingContext) {
Expand Down