Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add tests for dns-prefetch #1522

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions lychee-lib/src/extract/html/html5ever.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ impl TokenSink for LinkExtractor {
}
}

// Check and exclude rel=preconnect. Other than prefetch and preload,
// preconnect only does DNS lookups and might not be a link to a resource
// Check and exclude `rel=preconnect` and `rel=dns-prefetch`. Unlike `prefetch` and `preload`,
// `preconnect` and `dns-prefetch` only perform DNS lookups and do not necessarily link to a resource
if let Some(rel) = attrs.iter().find(|attr| &attr.name.local == "rel") {
if rel.value.contains("preconnect") {
if rel.value.contains("preconnect") || rel.value.contains("dns-prefetch") {
return TokenSinkResult::Continue;
}
}
Expand Down 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());
}
}
Loading