Helpviewer/AdBlock/AdBlockSubscription.py

changeset 1960
d8c45fe8a1b9
parent 1955
9e6da33c1c10
child 1963
9c5b3235abf9
--- a/Helpviewer/AdBlock/AdBlockSubscription.py	Thu Jul 26 18:38:15 2012 +0200
+++ b/Helpviewer/AdBlock/AdBlockSubscription.py	Sat Jul 28 11:23:12 2012 +0200
@@ -57,7 +57,6 @@
         
         self.__networkExceptionRules = []
         self.__networkBlockRules = []
-        self.__pageRules = []
         
         self.__parseUrl(url)
     
@@ -88,7 +87,7 @@
     
     def url(self):
         """
-        Public method to generate the url for this subscription.
+        Public method to generate the URL for this subscription.
         
         @return AdBlock URL for the subscription (QUrl)
         """
@@ -324,37 +323,76 @@
         for rule in self.__rules:
             textStream << rule.filter() << "\n"
     
-    def pageRules(self):
-        """
-        Public method to get the page rules of the subscription.
-        
-        @return list of rule objects (list of AdBlockRule)
+    def match(self, req, urlDomain, urlString):
         """
-        return self.__pageRules[:]
-    
-    def allow(self, urlString):
-        """
-        Public method to check, if the given URL is allowed.
+        Public method to check the subscription for a matching rule.
         
+        @param req reference to the network request (QNetworkRequest)
+        @param urlDomain domain of the URL (string)
+        @param urlString URL (string)
         @return reference to the rule object or None (AdBlockRule)
         """
         for rule in self.__networkExceptionRules:
-            if rule.networkMatch(urlString):
+            if rule.networkMatch(req, urlDomain, urlString):
+                return None
+        
+        for rule in self.__networkBlockRules:
+            if rule.networkMatch(req, urlDomain, urlString):
                 return rule
         
         return None
     
-    def block(self, urlString):
+    def adBlockDisabledForUrl(self, url):
+        """
+        Public method to check, if AdBlock is disabled for the given URL.
+        
+        @param url URL to check (QUrl)
+        @return flag indicating disabled state (boolean)
         """
-        Public method to check, if the given URL should be blocked.
+        for rule in self.__documentRules:
+            if rule.urlMatch(url):
+                return True
         
-        @return reference to the rule object or None (AdBlockRule)
+        return False
+    
+    def elemHideDisabledForUrl(self, url):
+        """
+        Public method to check, if element hiding is disabled for the given URL.
+        
+        @param url URL to check (QUrl)
+        @return flag indicating disabled state (boolean)
         """
-        for rule in self.__networkBlockRules:
-            if rule.networkMatch(urlString):
-                return rule
+        if self.adBlockDisabledForUrl(url):
+            return True
+        
+        for rule in self.__elemhideRules:
+            if rule.urlMatch(url):
+                return True
+        
+        return False
+    
+    def elementHidingRules(self):
+        """
+        Public method to get the element hiding rules.
         
-        return None
+        @return element hiding rules (string)
+        """
+        return self.__elementHidingRules
+    
+    def elementHidingRulesForDomain(self, domain):
+        """
+        Public method to get the element hiding rules for the given domain.
+        
+        @param domain domain name (string)
+        @return element hiding rules (string)
+        """
+        rules = ""
+        
+        for rule in self.__domainRestrictedCssRules:
+            if rule.matchDomain(domain):
+                rules += rule.cssSelector() + ","
+        
+        return rules
     
     def allRules(self):
         """
@@ -402,18 +440,26 @@
         """
         Private method to populate the various rule caches.
         """
+        self.__networkExceptionRules = []
         self.__networkBlockRules = []
-        self.__networkExceptionRules = []
-        self.__pageRules = []
-        if not self.isEnabled():
-            return
+        self.__domainRestrictedCssRules = []
+        self.__elementHidingRules = ""
+        self.__documentRules = []
+        self.__elemhideRules = []
         
         for rule in self.__rules:
             if not rule.isEnabled():
                 continue
             
             if rule.isCSSRule():
-                self.__pageRules.append(rule)
+                if rule.isDomainRestricted():
+                    self.__domainRestrictedCssRules.append(rule)
+                else:
+                    self.__elementHidingRules += rule.cssSelector() + ","
+            elif rule.isDocument():
+                self.__documentRules.append(rule)
+            elif rule.isElementHiding():
+                self.__elemhideRules.append(rule)
             elif rule.isException():
                 self.__networkExceptionRules.append(rule)
             else:

eric ide

mercurial