Skip to main content

Crate parse_srcset

Crate parse_srcset 

Source
Expand description

§parse-srcset — parse an HTML srcset attribute

Parse the value of an <img srcset="…"> attribute into a list of image candidates, each with a URL and an optional density (x), width (w), or height (h) descriptor. A faithful Rust port of the parse-srcset npm package, which follows the WHATWG algorithm. Zero dependencies and #![no_std].

use parse_srcset::parse_srcset;

let c = parse_srcset("small.jpg 480w, large.jpg 800w, fallback.jpg");
assert_eq!(c.len(), 3);
assert_eq!(c[0].url, "small.jpg");
assert_eq!(c[0].width, Some(480));
assert_eq!(c[2].width, None);

let c = parse_srcset("a.png 1x, b.png 2x");
assert_eq!(c[1].density, Some(2.0));

A candidate whose descriptors are invalid (e.g. mixing w and x) is skipped, matching the reference implementation.

Structs§

ImageCandidate
One image candidate from a srcset attribute.

Functions§

parse_srcset
Parse a srcset attribute value into its image candidates.