1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-05 18:32:30 +01:00

Fix parsing of recursive !#if-`!#endif directives

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/270
This commit is contained in:
Raymond Hill 2019-05-18 10:31:04 -04:00
parent ebf92c007f
commit de41c1bf53
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -923,36 +923,46 @@
µBlock.processDirectives = function(content) {
const reIf = /^!#(if|endif)\b([^\n]*)/gm;
const stack = [];
const shouldDiscard = ( ) => stack.some(v => v);
const parts = [];
let beg = 0, depth = 0, discard = false;
let beg = 0, discard = false;
while ( beg < content.length ) {
const match = reIf.exec(content);
if ( match === null ) { break; }
if ( match[1] === 'if' ) {
switch ( match[1] ) {
case 'if':
let expr = match[2].trim();
const target = expr.startsWith('!');
const target = expr.charCodeAt(0) === 0x21 /* '!' */;
if ( target ) { expr = expr.slice(1); }
const token = this.processDirectives.tokens.get(expr);
if (
depth === 0 &&
discard === false &&
const startDiscard =
token !== undefined &&
vAPI.webextFlavor.soup.has(token) === target
) {
vAPI.webextFlavor.soup.has(token) === target;
if ( discard === false && startDiscard ) {
parts.push(content.slice(beg, match.index));
discard = true;
}
depth += 1;
continue;
}
depth -= 1;
if ( depth < 0 ) { break; }
if ( depth === 0 && discard ) {
beg = match.index + match[0].length + 1;
discard = false;
stack.push(startDiscard);
break;
case 'endif':
stack.pop();
const stopDiscard = shouldDiscard() === false;
if ( discard && stopDiscard ) {
beg = match.index + match[0].length + 1;
discard = false;
}
break;
default:
break;
}
}
if ( depth === 0 && parts.length !== 0 ) {
if ( stack.length === 0 && parts.length !== 0 ) {
parts.push(content.slice(beg));
content = parts.join('\n');
}