From 38dd017dcd8063c38156e620fffdc562f9f72ae7 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Fri, 24 Apr 2026 02:39:51 -0400 Subject: [PATCH] refactor: remove duplicate escape functions; improve escapeHtml function --- SharpbinV3.Client/src/lib/utils/html.ts | 12 ++++++++++++ .../src/lib/workers/highlight.worker.ts | 4 +--- SharpbinV3.Client/src/routes/[id]/+page.svelte | 14 +++++--------- 3 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 SharpbinV3.Client/src/lib/utils/html.ts diff --git a/SharpbinV3.Client/src/lib/utils/html.ts b/SharpbinV3.Client/src/lib/utils/html.ts new file mode 100644 index 0000000..6c66601 --- /dev/null +++ b/SharpbinV3.Client/src/lib/utils/html.ts @@ -0,0 +1,12 @@ +const htmlEscapeMap: Record = { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": ''', + '`': '`' +}; + +export function escapeHtml(value: string): string { + return value.replace(/[&<>"'`]/g, (char) => htmlEscapeMap[char]); +} diff --git a/SharpbinV3.Client/src/lib/workers/highlight.worker.ts b/SharpbinV3.Client/src/lib/workers/highlight.worker.ts index 12ee8e0..df3b98c 100644 --- a/SharpbinV3.Client/src/lib/workers/highlight.worker.ts +++ b/SharpbinV3.Client/src/lib/workers/highlight.worker.ts @@ -1,4 +1,5 @@ import hljs from 'highlight.js'; +import { escapeHtml } from '../utils/html'; type HighlightRequest = { id: number; @@ -11,9 +12,6 @@ type HighlightResponse = { html: string; }; -const escapeHtml = (value: string) => - value.replace(/&/g, '&').replace(//g, '>'); - self.onmessage = (event: MessageEvent) => { const { id, code, lang } = event.data; let html = ''; diff --git a/SharpbinV3.Client/src/routes/[id]/+page.svelte b/SharpbinV3.Client/src/routes/[id]/+page.svelte index 11cae44..dec4a12 100644 --- a/SharpbinV3.Client/src/routes/[id]/+page.svelte +++ b/SharpbinV3.Client/src/routes/[id]/+page.svelte @@ -42,6 +42,7 @@ import { syntaxes, expiresOptions } from '$lib/consts'; import { resolve } from '$app/paths'; import { decryptAES, encryptAES } from '$lib/utils/encryption'; + import { escapeHtml } from '$lib/utils/html'; import { fade } from 'svelte/transition'; import { getToken, hasRole } from '$lib/utils/auth'; import { user } from '$lib/stores/user'; @@ -181,8 +182,6 @@ } else { try { if (lang === 'plaintext') { - const escapeHtml = (s: string) => - s.replace(/&/g, '&').replace(//g, '>'); highlighted = escapeHtml(code); } else if (lang && hljs.getLanguage && hljs.getLanguage(lang)) { highlighted = hljs.highlight(code, { language: lang, ignoreIllegals: true }).value; @@ -191,8 +190,6 @@ } } catch { if (lang === 'plaintext') { - const escapeHtml = (s: string) => - s.replace(/&/g, '&').replace(//g, '>'); highlighted = escapeHtml(code); } else { highlighted = hljs.highlightAuto(code).value; @@ -223,19 +220,18 @@ const rows = results.data as string[][]; const [header, ...body] = rows; - const escape = (v: string) => - v.replace(/&/g, '&').replace(//g, '>'); - const tableHtml = ` - ${header.map((c) => ``).join('')} + ${header.map((c) => ``).join('')} ${body - .map((row) => `${row.map((c) => ``).join('')}`) + .map( + (row) => `${row.map((c) => ``).join('')}` + ) .join('')}
${c ? escape(c) : ''}${c ? escapeHtml(c) : ''}
${c ? escape(c) : ''}
${c ? escapeHtml(c) : ''}