mirror of
https://github.com/svg/svgo.git
synced 2025-07-29 20:21:14 +03:00
new plugins/sortAttrs
This commit is contained in:
54
plugins/sortAttrs.js
Normal file
54
plugins/sortAttrs.js
Normal file
@ -0,0 +1,54 @@
|
||||
'use strict';
|
||||
|
||||
exports.type = 'perItem';
|
||||
|
||||
exports.active = true;
|
||||
|
||||
exports.params = {
|
||||
order: [
|
||||
'xmlns',
|
||||
'id',
|
||||
'width', 'height',
|
||||
'x', 'x1', 'x2',
|
||||
'y', 'y1', 'y2',
|
||||
'cx', 'cy', 'r',
|
||||
'fill', 'fill-opacity', 'fill-rule',
|
||||
'stroke', 'stroke-opacity', 'stroke-width', 'stroke-miterlimit', 'stroke-dashoffset',
|
||||
'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) {
|
||||
|
||||
var attrs = [],
|
||||
sorted = {},
|
||||
orderlen = params.order.length + 1;
|
||||
|
||||
if (item.elem) {
|
||||
|
||||
item.eachAttr(function(attr) {
|
||||
attrs.push(attr);
|
||||
});
|
||||
|
||||
attrs.sort(function(a, b) {
|
||||
return ((a = params.order.indexOf(a.name)) > -1 ? a : orderlen) -
|
||||
((b = params.order.indexOf(b.name)) > -1 ? b : orderlen);
|
||||
});
|
||||
|
||||
attrs.forEach(function (attr) {
|
||||
sorted[attr.name] = attr;
|
||||
});
|
||||
|
||||
item.attrs = sorted;
|
||||
|
||||
}
|
||||
|
||||
};
|
Reference in New Issue
Block a user