UNPKG

1.61 kBJavaScriptView Raw
1var shorterNames = require('./shorter-css-color-names');
2var REGEX = {
3 whitespace: /\s+/g,
4 urlHexPairs: /%[\dA-F]{2}/g,
5 quotes: /"/g,
6}
7
8function collapseWhitespace(str) {
9 return str.trim().replace(REGEX.whitespace, ' ');
10}
11
12function dataURIPayload(string) {
13 return encodeURIComponent(string)
14 .replace(REGEX.urlHexPairs, specialHexEncode);
15}
16
17// `#` gets converted to `%23`, so quite a few CSS named colors are shorter than
18// their equivalent URL-encoded hex codes.
19function 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
29function specialHexEncode(match) {
30 switch (match) { // Browsers tolerate these characters, and they're frequent
31 case '%20': return ' ';
32 case '%3D': return '=';
33 case '%3A': return ':';
34 case '%2F': return '/';
35 default: return match.toLowerCase(); // compresses better
36 }
37}
38
39function svgToTinyDataUri(svgString) {
40 if (typeof svgString !== 'string') {
41 throw new TypeError('Expected a string, but received ' + typeof svgString);
42 }
43 // Strip the Byte-Order Mark if the SVG has one
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
51svgToTinyDataUri.toSrcset = function toSrcset(svgString) {
52 return svgToTinyDataUri(svgString).replace(/ /g, '%20');
53}
54
55module.exports = svgToTinyDataUri;