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 } } } ] } ```
91 lines
1.9 KiB
JavaScript
91 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
const { parseName } = require('../lib/svgo/tools.js');
|
|
|
|
exports.name = 'sortAttrs';
|
|
|
|
exports.type = 'perItem';
|
|
|
|
exports.active = false;
|
|
|
|
exports.description = 'sorts element attributes (disabled by default)';
|
|
|
|
exports.params = {
|
|
order: [
|
|
'id',
|
|
'width',
|
|
'height',
|
|
'x',
|
|
'x1',
|
|
'x2',
|
|
'y',
|
|
'y1',
|
|
'y2',
|
|
'cx',
|
|
'cy',
|
|
'r',
|
|
'fill',
|
|
'stroke',
|
|
'marker',
|
|
'd',
|
|
'points',
|
|
],
|
|
};
|
|
|
|
/**
|
|
* Sort element attributes for epic readability.
|
|
*
|
|
* @param {Object} item current iteration item
|
|
* @param {Object} params plugin params
|
|
*
|
|
* @author Nikolay Frantsev
|
|
*/
|
|
exports.fn = function (item, params) {
|
|
const orderlen = params.order.length + 1;
|
|
const xmlnsOrder = params.xmlnsOrder || 'front';
|
|
|
|
if (item.type === 'element') {
|
|
const attrs = Object.entries(item.attributes);
|
|
|
|
attrs.sort(([aName], [bName]) => {
|
|
const { prefix: aPrefix } = parseName(aName);
|
|
const { prefix: bPrefix } = parseName(bName);
|
|
if (aPrefix != bPrefix) {
|
|
// xmlns attributes implicitly have the prefix xmlns
|
|
if (xmlnsOrder == 'front') {
|
|
if (aPrefix === 'xmlns') return -1;
|
|
if (bPrefix === 'xmlns') return 1;
|
|
}
|
|
return aPrefix < bPrefix ? -1 : 1;
|
|
}
|
|
|
|
let aindex = orderlen;
|
|
let bindex = orderlen;
|
|
|
|
for (let i = 0; i < params.order.length; i++) {
|
|
if (aName == params.order[i]) {
|
|
aindex = i;
|
|
} else if (aName.indexOf(params.order[i] + '-') === 0) {
|
|
aindex = i + 0.5;
|
|
}
|
|
if (bName == params.order[i]) {
|
|
bindex = i;
|
|
} else if (bName.indexOf(params.order[i] + '-') === 0) {
|
|
bindex = i + 0.5;
|
|
}
|
|
}
|
|
|
|
if (aindex != bindex) {
|
|
return aindex - bindex;
|
|
}
|
|
return aName < bName ? -1 : 1;
|
|
});
|
|
|
|
const sorted = {};
|
|
for (const [name, value] of attrs) {
|
|
sorted[name] = value;
|
|
}
|
|
item.attributes = sorted;
|
|
}
|
|
};
|