Skip to content

Commit b6abbbb

Browse files
committed
INCOMPLETE: Waiting on PR svg/sax#3 or svg/sax#2
fix: avoid escaping numeric entities or predefined entities lt and amp Also allows .idea in .gitignore
1 parent 79d4bab commit b6abbbb

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.idea
12
.pnp.*
23
.yarn/*
34
!.yarn/patches

lib/stringifier.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ const defaults = {
5858
textStart: '',
5959
textEnd: '',
6060
indent: 4,
61-
regEntities: /[&'"<>]/g,
62-
regValEntities: /[&"<>]/g,
61+
regEntities: /['"<>]|&(?!#x?[\da-f]+;)/gi,
62+
regValEntities: /["<>]|&(?!(?:#x?[\da-f]+|lt|amp);)/gi,
6363
encodeEntity: encodeEntity,
6464
pretty: false,
6565
useShortTags: true,

test/svgo/_index.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,53 @@ describe('svgo', () => {
3535
const result = optimize('<svg />', { input: 'file', path: 'input.svg' });
3636
expect(result.data).toEqual('<svg/>');
3737
});
38+
it('should resolve and not escape numeric character references', async () => {
39+
const result = optimize('<svg><text class="&#1234; &#xabcd;">&#xabcd; &#1234;</text></svg>', { input: 'file', path: 'input.svg' });
40+
expect(result.data).toEqual('<svg><text class="\u04d2 \uabcd">\uabcd \u04d2</text></svg>');
41+
});
42+
43+
it('should escape and not double-escape numeric character references representing <', async () => {
44+
const result = optimize('<svg><text font-family="&#60; and &#x3c;">&#x3c; and &#60;</text></svg>', { input: 'file', path: 'input.svg' });
45+
console.log('result5', result);
46+
expect(result.data).toEqual('<svg><text font-family="&lt; and &lt;">&lt; and &lt;</text></svg>');
47+
});
48+
it('should escape and not double-escape numeric character references representing &', async () => {
49+
const result = optimize('<svg><text font-family="&#38; and &#x26;">&#x26; and &#38;</text></svg>', { input: 'file', path: 'input.svg' });
50+
console.log('result6', result);
51+
expect(result.data).toEqual('<svg><text font-family="&amp; and &amp;">&amp; and &amp;</text></svg>');
52+
});
53+
54+
// '<svg><text><![CDATA ]&#x3e; and ]&#62; ]></text></svg>'
55+
// '<svg><text class="&#34; and &#39;">&#34; and &#39;</text></svg>',
56+
// '<svg><text class="&#x22; and &#x27;">&#x22; and &#x27;</text></svg>',
57+
it('should escape and not double-escape numeric character references representing >', async () => {
58+
const result = optimize('<svg><text>CDATA1: <![CDATA[ ]&#x3e; and ]&#62; ]]></text></svg>', { input: 'file', path: 'input.svg' });
59+
console.log('result7', result);
60+
expect(result.data).toEqual('<svg><text>CDATA1: <![CDATA[ ]&#x3e; and ]&#62; ]]></text></svg>');
61+
});
62+
it('should escape and not double-escape numeric character references representing \'', async () => {
63+
const result = optimize('<svg><text class="&#34; and &#39;">&#34; and &#39;</text></svg>', { input: 'file', path: 'input.svg' });
64+
console.log('result8', result);
65+
expect(result.data).toEqual('<svg><text class="&quot; and \'">&quot; and &apos;</text></svg>');
66+
});
67+
it('should escape and not double-escape numeric character references representing "', async () => {
68+
const result = optimize('<svg><text class="&#x22; and &#x27;">&#x22; and &#x27;</text></svg>', { input: 'file', path: 'input.svg' });
69+
console.log('result9', result);
70+
expect(result.data).toEqual('<svg><text class="&quot; and \'">&quot; and &apos;</text></svg>');
71+
});
72+
73+
it('should escape and not double-escape numeric character references representing \'', async () => {
74+
const result = optimize('<svg><text class=\'&#34; and &#39;\'>&#34; and &#39;</text></svg>', { input: 'file', path: 'input.svg' });
75+
console.log('result8', result);
76+
expect(result.data).toEqual('<svg><text class="&quot; and \'">&quot; and &apos;</text></svg>');
77+
});
78+
it('should escape and not double-escape numeric character references representing "', async () => {
79+
const result = optimize('<svg><text class="&#x22; and &#x27;">&#x22; and &#x27;</text></svg>', { input: 'file', path: 'input.svg' });
80+
console.log('result9', result);
81+
expect(result.data).toEqual('<svg><text class="&quot; and \'">&quot; and &apos;</text></svg>');
82+
});
83+
84+
3885
it('should preserve style specifity over attributes', async () => {
3986
const [original, expected] = await parseFixture('style-specifity.svg');
4087
const result = optimize(original, {

0 commit comments

Comments
 (0)