` elements produced by ``, ``, and macros.
//!
//! Content produced by these features may contain metadata such as ``, ``, and
//! ``, which belongs in the document's `` wherever it appears. User-authored HTML that
//! doesn't come from one of these features is never moved.
use crate::arena::{NodeData, Ref};
use html5ever::{ExpandedName, namespace_url, ns};
fn attr(node: Ref, name: &str) -> Option {
node.get_attribute(ExpandedName {
ns: &ns!(),
local: &name.into(),
})
}
/// Identifies `` elements that a later copy replaces rather than adds to.
pub fn head_key(node: Ref) -> Option {
let NodeData::Element { name, .. } = &node.data else {
return None;
};
if name.ns != ns!(html) {
return None;
}
match name.local.as_ref() {
"title" | "base" => Some(name.local.to_string()),
"meta" => {
if attr(node, "charset").is_some() {
Some("meta charset".into())
} else if let Some(name) = attr(node, "name") {
Some(format!("meta name={}", name.to_ascii_lowercase()))
} else if let Some(property) = attr(node, "property") {
Some(format!("meta property={property}"))
} else {
attr(node, "http-equiv").map(|v| format!("meta http-equiv={}", v.to_ascii_lowercase()))
}
}
"link" => {
let rel = attr(node, "rel")?.to_ascii_lowercase();
if rel == "canonical" {
Some("link canonical".into())
} else {
attr(node, "href").map(|href| format!("link {rel} {href}"))
}
}
"script" => attr(node, "src").map(|src| format!("script {src}")),
_ => None,
}
}
/// Whether an element belongs in ``. `` and `` with `itemprop` are microdata,
/// which is valid in the body. `