madrobby/zepto — pull request #521 by romanrudenko (2012-05-25 21:51:03+00:00)
Title: Protect ternary operators from being damaged by Firefox toString() conversion

There is an interesting bug in Firefox. When it performs a toString() conversion of a Javascript function, it will strip away brackets around the ternary operator condition. Sometimes, it leads to surprising results.

For example, if we do (function(a,b,c,d,e) { return (a ? b : c) ? d : e; }).toString(), we would get "function (a, b, c, d, e) { return a ? b : c ? d : e; }". Calling first function with parameters (1,2,3,4,5) will return 4, but calling second one will return 2.

This breakable pattern is present in $.fn.toggle() and $.fn.toggleClass(), and is corrected in this patch by adding !! in front of the condition.

Now, you are probably wondering why would someone ever want to wrap Zepto in a function, call toString() on it an execute resulting code instead of running it directly. Environments where this could make sense include cross-frame or cross-domain development, rewriting existing document with document.open(), and extension development. The main window may load a bunch of scripts all concatenated in one file, and desire to send some of the scripts to child windows for execution. When one of these scripts is Zepto and target browser is Firefox, the bug rears it ugly head.