Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix svg parsing in case it uses empty use tag or use with image href #7044

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this function returning on the others return early case? are we safe returning undefined?

Copy link
Contributor Author

@andrejslogins andrejslogins Apr 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no explicit returns are there in the following code, so it's safe

edit for clarity:

There are no other return statements in parseUseDirectives

Copy link
Contributor Author

@andrejslogins andrejslogins Apr 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only usage here (src/parser.js):

 fabric.parseSVGDocument = function(doc, callback, reviver, parsingOptions) {
    if (!doc) {
      return;
    }

    parseUseDirectives(doc);
    ..........
    ..........
    ..........

}

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)) {
Expand Down Expand Up @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions test/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,4 +802,35 @@
});
});

QUnit.test('parseSVGFromString with empty <use/>', function(assert) {
var done = assert.async();
var string =
'<svg viewBox="0 0 128 128" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' +
'<use/>' +
'<rect width="10" height="10" />' +
'</svg>';

fabric.loadSVGFromString(string, function(objects) {
assert.equal(objects[0].type, 'rect');
done();
});
});

QUnit.test('parseSVGFromString with <use/> having base64 image href', function(assert) {
var done = assert.async();
var string =
'<svg viewBox="0 0 128 128" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' +
'<defs>' +
'<image id="image" x="0" y="0" width="4346.7" height="4346.7" xlink:href="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"/>' +
'</defs>' +
'<use xlink:href="#image"/>' +
'<rect width="10" height="10" />' +
'</svg>';

fabric.loadSVGFromString(string, function(objects) {
assert.equal(objects[0].type, 'image');
done();
});
});

})();