Sun, 14 Apr 2019 15:09:21 +0200
Major restructuring of the source tree to get prepared for a setup.py based installation.
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

1  # * coding: utf8 * 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

2  
6645
ad476851d7e0
Updated copyright for 2019.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
6048
diff
changeset

3  # Copyright (c) 2009  2019 Detlev Offenbach <detlev@dieoffenbachs.de> 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

4  # 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

5  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

6  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

7  Module implementing the AdBlock rule class. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

8  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

9  
3145
a9de05d4a22f
# __IGNORE_WARNING__ added/ removed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3060
diff
changeset

10  from __future__ import unicode_literals 
2525
8b507a9a2d40
Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
2302
diff
changeset

11  
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

12  import re 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

13  
5736
000ea446ff4b
Prepared the code for Qt > 5.9.99.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5587
diff
changeset

14  from PyQt5.QtCore import Qt, QRegExp, QUrl 
3656
441956d8fce5
Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
3178
diff
changeset

15  from PyQt5.QtNetwork import QNetworkRequest 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

16  
5736
000ea446ff4b
Prepared the code for Qt > 5.9.99.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5587
diff
changeset

17  from Globals import qVersionTuple 
000ea446ff4b
Prepared the code for Qt > 5.9.99.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5587
diff
changeset

18  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

19  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

20  # Qt version < 4.8 has an issue; it will wrongly 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

21  # count .co.uk (and others) as secondlevel domains 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

22  def toSecondLevelDomain(url): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

23  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

24  Module function to get a second level domain from the given URL. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

25  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

26  @param url URL to extract domain from (QUrl) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

27  @return name of second level domain (string) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

28  """ 
5736
000ea446ff4b
Prepared the code for Qt > 5.9.99.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5587
diff
changeset

29  if qVersionTuple() >= (4, 8, 0): 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

30  topLevelDomain = url.topLevelDomain() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

31  urlHost = url.host() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

32  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

33  if not topLevelDomain or not urlHost: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

34  return "" 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

35  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

36  domain = urlHost[:len(urlHost)  len(topLevelDomain)] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

37  if domain.count(".") == 0: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

38  return urlHost 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

39  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

40  while domain.count(".") != 0: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

41  domain = domain[domain.find(".") + 1:] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

42  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

43  return domain + topLevelDomain 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

44  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

45  domain = url.host() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

46  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

47  if domain.count(".") == 0: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

48  return "" 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

49  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

50  while domain.count(".") != 1: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

51  domain = domain[domain.find(".") + 1:] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

52  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

53  return domain 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

54  
945
8cd4d08fa9f6
Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
791
diff
changeset

55  
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

56  class AdBlockRule(object): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

57  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

58  Class implementing the AdBlock rule. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

59  """ 
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5389
diff
changeset

60  def __init__(self, filterRule="", subscription=None): 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

61  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

62  Constructor 
2953
703452a2876f
Started correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2302
diff
changeset

63  
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5389
diff
changeset

64  @param filterRule filter string of the rule (string) 
2953
703452a2876f
Started correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2302
diff
changeset

65  @param subscription reference to the subscription object 
703452a2876f
Started correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2302
diff
changeset

66  (AdBlockSubscription) 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

67  """ 
1963
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

68  self.__subscription = subscription 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

69  
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

70  self.__regExp = QRegExp() 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

71  self.__options = [] 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

72  self.__blockedDomains = [] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

73  self.__allowedDomains = [] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

74  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

75  self.__enabled = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

76  self.__cssRule = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

77  self.__exception = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

78  self.__internalDisabled = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

79  self.__domainRestricted = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

80  self.__useRegExp = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

81  self.__useDomainMatch = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

82  self.__useEndsMatch = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

83  self.__thirdParty = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

84  self.__thirdPartyException = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

85  self.__object = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

86  self.__objectException = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

87  self.__subdocument = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

88  self.__subdocumentException = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

89  self.__xmlhttprequest = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

90  self.__xmlhttprequestException = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

91  self.__document = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

92  self.__elemhide = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

93  self.__caseSensitivity = Qt.CaseInsensitive 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

94  
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5389
diff
changeset

95  self.setFilter(filterRule) 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

96  
1963
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

97  def subscription(self): 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

98  """ 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

