Skip to content
Myr

html.ts

This file

Path

src/utils/html.ts

44 lines · 1.1 KiB · TypeScript

Last changed by

refactor(`utils/html`): remove `escape` function (#450)Yusuke Wadabefore adoptiona7ad89c

History of this file

Take it

perf(jsx): JSX performance improvement (#444)Taku Amanobefore adoption85cbf3e

lines 1–2why, one step back
1export type HtmlEscaped = { isEscaped: true }
2export type HtmlEscapedString = string & HtmlEscaped

refactor(`utils/html`): rename `Buffer` to `StringBuffer` (#449)Yusuke Wadabefore adoption9f2a270

line 3why, one step back
3export type StringBuffer = [string]

feat: Introduce html template literals tag. (#312)Taku Amanobefore adoption9488e33

line 4why, one step back
4

refactor(`utils/html`): remove `escape` function (#450)Yusuke Wadabefore adoptiona7ad89c

line 5why, one step back
5// The `escapeToBuffer` implementation is based on code from the MIT licensed `react-dom` package.

perf(jsx): JSX performance improvement (#444)Taku Amanobefore adoption85cbf3e

lines 6–9why, one step back
6// https://github.com/facebook/react/blob/main/packages/react-dom/src/server/escapeTextForBrowser.js
78const escapeRe = /[&<>"]/9

refactor(`utils/html`): rename `Buffer` to `StringBuffer` (#449)Yusuke Wadabefore adoption9f2a270

line 10why, one step back
10export const escapeToBuffer = (str: string, buffer: StringBuffer): void => {

perf(jsx): JSX performance improvement (#444)Taku Amanobefore adoption85cbf3e

lines 11–43why, one step back
11 const match = str.search(escapeRe)
12 if (match === -1) {13 buffer[0] += str14 return15 }1617 let escape18 let index19 let lastIndex = 02021 for (index = match; index < str.length; index++) {22 switch (str.charCodeAt(index)) {23 case 34: // "24 escape = '&quot;'25 break26 case 38: // &27 escape = '&amp;'28 break29 case 60: // <30 escape = '&lt;'31 break32 case 62: // >33 escape = '&gt;'34 break35 default:36 continue37 }3839 buffer[0] += str.substring(lastIndex, index) + escape40 lastIndex = index + 141 }4243 buffer[0] += str.substring(lastIndex, index)

feat: Introduce html template literals tag. (#312)Taku Amanobefore adoption9488e33

line 44why, one step back
44}