postcss-svgo
Advanced tools
Comparing version 5.0.1 to 5.0.2
@@ -19,3 +19,40 @@ "use strict"; | ||
const dataURIBase64 = /data:image\/svg\+xml;base64,/i; | ||
/** | ||
* @param {string} input the SVG string | ||
* @param {boolean} encode whether to encode the result | ||
* @return {object} the minification result | ||
*/ | ||
function minifySVG(input, opts) { | ||
let svg = input; | ||
let decodedUri, isUriEncoded; | ||
try { | ||
decodedUri = (0, _url.decode)(input); | ||
isUriEncoded = decodedUri !== input; | ||
} catch (e) { | ||
// Swallow exception if we cannot decode the value | ||
isUriEncoded = false; | ||
} | ||
if (isUriEncoded) { | ||
svg = decodedUri; | ||
} | ||
if (opts.encode !== undefined) { | ||
isUriEncoded = opts.encode; | ||
} | ||
const result = (0, _svgo.optimize)(svg, opts); | ||
if (result.error) { | ||
throw new Error(result.error); | ||
} | ||
return { | ||
result: result.data, | ||
isUriEncoded | ||
}; | ||
} | ||
function minify(decl, opts, postcssResult) { | ||
@@ -32,41 +69,28 @@ const parsed = (0, _postcssValueParser.default)(decl.value); | ||
} = node.nodes[0]; | ||
let isBase64, isUriEncoded; | ||
const url = new URL(value); | ||
let svg = value.replace(dataURI, ''); | ||
let optimizedValue; | ||
if (dataURIBase64.test(value)) { | ||
let base64String = `${url.protocol}${url.pathname}`.replace(dataURI, ''); | ||
svg = Buffer.from(base64String, 'base64').toString('utf8'); | ||
isBase64 = true; | ||
} else { | ||
if (!dataURI.test(value)) { | ||
return; | ||
} | ||
let decodedUri; | ||
try { | ||
decodedUri = (0, _url.decode)(svg); | ||
isUriEncoded = decodedUri !== svg; | ||
} catch (e) { | ||
// Swallow exception if we cannot decode the value | ||
isUriEncoded = false; | ||
} | ||
if (isUriEncoded) { | ||
svg = decodedUri; | ||
} | ||
if (opts.encode !== undefined) { | ||
isUriEncoded = opts.encode; | ||
} | ||
} | ||
let result; | ||
try { | ||
result = (0, _svgo.optimize)(svg, opts); | ||
if (dataURIBase64.test(value)) { | ||
const url = new URL(value); | ||
const base64String = `${url.protocol}${url.pathname}`.replace(dataURI, ''); | ||
const svg = Buffer.from(base64String, 'base64').toString('utf8'); | ||
const { | ||
result | ||
} = minifySVG(svg, opts); | ||
const data = Buffer.from(result).toString('base64'); | ||
optimizedValue = 'data:image/svg+xml;base64,' + data + url.hash; | ||
} else if (dataURI.test(value)) { | ||
const svg = value.replace(dataURI, ''); | ||
const { | ||
result, | ||
isUriEncoded | ||
} = minifySVG(svg, opts); | ||
let data = isUriEncoded ? (0, _url.encode)(result) : result; // Should always encode # otherwise we yield a broken SVG | ||
// in Firefox (works in Chrome however). See this issue: | ||
// https://github.com/cssnano/cssnano/issues/245 | ||
if (result.error) { | ||
decl.warn(postcssResult, `${result.error}`); | ||
data = data.replace(/#/g, '%23'); | ||
optimizedValue = 'data:image/svg+xml;charset=utf-8,' + data; | ||
quote = isUriEncoded ? '"' : "'"; | ||
} else { | ||
return; | ||
@@ -79,17 +103,2 @@ } | ||
let data, optimizedValue; | ||
if (isBase64) { | ||
data = Buffer.from(result.data).toString('base64'); | ||
optimizedValue = 'data:image/svg+xml;base64,' + data + url.hash; | ||
} else { | ||
data = isUriEncoded ? (0, _url.encode)(result.data) : result.data; // Should always encode # otherwise we yield a broken SVG | ||
// in Firefox (works in Chrome however). See this issue: | ||
// https://github.com/cssnano/cssnano/issues/245 | ||
data = data.replace(/#/g, '%23'); | ||
optimizedValue = 'data:image/svg+xml;charset=utf-8,' + data; | ||
quote = isUriEncoded ? '"' : "'"; | ||
} | ||
node.nodes[0] = Object.assign({}, node.nodes[0], { | ||
@@ -96,0 +105,0 @@ value: optimizedValue, |
{ | ||
"name": "postcss-svgo", | ||
"version": "5.0.1", | ||
"version": "5.0.2", | ||
"description": "Optimise inline SVG with PostCSS.", | ||
@@ -48,3 +48,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "28c247175032fa03f04911cde56ad82d74d211cc" | ||
"gitHead": "9b3c54fd94f3e2bdb503d1e21f171d7fe02f33ca" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
121
8973
5