feat: respect the disabled property for stylesheet links (#1716)

Signed-off-by: Keming <kemingy94@gmail.com>
This commit is contained in:
Keming 2025-05-25 19:13:22 +08:00 committed by GitHub
parent b128b86a48
commit 1c97f26aa2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 0 deletions

View file

@ -128,6 +128,11 @@ impl TokenSink for LinkExtractor {
if url.starts_with("/@") || url.starts_with('@') {
return false;
}
// Skip disabled stylesheets
// Ref: https://developer.mozilla.org/en-US/docs/Web/API/HTMLLinkElement/disabled
if attrs.iter().any(|attr| &attr.name.local == "disabled") {
return false;
}
}
!is_email || (is_mailto && is_href) || (is_phone && is_href)
@ -337,6 +342,22 @@ mod tests {
assert_eq!(uris, expected);
}
#[test]
fn test_exclude_disabled_stylesheet() {
let input = r#"
<link rel="stylesheet" href="https://disabled.com" disabled>
<link rel="stylesheet" href="https://disabled.com" disabled="disabled">
<a href="https://example.org">i'm fine</a>
"#;
let expected = vec![RawUri {
text: "https://example.org".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>

View file

@ -197,6 +197,14 @@ impl LinkExtractor {
return;
}
}
// Skip disabled stylesheets
// Ref: https://developer.mozilla.org/en-US/docs/Web/API/HTMLLinkElement/disabled
if self.current_attribute_name == "disabled"
|| self.current_attributes.contains_key("disabled")
{
self.current_attributes.clear();
return;
}
}
let new_urls = self
@ -517,6 +525,22 @@ mod tests {
assert_eq!(uris, expected);
}
#[test]
fn test_exclude_disabled_stylesheet() {
let input = r#"
<link rel="stylesheet" href="https://disabled.com" disabled>
<link rel="stylesheet" href="https://disabled.com" disabled="disabled">
<a href="https://example.org">i'm fine</a>
"#;
let expected = vec![RawUri {
text: "https://example.org".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_tel() {
let input = r#"<!DOCTYPE html>