Skip to content

Commit 7ea74b6

Browse files
authored
🤖 Merge PR DefinitelyTyped#74468 Add types for html-parse-stringify by @gasp
1 parent d419e95 commit 7ea74b6

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { ASTNode, CommentNode, parse, ParseOptions, stringify, TagNode, TextNode } from "html-parse-stringify";
2+
3+
// Test parse function
4+
const ast: ASTNode[] = parse("<div class=\"oh\"><p>hi</p></div>");
5+
6+
// Test parse with options
7+
const astWithOpts: ASTNode[] = parse("<div><my-component></my-component></div>", {
8+
components: {
9+
"my-component": true,
10+
},
11+
});
12+
13+
// Test stringify function
14+
const htmlString: string = stringify(ast);
15+
16+
// Test roundtrip
17+
const ast2: ASTNode[] = parse("<p>hello</p>");
18+
const html2: string = stringify(ast2);
19+
20+
// Test node type narrowing
21+
for (const node of ast) {
22+
if (node.type === "tag") {
23+
const tagNode: TagNode = node;
24+
const name: string = tagNode.name;
25+
const attrs: { [key: string]: string } = tagNode.attrs;
26+
const isVoid: boolean = tagNode.voidElement;
27+
const children: ASTNode[] = tagNode.children;
28+
} else if (node.type === "text") {
29+
const textNode: TextNode = node;
30+
const content: string = textNode.content;
31+
} else if (node.type === "comment") {
32+
const commentNode: CommentNode = node;
33+
const comment: string = commentNode.comment;
34+
} else if (node.type === "component") {
35+
const name: string = node.name;
36+
const attrs: { [key: string]: string } = node.attrs;
37+
}
38+
}
39+
40+
// Test with complex HTML
41+
const complexAst = parse(`
42+
<div id="main" class="container">
43+
<h1>Title</h1>
44+
<!-- A comment -->
45+
<p>Some <strong>bold</strong> text</p>
46+
<img src="image.jpg" />
47+
</div>
48+
`);
49+
50+
// Test roundtrip
51+
const original = "<div><p>test</p></div>";
52+
const roundtrip: string = stringify(parse(original));
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Attributes object for a tag node.
3+
*/
4+
export interface Attributes {
5+
[key: string]: string;
6+
}
7+
8+
/**
9+
* A tag node in the AST.
10+
*/
11+
export interface TagNode {
12+
type: "tag";
13+
name: string;
14+
attrs: Attributes;
15+
voidElement: boolean;
16+
children: ASTNode[];
17+
}
18+
19+
/**
20+
* A text node in the AST.
21+
*/
22+
export interface TextNode {
23+
type: "text";
24+
content: string;
25+
}
26+
27+
/**
28+
* A comment node in the AST.
29+
*/
30+
export interface CommentNode {
31+
type: "comment";
32+
comment: string;
33+
}
34+
35+
/**
36+
* A component node in the AST.
37+
* Similar to tag but children are ignored during parsing.
38+
*/
39+
export interface ComponentNode {
40+
type: "component";
41+
name: string;
42+
attrs: Attributes;
43+
voidElement: boolean;
44+
children: ASTNode[];
45+
}
46+
47+
/**
48+
* Any AST node type.
49+
*/
50+
export type ASTNode = TagNode | TextNode | CommentNode | ComponentNode;
51+
52+
/**
53+
* Options for parsing HTML.
54+
*/
55+
export interface ParseOptions {
56+
/**
57+
* Object of registered component names whose children will be ignored
58+
* when generating the AST.
59+
*/
60+
components?: {
61+
[componentName: string]: boolean | object;
62+
};
63+
}
64+
65+
/**
66+
* Parses a string of HTML into an AST.
67+
*
68+
* @param html - The HTML string to parse
69+
* @param options - Parse options
70+
* @returns An array of AST nodes
71+
*
72+
* @example
73+
* ```javascript
74+
* var HTML = require('html-parse-stringify');
75+
* var ast = HTML.parse('<div class="oh"><p>hi</p></div>');
76+
* ```
77+
*/
78+
export function parse(html: string, options?: ParseOptions): ASTNode[];
79+
80+
/**
81+
* Stringifies an AST back to HTML.
82+
*
83+
* @param ast - The AST to stringify
84+
* @returns The HTML string
85+
*
86+
* @example
87+
* ```javascript
88+
* var HTML = require('html-parse-stringify');
89+
* var html = HTML.stringify(ast);
90+
* ```
91+
*/
92+
export function stringify(ast: ASTNode[]): string;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "@types/html-parse-stringify",
4+
"version": "3.0.9999",
5+
"projects": [
6+
"https://github.com/henrikjoreteg/html-parse-stringify"
7+
],
8+
"devDependencies": {
9+
"@types/html-parse-stringify": "workspace:."
10+
},
11+
"owners": [
12+
{
13+
"name": "gaspard",
14+
"githubUsername": "gasp"
15+
}
16+
]
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictNullChecks": true,
10+
"strictFunctionTypes": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"html-parse-stringify-tests.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)