mirror of
https://github.com/svg/svgo.git
synced 2025-07-31 07:44:22 +03:00
new plugins/sortAttrs
This commit is contained in:
@ -42,3 +42,4 @@ plugins:
|
|||||||
- cleanupIDs
|
- cleanupIDs
|
||||||
- removeUnusedNS
|
- removeUnusedNS
|
||||||
- transformsWithOnePath
|
- transformsWithOnePath
|
||||||
|
- sortAttrs
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
9
test/plugins/sortAttrs.01.svg
Normal file
9
test/plugins/sortAttrs.01.svg
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<svg foo="bar" xmlns="http://www.w3.org/2000/svg" height="10" baz="quux" width="10" hello="world">
|
||||||
|
test
|
||||||
|
</svg>
|
||||||
|
|
||||||
|
@@@
|
||||||
|
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" foo="bar" baz="quux" hello="world">
|
||||||
|
test
|
||||||
|
</svg>
|
After Width: | Height: | Size: 236 B |
Reference in New Issue
Block a user