code review: minor fine tuning

This commit is contained in:
gorhill 2016-08-14 08:51:52 -04:00
parent 941692e7a1
commit 48416bebee
1 changed files with 37 additions and 34 deletions

View File

@ -161,6 +161,7 @@ var reParserEx = /:(?:matches-css|has|style|xpath)\(.+?\)$/;
var allExceptions = Object.create(null), var allExceptions = Object.create(null),
allSelectors = Object.create(null), allSelectors = Object.create(null),
commitTimer = null,
stagedNodes = [], stagedNodes = [],
matchesProp = vAPI.matchesProp; matchesProp = vAPI.matchesProp;
@ -356,17 +357,15 @@ var domFilterer = {
} }
}, },
checkStyleTags: function(commitIfNeeded) { checkStyleTags_: function() {
var doc = document, var doc = document,
html = doc.documentElement, html = doc.documentElement,
head = doc.head, head = doc.head,
newParent = head || html; newParent = head || html;
if ( newParent === null ) { if ( newParent === null ) { return; }
return false; this.removedNodesHandlerMissCount += 1;
}
var styles = this.styleTags, var styles = this.styleTags,
style, oldParent, style, oldParent;
mustCommit = false;
for ( var i = 0; i < styles.length; i++ ) { for ( var i = 0; i < styles.length; i++ ) {
style = styles[i]; style = styles[i];
oldParent = style.parentNode; oldParent = style.parentNode;
@ -380,26 +379,24 @@ var domFilterer = {
oldParent.removeChild(style); oldParent.removeChild(style);
oldParent = null; oldParent = null;
} }
if ( oldParent === head || oldParent === html ) { if ( oldParent === head || oldParent === html ) { continue; }
continue;
}
style.disabled = false; style.disabled = false;
newParent.appendChild(style); newParent.appendChild(style);
mustCommit = true; this.removedNodesHandlerMissCount = 0;
} }
if ( mustCommit && commitIfNeeded ) { },
this.commit('all');
checkStyleTags: function() {
if ( this.removedNodesHandlerMissCount < 16 ) {
this.checkStyleTags_();
} }
return mustCommit;
}, },
commit_: function() { commit_: function() {
if ( stagedNodes.length === 0 ) {
return;
}
vAPI.executionCost.start(); vAPI.executionCost.start();
commitTimer = null;
var beforeHiddenNodeCount = this.hiddenNodeCount, var beforeHiddenNodeCount = this.hiddenNodeCount,
styleText = '', i, n; styleText = '', i, n;
@ -486,16 +483,20 @@ var domFilterer = {
}, },
commit: function(nodes, commitNow) { commit: function(nodes, commitNow) {
var firstCommit = stagedNodes.length === 0;
if ( nodes === 'all' ) { if ( nodes === 'all' ) {
stagedNodes = [ document.documentElement ]; stagedNodes = [ document.documentElement ];
} else if ( stagedNodes[0] !== document.documentElement ) { } else if ( stagedNodes[0] !== document.documentElement ) {
stagedNodes = stagedNodes.concat(nodes); stagedNodes = stagedNodes.concat(nodes);
} }
if ( commitNow ) { if ( commitNow ) {
if ( commitTimer !== null ) {
window.cancelAnimationFrame(commitTimer);
}
this.commit_(); this.commit_();
} else if ( firstCommit ) { return;
window.requestAnimationFrame(this.commit_.bind(this)); }
if ( commitTimer === null ) {
commitTimer = window.requestAnimationFrame(this.commit_.bind(this));
} }
}, },
@ -609,17 +610,11 @@ var domFilterer = {
}, },
domChangedHandler: function(addedNodes, removedNodes) { domChangedHandler: function(addedNodes, removedNodes) {
if ( addedNodes.length !== 0 ) { this.commit(addedNodes);
this.commit(addedNodes);
}
// https://github.com/gorhill/uBlock/issues/873 // https://github.com/gorhill/uBlock/issues/873
// This will ensure our style elements will stay in the DOM. // This will ensure our style elements will stay in the DOM.
if ( removedNodes && this.removedNodesHandlerMissCount < 16 ) { if ( removedNodes ) {
if ( domFilterer.checkStyleTags(true) ) { domFilterer.checkStyleTags();
this.removedNodesHandlerMissCount = 0;
} else {
this.removedNodesHandlerMissCount += 1;
}
} }
}, },
@ -1477,7 +1472,7 @@ vAPI.domSurveyor = (function() {
surveyPhase1(addedNodes); surveyPhase1(addedNodes);
if ( removedNodes ) { if ( removedNodes ) {
domFilterer.domChangedHandler([], true); domFilterer.checkStyleTags();
} }
}; };
@ -1506,7 +1501,8 @@ vAPI.domIsLoaded = function(ev) {
return; return;
} }
if ( ev instanceof Event ) { var slowLoad = ev instanceof Event;
if ( slowLoad ) {
document.removeEventListener('DOMContentLoaded', vAPI.domIsLoaded); document.removeEventListener('DOMContentLoaded', vAPI.domIsLoaded);
} }
vAPI.domIsLoaded = null; vAPI.domIsLoaded = null;
@ -1519,9 +1515,16 @@ vAPI.domIsLoaded = function(ev) {
if ( vAPI.domFilterer ) { if ( vAPI.domFilterer ) {
// https://github.com/chrisaljoudi/uBlock/issues/789 // https://github.com/chrisaljoudi/uBlock/issues/789
// https://github.com/gorhill/uBlock/issues/873 // https://github.com/gorhill/uBlock/issues/873
// Be sure our style tags used for cosmetic filtering are still applied. // Be sure our style tags used for cosmetic filtering are still
vAPI.domFilterer.checkStyleTags(false); // applied.
vAPI.domFilterer.commit('all'); vAPI.domFilterer.checkStyleTags();
// To avoid neddless CPU overhead, we commit existing cosmetic filters
// only if the page loaded "slowly", i.e. if the code here had to wait
// for a DOMContentLoaded event -- in which case the DOM may have
// changed a lot since last time the domFilterer acted on it.
if ( slowLoad ) {
vAPI.domFilterer.commit('all');
}
if ( vAPI.domSurveyor ) { if ( vAPI.domSurveyor ) {
vAPI.domSurveyor.start(); vAPI.domSurveyor.start();
} else { } else {