Skip to content

Commit 765c0fd

Browse files
committed
Pull request #54: AG-9346 add content blocker engine
Merge in ADGUARD-IOS/safari-converter from feature/AG-9346 to master Squashed commit of the following: commit d405ea4 Author: Maxim Topciu <[email protected]> Date: Wed Aug 11 18:49:34 2021 +0300 AG-9346 use more swift like error messages commit e63654a Author: Maxim Topciu <[email protected]> Date: Wed Aug 11 18:47:04 2021 +0300 AG-9346 use djb2 hash function commit c8ea5b7 Author: Maxim Topciu <[email protected]> Date: Wed Aug 11 14:03:50 2021 +0300 AG-9346 check url length commit 59cf3ac Author: Maxim Topciu <[email protected]> Date: Wed Aug 11 13:38:58 2021 +0300 AG-9346 fix review notes commit ca12b00 Author: Maxim Topciu <[email protected]> Date: Wed Aug 11 12:58:11 2021 +0300 AG-9346 uncomment commented line of code commit f33b77e Merge: 64e8aca 4466281 Author: Maxim Topciu <[email protected]> Date: Wed Aug 11 12:46:46 2021 +0300 Merge branch 'master' into feature/AG-9346 commit 64e8aca Author: Maxim Topciu <[email protected]> Date: Tue Aug 10 19:38:47 2021 +0300 AG-9346 fix regex shortcut commit 07eda5e Author: Maxim Topciu <[email protected]> Date: Tue Aug 10 17:34:33 2021 +0300 AG-9346 set regexp once commit 225f49f Author: Maxim Topciu <[email protected]> Date: Tue Aug 10 16:56:23 2021 +0300 AG-9346 make BlockerRules structure flatter commit 166f9e0 Author: Maxim Topciu <[email protected]> Date: Tue Aug 10 16:31:42 2021 +0300 AG-9346 update comments with timings commit 19ff60d Author: Maxim Topciu <[email protected]> Date: Tue Aug 10 16:16:44 2021 +0300 AG-9346 use blocker entry to decode and blocker rule to serialize commit 6a7e652 Author: Maxim Topciu <[email protected]> Date: Mon Aug 9 18:49:08 2021 +0300 AG-9346 fix condition commit e98dc9e Author: Maxim Topciu <[email protected]> Date: Tue Aug 3 16:53:01 2021 +0300 AG-9346 add a few more guards commit 2aa604c Author: Maxim Topciu <[email protected]> Date: Tue Aug 3 16:36:48 2021 +0300 AG-9346 remove optional try before regexp commit d4f29f0 Author: Maxim Topciu <[email protected]> Date: Tue Aug 3 16:24:51 2021 +0300 AG-9346 add tests to LinuxMain.swift commit e33ebd5 Author: Maxim Topciu <[email protected]> Date: Tue Aug 3 16:20:24 2021 +0300 AG-9346 use max to look for longest commit 92073ae Author: Maxim Topciu <[email protected]> Date: Tue Aug 3 15:39:42 2021 +0300 AG-9346 make ContentBlockerContainer final commit cc6b047 Author: Maxim Topciu <[email protected]> Date: Tue Aug 3 15:38:14 2021 +0300 AG-9346 forbid property setting commit 0d23254 Author: Maxim Topciu <[email protected]> Date: Tue Aug 3 15:27:21 2021 +0300 AG-9346 fix review notes, make code more swift like commit 552b36c Author: Maxim Topciu <[email protected]> Date: Tue Aug 3 14:45:11 2021 +0300 AG-9346 fix ContentBlockerContainer review notes ... and 10 more commits
1 parent 4466281 commit 765c0fd

21 files changed

+1646
-18
lines changed

