mirror of
https://github.com/svg/svgo.git
synced 2025-04-19 10:22:15 +03:00
I saw complaints about `extendDefaultPlugins` api - it cannot be used when svgo is installed globally - it requires svgo to be installed when using svgo-loader or svgo-jsx - it prevents using serializable config formats like json In this diff I introduced the new plugin which is a bundle of all default plugins. ```js module.exports = { plugins: [ 'preset_default', // or { name: 'preset_default', floatPrecision: 4, overrides: { convertPathData: { applyTransforms: false } } } ] } ```
68 lines
1.7 KiB
JavaScript
68 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
const { parseName } = require('../lib/svgo/tools.js');
|
|
const { editorNamespaces } = require('./_collections');
|
|
|
|
exports.name = 'removeEditorsNSData';
|
|
|
|
exports.type = 'perItem';
|
|
|
|
exports.active = true;
|
|
|
|
exports.description = 'removes editors namespaces, elements and attributes';
|
|
|
|
const prefixes = [];
|
|
|
|
exports.params = {
|
|
additionalNamespaces: [],
|
|
};
|
|
|
|
/**
|
|
* Remove editors namespaces, elements and attributes.
|
|
*
|
|
* @example
|
|
* <svg xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd">
|
|
* <sodipodi:namedview/>
|
|
* <path sodipodi:nodetypes="cccc"/>
|
|
*
|
|
* @param {Object} item current iteration item
|
|
* @param {Object} params plugin params
|
|
* @return {Boolean} if false, item will be filtered out
|
|
*
|
|
* @author Kir Belevich
|
|
*/
|
|
exports.fn = function (item, params) {
|
|
let namespaces = editorNamespaces;
|
|
if (Array.isArray(params.additionalNamespaces)) {
|
|
namespaces = [...editorNamespaces, ...params.additionalNamespaces];
|
|
}
|
|
|
|
if (item.type === 'element') {
|
|
if (item.isElem('svg')) {
|
|
for (const [name, value] of Object.entries(item.attributes)) {
|
|
const { prefix, local } = parseName(name);
|
|
if (prefix === 'xmlns' && namespaces.includes(value)) {
|
|
prefixes.push(local);
|
|
|
|
// <svg xmlns:sodipodi="">
|
|
delete item.attributes[name];
|
|
}
|
|
}
|
|
}
|
|
|
|
// <* sodipodi:*="">
|
|
for (const name of Object.keys(item.attributes)) {
|
|
const { prefix } = parseName(name);
|
|
if (prefixes.includes(prefix)) {
|
|
delete item.attributes[name];
|
|
}
|
|
}
|
|
|
|
// <sodipodi:*>
|
|
const { prefix } = parseName(item.name);
|
|
if (prefixes.includes(prefix)) {
|
|
return false;
|
|
}
|
|
}
|
|
};
|