aboutsummaryrefslogtreecommitdiffstats
path: root/src/html.zig
blob: 8ec8564797a391d1d7f1429c7279d349cfa48ac4 (plain)
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
//! Transform the Zig docs single-page HTML5 into well-formed XHTML5 for EPUB.
//!
//! The page is self-contained (embedded <style>, inline base64 icons, in-page TOC,
//! entity-escaped code). The transform:
//!   1. replaces the leading `<!DOCTYPE html>` with an XML declaration,
//!   2. ensures the <html> element carries the XHTML namespace,
//!   3. strips <script>...</script> blocks (non-functional in EPUB, aids validation),
//!   4. normalizes HTML named entities (e.g. `&mdash;`) to numeric references,
//!      since XML predefines only `&amp; &lt; &gt; &quot; &apos;`,
//!   5. removes invalid control characters,
//!   6. extracts the in-page table of contents (flattened to one level, since
//!      Amazon's kindlegen rejects nested nav TOCs) for the EPUB navigation,
//!   7. removes the `#navigation` sidebar (it is `position: fixed`, so it bleeds
//!      onto every page, and the version-switcher dropdown is dead in an EPUB),
//!   8. self-closes void elements (`<meta>` -> `<meta/>`, etc.).

const std = @import("std");
const assert = std.debug.assert;

const xml_decl = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
const xhtml_ns = "http://www.w3.org/1999/xhtml";

/// File name of the content document the TOC links point into.
const content_href = "index.xhtml";

/// HTML void elements: never have children, must be self-closed in XHTML.
const void_elements = [_][]const u8{
    "meta", "link", "br", "hr", "img", "col", "input", "source", "area", "base", "wbr", "track", "param",
};

/// Result of transforming a docs page: the content document plus, when present,
/// the table-of-contents list extracted for the EPUB navigation document.
pub const Document = struct {
    allocator: std.mem.Allocator,
    /// Well-formed XHTML5 content, with the fixed sidebar removed.
    content: []u8,
    /// `<ol>...</ol>` of TOC entries (links rewritten to `index.xhtml#...`), or
    /// null if the page had no recognizable table of contents.
    toc: ?[]u8,

    pub fn deinit(self: *Document) void {
        self.allocator.free(self.content);
        if (self.toc) |t| self.allocator.free(t);
    }
};

/// Transform `page` (HTML5) into an EPUB-ready `Document`. Caller owns it.
pub fn transform(allocator: std.mem.Allocator, page: []const u8) !Document {
    assert(page.len > 0);

    const no_doctype = stripDoctype(page);
    const no_scripts = try stripScripts(allocator, no_doctype);
    defer allocator.free(no_scripts);

    const clean = try stripControlChars(allocator, no_scripts);
    defer allocator.free(clean);

    const escaped = try escapeEntities(allocator, clean);
    defer allocator.free(escaped);

    const toc = try extractToc(allocator, escaped);
    errdefer if (toc) |t| allocator.free(t);

    const no_sidebar = try removeNavigation(allocator, escaped);
    defer allocator.free(no_sidebar);

    // Section headings link back to their entry in the removed sidebar
    // (`href="#toc-X"`); redirect those to the matching anchors now in nav.xhtml,
    // otherwise they are dangling fragments (epubcheck RSC-012, breaks Kindle).
    const relinked = try std.mem.replaceOwned(u8, allocator, no_sidebar, "href=\"#toc-", "href=\"nav.xhtml#toc-");
    defer allocator.free(relinked);

    var out: std.Io.Writer.Allocating = .init(allocator);
    defer out.deinit();
    try out.writer.writeAll(xml_decl);
    try writeWithFixups(&out.writer, relinked);

    const content = try out.toOwnedSlice();
    assert(std.mem.startsWith(u8, content, "<?xml"));
    return .{ .allocator = allocator, .content = content, .toc = toc };
}

/// Convenience wrapper returning only the content document. Caller owns it.
pub fn toXhtml(allocator: std.mem.Allocator, page: []const u8) ![]u8 {
    const doc = try transform(allocator, page);
    defer if (doc.toc) |t| allocator.free(t);
    return doc.content;
}

