From f8e39a88e8d25f752b305ab01f0123cca4b38b8a Mon Sep 17 00:00:00 2001 From: eluisfonseca Date: Fri, 17 Jan 2020 04:35:35 +0000 Subject: [PATCH] Fix false-positive when the `` tag contains markup (#24) --- index.js | 8 +++++++- test.js | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 733211a..3d5cfe4 100644 --- a/index.js +++ b/index.js @@ -15,9 +15,15 @@ const isBinary = buffer => { return false; }; +const cleanEntities = svg => { + const entityRegex = /\s*/img; + // Remove entities + return svg.replace(entityRegex, ''); +}; + const regex = /^\s*(?:<\?xml[^>]*>\s*)?(?:]*\s*(?:\[?(?:\s*]*>\s*)*\]?)*[^>]*>\s*)?(?:]*>[^]*<\/svg>|]*\/\s*>)\s*$/i; -const isSvg = input => Boolean(input) && !isBinary(input) && regex.test(input.toString().replace(htmlCommentRegex, '')); +const isSvg = input => Boolean(input) && !isBinary(input) && regex.test(cleanEntities(input.toString()).replace(htmlCommentRegex, '')); module.exports = isSvg; // TODO: Remove this for the next major release diff --git a/test.js b/test.js index 9034cc2..22a6b7f 100644 --- a/test.js +++ b/test.js @@ -50,3 +50,23 @@ test('supports non-english characters', t => { `)); }); + +test('support markup inside Entity tags', t => { + t.true(isSvg(' "> ]>&Smile;')); + t.true(isSvg(' "> ]>&Smile;')); + t.true(isSvg(` + + "> + "> + ]> + + + &Melon; + + + &Orange; + + `)); +});