Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
icelam committed Aug 5, 2022
2 parents eb3cbd7 + 019a41d commit 2fb2cb8
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 4 deletions.
33 changes: 33 additions & 0 deletions __tests__/HtmlInlineScriptPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ignoreScriptsConfig from './cases/ignore-scripts/webpack.config';
import ignoreHtmlsConfig from './cases/ignore-htmls/webpack.config';
import ignoreScriptsAndHtmlsConfig from './cases/ignore-scripts-and-htmls/webpack.config';
import filenameWithSpecialCharactersConfig from './cases/filename-with-special-characters/webpack.config';
import escapeScriptTagEndConfig from './cases/escape-script-end-tag/webpack.config';

describe('HtmlInlineScriptPlugin', () => {
it('should build simple webpack config without error', async () => {
Expand Down Expand Up @@ -358,6 +359,38 @@ describe('HtmlInlineScriptPlugin', () => {
await webpackPromise;
});

it('should escape any "</script>" appears in source', async () => {
const webpackPromise = new Promise((resolve) => {
const compiler = webpack(escapeScriptTagEndConfig);

compiler.run((error, stats) => {
expect(error).toBeNull();

const statsErrors = stats?.compilation.errors;
expect(statsErrors?.length).toBe(0);

const result = fs.readFileSync(
path.join(__dirname, 'cases/escape-script-end-tag/dist/index.html'),
'utf8',
);

const expected = fs.readFileSync(
path.join(__dirname, 'cases/escape-script-end-tag/expected/index.html'),
'utf8',
);
expect(result).toBe(expected);

const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/escape-script-end-tag/expected/'));
const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/escape-script-end-tag/dist/'));
expect(expectedFileList.sort()).toEqual(generatedFileList.sort());

resolve(true);
});
});

await webpackPromise;
});

it('should build throw error if options passed to plugin is old options format', async () => {
const initializedPlugin = () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-new
Expand Down
1 change: 1 addition & 0 deletions __tests__/cases/escape-script-end-tag/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="language" content="English"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="viewport" content="minimum-scale=1,initial-scale=1,width=device-width,shrink-to-fit=no"/><title>webpack test</title></head><body><p>This is minimal code to demonstrate webpack usage</p><script defer="defer">document.write('<script>console.log("Hello world");<\/script>'),document.write('<script>console.log("Goodbye world");<\/script>');</script></body></html>
14 changes: 14 additions & 0 deletions __tests__/cases/escape-script-end-tag/fixtures/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="English" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no" />
<title>webpack test</title>
</head>
<body>
<p>This is minimal code to demonstrate webpack usage</p>
</body>
</html>
3 changes: 3 additions & 0 deletions __tests__/cases/escape-script-end-tag/fixtures/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// eslint-disable-next-line no-console
document.write('<script>console.log("Hello world");</script>');
document.write('<script>console.log("Goodbye world");</script>');
22 changes: 22 additions & 0 deletions __tests__/cases/escape-script-end-tag/webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import path from 'path';
import type { Configuration } from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import Self from '../../../dist';

const config: Configuration = {
mode: 'production',
entry: path.join(__dirname, './fixtures/index.js'),
output: {
path: path.join(__dirname, './dist'),
filename: '[name].js'
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './fixtures/index.html'),
inject: 'body'
}),
new Self()
]
};

export default config;
3 changes: 2 additions & 1 deletion src/HtmlInlineScriptPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {

return {
tagName: 'script',
innerHTML: asset.source() as string,
// escape '</script>' appears in source
innerHTML: (asset.source() as string).replace(/(<)(\/script>)/g, '\\x3C$2'),
voidTag: false,
attributes: attributesWithoutSrc,
meta: { plugin: 'html-inline-script-webpack-plugin' }
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2171,9 +2171,9 @@ eslint-plugin-import@^2.22.0:
tsconfig-paths "^3.14.1"

eslint-plugin-jest@^26.1.1:
version "26.6.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-jest/-/eslint-plugin-jest-26.6.0.tgz#546804fa42da75d7d58d4d3b278d5186abd3f6c0"
integrity sha512-f8n46/97ZFdU4KqeQYqO8AEVGIhHWvkpgNBWHH3jrM28/y8llnbf3IjfIKv6p2pZIMinK1PCqbbROxs9Eud02Q==
version "26.7.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-jest/-/eslint-plugin-jest-26.7.0.tgz#41d405ac9143e1284a3401282db47ed459436778"
integrity sha512-/YNitdfG3o3cC6juZziAdkk6nfJt01jXVfj4AgaYVLs7bupHzRDL5K+eipdzhDXtQsiqaX1TzfwSuRlEgeln1A==
dependencies:
"@typescript-eslint/utils" "^5.10.0"

Expand Down

0 comments on commit 2fb2cb8

Please sign in to comment.