/// Extract the in-page table of contents (the `<ul>` inside the
/// `table-of-contents` nav) as an EPUB `<ol>` with links rewritten to point into
/// the content document. Returns null if no such list is found. Caller owns it.
fn extractToc(allocator: std.mem.Allocator, html: []const u8) !?[]u8 {
    const marker = "aria-labelledby=\"table-of-contents\"";
    const m = std.mem.indexOf(u8, html, marker) orelse return null;
    const ul_open = findTagOpen(html, m, "ul") orelse return null;
    const ul_end = matchingClose(html, ul_open, "ul") orelse return null;
    return try flattenToc(allocator, html[ul_open..ul_end]);
}

/// Build a single-level `<ol>` from the (possibly nested) TOC region: collect
/// every `<a>...</a>` link, rewrite `href="#X"` → `href="index.xhtml#X"`, and
/// wrap each in its own `<li>`. The list is flattened on purpose — Amazon's
/// kindlegen rejects nested navigation TOCs (error E24011), which is what breaks
/// "Send to Kindle". Returns null if the region has no links. Caller owns it.
fn flattenToc(allocator: std.mem.Allocator, region: []const u8) !?[]u8 {
    var out: std.Io.Writer.Allocating = .init(allocator);
    defer out.deinit();
    try out.writer.writeAll("<ol>\n");

    var i: usize = 0;
    var count: usize = 0;
    while (std.mem.indexOfPos(u8, region, i, "<a")) |a_lt| {
        // Confirm this is an <a> element start (delimiter after the name).
        const after = a_lt + 2;
        if (after >= region.len or (region[after] != ' ' and region[after] != '>' and
            region[after] != '\t' and region[after] != '\n'))
        {
            i = after;
            continue;
        }
        const close = std.mem.indexOfPos(u8, region, a_lt, "</a>") orelse break;
        try out.writer.writeAll("<li>");
        try writeAnchor(&out.writer, region[a_lt .. close + 4]);
        try out.writer.writeAll("</li>\n");
        i = close + 4;
        count += 1;
    }

    try out.writer.writeAll("</ol>\n");
    if (count == 0) return null;
    return try out.toOwnedSlice();
}

/// Write an `<a>...</a>`, rewriting a leading `href="#"` to target the content.
fn writeAnchor(w: *std.Io.Writer, anchor: []const u8) !void {
    if (std.mem.indexOf(u8, anchor, "href=\"#")) |p| {
        try w.writeAll(anchor[0 .. p + 6]); // through the opening quote
        try w.writeAll(content_href);
        try w.writeAll(anchor[p + 6 ..]); // from '#' onward
    } else {
        try w.writeAll(anchor);
    }
}

/// Remove the `<div id="navigation">…</div>` sidebar. If absent, returns an
/// unmodified copy. Caller owns the result.
fn removeNavigation(allocator: std.mem.Allocator, html: []const u8) ![]u8 {
    const m = std.mem.indexOf(u8, html, "id=\"navigation\"") orelse
        return allocator.dupe(u8, html);
    const div_open = std.mem.lastIndexOfScalar(u8, html[0..m], '<') orelse
        return allocator.dupe(u8, html);
    const div_end = matchingClose(html, div_open, "div") orelse
        return allocator.dupe(u8, html);

    var out = try allocator.alloc(u8, html.len - (div_end - div_open));
    @memcpy(out[0..div_open], html[0..div_open]);
    @memcpy(out[div_open..], html[div_end..]);
    return out;
}

/// Index just past the `</name>` that closes the element opened at `open_lt`,
/// accounting for nested elements of the same name. Null if unbalanced.
fn matchingClose(html: []const u8, open_lt: usize, name: []const u8) ?usize {
    var i = open_lt;
    var depth: usize = 0;
    while (std.mem.indexOfScalarPos(u8, html, i, '<')) |lt| {
        const gt = tagEnd(html, lt) orelse return null;
        const tag = html[lt .. gt + 1];
        if (std.ascii.eqlIgnoreCase(tagName(tag), name)) {
            if (tag[1] == '/') {
                if (depth == 0) return null;
                depth -= 1;
                if (depth == 0) return gt + 1;
            } else if (!std.mem.endsWith(u8, tag, "/>")) {
                depth += 1;
            }
        }
        i = gt + 1;
    }
    return null;
}

