Source

strainer / tests / test_xhtmlify.py

Full commit
   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
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
import re
import encodings.aliases
import codecs
import sys
PY3 = True if sys.version_info[:1] == (3,) else False
from strainer.xhtmlify import xhtmlify as _xhtmlify, xmlparse, ValidationError
from strainer.xhtmlify import sniff_encoding, fix_xmldecl
from strainer.doctypes import DOCTYPE_XHTML1_STRICT
import six


def xhtmlify(html, *args, **kwargs):
    """Call the real xhtmlify and check it outputs well-formed XML
       and that it is idempotent (makes no changes when fed its output)."""
    _wrap = None
    if '_wrap' in kwargs:
        _wrap = kwargs['_wrap']
        del kwargs['_wrap']
    xhtml = _xhtmlify(html, *args, **kwargs)
    try:
        # ET can't handle <!...>
        stripped_xhtml = re.sub(r'(?s)<!(?!\[).*?>', '', xhtml)
        xmlparse(stripped_xhtml, wrap=_wrap)
    except Exception as e:
        assert False, (stripped_xhtml, str(e))
    assert xhtml == _xhtmlify(xhtml, *args, **kwargs), xhtml
    return xhtml

def test_simple1():
    s = '<p><b><i>test'
    e = '<p><b><i>test</i></b></p>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_simple2():
    s = '<p>'
    e = '<p></p>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_simple_comment():
    s = '<!-- test -->'
    e = '<!-- test -->'  # TODO: what to do about < and > in comments?
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_whitespace_in_tag():
    s = ('''<meta\thttp-equiv\f\f= 'content-type'\nid=x '''
         '''content =\ntext/html;charset=utf-8>''')
    e = ('''<meta\thttp-equiv  = 'content-type'\nid="x" '''
         '''content =\n"text/html;charset=utf-8" />''')
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, repr(r)

def test_touching_attrs():
    s = '''<p id="a"class=b><br id=d/></p>'''
    e = '''<p id="a" class="b"><br id="d/" /></p>'''
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, repr(r)

def test_bad_attr():
    s = '''<a b='1'c'=2/>'''
    e_exc = "Malformed tag contents at line 1, column 10 (char 10)"
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert str(exc)==e_exc, exc
    else:
        assert False, r

def test_bad_attr2():
    s = '''<a\tb="b>c'\t>'''
    e = '''<a\tb="&quot;b">c'\t&gt;</a>'''
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, repr(r)

def test_mini_p():
    s = '<p/>'
    e = '<p/>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, repr(r)

def test_dont_allow_nesting_ps():
    # Disallow nesting <p> tags since that's what HTML 4 says
    # and it simplifies our other logic for when to insert </p>.
    s = '<p><p></p></p>'
    e_exc = "Unexpected closing tag </p> at line 1, column 11 (char 11)"
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert str(exc)==e_exc, exc
    else:
        assert False, r

