File: style.test

package info (click to toggle)
tk-html3 3.0~fossil20110109-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,644 kB
  • ctags: 5,882
  • sloc: ansic: 48,994; tcl: 26,030; sh: 1,190; yacc: 161; makefile: 24
file content (518 lines) | stat: -rw-r--r-- 13,761 bytes parent folder | download | duplicates (5)
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

#---------------------------------------------------------------------
# This file contains automated tests for the Tkhtml stylesheet parser.
#

# Test script for Tkhtml
proc sourcefile {file} {
  set fname [file join [file dirname [info script]] $file] 
  uplevel #0 [list source $fname]
}
sourcefile common.tcl

# Test organization for tests in this file.
#
#     style-1.*:   Tests the parsing of 'background-position' property values.
#     style-2.*:   Tests the parsing of 'background' property values.
                 
#     style-5.*:   Tests some intentional stylesheet syntax errors observed 
#                  in the wild used to mask an @import directive. Apparently
#                  one of the "IE-hacks".
#
#     style-7.*:   Security test. Tests that only a default stylesheet can
#                  specify "tcl()" property values. If this were not the
#                  case web pages could cause arbitrary scripts to be 
#                  executed in the applications interpreter.
#
#     style-8.*:   Tests that Tkhtml handles certain types of syntax errors 
#                  in stylesheets. Specifically, these test cases come
#                  from the malformed selectors in the "acid2" test.
#

html .h
.h handler script style styleHandler 
proc styleHandler {attr data} {
  .h style $data
}

proc property {node prop} {
  array set properties [$node prop]
  return $properties($prop)
}


#--------------------------------------------------------------------------
# Test cases style-1.* test the parsing of the 'background-position' property.
#

proc background-position {node} {
    return [list                               \
        [$node property background-position-x] \
        [$node property background-position-y] \
    ]
}

proc test_background_position {name style value} {
  .h configure -defaultstyle ""
  .h reset
  .h style "div {background-position: $style}"

  tcltest::test $name {} -body {
    .h parse -final {<html><div></div></html>}
    set node [lindex [.h search div] 0]
    background-position $node
  } -result $value

#  puts [.h styleconfig]
}

test_background_position style-1.0 "50% 50%"          {50.0% 50.0%}
test_background_position style-1.1 "center center"    {50.0% 50.0%}
test_background_position style-1.2 "111 222"          {0px 0px}
test_background_position style-1.3 "right"            {100.0% 50.0%}
test_background_position style-1.4 "blah"             {0px 0px}

#--------------------------------------------------------------------------
# Test cases style-2.* test the parsing of the 'background' property.
#
tcltest::test style-2.0 {} -body {
  .h reset
  .h parse -final {
    <html>
    <style>
      .one {background: black url(shell-bg.jpg) 0 0 no-repeat fixed;}
      .two {background: black url(shell-bg.jpg) 50 no-repeat 100 fixed;}
    </style>
    <body>
    <div class="one"/>
    <div class="two"/>
    </body>
    </html>
  }

  set node [lindex [.h search .one] 0]
  background-position $node
} -result {0px 0px}

tcltest::test style-2.1 {} -body {
  property $node background-color
} -result {black}

# Note: in quirks mode the result would be {50px 100px}
tcltest::test style-2.2 {} -body {
  set node2 [lindex [.h search .two] 0]
  background-position $node2
} -result {0px 0px}

tcltest::test style-2.3 {} -body {
  property $node2 background-repeat
} -result {no-repeat}

#--------------------------------------------------------------------------
# Test cases style-3.* test some selectors
#
tcltest::test style-3.0 {} -body {
  .h reset
  .h parse -final {
    <html>
    <style>
      i {color:red}
      body>b {color:red}
    </style>
    <body>
    <i>Hello</i>
    <b>Hello</i>
    </body>
    </html>
  }

  set node [lindex [.h search i] 0]
  property $node color
} -result {red}
tcltest::test style-3.1 {} -body {
  set node [lindex [.h search b] 0]
  property $node color
} -result {red}

#--------------------------------------------------------------------------
# The following tests (style-4.*) test the parsing of stylesheets.
#
proc parse_stylesheet {stylesheet_text} {
  html .newwidget -defaultstyle ""
  .newwidget style $stylesheet_text
  set ret [list]
  foreach r [.newwidget styleconfig] {
    lappend ret [lrange $r 0 1]
  }
  destroy .newwidget
  return $ret
}

