Skip to content

Commit

Permalink
Resolves #1088 Special characters (like Umlaut) are not displayed cor…
Browse files Browse the repository at this point in the history
…rectly in client-side messages
  • Loading branch information
mgesing committed May 14, 2024
1 parent 3e2216e commit f2c732d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private static void Persist(IDictionary<string, object> bag, IEnumerable<NotifyE
.ToArray();

bag[NotificationsAccessKey] = TrimSet(existingHolder);

}

private static void HandleAjaxRequest(NotifyEntry entry, HttpResponse response)
Expand All @@ -67,7 +66,7 @@ private static void HandleAjaxRequest(NotifyEntry entry, HttpResponse response)
return;

response.Headers["X-Message-Type"] = entry.Type.ToString().ToLower();
response.Headers["X-Message"] = Convert.ToBase64String(entry.Message.ToString().GetBytes());
response.Headers["X-Message"] = Convert.ToBase64String(entry.Message.GetBytes());
}

private static NotifyEntriesHolder TrimSet(NotifyEntriesHolder holder)
Expand Down
26 changes: 26 additions & 0 deletions src/Smartstore.Web/wwwroot/js/smartstore.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@
return $('<div/>').html(value).text();
};

window.base64Encode = function (value) {
if (value) {
try {
var bytes = new TextEncoder().encode(value);
value = btoa(Array.from(bytes, (byte) => String.fromCodePoint(byte)).join(""));
}
catch (e) { }
}

return value;
};

// https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
// https://stackoverflow.com/a/30106551/23705546
window.base64Decode = function (value) {
if (value) {
try {
const bytes = Uint8Array.from(atob(value), (m) => m.codePointAt(0));
value = new TextDecoder().decode(bytes);
}
catch (e) { }
}

return value;
};

// TODO: Move to another location when current summernote developments are finished.
window.insertHtmlInSummernote = function (field, value) {
field.val(value);
Expand Down
20 changes: 1 addition & 19 deletions src/Smartstore.Web/wwwroot/js/smartstore.dropzoneWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1043,26 +1043,8 @@
}
}

function decodeMessage(str) {
if (str) {
try {
str = atob(str);
}
catch (e) { }

try {
return decodeURIComponent(escape(str));
}
catch (e) {
return str;
}
}

return str;
}

var showNotification = _.throttle(function (msg, type) {
displayNotification(decodeMessage(msg), type);
displayNotification(base64Decode(msg), type);
}, 750, { leading: true, trailing: true });

})(jQuery);
Expand Down
24 changes: 3 additions & 21 deletions src/Smartstore.Web/wwwroot/js/smartstore.globalinit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,6 @@ jQuery(function () {
win = $(window),
body = $(document.body);

function decode(str) {
if (str) {
try {
str = atob(str);
}
catch (e) { }

try {
return decodeURIComponent(encodeURIComponent(str));
}
catch (e) {
return str;
}
}

return str;
}

// Adjust initPNotify global defaults
if (typeof PNotify !== 'undefined') {
var stack = {
Expand Down Expand Up @@ -139,19 +121,19 @@ jQuery(function () {
.ajaxSuccess(function (e, xhr) {
var msg = xhr.getResponseHeader('X-Message');
if (msg) {
displayNotification(decode(msg), xhr.getResponseHeader('X-Message-Type'));
displayNotification(base64Decode(msg), xhr.getResponseHeader('X-Message-Type'));
}
})
.ajaxError(function (e, xhr) {
var msg = xhr.getResponseHeader('X-Message');
if (msg) {
displayNotification(decode(msg), xhr.getResponseHeader('X-Message-Type'));
displayNotification(base64Decode(msg), xhr.getResponseHeader('X-Message-Type'));
}
else {
try {
var data = JSON.parse(xhr.responseText);
if (data.message) {
displayNotification(decode(data.message), "error");
displayNotification(base64Decode(data.message), "error");
}
}
catch (ex) {
Expand Down

0 comments on commit f2c732d

Please sign in to comment.