diff --git a/lib/Viewer.js b/lib/Viewer.js index b342e68b..4ad8e1d5 100644 --- a/lib/Viewer.js +++ b/lib/Viewer.js @@ -227,27 +227,33 @@ Viewer.prototype.saveSVG = function(options, done) { options = {}; } - var canvas = this.get('canvas'); + var svg, err; - var contentNode = canvas.getDefaultLayer(), - defsNode = domQuery('defs', canvas._svg); + try { + var canvas = this.get('canvas'); - var contents = innerSVG(contentNode), - defs = defsNode ? '' + innerSVG(defsNode) + '' : ''; + var contentNode = canvas.getDefaultLayer(), + defsNode = domQuery('defs', canvas._svg); - var bbox = contentNode.getBBox(); + var contents = innerSVG(contentNode), + defs = defsNode ? '' + innerSVG(defsNode) + '' : ''; - var svg = - '\n' + - '\n' + - '\n' + - '' + - defs + contents + - ''; + var bbox = contentNode.getBBox(); - done(null, svg); + svg = + '\n' + + '\n' + + '\n' + + '' + + defs + contents + + ''; + } catch (e) { + err = e; + } + + done(err, svg); }; /** @@ -292,12 +298,8 @@ Viewer.prototype.saveSVG = function(options, done) { Viewer.prototype.importDefinitions = function(definitions, done) { - var err; - - // use try/catch to not swallow synchronous exceptions - // that may be raised during model parsing + // catch synchronous exceptions during #clear() try { - if (this._definitions) { // clear existing rendered diagram this.clear(); @@ -305,15 +307,12 @@ Viewer.prototype.importDefinitions = function(definitions, done) { // update definitions this._definitions = definitions; - - // perform graphical import - return importBpmnDiagram(this, definitions, done); } catch (e) { - err = e; + return done(e); } - // handle synchronous errors - return done(err); + // perform graphical import + return importBpmnDiagram(this, definitions, done); }; Viewer.prototype.getModules = function() { diff --git a/lib/import/Importer.js b/lib/import/Importer.js index 9f27ec99..13daee7e 100644 --- a/lib/import/Importer.js +++ b/lib/import/Importer.js @@ -14,9 +14,9 @@ var BpmnTreeWalker = require('./BpmnTreeWalker'); */ function importBpmnDiagram(diagram, definitions, done) { - var importer = diagram.get('bpmnImporter'), - eventBus = diagram.get('eventBus'), - translate = diagram.get('translate'); + var importer, + eventBus, + translate; var error, warnings = []; @@ -51,19 +51,23 @@ function importBpmnDiagram(diagram, definitions, done) { walker.handleDefinitions(definitions); } - eventBus.fire('import.render.start', { definitions: definitions }); - try { + importer = diagram.get('bpmnImporter'); + eventBus = diagram.get('eventBus'); + translate = diagram.get('translate'); + + eventBus.fire('import.render.start', { definitions: definitions }); + render(definitions); + + eventBus.fire('import.render.complete', { + error: error, + warnings: warnings + }); } catch (e) { error = e; } - eventBus.fire('import.render.complete', { - error: error, - warnings: warnings - }); - done(error, warnings); } diff --git a/test/spec/ModelerSpec.js b/test/spec/ModelerSpec.js index 94c3682f..b473dd9a 100644 --- a/test/spec/ModelerSpec.js +++ b/test/spec/ModelerSpec.js @@ -72,18 +72,10 @@ describe('Modeler', function() { // given createModeler(xml, function(err, warnings, modeler) { - if (err) { - return done(err); - } - // when // mimic re-import of same diagram modeler.importXML(xml, function(err, warnings) { - if (err) { - return done(err); - } - // then expect(warnings.length).to.equal(0); diff --git a/test/spec/ViewerSpec.js b/test/spec/ViewerSpec.js index 86a243df..944974d8 100644 --- a/test/spec/ViewerSpec.js +++ b/test/spec/ViewerSpec.js @@ -54,18 +54,10 @@ describe('Viewer', function() { // given createViewer(xml, function(err, warnings, viewer) { - if (err) { - return done(err); - } - // when // mimic re-import of same diagram viewer.importXML(xml, function(err, warnings) { - if (err) { - return done(err); - } - // then expect(warnings.length).to.equal(0); @@ -343,17 +335,9 @@ describe('Viewer', function() { createViewer(xml, function(err, warnings, viewer) { - if (err) { - return done(err); - } - // when viewer.saveXML({ format: true }, function(err, xml) { - if (err) { - return done(err); - } - // then expect(xml).to.contain(''); expect(xml).to.contain('' result = result.replace(/>\s+/g,'>'); @@ -262,7 +258,7 @@ describe('label bounds', function() { expect(width).to.equal(shape.label.width); expect(height).to.equal(shape.label.height); - done(); + done(err); }); }); }); @@ -289,10 +285,6 @@ describe('label bounds', function() { modeler.saveXML({ format: true }, function(err, result) { - if (err) { - return done(err); - } - // strip spaces and line breaks after '>' result = result.replace(/>\s+/g,'>'); @@ -305,7 +297,7 @@ describe('label bounds', function() { expect(width).to.equal(shape.label.width); expect(height).to.equal(shape.label.height); - done(); + done(err); }); }); }); @@ -332,7 +324,7 @@ describe('label bounds', function() { expect(result).to.equal(xml); - done(); + done(err); }); }); }); diff --git a/test/spec/import/ImporterSpec.js b/test/spec/import/ImporterSpec.js index 4efc5ddb..63b13efd 100644 --- a/test/spec/import/ImporterSpec.js +++ b/test/spec/import/ImporterSpec.js @@ -7,7 +7,7 @@ var TestContainer = require('mocha-test-container-support'); var Diagram = require('diagram-js/lib/Diagram'), BpmnModdle = require('bpmn-moddle'), - Importer = require('../../../lib/import/Importer'), + importBpmnDiagram = require('../../../lib/import/Importer').importBpmnDiagram, Viewer = require('../../../lib/Viewer'); var find = require('lodash/collection/find'); @@ -34,11 +34,7 @@ describe('import - Importer', function() { var moddle = new BpmnModdle(); moddle.fromXML(xml, function(err, definitions) { - if (err) { - return done(err); - } - - Importer.importBpmnDiagram(diagram, definitions, done); + importBpmnDiagram(diagram, definitions, done); }); } diff --git a/test/spec/import/elements/AssociationSpec.js b/test/spec/import/elements/AssociationSpec.js index d3d919cf..5eb4ad36 100644 --- a/test/spec/import/elements/AssociationSpec.js +++ b/test/spec/import/elements/AssociationSpec.js @@ -16,10 +16,6 @@ describe('import - associations', function() { // given bootstrapViewer(xml)(function(err) { - if (err) { - return done(err); - } - // when inject(function(elementRegistry) { @@ -28,7 +24,7 @@ describe('import - associations', function() { // then expect(association).to.exist; - done(); + done(err); })(); }); @@ -42,10 +38,6 @@ describe('import - associations', function() { // given bootstrapViewer(xml)(function(err) { - if (err) { - return done(err); - } - // when inject(function(elementRegistry) { @@ -90,17 +82,13 @@ describe('import - associations', function() { // given bootstrapViewer(xml)(function(err) { - if (err) { - return done(err); - } - // then expectRendered([ 'DataInputAssociation', 'DataOutputAssociation' ]); - done(); + done(err); }); }); @@ -112,17 +100,13 @@ describe('import - associations', function() { // given bootstrapViewer(xml)(function(err) { - if (err) { - return done(err); - } - // then expectRendered([ 'DataInputAssociation', 'DataOutputAssociation' ]); - done(); + done(err); }); }); @@ -134,18 +118,13 @@ describe('import - associations', function() { // given bootstrapViewer(xml)(function(err) { - if (err) { - return done(err); - } - // then expectRendered([ 'DataInputAssociation', 'DataOutputAssociation' ]); - done(); - + done(err); }); }); @@ -157,17 +136,13 @@ describe('import - associations', function() { // given bootstrapViewer(xml)(function(err) { - if (err) { - return done(err); - } - // then expectRendered([ 'DataInputAssociation', 'DataOutputAssociation' ]); - done(); + done(err); }); }); diff --git a/test/spec/import/elements/LabelSpec.js b/test/spec/import/elements/LabelSpec.js index c7160b26..ae631d3d 100644 --- a/test/spec/import/elements/LabelSpec.js +++ b/test/spec/import/elements/LabelSpec.js @@ -44,10 +44,6 @@ describe('import - labels', function() { // given bootstrapViewer(xml)(function(err) { - if (err) { - return done(err); - } - // when inject(function(elementRegistry) { @@ -68,7 +64,7 @@ describe('import - labels', function() { expect(sequenceFlowLabel.width).to.be.above(64); expect(sequenceFlowLabel.height).to.be.above(11); - done(); + done(err); })(); });