#
# Test 4.1 tests the bug reported by ticket #43. The embedded stylesheet
# contains an error - a ":" character is missing from a declaration. The
# correct behaviour is to ignore the broken declaration and resume
# parsing after the following semi-colon. In other words, the style-sheet
# specified in the <STYLE> element should parse identically to:
#
#     div {
#         color: red;
#         background-color: green;
#     }
#
tcltest::test style-4.1.1 {} -body {
  .h reset
  .h style {
      div {
        color: red;
        garbage moregarbage;
        background-color: green;
      }
  }
  .h parse -final {
    <div>Hello</div>
    <span>World</span>
  }
  set node [lindex [.h search div] 0]
  list [$node property color] [$node property background-color]
} -result {red green}

# Test 4.2 tests that semi-colons are treated correctly. 
tcltest::test style-4.2.1 {} -body {
  parse_stylesheet {
      div {
        color: red;; ; background-color: green
      }
  }
} -result {{div {color:red; background-color:green}}}
tcltest::test style-4.2.2 {} -body {
  parse_stylesheet {
      div {
        ;color: red;; ; background-color: green; ; ;
      ;}
  }
} -result {{div {color:red; background-color:green}}}

# Test 4.3 tests for parsing errors using the examples given in the CSS 2.1
# specification, section 4.2. All of the following declarations should
# be equivalent to "p {color:green}".
set tn 1
foreach ss [list \
  {p { color:green }}                                    \
  {p { color:green; color }}                             \
  {p { color:red;   color; color:green }}                \
  {p { color:green; color: }}                            \
  {p { color:red;   color:; color:green }}               \
  {p { color:green; {some more garbage}}}                \
  {p { some more garbage; color: green}}                 \
  {p { {some more garbage}; color: green}}               \
  {p { color:green; color{;color:maroon} }}              \
  {p { color:red;   color{;color:maroon}; color:green }}  
] {
  tcltest::test style-4.3.$tn {} -body {
    parse_stylesheet $ss
  } -result {{p color:green}}
  incr tn
}

#--------------------------------------------------------------------------
# Test cases numbered 5.* test that the parser correctly negotiates 
# @media blocks.

# Given the stylesheet document $style, return the number of @import 
# directives contained in the document that should be used by Tkhtml.
proc count_import_directives {style} {
}
proc incrvar {varname args} {
  uplevel #0 [list incr $varname]
}

