summaryrefslogtreecommitdiffstats
path: root/tcllib/modules/htmlparse/htmlparse.test
blob: e1d26bd4386af8c12eb24d503c4bec1d44adb007 (plain)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# -*- tcl -*-
# Tests for the HTML parser
#
# Sourcing this file into Tcl runs the tests and generates output for errors.
# No output means no errors were found.
#
# Copyright (c) 2001-2005 by ActiveState Tool Corp.
# All rights reserved.
#
# RCS: @(#) $Id: htmlparse.test,v 1.27 2012/08/02 22:21:54 andreas_kupries Exp $

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.3 ; # htmlparse itself is 8.2+, however struct::* need 8.3+
testsNeedTcltest 1.0

support {
    use struct/list.tcl     struct::list

    useAccel [useTcllibC] struct/tree.tcl struct::tree
    TestAccelInit                         struct::tree

    use struct/queue.tcl    struct::queue
    use struct/stack.tcl    struct::stack
    use cmdline/cmdline.tcl cmdline
}
testing {
    useLocal htmlparse.tcl htmlparse
}

# -------------------------------------------------------------------------

set html1 {<html><head><title>foo</title><meta name="..."></head><body><h2>Header<p>burble</body></html>}
set html2 {<html><head><title>foo</title><meta name="..."></head><body><h2>Header<p>burble</b}
set html3 {<html><head><title>foo</title><meta name="..."></head><body><h2>Header<p><b>burble</b><p><form><input type="..."></form></body></html>}

# Simple remembering callback ...
proc cb {args} {global tags ; lappend tags $args}

proc tlist {t n} {
    set tt [list]
    foreach c [$t children $n] {
	lappend tt [$t get $c synth]
    }
    $t set $n -key synth [list [$t get $n type] $tt]
}

# -------------------------------------------------------------------------

test htmlparse-1.0 {basic errors} {
    catch {htmlparse::parse} msg
    set msg
} {::htmlparse::parse : html string missing}

test htmlparse-1.2 {basic errors} {
    catch {htmlparse::parse -cmd "" -split -1 -incvar "" -vroot "" -queue "" a b} msg
    set msg
} {::htmlparse::parse : -cmd illegal argument (empty)}

test htmlparse-1.3 {basic errors} {
    catch {htmlparse::parse -split -1 -incvar "" -vroot "" -queue "" a b} msg
    set msg
} {::htmlparse::parse : -split illegal argument (<= 0)}

test htmlparse-1.4 {basic errors} {
    catch {htmlparse::parse -incvar "" -vroot "" -queue "" a b} msg
    set msg
} {::htmlparse::parse : -incvar illegal argument (empty)}

test htmlparse-1.5 {basic errors} {
    catch {htmlparse::parse -vroot "" -queue "" a b} msg
    set msg
} {::htmlparse::parse : -vroot illegal argument (empty)}

test htmlparse-1.6 {basic errors} {
    catch {htmlparse::parse -queue "" a b} msg
    set msg
} {::htmlparse::parse : -queue illegal argument (empty)}

test htmlparse-1.7 {basic errors} {
    catch {htmlparse::parse a b} msg
    set msg
} {::htmlparse::parse : to many arguments behind the options, expected one}

test htmlparse-1.8 {basic errors} {
    catch {htmlparse::parse -foo a} msg
    set msg
} {::htmlparse::parse : Illegal option "-foo"}

test htmlparse-1.9 {parsing errors} {
    catch {htmlparse::parse -cmd cb $html2} msg
    set msg
} {::htmlparse::parse : HTML is incomplete, option -incvar is missing}


test htmlparse-2.0 {normal parsing} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot foo $html1
    set tags
} [list \
	[list foo   {} {} {}] \
	[list html  {} {} {}] \
	[list head  {} {} {}] \
	[list title {} {} foo] \
	[list title /  {} {}] \
	[list meta  {} {name="..."} {}] \
	[list head  /  {} {}] \
	[list body  {} {} {}] \
	[list h2    {} {} Header] \
	[list p     {} {} burble] \
	[list body  /  {} {}] \
	[list html  /  {} {}] \
	[list foo   /  {} {}] \
	]

test htmlparse-2.1 {normal parsing} {
    set tags [list]
    htmlparse::parse -cmd {cb @} -vroot foo $html1
    set tags
} [list \
	[list @ foo   {} {} {}] \
	[list @ html  {} {} {}] \
	[list @ head  {} {} {}] \
	[list @ title {} {} foo] \
	[list @ title /  {} {}] \
	[list @ meta  {} {name="..."} {}] \
	[list @ head  /  {} {}] \
	[list @ body  {} {} {}] \
	[list @ h2    {} {} Header] \
	[list @ p     {} {} burble] \
	[list @ body  /  {} {}] \
	[list @ html  /  {} {}] \
	[list @ foo   /  {} {}] \
	]

