Skip to content

Commit

Permalink
Fix UTF-8 interoprtability.
Browse files Browse the repository at this point in the history
  • Loading branch information
dom111 committed May 11, 2022
1 parent aa09a47 commit d33c066
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
1 change: 0 additions & 1 deletion js/IO.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Editor } from 'codemirror';
import { decoders } from './Decoders';
import Renderers from './Renderers';

export class IO {
Expand Down
2 changes: 1 addition & 1 deletion js/Inputs/Code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class Code extends Abstract implements Input {
}

public readAsString(binaryReplacementChar: string | null = '.'): string {
const code = this.editor.getValue();
const code = unescape(encodeURIComponent(this.editor.getValue()));

if (binaryReplacementChar === null) {
return code;
Expand Down
11 changes: 9 additions & 2 deletions js/Renderers/Abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ export abstract class Abstract implements Renderer {
this.resize();
}

protected createElement(tag: string = 'div'): HTMLElement {
protected createElement(tag: 'div', hidden?: boolean): HTMLDivElement;
protected createElement(tag: 'iframe', hidden?: boolean): HTMLIFrameElement;
protected createElement(
tag: string = 'div',
hidden: boolean = true
): HTMLElement {
const element = document.createElement(tag);

element.setAttribute('hidden', '');
if (hidden) {
element.setAttribute('hidden', '');
}

return element;
}
Expand Down
14 changes: 12 additions & 2 deletions js/Renderers/IFrame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class IFrame extends Abstract implements Renderer {
public constructor(parent: HTMLElement) {
super();

this.container = this.createElement('iframe') as HTMLIFrameElement;
this.container = this.createElement('iframe');

parent.append(this.container);

Expand Down Expand Up @@ -43,7 +43,17 @@ export class IFrame extends Abstract implements Renderer {
}

private update(): void {
this.container.src = `data:${this.mimeType};base64,${btoa(this.buffer)}`;
// We need to destroy and recreate the IFrame so that we don't clobber the back button.
const container = this.createElement(
'iframe',
this.container.hasAttribute('hidden')
);

container.src = `data:${this.mimeType};base64,${btoa(this.buffer)}`;

this.container.replaceWith(container);

this.container = container;
}
}

Expand Down
8 changes: 4 additions & 4 deletions js/UI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,11 +553,11 @@ export class UI {
}

this.setLang(data.lang ?? this.getLangId());
this.codeHeader.write(data.header ?? '');
this.code.write(data.code ?? '');
this.codeFooter.write(data.footer ?? '');
this.codeHeader.write(decodeURIComponent(escape(data.header ?? '')));
this.code.write(decodeURIComponent(escape(data.code ?? '')));
this.codeFooter.write(decodeURIComponent(escape(data.footer ?? '')));
this.io.setArgs(data.args ?? '');
this.io.setStdin(data.input ?? '');
this.io.setStdin(decodeURIComponent(escape(data.input ?? '')));

if (data.mime) {
this.setMimeType(data.mime);
Expand Down
2 changes: 1 addition & 1 deletion js/replaceBinaryBytes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Don't replace newlines
export const replaceBinaryBytes = (code: string, replacement: string = '.') =>
code.replace(/[\x00-\x09\x0b-\x1f\x7f-\xff]/g, replacement);
code.replace(/[^\x09\x0a\x20-\x7e]/g, replacement);

export default replaceBinaryBytes;

0 comments on commit d33c066

Please sign in to comment.