/// Drop a leading `<!doctype html>` (case-insensitive), preserving the rest.
fn stripDoctype(page: []const u8) []const u8 {
    const start = std.mem.indexOfNone(u8, page, " \t\r\n") orelse 0;
    const rest = page[start..];
    if (rest.len >= 9 and std.ascii.eqlIgnoreCase(rest[0..9], "<!doctype")) {
        const end = std.mem.indexOfScalar(u8, rest, '>') orelse return rest;
        return rest[end + 1 ..];
    }
    return page;
}

/// Remove every `<script ...>...</script>` block. Caller owns the result.
fn stripScripts(allocator: std.mem.Allocator, html: []const u8) ![]u8 {
    var out: std.Io.Writer.Allocating = .init(allocator);
    defer out.deinit();

    var i: usize = 0;
    var guard: usize = 0;
    while (i < html.len) {
        guard += 1;
        assert(guard <= html.len + 1);

        const open = findTagOpen(html, i, "script") orelse {
            try out.writer.writeAll(html[i..]);
            break;
        };
        try out.writer.writeAll(html[i..open]);

        const close = findClose(html, open, "</script>") orelse {
            // Unterminated script: drop the remainder rather than emit broken markup.
            break;
        };
        i = close;
    }
    return out.toOwnedSlice();
}

/// Maximum element nesting depth we will validate (NASA Power-of-10: bounded).
const max_depth = 256;

/// Lightweight check that `xhtml` is well-formed: every element tag is balanced
/// and properly nested. This is not a full XML parser, but it catches the
/// structural breakage (unclosed/mis-nested tags) present in some older Zig docs
/// pages, letting the caller skip versions that would not be valid EPUB3.
///
/// Relies on our transform guarantees: scripts stripped, void elements
/// self-closed, code samples entity-escaped (so no stray `<` in text).
pub fn isWellFormed(xhtml: []const u8) bool {
    var stack: [max_depth][]const u8 = undefined;
    var depth: usize = 0;

    var i: usize = 0;
    while (std.mem.indexOfScalarPos(u8, xhtml, i, '<')) |lt| {
        // Markup we skip wholesale: processing instructions, comments, declarations.
        if (startsAt(xhtml, lt, "<?")) {
            i = (std.mem.indexOfPos(u8, xhtml, lt, "?>") orelse return false) + 2;
            continue;
        }
        if (startsAt(xhtml, lt, "<!--")) {
            i = (std.mem.indexOfPos(u8, xhtml, lt, "-->") orelse return false) + 3;
            continue;
        }
        if (startsAt(xhtml, lt, "<!")) {
            i = (tagEnd(xhtml, lt) orelse return false) + 1;
            continue;
        }

        const gt = tagEnd(xhtml, lt) orelse return false;
        const tag = xhtml[lt .. gt + 1];
        i = gt + 1;

        const name = tagName(tag);
        if (name.len == 0) return false;

        if (tag[1] == '/') {
            if (depth == 0 or !std.mem.eql(u8, stack[depth - 1], name)) return false;
            depth -= 1;
        } else if (std.mem.endsWith(u8, tag, "/>") or isVoid(name)) {
            // Self-contained element; nothing to balance.
        } else {
            if (depth >= max_depth) return false;
            stack[depth] = name;
            depth += 1;
        }
    }
    return depth == 0;
}

fn startsAt(s: []const u8, at: usize, prefix: []const u8) bool {
    return at + prefix.len <= s.len and std.mem.eql(u8, s[at .. at + prefix.len], prefix);
}

/// Index of the `>` closing the tag opened at `lt`, ignoring `>` inside quotes.
fn tagEnd(s: []const u8, lt: usize) ?usize {
    var i = lt + 1;
    var quote: u8 = 0;
    while (i < s.len) : (i += 1) {
        const c = s[i];
        if (quote != 0) {
            if (c == quote) quote = 0;
        } else switch (c) {
            '"', '\'' => quote = c,
            '>' => return i,
            else => {},
        }
    }
    return null;
}

