1// @flow strict 2 3// This file is intended for use in the GL-JS test suite 4// It implements a JSDOM window object for use in Node environments 5// In a browser environment, this file is replaced with ./src/util/browser/window.js 6// when Rollup builds the main bundle 7// See https://github.com/mapbox/mapbox-gl-js/blob/master/package.json#L104-L108 8 9import jsdom from 'jsdom'; 10 11import gl from 'gl'; 12import sinon from 'sinon'; 13 14import type {Window} from '../types/window'; 15 16const {window: _window} = new jsdom.JSDOM('', { 17 virtualConsole: new jsdom.VirtualConsole().sendTo(console) 18}); 19 20restore(); 21 22export default _window; 23 24function restore(): Window { 25 // Remove previous window from exported object 26 const previousWindow = _window; 27 if (previousWindow.close) previousWindow.close(); 28 for (const key in previousWindow) { 29 if (previousWindow.hasOwnProperty(key)) { 30 delete previousWindow[key]; 31 } 32 } 33 34 // Create new window and inject into exported object 35 const {window} = new jsdom.JSDOM('', { 36 // Send jsdom console output to the node console object. 37 virtualConsole: new jsdom.VirtualConsole().sendTo(console) 38 }); 39 40 // Delete local and session storage from JSDOM and stub them out with a warning log 41 // Accessing these properties during extend() produces an error in Node environments 42 // See https://github.com/mapbox/mapbox-gl-js/pull/7455 for discussion 43 delete window.localStorage; 44 delete window.sessionStorage; 45 window.localStorage = window.sessionStorage = () => console.log('Local and session storage not available in Node. Use a stub implementation if needed for testing.'); 46 47 window.devicePixelRatio = 1; 48 49 window.requestAnimationFrame = function(callback) { 50 return setImmediate(callback, 0); 51 }; 52 window.cancelAnimationFrame = clearImmediate; 53 54 // Add webgl context with the supplied GL 55 const originalGetContext = window.HTMLCanvasElement.prototype.getContext; 56 window.HTMLCanvasElement.prototype.getContext = function (type, attributes) { 57 if (type === 'webgl') { 58 if (!this._webGLContext) { 59 this._webGLContext = gl(this.width, this.height, attributes); 60 } 61 return this._webGLContext; 62 } 63 // Fallback to existing HTMLCanvasElement getContext behaviour 64 return originalGetContext.call(this, type, attributes); 65 }; 66 67 window.useFakeHTMLCanvasGetContext = function() { 68 this.HTMLCanvasElement.prototype.getContext = function() { return '2d'; }; 69 }; 70 71 window.useFakeXMLHttpRequest = function() { 72 sinon.xhr.supportsCORS = true; 73 this.server = sinon.fakeServer.create(); 74 this.XMLHttpRequest = this.server.xhr; 75 }; 76 77 window.URL.revokeObjectURL = function () {}; 78 79 window.restore = restore; 80 81 window.ImageData = window.ImageData || function() { return false; }; 82 window.ImageBitmap = window.ImageBitmap || function() { return false; }; 83 window.WebGLFramebuffer = window.WebGLFramebuffer || Object; 84 Object.assign(_window, window); // eslint-disable-line no-restricted-properties 85 86 return window; 87}
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.