@ciolabs/htmlparser2-source

中文文档
0.0.3发布于 2025/10/30Apache-2.0 WITH Commons-ClauseTypeScriptESM + CJS近 30 天下载 3.1万

A wrapper around htmlparser2 that adds source range information to the AST

原始使用文档
文档目录12 节
  1. Install
  2. Usage
  3. API
  4. parseDocument(data, options?)
  5. Options
  6. autofix
  7. Types
  8. SourceElement
  9. SourceDocument
  10. Utility Functions
  11. nodeToString(node)
  12. Type Guards

@ciolabs/htmlparser2-source

A wrapper around htmlparser2 that adds source range information to the AST.

Install

npm install @ciolabs/htmlparser2-source

Usage

import { parseDocument } from '@ciolabs/htmlparser2-source';

const html = '<div>hello</div>';

const document = parseDocument(html);

API

parseDocument(data, options?)

Parses HTML and returns a document with source range information attached to each element.

Parameters

Returns

Returns a SourceDocument with enhanced elements that include source range information.

Options

autofix

This will add in the missing close tags into the AST. Note, because they don't exist in the source, they will have index positions of -1.

import { parseDocument, nodeToString } from '@ciolabs/htmlparser2-source';

const html = '<div>hello';

const document = parseDocument(html, { autofix: true });

console.log(nodeToString(document)); //=> <div>hello</div>

Types

SourceElement

Enhanced HTML elements that include source range information:

type SourceElement = {
  source: {
    openTag: {
      startIndex: number;
      endIndex: number;
      data: string;
      name: string;
      isSelfClosing: boolean;
    };
    closeTag: {
      startIndex: number;
      endIndex: number;
      data: string;
      name: string;
    } | null;
    attributes: Array<{
      name: {
        startIndex: number;
        endIndex: number;
        data: string;
      };
      value: {
        startIndex: number;
        endIndex: number;
        data: string;
      } | null;
      quote: '"' | "'" | null | undefined;
      source: {
        startIndex: number;
        endIndex: number;
        data: string;
      };
    }>;
  };
  children: SourceChildNode[];
};

SourceDocument

Enhanced document with position utilities:

type SourceDocument = {
  children: SourceChildNode[];
  offsetToPosition(offset: number): { line: number; character: number };
};

Utility Functions

nodeToString(node)

Converts a parsed node back to its HTML string representation.

import { parseDocument, nodeToString } from '@ciolabs/htmlparser2-source';

const html = '<div class="test">content</div>';
const document = parseDocument(html);

console.log(nodeToString(document)); // '<div class="test">content</div>'

Type Guards

The package exports several type guard functions:

  • isTag(node) - Check if node is a SourceElement
  • isText(node) - Check if node is a text node
  • isComment(node) - Check if node is a comment node
  • isDocument(node) - Check if node is a SourceDocument
  • isCDATA(node) - Check if node is a CDATA section
  • isDirective(node) - Check if node is a processing instruction
  • isDoctype(node) - Check if node is a doctype declaration

版本与发布记录

@ciolabs/htmlparser2-source 共发布 4 个版本,首次发布于 2025/09/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.3latest2025/10/301 年前
0.0.22025/10/011.1 年前
0.0.12025/09/191.1 年前
0.0.02025/09/191.1 年前

关于 @ciolabs/htmlparser2-source 的常见问题

@ciolabs/htmlparser2-source 是什么?

A wrapper around htmlparser2 that adds source range information to the AST

如何安装 @ciolabs/htmlparser2-source?

在项目目录运行 npm install @ciolabs/htmlparser2-source,或使用 pnpm add / yarn add / bun add。

@ciolabs/htmlparser2-source 支持 TypeScript 吗?

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

@ciolabs/htmlparser2-source 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ciolabs/htmlparser2-source 可以商用吗?

@ciolabs/htmlparser2-source 使用 Apache-2.0 WITH Commons-Clause 许可。声明的许可为 Apache-2.0 WITH Commons-Clause,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@ciolabs/htmlparser2-source 的下载量有多少?

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

@ciolabs/htmlparser2-source 还在维护吗?

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