fix and tests for https://bugzilla.mozilla.org/show_bug.cgi?id=941037

thanks a lot to Vincent who helped me a great deal on this issue!