dom_query 0.29.0

HTML querying and manipulation with CSS selectors
Documentation
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
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
mod constants;
mod ctx;
mod elements;
mod ext;
mod heading;
mod serializer;
mod text_utils;

use tendril::StrTendril;

use crate::node::NodeRef;

pub fn serialize_md(
    root_node: &NodeRef,
    include_node: bool,
    skip_tags: Option<&[&str]>,
) -> StrTendril {
    serializer::MDSerializer::new(root_node, skip_tags).serialize(include_node)
}

#[cfg(test)]
mod tests {

    use crate::Document;

    use super::*;

    #[track_caller]
    fn html_2md_compare(html_contents: &str, expected: &str) {
        let doc = Document::from(html_contents);
        let root_node = &doc.root();
        let md_text = serialize_md(root_node, false, None);
        assert_eq!(md_text.as_ref(), expected);
    }

    #[test]
    fn test_heading_closing_hashes() {
        // a trailing run of `#` would be dropped as the closing sequence
        html_2md_compare("<h2>Title #</h2>", "## Title \\#");
        html_2md_compare("<h1>#</h1>", "# \\#");
        html_2md_compare("<h2>A ### </h2>", "## A \\###");
        html_2md_compare("<h1>C# and F#</h1>", "# C# and F#");
        html_2md_compare("<h2>Title #<br></h2>", "## Title \\#");
    }

    #[test]
    fn test_headings() {
        // when passing include_node: true, leading and trailing whitespaces will be kept.
        let contents = r"<h1>Heading 1</h1>
        <h2>Heading 2</h2>
        <h3>Heading 3</h3>
        <h4>Heading 4</h4>
        <h5>Heading 5</h5>
        <h6>Heading 6</h6>
        <h3><span>III.</span> Heading With Span</h3>
        <h3><span></span>Early years (2006–2009)</h3>
        <h3><span> </span> Early years (2006–2009)</h3>
        <hr>";

        let expected = "\n\n# Heading 1\n\n\
        ## Heading 2\n\n\
        ### Heading 3\n\n\
        #### Heading 4\n\n\
        ##### Heading 5\n\n\
        ###### Heading 6\n\n\
        ### III. Heading With Span\n\n\
        ### Early years (2006–2009)\n\n\
        ### Early years (2006–2009)\n\n\
        ---\n\n";

        let doc = Document::from(contents);
        let body_sel = &doc.select("body");
        let body_node = body_sel.nodes().first().unwrap();
        let md_text = serialize_md(body_node, true, None);
        assert_eq!(md_text.as_ref(), expected);
    }

    #[test]
    fn test_escaping() {
        // Punctuation that has no Markdown meaning in prose is not escaped.
        html_2md_compare("<p>Foo. Bar!</p>", "Foo. Bar!");
        html_2md_compare(
            "<p>Call (555) 123-4567, see {a: 1} and https://example.com/x.</p>",
            "Call (555) 123-4567, see {a: 1} and https://example.com/x.",
        );
        // mid-line `#`, `>` and `-` keep their literal meaning
        html_2md_compare(
            "<p>see # tags, a > b, x - y</p>",
            "see # tags, a > b, x - y",
        );

        // Characters that can start a Markdown block when a line begins are
        // escaped there.
        html_2md_compare("<p># not a heading</p>", "\\# not a heading");
        html_2md_compare("<p>> not a quote</p>", "\\> not a quote");
        html_2md_compare("<p>- not a list</p>", "\\- not a list");
        // ... and only the first character: later ones in the word are plain
        // text and have no block meaning
        html_2md_compare("<p>#tag-name</p>", "\\#tag-name");
        html_2md_compare("<p>+1-2</p>", "\\+1-2");
        html_2md_compare("<p>>foo>bar</p>", "\\>foo>bar");
        // a preceding space on the same line is not a line start
        html_2md_compare("<p><span>foo </span><span>#bar</span></p>", "foo #bar");
        // a line of only `=` would turn the line above into a heading
        html_2md_compare("<p>Title<br>===</p>", "Title  \n\\===");
        html_2md_compare("<p>=> a == b</p>", "=> a == b");
        html_2md_compare(
            "<p>2024. not an ordered list</p>",
            "2024\\. not an ordered list",
        );
        // ...but only there
        html_2md_compare("<p>see 2024. below</p>", "see 2024. below");

        // `!` is only escaped when it could introduce an image
        html_2md_compare("<p>Wow!</p>", "Wow!");
        html_2md_compare("<p>not ![an image]</p>", "not \\!\\[an image\\]");

        // Characters with Markdown meaning anywhere in the line are escaped.
        html_2md_compare(
            "<p>a *b* c _d_ e`f` [g]</p>",
            "a \\*b\\* c \\_d\\_ e\\`f\\` \\[g\\]",
        );
        // `|` always: it must not break generated table rows
        html_2md_compare("<p>a | b</p>", "a \\| b");
    }

    #[test]
    fn test_tilde_escaping() {
        html_2md_compare("<p>~~~</p><p>code?</p>", "\\~\\~\\~\n\ncode?");
        html_2md_compare("<p>~~strike~~</p>", "\\~\\~strike\\~\\~");
        html_2md_compare(
            "<p>run me@h:~/a$ make then me@h:~/b$ go</p>",
            "run me@h:\\~/a$ make then me@h:\\~/b$ go",
        );
        html_2md_compare("<p><code>~/a</code></p>", "`~/a`");
    }

