JCSompress - dealing with special characters in JS files? - javascript

I'm in the initial stages of investigating JSCompress for MSBUILD : http://msbuildtasks.tigris.org/
For my initial testing I have a few JS files over which I am running this task, some of the files include already minified JS files (JQuery Library etc..) and some files contain Special characters.
When the task runs everytime it encounters a special character in JS file it throws an error on the screen. How should I overcome this error so that it ignores special characters.
I do not want to exclude any files on the basis of wildcard on filenames (e.g. **\jquery*.js) since a developer can use the name as part of some other JS file which will then get excluded without minifying.
Is there a way to achieve this or should I be looking at other tools ?
Thanks !

Use the replace method to replace the special characters with a character entity of your own creation. Then after you have evaluated all the code use the replace method again to revert the special character conversion. I found I had to do this in my Pretty Diff tool because JavaScript cannot evaluate the difference between single and double quotes passed as string literals.

Related

Minified js file has unicode symbols like A-Za-zÀ-ÖØ-öø-ʸ̀֐ࠀ῿‎Ⰰ-﬜︀﹯﻽ which is breaking code

A third party minified js file that I am using for my project has unicode symbols like "A-Za-zÀ-ÖØ-öø-ʸ̀-Öà €-῿‎Ⰰ-﬜︀-﹯﻽-ï¿¿" which is breaking my code. But when i replace them with their equivalent unicode characters like: 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF' +
'\u200E\u2C00-\uFB1C\uFE00-\uFE6F\uFEFD-\uFFFF';
in the minified file itself, my code is working fine. But the problem arises when i try to check-in my code in my jira using putty command submit_files.pl, the sparta validator runs and gives a failure. How can i make that the validator pass? Is there any other way to deal with these unicode symbols?

How to allow semi-colons in input field and then do a indexof(";") JS call?

I don't have to HTML encode the string. I was trying various solutions, but the problem is still, how do you handle the semicolon entered by the user if you need to do a JS str.indexOf(";"); later on.
I’m using System.Net.WebUtility.HtmlEncode(test); encode my string,which adds semicolons (as you would expect for html encoding).
Later down the process this string gets utilized to create JavaScript commands that end with ';'. These commands are separated by doing a str.indexOf(";");
My issue is that the user is allowed to enter semi-colon in the field,which breaks the aforementioned indexof(";"), which I use to dynamically create the JavaScript commands.
How can I support users entering in semicolons into a string if I need to do a JS indexof(";") to separate the JS commands?
I tried in the C# side doing a
string myString = System.Net.WebUtility.HtmlEncode(test);
but that just makes the situation worse by adding even more semicolons as you would expect for HTML enconding.
The solution I came up with was to do a replace on the the C# side. In C# I do a .Replace of all % and (other problematic characters) with their URL encoded string versions before the JavaScript command ending ";" gets inserted(i.e. myString.Replace(";","%3B").Replace("=","%3D");).
Once it hits the JavaScript side I do the complete opposite, thus leaving my JS semicolons intact.
The aforementioned solution allowed me to distinguish between a user inserted semicolon and one entered in programmatically.

How to apply a localization to a javascript string

