dom-serializer

中文文档
3.1.1发布于 2026/05/02MITTypeScriptES Module构建来源已验证近 30 天下载 4.7亿

render domhandler DOM nodes to a string

原始使用文档
文档目录8 节
  1. render
  2. Options
  3. encodeEntities
  4. decodeEntities
  5. emptyAttrs
  6. selfClosingTags
  7. xmlMode
  8. Ecosystem

dom-serializer Node.js CI

Renders a domhandler DOM node or an array of domhandler DOM nodes to a string.

import render from "dom-serializer";

// OR

const render = require("dom-serializer").default;

API

render

▸ render(node: Node | Node[], options?: Options): string

Renders a DOM node or an array of DOM nodes to a string.

Can be thought of as the equivalent of the outerHTML of the passed node(s).

Parameters:

NameTypeDefault valueDescription
nodeNode | Node[]-Node to be rendered.
optionsDomSerializerOptions{}Changes serialization behavior

Returns: string

Options

encodeEntities

• Optional encodeEntities: boolean | "utf8"

Encode characters reserved in HTML or XML in text and attribute values.

If xmlMode is true or the value is not 'utf8', characters outside of the ASCII range will be encoded as well.

Security: Setting this to false disables encoding of <, >, and & in text and attribute values. This is intended for the round-trip case where the DOM was parsed with decodeEntities: false, so markup characters only exist as entity references. If the DOM contains raw markup characters (e.g., from a default-decoded parse, or from programmatic manipulation), they will be emitted literally — do not use this option with untrusted input unless you have validated the DOM yourself.

default decodeEntities


decodeEntities

• Optional decodeEntities: boolean

Default for encodeEntities. Named to match the parser option of the same name so a single options object can be threaded through parse and serialize. Despite the name, on the serializer this option controls encoding — setting it to false carries the same security caveat as encodeEntities: false.

default true


emptyAttrs

• Optional emptyAttrs: boolean

Print an empty attribute's value.

default xmlMode

example With emptyAttrs: false: <input checked>

example With emptyAttrs: true: <input checked="">


selfClosingTags

• Optional selfClosingTags: boolean

Print self-closing tags for tags without contents. If xmlMode is set, this will apply to all tags. Otherwise, only tags that are defined as self-closing in the HTML specification will be printed as such.

default xmlMode

example With selfClosingTags: false: <foo></foo><br></br>

example With xmlMode: true and selfClosingTags: true: <foo/><br/>

example With xmlMode: false and selfClosingTags: true: <foo></foo><br />


xmlMode

• Optional xmlMode: boolean | "foreign"

Treat the input as an XML document; enables the emptyAttrs and selfClosingTags options.

If the value is "foreign", it will try to correct mixed-case attribute names.

default false


Ecosystem

NameDescription
htmlparser2Fast & forgiving HTML/XML parser
domhandlerHandler for htmlparser2 that turns documents into a DOM
domutilsUtilities for working with domhandler's DOM
css-selectCSS selector engine, compatible with domhandler's DOM
cheerioThe jQuery API for domhandler's DOM
dom-serializerSerializer for domhandler's DOM

LICENSE: MIT

版本与发布记录

dom-serializer 共发布 20 个版本,首次发布于 2014/05/20;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.1.1latest2026/05/026 个月前
3.1.02026/05/026 个月前
3.0.02026/03/187 个月前
2.0.02022/04/104 年前
1.4.12022/04/104 年前
1.4.02022/04/104 年前

关于 dom-serializer 的常见问题

dom-serializer 是什么?

render domhandler DOM nodes to a string

如何安装 dom-serializer?

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

dom-serializer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

dom-serializer 支持 ESM 还是 CommonJS?

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

dom-serializer 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

dom-serializer 可以商用吗?

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

dom-serializer 的下载量有多少?

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

dom-serializer 还在维护吗?

最新版本 3.1.1 发布于 2026/05/02(6 个月前);至今共发布 20 个版本,其中过去 12 个月发布了 3 个;首次发布于 2014/05/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。