escapeHtml()
문자열의 HTML 특수 문자를 이스케이프합니다.
import { escapeHtml } from "@unifast/core";시그니처
function escapeHtml(str: string): string매개변수
str
| 속성 | 타입 | 기본값 | 설명 |
|---|---|---|---|
str | string | — | 이스케이프할 문자를 포함한 문자열 |
반환값
string — &, <, >, "가 각각의 HTML 엔티티로 치환된 문자열.
사용법
import { escapeHtml } from "@unifast/core";
const safe = escapeHtml('<script>alert("xss")</script>');
console.log(safe);
// <script>alert("xss")</script>예시
기본 이스케이프
import { escapeHtml } from "@unifast/core";
console.log(escapeHtml("Tom & Jerry"));
// Tom & Jerry
console.log(escapeHtml('class="main"'));
// class="main"
console.log(escapeHtml("1 < 2 > 0"));
// 1 < 2 > 0사용자 생성 콘텐츠 이스케이프
import { escapeHtml } from "@unifast/core";
const userComment = '<img src=x onerror="alert(1)">';
const html = `<div class="comment"></div>`;
console.log(html);
// <div class="comment"><img src=x onerror="alert(1)"></div>안전한 HTML 속성 만들기
import { escapeHtml } from "@unifast/core";
const title = 'He said "hello" & waved';
const html = `<span title="">Hover me</span>`;
console.log(html);
// <span title="He said "hello" & waved">Hover me</span>동작
&는&로 치환됩니다.<는<로 치환됩니다.>는>로 치환됩니다."는"로 치환됩니다.그 외의 문자는 그대로 유지됩니다.