"""Test CSS counters.""" import pytest from weasyprint import HTML from .testing_utils import assert_no_logs, assert_tree, parse_all, render_pages @assert_no_logs def test_counters_1(): assert_tree(parse_all('''

'''), [ ('p', 'Block', [ ('p', 'Line', [ ('p::before', 'Inline', [ ('p::before', 'Text', counter)])])]) for counter in '0 1 3 2 4 6 -11 -9 -7 44 46 48'.split()]) @assert_no_logs def test_counters_2(): assert_tree(parse_all('''
'''), [ ('ol', 'Block', [ ('li', 'Block', [ ('li', 'Line', [ ('li::marker', 'Inline', [ ('li::marker', 'Text', '1. ')])])]), ('li', 'Block', [ ('li', 'Line', [ ('li::marker', 'Inline', [ ('li::marker', 'Text', '2. ')])])]), ('li', 'Block', [ ('li', 'Line', [ ('li::marker', 'Inline', [ ('li::marker', 'Text', '3. ')])])]), ('li', 'Block', [ ('li', 'Block', [ ('li', 'Line', [ ('li::marker', 'Inline', [ ('li::marker', 'Text', '4. ')])])]), ('ol', 'Block', [ ('li', 'Block', [ ('li', 'Line', [ ('li::marker', 'Inline', [ ('li::marker', 'Text', '1. ')])])]), ('li', 'Block', [ ('li', 'Line', [ ('li::marker', 'Inline', [ ('li::marker', 'Text', '1. ')])])]), ('li', 'Block', [ ('li', 'Line', [ ('li::marker', 'Inline', [ ('li::marker', 'Text', '2. ')])])])])]), ('li', 'Block', [ ('li', 'Line', [ ('li::marker', 'Inline', [ ('li::marker', 'Text', '5. ')])])])])]) @assert_no_logs def test_counters_3(): assert_tree(parse_all('''

'''), [ ('div', 'Block', [ ('p', 'Block', [ ('p', 'Line', [ ('p::marker', 'Inline', [ ('p::marker', 'Text', '1. ')])])]), ('p', 'Block', [ ('p', 'Line', [ ('p::marker', 'Inline', [ ('p::marker', 'Text', '2. ')])])]), ('p', 'Block', [ ('p', 'Line', [ ('p::marker', 'Inline', [ ('p::marker', 'Text', '-55. ')])])])]), ('p', 'Block', [ ('p', 'Line', [ ('p::marker', 'Inline', [ ('p::marker', 'Text', '1. ')])])])]) @assert_no_logs def test_counters_4(): assert_tree(parse_all('''

'''), [ ('section', 'Block', [ ('section', 'Block', [ ('section', 'Line', [ ('section::before', 'Inline', [])])]), ('h1', 'Block', [ ('h1', 'Line', [ ('h1::before', 'Inline', [ ('h1::before', 'Text', '1')])])]), ('h1', 'Block', [ ('h1', 'Line', [ ('h1::before', 'Inline', [ ('h1::before', 'Text', '2')])])]), ('section', 'Block', [ ('section', 'Block', [ ('section', 'Line', [ ('section::before', 'Inline', [])])]), ('h1', 'Block', [ ('h1', 'Line', [ ('h1::before', 'Inline', [ ('h1::before', 'Text', '2.1')])])]), ('h1', 'Block', [ ('h1', 'Line', [ ('h1::before', 'Inline', [ ('h1::before', 'Text', '2.2')])])])]), ('h1', 'Block', [ ('h1', 'Line', [ ('h1::before', 'Inline', [ ('h1::before', 'Text', '3')])])])])]) @assert_no_logs def test_counters_5(): assert_tree(parse_all('''
Scope created now, deleted after the div

'''), [ ('div', 'Block', [ ('div', 'Line', [ ('span', 'Inline', [ ('span', 'Text', 'Scope created now, deleted after the div ')])])]), ('p', 'Block', [ ('p', 'Line', [ ('p::before', 'Inline', [ ('p::before', 'Text', '0')])])])]) @assert_no_logs def test_counters_6(): # counter-increment may interfere with display: list-item assert_tree(parse_all('''

'''), [ ('p', 'Block', [ ('p', 'Line', [ ('p::marker', 'Inline', [ ('p::marker', 'Text', '0. ')])])])]) @assert_no_logs def test_counters_7(): # Test that counters are case-sensitive # See https://github.com/Kozea/WeasyPrint/pull/827 assert_tree(parse_all('''

'''), [ ('p', 'Block', [ ('p', 'Line', [ ('p::before', 'Inline', [ ('p::before', 'Text', counter)])])]) for counter in '2.0 2.3 4.3'.split()]) @assert_no_logs def test_counters_8(): assert_tree(parse_all('''