def test_lowercases_attrs():
    s = '<p ID=foo clAss=\'bar\'><input valUe="TesT">'
    e = '<p id="foo" class=\'bar\'><input value="TesT" /></p>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_quoted_dquote_attr_is_unchanged():
    s = r'''<img alt='"' /><img alt='&quot;' />'''
    e = r'''<img alt='"' /><img alt='&quot;' />'''
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_quoted_squote_attr_is_unchanged():
    s = r'''<img alt="'" /><img alt='&apos;' />'''
    e = r'''<img alt="'" /><img alt='&apos;' />'''
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_single_dquote_attr():
    s = r'''<img alt=" />'''
    e = r'''<img alt="&quot;" />'''
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_single_squote_attr():
    s = r'''<img alt='>'''
    e = r'''<img alt="'" />'''
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_dquoted_lt_attr_is_replaced():
    s = r'''<img alt="<"/>'''
    e = r'''<img alt="&lt;"/>'''
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_squoted_lt_attr_is_replaced():
    s = r'''<img alt='<'/>'''
    e = r'''<img alt='&lt;'/>'''
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_self_closing():
    s = '<br><input value="test"><input id="x" value="test"  >'
    e = '<br /><input value="test" /><input id="x" value="test"  />'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_insert_end_p_before_p():
    s = '<p><p></p>'
    e = '<p></p><p></p>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_dont_insert_end_p_before_div():
    s = '<p><div></div>'
    e = '<p><div></div></p>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_insert_end_p_before_end_h1():
    s = '<h1><p></h1>'
    e = '<h1><p></p></h1>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_insert_end_li_before_end_ul():
    s = '<ul><li></ul>'
    e = '<ul><li></li></ul>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_insert_end_li_before_end_ol():
    s = '<ol><li></ol>'
    e = '<ol><li></li></ol>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_insert_end_td_before_end_tr():
    s = '<tr><td></tr>'
    e = '<tr><td></td></tr>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_insert_end_th_before_end_tr():
    s = '<tr><th></tr>'
    e = '<tr><th></th></tr>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_ampersand():
    s = '<p title="&">&</p><p>&amp;</p>'
    e = '<p title="&amp;">&amp;</p><p>&amp;</p>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_less_than():
    s = '<'
    e_exc = 'Unescaped "<" or unfinished tag at line 1, column 1 (char 1)'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert str(exc)==e_exc, exc
    else:
        assert False, r

def test_less_than2():
    s = '<p><</p>'
    e_exc = 'Unescaped "<" or unfinished tag at line 1, column 4 (char 4)'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert str(exc)==e_exc, exc
    else:
        assert False, r

def test_greater_than():
    s = '>'
    e = '&gt;'  # '>' would strictly be ok too...
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_apos():
    s = "<p>'</p><p>&apos;</p>"
    e = "<p>'</p><p>&apos;</p>"
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_quot():
    s = '<p>"</p><p>&quot;</p>'
    e = '<p>"</p><p>&quot;</p>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_euro():
    s = '<p>&euro;</p>'
    e = '<p>&#x20ac;</p>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_unknown_entity():
    s = '<p>&nosuch;</p>'
    e = '<p>&amp;nosuch;</p>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_cdata_end_marker():
    s = ']]>'
    e = ']]&gt;'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_end_tag_in_cdata():
    s = '<p><![CDATA[</p>]]></p>'
    e = '<p><![CDATA[</p>]]></p>'
    # '<p>&lt;/p></p>' would be a better XHTML/HTML polyglot, but
    # would break our rule of only making changes where necessary.
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_unclosed_cdata():
    s = '<p><![CDATA[</p>'
    e_exc = 'Unescaped "<" or unfinished tag at line 1, column 4 (char 4)'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert str(exc)==e_exc, exc
    else:
        assert False, r

