Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape \u2028 and \u2029 characters #244

Merged
merged 1 commit into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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