Eclipse Luna : can't remove default encoding for javascript and json - javascript

I'm trying to remove the default UTF-8 encoding of javascript / json files because I want to use the workspace default text encoding, but it doesn't seem to work with eclipse Luna.
1 - Default encoding set to UTF-8 for javascript files
2 - I remove the default encoding and click "update"
3 - If I leave and go back to check javascript file encoding, it's back to UTF-8 (1st image).
Am I missing something here ?

The problem here is that the plugin which defines the JavaScript content type specifies a default encoding of 'UTF-8', when you remove the Default encoding in the dialog the encoding just reverts to this default.
This means that you can't get this to default to the workspace settings.
The class org.eclipse.core.internal.content.ContentType defines this behaviour.

I too got this problem in old project. But UTF-8 is the most commonly used and recommended encoding for web, and eclipse (plugin in it) promote this idea for users in intrusive style. If you need create/edit few js-files with another encoding - may change encoding in properties of current file from "Default (determined from content type: UTF-8)" to other. But if this files are lot (more 20-50) - yes, its hurts to realize :-)
In my old project more then 100 js-files with cp1251 encoding, but i change encoding (from utf8) in properties only for few editable files. This does not affect build of the project

Related

How to determine if a JavaScript ArrayBuffer contains string or binary data?

I am building a simple application where users can load any file into the Monaco editor in a web browser.
I'm trying to work out if the file that the user has loaded is text, and therefore editable.
In JavaScript, the library I am using to load returns the loaded file as an ArrayBuffer. Of course I can just convert this to text regardless of whether or not it is text or binary and throw the result into the editor. Presumably binary converted to text will display as garbage in the Monaco editor.
I could also examine the mime type of the loaded file. This would go a long way towards solving the problem, but it means I somehow have to know which mime types are text- I have not been able to find anything that specifies this. Also, it means any file without the correct mime type set would not be editable.
So my question is, is there a way to know if the contents of a JavaScript ArrayBuffer is text or binary data such as an image or executable code, by examining the data itself, rather than referring to mime type?
EDIT:
This question is not a duplicate of questions that are simply asking how to convert an ArrayBuffer to text. Simply converting an ArrayBuffer to text doesn't tell whether nor not this is a file that contains editable text or if it is a binary file. Additional information is needed, such as the magic number suggested in the answers to this question.
You can check the Magic numbers of the ArrayBuffer. Magic numbers are a sort of constants in files buffer that you can check to distinguishing between many file formats
Wikipedia - Magic numbers
This NPM module use that approach. Here's a list of the module's supported file types, you can see that they don't support text types.
For SVG you can use https://github.com/sindresorhus/is-svg.
For CSV you can use https://www.npmjs.com/package/detect-csv, but you can't be sure at 100% like they're saying here
UPDATE: I've writed an article about this which contains more explanations and a little Sandbox

How to correctly include a javascript file into html

I am trying to run a script through HTML but I am having some problems. I searched online and found how to do so, however the issue is that even if I correctly type the path of the .js file, it seems to add some strange characters before it.
This is in index.html
<script type="text/javascript" src="fractalTest/fractalTest.js"></script>
I expected this to work but when I open index.html in google chrome and inspect then look under the elements tab, this "â©fractalTest/fractalTest.js" is replacing "fractalTest/fractalTest.js" in the path of the file. I believe this is whats causing the error but I do not know how to fix it!
...it seems to add some strange characters before it.
That usually means that the file is saved with a byte-order mark (BOM) of some kind, but isn't being sent with the correct charset for that byte-order mark.
Be sure that the server is configured to serve the files with a specific encoding (UTF-8 is a good choice), and that you save the files using that encoding (in your text editor, etc.). It's also usually best not to include a BOM on UTF-8 files (although it's valid, some tools don't handle it well).
Side note: No need for the type attribute. The default is JavaScript.

Create PDF in browser: Custom Font

