angular-html-parser
Version:
A HTML parser extracted from Angular with some modifications
115 lines (92 loc) • 2.54 kB
Markdown
[](https://www.npmjs.com/package/angular-html-parser)
An HTML parser extracted from Angular with some [modifications](
[](https://github.com/prettier/angular-html-parser/blob/master/packages/angular-html-parser/CHANGELOG.md)
[](https://github.com/prettier/angular-html-parser/compare/main...prettier:angular-html-parser:dev)
> [!TIP]
> Try sync `main` and `dev` branch with upstream first
```sh
npm install --save angular-html-parser
yarn add angular-html-parser
```
```js
import { parse } from "angular-html-parser";
const { rootNodes, errors } = parse(`
<!DOCTYPE html>
<html>
<head>
<title>Hello world!</title>
</head>
<body>
<div>Hello world!</div>
</body>
</html>
`);
```
```ts
declare function parse(input: string, options?: Options): ng.ParseTreeResult;
interface Options {
/**
* any element can self close
*
* defaults to false
*/
canSelfClose?: boolean;
/**
* support [`htm`](https://github.com/developit/htm) component closing tags (`<//>`)
*
* defaults to false
*/
allowHtmComponentClosingTags?: boolean;
/**
* do not lowercase tag names before querying their tag definitions
*
* defaults to false
*/
isTagNameCaseSensitive?: boolean;
/**
* customize tag content type
*
* defaults to the content type defined in the HTML spec
*/
getTagContentType?: (
tagName: string,
prefix: string,
hasParent: boolean,
attrs: Array<{ prefix: string; name: string; value?: string | undefined }>,
) => void | ng.TagContentType;
/**
* tokenize angular control flow block syntax
*/
tokenizeAngularBlocks?: boolean;
/**
* tokenize angular let declaration syntax
*/
tokenizeAngularLetDeclaration?: boolean;
}
```
- add `CDATA` node
- add `DocType` node
- add `nameSpan` field to `Element` and `Attribute`
- allow case-insensitive closing tags for non-foreign elements
- fix `Comment
- support [bogus comments](https://www.w3.org/TR/html5/syntax.html#bogus-comment-state) (`<!...>`, `<?...>`)
- ~~support full [named entities](https://html.spec.whatwg.org/multipage/entities.json)~~ (fixed upstream)
- add `type` property to nodes
- value span for attributes includes quotes
```sh
yarn run build
yarn run test
```
MIT © [Ika](https://github.com/ikatyang)