/// Remove characters XML 1.0 forbids: C0 controls except tab/newline/return.
/// Some older docs embed raw terminal escape codes (e.g. `\x1b[0K`) in captured
/// code samples, which are invalid in any XML document. Caller owns the result.
fn stripControlChars(allocator: std.mem.Allocator, html: []const u8) ![]u8 {
    var out: std.ArrayList(u8) = .empty;
    errdefer out.deinit(allocator);
    try out.ensureTotalCapacity(allocator, html.len);

    for (html) |c| {
        const forbidden = c < 0x20 and c != '\t' and c != '\n' and c != '\r';
        if (!forbidden) out.appendAssumeCapacity(c);
    }
    return out.toOwnedSlice(allocator);
}

/// XML predefines only these five entities; everything else must be numeric.
const xml_entities = [_][]const u8{ "amp", "lt", "gt", "quot", "apos" };

/// Common HTML named entities → Unicode code point. Not exhaustive; any name not
/// listed (and not XML-predefined or numeric) has its `&` escaped to `&amp;` so
/// output stays well-formed regardless of input.
const named_entities = std.StaticStringMap(u21).initComptime(.{
    .{ "nbsp", 0x00A0 }, .{ "copy", 0x00A9 }, .{ "reg", 0x00AE },  .{ "trade", 0x2122 },
    .{ "mdash", 0x2014 }, .{ "ndash", 0x2013 }, .{ "hellip", 0x2026 }, .{ "deg", 0x00B0 },
    .{ "plusmn", 0x00B1 }, .{ "times", 0x00D7 }, .{ "divide", 0x00F7 }, .{ "micro", 0x00B5 },
    .{ "middot", 0x00B7 }, .{ "bull", 0x2022 }, .{ "dagger", 0x2020 }, .{ "sect", 0x00A7 },
    .{ "para", 0x00B6 }, .{ "laquo", 0x00AB }, .{ "raquo", 0x00BB }, .{ "lsquo", 0x2018 },
    .{ "rsquo", 0x2019 }, .{ "ldquo", 0x201C }, .{ "rdquo", 0x201D }, .{ "larr", 0x2190 },
    .{ "rarr", 0x2192 }, .{ "uarr", 0x2191 }, .{ "darr", 0x2193 }, .{ "harr", 0x2194 },
    .{ "le", 0x2264 }, .{ "ge", 0x2265 }, .{ "ne", 0x2260 }, .{ "infin", 0x221E },
    .{ "sum", 0x2211 }, .{ "prod", 0x220F }, .{ "radic", 0x221A }, .{ "asymp", 0x2248 },
    .{ "equiv", 0x2261 }, .{ "frac12", 0x00BD }, .{ "frac14", 0x00BC }, .{ "frac34", 0x00BE },
    .{ "euro", 0x20AC }, .{ "pound", 0x00A3 }, .{ "yen", 0x00A5 }, .{ "cent", 0x00A2 },
});

/// Longest entity name we will look ahead for (e.g. "frac34").
const max_entity_name = 8;

/// Rewrite `&...;` tokens so the output is XML well-formed. Caller owns the result.
fn escapeEntities(allocator: std.mem.Allocator, html: []const u8) ![]u8 {
    var out: std.Io.Writer.Allocating = .init(allocator);
    defer out.deinit();

    var i: usize = 0;
    while (i < html.len) {
        const amp = std.mem.indexOfScalarPos(u8, html, i, '&') orelse {
            try out.writer.writeAll(html[i..]);
            break;
        };
        try out.writer.writeAll(html[i..amp]);

        if (entityToken(html, amp)) |tok| {
            if (isKeptEntity(tok)) {
                try out.writer.writeAll(html[amp .. amp + tok.len + 2]); // include & and ;
            } else if (named_entities.get(tok)) |cp| {
                try out.writer.print("&#{d};", .{cp});
            } else {
                try out.writer.writeAll("&amp;");
                i = amp + 1;
                continue;
            }
            i = amp + tok.len + 2;
        } else {
            // Bare '&' not forming an entity reference.
            try out.writer.writeAll("&amp;");
            i = amp + 1;
        }
    }
    return out.toOwnedSlice();
}

