From 692146fe00ca0acdb8bb530cc1709410c8244b6e Mon Sep 17 00:00:00 2001 From: kernelkind Date: Mon, 1 Jan 2024 15:17:13 -0500 Subject: [PATCH] add comma as disallowed char at end of url Do not include comma as part of a URL if it is followed by whitespace. This allows users to make lists of URLs. Closes: https://github.com/damus-io/damus/issues/1833 LNURL1DP68GURN8GHJ7EM9W3SKCCNE9E3K7MF0D3H82UNVWQHKWUN9V4HXGCTHDC6RZVGR8SW3G Signed-off-by: kernelkind Signed-off-by: William Casarin --- damus-c/cursor.h | 2 +- damusTests/UrlTests.swift | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/damus-c/cursor.h b/damus-c/cursor.h index 78aca7fb..69e88eae 100644 --- a/damus-c/cursor.h +++ b/damus-c/cursor.h @@ -498,7 +498,7 @@ static inline int next_char_is_whitespace(unsigned char *curChar, unsigned char } static int char_disallowed_at_end_url(char c){ - return c == '.'; + return c == '.' || c == ','; } static inline int is_final_url_char(unsigned char *curChar, unsigned char *endChar){ diff --git a/damusTests/UrlTests.swift b/damusTests/UrlTests.swift index f9fa09ac..ad1bf725 100644 --- a/damusTests/UrlTests.swift +++ b/damusTests/UrlTests.swift @@ -140,6 +140,51 @@ final class UrlTests: XCTestCase { 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_OneURLEndComma_RemovesComma(){ + testParseURL(inputURLString: "http://example.com,", expectedURLs: "http://example.com") + } + + func testParseURL_OneURL_RemovesComma(){ + testParseURL(inputURLString: "http://example.com/,test", expectedURLs: "http://example.com/,test") + } + + func testParseURL_OneURLEndCommaAndSpaceSimple_RemovesComma(){ + testParseURL(inputURLString: "http://example.com, ", expectedURLs: "http://example.com") + } + + func testParseURL_OneURLEndCommaComplex_RemovesComma(){ + testParseURL(inputURLString: "http://example.com/test,", expectedURLs: "http://example.com/test") + } + + func testParseURL_TwoURLEndCommaSimple_RemovesCommas(){ + testParseURL(inputURLString: "http://example.com, http://example.com,", expectedURLs: "http://example.com", "http://example.com") + } + + func testParseURL_ThreeURLEndCommaSimple_RemovesCommas(){ + testParseURL(inputURLString: "http://example.com, http://example.com, http://example.com,", expectedURLs: "http://example.com", "http://example.com", "http://example.com") + } + + func testParseURL_TwoURLEndCommaFirstComplexSecondSimple_RemovesCommas(){ + testParseURL(inputURLString: "http://example.com/test, http://example.com,", expectedURLs: "http://example.com/test", "http://example.com") + } + + func testParseURL_TwoURLEndCommaFirstSimpleSecondComplex_RemovesCommas(){ + testParseURL(inputURLString: "http://example.com, http://example.com/test,", expectedURLs: "http://example.com", "http://example.com/test") + } + + func testParseURL_TwoURLEndCommaFirstComplexSecondComplex_RemovesCommas(){ + testParseURL(inputURLString: "http://example.com/test, http://example.com/test,", expectedURLs: "http://example.com/test", "http://example.com/test") + } + + func testParseURL_OneURLEndCommaSerachQuery_RemovesComma(){ + testParseURL(inputURLString: "https://www.example.com/search?q=test+query,", expectedURLs: "https://www.example.com/search?q=test+query") + } + + func testParseURL_TwoURLFirstSimpleSecondSimpleNoSpace_RemovesCommas(){ + testParseURL(inputURLString: "http://example.com,http://example.com,", + expectedURLs: "http://example.com", "http://example.com") + } } func testParseURL(inputURLString: String, expectedURLs: String...) {