99  Public method to get the subscription this rule belongs to. 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

100  
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

101  @return subscription of the rule (AdBlockSubscription) 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

102  """ 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

103  return self.__subscription 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

104  
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

105  def filter(self): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

106  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

107  Public method to get the rule filter string. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

108  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

109  @return rule filter string (string) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

110  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

111  return self.__filter 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

112  
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5389
diff
changeset

113  def setFilter(self, filterRule): 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

114  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

115  Public method to set the rule filter string. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

116  
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5389
diff
changeset

117  @param filterRule rule filter string (string) 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

118  """ 
5587
ea526b78ee6c
Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
5389
diff
changeset

119  self.__filter = filterRule 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

120  self.__parseFilter() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

121  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

122  def __parseFilter(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

123  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

124  Private method to parse the filter pattern. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

125  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

126  parsedLine = self.__filter 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

127  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

128  # empty rule or just a comment 
1994
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

129  if not parsedLine.strip() or parsedLine.startswith(("!", "[Adblock")): 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

130  self.__enabled = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

131  return 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

132  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

133  # CSS element hiding rule 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

134  if "##" in parsedLine: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

135  self.__cssRule = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

136  pos = parsedLine.find("##") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

137  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

138  # domain restricted rule 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

139  if not parsedLine.startswith("##"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

140  domains = parsedLine[:pos] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

141  self.__parseDomains(domains, ",") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

142  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

143  self.__cssSelector = parsedLine[pos + 2:] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

144  # CSS rule cannot have more options > stop parsing 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

145  return 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

146  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

147  # Exception always starts with @@ 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

148  if parsedLine.startswith("@@"): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

149  self.__exception = True 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

150  parsedLine = parsedLine[2:] 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

151  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

152  # Parse all options following '$' character 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

153  optionsIndex = parsedLine.find("$") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

154  if optionsIndex >= 0: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

155  options = parsedLine[optionsIndex + 1:].split(",") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

156  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

157  handledOptions = 0 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

158  for option in options: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

159  if option.startswith("domain="): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

160  self.__parseDomains(option[7:], "") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

161  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

162  elif option == "matchcase": 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

163  self.__caseSensitivity = Qt.CaseSensitive 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

164  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

165  elif option.endswith("thirdparty"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

166  self.__thirdParty = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

167  self.__thirdPartyException = option.startswith("~") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

168  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

169  elif option.endswith("object"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

170  self.__object = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

171  self.__objectException = option.startswith("~") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

172  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

173  elif option.endswith("subdocument"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

174  self.__subdocument = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

175  self.__subdocumentException = option.startswith("~") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

176  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

177  elif option.endswith("xmlhttprequest"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

178  self.__xmlhttprequest = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

179  self.__xmlhttprequestException = option.startswith("~") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

180  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

181  elif option == "document" and self.__exception: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

182  self.__document = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

183  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

184  elif option == "elemhide" and self.__exception: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

185  self.__elemhide = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

186  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

187  elif option == "collapse": 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

188  # Hiding placeholders of blocked elements 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

189  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

190  
3000
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

191  # If we don't handle all options, it's safer to just disable 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

192  # this rule 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

193  if handledOptions != len(options): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

194  self.__internalDisabled = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

195  return 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

196  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

197  parsedLine = parsedLine[:optionsIndex] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

198  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

199  # Rule is classic regexp 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

200  if parsedLine.startswith("/") and parsedLine.endswith("/"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

201  parsedLine = parsedLine[1:1] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

202  self.__useRegExp = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

203  self.__regExp = QRegExp(parsedLine, self.__caseSensitivity, 
3035
36e9f388958b
Fixed a bunch of indentation issues.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
3022
diff
changeset

204  QRegExp.RegExp) 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

205  return 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

206  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

207  # Remove starting / ending wildcards 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

208  if parsedLine.startswith("*"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

209  parsedLine = parsedLine[1:] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

210  if parsedLine.endswith("*"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

211  parsedLine = parsedLine[:1] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

212  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

213  # Fast string matching for domain can be used 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

214  if parsedLine.startswith("") and \ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

215  parsedLine.endswith("^") and \ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

216  QRegExp("[/:?=&\\*]").indexIn(parsedLine) == 1: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

217  parsedLine = parsedLine[2:1] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

218  self.__useDomainMatch = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

219  self.__matchString = parsedLine 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

220  return 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

221  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

222  # If rule contains '' only at the end, string matching can be used 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

223  if parsedLine.endswith("") and \ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

224  QRegExp("[\\^\\*]").indexIn(parsedLine) == 1 and \ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

225  parsedLine.count("") == 1: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

226  parsedLine = parsedLine[:1] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

227  self.__useEndsMatch = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

228  self.__matchString = parsedLine 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

229  return 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

230  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

231  # If there is still a wildcard (*) or separator (^) or (), 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

232  # the rule must be modified to comply with QRegExp. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

233  if "*" in parsedLine or "^" in parsedLine or "" in parsedLine: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

234  pattern = self.__convertPatternToRegExp(parsedLine) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

235  self.__useRegExp = True 
3000
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

236  self.__regExp = QRegExp(pattern, self.__caseSensitivity, 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

237  QRegExp.RegExp) 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

238  return 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

239  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

240  # no regexp required 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

241  self.__useRegExp = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

242  self.__matchString = parsedLine 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

243  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

244  def __parseDomains(self, domains, separator): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

245  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

246  Private method to parse a string with a domain list. 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

247  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

248  @param domains list of domains (string) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

249  @param separator separator character used by the list (string) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

250  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

251  domainsList = domains.split(separator) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

252  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

253  for domain in domainsList: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

254  if not domain: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

255  continue 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

256  if domain.startswith("~"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

257  self.__blockedDomains.append(domain[1:]) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

258  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

259  self.__allowedDomains.append(domain) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

260  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

261  self.__domainRestricted = \ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

262  bool(self.__blockedDomains) or bool(self.__allowedDomains) 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

263  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

264  def networkMatch(self, request, domain, encodedUrl): 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

265  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

266  Public method to check the rule for a match. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

267  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

268  @param request reference to the network request (QNetworkRequest) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

269  @param domain domain name (string) 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

270  @param encodedUrl string encoded URL to be checked (string) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

271  @return flag indicating a match (boolean) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

272  """ 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

273  if self.__cssRule or not self.__enabled or self.__internalDisabled: 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

274  return False 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

275  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

276  matched = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

277  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

278  if self.__useRegExp: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

279  matched = self.__regExp.indexIn(encodedUrl) != 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

280  elif self.__useDomainMatch: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

281  matched = domain.endswith(self.__matchString) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

282  elif self.__useEndsMatch: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

283  if self.__caseSensitivity == Qt.CaseInsensitive: 
3000
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

284  matched = encodedUrl.lower().endswith( 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

285  self.__matchString.lower()) 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

286  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

287  matched = encodedUrl.endswith(self.__matchString) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

288  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

289  if self.__caseSensitivity == Qt.CaseInsensitive: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

290  matched = self.__matchString.lower() in encodedUrl.lower() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

291  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

292  matched = self.__matchString in encodedUrl 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

293  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

294  if matched: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

295  # check domain restrictions 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

296  if self.__domainRestricted and not self.matchDomain(domain): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

297  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

298  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

299  # check thirdparty restrictions 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

300  if self.__thirdParty and not self.matchThirdParty(request): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

301  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

302  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

303  # check object restrictions 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

304  if self.__object and not self.matchObject(request): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

305  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

306  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

307  # check subdocument restrictions 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

308  if self.__subdocument and not self.matchSubdocument(request): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

309  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

310  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

311  # check xmlhttprequest restriction 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

312  if self.__xmlhttprequest and not self.matchXmlHttpRequest(request): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

313  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

314  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

315  return matched 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

316  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

317  def urlMatch(self, url): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

318  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

319  Public method to check an URL against the rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

320  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

321  @param url URL to check (QUrl) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

322  @return flag indicating a match (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

323  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

324  if not self.__document and not self.__elemhide: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

325  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

326  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

327  encodedUrl = bytes(url.toEncoded()).decode() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

328  domain = url.host() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

329  return self.networkMatch(QNetworkRequest(url), domain, encodedUrl) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

330  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

331  def matchDomain(self, domain): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

332  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

333  Public method to match a domain. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

334  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

335  @param domain domain name to check (string) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

336  @return flag indicating a match (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

337  """ 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

338  if not self.__enabled: 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

339  return False 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

340  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

341  if not self.__domainRestricted: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

342  return True 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

343  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

344  if len(self.__blockedDomains) == 0: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

345  for dom in self.__allowedDomains: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

346  if domain.endswith(dom): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

347  return True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

348  elif len(self.__allowedDomains) == 0: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

349  for dom in self.__blockedDomains: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

350  if domain.endswith(dom): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

351  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

352  return True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

353  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

354  for dom in self.__blockedDomains: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

355  if domain.endswith(dom): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

356  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

357  for dom in self.__allowedDomains: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

358  if domain.endswith(dom): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

359  return True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

360  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

361  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

362  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

363  def matchThirdParty(self, req): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

364  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

365  Public slot to match a thirdparty rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

366  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

367  @param req request object to check (QNetworkRequest) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

368  @return flag indicating a match (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

369  """ 
5383
4bf49e0840f3
Fixed an issue in AdBlockRule.py caused by a wrong NetworkAccessManager setting.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
4631
diff
changeset

370  referer = \ 
4bf49e0840f3
Fixed an issue in AdBlockRule.py caused by a wrong NetworkAccessManager setting.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
4631
diff
changeset

371  bytes(req.attribute(QNetworkRequest.User + 200, b"")).decode() 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

372  if referer == "": 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

373  return False 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

374  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

375  # Thirdparty matching should be performed on secondlevel domains 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

376  refererHost = toSecondLevelDomain(QUrl(referer)) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

377  host = toSecondLevelDomain(req.url()) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

378  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

379  match = refererHost != host 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

380  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

381  if self.__thirdPartyException: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

382  return not match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

383  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

384  return match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

385  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

386  def matchObject(self, req): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

387  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

388  Public slot to match an object rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

389  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

390  @param req request object to check (QNetworkRequest) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

391  @return flag indicating a match (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

392  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

393  match = req.attribute(QNetworkRequest.User + 200) == "object" 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

394  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

395  if self.__objectException: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

396  return not match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

397  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

398  return match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

399  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

400  def matchSubdocument(self, req): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

401  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

402  Public slot to match a subdocument rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

403  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

404  @param req request object to check (QNetworkRequest) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

405  @return flag indicating a match (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

406  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

407  originatingFrame = req.originatingObject() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

408  if originatingFrame is None: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

409  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

410  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

411  page = originatingFrame.page() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

412  if page is None: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

413  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

414  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

415  match = originatingFrame != page.mainFrame() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

416  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

417  if self.__subdocumentException: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

418  return not match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

419  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

420  return match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

421  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

422  def matchXmlHttpRequest(self, req): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

423  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

424  Public slot to match a XmlHttpRequest rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

425  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

426  @param req request object to check (QNetworkRequest) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

427  @return flag indicating a match (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

428  """ 
4326
e52318f11812
Change code to make eric compatible to PyQt 5.5 (QByteArry issue).
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
4021
diff
changeset

429  match = req.rawHeader(b"XRequestWith") == "XMLHttpRequest" 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

430  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

431  if self.__xmlhttprequestException: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

432  return not match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

433  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

434  return match 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

435  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

436  def isException(self): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

437  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

438  Public method to check, if the rule defines an exception. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

439  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

440  @return flag indicating an exception (boolean) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

441  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

442  return self.__exception 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

443  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

444  def setException(self, exception): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

445  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

446  Public method to set the rule's exception flag. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

447  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

448  @param exception flag indicating an exception rule (boolean) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

449  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

450  self.__exception = exception 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

451  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

452  def isEnabled(self): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

453  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

454  Public method to check, if the rule is enabled. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

455  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

456  @return flag indicating enabled state (boolean) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

457  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

458  return self.__enabled 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

459  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

460  def setEnabled(self, enabled): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

461  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

462  Public method to set the rule's enabled state. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

463  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

464  @param enabled flag indicating the new enabled state (boolean) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

465  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

466  self.__enabled = enabled 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

467  if not enabled: 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

468  self.__filter = "!" + self.__filter 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

469  else: 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

470  self.__filter = self.__filter[1:] 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

471  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

472  def isCSSRule(self): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

473  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

474  Public method to check, if the rule is a CSS rule. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

475  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

476  @return flag indicating a CSS rule (boolean) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

477  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

478  return self.__cssRule 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

479  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

480  def cssSelector(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

481  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

482  Public method to get the CSS selector of the rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

483  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

484  @return CSS selector (string) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

485  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

486  return self.__cssSelector 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

487  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

488  def isDocument(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

489  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

490  Public method to check, if this is a document rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

491  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

492  @return flag indicating a document rule (boolean) 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

493  """ 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

494  return self.__document 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

495  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

496  def isElementHiding(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

497  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

498  Public method to check, if this is an element hiding rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

499  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

500  @return flag indicating an element hiding rule (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

501  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

502  return self.__elemhide 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

503  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

504  def isDomainRestricted(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

505  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

506  Public method to check, if this rule is restricted by domain. 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

507  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

508  @return flag indicating a domain restriction (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

509  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

510  return self.__domainRestricted 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

511  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

512  def isComment(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

513  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

514  Public method to check, if this is a comment. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

515  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

516  @return flag indicating a comment (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

517  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

518  return self.__filter.startswith("!") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

519  
1994
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

520  def isHeader(self): 
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

521  """ 
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

522  Public method to check, if this is a header. 
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

523  
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

524  @return flag indicating a header (boolean) 
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

525  """ 
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

526  return self.__filter.startswith("[Adblock") 
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

527  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

528  def isSlow(self): 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

529  """ 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

530  Public method to check, if this is a slow rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

531  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

532  @return flag indicating a slow rule (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

533  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

534  return self.__useRegExp 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

535  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

536  def isInternalDisabled(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

537  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

538  Public method to check, if this rule was disabled internally. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

539  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

540  @return flag indicating an internally disabled rule (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

541  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

542  return self.__internalDisabled 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

543  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

544  def __convertPatternToRegExp(self, wildcardPattern): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

545  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

546  Private method to convert a wildcard pattern to a regular expression. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

547  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

548  @param wildcardPattern string containing the wildcard pattern (string) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

549  @return string containing a regular expression (string) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

550  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

551  pattern = wildcardPattern 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

552  
3000
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

553  # remove multiple wildcards 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

554  pattern = re.sub(r"\*+", "*", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

555  # remove anchors following separator placeholder 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

556  pattern = re.sub(r"\^\$", "^", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

557  # remove leading wildcards 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

558  pattern = re.sub(r"^(\*)", "", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

559  # remove trailing wildcards 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

560  pattern = re.sub(r"(\*)$", "", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

561  # escape special symbols 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

562  pattern = re.sub(r"(\W)", r"\\\1", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

563  # process extended anchor at expression start 
3022
57179e4cdadd
Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
3000
diff
changeset

564  pattern = re.sub( 
57179e4cdadd
Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
3000
diff
changeset

565  r"^\\\\\\", 
3000
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

566  r"^[\w\]+:\/+(?!\/)(?:[^\/]+\.)?", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

567  # process separator placeholders 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

568  pattern = re.sub(r"\\\^", r"(?:[^\w\d\.%]$)", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

569  # process anchor at expression start 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

570  pattern = re.sub(r"^\\\", "^", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

571  # process anchor at expression end 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

572  pattern = re.sub(r"\\\$", "$", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

573  # replace wildcards by .* 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

574  pattern = re.sub(r"\\\*", ".*", pattern) 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

575  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

576  return pattern 