feat: Add tests for dns-prefetch (#1522)

* Exclude `rel=dns-prefetch` links

Resolves #1499

* Add tests for dns-prefetch

---------

Co-authored-by: wackget <136205263+wackget@users.noreply.github.com>
This commit is contained in:
Matthias Endler 2024-10-12 01:45:35 +02:00 committed by GitHub
parent e398325bb0
commit 913cf717a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 4 deletions

View file

@ -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());
}
}

View file

@ -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
@ -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;
@ -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());
}
}