html-to-markdown-rs 3.19.1

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
//! SVG and `MathML` element handling with serialization and base64 encoding.

use crate::converter::main_helpers::effective_max_depth;
use crate::converter::media::MediaContext;
use crate::converter::utility::content::normalized_tag_name;
use crate::converter::utility::escaping::escape_link_label;
use crate::converter::utility::serialization::escape_html_attribute_value;
use crate::converter::utility::svg_attrs::canonical_svg_attr;
use crate::options::conversion::NATIVE_STACK_SAFE_DEPTH;
use crate::options::{ConversionOptions, HiddenContent};
// ~keep reason: BTreeMap is only used when the inline-images feature is active.
#[allow(unused_imports)]
use std::collections::BTreeMap;
use tl::{NodeHandle, Parser};

#[cfg(feature = "inline-images")]
use crate::inline_images::{InlineImageBuild, InlineImageCollector, InlineImageFormat, InlineImageSource};

#[cfg(feature = "inline-images")]
type InlineCollectorHandle = std::rc::Rc<std::cell::RefCell<InlineImageCollector>>;

/// Handle inline SVG elements with size limits and base64 encoding.
///
/// # Features
/// - SVG serialization to HTML string
/// - Size validation with configurable limits
/// - Base64 encoding for data URI
/// - Metadata extraction (aria-label, title, dimensions)
#[cfg(feature = "inline-images")]
#[allow(clippy::trivially_copy_pass_by_ref)]
#[allow(clippy::needless_pass_by_value)]
#[allow(clippy::option_if_let_else)]
pub fn handle_inline_svg(
    collector_ref: &InlineCollectorHandle,
    node_handle: &NodeHandle,
    parser: &Parser,
    title_opt: Option<String>,
    attributes: BTreeMap<String, String>,
) {
    let max_size = {
        let borrow = collector_ref.borrow();
        if !borrow.capture_svg() {
            return;
        }
        borrow.max_decoded_size()
    };

    if max_size == 0 {
        let mut collector = collector_ref.borrow_mut();
        let index = collector.next_index();
        collector.warn_skip(index, "max SVG payload size is zero");
        return;
    }

    let mut collector = collector_ref.borrow_mut();
    let index = collector.next_index();

    let serialized = serialize_element(node_handle, parser);
    if serialized.is_empty() {
        collector.warn_skip(index, "unable to serialize SVG element");
        return;
    }

    let data = serialized.into_bytes();
    if data.len() as u64 > max_size {
        collector.warn_skip(
            index,
            format!(
                "serialized SVG payload ({} bytes) exceeds configured max ({})",
                data.len(),
                max_size
            ),
        );
        return;
    }

    let description = attributes
        .get("aria-label")
        .and_then(|value| non_empty_trimmed(value))
        .or_else(|| title_opt.as_deref().and_then(non_empty_trimmed));

    let filename_candidate = attributes
        .get("data-filename")
        .cloned()
        .or_else(|| attributes.get("filename").cloned())
        .or_else(|| attributes.get("data-name").cloned());

    let image = collector.build_image(InlineImageBuild {
        data,
        format: InlineImageFormat::Svg,
        filename: filename_candidate,
        description,
        dimensions: None,
        source: InlineImageSource::SvgElement,
        attributes,
    });

    collector.push_image(index, image);
}

/// Serialize an element to HTML string (for SVG and Math elements).
///
/// Attributes are sorted by name to guarantee deterministic output across
/// process invocations (the underlying parser stores them in a `HashMap`).
///
/// Depth-guarded by [`NATIVE_STACK_SAFE_DEPTH`]. Callers that already track the
/// element's depth in the wider DOM walk (`handle_svg`, `handle_math`) should call
/// [`serialize_element_at_depth`] instead, so the same recursion budget the caller is
/// already spending against `effective_max_depth` is honored here too.
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn serialize_element(node_handle: &NodeHandle, parser: &Parser) -> String {
    serialize_element_at_depth(node_handle, parser, 0, NATIVE_STACK_SAFE_DEPTH)
}

/// Serialize a node to HTML string.
///
/// See [`serialize_element`] for the depth-guard contract.
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn serialize_node(node_handle: &NodeHandle, parser: &Parser) -> String {
    serialize_node_at_depth(node_handle, parser, 0, NATIVE_STACK_SAFE_DEPTH)
}

