Do not generate DNR block rule for `redirect=` filters

DNR API does not support block-then-redirect concept, hence no
point in generating a DNR block rule for `redirect=` filters.

Additionally, better handle orphaned filter anchors (`||`, `|`).
This commit is contained in:
Raymond Hill 2023-03-06 13:06:57 -05:00
parent 3b63fef848
commit a0a23085ed
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 15 additions and 6 deletions

View File

@ -210,6 +210,7 @@ function addToDNR(context, list) {
sfp.utils.preparser.prune(list.text, env) sfp.utils.preparser.prune(list.text, env)
); );
const parser = new sfp.AstFilterParser({ const parser = new sfp.AstFilterParser({
toDNR: true,
nativeCssHas: env.includes('native_css_has'), nativeCssHas: env.includes('native_css_has'),
badTypes: [ sfp.NODE_TYPE_NET_OPTION_NAME_REDIRECTRULE ], badTypes: [ sfp.NODE_TYPE_NET_OPTION_NAME_REDIRECTRULE ],
}); });

View File

@ -697,6 +697,7 @@ export class AstFilterParser {
this.hasUnicode = false; this.hasUnicode = false;
this.hasUppercase = false; this.hasUppercase = false;
// Options // Options
this.options = options;
this.interactive = options.interactive || false; this.interactive = options.interactive || false;
this.expertMode = options.expertMode || false; this.expertMode = options.expertMode || false;
this.badTypes = new Set(options.badTypes || []); this.badTypes = new Set(options.badTypes || []);
@ -1478,7 +1479,7 @@ export class AstFilterParser {
// Left anchor // Left anchor
if ( c1st === 0x7C /* '|' */ ) { if ( c1st === 0x7C /* '|' */ ) {
if ( pattern.length > 2 && c2nd === 0x7C /* '|' */ ) { if ( c2nd === 0x7C /* '|' */ ) {
const type = this.isTokenCharCode(pattern.charCodeAt(2) || 0) const type = this.isTokenCharCode(pattern.charCodeAt(2) || 0)
? NODE_TYPE_NET_PATTERN_LEFT_HNANCHOR ? NODE_TYPE_NET_PATTERN_LEFT_HNANCHOR
: NODE_TYPE_IGNORE; : NODE_TYPE_IGNORE;
@ -1486,10 +1487,9 @@ export class AstFilterParser {
if ( type === NODE_TYPE_NET_PATTERN_LEFT_HNANCHOR ) { if ( type === NODE_TYPE_NET_PATTERN_LEFT_HNANCHOR ) {
this.addFlags(AST_FLAG_NET_PATTERN_LEFT_HNANCHOR); this.addFlags(AST_FLAG_NET_PATTERN_LEFT_HNANCHOR);
} }
prev = this.linkRight(prev, next);
patternBeg += 2; patternBeg += 2;
pattern = pattern.slice(2); pattern = pattern.slice(2);
} else if ( pattern.length > 1 ) { } else {
const type = this.isTokenCharCode(c2nd) const type = this.isTokenCharCode(c2nd)
? NODE_TYPE_NET_PATTERN_LEFT_ANCHOR ? NODE_TYPE_NET_PATTERN_LEFT_ANCHOR
: NODE_TYPE_IGNORE; : NODE_TYPE_IGNORE;
@ -1497,14 +1497,17 @@ export class AstFilterParser {
if ( type === NODE_TYPE_NET_PATTERN_LEFT_ANCHOR ) { if ( type === NODE_TYPE_NET_PATTERN_LEFT_ANCHOR ) {
this.addFlags(AST_FLAG_NET_PATTERN_LEFT_ANCHOR); this.addFlags(AST_FLAG_NET_PATTERN_LEFT_ANCHOR);
} }
prev = this.linkRight(prev, next);
patternBeg += 1; patternBeg += 1;
pattern = pattern.slice(1); pattern = pattern.slice(1);
} }
prev = this.linkRight(prev, next);
if ( patternBeg === patternEnd ) {
this.addNodeFlags(next, NODE_FLAG_IGNORE);
}
} }
// Right anchor // Right anchor
if ( clast === 0x7C /* | */ && pattern.length >= 2 ) { if ( exCharCodeAt(pattern, -1) === 0x7C /* | */ ) {
const type = exCharCodeAt(pattern, -2) !== 0x2A /* * */ const type = exCharCodeAt(pattern, -2) !== 0x2A /* * */
? NODE_TYPE_NET_PATTERN_RIGHT_ANCHOR ? NODE_TYPE_NET_PATTERN_RIGHT_ANCHOR
: NODE_TYPE_IGNORE; : NODE_TYPE_IGNORE;
@ -1514,6 +1517,9 @@ export class AstFilterParser {
} }
patternEnd -= 1; patternEnd -= 1;
pattern = pattern.slice(0, -1); pattern = pattern.slice(0, -1);
if ( patternEnd === patternBeg ) {
this.addNodeFlags(tail, NODE_FLAG_IGNORE);
}
} }
// Ignore pointless leading wildcards // Ignore pointless leading wildcards

View File

@ -3817,6 +3817,8 @@ class FilterCompiler {
parsedBlock.modifyType = undefined; parsedBlock.modifyType = undefined;
parsedBlock.optionUnitBits &= ~this.REDIRECT_BIT; parsedBlock.optionUnitBits &= ~this.REDIRECT_BIT;
parsedBlock.compileToFilter(writer); parsedBlock.compileToFilter(writer);
// Do not generate block rule when compiling to DNR ruleset
if ( parser.options.toDNR ) { return true; }
} }
this.compileToFilter(writer); this.compileToFilter(writer);