From 0d91a0455de462d923b97e96be343c7ddb03e758 Mon Sep 17 00:00:00 2001 From: GreLI Date: Mon, 4 Jan 2016 19:57:13 +0300 Subject: [PATCH] Parse XML entities. Fixes #225, fixes #272, fixes #314. Closes #476. --- lib/svgo/svg2js.js | 13 ++++++++++++- test/svg2js/_index.js | 29 +++++++++++++++++++++++++++++ test/svg2js/test.entities.svg | 8 ++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 test/svg2js/test.entities.svg diff --git a/lib/svgo/svg2js.js b/lib/svgo/svg2js.js index b9709a069..9b0746028 100644 --- a/lib/svgo/svg2js.js +++ b/lib/svgo/svg2js.js @@ -1,7 +1,8 @@ 'use strict'; var SAX = require('sax'), - JSAPI = require('./jsAPI'); + JSAPI = require('./jsAPI'), + entityDeclaration = //g; var config = { strict: true, @@ -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) { diff --git a/test/svg2js/_index.js b/test/svg2js/_index.js index d88a16f30..fcc88a9c5 100644 --- a/test/svg2js/_index.js +++ b/test/svg2js/_index.js @@ -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/'); + }); + }); + }); + }); diff --git a/test/svg2js/test.entities.svg b/test/svg2js/test.entities.svg new file mode 100644 index 000000000..03e4d43c0 --- /dev/null +++ b/test/svg2js/test.entities.svg @@ -0,0 +1,8 @@ + + + + + +]> + \ No newline at end of file