-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTestHtmlTokenizer.cs
More file actions
321 lines (269 loc) · 10.4 KB
/
Copy pathUnitTestHtmlTokenizer.cs
File metadata and controls
321 lines (269 loc) · 10.4 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System.Linq;
using WebExpress.WebCore.WebHtml.Parser;
namespace WebExpress.WebCore.Test.Html.Parser
{
/// <summary>
/// Unit tests for the <see cref="HtmlTokenizer"/> class.
/// </summary>
[Collection("NonParallelTests")]
public class UnitTestHtmlTokenizer
{
// ------------------------------------------------------------------
// Helper
// ------------------------------------------------------------------
private static HtmlToken[] Tokenize(string html)
{
var tokenizer = new HtmlTokenizer(html);
return [.. tokenizer.Tokenize()];
}
// ------------------------------------------------------------------
// Basic tokens
// ------------------------------------------------------------------
/// <summary>
/// An empty input produces only an EOF token.
/// </summary>
[Fact]
public void EmptyInput_ReturnsEof()
{
var tokens = Tokenize("");
Assert.Single(tokens);
Assert.Equal(HtmlTokenType.EndOfFile, tokens[0].Type);
}
/// <summary>
/// A plain text input produces a text token.
/// </summary>
[Fact]
public void PlainText_ReturnsTextToken()
{
var tokens = Tokenize("Hello World");
Assert.Equal(HtmlTokenType.Text, tokens[0].Type);
Assert.Equal("Hello World", tokens[0].Value);
}
/// <summary>
/// A simple start tag produces a start-tag token.
/// </summary>
[Fact]
public void SimpleStartTag_ReturnsStartTag()
{
var tokens = Tokenize("<div>");
Assert.Equal(HtmlTokenType.StartTag, tokens[0].Type);
Assert.Equal("div", tokens[0].TagName);
}
/// <summary>
/// A simple end tag produces an end-tag token.
/// </summary>
[Fact]
public void SimpleEndTag_ReturnsEndTag()
{
var tokens = Tokenize("</div>");
Assert.Equal(HtmlTokenType.EndTag, tokens[0].Type);
Assert.Equal("div", tokens[0].TagName);
}
/// <summary>
/// An explicit self-closing tag produces a self-closing token.
/// </summary>
[Fact]
public void ExplicitSelfClosingTag_ReturnsSelfClosing()
{
var tokens = Tokenize("<br/>");
Assert.Equal(HtmlTokenType.SelfClosingTag, tokens[0].Type);
Assert.Equal("br", tokens[0].TagName);
}
/// <summary>
/// A void element without a trailing slash is still emitted as self-closing.
/// </summary>
[Fact]
public void VoidElement_ReturnsSelfClosing()
{
var tokens = Tokenize("<img>");
Assert.Equal(HtmlTokenType.SelfClosingTag, tokens[0].Type);
Assert.Equal("img", tokens[0].TagName);
}
// ------------------------------------------------------------------
// Attributes
// ------------------------------------------------------------------
/// <summary>
/// Quoted attribute values are correctly extracted.
/// </summary>
[Fact]
public void TagWithQuotedAttribute_ExtractsAttribute()
{
var tokens = Tokenize("<div class=\"foo\">");
Assert.Equal(HtmlTokenType.StartTag, tokens[0].Type);
var attr = tokens[0].Attributes.Single();
Assert.Equal("class", attr.Name);
Assert.Equal("foo", attr.Value);
}
/// <summary>
/// Single-quoted attribute values are correctly extracted.
/// </summary>
[Fact]
public void TagWithSingleQuotedAttribute_ExtractsAttribute()
{
var tokens = Tokenize("<div class='bar'>");
var attr = tokens[0].Attributes.Single();
Assert.Equal("class", attr.Name);
Assert.Equal("bar", attr.Value);
}
/// <summary>
/// Multiple attributes on a single tag are all extracted.
/// </summary>
[Fact]
public void TagWithMultipleAttributes_ExtractsAll()
{
var tokens = Tokenize("<input id=\"x\" type=\"text\" disabled>");
var attrs = tokens[0].Attributes;
Assert.Equal(3, attrs.Count);
Assert.Equal("id", attrs[0].Name);
Assert.Equal("x", attrs[0].Value);
Assert.Equal("type", attrs[1].Name);
Assert.Equal("text", attrs[1].Value);
Assert.Equal("disabled", attrs[2].Name);
Assert.True(attrs[2].IsBoolean);
}
/// <summary>
/// Boolean (valueless) attributes are represented without a value.
/// </summary>
[Fact]
public void BooleanAttribute_IsBoolean()
{
var tokens = Tokenize("<input required>");
var attr = tokens[0].Attributes.Single();
Assert.True(attr.IsBoolean);
Assert.Null(attr.Value);
}
/// <summary>
/// Data attributes are preserved as-is.
/// </summary>
[Fact]
public void DataAttribute_IsPreserved()
{
var tokens = Tokenize("<div data-toggle=\"modal\">");
var attr = tokens[0].Attributes.Single();
Assert.Equal("data-toggle", attr.Name);
Assert.Equal("modal", attr.Value);
}
/// <summary>
/// ARIA attributes are preserved as-is.
/// </summary>
[Fact]
public void AriaAttribute_IsPreserved()
{
var tokens = Tokenize("<button aria-label=\"Close\"></button>");
var attr = tokens[0].Attributes.Single();
Assert.Equal("aria-label", attr.Name);
Assert.Equal("Close", attr.Value);
}
// ------------------------------------------------------------------
// Comments and DOCTYPE
// ------------------------------------------------------------------
/// <summary>
/// An HTML comment produces a comment token whose value does not include the delimiters.
/// </summary>
[Fact]
public void Comment_ReturnsCommentToken()
{
var tokens = Tokenize("<!-- remark -->");
Assert.Equal(HtmlTokenType.Comment, tokens[0].Type);
Assert.Equal("remark", tokens[0].Value);
}
/// <summary>
/// A DOCTYPE declaration produces a doctype token.
/// </summary>
[Fact]
public void Doctype_ReturnsDoctypeToken()
{
var tokens = Tokenize("<!DOCTYPE html>");
Assert.Equal(HtmlTokenType.Doctype, tokens[0].Type);
Assert.Equal("html", tokens[0].TagName);
}
// ------------------------------------------------------------------
// Compound input
// ------------------------------------------------------------------
/// <summary>
/// A typical HTML snippet produces the expected sequence of tokens.
/// </summary>
[Fact]
public void CompoundInput_ProducesExpectedSequence()
{
var tokens = Tokenize("<p>Hello</p>");
Assert.Equal(HtmlTokenType.StartTag, tokens[0].Type);
Assert.Equal("p", tokens[0].TagName);
Assert.Equal(HtmlTokenType.Text, tokens[1].Type);
Assert.Equal("Hello", tokens[1].Value);
Assert.Equal(HtmlTokenType.EndTag, tokens[2].Type);
Assert.Equal("p", tokens[2].TagName);
Assert.Equal(HtmlTokenType.EndOfFile, tokens[3].Type);
}
/// <summary>
/// Tags names are normalised to lower case.
/// </summary>
[Fact]
public void TagNameNormalisation_IsLowerCase()
{
var tokens = Tokenize("<DIV class=\"FOO\">");
Assert.Equal("div", tokens[0].TagName);
Assert.Equal("class", tokens[0].Attributes[0].Name);
}
// ------------------------------------------------------------------
// Whitespace and edge cases
// ------------------------------------------------------------------
/// <summary>
/// Whitespace-only text between tags is preserved as a text token.
/// </summary>
[Fact]
public void WhitespaceText_IsPreservedAsTextToken()
{
var tokens = Tokenize("<div> </div>");
Assert.Equal(HtmlTokenType.StartTag, tokens[0].Type);
Assert.Equal(HtmlTokenType.Text, tokens[1].Type);
Assert.Equal(" ", tokens[1].Value);
Assert.Equal(HtmlTokenType.EndTag, tokens[2].Type);
}
/// <summary>
/// An unquoted attribute value is read until whitespace or closing bracket.
/// </summary>
[Fact]
public void UnquotedAttributeValue_IsExtracted()
{
var tokens = Tokenize("<div class=foo>");
var attr = tokens[0].Attributes.Single();
Assert.Equal("class", attr.Name);
Assert.Equal("foo", attr.Value);
}
/// <summary>
/// An inline style attribute is preserved in its entirety.
/// </summary>
[Fact]
public void InlineStyleAttribute_IsPreserved()
{
var tokens = Tokenize("<div style=\"color: red; font-size: 14px;\">");
var attr = tokens[0].Attributes.Single();
Assert.Equal("style", attr.Name);
Assert.Equal("color: red; font-size: 14px;", attr.Value);
}
/// <summary>
/// A stray less-than character is emitted as text.
/// </summary>
[Fact]
public void StrayLessThan_IsEmittedAsText()
{
var tokens = Tokenize("a < b");
Assert.Equal(HtmlTokenType.Text, tokens[0].Type);
Assert.Equal("a ", tokens[0].Value);
Assert.Equal(HtmlTokenType.Text, tokens[1].Type);
Assert.Equal("<", tokens[1].Value);
Assert.Equal(HtmlTokenType.Text, tokens[2].Type);
}
/// <summary>
/// A keygen void element without slash is emitted as self-closing.
/// </summary>
[Fact]
public void KeygenVoidElement_ReturnsSelfClosing()
{
var tokens = Tokenize("<keygen>");
Assert.Equal(HtmlTokenType.SelfClosingTag, tokens[0].Type);
Assert.Equal("keygen", tokens[0].TagName);
}
}
}