Skip to content

Commit

Permalink
Browser compiler should convert the string to load to UTF-8, in case …
Browse files Browse the repository at this point in the history
…it is UTF-16 like the contents of a <script> block generally are (#4375)
  • Loading branch information
GeoffreyBooth authored Nov 28, 2016
1 parent 5c765f4 commit ac20f66
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/coffee-script/coffee-script.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/coffee-script.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ base64encode = (src) -> switch
when typeof Buffer is 'function'
new Buffer(src).toString('base64')
when typeof btoa is 'function'
btoa(src)
# The contents of a <script> block are encoded via UTF-16, so if any extended
# characters are used in the block, btoa will fail as it maxes out at UTF-8.
# See https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
# for the gory details, and for the solution implemented here.
btoa encodeURIComponent(src).replace /%([0-9A-F]{2})/g, (match, p1) ->
String.fromCharCode '0x' + p1
else
throw new Error('Unable to base64 encode inline sourcemap.')

Expand Down

0 comments on commit ac20f66

Please sign in to comment.