.swiftlint.yml

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
excluded:
2+
- ${PWD}/Carthage
3+
- ${PWD}/Pods
4+
- ${PWD}/DerivedData
5+
6+
disabled_rules:
7+
- discarded_notification_center_observer
8+
- notification_center_detachment
9+
- orphaned_doc_comment
10+
- todo
11+
- unused_capture_list
12+
- identifier_name
13+
- cyclomatic_complexity
14+
15+
opt_in_rules:
16+
- array_init
17+
- attributes
18+
- closure_end_indentation
19+
- closure_spacing
20+
- collection_alignment
21+
- colon # promote to error
22+
- convenience_type
23+
- discouraged_object_literal
24+
- empty_collection_literal
25+
- empty_count
26+
- empty_string
27+
- enum_case_associated_values_count
28+
- fatal_error_message
29+
- first_where
30+
- force_unwrapping
31+
- implicitly_unwrapped_optional
32+
- indentation_width
33+
- last_where
34+
- legacy_random
35+
- literal_expression_end_indentation
36+
- multiline_arguments
37+
- multiline_function_chains
38+
- multiline_literal_brackets
39+
- multiline_parameters
40+
- multiline_parameters_brackets
41+
- operator_usage_whitespace
42+
- overridden_super_call
43+
- pattern_matching_keywords
44+
- prefer_self_type_over_type_of_self
45+
- redundant_nil_coalescing
46+
- redundant_type_annotation
47+
- strict_fileprivate
48+
- toggle_bool
49+
- trailing_closure
50+
- unneeded_parentheses_in_closure_argument
51+
- unused_import
52+
- vertical_whitespace_closing_braces
53+
- vertical_whitespace_opening_braces
54+
- yoda_condition
55+
56+
57+
custom_rules:
58+
array_constructor:
59+
name: "Array/Dictionary initializer"
60+
regex: '[let,var] .+ = (\[.+\]\(\))'
61+
capture_group: 1
62+
message: "Use explicit type annotation when initializing empty arrays and dictionaries"
63+
severity: warning
64+
65+
66+
attributes:
67+
always_on_same_line:
68+
- "@IBSegueAction"
69+
- "@IBAction"
70+
- "@NSManaged"
71+
- "@objc"
72+
73+
force_cast: warning
74+
force_try: warning
75+
function_body_length:
76+
warning: 60
77+
78+
legacy_hashing: error
79+
80+
identifier_name:
81+
excluded:
82+
- i
83+
- id
84+
- x
85+
- y
86+
- z
87+
88+
indentation_width:
89+
indentation_width: 4
90+
include_comments: false
91+
92+
line_length:
93+
ignores_urls: true
94+
ignores_function_declarations: true
95+
ignores_comments: true
96+
97+
multiline_arguments:
98+
first_argument_location: next_line
99+
only_enforce_after_first_closure_on_first_line: true
100+
101+
private_over_fileprivate:
102+
validate_extensions: true
103+
104+
trailing_whitespace:
105+
ignores_empty_lines: false
106+
ignores_comments: true
107+
108+
vertical_whitespace:
109+
max_empty_lines: 2

Package.swift

+12-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let package = Package(
1010
.library(
1111
name: "ContentBlockerConverter",
1212
type: .static,
13-
targets: ["ContentBlockerConverter"]),
13+
targets: ["ContentBlockerConverter", "ContentBlockerEngine"]),
1414
.executable(
1515
name: "ConverterTool",
1616
targets: ["CommandLineWrapper"])
@@ -25,12 +25,21 @@ let package = Package(
2525
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2626
.target(
2727
name: "CommandLineWrapper",
28-
dependencies: ["ContentBlockerConverter"]),
28+
dependencies: ["ContentBlockerConverter", "Shared"]),
2929
.target(
3030
name: "ContentBlockerConverter",
31-
dependencies: ["Punnycode"]),
31+
dependencies: ["Punnycode", "Shared"]),
32+
.target(
33+
name: "ContentBlockerEngine",
34+
dependencies: ["ContentBlockerConverter", "Shared"]),
35+
.target(
36+
name: "Shared"),
3237
.testTarget(
3338
name: "ContentBlockerConverterTests",
3439
dependencies: ["ContentBlockerConverter"]),
40+
.testTarget(
41+
name: "ContentBlockerEngineTests",
42+
dependencies: ["ContentBlockerEngine"]
43+
)
3544
]
3645
)

