Skip to content

Commit

Permalink
Fix issue when merging <script> without leading ; #55
Browse files Browse the repository at this point in the history
  • Loading branch information
maltsev committed Aug 3, 2018
1 parent d2eb67e commit 391bbe5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
9 changes: 7 additions & 2 deletions lib/modules/mergeScripts.es6
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ export default function mergeScripts(tree) {
let lastScriptNode = scriptNodes.pop();
scriptNodes.reverse().forEach(scriptNode => {
let scriptContent = (scriptNode.content || []).join(' ');
lastScriptNode.content.unshift(scriptContent + ' ');
scriptContent = scriptContent.trim();
if (scriptContent.slice(-1) !== ';') {
scriptContent += ';';
}

lastScriptNode.content.unshift(scriptContent);

scriptNode.tag = false;
scriptNode.content = [];
});
}

return tree;
}
}
40 changes: 36 additions & 4 deletions test/modules/mergeScripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,43 @@ describe('mergeScripts', () => {
<script class="test" type="text/javascript">foo = 7;</script>`,

'\n \n \n \n ' +
`<script>var foo = 1; foo = 3; foo = 5;</script>
<script defer="defer">foo = 4; foo = 6;</script>
<script class="test" type="text/javascript">foo = 2; foo = 7;</script>`,
`<script>var foo = 1;foo = 3;foo = 5;</script>
<script defer="defer">foo = 4;foo = 6;</script>
<script class="test" type="text/javascript">foo = 2;foo = 7;</script>`,

options
);
});
});

it('should add trailing ; when it\'s missing in oneline <script>', () => {
return init(
'<script>document.write("Hello, ")</script><script>document.write("World!")</script>',
'<script>document.write("Hello, ");document.write("World!")</script>',
options
);
});

it('should add trailing ; when it\'s missing in multiline <script>', () => {
return init(
`<script>
// Some comment
var a = 5;
var b = a + 6
</script><script>
var c = 10
</script>`,
`<script>// Some comment
var a = 5;
var b = a + 6;
var c = 10
</script>`,
options
);
});
});

0 comments on commit 391bbe5

Please sign in to comment.