From cf7a6d1482648ad53bd6552e37dd73e5554aff1e Mon Sep 17 00:00:00 2001 From: Josh Pearce Date: Sun, 19 Aug 2012 15:44:34 -0400 Subject: [PATCH 1/2] Added reviver method in parser The optional method is used when asking fabric to load an SVG file and parse it, instantiating fabric objects. After each fabric object is created, reviver(ele, obj) is called so you can parse our custom attributes, or do anything else custom. --- src/parser.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/parser.js b/src/parser.js index 8af78f54..59727ed5 100644 --- a/src/parser.js +++ b/src/parser.js @@ -320,8 +320,9 @@ * @param {Array} elements Array of elements to parse * @param {Function} callback Being passed an array of fabric instances (transformed from SVG elements) * @param {Object} options Options object + * @param {Function} [reviver] Method for further pasring of SVG elements, called after each fabric object created. */ - function parseElements(elements, callback, options) { + function parseElements(elements, callback, options, reviver) { var instances = Array(elements.length), i = elements.length; function checkIfDone() { @@ -340,15 +341,18 @@ if (klass && klass.fromElement) { try { if (klass.async) { - klass.fromElement(el, (function(index) { + klass.fromElement(el, (function(index, el) { return function(obj) { + reviver && reviver(el, obj); instances.splice(index, 0, obj); checkIfDone(); }; })(index), options); } else { - instances.splice(index, 0, klass.fromElement(el, options)); + var obj = klass.fromElement(el, options); + reviver && reviver(el, obj); + instances.splice(index, 0, obj); checkIfDone(); } } @@ -438,6 +442,7 @@ * @method parseSVGDocument * @param {SVGDocument} doc SVG document to parse * @param {Function} callback Callback to call when parsing is finished; It's being passed an array of elements (parsed from a document). + * @param {Function} [reviver] Method for further pasring of SVG elements, called after each fabric object created. */ fabric.parseSVGDocument = (function() { @@ -467,7 +472,7 @@ return false; } - return function(doc, callback) { + return function(doc, callback, reviver) { if (!doc) return; var startTime = new Date(), @@ -525,7 +530,7 @@ if (callback) { callback(instances, options); } - }, clone(options)); + }, clone(options), reviver); }; })(); @@ -569,8 +574,9 @@ * @method loadSVGFromURL * @param {String} url * @param {Function} callback + * @param {Function} [reviver] Method for further pasring of SVG elements, called after each fabric object created. */ - function loadSVGFromURL(url, callback) { + function loadSVGFromURL(url, callback, reviver) { url = url.replace(/^\n\s*/, '').trim(); @@ -606,7 +612,7 @@ options: options }); callback(results, options); - }); + }, reviver); } } @@ -630,8 +636,9 @@ * @method loadSVGFromString * @param {String} string * @param {Function} callback + * @param {Function} [reviver] Method for further pasring of SVG elements, called after each fabric object created. */ - function loadSVGFromString(string, callback) { + function loadSVGFromString(string, callback, reviver) { string = string.trim(); var doc; if (typeof DOMParser !== 'undefined') { @@ -649,7 +656,7 @@ fabric.parseSVGDocument(doc.documentElement, function (results, options) { callback(results, options); - }); + }, reviver); } function createSVGFontFacesMarkup(objects) { From 5923e008f79d970acfc524016e3c0b403ac177cd Mon Sep 17 00:00:00 2001 From: joshpearce Date: Sun, 19 Aug 2012 21:36:37 -0300 Subject: [PATCH 2/2] Update src/parser.js --- src/parser.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/parser.js b/src/parser.js index 59727ed5..7c47fa9f 100644 --- a/src/parser.js +++ b/src/parser.js @@ -320,7 +320,7 @@ * @param {Array} elements Array of elements to parse * @param {Function} callback Being passed an array of fabric instances (transformed from SVG elements) * @param {Object} options Options object - * @param {Function} [reviver] Method for further pasring of SVG elements, called after each fabric object created. + * @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created. */ function parseElements(elements, callback, options, reviver) { var instances = Array(elements.length), i = elements.length; @@ -343,15 +343,15 @@ if (klass.async) { klass.fromElement(el, (function(index, el) { return function(obj) { - reviver && reviver(el, obj); + reviver && reviver(el, obj); instances.splice(index, 0, obj); checkIfDone(); }; })(index), options); } else { - var obj = klass.fromElement(el, options); - reviver && reviver(el, obj); + var obj = klass.fromElement(el, options); + reviver && reviver(el, obj); instances.splice(index, 0, obj); checkIfDone(); } @@ -442,7 +442,7 @@ * @method parseSVGDocument * @param {SVGDocument} doc SVG document to parse * @param {Function} callback Callback to call when parsing is finished; It's being passed an array of elements (parsed from a document). - * @param {Function} [reviver] Method for further pasring of SVG elements, called after each fabric object created. + * @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created. */ fabric.parseSVGDocument = (function() { @@ -574,7 +574,7 @@ * @method loadSVGFromURL * @param {String} url * @param {Function} callback - * @param {Function} [reviver] Method for further pasring of SVG elements, called after each fabric object created. + * @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created. */ function loadSVGFromURL(url, callback, reviver) { @@ -636,7 +636,7 @@ * @method loadSVGFromString * @param {String} string * @param {Function} callback - * @param {Function} [reviver] Method for further pasring of SVG elements, called after each fabric object created. + * @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created. */ function loadSVGFromString(string, callback, reviver) { string = string.trim();