File: test_presentational_hints.py

package info (click to toggle)
weasyprint 67.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,896 kB
  • sloc: python: 61,025; makefile: 12
file content (274 lines) | stat: -rw-r--r-- 9,387 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
"""Test the HTML presentational hints."""

from weasyprint import CSS, HTML

from .testing_utils import BASE_URL, assert_no_logs

PH_TESTING_CSS = CSS(string='''
@page {margin: 0; size: 1000px 1000px}
body {margin: 0}
''')


@assert_no_logs
def test_no_ph():
    # Test both CSS and non-CSS rules
    document = HTML(string='''
      <hr size=100 />
      <table align=right width=100><td>0</td></table>
    ''').render(stylesheets=[PH_TESTING_CSS])
    page, = document.pages
    html, = page._page_box.children
    body, = html.children
    hr, table = body.children
    assert hr.border_height() != 100
    assert table.position_x == 0


@assert_no_logs
def test_ph_page():
    document = HTML(string='''
      <body marginheight=2 topmargin=3 leftmargin=5
            bgcolor=red text=blue />
    ''').render(stylesheets=[PH_TESTING_CSS], presentational_hints=True)
    page, = document.pages
    html, = page._page_box.children
    body, = html.children
    assert body.margin_top == 2
    assert body.margin_bottom == 2
    assert body.margin_left == 5
    assert body.margin_right == 0
    assert body.style['background_color'] == (1, 0, 0, 1)
    assert body.style['color'] == (0, 0, 1, 1)


@assert_no_logs
def test_ph_flow():
    document = HTML(string='''
      <pre wrap></pre>
      <center></center>
      <div align=center></div>
      <div align=middle></div>
      <div align=left></div>
      <div align=right></div>
      <div align=justify></div>
    ''').render(stylesheets=[PH_TESTING_CSS], presentational_hints=True)
    page, = document.pages
    html, = page._page_box.children
    body, = html.children
    pre, center, div1, div2, div3, div4, div5 = body.children
    assert pre.style['white_space'] == 'pre-wrap'
    assert center.style['text_align_all'] == 'center'
    assert div1.style['text_align_all'] == 'center'
    assert div2.style['text_align_all'] == 'center'
    assert div3.style['text_align_all'] == 'left'
    assert div4.style['text_align_all'] == 'right'
    assert div5.style['text_align_all'] == 'justify'


@assert_no_logs
def test_ph_phrasing():
    document = HTML(string='''
      <br clear=left>
      <br clear=right />
      <br clear=both />
      <br clear=all />
      <font color=red face=weasyprint size=7></font>
      <Font size=4></Font>
      <font size=+5 ></font>
      <font size=-5 ></font>
    ''', base_url=BASE_URL).render(
        stylesheets=[PH_TESTING_CSS], presentational_hints=True)
    page, = document.pages
    html, = page._page_box.children
    body, = html.children
    line1, line2, line3, line4, line5 = body.children
    br1, = line1.children
    br2, = line2.children
    br3, = line3.children
    br4, = line4.children
    font1, font2, font3, font4 = line5.children
    assert br1.style['clear'] == 'left'
    assert br2.style['clear'] == 'right'
    assert br3.style['clear'] == 'both'
    assert br4.style['clear'] == 'both'
    assert font1.style['color'] == (1, 0, 0, 1)
    assert font1.style['font_family'] == ('weasyprint',)
    assert font1.style['font_size'] == 1.5 * 2 * 16
    assert font2.style['font_size'] == 6 / 5 * 16
    assert font3.style['font_size'] == 1.5 * 2 * 16
    assert font4.style['font_size'] == 8 / 9 * 16


@assert_no_logs
def test_ph_lists():
    document = HTML(string='''
      <ol>
        <li type=A></li>
        <li type=1></li>
        <li type=a></li>
        <li type=i></li>
        <li type=I></li>
      </ol>
      <ul>
        <li type=circle></li>
        <li type=disc></li>
        <li type=square></li>
      </ul>
    ''').render(stylesheets=[PH_TESTING_CSS], presentational_hints=True)
    page, = document.pages
    html, = page._page_box.children
    body, = html.children
    ol, ul = body.children
    oli1, oli2, oli3, oli4, oli5 = ol.children
    uli1, uli2, uli3 = ul.children
    assert oli1.style['list_style_type'] == 'upper-alpha'
    assert oli2.style['list_style_type'] == 'decimal'
    assert oli3.style['list_style_type'] == 'lower-alpha'
    assert oli4.style['list_style_type'] == 'lower-roman'
    assert oli5.style['list_style_type'] == 'upper-roman'
    assert uli1.style['list_style_type'] == 'circle'
    assert uli2.style['list_style_type'] == 'disc'
    assert uli3.style['list_style_type'] == 'square'


