source: trip-planner-front/node_modules/postcss-svgo/dist/index.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.3 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
9
10var _svgo = require("svgo");
11
12var _url = require("./lib/url");
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16const PLUGIN = 'postcss-svgo';
17const dataURI = /data:image\/svg\+xml(;((charset=)?utf-8|base64))?,/i;
18const dataURIBase64 = /data:image\/svg\+xml;base64,/i;
19/**
20 * @param {string} input the SVG string
21 * @param {boolean} encode whether to encode the result
22 * @return {object} the minification result
23 */
24
25function minifySVG(input, opts) {
26 let svg = input;
27 let decodedUri, isUriEncoded;
28
29 try {
30 decodedUri = (0, _url.decode)(input);
31 isUriEncoded = decodedUri !== input;
32 } catch (e) {
33 // Swallow exception if we cannot decode the value
34 isUriEncoded = false;
35 }
36
37 if (isUriEncoded) {
38 svg = decodedUri;
39 }
40
41 if (opts.encode !== undefined) {
42 isUriEncoded = opts.encode;
43 }
44
45 const result = (0, _svgo.optimize)(svg, opts);
46
47 if (result.error) {
48 throw new Error(result.error);
49 }
50
51 return {
52 result: result.data,
53 isUriEncoded
54 };
55}
56
57function minify(decl, opts, postcssResult) {
58 const parsed = (0, _postcssValueParser.default)(decl.value);
59 decl.value = parsed.walk(node => {
60 if (node.type !== 'function' || node.value.toLowerCase() !== 'url' || !node.nodes.length) {
61 return;
62 }
63
64 let {
65 value,
66 quote
67 } = node.nodes[0];
68 let optimizedValue;
69
70 try {
71 if (dataURIBase64.test(value)) {
72 const url = new URL(value);
73 const base64String = `${url.protocol}${url.pathname}`.replace(dataURI, '');
74 const svg = Buffer.from(base64String, 'base64').toString('utf8');
75 const {
76 result
77 } = minifySVG(svg, opts);
78 const data = Buffer.from(result).toString('base64');
79 optimizedValue = 'data:image/svg+xml;base64,' + data + url.hash;
80 } else if (dataURI.test(value)) {
81 const svg = value.replace(dataURI, '');
82 const {
83 result,
84 isUriEncoded
85 } = minifySVG(svg, opts);
86 let data = isUriEncoded ? (0, _url.encode)(result) : result; // Should always encode # otherwise we yield a broken SVG
87 // in Firefox (works in Chrome however). See this issue:
88 // https://github.com/cssnano/cssnano/issues/245
89
90 data = data.replace(/#/g, '%23');
91 optimizedValue = 'data:image/svg+xml;charset=utf-8,' + data;
92 quote = isUriEncoded ? '"' : "'";
93 } else {
94 return;
95 }
96 } catch (error) {
97 decl.warn(postcssResult, `${error}`);
98 return;
99 }
100
101 node.nodes[0] = Object.assign({}, node.nodes[0], {
102 value: optimizedValue,
103 quote: quote,
104 type: 'string',
105 before: '',
106 after: ''
107 });
108 return false;
109 });
110 decl.value = decl.value.toString();
111}
112
113function pluginCreator(opts = {}) {
114 return {
115 postcssPlugin: PLUGIN,
116
117 OnceExit(css, {
118 result
119 }) {
120 css.walkDecls(decl => {
121 if (!dataURI.test(decl.value)) {
122 return;
123 }
124
125 minify(decl, opts, result);
126 });
127 }
128
129 };
130}
131
132pluginCreator.postcss = true;
133var _default = pluginCreator;
134exports.default = _default;
135module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.