Skip to content

Commit

Permalink
Merge pull request #83 from vmg/underscore-in-subdomain
Browse files Browse the repository at this point in the history
allow underscores in subdomains
  • Loading branch information
Ashe Connor committed Feb 11, 2019
2 parents 994403d + 28b1a7f commit 3cc0a9b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Empty file modified COPYING
100755 → 100644
Empty file.
23 changes: 20 additions & 3 deletions ext/rinku/autolink.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
#define strncasecmp _strnicmp
#endif

static int
is_valid_hostchar(const uint8_t *link, size_t link_len)
{
size_t pos = 0;
int32_t ch = utf8proc_next(link, &pos);
return !utf8proc_is_space(ch) && !utf8proc_is_punctuation(ch);
}

bool
autolink_issafe(const uint8_t *link, size_t link_len)
{
Expand Down Expand Up @@ -160,16 +168,25 @@ static bool
check_domain(const uint8_t *data, size_t size,
struct autolink_pos *link, bool allow_short)
{
size_t i, np = 0;
size_t i, np = 0, uscore1 = 0, uscore2 = 0;

if (!rinku_isalnum(data[link->start]))
return false;

for (i = link->start + 1; i < size - 1; ++i) {
if (data[i] == '.') np++;
else if (!rinku_isalnum(data[i]) && data[i] != '-') break;
if (data[i] == '_') {
uscore2++;
} else if (data[i] == '.') {
uscore1 = uscore2;
uscore2 = 0;
np++;
} else if (!is_valid_hostchar(data + i, size - i) && data[i] != '-')
break;
}

if (uscore1 > 0 || uscore2 > 0)
return false;

link->end = i;

if (allow_short) {
Expand Down
8 changes: 8 additions & 0 deletions test/autolink_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,12 @@ def test_urls_with_quotes
assert_linked "'<a href=\"http://example.com\">http://example.com</a>'", "'http://example.com'"
assert_linked "\"<a href=\"http://example.com\">http://example.com</a>\"\"", "\"http://example.com\"\""
end

def test_underscore_in_domain
assert_linked "http://foo_bar.com", "http://foo_bar.com"
end

def test_underscore_in_subdomain
assert_linked "<a href=\"http://foo_bar.xyz.com\">http://foo_bar.xyz.com</a>", "http://foo_bar.xyz.com"
end
end

0 comments on commit 3cc0a9b

Please sign in to comment.