html-to-markdown-rs 3.18.3

High-performance HTML to Markdown converter using the astral-tl parser. Part of the Xberg ecosystem.
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
/// Entry point for the Tier-1 scanner.
pub fn scan(
    html: &str,
    options: &ConversionOptions,
    effective_base: Option<std::rc::Rc<url::Url>>,
) -> Result<ScanOutput, BailReason> {
    Tier1Scanner::new(html, options, effective_base).run()
}

struct Tier1Scanner<'a> {
    html: &'a str,
    bytes: &'a [u8],
    options: &'a ConversionOptions,
    state: Tier1State,
    table_probes: Vec<TableLayoutProbe>,
    pos: usize,
    text_start: usize,
}

impl<'a> Tier1Scanner<'a> {
    fn new(html: &'a str, options: &'a ConversionOptions, effective_base: Option<std::rc::Rc<url::Url>>) -> Self {
        Self {
            html,
            bytes: html.as_bytes(),
            options,
            state: Tier1State::new(html.len(), effective_base),
            table_probes: Vec::new(),
            pos: 0,
            text_start: 0,
        }
    }

    fn run(mut self) -> Result<ScanOutput, BailReason> {
        while self.pos < self.bytes.len() {
            if self.bytes[self.pos] == b'<' {
                self.scan_markup()?;
            } else {
                self.advance_text();
            }
        }
        finish_scan(
            self.state,
            self.html,
            self.text_start,
            self.pos,
            self.options,
            self.table_probes,
        )
    }

    fn scan_markup(&mut self) -> Result<(), BailReason> {
        self.flush_before_markup()?;
        let next = self.bytes.get(self.pos + 1).copied().unwrap_or(0);
        if next == b'!' {
            return self.scan_bang();
        }
        if next == b'?' {
            return Err(BailReason::Classifier);
        }
        if next == b'/' {
            return self.scan_close_tag();
        }
        if !parse::is_tag_name_start(next) {
            return self.emit_literal_lt();
        }
        self.scan_open_tag()
    }

    fn flush_before_markup(&mut self) -> Result<(), BailReason> {
        if self.text_start >= self.pos {
            return Ok(());
        }
        let next_tag_is_html = upcoming_tag_is_named(self.bytes, self.pos, b"html");
        let ignored_before_head = self.state.head_range.is_none()
            && crate::converter::main_helpers::is_ignorable_before_head(
                &self.html[self.text_start..self.pos],
                next_tag_is_html,
            );
        if ignored_before_head {
            return Ok(());
        }
        self.state.start_body(self.text_start);
        // ~keep Every decision on this text reads the white space of the source as characters,
        // ~keep as the full converter reads its decoded text: `&#10;` alone is white space.
        let html = self.html;
        let text = text_for_decisions(&html[self.text_start..self.pos], &self.state);
        let is_inline = upcoming_tag_is_inline(self.bytes, self.pos);
        // ~keep Only a text with content asks: white space alone takes its own path in
        // ~keep `flush_text`. A form feed is a character (issue #811), so the line end after it
        // ~keep is a space before an inline element that follows the comments.
        let inline_follows_comments = !text.bytes().all(|byte| matches!(byte, b' ' | b'\t' | b'\n' | b'\r'))
            && inline_follows_comments(self.bytes, self.pos);
        // ~keep Both scans of the markup ahead decide what a line end at the end of the text
        // ~keep becomes, so a text that ends with no line end does not scan: each scan passes
        // ~keep the open tags of the elements that only wrap text, and a run of them on every
        // ~keep text made the converter four times slower on deep nesting.
        let before_line_end = crate::converter::utility::content::without_trailing_line_end_in_source(&text);
        let ends_with_line_end = before_line_end.is_some();
        let upcoming = UpcomingTextSibling {
            is_list: upcoming_tag_is_list_open(self.bytes, self.pos),
            is_img: upcoming_tag_is_named(self.bytes, self.pos, b"img"),
            is_inline,
            inline_follows_comments,
            inline_starts_with_block: ends_with_line_end
                && (is_inline || inline_follows_comments)
                && upcoming_inline_starts_with_block(self.bytes, self.pos),
            // ~keep Rule 2 has two sides: the character before the line end, or the one after it.
            line_end_meets_zero_width_space: before_line_end.is_some_and(|text| {
                crate::converter::utility::content::ends_with_zero_width_space_in_source(text)
                    || zero_width_space_is_upcoming(self.html, self.pos, &self.state.stack)
            }),
        };
        flush_text(
            &mut self.state,
            TextFlush {
                raw: &text,
                base_offset: self.text_start,
                upcoming,
                br_in_tables: self.options.br_in_tables,
                output_format: self.options.output_format,
            },
        )
    }

