Tuesday, October 19, 2010

Is eval evil ?

var tact = (eval("args.S_TACT") == undefined ? "" : "S_TACT=" + eval("args.S_TACT"));

In this case it is really evil, and dangerous !
specially if args is "somehow built" from document.URL

For those interested: code comes from big blue web site.

Friday, October 15, 2010


This computer program contains valuable, confidential and proprietary

IsUndefined = function (obj) { return (typeof obj == 'undefined'); }
IsNull = function (obj) { return (obj == null); }
IsValid = function (obj) { return (!IsNull(obj) && !IsUndefined(obj)); }

Sad but true

More "valuable" code can be found here:

Monday, October 11, 2010

VS2010 Code Snippets for Java Script

Just random notes about another "cool VS2010 feature"

"Visual Studio 2010 will include more than 200 built-in snippets
that you can immediately use when you install the product."

Excelent ;-) there are 13 "all useless" Java Script
snippets for basic lang constructs that hardly anyone needs
(if someone needs them I do not need her in my team)
including "deprecated with()" and "difficult to type alert()" ;-))

+ 5 incomplete XML Comments tags for VS2010 intellisence"
+ 3 MS Ajax snippets

Compared to easy creation of Toolbox items (drag and dropp)
the creation process of snippets
(XML file, edit, save, import etc..)
seems to be bit complicated if not unusable.

Snippet Designer

This helps with creation specially "extract snipped" context menu but still...
a lot of clicking to finish the snipped.
And if you make mistake (wrong language, wrong folder)
all gets complicated again.

After 30+ mins experimenting, I have managed to create this template
(it took me 5 minutes to press F4 and change properties ;-)))

What else we get with snippets ?


If your not convinced that saved you that many keystrokes, theres another handy feature. See how the first "i" is highlighted

and the second two are surrounded by dotted lines? That means that those variables are supposed to be the same - which mean

that if you edit the first "i" right after you insert the code snippet, the other "i"s change to the new name automatically.

That 'linkage' isn't kept around forever, as soon as you go and edit something other than the snippet, those dotted lines go

away and any changes that you make later on don't automatically get propagated. But pretty cool, eh?
Cool but quite useless for while and for cycles.. but keep reading

MSDN (Offical Docs)



a) type cnstr,TAB or cn,TAB,TAB and it will generate whole thing
b) change cnstr and p
and you have your own jQuery style construction pattern done.

After someone skilled creates template in 2-20 minutes
and ensures distribution of template to
"some sort of sharable location"
every "developer" can monkey type
and learn and memorize nothing.....

Next catch: Shortcut
This text is used as a shortcut for inserting the code snippet. The text value of a Shortcut element can only contain

alphanumeric characters, hyphens ( - ), and underscores ( _ ).
from http://msdn.microsoft.com/en-us/library/ms171440(v=VS.80).aspx

This is bug of code or bug of docs, if you name your shortcut with - or _
it will not work with TAB,TAB scenario.
It will be visisble in top level intellisence popup,
but will not expand.
So the best shortcut name to include some sort of namespacing is
hungarian notation as aitkCnstr, aitkModule, aitkSynchro etc...

So far...

I admit, this may be too quick experience but
i'm not impressed and I will love my "old memory" for obvious tasks
old fashion Toolbox + replace for more esoteric snippets.

I may write more when I learn more...

My first trial:

save somewhere to:
My Documents\Visual Studio 2010\Code Snippets\Visual Web Developer\My JScript Snippets\cnstr.snippet

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Title>cnstr (jQuery style)</Title>
<Description>construction pattern ala jQuery</Description>
<Literal Editable="true">
<Literal Editable="true">
<Code Language="jscript" Kind="type decl"><![CDATA[(function (window, undef) {

function $cnstr$($p$) {
/// <param name="$p$" type="String"></param>
/// <returns type="$cnstr$" />
return new $cnstr$.prototype.$cnstr$($p$);

$cnstr$.prototype = {
$cnstr$: function ($p$) {
this.$p$ = $p$;
$cnstr$.prototype.$cnstr$.prototype = $cnstr$.prototype;

// exports
window.$cnstr$ = $cnstr$;

VS2010 Web Development (Code Only) Profile.

1. Java Script validation is turned off
2. HTML validation is turned off

I do not know what MS calls web development,
but for me it is JS, HTML, and CSS....
Also Error List and other windows must be opened manually.
I do not like this "damned default",
so if hou click CTRL+SHIFT+J as mad
and no errors (you sure have some) appear in your Error List
now you know why.

Comming soon:
detailed compare of profile settings,
and maybe exported setting for
more realistic web code only development.

Friday, October 8, 2010

Fast and correct htmlEncoding for JavaScript

Feel free to adjust two last matches in the regexp to any of your needs. I encode all > 0x07 and from allowed ascii, I encode only big 5. Using regexp is still faster (specially on MSIE) than any other impl. var _encodeHtmlRegExpImpl = (function() { // performance is 78ms on MSIE 7 (the slowest one) // on 80KB html markup from: http://www.w3.org/TR/html4/ var re = new RegExp( // surrogate pair (sp) "([\uD800-\uDBFF][\uDC00-\uDFFF])" + // html UNUSED including standalone surogates (un) "|([\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F\uD800-\uDFFF])" + // out of ascii (oa) "|([^\u0000-\u007F])" + // big 5 + add others (b5) "|([\u0022\u0026\u0027\u003C\u003E])", "g" ), toCodePoint = function(high, low) { return ((high - 0xD800) << 10) + (low - 0xDC00) + 0x010000; }, enc = function(m, sp, un, oa, b5) { // extracted out from main function and ifs changed to ternary // thanx to Andrea Giammarchi return "&#" + (oa || b5 ? m.charCodeAt(0) : (un ? "xFFFD" : toCodePoint(m.charCodeAt(0), m.charCodeAt(1)))) + ";"; }; return function(s) { return s.replace(re, enc); } } ());