Skip to content

Commit aa60ed6

Browse files
authored
Merge pull request #915 from metp/main
Fix data URL crash for Unicode characters
2 parents ee5610b + 85e2522 commit aa60ed6

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/wrapper/src/editorApp.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,14 @@ export class EditorApp {
279279
}
280280

281281
export const verifyUrlOrCreateDataUrl = (input: string | URL) => {
282-
return (input instanceof URL) ? input.href : new URL(`data:text/plain;base64,${btoa(input)}`).href;
282+
if (input instanceof URL) {
283+
return input.href;
284+
} else {
285+
const bytes = new TextEncoder().encode(input);
286+
const binString = Array.from(bytes, (b) => String.fromCodePoint(b)).join('');
287+
const base64 = btoa(binString);
288+
return new URL(`data:text/plain;base64,${base64}`).href;
289+
}
283290
};
284291

285292
export const didModelContentChange = (textModels: TextModels, onTextChanged?: (textChanges: TextContents) => void) => {

packages/wrapper/test/editorApp.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ describe('Test EditorApp', () => {
2222
test('verifyUrlorCreateDataUrl: url', async () => {
2323
const url = new URL('../../../node_modules/langium-statemachine-dsl/syntaxes/statemachine.tmLanguage.json', window.location.href);
2424
const text = await (await fetch(url)).text();
25-
expect(verifyUrlOrCreateDataUrl(text)).toBe(`data:text/plain;base64,${btoa(text)}`);
25+
const bytes = new TextEncoder().encode(text);
26+
const binString = Array.from(bytes, (b) => String.fromCodePoint(b)).join('');
27+
const base64 = btoa(binString);
28+
expect(verifyUrlOrCreateDataUrl(text)).toBe(`data:text/plain;base64,${base64}`);
29+
});
30+
31+
test('verifyUrlorCreateDataUrl: url', () => {
32+
const text = '✓✓';
33+
const bytes = new TextEncoder().encode(text);
34+
const binString = Array.from(bytes, (b) => String.fromCodePoint(b)).join('');
35+
const base64 = btoa(binString);
36+
expect(verifyUrlOrCreateDataUrl(text)).toBe(`data:text/plain;base64,${base64}`);
2637
});
2738

2839
test('config defaults', () => {

0 commit comments

Comments
 (0)