@assert_no_logs
def test_ph_lists_types():
    document = HTML(string='''
      <ol type=A></ol>
      <ol type=1></ol>
      <ol type=a></ol>
      <ol type=i></ol>
      <ol type=I></ol>
      <ul type=circle></ul>
      <ul type=disc></ul>
      <ul type=square></ul>
    ''').render(stylesheets=[PH_TESTING_CSS], presentational_hints=True)
    page, = document.pages
    html, = page._page_box.children
    body, = html.children
    ol1, ol2, ol3, ol4, ol5, ul1, ul2, ul3 = body.children
    assert ol1.style['list_style_type'] == 'upper-alpha'
    assert ol2.style['list_style_type'] == 'decimal'
    assert ol3.style['list_style_type'] == 'lower-alpha'
    assert ol4.style['list_style_type'] == 'lower-roman'
    assert ol5.style['list_style_type'] == 'upper-roman'
    assert ul1.style['list_style_type'] == 'circle'
    assert ul2.style['list_style_type'] == 'disc'
    assert ul3.style['list_style_type'] == 'square'


@assert_no_logs
def test_ph_tables():
    document = HTML(string='''
      <table align=left rules=none></table>
      <table align=right rules=groups></table>
      <table align=center rules=rows></table>
      <table border=10 cellspacing=3 bordercolor=green>
        <thead>
          <tr>
            <th valign=top></th>
          </tr>
        </thead>
        <tr>
          <td nowrap><h1 align=right></h1><p align=center></p></td>
        </tr>
        <tr>
        </tr>
        <tfoot align=justify>
          <tr>
            <td></td>
          </tr>
        </tfoot>
      </table>
    ''').render(stylesheets=[PH_TESTING_CSS], presentational_hints=True)
    page, = document.pages
    html, = page._page_box.children
    body, = html.children
    wrapper1, wrapper2, wrapper3, wrapper4, = body.children
    assert wrapper1.style['float'] == 'left'
    assert wrapper2.style['float'] == 'right'
    assert wrapper3.style['margin_left'] == 'auto'
    assert wrapper3.style['margin_right'] == 'auto'
    assert wrapper1.children[0].style['border_left_style'] == 'hidden'
    assert wrapper1.style['border_collapse'] == 'collapse'
    assert wrapper2.children[0].style['border_left_style'] == 'hidden'
    assert wrapper2.style['border_collapse'] == 'collapse'
    assert wrapper3.children[0].style['border_left_style'] == 'hidden'
    assert wrapper3.style['border_collapse'] == 'collapse'

    table4, = wrapper4.children
    assert table4.style['border_top_style'] == 'outset'
    assert table4.style['border_top_width'] == 10
    assert table4.style['border_spacing'] == (3, 3)
    r, g, b, a = table4.style['border_left_color']
    assert g > r
    assert g > b
    head_group, rows_group, foot_group = table4.children
    head, = head_group.children
    th, = head.children
    assert th.style['vertical_align'] == 'top'
    line1, line2 = rows_group.children
    td, = line1.children
    assert td.style['white_space'] == 'nowrap'
    assert td.style['border_top_width'] == 1
    assert td.style['border_top_style'] == 'inset'
    h1, p = td.children
    assert h1.style['text_align_all'] == 'right'
    assert p.style['text_align_all'] == 'center'
    foot, = foot_group.children
    tr, = foot.children
    assert tr.style['text_align_all'] == 'justify'


@assert_no_logs
def test_ph_hr():
    document = HTML(string='''
      <hr align=left>
      <hr align=right />
      <hr align=both color=red />
      <hr align=center noshade size=10 />
      <hr align=all size=8 width=100 />
    ''').render(stylesheets=[PH_TESTING_CSS], presentational_hints=True)
    page, = document.pages
    html, = page._page_box.children
    body, = html.children
    hr1, hr2, hr3, hr4, hr5 = body.children
    assert hr1.margin_left == 0
    assert hr1.style['margin_right'] == 'auto'
    assert hr2.style['margin_left'] == 'auto'
    assert hr2.margin_right == 0
    assert hr3.style['margin_left'] == 'auto'
    assert hr3.style['margin_right'] == 'auto'
    assert hr3.style['color'] == (1, 0, 0, 1)
    assert hr4.style['margin_left'] == 'auto'
    assert hr4.style['margin_right'] == 'auto'
    assert hr4.border_height() == 10
    assert hr4.style['border_top_width'] == 5
    assert hr5.border_height() == 8
    assert hr5.height == 6
    assert hr5.width == 100
    assert hr5.style['border_top_width'] == 1


@assert_no_logs
def test_ph_embedded():
    document = HTML(string='''
      <object data="data:image/svg+xml,<svg></svg>"
              align=top hspace=10 vspace=20></object>
      <img src="data:image/svg+xml,<svg></svg>" alt=text
              align=right width=10 height=20 />
      <embed src="data:image/svg+xml,<svg></svg>" align=texttop />
    ''').render(stylesheets=[PH_TESTING_CSS], presentational_hints=True)
    page, = document.pages
    html, = page._page_box.children
    body, = html.children
    line, = body.children
    object_, text1, img, embed, text2 = line.children
    assert embed.style['vertical_align'] == 'text-top'
    assert object_.style['vertical_align'] == 'top'
    assert object_.margin_top == 20
    assert object_.margin_left == 10
    assert img.style['float'] == 'right'
    assert img.width == 10
    assert img.height == 20