dracoblue.net

Fix DOM Exception 5 invalid character with atob

When I was developing on http://web.twtxt.org I ran into this mysterious javascript error:

InvalidCharacterError: DOM Exception 5: An invalid or illegal character was specified, such as in an XML name.
atobbundle.js:245
(anonyme Funktion)bundle.js:245
emitbundle.js:1119
handlebundle.js:1673
onreadystatechangebundle.js:1465

The source code in question took a base64 encoded content of the github api and converted it with window.atob into an utf8 string.

This worked well except for safari on iOS!

Thanks to Ross117 at SO I looked further at the base64 encoded string:

atob("MjAxNi0wMy0xOFQxOToxNzozOC4xMTNaCS9uaWNrIGRyYWNvYmx1ZQoyMDE2\nLTAzLTE4VDE5OjE3OjM4LjExM1oJL3R3dHVybCBodHRwczovL2RyYWNvYmx1");
// error:  DOM Exception 5: An invalid or illegal character was specified, such as in an XML name.

As you can see there is a small \n within the string. Chrome manages to ignore that, but Safari fails with this DOM Exception 5 error.

So this is my fix:

var input = "MjAxNi0wMy0xOFQxOToxNzozOC4xMTNaCS9uaWNrIGRyYWNvYmx1ZQoyMDE2\nLTAzLTE4VDE5OjE3OjM4LjExM1oJL3R3dHVybCBodHRwczovL2RyYWNvYmx1";
var output = atob(input.replace(/\s/g, ""));
// works!

to get rid of the whitespaces. I filled a support request at github, to ask if they can remove the additional \n in their api response, since it seems like it is not necessary at all.

In ios, javascript, safari by
@ 21 Mar 2016, Comments at Reddit & Hackernews

Give something back

Were my blog posts useful to you? If you want to give back, support one of these charities, too!

Report hate in social media Campact e.V. With our technology and your help, we protect the oceans from plastic waste. Gesellschaft fur Freiheitsrechte e. V. The civil eye in the mediterranean

Recent Dev-Articles

Read recently

Recent Files

About