From fd9726a26a384a6d50a953ddf15d2750891e5cd1 Mon Sep 17 00:00:00 2001 From: Andrejs Logins Date: Tue, 27 Apr 2021 08:54:28 +0300 Subject: [PATCH] Fix svg parsing in case it uses empty use tag or use with image href --- src/parser.js | 17 ++++++++++++++--- test/unit/parser.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/parser.js b/src/parser.js index f87084141cb..c4144da251a 100644 --- a/src/parser.js +++ b/src/parser.js @@ -471,12 +471,23 @@ var nodelist = _getMultipleNodes(doc, ['use', 'svg:use']), i = 0; while (nodelist.length && i < nodelist.length) { var el = nodelist[i], - xlink = (el.getAttribute('xlink:href') || el.getAttribute('href')).substr(1), + xlinkAttribute = el.getAttribute('xlink:href') || el.getAttribute('href'); + + if (xlinkAttribute === null) { + return; + } + + var xlink = xlinkAttribute.substr(1), x = el.getAttribute('x') || 0, y = el.getAttribute('y') || 0, el2 = elementById(doc, xlink).cloneNode(true), currentTrans = (el2.getAttribute('transform') || '') + ' translate(' + x + ', ' + y + ')', - parentNode, oldLength = nodelist.length, attr, j, attrs, len, namespace = fabric.svgNS; + parentNode, + oldLength = nodelist.length, attr, + j, + attrs, + len, + namespace = fabric.svgNS; applyViewboxTransform(el2); if (/^svg$/i.test(el2.nodeName)) { @@ -558,7 +569,7 @@ parsedDim.toBeParsed = toBeParsed; if (missingViewBox) { - if (((x || y) && element.parentNode.nodeName !== '#document')) { + if (((x || y) && element.parentNode && element.parentNode.nodeName !== '#document')) { translateMatrix = ' translate(' + parseUnit(x) + ' ' + parseUnit(y) + ') '; matrix = (element.getAttribute('transform') || '') + translateMatrix; element.setAttribute('transform', matrix); diff --git a/test/unit/parser.js b/test/unit/parser.js index 4daf2d34590..d14d072e2ea 100644 --- a/test/unit/parser.js +++ b/test/unit/parser.js @@ -802,4 +802,35 @@ }); }); + QUnit.test('parseSVGFromString with empty ', function(assert) { + var done = assert.async(); + var string = + '' + + '' + + '' + + ''; + + fabric.loadSVGFromString(string, function(objects) { + assert.equal(objects[0].type, 'rect'); + done(); + }); + }); + + QUnit.test('parseSVGFromString with having base64 image href', function(assert) { + var done = assert.async(); + var string = + '' + + '' + + '' + + '' + + '' + + '' + + ''; + + fabric.loadSVGFromString(string, function(objects) { + assert.equal(objects[0].type, 'image'); + done(); + }); + }); + })();