File: test_box.py

package info (click to toggle)
weasyprint 62.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,964 kB
  • sloc: python: 54,652; makefile: 12
file content (578 lines) | stat: -rw-r--r-- 13,156 bytes parent folder | download
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
578
"""Test how boxes, borders, outlines are drawn."""

import itertools

import pytest

from weasyprint import HTML

from ..testing_utils import assert_no_logs


@assert_no_logs
def test_borders(assert_pixels, assert_different_renderings, margin='10px',
                 prop='border'):
    """Test the rendering of borders"""
    source = '''
      <style>
        @page { size: 140px 110px }
        body { width: 100px; height: 70px; margin: %s; %s: 10px %s blue }
      </style>
      <body>'''

    # Do not test the exact rendering of earch border style but at least
    # check that they do not do the same.
    documents = (
        source % (margin, prop, border_style)
        for border_style in (
            'none', 'solid', 'dashed', 'dotted', 'double',
            'inset', 'outset', 'groove', 'ridge'))
    assert_different_renderings(*documents)

    css_margin = margin
    width = 140
    height = 110
    margin = 10
    border = 10
    solid_pixels = [['_'] * width for _ in range(height)]
    for x in range(margin, width - margin):
        for y in itertools.chain(
                range(margin, margin + border),
                range(height - margin - border, height - margin)):
            solid_pixels[y][x] = 'B'
    for y in range(margin, height - margin):
        for x in itertools.chain(
                range(margin, margin + border),
                range(width - margin - border, width - margin)):
            solid_pixels[y][x] = 'B'
    pixels = '\n'.join(''.join(chars) for chars in solid_pixels)
    html = source % (css_margin, prop, 'solid')
    assert_pixels(pixels, html)


@assert_no_logs
def test_borders_table_collapse(assert_pixels, assert_different_renderings):
    """Test the rendering of collapsing borders."""
    source = '''
      <style>
        @page { size: 140px 110px }
        table { width: 100px; height: 70px; margin: 10px;
                border-collapse: collapse; border: 10px %s blue }
      </style>
      <table><td>abc</td>'''

    # Do not test the exact rendering of earch border style but at least
    # check that they do not do the same.
    documents = (
        source % border_style
        for border_style in (
            'none', 'solid', 'dashed', 'dotted', 'double',
            'inset', 'outset', 'groove', 'ridge'))
    assert_different_renderings(*documents)


@assert_no_logs
def test_outlines(assert_pixels, assert_different_renderings):
    return test_borders(
        assert_pixels, assert_different_renderings,
        margin='20px', prop='outline')


@assert_no_logs
@pytest.mark.parametrize('border_style', ('none', 'solid', 'dashed', 'dotted'))
def test_small_borders_1(border_style):
    # Regression test for ZeroDivisionError on dashed or dotted borders
    # smaller than a dash/dot.
    # https://github.com/Kozea/WeasyPrint/issues/49
    html = '''
      <style>
        @page { size: 50px 50px }
        body { margin: 5px; height: 0; border: 10px %s blue }
      </style>
      <body>''' % border_style
    HTML(string=html).write_pdf()


@assert_no_logs
@pytest.mark.parametrize('border_style', ('none', 'solid', 'dashed', 'dotted'))
def test_small_borders_2(border_style):
    # Regression test for ZeroDivisionError on dashed or dotted borders
    # smaller than a dash/dot.
    # https://github.com/Kozea/WeasyPrint/issues/146
    html = '''
      <style>
        @page { size: 50px 50px }
        body { height: 0; width: 0; border-width: 1px 0; border-style: %s }
      </style>
      <body>''' % border_style
    HTML(string=html).write_pdf()


@assert_no_logs
def test_em_borders():
    # Regression test for https://github.com/Kozea/WeasyPrint/issues/1378
    html = '<body style="border: 1em solid">'
    HTML(string=html).write_pdf()


@assert_no_logs
def test_borders_box_sizing(assert_pixels):
    assert_pixels('''
        ________
        _RRRRRR_
        _R____R_
        _RRRRRR_
        ________
    ''', '''
      <style>
        @page {
          size: 8px 5px;
        }
        div {
          border: 1px solid red;
          box-sizing: border-box;
          height: 3px;
          margin: 1px;
          min-height: auto;
          min-width: auto;
          width: 6px;
        }
      </style>
      <div></div>
    ''')