Sources/CommandLineWrapper/main.swift

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import ContentBlockerConverter
3+
import Shared
34

45
/**
56
* Command line wrapper

Sources/ContentBlockerConverter/Compiler/BlockerEntry.swift

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import Foundation
33
/**
44
* Blocker entry object description
55
*/
6-
struct BlockerEntry: Codable {
7-
var trigger: Trigger
8-
let action: Action
6+
public struct BlockerEntry: Codable {
7+
public var trigger: Trigger
8+
public let action: Action
99

10-
struct Trigger : Codable {
11-
var ifDomain: [String]?
12-
var urlFilter: String?
13-
var unlessDomain: [String]?
10+
public struct Trigger : Codable {
11+
public var ifDomain: [String]?
12+
public var urlFilter: String?
13+
public var unlessDomain: [String]?
1414

1515
var shortcut: String?
1616
var regex: NSRegularExpression?
@@ -46,12 +46,12 @@ struct BlockerEntry: Codable {
4646
}
4747
}
4848

49-
struct Action : Codable {
50-
var type: String
49+
public struct Action : Codable {
50+
public var type: String
5151
var selector: String?
52-
var css: String?
53-
var script: String?
54-
var scriptlet: String?
55-
var scriptletParam: String?
52+
public var css: String?
53+
public var script: String?
54+
public var scriptlet: String?
55+
public var scriptletParam: String?
5656
}
5757
}

Sources/ContentBlockerConverter/Compiler/BlockerEntryFactory.swift

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import Shared
23

34
/**
45
* Blocker entries factory class

Sources/ContentBlockerConverter/ContentBlockerConverter.swift

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import Shared
23

34
/**
45
* Entry point

Sources/ContentBlockerConverter/ConversionResult.swift

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import Foundation
2+
import Shared
3+
24
/**
35
* Conversion result wrapper class
46
*/

Sources/ContentBlockerConverter/Rules/RuleFactory.swift

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import Shared
23

34
/**
45
* Rule factory creates rules from source texts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Foundation
2+
3+
// Wrapper result class
4+
final class BlockerData: Codable {
5+
var scripts: [String] = []
6+
var cssExtended: [String] = []
7+
var cssInject: [String] = []
8+
var scriptlets: [String] = []
9+
10+
func addScript(script: String?) {
11+
guard let script = script, !script.isEmpty else {
12+
return
13+
}
14+
scripts.append(script)
15+
}
16+
17+
func addCssExtended(style: String?) {
18+
guard let style = style, !style.isEmpty else {
19+
return
20+
}
21+
cssExtended.append(style)
22+
}
23+
24+
func addCssInject(style: String?) {
25+
guard let style = style, !style.isEmpty else {
26+
return
27+
}
28+
cssInject.append(style)
29+
}
30+
31+
func addScriptlet(scriptlet: String?) {
32+
guard let scriptlet = scriptlet, !scriptlet.isEmpty else {
33+
return
34+
}
35+
scriptlets.append(scriptlet)
36+
}
37+
38+
func clear() {
39+
scripts = []
40+
cssExtended = []
41+
cssInject = []
42+
scriptlets = []
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Foundation
2+
3+
// Json decoded object description
4+
struct BlockerRule: Codable {
5+
let ifDomain: [String]?
6+
let urlFilter: String?
7+
let unlessDomain: [String]?
8+
let shortcut: String?
9+
let type: String
10+
let css: String?
11+
let script: String?
12+
let scriptlet: String?
13+
let scriptletParam: String?
14+
15+
lazy var regex: NSRegularExpression? = {
16+
if let urlFilter = urlFilter {
17+
let regex = try? NSRegularExpression(pattern: urlFilter, options: [])
18+
return regex
19+
}
20+
return nil
21+
}()
22+
}

0 commit comments

Comments
 (0)