generated from PythonCoderAS/typescript-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.ts
252 lines (236 loc) · 10.4 KB
/
index.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import { expect } from "chai";
import defaultParser, { ListItemNode, Node, TextNode } from "../src/index";
import { Parser } from "../src";
describe("bbcode-ast tests", () => {
it("Should parse tag-free text", () => {
const parsed = defaultParser.parse("Hello world!");
expect(parsed.children.length).to.equal(1);
expect(parsed.children[0].name).to.equal("TextNode");
expect((parsed.children[0] as TextNode).text).to.equal("Hello world!");
expect(parsed.toString()).to.equal("Hello world!");
});
it("Should parse simple tag", () => {
const parsed = defaultParser.parse("[b]Hello world![/b]");
expect(parsed.children.length).to.equal(1);
expect(parsed.children[0].name).to.equal("b");
expect((parsed.children[0] as Node).children.length).to.equal(1);
expect((parsed.children[0] as Node).children[0].name).to.equal("TextNode");
expect(
((parsed.children[0] as Node).children[0] as TextNode).text
).to.equal("Hello world!");
expect(parsed.toString()).to.equal("[b]Hello world![/b]");
});
it("Should parse multiple simple tags", () => {
const parsed = defaultParser.parse(
"[b]Hello world![/b][i]Hello world![/i]"
);
expect(parsed.children.length).to.equal(2);
expect(parsed.children[0].name).to.equal("b");
expect((parsed.children[0] as Node).children.length).to.equal(1);
expect((parsed.children[0] as Node).children[0].name).to.equal("TextNode");
expect(
((parsed.children[0] as Node).children[0] as TextNode).text
).to.equal("Hello world!");
expect(parsed.children[1].name).to.equal("i");
expect((parsed.children[1] as Node).children.length).to.equal(1);
expect((parsed.children[1] as Node).children[0].name).to.equal("TextNode");
expect(
((parsed.children[1] as Node).children[0] as TextNode).text
).to.equal("Hello world!");
expect(parsed.toString()).to.equal(
"[b]Hello world![/b][i]Hello world![/i]"
);
});
it("Should parse nested tags", () => {
const parsed = defaultParser.parse("[b][i]Hello world![/i][/b]");
expect(parsed.children.length).to.equal(1);
expect(parsed.children[0].name).to.equal("b");
expect((parsed.children[0] as Node).children.length).to.equal(1);
expect((parsed.children[0] as Node).children[0].name).to.equal("i");
expect(
((parsed.children[0] as Node).children[0] as Node).children.length
).to.equal(1);
expect(
(
((parsed.children[0] as Node).children[0] as Node)
.children[0] as TextNode
).text
).to.equal("Hello world!");
expect(parsed.toString()).to.equal("[b][i]Hello world![/i][/b]");
});
it("Should parse code tags and ignore content", () => {
const parsed = defaultParser.parse("[code][b]Hello world![/b][/code]");
expect(parsed.children.length).to.equal(1);
expect(parsed.children[0].name).to.equal("code");
expect((parsed.children[0] as Node).children.length).to.equal(1);
expect((parsed.children[0] as Node).children[0].name).to.equal("TextNode");
expect(
((parsed.children[0] as Node).children[0] as TextNode).text
).to.equal("[b]Hello world![/b]");
expect(parsed.toString()).to.equal("[code][b]Hello world![/b][/code]");
});
it("Should parse list tags", () => {
const parsed = defaultParser.parse(
"[list][*]Hello world![*]Hello world![/list]"
);
expect(parsed.children.length).to.equal(1);
expect(parsed.children[0].name).to.equal("list");
expect((parsed.children[0] as Node).children.length).to.equal(2);
expect((parsed.children[0] as Node).children[0].name).to.equal("*");
expect(
((parsed.children[0] as Node).children[0] as ListItemNode).children.length
).to.equal(1);
expect(
((parsed.children[0] as Node).children[0] as ListItemNode).children[0]
.name
).to.equal("TextNode");
expect(
(
((parsed.children[0] as Node).children[0] as ListItemNode)
.children[0] as TextNode
).text
).to.equal("Hello world!");
expect((parsed.children[0] as Node).children[1].name).to.equal("*");
expect(
((parsed.children[0] as Node).children[1] as ListItemNode).children.length
).to.equal(1);
expect(
((parsed.children[0] as Node).children[1] as ListItemNode).children[0]
.name
).to.equal("TextNode");
expect(
(
((parsed.children[0] as Node).children[1] as ListItemNode)
.children[0] as TextNode
).text
).to.equal("Hello world!");
expect(parsed.toString()).to.equal(
"[list][*]Hello world![*]Hello world![/list]"
);
});
it("Should parse simple attributes", () => {
const parsed = defaultParser.parse("[color=red]Hello world![/color]");
expect(parsed.children.length).to.equal(1);
expect(parsed.children[0].name).to.equal("color");
expect((parsed.children[0] as Node).value).to.equal("red");
expect((parsed.children[0] as Node).children.length).to.equal(1);
expect((parsed.children[0] as Node).children[0].name).to.equal("TextNode");
expect(
((parsed.children[0] as Node).children[0] as TextNode).text
).to.equal("Hello world!");
expect(parsed.toString()).to.equal("[color=red]Hello world![/color]");
});
it("Should parse simple attributes with space and quotes", () => {
const parsed = defaultParser.parse(
'[spoiler="a spoiler"]Hello world![/spoiler]'
);
expect(parsed.children.length).to.equal(1);
expect(parsed.children[0].name).to.equal("spoiler");
expect((parsed.children[0] as Node).value).to.equal('"a spoiler"');
expect((parsed.children[0] as Node).children.length).to.equal(1);
expect((parsed.children[0] as Node).children[0].name).to.equal("TextNode");
expect(
((parsed.children[0] as Node).children[0] as TextNode).text
).to.equal("Hello world!");
expect(parsed.toString()).to.equal(
'[spoiler="a spoiler"]Hello world![/spoiler]'
);
});
it("Should parse complex attributes", () => {
const parsed = defaultParser.parse(
"[url=https://www.google.com][img align=right]https://i.imgur.com/oz0a7.jpg[/img][/url]"
);
expect(parsed.children.length).to.equal(1);
expect(parsed.children[0].name).to.equal("url");
expect((parsed.children[0] as Node).value).to.equal(
"https://www.google.com"
);
expect((parsed.children[0] as Node).children.length).to.equal(1);
expect((parsed.children[0] as Node).children[0].name).to.equal("img");
expect(
((parsed.children[0] as Node).children[0] as Node).attributes
).to.deep.equal({ align: "right" });
expect(
(
((parsed.children[0] as Node).children[0] as Node)
.children[0] as TextNode
).text
).to.equal("https://i.imgur.com/oz0a7.jpg");
expect(parsed.toString()).to.equal(
"[url=https://www.google.com][img align=right]https://i.imgur.com/oz0a7.jpg[/img][/url]"
);
});
it("Should parse complex attributes and quotes", () => {
const parsed = defaultParser.parse(
"[url=https://www.google.com][img align='right']https://i.imgur.com/oz0a7.jpg[/img][/url]"
);
expect(parsed.children.length).to.equal(1);
expect(parsed.children[0].name).to.equal("url");
expect((parsed.children[0] as Node).value).to.equal(
"https://www.google.com"
);
expect((parsed.children[0] as Node).children.length).to.equal(1);
expect((parsed.children[0] as Node).children[0].name).to.equal("img");
expect(
((parsed.children[0] as Node).children[0] as Node).attributes
).to.deep.equal({ align: "'right'" });
expect(
(
((parsed.children[0] as Node).children[0] as Node)
.children[0] as TextNode
).text
).to.equal("https://i.imgur.com/oz0a7.jpg");
expect(parsed.toString()).to.equal(
"[url=https://www.google.com][img align='right']https://i.imgur.com/oz0a7.jpg[/img][/url]"
);
});
it("Parse very nested BBCode", () => {
const text = `[b][i][u][s][color=red][url=https://www.google.com][img align=right]https://i.imgur.com/oz0a7.jpg[/img][/url][/color][/s][/u][/i][/b]`;
const parsed = defaultParser.parse(text);
expect(parsed.children.length).to.equal(1);
expect(parsed.toString() === text);
});
it("Parse real-world example", () => {
const text = `Some quick suggestions:
[list=1]
[*]Update your includes so the script runs on more pages. Here is a sample list (there might be more to add):[code]// @match https://myanimelist.net/forum/index.php?topicid=*
// @match https://myanimelist.net/people/*
// @match https://myanimelist.net/blog.php?eid=*
// @match https://myanimelist.net/news/*
// @match https://myanimelist.net/comtocom.php*
[/code]
Basically, wherever user-entered text is rendered with BBCode, you want to load the script there.
[*]From your comment, I am confused about what you are trying to do. [quote=hacker09] Right now it works great for my 2 test cases, but if they are copy pasted into any text box and it includes forum links the script breaks if they are the first links I think. Or at least won't convert the forum links but will convert everything else.[/quote] Does this mean there's a way to convert a textbox into formatted links? How can I trigger this?
[/list]`;
const parsed = defaultParser.parse(text);
expect(parsed.children.length).to.equal(2);
expect(parsed.toString() === text);
});
it("Custom parser", () => {
const parser = new Parser(["b"]);
const text = "[b][i]Hello world![/i][/b]";
const parsed = parser.parse(text);
expect(parsed.children.length).to.equal(1);
expect(parsed.children[0].name).to.equal("b");
expect((parsed.children[0] as Node).children.length).to.equal(1);
expect((parsed.children[0] as Node).children[0].name).to.equal("TextNode");
expect(
((parsed.children[0] as Node).children[0] as TextNode).text
).to.equal("[i]Hello world![/i]");
expect(parsed.toString()).to.equal(text);
});
it("Custom parser with lenient mode", () => {
const parser = new Parser(["b", "i", "s"], false, true);
const text = "[b][i][s]Hello world![/i][/b]";
const parsed = parser.parse(text);
expect(parsed.children.length).to.equal(1);
expect(parsed.toString()).to.equal(text);
});
it("Custom parser with lenient mode and no closing tags", () => {
const parser = new Parser(["b", "i", "s"], false, true);
const text = "[b]Test![/b][b][i][s]Hello world![b]Test![/b]";
const parsed = parser.parse(text);
expect(parsed.children.length).to.equal(2);
expect(parsed.toString()).to.equal(text);
});
});