This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from inikulin/parse5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
80 lines (63 loc) · 2.83 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { readFile, writeFile } from 'node:fs/promises';
import { basename } from 'node:path';
import { Parser } from '../../packages/parse5/dist/parser/index.js';
import * as defaultTreeAdapter from '../../packages/parse5/dist/tree-adapters/default.js';
import { convertTokenToHtml5Lib } from 'parse5-test-utils/utils/generate-tokenization-tests.js';
import { parseDatFile } from 'parse5-test-utils/utils/parse-dat-file.js';
import { addSlashes } from 'parse5-test-utils/utils/common.js';
import { TokenType, Token } from '../../packages/parse5/dist/common/token.js';
import type { TreeAdapterTypeMap } from '../../packages/parse5/dist/tree-adapters/interface.js';
// eslint-disable-next-line no-console
main().catch(console.error);
function main(): Promise<void[]> {
const convertPromises = process.argv.slice(2).map(async (file) => {
const content = await readFile(file, 'utf-8');
const feedbackTestContent = generateParserFeedbackTest(content);
const feedbackTestFile = `test/data/parser-feedback/${basename(file, '.dat')}.test`;
await writeFile(feedbackTestFile, feedbackTestContent);
});
return Promise.all(convertPromises);
}
function appendToken(dest: Token[], token: Token): void {
if (token.type === TokenType.EOF) return;
if (token.type === TokenType.NULL_CHARACTER || token.type === TokenType.WHITESPACE_CHARACTER) {
token.type = TokenType.CHARACTER;
}
if (token.type === TokenType.CHARACTER) {
const lastToken = dest[dest.length - 1];
if (lastToken?.type === TokenType.CHARACTER) {
lastToken.chars += token.chars;
return;
}
}
dest.push(token);
}
function collectParserTokens(html: string): ReturnType<typeof convertTokenToHtml5Lib>[] {
const tokens: Token[] = [];
class ExtendedParser<T extends TreeAdapterTypeMap> extends Parser<T> {
override _processInputToken(token: Token): void {
super._processInputToken(token);
// NOTE: Needed to split attributes of duplicate <html> and <body>
// which are otherwise merged as per tree constructor spec
if (token.type === TokenType.START_TAG) {
token.attrs = [...token.attrs];
}
appendToken(tokens, token);
}
}
ExtendedParser.parse(html);
return tokens.map((token) => convertTokenToHtml5Lib(token));
}
function generateParserFeedbackTest(parserTestFile: string): string {
const tests = parseDatFile(parserTestFile, defaultTreeAdapter);
const feedbackTest = {
tests: tests
.filter((test) => !test.fragmentContext) // TODO
.map(({ input }) => ({
description: addSlashes(input),
input,
output: collectParserTokens(input),
})),
};
return JSON.stringify(feedbackTest, null, 4);
}