Skip to content

Commit

Permalink
Parse XML entities.
Browse files Browse the repository at this point in the history
Fixes #225, fixes #272, fixes #314. Closes #476.
  • Loading branch information
GreLI committed Jan 4, 2016
1 parent bca9fed commit 0d91a04
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/svgo/svg2js.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var SAX = require('sax'),
JSAPI = require('./jsAPI');
JSAPI = require('./jsAPI'),
entityDeclaration = /<!ENTITY\s+(\S+)\s+(?:'([^\']+)'|"([^\"]+)")\s*>/g;

var config = {
strict: true,
Expand Down Expand Up @@ -42,6 +43,16 @@ module.exports = function(data, callback) {
doctype: doctype
});

var subsetStart = doctype.indexOf('['),
entityMatch;

if (subsetStart >= 0) {
entityDeclaration.lastIndex = subsetStart;

while ((entityMatch = entityDeclaration.exec(data)) != null) {
sax.ENTITIES[entityMatch[1]] = entityMatch[2] || entityMatch[3];
}
}
};

sax.onprocessinginstruction = function(data) {
Expand Down
29 changes: 29 additions & 0 deletions test/svg2js/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,33 @@ describe('svg2js', function() {

});

describe('entities', function() {
var filepath = PATH.resolve(__dirname, './test.entities.svg'),
root;

before(function(done) {
FS.readFile(filepath, 'utf8', function(err, data) {
if (err) throw err;

SVG2JS(data, function(result) {
root = result;
});
done();
});
});

describe('root', function() {
it('should exist', function() {
return root.should.exist;
});

it('should have correctly parsed entities', function() {
var attrs = root.content[root.content.length - 1].attrs;

attrs['xmlns:x'].value.should.be.equal('http://ns.adobe.com/Extensibility/1.0/');
attrs['xmlns:graph'].value.should.be.equal('http://ns.adobe.com/Graphs/1.0/');
});
});
});

});
8 changes: 8 additions & 0 deletions test/svg2js/test.entities.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0d91a04

Please sign in to comment.