    fn scan_bang(&mut self) -> Result<(), BailReason> {
        if self.html[self.pos..].starts_with("<![CDATA[") {
            return Err(BailReason::Cdata { offset: self.pos });
        }
        self.pos = skip_bang(self.bytes, self.pos)?;
        self.text_start = self.pos;
        Ok(())
    }

    fn scan_close_tag(&mut self) -> Result<(), BailReason> {
        let name_start = self.pos + 2;
        let name_end = parse::scan_tag_name(self.bytes, name_start);
        if name_end == name_start {
            return Err(BailReason::LiteralLt { offset: self.pos });
        }
        let close = parse::find_tag_close(self.bytes, name_end).ok_or(BailReason::LiteralLt { offset: self.pos })?;
        let tag_name = &self.bytes[name_start..name_end];
        let len_before_close = self.state.cell_or_output_mut().len();
        let join_open = self.state.pending_newline_join == Some(len_before_close);
        let place_after = emit_close(&mut self.state, tag_name, self.options, &mut self.table_probes)?;
        let mut name_buf = [0u8; MAX_TAG_NAME_BYTES];
        let name_lower = lowercase_into(tag_name, &mut name_buf);
        let ends_own_line = tier1::lookup(name_lower).is_some_and(|spec| spec.is_block)
            || matches!(
                name_lower,
                b"optgroup" | b"progress" | b"meter" | b"output" | b"datalist"
            );
        let dest = self.state.cell_or_output_mut();
        let (dest_len, ends_in_newline) = (dest.len(), dest.ends_with('\n'));
        self.state.pending_newline_join = (join_open && ends_in_newline && !ends_own_line).then_some(dest_len);
        self.pos = close.0 + 1;
        self.state.text_place = place_after.map(|place| (self.pos, place));
        self.text_start = self.pos;
        Ok(())
    }

    fn emit_literal_lt(&mut self) -> Result<(), BailReason> {
        self.state.start_body(self.pos);
        flush_text(
            &mut self.state,
            TextFlush {
                raw: "<",
                base_offset: self.pos,
                upcoming: UpcomingTextSibling::default(),
                br_in_tables: self.options.br_in_tables,
                output_format: self.options.output_format,
            },
        )?;
        self.pos += 1;
        self.text_start = self.pos;
        Ok(())
    }

    fn scan_open_tag(&mut self) -> Result<(), BailReason> {
        let name_start = self.pos + 1;
        let name_end = parse::scan_tag_name(self.bytes, name_start);
        let tag_name_bytes = &self.bytes[name_start..name_end];

        let mut name_buf = [0u8; MAX_TAG_NAME_BYTES];
        let name_lower = lowercase_into(tag_name_bytes, &mut name_buf);
        self.prepare_open_tag(name_lower)?;
        if name_lower == b"svg" && self.scan_svg(name_end)? {
            return Ok(());
        }
        if name_lower == b"template" && self.scan_template(name_end)? {
            return Ok(());
        }

        let spec = resolve_tag_spec(name_lower, tag_name_bytes, self.pos)?;
        if matches!(spec.kind, TagKind::Ignored) && spec.is_rawtext {
            self.scan_rawtext_ignored(name_start, name_end, name_lower)?;
            return Ok(());
        }
        if matches!(spec.kind, TagKind::Ignored) {
            self.scan_ignored(spec, name_end, name_lower)?;
            return Ok(());
        }

        bail_unsupported(spec, self.pos)?;

        if self.skip_preprocessed_tag(name_lower, name_end)? {
            return Ok(());
        }

        self.bail_for_code_rules(spec)?;

        let close = parse::find_tag_close(self.bytes, name_end).ok_or(BailReason::LiteralLt { offset: self.pos })?;

        let attrs = collect_open_attrs(spec, name_lower, self.bytes, name_end, close);

        self.pos = close.0 + 1;

        self.state.link_stack.iter_mut().for_each(|entry| entry.2 = true);

        if spec.is_void || close.1 {
            if cell_needs_tier2(&mut self.state, spec, name_lower) {
                return Err(BailReason::TableBlockChildInCell);
            }
            self.bail_for_image_label(name_lower, &attrs)?;
            emit_void(&mut self.state, spec, name_lower, &attrs, self.html, self.options)?;
            self.record_heading_break_place(spec);
            self.text_start = self.pos;
            return Ok(());
        }

        self.close_implicit_tags(spec)?;
        validate_cell_open(&mut self.state, spec, name_lower)?;
        validate_list_open(&mut self.state, spec, name_lower)?;
        let (prev_ctx, ol_start) = self.prepare_open_metadata(spec, name_lower, &attrs)?;
        check_open_depth(&self.state, self.options)?;
        emit_open(
            &mut self.state,
            spec,
            name_lower,
            &attrs,
            &mut self.table_probes,
            self.options,
        )?;
        push_open_frame(
            &mut self.state,
            spec,
            name_lower,
            name_start..name_end,
            prev_ctx,
            ol_start,
        );
        apply_open_escape_ctx(&mut self.state, spec);

        self.state.text_place = Some((self.pos, TextPlace::ElementStart));
        self.text_start = self.pos;
        Ok(())
    }