# <script> and <style> tags are a complete nightmare.  Our output has to
# parse sensibly in both HTML and XHTML parsers, which is far from easy.
# Our approach is to only escape '<', '>' and '&', and to do it in different
# ways depending on the JavaScript/CSS syntactic context.  Escaping > is
# needed to prevent validity-breaking occurrences of "]]>".
#
# Possible JavaScript/CSS syntactic contexts we need to handle:
#
#   * a string, e.g. "<" or '&'
#   * a comment, e.g. /* < */  or  // &
#   * single tokens: 1 < 2  or  a &b;  (mustn't confuse this with an entity)
#   * a CSS URL
#   * invalid, e.g. <&]]>
#
# We can simplify this to three cases:
#
#   a) inside a string (with either kind of delimiter),
#   b) inside a block comment, or
#   c) other.
#
# We escape <, > and & in the three cases as:
#   a) \x3c, \x3e and \x26  (also replaces \<, \> and \>)
#   b) <![CDATA[<]]>, <![CDATA[>]]> and <![CDATA[&]]>
#   c) /*<![CDATA[<]]>*/, /*<![CDATA[>]]>*/ and /*<![CDATA[&]]>*/
#
# We must not change any text already inside <![CDATA[...]]>.
def test_script_simple():
    s = '<script>/* test */</script>'
    e = '<script>/* test */</script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_lt():
    s = '<script> 1 < 2 </script>'
    e = '<script> 1 /*<![CDATA[*/ < /*]]>*/ 2 </script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_gt():
    s = '<script> 1 > 2 </script>'
    e = '<script> 1 /*<![CDATA[*/ > /*]]>*/ 2 </script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_amp():
    s = '<script> 1 & 2 </script>'
    e = '<script> 1 /*<![CDATA[*/ & /*]]>*/ 2 </script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_amp_entity():
    s = '<script>var amp=2; amp += 3 &amp;</script>'
    e = '<script>var amp=2; amp += 3 /*<![CDATA[*/ & /*]]>*/amp;</script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_lt_in_block_comment():
    s = '<script>/* < */</script>'
    e = '<script>/* <![CDATA[<]]> */</script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_gt_in_block_comment():
    s = '<script>/* > */</script>'
    e = '<script>/* <![CDATA[>]]> */</script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_amp_in_block_comment():
    s = '<script>/* & */</script>'
    e = '<script>/* <![CDATA[&]]> */</script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_lt_in_line_comment():
    s = '<script>// < </script>'
    e = '<script>// /*<![CDATA[*/ < /*]]>*/ </script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_gt_in_line_comment():
    s = '<script>// > </script>'
    e = '<script>// /*<![CDATA[*/ > /*]]>*/ </script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_amp_in_line_comment():
    s = '<script>// & </script>'
    e = '<script>// /*<![CDATA[*/ & /*]]>*/ </script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_end_marker_in_block_comment():
    s = '<script>/* <![CDATA[x]]> ]]> */</script>'
    e = '<script>/* <![CDATA[x]]> ]]<![CDATA[>]]> */</script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_end_marker_in_line_comment():
    s = '<script>// <![CDATA[x]]> ]]> </script>'
    e = '<script>// <![CDATA[x]]> ]]/*<![CDATA[*/ > /*]]>*/ </script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_lt_in_dquote_string():
    s = r'<script> " \"< " </script>'
    e = r'<script> " \"\x%02x " </script>' % ord('<')
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_gt_in_dquote_string():
    s = r'<script> " \"> " </script>'
    e = r'<script> " \"\x%02x " </script>' % ord('>')
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_amp_in_dquote_string():
    s = r'<script> " \"& " </script>'
    e = r'<script> " \"\x%02x " </script>' % ord('&')
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_esc_lt_in_dquote_string():
    s = r'<script> " \< " </script>'
    e = r'<script> " \x%02x " </script>' % ord('<')
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_esc_gt_in_dquote_string():
    s = r'<script> " \> " </script>'
    e = r'<script> " \x%02x " </script>' % ord('>')
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_lt_in_squote_string():
    s = r"<script> ' \'< ' </script>"
    e = r"<script> ' \'\x%02x ' </script>" % ord('<')
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_gt_in_squote_string():
    s = r"<script> ' \'> ' </script>"
    e = r"<script> ' \'\x%02x ' </script>" % ord('>')
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_amp_in_squote_string():
    s = r"<script> ' \'& ' </script>"
    e = r"<script> ' \'\x%02x ' </script>" % ord('&')
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_ends_in_squote_string():
    s = r"<script> <![CDATA['x ]]>& ' </script>"
    e = r"<script> <![CDATA['x ]]>\x%02x ' </script>" % ord('&')
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_esc_lt_in_squote_string():
    s = r"<script> ' \< ' </script>"
    e = r"<script> ' \x%02x ' </script>" % ord('<')
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_esc_gt_in_squote_string():
    s = r"<script> ' \> ' </script>"
    e = r"<script> ' \x%02x ' </script>" % ord('>')
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_end_script_end_cdata_end_script():
    s = r"<script><![CDATA[</script>]]></script>"
    e = r"<script><![CDATA[</script>]]></script>"
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_unexpected_cdata_end_in_dquote_string():
    s = r'<script>"<]]>"</script>'
    e = r'<script>"\x%02x]]\x%02x"</script>' % (ord('<'), ord('>'))
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_unexpected_tag_gets_escaped():
    s = r'<script><evil></script>'
    e = r'<script>/*<![CDATA[*/ < /*]]>*/evil/*<![CDATA[*/ > /*]]>*/</script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_unexpected_tag_in_dqstring():
    s = r'<script>document.write("<b>");</script>'
    e = r'<script>document.write("\x3cb\x3e");</script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_unexpected_tag_in_sqstring():
    s = r"<script>document.write('<b>');</script>"
    e = r"<script>document.write('\x3cb\x3e');</script>"
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_unexpected_tag_in_block_comment():
    s = r"<script>/* <evil> */</script>"
    e = r"<script>/* <![CDATA[<]]>evil<![CDATA[>]]> */</script>"
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_script_cdata_unexpected_eof_escapes_contained_tags():
    s = r'<script><evil>'
    e = r'<script>/*<![CDATA[*/ < /*]]>*/evil/*<![CDATA[*/ > /*]]>*/</script>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_space_before_tag_name():
    s = r"< p>"
    e_exc = 'Malformed tag at line 1, column 1 (char 1)'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert str(exc)==e_exc, exc
    else:
        assert False, r

