Skip to main content

resolve_srcset

Function resolve_srcset 

Source
pub fn resolve_srcset(base: &str, srcset: &str) -> String
Expand description

Resolve every URL in a srcset attribute value ("a.png 1x, b.png 2x") against base with resolve_url, keeping each candidate’s descriptor.

Candidates are split as the HTML standard’s srcset parser does: a URL is a run of non-whitespace (so URLs containing commas survive), trailing commas end a candidate, descriptors run to the next comma outside parentheses, and empty candidates are dropped. The result is normalized to "url descriptor, url descriptor".

use domtree::resolve_srcset;

assert_eq!(
    resolve_srcset("https://x.dev/p/", "a.png 1x, /b.png 2x"),
    "https://x.dev/p/a.png 1x, https://x.dev/b.png 2x",
);
// Commas inside URLs are not separators:
assert_eq!(
    resolve_srcset("https://x.dev/", "/c_fill,w_300/a.jpg 300w,/c_fill,w_600/a.jpg 600w"),
    "https://x.dev/c_fill,w_300/a.jpg 300w, https://x.dev/c_fill,w_600/a.jpg 600w",
);