excelerate 0.15.0

Read, write and recalculate spreadsheets: xlsx, xls, ods, csv, html, and a 518-function formula engine
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
//! Reading HTML.
//!
//! A page becomes one sheet: a `<table>` is a block of cells, `<tr>` a row,
//! `<td>` a cell, `colspan`/`rowspan` a merge. Outside a table the text still
//! lands on the sheet - a `<p>` or an `<h1>` fills one cell and moves the
//! cursor down a row - which is what a reader has to do and what makes a plain
//! article readable as a column of text.
//!
//! A full HTML parser would do, but that is a dependency we do
//! not have. The scanner below is the small part of one that a spreadsheet
//! needs: tags, attributes, entities, and the implied end tags (`<tr>` closes
//! an open `<td>`) without which real-world tables collapse into one cell.
//! Everything a browser needs and a sheet does not - the element categories,
//! foster parenting, character encoding declared halfway through the file - is
//! left out.
//!
//! Not ported: images (the model has no drawings yet), comments, document
//! properties from `<meta>`, and `data-printarea`, which is a defined name on
//! the workbook rather than anything the sheet holds.

use crate::error::{Error, Result};
use crate::model::{CellValue, ColumnRun, Hyperlink, LinkTarget, Spreadsheet, TextRun, Worksheet};
use crate::style::{
    Border, BorderStyle, Color, DiffFont, HorizontalAlign, NumberFormat, Pattern, Script, Style,
    StyleTable, Underline, VerticalAlign,
};
use crate::{CellError, CellRef, Col, Range, Row};
use std::collections::HashMap;

/// Reads a page from a file.
///
/// # Errors
/// [`Error::Html`] if the file cannot be read. Malformed markup is not an
/// error: a browser shows what it can, and so does this.
pub fn read_html(path: impl AsRef<std::path::Path>) -> Result<Spreadsheet> {
    let bytes = std::fs::read(path).map_err(|e| Error::Html(e.to_string()))?;
    Ok(read_html_str(&super::csv::decode(&bytes)))
}

/// Reads a page already in memory.
#[must_use]
pub fn read_html_str(html: &str) -> Spreadsheet {
    let mut build = Build::new();
    build.classes = stylesheet(html);
    build.children(&parse(html));
    build.finish()
}

// ---------------------------------------------------------------- the scanner

/// One node of the tree the scanner builds.
#[derive(Debug)]
enum Node {
    /// Character data, entities already resolved.
    Text(String),
    /// An element with its attributes and content.
    Elem(Elem),
}

/// An element. Names and attribute names are lower-cased.
#[derive(Debug, Default)]
struct Elem {
    name: String,
    attrs: Vec<(String, String)>,
    children: Vec<Node>,
}

impl Elem {
    /// One attribute by name.
    fn attr(&self, name: &str) -> Option<&str> {
        self.attrs
            .iter()
            .find(|(k, _)| k == name)
            .map(|(_, v)| v.as_str())
    }

    /// An attribute read as a number, ignoring any unit that follows it.
    fn number(&self, name: &str) -> Option<f64> {
        let value = self.attr(name)?;
        let digits: String = value
            .trim()
            .chars()
            .take_while(|c| c.is_ascii_digit() || *c == '.')
            .collect();
        digits.parse().ok()
    }

    /// A span attribute (`colspan`, `rowspan`) as a count of at least one.
    fn span(&self, name: &str) -> Option<u32> {
        #[expect(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        self.number(name).map(|n| (n.max(1.0) as u32).max(1))
    }
}

/// How deep the tree may nest. The walk over it recurses, and a page is free
/// to open ten thousand `<div>`s; past the cap an element keeps its attributes
/// but its content is read as the parent's.
const MAX_DEPTH: usize = 256;

/// Elements that never have content, so they are never left open.
const VOID: [&str; 14] = [
    "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source",
    "track", "wbr",
];

/// What an opening tag closes for us when the page left it open. This is the
/// slice of the HTML5 tree builder a table needs: without it `<tr><td>a<td>b`
/// nests the second cell inside the first.
fn implied_closes(name: &str) -> &'static [&'static str] {
    match name {
        "tr" => &["td", "th", "tr"],
        "td" | "th" => &["td", "th"],
        "li" => &["li"],
        "p" => &["p"],
        "tbody" | "thead" | "tfoot" => &["td", "th", "tr", "tbody", "thead", "tfoot"],
        _ => &[],
    }
}

/// Builds a tree from markup.
fn parse(html: &str) -> Vec<Node> {
    let mut root = Vec::new();
    let mut stack: Vec<Elem> = Vec::new();
    let mut rest = html;

    while !rest.is_empty() {
        let Some(lt) = rest.find('<') else {
            push_text(&mut root, &mut stack, rest);
            break;
        };
        if lt > 0 {
            push_text(&mut root, &mut stack, &rest[..lt]);
        }
        let tail = &rest[lt..];

        // Comments, doctypes and processing instructions carry nothing we want.
        if let Some(body) = tail.strip_prefix("<!--") {
            rest = body.find("-->").map_or("", |end| &body[end + 3..]);
            continue;
        }
        if tail.starts_with("<!") || tail.starts_with("<?") {
            rest = tail.find('>').map_or("", |end| &tail[end + 1..]);
            continue;
        }
        if let Some(body) = tail.strip_prefix("</") {
            let end = body.find('>').unwrap_or(body.len());
            let name = body[..end].trim().to_ascii_lowercase();
            close(&mut root, &mut stack, &name);
            rest = body.get(end + 1..).unwrap_or("");
            continue;
        }

        let after = &tail[1..];
        if !after.starts_with(|c: char| c.is_ascii_alphabetic()) {
            // A stray `<` is text, the way a browser reads it.
            push_text(&mut root, &mut stack, "<");
            rest = after;
            continue;
        }
        let (elem, self_closing, tail) = open_tag(after);

        // Script and style hold text that is not markup; skipping to the end
        // tag keeps a stylesheet out of the cells.
        if elem.name == "script" || elem.name == "style" {
            let end = format!("</{}", elem.name);
            rest = match tail.to_ascii_lowercase().find(&end) {
                Some(at) => tail[at..].find('>').map_or("", |gt| &tail[at + gt + 1..]),
                None => "",
            };
            continue;
        }

        for closing in implied_closes(&elem.name) {
            if stack.iter().any(|e| &e.name == closing) {
                close(&mut root, &mut stack, closing);
            }
        }
        if self_closing || VOID.contains(&elem.name.as_str()) || stack.len() >= MAX_DEPTH {
            push_node(&mut root, &mut stack, Node::Elem(elem));
        } else {
            stack.push(elem);
        }
        rest = tail;
    }

    // Whatever the page left open is closed at the end of it.
    while let Some(elem) = stack.pop() {
        push_node(&mut root, &mut stack, Node::Elem(elem));
    }
    root
}

