Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

script type=text/coffeescript tag does not support unicode (utf-8) properly #4263

Closed
Masterxilo opened this issue May 7, 2016 · 4 comments · Fixed by #4375
Closed

script type=text/coffeescript tag does not support unicode (utf-8) properly #4263

Masterxilo opened this issue May 7, 2016 · 4 comments · Fixed by #4375

Comments

@Masterxilo
Copy link

Masterxilo commented May 7, 2016

Running this html file, saved with utf-8 encoding (no BOM) in Chrome:

<meta charset=utf-8>
<script src=/lib/coffee-script.js></script>
<script>
alert('js:おねがいします')
</script>
<script type=text/coffeescript>
alert('cs:おねがいします')
</script>

generates

coffee-script.js:6459 Uncaught InvalidCharacterError: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.

The error is generated on the lines saying

      case typeof btoa !== 'function':
        return btoa(src);

in coffee-script.js

The javascript tag has no problems with this string.

@Masterxilo
Copy link
Author

I guess this lists solutions on how to make btoa deal with utf-8:

https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_.232_.E2.80.93_rewriting_atob()_and_btoa()_using_TypedArrays_and_UTF-8

@Masterxilo
Copy link
Author

Masterxilo commented May 7, 2016

As by these suggestions, replacing

      case typeof btoa !== 'function':
        return btoa(src);

with

case typeof btoa !== 'function':
function b64EncodeUnicode(str) {
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
        return String.fromCharCode('0x' + p1);
    }));
}
return b64EncodeUnicode(src);

seems to fix the problem for now. More work is needed for determining the implications of this.

(I for one don't even know why this encoding to base64 is needed).


Interestingly, the parser does not generally have a problem with utf-8 (that would have been noticed earlier I guess). The following works as expected:

<meta charset=utf-8>
<script src=/lib/coffee-script.js></script>
<script>
alert('js:おねがいします')
</script>
<div id=text>alert('cs:おねがいします')</div>
<script type=text/coffeescript>
eval(document.getElementById('text').innerHTML)
</script>

It might be also of interest that the error in the OP appears even when only comments contain non-ascii characters.

@vlad0337187
Copy link

vlad0337187 commented Nov 23, 2016

I have similar error:
<script type="text/javascript" src="./scripts/coffee-script.js" charset="utf-8"></script>
<script type="text/coffeescript" charset='utf-8'>alert "Рукава и горсти"</script>

throws me into error:
"InvalidCharacterError: String contains an invalid character"

If there will be:
<script type="text/coffeescript" charset='utf-8'>alert "Text in English"</script>
, all will work well.

How to solve this?

@vlad0337187
Copy link

vlad0337187 commented Nov 23, 2016

I changed this:
2016-11-23 02-47-14
to this:
2016-11-23 02-48-06

And all worked.

So as for not-optimized and compressed sources, I think the problem is here:
https://github.com/jashkenas/coffeescript/blob/master/lib/coffee-script/coffee-script.js#L31
Simply to change
case typeof btoa !== 'function': return btoa(src);
to
case typeof btoa !== 'function': return src;
will create "hotfix" for this problem.

Thanks to author of the topic for pointing me to the place of problem - strange item "btoa" =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants