fix(Viewer): correctly export SVG

Closes #26
This commit is contained in:
Nico Rehwaldt 2014-04-03 21:20:13 +02:00
parent bd6aa53662
commit 6737d90b92
2 changed files with 52 additions and 8 deletions

View File

@ -56,6 +56,8 @@ var SVG_HEADER =
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ' + '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ' +
'version="1.1" baseProfile="basic">\n'; 'version="1.1" baseProfile="basic">\n';
var SVG_FOOTER = '</svg>';
Viewer.prototype.saveSVG = function(options, done) { Viewer.prototype.saveSVG = function(options, done) {
if (!done) { if (!done) {
done = options; done = options;
@ -66,9 +68,11 @@ Viewer.prototype.saveSVG = function(options, done) {
return done(new Error('no definitions loaded')); return done(new Error('no definitions loaded'));
} }
var svg = this.container.innerHTML; var svgElement = this.diagram.get('canvas').getContext();
svg = svg.replace(/<svg[^>]+>/, SVG_HEADER); var svg = svgElement.node.innerHTML;
svg = SVG_HEADER + svg + SVG_FOOTER;
done(null, svg); done(null, svg);
}; };

View File

@ -53,18 +53,58 @@ describe('Viewer', function() {
}); });
it('should handle errors', function(done) { describe('error handling', function() {
var xml = 'invalid stuff'; it('should handle errors', function(done) {
var renderer = new Viewer(container); var xml = 'invalid stuff';
renderer.importXML(xml, function(err) { var renderer = new Viewer(container);
expect(err).toBeDefined(); renderer.importXML(xml, function(err) {
done(); expect(err).toBeDefined();
done();
});
}); });
});
describe('export', function() {
it('should export svg', function(done) {
// given
var xml = fs.readFileSync('test/fixtures/bpmn/empty-definitions.bpmn', 'utf8');
var renderer = new Viewer(container);
renderer.importXML(xml, function(err) {
if (err) {
return done(err);
}
// when
renderer.saveSVG(function(err, svg) {
if (err) {
return done(err);
}
var expectedStart = '<?xml version="1.0" encoding="utf-8"?>';
var expectedEnd = '</svg>';
// then
expect(svg.indexOf(expectedStart)).toEqual(0);
expect(svg.indexOf(expectedEnd)).toEqual(svg.length - expectedEnd.length);
done();
});
});
});
}); });
}); });