From cdb68d3e5128a93b5c9b35041d780619f1af5e5a Mon Sep 17 00:00:00 2001 From: edfus Date: Thu, 29 Apr 2021 16:53:06 +0800 Subject: [PATCH] fix: accept svg strings with data URI scheme. a SVG that has a xlink:href beggining with `data:image/svg+xml;base64,` will be wrongly treated as a base64 data URI in current decision making statements. --- index.js | 2 +- test/atob.svg | 26 ++++++++++++++++++++++++++ test/specs.js | 10 ++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/atob.svg diff --git a/index.js b/index.js index 6d91e1f..6b38496 100644 --- a/index.js +++ b/index.js @@ -172,7 +172,7 @@ function loadSVGContent(svg, callback) { if (Buffer.isBuffer(svg)) { svg = svg.toString('utf-8'); } - if (svg.indexOf('data:image/svg+xml;base64,') >= 0) { + if (svg.indexOf('data:image/svg+xml;base64,') >= 0 && !/^= 0) { callback(null, svg); diff --git a/test/atob.svg b/test/atob.svg new file mode 100644 index 0000000..105e614 --- /dev/null +++ b/test/atob.svg @@ -0,0 +1,26 @@ + + npm: v1.9.0 + + + + + + + + + + + + + + + npmv1.9.0 + + \ No newline at end of file diff --git a/test/specs.js b/test/specs.js index 0d0d29c..966c264 100644 --- a/test/specs.js +++ b/test/specs.js @@ -62,6 +62,16 @@ describe('Convert SVG', function () { }) }); + it('convert a svg string with data URI to png',function(done) { + var svg = fs.readFileSync(__dirname+'/atob.svg'); + svg2img(svg, null, function(error, data) { + expect(error).not.to.be.ok(); + expect(Buffer.isBuffer(data)).to.be.ok(); + expect(data.length).to.be.above(0); + done(); + }) + }); + it('convert a svg string to jpg',function(done) { var svg = fs.readFileSync(__dirname+'/ph.svg'); svg2img(svg, {format:'jpeg'} ,function(error, data) {