Skip to content

Commit 8507d63

Browse files
committed
Add support for negated hostnames in HTML filters
Related issue: - uBlockOrigin/uBlock-issues#6
1 parent 3658d70 commit 8507d63

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

‎src/js/html-filtering.js‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,14 @@ function applyCSSSelector(details, selector) {
299299
return modified;
300300
}
301301

302+
function logError(writer, msg) {
303+
logger.writeOne({
304+
realm: 'message',
305+
type: 'error',
306+
text: msg.replace('{who}', writer.properties.get('name') || '?')
307+
});
308+
}
309+
302310
htmlFilteringEngine.reset = function() {
303311
filterDB.clear();
304312
pselectors.clear();
@@ -316,13 +324,7 @@ htmlFilteringEngine.compile = function(parser, writer) {
316324
const isException = parser.isException();
317325
const { raw, compiled } = parser.result;
318326
if ( compiled === undefined ) {
319-
const who = writer.properties.get('name') || '?';
320-
logger.writeOne({
321-
realm: 'message',
322-
type: 'error',
323-
text: `Invalid HTML filter in ${who}: ##${raw}`
324-
});
325-
return;
327+
return logError(writer, `Invalid HTML filter in {who}: ##${raw}`);
326328
}
327329

328330
writer.select('HTML_FILTERS');
@@ -335,20 +337,28 @@ htmlFilteringEngine.compile = function(parser, writer) {
335337
return;
336338
}
337339

338-
// TODO: Mind negated hostnames, they are currently discarded.
339-
340+
const compiledFilters = [];
341+
let hasOnlyNegated = true;
340342
for ( const { hn, not, bad } of parser.getExtFilterDomainIterator() ) {
341343
if ( bad ) { continue; }
342-
let kind = 0;
343-
if ( isException ) {
344-
if ( not ) { continue; }
345-
kind |= 0b01;
344+
let kind = isException ? 0b01 : 0b00;
345+
if ( not ) {
346+
kind ^= 0b01;
347+
} else {
348+
hasOnlyNegated = false;
346349
}
347350
if ( compiled.charCodeAt(0) === 0x7B /* '{' */ ) {
348351
kind |= 0b10;
349352
}
350-
writer.push([ 64, hn, kind, compiled ]);
353+
compiledFilters.push([ 64, hn, kind, compiled ]);
354+
}
355+
356+
// Not allowed since it's equivalent to forbidden generic HTML filters
357+
if ( isException === false && hasOnlyNegated ) {
358+
return logError(writer, `Invalid HTML filter in {who}: ##${raw}`);
351359
}
360+
361+
writer.pushMany(compiledFilters);
352362
};
353363

354364
htmlFilteringEngine.fromCompiledContent = function(reader) {

‎src/js/static-filtering-io.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class CompiledListWriter {
4444
push(args) {
4545
this.block.push(serialize(args));
4646
}
47+
pushMany(many) {
48+
for ( const args of many ) {
49+
this.block.push(serialize(args));
50+
}
51+
}
4752
last() {
4853
if ( Array.isArray(this.block) && this.block.length !== 0 ) {
4954
return this.block[this.block.length - 1];

0 commit comments

Comments
 (0)