Fix js escaping

By doing all our escaping in one place at the last possible opportunity, all the values are properly escaped in `buffer` and we never have to do the bizarre un-escaping or double-escaping.  This should prove itself to be vastly more robust.

This fixes #830 and is much more robust than #847 which just piles another hack on top of the existing pile of hacks.

This depends on #962 so assuming #962 is merged on 2013-04-15 I will merge this if there are no complaints by 2013-04-15T18:00:00Z.

(Note only the last 2 commits are unique to this Pull Request)