html-escaper

中文文档
3.0.3发布于 2021/02/18MITES Module零依赖近 30 天下载 3.6亿

fast and safe way to escape and unescape &<>'" chars

原始使用文档
文档目录4 节
  1. V3 ESM Only Release
  2. How
  3. Why
  4. Backtick

html-escaper

Downloads Build Status Coverage Status WebReflection status

A simple module to escape/unescape common problematic entities.

Go sloppy if you like!

If you'd like to deal with any kind of input, including null or undefined, and even symbol kind, check html-sloppy-escaper out: it's this very same module, except it never throws errors 👍

V3 ESM Only Release

The version 3 of this module ditches entirely legacy browsers and nodejs with broken loaders, such as v13.0.0 and v13.1.0.

As the code is basically identical, simply stick with version 2 if you have any issue with this one 👋

How

This package is available in npm so npm install html-escaper is all you need to do, using eventually the global flag too.

Once the module is present

import {escape, unescape} from 'html-escaper';

escape('string');
unescape('escaped string');

Why

there is basically one rule only: do not ever replace one char after another if you are transforming a string into another.

// WARNING: THIS IS WRONG
// if you are that kind of dev that does this
function escape(s) {
  return s.replace(/&/g, "&amp;")
          .replace(/</g, "&lt;")
          .replace(/>/g, "&gt;")
          .replace(/'/g, "&#39;")
          .replace(/"/g, "&quot;");
}

// you might be the same dev that does this too
function unescape(s) {
  return s.replace(/&amp;/g, "&")
          .replace(/&lt;/g, "<")
          .replace(/&gt;/g, ">")
          .replace(/&#39;/g, "'")
          .replace(/&quot;/g, '"');
}

// guess what we have here ?
unescape('&amp;lt;');

// now guess this XSS too ...
unescape('&amp;lt;script&amp;gt;alert("yo")&amp;lt;/script&amp;gt;');


The last example will produce <script>alert("yo")</script> instead of the expected &lt;script&gt;alert("yo")&lt;/script&gt;.

Nothing like this could possibly happen if we grab all chars at once and either ways. It's just a fortunate case that after swapping & with &amp; no other replace will be affected, but it's not portable and universally a bad practice.

Grab all chars at once, no excuses!

more details As somebody might think it's an unescape issue only, it's not. Being an anti-pattern with side effects works both ways.

As example, changing the order of the replacement in escaping would produce the unexpected:

function escape(s) {
  return s.replace(/</g, "&lt;")
          .replace(/>/g, "&gt;")
          .replace(/'/g, "&#39;")
          .replace(/"/g, "&quot;")
          .replace(/&/g, "&amp;");
}

escape('<'); // &amp;lt; instead of &lt;

If we do not want to code with the fear that the order wasn't perfect or that our order in either escaping or unescaping is different from the order another method or function used, if we understand the issue and we agree it's potentially a disaster prone approach, if we add the fact in this case creating 4 RegExp objects each time and invoking 4 times .replace trough the String.prototype is also potentially slower than creating one function only holding one object, or holding the function too, we should agree there is not absolutely any valid reason to keep proposing a char-by-char implementation.

We have proofs this approach can fail already so ... why should we risk? Just avoid and grab all chars at once or simply use this tiny utility.

Backtick

Internt explorer < 9 has some backtick issue

For compatibility sake with common server-side HTML entities encoders and decoders, and in order to have the most reliable I/O, this little utility will NOT fix this IE < 9 problem.

It is also important to note that if we create valid HTML and we set attributes at runtime through this utility, backticks in strings cannot possibly affect attribute behaviors.

var img = new Image();
img.src = html.escape(
  'x` `<script>alert(1)</script>"` `'
);
// it won't cause problems even in IE < 9

However, if you use innerHTML and you target IE < 9 then this might be a problem.

Accordingly, if you need more chars and/or backticks to be escaped and unescaped, feel free to use alternatives like lodash or he

Here a bit more of my POV and why I haven't implemented same thing alternatives did. Good news: those are alternatives ;-)

版本与发布记录

html-escaper 共发布 9 个版本,首次发布于 2015/04/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.3latest2021/02/185 年前
3.0.22021/02/185 年前
3.0.12021/02/185 年前
3.0.02020/03/276 年前
2.0.22020/03/276 年前
2.0.12020/03/216 年前

关于 html-escaper 的常见问题

html-escaper 是什么?

fast and safe way to escape and unescape &<>'" chars

如何安装 html-escaper?

在项目目录运行 npm install html-escaper,或使用 pnpm add / yarn add / bun add。

html-escaper 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/html-escaper 是否存在。

html-escaper 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

html-escaper 可以商用吗?

html-escaper 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

html-escaper 的下载量有多少?

根据 npm 官方下载统计,html-escaper 在 2026/10/05 – 2026/11/03 这 30 天内被下载 355,511,330 次,日均约 11,850,378 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

html-escaper 还在维护吗?

最新版本 3.0.3 发布于 2021/02/18(5 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2015/04/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。