-
-
Notifications
You must be signed in to change notification settings - Fork 395
Expand file tree
/
Copy pathParser.spec.ts
More file actions
196 lines (161 loc) · 5.35 KB
/
Parser.spec.ts
File metadata and controls
196 lines (161 loc) · 5.35 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { describe, expect, it, vi } from "vitest";
import { Parser, Tokenizer } from "./index.js";
import type { Handler } from "./Parser.js";
describe("API", () => {
it("should work without callbacks", () => {
const cbs: Partial<Handler> = { onerror: vi.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 = vi.fn();
p.write("<a foo");
delete cbs.onopentag;
p.write(">");
// Pause/resume
const onText = vi.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");
});
it("should back out of numeric entities (#125)", () => {
const onend = vi.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");
});
it("should not have the start index be greater than the end index", () => {
const onopentag = vi.fn();
const onclosetag = vi.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).toHaveBeenCalledTimes(2);
expect(onclosetag).toHaveBeenNthCalledWith(1, "p", 9);
expect(onclosetag).toHaveBeenNthCalledWith(2, "hr", 9);
});
it("should preserve declaration names outside of HTML mode", () => {
const onprocessinginstruction = vi.fn();
new Parser(
{
onprocessinginstruction,
},
{ xmlMode: true },
).end("<!DOCTYPEhtml>");
expect(onprocessinginstruction).toHaveBeenCalledWith(
"!DOCTYPEhtml",
"!DOCTYPEhtml",
);
});
it("should preserve declaration casing when lowerCaseTags is disabled", () => {
const onprocessinginstruction = vi.fn();
new Parser(
{
onprocessinginstruction,
},
{ lowerCaseTags: false },
).end("<!DOCTYPEhtml>");
expect(onprocessinginstruction).toHaveBeenCalledWith(
"!DOCTYPE",
"!DOCTYPEhtml",
);
});
it("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);
});
it("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);
});
it("should parse <__proto__> (#387)", () => {
const p = new Parser(null);
// Should not throw
p.write("<__proto__>");
});
it("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.end();
});
});