    /// Record the place of a text that starts where the `<br>` of a heading ends, at `self.pos`.
    ///
    /// ~keep Such a text joins its Unicode space to the space of that `<br>`, in both converters.
    /// ~keep In a table cell the place stays unknown: Tier-2 keeps the Unicode space there.
    fn record_heading_break_place(&mut self, spec: &TagSpec) {
        if matches!(spec.kind, TagKind::LineBreak)
            && self.state.escape_ctx.contains(EscapeCtx::HEADING)
            && !self.state.in_table_cell()
        {
            self.state.text_place = Some((self.pos, TextPlace::AfterBreak));
        }
    }

    /// Leave to Tier-2 an open tag whose code block rule this scanner does not have.
    fn bail_for_code_rules(&self, spec: &TagSpec) -> Result<(), BailReason> {
        // ~keep Tilde fences still require Tier-2; Tier-1 supports indented/backtick pre blocks.
        if matches!(spec.kind, TagKind::Pre) && self.options.code_block_style == crate::options::CodeBlockStyle::Tildes
        {
            return Err(BailReason::Classifier);
        }
        // ~keep Tier-2 knows the rule for a link in code. An image in code is its alt text, which
        // ~keep this scanner writes itself.
        if matches!(spec.kind, TagKind::Link) && self.state.escape_ctx.intersects(EscapeCtx::CODE | EscapeCtx::PRE) {
            return Err(BailReason::Classifier);
        }
        Ok(())
    }

    // ~keep Labels that touch surrounding content need the DOM walk's rendered boundaries (#918).
    fn bail_for_image_label(&mut self, name: &[u8], attrs: &[(&[u8], Option<&[u8]>)]) -> Result<(), BailReason> {
        if name != b"img" || self.options.skip_images {
            return Ok(());
        }
        if self.options.inline_data_media != crate::options::InlineDataMedia::Keep {
            let src = decode_attr(find_attr(attrs, b"src").unwrap_or_default())?;
            if crate::converter::media::is_inline_data(&src) {
                return Err(BailReason::Classifier);
            }
        }
        let is_label = self.state.escape_ctx.intersects(EscapeCtx::CODE | EscapeCtx::PRE)
            || !should_keep_image_as_markdown(self.html, &self.state.stack, self.options);
        if !is_label {
            return Ok(());
        }
        let alt = decode_attr(find_attr(attrs, b"alt").unwrap_or_default())?;
        if !alt.chars().any(char::is_alphanumeric) {
            return Ok(());
        }
        let before_touches = self.state.cell_or_output_mut().ends_with(|c: char| !c.is_whitespace())
            && alt.starts_with(|c: char| !c.is_whitespace());
        let after_touches = alt.ends_with(|c: char| !c.is_whitespace())
            && self.html[self.pos..].starts_with(|c: char| !c.is_whitespace())
            && !self.image_ends_heading();
        if before_touches || after_touches {
            return Err(BailReason::Classifier);
        }
        Ok(())
    }

    // ~keep A matching heading close ends the word; nested tags and entity edges remain uncertain (#918).
    fn image_ends_heading(&self) -> bool {
        let Some(heading) = self
            .state
            .stack
            .last()
            .filter(|frame| matches!(frame.spec.kind, TagKind::Heading(_)))
        else {
            return false;
        };
        let Some(tail) = self.bytes[self.pos..].strip_prefix(b"</") else {
            return false;
        };
        let name_end = parse::scan_tag_name(tail, 0);
        let close = parse::skip_ws(tail, name_end);
        tail[..name_end].eq_ignore_ascii_case(&self.bytes[heading.name_range.clone()])
            && tail.get(close) == Some(&b'>')
            && tail[close + 1..].iter().all(u8::is_ascii_whitespace)
    }

