diff --git a/.svgo.yml b/.svgo.yml index 43a15d5bd..eedb2ea1c 100644 --- a/.svgo.yml +++ b/.svgo.yml @@ -55,3 +55,11 @@ plugins: - removeAttrs - addClassesToSVGElement - removeStyleElement + +# configure the indent (default 4 spaces) used by `--pretty` here: +# +# @see https://github.com/svg/svgo/blob/master/lib/svgo/js2svg.js#L6 for more config options +# +# js2svg: +# pretty: true +# indent: ' ' diff --git a/lib/svgo/jsAPI.js b/lib/svgo/jsAPI.js index 6355b9023..bbc99fb4b 100644 --- a/lib/svgo/jsAPI.js +++ b/lib/svgo/jsAPI.js @@ -22,7 +22,7 @@ JSAPI.prototype.clone = function() { var nodeData = {}; Object.keys(node).forEach(function(key) { - if (key != 'content') { + if (key !== 'content') { nodeData[key] = node[key]; } }); @@ -72,7 +72,7 @@ JSAPI.prototype.isElem = function(param) { */ JSAPI.prototype.renameElem = function(name) { - if (typeof name == 'string' && name != '') + if (name && typeof name === 'string') this.elem = this.local = name; return this; @@ -198,16 +198,16 @@ JSAPI.prototype.renameElem = function(name) { /** * Add attribute. * - * @param {Object} attr attribute object - * @return {Object} created attribute + * @param {Object} [attr={}] attribute object + * @return {Object|Boolean} created attribute or false if no attr was passed in */ JSAPI.prototype.addAttr = function(attr) { + attr = attr || {}; - if (!attr || - (attr && attr.name === undefined) || - (attr && attr.value === undefined) || - (attr && attr.prefix === undefined) || - (attr && attr.local === undefined) + if (attr.name === undefined || + attr.value === undefined || + attr.prefix === undefined || + attr.local === undefined ) return false; this.attrs = this.attrs || {}; diff --git a/test/svg2js/_index.js b/test/svg2js/_index.js index b57afd917..d88a16f30 100644 --- a/test/svg2js/_index.js +++ b/test/svg2js/_index.js @@ -380,4 +380,73 @@ describe('svg2js', function() { }); + describe('malformed svg', function() { + + var filepath = PATH.resolve(__dirname, './test.bad.svg'), + root, + error; + + before(function(done) { + + FS.readFile(filepath, 'utf8', function(err, data) { + if (err) { + throw err; + } + + try { + SVG2JS(data, function(result) { + root = result; + }); + } catch (e) { + error = e; + } + + done(); + }); + + }); + + describe('root', function() { + + it('should have property "error"', function() { + return root.should.have.property('error'); + }); + + }); + + describe('root.error', function() { + + it('should be an instance of String', function() { + return root.error.should.an.instanceOf(String); + }); + + it('should be "Error in parsing: Unmatched closing tag: style"', function() { + return root.error.should.equal('Error in parsing: Unmatched closing tag: style'); + }); + + }); + + describe('thrown error', function() { + + it('should be an instance of Error', function() { + return error.should.be.an.instanceOf(Error); + }); + + it('should stringify to "Error: Unmatched closing tag: style"', function() { + return error.toString().should.equal('Error: Unmatched closing tag: style'); + }); + + it('should roughly match root.error', function() { + var rootErrorParts = root.error.split(':'), + thrownErrorParts = error.toString().split(':'); + + return rootErrorParts.reduce(function(result, value, index) { + return result && value.indexOf(thrownErrorParts[index]) === 0; + }, true).should.be.true(); + }); + + }); + + }); + });