use url::Url;
pub fn compute_effective_base(html: &str, caller_base_url: &str) -> Option<Url> {
let caller_base = Url::parse(caller_base_url).ok()?;
match scan_document_base_href(html) {
Some(href) if !href.is_empty() => Some(caller_base.join(&href).unwrap_or(caller_base)),
_ => Some(caller_base),
}
}
pub fn resolve_attribute_url(base: &Url, value: &str) -> Option<String> {
if value.is_empty() {
return None;
}
if Url::parse(value).is_ok() {
return None;
}
base.join(value).ok().map(|joined| joined.to_string())
}
fn scan_document_base_href(html: &str) -> Option<String> {
let bytes = html.as_bytes();
let mut idx = 0usize;
while let Some(offset) = memchr::memchr(b'<', &bytes[idx..]) {
let tag_start = idx + offset;
let name_start = tag_start + 1;
if !super::prescan::matches_tag_start(bytes, name_start, b"base") {
idx = tag_start + 1;
continue;
}
let tag_end = super::prescan::find_tag_end(bytes, name_start + b"base".len())?;
if let Some(href) = extract_href_attr(&html[tag_start..tag_end]) {
return Some(href);
}
idx = tag_end;
}
None
}
fn extract_href_attr(tag_text: &str) -> Option<String> {
let bytes = tag_text.as_bytes();
let lower = tag_text.to_ascii_lowercase();
let mut search_from = 0usize;
while let Some(rel) = lower[search_from..].find("href") {
let pos = search_from + rel;
search_from = pos + "href".len();
let preceded_by_boundary = pos == 0 || bytes[pos - 1].is_ascii_whitespace();
if !preceded_by_boundary {
continue;
}
let mut after = pos + "href".len();
while after < bytes.len() && bytes[after].is_ascii_whitespace() {
after += 1;
}
if bytes.get(after) != Some(&b'=') {
continue;
}
after += 1;
while after < bytes.len() && bytes[after].is_ascii_whitespace() {
after += 1;
}
let value = match bytes.get(after) {
Some(b'"') => {
let start = after + 1;
let end = start + tag_text[start..].find('"')?;
&tag_text[start..end]
}
Some(b'\'') => {
let start = after + 1;
let end = start + tag_text[start..].find('\'')?;
&tag_text[start..end]
}
Some(_) => {
let start = after;
let end = tag_text[start..]
.find(|c: char| c.is_whitespace() || c == '>' || c == '/')
.map_or(tag_text.len(), |i| start + i);
&tag_text[start..end]
}
None => return None,
};
return Some(html_escape::decode_html_entities(value).into_owned());
}
None
}
#[cfg(test)]
mod tests {
use super::*;
fn base(url: &str) -> Url {
Url::parse(url).unwrap()
}
#[test]
fn test_resolve_leaves_absolute_url_unchanged() {
let result = resolve_attribute_url(&base("https://example.com/page"), "https://other.example/x");
assert_eq!(result, None);
}
#[test]
fn test_resolve_protocol_relative_uses_base_scheme() {
let result = resolve_attribute_url(&base("https://example.com/page"), "//cdn.example.com/x.png");
assert_eq!(result, Some("https://cdn.example.com/x.png".to_string()));
}
#[test]
fn test_resolve_fragment_resolves_against_full_page_url() {
let result = resolve_attribute_url(&base("https://example.com/blog/post.html"), "#section");
assert_eq!(result, Some("https://example.com/blog/post.html#section".to_string()));
}
#[test]
fn test_resolve_leaves_mailto_unchanged() {
assert_eq!(
resolve_attribute_url(&base("https://example.com/"), "mailto:foo@bar.com"),
None
);
}
#[test]
fn test_resolve_leaves_tel_unchanged() {
assert_eq!(resolve_attribute_url(&base("https://example.com/"), "tel:+12345"), None);
}
#[test]
fn test_resolve_leaves_javascript_unchanged() {
assert_eq!(
resolve_attribute_url(&base("https://example.com/"), "javascript:alert(1)"),
None
);
}
#[test]
fn test_resolve_leaves_data_uri_unchanged() {
assert_eq!(
resolve_attribute_url(&base("https://example.com/"), "data:image/png;base64,AAA"),
None
);
}
#[test]
fn test_resolve_leaves_empty_href_unchanged() {
assert_eq!(resolve_attribute_url(&base("https://example.com/"), ""), None);
}
#[test]
fn test_resolve_leaves_malformed_url_unchanged() {
assert_eq!(
resolve_attribute_url(&base("https://example.com/"), "http://[not-a-valid-host"),
None
);
}
#[test]
fn test_resolve_relative_path_joins_against_base_directory() {
let result = resolve_attribute_url(&base("https://example.com/blog/index.html"), "post/child.html");
assert_eq!(result, Some("https://example.com/blog/post/child.html".to_string()));
}
#[test]
fn test_resolve_absolute_path_replaces_base_path() {
let result = resolve_attribute_url(&base("https://example.com/blog/index.html"), "/about");
assert_eq!(result, Some("https://example.com/about".to_string()));
}
#[test]
fn test_compute_effective_base_no_document_base_uses_caller_base() {
let effective = compute_effective_base("<html><body></body></html>", "https://example.com/blog/");
assert_eq!(effective.unwrap().as_str(), "https://example.com/blog/");
}
#[test]
fn test_compute_effective_base_relative_document_base_resolves_against_caller_base() {
let html = r#"<html><head><base href="/assets/"></head><body></body></html>"#;
let effective = compute_effective_base(html, "https://example.com/blog/post.html");
assert_eq!(effective.unwrap().as_str(), "https://example.com/assets/");
}
#[test]
fn test_compute_effective_base_absolute_document_base_overrides_caller_base() {
let html = r#"<html><head><base href="https://cdn.example.com/"></head><body></body></html>"#;
let effective = compute_effective_base(html, "https://example.com/blog/post.html");
assert_eq!(effective.unwrap().as_str(), "https://cdn.example.com/");
}
#[test]
fn test_compute_effective_base_empty_document_base_href_uses_caller_base() {
let html = r#"<html><head><base href=""></head><body></body></html>"#;
let effective = compute_effective_base(html, "https://example.com/blog/post.html");
assert_eq!(effective.unwrap().as_str(), "https://example.com/blog/post.html");
}
#[test]
fn test_compute_effective_base_malformed_caller_base_disables_resolution() {
let effective = compute_effective_base("<html></html>", "not a url");
assert_eq!(effective, None);
}
#[test]
fn test_compute_effective_base_finds_base_href_outside_head_range() {
let html = r#"<base href="/x/"><body></body>"#;
let effective = compute_effective_base(html, "https://example.com/");
assert_eq!(effective.unwrap().as_str(), "https://example.com/x/");
}
#[test]
fn test_scan_document_base_href_decodes_entities() {
let html = r#"<head><base href="/a&b"></head>"#;
assert_eq!(scan_document_base_href(html), Some("/a&b".to_string()));
}
#[test]
fn test_scan_document_base_href_ignores_tag_named_basefoo() {
let html = r#"<basefoo href="/wrong"></basefoo><base href="/right">"#;
assert_eq!(scan_document_base_href(html), Some("/right".to_string()));
}
}