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

Commit

Permalink
fix(html): limit regex to only applicable script tags for replacing c…
Browse files Browse the repository at this point in the history
…ontent
  • Loading branch information
felpi authored and danbucholtz committed Oct 12, 2017
1 parent d62f5da commit 93db0ef
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/core/inject-script.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,40 @@ describe('Inject Scripts', () => {
'</html>');
});

it('should replace only one existed injected script tag', () => {
const inputHtml = '' +
'<html>\n' +
'<head>\n' +
' <script data-ionic="inject">\n' +
' alert(11111);\n' +
' </script>\n' +
' <script>\n' +
' alert(222);\n' +
' </script>\n' +
'</head>\n' +
'<body>\n' +
'</body>\n' +
'</html>';

const output = injectCoreHtml(inputHtml, ' <script data-ionic="inject">\n' +
' alert(55555);\n' +
' </script>');

expect(output).toEqual(
'<html>\n' +
'<head>\n' +
' <script data-ionic="inject">\n' +
' alert(55555);\n' +
' </script>\n' +
' <script>\n' +
' alert(222);\n' +
' </script>\n' +
'</head>\n' +
'<body>\n' +
'</body>\n' +
'</html>');
});

it('should add script to top of file when no html tag', () => {
const inputHtml = '' +
'<body>\n' +
Expand Down
2 changes: 1 addition & 1 deletion src/core/inject-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function injectCoreScripts(context: BuildContext, indexHtml: string) {

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

0 comments on commit 93db0ef

Please sign in to comment.