domparser-rs
v0.0.5
Published
A super fast html parser and manipulator written in rust.
Maintainers
Readme
domparser-rs
一个使用 Rust 编写的超快 Node.js HTML 解析和操作插件。它旨在为 Node.js 提供符合标准的 DOM API。
特性
- 符合标准:实现了标准的 DOM API,包括
DOMParser、querySelector、classList等,使得从基于浏览器的代码迁移变得容易。 - 高性能的 DOM 解析和操作
- 通过 NAPI-RS 暴露简单的 JavaScript API
- 专为服务端和 CLI HTML 处理而设计
- 使用 Rust 编写,兼顾速度与安全性
安装
yarn add domparser-rs
# 或者
npm install domparser-rs使用
const { parse } = require('domparser-rs');
const root = parse('<div id="foo" class="bar">hello <span>world</span></div>');
const div = root.select('div');
console.log(div.getAttribute('id')); // "foo"
console.log(div.text()); // "hello world"
div.setAttribute('title', 'my-title');
console.log(div.outerHtml()); // <div id="foo" class="bar" title="my-title">hello <span>world</span></div>API 文档
parse(html: string): NodeRepr
解析 HTML 字符串并返回代表根节点的 NodeRepr 实例。
DOMParser 类
parseFromString(string: string, mimeType: string): NodeRepr
使用指定的 MIME 类型(例如 "text/html")解析字符串。
NodeRepr 类
表示一个 DOM 节点并提供各种操作方法。
属性 (Getters/Setters)
nodeType: numbernodeName: stringtagName: string | nullnamespaceURI: string | nullprefix: string | nulllocalName: string | nullid: stringclassName: stringparentNode: NodeRepr | nullparentElement: NodeRepr | nullfirstChild: NodeRepr | nulllastChild: NodeRepr | nullpreviousSibling: NodeRepr | nullnextSibling: NodeRepr | nullfirstElementChild: NodeRepr | nulllastElementChild: NodeRepr | nullpreviousElementSibling: NodeRepr | nullnextElementSibling: NodeRepr | nullchildren: NodeRepr[]childElementCount: numbernodeValue: string | nulldata: string | nulltextContent: stringinnerHTML: stringouterHTML: stringownerDocument: NodeRepr | nullchildNodes: NodeRepr[]isConnected: booleandoctype: NodeRepr | nullhead: NodeRepr | nullbody: NodeRepr | nulltitle: stringdocumentElement: NodeRepr | null
操作方法
append(newChild: NodeRepr): voidappendChild(newChild: NodeRepr): NodeReprprepend(newChild: NodeRepr): voidafter(newSibling: NodeRepr): voidbefore(newSibling: NodeRepr): voidinsertBefore(newNode: NodeRepr, refNode?: NodeRepr | null): NodeReprreplaceChild(newChild: NodeRepr, oldChild: NodeRepr): NodeReprreplaceWith(newNode: NodeRepr): voidremove(): voidclone(): NodeRepr(浅拷贝)cloneRecursive(): NodeRepr(深拷贝)cloneNode(deep?: boolean): NodeReprimportNode(externalNode: NodeRepr, deep?: boolean): NodeRepradoptNode(externalNode: NodeRepr): NodeReprnormalize(): void
属性方法
getAttribute(name: string): string | nullsetAttribute(name: string, value: string): voidremoveAttribute(name: string): voidtoggleAttribute(name: string, force?: boolean): booleanhasAttribute(name: string): booleangetAttributeNames(): string[]getAttributes(): Record<string, string>getAttributeNS(namespace: string | null, localName: string): string | nullsetAttributeNS(namespace: string | null, name: string, value: string): voidremoveAttributeNS(namespace: string | null, localName: string): voidhasAttributeNS(namespace: string | null, localName: string): boolean
查询与选择方法
select(selectors: string): NodeRepr | nullselectAll(selectors: string): NodeRepr[]querySelector(selectors: string): NodeRepr | nullquerySelectorAll(selectors: string): NodeRepr[]getElementById(id: string): NodeRepr | nullgetElementsByClassName(classNames: string): NodeRepr[]getElementsByTagName(tagName: string): NodeRepr[]matches(selectors: string): booleanclosest(selectors: string): NodeRepr | nullcontains(otherNode: NodeRepr): boolean
ClassList & Dataset
classListAdd(className: string): voidclassListRemove(className: string): voidclassListToggle(className: string, force?: boolean): booleanclassListContains(className: string): booleandatasetGet(): Record<string, string>datasetSet(key: string, value: string): voiddatasetRemove(key: string): void
其他方法
text(): stringinnerHtml(): stringouterHtml(): stringcreateElement(tagName: string): NodeReprcreateTextNode(data: string): NodeReprcreateComment(data: string): NodeReprcreateDocumentFragment(): NodeReprcreateProcessingInstruction(target: string, data: string): NodeReprisSameNode(otherNode: NodeRepr): booleanisEqualNode(otherNode: NodeRepr): booleancompareDocumentPosition(other: NodeRepr): numberlookupPrefix(namespace?: string): string | nulllookupNamespaceURI(prefix?: string): string | nullisDefaultNamespace(namespace?: string): booleangetRootNode(): NodeReprhasChildNodes(): booleanhasAttributes(): boolean
文本节点方法
splitText(offset: number): NodeReprsubstringData(offset: number, count: number): stringappendData(data: string): voidinsertData(offset: number, data: string): voiddeleteData(offset: number, count: number): voidreplaceData(offset: number, count: number, data: string): void
插入方法
insertAdjacentElement(position: string, element: NodeRepr): NodeRepr | nullinsertAdjacentText(position: string, data: string): voidinsertAdjacentHTML(position: string, html: string): void
贡献
npm install
npm run build
npm test基准测试
cargo benchmark
npm run benchmark更多使用示例和高级 API,请参阅仓库中的源代码和基准测试。
