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

d3.v3.js does not work if document encoding is not UTF8 #1195

Closed
ikriv opened this issue Apr 11, 2013 · 5 comments
Closed

d3.v3.js does not work if document encoding is not UTF8 #1195

ikriv opened this issue Apr 11, 2013 · 5 comments

Comments

@ikriv
Copy link

ikriv commented Apr 11, 2013

On line 1285 of d3.v3.js we find some non-ASCII characters related to the definition of Pi.
var π = Math.PI, ε = 1e-6

While it may look cool to define variables named with Greek letters, it creates issue in HTML documents encoded in encodings other than UTF-8. If document encoding is not utf-8, and if D3JS scripts are included without explicit charset="utf-8" attribute, an "illegal character" error will pop up.

Possible resolutions may include:

  • stick to ASCII in variable names (probably the best option)
  • add charset="utf-8" to all script tags in all examples
@mbostock
Copy link
Member

This is correct. You either need:

<!DOCTYPE html>
<meta charset="utf-8">

Or:

<script charset="utf-8" src="d3.js"></script>

Or you can use the minified version, which only contains ASCII:

<script src="d3.min.js"></script>

@mbostock
Copy link
Member

Actually, I take that back, you need UTF-8 encoding even with the minified version because of embedded format strings, such as "µ" for SI-prefix formatting.

@mathiasbynens
Copy link

mathiasbynens commented Aug 12, 2013

@mbostock Two small suggestions:

  1. Make a good example of http://d3js.org/d3.v3.js and http://d3js.org/d3.v3.min.js, by sending it with the appropriate MIME type. Currently the following HTTP header is sent with these resources: Content-Type: application/x-javascript. The correct MIME type for JavaScript is text/javascript, though, and it’s possible to specify the character encoding in this header too. My suggestion would be to make this Content-Type: text/javascript;charset=utf-8.
  2. Why not use a JavaScript escape sequence for any non-ASCII symbols in the source? For example, µ can be written as \xB5 or \u00B5, which would avoid this issue for the production version (the minified version).

@mbostock
Copy link
Member

I would love to fix the header, but unfortunately I have no control over the headers served by GitHub Pages.

The minified version already has munged symbol names using ASCII characters due to UglifyJS. But there is currently one non-ASCII character (µ, 0xb5) in a format string, which I can fix by adding a flag to UglifyJS.

@mathiasbynens
Copy link

Thanks for the quick patch!

janekbettinger pushed a commit to janekbettinger/backbone.d3 that referenced this issue Apr 9, 2014
mattpage pushed a commit to jivesoftware/react-d3-charts that referenced this issue Feb 25, 2016
teamdandelion pushed a commit to tensorflow/tensorboard that referenced this issue May 23, 2017
… the charset to UTF-8.

See d3/d3#1195 for the related d3 issue.
Change: 135417926
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants