Multiple issues; a patch with a test case forthcoming.
Created attachment 13810 [details] proposed fix
Comment on attachment 13810 [details] proposed fix r=me
Committed revision 20554.