/// Reads a tag after its `<`, returning the element, whether it closed itself,
/// and what follows it.
fn open_tag(after: &str) -> (Elem, bool, &str) {
    let end = after
        .find(|c: char| c.is_ascii_whitespace() || c == '>' || c == '/')
        .unwrap_or(after.len());
    let mut elem = Elem {
        name: after[..end].to_ascii_lowercase(),
        ..Elem::default()
    };
    let mut rest = &after[end..];
    let mut self_closing = false;

    loop {
        rest = rest.trim_start_matches(char::is_whitespace);
        match rest.as_bytes().first() {
            None => break,
            Some(b'>') => {
                rest = &rest[1..];
                break;
            }
            Some(b'/') => {
                self_closing = true;
                rest = &rest[1..];
                continue;
            }
            Some(_) => {}
        }
        let end = rest
            .find(|c: char| c.is_ascii_whitespace() || c == '=' || c == '>')
            .unwrap_or(rest.len());
        let name = rest[..end].to_ascii_lowercase();
        rest = rest[end..].trim_start_matches(char::is_whitespace);
        let mut value = String::new();
        if let Some(tail) = rest.strip_prefix('=') {
            let tail = tail.trim_start_matches(char::is_whitespace);
            let (raw, tail) = if let Some(&quote @ (b'"' | b'\'')) = tail.as_bytes().first() {
                let body = &tail[1..];
                let end = body.find(char::from(quote)).unwrap_or(body.len());
                (&body[..end], body.get(end + 1..).unwrap_or(""))
            } else {
                let end = tail
                    .find(|c: char| c.is_ascii_whitespace() || c == '>')
                    .unwrap_or(tail.len());
                (&tail[..end], &tail[end..])
            };
            value = unescape(raw);
            rest = tail;
        }
        if !name.is_empty() {
            elem.attrs.push((name, value));
        }
    }
    (elem, self_closing, rest)
}

/// Closes the innermost element of a name, and everything the page left open
/// inside it.
fn close(root: &mut Vec<Node>, stack: &mut Vec<Elem>, name: &str) {
    if !stack.iter().any(|e| e.name == name) {
        // An end tag with no start tag is noise; a browser drops it too.
        return;
    }
    while let Some(elem) = stack.pop() {
        let done = elem.name == name;
        push_node(root, stack, Node::Elem(elem));
        if done {
            return;
        }
    }
}

/// Adds a node to whatever is open, or to the document.
fn push_node(root: &mut Vec<Node>, stack: &mut [Elem], node: Node) {
    match stack.last_mut() {
        Some(parent) => parent.children.push(node),
        None => root.push(node),
    }
}

/// Adds character data, resolving its entities.
fn push_text(root: &mut Vec<Node>, stack: &mut [Elem], text: &str) {
    push_node(root, stack, Node::Text(unescape(text)));
}

/// Resolves character references. An unknown name is left as it was written:
/// the page means those characters, and inventing a replacement is worse than
/// showing the source.
fn unescape(text: &str) -> String {
    if !text.contains('&') {
        return text.to_owned();
    }
    let mut out = String::with_capacity(text.len());
    let mut rest = text;
    while let Some(amp) = rest.find('&') {
        out.push_str(&rest[..amp]);
        let body = &rest[amp + 1..];
        let end = body.find(';').filter(|&end| end <= 8);
        if let Some((c, end)) = end.and_then(|end| entity(&body[..end]).map(|c| (c, end))) {
            out.push(c);
            rest = &body[end + 1..];
        } else {
            out.push('&');
            rest = body;
        }
    }
    out.push_str(rest);
    out
}

/// One character reference by name or number.
fn entity(name: &str) -> Option<char> {
    match name {
        "amp" => return Some('&'),
        "lt" => return Some('<'),
        "gt" => return Some('>'),
        "quot" => return Some('"'),
        "apos" | "#39" => return Some('\''),
        "nbsp" => return Some('\u{a0}'),
        _ => {}
    }
    let digits = name.strip_prefix('#')?;
    let code = match digits.strip_prefix(['x', 'X']) {
        Some(hex) => u32::from_str_radix(hex, 16).ok()?,
        None => digits.parse().ok()?,
    };
    char::from_u32(code)
}

/// The rules of every `<style>` block that name a class of cells: `.x`,
/// `td.x`, `th.x`, as our writer and Excel's export write them, and of
/// columns and rows, `col.x` and `tr.x`, as `PhpSpreadsheet` sizes them. Descendant
/// selectors count by their last part; anything fancier is ignored.
// ponytail: no specificity or cascade order; rules of one class apply in page order.
fn stylesheet(html: &str) -> HashMap<String, String> {
    let lower = html.to_ascii_lowercase();
    let mut classes: HashMap<String, String> = HashMap::new();
    let mut from = 0;
    while let Some(open) = lower[from..].find("<style").map(|at| from + at) {
        let Some(body) = lower[open..].find('>').map(|gt| open + gt + 1) else {
            break;
        };
        let end = lower[body..]
            .find("</style")
            .map_or(html.len(), |at| body + at);
        let mut sheet = html[body..end].to_owned();
        while let Some(start) = sheet.find("/*") {
            let stop = sheet[start..]
                .find("*/")
                .map_or(sheet.len(), |at| start + at + 2);
            sheet.replace_range(start..stop, "");
        }
        for rule in sheet.split('}') {
            let Some((selectors, declarations)) = rule.split_once('{') else {
                continue;
            };
            for selector in selectors.split(',') {
                let last = selector.split_whitespace().last().unwrap_or_default();
                let Some((tag, class)) = last.split_once('.') else {
                    continue;
                };
                if matches!(
                    tag.to_ascii_lowercase().as_str(),
                    "" | "td" | "th" | "col" | "tr"
                ) && !class.is_empty()
                {
                    let entry = classes.entry(class.to_owned()).or_default();
                    entry.push_str(declarations);
                    entry.push(';');
                }
            }
        }
        from = end;
    }
    classes
}

// ------------------------------------------------------------------ the walk

