From 27999a327cafecc94e9b1cbcf7feb718591f8f51 Mon Sep 17 00:00:00 2001 From: kangax Date: Thu, 4 Aug 2011 20:48:25 -0400 Subject: [PATCH] Add unit tests for `fabric.loadSVGFromString`. --- demos/kitchensink/demo.js | 4 ++++ demos/kitchensink/index.html | 39 +++++++++++++++++----------------- dist/all.js | 11 +++++++--- src/util/misc.js | 1 + test/unit/util.js | 41 ++++++++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 22 deletions(-) diff --git a/demos/kitchensink/demo.js b/demos/kitchensink/demo.js index 9ae98e8a..7d825e90 100644 --- a/demos/kitchensink/demo.js +++ b/demos/kitchensink/demo.js @@ -614,6 +614,10 @@ if (objects.length === 1) { canvas.add(objects[0]); } + else { + canvas.add(new fabric.PathGroup(objects)); + } + canvas.renderAll(); }); }; diff --git a/demos/kitchensink/index.html b/demos/kitchensink/index.html index 25745e90..483a992e 100644 --- a/demos/kitchensink/index.html +++ b/demos/kitchensink/index.html @@ -257,7 +257,26 @@ - + +
  • + + + + + +
  • +
  • -
  • - - - - - -
  • diff --git a/dist/all.js b/dist/all.js index e5e642f1..41b02006 100644 --- a/dist/all.js +++ b/dist/all.js @@ -1767,7 +1767,7 @@ fabric.Observable = { }; /** - * Takes url corresponding to an SVG document, and parses it to a set of objects + * Takes url corresponding to an SVG document, and parses it into a set of fabric objects * @method loadSVGFromURL * @param {String} url * @param {Function} callback @@ -1799,8 +1799,6 @@ fabric.Observable = { var doc = xml.documentElement; if (!doc) return; - console.log(doc); - fabric.parseSVGDocument(doc, function (results, options) { svgCache.set(url, { objects: fabric.util.array.invoke(results, 'toObject'), @@ -1826,7 +1824,14 @@ fabric.Observable = { return ({ objects: objects, options: options }); } + /** + * Takes string corresponding to an SVG document, and parses it into a set of fabric objects + * @method loadSVGFromString + * @param {String} string + * @param {Function} callback + */ function loadSVGFromString(string, callback) { + string = string.trim(); var doc; if (typeof DOMParser !== 'undefined') { var parser = new DOMParser(); diff --git a/src/util/misc.js b/src/util/misc.js index 6e4f0b9f..72c539e3 100644 --- a/src/util/misc.js +++ b/src/util/misc.js @@ -213,6 +213,7 @@ * @param {Function} callback */ function loadSVGFromString(string, callback) { + string = string.trim(); var doc; if (typeof DOMParser !== 'undefined') { var parser = new DOMParser(); diff --git a/test/unit/util.js b/test/unit/util.js index e6e4998f..c7b5f522 100644 --- a/test/unit/util.js +++ b/test/unit/util.js @@ -282,4 +282,45 @@ equals('none', el.userSelect); } }); + + test('fabric.loadSVGFromURL', function() { + equal("function", typeof fabric.loadSVGFromURL); + }); + + var SVG_DOC_AS_STRING = '\ + \ + \ + \ + '; + + asyncTest('fabric.loadSVGFromString', function() { + equal("function", typeof fabric.loadSVGFromString); + + var loadedObjects = [ ]; + fabric.loadSVGFromString(SVG_DOC_AS_STRING, function(objects) { + loadedObjects = objects; + }); + + setTimeout(function() { + ok(loadedObjects[0] instanceof fabric.Polygon); + equals('red', loadedObjects[0].fill); + start(); + }, 1000); + }); + + asyncTest('fabric.loadSVGFromString with surrounding whitespace', function() { + var loadedObjects = [ ]; + fabric.loadSVGFromString(' \n\n ' + SVG_DOC_AS_STRING + ' ', function(objects) { + loadedObjects = objects; + }); + + setTimeout(function() { + ok(loadedObjects[0] instanceof fabric.Polygon); + equals('red', loadedObjects[0] && loadedObjects[0].fill); + start(); + }, 1000); + }); + + })(); \ No newline at end of file