@assert_no_logs
def test_margin_boxes(assert_pixels):
    assert_pixels('''
        _______________
        _GGG______BBBB_
        _GGG______BBBB_
        _______________
        _____RRRR______
        _____RRRR______
        _____RRRR______
        _____RRRR______
        _______________
        _bbb______gggg_
        _bbb______gggg_
        _bbb______gggg_
        _bbb______gggg_
        _bbb______gggg_
        _______________
    ''', '''
      <style>
        html { height: 100% }
        body { background: #f00; height: 100% }
        @page {
          size: 15px;
          margin: 4px 6px 7px 5px;

          @top-left-corner {
            margin: 1px;
            content: " ";
            background: #0f0;
          }
          @top-right-corner {
            margin: 1px;
            content: " ";
            background: #00f;
          }
          @bottom-right-corner {
            margin: 1px;
            content: " ";
            background: #008000;
          }
          @bottom-left-corner {
            margin: 1px;
            content: " ";
            background: #000080;
          }
        }
      </style>
      <body>''')


@assert_no_logs
def test_display_inline_block_twice():
    # Regression test for inline blocks displayed twice.
    # https://github.com/Kozea/WeasyPrint/issues/880
    html = '<div style="background: red; display: inline-block">'
    document = HTML(string=html).render()
    assert document.write_pdf() == document.write_pdf()


@assert_no_logs
def test_draw_border_radius(assert_pixels):
    assert_pixels('''
        ___zzzzz
        __zzzzzz
        _zzzzzzz
        zzzzzzzz
        zzzzzzzz
        zzzzzzzR
        zzzzzzRR
        zzzzzRRR
    ''', '''
      <style>
        @page {
          size: 8px 8px;
        }
        div {
          background: red;
          border-radius: 50% 0 0 0;
          height: 16px;
          width: 16px;
        }
      </style>
      <div></div>
    ''')


@assert_no_logs
def test_draw_split_border_radius(assert_pixels):
    assert_pixels('''
        ___zzzzz
        __zzzzzz
        _zzzzzzz
        zzzzzzzz
        zzzzzzzz
        zzzzzzzz
        zzzzzzRR
        zzzzzRRR

        RRRRRRRR
        RRRRRRRR
        RRRRRRRR
        RRRRRRRR
        RRRRRRRR
        RRRRRRRR
        RRRRRRRR
        RRRRRRRR

        zzzzzzRR
        zzzzzzzR
        zzzzzzzz
        zzzzzzzz
        zzzzzzzz
        zzzzzzzz
        _zzzzzzz
        __zzzzzz
    ''', '''
      <style>
        @page {
          size: 8px 8px;
        }
        div {
          background: red;
          color: transparent;
          border-radius: 8px;
          line-height: 9px;
          width: 16px;
        }
      </style>
      <div>a b c</div>
    ''')


@assert_no_logs
def test_border_image_stretch(assert_pixels):
    assert_pixels('''
        __________
        _RYYYMMMG_
        _M______C_
        _M______C_
        _Y______Y_
        _Y______Y_
        _BYYYCCCK_
        __________
    ''', '''
      <style>
        @page {
          size: 10px 8px;
        }
        div {
          border: 1px solid black;
          border-image-source: url(border.svg);
          border-image-slice: 25%;
          height: 4px;
          margin: 1px;
          width: 6px;
        }
      </style>
      <div></div>
    ''')


@assert_no_logs
def test_border_image_fill(assert_pixels):
    assert_pixels('''
        __________
        _RYYYMMMG_
        _MbbbgggC_
        _MbbbgggC_
        _YgggbbbY_
        _YgggbbbY_
        _BYYYCCCK_
        __________
    ''', '''
      <style>
        @page {
          size: 10px 8px;
        }
        div {
          border: 1px solid black;
          border-image-source: url(border.svg);
          border-image-slice: 25% fill;
          height: 4px;
          margin: 1px;
          width: 6px;
        }
      </style>
      <div></div>
    ''')


@assert_no_logs
def test_border_image_default_slice(assert_pixels):
    assert_pixels('''
        _____________
        _RYMG___RYMG_
        _MbgC___MbgC_
        _YgbY___YgbY_
        _BYCK___BYCK_
        _____________
        _____________
        _RYMG___RYMG_
        _MbgC___MbgC_
        _YgbY___YgbY_
        _BYCK___BYCK_
        _____________
    ''', '''
      <style>
        @page {
          size: 13px 12px;
        }
        div {
          border: 4px solid black;
          border-image-source: url(border.svg);
          height: 2px;
          margin: 1px;
          width: 3px;
        }
      </style>
      <div></div>
    ''')


