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 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
|
"""Test expanders for shorthand properties."""
import pytest
import tinycss2
from tinycss2.color3 import parse_color
from weasyprint.css import preprocess_declarations
from weasyprint.css.properties import INITIAL_VALUES, ZERO_PIXELS
from weasyprint.css.validation.expanders import EXPANDERS
from ..testing_utils import assert_no_logs, capture_logs
def expand_to_dict(css, expected_error=None):
"""Helper to test shorthand properties expander functions."""
declarations = tinycss2.parse_blocks_contents(css)
with capture_logs() as logs:
base_url = 'https://weasyprint.org/foo/'
declarations = list(preprocess_declarations(base_url, declarations))
if expected_error:
assert len(logs) == 1
assert expected_error in logs[0]
else:
assert not logs
return {
name: value for name, value, _ in declarations if value != 'initial'}
def assert_invalid(css, message='invalid'):
assert expand_to_dict(css, message) == {}
@assert_no_logs
@pytest.mark.parametrize('expander', EXPANDERS)
def test_empty_expander_value(expander):
assert_invalid(f'{expander}:', message='Ignored')
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('none', {'text_decoration_line': 'none'}),
('overline', {'text_decoration_line': {'overline'}}),
('overline blink line-through', {
'text_decoration_line': {'blink', 'line-through', 'overline'},
}),
('red', {'text_decoration_color': parse_color('red')}),
('inherit', {
f'text_decoration_{key}': 'inherit'
for key in ('color', 'line', 'style')}),
))
def test_text_decoration(rule, result):
assert expand_to_dict(f'text-decoration: {rule}') == result
@assert_no_logs
@pytest.mark.parametrize('rule', (
'solid solid',
'red red',
'1px',
'underline none',
'none none',
))
def test_text_decoration_invalid(rule):
assert_invalid(f'text-decoration: {rule}')
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('margin: inherit', {
'margin_top': 'inherit',
'margin_right': 'inherit',
'margin_bottom': 'inherit',
'margin_left': 'inherit',
}),
('margin: 1em', {
'margin_top': (1, 'em'),
'margin_right': (1, 'em'),
'margin_bottom': (1, 'em'),
'margin_left': (1, 'em'),
}),
('margin: -1em auto 20%', {
'margin_top': (-1, 'em'),
'margin_right': 'auto',
'margin_bottom': (20, '%'),
'margin_left': 'auto',
}),
('padding: 1em 0', {
'padding_top': (1, 'em'),
'padding_right': (0, None),
'padding_bottom': (1, 'em'),
'padding_left': (0, None),
}),
('padding: 1em 0 2%', {
'padding_top': (1, 'em'),
'padding_right': (0, None),
'padding_bottom': (2, '%'),
'padding_left': (0, None),
}),
('padding: 1em 0 2em 5px', {
'padding_top': (1, 'em'),
'padding_right': (0, None),
'padding_bottom': (2, 'em'),
'padding_left': (5, 'px'),
}),
))
def test_four_sides(rule, result):
assert expand_to_dict(rule) == result
@assert_no_logs
def test_four_sides_warning():
assert expand_to_dict(
'padding: 1 2 3 4 5', 'Expected 1 to 4 token components got 5') == {}
@assert_no_logs
@pytest.mark.parametrize('rule', (
'margin: rgb(0, 0, 0)',
'padding: auto',
'padding: -12px',
'border-width: -3em',
'border-width: 12%',
))
def test_four_sides_invalid(rule):
assert_invalid(rule)
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('border-top: 3px dotted red', {
'border_top_width': (3, 'px'),
'border_top_style': 'dotted',
'border_top_color': (1, 0, 0, 1), # red
}),
('border-top: 3px dotted', {
'border_top_width': (3, 'px'),
'border_top_style': 'dotted',
}),
('border-top: 3px red', {
'border_top_width': (3, 'px'),
'border_top_color': (1, 0, 0, 1), # red
}),
('border-top: solid', {'border_top_style': 'solid'}),
('border: 6px dashed lime', {
'border_top_width': (6, 'px'),
'border_top_style': 'dashed',
'border_top_color': (0, 1, 0, 1), # lime
'border_left_width': (6, 'px'),
'border_left_style': 'dashed',
'border_left_color': (0, 1, 0, 1), # lime
'border_bottom_width': (6, 'px'),
'border_bottom_style': 'dashed',
'border_bottom_color': (0, 1, 0, 1), # lime
'border_right_width': (6, 'px'),
'border_right_style': 'dashed',
'border_right_color': (0, 1, 0, 1), # lime
}),
))
def test_borders(rule, result):
assert expand_to_dict(rule) == result
@assert_no_logs
def test_borders_invalid():
assert_invalid('border: 6px dashed left')
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('list-style: inherit', {
'list_style_position': 'inherit',
'list_style_image': 'inherit',
'list_style_type': 'inherit',
}),
('list-style: url(../bar/lipsum.png)', {
'list_style_image': ('url', 'https://weasyprint.org/bar/lipsum.png'),
}),
('list-style: square', {
'list_style_type': 'square',
}),
('list-style: circle inside', {
'list_style_position': 'inside',
'list_style_type': 'circle',
}),
('list-style: none circle inside', {
'list_style_position': 'inside',
'list_style_image': ('none', None),
'list_style_type': 'circle',
}),
('list-style: none inside none', {
'list_style_position': 'inside',
'list_style_image': ('none', None),
'list_style_type': 'none',
}),
('list-style: inside special none', {
'list_style_position': 'inside',
'list_style_image': ('none', None),
'list_style_type': 'special',
}),
))
def test_list_style(rule, result):
assert expand_to_dict(rule) == result
@assert_no_logs
def test_list_style_warning():
assert_invalid(
'list-style: circle disc',
'got multiple type values in a list-style shorthand')
@assert_no_logs
@pytest.mark.parametrize('rule', (
'list-style: none inside none none',
'list-style: 1px',
))
def test_list_style_invalid(rule):
assert_invalid(rule)
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('red', {'background_color': (1, 0, 0, 1)}),
('url(lipsum.png)', {
'background_image': [
('url', 'https://weasyprint.org/foo/lipsum.png')]}),
('no-repeat', {
'background_repeat': [('no-repeat', 'no-repeat')]}),
('fixed', {
'background_attachment': ['fixed']}),
('repeat no-repeat fixed', {
'background_repeat': [('repeat', 'no-repeat')],
'background_attachment': ['fixed']}),
('inherit', {
'background_repeat': 'inherit',
'background_attachment': 'inherit',
'background_image': 'inherit',
'background_position': 'inherit',
'background_size': 'inherit',
'background_clip': 'inherit',
'background_origin': 'inherit',
'background_color': 'inherit'}),
('top', {
'background_position': [('left', (50, '%'), 'top', (0, '%'))]}),
('top right', {
'background_position': [('left', (100, '%'), 'top', (0, '%'))]}),
('top right 20px', {
'background_position': [('right', (20, 'px'), 'top', (0, '%'))]}),
('top 1% right 20px', {
'background_position': [('right', (20, 'px'), 'top', (1, '%'))]}),
('top no-repeat', {
'background_repeat': [('no-repeat', 'no-repeat')],
'background_position': [('left', (50, '%'), 'top', (0, '%'))]}),
('top right no-repeat', {
'background_repeat': [('no-repeat', 'no-repeat')],
'background_position': [('left', (100, '%'), 'top', (0, '%'))]}),
('top right 20px no-repeat', {
'background_repeat': [('no-repeat', 'no-repeat')],
'background_position': [('right', (20, 'px'), 'top', (0, '%'))]}),
('top 1% right 20px no-repeat', {
'background_repeat': [('no-repeat', 'no-repeat')],
'background_position': [('right', (20, 'px'), 'top', (1, '%'))]}),
('url(bar) #f00 repeat-y center left fixed', {
'background_color': (1, 0, 0, 1),
'background_image': [('url', 'https://weasyprint.org/foo/bar')],
'background_repeat': [('no-repeat', 'repeat')],
'background_attachment': ['fixed'],
'background_position': [('left', (0, '%'), 'top', (50, '%'))]}),
('#00f 10% 200px', {
'background_color': (0, 0, 1, 1),
'background_position': [('left', (10, '%'), 'top', (200, 'px'))]}),
('right 78px fixed', {
'background_attachment': ['fixed'],
'background_position': [('left', (100, '%'), 'top', (78, 'px'))]}),
('center / cover red', {
'background_size': ['cover'],
'background_position': [('left', (50, '%'), 'top', (50, '%'))],
'background_color': (1, 0, 0, 1)}),
('center / auto red', {
'background_size': [('auto', 'auto')],
'background_position': [('left', (50, '%'), 'top', (50, '%'))],
'background_color': (1, 0, 0, 1)}),
('center / 42px', {
'background_size': [((42, 'px'), 'auto')],
'background_position': [('left', (50, '%'), 'top', (50, '%'))]}),
('center / 7% 4em', {
'background_size': [((7, '%'), (4, 'em'))],
'background_position': [('left', (50, '%'), 'top', (50, '%'))]}),
('red content-box', {
'background_color': (1, 0, 0, 1),
'background_origin': ['content-box'],
'background_clip': ['content-box']}),
('red border-box content-box', {
'background_color': (1, 0, 0, 1),
'background_origin': ['border-box'],
'background_clip': ['content-box']}),
('border-box red', {
'background_color': (1, 0, 0, 1),
'background_origin': ['border-box']}),
('url(bar) center, no-repeat', {
'background_color': (0, 0, 0, 0),
'background_image': [
('url', 'https://weasyprint.org/foo/bar'), ('none', None)],
'background_position': [
('left', (50, '%'), 'top', (50, '%')),
('left', (0, '%'), 'top', (0, '%'))],
'background_repeat': [
('repeat', 'repeat'), ('no-repeat', 'no-repeat')]}),
))
def test_background(rule, result):
expanded = expand_to_dict(f'background: {rule}')
assert expanded.pop('background_color') == result.pop(
'background_color', INITIAL_VALUES['background_color'])
nb_layers = len(expanded['background_image'])
for name, value in result.items():
assert expanded.pop(name) == value
for name, value in expanded.items():
assert tuple(value) == INITIAL_VALUES[name] * nb_layers
@assert_no_logs
@pytest.mark.parametrize('rule', (
'red, url(foo)',
'10px lipsum',
'content-box red content-box',
))
def test_background_invalid(rule):
assert_invalid(f'background: {rule}')
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('1px', {
'border_top_left_radius': ((1, 'px'), (1, 'px')),
'border_top_right_radius': ((1, 'px'), (1, 'px')),
'border_bottom_right_radius': ((1, 'px'), (1, 'px')),
'border_bottom_left_radius': ((1, 'px'), (1, 'px')),
}),
('1px 2em', {
'border_top_left_radius': ((1, 'px'), (1, 'px')),
'border_top_right_radius': ((2, 'em'), (2, 'em')),
'border_bottom_right_radius': ((1, 'px'), (1, 'px')),
'border_bottom_left_radius': ((2, 'em'), (2, 'em')),
}),
('1px / 2em', {
'border_top_left_radius': ((1, 'px'), (2, 'em')),
'border_top_right_radius': ((1, 'px'), (2, 'em')),
'border_bottom_right_radius': ((1, 'px'), (2, 'em')),
'border_bottom_left_radius': ((1, 'px'), (2, 'em')),
}),
('1px 3px / 2em 4%', {
'border_top_left_radius': ((1, 'px'), (2, 'em')),
'border_top_right_radius': ((3, 'px'), (4, '%')),
'border_bottom_right_radius': ((1, 'px'), (2, 'em')),
'border_bottom_left_radius': ((3, 'px'), (4, '%')),
}),
('1px 2em 3%', {
'border_top_left_radius': ((1, 'px'), (1, 'px')),
'border_top_right_radius': ((2, 'em'), (2, 'em')),
'border_bottom_right_radius': ((3, '%'), (3, '%')),
'border_bottom_left_radius': ((2, 'em'), (2, 'em')),
}),
('1px 2em 3% 4rem', {
'border_top_left_radius': ((1, 'px'), (1, 'px')),
'border_top_right_radius': ((2, 'em'), (2, 'em')),
'border_bottom_right_radius': ((3, '%'), (3, '%')),
'border_bottom_left_radius': ((4, 'rem'), (4, 'rem')),
}),
('inherit', {
'border_top_left_radius': 'inherit',
'border_top_right_radius': 'inherit',
'border_bottom_right_radius': 'inherit',
'border_bottom_left_radius': 'inherit',
}),
))
def test_border_radius(rule, result):
assert expand_to_dict(f'border-radius: {rule}') == result
@assert_no_logs
@pytest.mark.parametrize('rule, message', (
('1px 1px 1px 1px 1px', '1 to 4 token'),
('1px 1px 1px 1px 1px / 1px', '1 to 4 token'),
('1px / 1px / 1px', 'only one "/"'),
('12deg', 'invalid'),
('1px 1px 1px 12deg', 'invalid'),
('super', 'invalid'),
('1px, 1px', 'invalid'),
('1px /', 'value after "/"'),
))
def test_border_radius_invalid(rule, message):
assert_invalid(f'border-radius: {rule}', message)
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('url(border.png) 27', {
'border_image_source': ('url', 'https://weasyprint.org/foo/border.png'),
'border_image_slice': ((27, None),),
}),
('url(border.png) 10 / 4 / 2 round stretch', {
'border_image_source': ('url', 'https://weasyprint.org/foo/border.png'),
'border_image_slice': ((10, None),),
'border_image_width': ((4, None),),
'border_image_outset': ((2, None),),
'border_image_repeat': (('round', 'stretch')),
}),
('10 // 2', {
'border_image_slice': ((10, None),),
'border_image_outset': ((2, None),),
}),
('5.5%', {
'border_image_slice': ((5.5, '%'),),
}),
('stretch 2 url("border.png")', {
'border_image_source': ('url', 'https://weasyprint.org/foo/border.png'),
'border_image_slice': ((2, None),),
'border_image_repeat': (('stretch',)),
}),
('1/2 round', {
'border_image_slice': ((1, None),),
'border_image_width': ((2, None),),
'border_image_repeat': (('round',)),
}),
('none', {
'border_image_source': ('none', None),
}),
))
def test_border_image(rule, result):
assert expand_to_dict(f'border-image: {rule}') == result
@assert_no_logs
@pytest.mark.parametrize('rule, reason', (
('url(border.png) url(border.png)', 'multiple source'),
('10 10 10 10 10', 'multiple slice'),
('1 / 2 / 3 / 4', 'invalid'),
('/1', 'invalid'),
('/1', 'invalid'),
('round round round', 'invalid'),
('-1', 'invalid'),
('1 repeat 2', 'multiple slice'),
('1% // 1%', 'invalid'),
('1 / repeat', 'invalid'),
('', 'no value'),
))
def test_border_image_invalid(rule, reason):
assert_invalid(f'border-image: {rule}', reason)
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('12px My Fancy Font, serif', {
'font_size': (12, 'px'),
'font_family': ('My Fancy Font', 'serif'),
}),
('small/1.2 "Some Font", serif', {
'font_size': 'small',
'line_height': (1.2, None),
'font_family': ('Some Font', 'serif'),
}),
('small-caps italic 700 large serif', {
'font_style': 'italic',
'font_variant_caps': 'small-caps',
'font_weight': 700,
'font_size': 'large',
'font_family': ('serif',),
}),
('small-caps condensed normal 700 large serif', {
'font_stretch': 'condensed',
'font_variant_caps': 'small-caps',
'font_weight': 700,
'font_size': 'large',
'font_family': ('serif',),
}),
))
def test_font(rule, result):
assert expand_to_dict(f'font: {rule}') == result
@assert_no_logs
@pytest.mark.parametrize('rule, message', (
('menu', 'System fonts are not supported'),
('12deg My Fancy Font, serif', 'invalid'),
('12px', 'invalid'),
('12px/foo serif', 'invalid'),
('12px "Invalid" family', 'invalid'),
('normal normal normal normal normal large serif', 'invalid'),
('normal small-caps italic 700 condensed large serif', 'invalid'),
('small-caps italic 700 normal condensed large serif', 'invalid'),
('small-caps italic 700 condensed normal large serif', 'invalid'),
('normal normal normal normal', 'invalid'),
('normal normal normal italic', 'invalid'),
('caption', 'System fonts'),
))
def test_font_invalid(rule, message):
assert_invalid(f'font: {rule}', message)
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('normal', {
'font_variant_alternates': 'normal',
'font_variant_caps': 'normal',
'font_variant_east_asian': 'normal',
'font_variant_ligatures': 'normal',
'font_variant_numeric': 'normal',
'font_variant_position': 'normal',
}),
('none', {
'font_variant_alternates': 'normal',
'font_variant_caps': 'normal',
'font_variant_east_asian': 'normal',
'font_variant_ligatures': 'none',
'font_variant_numeric': 'normal',
'font_variant_position': 'normal',
}),
('historical-forms petite-caps', {
'font_variant_alternates': 'historical-forms',
'font_variant_caps': 'petite-caps',
}),
('lining-nums contextual small-caps common-ligatures', {
'font_variant_ligatures': ('contextual', 'common-ligatures'),
'font_variant_numeric': ('lining-nums',),
'font_variant_caps': 'small-caps',
}),
('jis78 ruby proportional-width', {
'font_variant_east_asian': ('jis78', 'ruby', 'proportional-width'),
}),
# CSS2-style font-variant
('small-caps', {'font_variant_caps': 'small-caps'}),
))
def test_font_variant(rule, result):
assert expand_to_dict(f'font-variant: {rule}') == result
@assert_no_logs
@pytest.mark.parametrize('rule', (
'normal normal',
'2',
'""',
'extra',
'jis78 jis04',
'full-width lining-nums ordinal normal',
'diagonal-fractions stacked-fractions',
'common-ligatures contextual no-common-ligatures',
'sub super',
'slashed-zero slashed-zero',
))
def test_font_variant_invalid(rule):
assert_invalid(f'font-variant: {rule}')
@assert_no_logs
def test_word_wrap():
assert expand_to_dict('word-wrap: normal') == {
'overflow_wrap': 'normal'}
assert expand_to_dict('word-wrap: break-word') == {
'overflow_wrap': 'break-word'}
assert expand_to_dict('word-wrap: inherit') == {
'overflow_wrap': 'inherit'}
assert_invalid('word-wrap: none')
assert_invalid('word-wrap: normal, break-word')
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('auto', {'flex_grow': 1, 'flex_shrink': 1, 'flex_basis': 'auto'}),
('none', {'flex_grow': 0, 'flex_shrink': 0, 'flex_basis': 'auto'}),
('10', {'flex_grow': 10, 'flex_shrink': 1, 'flex_basis': ZERO_PIXELS}),
('2 2', {'flex_grow': 2, 'flex_shrink': 2, 'flex_basis': ZERO_PIXELS}),
('2 2 1px', {'flex_grow': 2, 'flex_shrink': 2, 'flex_basis': (1, 'px')}),
('2 2 auto', {'flex_grow': 2, 'flex_shrink': 2, 'flex_basis': 'auto'}),
('2 auto', {'flex_grow': 2, 'flex_shrink': 1, 'flex_basis': 'auto'}),
('0 auto', {'flex_grow': 0, 'flex_shrink': 1, 'flex_basis': 'auto'}),
('inherit', {
'flex_grow': 'inherit',
'flex_shrink': 'inherit',
'flex_basis': 'inherit'}),
))
def test_flex(rule, result):
assert expand_to_dict(f'flex: {rule}') == result
@assert_no_logs
@pytest.mark.parametrize('rule', (
'auto 0 0 0',
'1px 2px',
'auto auto',
'auto 1 auto',
))
def test_flex_invalid(rule):
assert_invalid(f'flex: {rule}')
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('column', {'flex_direction': 'column'}),
('wrap', {'flex_wrap': 'wrap'}),
('wrap column', {'flex_direction': 'column', 'flex_wrap': 'wrap'}),
('row wrap', {'flex_direction': 'row', 'flex_wrap': 'wrap'}),
('inherit', {'flex_direction': 'inherit', 'flex_wrap': 'inherit'}),
))
def test_flex_flow(rule, result):
assert expand_to_dict(f'flex-flow: {rule}') == result
@assert_no_logs
@pytest.mark.parametrize('rule', (
'1px',
'wrap 1px',
'row row',
'wrap nowrap',
'column wrap nowrap row',
))
def test_flex_flow_invalid(rule):
assert_invalid(f'flex-flow: {rule}')
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('auto', {'start': 'auto', 'end': 'auto'}),
('auto / auto', {'start': 'auto', 'end': 'auto'}),
('4', {'start': (None, 4, None), 'end': 'auto'}),
('c', {'start': (None, None, 'c'), 'end': (None, None, 'c')}),
('4 / -4', {'start': (None, 4, None), 'end': (None, -4, None)}),
('c / d', {'start': (None, None, 'c'), 'end': (None, None, 'd')}),
('ab / cd 4', {'start': (None, None, 'ab'), 'end': (None, 4, 'cd')}),
('ab 2 span', {'start': ('span', 2, 'ab'), 'end': 'auto'}),
))
def test_grid_column_row(rule, result):
assert expand_to_dict(f'grid-column: {rule}') == dict(
(f'grid_column_{key}', value) for key, value in result.items())
assert expand_to_dict(f'grid-row: {rule}') == dict(
(f'grid_row_{key}', value) for key, value in result.items())
@assert_no_logs
@pytest.mark.parametrize('rule', (
'auto auto',
'4 / 2 / c',
'span',
'4 / span',
'c /',
'/4',
'col / 2.1',
))
def test_grid_column_row_invalid(rule):
assert_invalid(f'grid-column: {rule}')
assert_invalid(f'grid-row: {rule}')
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('auto', {
'row_start': 'auto', 'row_end': 'auto',
'column_start': 'auto', 'column_end': 'auto'}),
('auto / auto', {
'row_start': 'auto', 'row_end': 'auto',
'column_start': 'auto', 'column_end': 'auto'}),
('auto / auto / auto', {
'row_start': 'auto', 'row_end': 'auto',
'column_start': 'auto', 'column_end': 'auto'}),
('auto / auto / auto / auto', {
'row_start': 'auto', 'row_end': 'auto',
'column_start': 'auto', 'column_end': 'auto'}),
('1/c/2 d/span 2 ab', {
'row_start': (None, 1, None), 'column_start': (None, None, 'c'),
'row_end': (None, 2, 'd'), 'column_end': ('span', 2, 'ab')}),
('1 / c', {
'row_start': (None, 1, None), 'column_start': (None, None, 'c'),
'row_end': 'auto', 'column_end': (None, None, 'c')}),
('a / c 2', {
'row_start': (None, None, 'a'), 'column_start': (None, 2, 'c'),
'row_end': (None, None, 'a'), 'column_end': 'auto'}),
('a', {
'row_start': (None, None, 'a'), 'row_end': (None, None, 'a'),
'column_start': (None, None, 'a'), 'column_end': (None, None, 'a')}),
('span 2', {
'row_start': ('span', 2, None), 'row_end': 'auto',
'column_start': 'auto', 'column_end': 'auto'}),
))
def test_grid_area(rule, result):
assert expand_to_dict(f'grid-area: {rule}') == dict(
(f'grid_{key}', value) for key, value in result.items())
@assert_no_logs
@pytest.mark.parametrize('rule', (
'auto auto',
'auto / auto auto',
'4 / 2 / c / d / e',
'span',
'4 / span',
'c /',
'/4',
'c//4',
'/',
'1 / 2 / 4 / 0.5',
))
def test_grid_area_invalid(rule):
assert_invalid(f'grid-area: {rule}')
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('none', {
'rows': 'none', 'columns': 'none', 'areas': 'none',
}),
('subgrid / [outer-edge] 20px [main-start]', {
'rows': ('subgrid', ()),
'columns': (('outer-edge',), (20, 'px'), ('main-start',)),
'areas': 'none',
}),
('repeat(2, [e] 40px) repeat(5, auto) / subgrid [a] repeat(auto-fill, [b])', {
'rows': (
(), ('repeat()', 2, (('e',), (40, 'px'), ())), (),
('repeat()', 5, ((), 'auto', ())), ()),
'columns': ('subgrid', (('a',), ('repeat()', 'auto-fill', (('b',),)))),
'areas': 'none',
}),
# TODO: support last syntax
# ('[a b] "x y y" [c] [d] "x y y" 1fr [e] / auto 2fr auto', {
# 'rows': 'none', 'columns': 'none', 'areas': 'none',
# }),
# ('[a b c] "x x x" 2fr', {
# 'rows': 'none', 'columns': 'none', 'areas': 'none',
# }),
))
def test_grid_template(rule, result):
assert expand_to_dict(f'grid-template: {rule}') == dict(
(f'grid_template_{key}', value) for key, value in result.items())
@assert_no_logs
@pytest.mark.parametrize('rule', (
'none none',
'auto',
'subgrid / subgrid / subgrid',
'[a] 1px [b] / none /',
'[a] 1px [b] // none',
'[a] 1px [b] none',
))
def test_grid_template_invalid(rule):
assert_invalid(f'grid-template: {rule}')
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('none', {
'template_rows': 'none', 'template_columns': 'none',
'template_areas': 'none',
'auto_rows': ('auto',), 'auto_columns': ('auto',),
'auto_flow': ('row',),
}),
('subgrid / [outer-edge] 20px [main-start]', {
'template_rows': ('subgrid', ()),
'template_columns': (('outer-edge',), (20, 'px'), ('main-start',)),
'template_areas': 'none',
'auto_rows': ('auto',), 'auto_columns': ('auto',),
'auto_flow': ('row',),
}),
('repeat(2, [e] 40px) repeat(5, auto) / subgrid [a] repeat(auto-fill, [b])', {
'template_rows': (
(), ('repeat()', 2, (('e',), (40, 'px'), ())), (),
('repeat()', 5, ((), 'auto', ())), ()),
'template_columns': ('subgrid', (('a',), ('repeat()', 'auto-fill', (('b',),)))),
'template_areas': 'none',
'auto_rows': ('auto',), 'auto_columns': ('auto',),
'auto_flow': ('row',),
}),
('auto-flow 1fr / 100px', {
'template_rows': 'none', 'template_columns': ((), (100, 'px'), ()),
'template_areas': 'none',
'auto_rows': ((1, 'fr'),), 'auto_columns': ('auto',),
'auto_flow': ('row',),
}),
('none / dense auto-flow 1fr', {
'template_rows': 'none', 'template_columns': 'none',
'template_areas': 'none',
'auto_rows': ('auto',), 'auto_columns': ((1, 'fr'),),
'auto_flow': ('column', 'dense'),
}),
# TODO: support last grid-template syntax
# ('[a b] "x y y" [c] [d] "x y y" 1fr [e] / auto 2fr auto', {
# }),
# ('[a b c] "x x x" 2fr', {
# }),
))
def test_grid(rule, result):
assert expand_to_dict(f'grid: {rule}') == dict(
(f'grid_{key}', value) for key, value in result.items())
@assert_no_logs
@pytest.mark.parametrize('rule', (
'none none',
'auto',
'subgrid / subgrid / subgrid',
'[a] 1px [b] / none /',
'[a] 1px [b] // none',
'[a] 1px [b] none',
'none / auto-flow 1fr dense',
'none / dense 1fr auto-flow',
'100px auto-flow / none',
'dense 100px / auto-flow 1fr'
))
def test_grid_invalid(rule):
assert_invalid(f'grid: {rule}')
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('page-break-after: left', {'break_after': 'left'}),
('page-break-before: always', {'break_before': 'page'}),
('page-break-after: inherit', {'break_after': 'inherit'}),
('page-break-before: inherit', {'break_before': 'inherit'}),
))
def test_page_break(rule, result):
assert expand_to_dict(rule) == result
@assert_no_logs
@pytest.mark.parametrize('rule', (
'page-break-after: top',
'page-break-before: 1px',
))
def test_page_break_invalid(rule):
assert_invalid(rule)
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('avoid', {'break_inside': 'avoid'}),
('inherit', {'break_inside': 'inherit'}),
))
def test_page_break_inside(rule, result):
assert expand_to_dict(f'page-break-inside: {rule}') == result
@assert_no_logs
@pytest.mark.parametrize('rule', (
'top',
))
def test_page_break_inside_invalid(rule):
assert_invalid(f'page-break-inside: {rule}')
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('1em', {'column_width': (1, 'em'), 'column_count': 'auto'}),
('auto', {'column_width': 'auto', 'column_count': 'auto'}),
('auto auto', {'column_width': 'auto', 'column_count': 'auto'}),
))
def test_columns(rule, result):
assert expand_to_dict(f'columns: {rule}') == result
@assert_no_logs
@pytest.mark.parametrize('rule, reason', (
('1px 2px', 'invalid'),
('auto auto auto', 'multiple'),
))
def test_columns_invalid(rule, reason):
assert_invalid(f'columns: {rule}', reason)
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('none', {
'max_lines': 'none', 'continue': 'auto', 'block_ellipsis': 'none'}),
('2', {
'max_lines': 2, 'continue': 'discard', 'block_ellipsis': 'auto'}),
('3 "…"', {
'max_lines': 3, 'continue': 'discard',
'block_ellipsis': ('string', '…')}),
('inherit', {
'max_lines': 'inherit', 'continue': 'inherit',
'block_ellipsis': 'inherit'}),
))
def test_line_clamp(rule, result):
assert expand_to_dict(f'line-clamp: {rule}') == result
@assert_no_logs
@pytest.mark.parametrize('rule, reason', (
('none none none', 'invalid'),
('1px', 'invalid'),
('0 "…"', 'invalid'),
('1px 2px', 'invalid'),
))
def test_line_clamp_invalid(rule, reason):
assert_invalid(f'line-clamp: {rule}', reason)
@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('start', {'text_align_all': 'start', 'text_align_last': 'start'}),
('right', {'text_align_all': 'right', 'text_align_last': 'right'}),
('justify', {'text_align_all': 'justify', 'text_align_last': 'start'}),
('justify-all', {
'text_align_all': 'justify', 'text_align_last': 'justify'}),
('inherit', {'text_align_all': 'inherit', 'text_align_last': 'inherit'}),
))
def test_text_align(rule, result):
assert expand_to_dict(f'text-align: {rule}') == result
@assert_no_logs
@pytest.mark.parametrize('rule, reason', (
('none', 'invalid'),
('start end', 'invalid'),
('1', 'invalid'),
('left left', 'invalid'),
('top', 'invalid'),
('"right"', 'invalid'),
('1px', 'invalid'),
))
def test_text_align_invalid(rule, reason):
assert_invalid(f'text-align: {rule}', reason)
|