/// The cursor the walk carries: the state threaded through the
/// `processDomElement*` chain, which is one method per element name written as
/// a fall-through.
struct Build {
    sheet: Worksheet,
    styles: StyleTable,
    /// One-based, like the sheet. Starting at row 0 and letting `<body>`
    /// set it to 1; `DOMDocument` always supplies a `<body>`, our scanner does
    /// not, so the cursor starts on the first row instead.
    row: u32,
    col: u32,
    /// Text collected since the last flush, the content of the cell to come.
    content: String,
    /// The same text in runs, once an inline element inside a cell changes
    /// the font; empty until then, so a plain cell pays nothing.
    runs: Vec<TextRun>,
    /// The fonts of the inline elements open around the text, innermost
    /// last, each already laid over the one outside it.
    fonts: Vec<DiffFont>,
    table_level: u32,
    /// The column each open table starts at, `nestedColumn` in the HTML reader's own terms.
    nested_column: Vec<u32>,
    /// What a `rowspan` already covers, carried from the row above. Kept as
    /// ranges rather than as a set of addresses: a page is free to claim a span
    /// of a million rows, and the set would then hold a million entries.
    spanned: Vec<Range>,
    /// Which column the next `<col>` describes.
    current_column: u32,
    /// Declarations of the page's `<style>` rules, by class name.
    classes: HashMap<String, String>,
}

/// How many cells one `style=` attribute may be spread over. A `rowspan` is a
/// number out of the file, and a merge of a million rows would otherwise mean a
/// million cells created to hold one background colour.
// ponytail: fixed cap; a per-cell style on a huge merge is not worth the memory.
const MAX_STYLED_SPAN: u64 = 4096;

impl Build {
    fn new() -> Self {
        Self {
            sheet: Worksheet::new("Worksheet").unwrap_or_default(),
            styles: StyleTable::default(),
            row: 1,
            col: 1,
            content: String::new(),
            runs: Vec::new(),
            fonts: Vec::new(),
            table_level: 0,
            nested_column: vec![1],
            spanned: Vec::new(),
            current_column: 1,
            classes: HashMap::new(),
        }
    }

    /// The sheet as a workbook of its own.
    fn finish(self) -> Spreadsheet {
        let mut book = Spreadsheet::empty();
        book.styles = self.styles;
        let _ = book.add_sheet(self.sheet);
        book
    }

    /// The cell the cursor is on, `None` once it has run past the sheet.
    fn at(&self) -> Option<CellRef> {
        Some(CellRef::new(
            Col::from_one_based(u64::from(self.col)).ok()?,
            Row::from_one_based(u64::from(self.row)).ok()?,
        ))
    }

    fn children(&mut self, nodes: &[Node]) {
        for node in nodes {
            match node {
                Node::Text(text) => {
                    let text = collapse(text);
                    // A cell holding one non-breaking space is an empty cell:
                    // that is how a page spells "nothing here".
                    if text.trim() != "\u{a0}" {
                        self.push_content(&text);
                    }
                }
                Node::Elem(elem) => self.element(elem),
            }
        }
    }

    /// Text of the cell to come: whole, and in runs by the font of the
    /// inline elements around it. Whitespace collapses across elements, as a
    /// browser lays it out: a space after a space, or at the start, is dropped.
    fn push_content(&mut self, text: &str) {
        let text = if self.content.is_empty() || self.content.ends_with([' ', '\n']) {
            text.trim_start_matches(' ')
        } else {
            text
        };
        if text.is_empty() {
            return;
        }
        let font = self.fonts.last().cloned();
        if self.runs.is_empty() && font.is_some() && !self.content.is_empty() {
            self.runs.push(TextRun {
                text: self.content.clone(),
                font: None,
            });
        }
        self.content.push_str(text);
        if self.runs.is_empty() && font.is_none() {
            return;
        }
        match self.runs.last_mut() {
            Some(run) if run.font == font => run.text.push_str(text),
            _ => self.runs.push(TextRun {
                text: text.to_owned(),
                font,
            }),
        }
    }

    /// An inline element inside a cell: its text is a run in the font it
    /// sets, laid over the font of the elements around it.
    fn inline(&mut self, e: &Elem) {
        let mut font = self.fonts.last().cloned().unwrap_or_default();
        inline_font(&e.name, &self.declarations(e), e, &mut font);
        self.fonts.push(font);
        self.children(&e.children);
        self.fonts.pop();
    }

    /// One element.
    fn element(&mut self, e: &Elem) {
        match e.name.as_str() {
            "body" => {
                self.row = 1;
                self.col = 1;
                self.content.clear();
                self.table_level = 0;
                self.children(&e.children);
            }
            "title" => {
                self.children(&e.children);
                let title = std::mem::take(&mut self.content);
                // A title too long, or holding a character Excel bans, leaves
                // the sheet named as it was.
                let _ = self.sheet.set_title(title);
            }
            "span" | "font" | "i" | "em" | "strong" | "b" | "u" | "ins" | "s" | "strike"
            | "del" | "sup" | "sub"
                if self.table_level > 0 =>
            {
                self.inline(e);
            }
            "span" | "div" | "font" | "i" | "em" | "strong" | "b" => {
                self.children(&e.children);
                self.tag_format(&e.name);
            }
            "hr" => {
                self.flush_cell(Some(e));
                self.row += 1;
                self.tag_format("hr");
                self.row += 1;
                self.line_break(e);
            }
            "br" => self.line_break(e),
            "a" => {
                if let Some(href) = e.attr("href") {
                    self.link(href);
                }
                self.children(&e.children);
            }
            "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "ol" | "ul" | "p" => self.block(e),
            "li" => self.list_item(e),
            "table" => self.table(e),
            "col" => {
                self.style_attribute(e, None, self.current_column, None);
                self.current_column += 1;
            }
            "tr" => self.table_row(e),
            "td" | "th" => self.table_cell(e),
            // An image wants a drawing, which the model does not have yet.
            "img" => {}
            _ => self.children(&e.children),
        }
    }

    fn line_break(&mut self, e: &Elem) {
        if self.table_level > 0 {
            self.push_content("\n");
            self.restyle_current(|style| style.alignment.wrap_text = true);
        } else {
            self.flush_cell(Some(e));
            self.row += 1;
        }
    }

    fn block(&mut self, e: &Elem) {
        if self.table_level > 0 {
            if !self.content.is_empty() {
                self.content.push('\n');
            }
            self.restyle_current(|style| style.alignment.wrap_text = true);
            self.children(&e.children);
            return;
        }
        if !self.content.is_empty() {
            self.flush_cell(Some(e));
            self.row += 1;
        }
        self.children(&e.children);
        self.flush_cell(Some(e));
        self.tag_format(&e.name);
        self.row += 1;
        self.col = 1;
    }

    fn list_item(&mut self, e: &Elem) {
        if self.table_level > 0 {
            if !self.content.is_empty() {
                self.content.push('\n');
            }
            self.children(&e.children);
            return;
        }
        if !self.content.is_empty() {
            self.flush_cell(Some(e));
        }
        self.row += 1;
        self.children(&e.children);
        self.flush_cell(Some(e));
        self.col = 1;
    }

