Skip to content

Commit

Permalink
fix: Support unicode in REPL query param (#1162)
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian authored Jul 3, 2024
1 parent aeb46af commit d986799
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/components/controllers/repl-page.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useLocation, useRoute } from 'preact-iso';
import { Repl } from './repl';
import { base64ToText } from './repl/query-encode.js';
import { fetchExample } from './repl/examples';
import { useContent, useResource } from '../../lib/use-resource';
import { useTitle, useDescription } from './utils';
Expand Down Expand Up @@ -39,7 +40,7 @@ async function getInitialCode(query) {
const { route } = useLocation();
let code;
if (query.code) {
code = querySafetyCheck(atob(query.code));
code = querySafetyCheck() && base64ToText(query.code);
} else if (query.example) {
code = await fetchExample(query.example);
if (!code) {
Expand All @@ -62,13 +63,11 @@ async function getInitialCode(query) {
return code;
}

async function querySafetyCheck(code) {
function querySafetyCheck() {
return (
!document.referrer ||
document.referrer.indexOf(location.origin) === 0 ||
// eslint-disable-next-line no-alert
confirm('Are you sure you want to run the code contained in this link?')
)
? code
: undefined;
);
}
3 changes: 2 additions & 1 deletion src/components/controllers/repl/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useState } from 'preact/hooks';
import { useLocation, useRoute } from 'preact-iso';
import { Splitter } from '../../splitter';
import { textToBase64 } from './query-encode.js';
import { EXAMPLES, fetchExample } from './examples';
import { ErrorOverlay } from './error-overlay';
import { useStoredValue } from '../../../lib/localstorage';
Expand Down Expand Up @@ -67,7 +68,7 @@ export function Repl({ code }) {
if (!query.example) {
// We use `history.replaceState` here as the code is only relevant on mount.
// There's no need to notify the router of the change.
history.replaceState(null, null, `/repl?code=${encodeURIComponent(btoa(editorCode))}`);
history.replaceState(null, null, `/repl?code=${encodeURIComponent(textToBase64(editorCode))}`);
}

try {
Expand Down
19 changes: 19 additions & 0 deletions src/components/controllers/repl/query-encode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Encode text to base64 with unicode support
*
* @param {string} text
*/
export function textToBase64(text) {
const bytes = new TextEncoder().encode(text);
return btoa(String.fromCharCode(...bytes));
}

/**
* Decode text from base64 with unicode support
*
* @param {string} base64
*/
export function base64ToText(base64) {
const bytes = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
return new TextDecoder().decode(bytes);
}

0 comments on commit d986799

Please sign in to comment.