    fn prepare_open_tag(&mut self, name_lower: &[u8]) -> Result<(), BailReason> {
        // ~keep Form ancestry cannot decide HTML5's form-pointer semantics; Tier-2 repairs it.
        if name_lower == b"form"
            && self
                .state
                .stack
                .iter()
                .any(|frame| self.bytes[frame.name_range.clone()].eq_ignore_ascii_case(b"form"))
        {
            return Err(BailReason::Classifier);
        }
        // ~keep A heading's code divs use spaces; scanner block newlines would split the code span.
        if name_lower == b"div"
            && self.state.escape_ctx.contains(EscapeCtx::CODE | EscapeCtx::HEADING)
            && !self.state.in_table_cell()
        {
            return Err(BailReason::Classifier);
        }
        // ~keep Outside cells, heading labels need Tier-2's structural flattening and visitor hooks.
        // ~keep The scanner's existing cell path already flattens quote and list markers.
        if self.state.escape_ctx.contains(EscapeCtx::HEADING)
            && !self.state.in_table_cell()
            && matches!(name_lower, b"blockquote" | b"ul" | b"ol" | b"li")
        {
            return Err(BailReason::Classifier);
        }
        // ~keep Only a br or stripped raw-text tag keeps a pending newline join open.
        if !matches!(name_lower, b"br" | b"script" | b"style") {
            self.state.pending_newline_join = None;
        }
        let tag_end = crate::converter::utility::preprocessing::find_tag_end(self.bytes, self.pos + 1)
            .unwrap_or(self.bytes.len());
        let tag_slice = &self.html[self.pos..tag_end];
        if crate::converter::utility::preprocessing::tag_has_hidden_attribute(tag_slice)
            || crate::converter::utility::preprocessing::tag_has_hidden_style(tag_slice)
        {
            return Err(BailReason::HiddenElement { offset: self.pos });
        }
        if crate::converter::main_helpers::starts_body(name_lower) {
            self.state.start_body(self.pos);
        }
        Ok(())
    }

    fn scan_svg(&mut self, name_end: usize) -> Result<bool, BailReason> {
        // ~keep A heading writes a graphic as its text, and a link in a heading that this leaves
        // ~keep empty is named or dropped by rules of the full converter. This scanner has none of
        // ~keep these rules, so it leaves a page with a graphic in a heading to that converter (#766).
        let in_heading = self
            .state
            .stack
            .iter()
            .any(|frame| matches!(frame.spec.kind, TagKind::Heading(_)));
        if in_heading {
            return Err(BailReason::Classifier);
        }
        // ~keep A graphic in code writes its text and no marks; Tier-2 knows the rule.
        if self.state.escape_ctx.intersects(EscapeCtx::CODE | EscapeCtx::PRE) {
            return Err(BailReason::Classifier);
        }
        let tag_open_start = self.pos;
        let Some((close_pos, is_self_closing)) = parse::find_tag_close(self.bytes, name_end) else {
            self.pos = self.bytes.len();
            self.text_start = self.pos;
            return Ok(true);
        };
        let open_tag_end = close_pos + 1;
        let svg_end = if is_self_closing {
            open_tag_end
        } else {
            find_svg_close(self.bytes, open_tag_end).unwrap_or(self.bytes.len())
        };
        let written_from = self.state.cell_or_output_mut().len();
        emit_svg_from_slice(
            &self.html[tag_open_start..svg_end],
            tag_open_start,
            &mut self.state,
            self.options,
        )?;
        self.state.end_document_start_if_written(written_from);
        self.pos = svg_end;
        self.text_start = self.pos;
        Ok(true)
    }

    fn scan_template(&mut self, name_end: usize) -> Result<bool, BailReason> {
        // ~keep Tier-1 skips a template. A caller that keeps hidden content needs Tier-2,
        // ~keep whose preprocessing removes the tags of the templates it keeps.
        if self.options.hidden_content != crate::options::HiddenContent::Drop {
            return Err(BailReason::Classifier);
        }
        let Some((close_pos, is_self_closing)) = parse::find_tag_close(self.bytes, name_end) else {
            self.pos = self.bytes.len();
            self.text_start = self.pos;
            return Ok(true);
        };
        let open_tag_end = close_pos + 1;
        self.pos = if is_self_closing {
            open_tag_end
        } else {
            find_balanced_close(self.bytes, open_tag_end, b"template").unwrap_or(self.bytes.len())
        };
        self.text_start = self.pos;
        Ok(true)
    }

