Implement PathItem.create(pathData)

determining if the data describes a plain path or a compound-path with multiple sub-paths.
This commit is contained in:
Jürg Lehni 2016-02-14 23:16:22 +01:00
parent 9f9222f416
commit 21033f7850
2 changed files with 19 additions and 10 deletions

View file

@ -27,6 +27,24 @@ var PathItem = Item.extend(/** @lends PathItem# */{
// Do nothing.
},
statics: /** @lends PathItem */{
/**
* Creates a path item from the given SVG path-data, determining if the
* data describes a plain path or a compound-path with multiple
* sub-paths.
*
* @param {String} pathData the SVG path-data to parse
* @return {Path|CompoundPath} the newly created path item
*/
create: function(pathData) {
// If there are multiple moveTo commands or a closePath command
// followed by other commands, we have a CompoundPath.
var ctor = (pathData && pathData.match(/m/gi) || []).length > 1
|| /z\s*\S+/i.test(pathData) ? CompoundPath : Path;
return new ctor(pathData);
}
},
_asPathItem: function() {
// See Item#_asPathItem()
return this;

View file

@ -135,16 +135,7 @@ new function() {
}
function importPath(node) {
// Get the path data, and determine whether it is a compound path or a
// normal path based on the amount of moveTo commands inside it.
var data = node.getAttribute('d'),
param = { pathData: data };
// If there are multiple moveTo commands or a closePath command followed
// by other commands, we have a CompoundPath:
// TODO: PathItem.create()?
return (data && data.match(/m/gi) || []).length > 1 || /z\b/i.test(data)
? new CompoundPath(param)
: new Path(param);
return PathItem.create(node.getAttribute('d'));
}
function importGradient(node, type) {