Library / SDK
mathiasbynens/he avatar
mathiasbynens/he

he encodes HTML entities as hex escapes by default and decodes them the way a browser parser would

A robust HTML entity encoder/decoder written in JavaScript.

3,614 stars266 forksJavaScriptMIT

At a glance

What is it?
mathiasbynens/he is a small MIT-licensed JavaScript library that encodes and decodes HTML character references. Version 2.0.0 is an ES module that needs Node 22 or later, and its defaults are tuned for old-browser safety rather than short output.
Who is it for?
Use he when you need to turn text into HTML-safe character references or decode references the way an HTML parser would, and you can run Node 22 or later with ES modules. Do not use it for sanitising untrusted markup, since it converts characters and does not remove tags.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 18 days ago.
What is it written in?
Mainly JavaScript, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 25, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • mathiasbynens/he3,614 ★ · +1
Star history
Repository2026-11-032026-11-25
mathiasbynens/he3.6k3.6k

1 Stars in the last 22 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

he treats entity decoding as a browser would, including ambiguous ampersands

he, short for HTML entities, is a JavaScript encoder and decoder for HTML character references by Mathias Bynens. Its README makes four claims. It supports all standardized named character references from the HTML specification. It handles ambiguous ampersands and other edge cases just like a browser would, following the spec's tokenizing rules for character references. It has an extensive test suite. And it handles astral Unicode symbols, which the README says many other JavaScript solutions do not.

Astral symbols are the characters beyond the Basic Multilingual Plane, the ones that JavaScript strings store as surrogate pairs. The README's own encode example shows one: the symbol 𝌆 comes out as the single reference 𝌆, not as two halves. A library that walks a string one UTF-16 unit at a time would split it.

The package metadata describes the same thing as a HTML entities encoder and decoder with full Unicode support. The license is MIT, the author is Mathias Bynens, and the homepage is https://mths.be/he. An online demo is linked from the README at mothereff.in/html-entities, which lets you try an input before adding the dependency.

Version 2.0.0 ships only as an ES module with four named exports

Install is a single npm command:

bash
npm install he

The package.json has type set to module, and its exports map points the root at ./src/he.mjs and also exposes ./package.json. There is no second entry for CommonJS. The README says he is published as an ECMAScript module with named exports, and the import line is:

js
import { decode, encode, escape, unescape } from 'he';

If you prefer the he.* style used in the README's examples, import the namespace instead with import * as he from 'he'. That gives you he.encode, he.decode and the rest.

In a browser you load the file with a script of type module and a path to the package's src/he.mjs, for example ./node_modules/he/src/he.mjs. There is no pre-bundled global script in the published files, which are LICENSE-MIT.txt, src/, bin/ and man/.

CommonJS code is supported only on Node versions with require(esm). There, require('he') returns the module namespace object, so require('he').encode(…) works. On a Node release without that feature, require fails and you must switch the calling file to import. The engines field in package.json asks for Node 22 or later, so the library is not offered for anything older.

encode escapes everything outside printable ASCII and writes hexadecimal references

he.encode(text, options) takes a string and, by default, replaces two groups of characters with references: any symbol that is not printable ASCII, and the six characters &, <, >, double quote, single quote and the backtick. The README's example input is 'foo © bar ≠ baz 𝌆 qux', and the default output is 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'. Every non-ASCII symbol became a hexadecimal escape.

The output is always valid HTML as long as the input holds only allowed code points. Invalid code points that cannot be written as a character reference are left alone, so he.encode('foo \0 bar') returns 'foo \0 bar' with the null character intact. If you hand such text to a validator afterward, the invalid character is still there.

To fail instead of passing invalid characters through, turn on the strict option. With strict enabled, he.encode either throws, when the input contains invalid code points, or returns a string of valid HTML. The README says this makes it possible to use he as part of HTML parsers and validators. In the default error-tolerant mode, he.encode('\x01') returns '&#x1;', and with strict set to true the same call produces a parse error.

The options object is optional, and every option below defaults to false.

Named references are off by default because older browsers may not know them

The useNamedReferences option defaults to false, so encode writes hexadecimal escapes such as &#xA9; instead of named references such as &copy;. Set it to true and the same sample string becomes 'foo &copy; bar &ne; baz &#x1D306; qux'. The astral symbol still comes out numeric, because no named reference exists for it in that output.

js
he.encode('foo © bar ≠ baz 𝌆 qux', {
	useNamedReferences: true,
});
// → 'foo &copy; bar &ne; baz &#x1D306; qux'

The README adds a warning: if compatibility with older browsers is a concern, this option should remain disabled. The cost of the default is longer, less readable output. The cost of turning it on is that the page can break in a browser that does not recognise a given name.

