Account for the separator between host and path

This commit is contained in:
Michael Howell
2026-09-17 01:24:45 -07:00
parent 8e4f28735b
commit 459ba37b44
4 changed files with 98 additions and 29 deletions

View File

@@ -98,41 +98,50 @@ fn find_raw_urls(
// For now, we only check "full" URLs (meaning, starting with "http://" or "https://").
for match_ in URL_SCHEME_HOST_REGEX.find_iter(text) {
let mut url_range = match_.range();
// We found the scheme and host. Find the path, now.
// We found the scheme and host. Find the path, query, or fragment.
// We want to check for matching, balanced parens,
// but regex isn't powerful enough for that.
let mut paren_stack = Vec::with_capacity(3);
while let Some(&c) = text.as_bytes().get(url_range.end) {
if c == b'(' {
paren_stack.push(url_range.end);
} else if c == b')' {
if paren_stack.pop().is_none() {
break;
}
} else if !matches!(
c,
b'-'
| b'a'..=b'z'
| b'A'..=b'Z'
| b'0'..=b'9'
| b'@'
| b':'
| b'%'
| b'_'
| b'\\'
| b'+'
| b'.'
| b'~'
| b'#'
| b'?'
| b'&'
| b'/'
| b'='
) {
'parts: while let Some(&sep) = text.as_bytes().get(url_range.end) {
// The hostname must be immediately followed by a path, query,
// or fragment-declaring separator.
if !matches!(sep, b'/' | b'?' | b'#') {
break;
}
url_range.end += 1;
while let Some(&c) = text.as_bytes().get(url_range.end) {
if c == b'(' {
paren_stack.push(url_range.end);
} else if c == b')' {
// We assume the first unmatched parenthesis marks the end of the url,
// as urls rarely contain unbalanced parenthesis in practice.
if paren_stack.pop().is_none() {
break 'parts;
}
} else if !matches!(
c,
b'-'
| b'a'..=b'z'
| b'A'..=b'Z'
| b'0'..=b'9'
| b'@'
| b':'
| b'%'
| b'_'
| b'\\'
| b'+'
| b'.'
| b'~'
| b'&'
| b'='
) {
break;
}
url_range.end += 1;
}
}
// We assume the first unmatched parenthesis marks the end of the url,
// as urls rarely contain unbalanced parenthesis in practice.
if let Some(&end) = paren_stack.first() {
url_range.end = end;
}

View File

@@ -143,3 +143,15 @@ pub fn nested_parens_unbalanced_5() {}
/// See http://ex(amp)le.com/
pub fn nested_parens_cannot_appear_in_host() {}
/// The path component of a URL must be separated from the rest by a `/`,
/// so the `=a` isn't part of the URL.
/// <https://foo.bar>=a
//~^ ERROR this URL is not a hyperlink
pub fn equal_sign_after_host() {}
/// A URL can, however, have a fragment or query without a slash.
/// <https://foo.bar#a> or <https://foo.bar?a>
//~^ ERROR this URL is not a hyperlink
//~| ERROR this URL is not a hyperlink
pub fn hash_or_question_after_host() {}

View File

@@ -143,3 +143,15 @@ pub fn nested_parens_unbalanced_5() {}
/// See http://ex(amp)le.com/
pub fn nested_parens_cannot_appear_in_host() {}
/// The path component of a URL must be separated from the rest by a `/`,
/// so the `=a` isn't part of the URL.
/// https://foo.bar=a
//~^ ERROR this URL is not a hyperlink
pub fn equal_sign_after_host() {}
/// A URL can, however, have a fragment or query without a slash.
/// https://foo.bar#a or https://foo.bar?a
//~^ ERROR this URL is not a hyperlink
//~| ERROR this URL is not a hyperlink
pub fn hash_or_question_after_host() {}

View File

@@ -508,5 +508,41 @@ help: use an automatic link instead
LL | /// See <http://example.com/test>.)
| + +
error: aborting due to 42 previous errors
error: this URL is not a hyperlink
--> $DIR/bare-urls.rs:149:5
|
LL | /// https://foo.bar=a
| ^^^^^^^^^^^^^^^
|
= note: bare URLs are not automatically turned into clickable links
help: use an automatic link instead
|
LL | /// <https://foo.bar>=a
| + +
error: this URL is not a hyperlink
--> $DIR/bare-urls.rs:154:5
|
LL | /// https://foo.bar#a or https://foo.bar?a
| ^^^^^^^^^^^^^^^^^
|
= note: bare URLs are not automatically turned into clickable links
help: use an automatic link instead
|
LL | /// <https://foo.bar#a> or https://foo.bar?a
| + +
error: this URL is not a hyperlink
--> $DIR/bare-urls.rs:154:26
|
LL | /// https://foo.bar#a or https://foo.bar?a
| ^^^^^^^^^^^^^^^^^
|
= note: bare URLs are not automatically turned into clickable links
help: use an automatic link instead
|
LL | /// https://foo.bar#a or <https://foo.bar?a>
| + +
error: aborting due to 45 previous errors