From 139d97179f82e73025d9d498ab1dd710b8325bae Mon Sep 17 00:00:00 2001 From: gorhill Date: Mon, 15 May 2017 08:51:25 -0400 Subject: [PATCH] as per #2612, use native Set() in content scripts --- platform/chromium/vapi-client.js | 108 ------------------------------- platform/firefox/vapi-client.js | 40 ------------ src/js/contentscript.js | 26 +++----- 3 files changed, 10 insertions(+), 164 deletions(-) diff --git a/platform/chromium/vapi-client.js b/platform/chromium/vapi-client.js index 8e659499d..39768c15f 100644 --- a/platform/chromium/vapi-client.js +++ b/platform/chromium/vapi-client.js @@ -72,114 +72,6 @@ if ( vAPI.sessionId ) { /******************************************************************************/ -// Support minimally working Set() for legacy Chromium. - -if ( self.Set instanceof Function ) { - self.createSet = function() { - return new Set(); - }; -} else { - self.createSet = (function() { - //console.log('Polyfilling for ES6-like Set().'); - var PrimitiveSet = function() { - this.clear(); - }; - PrimitiveSet.prototype = { - add: function(k) { - if ( this._set[k] === undefined ) { - this._set[k] = true; - this.size += 1; - } - return this; - }, - clear: function() { - this._set = Object.create(null); - this.size = 0; - this._values = undefined; - this._i = undefined; - this.value = undefined; - this.done = true; - }, - delete: function(k) { - if ( this._set[k] === undefined ) { return false; } - delete this._set[k]; - this.size -= 1; - return true; - }, - has: function(k) { - return this._set[k] !== undefined; - }, - next: function() { - if ( this._i < this.size ) { - this.value = this._values[this._i++]; - } else { - this._values = undefined; - this.value = undefined; - this.done = true; - } - return this; - }, - polyfill: true, - values: function() { - this._values = Object.keys(this._set); - this._i = 0; - this.value = undefined; - this.done = false; - return this; - } - }; - var ReferenceSet = function() { - this.clear(); - }; - ReferenceSet.prototype = { - add: function(k) { - if ( this._set.indexOf(k) === -1 ) { - this._set.push(k); - } - }, - clear: function() { - this._set = []; - this._i = 0; - this.value = undefined; - this.done = true; - }, - delete: function(k) { - var pos = this._set.indexOf(k); - if ( pos === -1 ) { return false; } - this._set.splice(pos, 1); - return true; - }, - has: function(k) { - return this._set.indexOf(k) !== -1; - }, - next: function() { - if ( this._i === this._set.length ) { - this.value = undefined; - this.done = true; - } else { - this.value = this._set[this._i]; - this._i += 1; - } - return this; - }, - polyfill: true, - values: function() { - this._i = 0; - this.done = false; - return this; - } - }; - Object.defineProperty(ReferenceSet.prototype, 'size', { - get: function() { return this._set.length; } - }); - return function(type) { - return type === 'object' ? new ReferenceSet() : new PrimitiveSet(); - }; - })(); -} - -/******************************************************************************/ - var referenceCounter = 0; vAPI.lock = function() { diff --git a/platform/firefox/vapi-client.js b/platform/firefox/vapi-client.js index 606f36f12..5ec47c0eb 100644 --- a/platform/firefox/vapi-client.js +++ b/platform/firefox/vapi-client.js @@ -54,46 +54,6 @@ var vAPI = self.vAPI; /******************************************************************************/ -// Support minimally working Set() for legacy Firefox: iterator for legacy -// Set() does not work like the one for standard ES6 Set(). - -if ( self.Set.prototype.iterator instanceof Function === false ) { - self.createSet = function() { - return new Set(); - }; -} else { - self.createSet = (function() { - //console.log('Patching non-ES6 Set() to be more ES6-like.'); - var values = function() { - this._valueIter = this.prototype.values(); - this.value = undefined; - this.done = false; - return this; - }; - var next = function() { - try { - this.value = this._valueIter.next(); - } catch (ex) { - this._valueIter = undefined; - this.value = undefined; - this.done = true; - } - return this; - }; - return function() { - var r = new Set(); - r._valueIter = undefined; - r.value = undefined; - r.done = false; - r.values = values.bind(r); - r.next = next.bind(r); - return r; - }; - })(); -} - -/******************************************************************************/ - var referenceCounter = 0; vAPI.lock = function() { diff --git a/src/js/contentscript.js b/src/js/contentscript.js index dab855408..730f0d895 100644 --- a/src/js/contentscript.js +++ b/src/js/contentscript.js @@ -19,8 +19,6 @@ Home: https://github.com/gorhill/uBlock */ -/* global createSet */ - 'use strict'; /******************************************************************************* @@ -139,15 +137,15 @@ vAPI.domFilterer = (function() { /******************************************************************************/ -var allExceptions = createSet(), - allSelectors = createSet(), +var allExceptions = new Set(), + allSelectors = new Set(), stagedNodes = []; // Complex selectors, due to their nature may need to be "de-committed". A // Set() is used to implement this functionality. var complexSelectorsOldResultSet, - complexSelectorsCurrentResultSet = createSet('object'); + complexSelectorsCurrentResultSet = new Set(); /******************************************************************************/ @@ -597,7 +595,7 @@ var domFilterer = { this.commitTimer.clear(); var beforeHiddenNodeCount = this.hiddenNodeCount, - styleText = '', i; + styleText = ''; // CSS rules/hide if ( this.newHideSelectorBuffer.length ) { @@ -615,7 +613,7 @@ var domFilterer = { // Simple css selectors/hide if ( this.simpleHideSelectors.entries.length ) { - i = stagedNodes.length; + var i = stagedNodes.length; while ( i-- ) { this.simpleHideSelectors.forEachNode(hideNode, stagedNodes[i], cssNotHiddenId); } @@ -624,7 +622,7 @@ var domFilterer = { // Complex selectors: non-incremental. complexSelectorsOldResultSet = complexSelectorsCurrentResultSet; - complexSelectorsCurrentResultSet = createSet('object'); + complexSelectorsCurrentResultSet = new Set(); // Complex css selectors/hide // The handling of these can be considered optional, since they are @@ -658,14 +656,10 @@ var domFilterer = { } // Un-hide nodes previously hidden. - i = complexSelectorsOldResultSet.size; - if ( i !== 0 ) { - var iter = complexSelectorsOldResultSet.values(); - while ( i-- ) { - this.unhideNode(iter.next().value); - } - complexSelectorsOldResultSet.clear(); + for ( var node of complexSelectorsOldResultSet ) { + this.unhideNode(node); } + complexSelectorsOldResultSet.clear(); // If DOM nodes have been affected, lazily notify core process. if ( @@ -1303,7 +1297,7 @@ vAPI.domSurveyor = (function() { cosmeticSurveyingMissCount = 0, highGenerics = null, lowGenericSelectors = [], - queriedSelectors = createSet(), + queriedSelectors = new Set(), surveyCost = 0; // Handle main process' response.