def test_pi_unsupported():
    # If adding support, see http://bugs.python.org/issue2746
    # It would be easy to accidentally introduce an XSS vector.
    s = r"<p><?php 1 & 2 ?></p>"
    e_exc = 'Malformed tag at line 1, column 4 (char 4)'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert str(exc)==e_exc, exc
    else:
        assert False, r

def test_doctype_is_uppercased():
    s = r'<!doctype html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
    e = r'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_bad_character_reference():
    s = r'<p>&#7;</p>'
    e = r'<p>&amp;#7;</p>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_repeated_attribute():
    s = r"<p><div style='margin-top: 0px' id=x style='margin-left: 0px' />"
    e_exc = 'Repeated attribute "style" at line 1, column 38 (char 38)'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert str(exc)==e_exc, exc
    else:
        assert False, r

def test_attribute_minimization():
    s = r'<option selected>'
    e = r'<option selected="selected"></option>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_junk_in_final_attribute():
    # Including the endslash is correct, we try to treat our input as HTML.
    s = r'<p x=:;"/><img>'
    e = r'<p x=":;&quot;/"><img /></p>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_one_colon_in_name():
    s = r'<py:test xmlns:py="..."/>'
    e = r'<py:test xmlns:py="..."/>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_two_colons_in_name():
    s = r'<a:b:c/>'
    e_exc = r'Malformed tag at line 1, column 1 (char 1)'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert str(exc)==e_exc, exc
    else:
        assert False, r

def test_closing_tags_at_end():
    s = r'this<p>is<a href="..">a test'
    e = r'this<p>is<a href="..">a test</a></p>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_html_gets_xmlns_attribute():
    s = r'<html><body>Test'
    e = r'<html xmlns="http://www.w3.org/1999/xhtml"><body>Test</body></html>'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert False, exc
    else:
        assert r==e, r

def test_innards_re_not_exponential():
    s = '<abcdefh.ijklmnopqrst uvwxy z 0>'
    e_exc = r'Malformed tag at line 1, column 1 (char 1)'
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert str(exc)==e_exc, exc
    else:
        assert False, r