@assert_no_logs
def test_border_image_uneven_width(assert_pixels):
    assert_pixels('''
        ____________
        _RRRYYYMMMG_
        _MMM______C_
        _MMM______C_
        _YYY______Y_
        _YYY______Y_
        _BBBYYYCCCK_
        ____________
    ''', '''
      <style>
        @page {
          size: 12px 8px;
        }
        div {
          border: 1px solid black;
          border-left-width: 3px;
          border-image-source: url(border.svg);
          border-image-slice: 25%;
          height: 4px;
          margin: 1px;
          width: 6px;
        }
      </style>
      <div></div>
    ''')


@assert_no_logs
def test_border_image_not_percent(assert_pixels):
    assert_pixels('''
        __________
        _RYYYMMMG_
        _M______C_
        _M______C_
        _Y______Y_
        _Y______Y_
        _BYYYCCCK_
        __________
    ''', '''
      <style>
        @page {
          size: 10px 8px;
        }
        div {
          border: 1px solid black;
          border-image-source: url(border.svg);
          border-image-slice: 1;
          height: 4px;
          margin: 1px;
          width: 6px;
        }
      </style>
      <div></div>
    ''')


@assert_no_logs
def test_border_image_repeat(assert_pixels):
    assert_pixels('''
        ___________
        _RYMYMYMYG_
        _M_______C_
        _Y_______Y_
        _M_______C_
        _Y_______Y_
        _BYCYCYCYK_
        ___________
    ''', '''
      <style>
        @page {
          size: 11px 8px;
        }
        div {
          border: 1px solid black;
          border-image-source: url(border.svg);
          border-image-slice: 25%;
          border-image-repeat: repeat;
          height: 4px;
          margin: 1px;
          width: 7px;
        }
      </style>
      <div></div>
    ''')


@assert_no_logs
def test_border_image_space(assert_pixels):
    assert_pixels('''
        _________
        _R_YMC_G_
        _________
        _M_____C_
        _Y_____Y_
        _C_____M_
        _________
        _B_YCM_K_
        _________
    ''', '''
      <style>
        @page {
          size: 9px 9px;
        }
        div {
          border: 1px solid black;
          border-image-source: url(border2.svg);
          border-image-slice: 20%;
          border-image-repeat: space;
          height: 5px;
          margin: 1px;
          width: 5px;
        }
      </style>
      <div></div>
    ''')


@assert_no_logs
def test_border_image_outset(assert_pixels):
    assert_pixels('''
        ____________
        _RYYYYMMMMG_
        _M________C_
        _M_bbbbbb_C_
        _M_bbbbbb_C_
        _Y_bbbbbb_Y_
        _Y_bbbbbb_Y_
        _Y________Y_
        _BYYYYCCCCK_
        ____________
    ''', '''
      <style>
        @page {
          size: 12px 10px;
        }
        div {
          border: 1px solid black;
          border-image-source: url(border.svg);
          border-image-slice: 25%;
          border-image-outset: 2px;
          height: 2px;
          margin: 3px;
          width: 4px;
          background: #000080
        }
      </style>
      <div></div>
    ''')


@assert_no_logs
def test_border_image_width(assert_pixels):
    assert_pixels('''
        __________
        _RRYYMMGG_
        _RRYYMMGG_
        _MM____CC_
        _YY____YY_
        _BBYYCCKK_
        _BBYYCCKK_
        __________
    ''', '''
      <style>
        @page {
          size: 10px 8px;
        }
        div {
          border: 1px solid black;
          border-image-source: url(border.svg);
          border-image-slice: 25%;
          border-image-width: 2;
          height: 4px;
          margin: 1px;
          width: 6px;
        }
      </style>
      <div></div>
    ''')


@assert_no_logs
def test_border_image_gradient(assert_pixels):
    assert_pixels('''
        __________
        _RRRRRRRR_
        _RRRRRRRR_
        _RR____RR_
        _BB____BB_
        _BBBBBBBB_
        _BBBBBBBB_
        __________
    ''', '''
      <style>
        @page {
          size: 10px 8px;
        }
        div {
          border: 1px solid black;
          border-image-source: linear-gradient(to bottom, red, red 50%, blue 50%, blue);
          border-image-slice: 25%;
          border-image-width: 2;
          height: 4px;
          margin: 1px;
          width: 6px;
        }
      </style>
      <div></div>
    ''')