diff --git a/.gitignore b/.gitignore index 1c9775de0..4d24b361c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea .pnp.* .yarn/* !.yarn/patches diff --git a/lib/stringifier.js b/lib/stringifier.js index e490fd483..fee8c255d 100644 --- a/lib/stringifier.js +++ b/lib/stringifier.js @@ -58,8 +58,8 @@ const defaults = { textStart: '', textEnd: '', indent: 4, - regEntities: /[&'"<>]/g, - regValEntities: /[&"<>]/g, + regEntities: /['"<>]|&(?!#x?[\da-f]+;)/gi, + regValEntities: /["<>]|&(?!(?:#x?[\da-f]+|lt|amp);)/gi, encodeEntity: encodeEntity, pretty: false, useShortTags: true, diff --git a/test/svgo/_index.test.js b/test/svgo/_index.test.js index 99c176da6..085895be2 100644 --- a/test/svgo/_index.test.js +++ b/test/svgo/_index.test.js @@ -35,6 +35,53 @@ describe('svgo', () => { const result = optimize('', { input: 'file', path: 'input.svg' }); expect(result.data).toEqual(''); }); + it('should resolve and not escape numeric character references', async () => { + const result = optimize('ꯍ Ӓ', { input: 'file', path: 'input.svg' }); + expect(result.data).toEqual('\uabcd \u04d2'); + }); + + it('should escape and not double-escape numeric character references representing <', async () => { + const result = optimize('< and <', { input: 'file', path: 'input.svg' }); + console.log('result5', result); + expect(result.data).toEqual('< and <'); + }); + it('should escape and not double-escape numeric character references representing &', async () => { + const result = optimize('& and &', { input: 'file', path: 'input.svg' }); + console.log('result6', result); + expect(result.data).toEqual('& and &'); + }); + + // '' + // '" and '', + // '" and '', + it('should escape and not double-escape numeric character references representing >', async () => { + const result = optimize('CDATA1: ]&#x3e; and ]&#62; ', { input: 'file', path: 'input.svg' }); + console.log('result7', result); + expect(result.data).toEqual('CDATA1: ]&#x3e; and ]&#62; '); + }); + it('should escape and not double-escape numeric character references representing \'', async () => { + const result = optimize('" and '', { input: 'file', path: 'input.svg' }); + console.log('result8', result); + expect(result.data).toEqual('" and ''); + }); + it('should escape and not double-escape numeric character references representing "', async () => { + const result = optimize('" and '', { input: 'file', path: 'input.svg' }); + console.log('result9', result); + expect(result.data).toEqual('" and ''); + }); + + it('should escape and not double-escape numeric character references representing \'', async () => { + const result = optimize('" and '', { input: 'file', path: 'input.svg' }); + console.log('result8', result); + expect(result.data).toEqual('" and ''); + }); + it('should escape and not double-escape numeric character references representing "', async () => { + const result = optimize('" and '', { input: 'file', path: 'input.svg' }); + console.log('result9', result); + expect(result.data).toEqual('" and ''); + }); + + it('should preserve style specifity over attributes', async () => { const [original, expected] = await parseFixture('style-specifity.svg'); const result = optimize(original, {