Skip to content

fix(editor): Fix the issue that the contents of json, html, csv, md, txt, and css files contain garbled Chinese characters #16118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ref, onMounted, computed } from 'vue';
import { useWorkflowsStore } from '@/stores/workflows.store';
import type { IBinaryData } from 'n8n-workflow';
import { jsonParse } from 'n8n-workflow';
import { jsonParse, base64DecodeUTF8 } from 'n8n-workflow';
import VueJsonPretty from 'vue-json-pretty';
import RunDataHtml from '@/components/RunDataHtml.vue';
import { useI18n } from '@n8n/i18n';
Expand All @@ -28,12 +28,13 @@ onMounted(async () => {
const { id, data: binaryData, fileName, fileType, mimeType } = props.binaryData;
const isJSONData = fileType === 'json';
const isHTMLData = fileType === 'html';

if (!id) {
if (isJSONData || isHTMLData) {
data.value = jsonParse(atob(binaryData));
data.value = isJSONData
? jsonParse(base64DecodeUTF8(binaryData))
: base64DecodeUTF8(binaryData);
} else {
embedSource.value = 'data:' + mimeType + ';base64,' + binaryData;
embedSource.value = `data:${mimeType};charset=utf-8;base64,${binaryData}`;
}
} else {
try {
Expand Down
1 change: 1 addition & 0 deletions packages/workflow/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
isObjectEmpty,
deepCopy,
jsonParse,
base64DecodeUTF8,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The base64DecodeUTF8 function uses a deprecated escape() function in its fallback path which is marked for removal from JavaScript.

jsonStringify,
replaceCircularReferences,
sleep,
Expand Down
22 changes: 22 additions & 0 deletions packages/workflow/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ type JSONStringifyOptions = {
replaceCircularRefs?: boolean;
};

/**
* Decodes a Base64 string with proper UTF-8 character handling.
*
* @param str - The Base64 string to decode
* @returns The decoded UTF-8 string
*/
export const base64DecodeUTF8 = (str: string): string => {
try {
// Use modern TextDecoder for proper UTF-8 handling
const bytes = new Uint8Array(
atob(str)
.split('')
.map((char) => char.charCodeAt(0)),
);
return new TextDecoder('utf-8').decode(bytes);
} catch (error) {
// Fallback method for older browsers
console.warn('TextDecoder not available, using fallback method');
return atob(str);
}
};

export const replaceCircularReferences = <T>(value: T, knownObjects = new WeakSet()): T => {
if (typeof value !== 'object' || value === null || value instanceof RegExp) return value;
if ('toJSON' in value && typeof value.toJSON === 'function') return value.toJSON() as T;
Expand Down