/// Serialize an element to HTML string, stopping descent once `depth` reaches `max_depth`.
///
/// Mutually recursive with [`serialize_node_at_depth`] over `tag.children()`. Follows the
/// same convention as the main DOM walker (`walk_node` in `converter/main.rs`): the depth
/// counter increments on every descent into a child, and reaching the limit stops further
/// descent rather than erroring, so a pathologically nested `<svg>`/`<math>` subtree cannot
/// overflow the stack (audit #23). The element's own opening tag and attributes are always
/// emitted; only its descendants are dropped once the budget is exhausted.
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn serialize_element_at_depth(node_handle: &NodeHandle, parser: &Parser, depth: usize, max_depth: usize) -> String {
    if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
        let tag_name = normalized_tag_name(tag.name().as_utf8_str());
        let mut html = String::with_capacity(256);
        html.push('<');
        html.push_str(&tag_name);

        let mut attrs: Vec<_> = tag.attributes().iter().collect();
        attrs.sort_by(|(a, _), (b, _)| a.as_ref().cmp(b.as_ref()));
        for (key, value_opt) in attrs {
            html.push(' ');
            // ~keep Restore camelCase for SVG/MathML attributes whose canonical
            // ~keep WHATWG spelling is mixed-case.  tl lowercases all attribute
            // ~keep names when it re-parses a wrapped fragment (Tier-1 path via
            // ~keep emit_svg_from_slice), but preserves case on a full-document
            // ~keep parse (Tier-2 path).  Applying the lookup in both paths is
            // ~keep safe: Tier-2 already has the correct spelling so the lookup
            // ~keep returns None and the original key is used unchanged.
            let key_str = key.as_ref();
            let canonical = canonical_svg_attr(key_str);
            html.push_str(canonical.unwrap_or(key_str));
            if let Some(value) = value_opt {
                // ~keep Treat empty value identically to a bare attribute.  When tl
                // ~keep re-parses a wrapped SVG slice (Tier-1's emit_svg_from_slice)
                // ~keep it yields `None` for `attr=""` while a single full-document
                // ~keep parse (Tier-2) yields `Some("")`.  Both forms are
                // ~keep HTML5-equivalent; normalise here so both tiers produce
                // ~keep byte-identical output.
                if !value.is_empty() {
                    html.push_str("=\"");
                    html.push_str(&escape_html_attribute_value(&value));
                    html.push('"');
                }
            }
        }

        let has_children = !tag.children().top().is_empty();
        if has_children {
            html.push('>');
            if depth >= max_depth {
                tracing::warn!(
                    target: "html_to_markdown::convert",
                    max_depth,
                    tag = %tag_name,
                    "SVG/MathML serialization reached the effective depth limit; descendants were skipped"
                );
            } else {
                let children = tag.children();
                for child_handle in children.top().iter() {
                    html.push_str(&serialize_node_at_depth(child_handle, parser, depth + 1, max_depth));
                }
            }
            html.push_str("</");
            html.push_str(&tag_name);
            html.push('>');
        } else {
            html.push_str(" />");
        }
        return html;
    }
    String::new()
}

/// Serialize a node to HTML string, stopping descent once `depth` reaches `max_depth`.
///
/// See [`serialize_element_at_depth`] for the depth-guard contract.
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn serialize_node_at_depth(node_handle: &NodeHandle, parser: &Parser, depth: usize, max_depth: usize) -> String {
    if let Some(node) = node_handle.get(parser) {
        match node {
            tl::Node::Raw(bytes) => bytes.as_utf8_str().to_string(),
            tl::Node::Tag(_) => serialize_element_at_depth(node_handle, parser, depth, max_depth),
            _ => String::new(),
        }
    } else {
        String::new()
    }
}

/// Extract non-empty trimmed string or return None.
#[cfg(feature = "inline-images")]
fn non_empty_trimmed(value: &str) -> Option<String> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        None
    } else {
        Some(trimmed.to_string())
    }
}

/// Where the walk of [`graphic_text`] is, which decides what a child gives.
#[derive(Clone, Copy)]
enum GraphicScope {
    /// Among drawn elements. `of_svg`: the parent is an `svg` element, whose `title` and `desc`
    /// name the graphic. `named`: no `aria-hidden="true"` hides that name.
    Drawing { of_svg: bool, named: bool },
    /// Inside a `title` or a `desc`: every text node.
    Label,
    /// Inside a `text` element: every text node but the tooltip of a part.
    Text,
    /// Inside a `foreignObject`: HTML.
    Html,
}