    fn table(&mut self, e: &Elem) {
        if let Some(class) = e.attr("class") {
            let classes: Vec<&str> = class.split_whitespace().collect();
            self.sheet.view.show_grid_lines = classes.contains(&"gridlines");
            self.sheet.print_options.grid_lines = classes.contains(&"gridlinesp");
        }
        if e.attr("dir") == Some("rtl") {
            self.sheet.view.right_to_left = true;
        }
        self.current_column = 1;
        self.flush_cell(Some(e));

        // setTableStartColumn: a table at the top level always starts at A.
        if self.table_level == 0 {
            self.col = 1;
        }
        self.table_level += 1;
        self.nested_column.push(self.col);
        if self.table_level > 1 && self.row > 1 {
            self.row -= 1;
        }

        self.children(&e.children);

        self.table_level = self.table_level.saturating_sub(1);
        self.col = self.nested_column.pop().unwrap_or(1);
        // The other branch increments a copy of the column and throws it
        // away, so closing a table only ever moves the cursor down a row.
        if self.table_level <= 1 {
            self.row += 1;
        }
    }

    fn table_row(&mut self, e: &Elem) {
        self.col = self
            .nested_column
            .get(self.table_level as usize)
            .copied()
            .unwrap_or(1);
        self.content.clear();
        self.children(&e.children);
        // Only the height of a row's CSS is kept: the rest styles cells.
        let height = self
            .declarations(e)
            .split(';')
            .rev()
            .find_map(|declaration| {
                let (name, value) = declaration.split_once(':')?;
                (name.trim().eq_ignore_ascii_case("height"))
                    .then(|| css_height(value.trim()))
                    .flatten()
            });
        if let Some(height) = height {
            self.set_row_height(self.row, height);
        }
        if let Some(height) = e.number("height") {
            self.set_row_height(self.row, height);
        }
        self.row += 1;
    }

    fn table_cell(&mut self, e: &Elem) {
        while let Some(at) = self.at() {
            if !self.spanned.iter().any(|range| range.contains(at)) {
                break;
            }
            self.col += 1;
        }
        self.children(&e.children);

        let merge = self.merge_range(e);
        self.style_attribute(e, merge, self.col, Some(self.row));
        self.flush_cell(Some(e));

        if let Some(colour) = e.attr("bgcolor").and_then(colour) {
            self.restyle_current(|style| {
                style.fill.pattern = Pattern::Solid;
                style.fill.foreground = colour;
            });
        }
        if let Some(width) = e.attr("width").and_then(css_width) {
            self.set_column_width(self.col, width);
        }
        if let Some(height) = e.attr("height").and_then(css_height) {
            self.set_row_height(self.row, height);
        }
        if let Some(align) = e.attr("align").map(HorizontalAlign::parse) {
            self.restyle_current(|style| style.alignment.horizontal = align);
        }
        if let Some(align) = e.attr("valign").map(vertical) {
            self.restyle_current(|style| style.alignment.vertical = align);
        }

        if let Some(range) = merge {
            if e.attr("rowspan").is_some() {
                self.spanned.push(range);
            }
            self.sheet.merges.push(range);
            self.col = range.end.col.one_based();
        }
        self.col += 1;
    }

    /// The range a cell's spans cover, `None` when it spans nothing and `None`
    /// again when the spans run off the sheet.
    fn merge_range(&self, e: &Elem) -> Option<Range> {
        let cols = e.span("colspan").unwrap_or(1);
        let rows = e.span("rowspan").unwrap_or(1);
        if cols == 1 && rows == 1 {
            return None;
        }
        let start = self.at()?;
        let end = CellRef::new(
            Col::from_one_based(u64::from(self.col) + u64::from(cols) - 1).ok()?,
            Row::from_one_based(u64::from(self.row) + u64::from(rows) - 1).ok()?,
        );
        Some(Range::new(start, end))
    }

    fn flush_cell(&mut self, e: Option<&Elem>) {
        let content = std::mem::take(&mut self.content);
        let mut runs = std::mem::take(&mut self.runs);
        if content.trim().is_empty() {
            return;
        }
        let Some(at) = self.at() else { return };
        let value = value_of(content.trim_end_matches(' '), e);
        if !matches!(value, CellValue::Text(_)) || runs.is_empty() {
            self.sheet.entry(at).value = value;
            return;
        }
        if let Some(last) = runs.last_mut() {
            let kept = last.text.trim_end_matches(' ').len();
            last.text.truncate(kept);
        }
        runs.retain(|r| !r.text.is_empty());
        match runs.as_slice() {
            // One font over the whole text is the cell's font, which is how
            // `<td><b>total</b></td>` has always read.
            [
                TextRun {
                    font: Some(font), ..
                },
            ] => {
                let font = font.clone();
                self.sheet.entry(at).value = value;
                self.restyle_current(|style| style.font = style.font.with(&font));
            }
            _ => self.sheet.entry(at).value = CellValue::RichText(runs),
        }
    }

    /// The hyperlink of an `<a href>`, plus the blue underline browsers give
    /// it from its `FORMATS` table.
    fn link(&mut self, href: &str) {
        let Some(at) = self.at() else { return };
        self.sheet.hyperlinks.push(Hyperlink {
            range: Range::new(at, at),
            target: match href.strip_prefix('#') {
                Some(place) => LinkTarget::Inside(place.to_owned()),
                None => LinkTarget::Outside(href.to_owned()),
            },
            display: None,
            tooltip: None,
        });
        self.tag_format("a");
    }

    fn tag_format(&mut self, name: &str) {
        let points = match name {
            "h1" => Some(24.0),
            "h2" => Some(18.0),
            "h3" => Some(13.5),
            "h4" => Some(12.0),
            "h5" => Some(10.0),
            "h6" => Some(7.5),
            _ => None,
        };
        self.restyle_current(|style| match name {
            "b" | "strong" => style.font.bold = true,
            "i" | "em" => style.font.italic = true,
            "a" => {
                style.font.underline = Underline::Single;
                style.font.color = Color::Argb(0xFF00_00FF);
            }
            "hr" => {
                style.borders.bottom = Border {
                    style: BorderStyle::parse("thin"),
                    color: Color::Argb(0xFF00_0000),
                };
            }
            _ => {
                if let Some(points) = points {
                    style.font.bold = true;
                    style.font.set_size_points(points);
                }
            }
        });
    }

    /// The CSS that applies to an element: its classes' rules from the page's
    /// stylesheet, then its own `style=`, which wins by coming last.
    fn declarations(&self, e: &Elem) -> String {
        let mut css: String = e
            .attr("class")
            .into_iter()
            .flat_map(str::split_whitespace)
            .filter_map(|class| self.classes.get(class))
            .fold(String::new(), |all, rule| all + rule + ";");
        css.push_str(e.attr("style").unwrap_or_default());
        css
    }

