1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00

as per #2612, use native Set() in content scripts

This commit is contained in:
gorhill 2017-05-15 08:51:25 -04:00
parent 8f42232105
commit 139d97179f
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 10 additions and 164 deletions

View File

@ -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() {

View File

@ -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() {

View File

@ -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.