test htmlparse-2.2 {normal parsing} {
    set tags [list]
    set incomplete ""
    htmlparse::parse -cmd cb -incvar incomplete -vroot foo $html2
    list $tags $incomplete
} [list [list \
	[list foo   {} {} {}] \
	[list html  {} {} {}] \
	[list head  {} {} {}] \
	[list title {} {} foo] \
	[list title /  {} {}] \
	[list meta  {} {name="..."} {}] \
	[list head  /  {} {}] \
	[list body  {} {} {}] \
	[list h2    {} {} Header] \
	[list p     {} {} burble] \
	[list foo   /  {} {}] \
	] "</b"]

test htmlparse-3.0 {incremental parsing} {
    set tags [list]
    catch {q destroy}
    struct::queue q
    htmlparse::parse -cmd cb -vroot foo -queue q -split 1 $html1

    list $tags [q size] [q peek [q size]]
} {{} 12 {{{cb} @win@ {foo} {} {} {}} {{cb} @win@ {html} {} {} {}} {{cb} @win@ {head} {} {} {}} {{cb} @win@ {title} {} {} {foo}} {{cb} @win@ {title} {/} {} {}} {{cb} @win@ {meta} {} {name="..."} {}} {{cb} @win@ {head} {/} {} {}} {{cb} @win@ {body} {} {} {}} {{cb} @win@ {h2} {} {} {Header}} {{cb} @win@ {p} {} {} {burble}} {{cb} @win@ {body} {/} {} {}} {{cb} @win@ {html} {/} {} {}}}}

test htmlparse-3.1 {incremental parsing} {
    set tags [list]
    catch {q destroy}
    struct::queue q
    htmlparse::parse -cmd cb -vroot foo -queue q -split 2 $html1

    list $tags [q size] [q peek [q size]]
} {{} 6 {{{cb} @win@ {foo} {} {} {}
{cb} @win@ {html} {} {} {}} {{cb} @win@ {head} {} {} {}
{cb} @win@ {title} {} {} {foo}} {{cb} @win@ {title} {/} {} {}
{cb} @win@ {meta} {} {name="..."} {}} {{cb} @win@ {head} {/} {} {}
{cb} @win@ {body} {} {} {}} {{cb} @win@ {h2} {} {} {Header}
{cb} @win@ {p} {} {} {burble}} {{cb} @win@ {body} {/} {} {}
{cb} @win@ {html} {/} {} {}}}}

test htmlparse-3.2 {incremental parsing} {
    set tags [list]
    set incomplete ""
    catch {q destroy}
    struct::queue q

    htmlparse::parse -cmd cb -incvar incomplete -vroot foo -queue q -split 1 $html2
    list $tags [q size] [q peek [q size]] $incomplete
} {{} 10 {{{cb} @win@ {foo} {} {} {}} {{cb} @win@ {html} {} {} {}} {{cb} @win@ {head} {} {} {}} {{cb} @win@ {title} {} {} {foo}} {{cb} @win@ {title} {/} {} {}} {{cb} @win@ {meta} {} {name="..."} {}} {{cb} @win@ {head} {/} {} {}} {{cb} @win@ {body} {} {} {}} {{cb} @win@ {h2} {} {} {Header}} {{cb} @win@ {p} {} {} {burble}}} </b}

test htmlparse-3.3 {incremental parsing} {
    set tags [list]
    set incomplete ""
    catch {q destroy}
    struct::queue q

    htmlparse::parse -cmd {cb @} -incvar incomplete -vroot foo -queue q -split 1 $html2
    list $tags [q size] [q peek [q size]] $incomplete
} {{} 10 {{{cb} {@} @win@ {foo} {} {} {}} {{cb} {@} @win@ {html} {} {} {}} {{cb} {@} @win@ {head} {} {} {}} {{cb} {@} @win@ {title} {} {} {foo}} {{cb} {@} @win@ {title} {/} {} {}} {{cb} {@} @win@ {meta} {} {name="..."} {}} {{cb} {@} @win@ {head} {/} {} {}} {{cb} {@} @win@ {body} {} {} {}} {{cb} {@} @win@ {h2} {} {} {Header}} {{cb} {@} @win@ {p} {} {} {burble}}} </b}


proc cb_foo {args} {
    if {[string equal [lindex $args 1] FOO]} {return }
    global tags ; lappend tags $args
}