    fn skip_preprocessed_tag(&mut self, name_lower: &[u8], name_end: usize) -> Result<bool, BailReason> {
        if !is_preprocessing_skip_candidate(name_lower) {
            return Ok(false);
        }
        let close = parse::find_tag_close(self.bytes, name_end).ok_or(BailReason::LiteralLt { offset: self.pos })?;
        let attrs_end = if close.1 { close.0.saturating_sub(1) } else { close.0 };
        let attrs = parse::collect_attrs(self.bytes, name_end, attrs_end);
        let is_page_header = name_lower == b"header" && header_is_page_level(&self.state, self.html);
        if !should_skip_preprocessing(name_lower, &attrs, self.options, is_page_header) {
            return Ok(false);
        }
        // ~keep HTML5 removes only the form from its stack at </form>; an unclosed label can
        // ~keep still contain the tail. Nested form starts are ignored. A textual balanced skip
        // ~keep cannot identify the form subtree, so Tier-2 decides what removal drops (#829).
        if name_lower == b"form" {
            return Err(BailReason::Classifier);
        }
        if !self.state.in_table_cell() && self.state.list_continuation_indent_width() > 0 {
            return Err(BailReason::ListItemUnsupportedBlockChild);
        }
        self.state.last_closed_block = true;
        let open_end = close.0 + 1;
        self.pos = if close.1 {
            open_end
        } else {
            find_balanced_close(self.bytes, open_end, name_lower).unwrap_or(self.bytes.len())
        };
        self.text_start = self.pos;
        Ok(true)
    }

    fn close_implicit_tags(&mut self, spec: &TagSpec) -> Result<(), BailReason> {
        while let Some(top) = self.state.stack.last() {
            if !spec_rules::should_close_for_new_tag(top.spec, spec) {
                break;
            }
            emit_close_for_implicit(&mut self.state, self.options, &mut self.table_probes)?;
        }
        Ok(())
    }

    fn prepare_open_metadata(
        &mut self,
        spec: &TagSpec,
        name_lower: &[u8],
        attrs: &[(&[u8], Option<&[u8]>)],
    ) -> Result<(EscapeCtx, u16), BailReason> {
        let prev_ctx = self.state.escape_ctx;
        let ol_start = if matches!(spec.kind, TagKind::List(ListKind::Ordered)) {
            extract_ol_start(attrs)
        } else {
            1
        };
        if matches!(spec.kind, TagKind::Link) {
            let (raw_href, title) = extract_link_attrs(attrs)?;
            // ~keep A link with no address is running text in Tier-2. This scanner writes it as
            // ~keep a label and loses the white space at the ends of its content (`close_link`
            // ~keep has the other end), so it leaves the page to Tier-2. The decision reads the
            // ~keep decoded text of the content: `&#32;`, a comment or an empty element first,
            // ~keep or a space inside the first inline child, is white space at the start too.
            if raw_href.is_none() && white_space_is_upcoming(self.html, self.pos, &self.state.stack) {
                return Err(BailReason::Classifier);
            }
            let href = raw_href.as_deref().map(|value| {
                self.state
                    .effective_base
                    .as_deref()
                    .and_then(|base| crate::converter::url_resolve::resolve_link_url(base, value))
                    .unwrap_or_else(|| value.to_owned())
            });
            let aria_label = find_attr(attrs, b"aria-label").map(decode_attr).transpose()?;
            let has_name =
                crate::converter::utility::content::link_accessible_name(aria_label.as_deref(), title.as_deref())
                    .is_some();
            let same_page = raw_href.as_deref().zip(href.as_deref()).is_some_and(|(raw, resolved)| {
                self.state
                    .own_page
                    .get_or_init(|| {
                        crate::converter::url_resolve::OwnPage::of(self.html, self.options.base_url.as_deref())
                    })
                    .holds(raw, resolved)
            });
            self.state.link_stack.push((href, title, false, has_name || same_page));
        }
        if name_lower == b"abbr" {
            let title = find_attr(attrs, b"title")
                .map(decode_attr)
                .transpose()?
                .map(|value| value.trim().to_owned())
                .filter(|value| self.options.expand_abbreviations && !value.is_empty());
            self.state.abbr_titles.push(title);
        }
        Ok((prev_ctx, ol_start))
    }

