Fix URL detection matching invalid URLs

Fixes #2450.
This commit is contained in:
Christian Duerr 2019-05-16 21:05:00 +00:00 committed by GitHub
parent 2a6e9843ea
commit d934df6c0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 10 deletions

View File

@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Mouse mode generating events when the cell has not changed
- Selections not automatically expanding across double-width characters
- On macOS, automatic graphics switching has been enabled again
- Text getting recognized as URLs without slashes separating the scheme
### Removed

View File

@ -13,14 +13,14 @@
// limitations under the License.
use unicode_width::UnicodeWidthChar;
use url;
use crate::term::cell::{Cell, Flags};
// See https://tools.ietf.org/html/rfc3987#page-13
const URL_SEPARATOR_CHARS: [char; 10] = ['<', '>', '"', ' ', '{', '}', '|', '\\', '^', '`'];
const URL_DENY_END_CHARS: [char; 8] = ['.', ',', ';', ':', '?', '!', '/', '('];
const URL_SCHEMES: [&str; 8] = ["http", "https", "mailto", "news", "file", "git", "ssh", "ftp"];
const URL_SCHEMES: [&str; 8] =
["http://", "https://", "mailto:", "news:", "file://", "git://", "ssh://", "ftp://"];
/// URL text and origin of the original click position.
#[derive(Debug, PartialEq)]
@ -117,16 +117,15 @@ impl UrlParser {
}
// Check if string is valid url
match url::Url::parse(&self.state) {
Ok(url) => {
if URL_SCHEMES.contains(&url.scheme()) && self.origin > 0 {
Some(Url { origin: self.origin - 1, text: self.state })
} else {
None
if self.origin > 0 && url::Url::parse(&self.state).is_ok() {
for scheme in &URL_SCHEMES {
if self.state.starts_with(scheme) {
return Some(Url { origin: self.origin - 1, text: self.state });
}
},
Err(_) => None,
}
}
None
}
fn advance(&mut self, c: char, pos: usize) -> bool {
@ -305,5 +304,14 @@ mod tests {
url_test("git://example.org", "git://example.org");
url_test("ssh://example.org", "ssh://example.org");
url_test("ftp://example.org", "ftp://example.org");
assert_eq!(url_create_term("mailto.example.org").url_search(Point::default()), None);
assert_eq!(url_create_term("https:example.org").url_search(Point::default()), None);
assert_eq!(url_create_term("http:example.org").url_search(Point::default()), None);
assert_eq!(url_create_term("news.example.org").url_search(Point::default()), None);
assert_eq!(url_create_term("file:example.org").url_search(Point::default()), None);
assert_eq!(url_create_term("git:example.org").url_search(Point::default()), None);
assert_eq!(url_create_term("ssh:example.org").url_search(Point::default()), None);
assert_eq!(url_create_term("ftp:example.org").url_search(Point::default()), None);
}
}