/// If an `&...;` reference starts at `amp`, return the name between `&` and `;`.
fn entityToken(html: []const u8, amp: usize) ?[]const u8 {
    assert(html[amp] == '&');
    const max = @min(html.len, amp + 2 + max_entity_name);
    const semi = std.mem.indexOfScalarPos(u8, html[0..max], amp + 1, ';') orelse return null;
    const tok = html[amp + 1 .. semi];
    if (tok.len == 0) return null;
    return tok;
}

/// True for entities XML keeps verbatim: the predefined five and numeric refs.
fn isKeptEntity(tok: []const u8) bool {
    if (tok[0] == '#') {
        const digits = tok[1..];
        if (digits.len == 0) return false;
        const hex = digits[0] == 'x' or digits[0] == 'X';
        const rest = if (hex) digits[1..] else digits;
        if (rest.len == 0) return false;
        for (rest) |c| {
            const ok = if (hex) std.ascii.isHex(c) else std.ascii.isDigit(c);
            if (!ok) return false;
        }
        return true;
    }
    for (xml_entities) |e| {
        if (std.mem.eql(u8, tok, e)) return true;
    }
    return false;
}

/// Copy `html` to `w`, adding the XHTML namespace to <html> and self-closing
/// void elements as they are encountered.
fn writeWithFixups(w: *std.Io.Writer, html: []const u8) !void {
    var i: usize = 0;
    while (i < html.len) {
        const lt = std.mem.indexOfScalarPos(u8, html, i, '<') orelse {
            try w.writeAll(html[i..]);
            return;
        };
        try w.writeAll(html[i..lt]);

        const gt = std.mem.indexOfScalarPos(u8, html, lt, '>') orelse {
            try w.writeAll(html[lt..]);
            return;
        };
        const tag = html[lt .. gt + 1]; // includes < and >
        try writeTag(w, tag);
        i = gt + 1;
    }
}

/// Emit a single tag (`<...>`), applying namespace/void-element fixups.
fn writeTag(w: *std.Io.Writer, tag: []const u8) !void {
    assert(tag.len >= 2);
    assert(tag[0] == '<' and tag[tag.len - 1] == '>');

    const name = tagName(tag);
    const is_closing = tag.len >= 2 and tag[1] == '/';

    if (!is_closing and std.ascii.eqlIgnoreCase(name, "html") and std.mem.indexOf(u8, tag, "xmlns") == null) {
        try w.print("<html xmlns=\"{s}\"", .{xhtml_ns});
        try w.writeAll(tag[1 + name.len ..]);
        return;
    }

    if (isVoid(name) and !std.mem.endsWith(u8, tag, "/>")) {
        try w.writeAll(tag[0 .. tag.len - 1]);
        try w.writeAll("/>");
        return;
    }

    try w.writeAll(tag);
}

/// Extract the element name from a tag slice (without `<`, `>`, `/`, attrs).
fn tagName(tag: []const u8) []const u8 {
    var s: usize = 1; // skip '<'
    if (s < tag.len and tag[s] == '/') s += 1;
    var e = s;
    while (e < tag.len and isNameChar(tag[e])) e += 1;
    return tag[s..e];
}

fn isNameChar(c: u8) bool {
    return std.ascii.isAlphanumeric(c) or c == '-' or c == ':';
}

fn isVoid(name: []const u8) bool {
    for (void_elements) |v| {
        if (std.ascii.eqlIgnoreCase(name, v)) return true;
    }
    return false;
}

/// Find the next `<name` opening tag at or after `from`, returning the `<` index.
fn findTagOpen(html: []const u8, from: usize, name: []const u8) ?usize {
    var i = from;
    while (std.mem.indexOfScalarPos(u8, html, i, '<')) |lt| {
        const after = lt + 1;
        if (after + name.len <= html.len and
            std.ascii.eqlIgnoreCase(html[after .. after + name.len], name))
        {
            const next = html[after + name.len];
            if (next == '>' or next == ' ' or next == '\t' or next == '\n' or next == '/') return lt;
        }
        i = lt + 1;
    }
    return null;
}