    /// `target` is the range a merged cell covers; `row` is `None` for a
    /// `<col>`, which carries a width and nothing a cell could hold.
    fn style_attribute(&mut self, e: &Elem, target: Option<Range>, col: u32, row: Option<u32>) {
        let css = self.declarations(e);
        let format = e.attr("data-format").filter(|_| row.is_some());
        if css.is_empty() && format.is_none() {
            return;
        }
        let range = match (target, row) {
            (Some(range), _) => Some(range),
            (None, Some(_)) => self.at().map(|at| Range::new(at, at)),
            (None, None) => None,
        };
        let mut style = range
            .and_then(|range| self.sheet.get(range.start))
            .map(|cell| cell.style)
            .and_then(|id| self.styles.get(id).cloned())
            .unwrap_or_default();

        for declaration in css.split(';') {
            let Some((name, value)) = declaration.split_once(':') else {
                continue;
            };
            let (name, value) = (name.trim().to_ascii_lowercase(), value.trim());
            match name.as_str() {
                "background" | "background-color" => {
                    if let Some(colour) = colour(value) {
                        style.fill.pattern = Pattern::Solid;
                        style.fill.foreground = colour;
                    }
                }
                "color" => {
                    if let Some(colour) = colour(value) {
                        style.font.color = colour;
                    }
                }
                "border" => set_border(&mut style, value, &["top", "right", "bottom", "left"]),
                "border-top" => set_border(&mut style, value, &["top"]),
                "border-right" => set_border(&mut style, value, &["right"]),
                "border-bottom" => set_border(&mut style, value, &["bottom"]),
                "border-left" => set_border(&mut style, value, &["left"]),
                "font-size" => {
                    if let Some((size, _)) = length(value) {
                        style.font.set_size_points(size);
                    }
                }
                "font-weight" => {
                    style.font.bold |=
                        value == "bold" || length(value).is_some_and(|(weight, _)| weight >= 500.0);
                }
                "font-style" => style.font.italic |= value == "italic",
                "font-family" => style.font.name = value.replace(['\'', '"'], ""),
                "text-decoration" => match value {
                    "underline" => style.font.underline = Underline::Single,
                    "line-through" => style.font.strike = true,
                    _ => {}
                },
                "text-align" => style.alignment.horizontal = HorizontalAlign::parse(value),
                "vertical-align" => style.alignment.vertical = vertical(value),
                "word-wrap" | "overflow-wrap" => {
                    style.alignment.wrap_text = value == "break-word" || value == "anywhere";
                }
                // Our writer says `pre-wrap`, Excel's own export `normal`; both
                // are a cell that wraps, and `nowrap` or `pre` one that does not.
                "white-space" => {
                    style.alignment.wrap_text = matches!(value, "normal" | "pre-wrap" | "pre-line");
                }
                "text-indent" => {
                    if let Some(indent) = css_pixels(value) {
                        #[expect(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
                        {
                            style.alignment.indent = (indent.max(0.0) / 9.0) as u32;
                        }
                    }
                }
                "width" => {
                    if let Some(width) = css_width(value) {
                        self.set_column_width(col, width);
                    }
                }
                "height" => {
                    if let (Some(row), Some(height)) = (row, css_height(value)) {
                        self.set_row_height(row, height);
                    }
                }
                // `direction` has no counterpart: the model keeps the reading
                // order of a sheet, not of a cell.
                _ => {}
            }
        }

        if let Some(code) = format {
            style.number_format = NumberFormat::Custom(code.to_owned());
        }

        let Some(range) = range.filter(|_| row.is_some()) else {
            return;
        };
        let cells = u64::from(range.width()) * u64::from(range.height());
        let range = if cells > MAX_STYLED_SPAN {
            Range::new(range.start, range.start)
        } else {
            range
        };
        // A merge is one `<td>`, so every cell under it gets the block's style;
        // what a covered cell had inside the block is not drawn and not on the
        // page.
        let id = self.styles.intern(style);
        for at in range.cells() {
            self.sheet.entry(at).style = id;
        }
    }

    /// Rewrites the style of the cell under the cursor.
    fn restyle_current(&mut self, apply: impl FnOnce(&mut Style)) {
        let Some(at) = self.at() else { return };
        let id = self.sheet.entry(at).style;
        let mut style = self.styles.get(id).cloned().unwrap_or_default();
        apply(&mut style);
        let id = self.styles.intern(style);
        self.sheet.entry(at).style = id;
    }

    /// Sets one column's width, in the character units xlsx counts in.
    fn set_column_width(&mut self, col: u32, width: f64) {
        let Ok(col) = Col::from_one_based(u64::from(col)) else {
            return;
        };
        if !self
            .sheet
            .columns
            .iter()
            .any(|run| run.first == col && run.last == col)
        {
            self.sheet.columns.push(ColumnRun::new(col, col));
        }
        if let Some(run) = self
            .sheet
            .columns
            .iter_mut()
            .find(|run| run.first == col && run.last == col)
        {
            run.width = Some(width);
            run.custom_width = true;
        }
    }

    /// Sets one row's height, in points.
    fn set_row_height(&mut self, row: u32, height: f64) {
        let Ok(row) = Row::from_one_based(u64::from(row)) else {
            return;
        };
        let properties = self.sheet.rows.entry(row).or_default();
        properties.height = Some(height);
        properties.custom_height = true;
    }
}

/// Collapses runs of whitespace the way a browser lays text out, so the markup's
/// own indentation does not reach the cell. A run at either end becomes one
/// space rather than nothing: `a <b>b</b>` is two words, and the cell trims
/// what is left over at its own edges.
fn collapse(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut space = false;
    for c in text.chars() {
        if c.is_ascii_whitespace() {
            space = true;
            continue;
        }
        if space {
            out.push(' ');
            space = false;
        }
        out.push(c);
    }
    if space {
        out.push(' ');
    }
    out
}