    #[test]
    fn test_italic() {
        let contents = r"<h4><i>Italic Text</i></h4>";
        let expected = "#### *Italic Text*";

        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_span_italic() {
        let contents = r"<span>It`s like <i>that</i></span>";
        let expected = r"It\`s like *that*";

        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_bold_italic() {
        let contents = r"<span>It`s like <b><i>that</i></b></span>";
        let expected = r"It\`s like ***that***";

        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_simple_code() {
        let contents = r"<span>It`s like <code>that</code></span>";
        let expected = r"It\`s like `that`";
        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_code_edge_spaces() {
        // a parser strips one space from each side of `` ` a ` ``, so the
        // content is padded to keep both
        html_2md_compare("<p><code> a </code></p>", "`  a  `");
        // a space on one side only is kept as is
        html_2md_compare("<p><code> a</code></p>", "` a`");
    }

    #[test]
    fn test_false_multiline_code() {
        let contents = r"<span>
        It`s like 
        <code>
        that
        </code>
        </span>";
        let expected = r"It\`s like `that`";
        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_multiline_code() {
        let contents = r"<code>$ cargo new hello
    Created binary (application) `hello` package

$ cd hello</code>";
        let expected = r"```
$ cargo new hello
    Created binary (application) `hello` package

$ cd hello
```";
        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_code_span_with_backticks() {
        // Backslash escapes are not interpreted inside code spans, so content
        // containing backticks must be wrapped in a longer delimiter run
        // (CommonMark code fence rule), with spaces padding the content.
        html_2md_compare("<p><code>a `b` c</code></p>", "``a `b` c``");
        html_2md_compare("<p><code>`leading</code></p>", "`` `leading ``");
        html_2md_compare("<p><code>trailing`</code></p>", "`` trailing` ``");
        html_2md_compare("<p><code>```</code></p>", "```` ``` ````");
        // content without backticks keeps the single-backtick form
        html_2md_compare("<p><code>go.sum</code></p>", "`go.sum`");
    }
    #[test]
    fn test_ul() {
        let contents = "<h3>Pizza Margherita Ingredients</h3>\
        <ul>\
            <li>Pizza Dough</li>\
            <li>Mozzarella cheese</li>\
            <li>Tomatoes</li>\
            <li>Olive Oil</li>\
            <li><i>Basil</i></li>\
            <li><b>Salt</b></li>\
        </ul>";

        let expected = "### Pizza Margherita Ingredients\n\n\
        - Pizza Dough\n\
        - Mozzarella cheese\n\
        - Tomatoes\n\
        - Olive Oil\n\
        - *Basil*\n\
        - **Salt**";

        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_ol() {
        let contents = "<h3>Pizza Margherita Ingredients</h3>\
        <ol>\
            <li>Pizza Dough</li>\
            <li>Mozzarella cheese</li>\
            <li>Tomatoes</li>\
            <li>Olive Oil</li>\
            <li><i>Basil</i></li>\
            <li><b>Salt</b></li>\
        </ol>";

        let expected = "### Pizza Margherita Ingredients\n\n\
        1. Pizza Dough\n\
        2. Mozzarella cheese\n\
        3. Tomatoes\n\
        4. Olive Oil\n\
        5. *Basil*\n\
        6. **Salt**";

        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_bad_ol() {
        let contents = "<h3>Pizza Margherita Ingredients</h3>\
        <ol>\
            <li>Pizza Dough</li>\
            <li>Mozzarella cheese</li>\
            <li>Tomatoes</li>\
            <li>Olive Oil</li>\
            <div><i>Basil</i></div>\
            <li><b>Salt</b></li>\
        </ol>";

        let expected = "### Pizza Margherita Ingredients\n\n\
        1. Pizza Dough\n\
        2. Mozzarella cheese\n\
        3. Tomatoes\n\
        4. Olive Oil\n\
        \n*Basil*\n\n\
        5. **Salt**";

        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_ol_inline() {
        let contents = "
        <ol>\
            <li>Item 1</li>\
            <li>Item 2</li>\
            <li>Item 3\
                <div>\
                    <ol>\
                        <li>Item 3-1</li>\
                        <li>Item 3-2</li>\
                        <li>Item 3-3\
                            <ol>\
                                <li>Item 3-3-1</li>\
                                <li>Item 3-3-2</li>\
                                <li>Item 3-3-3</li>\
                            </ol>
                        </li>\
                    </ol>\
                </div>
            </li>\
        </ol>";

        let expected = "\
1. Item 1
2. Item 2
3. Item 3
    1. Item 3-1
    2. Item 3-2
    3. Item 3-3
        1. Item 3-3-1
        2. Item 3-3-2
        3. Item 3-3-3";

        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_ul_inline() {
        let contents = "
        <ul>\
            <li>Item 1</li>\
            <li>Item 2</li>\
            <li>Item 3\
                <div>\
                    <ul>\
                        <li>Item 3-1</li>\
                        <li>Item 3-2</li>\
                        <li>Item 3-3\
                            <ul>\
                                <li>Item 3-3-1</li>\
                                <li>Item 3-3-2</li>\
                                <li>Item 3-3-3</li>\
                            </ul>
                        </li>\
                    </ul>\
                </div>
            </li>\
        </ul>";

        let expected = "\
- Item 1
- Item 2
- Item 3
    - Item 3-1
    - Item 3-2
    - Item 3-3
        - Item 3-3-1
        - Item 3-3-2
        - Item 3-3-3";

        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_list_with_paragraphs() {
        let contents = "<ol>
            <li>
                <p>Paragraph 1-1</p>
                <p>Paragraph 1-2</p>
            </li>
            <li><p>Paragraph 2-1</p><p>Paragraph 2-2</p></li>
            <li><p>Paragraph 3-1</p></li>
        </ol>
        <p>Another Paragraph</p>";

        let expected = "\
1. Paragraph 1-1

   Paragraph 1-2

2. Paragraph 2-1

   Paragraph 2-2

3. Paragraph 3-1

Another Paragraph";

        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_nested_list_in_item_with_paragraphs() {
        html_2md_compare(
            "<ul><li><p>a</p><ul><li><p>b</p></li><li><p>c</p></li></ul></li></ul>",
            "- a\n\n    - b\n\n    - c",
        );
        // a tight nested list gets no hard break and stays tight
        html_2md_compare(
            "<ul><li><p>a</p><ul><li>b</li><li>c</li></ul></li></ul>",
            "- a\n\n    - b\n    - c",
        );
    }

    #[test]
    fn test_ordered_list_start_and_value() {
        html_2md_compare("<ol start=\"5\"><li>a</li><li>b</li></ol>", "5. a\n6. b");
        html_2md_compare(
            "<ol><li>a</li><li value=\"9\">b</li><li>c</li></ol>",
            "1. a\n9. b\n10. c",
        );
        // a non-numeric start falls back to the default numbering
        html_2md_compare("<ol start=\"x\"><li>a</li></ol>", "1. a");
        // HTML reads the leading integer and ignores the rest
        html_2md_compare("<ol start=\" 5abc\"><li>a</li></ol>", "5. a");
        html_2md_compare("<ol start=\"+3\"><li>a</li></ol>", "3. a");
        // Markdown numbers cannot be negative; 0 is the closest
        html_2md_compare("<ol start=\"-2\"><li>a</li><li>b</li></ol>", "0. a\n1. b");
        html_2md_compare("<ol><li>a</li><li value=\"-1\">b</li></ol>", "1. a\n0. b");
        // CommonMark markers hold at most nine digits, and the counter must
        // not overflow on huge values
        html_2md_compare(
            "<ol start=\"1234567890\"><li>a</li><li>b</li></ol>",
            "999999999. a\n999999999. b",
        );
        html_2md_compare(
            "<ol start=\"18446744073709551615\"><li>a</li></ol>",
            "999999999. a",
        );
        // huge values with leading zeros are clamped to the nine-digit limit
        html_2md_compare(
            "<ol start=\" 00000018446744073709551615\"><li>a</li></ol>",
            "999999999. a",
        );
        // leading zeros are stripped from the marker value
        html_2md_compare("<ol start=\"000000\"><li>a</li></ol>", "0. a");
        html_2md_compare(
            "<ol><li>a</li><li value=\"1234567890\">b</li></ol>",
            "1. a\n999999999. b",
        );
        // block syntax after a nine-digit marker is still escaped
        html_2md_compare(
            "<ol start=\"999999999\"><li>2)</li></ol>",
            "999999999. 2\\)",
        );
        html_2md_compare(
            "<ol start=\"123456789\"><li>- x</li></ol>",
            "123456789. \\- x",
        );
        html_2md_compare(
            "<ol start=\"123456789\"><li>a - x</li></ol>",
            "123456789. a - x",
        );
        html_2md_compare(
            "<ol start=\"999999998\"><li>a</li><li>2)</li></ol>",
            "999999998. a\n999999999. 2\\)",
        );
    }

    #[test]
    fn test_split_ordered_list_marker() {
        // digits at a line start and a `.` from the next node form a list marker
        html_2md_compare("<p><span>1</span>. foo</p>", "1\\. foo");
        html_2md_compare("<p>1<span>. not a list</span></p>", "1\\. not a list");
        html_2md_compare("<ul><li><span>2</span>) x</li></ul>", "- 2\\) x");
        html_2md_compare(
            "<ul><li>a<ul><li><span>3</span>. x</li></ul></li></ul>",
            "- a\n    - 3\\. x",
        );
        // the item's own marker is already written: `1. 3. x` would nest a list
        html_2md_compare("<ol><li><span>3</span>. x</li></ol>", "1. 3\\. x");
        html_2md_compare(
            "<ol start=\"12\"><li><span>3</span>) x</li></ol>",
            "12. 3\\) x",
        );
        // not a marker: mid-line digits, or a delimiter with no space after it
        html_2md_compare("<p><b>see</b> <span>1</span>. foo</p>", "**see** 1. foo");
        html_2md_compare("<p>In <span>2024</span>. Then</p>", "In 2024. Then");
        html_2md_compare("<p><span>1</span>.5 stars</p>", "1.5 stars");
        // not a marker: more than nine digits
        html_2md_compare("<p><span>10000000000</span>. foo</p>", "10000000000. foo");
    }

    #[test]
    fn test_nested_list_under_wide_marker() {
        // a child list must start at the parent item's content column; at a
        // fixed four columns it sits left of `100. ` and parses as an
        // indented code block
        let md = "100. a\n     1. x";
        html_2md_compare("<ol start=\"100\"><li>a<ol><li>x</li></ol></li></ol>", md);
        html_2md_compare(
            "<ol start=\"98\"><li>a</li><li>b</li><li>c<ul><li>n</li></ul></li></ol>",
            "98. a\n99. b\n100. c\n     - n",
        );
    }

    #[test]
    fn test_nested_list_not_starting_at_one() {
        // an ordered list that doesn't start at 1 can't interrupt a paragraph,
        // so it needs a blank line after the parent item's text
        html_2md_compare(
            "<ul><li>a<ol start=\"2\"><li>b</li></ol></li></ul>",
            "- a\n\n    2. b",
        );
        html_2md_compare(
            "<ul><li>a<ol><li value=\"3\">b</li></ol></li></ul>",
            "- a\n\n    3. b",
        );
    }

    #[test]
    fn test_nested_list_starting_with_empty_item() {
        // an empty item can't interrupt a paragraph, and a bare `-` under the
        // parent item's text would make that text a heading
        html_2md_compare(
            "<ul><li>a<ul><li></li><li>b</li></ul></li></ul>",
            "- a\n\n    -\n    - b",
        );
        html_2md_compare(
            "<ul><li>a<ul><li><ul><li>c</li></ul></li></ul></li></ul>",
            "- a\n\n    -\n        - c",
        );
    }

    #[test]
    fn test_nested_list_item_paragraphs() {
        // a nested item's second paragraph is indented to that item's content
        // column; at the outer item's column it parses as part of the outer item
        html_2md_compare(
            "<ul><li>a<ul><li><p>b</p><p>c</p></li></ul></li></ul>",
            "- a\n    - b\n\n      c",
        );
    }

    #[test]
    fn test_list_item_code_block() {
        // every line of the code block is indented to the item's content
        // column; at column zero the fence ends the list
        html_2md_compare(
            "<ul><li><p>a</p><pre><code>b\n\nc\n</code></pre><p>d</p></li></ul>",
            "- a\n\n  ```\n  b\n\n  c\n  ```\n\n  d",
        );
        html_2md_compare(
            "<ol start=\"10\"><li><pre><code>x\n</code></pre></li></ol>",
            "10. ```\n    x\n    ```",
        );
        html_2md_compare(
            "<ul><li>a<ul><li><p>b</p><pre><code>c\n</code></pre></li></ul></li></ul>",
            "- a\n    - b\n\n      ```\n      c\n      ```",
        );
        // after inline text the fence starts a new line
        html_2md_compare(
            "<ul><li>Run:<pre><code>make\n</code></pre></li></ul>",
            "- Run:\n  ```\n  make\n  ```",
        );
    }

    #[test]
    fn test_list_item_blockquote() {
        // every line of the quote is indented to the item's content column;
        // at column zero the quote ends the list
        html_2md_compare(
            "<ul><li><p>a</p><blockquote><p>b</p><p>c</p></blockquote><p>d</p></li></ul>",
            "- a\n\n  > b\n  > \n  > c\n\n  d",
        );
        html_2md_compare(
            "<ol start=\"10\"><li><blockquote><p>x</p><p>y</p></blockquote></li></ol>",
            "10. > x\n    > \n    > y",
        );
        // after inline text the quote starts a new line
        html_2md_compare(
            "<ul><li>a<blockquote><p>b</p></blockquote></li></ul>",
            "- a\n  > b",
        );
    }

    #[test]
    fn test_list_item_thematic_break() {
        html_2md_compare("<ul><li><hr></li><li>two</li></ul>", "-\n  ---\n- two");
        html_2md_compare(
            "<ul><li><p>a</p><hr><p>b</p></li><li>c</li></ul>",
            "- a\n\n  ---\n  b\n\n- c",
        );
        html_2md_compare("<ul><li>a<hr>b</li></ul>", "- a\n\n  ---\n  b");
        html_2md_compare(
            "<ol start=\"3\"><li><hr></li><li><p>a</p><hr></li></ol>",
            "3.\n   ---\n4. a\n\n   ---",
        );
        // a table cell has no line for the rule, but the break keeps the words apart
        html_2md_compare(
            "<table><tr><td><ul><li>a<hr>b</li></ul></td></tr></table>",
            "|   |\n| - |\n| + a<br>b |",
        );
    }

    #[test]
    fn test_list_tight_after_item_block() {
        // a blank line after an item's closing code block or quote would make
        // the list loose
        html_2md_compare(
            "<ul><li>a</li><li><pre><code>b\n</code></pre></li><li>c</li></ul>",
            "- a\n- ```\n  b\n  ```\n- c",
        );
        html_2md_compare(
            "<ul><li>a<blockquote><p>b</p></blockquote> <pre><code>c\n</code></pre> </li><li>d</li></ul>",
            "- a\n  > b\n  ```\n  c\n  ```\n- d",
        );
        html_2md_compare(
            "<ul><li><blockquote><p>a</p></blockquote></li><li>b</li></ul>",
            "- > a\n- b",
        );
        // two quotes in a row would merge without the blank line
        html_2md_compare(
            "<ul><li><blockquote><p>a</p></blockquote><blockquote><p>b</p></blockquote></li></ul>",
            "- > a\n\n  > b",
        );
        // text after the list would continue a quote that ends the last item
        html_2md_compare(
            "<ul><li>a</li><li><blockquote><p>b</p></blockquote></li></ul>c",
            "- a\n- > b\n\nc",
        );
    }

    #[test]
    fn test_list_item_heading() {
        // the heading keeps its marker, and text after it stays in the item
        html_2md_compare(
            "<ul><li><h1>Foo</h1></li><li><h2>Bar</h2>baz</li></ul>",
            "- # Foo\n- ## Bar\n  baz",
        );
        html_2md_compare(
            "<ol><li><h3>a #</h3><p>b</p></li><li>c</li></ol>",
            "1. ### a \\#\n   b\n\n2. c",
        );
    }

    #[test]
    fn test_list_item_nested_blocks() {
        // blocks inside a `<div>` in an item are written as the item's own blocks
        html_2md_compare(
            "<ul><li><div><p>a</p></div></li><li>two</li></ul>",
            "- a\n\n- two",
        );
        html_2md_compare(
            "<ul><li><div><p>a</p><p>b</p></div></li><li>two</li></ul>",
            "- a\n\n  b\n\n- two",
        );
        html_2md_compare(
            "<ol start=\"5\"><li><div><p>a</p></div></li><li>b</li></ol>",
            "5. a\n\n6. b",
        );
        html_2md_compare(
            "<ul><li><div><section><p>a</p><blockquote><p>b</p></blockquote></section></div></li></ul>",
            "- a\n\n  > b",
        );
        html_2md_compare(
            "<ul><li><div><p>a</p><!-- c --><p>b</p></div></li></ul>",
            "- a\n\n  b",
        );
        // whitespace between the blocks is not content
        html_2md_compare(
            "<ul><li><div>\n  <p>a</p>\n  <p>b</p>\n</div></li><li>two</li></ul>",
            "- a\n\n  b\n\n- two",
        );
    }

    #[test]
    fn test_list_item_nested_blocks_after_inline() {
        // a block after inline content starts its own indented line
        html_2md_compare("<ul><li>text<div><p>a</p></div></li></ul>", "- text\n  a");
        html_2md_compare(
            "<ul><li>text<div><h2>h</h2></div></li></ul>",
            "- text\n  ## h",
        );
        html_2md_compare(
            "<ul><li><a href=\"/u\">label</a><div><p>a</p></div></li></ul>",
            "- [label](/u)\n  a",
        );
        html_2md_compare(
            "<ul><li>text<br><div><p>a</p></div></li></ul>",
            "- text  \n  a",
        );
        // a block after inline text between blocks starts on a new line
        html_2md_compare(
            "<ul><li><div><p>a</p></div>tail<div><p>b</p></div></li></ul>",
            "- a\n\n  tail\n  b",
        );
    }

    #[test]
    fn test_list_item_nested_blocks_other_wrappers() {
        html_2md_compare(
            "<ul><li><article><p>a</p><p>b</p></article></li></ul>",
            "- a\n\n  b",
        );
        html_2md_compare(
            "<ul><li><div><pre>x\ny</pre></div></li></ul>",
            "- ```\n  x\n  y\n  ```",
        );
        // a wrapper holding a table or a rule is not unwrapped
        html_2md_compare(
            "<ul><li><div><p>a</p><table><tr><td>t</td></tr></table></div></li></ul>",
            "-\n\na\n\n\n|   |\n| - |\n| t |",
        );
        html_2md_compare(
            "<ul><li><div><p>a</p><hr><p>b</p></div></li></ul>",
            "-\n\na\n\n---\n\nb",
        );
    }

    #[test]
    fn test_list_item_nested_blocks_skip_tags() {
        let doc = Document::from("<ul><li><div><section><p>a</p></section></div></li></ul>");
        assert_eq!(doc.md(Some(&["section"])).as_ref(), "-");
        let doc = Document::from("<ul><li><div><p>a</p></div></li></ul>");
        assert_eq!(doc.md(Some(&["p"])).as_ref(), "-");
    }

    #[test]
    fn test_list_item_text_after_block() {
        // unindented, the text would end the list and run into the next marker
        html_2md_compare(
            "<ul><li><p>a</p>b <em>c</em></li><li>d</li></ul>",
            "- a\n\n  b *c*\n- d",
        );
        html_2md_compare(
            "<ul><li><pre><code>a\n</code></pre>b</li><li>c</li></ul>",
            "- ```\n  a\n  ```\n\n  b\n- c",
        );
        // the text starts a line, so block syntax in it is escaped
        html_2md_compare(
            "<ul><li><p>a</p># b<br>+ c</li><li>+ d<p>e</p></li></ul>",
            "- a\n\n  \\# b  \n  \\+ c\n- \\+ d\n  e",
        );
    }

    #[test]
    fn test_list_item_block_syntax_text() {
        // text inside a list item that begins with block syntax must be
        // escaped, or it turns the line into a nested construct
        html_2md_compare("<ul><li>1. Preheat oven</li></ul>", "- 1\\. Preheat oven");
        html_2md_compare("<ul><li># tag</li></ul>", "- \\# tag");
        html_2md_compare("<ul><li>- x</li></ul>", "- \\- x");
        html_2md_compare("<ul><li>+ x</li></ul>", "- \\+ x");
        html_2md_compare("<ul><li>&gt; x</li></ul>", "- \\> x");
        html_2md_compare("<ol><li># tag</li></ol>", "1. \\# tag");
        // continuation blocks inside an item are indented, not at a raw line
        // start, and need the same protection
        html_2md_compare("<ul><li><p>a</p><p># b</p></li></ul>", "- a\n\n  \\# b");
        // plain inline content in the same positions stays unescaped
        html_2md_compare("<ul><li>a #b</li></ul>", "- a #b");
    }

    #[test]
    fn test_list_item_comment() {
        // a comment writes nothing, so it must not indent or space the next block
        html_2md_compare("<ul><li><p>a</p><!-- c --><p>b</p></li></ul>", "- a\n\n  b");
        html_2md_compare(
            "<ol><li><p>a</p><!-- c --><p>b</p></li></ol>",
            "1. a\n\n   b",
        );
        html_2md_compare(
            "<ul><li><pre>a</pre><!-- c --></li><li>b</li></ul>",
            "- ```\n  a\n  ```\n- b",
        );
        html_2md_compare(
            "<ul><li>x<ul><li><p>n</p><!-- c --><p>m</p></li></ul></li></ul>",
            "- x\n    - n\n\n      m",
        );
    }

    #[test]
    fn test_nbsp_is_not_collapsed() {
        // HTML collapses only ASCII whitespace; `&nbsp;` is content
        html_2md_compare("<p>a&nbsp;&nbsp;b</p>", "a\u{a0}\u{a0}b");
        html_2md_compare("<p>&nbsp;a&nbsp;</p>", "\u{a0}a\u{a0}");
        html_2md_compare("<p>a <span>&nbsp;</span> b</p>", "a \u{a0} b");
    }

    #[test]
    fn test_paragraphs() {
        let contents =
            "<p>To create paragraphs, use a blank line to separate one or more lines of text.</p>
        <p>I really like using <span>Markdown</span><span>  text</span>.</p>

        <p>I think I'll use it to format all of my documents from now on.</p>";

        let expected = "To create paragraphs, use a blank line to separate one or more lines of text.\n\n\
        I really like using Markdown text.\n\n\
        I think I'll use it to format all of my documents from now on.";

        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_links() {
        let simple_contents = r#"<p>My favorite search engine is <a href="https://duckduckgo.com">Duck Duck Go</a>.</p>"#;
        let simple_expected =
            r"My favorite search engine is [Duck Duck Go](https://duckduckgo.com).";
        html_2md_compare(simple_contents, simple_expected);

        // link with title attribute
        let title_contents = r#"<p>My favorite search engine is <a href="https://duckduckgo.com" title="Duck Duck Go">Duck Duck Go</a>.</p>"#;
        let title_expected = r#"My favorite search engine is [Duck Duck Go](https://duckduckgo.com "Duck Duck Go")."#;
        html_2md_compare(title_contents, title_expected);

        let bold_contents = r#"<p>My favorite search engine is <b><a href="https://duckduckgo.com">Duck Duck Go</a></b>.</p>"#;
        let bold_expected =
            r"My favorite search engine is **[Duck Duck Go](https://duckduckgo.com)**.";
        html_2md_compare(bold_contents, bold_expected);

        // bold inside of link is not supported.
        let bold_ignored_contents = r#"<p>My favorite search engine is <a href="https://duckduckgo.com"><b>Duck Duck Go</b></a>.</p>"#;
        let bold_ignored_expected =
            r"My favorite search engine is [Duck Duck Go](https://duckduckgo.com).";
        html_2md_compare(bold_ignored_contents, bold_ignored_expected);

        // any elements inside `a` elements are also ignored,
        // html5ever transforms a > div to div > a, and there is no way to determine how it was.
        // This is an open question.
        let ignored_contents = r#"<p>My favorite search engine is <a href="https://duckduckgo.com"><div>Duck Duck Go</div></a>.</p>"#;
        let ignored_expected =
            "My favorite search engine is\n\n[Duck Duck Go](https://duckduckgo.com)

.";
        html_2md_compare(ignored_contents, ignored_expected);

        let no_href_contents = r"<p>My favorite search engine is <a>Duck Duck Go</a>.</p>";
        let no_href_expected = "My favorite search engine is Duck Duck Go.";
        html_2md_compare(no_href_contents, no_href_expected);

        let complex_contents =
            r#"<a href="https://duckduckgo.com" title="My &quot;Search&quot;">Duck Duck Go</a>"#;
        let comptex_expected = r#"[Duck Duck Go](https://duckduckgo.com "My \"Search\"")"#;
        html_2md_compare(complex_contents, comptex_expected);

        // avoid escaping links twice
        let content = r##"<p>See <a href="http://x">[Link]</a> and <a href="http://y">utm_source</a></p>
            <h2>What I wanted <a href="#w">#</a></h2>"##;
        let expected = concat!(
            "See [\\[Link\\]](http://x) and [utm\\_source](http://y)\n\n",
            "## What I wanted [#](#w)"
        );
        html_2md_compare(content, expected);
    }

    #[test]
    fn test_bang_before_link_or_image() {
        // a trailing `!` must not turn the link into image syntax
        html_2md_compare(
            r#"<p>Wow!<a href="https://e.com">x</a></p>"#,
            r"Wow\![x](https://e.com)",
        );
        // a backslash before it is text and does not escape the `!`
        html_2md_compare(
            r#"<p>Wow\!<a href="https://e.com">x</a></p>"#,
            r"Wow\\\![x](https://e.com)",
        );
        html_2md_compare(
            r#"<p>Wow!<a href="u"><img src="i.png" alt="a"></a></p>"#,
            r"Wow\![![a](i.png)](u)",
        );
        // emphasis merged into its parent or neighbour writes no marker in between
        html_2md_compare(
            r#"<p><b>Wow!<b><a href="u">x</a></b></b></p>"#,
            r"**Wow\![x](u)**",
        );
        html_2md_compare(
            r#"<p><b>Wow!</b><b><a href="u">x</a></b></p>"#,
            r"**Wow\![x](u)**",
        );
        html_2md_compare(r#"<p>Wow!<b><a href="u">x</a></b></p>"#, r"Wow!**[x](u)**");
    }

    #[test]
    fn test_images() {
        let simple_contents = r#"<p>Image: <img src="/path/to/img.jpg" alt="Alt text"></p>"#;
        let simple_expected = "Image: ![Alt text](/path/to/img.jpg)";
        html_2md_compare(simple_contents, simple_expected);

        // with title
        let simple_contents =
            r#"<p>Image: <img src="/path/to/img.jpg" alt="Alt text" title="Title"></p>"#;
        let simple_expected = r#"Image: ![Alt text](/path/to/img.jpg "Title")"#;
        html_2md_compare(simple_contents, simple_expected);

        // without alt
        let simple_contents = r#"<p>Image: <img src="/path/to/img.jpg"></p>"#;
        let simple_expected = r"Image: ![](/path/to/img.jpg)";
        html_2md_compare(simple_contents, simple_expected);

        // no img
        let simple_contents = r#"<p>Image:  <img alt="Alt text" title="Title"></p>"#;
        let simple_expected = "Image:";
        html_2md_compare(simple_contents, simple_expected);
    }

    #[test]
    fn test_img_src_fallbacks() {
        // lazy-load pages carry the image URL in srcset or data-src
        html_2md_compare(
            "<p><img srcset=\"https://i.e.com/p.png 1x, https://i.e.com/p2x.png 2x\" alt=\"pic\"></p>",
            "![pic](https://i.e.com/p.png)",
        );
        html_2md_compare(
            "<p><img srcset=\"https://i.e.com/p.png, https://i.e.com/p2x.png 2x\" alt=\"pic\"></p>",
            "![pic](https://i.e.com/p.png)",
        );
        html_2md_compare(
            "<p><img data-src=\"https://i.e.com/p.png\" alt=\"pic\"></p>",
            "![pic](https://i.e.com/p.png)",
        );
        // a real src still wins
        html_2md_compare(
            "<p><img src=\"https://i.e.com/s.png\" data-src=\"https://i.e.com/d.png\" alt=\"pic\"></p>",
            "![pic](https://i.e.com/s.png)",
        );
        // an empty placeholder src counts as missing
        html_2md_compare(
            "<p><img src=\"\" data-src=\"https://i.e.com/p.png\" alt=\"pic\"></p>",
            "![pic](https://i.e.com/p.png)",
        );
        // no URL anywhere: still dropped
        html_2md_compare("<p><img alt=\"pic\"></p>", "");
        html_2md_compare("<p><img src=\"\" alt=\"pic\"></p>", "");
    }

    #[test]
    fn test_whitespace_only_link() {
        html_2md_compare(
            "<h2>VMAF <a href=\"#vmaf\"> </a></h2><p>body</p>",
            "## VMAF\n\nbody",
        );
        // the words around it stay apart
        html_2md_compare("<p>x<a href=\"/p\"> </a>y</p>", "x y");
        // text after it at a line start is still escaped
        html_2md_compare("<p><a href=\"/p\"> </a># x</p>", "\\# x");
    }

    #[test]
    fn test_linked_image() {
        // an image wrapped in a link must become the link body, not be dropped
        html_2md_compare(
            r#"<p><a href="https://e.com"><img src="https://i.e.com/p.png" alt="pic"></a></p>"#,
            "[![pic](https://i.e.com/p.png)](https://e.com)",
        );
        // an image with a text sibling keeps both
        html_2md_compare(
            r#"<p><a href="https://e.com">see <img src="https://i.e.com/p.png" alt="pic"></a></p>"#,
            "[see ![pic](https://i.e.com/p.png)](https://e.com)",
        );
        // a genuinely empty link is still skipped
        html_2md_compare(r#"<p><a href="https://e.com"></a>x</p>"#, "x");

        let simple_contents =
            r#"<p>Image: <img src="/path/to/img.jpg" alt="Alt text" title='A "Great" Photo'></p>"#;
        let simple_expected = r#"Image: ![Alt text](/path/to/img.jpg "A \"Great\" Photo")"#;
        html_2md_compare(simple_contents, simple_expected);
    }

    #[test]
    fn test_link_wrapping_blocks() {
        // a blank line inside `[...]` ends the paragraph and breaks the link
        html_2md_compare(
            "<a href=\"/post\"><img src=\"a.jpg\" alt=\"i\"><h3>Title</h3><p>Blurb</p></a>",
            "[![i](a.jpg) Title Blurb](/post)",
        );
        html_2md_compare(
            "<a href=\"/p\"><img src=\"a.jpg\"><ul><li>a</li><li>b<ol><li>c</li></ol></li></ul></a>",
            "[![](a.jpg) a b c](/p)",
        );
        html_2md_compare(
            "<a href=\"/p\"><img src=\"a.jpg\"><blockquote><p>q</p><p>r</p></blockquote></a>",
            "[![](a.jpg) q r](/p)",
        );
        html_2md_compare(
            "<a href=\"/p\"><img src=\"a.jpg\"><table><tr><th>h</th><th>i</th></tr><tr><td>c</td><td>d</td></tr></table></a>",
            "[![](a.jpg) h i c d](/p)",
        );
        // code becomes a span; its `#` lines are text, not headings
        html_2md_compare(
            "<a href=\"/p\"><img src=\"a.jpg\"><pre><code class=\"language-rust\"># comment\n```\n# x</code></pre></a>",
            "[![](a.jpg) ````# comment ``` # x````](/p)",
        );
        // a hard break must not leave a blank line inside `[...]`, or in a table cell
        html_2md_compare(
            "<a href=\"/p\"><img src=\"a.jpg\">a<br><br>b</a>",
            "[![](a.jpg)a<br><br>b](/p)",
        );
        html_2md_compare(
            "<table><tr><th>h</th></tr><tr><td><a href=\"/p\"><img src=\"a.jpg\"><h3>T</h3><p>B</p><br>x</a></td></tr></table>",
            "| h |\n| - |\n| [![](a.jpg) T B<br>x](/p) |",
        );
    }

    #[test]
    fn test_link_destination_and_alt_escaping() {
        // balanced parens need no escaping
        html_2md_compare(
            "<p><a href=\"https://e.com/x(y)z\">x</a></p>",
            "[x](https://e.com/x(y)z)",
        );
        // unbalanced parens truncate the destination at the stray `)`
        html_2md_compare(
            "<p><a href=\"https://e.com/x)y\">x</a></p>",
            "[x](<https://e.com/x)y>)",
        );
        // a space turns the whole destination into plain text
        html_2md_compare(
            "<p><a href=\"https://e.com/a b.png\">x</a></p>",
            "[x](<https://e.com/a b.png>)",
        );
        // `>` alone is legal in a bare destination
        html_2md_compare(
            "<p><a href=\"https://e.com/a>b\">x</a></p>",
            "[x](https://e.com/a>b)",
        );
        // both brackets in the link
        html_2md_compare(
            r#"<p><a href="https://e.com/a<b>">x</a></p>"#,
            r"[x](<https://e.com/a\<b\>>)",
        );
        // brackets in alt text break the image
        html_2md_compare(
            "<p><img src=\"https://i.e.com/p.png\" alt=\"a [b] c\"></p>",
            "![a \\[b\\] c](https://i.e.com/p.png)",
        );
        // single '<'
        html_2md_compare(
            r#"<p><a href="https://e.com/a<b">x</a></p>"#,
            r"[x](<https://e.com/a\<b>)",
        );

        // '\n' (%0A)
        html_2md_compare(
            "<p><a href=\"https://e.com/a\nb\">x</a></p>",
            "[x](<https://e.com/a%0Ab>)",
        );

        // '\r' (%0D) — pass with &#13; или &#xD;
        html_2md_compare(
            r#"<p><a href="https://e.com/a&#13;b">x</a></p>"#,
            r"[x](<https://e.com/a%0Db>)",
        );

        // a backslash is doubled in either form: `\>` would escape the
        // wrapper's closing `>`, `\*` would lose the backslash
        html_2md_compare("<p><a href=\"a b\\\">x</a></p>", "[x](<a b\\\\>)");
        html_2md_compare("<p><a href=\"a\\*b\">x</a></p>", "[x](a\\\\*b)");
        // a tab also needs the wrapper
        html_2md_compare("<p><a href=\"a\tb\">x</a></p>", "[x](<a\tb>)");
        // alt text is escaped like other inline text
        html_2md_compare("<p><img src=\"i.png\" alt=\"a\\\"></p>", "![a\\\\](i.png)");
        html_2md_compare(
            "<p><img src=\"i.png\" alt=\"*y*\"></p>",
            "![\\*y\\*](i.png)",
        );
    }

    #[test]
    fn test_pre_code() {
        let simple_contents = "<pre>\
<span>fn</span> <span>main</span><span>()</span><span> </span><span>{</span>\n\
<span>    </span><span>println!</span><span>(</span><span>\"Hello, World!\"</span><span>);</span>\n\
<span>}</span>\
</pre>";
        let simple_expected = "```
fn main() {
    println!(\"Hello, World!\");
}
```";
        html_2md_compare(simple_contents, simple_expected);
    }

    #[test]
    fn test_pre_code_with_data_lang_attribute() {
        let simple_contents = "<pre data-lang=\"rust\">\
<span>fn</span> <span>main</span><span>()</span><span> </span><span>{</span>\n\
<span>    </span><span>println!</span><span>(</span><span>\"Hello, World!\"</span><span>);</span>\n\
<span>}</span>\n\
</pre>";
        let simple_expected = "```rust
fn main() {
    println!(\"Hello, World!\");
}
```";
        html_2md_compare(simple_contents, simple_expected);
    }

    #[test]
    fn test_pre_code_with_data_lang_attribute_in_parent_tag() {
        let simple_contents = "<div data-lang=\"rust\"><pre>\
<span>fn</span> <span>main</span><span>()</span><span> </span><span>{</span>\n\
<span>    </span><span>println!</span><span>(</span><span>\"Hello, World!\"</span><span>);</span>\n\
<span>}</span>\n\
</pre></div>";
        let simple_expected = "```rust
fn main() {
    println!(\"Hello, World!\");
}
```";
        html_2md_compare(simple_contents, simple_expected);
    }

    #[test]
    fn test_pre_code_with_language_css_class_in_child_code_tag() {
        let contents = "<pre><code class=\"language-rust something else\">\
<span>fn</span> <span>main</span><span>()</span><span> </span><span>{</span>\n\
<span>    </span><span>println!</span><span>(</span><span>\"Hello, World!\"</span><span>);</span>\n\
<span>}</span>\n\
</code></pre>";
        let expected = "```rust
fn main() {
    println!(\"Hello, World!\");
}
```";
        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_pre_empty() {
        html_2md_compare("<pre><code></code></pre>", "```\n```");
    }

    #[test]
    fn test_blockquote() {
        let simple_contents = "<blockquote><p>Quoted text</p></blockquote>";
        let simple_expected = "> Quoted text";
        html_2md_compare(simple_contents, simple_expected);

        let complex_contents = "<blockquote>
<p>
Who has seen the wind?<br>
Neither I nor you:<br>
But when the leaves hang trembling,<br>
The wind is passing through.
</p>
<p>
Who has seen the wind?<br>
Neither you nor I:<br>
But when the trees bow down their heads,<br>
The wind is passing by.
</p>
</blockquote>
<p><i>Christina Rossetti</i></p>";
        let complex_expected = r"> Who has seen the wind?  
> Neither I nor you:  
> But when the leaves hang trembling,  
> The wind is passing through.
> 
> Who has seen the wind?  
> Neither you nor I:  
> But when the trees bow down their heads,  
> The wind is passing by.

*Christina Rossetti*";
        html_2md_compare(complex_contents, complex_expected);

        let empty_contents = "<blockquote></blockquote>";
        let empty_expected = "";
        html_2md_compare(empty_contents, empty_expected);
    }

    #[test]
    fn test_inline_blockquote() {
        let contents = "<blockquote>
<p>
Who has seen the wind?<br>
Neither I nor you:<br>
But when the leaves hang trembling,<br>
The wind is passing through.
</p>
<blockquote>
<p>
Who has seen the wind?<br>
Neither you nor I:<br>
But when the trees bow down their heads,<br>
The wind is passing by.
</p>
</blockquote>
</blockquote>";
        let expected = r"> Who has seen the wind?  
> Neither I nor you:  
> But when the leaves hang trembling,  
> The wind is passing through.
> 
> > Who has seen the wind?  
> > Neither you nor I:  
> > But when the trees bow down their heads,  
> > The wind is passing by.";
        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_table() {
        let contents = "<table>
    <tr>
        <th>Column 1</th>
        <th>Column 2</th>
        <th>Column 3</th>
    </tr>
    <tr>
        <td>R 1, <i>C 1</i></td>
        <td>R 1, <i>C 2</i></td>
        <td>R 1, <i>C 3</i></td>
    </tr>
    <tr>
        <td>R 2, <i>C 1</i></td>
        <td>R 2, <i>C 2</i></td>
        <td>R 2, <i>C 3</i></td>
    </tr>
</table>";
        let expected = "| Column 1 | Column 2 | Column 3 |
| -------- | -------- | -------- |
| R 1, *C 1* | R 1, *C 2* | R 1, *C 3* |
| R 2, *C 1* | R 2, *C 2* | R 2, *C 3* |";

        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_table_inside_table() {
        let contents = "<table>
    <tr>
        <td>
            <table>
                <tr>
                    <th>Column 1</th>
                    <th>Column 2</th>
                    <th>Column 3</th>
                </tr>
                <tr>
                    <td>R 1, <i>C 1</i></td>
                    <td>R 1, <i>C 2</i></td>
                    <td>R 1, <i>C 3</i></td>
                </tr>
                <tr>
                    <td>R 2, <i>C 1</i></td>
                    <td>R 2, <i>C 2</i></td>
                    <td>R 2, <i>C 3</i></td>
                </tr>
            </table>
        </td>
    </tr>
</table>";
        let expected = "| Column 1 | Column 2 | Column 3 |
| -------- | -------- | -------- |
| R 1, *C 1* | R 1, *C 2* | R 1, *C 3* |
| R 2, *C 1* | R 2, *C 2* | R 2, *C 3* |";
        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_table_without_headings() {
        let contents = "<table>
    <tr>
        <td>R 1, <i>C 1</i></td>
        <td>R 1, <i>C 2</i></td>
        <td>R 1, <i>C 3</i></td>
    </tr>
    <tr>
        <td>R 2, <i>C 1</i></td>
        <td>R 2, <i>C 2</i></td>
        <td>R 2, <i>C 3</i></td>
    </tr>
</table>";
        let expected = "|   |   |   |
| - | - | - |
| R 1, *C 1* | R 1, *C 2* | R 1, *C 3* |
| R 2, *C 1* | R 2, *C 2* | R 2, *C 3* |";
        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_table_skip() {
        let contents = "<table>
    <tr>
        <td>R 1, <i>C 1</i></td>
        <td>R 1, <i>C 2</i></td>
        <td>R 1, <i>C 3</i></td>
    </tr>
    <tr>
        <td>R 2, <i>C 1</i></td>
        <td>R 2, <i>C 2</i></td>
    </tr>
</table>";
        let expected = "R 1, *C 1* R 1, *C 2* R 1, *C 3*  
R 2, *C 1* R 2, *C 2*";
        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_table_empty() {
        let contents = "<table>
    <tr></tr>
    <tr></tr>
</table>";
        let expected = "";
        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_table_with_list() {
        let contents = "<table>
    <tr>
        <td>1</td>
        <td>
            <ul><li>Lemon</li></ul>
            <ul><li>Lime</li></ul>
            <ul><li>Grapefruit</li></ul>
            <ul><li>Orange</li></ul>
        </td>
    </tr>
</table>";
        let expected =
            "|   |   |\n| - | - |\n| 1 | + Lemon<br>+ Lime<br>+ Grapefruit<br>+ Orange |";
        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_table_cell_blocks() {
        // block children of a table cell are joined with the cell linebreak
        html_2md_compare(
            "<table><tr><th>H</th></tr><tr><td><p>para1</p><p>para2</p></td></tr></table>",
            "| H |\n| - |\n| para1<br>para2 |",
        );
    }

    #[test]
    fn test_table_cell_trailing_br() {
        html_2md_compare(
            "<table><tr><th>a</th></tr><tr><td>x<br></td></tr></table>",
            "| a |\n| - |\n| x |",
        );
        // text that reads `<br>` at the end of a cell is not a linebreak
        html_2md_compare(
            "<table><tr><th>H</th></tr><tr><td>use &lt;br&gt;</td></tr></table>",
            "| H |\n| - |\n| use \\<br> |",
        );
        // an escaped backslash does not escape the linebreak after it
        html_2md_compare(
            "<table><tr><th>H</th></tr><tr><td><p>a\\</p></td></tr></table>",
            "| H |\n| - |\n| a\\\\ |",
        );
        // an escaped `<br>` that ends a block does not swallow the next block's break
        html_2md_compare(
            "<table><tr><th>H</th></tr><tr><td><p>use &lt;br&gt;</p><p>x</p></td></tr></table>",
            "| H |\n| - |\n| use \\<br><br>x |",
        );
    }

    #[test]
    fn test_table_row_headers() {
        // a `th` beside a `td` labels its row, it isn't a column heading
        let contents = "<table>
    <tr><th>Lib</th><th>First</th></tr>
    <tr><th>Rustls</th><td>14</td></tr>
    <tr><th>BoringSSL</th><td>2</td></tr>
</table>";
        let expected = "| Lib | First |\n| --- | ----- |\n| Rustls | 14 |\n| BoringSSL | 2 |";
        html_2md_compare(contents, expected);

        let contents = "<table>
    <tr><th>Rustls</th><td>14</td></tr>
    <tr><th>BoringSSL</th><td>2</td></tr>
</table>";
        let expected = "|   |   |\n| - | - |\n| Rustls | 14 |\n| BoringSSL | 2 |";
        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_table_headings_after_empty_row() {
        // the header row is the first row that has cells
        let contents = "<table>
    <tr></tr>
    <tr><th>A</th><th>B</th></tr>
    <tr><td>1</td><td>2</td></tr>
</table>";
        html_2md_compare(contents, "| A | B |\n| - | - |\n| 1 | 2 |");
    }

    #[test]
    fn test_table_only_headings() {
        let contents = "<table><tr><th>a</th><th>b</th></tr></table>";
        html_2md_compare(contents, "| a | b |\n| - | - |");
    }

    #[test]
    fn test_table_empty_th() {
        let contents = "<table>
        <tr>
            <th></th>
            <th>x</th>
          </tr>
        <tr>
            <td>a</td>
            <td>b</td>
            </tr>
    </table>";
        let expected = "|  | x |\n| - | - |\n| a | b |";
        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_table_cell_escape() {
        // code with unescaped pipe, link with unescaped pipe
        let contents = r#"<table>
            <tr>
                <th>a</th>
            </tr>
            <tr>
                <td><code>a|b</code> <a href="http://x/a|b">l</a></td>
            </tr>
        </table>"#;
        let expected = "| a |\n| - |\n| `a\\|b` [l](http://x/a\\|b) |";
        html_2md_compare(contents, expected);

        // skipping escape table inside emphasis
        let contents = r"<table>
            <tr>
                <th><b>a|b</b></th>
            </tr>
            <tr>
                <td><i>c|d</i></td>
            </tr>
        </table>";
        let expected = "| **a\\|b** |\n| -------- |\n| *c\\|d* |";
        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_skip_tags_default() {
        // By default, formatter will skip ["script", "style", "meta", "head"]
        let contents = "
        <style>p {color: blue;}</style>
        <p>I really like using <b>Markdown</b>.</p>

        <p>I think I'll use it to format all of my documents from now on.</p>";

        let expected = "I really like using **Markdown**.\n\n\
        I think I'll use it to format all of my documents from now on.";

        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_skip_tags() {
        // If you need all text content of the elements, you need to pass Some(&vec![]) to `md`.
        // If you pass a structure like this into `Document::from`, the html5ever will create html > head > style.
        // If you want to preserve order use `Document::fragment`.
        let contents = "<style>p {color: blue;}</style>\
        <div><h1>Content Heading<h1></div>\
        <p>I really like using Markdown.</p>\
        <p>I think I'll use it to format all of my documents from now on.</p>";

        let expected = "p {color: blue;}\n\n\
        I really like using Markdown.\n\n\
        I think I'll use it to format all of my documents from now on.";

        let doc = Document::fragment(contents);
        let html_node = &doc.root();
        let md_text = serialize_md(html_node, false, Some(&["div"]));
        assert_eq!(md_text.as_ref(), expected);
    }

    #[test]
    fn test_text_after_list() {
        // on the next line the text would continue the last item
        html_2md_compare("<ul><li>a</li></ul>b", "- a\n\nb");
        html_2md_compare(
            "<ul><li>a</li></ul><pre><code>b\n</code></pre>",
            "- a\n\n```\nb\n```",
        );
        // a nested list that ends its item doesn't make the parent list loose
        html_2md_compare(
            "<ul><li>a<ul><li>b</li></ul></li><li>c</li></ul>d",
            "- a\n    - b\n- c\n\nd",
        );
    }
    #[test]
    fn test_linebreak_after_lists() {
        let contents = r"Influenced
        <ul>
         <li>Idris (programming language)</li>
         <li>Project Verona</li>
         <li>Spark</li>
         <li>Swift</li>
         <li>V</li>
         <li>Zig</li>
        </ul>
        <p><b>Rust</b> is a general-purpose programming language</p>";
        let expected = "Influenced\n\n\
- Idris (programming language)
- Project Verona
- Spark
- Swift
- V
- Zig

**Rust** is a general-purpose programming language";

        html_2md_compare(contents, expected);
    }

    #[test]
    fn test_pre_code_without_new_line() {
        let simple_contents = r#"<pre>
<span>fn</span> <span>main</span><span>()</span><span> </span><span>{</span>
<span>    </span><span>println!</span><span>(</span><span>"Hello, World!"</span><span>);</span>
<span>}</span></pre>"#;
        let simple_expected = "```
fn main() {
    println!(\"Hello, World!\");
}
```";
        html_2md_compare(simple_contents, simple_expected);
    }

    #[test]
    fn test_pre_with_interior_backticks() {
        // A fenced block whose content contains a fence-length backtick line
        // would terminate at that line; the fence must be longer than any
        // interior backtick run (CommonMark §fenced-code-blocks).
        html_2md_compare(
            "<pre><code>```bash\nls\n```</code></pre>",
            "````\n```bash\nls\n```\n````",
        );
        // a short interior run does not force a longer fence
        html_2md_compare("<pre><code>a`b</code></pre>", "```\na`b\n```");
    }

    #[test]
    fn test_empty_and_adjacent_code_spans() {
        // an empty code element writes nothing, instead of a stray backtick run
        html_2md_compare("<p>a<code></code>b</p>", "ab");
        // a whitespace-only one is a valid code span
        html_2md_compare("<p><code>  </code>x</p>", "` `x");
        html_2md_compare("<p>a<code></code>|<code> </code>b</p>", "a\\|` `b");
        // adjacent code spans are kept apart: `x``y` would be a single span
        let md = "`x` `y`";
        html_2md_compare("<p><code>x</code><code>y</code></p>", md);
    }

    #[test]
    fn test_code_span_with_elements() {
        // markup inside a code span has no meaning: nested elements give
        // their raw text, without backslash escapes or extra backticks
        html_2md_compare("<p><code><code>x</code></code></p>", "`x`");
        html_2md_compare("<p><code><a href=\"/u\">~</a></code></p>", "`~`");
        html_2md_compare("<p><code>a <b>*b*</b> c</code></p>", "`a *b* c`");
        html_2md_compare("<p><code>a<br>b</code></p>", "`a b`");
        html_2md_compare("<p><code>x_1<span>[y]</span></code></p>", "`x_1[y]`");
        // skipped tags stay skipped
        html_2md_compare("<p><code>a<script>s</script></code></p>", "`a`");
    }

    #[test]
    fn test_deeply_nested_code() {
        // each level used to rewrap the inner span in a longer fence, so
        // time was cubic and output quadratic in the depth; the exact output
        // pins both, without a wall-clock bound that could flake in CI
        let depth = 100;
        let html = format!(
            "<p>{}x{}</p>",
            "<code>".repeat(depth),
            "</code>".repeat(depth)
        );
        let doc = Document::from(html.as_str());
        let md = serialize_md(&doc.root(), false, None);
        assert_eq!(md.as_ref(), "`x`");
    }

    #[test]
    fn test_pre_with_trailing_newline() {
        // the content's final newline must not become an extra empty line
        let md = "```\na\nb\n```";
        html_2md_compare("<pre>a\nb\n</pre>", md);
    }

    #[test]
    fn test_emphasis_boundary_whitespace() {
        // CommonMark requires an opening delimiter to be followed, and a closing
        // delimiter to be preceded, by a non-whitespace character. Whitespace
        // staying inside the delimiter run renders as literal `**`/`*`.
        html_2md_compare(
            "<p><strong>The Rundown: </strong>Body text</p>",
            "**The Rundown:** Body text",
        );
        html_2md_compare(
            "<p><em> Leading</em> and <strong>trailing </strong></p>",
            "*Leading* and **trailing**",
        );
        // nested emphasis keeps working
        html_2md_compare(
            "<p><strong><em>both </em>bold</strong></p>",
            "***both* bold**",
        );
        // whitespace-only emphasis drops its delimiters instead of emitting an
        // unbalanced run
        html_2md_compare("<p>a<strong>   </strong>b</p>", "a b");
        html_2md_compare("Hello<strong> world</strong>.", "Hello **world**.");
        html_2md_compare("x<em> y</em>z", "x *y*z");
        html_2md_compare("a <em> x</em>", "a *x*");
        html_2md_compare("<p><strong> <em> x</em></strong></p>", "***x***");
        html_2md_compare("a<strong> <em> x</em></strong>", "a ***x***");
    }

    #[test]
    fn test_nested_same_type_emphasis() {
        // an element nested in one of the same type writes no delimiters:
        // `<i><i>b</i></i>` is one emphasis, not `**b**` (strong)
        html_2md_compare("<p><i><i>b</i></i></p>", "*b*");
        html_2md_compare("<p><b>a<span><b>b</b></span>c</b></p>", "**abc**");
        // a merge must not reopen the run with the inner element's opener,
        // which would close it right after `a`
        html_2md_compare("<p><em>a</em><em><i>b</i></em>c</p>", "*ab*c");

        html_2md_compare("<p><b>a</b><b><b>b</b> c</b></p>", "**ab c**");
        html_2md_compare("<p><b>a</b> <b> <b>b</b> c</b></p>", "**a b c**");
        // a different type nested inside still gets its own delimiters
        html_2md_compare("<p><b>a<i><b>b</b></i></b></p>", "**a*b***");
        html_2md_compare(r"<p><b>a*<i><b>b</b></i></b></p>", r"**a\**b***");
        html_2md_compare("<p><em>a</em><br><em>b</em></p>", "*a*  \n*b*");
        html_2md_compare(r"<p>a*b</p>", r"a\*b");

        html_2md_compare("<p><b>a</b><em>b</em></p>", "**a***b*");
        html_2md_compare("<p><b>a</b> <em>b</em></p>", "**a** *b*");
    }
}