Skip to content

Commit

Permalink
fix: escape \u2028 and \u2029 characters (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Mar 4, 2020
1 parent 24b0427 commit b7af031
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/utils.js
Expand Up @@ -63,8 +63,11 @@ export function getExportCode(html, replacers, options) {
let exportCode = html;

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

for (const replacer of replacers) {
Expand Down
10 changes: 2 additions & 8 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -4,16 +4,10 @@ exports[`loader should not failed contain invisible spaces: errors 1`] = `Array

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

exports[`loader should not failed contain invisible spaces: result 1`] = `
"<ul>
<li> \\\\u2028 - 
 </li>
<li> \\\\u2029 - 
 </li>
</ul>
"
`;
exports[`loader should not failed contain invisible spaces: result 1`] = `"<ul><li> \\\\u2028 - 
 Text 
 </li><li> \\\\u2029 - 
 Text 
 </li></ul>"`;

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

Expand Down
5 changes: 1 addition & 4 deletions test/fixtures/invisible-space.html
@@ -1,4 +1 @@
<ul>
<li> \u2028 - 
 </li>
<li> \u2029 - 
 </li>
</ul>
<ul><li> \u2028 - 
 Text 
 </li><li> \u2029 - 
 Text 
 </li></ul>
16 changes: 13 additions & 3 deletions test/loader.test.js
@@ -1,3 +1,6 @@
import fs from 'fs';
import path from 'path';

import {
compile,
getCompiler,
Expand Down Expand Up @@ -34,12 +37,19 @@ describe('loader', () => {
});

it('should not failed contain invisible spaces', async () => {
const source = fs.readFileSync(
path.resolve(__dirname, './fixtures/invisible-space.html')
);

expect(/[\u2028\u2029]/.test(source)).toBe(true);

const compiler = getCompiler('invisible-space.js');
const stats = await compile(compiler);

expect(getModuleSource('./invisible-space.html', stats)).toMatchSnapshot(
'module'
);
const moduleSource = getModuleSource('./invisible-space.html', stats);

expect(moduleSource).toMatchSnapshot('module');
expect(/[\u2028\u2029]/.test(moduleSource)).toBe(false);
expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
Expand Down

0 comments on commit b7af031

Please sign in to comment.