enum GraphicStep {
    Visit(tl::NodeHandle, GraphicScope),
    Space,
    EndPart,
}

/// Collects the parts of [`graphic_text`], each with its white space collapsed.
#[derive(Default)]
struct GraphicParts {
    use_accessible_names: bool,
    label: Option<String>,
    parts: Vec<String>,
    current: String,
}

impl GraphicParts {
    fn end_part(&mut self) {
        let part = self.current.split_whitespace().collect::<Vec<_>>().join(" ");
        self.current.clear();
        // ~keep A title that repeats the `aria-label` is one name, not two.
        if !part.is_empty() && self.label.as_deref() != Some(part.as_str()) {
            self.parts.push(part);
        }
    }
}

fn is_aria_hidden(tag: &tl::HTMLTag<'_>) -> bool {
    tag.attributes()
        .get("aria-hidden")
        .flatten()
        .is_some_and(|value| value.as_utf8_str().trim().eq_ignore_ascii_case("true"))
}

/// Whether the `display` or `visibility` attribute of an element of a graphic hides it.
///
/// ~keep The two attributes are the SVG spelling of the style declarations with the same names.
/// ~keep The function that judges a `style` attribute judges them, so text in a graphic and text
/// ~keep outside one are hidden by one decision. An element hidden by its `style` or by the
/// ~keep `hidden` attribute is already gone when the graphic is parsed. A caller that keeps hidden
/// ~keep content keeps this text too, so no element is hidden for it.
fn is_hidden_in_graphic(tag: &tl::HTMLTag<'_>, hidden_content: HiddenContent) -> bool {
    use crate::converter::utility::preprocessing::{HiddenStyleReason, style_value_hidden_reason};

    hidden_content == HiddenContent::Drop
        && ["display", "visibility"].iter().any(|property| {
            tag.attributes().get(*property).flatten().is_some_and(|value| {
                let declaration = format!("{property}:{}", value.as_utf8_str());
                style_value_hidden_reason(&declaration) == Some(HiddenStyleReason::Definitive)
            })
        })
}

/// Whether the `systemLanguage` test of a child of a `switch` holds for a reader of English.
fn system_language_holds(tag: &tl::HTMLTag<'_>) -> bool {
    // ~keep The parser keeps the case of the name in a page and lowers it in a fragment.
    let Some(value) = ["systemLanguage", "systemlanguage"]
        .iter()
        .find_map(|key| tag.attributes().get(*key))
    else {
        return true;
    };
    value.is_some_and(|value| {
        value.as_utf8_str().split(',').any(|language| {
            let language = language.trim();
            language.eq_ignore_ascii_case("en")
                || language
                    .get(..3)
                    .is_some_and(|prefix| prefix.eq_ignore_ascii_case("en-"))
        })
    })
}

/// The text of an inline `<svg>` for a reader who does not get the picture, on one line.
///
/// In reading order: the `aria-label` of the graphic, then the `title` and `desc` of each `svg`
/// element, each `text` element and each `foreignObject`, joined by one space. Only elements that
/// draw their children are entered (`svg`, `g`, `a`, the child of `switch` that a reader of
/// English gets), so `defs`, `symbol`, `style`, `script` and `metadata` give nothing, and a `use`
/// reference is not followed. An element with `display="none"` or `visibility="hidden"` gives
/// nothing, unless `hidden_content` keeps hidden text. `aria-hidden="true"` removes the label,
/// the title and the description, and keeps drawn text. A graphic with none of these gives the
/// empty string. When `use_accessible_names` is false, only drawn text is collected.
pub fn graphic_text(
    svg: &tl::HTMLTag<'_>,
    parser: &Parser<'_>,
    options: &ConversionOptions,
    policy: crate::convert_api::AccessibleNamePolicy,
) -> String {
    let hidden_content = options.hidden_content;
    if is_hidden_in_graphic(svg, hidden_content) {
        return String::new();
    }
    let named = policy.0 && !is_aria_hidden(svg);
    let mut out = GraphicParts {
        use_accessible_names: policy.0,
        ..GraphicParts::default()
    };
    if named {
        if let Some(label) = crate::converter::utility::attributes::decoded_attribute(svg, "aria-label") {
            out.current.push_str(&label);
            out.end_part();
            out.label = out.parts.first().cloned();
        }
    }
    // ~keep An explicit stack: the nesting is the page's, and native recursion over it can overflow.
    let mut stack = Vec::new();
    push_graphic_children(&mut stack, svg, GraphicScope::Drawing { of_svg: true, named });
    while let Some(step) = stack.pop() {
        let (handle, scope) = match step {
            GraphicStep::Visit(handle, scope) => (handle, scope),
            GraphicStep::Space => {
                out.current.push(' ');
                continue;
            }
            GraphicStep::EndPart => {
                out.end_part();
                continue;
            }
        };
        match handle.get(parser) {
            Some(tl::Node::Raw(bytes)) if !matches!(scope, GraphicScope::Drawing { .. }) => {
                out.current
                    .push_str(&crate::text::decode_html_entities_cow(bytes.as_utf8_str().as_ref()));
            }
            Some(tl::Node::Tag(tag)) if is_hidden_in_graphic(tag, hidden_content) => {}
            Some(tl::Node::Tag(tag)) => visit_graphic_tag(tag, parser, scope, &mut stack, &mut out),
            _ => {}
        }
    }
    out.end_part();
    out.parts.join(" ")
}

