https://github.com/emptydreams/fast-html-checker
快速检查 HTML 中是否包含非法标签和属性。
https://github.com/emptydreams/fast-html-checker
Last synced: 3 months ago
JSON representation
快速检查 HTML 中是否包含非法标签和属性。
- Host: GitHub
- URL: https://github.com/emptydreams/fast-html-checker
- Owner: EmptyDreams
- License: agpl-3.0
- Created: 2023-08-23T10:06:08.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-09-01T13:07:51.000Z (over 1 year ago)
- Last Synced: 2025-01-01T12:40:16.404Z (4 months ago)
- Language: TypeScript
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## 快速开始
使用如下代码检查 HTML 内容:
```javascript
const HTMLChecker = require('fast-html-checker')const htmlContent = ''
const result = HTMLChecker.check(htmlContent, {
allowTags: ['a']
}) // undefined
```当 `check` 函数返回 `undefined` 时表示检查通过,不存在非法元素,若返回一个非空字符串则表示发现了非法元素,字符串内容用来标明非法的原因。
## 配置项
调用 `check` 时第二个参数为配置项:
```typescript
export type CheckResult = string | undefined
export type ElementChecker = (element: HTMLElement) => CheckResultexport interface CheckerOptional {
/** 允许的标签列表 */
allowTags: (TagItemInfo | string)[],
/** 元素内容检查器 */
checkers?: {[propName: string]: ElementChecker}}
export interface TagItemInfo {
/** 标签名称 */
name: string,
/** 允许的 attr */
allowAttrs: {
[key: string]: ((content: string) => CheckResult) | undefined | null
} | string[]}
```当 `allowTags` 的值为 `string` 类型时标明允许所有的 `attr`,传入 `TagItemInfo` 类型来对 `attr` 进行验证。
`allowAttrs` 中的 `key` 为 `attr` 的名称,`value` 为内容检查器,传入 `undefined` 或 `null` 表明允许任何内容。