Skip to content

Commit 85d4dad

Browse files
authored
Merge pull request #275 from CodeSmith32/main
Proper dataurl charset handling
2 parents ad41662 + 94df695 commit 85d4dad

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/renderers/html/index.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,24 @@ import { dataURLFileLoader } from "../../utils/fileLoaders";
66
const HTMLRenderer: DocRenderer = ({ mainState: { currentDocument } }) => {
77
useEffect(() => {
88
const b64String = currentDocument?.fileData as string;
9-
const bodyBase64 = b64String?.replace("data:text/html;base64,", "") || "";
10-
const body: string = window.atob(bodyBase64);
9+
10+
let encoding = "";
11+
const bodyBase64 =
12+
b64String?.replace(
13+
/^data:text\/html;(?:charset=([^;]*);)?base64,/,
14+
(_, charset) => {
15+
encoding = charset;
16+
return "";
17+
},
18+
) || "";
19+
let body: string = window.atob(bodyBase64);
20+
21+
if (encoding) {
22+
// decode charset
23+
const buffer = new Uint8Array(body.length);
24+
for (let i = 0; i < body.length; i++) buffer[i] = body.charCodeAt(i);
25+
body = new TextDecoder(encoding).decode(buffer);
26+
}
1127

1228
const iframeCont = document.getElementById(
1329
"html-body",

0 commit comments

Comments
 (0)