The decimal option, also false by default, switches numeric escapes from hexadecimal to decimal, so &#xA9; becomes &#169; and &#x1D306; becomes &#119558;. It combines with useNamedReferences. When both are enabled, named references win over decimal escapes, and symbols without a name fall back to decimal. This matters if a downstream system only accepts decimal numeric references.

encodeEverything and allowUnsafeSymbols pull in opposite directions, and encodeEverything wins

encodeEverything defaults to false, which means printable ASCII that needs no escaping is left alone. Set it to true and every symbol in the input is encoded, so 'foo' becomes &#x66;&#x6F;&#x6F; and the whole sample string turns into a run of hexadecimal references. It can be combined with useNamedReferences.

allowUnsafeSymbols goes the other way. It also defaults to false, which means the unsafe characters (&, <, >, double quote, single quote and the backtick) are encoded. Set it to true and only non-ASCII characters are encoded, leaving those six characters raw in the output.

The README is explicit about precedence. When encodeEverything is true, it takes precedence, and setting allowUnsafeSymbols to true in that case has no effect. The second option is ignored. A caller who sets both expecting a middle path gets full encoding.

The consequence of allowUnsafeSymbols is a safety one. Output with raw angle brackets and ampersands is only safe to place into HTML if the input is already trusted markup or you are writing into a context where those characters have no meaning. For text going into an attribute or element body from a user, keep the default.

escape, unescape and decode cover cases beyond full encoding, with the details outside what is retrieved

The import line lists four functions: decode, encode, escape and unescape. The encode section is documented in full above. The retrieved README text is cut off partway through the encode options, so the details of decode, escape and unescape, along with any decode options, are not available here. Read that part of the README before relying on their exact behavior.

The name pairing suggests a split. encode and decode are the full conversions between text and character references. escape and unescape are separate functions, and the library's own repository holds a man page at man/he.1 and a bin/he.mjs script declared as the he command in package.json. How the command line takes input and which flags it accepts are not in the retrieved text either.

One claim does carry over from the summary at the top of the README: decoding handles ambiguous ampersands and edge cases as a browser would. That is a statement about behavior, and the project backs it with a test suite run through node --test over tests/**/*.test.mjs. A coverage script uses the experimental test coverage flag against src. Dependencies for the tests and build are jsesc, linkedom, oxfmt and regenerate, all dev-only, so installing he adds no runtime dependencies to your project.

The entity table is generated from the WHATWG spec, and the repository has no GitHub releases

The data behind the named references is not hand-typed. The fetch script runs curl against https://html.spec.whatwg.org/entities.json, saves it to data/entities.json, then runs another fetch-spec script and the build. The build script runs process-data.mjs, then build.mjs, then oxfmt on data and src/data.mjs. Updating the library to a new spec revision is therefore a command sequence, not an edit.

Maintenance status is mixed. The repository is not archived, the default branch is main, and the last push was on 2026-10-04. The package is at version 2.0.0, and the repository has no GitHub releases, so the project does not publish release notes there. To see what changed in 2.0.0 compared with an older install, compare package versions on npm and read the commit history yourself.

That matters because 2.0.0 is the module-only package with the Node 22 floor. An application still on CommonJS or on an older Node cannot take the update unchanged. Pinning the version you already run is the safe move until you have moved the import style and the runtime.

He is a converter, not a sanitiser. It turns characters into references and back, and nothing in the README claims it removes script tags or filters attributes.

Editorial conclusion

Use he when you need to turn text into HTML-safe character references or decode references the way an HTML parser would, and you can run Node 22 or later with ES modules. Do not use it for sanitising untrusted markup, since it converts characters and does not remove tags. Before upgrading from an older release, check that your import style works with the 2.0.0 module-only package and that your Node version meets the engines field.

Frequently asked questions

How can I decode HTML code with he?

Import decode from the he package, or import the whole namespace as he, and call it on a string with character references. The README says it decodes ambiguous ampersands and edge cases the way a browser would.

What do HTML entities do, and what does he do with them?

he converts between text and HTML character references. By default encode turns non-ASCII symbols and the characters &, <, >, quotes and the backtick into references such as &#xA9;.

How to write an ampersand in HTML using he?

Call he.encode on the text. By default it encodes & together with <, >, double quote, single quote and the backtick. Setting allowUnsafeSymbols to true leaves those characters unencoded, so keep it false for user text.

Official sources

  1. Issues
  2. License: MIT
  3. mathiasbynens/he on GitHub
  4. Project website
  5. README
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/mathiasbynens-he.svg)](https://hysenlabs.com/projects/mathiasbynens-he)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/mathiasbynens-he.svg)](https://hysenlabs.com/projects/mathiasbynens-he)

Was this useful?