Skip to main content

Module srcset

Module srcset 

Source
Expand description

Extract all image URLs from a srcset.

A srcset is a string containing a comma-separated list of one or more image candidate strings to be used when determining which image resource to present inside an <img> element.

Each image candidate string must begin with a valid URL referencing a non-interactive graphic resource. This is followed by whitespace and then a condition descriptor that indicates the circumstances in which the indicated image should be used. Space characters, other than the whitespace separating the URL and the corresponding condition descriptor, are ignored; this includes both leading and trailing space, as well as space before or after each comma.

Note: this handles cases where a URL contains a comma, which should be escaped, but is a valid character in a URL and occurs in the wild. Note: we cannot assume that commas within URLs are encoded as %2C, as they should be according to RFC 3986. Thus, the parsing process becomes significantly more complex and we need to use a state machine to keep track of the current state.

Enums§

State 🔒

Functions§

parse 🔒
Parse a srcset string into a list of URLs.
parse_one_url 🔒
Implements one iteration of the “splitting loop” from the reference algorithm. This is intended to be repeatedly called until the remaining string is empty.
skip_descriptor 🔒
Helper function to skip over a descriptor. Returns the string remaining after the descriptor (i.e. a string beginning after the next comma or an empty string).
split_at 🔒
Split an input string at the first character for which the predicate returns false.