    fn scan_rawtext_ignored(
        &mut self,
        name_start: usize,
        name_end: usize,
        name_lower: &[u8],
    ) -> Result<(), BailReason> {
        let open_end = match parse::find_tag_close(self.bytes, name_end) {
            Some(close) => close.0 + 1,
            None => self.bytes.len(),
        };
        // ~keep The rule for the end of raw text is the one Tier-2 uses.
        self.pos = crate::converter::utility::preprocessing::find_raw_text_end_tag(self.bytes, open_end, name_lower)
            .map(|(_, end)| end)
            .ok_or(BailReason::EofWithOpenBlock { open_count: 1 })?;

        // ~keep Tier-2 inserts a boundary space per removed style when its source neighbors
        // ~keep are non-whitespace. Adjacent raw-text tags can coalesce those spaces into a
        // ~keep whitespace-only DOM text node whose rendering the scanner cannot mirror.
        // ~keep Keep the adjacency fallback so Tier-2 handles that case.
        if is_adjacent_rawtext_ignored_open(self.bytes, self.pos) {
            return Err(BailReason::AdjacentRawTextTags { offset: self.pos });
        }
        // ~keep Tier-2 reads the whitespace on both sides of the removed element as one
        // ~keep text, so `First\n<script>x</script>\n<br>` ends in a blank line, as
        // ~keep `First\n\n<br>` does: the `<br>` must not remove the join (issue #683).
        let after = &self.bytes[self.pos..];
        let after_ws = &after[..after
            .iter()
            .position(|b| !b.is_ascii_whitespace())
            .unwrap_or(after.len())];
        let newline_before = name_start >= 2 && matches!(self.bytes[name_start - 2], b'\n' | b'\r');
        if (newline_before && after_ws.first().is_some_and(|byte| matches!(byte, b'\n' | b'\r')))
            || contains_blank_line(after_ws)
        {
            self.state.pending_newline_join = None;
        }

        self.text_start = self.pos;
        // ~keep Mirror the style boundary-space rule: a space is
        // inserted only when the removed style would otherwise glue two
        // word characters together.  The "before" check uses the emitted
        // output tail (not the raw source byte) so that a space already
        // produced by a preceding sibling is never doubled up; the "after"
        // check peeks the next source byte, matching Tier-2's boundary
        // condition exactly.
        if name_lower == b"style" && !self.state.escape_ctx.contains(EscapeCtx::PRE) {
            let after_is_word = self.pos < self.bytes.len() && !self.bytes[self.pos].is_ascii_whitespace();
            if after_is_word {
                let dest = self.state.cell_or_output_mut();
                let ends_with_word = !dest.is_empty()
                    && !dest.ends_with(' ')
                    && !dest.ends_with('\t')
                    && !dest.ends_with('\n')
                    && !dest.ends_with('<')
                    && !dest.ends_with("<br>");
                if ends_with_word {
                    dest.push(' ');
                }
            }
        }
        Ok(())
    }

    fn scan_ignored(&mut self, spec: &TagSpec, name_end: usize, name_lower: &[u8]) -> Result<(), BailReason> {
        let open_end = match parse::find_tag_close(self.bytes, name_end) {
            Some(close) => close.0 + 1,
            None => self.bytes.len(),
        };
        if spec.is_void {
            self.pos = open_end;
            self.text_start = self.pos;
            return Ok(());
        }
        // ~keep With no end tag, the start tag of the body content ends the `<head>`; only the
        // ~keep HTML tree builder knows where, so Tier-2 converts the document (issue #772).
        let Some((close_start, close_end)) = find_close_tag_range(self.bytes, open_end, name_lower) else {
            return Err(BailReason::EofWithOpenBlock { open_count: 1 });
        };
        if self.state.head_range.is_none() {
            self.state.head_range = Some(open_end..close_start);
        }
        self.pos = close_end;
        self.text_start = self.pos;
        Ok(())
    }

    fn advance_text(&mut self) {
        match memchr2(b'<', b'&', &self.bytes[self.pos..]) {
            Some(offset) if offset > 0 => self.pos += offset,
            Some(_) => self.pos += 1,
            None => self.pos = self.bytes.len(),
        }
    }
}

fn collect_open_attrs<'a>(
    spec: &TagSpec,
    name_lower: &[u8],
    bytes: &'a [u8],
    name_end: usize,
    close: (usize, bool),
) -> Vec<(&'a [u8], Option<&'a [u8]>)> {
    let needs_attrs = matches!(
        spec.kind,
        TagKind::Link
            | TagKind::Image
            | TagKind::List(ListKind::Ordered)
            | TagKind::Table
            | TagKind::TableCell { .. }
            | TagKind::Pre
            | TagKind::Code
            | TagKind::Blockquote
    ) || matches!(name_lower, b"abbr" | b"input");
    if !needs_attrs {
        return Vec::new();
    }
    let attrs_end = if close.1 { close.0.saturating_sub(1) } else { close.0 };
    parse::collect_attrs(bytes, name_end, attrs_end)
}

/// ~keep Custom elements use inline passthrough semantics; unknown standard tags bail.
fn resolve_tag_spec(name_lower: &[u8], tag_name: &[u8], offset: usize) -> Result<&'static TagSpec, BailReason> {
    if name_lower.contains(&b'-') {
        return Ok(&CUSTOM_ELEMENT_INLINE_SPEC);
    }
    tier1::lookup(name_lower).ok_or_else(|| BailReason::UnknownCustomElement {
        name: bytes_to_string(tag_name).into(),
        offset,
    })
}

