1import type { ParserOptions, TreeAdapterTypeMap } from 'parse5'; 2import { ParserStream } from '../../lib/index.js'; 3import { makeChunks } from 'parse5-test-utils/utils/common.js'; 4 5export function parseChunked<T extends TreeAdapterTypeMap>( 6 test: { input: string; fragmentContext?: T['parentNode'] }, 7 opts: ParserOptions<T>, 8 minChunkSize?: number, 9 maxChunkSize?: number 10): { node: TreeAdapterTypeMap['document']; chunks: string[] } { 11 const parserStream = test.fragmentContext 12 ? ParserStream.getFragmentStream(test.fragmentContext, opts) 13 : new ParserStream(opts); 14 const chunks = makeChunks(test.input, minChunkSize, maxChunkSize); 15 16 // NOTE: set small waterline for testing purposes 17 parserStream.parser.tokenizer.preprocessor.bufferWaterline = 8; 18 19 for (let i = 0; i < chunks.length - 1; i++) { 20 if (typeof chunks[i] !== 'string') { 21 throw new TypeError('Expected chunk to be a string'); 22 } 23 parserStream.write(chunks[i]); 24 } 25 26 parserStream.end(chunks[chunks.length - 1]); 27 28 return { 29 node: test.fragmentContext ? parserStream.getFragment() : parserStream.document, 30 chunks, 31 }; 32} 33