fn push_graphic_children(stack: &mut Vec<GraphicStep>, tag: &tl::HTMLTag<'_>, scope: GraphicScope) {
    stack.extend(
        tag.children()
            .top()
            .as_slice()
            .iter()
            .rev()
            .map(|child| GraphicStep::Visit(*child, scope)),
    );
}

/// Schedules the children of `tag` as one part of the text.
fn push_graphic_part(stack: &mut Vec<GraphicStep>, out: &mut GraphicParts, tag: &tl::HTMLTag<'_>, scope: GraphicScope) {
    out.end_part();
    stack.push(GraphicStep::EndPart);
    push_graphic_children(stack, tag, scope);
}

fn visit_graphic_tag(
    tag: &tl::HTMLTag<'_>,
    parser: &Parser<'_>,
    scope: GraphicScope,
    stack: &mut Vec<GraphicStep>,
    out: &mut GraphicParts,
) {
    let name = normalized_tag_name(tag.name().as_utf8_str());
    let nested_svg = |named: bool| GraphicScope::Drawing {
        of_svg: true,
        named: named && !is_aria_hidden(tag),
    };
    match (scope, &*name) {
        (GraphicScope::Drawing { of_svg, named }, "title" | "desc") => {
            if of_svg && named {
                push_graphic_part(stack, out, tag, GraphicScope::Label);
            }
        }
        (GraphicScope::Drawing { .. }, "text") => push_graphic_part(stack, out, tag, GraphicScope::Text),
        (GraphicScope::Drawing { .. }, "foreignobject") => push_graphic_part(stack, out, tag, GraphicScope::Html),
        (GraphicScope::Drawing { named, .. }, "svg") => push_graphic_children(stack, tag, nested_svg(named)),
        (GraphicScope::Drawing { named, .. }, "g" | "a") => {
            push_graphic_children(stack, tag, GraphicScope::Drawing { of_svg: false, named });
        }
        (GraphicScope::Drawing { named, .. }, "switch") => {
            // ~keep A `switch` draws one child: the first element whose test attributes hold. The
            // ~keep converter knows no language of the reader, so a `systemLanguage` holds only
            // ~keep when it names English, and a child without the attribute is the fallback.
            let first = tag
                .children()
                .top()
                .iter()
                .find(|child| matches!(child.get(parser), Some(tl::Node::Tag(child_tag)) if system_language_holds(child_tag)))
                .copied();
            stack.extend(first.map(|child| GraphicStep::Visit(child, GraphicScope::Drawing { of_svg: false, named })));
        }
        (GraphicScope::Drawing { .. }, _) => {}
        (GraphicScope::Label, _) => push_graphic_children(stack, tag, GraphicScope::Label),
        (GraphicScope::Text, "title" | "desc") | (GraphicScope::Html, "script" | "style" | "template") => {}
        (GraphicScope::Text, _) => {
            push_graphic_children(stack, tag, GraphicScope::Text);
            // ~keep A span with its own `x` or `y` starts at a new place, as a new line of a label does.
            if ["x", "y"].iter().any(|key| tag.attributes().get(*key).is_some()) {
                stack.push(GraphicStep::Space);
            }
        }
        (GraphicScope::Html, "svg") => {
            out.end_part();
            stack.push(GraphicStep::EndPart);
            push_graphic_children(stack, tag, nested_svg(out.use_accessible_names));
        }
        (GraphicScope::Html, "br") => out.current.push(' '),
        (GraphicScope::Html, html_name) => {
            let is_block = crate::converter::utility::content::is_block_level_element(html_name);
            if is_block {
                stack.push(GraphicStep::Space);
            }
            push_graphic_children(stack, tag, GraphicScope::Html);
            if is_block {
                stack.push(GraphicStep::Space);
            }
        }
    }
}

