diff --git a/xcode/Mac-Tests/UserscriptsTests.swift b/xcode/Mac-Tests/UserscriptsTests.swift index e708ecc9..12c91bd0 100644 --- a/xcode/Mac-Tests/UserscriptsTests.swift +++ b/xcode/Mac-Tests/UserscriptsTests.swift @@ -7,7 +7,7 @@ // import XCTest -@testable import Userscripts_Extension +@testable import Userscripts_Mac_Safari_Extension class UserscriptsTests: XCTestCase { @@ -144,6 +144,10 @@ class UserscriptsTests: XCTestCase { "http://127.0.0.1/*": [ "http://127.0.0.1/", "http://127.0.0.1/foo/bar.html" + ], + "*://*.example.com/*?a=1*": [ + "http://example.com/?a=1", + "https://www.example.com/index?a=1&b=2" ] ] let patternDictFails = [ @@ -163,37 +167,25 @@ class UserscriptsTests: XCTestCase { ], "https://www.example*/*": [ "https://www.example.com/" + ], + "*://*.example.com/*?a=1*": [ + "http://example.com/", + "https://www.example.com/?a=2" ] ] for (pattern, urls) in patternDict { count = count + urls.count for url in urls { - if - let parts = getUrlProps(url), - let ptcl = parts["protocol"], - let host = parts["host"], - let path = parts["pathname"] - { - if match(ptcl, host, path, pattern) { - result.append("1") - } + if match(url, pattern) { + result.append("1") } } } for (pattern, urls) in patternDictFails { // don't increment count since these tests should fail for url in urls { - if - let parts = getUrlProps(url), - let ptcl = parts["protocol"], - let host = parts["host"], - let path = parts["pathname"] - { - if match(ptcl, host, path, pattern) { - // if these match, results will get an extra element - // and then the test will fail - result.append("1") - } + if match(url, pattern) { + result.removeLast() } } } diff --git a/xcode/Safari-Extension/Functions.swift b/xcode/Safari-Extension/Functions.swift index 1e06aaef..4a9ec557 100644 --- a/xcode/Safari-Extension/Functions.swift +++ b/xcode/Safari-Extension/Functions.swift @@ -1072,7 +1072,17 @@ func getUrlProps(_ url: String) -> [String: String]? { err("failed to parse url in getUrlProps") return nil } - return ["protocol": "\(ptcl):", "host": host, "pathname": parts.path, "href": url] + var search = "" + if let query = parts.query { + search = "?" + query + } + return [ + "protocol": "\(ptcl):", + "host": host, + "pathname": parts.path, + "search": search, + "href": url + ] } func stringToRegex(_ stringPattern: String) -> NSRegularExpression? { @@ -1085,7 +1095,23 @@ func stringToRegex(_ stringPattern: String) -> NSRegularExpression? { return regex } -func match(_ ptcl: String,_ host: String,_ path: String,_ matchPattern: String) -> Bool { +func match(_ url: String, _ matchPattern: String) -> Bool { + guard + let parts = getUrlProps(url), + let ptcl = parts["protocol"], + let host = parts["host"], + var path = parts["pathname"] + else { + err("invalid url \(url)") + return false + } + + // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns#path + // The value for the path matches against the string which is the URL path plus the URL query string + if let search = parts["search"], search.count > 0 { + path += search + } + // matchPattern is the value from metatdata key @match or @exclude-match if (matchPattern == "") { return true @@ -1156,17 +1182,7 @@ func include(_ url: String,_ pattern: String) -> Bool { func getMatchedFiles(_ url: String, _ optionalManifest: Manifest?, _ checkBlocklist: Bool) -> [String] { logText("Getting matched files for \(url)") let manifest = optionalManifest ?? getManifest() - guard - let parts = getUrlProps(url), - let ptcl = parts["protocol"], - let host = parts["host"], - let path = parts["pathname"], - let href = parts["href"] - else { - err("getMatchedFiles failed at (1) for \(url)") - return [String]() - } - + // filenames that should not load for the passed url // the manifest values from @exclude and @exclude-match populate this set var excludedFilenames: Set = [] @@ -1185,7 +1201,7 @@ func getMatchedFiles(_ url: String, _ optionalManifest: Manifest?, _ checkBlockl // if url matches a pattern in blocklist, no injection for this url if (checkBlocklist) { for pattern in manifest.blacklist { - if match(ptcl, host, path, pattern) { + if match(url, pattern) { // return empty array return Array(matchedFilenames) } @@ -1195,7 +1211,7 @@ func getMatchedFiles(_ url: String, _ optionalManifest: Manifest?, _ checkBlockl // loop through all the @exclude-match patterns // if any match passed url, push all filenames to excludedFilenames set for pattern in excludeMatchPatterns { - if match(ptcl, host, path, pattern) { + if match(url, pattern) { guard let filenames = manifest.excludeMatch[pattern] else { err("getMatchedFiles failed at (2) for \(pattern)") continue @@ -1204,7 +1220,7 @@ func getMatchedFiles(_ url: String, _ optionalManifest: Manifest?, _ checkBlockl } } for exp in excludeExpressions { - if include(href, exp) { + if include(url, exp) { guard let filenames = manifest.exclude[exp] else { err("getMatchedFiles failed at (3) for \(exp)") continue @@ -1213,7 +1229,7 @@ func getMatchedFiles(_ url: String, _ optionalManifest: Manifest?, _ checkBlockl } } for pattern in matchPatterns { - if match(ptcl, host, path, pattern) { + if match(url, pattern) { guard let filenames = manifest.match[pattern] else { err("getMatchedFiles failed at (4) for \(pattern)") continue @@ -1222,7 +1238,7 @@ func getMatchedFiles(_ url: String, _ optionalManifest: Manifest?, _ checkBlockl } } for exp in includeExpressions { - if include(href, exp) { + if include(url, exp) { guard let filenames = manifest.include[exp] else { err("getMatchedFiles failed at (5) for \(exp)") continue @@ -1616,14 +1632,10 @@ func getPopupBadgeCount(_ url: String, _ subframeUrls: [String]) -> Int? { if showCount == "false" { return 0 } - if let parts = getUrlProps(url), let ptcl = parts["protocol"], let host = parts["host"], let path = parts["pathname"] { - for pattern in manifest.blacklist { - if match(ptcl, host, path, pattern) { - return 0 - } + for pattern in manifest.blacklist { + if match(url, pattern) { + return 0 } - } else { - return 0 } if active != "true" { return 0 diff --git a/xcode/xcconfig/Mac-Tests.xcconfig b/xcode/xcconfig/Mac-Tests.xcconfig index d267dd62..998554bc 100644 --- a/xcode/xcconfig/Mac-Tests.xcconfig +++ b/xcode/xcconfig/Mac-Tests.xcconfig @@ -5,7 +5,7 @@ MARKETING_VERSION = 1.0 PRODUCT_BUNDLE_IDENTIFIER = $(ORG_IDENTIFIER).UserscriptsTests PRODUCT_NAME = $(TARGET_NAME) SWIFT_EMIT_LOC_STRINGS = NO -TEST_HOST = $(BUILT_PRODUCTS_DIR)/Userscripts.app/Contents/MacOS/Userscripts +TEST_HOST = $(BUILT_PRODUCTS_DIR)/Userscripts-Debug.app/Contents/MacOS/Userscripts-Debug // Override this file #include? "Mac-Tests.dev.xcconfig" \ No newline at end of file