From 77862ef41cc7b8c3eb471f675457819e7f00556a Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Sun, 1 Apr 2018 20:39:49 +0200 Subject: [PATCH] fix the parsing of the URL part of a property. (#4881) * fix gradient parse url * added test * fix lint --- src/elements_parser.js | 6 ++++-- src/gradient.class.js | 1 - src/parser.js | 1 - test/unit/parser.js | 25 +++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/elements_parser.js b/src/elements_parser.js index 6be9d189748..7a212ba048c 100644 --- a/src/elements_parser.js +++ b/src/elements_parser.js @@ -5,6 +5,7 @@ fabric.ElementsParser = function(elements, callback, options, reviver, parsingOp this.reviver = reviver; this.svgUid = (options && options.svgUid) || 0; this.parsingOptions = parsingOptions; + this.regexUrl = /^url\(['"]?#([^'"]+)['"]?\)/g; }; fabric.ElementsParser.prototype.parse = function() { @@ -62,11 +63,12 @@ fabric.ElementsParser.prototype.createCallback = function(index, el) { fabric.ElementsParser.prototype.resolveGradient = function(obj, property) { - var instanceFillValue = obj.get(property); + var instanceFillValue = obj[property]; if (!(/^url\(/).test(instanceFillValue)) { return; } - var gradientId = instanceFillValue.slice(5, instanceFillValue.length - 1); + var gradientId = this.regexUrl.exec(instanceFillValue)[1]; + this.regexUrl.lastIndex = 0; if (fabric.gradientDefs[this.svgUid][gradientId]) { obj.set(property, fabric.Gradient.fromElement(fabric.gradientDefs[this.svgUid][gradientId], obj)); diff --git a/src/gradient.class.js b/src/gradient.class.js index a3bfa35efef..9a2bda25c39 100644 --- a/src/gradient.class.js +++ b/src/gradient.class.js @@ -309,7 +309,6 @@ * @see http://www.w3.org/TR/SVG/pservers.html#RadialGradientElement */ fromElement: function(el, instance) { - /** * @example: * diff --git a/src/parser.js b/src/parser.js index ba13f9e537d..5cf70afb938 100644 --- a/src/parser.js +++ b/src/parser.js @@ -743,7 +743,6 @@ elList = _getMultipleNodes(doc, tagArray), el, j = 0, id, xlink, gradientDefs = { }, idsToXlinkMap = { }; - j = elList.length; while (j--) { diff --git a/test/unit/parser.js b/test/unit/parser.js index 4b8cc705701..a90249b7f22 100644 --- a/test/unit/parser.js +++ b/test/unit/parser.js @@ -347,6 +347,31 @@ }); }); + QUnit.test('parseSVGFromString with gradient and fill url with quotes', function(assert) { + var done = assert.async(); + var string = '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + ''; + + fabric.loadSVGFromString(string, function(objects) { + assert.equal(objects[0].fill.type, 'linear', 'first path has gradient'); + assert.equal(objects[1].fill.type, 'linear', 'second path has gradient'); + assert.equal(objects[2].fill.type, 'linear', 'second path has gradient'); + done(); + }); + }); + QUnit.test('parseSVGFromString nested opacity', function(assert) { var done = assert.async(); var string = '' +