Strip common punctuations from URLs

Changelog-Fixed: Fix punctuation getting included in some urls
Closes: #575
This commit is contained in:
Gert Goet
2023-02-12 13:44:03 +01:00
committed by William Casarin
parent bb9fc6f905
commit f0df4aa218
2 changed files with 15 additions and 0 deletions

View File

@@ -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;

View File

@@ -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"]])