Skip to content

Commit

Permalink
Feature/add href parsing (#5156)
Browse files Browse the repository at this point in the history
* Add href support for parsing use directives (svg)

* Add href support for parsing use directives (svg)

* Add href support for parsing use directives (svg)

* Fix linting errors

* add xlink:href test

* Update parser.js
  • Loading branch information
DylanPaulusSEL authored and asturur committed Aug 12, 2018
1 parent 8c3f677 commit 87c994d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@

while (nodelist.length && i < nodelist.length) {
var el = nodelist[i],
xlink = el.getAttribute('xlink:href').substr(1),
xlink = (el.getAttribute('xlink:href') || el.getAttribute('href')).substr(1),
x = el.getAttribute('x') || 0,
y = el.getAttribute('y') || 0,
el2 = elementById(doc, xlink).cloneNode(true),
Expand All @@ -487,7 +487,8 @@

for (j = 0, attrs = el.attributes, len = attrs.length; j < len; j++) {
attr = attrs.item(j);
if (attr.nodeName === 'x' || attr.nodeName === 'y' || attr.nodeName === 'xlink:href') {
if (attr.nodeName === 'x' || attr.nodeName === 'y' ||
attr.nodeName === 'xlink:href' || attr.nodeName === 'href') {
continue;
}

Expand Down
34 changes: 34 additions & 0 deletions test/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,40 @@
});
});

QUnit.test('parseSVGFromString with xlink:href', function(assert) {
var done = assert.async();
var string = '<?xml version="1.0" standalone="no"?><svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' +
'<defs><rect id="myrect" width="300" height="100" style="fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)"/></defs>' +
'<use xlink:href="#myrect" x="50" y="50" ></use>' +
'</svg>',
rect;

assert.ok(fabric.loadSVGFromString);

fabric.loadSVGFromString(string, function(objects) {
rect = objects[0];
assert.ok(rect instanceof fabric.Rect);
done();
});
});

QUnit.test('parseSVGFromString with href', function(assert) {
var done = assert.async();
var string = '<?xml version="1.0" standalone="no"?><svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' +
'<defs><rect id="myrect" width="300" height="100" style="fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)"/></defs>' +
'<use href="#myrect" x="50" y="50" ></use>' +
'</svg>',
rect;

assert.ok(fabric.loadSVGFromString);

fabric.loadSVGFromString(string, function(objects) {
rect = objects[0];
assert.ok(rect instanceof fabric.Rect);
done();
});
});

QUnit.test('parseSVGFromString nested opacity', function(assert) {
var done = assert.async();
var string = '<?xml version="1.0" encoding="UTF-8"?>' +
Expand Down

0 comments on commit 87c994d

Please sign in to comment.