'''), 2 * [ ('p', 'Block', [ ('p::before', 'Block', [ ('p::marker', 'Block', [ ('p::marker', 'Line', [ ('p::marker', 'Text', '• ')])]), ('p::before', 'Block', [ ('p::before', 'Line', [ ('p::before', 'Text', 'a')])])])])]) @pytest.mark.xfail @assert_no_logs def test_counters_9(): document = HTML(string='''
''').render() page, = document.pages html, = page._page_box.children body, = html.children ol1, = body.children oli1, oli2, oli3 = ol1.children marker, ol2 = oli2.children oli21, oli22 = ol2.children assert oli1.children[0].children[0].children[0].text == '1. ' assert oli2.children[0].children[0].children[0].text == '2. ' assert oli21.children[0].children[0].children[0].text == '1. ' assert oli22.children[0].children[0].children[0].text == '2. ' assert oli3.children[0].children[0].children[0].text == '3. ' @assert_no_logs def test_counter_styles_1(): assert_tree(parse_all('''

'''), [ ('p', 'Block', [ ('p', 'Line', [ ('p::before', 'Inline', [ ('p::before', 'Text', counter)])])]) for counter in '-- • ◦ ▪ -7 Counter:-6 -5:Counter'.split()]) @assert_no_logs def test_counter_styles_2(): assert_tree(parse_all('''

'''), [ ('p', 'Block', [ ('p', 'Line', [ ('p::before', 'Inline', [ ('p::before', 'Text', counter)])])]) for counter in '''-1986 -1985 -11 -10 -9 -8 -1 00 01 02 09 10 11 99 100 101 4135 4136'''.split()]) @assert_no_logs def test_counter_styles_3(): render = HTML(string='')._ua_counter_style()[0].render_value assert [render(value, 'decimal-leading-zero') for value in [ -1986, -1985, -11, -10, -9, -8, -1, 0, 1, 2, 9, 10, 11, 99, 100, 101, 4135, 4136 ]] == ''' -1986 -1985 -11 -10 -9 -8 -1 00 01 02 09 10 11 99 100 101 4135 4136 '''.split() @assert_no_logs def test_counter_styles_4(): render = HTML(string='')._ua_counter_style()[0].render_value assert [render(value, 'lower-roman') for value in [ -1986, -1985, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 49, 50, 389, 390, 3489, 3490, 3491, 4999, 5000, 5001 ]] == ''' -1986 -1985 -1 0 i ii iii iv v vi vii viii ix x xi xii xlix l ccclxxxix cccxc mmmcdlxxxix mmmcdxc mmmcdxci 4999 5000 5001 '''.split() @assert_no_logs def test_counter_styles_5(): render = HTML(string='')._ua_counter_style()[0].render_value assert [render(value, 'upper-roman') for value in [ -1986, -1985, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 49, 50, 389, 390, 3489, 3490, 3491, 4999, 5000, 5001 ]] == ''' -1986 -1985 -1 0 I II III IV V VI VII VIII IX X XI XII XLIX L CCCLXXXIX CCCXC MMMCDLXXXIX MMMCDXC MMMCDXCI 4999 5000 5001 '''.split() @assert_no_logs def test_counter_styles_6(): render = HTML(string='')._ua_counter_style()[0].render_value assert [render(value, 'lower-alpha') for value in [ -1986, -1985, -1, 0, 1, 2, 3, 4, 25, 26, 27, 28, 29, 2002, 2003 ]] == ''' -1986 -1985 -1 0 a b c d y z aa ab ac bxz bya '''.split() @assert_no_logs def test_counter_styles_7(): render = HTML(string='')._ua_counter_style()[0].render_value assert [render(value, 'upper-alpha') for value in [ -1986, -1985, -1, 0, 1, 2, 3, 4, 25, 26, 27, 28, 29, 2002, 2003 ]] == ''' -1986 -1985 -1 0 A B C D Y Z AA AB AC BXZ BYA '''.split() @assert_no_logs def test_counter_styles_8(): render = HTML(string='')._ua_counter_style()[0].render_value assert [render(value, 'lower-latin') for value in [ -1986, -1985, -1, 0, 1, 2, 3, 4, 25, 26, 27, 28, 29, 2002, 2003 ]] == ''' -1986 -1985 -1 0 a b c d y z aa ab ac bxz bya '''.split() @assert_no_logs def test_counter_styles_9(): render = HTML(string='')._ua_counter_style()[0].render_value assert [render(value, 'upper-latin') for value in [ -1986, -1985, -1, 0, 1, 2, 3, 4, 25, 26, 27, 28, 29, 2002, 2003 ]] == ''' -1986 -1985 -1 0 A B C D Y Z AA AB AC BXZ BYA '''.split() @assert_no_logs def test_counter_styles_10(): render = HTML(string='')._ua_counter_style()[0].render_value assert [render(value, 'georgian') for value in [ -1986, -1985, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 19999, 20000, 20001 ]] == ''' -1986 -1985 -1 0 ა ბ გ დ ე ვ ზ ჱ თ ი ია იბ კ ლ მ ნ ჲ ო პ ჟ რ ს ტ ჳ ფ ქ ღ ყ შ ჩ ც ძ წ ჭ ხ ჴ ჯ ჰ ჵ ჵჰშჟთ 20000 20001 '''.split() @assert_no_logs def test_counter_styles_11(): render = HTML(string='')._ua_counter_style()[0].render_value assert [render(value, 'armenian') for value in [ -1986, -1985, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 9999, 10000, 10001 ]] == ''' -1986 -1985 -1 0 Ա Բ Գ Դ Ե Զ Է Ը Թ Ժ ԺԱ ԺԲ Ի Լ Խ Ծ Կ Հ Ձ Ղ Ճ Մ Յ Ն Շ Ո Չ Պ Ջ Ռ Ս Վ Տ Ր Ց Ւ Փ Ք ՔՋՂԹ 10000 10001 '''.split() @assert_no_logs @pytest.mark.parametrize('arguments, values', ( ('cyclic "a" "b" "c"', ('a ', 'b ', 'c ', 'a ')), ('symbolic "a" "b"', ('a ', 'b ', 'aa ', 'bb ')), ('"a" "b"', ('a ', 'b ', 'aa ', 'bb ')), ('alphabetic "a" "b"', ('a ', 'b ', 'aa ', 'ab ')), ('fixed "a" "b"', ('a ', 'b ', '3 ', '4 ')), ('numeric "0" "1" "2"', ('1 ', '2 ', '10 ', '11 ')), )) def test_counter_symbols(arguments, values): page, = render_pages('''
  1. abc
  2. abc
  3. abc
  4. abc