/// What an inline element sets about the font, laid over `font`: the tag's
/// own meaning, the attributes of `<font>`, then its CSS.
fn inline_font(name: &str, css: &str, e: &Elem, font: &mut DiffFont) {
    match name {
        "b" | "strong" => font.bold = Some(true),
        "i" | "em" => font.italic = Some(true),
        "u" | "ins" => font.underline = Some(Underline::Single),
        "s" | "strike" | "del" => font.strike = Some(true),
        "sup" => font.script = Some(Script::Superscript),
        "sub" => font.script = Some(Script::Subscript),
        "font" => {
            if let Some(face) = e.attr("face") {
                font.name = face
                    .split(',')
                    .next()
                    .map(|f| f.trim().replace(['\'', '"'], ""));
            }
            if let Some(c) = e.attr("color").and_then(colour) {
                font.color = Some(c);
            }
        }
        _ => {}
    }
    for declaration in css.split(';') {
        let Some((key, value)) = declaration.split_once(':') else {
            continue;
        };
        let (key, value) = (key.trim().to_ascii_lowercase(), value.trim());
        match key.as_str() {
            "font-weight" => {
                font.bold = Some(
                    value == "bold" || length(value).is_some_and(|(weight, _)| weight >= 500.0),
                );
            }
            "font-style" => font.italic = Some(value == "italic" || value == "oblique"),
            "font-family" => {
                font.name = value
                    .split(',')
                    .next()
                    .map(|f| f.trim().replace(['\'', '"'], ""));
            }
            "font-size" => {
                if let Some((points, _)) = length(value).filter(|(p, _)| *p > 0.0 && *p < 4096.0) {
                    #[expect(
                        clippy::cast_possible_truncation,
                        clippy::cast_sign_loss,
                        reason = "bounded to a positive number of points above"
                    )]
                    let hundredths = (points * 100.0).round() as u32;
                    font.size = Some(hundredths);
                }
            }
            "color" => {
                if let Some(c) = colour(value) {
                    font.color = Some(c);
                }
            }
            "text-decoration" | "text-decoration-line" => {
                font.underline = Some(if value.contains("underline") {
                    Underline::Single
                } else {
                    Underline::None
                });
                font.strike = Some(value.contains("line-through"));
            }
            "vertical-align" => {
                font.script = match value {
                    "super" => Some(Script::Superscript),
                    "sub" => Some(Script::Subscript),
                    "baseline" => Some(Script::Baseline),
                    _ => font.script,
                };
            }
            _ => {}
        }
    }
}

/// What a cell's text means.
///
/// The `data-` attributes are how `Writer/Html` records the type it knew, so a
/// page it wrote reads back as numbers and formulas rather than as text.
/// Without them the text is read the way a CSV field is, which is the same
/// value binder a reader falls back on.
fn value_of(content: &str, e: Option<&Elem>) -> CellValue {
    let attr = |name: &str| e.and_then(|e| e.attr(name));
    if let Some(formula) = attr("data-formula") {
        return CellValue::Formula {
            formula: formula.trim_start_matches('=').to_owned(),
            cached: Some(Box::new(typed_value(content, e))),
        };
    }
    typed_value(content, e)
}

/// The value a cell's `data-type` and `data-value` name, or its text read
/// the way CSV reads a field.
fn typed_value(content: &str, e: Option<&Elem>) -> CellValue {
    let attr = |name: &str| e.and_then(|e| e.attr(name));
    let text = attr("data-value").unwrap_or(content);
    match attr("data-type") {
        Some("s" | "str" | "inlineStr") => CellValue::text(text),
        Some("n") => text
            .trim()
            .parse()
            .map_or_else(|_| CellValue::text(text), CellValue::Number),
        Some("b") => CellValue::Bool(!matches!(
            text.trim().to_ascii_lowercase().as_str(),
            "" | "0" | "false"
        )),
        Some("e") => {
            CellError::parse(text.trim()).map_or_else(|| CellValue::text(text), CellValue::Error)
        }
        _ => super::csv::value_of(text),
    }
}

/// Vertical placement. HTML's word for the middle is `middle`, Excel's is
/// `center`; the rest of the values the two spell the same.
fn vertical(value: &str) -> VerticalAlign {
    match value {
        "middle" => VerticalAlign::Center,
        other => VerticalAlign::parse(other),
    }
}

fn set_border(style: &mut Style, value: &str, sides: &[&str]) {
    let words: Vec<&str> = value.split_whitespace().collect();
    let (line, colour) = if value.trim() == "none" {
        ("none", None)
    } else if words.len() >= 3 {
        (words[1], Some(words[2]))
    } else {
        (*words.first().unwrap_or(&"none"), words.get(1).copied())
    };
    let border = Border {
        style: border_style(line),
        color: colour.and_then(self::colour).unwrap_or_default(),
    };
    for side in sides {
        match *side {
            "top" => style.borders.top = border.clone(),
            "right" => style.borders.right = border.clone(),
            "bottom" => style.borders.bottom = border.clone(),
            _ => style.borders.left = border.clone(),
        }
    }
}

fn border_style(name: &str) -> BorderStyle {
    BorderStyle::parse(match name {
        "solid" => "thin",
        "dash-dot" => "dashDot",
        "dash-dot-dot" => "dashDotDot",
        "medium-dashed" => "mediumDashed",
        "medium-dash-dot" => "mediumDashDot",
        "medium-dash-dot-dot" => "mediumDashDotDot",
        "slant-dash-dot" => "slantDashDot",
        other => other,
    })
}

/// A CSS colour: `#rgb`, `#rrggbb` or one of the names.
fn colour(value: &str) -> Option<Color> {
    let value = value.trim();
    if let Some(hex) = value.strip_prefix('#') {
        let hex: String = match hex.len() {
            // `#abc` is `#aabbcc`.
            3 => hex.chars().flat_map(|c| [c, c]).collect(),
            6 => hex.to_owned(),
            _ => return None,
        };
        return Color::from_argb_str(&format!("FF{hex}"));
    }
    let name = value.to_ascii_lowercase();
    COLOUR_NAMES
        .iter()
        .find(|(known, _)| *known == name)
        .map(|(_, rgb)| Color::Argb(0xFF00_0000 | rgb))
}

/// A CSS length as its number and, when it named an absolute unit, that length
/// in pixels at the 96 dpi the format assumes.
fn length(value: &str) -> Option<(f64, Option<f64>)> {
    let value = value.trim();
    let end = value
        .find(|c: char| !c.is_ascii_digit() && c != '.')
        .unwrap_or(value.len());
    let size: f64 = value[..end].parse().ok()?;
    if !size.is_finite() {
        return None;
    }
    let unit = value[end..].trim().to_ascii_lowercase();
    let per_unit = match unit.as_str() {
        "" => return Some((size, None)),
        "px" => 1.0,
        "cm" => 96.0 / 2.54,
        "mm" => 96.0 / 25.4,
        "in" => 96.0,
        "pt" => 96.0 / 72.0,
        "pc" => 96.0 * 12.0 / 72.0,
        _ => return None,
    };
    Some((size, Some(size * per_unit)))
}

/// A CSS length in pixels, whatever unit it was written in.
fn css_pixels(value: &str) -> Option<f64> {
    let (size, pixels) = length(value)?;
    Some(pixels.unwrap_or(size))
}

/// A CSS length as a column width. A number with no unit is already in the
/// character units xlsx counts in, which is what `Dimension::width` returns.
fn css_width(value: &str) -> Option<f64> {
    let (size, pixels) = length(value)?;
    // The inverse of `Drawing::pixelsToCellDimension` for Calibri 11, the font
    // measures with here.
    Some(pixels.map_or(size, |pixels| pixels * 9.140_625 / 64.0))
}