/// Find the index just past a closing tag (e.g. `</script>`) at or after `from`.
fn findClose(html: []const u8, from: usize, close: []const u8) ?usize {
    const idx = std.ascii.indexOfIgnoreCasePos(html, from, close) orelse return null;
    return idx + close.len;
}

test "strips doctype and adds xml declaration" {
    const gpa = std.testing.allocator;
    const out = try toXhtml(gpa, "<!DOCTYPE html>\n<html><body>x</body></html>");
    defer gpa.free(out);
    try std.testing.expect(std.mem.startsWith(u8, out, "<?xml"));
    try std.testing.expect(std.mem.indexOf(u8, out, "<!DOCTYPE") == null);
}

test "adds xhtml namespace to html element" {
    const gpa = std.testing.allocator;
    const out = try toXhtml(gpa, "<html lang=\"en\"><head></head></html>");
    defer gpa.free(out);
    try std.testing.expect(std.mem.indexOf(u8, out, "xmlns=\"http://www.w3.org/1999/xhtml\"") != null);
    try std.testing.expect(std.mem.indexOf(u8, out, "lang=\"en\"") != null);
    // The closing tag must be left intact (not rewritten with a namespace).
    try std.testing.expect(std.mem.endsWith(u8, out, "</html>"));
    try std.testing.expect(std.mem.indexOf(u8, out, "xhtml\"l>") == null);
}

test "self-closes void elements" {
    const gpa = std.testing.allocator;
    const out = try toXhtml(gpa, "<html><head><meta charset=\"utf-8\"><br></head></html>");
    defer gpa.free(out);
    try std.testing.expect(std.mem.indexOf(u8, out, "<meta charset=\"utf-8\"/>") != null);
    try std.testing.expect(std.mem.indexOf(u8, out, "<br/>") != null);
}

test "strips script blocks" {
    const gpa = std.testing.allocator;
    const out = try toXhtml(gpa, "<html><body>a<script>var x = 1 < 2;</script>b</body></html>");
    defer gpa.free(out);
    try std.testing.expect(std.mem.indexOf(u8, out, "<script") == null);
    try std.testing.expect(std.mem.indexOf(u8, out, "var x") == null);
    try std.testing.expect(std.mem.indexOf(u8, out, ">a") != null);
    try std.testing.expect(std.mem.indexOf(u8, out, "b<") != null);
}

test "normalizes named entities and escapes stray ampersands" {
    const gpa = std.testing.allocator;
    const out = try toXhtml(gpa, "<html><body>a&mdash;b &amp; c&nbsp;d R&D &lt;x&gt; &#8212; &unknownent;</body></html>");
    defer gpa.free(out);
    try std.testing.expect(std.mem.indexOf(u8, out, "&mdash;") == null);
    try std.testing.expect(std.mem.indexOf(u8, out, "&#8212;") != null); // mdash -> numeric
    try std.testing.expect(std.mem.indexOf(u8, out, "&#160;") != null); // nbsp -> numeric
    try std.testing.expect(std.mem.indexOf(u8, out, "R&amp;D") != null); // stray & escaped
    try std.testing.expect(std.mem.indexOf(u8, out, "&amp;unknownent;") != null); // unknown name escaped
    try std.testing.expect(std.mem.indexOf(u8, out, "&amp; c") != null); // existing &amp; kept (not double-escaped)
    try std.testing.expect(std.mem.indexOf(u8, out, "&lt;x&gt;") != null);
}

test "strips XML-invalid control characters but keeps whitespace" {
    const gpa = std.testing.allocator;
    const out = try toXhtml(gpa, "<html><body>a\x1b[0Kb\tc\nd</body></html>");
    defer gpa.free(out);
    try std.testing.expect(std.mem.indexOfScalar(u8, out, 0x1b) == null); // ESC removed
    try std.testing.expect(std.mem.indexOf(u8, out, "a[0Kb") != null); // only the control byte dropped
    try std.testing.expect(std.mem.indexOf(u8, out, "c\nd") != null); // tab/newline preserved
}