/// Handle SVG element conversion to Markdown.
///
/// Handles inline image collection, and writes either the text of the graphic (in inline mode
/// and for `alt_text_only`) or a base64-encoded image with that text as its alt text.
pub fn handle_svg(
    node_handle: &NodeHandle,
    tag: &tl::HTMLTag,
    parser: &Parser,
    output: &mut String,
    context: MediaContext<'_>,
) {
    let MediaContext { options, ctx, .. } = context;

    #[cfg(feature = "inline-images")]
    if let Some(ref collector_ref) = ctx.inline_collector {
        collect_inline_svg(collector_ref, *node_handle, tag, parser, context.dom_ctx);
    }

    // ~keep The converter writes an inline SVG as a `data:` URL it builds itself, so the
    // ~keep inline-data choice always applies.
    if options.skip_images {
        return;
    }
    let inline_data = super::inline_data_treatment(options.inline_data_media, "data:image/svg+xml");
    if inline_data == crate::options::InlineDataMedia::DropElement {
        // ~keep Only this choice removes the element. A link around a graphic that is written as
        // ~keep its text keeps its address also when that text is empty, so the flag stays unset.
        ctx.inline_data_replaced.set(true);
        return;
    }

    let title = graphic_text(tag, parser, options, ctx.accessible_names);
    if ctx.in_code || ctx.convert_as_inline || inline_data == crate::options::InlineDataMedia::AltTextOnly {
        super::record_text_label(&title, ctx);
    }
    // ~keep Code shows every character as text, so a graphic in code writes its text and no marks.
    if ctx.in_code {
        output.push_str(&title);
    } else if ctx.convert_as_inline || inline_data == crate::options::InlineDataMedia::AltTextOnly {
        write_graphic_as_text(*node_handle, parser, output, context, &title);
    } else {
        write_graphic_as_image(*node_handle, parser, output, context, &title);
    }
}

/// Gives an inline `<svg>` to the collector of inline images, with the text of its `title` child
/// and the attributes that describe it.
#[cfg(feature = "inline-images")]
fn collect_inline_svg(
    collector_ref: &InlineCollectorHandle,
    node_handle: NodeHandle,
    tag: &tl::HTMLTag,
    parser: &Parser,
    dom_ctx: &crate::converter::DomContext,
) {
    let title_opt = tag
        .children()
        .top()
        .iter()
        .find(|child| {
            matches!(child.get(parser), Some(tl::Node::Tag(child_tag)) if child_tag.name().as_utf8_str().eq_ignore_ascii_case("title"))
        })
        .map(|child| {
            crate::converter::utility::content::get_text_content(child, parser, dom_ctx)
                .trim()
                .to_string()
        });
    let mut attributes_map = BTreeMap::new();
    for (key, value_opt) in tag.attributes().iter() {
        let key_str = key.to_string();
        let keep = key_str == "width"
            || key_str == "height"
            || key_str == "filename"
            || key_str == "aria-label"
            || key_str.starts_with("data-");
        if keep {
            let value = value_opt.map(|value| value.to_string()).unwrap_or_default();
            attributes_map.insert(key_str, value);
        }
    }
    handle_inline_svg(collector_ref, &node_handle, parser, title_opt, attributes_map);
}

/// Writes the text of a graphic as running text.
fn write_graphic_as_text(
    node_handle: NodeHandle,
    parser: &Parser,
    output: &mut String,
    context: MediaContext<'_>,
    title: &str,
) {
    let MediaContext {
        options, ctx, dom_ctx, ..
    } = context;
    // ~keep Written as running text through the function a text node uses, so it gets the same
    // ~keep escaping, also where it starts a line.
    let escaped = crate::text::escape(
        title,
        options.escape_misc,
        options.escape_asterisks,
        options.escape_underscores,
        options.escape_ascii,
    );
    if !escaped.is_empty() {
        crate::converter::text_node::push_running_text(
            output,
            &escaped,
            crate::converter::text_node::TextSite {
                node_handle: &node_handle,
                parser,
                options,
                ctx,
                dom_ctx,
            },
        );
    }
}

