From c16ac714160aceabd5f404b2f4be6c90369d0a49 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Thu, 13 Aug 2015 06:58:44 -0400
Subject: [PATCH] Fix base64.encode/decode for unicode characters - use fallback if btoa/atob functions fail

---
 program/js/common.js |   18 ++++++++++++++----
 1 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/program/js/common.js b/program/js/common.js
index 3babf1e..2c699d0 100644
--- a/program/js/common.js
+++ b/program/js/common.js
@@ -695,8 +695,13 @@
      * @param {String} input The string to encode in base64.
      */
     encode: function (input) {
-      if (typeof(window.btoa) === 'function')
-        return btoa(input);
+      if (typeof(window.btoa) === 'function') {
+        // it may fail on unicode characters, the fallback can handle them
+        try {
+          return btoa(input);
+        }
+        catch (e) {};
+      }
 
       var chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0, output = '', len = input.length;
 
@@ -728,8 +733,13 @@
      * @param {String} input The string to decode.
      */
     decode: function (input) {
-      if (typeof(window.atob) === 'function')
-         return atob(input);
+      if (typeof(window.atob) === 'function') {
+        // it may fail on unicode characters, the fallback can handle them
+        try {
+          return atob(input);
+        }
+        catch (e) {};
+      }
 
       var chr1, chr2, chr3, enc1, enc2, enc3, enc4, len, i = 0, output = '';
 

--
Gitblit v1.9.1