pub fn hreflang_attr(tag: &str, name: &str) -> Option<String>Expand description
Reads a single attribute value out of a tag.
Accepts double, single, or unquoted attribute values, plus
valueless (boolean) attributes — HTML minifiers collapse alt=""
to a bare alt, which returns Some(String::new()). Match is
case-insensitive on the attribute name, case-preserving on the
value, and tokenises real attribute boundaries so data-href=
never satisfies a lookup for href.
The slightly odd name (hreflang_attr) is historical — this used
to live in the hreflang gate; promoted to a shared helper when the
CSP gate needed the same parser.
§Examples
use ssg::audit::gates::hreflang_attr;
let tag = r#"<link rel="alternate" hreflang="en" href="/en/">"#;
assert_eq!(hreflang_attr(tag, "hreflang"), Some("en".to_string()));
assert_eq!(hreflang_attr(tag, "href"), Some("/en/".to_string()));
// Minified valueless attribute (`alt=""` collapsed to `alt`).
assert_eq!(hreflang_attr("<img alt src=a.png>", "alt"), Some(String::new()));