Skip to content

Commit

Permalink
HTML Reporter: Avoid leaking 'Map' in older browsers
Browse files Browse the repository at this point in the history
On IE6-8 and in PhantomJS, our partial ES6 Map implementation
was leaking as global variable.

Follows 2f2eaba.
Fixes #1508.
  • Loading branch information
Krinkle authored Nov 15, 2020
1 parent e91a659 commit 259f9af
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
15 changes: 15 additions & 0 deletions reporter/es6-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Support IE 9-10, PhantomJS: Fallback for fuzzysort.js used by /reporter/html.js
// eslint-disable-next-line no-unused-vars
var Map = typeof Map === "function" ? Map : function StringMap() {
var store = Object.create( null );
this.get = function( strKey ) {
return store[ strKey ];
};
this.set = function( strKey, val ) {
store[ strKey ] = val;
return this;
};
this.clear = function() {
store = Object.create( null );
};
};
11 changes: 11 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env node */

const fs = require( "fs" );
const { babel } = require( "@rollup/plugin-babel" );
const { nodeResolve } = require( "@rollup/plugin-node-resolve" );
const commonjs = require( "@rollup/plugin-commonjs" );
Expand Down Expand Up @@ -28,6 +29,16 @@ module.exports = {
* Date: @DATE\n\
*/",

intro: function() {

// Define the (partial) ES6 Map polyfill for "fuzzysort".
// Per https://github.com/qunitjs/qunit/issues/1508:
// 1. Must not leak as global variable, since it's not full Map implementation.
// 2. Must be seen by fuzzysort as-is (e.g. not get renamed as normal
// variables in an imported file would be).
return fs.readFileSync( __dirname + "/reporter/es6-map.js", "utf-8" ).toString().trim();
},

globals: {
global: "(function() { return this; }())"
}
Expand Down
17 changes: 0 additions & 17 deletions src/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,3 @@ export const localSessionStorage = ( function() {
return undefined;
}
}() );

// Support IE 9-10: Fallback for fuzzysort.js used by /reporter/html.js
if ( !global.Map ) {
global.Map = function StringMap() {
var store = Object.create( null );
this.get = function( strKey ) {
return store[ strKey ];
};
this.set = function( strKey, val ) {
store[ strKey ] = val;
return this;
};
this.clear = function() {
store = Object.create( null );
};
};
}

0 comments on commit 259f9af

Please sign in to comment.