Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 93db0ef

Browse files
felpidanbucholtz
authored andcommitted
fix(html): limit regex to only applicable script tags for replacing content
1 parent d62f5da commit 93db0ef

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/core/inject-script.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,40 @@ describe('Inject Scripts', () => {
3333
'</html>');
3434
});
3535

36+
it('should replace only one existed injected script tag', () => {
37+
const inputHtml = '' +
38+
'<html>\n' +
39+
'<head>\n' +
40+
' <script data-ionic="inject">\n' +
41+
' alert(11111);\n' +
42+
' </script>\n' +
43+
' <script>\n' +
44+
' alert(222);\n' +
45+
' </script>\n' +
46+
'</head>\n' +
47+
'<body>\n' +
48+
'</body>\n' +
49+
'</html>';
50+
51+
const output = injectCoreHtml(inputHtml, ' <script data-ionic="inject">\n' +
52+
' alert(55555);\n' +
53+
' </script>');
54+
55+
expect(output).toEqual(
56+
'<html>\n' +
57+
'<head>\n' +
58+
' <script data-ionic="inject">\n' +
59+
' alert(55555);\n' +
60+
' </script>\n' +
61+
' <script>\n' +
62+
' alert(222);\n' +
63+
' </script>\n' +
64+
'</head>\n' +
65+
'<body>\n' +
66+
'</body>\n' +
67+
'</html>');
68+
});
69+
3670
it('should add script to top of file when no html tag', () => {
3771
const inputHtml = '' +
3872
'<body>\n' +

src/core/inject-scripts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function injectCoreScripts(context: BuildContext, indexHtml: string) {
3434

3535
export function injectCoreHtml(indexHtml: string, inject: string) {
3636
// see if we can find an existing ionic script tag and replace it entirely
37-
const existingTag = indexHtml.match(/<script data-ionic="inject">[\s\S]*<\/script>/gi);
37+
const existingTag = indexHtml.match(/<script data-ionic="inject">[\s\S]*?<\/script>/gi);
3838
if (existingTag) {
3939
return indexHtml.replace(existingTag[0], inject.trim());
4040
}

0 commit comments

Comments
 (0)