I know two libraries to create PDF files using Javascript in the browser ([1], [2]) but none of them allows to embed a custom font into the document.
[2] allows to set a custom font, but only for the standard PDF fonts (Courier, Times-Roman) and none of them is actively developed anymore.
Does anyone know a library to create PDF files in the browser that is still actively developed and supports the embedding of custom fonts?
Cheers,
Manuel
Ok, looks like current implementations do not support it.
So I'm porting libharu to javascript using emscripten:
Project:
https://github.com/manuels/hpdf.js
Demos:
http://manuels.github.com/hpdf.js/
If anyone else is looking, there's also this: https://github.com/devongovett/pdfkit
It looks more actively developed than hpdf, BUT I couldn't get it working just using browserify with the node module brfs as the docs mention (firstly brfs only works with static paths, but it also didn't seem to output the raw data from the font properly), I had to do this to get it to work:
if your font has no cmap: open, then export the font as ttf (with glyph map in the export options) with fontforge
get the base64 of the ttf file in string format (I used python to read the contents of the ttf file, encode with base64, remove line breaks, then save out to another file)
paste the string as a variable in your script
create a buffer object, and pass that as the font with pdfkit, ie
fontCenturyGothicBase64 = "your base64 encoded string here";
fontCenturyGothic = new Buffer(fontCenturyGothicBase64, 'base64');
doc.font(fontCenturyGothic);
use browserify on the javascript file (Buffer is a node object rather than pure js)
Maybe it's possible without using the Buffer object (and thus browserify), I haven't tried.

Javascript character encoding

In an external javascript file I have a function that is used to append text to table cells (within the HTML doc that the javascript file is added to), text that can sometimes have Finnish characters (such as ä). That text is passed as an argument to my function:
content += addTableField(XML, 'Käyttötarkoitus', 'purpose', 255);
The problem is that diacritics such as "ä" get converted to some other bogus characters, such as "�". I see this when viewing the HTML doc in a browser. This is obviously not desirable, and is quite strange as well since the character encoding for the HTML doc is UTF-8.
How can I solve this problem?
Thanks in advance for helping out!
The file that contains content += addTableField(XML, 'Käyttötarkoitus', 'purpose', 255); is not saved in UTF-8 encoding.
I don't know what editor you are using but you can find it in settings or in the save dialog.
Example:
If you can't get this to work you could always write out the literal code points in javascript:
content += addTableField(XML, 'K\u00E4ytt\u00f6tarkoitus', 'purpose', 255);
credit: triplee
To check out the character encoding announced by a server, you can use Firebug (in the Info menu, there’s a command for viewing HTTP headers). Alternatively, you can use online services like Web-Sniffer.
If the headers for the external .js file specify a charset parameter, you need to use that encoding, unless you can change the relevant server settings (perhaps a .htaccess file).
If they lack a charset parameter, you can specify the encoding in the script element, e.g. <script src="foo.js" charset="utf-8">.
The declared encoding should of course match the actual encoding, which you can normally select when you save a file (using “Save As” command if needed).
The character encoding of the HTML file / doc does not matter any external ressource.
You will need to deliver the script file with UTF8 character encoding. If it was saved as such, your server config is bogus.

javascript - reading local text file - charset issue

I am reading local text file using input-type-file and FileReader.readAsText(). The problem arises when the local text file contains characters like Ü. In that case it is converted to ï¿. Of course I can set encoding manually to iso8859-1 as a parameter of FileReader.readAsText(File, encoding) but the thing is that I have no clue what kind of encoding user has set on his side.
My question is whether there is an option to determine encoding on client machine ?
Best regards
kkris1983
You'd need to analyze the raw binaries of the text file to have a best guess at what the encoding is. There isn't any libraries for this in javascript AFAIK but you could port one from other languages.
Since that isn't very robust, you should also provide a manual override like Characters not showing correctly? Change encoding:
You can also have smart defaults, for example ISO-8859-1 if you detect it's western windows machine.

Categories