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 fileTake it
perf(jsx): JSX performance improvement (#444)Taku Amanobefore adoption85cbf3e
lines 1–2why, one step back1export type HtmlEscaped = { isEscaped: true }2export type HtmlEscapedString = string & HtmlEscapedrefactor(`utils/html`): rename `Buffer` to `StringBuffer` (#449)Yusuke Wadabefore adoption9f2a270
line 3why, one step back3export type StringBuffer = [string]feat: Introduce html template literals tag. (#312)Taku Amanobefore adoption9488e33
line 4why, one step back4refactor(`utils/html`): remove `escape` function (#450)Yusuke Wadabefore adoptiona7ad89c
line 5why, one step back5// 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 back6// https://github.com/facebook/react/blob/main/packages/react-dom/src/server/escapeTextForBrowser.js78const escapeRe = /[&<>"]/9refactor(`utils/html`): rename `Buffer` to `StringBuffer` (#449)Yusuke Wadabefore adoption9f2a270
line 10why, one step back10export const escapeToBuffer = (str: string, buffer: StringBuffer): void => {perf(jsx): JSX performance improvement (#444)Taku Amanobefore adoption85cbf3e
lines 11–43why, one step back11 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 = '"'25 break26 case 38: // &27 escape = '&'28 break29 case 60: // <30 escape = '<'31 break32 case 62: // >33 escape = '>'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 back44}