1 | var shorterNames = require('./shorter-css-color-names');
|
2 | var REGEX = {
|
3 | whitespace: /\s+/g,
|
4 | urlHexPairs: /%[\dA-F]{2}/g,
|
5 | quotes: /"/g,
|
6 | }
|
7 |
|
8 | function collapseWhitespace(str) {
|
9 | return str.trim().replace(REGEX.whitespace, ' ');
|
10 | }
|
11 |
|
12 | function dataURIPayload(string) {
|
13 | return encodeURIComponent(string)
|
14 | .replace(REGEX.urlHexPairs, specialHexEncode);
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 | function colorCodeToShorterNames(string) {
|
20 | Object.keys(shorterNames).forEach(function(key) {
|
21 | if (shorterNames[key].test(string)) {
|
22 | string = string.replace(shorterNames[key], key);
|
23 | }
|
24 | });
|
25 |
|
26 | return string;
|
27 | }
|
28 |
|
29 | function specialHexEncode(match) {
|
30 | switch (match) {
|
31 | case '%20': return ' ';
|
32 | case '%3D': return '=';
|
33 | case '%3A': return ':';
|
34 | case '%2F': return '/';
|
35 | default: return match.toLowerCase();
|
36 | }
|
37 | }
|
38 |
|
39 | function svgToTinyDataUri(svgString) {
|
40 | if (typeof svgString !== 'string') {
|
41 | throw new TypeError('Expected a string, but received ' + typeof svgString);
|
42 | }
|
43 |
|
44 | if (svgString.charCodeAt(0) === 0xfeff) { svgString = svgString.slice(1) }
|
45 |
|
46 | var body = colorCodeToShorterNames(collapseWhitespace(svgString))
|
47 | .replace(REGEX.quotes, "'");
|
48 | return 'data:image/svg+xml,' + dataURIPayload(body);
|
49 | }
|
50 |
|
51 | svgToTinyDataUri.toSrcset = function toSrcset(svgString) {
|
52 | return svgToTinyDataUri(svgString).replace(/ /g, '%20');
|
53 | }
|
54 |
|
55 | module.exports = svgToTinyDataUri;
|