diff options
author | Jesús <heckyel@hyperbola.info> | 2019-12-30 16:10:54 -0500 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2019-12-30 16:10:54 -0500 |
commit | d1d3aaa224ec068dc8d267e4d64255f88b6641a0 (patch) | |
tree | 9e6b679c529dca7217becc7856ea6aac2d3069f7 | |
parent | fa6fb12f089ff0170d0f3ee93b6ce7131a6a2254 (diff) | |
download | ematrix-d1d3aaa224ec068dc8d267e4d64255f88b6641a0.tar.lz ematrix-d1d3aaa224ec068dc8d267e4d64255f88b6641a0.tar.xz ematrix-d1d3aaa224ec068dc8d267e4d64255f88b6641a0.zip |
fix syntax libraries
-rw-r--r-- | lib/CallbackWrapper.jsm | 16 | ||||
-rw-r--r-- | lib/FrameModule.jsm | 68 | ||||
-rw-r--r-- | lib/HttpRequestHeaders.jsm | 28 | ||||
-rw-r--r-- | lib/LiquidDict.jsm | 92 | ||||
-rw-r--r-- | lib/PendingRequests.jsm | 110 | ||||
-rw-r--r-- | lib/PublicSuffixList.jsm | 270 | ||||
-rw-r--r-- | lib/Punycode.jsm | 260 | ||||
-rw-r--r-- | lib/UriTools.jsm | 390 |
8 files changed, 617 insertions, 617 deletions
diff --git a/lib/CallbackWrapper.jsm b/lib/CallbackWrapper.jsm index 1d3664e..55fdf11 100644 --- a/lib/CallbackWrapper.jsm +++ b/lib/CallbackWrapper.jsm @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see {http://www.gnu.org/licenses/}. - Home: https://gitlab.com/vannilla/ematrix + Home: https://libregit.org/heckyel/ematrix uMatrix Home: https://github.com/gorhill/uMatrix */ @@ -28,7 +28,7 @@ var EXPORTED_SYMBOLS = ['CallbackWrapper']; var junkyard = []; var CallbackWrapper = function (messageManager, channelName, - listenerId, requestId) { + listenerId, requestId) { // This allows to avoid creating a closure for every single // message which expects an answer. Having a closure created // each time a message is processed has been always bothering @@ -52,19 +52,19 @@ var CallbackWrapper = function (messageManager, channelName, }; CallbackWrapper.factory = function (messageManager, channelName, - listenerId, requestId) { + listenerId, requestId) { let wrapper = junkyard.pop(); if (wrapper) { wrapper.init(messageManager, channelName, listenerId, requestId); return wrapper; } - + return new CallbackWrapper(messageManager, channelName, - listenerId, requestId); + listenerId, requestId); }; CallbackWrapper.prototype.init = function (messageManager, channelName, - listenerId, requestId) { + listenerId, requestId) { this.messageManager = messageManager; this.channelName = channelName; this.listenerId = listenerId; @@ -86,7 +86,7 @@ CallbackWrapper.prototype.proxy = function (response) { // Mark for reuse this.messageManager = this.channelName = - this.requestId = this.listenerId = null; - + this.requestId = this.listenerId = null; + junkyard.push(this); }; diff --git a/lib/FrameModule.jsm b/lib/FrameModule.jsm index ef149ae..016327d 100644 --- a/lib/FrameModule.jsm +++ b/lib/FrameModule.jsm @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see {http://www.gnu.org/licenses/}. - Home: https://gitlab.com/vannilla/ematrix + Home: https://libregit.org/heckyel/ematrix uMatrix Home: https://github.com/gorhill/uMatrix */ @@ -70,7 +70,7 @@ var contentObserver = { modernFirefox: (Services.appinfo.ID === '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}' || Services.appinfo.ID === '{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}') - && Services.vc.compare(Services.appinfo.version, '44') > 0, + && Services.vc.compare(Services.appinfo.version, '44') > 0, get componentRegistrar() { return Components.manager.QueryInterface(Ci.nsIComponentRegistrar); @@ -78,14 +78,14 @@ var contentObserver = { get categoryManager() { return Components.classes['@mozilla.org/categorymanager;1'] - .getService(Ci.nsICategoryManager); + .getService(Ci.nsICategoryManager); }, QueryInterface: XPCOMUtils.generateQI([ - Ci.nsIFactory, - Ci.nsIObserver, - Ci.nsIContentPolicy, - Ci.nsISupportsWeakReference + Ci.nsIFactory, + Ci.nsIObserver, + Ci.nsIContentPolicy, + Ci.nsISupportsWeakReference ]), createInstance: function (outer, iid) { @@ -101,16 +101,16 @@ var contentObserver = { if (!this.modernFirefox) { this.componentRegistrar - .registerFactory(this.classID, - this.classDescription, - this.contractID, - this); + .registerFactory(this.classID, + this.classDescription, + this.contractID, + this); this.categoryManager - .addCategoryEntry('content-policy', - this.contractID, - this.contractID, - false, - true); + .addCategoryEntry('content-policy', + this.contractID, + this.contractID, + false, + true); } }, @@ -119,12 +119,12 @@ var contentObserver = { if (!this.modernFirefox) { this.componentRegistrar - .unregisterFactory(this.classID, - this); + .unregisterFactory(this.classID, + this); this.categoryManager - .deleteCategoryEntry('content-policy', - this.contractID, - false); + .deleteCategoryEntry('content-policy', + this.contractID, + false); } }, @@ -221,17 +221,17 @@ var contentObserver = { }; messager.addMessageListener(sandbox._sandboxId_, - sandbox._messageListener_); + sandbox._messageListener_); messager.addMessageListener(hostName + ':broadcast', - sandbox._messageListener_); + sandbox._messageListener_); }; sandbox.removeMessageListener = function () { try { messager.removeMessageListener(sandbox._sandboxId_, - sandbox._messageListener_); + sandbox._messageListener_); messager.removeMessageListener(hostName + ':broadcast', - sandbox._messageListener_); + sandbox._messageListener_); } catch (ex) { // It throws sometimes, mostly when the popup closes } @@ -261,8 +261,8 @@ var contentObserver = { } if (loc.protocol !== 'http:' - && loc.protocol !== 'https:' - && loc.protocol !== 'file:') { + && loc.protocol !== 'https:' + && loc.protocol !== 'file:') { if (loc.protocol === 'chrome:' && loc.host === hostName) { this.initContentScripts(win); } @@ -293,9 +293,9 @@ var contentObserver = { doc.addEventListener('DOMContentLoaded', docReady, true); } else { docReady({ - target: doc, - type: 'DOMContentLoaded', - }); + target: doc, + type: 'DOMContentLoaded', + }); } } }; @@ -325,13 +325,13 @@ LocationChangeListener.prototype.QueryInterface = XPCOMUtils.generateQI([ LocationChangeListener.prototype.onLocationChange = function (webProgress, request, location, flags) { - if (!webProgress.isTopLevel) { + if (!webProgress.isTopLevel) { return; - } - this.messageManager.sendAsyncMessage(locationChangedMessageName, { + } + this.messageManager.sendAsyncMessage(locationChangedMessageName, { url: location.asciiSpec, flags: flags, - }); + }); }; contentObserver.register(); diff --git a/lib/HttpRequestHeaders.jsm b/lib/HttpRequestHeaders.jsm index 4c125eb..9662022 100644 --- a/lib/HttpRequestHeaders.jsm +++ b/lib/HttpRequestHeaders.jsm @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see {http://www.gnu.org/licenses/}. - Home: https://gitlab.com/vannilla/ematrix + Home: https://libregit.org/heckyel/ematrix uMatrix Home: https://github.com/gorhill/uMatrix */ @@ -34,7 +34,7 @@ var HTTPRequestHeaders = function (channel) { HTTPRequestHeaders.factory = function (channel) { let entry = junkyard.pop(); if (entry) { - return entry.init(channel); + return entry.init(channel); } return new HTTPRequestHeaders(channel); @@ -44,14 +44,14 @@ HTTPRequestHeaders.prototype.init = function (channel) { this.channel = channel; this.headers = new Array(); this.originalHeaderNames = new Array(); - + channel.visitRequestHeaders({ - visitHeader: function (name, value) { - this.headers.push({name: name, value: value}); - this.originalHeaderNames.push(name); - }.bind(this) + visitHeader: function (name, value) { + this.headers.push({name: name, value: value}); + this.originalHeaderNames.push(name); + }.bind(this) }); - + return this; }; @@ -68,11 +68,11 @@ HTTPRequestHeaders.prototype.update = function () { this.setHeader(header.name, header.value, true); newHeaderNames.add(header.name); } - + //Clear any headers that were removed for (let name of this.originalHeaderNames) { if (!newHeaderNames.has(name)) { - this.channel.setRequestHeader(name, '', false); + this.channel.setRequestHeader(name, '', false); } } }; @@ -81,9 +81,9 @@ HTTPRequestHeaders.prototype.getHeader = function (name) { try { return this.channel.getRequestHeader(name); } catch (e) { - // Ignore + // Ignore } - + return ''; }; @@ -92,11 +92,11 @@ HTTPRequestHeaders.prototype.setHeader = function (name, newValue, create) { if (newValue === oldValue) { return false; } - + if (oldValue === '' && create !== true) { return false; } - + this.channel.setRequestHeader(name, newValue, false); return true; }; diff --git a/lib/LiquidDict.jsm b/lib/LiquidDict.jsm index 65de9c5..a3b6c00 100644 --- a/lib/LiquidDict.jsm +++ b/lib/LiquidDict.jsm @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see {http://www.gnu.org/licenses/}. - Home: https://gitlab.com/vannilla/ematrix + Home: https://libregit.org/heckyel/ematrix uMatrix Home: https://github.com/gorhill/uMatrix */ @@ -40,13 +40,13 @@ function meltBucket(dict, len, bucket) { --dict.frozenCount; if (bucket.charAt(0) === ' ') { - bucket.trim().split(' ').map(function (e) { - map[e] = true; - }); + bucket.trim().split(' ').map(function (e) { + map[e] = true; + }); } else { - for (let i=0; i<bucket.length; i+=len) { - map[bucket.substring(i, len)] = true; - } + for (let i=0; i<bucket.length; i+=len) { + map[bucket.substring(i, len)] = true; + } } return map; @@ -55,10 +55,10 @@ function meltBucket(dict, len, bucket) { function melt(dict) { let buckets = dict.dict; for (let key in buckets) { - let bucket = buckets[key]; - if (typeof bucket === 'string') { - buckets[key] = meltBucket(dict, key.charCodeAt(0) & 0xFF, bucket); - } + let bucket = buckets[key]; + if (typeof bucket === 'string') { + buckets[key] = meltBucket(dict, key.charCodeAt(0) & 0xFF, bucket); + } } } @@ -69,7 +69,7 @@ function freezeBucket(dict, bucket) { ++dict.frozenCount; if (wlen * words.length < dict.cutoff) { - return ' ' + words.join(' ') + ' '; + return ' ' + words.join(' ') + ' '; } return words.sort().join(''); @@ -78,31 +78,31 @@ function freezeBucket(dict, bucket) { LiquidDict.prototype.makeKey = function (word) { let len = word.length; if (len > 255) { - len = 255; + len = 255; } let i = len >> 2; return String.fromCharCode((word.charCodeAt(0) & 0x03) << 14 - | (word.charCodeAt(i) & 0x03) << 12 - | (word.charCodeAt(i+i) & 0x03) << 10 - | (word.charCodeAt(i+i+i) & 0x03) << 8 - | len); + | (word.charCodeAt(i) & 0x03) << 12 + | (word.charCodeAt(i+i) & 0x03) << 10 + | (word.charCodeAt(i+i+i) & 0x03) << 8 + | len); }; LiquidDict.prototype.test = function (word) { let key = this.makeKey(word); let bucket = this.dict[key]; - + if (bucket === undefined) { - return false; + return false; } if (typeof bucket === 'object') { - return bucket[word] !== undefined; + return bucket[word] !== undefined; } if (bucket.charAt(0) === ' ') { - return bucket.indexOf(' ' + word + ' ') >= 0; + return bucket.indexOf(' ' + word + ' ') >= 0; } let len = word.length; @@ -110,16 +110,16 @@ LiquidDict.prototype.test = function (word) { let right = ~~(bucket.length / len + 0.5); while (left < right) { - let i = left + right >> 1; - let needle = bucket.substr(len * i, len); - - if (word < needle) { - right = i; - } else if (word > needle) { - left = i + 1; - } else { - return true; - } + let i = left + right >> 1; + let needle = bucket.substr(len * i, len); + + if (word < needle) { + right = i; + } else if (word > needle) { + left = i + 1; + } else { + return true; + } } return false; @@ -128,26 +128,26 @@ LiquidDict.prototype.test = function (word) { LiquidDict.prototype.add = function (word) { let key = this.makeKey(word); if (key === undefined) { - return false; + return false; } let bucket = this.dict[key]; if (bucket === undefined) { - this.dict[key] = bucket = {}; - ++this.bucketCount; - bucket[word] = true; - ++this.count; - - return true; + this.dict[key] = bucket = {}; + ++this.bucketCount; + bucket[word] = true; + ++this.count; + + return true; } else if (typeof bucket === 'string') { - this.dict[key] = bucket = meltBucket(this, word.len, bucket); + this.dict[key] = bucket = meltBucket(this, word.len, bucket); } if (bucket[word] === undefined) { - bucket[word] = true; - ++this.count; + bucket[word] = true; + ++this.count; - return true; + return true; } ++this.duplicateCount; @@ -159,10 +159,10 @@ LiquidDict.prototype.freeze = function () { let buckets = this.dict; for (let key in buckets) { - let bucket = buckets[key]; - if (typeof bucket === 'object') { - buckets[key] = freezeBucket(this, bucket); - } + let bucket = buckets[key]; + if (typeof bucket === 'object') { + buckets[key] = freezeBucket(this, bucket); + } } }; diff --git a/lib/PendingRequests.jsm b/lib/PendingRequests.jsm index 1214c3c..fdfab1e 100644 --- a/lib/PendingRequests.jsm +++ b/lib/PendingRequests.jsm @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see {http://www.gnu.org/licenses/}. - Home: https://gitlab.com/vannilla/ematrix + Home: https://libregit.org/heckyel/ematrix uMatrix Home: https://github.com/gorhill/uMatrix */ @@ -37,61 +37,61 @@ var ringBuffer = new Array(256); var PendingRequestBuffer = (function () { for (let i=ringBuffer.length-1; i>=0; --i) { - ringBuffer[i] = new PendingRequest(); + ringBuffer[i] = new PendingRequest(); } - + return { - createRequest: function (url) { - // URL to ring buffer index map: - // { k = URL, s = ring buffer indices } - // - // s is a string which character codes map to ring buffer - // indices -- for when the same URL is received multiple times - // by shouldLoadListener() before the existing one is serviced - // by the network request observer. I believe the use of a - // string in lieu of an array reduces memory churning. - let bucket; - let i = writePointer; - writePointer = i + 1 & 255; - - let req = ringBuffer[i]; - let str = String.fromCharCode(i); - - if (req._key !== '') { - bucket = urlToIndex.get(req._key); - if (bucket.lenght === 1) { - urlToIndex.delete(req._key); - } else { - let pos = bucket.indexOf(str); - urlToIndex.set(req._key, - bucket.slice(0, pos)+bucket.slice(pos+1)); - } - } - - bucket = urlToIndex.get(url); - urlToIndex.set(url, - (bucket === undefined) ? str : bucket + str); - req._key = url; - - return req; - }, - lookupRequest: function (url) { - let bucket = urlToIndex.get(url); - if (bucket === undefined) { - return null; - } - - let i = bucket.charCodeAt(0); - if (bucket.length === 1) { - urlToIndex.delete(url); - } else { - urlToIndex.set(url, bucket.slice(1)); - } - - let req = ringBuffer[i]; - req._key = ''; - - return req; - }, + createRequest: function (url) { + // URL to ring buffer index map: + // { k = URL, s = ring buffer indices } + // + // s is a string which character codes map to ring buffer + // indices -- for when the same URL is received multiple times + // by shouldLoadListener() before the existing one is serviced + // by the network request observer. I believe the use of a + // string in lieu of an array reduces memory churning. + let bucket; + let i = writePointer; + writePointer = i + 1 & 255; + + let req = ringBuffer[i]; + let str = String.fromCharCode(i); + + if (req._key !== '') { + bucket = urlToIndex.get(req._key); + if (bucket.lenght === 1) { + urlToIndex.delete(req._key); + } else { + let pos = bucket.indexOf(str); + urlToIndex.set(req._key, + bucket.slice(0, pos)+bucket.slice(pos+1)); + } + } + + bucket = urlToIndex.get(url); + urlToIndex.set(url, + (bucket === undefined) ? str : bucket + str); + req._key = url; + + return req; + }, + lookupRequest: function (url) { + let bucket = urlToIndex.get(url); + if (bucket === undefined) { + return null; + } + + let i = bucket.charCodeAt(0); + if (bucket.length === 1) { + urlToIndex.delete(url); + } else { + urlToIndex.set(url, bucket.slice(1)); + } + + let req = ringBuffer[i]; + req._key = ''; + + return req; + }, } })(); diff --git a/lib/PublicSuffixList.jsm b/lib/PublicSuffixList.jsm index 72b0669..b28e3b3 100644 --- a/lib/PublicSuffixList.jsm +++ b/lib/PublicSuffixList.jsm @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see {http://www.gnu.org/licenses/}. - Home: https://gitlab.com/vannilla/ematrix + Home: https://libregit.org/heckyel/ematrix uMatrix Home: https://github.com/gorhill/uMatrix */ @@ -42,43 +42,43 @@ function search(store, hostname) { let pos = hostname.lastIndexOf('.'); let tld; let remainder; - + if (pos < 0) { - tld = hostname; - remainder = hostname; + tld = hostname; + remainder = hostname; } else { - tld = hostname.slice(pos+1); - remainder = hostname.slice(0, pos); + tld = hostname.slice(pos+1); + remainder = hostname.slice(0, pos); } let sub = store[tld]; if (!sub) { - return false; + return false; } if (typeof sub === 'string') { - return (sub.indexOf(' '+remainder+' ') >= 0); + return (sub.indexOf(' '+remainder+' ') >= 0); } let l = remainder.length; let val = sub[l]; if (!val) { - return false; + return false; } let left = 0; let right = Math.floor(val.length/l+0.5); - + while (left < right) { - let i = left+right >> 1; - let key = val.substr(l*i, l); - if (remainder < key) { - right = i; - } else if (remainder > key) { - left = i+1; - } else { - return true; - } + let i = left+right >> 1; + let key = val.substr(l*i, l); + if (remainder < key) { + right = i; + } else if (remainder > key) { + left = i+1; + } else { + return true; + } } return false; @@ -86,47 +86,47 @@ function search(store, hostname) { function getPublicSuffix(hostname) { if (!hostname) { - return ''; + return ''; } while (true) { - let pos = hostname.indexOf('.'); - if (pos < 0) { - return hostname; - } + let pos = hostname.indexOf('.'); + if (pos < 0) { + return hostname; + } - if (search(exceptions, hostname)) { - return hostname.slice(pos+1); - } + if (search(exceptions, hostname)) { + return hostname.slice(pos+1); + } - if (search(rules, hostname)) { - return hostname; - } + if (search(rules, hostname)) { + return hostname; + } - if (search(rules, '*'+hostname.slice(pos))) { - return hostname; - } + if (search(rules, '*'+hostname.slice(pos))) { + return hostname; + } - hostname = hostname.slice(pos+1); + hostname = hostname.slice(pos+1); } } function getDomain(hostname) { if (!hostname || hostname.charAt(0) == '.') { - return ''; + return ''; } hostname = hostname.toLowerCase(); let suffix = getPublicSuffix(hostname); if (suffix === hostname) { - return ''; + return ''; } let len = hostname.length-suffix.length; let pos = hostname.lastIndexOf('.', hostname.lastIndexOf('.', len) - 1); if (pos <= 0) { - return hostname; + return hostname; } return hostname.slice(pos+1); @@ -134,38 +134,38 @@ function getDomain(hostname) { function crystallize(store) { for (let tld in store) { - if (!store.hasOwnProperty(tld)) { - continue; - } - - let suff = store[tld].join(' '); - if (!suff) { - store[tld] = ''; - continue; - } - - if (suff.length < cutoffLength) { - store[tld] = ' ' + suff + ' '; - continue; - } - - suff = []; - for (let i=store[tld].length-1; i>=0; --i) { - let s = store[tld][i]; - let l = s.length; - if (!suff[l]) { - suff[l] = []; - } - suff[l].push(s); - } - - for (let i=suff.length-1; i>=0; --i) { - if (suff[i]) { - suff[i] = suff[i].sort().join(''); - } - } - - store[tld] = suff; + if (!store.hasOwnProperty(tld)) { + continue; + } + + let suff = store[tld].join(' '); + if (!suff) { + store[tld] = ''; + continue; + } + + if (suff.length < cutoffLength) { + store[tld] = ' ' + suff + ' '; + continue; + } + + suff = []; + for (let i=store[tld].length-1; i>=0; --i) { + let s = store[tld][i]; + let l = s.length; + if (!suff[l]) { + suff[l] = []; + } + suff[l].push(s); + } + + for (let i=suff.length-1; i>=0; --i) { + if (suff[i]) { + suff[i] = suff[i].sort().join(''); + } + } + + store[tld] = suff; } return store; @@ -180,61 +180,61 @@ function parse(text, toAscii) { let tend = text.length; while (beg < tend) { - end = text.indexOf('\n', beg); - if (end < 0) { - end = text.indexOf('\r', beg); - if (end < 0) { - end = tend; - } - } - - let line = text.slice(beg, end).trim(); - beg = end+1; - - if (line.length === 0) { - continue; - } - - let pos = line.indexOf('//'); - if (pos >= 0) { - line = line.slice(0, pos); - } - - line = line.trim(); - if (!line) { - continue; - } - - let store; - if (line.charAt(0) == '!') { - store = exceptions; - line = line.slice(1); - } else { - store = rules; - } - - if (reMustPunycode.test(line)) { - line = toAscii(line); - } - - line = line.toLowerCase(); - - let tld; - pos = line.lastIndexOf('.'); - if (pos < 0) { - tld = line; - } else { - tld = line.slice(pos+1); - line = line.slice(0, pos); - } - - if (!store.hasOwnProperty(tld)) { - store[tld] = []; - } - - if (line) { - store[tld].push(line); - } + end = text.indexOf('\n', beg); + if (end < 0) { + end = text.indexOf('\r', beg); + if (end < 0) { + end = tend; + } + } + + let line = text.slice(beg, end).trim(); + beg = end+1; + + if (line.length === 0) { + continue; + } + + let pos = line.indexOf('//'); + if (pos >= 0) { + line = line.slice(0, pos); + } + + line = line.trim(); + if (!line) { + continue; + } + + let store; + if (line.charAt(0) == '!') { + store = exceptions; + line = line.slice(1); + } else { + store = rules; + } + + if (reMustPunycode.test(line)) { + line = toAscii(line); + } + + line = line.toLowerCase(); + + let tld; + pos = line.lastIndexOf('.'); + if (pos < 0) { + tld = line; + } else { + tld = line.slice(pos+1); + line = line.slice(0, pos); + } + + if (!store.hasOwnProperty(tld)) { + store[tld] = []; + } + + if (line) { + store[tld].push(line); + } } crystallize(exceptions); @@ -245,16 +245,16 @@ function parse(text, toAscii) { function toSelfie() { return { - magic: magic, - rules: rules, - exceptions: exception, + magic: magic, + rules: rules, + exceptions: exception, }; } function fromSelfie(selfie) { if (typeof selfie !== 'object' || typeof selfie.magic !== 'string' - || selfie.magic !== magic) { - return false; + || selfie.magic !== magic) { + return false; } rules = selfie.rules; @@ -266,33 +266,33 @@ function fromSelfie(selfie) { var addListener = function (listeners, callback) { if (typeof callback !== 'function') { - return; + return; } if (listeners.indexOf(callback) === -1) { - listeners.push(callback); + listeners.push(callback); } }; var removeListener = function (listeners, callback) { let pos = listeners.indexOf(callback); if (pos !== -1) { - listeners.splice(pos, 1); + listeners.splice(pos, 1); } }; var callListeners = function (listeners) { for (let i=0; i<listeners.length; ++i) { - listeners[i](); + listeners[i](); } }; var onChanged = { addListener: function (callback) { - addListener(onChangedListeners, callback); + addListener(onChangedListeners, callback); }, removeListener: function (callback) { - removeListener(onChangedListeners, callback); + removeListener(onChangedListeners, callback); }, }; diff --git a/lib/Punycode.jsm b/lib/Punycode.jsm index 7486186..fb91610 100644 --- a/lib/Punycode.jsm +++ b/lib/Punycode.jsm @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see {http://www.gnu.org/licenses/}. - Home: https://gitlab.com/vannilla/ematrix + Home: https://libregit.org/heckyel/ematrix uMatrix Home: https://github.com/gorhill/uMatrix */ @@ -44,8 +44,8 @@ function mapDomain(domain, cb) { let res = ''; if (parts.length > 1) { - res = parts[0] + '@'; - domain = parts[1]; + res = parts[0] + '@'; + domain = parts[1]; } domain = domain.replace(reSeparator, '\x2E'); @@ -62,22 +62,22 @@ function ucs2decode(str) { let len = str.length; while (count < len) { - let val = str.charCodeAt(count); - ++count; - - if (val >= 0xD800 && val <= 0xDBFF && cound < len) { - let extra = str.charCodeAt(count); - ++count; - - if ((extra & 0xFC00) == 0xDC00) { - res.push(((val & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); - } else { - res.push(val); - --count; - } - } else { - res.push(val); - } + let val = str.charCodeAt(count); + ++count; + + if (val >= 0xD800 && val <= 0xDBFF && cound < len) { + let extra = str.charCodeAt(count); + ++count; + + if ((extra & 0xFC00) == 0xDC00) { + res.push(((val & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); + } else { + res.push(val); + --count; + } + } else { + res.push(val); + } } return res; @@ -85,29 +85,29 @@ function ucs2decode(str) { function ucs2encode(array) { return array.map(function (e) { - let res = ''; + let res = ''; - if (e > 0xFFFF) { - e -= 0x10000; - res += String.fromCharCode(e >>> 10 & 0x3FF | 0xD800); - e = 0xDC00 | e & 0x3FF; - } + if (e > 0xFFFF) { + e -= 0x10000; + res += String.fromCharCode(e >>> 10 & 0x3FF | 0xD800); + e = 0xDC00 | e & 0x3FF; + } - res += String.fromCharCode(e); + res += String.fromCharCode(e); - return res; + return res; }).join(''); } function basicToDigit(point) { if (point - 0x30 < 0x0A) { - return point - 0x16; + return point - 0x16; } if (point - 0x41 < 0x1A) { - return point - 0x41; + return point - 0x41; } if (point - 0x61 < 0x1A) { - return point - 0x61; + return point - 0x61; } return base; } @@ -122,7 +122,7 @@ function adapt(delta, num, first) { delta += Math.floor(delta/num); for (; delta>(base - tMin) * tMax >> 1; k+=base) { - delta = Math.floor(delta/(base-tMin)); + delta = Math.floor(delta/(base-tMin)); } return Math.floor(k + (base - tMin + 1) * delta / (delta + skew)); @@ -137,63 +137,63 @@ function decode(input) { let basic = input.lastIndexOf('-'); if (basic < 0) { - basic = 0; + basic = 0; } for (let j=0; j<basic; ++j) { - if (input.charCodeAt(j) >= 0x80) { - throw new Error('not basic code point'); - } + if (input.charCodeAt(j) >= 0x80) { + throw new Error('not basic code point'); + } - res.push(input.charCodeAt(j)); + res.push(input.charCodeAt(j)); } for (let k=(basic > 0) ? basic + 1 : 0; k<len;) { - let old = i; + let old = i; - for (let w=1, x=base; ; x+=base) { - if (k >= len) { - throw new Error('invalid input'); - } + for (let w=1, x=base; ; x+=base) { + if (k >= len) { + throw new Error('invalid input'); + } - let digit = basicToDigit(input.charCodeAt(k)); - ++k; + let digit = basicToDigit(input.charCodeAt(k)); + ++k; - if (digit >= base || digit > Math.floor((maxInt-i) / w)) { - throw new Error('overflow'); - } + if (digit >= base || digit > Math.floor((maxInt-i) / w)) { + throw new Error('overflow'); + } - i += digit * w; + i += digit * w; - let t = x <= bias ? - tMin : - (t >= bias + tMax ? - tMax : - k - bias); + let t = x <= bias ? + tMin : + (t >= bias + tMax ? + tMax : + k - bias); - if (digit < t) { - break; - } + if (digit < t) { + break; + } - if (w > Math.floor(maxInt/(base - t))) { - throw new Error('overflow'); - } + if (w > Math.floor(maxInt/(base - t))) { + throw new Error('overflow'); + } - w *= (base -t); - } + w *= (base -t); + } - let out = res.length+1; - bias = adapt(i-old, out, old==0); + let out = res.length+1; + bias = adapt(i-old, out, old==0); - if (Math.floor(i/out) > maxInt-n) { - throw new Error('overflow'); - } + if (Math.floor(i/out) > maxInt-n) { + throw new Error('overflow'); + } - n += Math.floor(i/out); - i %= out; + n += Math.floor(i/out); + i %= out; - res.splice(i, 0, n); - ++i; + res.splice(i, 0, n); + ++i; } return ucs2encode(res); @@ -211,71 +211,71 @@ function encode(input) { let bias = 72; for (let j=0; j<len; ++j) { - let val = input[j]; - if (val < 0x80) { - res.push(String.fromCharCode(val)); - } + let val = input[j]; + if (val < 0x80) { + res.push(String.fromCharCode(val)); + } } let blen = res.length; let count = blen; if (blen) { - res.push('-'); + res.push('-'); } - + while (count < len) { - let m = maxInt; - for (let j=0; j<len; ++j) { - let val = input[j]; - if (val >= n && val <= m) { - m = val; - } - } - - if (m - n > Math.floor((maxInt - delta)/(count+1))) { - throw new Error('overflow'); - } - - delta += (m - n) * (count + 1); - n = m; - - for (let j=0; j<len; ++j) { - let val = input[j]; - - if (val < n && ++delta > maxInt) { - throw new Error('overflow'); - } - - if (val == n) { - let q = delta; - for (let k=base; ; k+=base) { - let t = k <= bias ? - tMin : - (k >= bias + tMax ? - tMax: - k - bias); - - if (q < t) { - break; - } - - res.push - (String.fromCharCode - (digitToBasic(t + (q-t) % (base-t), 0))); - - q = Math.floor((q-t)/(base-t)); - } - - res.push(String.fromCharCode(digitToBasic(q, 0))); - bias = adapt(delta, count+1, count==blen); - delta = 0; - ++count; - } - } - - ++delta; - ++n; + let m = maxInt; + for (let j=0; j<len; ++j) { + let val = input[j]; + if (val >= n && val <= m) { + m = val; + } + } + + if (m - n > Math.floor((maxInt - delta)/(count+1))) { + throw new Error('overflow'); + } + + delta += (m - n) * (count + 1); + n = m; + + for (let j=0; j<len; ++j) { + let val = input[j]; + + if (val < n && ++delta > maxInt) { + throw new Error('overflow'); + } + + if (val == n) { + let q = delta; + for (let k=base; ; k+=base) { + let t = k <= bias ? + tMin : + (k >= bias + tMax ? + tMax: + k - bias); + + if (q < t) { + break; + } + + res.push + (String.fromCharCode + (digitToBasic(t + (q-t) % (base-t), 0))); + + q = Math.floor((q-t)/(base-t)); + } + + res.push(String.fromCharCode(digitToBasic(q, 0))); + bias = adapt(delta, count+1, count==blen); + delta = 0; + ++count; + } + } + + ++delta; + ++n; } return res.join(''); @@ -283,20 +283,20 @@ function encode(input) { function toUnicode(input) { return mapDomain(input, function (e) { - return rePuny.test(e) ? decode(e.slice(4).toLowerCase()) : e; + return rePuny.test(e) ? decode(e.slice(4).toLowerCase()) : e; }); } function toASCII(input) { return mapDomain(input, function (e) { - return reNonAscii.test(e) ? 'xn--' + encode(e) : e; + return reNonAscii.test(e) ? 'xn--' + encode(e) : e; }); } var Punycode = { ucs2: { - decode: ucs2decode, - encode: ucs2encode, + decode: ucs2decode, + encode: ucs2encode, }, decode: decode, encode: encode, diff --git a/lib/UriTools.jsm b/lib/UriTools.jsm index 4971909..745bb56 100644 --- a/lib/UriTools.jsm +++ b/lib/UriTools.jsm @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see {http://www.gnu.org/licenses/}. - Home: https://gitlab.com/vannilla/ematrix + Home: https://libregit.org/heckyel/ematrix uMatrix Home: https://github.com/gorhill/uMatrix */ @@ -99,9 +99,9 @@ function URI() { this.fragmentBit = (1 << 7), this.allBits = (0xFFFF), this.authorityBit = - (this.userBit | this.passwordBit | this.hostnameBit | this.portBit); + (this.userBit | this.passwordBit | this.hostnameBit | this.portBit); this.normalizeBits = - (this.schemeBit | this.hostnameBit | this.pathBit | this.queryBit); + (this.schemeBit | this.hostnameBit | this.pathBit | this.queryBit); } var cached = new URI(); @@ -125,28 +125,28 @@ DomainCacheEntry.prototype.init = function (domain) { DomainCacheEntry.prototype.dispose = function () { this.domain = ''; if (cacheJunkyard.length < junkyardMax) { - cacheJunkyard.push(this); + cacheJunkyard.push(this); } }; var domainCacheEntryFactory = function (domain) { let entry = cacheJunkyard.pop(); if (entry) { - return entry.init(domain); + return entry.init(domain); } return new DomainCacheEntry(domain); }; var domainCacheAdd = function (hostname, domain) { let entry = domainCache.get(hostname); - + if (entry !== undefined) { - entry.tstamp = Date.now(); + entry.tstamp = Date.now(); } else { - domainCache.set(hostname, domainCacheEntryFactory(domain)); - if (domainCache.size === cacheCountHigh) { - domainCachePrune(); - } + domainCache.set(hostname, domainCacheEntryFactory(domain)); + if (domainCache.size === cacheCountHigh) { + domainCachePrune(); + } } return domain; @@ -158,11 +158,11 @@ var domainCacheSort = function (a, b) { var domainCachePrune = function () { let hostnames = - Array.from(domainCache.keys()).sort(domainCacheSort).slice(cacheCountLow); + Array.from(domainCache.keys()).sort(domainCacheSort).slice(cacheCountLow); for (let i=hostnames.length-1; i>=0; --i) { - domainCache.get(hostnames[i]).dispose(); - domainCache.delete(hostnames[i]); + domainCache.get(hostnames[i]).dispose(); + domainCache.delete(hostnames[i]); } }; @@ -174,232 +174,232 @@ publicSuffixList.onChanged.addListener(domainCacheReset); var UriTools = { set: function (uri) { - if (uri === undefined) { - return reset(cached); - } - - let matches = reRFC3986.exec(uri); - if (!matches) { - return reset(cached); - } - - cached.scheme = matches[1] !== undefined ? - matches[1].slice(0, -1) : - ''; - cached.authority = matches[2] !== undefined ? - matches[2].slice(2).toLowerCase() : - ''; - cached.path = matches[3] !== undefined ? - matches[3] : - ''; - - // As per RFC3986 - if (cached.authority !== '' && cached.path === '') { - cached.path = '/'; - } - - cached.query = matches[4] !== undefined ? - matches[4].slice(1) : - ''; - cached.fragment = matches[5] !== undefined ? - matches[5].slice(1) : - ''; - - if (reHostFromNakedAuthority.test(cached.authority)) { - cached.hostname = cached.authority; - cached.port = ''; - return cached; - } - - matches = reHostPortFromAuthority.exec(cached.authority); - if (!matches) { - matches = reIPv6PortFromAuthority.exec(cached.authority); - if (!matches) { - return resetAuthority(cached); - } - } - - cached.hostname = matches[1] !== undefined ? - matches[1] : - ''; - - if (cached.hostname.slice(-1) === '.') { - cached.hostname = cached.hostname.slice(0, -1); - } - - cached.port = matches[2] !== undefined ? - matches[2].slice(1) : - ''; - - return cached; + if (uri === undefined) { + return reset(cached); + } + + let matches = reRFC3986.exec(uri); + if (!matches) { + return reset(cached); + } + + cached.scheme = matches[1] !== undefined ? + matches[1].slice(0, -1) : + ''; + cached.authority = matches[2] !== undefined ? + matches[2].slice(2).toLowerCase() : + ''; + cached.path = matches[3] !== undefined ? + matches[3] : + ''; + + // As per RFC3986 + if (cached.authority !== '' && cached.path === '') { + cached.path = '/'; + } + + cached.query = matches[4] !== undefined ? + matches[4].slice(1) : + ''; + cached.fragment = matches[5] !== undefined ? + matches[5].slice(1) : + ''; + + if (reHostFromNakedAuthority.test(cached.authority)) { + cached.hostname = cached.authority; + cached.port = ''; + return cached; + } + + matches = reHostPortFromAuthority.exec(cached.authority); + if (!matches) { + matches = reIPv6PortFromAuthority.exec(cached.authority); + if (!matches) { + return resetAuthority(cached); + } + } + + cached.hostname = matches[1] !== undefined ? + matches[1] : + ''; + + if (cached.hostname.slice(-1) === '.') { + cached.hostname = cached.hostname.slice(0, -1); + } + + cached.port = matches[2] !== undefined ? + matches[2].slice(1) : + ''; + + return cached; }, assemble: function (bits) { - if (bits === undefined) { - bits = cached.allBits; - } + if (bits === undefined) { + bits = cached.allBits; + } - let s = []; + let s = []; - if (cached.scheme && (bits && cached.schemeBit)) { - s.push(cached.scheme, ':'); - } - if (cached.hostname && (bits & cached.hostnameBit)) { + if (cached.scheme && (bits && cached.schemeBit)) { + s.push(cached.scheme, ':'); + } + if (cached.hostname && (bits & cached.hostnameBit)) { s.push('//', cached.hostname); - } - if (cached.port && (bits & cached.portBit)) { + } + if (cached.port && (bits & cached.portBit)) { s.push(':', cached.port); - } - if (cached.path && (bits & cached.pathBit)) { + } + if (cached.path && (bits & cached.pathBit)) { s.push(cached.path); - } - if (cached.query && (bits & cached.queryBit)) { + } + if (cached.query && (bits & cached.queryBit)) { s.push('?', cached.query); - } - if (cached.fragment && (bits & cached.fragmentBit)) { + } + if (cached.fragment && (bits & cached.fragmentBit)) { s.push('#', cached.fragment); - } + } - return s.join(''); + return s.join(''); }, isNetworkScheme: function (scheme) { - return reNetworkScheme.test(scheme); + return reNetworkScheme.test(scheme); }, isSecureScheme: function(scheme) { - return reSecureScheme.test(scheme); + return reSecureScheme.test(scheme); }, originFromURI: function (uri) { - let matches = reOriginFromURI.exec(uri); - return matches !== null ? matches[0].toLowerCase() : ''; + let matches = reOriginFromURI.exec(uri); + return matches !== null ? matches[0].toLowerCase() : ''; }, schemeFromURI: function (uri) { - let matches = reSchemeFromURI.exec(uri); - return matches !== null ? matches[0].slice(0, -1).toLowerCase() : ''; + let matches = reSchemeFromURI.exec(uri); + return matches !== null ? matches[0].slice(0, -1).toLowerCase() : ''; }, authorityFromURI: function (uri) { - let matches = reAuthorityFromURI.exec(uri); - return matches !== null ? matches[1].slice(1).toLowerCase() : ''; + let matches = reAuthorityFromURI.exec(uri); + return matches !== null ? matches[1].slice(1).toLowerCase() : ''; }, hostnameFromURI: function (uri) { - let matches = reCommonHostnameFromURL.exec(uri); - if (matches) { - return matches[1]; - } - - matches = reAuthorityFromURI.exec(uri); - if (!matches) { - return ''; - } - - let auth = matches[1].slice(2); - - if (reHostFromNakedAuthority.test(auth)) { - return auth.toLowerCase(); - } - - matches = reHostFromAuthority.exec(auth); - if (!matches) { - matches = reIPv6FromAuthority.exec(auth); - if (!matches) { - return ''; - } - } - - let hostname = matches[1]; - while (hostname.endsWith('.')) { - hostname = hostname.slice(0, -1); - } - - if (reMustNormalizeHostname.test(hostname)) { - Punycode.toASCII(hostname.toLowerCase()); - } - - return hostname; + let matches = reCommonHostnameFromURL.exec(uri); + if (matches) { + return matches[1]; + } + + matches = reAuthorityFromURI.exec(uri); + if (!matches) { + return ''; + } + + let auth = matches[1].slice(2); + + if (reHostFromNakedAuthority.test(auth)) { + return auth.toLowerCase(); + } + + matches = reHostFromAuthority.exec(auth); + if (!matches) { + matches = reIPv6FromAuthority.exec(auth); + if (!matches) { + return ''; + } + } + + let hostname = matches[1]; + while (hostname.endsWith('.')) { + hostname = hostname.slice(0, -1); + } + + if (reMustNormalizeHostname.test(hostname)) { + Punycode.toASCII(hostname.toLowerCase()); + } + + return hostname; }, domainFromHostname: function (hostname) { - let entry = domainCache.get(hostname); - if (entry !== undefined) { - entry.tstamp = Date.now(); - return entry.domain; - } - - if (reIPAddressNaive.test(hostname) == false) { - return domainCacheAdd(hostname, - publicSuffixList.getDomain(hostname)); - } - - return domainCacheAdd(hostname, hostname); + let entry = domainCache.get(hostname); + if (entry !== undefined) { + entry.tstamp = Date.now(); + return entry.domain; + } + + if (reIPAddressNaive.test(hostname) == false) { + return domainCacheAdd(hostname, + publicSuffixList.getDomain(hostname)); + } + + return domainCacheAdd(hostname, hostname); }, domainFromURI: function (uri) { - if (!uri) { - return ''; - } - return UriTools.domainFromHostname(UriTools.hostnameFromURI(uri)); + if (!uri) { + return ''; + } + return UriTools.domainFromHostname(UriTools.hostnameFromURI(uri)); }, domain: function() { - return UriTools.domainFromHostname(cached.hostname); + return UriTools.domainFromHostname(cached.hostname); }, pathFromURI: function (uri) { - let matches = rePathFromURI.exec(uri); - return matches !== null ? matches[1] : ''; + let matches = rePathFromURI.exec(uri); + return matches !== null ? matches[1] : ''; }, normalizedURI: function () { - return UriTools.assemble(cached.normalizeBits); + return UriTools.assemble(cached.normalizeBits); }, rootURL: function () { - if (!cached.hostname) { - return ''; - } - return UriTools.assemble(cached.scemeBit | cached.hostnameBit); + if (!cached.hostname) { + return ''; + } + return UriTools.assemble(cached.scemeBit | cached.hostnameBit); }, isValidHostname: function (hostname) { - try { - let r = reValidHostname.test(hostname); - return r; - } catch (e) { - return false; - } + try { + let r = reValidHostname.test(hostname); + return r; + } catch (e) { + return false; + } }, parentHostnameFromHostname: function (hostname) { - // "locahost" => "" - // "example.org" => "example.org" - // "www.example.org" => "example.org" - // "tomato.www.example.org" => "example.org" - let domain = UriTools.domainFromHostname(hostname); + // "locahost" => "" + // "example.org" => "example.org" + // "www.example.org" => "example.org" + // "tomato.www.example.org" => "example.org" + let domain = UriTools.domainFromHostname(hostname); - if (domain === '' || domain === hostname) { - return undefined; - } + if (domain === '' || domain === hostname) { + return undefined; + } - return hostname.slice(hostname.indexOf('.') + 1); + return hostname.slice(hostname.indexOf('.') + 1); }, parentHostnamesFromHostname: function (hostname) { - let domain = UriTools.domainFromHostname(hostname); - if (domain === '' || domain === hostname) { - return []; - } - - let nodes = []; - for (;;) { - let pos = hostname.indexOf('.'); - if (pos < 0) { - break; - } - - hostname = hostname.slice(pos+1); - nodes.push(hostname); - if (hostname === domain) { - break; - } - } - - return nodes; + let domain = UriTools.domainFromHostname(hostname); + if (domain === '' || domain === hostname) { + return []; + } + + let nodes = []; + for (;;) { + let pos = hostname.indexOf('.'); + if (pos < 0) { + break; + } + + hostname = hostname.slice(pos+1); + nodes.push(hostname); + if (hostname === domain) { + break; + } + } + + return nodes; }, allHostNamesFromHostname: function (hostname) { - let nodes = UriTools.parentHostnamesFromHostname(hostname); - nodes.unshift(hostname); - return nodes; + let nodes = UriTools.parentHostnamesFromHostname(hostname); + nodes.unshift(hostname); + return nodes; }, toString: function () { - return UriTools.assemble(); + return UriTools.assemble(); }, }; |