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
|
# Features covered: HTML parser
#
# This file contains a collection of tests for the HTML parser.
# Tested functionalities:
# html5-1.*: Character encoding
# html5-2.*: Parsing tests
# html5-3.*: Bad data
# html5-4.*: DOM building
# html5-5.*: Namespaces
#
# Copyright (c) 2017 Rolf Ade.
source [file join [file dir [info script]] loadtdom.tcl]
testConstraint html5 [dom featureinfo html5]
test html5-1.1 {HTML character entities} {html5} {
set doc [dom parse -html5 -ignorexmlns {<html><body> ¡Äü</body></html>}]
set root [$doc documentElement]
set body [$root selectNodes body]
set result [$body text]
$doc delete
set result
} "\u00A0\u00A1\u00c4\u00fc"
test html5-1.2 {character entities} {html5} {
set doc [dom parse -html5 -ignorexmlns {<html><body>ÖÄÄ</body></html>}]
set root [$doc documentElement]
set body [$root selectNodes body]
set result [$body text]
$doc delete
set result
} "\u00d6\u00c4\u00c4"
test html5-1.3 {character entities} {html5} {
set doc [dom parse -html5 -ignorexmlns {<html>€∋</html>}]
set root [$doc documentElement]
set body [$root selectNodes body]
set result [$body text]
$doc delete
set result
} "\u20ac\u220b"
test html5-1.4 {invalid characters} {html5} {
set doc [dom parse -html5 -ignorexmlns {<html>foo;</html>}]
set root [$doc documentElement]
set body [$root selectNodes body]
set result [$body text]
$doc delete
set result
} "\u0001\u0002\u0003\u0004foo;"
test html5-2.1 {not closed p tags} {html5} {
set doc [dom parse -html5 -ignorexmlns {
<html><body><p>Para 1<p>Para 2<p>Para 3</body></html>
}]
set result [$doc asXML -indent none]
$doc delete
set result
} {<html><head/><body><p>Para 1</p><p>Para 2</p><p>Para 3
</p></body></html>}
test html5-2.2 {HTML parsing} {html5} {
set doc [dom parse -html5 -ignorexmlns {
<HTML><HEAD></HEAD>
<BODY>
<H1>HTML</H1>
</BODY>
</HTML>
}]
set result [$doc asXML -indent none]
$doc delete
set result
} {<html><head/><body><h1>HTML</h1></body></html>}
test html5-2.3 {HTML parsing} {html5} {
set doc [dom parse -html5 -ignorexmlns {
<!-- comment -->
<HTML><HEAD></HEAD>
<BODY>
<H1>HTML</H1>
</BODY>
</HTML>
}]
set result [$doc asXML -indent none]
$doc delete
set result
} {<!-- comment --><html><head/><body><h1>HTML</h1></body></html>}
test html5-2.4 {HTML parsing} {html5} {
set doc [dom parse -html5 -ignorexmlns {
<!-- comment -->
<HTML><HEAD></HEAD>
<BODY>
<H1>HTML</H1>
</BODY>
</HTML>
<!-- comment -->
}]
$doc documentElement root
set result [$root nodeName]
$doc delete
set result
} {html}
test html5-2.5 {HTML parsing} {html5} {
set doc [dom parse -html5 -ignorexmlns {<html> <head><title></title></head><body>
<form>
<select id="L" name="nls_language">
<option value="">--</option>
<option value="en_US" selected="on">en_US</option>
<option value="es_ES">es_ES</option>
<option value="de_DE">de_DE</option>
</select>
</form>
</body></html>
}]
$doc asHTML
} {<html>
<head><title></title></head><body><form><select id="L" name="nls_language">
<option value="">--</option><option value="en_US" selected="on">en_US</option><option value="es_ES">es_ES</option><option value="de_DE">de_DE</option>
</select></form></body>
</html>}
test html5-2.6 {HTML parsing} {html5} {
set doc [dom parse -html5 -ignorexmlns {<html> <head><title></title></head><body>
<form>
<select id="L" name="nls_language">
<option value="">--
<option value="en_US" selected="on">en_US
<option value="es_ES">es_ES
<option value="de_DE">de_DE
</select>
</form>
</body></html>
}]
$doc asHTML
} {<html>
<head><title></title></head><body><form><select id="L" name="nls_language">
<option value="">--
</option><option value="en_US" selected="on">en_US
</option><option value="es_ES">es_ES
</option><option value="de_DE">de_DE
</option>
</select></form></body>
</html>}
test html5-2.7 {HTML parsing} {html5} {
set doc [dom parse -html5 -ignorexmlns {<html> <head><title></title></head><body>
<form>
<select id="L" name="nls_language">
<option value="">--
<option value="en_US" selected>en_US
<option value="de_DE">de_DE
</select>
</form>
</body></html>
}]
$doc asHTML
} {<html>
<head><title></title></head><body><form><select id="L" name="nls_language">
<option value="">--
</option><option value="en_US" selected="selected">en_US
</option><option value="de_DE">de_DE
</option>
</select></form></body>
</html>}
test html5-2.8 {HTML parsing} {html5} {
set doc [dom parse -html5 -ignorexmlns {<html> <head><title></title></head><body>
<form>
<select id="L" name="nls_language">
<option value="">--
<option selected value="en_US">en_US
<option value="de_DE">de_DE
</select>
</form>
</body></html>
}]
$doc asHTML
} {<html>
<head><title></title></head><body><form><select id="L" name="nls_language">
<option value="">--
</option><option selected="selected" value="en_US">en_US
</option><option value="de_DE">de_DE
</option>
</select></form></body>
</html>}
test html5-3.1 {Bad data} {html5} {
set data {line 6 column 17 - Warning: <script> lacks "type" attribute
line 10 column 17 - Warning: <script> lacks "type" attribute
line 11 column 17 - Warning: <table> lacks "summary" attribute}
set doc [dom parse -html5 -ignorexmlns $data]
set result [$doc asXML -indent none]
$doc delete
set result
} {<html><head/><body>line 6 column 17 - Warning: <script> lacks "type" attribute
line 10 column 17 - Warning: <script> lacks "type" attribute
line 11 column 17 - Warning: <table> lacks "summary" attribute</script></body></html>}
test html5-3.2 {Bad data} {html5} {
set doc [dom parse -html5 -ignorexmlns {<a>}]
set result [$doc asXML -indent none]
$doc delete
set result
} {<html><head/><body><a/></body></html>}
test html5-4.1 {Tag name case normalization} {html5} {
set doc [dom parse -html5 -ignorexmlns {<HtmL><boDY></BODy></HTml>}]
set result [$doc asXML -indent none]
$doc delete
set result
} {<html><head/><body/></html>}
test html5-4.2 {Tag name case normalization} {html5} {
set doc [dom parse -html5 -ignorexmlns {<HtmL><NotaHTML_Tag/></HTml>}]
set result [$doc asXML -indent none]
$doc delete
set result
} {<html><head/><body><notahtml_tag/></body></html>}
test html5-4.3 {Attribute normalization} {html5} {
set doc [dom parse -html5 -ignorexmlns {<HtmL><Body Id='3' FOO="Bar" note=this GriLL></body></html>}]
set result [$doc asXML -indent none]
$doc delete
set result
} {<html><head/><body id="3" foo="Bar" note="note" grill="grill"/></html>}
test html5-4.4 {ID Attribute handling} {html5} {
set doc [dom parse -html5 -ignorexmlns {<HtmL><p ID="1"/><p id="2"/><p/></html>}]
set result [[$doc getElementById 1] getAttribute id]
$doc delete
set result
} {1}
set xhtml {h "http://www.w3.org/1999/xhtml"}
set svg {svg "http://www.w3.org/2000/svg"}
set mathml {mml "http://www.w3.org/1998/Math/MathML"}
set xlink {xlink "http://www.w3.org/1999/xlink"}
set html5 {<!doctype html>
<html>
<body>
<p>
<svg xmlns="http://www.w3.org/2000/svg">
<circle cx="30" cy="30" r="30" fill="green"></circle>
</svg>
<p>text</p>
</body>
</html>}
test html5-5.1 {svg subtrees} {html5} {
set doc [dom parse -html5 $html5]
set result [$doc selectNodes -namespaces $xhtml {string(/h:html/h:body/h:p[2])}]
$doc delete
set result
} {text}
set html5 {<!DOCTYPE html>
<html>
<head>
<title>HTML5 SVG demo</title>
</head>
<body>
<h1>HTML5 SVG Demo</h1>
A nice green circle:
<svg id="circle" height="200" xmlns="http://www.w3.org/2000/svg">
<circle id="greencircle" cx="30" cy="30" r="30" fill="green" />
</svg>
<hr>
<address>Created by DKS. This is free code</address>
</body>
</html>}
test html5-5.2 {svg subtrees} {html5} {
set doc [dom parse -html5 $html5]
set svg [$doc selectNodes -namespaces $svg //svg:circle]
set result [$svg @fill]
$doc delete
set result
} {green}
set html5 {<html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Binomial Theorem</title>
</head>
<body>
<p>Binomial Theorem:</p>
<math XMLNS="http://www.w3.org/1998/Math/MathML" FOO="bar">
<mrow>
<MSUp>
<mfenced BAR="grill">
<mrow>
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</mrow>
</mfenced>
<mn>2</mn>
</msup>
<mo>=</mo>
<msup>
<mrow>
<mi>a</mi>
</mrow>
<mn>2</mn>
</msup>
<mo>+</mo>
<msup>
<mrow>
<mi>b</mi>
</mrow>
<mn>2</mn>
</msup>
<mo>+</mo>
<mrow>
<mn>2</mn>
<mi>a</mi>
<mi>b</mi>
</mrow>
</mrow>
</math>
</body>
</html>}
test html5-5.3 {mathml subtrees} {html5} {
set doc [dom parse -html5 $html5]
set mis [$doc selectNodes -namespaces $mathml //mml:mi]
set result ""
foreach mi $mis {
append result [$mi text]
}
$doc delete
set result
} {ababab}
set html5 {<html>
<body>
<svg id="circle" height="60" width="60"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<image id="image" x="0" y="0" height="60" width="60" xlink:href="huge-red-circle.svg" />
</svg>
</body>}
test html5-5.4 {xlink attribute} {html5} {
set doc [dom parse -html5 $html5]
set node [$doc selectNodes -namespaces $xlink {//*[@xlink:href]}]
set result [$node getAttributeNS [lindex $xlink 1] href]
$doc delete
set result
} {huge-red-circle.svg}
test html5-5.5 {xlink attribute} {html5} {
set doc [dom parse -html5 -ignorexmlns $html5]
set node [$doc getElementById "image"]
set result [$node getAttribute "xlink:href"]
$doc delete
set result
} {huge-red-circle.svg}
# cleanup
::tcltest::cleanupTests
return
|