/
parse-chunked.ts
32 lines (27 loc) · 1.16 KB
/
parse-chunked.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
import type { ParserOptions, TreeAdapterTypeMap } from 'parse5';
import { ParserStream } from '../../lib/index.js';
import { makeChunks } from 'parse5-test-utils/utils/common.js';
export function parseChunked<T extends TreeAdapterTypeMap>(
test: { input: string; fragmentContext?: T['parentNode'] },
opts: ParserOptions<T>,
minChunkSize?: number,
maxChunkSize?: number
): { node: TreeAdapterTypeMap['document']; chunks: string[] } {
const parserStream = test.fragmentContext
? ParserStream.getFragmentStream(test.fragmentContext, opts)
: new ParserStream(opts);
const chunks = makeChunks(test.input, minChunkSize, maxChunkSize);
// NOTE: set small waterline for testing purposes
parserStream.parser.tokenizer.preprocessor.bufferWaterline = 8;
for (let i = 0; i < chunks.length - 1; i++) {
if (typeof chunks[i] !== 'string') {
throw new TypeError('Expected chunk to be a string');
}
parserStream.write(chunks[i]);
}
parserStream.end(chunks[chunks.length - 1]);
return {
node: test.fragmentContext ? parserStream.getFragment() : parserStream.document,
chunks,
};
}