@@ -19,9 +19,8 @@ def process_and_sort_domains(domains):
1919 """Sorts and adds domains to the list, returning successfully added domains."""
2020 added_domains = []
2121 previous_domain = None
22- output = []
2322 for domain in sorted (domains ):
24- if add_domain_to_list (domain , domain , previous_domain , None , output ):
23+ if add_domain_to_list (domain , domain , previous_domain , None , [] ):
2524 added_domains .append (domain )
2625 previous_domain = domain
2726 return added_domains
@@ -55,6 +54,36 @@ def find_entity_for_resource(resource, entities):
5554 return None
5655
5756def build_rule (resource , action_type , entities ):
57+ """
58+ Builds a content blocking rule for WebKit based on the given resource, action type, and associated entities.
59+
60+ Content blocking rules in WebKit follow a declarative format.
61+ Each rule consists of a `trigger` defining when the rule activates and an `action` specifying what
62+ happens when it is activated.
63+
64+ - `resource`: The URL to block (used to create a `url-filter`).
65+ - `action_type`: The action type, e.g., "block" or "block-cookies".
66+ - `entities`: A mapping of resources to their associated entities and properties.
67+
68+ The `url-filter` is derived from the `resource`, specifying the URL pattern to match.
69+ If an entity is found for the resource, its properties are used to populate `unless-domain`,
70+ which specifies domains exempted from this rule.
71+
72+ The `load-type` is set to `["third-party"]` to limit the rule to third-party resources.
73+ NOTE: We can support first-party later by including `["first-party"]`.
74+
75+ Example of a WebKit rule:
76+ {
77+ "trigger": {
78+ "url-filter": "evil-tracker\\ .js",
79+ "unless-domain": ["trusted.com"]
80+ },
81+ "action": {
82+ "type": "block"
83+ }
84+ }
85+ For more information, see: https://webkit.org/blog/3476/content-blockers-first-look/
86+ """
5887 url_filter = build_url_filter (resource )
5988 entity = entities .get (resource ) or find_entity_for_resource (resource , entities )
6089 unless_domains = [f"*{ domain } " for domain in entity ["properties" ]] if entity and isinstance (entity .get ("properties" ), list ) else []
0 commit comments