/// A CSS length as a row height, in points.
fn css_height(value: &str) -> Option<f64> {
    let (size, pixels) = length(value)?;
    Some(pixels.map_or(size, |pixels| pixels * 0.75))
}

/// The CSS colour names. The X11 numbered
/// variants (`antiquewhite3`) are left out: no page writes them.
const COLOUR_NAMES: [(&str, u32); 112] = [
    ("aliceblue", 0xf0_f8ff),
    ("antiquewhite", 0xfa_ebd7),
    ("aqua", 0x00_ffff),
    ("beige", 0xf5_f5dc),
    ("black", 0x00_0000),
    ("blanchedalmond", 0xff_ebcd),
    ("blue", 0x00_00ff),
    ("blueviolet", 0x8a_2be2),
    ("brown", 0xa5_2a2a),
    ("burlywood", 0xde_b887),
    ("cadetblue", 0x5f_9ea0),
    ("chocolate", 0xd2_691e),
    ("coral", 0xff_7f50),
    ("cornflowerblue", 0x64_95ed),
    ("darkgoldenrod", 0xb8_860b),
    ("darkgreen", 0x00_6400),
    ("darkkhaki", 0xbd_b76b),
    ("darkolivegreen", 0x55_6b2f),
    ("darkorange", 0xff_8c00),
    ("darkorchid", 0x99_32cc),
    ("darksalmon", 0xe9_967a),
    ("darkseagreen", 0x8f_bc8f),
    ("darkslateblue", 0x48_3d8b),
    ("darkslategray", 0x2f_4f4f),
    ("darkturquoise", 0x00_ced1),
    ("darkviolet", 0x94_00d3),
    ("dimgray", 0x69_6969),
    ("firebrick", 0xb2_2222),
    ("floralwhite", 0xff_faf0),
    ("forestgreen", 0x22_8b22),
    ("fuchsia", 0xff_00ff),
    ("gainsboro", 0xdc_dcdc),
    ("ghostwhite", 0xf8_f8ff),
    ("goldenrod", 0xda_a520),
    ("gray", 0xbe_bebe),
    ("green", 0x00_ff00),
    ("greenyellow", 0xad_ff2f),
    ("hotpink", 0xff_69b4),
    ("indianred", 0xcd_5c5c),
    ("khaki", 0xf0_e68c),
    ("lavender", 0xe6_e6fa),
    ("lawngreen", 0x7c_fc00),
    ("light", 0xee_dd82),
    ("lightblue", 0xad_d8e6),
    ("lightcoral", 0xf0_8080),
    ("lightgoldenrodyellow", 0xfa_fad2),
    ("lightgray", 0xd3_d3d3),
    ("lightpink", 0xff_b6c1),
    ("lightseagreen", 0x20_b2aa),
    ("lightskyblue", 0x87_cefa),
    ("lightslateblue", 0x84_70ff),
    ("lightslategray", 0x77_8899),
    ("lightsteelblue", 0xb0_c4de),
    ("lime", 0x00_ff00),
    ("limegreen", 0x32_cd32),
    ("linen", 0xfa_f0e6),
    ("magenta", 0xff_00ff),
    ("maroon", 0xb0_3060),
    ("medium", 0x66_cdaa),
    ("mediumaquamarine", 0x66_cdaa),
    ("mediumblue", 0x00_00cd),
    ("mediumorchid", 0xba_55d3),
    ("mediumpurple", 0x93_70db),
    ("mediumseagreen", 0x3c_b371),
    ("mediumslateblue", 0x7b_68ee),
    ("mediumspringgreen", 0x00_fa9a),
    ("mediumturquoise", 0x48_d1cc),
    ("mediumvioletred", 0xc7_1585),
    ("midnightblue", 0x19_1970),
    ("mintcream", 0xf5_fffa),
    ("moccasin", 0xff_e4b5),
    ("navy", 0x00_0080),
    ("navyblue", 0x00_0080),
    ("oldlace", 0xfd_f5e6),
    ("olive", 0x80_8000),
    ("olivedrab", 0x6b_8e23),
    ("orange", 0xff_a500),
    ("orchid", 0xda_70d6),
    ("pale", 0xdb_7093),
    ("palegoldenrod", 0xee_e8aa),
    ("palegreen", 0x98_fb98),
    ("paleturquoise", 0xaf_eeee),
    ("palevioletred", 0xdb_7093),
    ("papayawhip", 0xff_efd5),
    ("pink", 0xff_c0cb),
    ("plum", 0xdd_a0dd),
    ("powderblue", 0xb0_e0e6),
    ("purple", 0xa0_20f0),
    ("rebeccapurple", 0x66_3399),
    ("red", 0xff_0000),
    ("rosybrown", 0xbc_8f8f),
    ("royalblue", 0x41_69e1),
    ("saddlebrown", 0x8b_4513),
    ("salmon", 0xfa_8072),
    ("sandybrown", 0xf4_a460),
    ("sienna", 0xa0_522d),
    ("silver", 0xc0_c0c0),
    ("skyblue", 0x87_ceeb),
    ("slateblue", 0x6a_5acd),
    ("slategray", 0x70_8090),
    ("steelblue", 0x46_82b4),
    ("tan", 0xd2_b48c),
    ("teal", 0x00_8080),
    ("thistle", 0xd8_bfd8),
    ("turquoise", 0x40_e0d0),
    ("violet", 0xee_82ee),
    ("violetred", 0xd0_2090),
    ("wheat", 0xf5_deb3),
    ("white", 0xff_ffff),
    ("whitesmoke", 0xf5_f5f5),
    ("yellow", 0xff_ff00),
    ("yellowgreen", 0x9a_cd32),
];

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::CellValue;

    fn cells(html: &str) -> Vec<(String, CellValue)> {
        let book = read_html_str(html);
        let sheet = book.sheet(0).expect("the reader always makes one sheet");
        sheet
            .iter()
            .map(|(at, cell)| (at.to_string(), cell.value.clone()))
            .collect()
    }

    #[test]
    fn a_table_becomes_a_grid() {
        let cells = cells(
            "<html><body><table>\n\
             <tr><td>Name</td><td>Count</td></tr>\n\
             <tr><td>Widget</td><td>12</td></tr>\n\
             </table></body></html>",
        );
        assert_eq!(
            cells,
            vec![
                ("A1".to_owned(), CellValue::text("Name")),
                ("B1".to_owned(), CellValue::text("Count")),
                ("A2".to_owned(), CellValue::text("Widget")),
                ("B2".to_owned(), CellValue::Number(12.0)),
            ]
        );
    }

    #[test]
    fn cells_left_open_still_end_where_the_next_one_starts() {
        // What a hand-written page looks like, and what `DOMDocument` fixes up.
        let cells = cells("<table><tr><td>a<td>b<tr><td>c<td>d</table>");
        assert_eq!(
            cells.iter().map(|(at, _)| at.as_str()).collect::<Vec<_>>(),
            ["A1", "B1", "A2", "B2"]
        );
        assert_eq!(cells[3].1, CellValue::text("d"));
    }

    #[test]
    fn spans_merge_and_push_the_cursor_along() {
        let book = read_html_str(
            "<table>\
             <tr><td rowspan=\"2\">a</td><td colspan=\"2\">b</td></tr>\
             <tr><td>c</td><td>d</td></tr>\
             </table>",
        );
        let sheet = book.sheet(0).expect("one sheet");
        assert_eq!(
            sheet
                .merges
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>(),
            ["A1:A2", "B1:C1"]
        );
        // The second row starts under the rowspan, so `c` lands in B2, not A2.
        let values: Vec<String> = sheet.iter().map(|(at, _)| at.to_string()).collect();
        assert!(values.contains(&"B2".to_owned()), "{values:?}");
        assert!(values.contains(&"C2".to_owned()), "{values:?}");
    }

    #[test]
    fn entities_and_layout_whitespace_are_resolved() {
        let cells = cells("<table><tr><td>  a &amp;\n  b&#66;  </td><td>&nbsp;</td></tr></table>");
        assert_eq!(cells, vec![("A1".to_owned(), CellValue::text("a & bB"))]);
    }

    #[test]
    fn a_line_break_inside_a_cell_stays_in_the_cell() {
        let cells = cells("<table><tr><td>a<br>b</td></tr></table>");
        assert_eq!(cells, vec![("A1".to_owned(), CellValue::text("a\nb"))]);
    }

    #[test]
    fn a_script_is_not_content() {
        let cells = cells("<table><tr><td><script>var a = '<b>';</script>x</td></tr></table>");
        assert_eq!(cells, vec![("A1".to_owned(), CellValue::text("x"))]);
    }

    #[test]
    fn inline_style_reaches_the_cell() {
        let book = read_html_str(
            "<table><tr><td style=\"background-color:#ff0000;font-weight:bold;text-align:center\">\
             x</td></tr></table>",
        );
        let sheet = book.sheet(0).expect("one sheet");
        let cell = sheet
            .get(CellRef::parse("A1").expect("A1 parses"))
            .expect("the cell was written");
        let style = book
            .styles
            .get(cell.style)
            .expect("its style is in the table");
        assert_eq!(style.fill.foreground, Color::Argb(0xffff_0000));
        assert!(style.font.bold);
        assert_eq!(style.alignment.horizontal, HorizontalAlign::Center);
    }

    #[test]
    fn a_link_becomes_a_hyperlink() {
        let book = read_html_str(
            "<table><tr><td><a href=\"https://example.com\">go</a></td></tr></table>",
        );
        let sheet = book.sheet(0).expect("one sheet");
        assert_eq!(sheet.hyperlinks.len(), 1);
        assert_eq!(
            sheet.hyperlinks[0].target,
            LinkTarget::Outside("https://example.com".to_owned())
        );
    }

    #[test]
    fn the_page_title_names_the_sheet() {
        let book = read_html_str("<html><head><title>Sales</title></head><body></body></html>");
        assert_eq!(book.sheet(0).expect("one sheet").title(), "Sales");
    }

    #[test]
    fn text_outside_a_table_fills_a_column() {
        let cells = cells("<body><h1>Title</h1><p>One</p><p>Two</p></body>");
        assert_eq!(
            cells,
            vec![
                ("A1".to_owned(), CellValue::text("Title")),
                ("A2".to_owned(), CellValue::text("One")),
                ("A3".to_owned(), CellValue::text("Two")),
            ]
        );
    }

    #[test]
    fn the_writers_own_types_survive_the_page() {
        let cells = cells(
            "<table><tr>\
             <td data-type=\"n\" data-value=\"1234.5\">1 234,50</td>\
             <td data-type=\"b\">TRUE</td>\
             <td data-formula=\"=A1*2\">2469</td>\
             </tr></table>",
        );
        assert_eq!(cells[0].1, CellValue::Number(1234.5));
        assert_eq!(cells[1].1, CellValue::Bool(true));
        assert_eq!(
            cells[2].1,
            CellValue::Formula {
                formula: "A1*2".to_owned(),
                cached: Some(Box::new(CellValue::Number(2469.0))),
            }
        );
    }

    #[test]
    fn widths_and_heights_come_through_in_the_units_the_sheet_keeps() {
        let book = read_html_str(
            "<table><tr height=\"20\"><td style=\"width:64px;height:15pt\">x</td></tr></table>",
        );
        let sheet = book.sheet(0).expect("one sheet");
        let width = sheet
            .column_width(Col::from_one_based(1).expect("column A"))
            .expect("the width was set");
        assert!((width - 9.140_625).abs() < 1e-6, "{width}");
        let height = sheet
            .row_height(Row::from_one_based(1).expect("row 1"))
            .expect("the height was set");
        // The `height` attribute of the row is applied last and wins.
        assert!((height - 20.0).abs() < 1e-6, "{height}");
    }

    #[test]
    fn column_and_row_classes_size_the_grid() {
        let book = read_html_str(
            "<style>table.sheet0 col.col1 { width: 108pt; }\n\
             table.sheet0 tr.row1 { height: 24pt; }</style>\
             <table class=\"sheet0\"><colgroup><col class=\"col1\"></colgroup>\
             <tr class=\"row1\"><td>x</td></tr>\
             <tr style=\"height:2pt\"><td>y</td></tr></table>",
        );
        let sheet = book.sheet(0).expect("one sheet");
        let width = sheet
            .column_width(Col::from_one_based(1).expect("column A"))
            .expect("the width was set");
        // 108pt is 144px, in Calibri 11 characters.
        assert!((width - 20.566_406_25).abs() < 1e-6, "{width}");
        for (row, expected) in [(1, 24.0), (2, 2.0)] {
            let height = sheet
                .row_height(Row::from_one_based(row).expect("row"))
                .expect("the height was set");
            assert!((height - expected).abs() < 1e-6, "{row}: {height}");
        }
    }

    #[test]
    fn a_class_from_the_stylesheet_styles_the_cell() {
        let book = read_html_str(
            "<style>/* x */ td.style1, .other { font-weight: bold; color: #FF0000 }</style>\
             <table><tr><td class=\"column1 style1\" data-format=\"0.00\">1</td></tr></table>",
        );
        let ws = book.sheet(0).expect("one sheet");
        let id = ws
            .get(CellRef::parse("A1").expect("A1"))
            .expect("a cell")
            .style;
        let style = book.styles.get(id).expect("the style");
        assert!(style.font.bold);
        assert_eq!(style.number_format.code(), "0.00");
    }
}