Skip to content

Commit

Permalink
Exclude tel scheme from being checked (#1429)
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed May 19, 2024
1 parent 9e031b6 commit c3f7fe7
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
17 changes: 17 additions & 0 deletions lychee-lib/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ impl Client {
let status = match uri.scheme() {
_ if uri.is_file() => self.check_file(uri).await,
_ if uri.is_mail() => self.check_mail(uri).await,
_ if uri.is_tel() => self.check_tel(uri).await,
_ => self.check_website(uri, default_chain).await?,
};

Expand Down Expand Up @@ -702,6 +703,14 @@ impl Client {
pub async fn check_mail(&self, _uri: &Uri) -> Status {
Status::Excluded
}

/// Check a tel
///
/// This implementation simply excludes all tel.
#[allow(clippy::unused_async)]
pub async fn check_tel(&self, _uri: &Uri) -> Status {
Status::Excluded
}
}

// Check if the given `Url` would cause `reqwest` to panic.
Expand Down Expand Up @@ -907,6 +916,14 @@ mod tests {
}));
}

#[tokio::test]
async fn test_include_tel() {
let client = ClientBuilder::builder().build().client().unwrap();
assert!(client.is_excluded(&Uri {
url: "tel:1234567890".try_into().unwrap()
}));
}

#[tokio::test]
async fn test_require_https() {
let client = ClientBuilder::builder().build().client().unwrap();
Expand Down
26 changes: 25 additions & 1 deletion lychee-lib/src/extract/html/html5ever.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ impl TokenSink for LinkExtractor {
// This ignores links like `<img srcset="v2@1.5x.png">`
let is_email = is_email_link(url);
let is_mailto = url.starts_with("mailto:");
let is_phone = url.starts_with("tel:");
let is_href = attr.name.local.as_ref() == "href";

!is_email || (is_mailto && is_href)
!is_email || (is_mailto && is_href) || (is_phone && is_href)
})
.map(|url| RawUri {
text: url.to_string(),
Expand Down Expand Up @@ -318,6 +319,29 @@ mod tests {
let uris = extract_html(input, false);
assert_eq!(uris, expected);
}

#[test]
fn test_valid_tel() {
let input = r#"<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<a href="tel:1234567890">
</body>
</html>"#;

let expected = vec![RawUri {
text: "tel:1234567890".to_string(),
element: Some("a".to_string()),
attribute: Some("href".to_string()),
}];
let uris = extract_html(input, false);
assert_eq!(uris, expected);
}

#[test]
fn test_exclude_email_without_mailto() {
let input = r#"<!DOCTYPE html>
Expand Down
25 changes: 24 additions & 1 deletion lychee-lib/src/extract/html/html5gum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ impl LinkExtractor {
// This ignores links like `<img srcset="v2@1.5x.png">`
let is_email = is_email_link(url);
let is_mailto = url.starts_with("mailto:");
let is_phone = url.starts_with("tel:");
let is_href = attr == "href";

!is_email || (is_mailto && is_href)
!is_email || (is_mailto && is_href) || (is_phone && is_href)
})
.map(|url| RawUri {
text: url.to_string(),
Expand Down Expand Up @@ -453,6 +454,28 @@ mod tests {
assert_eq!(uris, expected);
}

#[test]
fn test_valid_tel() {
let input = r#"<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<a href="tel:1234567890">
</body>
</html>"#;

let expected = vec![RawUri {
text: "tel:1234567890".to_string(),
element: Some("a".to_string()),
attribute: Some("href".to_string()),
}];
let uris = extract_html(input, false);
assert_eq!(uris, expected);
}

#[test]
fn test_valid_email() {
let input = r#"<!DOCTYPE html>
Expand Down
1 change: 1 addition & 0 deletions lychee-lib/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ impl Filter {
|| self.is_host_excluded(uri)
|| self.is_ip_excluded(uri)
|| self.is_mail_excluded(uri)
|| uri.is_tel()
|| is_example_domain(uri)
|| is_unsupported_domain(uri)
{
Expand Down
15 changes: 15 additions & 0 deletions lychee-lib/src/types/uri/valid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ impl Uri {
self.scheme() == "mailto"
}

#[inline]
#[must_use]
/// Check if the URI is a tel
pub fn is_tel(&self) -> bool {
self.scheme() == "tel"
}

#[inline]
#[must_use]
/// Check if the URI is a file
Expand Down Expand Up @@ -325,6 +332,14 @@ mod tests {
);
}

#[test]
fn test_uri_tel() {
assert_eq!(
Uri::try_from("tel:1234567890"),
Ok(Uri::try_from("tel:1234567890").unwrap())
);
}

#[test]
fn test_uri_host_ip_v4() {
assert_eq!(
Expand Down

0 comments on commit c3f7fe7

Please sign in to comment.