Skip to content

Commit 0886848

Browse files
committed
Fixed an issue with UTF-8 not being base64-encoded properly
1 parent a6a01ce commit 0886848

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

js/admin.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,23 @@ jQuery(document).ready(function($) {
320320
return str;
321321
}
322322

323+
var utf8btoa = function(str)
324+
{
325+
// see https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
326+
var binstr = '';
327+
(new TextEncoder()).encode(str).forEach(byte => binstr += String.fromCharCode(byte));
328+
return btoa(binstr);
329+
};
330+
331+
var utf8atob = function(str)
332+
{
333+
// see https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
334+
return (new TextDecoder()).decode(Uint8Array.from(atob(str), c => c.charCodeAt(0)));
335+
};
336+
323337
var base64urldecode = function(data)
324338
{
325-
return window.atob(strtr(data, {'.': '+', '_': '/'}).padEnd(data.length % 4, '='));
339+
return utf8atob(strtr(data, {'.': '+', '_': '/'}).padEnd(data.length % 4, '='));
326340
}
327341

328342
var getPdfFieldData = function(id)
@@ -549,7 +563,7 @@ jQuery(document).ready(function($) {
549563
var updatePluginDataField = function()
550564
{
551565
// we can't use json directly, we have to use encoding without slashes and quotes due to an unnecessary stripslashes() call in wpforms/includes/admin/builder/panels/class-base.php
552-
data_tag.val(btoa(JSON.stringify(pluginData)));
566+
data_tag.val(utf8btoa(JSON.stringify(pluginData)));
553567
}
554568

555569
var getAttachments = function()
@@ -777,7 +791,7 @@ jQuery(document).ready(function($) {
777791
var data_base64 = data_tag.val();
778792
var data_json = null;
779793
var data = {};
780-
try { if(data_base64) data_json = atob(data_base64); }
794+
try { if(data_base64) data_json = utf8atob(data_base64); }
781795
catch(e) { data_json = data_base64; }
782796
try { if(data_json) data = JSON.parse(data_json); }
783797
catch(e) { return errorMessage(e.message); }

0 commit comments

Comments
 (0)