HTML5 Srcset Attribute Parser

1.0.2 · abandoned · verified Sun Apr 19

parse-srcset is a JavaScript parser for the HTML5 `srcset` attribute, designed to be spec-conformant with the WHATWG reference algorithm. It takes a `srcset` string as input and returns an array of objects, each representing a potential image source with properties like `url`, `width`, `height`, and `density`. The package is at version 1.0.2, with its last known update around 10 years ago, indicating it is no longer actively maintained. While it provides a foundational implementation for `srcset` parsing, developers seeking current specifications, ongoing support, or additional features like stringification should consider more recently updated alternatives. Its release cadence was sporadic, reflecting its early development and subsequent abandonment, distinguishing it from modern, regularly updated libraries in this domain. The library relies on an extensive test suite to ensure conformance to the W3C srcset checker, a strong point at the time of its development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a standard HTML `srcset` string using `require()` and then iterates through the resulting array of image source objects, logging their URLs and descriptors (density or width).

const parseSrcset = require('parse-srcset');

const srcsetString = 'elva-fairy-320w.jpg, elva-fairy-480w.jpg 1.5x, elva-fairy-640w.jpg 2x';
const parsed = parseSrcset(srcsetString);

console.log('Original srcset:', srcsetString);
console.log('Parsed output:');
parsed.forEach(item => {
  console.log(`- URL: ${item.url}, Descriptor: ${item.density || (item.width ? item.width + 'w' : '')}`);
});

// Example with width descriptors (though this old version might interpret differently)
const widthSrcsetString = 'image-160.jpg 160w, image-320.jpg 320w, image-640.jpg 640w';
const parsedWidth = parseSrcset(widthSrcsetString);

console.log('\nOriginal srcset (width descriptors):', widthSrcsetString);
console.log('Parsed output (width descriptors):');
parsedWidth.forEach(item => {
  console.log(`- URL: ${item.url}, Descriptor: ${item.width ? item.width + 'w' : item.density}`);
});

view raw JSON →