''' % arguments) html, = page.children body, = html.children ol, = body.children li_1, li_2, li_3, li_4 = ol.children assert li_1.children[0].children[0].children[0].text == values[0] assert li_2.children[0].children[0].children[0].text == values[1] assert li_3.children[0].children[0].children[0].text == values[2] assert li_4.children[0].children[0].children[0].text == values[3] @assert_no_logs @pytest.mark.parametrize('style_type, values', ( ('decimal', ('1. ', '2. ', '3. ', '4. ')), ('"/"', ('/', '/', '/', '/')), )) def test_list_style_types(style_type, values): page, = render_pages('''
  1. abc
  2. abc
  3. abc
  4. abc
''' % style_type) html, = page.children body, = html.children ol, = body.children li_1, li_2, li_3, li_4 = ol.children assert li_1.children[0].children[0].children[0].text == values[0] assert li_2.children[0].children[0].children[0].text == values[1] assert li_3.children[0].children[0].children[0].text == values[2] assert li_4.children[0].children[0].children[0].text == values[3] def test_counter_set(): page, = render_pages('''

''') html, = page.children body, = html.children art_1, art_2, art_3, art_4, art_5, art_6 = body.children h1, = art_1.children assert h1.children[0].children[0].children[0].text == '1' h2, h3, = art_2.children assert h2.children[0].children[0].children[0].text == '3' assert h3.children[0].children[0].children[0].text == '4' h3, = art_3.children assert h3.children[0].children[0].children[0].text == '5' h2, = art_4.children assert h2.children[0].children[0].children[0].text == '3' h3_1, h3_2 = art_5.children assert h3_1.children[0].children[0].children[0].text == '4' assert h3_2.children[0].children[0].children[0].text == '5' h1, h2, h3 = art_6.children assert h1.children[0].children[0].children[0].text == '1' assert h2.children[0].children[0].children[0].text == '3' assert h3.children[0].children[0].children[0].text == '4' def test_counter_multiple_extends(): # Inspired by W3C failing test system-extends-invalid page, = render_pages('''
''') html, = page.children body, = html.children ol, = body.children li_1, li_2, li_3, li_4, li_5, li_6, li_7, li_8 = ol.children assert li_1.children[0].children[0].children[0].text == 'a1b' assert li_2.children[0].children[0].children[0].text == '2b' assert li_3.children[0].children[0].children[0].text == 'c3. ' assert li_4.children[0].children[0].children[0].text == 'd4. ' assert li_5.children[0].children[0].children[0].text == 'e5. ' assert li_6.children[0].children[0].children[0].text == '6. ' assert li_7.children[0].children[0].children[0].text == '7. ' assert li_8.children[0].children[0].children[0].text == '8. '