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

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

primeNG components

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[6a3a178]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;
[e29cc2e]18const dataURIBase64 = /data:image\/svg\+xml;base64,/i; // the following regex will globally match:
19// \b([\w-]+) --> a word (a sequence of one or more [alphanumeric|underscore|dash] characters; followed by
20// \s*=\s* --> an equal sign character (=) between optional whitespaces; followed by
21// \\"([\S\s]+?)\\" --> any characters (including whitespaces and newlines) between literal escaped quotes (\")
22
23const escapedQuotes = /\b([\w-]+)\s*=\s*\\"([\S\s]+?)\\"/g;
[6a3a178]24/**
25 * @param {string} input the SVG string
26 * @param {boolean} encode whether to encode the result
27 * @return {object} the minification result
28 */
29
30function minifySVG(input, opts) {
31 let svg = input;
32 let decodedUri, isUriEncoded;
33
34 try {
35 decodedUri = (0, _url.decode)(input);
36 isUriEncoded = decodedUri !== input;
37 } catch (e) {
38 // Swallow exception if we cannot decode the value
39 isUriEncoded = false;
40 }
41
42 if (isUriEncoded) {
43 svg = decodedUri;
44 }
45
46 if (opts.encode !== undefined) {
47 isUriEncoded = opts.encode;
[e29cc2e]48 } // normalize all escaped quote characters from svg attributes
49 // from <svg attr=\"value\"... /> to <svg attr="value"... />
50 // see: https://github.com/cssnano/cssnano/issues/1194
51
[6a3a178]52
[e29cc2e]53 svg = svg.replace(escapedQuotes, '$1="$2"');
[6a3a178]54 const result = (0, _svgo.optimize)(svg, opts);
55
56 if (result.error) {
57 throw new Error(result.error);
58 }
59
60 return {
61 result: result.data,
62 isUriEncoded
63 };
64}
65
66function minify(decl, opts, postcssResult) {
67 const parsed = (0, _postcssValueParser.default)(decl.value);
68 decl.value = parsed.walk(node => {
69 if (node.type !== 'function' || node.value.toLowerCase() !== 'url' || !node.nodes.length) {
70 return;
71 }
72
73 let {
74 value,
75 quote
76 } = node.nodes[0];
77 let optimizedValue;
78
79 try {
80 if (dataURIBase64.test(value)) {
81 const url = new URL(value);
82 const base64String = `${url.protocol}${url.pathname}`.replace(dataURI, '');
83 const svg = Buffer.from(base64String, 'base64').toString('utf8');
84 const {
85 result
86 } = minifySVG(svg, opts);
87 const data = Buffer.from(result).toString('base64');
88 optimizedValue = 'data:image/svg+xml;base64,' + data + url.hash;
89 } else if (dataURI.test(value)) {
90 const svg = value.replace(dataURI, '');
91 const {
92 result,
93 isUriEncoded
94 } = minifySVG(svg, opts);
95 let data = isUriEncoded ? (0, _url.encode)(result) : result; // Should always encode # otherwise we yield a broken SVG
96 // in Firefox (works in Chrome however). See this issue:
97 // https://github.com/cssnano/cssnano/issues/245
98
99 data = data.replace(/#/g, '%23');
100 optimizedValue = 'data:image/svg+xml;charset=utf-8,' + data;
101 quote = isUriEncoded ? '"' : "'";
102 } else {
103 return;
104 }
105 } catch (error) {
106 decl.warn(postcssResult, `${error}`);
107 return;
108 }
109
110 node.nodes[0] = Object.assign({}, node.nodes[0], {
111 value: optimizedValue,
112 quote: quote,
113 type: 'string',
114 before: '',
115 after: ''
116 });
117 return false;
118 });
119 decl.value = decl.value.toString();
120}
121
122function pluginCreator(opts = {}) {
123 return {
124 postcssPlugin: PLUGIN,
125
126 OnceExit(css, {
127 result
128 }) {
129 css.walkDecls(decl => {
130 if (!dataURI.test(decl.value)) {
131 return;
132 }
133
134 minify(decl, opts, result);
135 });
136 }
137
138 };
139}
140
141pluginCreator.postcss = true;
142var _default = pluginCreator;
143exports.default = _default;
144module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.