eric6/WebBrowser/AdBlock/AdBlockRule.py

changeset 8207
d359172d11be
parent 8143
2c730d5fd177
equal deleted inserted replaced
8206:adf11836cfce 8207:d359172d11be
74 # Exception only options 74 # Exception only options
75 DocumentOption = 8192 75 DocumentOption = 8192
76 ElementHideOption = 16384 76 ElementHideOption = 16384
77 77
78 78
79 class AdBlockRule(object): 79 class AdBlockRule:
80 """ 80 """
81 Class implementing the AdBlock rule. 81 Class implementing the AdBlock rule.
82 """ 82 """
83 def __init__(self, filterRule="", subscription=None): 83 def __init__(self, filterRule="", subscription=None):
84 """ 84 """
527 527
528 if not self.__hasOption(AdBlockRuleOption.DomainRestrictedOption): 528 if not self.__hasOption(AdBlockRuleOption.DomainRestrictedOption):
529 return True 529 return True
530 530
531 if len(self.__blockedDomains) == 0: 531 if len(self.__blockedDomains) == 0:
532 for dom in self.__allowedDomains: 532 return any(self.__isMatchingDomain(domain, dom)
533 if self.__isMatchingDomain(domain, dom): 533 for dom in self.__allowedDomains)
534 return True
535 elif len(self.__allowedDomains) == 0: 534 elif len(self.__allowedDomains) == 0:
536 for dom in self.__blockedDomains: 535 return all(not self.__isMatchingDomain(domain, dom)
537 if self.__isMatchingDomain(domain, dom): 536 for dom in self.__blockedDomains)
538 return False 537 else:
539 return True 538 return (
540 else: 539 all(not self.__isMatchingDomain(domain, dom)
541 for dom in self.__blockedDomains: 540 for dom in self.__blockedDomains) and
542 if self.__isMatchingDomain(domain, dom): 541 any(self.__isMatchingDomain(domain, dom)
543 return False 542 for dom in self.__allowedDomains)
544 for dom in self.__allowedDomains: 543 )
545 if self.__isMatchingDomain(domain, dom):
546 return True
547
548 return False
549 544
550 def matchThirdParty(self, req): 545 def matchThirdParty(self, req):
551 """ 546 """
552 Public method to match a third-party rule. 547 Public method to match a third-party rule.
553 548
985 @rtype bool 980 @rtype bool
986 """ 981 """
987 if not filterString.endswith("^") or not filterString.startswith("||"): 982 if not filterString.endswith("^") or not filterString.startswith("||"):
988 return False 983 return False
989 984
990 for filterChar in filterString: 985 return all(filterChar not in ["/", ":", "?", "=", "&", "*"]
991 if filterChar in ["/", ":", "?", "=", "&", "*"]: 986 for filterChar in filterString)
992 return False
993
994 return True
995 987
996 def __filterIsOnlyEndsMatch(self, filterString): 988 def __filterIsOnlyEndsMatch(self, filterString):
997 """ 989 """
998 Private method to check, if the given filter is to match against the 990 Private method to check, if the given filter is to match against the
999 end of a string. 991 end of a string.
1001 @param filterString filter string to be checked 993 @param filterString filter string to be checked
1002 @type str 994 @type str
1003 @return flag indicating a end of string match filter 995 @return flag indicating a end of string match filter
1004 @rtype bool 996 @rtype bool
1005 """ 997 """
1006 index = 0 998 for index, filterChar in enumerate(filterString):
1007 for filterChar in filterString: 999 # __IGNORE_WARNING_Y111__
1008 if filterChar in ["^", "*"]: 1000 if filterChar in ["^", "*"]:
1009 return False 1001 return False
1010 elif filterChar == "|": 1002 elif filterChar == "|":
1011 return bool(index == len(filterString) - 1) 1003 return index == len(filterString) - 1
1012 index += 1
1013 1004
1014 return False 1005 return False
1015 1006
1016 def __isMatchingDomain(self, domain, filterString): 1007 def __isMatchingDomain(self, domain, filterString):
1017 """ 1008 """
1044 @type str 1035 @type str
1045 @return flag indicating a match 1036 @return flag indicating a match
1046 @rtype bool 1037 @rtype bool
1047 """ 1038 """
1048 if self.__regExp is not None: 1039 if self.__regExp is not None:
1049 for matcher in self.__stringMatchers: 1040 return all(matcher in url for matcher in self.__stringMatchers)
1050 if matcher not in url:
1051 return False
1052 1041
1053 return True 1042 return True
1054 1043
1055 def __parseRegExpFilter(self, filterString): 1044 def __parseRegExpFilter(self, filterString):
1056 """ 1045 """

eric ide

mercurial