Skip to content

Commit b7af031

Browse files
fix: escape \u2028 and \u2029 characters (#244)
1 parent 24b0427 commit b7af031

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

src/utils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ export function getExportCode(html, replacers, options) {
6363
let exportCode = html;
6464

6565
if (!options.interpolate) {
66-
// eslint-disable-next-line no-param-reassign
67-
exportCode = JSON.stringify(exportCode);
66+
exportCode = JSON.stringify(exportCode)
67+
// Invalid in JavaScript but valid HTML
68+
.replace(/[\u2028\u2029]/g, (str) =>
69+
str === '\u2029' ? '\\u2029' : '\\u2028'
70+
);
6871
}
6972

7073
for (const replacer of replacers) {

test/__snapshots__/loader.test.js.snap

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@ exports[`loader should not failed contain invisible spaces: errors 1`] = `Array
44

55
exports[`loader should not failed contain invisible spaces: module 1`] = `
66
"// Exports
7-
module.exports = \\"<ul>\\\\n <li> \\\\\\\\u2028 - 
 </li>\\\\n <li> \\\\\\\\u2029 - 
 </li>\\\\n</ul>\\\\n\\";"
7+
module.exports = \\"<ul><li> \\\\\\\\u2028 - \\\\u2028 Text \\\\u2028 </li><li> \\\\\\\\u2029 - \\\\u2029 Text \\\\u2029 </li></ul>\\";"
88
`;
99

10-
exports[`loader should not failed contain invisible spaces: result 1`] = `
11-
"<ul>
12-
<li> \\\\u2028 - 
 </li>
13-
<li> \\\\u2029 - 
 </li>
14-
</ul>
15-
"
16-
`;
10+
exports[`loader should not failed contain invisible spaces: result 1`] = `"<ul><li> \\\\u2028 - 
 Text 
 </li><li> \\\\u2029 - 
 Text 
 </li></ul>"`;
1711

1812
exports[`loader should not failed contain invisible spaces: warnings 1`] = `Array []`;
1913

test/fixtures/invisible-space.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
<ul>
2-
<li> \u2028 - 
 </li>
3-
<li> \u2029 - 
 </li>
4-
</ul>
1+
<ul><li> \u2028 - 
 Text 
 </li><li> \u2029 - 
 Text 
 </li></ul>

test/loader.test.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
14
import {
25
compile,
36
getCompiler,
@@ -34,12 +37,19 @@ describe('loader', () => {
3437
});
3538

3639
it('should not failed contain invisible spaces', async () => {
40+
const source = fs.readFileSync(
41+
path.resolve(__dirname, './fixtures/invisible-space.html')
42+
);
43+
44+
expect(/[\u2028\u2029]/.test(source)).toBe(true);
45+
3746
const compiler = getCompiler('invisible-space.js');
3847
const stats = await compile(compiler);
3948

40-
expect(getModuleSource('./invisible-space.html', stats)).toMatchSnapshot(
41-
'module'
42-
);
49+
const moduleSource = getModuleSource('./invisible-space.html', stats);
50+
51+
expect(moduleSource).toMatchSnapshot('module');
52+
expect(/[\u2028\u2029]/.test(moduleSource)).toBe(false);
4353
expect(
4454
execute(readAsset('main.bundle.js', compiler, stats))
4555
).toMatchSnapshot('result');

0 commit comments

Comments
 (0)