Skip to content

Commit

Permalink
feat: Add tests for dns-prefetch (#1522)
Browse files Browse the repository at this point in the history
* Exclude `rel=dns-prefetch` links

Resolves #1499

* Add tests for dns-prefetch

---------

Co-authored-by: wackget <[email protected]>
  • Loading branch information
mre and wackget authored Oct 11, 2024
1 parent e398325 commit 913cf71
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
20 changes: 20 additions & 0 deletions lychee-lib/src/extract/html/html5ever.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,24 @@ mod tests {
let uris = extract_html(input, false);
assert!(uris.is_empty());
}

#[test]
fn test_skip_dns_prefetch() {
let input = r#"
<link rel="dns-prefetch" href="https://example.com">
"#;

let uris = extract_html(input, false);
assert!(uris.is_empty());
}

#[test]
fn test_skip_dns_prefetch_reverse_order() {
let input = r#"
<link href="https://example.com" rel="dns-prefetch">
"#;

let uris = extract_html(input, false);
assert!(uris.is_empty());
}
}
29 changes: 25 additions & 4 deletions lychee-lib/src/extract/html/html5gum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ impl LinkExtractor {
/// Here are the rules for extracting links:
/// - If the current element has a `rel=nofollow` attribute, the current attribute
/// value is ignored.
/// - If the current element has a `rel=preconnect` attribute, the current attribute
/// value is ignored.
/// - If the current element has a `rel=preconnect` or `rel=dns-prefetch`
/// attribute, the current attribute value is ignored.
/// - If the current attribute value is not a URL, it is treated as plain text and
/// added to the links vector.
/// - If the current attribute name is `id`, the current attribute value is added
Expand All @@ -170,8 +170,9 @@ impl LinkExtractor {
}

if self.current_attributes.get("rel").map_or(false, |rel| {
rel.split(',')
.any(|r| r.trim() == "nofollow" || r.trim() == "preconnect")
rel.split(',').any(|r| {
r.trim() == "nofollow" || r.trim() == "preconnect" || r.trim() == "dns-prefetch"
})
}) {
self.current_attributes.clear();
return;
Expand Down Expand Up @@ -607,4 +608,24 @@ mod tests {
let uris = extract_html(input, false);
assert!(uris.is_empty());
}

#[test]
fn test_skip_dns_prefetch() {
let input = r#"
<link rel="dns-prefetch" href="https://example.com">
"#;

let uris = extract_html(input, false);
assert!(uris.is_empty());
}

#[test]
fn test_skip_dns_prefetch_reverse_order() {
let input = r#"
<link href="https://example.com" rel="dns-prefetch">
"#;

let uris = extract_html(input, false);
assert!(uris.is_empty());
}
}

0 comments on commit 913cf71

Please sign in to comment.