I assigned a string to a javascript string object, such like :
var word = "Please input correct verb"
I want this string be in control by resource file in asp.net project. Does it provide the function to replace the string using a ASP.NET syntax to switch languages?
<%$ Resources:Registration, correctverb%>
Thanks.
There are various l18n projects for JavaScript, e.g. http://i18next.com/
If you have ResX files in your ASP project and you want them as JavaScript or JSON files you can convert them here; or via the REST API you could convert a resource file as follows:
$ curl --data-binary #messages.resx \
http://localise.biz/api/convert/resx/messages.json
(example in cURL, which I guess you may not have if you're on Windows)
A common approach for this is creating an HTTP handler that evaluates requests for say files with the extension *.js.axd (or whatever extension you come up with) and then parse the javascript file by replacing defined tokens with the actual localized resource value.
It may be costly only the first time the file is requested but then everything should run smoothly if caching is applied. Here's an example of how to create a handler, parsing the file should be trivial. You could use the same syntax to define localized strings on your file: <% LocalizedResourceName %>

How do you write a Javascript Regex that matches if file extension is not within (exe|js|htaccess)

I'm trying to write a javascript regexp for a blacklist of file extensions. I'm using a jquery plugin that has an option for acceptable file types that takes a regex, but rather than maintain a whitelist we would like to maintain a blacklist. So I need the regex to only match if the string doesn't contain certain file extensions. Currently there is a regex we use to whitelist photo extensions on our photo upload:
/(\.|\/)(gif|jpe?g|png)$/i
For our document upload we would like to simply do a blacklist, but I haven't been able to make the ?! delimeter work. So for the sake of an example how would I reverse this regex to match as long as the file extension doesn't contain gif, jpg, jpeg, png?
I've tried several different ways of using the ?!, but nothing I've tried has worked properly. Heres some examples of what I have tried unsuccessfully:
/(\.|\/)(?!gif|jpe?g|png)$/i
/(\.|\/)(?!(gif|jpe?g|png))$/i
Essentially I need this regex to always return true unless the blacklisted file extensions are matched.
This works:
/\.(?!(?:exe|js|htaccess)$)|\/(?!(?:exe|js|htaccess)$)/i
I think it's because the "match only if not followed by" doesn't like parentheses before it, but I'm not sure.
This new regex now works with the parentheses and multiple extensions:
/^.*(\.|\/)(?!(exe|js|htaccess)$)(?![^\.\/]*(\.|\/))/i
However, to allow files with no extension, this must be used:
/(^.*(\.|\/)(?!(exe|js|htaccess)$)(?![^\.\/]*(\.|\/)))|(^[^\.]+$)/i
// ^^^^^^^^^
// This part matches a name without any dots
Looking at the complexity of that regex, I suggest that you implement something server-side or use another plugin.
Hack the jQuery plugin to accept a callback function instead of (or in addition to) a regular expression. Use the negation operator (!) and the positive regular expression to supply an appropriate callback. And write a mail to the maintainer of the plugin asking him to accept your patch.
I'm not a regex expert, but something like this appears to work: /\.(exe|js|htaccess)$/ig.test(filename) true results when the file is on your blacklist.
var shouldblockUpload = /\.(exe|js|htaccess)$/ig.test(filename);
//Inform user illegal upload
You can also explicitly only allow certain filetypes through the accept attribute on file inputs to help hint users to the right ones.

HTML syntax highlighting in javascript strings in vim

I don't know if this is possible/sensible, but I was curious to know if I can have my strings in javascript files have html highlighting. I discovered that strings in php could have SQL syntax highlighting, so I believe it is possible.
But, I don't know vim-scripting, so any help on this appreciated.
I am using the Better Javascript syntax.
PS: If there could be an option to turn it on and off while editing a js file, that would be wonderful
Thanks
Yes, it's possible if you don't mind some syntax file hacking. First you need to include the HTML syntax file from within the Javascript syntax file -- see :help syn-include for info on that; second you need to declare that HTML syntax can be found inside of certain elements (i.e. strings). Third, if you want to have the option of enabling and disabling it, you can make those commands dependent on a global variable, and write some mappings that set or unset the variable and then reload the syntax file.
For examples on how inclusion works, take a look at syntax/html.vim (which includes the Javascript and CSS syntax files), syntax/perl.vim (which includes the POD syntax file), or php.vim (which includes SQL syntax highlighting in strings, conditional on a global ariable).
Edit: did some work on actually making this happen in my copy.
In the head of syntax/javascript.vim, just below syn case ignore, add
syn include #javaScriptHTML syntax/html.vim
unlet b:current_syntax
syn spell default " HTML enables spell-checking globally, turn it off
Then add #javaScriptHTML to the contained= lists for javaScriptStringD and javaScriptStringS.
Finally you have to edit syntax/html.vim to prevent it from trying to include syntax/javascript.vim if it was loaded from javascript: find the line that reads
if main_syntax != 'java' || exists("java_javascript")
and change it to
if main_syntax != 'javascript' && ( main_syntax != 'java' || exists("java_javascript")

Categories