fn validate_cell_open(state: &mut Tier1State, spec: &TagSpec, name_lower: &[u8]) -> Result<(), BailReason> {
    if cell_needs_tier2(state, spec, name_lower) {
        return Err(BailReason::TableBlockChildInCell);
    }
    let inlineable = matches!(
        spec.kind,
        TagKind::Paragraph
            | TagKind::Block
            | TagKind::Summary
            | TagKind::Figcaption
            | TagKind::Blockquote
            | TagKind::Pre
            | TagKind::List(_)
            | TagKind::ListItem
            | TagKind::Heading(_)
            | TagKind::DefinitionTerm
            | TagKind::DefinitionDescription
            | TagKind::Table
    );
    if state.in_table_cell() && spec.is_block && !inlineable {
        return Err(BailReason::TableBlockChildInCell);
    }
    Ok(())
}

fn validate_list_open(state: &mut Tier1State, spec: &TagSpec, name_lower: &[u8]) -> Result<(), BailReason> {
    if let TagKind::List(kind) = spec.kind {
        let ordered_ancestor = find_parent_list_kind(&state.stack) == Some(ListKind::Ordered);
        if kind != ListKind::Definition && state.list_depth > 0 && (kind == ListKind::Ordered || ordered_ancestor) {
            return Err(BailReason::ListNestedOrdered);
        }
        if let Some(end) = state.last_ordered_list_end.filter(|_| kind == ListKind::Ordered) {
            if state
                .cell_or_output_mut()
                .get(end..)
                .is_some_and(|rest| rest.trim().is_empty())
            {
                return Err(BailReason::OrderedListAfterOrderedList);
            }
        }
    }
    validate_list_item_block(state, spec, name_lower)
}

fn validate_list_item_block(state: &Tier1State, spec: &TagSpec, name_lower: &[u8]) -> Result<(), BailReason> {
    if state.in_table_cell() || state.list_continuation_indent_width() == 0 {
        return Ok(());
    }
    let bare_marker = line_is_bare_list_marker(&state.output);
    let unsupported = match spec.kind {
        TagKind::Blockquote | TagKind::Table | TagKind::Heading(_) => true,
        TagKind::Block | TagKind::List(ListKind::Definition) => true,
        TagKind::Paragraph => !bare_marker,
        TagKind::Pre => bare_marker,
        TagKind::List(_) | TagKind::ListItem | TagKind::DefinitionTerm | TagKind::DefinitionDescription => false,
        _ => is_block_tag(name_lower) && !bare_marker,
    };
    if unsupported {
        Err(BailReason::ListItemUnsupportedBlockChild)
    } else {
        Ok(())
    }
}

fn check_open_depth(state: &Tier1State, options: &ConversionOptions) -> Result<(), BailReason> {
    let max_depth = crate::converter::main_helpers::effective_max_depth(options);
    if state.stack.len() >= max_depth {
        return Err(BailReason::DepthLimitExceeded {
            depth: state.stack.len(),
            max_depth,
        });
    }
    Ok(())
}

fn push_open_frame(
    state: &mut Tier1State,
    spec: &'static TagSpec,
    name_lower: &[u8],
    name_range: std::ops::Range<usize>,
    prev_ctx: EscapeCtx,
    ol_start: u16,
) {
    let content_start = state.cell_or_output_mut().len();
    state.stack.push(OpenTag {
        spec,
        content_start,
        prev_escape_ctx: prev_ctx,
        list_index: 0,
        ol_start,
        name_range,
        dropped_whitespace_only_text: false,
        own_buffer: renders_into_own_buffer(spec.kind, name_lower, prev_ctx),
        starts_with_whitespace: false,
        texts_before: state.text_count,
    });
}

/// The source text `source` as the decisions on it read it: with its references to white space
/// decoded and, outside code and `pre`, each carriage return a line feed.
fn text_for_decisions<'source>(source: &'source str, state: &Tier1State) -> std::borrow::Cow<'source, str> {
    use crate::converter::utility::white_space::{with_line_feeds, with_white_space_references_decoded};

    let text = with_white_space_references_decoded(source);
    if state.escape_ctx.intersects(EscapeCtx::CODE | EscapeCtx::PRE) {
        text
    } else {
        with_line_feeds(text)
    }
}

/// ~keep EOF closes open frames, then applies Tier-2's trim/collapse/trailing-newline order.
fn finish_scan(
    mut state: Tier1State,
    html: &str,
    text_start: usize,
    pos: usize,
    options: &ConversionOptions,
    mut table_probes: Vec<TableLayoutProbe>,
) -> Result<ScanOutput, BailReason> {
    if text_start < pos {
        let text = text_for_decisions(&html[text_start..pos], &state);
        flush_text(
            &mut state,
            TextFlush {
                raw: &text,
                base_offset: text_start,
                upcoming: UpcomingTextSibling::default(),
                br_in_tables: options.br_in_tables,
                output_format: options.output_format,
            },
        )?;
    }

    while !state.stack.is_empty() {
        let buf = &mut state.output;
        while matches!(buf.as_bytes().last(), Some(b' ' | b'\t' | b'\n' | b'\r')) {
            buf.pop();
        }
        emit_close_for_implicit(&mut state, options, &mut table_probes)?;
    }

    crate::converter::main_helpers::trim_line_end_whitespace(&mut state.output, options.code_block_style);
    crate::converter::main_helpers::collapse_excess_blank_lines(&mut state.output, options.code_block_style);

    if !state.output.is_empty() {
        let trimmed_end = state.output.trim_end_matches('\n');
        if trimmed_end.is_empty() {
            state.output.clear();
        } else {
            let trimmed_len = trimmed_end.len();
            state.output.truncate(trimmed_len);
            state.output.push('\n');
        }
    }

    Ok(ScanOutput {
        body: state.output,
        head_range: state.head_range,
    })
}

