refactor(`utils/html`): remove `escape` function (#450)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #445
authored by Yusuke Wada · committed by Yusuke Wada · parent 9f2a270 · Browse files · 3 files·+3 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+3 added−38 removed
- tests:+1 added−10 removed
- other:+2 added−28 removed
Provenance
- Commit
a7ad89c- Tree
f0deca7- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
Yusuke Wada <yusuke@kamawada.com>- Parent
9f2a270
3 files changed+3 added−38 removed
modified:deno_dist/utils/html.ts+1 added-14 removed
1234556789101112131415161718192021221011121331 unmodified linesexport type HtmlEscaped = { isEscaped: true }export type HtmlEscapedString = string & HtmlEscapedexport type StringBuffer = [string]
// The `escape` and `escapeToBuffer` implementations are based on code from the MIT licensed `react-dom` package.// The `escapeToBuffer` implementation is based on code from the MIT licensed `react-dom` package.// https://github.com/facebook/react/blob/main/packages/react-dom/src/server/escapeTextForBrowser.js
const escapeRe = /[&<>"]/
export const escape = (str: string): string => { // This function is an alias for `escapeToBuffer`, // but returns immediately if `str` does not contain the character to be escaped. const match = str.search(escapeRe) if (match === -1) { return str }
const buffer: StringBuffer = [''] escapeToBuffer(str, buffer) return buffer[0]}
export const escapeToBuffer = (str: string, buffer: StringBuffer): void => { const match = str.search(escapeRe) if (match === -1) { buffer[0] += str31 unmodified linesmodified:src/utils/html.test.ts+1 added-10 removed
11234567891011121356788 unmodified linesimport { escape, escapeToBuffer } from './html'import { escapeToBuffer } from './html'import type { StringBuffer } from './html'
describe('HTML utilities', () => { describe('escape', () => { it('Should escape special characters', () => { expect(escape('I <b>think</b> this is good.')).toBe( 'I <b>think</b> this is good.' ) expect(escape('John "Johnny" Smith')).toBe('John "Johnny" Smith') }) })
describe('escapeToBuffer', () => { it('Should escape special characters', () => { let buffer: StringBuffer = [''] escapeToBuffer('I <b>think</b> this is good.', buffer)8 unmodified linesmodified:src/utils/html.ts+1 added-14 removed
1234556789101112131415161718192021221011121331 unmodified linesexport type HtmlEscaped = { isEscaped: true }export type HtmlEscapedString = string & HtmlEscapedexport type StringBuffer = [string]
// The `escape` and `escapeToBuffer` implementations are based on code from the MIT licensed `react-dom` package.// The `escapeToBuffer` implementation is based on code from the MIT licensed `react-dom` package.// https://github.com/facebook/react/blob/main/packages/react-dom/src/server/escapeTextForBrowser.js
const escapeRe = /[&<>"]/
export const escape = (str: string): string => { // This function is an alias for `escapeToBuffer`, // but returns immediately if `str` does not contain the character to be escaped. const match = str.search(escapeRe) if (match === -1) { return str }
const buffer: StringBuffer = [''] escapeToBuffer(str, buffer) return buffer[0]}
export const escapeToBuffer = (str: string, buffer: StringBuffer): void => { const match = str.search(escapeRe) if (match === -1) { buffer[0] += str31 unmodified lines