handle errors consistently

Currently, document.createElement is used in an attempt to generate an
error similar to those thrown by native implementations of window.btoa
and window.atob, throwing a ReferenceError in non-browser environments.

This commit adds and makes use of an InvalidCharacterError constructor
for environment-independent handling of errors.