mirror of
https://github.com/svg/svgo.git
synced 2025-07-29 20:21:14 +03:00
plugins/mergePaths: new plugin
This commit is contained in:
45
plugins/mergePaths.js
Normal file
45
plugins/mergePaths.js
Normal file
@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
exports.type = 'perItem';
|
||||
|
||||
exports.active = true;
|
||||
|
||||
/**
|
||||
* Merge multiple Paths into one.
|
||||
*
|
||||
* @param {Object} item current iteration item
|
||||
* @return {Boolean} if false, item will be filtered out
|
||||
*
|
||||
* @author Kir Belevich
|
||||
*/
|
||||
exports.fn = function(item) {
|
||||
|
||||
if (item.isElem() && !item.isEmpty()) {
|
||||
|
||||
var prevContentItem;
|
||||
|
||||
item.content = item.content.filter(function(contentItem) {
|
||||
|
||||
// merge only <path d="...z" />
|
||||
if (prevContentItem &&
|
||||
prevContentItem.isElem('path') &&
|
||||
prevContentItem.hasAttr('d') &&
|
||||
Object.keys(prevContentItem.attrs).length === 1 &&
|
||||
prevContentItem.attr('d').value.charAt(prevContentItem.attr('d').value.length - 1) === 'z' &&
|
||||
contentItem.isElem('path') &&
|
||||
contentItem.hasAttr('d') &&
|
||||
Object.keys(contentItem.attrs).length === 1
|
||||
) {
|
||||
prevContentItem.attr('d').value += contentItem.attr('d').value;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
prevContentItem = contentItem;
|
||||
|
||||
return true;
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
};
|
Reference in New Issue
Block a user