/// Writes a graphic as an image with a base64 `data:` URL, with the text of the graphic as its
/// alt text.
fn write_graphic_as_image(
    node_handle: NodeHandle,
    parser: &Parser,
    output: &mut String,
    context: MediaContext<'_>,
    title: &str,
) {
    use base64::{Engine as _, engine::general_purpose::STANDARD};

    let MediaContext { options, depth, .. } = context;
    // ~keep The recursive serializer gets 64 levels from this SVG's root, bounded also
    // ~keep by the remaining conversion budget; outer HTML consumes no native frames (#924).
    let max_depth = effective_max_depth(options).min(depth.saturating_add(NATIVE_STACK_SAFE_DEPTH));
    let svg_html = serialize_element_at_depth(&node_handle, parser, depth, max_depth);
    let base64_svg = STANDARD.encode(svg_html.as_bytes());

    output.push_str("![");
    output.push_str(&escape_link_label(title));
    output.push_str("](data:image/svg+xml;base64,");
    output.push_str(&base64_svg);
    output.push(')');
}

/// Handle `MathML` element conversion to Markdown.
///
/// Outputs visible `MathML` text content with escaping.
pub fn handle_math(
    node_handle: &NodeHandle,
    tag: &tl::HTMLTag,
    parser: &Parser,
    output: &mut String,
    context: MediaContext<'_>,
) {
    let MediaContext {
        options, ctx, dom_ctx, ..
    } = context;
    use crate::converter::utility::content::get_math_text_content;
    use crate::text;

    let text_content = get_math_text_content(node_handle, parser, dom_ctx, options.hidden_content);

    if text_content.is_empty() {
        return;
    }

    let escaped_text = text::escape(
        &text_content,
        options.escape_misc,
        options.escape_asterisks,
        options.escape_underscores,
        options.escape_ascii,
    );

    let is_display_block = tag
        .attributes()
        .get("display")
        .flatten()
        .is_some_and(|v| v.as_utf8_str() == "block");

    if is_display_block && !ctx.in_paragraph && !ctx.convert_as_inline {
        output.push_str("\n\n");
    }

    output.push_str(&escaped_text);

    if is_display_block && !ctx.in_paragraph && !ctx.convert_as_inline {
        output.push_str("\n\n");
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn should_escape_svg_title_caption_that_would_open_a_new_image_link() {
        // ~keep audit #24 finding 5: the `<svg><title>` used as the data-URI image caption was
        // pushed unescaped, so an inert `]`/`(` in it could manufacture a second, live image
        // pointing at an attacker-controlled URL — same mechanism as finding 1 (`<img alt>`).
        let html = "<svg><title>a](https://evil.example/payload)</title></svg>";
        let result = crate::convert(html, None).unwrap();
        let content = result.content.unwrap_or_default();
        assert_eq!(
            content,
            "![a\\](https://evil.example/payload)](data:image/svg+xml;base64,\
             PHN2Zz48dGl0bGU+YV0oaHR0cHM6Ly9ldmlsLmV4YW1wbGUvcGF5bG9hZCk8L3RpdGxlPjwvc3ZnPg==)\n"
        );
    }
}

#[cfg(test)]
mod attribute_escaping_tests {
    use super::serialize_element;

    #[test]
    fn should_escape_a_quote_inside_a_reconstructed_attribute_value() {
        // ~keep audit #24 finding 3 (media/svg.rs duplicate): the source HTML's `"` is valid,
        // inert content inside a single-quoted attribute. Reconstructing it into a
        // double-quoted attribute without escaping manufactures a real `onclick` that never
        // existed as an attribute in the original document.
        let html = r#"<foo title='x" onclick="alert(1)" y=' data-safe="1">"#;
        let dom = tl::parse(html, tl::ParserOptions::default()).unwrap();
        let parser = dom.parser();
        let node_handle = dom
            .children()
            .iter()
            .find(|handle| matches!(handle.get(parser), Some(tl::Node::Tag(_))))
            .expect("tag node");
        let result = serialize_element(node_handle, parser);
        assert_eq!(
            result,
            "<foo data-safe=\"1\" title=\"x&quot; onclick=&quot;alert(1)&quot; y=\" />"
        );
    }
}