Skip to content

Commit d8bb109

Browse files
authored
fix: metadata @match now for url path plus query string (#463)
* fix: `@match` for path plus query string * fix: xctest with new cases
1 parent b4d90e1 commit d8bb109

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

xcode/Mac-Tests/UserscriptsTests.swift

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
import XCTest
10-
@testable import Userscripts_Extension
10+
@testable import Userscripts_Mac_Safari_Extension
1111

1212
class UserscriptsTests: XCTestCase {
1313

@@ -144,6 +144,10 @@ class UserscriptsTests: XCTestCase {
144144
"http://127.0.0.1/*": [
145145
"http://127.0.0.1/",
146146
"http://127.0.0.1/foo/bar.html"
147+
],
148+
"*://*.example.com/*?a=1*": [
149+
"http://example.com/?a=1",
150+
"https://www.example.com/index?a=1&b=2"
147151
]
148152
]
149153
let patternDictFails = [
@@ -163,37 +167,25 @@ class UserscriptsTests: XCTestCase {
163167
],
164168
"https://www.example*/*": [
165169
"https://www.example.com/"
170+
],
171+
"*://*.example.com/*?a=1*": [
172+
"http://example.com/",
173+
"https://www.example.com/?a=2"
166174
]
167175
]
168176
for (pattern, urls) in patternDict {
169177
count = count + urls.count
170178
for url in urls {
171-
if
172-
let parts = getUrlProps(url),
173-
let ptcl = parts["protocol"],
174-
let host = parts["host"],
175-
let path = parts["pathname"]
176-
{
177-
if match(ptcl, host, path, pattern) {
178-
result.append("1")
179-
}
179+
if match(url, pattern) {
180+
result.append("1")
180181
}
181182
}
182183
}
183184
for (pattern, urls) in patternDictFails {
184185
// don't increment count since these tests should fail
185186
for url in urls {
186-
if
187-
let parts = getUrlProps(url),
188-
let ptcl = parts["protocol"],
189-
let host = parts["host"],
190-
let path = parts["pathname"]
191-
{
192-
if match(ptcl, host, path, pattern) {
193-
// if these match, results will get an extra element
194-
// and then the test will fail
195-
result.append("1")
196-
}
187+
if match(url, pattern) {
188+
result.removeLast()
197189
}
198190
}
199191
}

xcode/Safari-Extension/Functions.swift

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,17 @@ func getUrlProps(_ url: String) -> [String: String]? {
10721072
err("failed to parse url in getUrlProps")
10731073
return nil
10741074
}
1075-
return ["protocol": "\(ptcl):", "host": host, "pathname": parts.path, "href": url]
1075+
var search = ""
1076+
if let query = parts.query {
1077+
search = "?" + query
1078+
}
1079+
return [
1080+
"protocol": "\(ptcl):",
1081+
"host": host,
1082+
"pathname": parts.path,
1083+
"search": search,
1084+
"href": url
1085+
]
10761086
}
10771087

10781088
func stringToRegex(_ stringPattern: String) -> NSRegularExpression? {
@@ -1085,7 +1095,23 @@ func stringToRegex(_ stringPattern: String) -> NSRegularExpression? {
10851095
return regex
10861096
}
10871097

1088-
func match(_ ptcl: String,_ host: String,_ path: String,_ matchPattern: String) -> Bool {
1098+
func match(_ url: String, _ matchPattern: String) -> Bool {
1099+
guard
1100+
let parts = getUrlProps(url),
1101+
let ptcl = parts["protocol"],
1102+
let host = parts["host"],
1103+
var path = parts["pathname"]
1104+
else {
1105+
err("invalid url \(url)")
1106+
return false
1107+
}
1108+
1109+
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns#path
1110+
// The value for the path matches against the string which is the URL path plus the URL query string
1111+
if let search = parts["search"], search.count > 0 {
1112+
path += search
1113+
}
1114+
10891115
// matchPattern is the value from metatdata key @match or @exclude-match
10901116
if (matchPattern == "<all_urls>") {
10911117
return true
@@ -1156,17 +1182,7 @@ func include(_ url: String,_ pattern: String) -> Bool {
11561182
func getMatchedFiles(_ url: String, _ optionalManifest: Manifest?, _ checkBlocklist: Bool) -> [String] {
11571183
logText("Getting matched files for \(url)")
11581184
let manifest = optionalManifest ?? getManifest()
1159-
guard
1160-
let parts = getUrlProps(url),
1161-
let ptcl = parts["protocol"],
1162-
let host = parts["host"],
1163-
let path = parts["pathname"],
1164-
let href = parts["href"]
1165-
else {
1166-
err("getMatchedFiles failed at (1) for \(url)")
1167-
return [String]()
1168-
}
1169-
1185+
11701186
// filenames that should not load for the passed url
11711187
// the manifest values from @exclude and @exclude-match populate this set
11721188
var excludedFilenames: Set<String> = []
@@ -1185,7 +1201,7 @@ func getMatchedFiles(_ url: String, _ optionalManifest: Manifest?, _ checkBlockl
11851201
// if url matches a pattern in blocklist, no injection for this url
11861202
if (checkBlocklist) {
11871203
for pattern in manifest.blacklist {
1188-
if match(ptcl, host, path, pattern) {
1204+
if match(url, pattern) {
11891205
// return empty array
11901206
return Array(matchedFilenames)
11911207
}
@@ -1195,7 +1211,7 @@ func getMatchedFiles(_ url: String, _ optionalManifest: Manifest?, _ checkBlockl
11951211
// loop through all the @exclude-match patterns
11961212
// if any match passed url, push all filenames to excludedFilenames set
11971213
for pattern in excludeMatchPatterns {
1198-
if match(ptcl, host, path, pattern) {
1214+
if match(url, pattern) {
11991215
guard let filenames = manifest.excludeMatch[pattern] else {
12001216
err("getMatchedFiles failed at (2) for \(pattern)")
12011217
continue
@@ -1204,7 +1220,7 @@ func getMatchedFiles(_ url: String, _ optionalManifest: Manifest?, _ checkBlockl
12041220
}
12051221
}
12061222
for exp in excludeExpressions {
1207-
if include(href, exp) {
1223+
if include(url, exp) {
12081224
guard let filenames = manifest.exclude[exp] else {
12091225
err("getMatchedFiles failed at (3) for \(exp)")
12101226
continue
@@ -1213,7 +1229,7 @@ func getMatchedFiles(_ url: String, _ optionalManifest: Manifest?, _ checkBlockl
12131229
}
12141230
}
12151231
for pattern in matchPatterns {
1216-
if match(ptcl, host, path, pattern) {
1232+
if match(url, pattern) {
12171233
guard let filenames = manifest.match[pattern] else {
12181234
err("getMatchedFiles failed at (4) for \(pattern)")
12191235
continue
@@ -1222,7 +1238,7 @@ func getMatchedFiles(_ url: String, _ optionalManifest: Manifest?, _ checkBlockl
12221238
}
12231239
}
12241240
for exp in includeExpressions {
1225-
if include(href, exp) {
1241+
if include(url, exp) {
12261242
guard let filenames = manifest.include[exp] else {
12271243
err("getMatchedFiles failed at (5) for \(exp)")
12281244
continue
@@ -1616,14 +1632,10 @@ func getPopupBadgeCount(_ url: String, _ subframeUrls: [String]) -> Int? {
16161632
if showCount == "false" {
16171633
return 0
16181634
}
1619-
if let parts = getUrlProps(url), let ptcl = parts["protocol"], let host = parts["host"], let path = parts["pathname"] {
1620-
for pattern in manifest.blacklist {
1621-
if match(ptcl, host, path, pattern) {
1622-
return 0
1623-
}
1635+
for pattern in manifest.blacklist {
1636+
if match(url, pattern) {
1637+
return 0
16241638
}
1625-
} else {
1626-
return 0
16271639
}
16281640
if active != "true" {
16291641
return 0

xcode/xcconfig/Mac-Tests.xcconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ MARKETING_VERSION = 1.0
55
PRODUCT_BUNDLE_IDENTIFIER = $(ORG_IDENTIFIER).UserscriptsTests
66
PRODUCT_NAME = $(TARGET_NAME)
77
SWIFT_EMIT_LOC_STRINGS = NO
8-
TEST_HOST = $(BUILT_PRODUCTS_DIR)/Userscripts.app/Contents/MacOS/Userscripts
8+
TEST_HOST = $(BUILT_PRODUCTS_DIR)/Userscripts-Debug.app/Contents/MacOS/Userscripts-Debug
99

1010
// Override this file
1111
#include? "Mac-Tests.dev.xcconfig"

0 commit comments

Comments
 (0)