From bdda3a2e66be65170e02279adad70c7ec4570741 Mon Sep 17 00:00:00 2001 From: fankun Date: Thu, 1 Feb 2024 15:55:11 +0800 Subject: [PATCH] =?UTF-8?q?[Issues:=20#I8ZN82]=20=20=E6=B7=BB=E5=8A=A0html?= =?UTF-8?q?parser2=E6=8C=87=E5=AF=BC=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1224/htmlparser2.md | 108 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 1224/htmlparser2.md diff --git a/1224/htmlparser2.md b/1224/htmlparser2.md new file mode 100644 index 00000000..d24ed916 --- /dev/null +++ b/1224/htmlparser2.md @@ -0,0 +1,108 @@ +> 模板版本:v0.1.2 + +

+

htmlparser2

+

+

+ + Supported platforms + + + License + +

+ + +> [!tip] [Github 地址](https://github.com/fb55/htmlparser2) + +## 安装与使用 + +进入到工程目录并输入以下命令: + + + +**yarn** + +```bash +yarn add htmlparser2@9.1.0 +``` + +#### **npm** + +```bash +npm install htmlparser2@9.1.0 +``` + + + +下面的代码展示了这个库的基本使用场景: + +```js +import * as htmlparser2 from "htmlparser2"; + +const parser = new htmlparser2.Parser({ + onopentag(name, attributes) { + /* + * This fires when a new tag is opened. + * + * If you don't need an aggregated `attributes` object, + * have a look at the `onopentagname` and `onattribute` events. + */ + if (name === "script" && attributes.type === "text/javascript") { + console.log("JS! Hooray!"); + } + }, + ontext(text) { + /* + * Fires whenever a section of text was processed. + * + * Note that this can fire at any point within text and you might + * have to stitch together multiple pieces. + */ + console.log("-->", text); + }, + onclosetag(tagname) { + /* + * Fires when a tag is closed. + * + * You can rely on this event only firing when you have received an + * equivalent opening tag before. Closing tags without corresponding + * opening tags will be ignored. + */ + if (tagname === "script") { + console.log("That's it?!"); + } + }, +}); +parser.write( + "Xyz ", +); +parser.end(); +``` + +## 兼容性 + +在下述版本验证通过: + +1. IDE:4.1.3.500; SDK:HarmonyOS NEXT Devloper Preview1; 测试设备:Mate60 (BAR-AL00); Rom:204.1.0.59(SP2DEVC00E60R4P1); RNOH:0.72.13。 + +## 生态系统 + +> | Name | Description | +> | ------------------------------------------------------------ | ------------------------------------------------------- | +> | [htmlparser2](https://github.com/fb55/htmlparser2) | Fast & forgiving HTML/XML parser | +> | [domhandler](https://github.com/fb55/domhandler) | Handler for htmlparser2 that turns documents into a DOM | +> | [domutils](https://github.com/fb55/domutils) | Utilities for working with domhandler's DOM | +> | [css-select](https://github.com/fb55/css-select) | CSS selector engine, compatible with domhandler's DOM | +> | [cheerio](https://github.com/cheeriojs/cheerio) | The jQuery API for domhandler's DOM | +> | [dom-serializer](https://github.com/cheeriojs/dom-serializer) | Serializer for domhandler's DOM | + +## 遗留问题 + +解析器错误地将(小于)识别为起始标签: [issue#1](https://github.com/fb55/htmlparser2/issues/1620) + +## 其他 + +## 开源协议 + +本项目基于 [The MIT License (MIT)](https://github.com/fb55/htmlparser2/blob/master/LICENSE) ,请自由地享受和参与开源。 -- Gitee