Skip to main content
On this page

@std/html

Overview Jump to heading

Functions for HTML tasks such as escaping or unescaping HTML entities.

import { unescape } from "@std/html/entities";
import { assertEquals } from "@std/assert";

assertEquals(unescape("&lt;&gt;'&amp;AA"), "<>'&AA");
assertEquals(unescape("&thorn;&eth;"), "&thorn;&eth;");

Add to your project Jump to heading

deno add jsr:@std/html

See all symbols in @std/html on

这个包是什么? Jump to heading

一个实用库,用于安全地转义和反转义 HTML 实体,以防止在将用户提供的内容插入 HTML 时发生 XSS 漏洞。

为什么使用 @std/html? Jump to heading

你的应用可能需要在 HTML 中显示用户生成的内容。为了防止跨站脚本攻击 (XSS),在将用户输入嵌入 HTML 之前,关键在于转义诸如 <>&"' 等特殊字符。

示例 Jump to heading

import { escape, unescape } from "@std/html/entities";

const safe = escape(`<img src=x onerror=alert(1)>`); // &lt;img src=x onerror=alert(1)&gt;
const back = unescape("&amp;lt;b&amp;gt;ok&amp;lt;/b&amp;gt;"); // <b>ok</b>

提示 Jump to heading

  • 转义和反转义仅针对实体,不是完整的 HTML 消毒。若需移除标签/属性,请使用消毒器。
  • 转义操作是幂等的;避免重复转义(例如 &amp;amp;)。

你找到了你需要的东西吗?

编辑此页面
隐私政策