Skip to content

Commit 680dfd8

Browse files
author
MichaelMitchell-at
committed
[TS Workbench] Correctly HTML escape rendered code
1 parent 3173c39 commit 680dfd8

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

packages/playground/src/ds/createDesignSystem.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Sandbox } from "@typescript/sandbox"
22
import type { DiagnosticRelatedInformation, Node } from "typescript"
3+
import { htmlEscape } from "../htmlEscape"
34

45
export type LocalStorageOption = {
56
blurb: string
@@ -154,7 +155,7 @@ export const createDesignSystem = (sandbox: Sandbox) => {
154155
createCodePre.setAttribute("tabindex", "0")
155156
const codeElement = document.createElement("code")
156157

157-
codeElement.innerHTML = code
158+
codeElement.innerHTML = htmlEscape(code)
158159

159160
createCodePre.appendChild(codeElement)
160161
container.appendChild(createCodePre)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function htmlEscape(str: string) {
2+
return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;")
3+
}

packages/playground/src/sidebar/runtime.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PlaygroundPlugin, PluginFactory } from ".."
22
import { createUI } from "../createUI"
3+
import { htmlEscape } from "../htmlEscape"
34
import { localize } from "../localizeWithFallback"
45

56
let allLogs: string[] = []
@@ -259,7 +260,3 @@ function rewireLoggingToElement(
259260
function sanitizeJS(code: string) {
260261
return code.replace(`import "reflect-metadata"`, "").replace(`require("reflect-metadata")`, "")
261262
}
262-
263-
function htmlEscape(str: string) {
264-
return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;")
265-
}

0 commit comments

Comments
 (0)