Comparing version 3.0.0 to 3.0.1
67
index.js
'use strict'; | ||
const integerRegex = /^-?\d+$/; | ||
/** | ||
This regex represents a loose rule of an “image candidate string”. | ||
@see https://html.spec.whatwg.org/multipage/images.html#srcset-attribute | ||
An “image candidate string” roughly consists of the following: | ||
1. Zero or more whitespace characters. | ||
2. A non-empty URL that does not start or end with `,`. | ||
3. Zero or more whitespace characters. | ||
4. An optional “descriptor” that starts with a whitespace character. | ||
5. Zero or more whitespace characters. | ||
6. Each image candidate string is separated by a `,`. | ||
We intentionally implement a loose rule here so that we can perform more aggressive error handling and reporting in the below code. | ||
*/ | ||
const imageCandidateRegex = /\s*([^,]\S*[^,](?:\s+[^,]+)?)\s*(?:,|$)/; | ||
function deepUnique(array) { | ||
@@ -13,33 +28,35 @@ return array.sort().filter((element, index) => { | ||
return deepUnique( | ||
string.split(/,\s+/).map(part => { | ||
const result = {}; | ||
string.split(imageCandidateRegex) | ||
.filter((part, index) => index % 2 === 1) | ||
.map(part => { | ||
const [url, ...elements] = part.trim().split(/\s+/); | ||
part | ||
.trim() | ||
.split(/\s+/) | ||
.forEach((element, index) => { | ||
if (index === 0) { | ||
result.url = element; | ||
return; | ||
const result = {url}; | ||
const descriptors = elements.length > 0 ? elements : ['1x']; | ||
for (const descriptor of descriptors) { | ||
const postfix = descriptor[descriptor.length - 1]; | ||
const value = Number.parseFloat(descriptor.slice(0, -1)); | ||
if (Number.isNaN(value)) { | ||
throw new TypeError(`${descriptor.slice(0, -1)} is not a valid number`); | ||
} | ||
const value = element.slice(0, -1); | ||
const postfix = element[element.length - 1]; | ||
const integerValue = Number.parseInt(value, 10); | ||
const floatValue = Number.parseFloat(value); | ||
if (postfix === 'w' && integerRegex.test(value)) { | ||
if (integerValue <= 0) { | ||
if (postfix === 'w') { | ||
if (value <= 0) { | ||
throw new Error('Width descriptor must be greater than zero'); | ||
} else if (!Number.isInteger(value)) { | ||
throw new TypeError('Width descriptor must be an integer'); | ||
} | ||
result.width = integerValue; | ||
} else if (postfix === 'x' && !Number.isNaN(floatValue)) { | ||
if (floatValue <= 0) { | ||
result.width = value; | ||
} else if (postfix === 'x') { | ||
if (value <= 0) { | ||
throw new Error('Pixel density descriptor must be greater than zero'); | ||
} | ||
result.density = floatValue; | ||
result.density = value; | ||
} else { | ||
throw new Error(`Invalid srcset descriptor: ${element}`); | ||
throw new Error(`Invalid srcset descriptor: ${descriptor}`); | ||
} | ||
@@ -50,6 +67,6 @@ | ||
} | ||
}); | ||
} | ||
return result; | ||
}) | ||
return result; | ||
}) | ||
); | ||
@@ -56,0 +73,0 @@ }; |
{ | ||
"name": "srcset", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "Parse and stringify the HTML `<img>` srcset attribute", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -1,2 +0,2 @@ | ||
# srcset [![Build Status](https://travis-ci.com/sindresorhus/srcset.svg?branch=master)](https://travis-ci.com/github/sindresorhus/srcset) | ||
# srcset | ||
@@ -3,0 +3,0 @@ > Parse and stringify the HTML `<img>` [srcset](https://www.smashingmagazine.com/2013/08/webkit-implements-srcset-and-why-its-a-good-thing/) attribute. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7254
129