-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathscripting.test.ts
79 lines (61 loc) · 2.53 KB
/
scripting.test.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
import { ParserStream } from '../lib/index.js';
import { generateParsingTests } from 'parse5-test-utils/utils/generate-parsing-tests.js';
import { makeChunks, generateTestsForEachTreeAdapter } from 'parse5-test-utils/utils/common.js';
import { runInNewContext } from 'node:vm';
import { finished } from 'parse5-test-utils/utils/common.js';
function pause(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, 5));
}
const suitePath = new URL('../../../test/data/tree-construction-scripting', import.meta.url);
generateParsingTests(
'ParserStream - Scripting',
'ParserStream - Scripting',
{
withoutErrors: true,
suitePath,
},
async (test, opts) => {
const chunks = makeChunks(test.input);
const parser = test.fragmentContext
? ParserStream.getFragmentStream(test.fragmentContext, opts)
: new ParserStream(opts);
parser.on('script', async (scriptElement, documentWrite, resume) => {
const scriptTextNode = opts.treeAdapter.getChildNodes(scriptElement)[0];
const script = scriptTextNode ? opts.treeAdapter.getTextNodeContent(scriptTextNode) : '';
//NOTE: emulate postponed script execution
await pause();
try {
runInNewContext(script, { document: { write: documentWrite } });
resume();
} catch (error) {
parser.emit('error', error);
}
});
//NOTE: emulate async input stream behavior
for (const chunk of chunks) {
parser.write(chunk);
await pause();
}
parser.end();
await finished(parser);
return {
node: test.fragmentContext ? parser.getFragment() : parser.document,
chunks,
};
}
);
generateTestsForEachTreeAdapter('ParserStream', (treeAdapter) => {
test('Regression - Synchronously calling resume() leads to crash (GH-98)', (done) => {
const parser = new ParserStream({ treeAdapter });
parser.on('script', (_el, _docWrite, resume) => resume());
parser.end('<!doctype html><script>abc</script>');
process.nextTick(done);
});
test('Regression - Parsing loop lock causes accidental hang ups (GH-101)', () => {
const parser = new ParserStream({ treeAdapter });
parser.on('script', (_scriptElement, _documentWrite, resume) => process.nextTick(resume));
parser.write('<script>yo</script>');
parser.end('dawg');
return finished(parser);
});
});