// ~keep ── Bail guard ────────────────────────────────────────────────────────────────

/// Return `Err(BailReason::Classifier)` for tag kinds not supported in M9.
///
/// Table-related tags are now handled by the scanner (M9); they are no longer
/// bailed here.  Table-specific bail reasons are emitted by the table-handling
/// code in `emit_open` and `emit_close`.
/// Locate the matching close tag for `tag_name` starting at `open_end`.
///
/// Returns `Some((close_start, close_end))` where `close_start` is the byte
/// index of the `<` opening the `</tag>` close and `close_end` is the byte
/// index immediately after its `>`.  `None` when no matching close exists.
///
/// Used by `<head>` silent-skip to record the content slice
/// (`open_end..close_start`) for metadata extraction while advancing past the
/// entire `<head>…</head>` block.
fn find_close_tag_range(bytes: &[u8], open_end: usize, tag_name: &[u8]) -> Option<(usize, usize)> {
    let mut idx = open_end;
    while idx < bytes.len() {
        match memchr3(b'<', b'<', b'<', &bytes[idx..]) {
            Some(off) => idx += off,
            None => return None,
        }
        let is_close = bytes.get(idx + 1) == Some(&b'/');
        let name_start = idx + usize::from(is_close) + 1;
        if is_close && let Some(after) = matching_tag_after(bytes, name_start, tag_name) {
            return find_closing_bracket(bytes, after).map(|end| (idx, end));
        }
        idx += 1;
    }
    None
}

// ~keep ── SVG helpers ───────────────────────────────────────────────────────────────

/// Find the byte offset immediately after the matching `</svg>` close tag,
/// starting from `open_end` (the byte after the `>` of the opening `<svg ...>`).
///
/// Tracks nesting depth so nested `<svg>` elements (valid in SVG 1.1) are
/// handled correctly.  Returns `None` when no matching close is found.
fn find_svg_close(bytes: &[u8], open_end: usize) -> Option<usize> {
    find_balanced_close(bytes, open_end, b"svg")
}

/// Find the byte offset immediately after the matching close tag for
/// `tag_name`, starting from `open_end` (the byte after the `>` of the
/// opening tag).  Tracks nesting depth so nested same-name elements are
/// handled correctly.  Returns `None` when no matching close is found.
fn find_balanced_close(bytes: &[u8], open_end: usize, tag_name: &[u8]) -> Option<usize> {
    let mut idx = open_end;
    let mut depth = 1usize;
    while idx < bytes.len() {
        match memchr::memchr(b'<', &bytes[idx..]) {
            Some(off) => idx += off,
            None => return None,
        }
        let is_close = bytes.get(idx + 1) == Some(&b'/');
        let name_start = idx + usize::from(is_close) + 1;
        let Some(after) = matching_tag_after(bytes, name_start, tag_name) else {
            idx += 1;
            continue;
        };
        if is_close {
            depth -= 1;
            if depth == 0 {
                return find_closing_bracket(bytes, after);
            }
        } else if !tag_is_self_closing(bytes, after) {
            depth += 1;
        }
        idx += 1;
    }
    None
}

fn matching_tag_after(bytes: &[u8], name_start: usize, tag_name: &[u8]) -> Option<usize> {
    let after = name_start.checked_add(tag_name.len())?;
    if after > bytes.len() || !bytes[name_start..after].eq_ignore_ascii_case(tag_name) {
        return None;
    }
    matches!(
        bytes.get(after),
        Some(b'>' | b'/' | b' ' | b'\t' | b'\n' | b'\r') | None
    )
    .then_some(after)
}

fn find_closing_bracket(bytes: &[u8], after_name: usize) -> Option<usize> {
    memchr::memchr(b'>', bytes.get(after_name..)?).map(|offset| after_name + offset + 1)
}

fn tag_is_self_closing(bytes: &[u8], after_name: usize) -> bool {
    parse::find_tag_close(bytes, after_name).is_some_and(|(_, self_closing)| self_closing)
}