# The following block comes from the page at www.theaustralian.com.au.
# I have no idea what they are trying to achieve, but I do know the 
# page renders incorrectly if the @import directives are recognised.
# So the following test cases test that they are ignored.
set ::style_block "
/* IE legacy version fixes - leave at end */
@media tty {
   i{content:\"\";/*\" \"*/}}@m; @import '/css/ie55/0,20954,,00.css'; /*\";}
   }/* */

@media tty {
   i{content:\"\";/*\" \"*/}}; @import '/css/ie50/0,20955,,00.css'; {;}/*\";}
   }/* */
"
tcltest::test style-5.1 {} -body {
  html .newwidget 
  set ::import_cnt 0
  .newwidget style -importcmd [list incrvar ::import_cnt] $::style_block
  destroy .newwidget
  set ::import_cnt
} -result {0}


tcltest::test style-6.1 {} -body {
  parse_stylesheet {
    #nav-main {background: #fc3 url(/files/theme/nav-main.png) repeat-x 0 100%;}
  }
} -result [list [list \
  {#nav-main}  \
  {background-image:url(/files/theme/nav-main.png); background-attachment:scrol; background-color:#fc3; background-repeat:repeat-x; background-position-x:0.00; background-position-y:100.00%}
]]

tcltest::test style-6.2 {} -body {
  parse_stylesheet {
    @media print {
      P {color: green}
    }
  }
} -result [list]

tcltest::test style-6.3 {} -body {
  parse_stylesheet {
    @media print {
      P {
        color: green
      }
      P {
        color: red
      }
    }
  }
} -result [list]

tcltest::test style-6.4 {} -body {
  parse_stylesheet {
    @page {
      @bottom-right {
        font-family: helvetica, sans-serif;
        content: counter(page);
        text-align: right;
        vertical-align: top;
        padding-top: 2em;
      }
    }

    @media print {
      P {
        color: red
      }
    }
  }
} -result [list]

tcltest::test style-6.5 {} -body {
  parse_stylesheet {
SUP {
    vertical-align: super;
    font-size: 80%
}

@page {
  @bottom-right {
    font-family: helvetica, sans-serif;
    content: counter(page);
    text-align: right;
    vertical-align: top;
    padding-top: 2em;
  }
}

@media print {
P { margin-top: 0; }
.dlink { display: none; }
}
  }
} -result [list [list sup {vertical-align:super; font-size:80.00%}]]

# Ensure only the default stylesheet can register Tcl scripts.
tcltest::test style-7.1 {} -body {
  parse_stylesheet {
    p {color:tcl(script)}
  }
} -result [list [list p {}]]

#----------------------------------------------------------------------------
# The following tests - style-8.* - come from the selectors in the acid2 page.
#
tcltest::test style-8.1 {} -body {
  .h reset
  .h parse -final {
    <style>
      [class~=one].first.one {color: red}
    </style>
    <div class="first one">
  }
  set node [lindex [.h search div] 0]
  property $node color
} -result red
tcltest::test style-8.2 {} -body {
  .h reset
  .h configure -defaultstyle ""
  .h parse -final {
    <style>
      [class~=one][class~=first] [class=second\ two][class="second two"] 
      {color : red}
    </style>
    <div class="first one">
      <div id="1" class="second two">
  }
  set node [lindex [.h search {#1}] 0]
  property $node color
} -result red

tcltest::test style-8.3 {} -body {
  .h configure -defaultstyle ""
  .h reset
  .h parse -final {
    <style>
      div ] .eyes{position:absolute}
    </style>
    <div class="eyes">
  }
  set node [lindex [.h search .eyes] 0]
  property $node position
} -result static

tcltest::test style-8.4 {} -body {
  .h reset
  .h configure -defaultstyle ""
  .h parse -final {
    <style>
      /* Note the syntax error in the next line! */
      [class=second two] { background: red; }
      .eyes { position: absolute }
    </style>
    <div class="eyes">
  }
  set node [lindex [.h search .eyes] 0]
  property $node position
} -result absolute

tcltest::test style-8.5 {} -body {
  .h reset
  .h configure -defaultstyle ""
  .h parse -final {
    <style>
    div {
background: yellow /* comment */ no-repeat /* comment */ ;
    }
    </style>
    <div>
  }
  set node [lindex [.h search div] 0]
  property $node background-color
} -result yellow

tcltest::test style-9.1 {} -body {
  .h reset
  .h configure -defaultstyle ""
  .h parse -final {
    <style>
.four {color: purple;}
@import url(imptest2.css);
P.six {color: green;}
    </style>
    <p class=six>
  }
  set node [lindex [.h search p] 0]
  property $node color
} -result {green}

tcltest::test style-9.2 {} -body {
  parse_stylesheet {
    p ! p {color: purple;}
  }
} -result {}
tcltest::test style-9.3 {} -body {
  parse_stylesheet {
    p {COLOR: PURPLE;}
  }
} -result [list {p color:PURPLE}]
tcltest::test style-9.4 {} -body {
  parse_stylesheet {
    @three-dee {
      @background-lighting { 
        azimuth: 30deg; elevation: 190deg; 
      }
      P.seven { color: red }
    }
    P.eight {COLOR: GREEN;}
  }
} -result [list {p.eight color:GREEN}]


#----------------------------------------------------------------------------
# The following tests - style-10.* - test that the 'line-height' property
# is parsed and applied to elements correctly.
#
tcltest::test style-10.1 {} -body {
  .h reset
  .h parse -final {
    <html><head><style>

      .one   { line-height: normal; }
      .two   { line-height: 50%; font-size:20px; }
      .three { line-height: 130px;  }
      .four  { line-height: 2;  }
      .five  { line-height: -10px; }    /* Negative values are illegal */

    </style></head><body>
    <div class=one></div>
    <div class=two></div>
    <div class=three></div>
    <div class=four></div>
    <div class=five></div>
  }

  set res [list]
  foreach class [list .one .two .three .four .five] {
    lappend res [[lindex [.h search $class] 0] property line-height]
  }
  set res
} -result [list normal 10px 130px 2.00 normal]

#----------------------------------------------------------------------------
# The following tests - style-10.* - test property and override method.
#
tcltest::test style-11.1 {} -body {
    .h reset
    .h parse -final {
	<html>
	<body>
	<div id="outer">
	<h2>foo</h2>
	<div id="inner" style="background: none repeat scroll 0% 0%">bar</div>
	baz</div>
	</body>
	</html>
    }
    pack .h
    set n [.h search {div[id="inner"]}]
    # wish: ./src/htmlprop.c:3035: HtmlNodeGetProperty:
    #  Assertion `eProp <= 108' failed.
    $n property background
} -result [list none repeat scroll 0% 0%]

tcltest::test style-11.2 {} -body {
    $n override 
} -result {}

tcltest::test style-11.3 {} -body {
    # Missusing of trailing ':'
    set res [$n override {background-color: red}]
    after idle [list set ::wait 1]
    vwait ::wait
    # wish: ./src/htmlprop.c:300: getPropertyDef:
    #  Assertion `eProp >= 0' failed.
    set res
} -result {background-color: red}

tcltest::test style-11.4 {} -body {
    set res [$n override {background-color red}]
} -result {background-color red}


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

finish_test