@@ -560,7 +560,9 @@ impl TryFrom<CosmeticFilter> for CbRule {
560
560
type Error = CbRuleCreationFailure ;
561
561
562
562
fn try_from ( v : CosmeticFilter ) -> Result < Self , Self :: Error > {
563
- use crate :: filters:: cosmetic:: { CosmeticFilterLocationType , CosmeticFilterMask } ;
563
+ use crate :: filters:: cosmetic:: {
564
+ CosmeticFilterLocationType as LocationType , CosmeticFilterMask ,
565
+ } ;
564
566
565
567
if v. action . is_some ( ) {
566
568
return Err ( CbRuleCreationFailure :: CosmeticActionRulesNotSupported ) ;
@@ -573,28 +575,29 @@ impl TryFrom<CosmeticFilter> for CbRule {
573
575
let mut hostnames_vec = vec ! [ ] ;
574
576
let mut not_hostnames_vec = vec ! [ ] ;
575
577
576
- let mut any_entities = false ;
578
+ let mut any_unsupported = false ;
577
579
578
580
// Unwrap is okay here - cosmetic rules must have a '#' character
579
581
let sharp_index = find_char ( b'#' , raw_line. as_bytes ( ) ) . unwrap ( ) ;
580
582
CosmeticFilter :: locations_before_sharp ( & raw_line, sharp_index) . for_each (
581
583
|( location_type, location) | match location_type {
582
- CosmeticFilterLocationType :: Entity => any_entities = true ,
583
- CosmeticFilterLocationType :: NotEntity => any_entities = true ,
584
- CosmeticFilterLocationType :: Hostname => {
584
+ LocationType :: Entity | LocationType :: NotEntity | LocationType :: Unsupported => {
585
+ any_unsupported = true
586
+ }
587
+ LocationType :: Hostname => {
585
588
if let Ok ( encoded) = idna:: domain_to_ascii ( location) {
586
589
hostnames_vec. push ( encoded) ;
587
590
}
588
591
}
589
- CosmeticFilterLocationType :: NotHostname => {
592
+ LocationType :: NotHostname => {
590
593
if let Ok ( encoded) = idna:: domain_to_ascii ( location) {
591
594
not_hostnames_vec. push ( encoded) ;
592
595
}
593
596
}
594
597
} ,
595
598
) ;
596
599
597
- if any_entities {
600
+ if any_unsupported && hostnames_vec . is_empty ( ) && not_hostnames_vec . is_empty ( ) {
598
601
return Err ( CbRuleCreationFailure :: CosmeticEntitiesUnsupported ) ;
599
602
}
600
603
@@ -1403,4 +1406,25 @@ mod filterset_tests {
1403
1406
1404
1407
Ok ( ( ) )
1405
1408
}
1409
+
1410
+ #[ test]
1411
+ fn convert_cosmetic_filter_locations ( ) -> Result < ( ) , ( ) > {
1412
+ let list = [
1413
+ r"/^dizipal\d+\.com$/##.web" ,
1414
+ r"/^example\d+\.com$/,test.net,b.*##.ad" ,
1415
+ ] ;
1416
+ let mut set = FilterSet :: new ( true ) ;
1417
+ set. add_filters ( & list, Default :: default ( ) ) ;
1418
+
1419
+ let ( cb_rules, used_rules) = set. into_content_blocking ( ) ?;
1420
+ assert_eq ! ( used_rules. len( ) , 1 ) ;
1421
+ assert_eq ! ( cb_rules. len( ) , 1 ) ;
1422
+ assert ! ( cb_rules[ 0 ] . trigger. if_domain. is_some( ) ) ;
1423
+ assert_eq ! (
1424
+ cb_rules[ 0 ] . trigger. if_domain. as_ref( ) . unwrap( ) ,
1425
+ & [ "test.net" ]
1426
+ ) ;
1427
+
1428
+ Ok ( ( ) )
1429
+ }
1406
1430
}
0 commit comments