From ef425d18c5bae16c8c60132d53143b3eb84a31ca Mon Sep 17 00:00:00 2001 From: kangax Date: Fri, 11 Feb 2011 01:44:08 -0500 Subject: [PATCH] Fix some of the parser.js unit tests by making `resolveGradients` more lenient. --- dist/all.js | 12 +++++------- src/parser.js | 23 ++++++++++------------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/dist/all.js b/dist/all.js index 84d013e8..8648847f 100644 --- a/dist/all.js +++ b/dist/all.js @@ -2744,7 +2744,10 @@ fabric.util.animate = animate; }; function resolveGradients(instances) { - var ctx = fabric.Element.activeInstance.getContext(); + var activeInstance = fabric.Element.activeInstance, + ctx = activeInstance ? activeInstance.getContext() : null; + + if (!ctx) return; for (var i = instances.length; i--; ) { var instanceFillValue = instances[i].get('fill'); @@ -2755,12 +2758,7 @@ fabric.util.animate = animate; if (fabric.gradientDefs[gradientId]) { instances[i].set('fill', - fabric.Gradient.fromElement( - fabric.gradientDefs[gradientId], - ctx, - instances[i] - ) - ); + fabric.Gradient.fromElement(fabric.gradientDefs[gradientId], ctx, instances[i])); } } } diff --git a/src/parser.js b/src/parser.js index 46927e90..9f44edd9 100644 --- a/src/parser.js +++ b/src/parser.js @@ -287,24 +287,21 @@ }; function resolveGradients(instances) { - var ctx = fabric.Element.activeInstance.getContext(); - + var activeInstance = fabric.Element.activeInstance, + ctx = activeInstance ? activeInstance.getContext() : null; + + if (!ctx) return; + for (var i = instances.length; i--; ) { var instanceFillValue = instances[i].get('fill'); - + if (/^url\(/.test(instanceFillValue)) { - - // url(#grad1) --> grad1 + var gradientId = instanceFillValue.slice(5, instanceFillValue.length - 1); - + if (fabric.gradientDefs[gradientId]) { - instances[i].set('fill', - fabric.Gradient.fromElement( - fabric.gradientDefs[gradientId], - ctx, - instances[i] - ) - ); + instances[i].set('fill', + fabric.Gradient.fromElement(fabric.gradientDefs[gradientId], ctx, instances[i])); } } }