Skip to content

Commit

Permalink
Merge pull request #475 from yandavid/master
Browse files Browse the repository at this point in the history
Added tests for svg2js behavior if passed in malformed svg
  • Loading branch information
GreLI committed Jan 4, 2016
2 parents 5c3cb26 + 1771cc1 commit bca9fed
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .svgo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ' '
18 changes: 9 additions & 9 deletions lib/svgo/jsAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
});
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 || {};
Expand Down
69 changes: 69 additions & 0 deletions test/svg2js/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

});

});

});

0 comments on commit bca9fed

Please sign in to comment.