From 21033f7850965cf185cd2753ba275bc6de88dec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sun, 14 Feb 2016 23:16:22 +0100 Subject: [PATCH] Implement PathItem.create(pathData) determining if the data describes a plain path or a compound-path with multiple sub-paths. --- src/path/PathItem.js | 18 ++++++++++++++++++ src/svg/SvgImport.js | 11 +---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/path/PathItem.js b/src/path/PathItem.js index b84a1e3c..e2de9408 100644 --- a/src/path/PathItem.js +++ b/src/path/PathItem.js @@ -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; diff --git a/src/svg/SvgImport.js b/src/svg/SvgImport.js index 1c6271a9..04d060a6 100644 --- a/src/svg/SvgImport.js +++ b/src/svg/SvgImport.js @@ -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) {