def test_sniffer():
    tests = [
        (r'',
         'UTF-8'),
        (r'''<?xml version='1.0' encoding='ISO-8859-1' ?>''',
         'ISO-8859-1'),
        (r'''<?xml version="1.0" encoding='ISO-8859-1' ?>''',
         'ISO-8859-1'),
        (r'''<?xml version="1.0" encoding='ISO-8859-1' standalone='no'?>''',
         'ISO-8859-1'),
        (r'''<?xml version='1.1' encoding="ISO-8859-1" standalone="yes" ?>''',
         'ISO-8859-1'),
        (r'''<?xml version="1.0" encoding="EBCDIC-some-cp" ?>'''.encode('cp037'),
         'EBCDIC-some-cp'),
        # and now the really viciously pedantic refusals...
        (r''' <?xml version="1.0" encoding="ISO-8859-1" ?>''',
         'UTF-8'),  # bad: space before decl
        (r'''<?xml version=1.0 encoding="ISO-8859-1" ?>''',
         'UTF-8'),  # bad: no quotes around version value
        (r'''<?xml encoding="ISO-8859-1" version="1.0" ?>''',
         'UTF-8'),  # bad: wrong order for attributes
        (r'''<?xml version="1.0" encoding="ISO-8859-1" standalone=no ?>''',
         'UTF-8'),  # bad: no quotes around standalone value
        (r'''<?xml version=" 1.0" encoding="ISO-8859-1" ?>''',
         'UTF-8'),  # bad: whitespace before version value
        (r'''<?xml version="1.0 " encoding="ISO-8859-1" ?>''',
         'UTF-8'),  # bad: whitespace after version value
        (r'''<?xml version="1.0" encoding=" ISO-8859-1" ?>''',
         'UTF-8'),  # bad: whitespace before encoding value
        (r'''<?xml version="1.0" encoding="ISO-8859-1 " ?>''',
         'UTF-8'),  # bad: whitespace after encoding value
        (r'''<?xml version="1.0" encoding=Big5 ?>''',
         'UTF-8'),  # bad: no quotes around encoding value
    ]
    for i, (s, e) in enumerate(tests):
        try:
            r = sniff_encoding(s)
        except ValidationError as exc:
            assert False, (exc, i)
        else:
            assert r==e, (r, i)

def test_sniffer_exc():
    s = six.u('<?xml version="1.0" encoding="Cp037" ?>').encode('utf-8-sig')
    e_exc = r'Multiply-specified encoding (BOM: utf_8_sig, XML decl: Cp037) at line 1, column 1 (char 1)'
    try:
        r = sniff_encoding(s)
    except ValidationError as exc:
        assert str(exc)==e_exc, exc
    else:
        assert False, r

def test_fix_xmldecl():
    # Slow compared to the other tests, but still only a few seconds.
    for encoding in encodings.aliases.aliases.values():
        if encoding in ('rot_13', 'quopri_codec', 'zlib_codec',
                        'base64_codec', 'uu_codec', 'tactis',
                        'hex_codec', 'bz2_codec'):
            continue
        try:
            ''.encode(encoding)
        except LookupError:  # not trying to handle unknown encodings yet
            continue
        xmldecl = fix_xmldecl(six.u('  <?xml>'), encoding, add_encoding=True)
        if encoding.lower().startswith('utf'):
            if '16' in encoding:
                if 'le' in encoding.lower():
                    assert xmldecl.startswith(codecs.BOM_UTF16_LE)
                if 'be' in encoding.lower():
                    assert xmldecl.startswith(codecs.BOM_UTF16_BE)
        sniffed = sniff_encoding(xmldecl)
        assert sniffed==encoding, (xmldecl, encoding, sniffed)
        xmldecl = fix_xmldecl(six.u('  <?xml>').encode(encoding), encoding,
                              add_encoding=True)
        if encoding.lower().startswith('utf'):
            if '16' in encoding:
                if 'le' in encoding.lower():
                    assert xmldecl.startswith(codecs.BOM_UTF16_LE)
                if 'be' in encoding.lower():
                    assert xmldecl.startswith(codecs.BOM_UTF16_BE)
        sniffed = sniff_encoding(xmldecl)
        assert sniffed==encoding, (xmldecl, encoding, sniffed)

