/
Parser.spec.ts
163 lines (134 loc) · 4.48 KB
/
Parser.spec.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { Parser, Tokenizer } from ".";
import type { Handler } from "./Parser";
describe("API", () => {
test("should work without callbacks", () => {
const cbs: Partial<Handler> = { onerror: jest.fn() };
const p = new Parser(cbs, {
xmlMode: true,
lowerCaseAttributeNames: true,
});
p.end("<a foo><bar></a><!-- --><![CDATA[]]]><?foo?><!bar><boo/>boohay");
p.write("foo");
// Check for an error
p.end();
p.write("foo");
expect(cbs.onerror).toHaveBeenLastCalledWith(
new Error(".write() after done!")
);
p.end();
expect(cbs.onerror).toHaveBeenLastCalledWith(
new Error(".end() after done!")
);
// Should ignore the error if there is no callback
delete cbs.onerror;
p.write("foo");
p.reset();
// Remove method
cbs.onopentag = jest.fn();
p.write("<a foo");
delete cbs.onopentag;
p.write(">");
// Pause/resume
const onText = jest.fn();
cbs.ontext = onText;
p.pause();
p.write("foo");
expect(onText).not.toHaveBeenCalled();
p.resume();
expect(onText).toHaveBeenLastCalledWith("foo");
p.pause();
expect(onText).toHaveBeenCalledTimes(1);
p.resume();
expect(onText).toHaveBeenCalledTimes(1);
p.pause();
p.end("bar");
expect(onText).toHaveBeenCalledTimes(1);
p.resume();
expect(onText).toHaveBeenCalledTimes(2);
expect(onText).toHaveBeenLastCalledWith("bar");
});
test("should back out of numeric entities (#125)", () => {
const onend = jest.fn();
let text = "";
const p = new Parser({
ontext(data) {
text += data;
},
onend,
});
p.end("id=770&#anchor");
expect(onend).toHaveBeenCalledTimes(1);
expect(text).toBe("id=770&#anchor");
p.reset();
text = "";
p.end("0&#xn");
expect(onend).toHaveBeenCalledTimes(2);
expect(text).toBe("0&#xn");
});
test("should not have the start index be greater than the end index", () => {
const onopentag = jest.fn();
const onclosetag = jest.fn();
const p = new Parser({
onopentag(tag) {
expect(p.startIndex).toBeLessThanOrEqual(p.endIndex);
onopentag(tag, p.startIndex, p.endIndex);
},
onclosetag(tag) {
expect(p.startIndex).toBeLessThanOrEqual(p.endIndex);
onclosetag(tag, p.endIndex);
},
});
p.write("<p>");
expect(onopentag).toHaveBeenLastCalledWith("p", 0, 2);
expect(onclosetag).not.toHaveBeenCalled();
p.write("Foo");
p.write("<hr>");
expect(onopentag).toHaveBeenLastCalledWith("hr", 6, 9);
expect(onclosetag).toBeCalledTimes(2);
expect(onclosetag).toHaveBeenNthCalledWith(1, "p", 9);
expect(onclosetag).toHaveBeenNthCalledWith(2, "hr", 9);
});
test("should update the position when a single tag is spread across multiple chunks", () => {
let called = false;
const p = new Parser({
onopentag() {
called = true;
expect(p.startIndex).toBe(0);
expect(p.endIndex).toBe(12);
},
});
p.write("<div ");
p.write("foo=bar>");
expect(called).toBe(true);
});
test("should have the correct position for implied opening tags", () => {
let called = false;
const p = new Parser({
onopentag() {
called = true;
expect(p.startIndex).toBe(0);
expect(p.endIndex).toBe(3);
},
});
p.write("</p>");
expect(called).toBe(true);
});
test("should parse <__proto__> (#387)", () => {
const p = new Parser(null);
// Should not throw
p.parseChunk("<__proto__>");
});
test("should support custom tokenizer", () => {
class CustomTokenizer extends Tokenizer {}
const p = new Parser(
{
onparserinit(parser: Parser) {
// @ts-expect-error Accessing private tokenizer here
expect(parser.tokenizer).toBeInstanceOf(CustomTokenizer);
},
},
{ Tokenizer: CustomTokenizer }
);
p.done();
});
});