commit f0df4aa218cc1997e5db671149b7cbcf913c7fa8
parent bb9fc6f905abaa403825acb76593b3b93c0a9074
Author: Gert Goet <gert@thinkcreate.dk>
Date: Sun, 12 Feb 2023 13:44:03 +0100
Strip common punctuations from URLs
Changelog-Fixed: Fix punctuation getting included in some urls
Closes: #575
Diffstat:
2 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/damus-c/damus.c b/damus-c/damus.c
@@ -26,6 +26,10 @@ static inline int is_boundary(char c) {
return !isalnum(c);
}
+static inline int is_invalid_url_ending(char c) {
+ return c == '!' || c == '?' || c == ')' || c == '.' || c == ',' || c == ';';
+}
+
static void make_cursor(struct cursor *c, const u8 *content, size_t len)
{
c->start = content;
@@ -221,6 +225,9 @@ static int parse_url(struct cursor *cur, struct block *block) {
return 0;
}
+ // strip any unwanted characters
+ while(is_invalid_url_ending(peek_char(cur, -1))) cur->p--;
+
block->type = BLOCK_URL;
block->block.str.start = (const char *)start;
block->block.str.end = (const char *)cur->p;
diff --git a/damusTests/damusTests.swift b/damusTests/damusTests.swift
@@ -148,6 +148,14 @@ class damusTests: XCTestCase {
XCTAssertEqual(parsed[0].is_text, testString)
}
+ func testNoParseUrlTrailingCharacters() {
+ let testString = "https://foo.bar, "
+ let parsed = parse_mentions(content: testString, tags: [])
+
+ XCTAssertNotNil(parsed)
+ XCTAssertEqual(parsed[0].is_url?.absoluteString, "https://foo.bar")
+ }
+
func testParseMentionBlank() {
let parsed = parse_mentions(content: "", tags: [["e", "event_id"]])