WIP - add extractor, generate snippet_data
This commit is contained in:
63
node_modules/srcset/index.js
generated
vendored
Normal file
63
node_modules/srcset/index.js
generated
vendored
Normal file
@ -0,0 +1,63 @@
|
||||
'use strict';
|
||||
var numberIsNan = require('number-is-nan');
|
||||
var arrayUniq = require('array-uniq');
|
||||
var reInt = /^\d+$/;
|
||||
|
||||
function deepUnique(arr) {
|
||||
return arr.sort().filter(function (el, i) {
|
||||
return JSON.stringify(el) !== JSON.stringify(arr[i - 1]);
|
||||
});
|
||||
}
|
||||
|
||||
exports.parse = function (str) {
|
||||
return deepUnique(str.split(',').map(function (el) {
|
||||
var ret = {};
|
||||
|
||||
el.trim().split(/\s+/).forEach(function (el, i) {
|
||||
if (i === 0) {
|
||||
return ret.url = el;
|
||||
}
|
||||
|
||||
var value = el.substring(0, el.length - 1);
|
||||
var postfix = el[el.length - 1];
|
||||
var intVal = parseInt(value, 10);
|
||||
var floatVal = parseFloat(value);
|
||||
|
||||
if (postfix === 'w' && reInt.test(value)) {
|
||||
ret.width = intVal;
|
||||
} else if (postfix === 'h' && reInt.test(value)) {
|
||||
ret.height = intVal;
|
||||
} else if (postfix === 'x' && !numberIsNan(floatVal)) {
|
||||
ret.density = floatVal;
|
||||
} else {
|
||||
throw new Error('Invalid srcset descriptor: ' + el + '.');
|
||||
}
|
||||
});
|
||||
|
||||
return ret;
|
||||
}));
|
||||
}
|
||||
|
||||
exports.stringify = function (arr) {
|
||||
return arrayUniq(arr.map(function (el) {
|
||||
if (!el.url) {
|
||||
throw new Error('URL is required.');
|
||||
}
|
||||
|
||||
var ret = [el.url];
|
||||
|
||||
if (el.width) {
|
||||
ret.push(el.width + 'w');
|
||||
}
|
||||
|
||||
if (el.height) {
|
||||
ret.push(el.height + 'h');
|
||||
}
|
||||
|
||||
if (el.density) {
|
||||
ret.push(el.density + 'x');
|
||||
}
|
||||
|
||||
return ret.join(' ');
|
||||
})).join(', ');
|
||||
}
|
||||
21
node_modules/srcset/license
generated
vendored
Normal file
21
node_modules/srcset/license
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
73
node_modules/srcset/package.json
generated
vendored
Normal file
73
node_modules/srcset/package.json
generated
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
{
|
||||
"_from": "srcset@^1.0.0",
|
||||
"_id": "srcset@1.0.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-pWad4StC87HV6D7QPHEEb8SPQe8=",
|
||||
"_location": "/srcset",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "srcset@^1.0.0",
|
||||
"name": "srcset",
|
||||
"escapedName": "srcset",
|
||||
"rawSpec": "^1.0.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^1.0.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/sanitize-html"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/srcset/-/srcset-1.0.0.tgz",
|
||||
"_shasum": "a5669de12b42f3b1d5e83ed03c71046fc48f41ef",
|
||||
"_spec": "srcset@^1.0.0",
|
||||
"_where": "/Users/stefanfejes/Projects/30-seconds-of-python-code/node_modules/sanitize-html",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "sindresorhus.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/sindresorhus/srcset/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"array-uniq": "^1.0.2",
|
||||
"number-is-nan": "^1.0.0"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "Parse and stringify the HTML <img> srcset attribute",
|
||||
"devDependencies": {
|
||||
"mocha": "*"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"homepage": "https://github.com/sindresorhus/srcset#readme",
|
||||
"keywords": [
|
||||
"html",
|
||||
"attribute",
|
||||
"image",
|
||||
"img",
|
||||
"src",
|
||||
"parse",
|
||||
"stringify",
|
||||
"srcset",
|
||||
"responsive",
|
||||
"picture",
|
||||
"element"
|
||||
],
|
||||
"license": "MIT",
|
||||
"name": "srcset",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/sindresorhus/srcset.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
},
|
||||
"version": "1.0.0"
|
||||
}
|
||||
62
node_modules/srcset/readme.md
generated
vendored
Normal file
62
node_modules/srcset/readme.md
generated
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
# srcset [](https://travis-ci.org/sindresorhus/srcset)
|
||||
|
||||
> Parse and stringify the HTML `<img>` [srcset](http://mobile.smashingmagazine.com/2013/08/21/webkit-implements-srcset-and-why-its-a-good-thing/) attribute.
|
||||
|
||||
Useful if you're creating a polyfill, build-tool, etc.
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install --save srcset
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
How an image with `srcset` might look like:
|
||||
|
||||
```html
|
||||
<img alt="The Breakfast Combo"
|
||||
src="banner.jpg"
|
||||
srcset="banner-HD.jpg 2x, banner-phone.jpg 100w, banner-phone-HD.jpg 100w 2x">
|
||||
```
|
||||
|
||||
Then have some fun with it:
|
||||
|
||||
```js
|
||||
var srcset = require('srcset');
|
||||
|
||||
var parsed = srcset.parse('banner-HD.jpg 2x, banner-phone.jpg 100w');
|
||||
console.log(parsed);
|
||||
/*
|
||||
[
|
||||
{ url: 'banner-HD.jpg', density: 2 },
|
||||
{ url: 'banner-phone.jpg', width: 100 }
|
||||
]
|
||||
*/
|
||||
|
||||
parsed.push({ url: 'banner-phone-HD.jpg', width: 100, density: 2 });
|
||||
|
||||
var stringified = srcset.stringify(parsed);
|
||||
console.log(stringified);
|
||||
/*
|
||||
banner-HD.jpg 2x, banner-phone.jpg 100w, banner-phone-HD.jpg 100w 2x
|
||||
*/
|
||||
```
|
||||
|
||||
|
||||
## API
|
||||
|
||||
### .parse()
|
||||
|
||||
Accepts a srcset string and returns an array of objects with the possible properties: `url` (always), `width`, `height`, `density`.
|
||||
|
||||
### .stringify()
|
||||
|
||||
Accepts an array of objects with the possible properties: `url` (required), `width`, `height`, `density` and returns a srcset string.
|
||||
|
||||
|
||||
## License
|
||||
|
||||
MIT © [Sindre Sorhus](http://sindresorhus.com)
|
||||
Reference in New Issue
Block a user