Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Make sure scripts in imports with unicode characters are kept in the …
Browse files Browse the repository at this point in the history
…correct encoding

Fixes Polymer/polymer#498

add test for unicode scripts
  • Loading branch information
dfreedm committed Jul 9, 2014
1 parent 8e8d01a commit 5b24a18
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,16 @@ function nodeIsImport(elt) {
}

function generateScriptDataUrl(script) {
var scriptContent = generateScriptContent(script), b64;
var scriptContent = generateScriptContent(script);
var b64 = 'data:text/javascript';
// base64 may be smaller, but does not handle unicode characters
// attempt base64 first, fall back to escaped text
try {
b64 = btoa(scriptContent);
b64 += (';base64,' + btoa(scriptContent));
} catch(e) {
b64 = btoa(unescape(encodeURIComponent(scriptContent)));
console.warn('Script contained non-latin characters that were forced ' +
'to latin. Some characters may be wrong.', script);
b64 += (';charset=utf-8,' + encodeURIComponent(scriptContent));
}
return 'data:text/javascript;base64,' + b64;
return b64;
}

function generateScriptContent(script) {
Expand Down
5 changes: 4 additions & 1 deletion test/html/imports/parsed-import-1.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<link rel="import" href="parsed-import-2.html">
<script>
window.inlineScriptParsed = new Date().getTime();
</script>
</script>
<script>
window.uc = '♡ 㒨';
</script>
2 changes: 1 addition & 1 deletion test/html/imports/parsed-import-2.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<script src="external-script.js"></script>
<script src="external-script.js"></script>
1 change: 1 addition & 0 deletions test/html/parser.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
addEventListener('HTMLImportsLoaded', function() {
chai.assert.ok(window.inlineScriptParsed, 'inlineScriptParsed');
chai.assert.ok(window.externalScriptParsed, 'externalScriptParsed');
chai.assert.equal(window.uc, '\u2661 \u34A8', 'unicode matches');
done();
});
</script>
Expand Down

0 comments on commit 5b24a18

Please sign in to comment.