| Index: lib/content/elemHideEmulation.js |
| =================================================================== |
| --- a/lib/content/elemHideEmulation.js |
| +++ b/lib/content/elemHideEmulation.js |
| @@ -302,14 +302,12 @@ |
| !pattern.selectors.some(s => s.requiresHiding); |
| } |
| -function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc, |
| - hideElemsFunc) |
| +function ElemHideEmulation(addSelectorsFunc, hideElemsFunc) |
| { |
| - this.window = window; |
| - this.getFiltersFunc = getFiltersFunc; |
| + this.document = document; |
|
Sebastian Noack
2017/09/27 00:20:45
We still need a way to mock the document the eleme
|
| this.addSelectorsFunc = addSelectorsFunc; |
| this.hideElemsFunc = hideElemsFunc; |
| - this.observer = new window.MutationObserver(this.observe.bind(this)); |
| + this.observer = new MutationObserver(this.observe.bind(this)); |
| } |
| ElemHideEmulation.prototype = { |
| @@ -317,7 +315,7 @@ |
| { |
| try |
| { |
| - return new URL(stylesheet.href).origin == this.window.location.origin; |
| + return new URL(stylesheet.href).origin == this.document.location.origin; |
| } |
| catch (e) |
| { |
| @@ -348,10 +346,9 @@ |
| let content = parseSelectorContent(selector, startIndex); |
| if (!content) |
| { |
| - this.window.console.error( |
| - new SyntaxError("Failed to parse Adblock Plus " + |
| - `selector ${selector} ` + |
| - "due to unmatched parentheses.")); |
| + console.error(new SyntaxError("Failed to parse Adblock Plus " + |
| + `selector ${selector} ` + |
| + "due to unmatched parentheses.")); |
| return null; |
| } |
| if (match[1] == "properties") |
| @@ -368,10 +365,9 @@ |
| else |
| { |
| // this is an error, can't parse selector. |
| - this.window.console.error( |
| - new SyntaxError("Failed to parse Adblock Plus " + |
| - `selector ${selector}, invalid ` + |
| - `pseudo-class :-abp-${match[1]}().`)); |
| + console.error(new SyntaxError("Failed to parse Adblock Plus " + |
| + `selector ${selector}, invalid ` + |
| + `pseudo-class :-abp-${match[1]}().`)); |
| return null; |
| } |
| @@ -383,10 +379,9 @@ |
| if (selectors.length == 1 && selectors[0] instanceof ContainsSelector) |
| { |
| - this.window.console.error( |
| - new SyntaxError("Failed to parse Adblock Plus " + |
| - `selector ${selector}, can't ` + |
| - "have a lonely :-abp-contains().")); |
| + console.error(new SyntaxError("Failed to parse Adblock Plus " + |
| + `selector ${selector}, can't ` + |
| + "have a lonely :-abp-contains().")); |
| return null; |
| } |
| return selectors; |
| @@ -414,7 +409,7 @@ |
| let stylesheetOnlyChange = !!stylesheets; |
| if (!stylesheets) |
| - stylesheets = this.window.document.styleSheets; |
| + stylesheets = this.document.styleSheets; |
| // Chrome < 51 doesn't have an iterable StyleSheetList |
| // https://issues.adblockplus.org/ticket/5381 |
| @@ -442,15 +437,13 @@ |
| } |
| } |
| - let {document} = this.window; |
| - |
| let patterns = this.patterns.slice(); |
| let pattern = null; |
| let generator = null; |
| let processPatterns = () => |
| { |
| - let cycleStart = this.window.performance.now(); |
| + let cycleStart = performance.now(); |
| if (!pattern) |
| { |
| @@ -471,7 +464,8 @@ |
| pattern = null; |
| return processPatterns(); |
| } |
| - generator = evaluate(pattern.selectors, 0, "", document, cssStyles); |
| + generator = evaluate(pattern.selectors, 0, "", |
| + this.document, cssStyles); |
| } |
| for (let selector of generator) |
| { |
| @@ -484,17 +478,16 @@ |
| } |
| else |
| { |
| - for (let element of document.querySelectorAll(selector)) |
| + for (let element of this.document.querySelectorAll(selector)) |
| { |
| elements.push(element); |
| elementFilters.push(pattern.text); |
| } |
| } |
| } |
| - if (this.window.performance.now() - |
| - cycleStart > MAX_SYNCHRONOUS_PROCESSING_TIME) |
| + if (performance.now() - cycleStart > MAX_SYNCHRONOUS_PROCESSING_TIME) |
| { |
| - this.window.setTimeout(processPatterns, 0); |
| + setTimeout(processPatterns, 0); |
| return; |
| } |
| } |
| @@ -531,7 +524,7 @@ |
| { |
| let completion = () => |
| { |
| - this._lastInvocation = this.window.performance.now(); |
| + this._lastInvocation = performance.now(); |
| this._filteringInProgress = false; |
| if (this._scheduledProcessing) |
| { |
| @@ -552,19 +545,17 @@ |
| { |
| this._scheduledProcessing = {stylesheets}; |
| } |
| - else if (this.window.performance.now() - |
| - this._lastInvocation < MIN_INVOCATION_INTERVAL) |
| + else if (performance.now() - this._lastInvocation < MIN_INVOCATION_INTERVAL) |
| { |
| this._scheduledProcessing = {stylesheets}; |
| - this.window.setTimeout(() => |
| + setTimeout(() => |
| { |
| let newStylesheets = this._scheduledProcessing.stylesheets; |
| this._filteringInProgress = true; |
| this._scheduledProcessing = null; |
| this._addSelectors(newStylesheets, completion); |
| }, |
| - MIN_INVOCATION_INTERVAL - |
| - (this.window.performance.now() - this._lastInvocation)); |
| + MIN_INVOCATION_INTERVAL - (performance.now() - this._lastInvocation)); |
| } |
| else |
| { |
| @@ -585,34 +576,30 @@ |
| this.queueFiltering(); |
| }, |
| - apply() |
| + apply(patterns) |
| { |
| - this.getFiltersFunc(patterns => |
| + this.patterns = []; |
| + for (let pattern of patterns) |
| { |
| - this.patterns = []; |
| - for (let pattern of patterns) |
| - { |
| - let selectors = this.parseSelector(pattern.selector); |
| - if (selectors != null && selectors.length > 0) |
| - this.patterns.push({selectors, text: pattern.text}); |
| - } |
| + let selectors = this.parseSelector(pattern.selector); |
| + if (selectors != null && selectors.length > 0) |
| + this.patterns.push({selectors, text: pattern.text}); |
| + } |
| - if (this.patterns.length > 0) |
| - { |
| - let {document} = this.window; |
| - this.queueFiltering(); |
| - this.observer.observe( |
| - document, |
| - { |
| - childList: true, |
| - attributes: true, |
| - characterData: true, |
| - subtree: true |
| - } |
| - ); |
| - document.addEventListener("load", this.onLoad.bind(this), true); |
| - } |
| - }); |
| + if (this.patterns.length > 0) |
| + { |
| + this.queueFiltering(); |
| + this.observer.observe( |
| + this.document, |
| + { |
| + childList: true, |
| + attributes: true, |
| + characterData: true, |
| + subtree: true |
| + } |
| + ); |
| + this.document.addEventListener("load", this.onLoad.bind(this), true); |
| + } |
| } |
| }; |