def test_formfeed_in_xmldecl():
    xmldecl = fix_xmldecl(' \f\t\f\n\f\r\f<?xml>'.encode('utf16'))
    assert xmldecl.decode('utf16')=="<?xml version='1.0'?>", xmldecl
    xmldecl = fix_xmldecl((' \f\t\f\n\f\r\f<?xml\fversion="1.0"'
                           '\f\fstandalone=no\f\f?>').encode('utf16'))
    assert xmldecl.decode('utf16')==(
        '''<?xml version="1.0"  standalone='no'  ?>'''), xmldecl

def test_xhtmlify_handles_utf8_xmldecl():
    result = xhtmlify(six.u('<?xml><html>'), 'utf-8', _wrap=False)
    assert result.decode('utf-8')==six.u('<?xml version=\'1.0\'?><html xmlns="http://www.w3.org/1999/xhtml"></html>')

def test_xhtmlify_handles_utf16_xmldecl():
    result = xhtmlify(six.u('<?xml><html>'), 'utf_16_be', _wrap=False)
    assert result.decode('utf16')==six.u('<?xml version=\'1.0\'?><html xmlns="http://www.w3.org/1999/xhtml"></html>')

def test_doctype():
    s = r'''<?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head><title>test</title></head><body></body></html>'''
    e = r'''<?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>test</title></head><body></body></html>'''
    r = xhtmlify(s)
    assert r==e, r

def test_double_doctype():
    s = DOCTYPE_XHTML1_STRICT + DOCTYPE_XHTML1_STRICT
    assert s.split('\n')[1].startswith('<!DOCTYPE ')
    assert s[110:].startswith('<!DOCTYPE ')
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert str(exc).startswith(
            'Malformed tag at line 2, column 1 (char 111)'), exc
    else:
        assert False, r

def test_embedded_doctype():
    s = DOCTYPE_XHTML1_STRICT + '<html><head>' + DOCTYPE_XHTML1_STRICT
    assert s.split('\n')[1][12:].startswith('<!DOCTYPE ')
    assert s[122:].startswith('<!DOCTYPE '), s[122:]
    try:
        r = xhtmlify(s)
    except ValidationError as exc:
        assert str(exc).startswith(
            'Malformed tag at line 2, column 13 (char 123)'), exc
    else:
        assert False, r

def test_complex_doctype():
    s = r'''<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.1//EN"
               "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
    [
        <!ATTLIST form autocomplete CDATA #IMPLIED>
    ]>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head><title /></head><body/></html>'''
    assert _xhtmlify(s)==s

def test_xhtml_appendix_b_prohibitions():
    prohibitions = {
        'a': ['a'],
        'pre': ['img', 'object', 'big', 'small', 'sub', 'sup'],
        'button': ['input', 'select', 'textarea', 'label', 'button',
                   'form', 'fieldset', 'iframe', 'isindex'],
        'label': ['label'],
        'form': ['form'],
    }
    for parent in prohibitions:
        for child in prohibitions[parent]:
            s = '<%s><p><%s>' % (parent, child)
            if child==parent:
                e_exc = ("XHTML <%s> elements must not "
                         "contain other <%s> elements")
            else:
                e_exc = ("XHTML <%s> elements must not "
                         "contain <%s> elements")
            e_exc %= (parent, child)
            try:
                r = xhtmlify(s)
            except ValidationError as e:
                assert str(e).startswith(e_exc + ' at line 1'), (s, e)
            else:
                assert False, (s, r)

def test_fieldset_nesting():
    s = '<fieldset><fieldset></fieldset></fieldset>'
    e = s
    r = xhtmlify(s)
    assert r==e, repr(r)

def test_sup_nesting():
    s = '<sup><sup></sup></sup>'
    e = s
    r = xhtmlify(s)
    assert r==e, repr(r)