test htmlparse-3.4 {incremental parsing} {
    set tags [list]
    set incomplete ""
    catch {q destroy}
    struct::queue q

    set lines [list]
    lappend lines {<root>} 
    lappend lines {<tag>Hi there</tag>} 
    lappend lines {<tag} 
    lappend lines {>Hi there<} 
    lappend lines {/tag></root>} 

    foreach l $lines {
	htmlparse::parse -cmd {cb_foo @} -incvar incomplete -vroot FOO $l
    }
    list $tags $incomplete
} {{{@ root {} {} {}} {@ tag {} {} {Hi there}} {@ tag / {} {}} {@ tag {} {} {Hi there}} {@ tag / {} {}} {@ root / {} {}}} {}}


# Don't test: ::htmlparse::debugCallback

test htmlparse-4.0 {predefined (HTML 2.0) entities} {
    ::htmlparse::mapEscapes {&gt;&lt;&amp;}
} {><&}

test htmlparse-4.1 {non entities unharmed} {
    ::htmlparse::mapEscapes {this&that&those as well}
} {this&that&those as well}

test htmlparse-4.2 {loose SGML parsing for entities} {
    ::htmlparse::mapEscapes "&amp&amp &amp\n&amp"
} {&& &
&}

test htmlparse-4.3 {numeric, decimal entities} {
    ::htmlparse::mapEscapes {emdash: &#8212; euro: &#8364;}
} "emdash: \u2014 euro: \u20ac"

test htmlparse-4.4 {numeric, hexadecimal entities} {
    ::htmlparse::mapEscapes {emdash: &#x2014; euro: &#x20ac;}
} "emdash: \u2014 euro: \u20ac"

test htmlparse-4.5 {Unknown named entities shall not be mangled} {
    ::htmlparse::mapEscapes {I am &FOO;! You are &FOO
We all are &FOO}
} {I am &FOO;! You are &FOO
We all are &FOO}

test htmlparse-4.6 {numeric, decimal entities; out-of-range} {
    ::htmlparse::mapEscapes {too big: &#89998; and unharmed}
} {too big: &#89998; and unharmed}

test htmlparse-4.7 {numeric, hexadecimal entities; out-of-range} {
    ::htmlparse::mapEscapes {too big: &#xffff; and unharmed}
} {too big: &#xffff; and unharmed}

# Bug #1039961
test htmlparse-4.8 {numeric character references, leading zeros} {
    ::htmlparse::mapEscapes {Ampersand: &#038;.}
} {Ampersand: &.}

test htmlparse-4.9 {XHTML/XML entity apos, bug 2028993} {
    ::htmlparse::mapEscapes {Apostrophe &apos;}
} {Apostrophe '}

# Ticket 86c971506c - HTML 5.1 entities
test htmlparse-4.10 {predefined (HTML 5.1) entities} {
    ::htmlparse::mapEscapes {&blacktriangle;&hbar;}
} "\u25b4\u210f"

# Bug #861277
test htmlparse-6.1 {Backslashes in content} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<p>\\</p>"
    set tags
} [list \
    [list html  {} {} {}] \
    [list p     {} {} {&#92;}] \
    [list p     /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-6.2 {More backslashes in content} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<p>\\abcde</p>"
    set tags
} [list \
    [list html  {} {} {}] \
    [list p     {} {} {&#92;abcde}] \
    [list p     /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-6.3 {Substitutions from backslashes in content} {
    htmlparse::mapEscapes {&#92;abcde}
} {\abcde}

test htmlparse-6.4 {$ in content} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html {<p>$abcde</p>}
    set tags
} [list \
    [list html  {} {} {}] \
    [list p     {} {} {$abcde}] \
    [list p     /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-6.5 {Substitutions from $ in content} {
    htmlparse::mapEscapes {$abcde}
} {$abcde}

test htmlparse-6.6 {Braces in content} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<p>\{\}</p>"
    set tags
} [list \
    [list html  {} {} {}] \
       [list p     {} {} {&#123;&#125;}] \
    [list p     /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-6.7 {More braces in content} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<p>\{abcde\}</p>"
    set tags
} [list \
    [list html  {} {} {}] \
       [list p     {} {} {&#123;abcde&#125;}] \
    [list p     /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-6.8 {Substitutions from braces in content} {
    htmlparse::mapEscapes {&#123;abcde&#125;}
} {{abcde}}

# Tcllib SF Bug 861287 - Processing of comments.
test htmlparse-7.1 {html comments} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<pre>&<!-- a comment --></pre>"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list pre   {} {} &] \
    [list pre   /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-7.2 {html comments} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<pre>&</pre><!-- a comment -->"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list pre   {} {} &] \
    [list pre   /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-7.3 {html comments} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<!-- a comment --><pre>&</pre>"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list pre   {} {} &] \
    [list pre   /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-7.4 {html comments} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<pre>&<!-- a comment -- ></pre>"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list pre   {} {} &] \
    [list pre   /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-7.5 {html comments} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<pre>&</pre><!-- a comment -- >"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list pre   {} {} &] \
    [list pre   /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-7.6 {html comments} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<!-- a comment -- ><pre>&</pre>"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list pre   {} {} &] \
    [list pre   /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-8.1 {html comments} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<pre>&<-- no comment --></pre>"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list pre   {} {} {&&lt;-- no comment --&gt;}] \
    [list pre   /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-8.2 {html comments} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<pre>&</pre><-- no comment -->"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list pre   {} {} &] \
    [list pre   /  {} {&lt;-- no comment --&gt;}] \
    [list html  /  {} {}] ]

test htmlparse-8.3 {html comments} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<-- no comment --><pre>&</pre>"
    set tags
}  [list \
    [list html  {} {} {&lt;-- no comment --&gt;}] \
    [list pre   {} {} &] \
    [list pre   /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-8.4 {html comments} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<pre>&<-- no comment -- ></pre>"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list pre   {} {} {&&lt;-- no comment -- &gt;}] \
    [list pre   /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-8.5 {html comments} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<pre>&</pre><-- no comment -- >"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list pre   {} {} &] \
    [list pre   /  {} {&lt;-- no comment -- &gt;}] \
    [list html  /  {} {}] ]

test htmlparse-8.6 {html comments} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<-- no comment -- ><pre>&</pre>"
    set tags
}  [list \
    [list html  {} {} {&lt;-- no comment -- &gt;}] \
    [list pre   {} {} &]  \
    [list pre   /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-9.0 {handle empty tags} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<b><a/></b>"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list b     {} {} {}] \
    [list a     {} {} {}] \
    [list a     /  {} {}] \
    [list b     /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-9.1 {handle empty tags, attributes} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<b><a href=\"b\"/></b>"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list b     {} {} {}] \
    [list a     {} {href="b"} {}] \
    [list a     /  {} {}] \
    [list b     /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-9.2 {handle empty tags, text coming after} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<b><a/>xx</b>"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list b     {} {} {}] \
    [list a     {} {} {}] \
    [list a     /  {} xx] \
    [list b     /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-9.3 {handle empty tags, text coming before} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<b>xx<a/></b>"
    set tags
}  [list \
    [list html  {} {} {}] \
    [list b     {} {} xx] \
    [list a     {} {} {}] \
    [list a     /  {} {}] \
    [list b     /  {} {}] \
    [list html  /  {} {}] ]

test htmlparse-10.0 {bad html, raising error} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html "<a<a/>>"
    set tags
}  [list \
     [list html {} {} {}] \
     [list a<a  {} {} {}] \
     [list a<a  /  {} {>}] \
     [list html /  {} {}] ]

test htmlparse-10.1 {bad html, varying argument counts} {
    set tags [list]
    htmlparse::parse -cmd cb -vroot html {<a b="<a"/><a>}
    set tags
}  [list \
     [list html {} {} {}] \
     [list a    {} {b="<a"} {}] \
     [list a    /  {} {}] \
     [list a    {} {} {}] \
     [list html /  {} {}] ]


set extraarg {a & b \1 \0 \\1 \\0 \} $ [ ] }
test htmlparse-11.0 {metachar callback} {
    set tags [list]
    htmlparse::parse -cmd [list cb $extraarg] -vroot html {<a b="a">x</a>}
    set tags
}  [list \
     [list $extraarg html {} {} {}] \
     [list $extraarg a    {} {b="a"} {x}] \
     [list $extraarg a    /  {} {}] \
     [list $extraarg html /  {} {}] ]


# -------------------------------------------------------------------------
# In this section we run all the tests depending on a struct::tree,
# and thus have to test all the available implementations.

set tests [file join [file dirname [info script]] htmlparse.tree_testsuite]

catch {memory validate on}

TestAccelDo struct::tree impl {
    # The global variable 'impl' is part of the public API the
    # testsuit (in htmlparse_tree.testsuite) can expect from the
    # environment.

    namespace import -force struct::tree

    set usec [time {source $tests} 1]

    #puts "$impl:\t$usec"
}

catch {memory validate off}

unset usec
unset tests

# -------------------------------------------------------------------------

# Take a look at the cache.
#parray ::htmlparse::splitdata
TestAccelExit struct::tree
testsuiteCleanup
return