@@ -12,7 +12,7 @@ lazy_static! {
12
12
static ref PUBLIC_SUFFIXES : psl:: List = psl:: List :: new( ) ;
13
13
}
14
14
15
- /// Contains cosmetic filter information intended to be injected into a particular hostname .
15
+ /// Contains cosmetic filter information intended to be used on a particular URL .
16
16
///
17
17
/// `hide_selectors` is a set of any CSS selector on the page that should be hidden, i.e. styled as
18
18
/// `{ display: none !important; }`.
@@ -25,21 +25,27 @@ lazy_static! {
25
25
///
26
26
/// `injected_script` is the Javascript code for any scriptlets that should be injected into the
27
27
/// page.
28
+ ///
29
+ /// `generichide` is set to true if there is a corresponding `$generichide` exception network
30
+ /// filter. If so, the page should not query for additional generic rules using
31
+ /// `hidden_class_id_selectors`.
28
32
#[ derive( Debug , PartialEq , Eq , Deserialize , Serialize ) ]
29
- pub struct HostnameSpecificResources {
33
+ pub struct UrlSpecificResources {
30
34
pub hide_selectors : HashSet < String > ,
31
35
pub style_selectors : HashMap < String , Vec < String > > ,
32
36
pub exceptions : HashSet < String > ,
33
37
pub injected_script : String ,
38
+ pub generichide : bool ,
34
39
}
35
40
36
- impl HostnameSpecificResources {
41
+ impl UrlSpecificResources {
37
42
pub fn empty ( ) -> Self {
38
43
Self {
39
44
hide_selectors : HashSet :: new ( ) ,
40
45
style_selectors : HashMap :: new ( ) ,
41
46
exceptions : HashSet :: new ( ) ,
42
47
injected_script : String :: new ( ) ,
48
+ generichide : false ,
43
49
}
44
50
}
45
51
}
@@ -197,10 +203,10 @@ impl CosmeticFilterCache {
197
203
. collect :: < Vec < _ > > ( )
198
204
}
199
205
200
- pub fn hostname_cosmetic_resources ( & self , hostname : & str ) -> HostnameSpecificResources {
206
+ pub fn hostname_cosmetic_resources ( & self , hostname : & str , generichide : bool ) -> UrlSpecificResources {
201
207
let domain = match PUBLIC_SUFFIXES . domain ( hostname) {
202
208
Some ( domain) => domain,
203
- None => return HostnameSpecificResources :: empty ( ) ,
209
+ None => return UrlSpecificResources :: empty ( ) ,
204
210
} ;
205
211
let domain_str = domain. to_str ( ) ;
206
212
@@ -225,8 +231,13 @@ impl CosmeticFilterCache {
225
231
226
232
let ( hostname_hide_selectors, style_selectors, script_injections) = hostname_specific_rules ( & rules_that_apply[ ..] ) ;
227
233
228
- let mut hide_selectors = self . misc_generic_selectors . difference ( & exceptions. hide_exceptions ) . cloned ( ) . collect :: < HashSet < _ > > ( ) ;
229
- hostname_hide_selectors. into_iter ( ) . for_each ( |sel| { hide_selectors. insert ( sel) ; } ) ;
234
+ let hide_selectors = if generichide {
235
+ hostname_hide_selectors
236
+ } else {
237
+ let mut hide_selectors = self . misc_generic_selectors . difference ( & exceptions. hide_exceptions ) . cloned ( ) . collect :: < HashSet < _ > > ( ) ;
238
+ hostname_hide_selectors. into_iter ( ) . for_each ( |sel| { hide_selectors. insert ( sel) ; } ) ;
239
+ hide_selectors
240
+ } ;
230
241
231
242
let mut injected_script = String :: new ( ) ;
232
243
script_injections. iter ( ) . for_each ( |s| {
@@ -236,11 +247,12 @@ impl CosmeticFilterCache {
236
247
}
237
248
} ) ;
238
249
239
- HostnameSpecificResources {
250
+ UrlSpecificResources {
240
251
hide_selectors,
241
252
style_selectors,
242
253
exceptions : exceptions. hide_exceptions ,
243
254
injected_script,
255
+ generichide,
244
256
}
245
257
}
246
258
@@ -446,15 +458,15 @@ mod cosmetic_cache_tests {
446
458
"sub.example.com#@#.item2" ,
447
459
] ) ;
448
460
449
- let out = cfcache. hostname_cosmetic_resources ( "test.com" ) ;
450
- let mut expected = HostnameSpecificResources :: empty ( ) ;
461
+ let out = cfcache. hostname_cosmetic_resources ( "test.com" , false ) ;
462
+ let mut expected = UrlSpecificResources :: empty ( ) ;
451
463
assert_eq ! ( out, expected) ;
452
464
453
- let out = cfcache. hostname_cosmetic_resources ( "example.com" ) ;
465
+ let out = cfcache. hostname_cosmetic_resources ( "example.com" , false ) ;
454
466
expected. exceptions . insert ( ".item" . into ( ) ) ;
455
467
assert_eq ! ( out, expected) ;
456
468
457
- let out = cfcache. hostname_cosmetic_resources ( "sub.example.com" ) ;
469
+ let out = cfcache. hostname_cosmetic_resources ( "sub.example.com" , false ) ;
458
470
expected. exceptions . insert ( ".item2" . into ( ) ) ;
459
471
assert_eq ! ( out, expected) ;
460
472
}
@@ -465,16 +477,16 @@ mod cosmetic_cache_tests {
465
477
"example.com,~sub.example.com##.item" ,
466
478
] ) ;
467
479
468
- let out = cfcache. hostname_cosmetic_resources ( "test.com" ) ;
469
- let mut expected = HostnameSpecificResources :: empty ( ) ;
480
+ let out = cfcache. hostname_cosmetic_resources ( "test.com" , false ) ;
481
+ let mut expected = UrlSpecificResources :: empty ( ) ;
470
482
assert_eq ! ( out, expected) ;
471
483
472
- let out = cfcache. hostname_cosmetic_resources ( "example.com" ) ;
484
+ let out = cfcache. hostname_cosmetic_resources ( "example.com" , false ) ;
473
485
expected. hide_selectors . insert ( ".item" . to_owned ( ) ) ;
474
486
assert_eq ! ( out, expected) ;
475
487
476
- let out = cfcache. hostname_cosmetic_resources ( "sub.example.com" ) ;
477
- let mut expected = HostnameSpecificResources :: empty ( ) ;
488
+ let out = cfcache. hostname_cosmetic_resources ( "sub.example.com" , false ) ;
489
+ let mut expected = UrlSpecificResources :: empty ( ) ;
478
490
expected. exceptions . insert ( ".item" . into ( ) ) ;
479
491
assert_eq ! ( out, expected) ;
480
492
}
@@ -488,23 +500,23 @@ mod cosmetic_cache_tests {
488
500
"a2.sub.example.com##.element:style(background: #000)" ,
489
501
] ) ;
490
502
491
- let out = cfcache. hostname_cosmetic_resources ( "sub.example.com" ) ;
492
- let mut expected = HostnameSpecificResources :: empty ( ) ;
503
+ let out = cfcache. hostname_cosmetic_resources ( "sub.example.com" , false ) ;
504
+ let mut expected = UrlSpecificResources :: empty ( ) ;
493
505
assert_eq ! ( out, expected) ;
494
506
495
- let out = cfcache. hostname_cosmetic_resources ( "sub.test.example.com" ) ;
507
+ let out = cfcache. hostname_cosmetic_resources ( "sub.test.example.com" , false ) ;
496
508
assert_eq ! ( out, expected) ;
497
509
498
- let out = cfcache. hostname_cosmetic_resources ( "a1.sub.example.com" ) ;
510
+ let out = cfcache. hostname_cosmetic_resources ( "a1.sub.example.com" , false ) ;
499
511
expected. hide_selectors . insert ( ".element" . to_owned ( ) ) ;
500
512
assert_eq ! ( out, expected) ;
501
513
502
- let out = cfcache. hostname_cosmetic_resources ( "test.example.com" ) ;
514
+ let out = cfcache. hostname_cosmetic_resources ( "test.example.com" , false ) ;
503
515
expected. hide_selectors . clear ( ) ;
504
516
expected. style_selectors . insert ( ".element" . to_owned ( ) , vec ! [ "background: #fff" . to_owned( ) ] ) ;
505
517
assert_eq ! ( out, expected) ;
506
518
507
- let out = cfcache. hostname_cosmetic_resources ( "a2.sub.example.com" ) ;
519
+ let out = cfcache. hostname_cosmetic_resources ( "a2.sub.example.com" , false ) ;
508
520
expected. style_selectors . clear ( ) ;
509
521
expected. style_selectors . insert ( ".element" . to_owned ( ) , vec ! [ "background: #000" . to_owned( ) ] ) ;
510
522
assert_eq ! ( out, expected) ;
@@ -547,26 +559,26 @@ mod cosmetic_cache_tests {
547
559
} ,
548
560
] ) ;
549
561
550
- let out = cfcache. hostname_cosmetic_resources ( "sub.example.com" ) ;
551
- let mut expected = HostnameSpecificResources :: empty ( ) ;
562
+ let out = cfcache. hostname_cosmetic_resources ( "sub.example.com" , false ) ;
563
+ let mut expected = UrlSpecificResources :: empty ( ) ;
552
564
assert_eq ! ( out, expected) ;
553
565
554
- let out = cfcache. hostname_cosmetic_resources ( "sub.test.example.com" ) ;
566
+ let out = cfcache. hostname_cosmetic_resources ( "sub.test.example.com" , false ) ;
555
567
assert_eq ! ( out, expected) ;
556
568
557
- let out = cfcache. hostname_cosmetic_resources ( "test.example.com" ) ;
569
+ let out = cfcache. hostname_cosmetic_resources ( "test.example.com" , false ) ;
558
570
expected. injected_script = "set-constant.js, atob, trueFunc\n " . to_owned ( ) ;
559
571
assert_eq ! ( out, expected) ;
560
572
561
- let out = cfcache. hostname_cosmetic_resources ( "cosmetic.net" ) ;
573
+ let out = cfcache. hostname_cosmetic_resources ( "cosmetic.net" , false ) ;
562
574
expected. injected_script = "nowebrtc.js\n " . to_owned ( ) ;
563
575
assert_eq ! ( out, expected) ;
564
576
565
- let out = cfcache. hostname_cosmetic_resources ( "g.cosmetic.net" ) ;
577
+ let out = cfcache. hostname_cosmetic_resources ( "g.cosmetic.net" , false ) ;
566
578
expected. injected_script = "nowebrtc.js\n window.open-defuser.js\n " . to_owned ( ) ;
567
579
assert_eq ! ( out, expected) ;
568
580
569
- let out = cfcache. hostname_cosmetic_resources ( "c.g.cosmetic.net" ) ;
581
+ let out = cfcache. hostname_cosmetic_resources ( "c.g.cosmetic.net" , false ) ;
570
582
expected. injected_script = "window.open-defuser.js\n " . to_owned ( ) ;
571
583
assert_eq ! ( out, expected) ;
572
584
}
@@ -619,7 +631,7 @@ mod cosmetic_cache_tests {
619
631
"~test.com###test-element" ,
620
632
] ;
621
633
let cfcache = CosmeticFilterCache :: new ( rules. iter ( ) . map ( |r| CosmeticFilter :: parse ( r, false ) . unwrap ( ) ) . collect :: < Vec < _ > > ( ) ) ;
622
- let exceptions = cfcache. hostname_cosmetic_resources ( "example.co.uk" ) . exceptions ;
634
+ let exceptions = cfcache. hostname_cosmetic_resources ( "example.co.uk" , false ) . exceptions ;
623
635
624
636
let out = cfcache. hidden_class_id_selectors ( & [ "a-class" . into ( ) ] , & [ ] , & exceptions) ;
625
637
assert_eq ! ( out, [ ".a-class .with .children" ] ) ;
@@ -630,7 +642,7 @@ mod cosmetic_cache_tests {
630
642
let out = cfcache. hidden_class_id_selectors ( & [ ] , & [ "test-element" . into ( ) ] , & exceptions) ;
631
643
assert_eq ! ( out, [ "#test-element" ] ) ;
632
644
633
- let exceptions = cfcache. hostname_cosmetic_resources ( "a1.test.com" ) . exceptions ;
645
+ let exceptions = cfcache. hostname_cosmetic_resources ( "a1.test.com" , false ) . exceptions ;
634
646
635
647
let out = cfcache. hidden_class_id_selectors ( & [ "a-class" . into ( ) ] , & [ ] , & exceptions) ;
636
648
assert_eq ! ( out, [ ".a-class" , ".a-class .with .children" ] ) ;
@@ -653,14 +665,14 @@ mod cosmetic_cache_tests {
653
665
] ;
654
666
let cfcache = CosmeticFilterCache :: new ( rules. iter ( ) . map ( |r| CosmeticFilter :: parse ( r, false ) . unwrap ( ) ) . collect :: < Vec < _ > > ( ) ) ;
655
667
656
- let hide_selectors = cfcache. hostname_cosmetic_resources ( "test.com" ) . hide_selectors ;
668
+ let hide_selectors = cfcache. hostname_cosmetic_resources ( "test.com" , false ) . hide_selectors ;
657
669
let mut expected_hides = HashSet :: new ( ) ;
658
670
expected_hides. insert ( "a[href=\" bad.com\" ]" . to_owned ( ) ) ;
659
671
expected_hides. insert ( "div > p" . to_owned ( ) ) ;
660
672
expected_hides. insert ( "a[href=\" notbad.com\" ]" . to_owned ( ) ) ;
661
673
assert_eq ! ( hide_selectors, expected_hides) ;
662
674
663
- let hide_selectors = cfcache. hostname_cosmetic_resources ( "example.com" ) . hide_selectors ;
675
+ let hide_selectors = cfcache. hostname_cosmetic_resources ( "example.com" , false ) . hide_selectors ;
664
676
let mut expected_hides = HashSet :: new ( ) ;
665
677
expected_hides. insert ( "a[href=\" bad.com\" ]" . to_owned ( ) ) ;
666
678
assert_eq ! ( hide_selectors, expected_hides) ;
0 commit comments