Handle period at end of URL

Fix parsing URL when encountering a period at the end of the url by
setting it as disallowed from being present at the end of a
URL.

Some characters are disallowed to be present at the end of URLs.
Presently, the period character is the only disallowed character.
A character is the last character in the URL if it is followed by
is_whitespace() or if it's the last character in the string.

Closes: https://github.com/damus-io/damus/issues/1638

LNURL1DP68GURN8GHJ7EM9W3SKCCNE9E3K7MF0D3H82UNVWQHKWUN9V4HXGCTHDC6RZVGR8SW3G

Signed-off-by: kernelkind <kernelkind@gmail.com>
Reviewed-by: William Casarin <jb55@jb55.com>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
kernelkind
2023-12-21 14:40:06 -05:00
committed by William Casarin
parent f6044a9eea
commit e547e26d99
3 changed files with 105 additions and 4 deletions

View File

@@ -100,5 +100,63 @@ final class UrlTests: XCTestCase {
XCTAssertEqual(blocks[1].asURL, testURL)
XCTAssertEqual(blocks[2].asText, " this is not a hashtag!")
}
func testParseURL_OneURLEndPeriodSimple_RemovesPeriod(){
testParseURL(inputURLString: "http://example.com.", expectedURLs: "http://example.com")
}
func testParseURL_OneURL_RemovesPeriod(){
testParseURL(inputURLString: "http://example.com/.test", expectedURLs: "http://example.com/.test")
}
func testParseURL_OneURLEndPeriodAndSpaceSimple_RemovesPeriod(){
testParseURL(inputURLString: "http://example.com. ", expectedURLs: "http://example.com")
}
func testParseURL_OneURLEndPeriodComplex_RemovesPeriod(){
testParseURL(inputURLString: "http://example.com/test.", expectedURLs: "http://example.com/test")
}
func testParseURL_TwoURLEndPeriodSimple_RemovesPeriods(){
testParseURL(inputURLString: "http://example.com. http://example.com.", expectedURLs: "http://example.com", "http://example.com")
}
func testParseURL_ThreeURLEndPeriodSimple_RemovesPeriods(){
testParseURL(inputURLString: "http://example.com. http://example.com. http://example.com.", expectedURLs: "http://example.com", "http://example.com", "http://example.com")
}
func testParseURL_TwoURLEndPeriodFirstComplexSecondSimple_RemovesPeriods(){
testParseURL(inputURLString: "http://example.com/test. http://example.com.", expectedURLs: "http://example.com/test", "http://example.com")
}
func testParseURL_TwoURLEndPeriodFirstSimpleSecondComplex_RemovesPeriods(){
testParseURL(inputURLString: "http://example.com. http://example.com/test.", expectedURLs: "http://example.com", "http://example.com/test")
}
func testParseURL_TwoURLEndPeriodFirstComplexSecondComplex_RemovesPeriods(){
testParseURL(inputURLString: "http://example.com/test. http://example.com/test.", expectedURLs: "http://example.com/test", "http://example.com/test")
}
func testParseURL_OneURLEndPeriodSerachQuery_RemovesPeriod(){
testParseURL(inputURLString: "https://www.example.com/search?q=test+query.", expectedURLs: "https://www.example.com/search?q=test+query")
}
}
func testParseURL(inputURLString: String, expectedURLs: String...) {
let parsedURL: [Block] = parse_note_content(content: .content(inputURLString, nil)).blocks.filter {
$0.isURL
}
if(expectedURLs.count != parsedURL.count) {
XCTFail()
}
for i in 0..<parsedURL.count {
guard let expectedURL = URL(string: expectedURLs[i]) else {
XCTFail()
return
}
XCTAssertEqual(parsedURL[i].asURL, expectedURL)
}
}