test "transform extracts TOC and removes the navigation sidebar" {
    const gpa = std.testing.allocator;
    const page =
        "<!DOCTYPE html><html><body><h1>Title</h1>" ++
        "<div id=\"main-wrapper\">" ++
        "<div id=\"navigation\">" ++
        "<nav aria-labelledby=\"zig-version\"><h2 id=\"zig-version\">0.x</h2></nav>" ++
        "<nav aria-labelledby=\"table-of-contents\"><h2 id=\"table-of-contents\">Table of Contents</h2>" ++
        "<ul><li><a id=\"toc-Intro\" href=\"#Intro\">Intro</a>" ++
        "<ul><li><a href=\"#Sub\">Sub</a></li></ul></li></ul>" ++
        "</nav></div>" ++
        "<main id=\"contents\"><h2 id=\"Intro\"><a href=\"#toc-Intro\">Intro</a></h2><p>body</p></main>" ++
        "</div></body></html>";

    var doc = try transform(gpa, page);
    defer doc.deinit();

    // Sidebar (and its version dropdown) is gone from the content.
    try std.testing.expect(std.mem.indexOf(u8, doc.content, "id=\"navigation\"") == null);
    try std.testing.expect(std.mem.indexOf(u8, doc.content, "zig-version") == null);
    // Content itself is preserved.
    try std.testing.expect(std.mem.indexOf(u8, doc.content, "<main id=\"contents\">") != null);
    try std.testing.expect(std.mem.indexOf(u8, doc.content, "<h2 id=\"Intro\">") != null);
    try std.testing.expect(isWellFormed(doc.content));
    // Back-to-contents link is redirected to the nav document (no dangling fragment).
    try std.testing.expect(std.mem.indexOf(u8, doc.content, "href=\"nav.xhtml#toc-Intro\"") != null);
    try std.testing.expect(std.mem.indexOf(u8, doc.content, "href=\"#toc-Intro\"") == null);

    // TOC extracted, ul->ol, links rewritten into the content document.
    try std.testing.expect(doc.toc != null);
    const toc = doc.toc.?;
    try std.testing.expect(std.mem.indexOf(u8, toc, "<ol>") != null);
    try std.testing.expect(std.mem.indexOf(u8, toc, "<ul>") == null);
    try std.testing.expect(std.mem.indexOf(u8, toc, "href=\"index.xhtml#Intro\"") != null);
    try std.testing.expect(std.mem.indexOf(u8, toc, "href=\"index.xhtml#Sub\"") != null);
}

test "transform yields null TOC when no table of contents is present" {
    const gpa = std.testing.allocator;
    var doc = try transform(gpa, "<html><body><p>no toc here</p></body></html>");
    defer doc.deinit();
    try std.testing.expect(doc.toc == null);
}

test "isWellFormed accepts balanced markup and rejects mismatches" {
    const decl = "<?xml version=\"1.0\"?>";
    // Balanced, with self-closed void, comment, and a '>' inside an attribute.
    try std.testing.expect(isWellFormed(decl ++ "<html><head><meta charset=\"u\"/></head>" ++
        "<body><!-- c --><p title=\"a &gt; b\">x<br/></p></body></html>"));
    // Unclosed <p> before </body>.
    try std.testing.expect(!isWellFormed("<html><body><p>x</body></html>"));
    // Stray closing tag.
    try std.testing.expect(!isWellFormed("<html><body></span></body></html>"));
    // Unbalanced at end of document.
    try std.testing.expect(!isWellFormed("<html><body><div></body>"));
}

test "leaves already self-closed void elements unchanged" {
    const gpa = std.testing.allocator;
    const out = try toXhtml(gpa, "<html><head><meta charset=\"utf-8\"/></head></html>");
    defer gpa.free(out);
    try std.testing.expect(std.mem.indexOf(u8, out, "<meta charset=\"utf-8\"//>") == null);
    try std.testing.expect(std.mem.indexOf(u8, out, "<meta charset=\"utf-8\"/>") != null);
}