Skip to content

Commit

Permalink
Properly decode unicode
Browse files Browse the repository at this point in the history
- properly decode unicode charsets
  • Loading branch information
CodeSmith32 committed Jun 17, 2024
1 parent d422aac commit 94df695
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/renderers/html/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,24 @@ import { dataURLFileLoader } from "../../utils/fileLoaders";
const HTMLRenderer: DocRenderer = ({ mainState: { currentDocument } }) => {
useEffect(() => {
const b64String = currentDocument?.fileData as string;

let encoding = "";
const bodyBase64 =
b64String?.replace(/^data:text\/html;([^;]*?;)?base64,/, "") || "";
const body: string = window.atob(bodyBase64);
b64String?.replace(
/^data:text\/html;(?:charset=([^;]*);)?base64,/,
(_, charset) => {
encoding = charset;
return "";
},
) || "";
let body: string = window.atob(bodyBase64);

if (encoding) {
// decode charset
const buffer = new Uint8Array(body.length);
for (let i = 0; i < body.length; i++) buffer[i] = body.charCodeAt(i);
body = new TextDecoder(encoding).decode(buffer);
}

const iframeCont = document.getElementById(
"html-body",
Expand Down

0 comments on commit 94df695

Please sign in to comment.