PageSourceSearch

https://satcar.dk/templates/satcar/js/mapbox-gl/src/util/webp_supported

js satcar.dk collected 2026-10-02 01:55:01 UTC 2,161 bytes, 69 lines download raw bytes

vendor: 2,161 bytes, lines 1-69
1// @flow strict
2
3import window from './window';
4
5const exported = {
6    supported: false,
7    testSupport
8};
9
10export default exported;
11
12let glForTesting;
13let webpCheckComplete = false;
14let webpImgTest;
15let webpImgTestOnloadComplete = false;
16
17if (window.document) {
18    webpImgTest = window.document.createElement('img');
19    webpImgTest.onload = function() {
20        if (glForTesting) testWebpTextureUpload(glForTesting);
21        glForTesting = null;
22        webpImgTestOnloadComplete = true;
23    };
24    webpImgTest.onerror = function() {
25        webpCheckComplete = true;
26        glForTesting = null;
27    };
28    webpImgTest.src = 'data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAQAAAAfQ//73v/+BiOh/AAA=';
29}
30
31function testSupport(gl: WebGLRenderingContext) {
32    if (webpCheckComplete || !webpImgTest) return;
33
34    // HTMLImageElement.complete is set when an image is done loading it's source
35    // regardless of whether the load was successful or not.
36    // It's possible for an error to set HTMLImageElement.complete to true which would trigger
37    // testWebpTextureUpload and mistakenly set exported.supported to true in browsers which don't support webp
38    // To avoid this, we set a flag in the image's onload handler and only call testWebpTextureUpload
39    // after a successful image load event.
40    if (webpImgTestOnloadComplete) {
41        testWebpTextureUpload(gl);
42    } else {
43        glForTesting = gl;
44
45    }
46}
47
48function testWebpTextureUpload(gl: WebGLRenderingContext) {
49    // Edge 18 supports WebP but not uploading a WebP image to a gl texture
50    // Test support for this before allowing WebP images.
51    // https://github.com/mapbox/mapbox-gl-js/issues/7671
52    const texture = gl.createTexture();
53    gl.bindTexture(gl.TEXTURE_2D, texture);
54
55    try {
56        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, webpImgTest);
57
58        // The error does not get triggered in Edge if the context is lost
59        if (gl.isContextLost()) return;
60
61        exported.supported = true;
62    } catch (e) {
63        // Catch "Unspecified Error." in Edge 18.
64    }
65
66    gl.deleteTexture(texture);
67
68    webpCheckComplete = true;
69}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.