Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex: Fixes Permalink action on timeline #303

Merged
merged 7 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions ElementX/Sources/Other/Extensions/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ extension String {

return string
}

/// Whether or not the string is a Matrix user ID.
var isMatrixUserID: Bool {
let range = NSRange(location: 0, length: count)

let detector = try? NSRegularExpression(pattern: MatrixEntityRegex.userId.rawValue, options: .caseInsensitive)
return detector?.numberOfMatches(in: self, range: range) ?? 0 == 1
}

/// Calculates a numeric hash same as Element Web
/// See original function here https://github.com/matrix-org/matrix-react-sdk/blob/321dd49db4fbe360fc2ff109ac117305c955b061/src/utils/FormattingUtils.js#L47
Expand Down
4 changes: 2 additions & 2 deletions ElementX/Sources/Other/MatrixEntityRegex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ enum MatrixEntityRegex: String {
case .roomId:
return "![A-Z0-9]+:" + MatrixEntityRegex.homeserver.rawValue
case .eventId:
return "\\$[A-Z0-9\\/+]+"
return "\\$[A-Z0-9-_.]+"
}
}

Expand All @@ -49,7 +49,7 @@ enum MatrixEntityRegex: String {
// swiftlint:enable force_try

static func isMatrixHomeserver(_ homeserver: String) -> Bool {
guard let match = userIdentifierRegex.firstMatch(in: homeserver, range: .init(location: 0, length: homeserver.count)) else {
guard let match = homeserverRegex.firstMatch(in: homeserver, range: .init(location: 0, length: homeserver.count)) else {
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ final class LoginCoordinator: Coordinator, Presentable {

/// Parses the specified username and looks up the homeserver when a Matrix ID is entered.
private func parseUsername(_ username: String) {
guard username.isMatrixUserID else { return }
guard MatrixEntityRegex.isMatrixUserIdentifier(username) else { return }

let homeserverDomain = String(username.split(separator: ":")[1])

Expand Down
53 changes: 53 additions & 0 deletions UnitTests/Sources/MatrixEntityRegexTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

import XCTest

@testable import ElementX

class MatrixEntityRegexTests: XCTestCase {
func testHomeserver() {
XCTAssertTrue(MatrixEntityRegex.isMatrixHomeserver("matrix.org"))
XCTAssertTrue(MatrixEntityRegex.isMatrixHomeserver("MATRIX.ORG"))
XCTAssertFalse(MatrixEntityRegex.isMatrixHomeserver("matrix?.org"))
}

func testUserId() {
XCTAssertTrue(MatrixEntityRegex.isMatrixUserIdentifier("@username:example.com"))
XCTAssertFalse(MatrixEntityRegex.isMatrixUserIdentifier("username:example.com"))
XCTAssertFalse(MatrixEntityRegex.isMatrixUserIdentifier("@username.example.com"))
}

func testRoomAlias() {
XCTAssertTrue(MatrixEntityRegex.isMatrixRoomAlias("#element-ios:matrix.org"))
XCTAssertFalse(MatrixEntityRegex.isMatrixRoomAlias("element-ios:matrix.org"))
XCTAssertFalse(MatrixEntityRegex.isMatrixRoomAlias("#element-ios.matrix.org"))
}

func testRoomId() {
XCTAssertTrue(MatrixEntityRegex.isMatrixRoomIdentifier("!pMBteVpcoJRdCJxDmn:matrix.org"))
XCTAssertFalse(MatrixEntityRegex.isMatrixRoomIdentifier("pMBteVpcoJRdCJxDmn:matrix.org"))
XCTAssertFalse(MatrixEntityRegex.isMatrixRoomIdentifier("!pMBteVpcoJRdCJxDmn.matrix.org"))
}

func testEventId() {
XCTAssertTrue(MatrixEntityRegex.isMatrixEventIdentifier("$shlXuTEUoMpcJqwqsDjIu33PY9bZny6US5EnX8mkMjc"))
XCTAssertTrue(MatrixEntityRegex.isMatrixEventIdentifier("$8dDBOeRGpH3I-Mup6ypy2HA4MgkadFGG2h0R_pK_ebs"))
XCTAssertFalse(MatrixEntityRegex.isMatrixEventIdentifier("$8dDBOeRGpH3I-Mup6ypy2HA4MgkadFGG2h0R_pK_ebs?"))
}
}