From 75c40babc9662e1299cbc05459eeb6ebd34c85c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sun, 14 Feb 2016 15:12:52 +0100 Subject: [PATCH] SvgImport: Implement support for onLoad and onError callbacks on string input. Closes #827 --- src/svg/SvgImport.js | 45 +++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/svg/SvgImport.js b/src/svg/SvgImport.js index 1bbf6e17..84ba1369 100644 --- a/src/svg/SvgImport.js +++ b/src/svg/SvgImport.js @@ -640,22 +640,32 @@ new function() { return null; options = typeof options === 'function' ? { onLoad: options } : options || {}; - var node = source, - // Remember current scope so we can restore it in onLoad. - scope = paper; + // Remember current scope so we can restore it in onLoad. + var scope = paper, + item = null; function onLoad(svg) { - paper = scope; - var item = importSVG(svg, options, true), - onLoad = options.onLoad; - if (onLoad) - onLoad.call(this, item, svg); + try { + var node = typeof svg === 'object' ? svg : new window.DOMParser() + .parseFromString(svg, 'image/svg+xml'); + if (!node.nodeName) { + node = null; + throw new Error('Unsupported SVG source: ' + source); + } + paper = scope; + item = importNode(node, options, true); + var onLoad = options.onLoad; + if (onLoad) + onLoad(item, svg); + } catch (e) { + onError(e); + } } function onError(message, status) { var onError = options.onError; if (onError) { - onError.call(this, message, status); + onError(message, status); } else { throw new Error(message); } @@ -667,15 +677,16 @@ new function() { if (typeof source === 'string' && !/^.*