@desertnet/html-parser

中文文档
1.0.1发布于 2018/08/02MITCommonJS近 30 天下载 8763

HTML parser and non-strict validator

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. API
  4. HTMLParser.validate(htmlString)
  5. HTMLParser
  6. HTMLParseError
  7. HTMLNode

html-parser

This HTML parser and validator is not a strict HTML parser in the vein of the XHTML strict validators of old. Instead, it aims to parse HTML as browsers do and only surface errors that are likely to confuse browsers or that are indicative of a confused HTML author.

Installation

npm install --save @desertnet/html-parser

Usage

const HTMLParser = require('@desertnet/html-parser')

const html = `
<p>
  <b>I forgot to close my b tag.
</p>

<p>
  I&npsb;misspelled the no-break-space entity.
</p>
`.trim()

const errors = HTMLParser.validate(html)

errors.forEach(error => {
  console.log(`${error.message} (line: ${error.line}, column: ${error.column})`)
})

Outputs:

Could not find closing tag for "<b>". (line: 2, column: 3)
Unexpected closing tag, "</p>". Expected closing tag for "<b>". (line: 3, column: 1)
Invalid HTML entity name for "&npsb;". (line: 6, column: 5)

API

HTMLParser.validate(htmlString)

Static method on HTMLParser constructor. It parses the HTML fragment in htmlString and returns an array of HTMLParseErrors. If there were no errors, an empty array is returned.

HTMLParser

The HTMLParser constructor. It takes no arguments.

const parser = new HTMLParser()

.parse(htmlString)

Parses the HTML fragment in htmlString and returns an HTMLNode object containing the parsed HTML.

const parseTree = parser.parse('<h1>Hello world!</h1>')

HTMLParseError

Represents an error discovered durning parsing of an HTML fragment.

parseTree.errors.forEach(error => {
  console.log(`${error.message} (line: ${error.line}, column: ${error.column})`)
})

.message

Property that is a string containing an English description of the error.

.startIndex

Property that is a number indicating the index into the source string where the error begins.

.endIndex

Property that is a number indicating the index into the source string where the error ends.

.line

Property that is a number indicating the line number where the error begins. Line numbers being at 1, not 0.

.column

Property that is a number indicating the column of the line where the error begins. Columns also begin at 1, not 0.

HTMLNode

The base class for all node types. Returned by HTMLParser's .parse method.

console.log(indentedNodeList(parseTree))

function indentedNodeList (node, indent = '') {
  let str = ''
  if (node.children) {
    str += node.children.reduce((prev, child) => {
      return prev + indentedNodeList(child, `${indent}  `)
    }, '')
  }
  return `${indent}${node.type}\n${str}`
}

.type

Property indicating the type of node. It will be one of the following strings:

  • ROOT: Root node of the tree.
  • TAG: An HTML tag.
  • ATTRIBUTE: An attribute of an HTML tag.
  • TEXT: A text content node.
  • ENTITY: An HTML entity (i.e. &amp;)
  • COMMENT: An HTML comment tag.
  • CLOSETAG: A closing HTML tag.

.children

Property that is either null, or an array of HTMLNodes that are the node's children.

.indexRange

Property that, if non-null, will be an array of two numbers. The first is the index of the source string where the node begins, and the second is the index of the source string where the node ends.

.errors

Property that is an array of HTMLParseErrors associated with this node and its descendants.

.ownErrors

Property that is an array of HTMLParseErrors associated with only this node (not its descendants).

.tagName

On TAG and CLOSETAG type nodes, this is a string of the lowercased tag name.

.attributes

On TAG type nodes, this is an array of ATTRIBUTE nodes that belong to the tag.

.closingTag

On TAG type nodes, this is a CLOSETAG node, if a closing tag for this node was found.

版本与发布记录

@desertnet/html-parser 共发布 2 个版本,首次发布于 2016/08/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.1latest2018/08/028 年前
1.0.02016/08/1810 年前

关于 @desertnet/html-parser 的常见问题

@desertnet/html-parser 是什么?

HTML parser and non-strict validator

如何安装 @desertnet/html-parser?

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

@desertnet/html-parser 支持 TypeScript 吗?

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

@desertnet/html-parser 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@desertnet/html-parser 可以商用吗?

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

@desertnet/html-parser 的下载量有多少?

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

@desertnet/html-parser 还在维护吗?

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