From eb9b7f45b93b446366db53eb0e4f5e02cefd78d2 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Sat, 21 May 2016 15:07:04 +0200 Subject: [PATCH] Add ID to svg export (#2993) --- src/mixins/object.svg_export.js | 8 ++++++++ src/shapes/circle.class.js | 2 +- src/shapes/ellipse.class.js | 2 +- src/shapes/group.class.js | 2 +- src/shapes/image.class.js | 2 +- src/shapes/line.class.js | 2 +- src/shapes/path.class.js | 2 +- src/shapes/path_group.class.js | 2 +- src/shapes/polygon.class.js | 2 +- src/shapes/rect.class.js | 2 +- src/shapes/text.class.js | 2 +- src/shapes/triangle.class.js | 2 +- test/unit/rect.js | 6 ++++++ 13 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/mixins/object.svg_export.js b/src/mixins/object.svg_export.js index 3689021bcae..bfea0eb036d 100644 --- a/src/mixins/object.svg_export.js +++ b/src/mixins/object.svg_export.js @@ -63,6 +63,14 @@ return this.shadow ? 'filter: url(#SVGID_' + this.shadow.id + ');' : ''; }, + /** + * Returns id attribute for svg output + * @return {String} + */ + getSvgId: function() { + return this.id ? 'id="' + this.id + '" ' : ''; + }, + /** * Returns transform-string for svg-export * @return {String} diff --git a/src/shapes/circle.class.js b/src/shapes/circle.class.js index e3b386b3c02..3363cc03b19 100644 --- a/src/shapes/circle.class.js +++ b/src/shapes/circle.class.js @@ -107,7 +107,7 @@ y = this.top + this.radius; } markup.push( - '\n', - '\n' diff --git a/src/shapes/polygon.class.js b/src/shapes/polygon.class.js index 518d043eeaf..a1a001bd662 100644 --- a/src/shapes/polygon.class.js +++ b/src/shapes/polygon.class.js @@ -118,7 +118,7 @@ addTransform = ' translate(' + (-this.pathOffset.x) + ', ' + (-this.pathOffset.y) + ') '; } markup.push( - '<', this.type, ' ', + '<', this.type, ' ', this.getSvgId(), 'points="', points.join(''), '" style="', this.getSvgStyles(), '" transform="', this.getSvgTransform(), addTransform, diff --git a/src/shapes/rect.class.js b/src/shapes/rect.class.js index 92b7a6f1ef2..d6efd2820a9 100644 --- a/src/shapes/rect.class.js +++ b/src/shapes/rect.class.js @@ -172,7 +172,7 @@ y = -this.height / 2; } markup.push( - '\n', textAndBg.textBgRects.join(''), '\t\t\n'); }); + test('toSVG with id', function() { + var rect = new fabric.Rect({id: 'myRect', width: 100, height: 100, strokeWidth: 0, fill: 'rgba(255, 0, 0, 0.5)' }); + var svg = rect.toSVG(); + equal(svg, '\n'); + }); + test('toSVG with alpha colors stroke', function() { var rect = new fabric.Rect({ width: 100, height: 100, strokeWidth: 0, fill: '', stroke: 'rgba(255, 0, 0, 0.5)' }); var svg = rect.toSVG();