Skip to content

Commit

Permalink
Merge pull request #275 from CodeSmith32/main
Browse files Browse the repository at this point in the history
Proper dataurl charset handling
  • Loading branch information
cyntler authored Jun 28, 2024
2 parents ad41662 + 94df695 commit 85d4dad
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/renderers/html/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,24 @@ import { dataURLFileLoader } from "../../utils/fileLoaders";
const HTMLRenderer: DocRenderer = ({ mainState: { currentDocument } }) => {
useEffect(() => {
const b64String = currentDocument?.fileData as string;
const bodyBase64 = b64String?.replace("data:text/html;base64,", "") || "";
const body: string = window.atob(bodyBase64);

let encoding = "";
const 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 85d4dad

Please sign in to comment.