merman-render 0.8.0-alpha.6

Headless layout + SVG renderer for Mermaid (parity-focused; upstream SVG goldens).
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
use super::IconRenderRequest;
use super::ingest::ResolvedIcon;
use super::xml::ValidatedIconBody;
use crate::svg::pipeline::validate_well_formed_svg_with_controls;
use merman_core::OperationPhase;
use merman_core::sanitize::{SanitizeFailure, SanitizeOutputSink};

const WORK_BYTES_PER_UNIT: usize = 256;
// lol_html serializes a literal double quote in an attribute value as `"` (six bytes).
// The element allowance covers handler-added attributes and normalized closing tags without
// relying on an empirical whole-document multiplier.
const SANITIZER_MAX_BYTE_EXPANSION: usize = """.len();
const SANITIZER_MAX_ADDED_BYTES_PER_ELEMENT: usize = 64;
const SANITIZER_FIXED_OVERHEAD_BYTES: usize = 1_024;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BoundedSanitizeError {
    OutputLimitExceeded { max_bytes: usize },
    AllocationFailed,
}

#[derive(Debug, Clone, Copy)]
struct BoundedSanitizeSink {
    max_output_bytes: usize,
}

impl BoundedSanitizeSink {
    const fn new(max_output_bytes: usize) -> Self {
        Self { max_output_bytes }
    }

    const fn output_limit_error(self) -> BoundedSanitizeError {
        BoundedSanitizeError::OutputLimitExceeded {
            max_bytes: self.max_output_bytes,
        }
    }
}

impl SanitizeOutputSink for BoundedSanitizeSink {
    type Error = BoundedSanitizeError;

    fn checked_output_len(&self, current: usize, additional: usize) -> Result<usize, Self::Error> {
        let next = current
            .checked_add(additional)
            .ok_or_else(|| self.output_limit_error())?;
        if next > self.max_output_bytes {
            return Err(self.output_limit_error());
        }
        Ok(next)
    }

    fn string_with_capacity(&self, capacity: usize) -> Result<String, Self::Error> {
        self.checked_output_len(0, capacity)?;
        let mut output = String::new();
        output
            .try_reserve_exact(capacity)
            .map_err(|_| BoundedSanitizeError::AllocationFailed)?;
        Ok(output)
    }

    fn output_buffer(&self, input_len: usize) -> Result<Vec<u8>, Self::Error> {
        let mut output = Vec::new();
        output
            .try_reserve_exact(input_len.min(self.max_output_bytes))
            .map_err(|_| BoundedSanitizeError::AllocationFailed)?;
        Ok(output)
    }

    fn push_output_chunk(&self, output: &mut Vec<u8>, chunk: &[u8]) -> Result<(), Self::Error> {
        self.checked_output_len(output.len(), chunk.len())?;
        output
            .try_reserve(chunk.len())
            .map_err(|_| BoundedSanitizeError::AllocationFailed)?;
        output.extend_from_slice(chunk);
        Ok(())
    }
}

pub(super) fn render_resolved_icon(
    icon: &ResolvedIcon,
    request: &IconRenderRequest<'_>,
) -> crate::Result<String> {
    let width = render_dimension(request.width_px, "width")?;
    let height = render_dimension(request.height_px, "height")?;
    let transformed = transformed_icon(icon)?;
    let class_attr = request
        .extra_class
        .map(|class| format!(r#" class="{}""#, escape_xml_attr(class)))
        .unwrap_or_default();
    let xlink_attr = if icon.body.uses_xlink() {
        r#" xmlns:xlink="http://www.w3.org/1999/xlink""#
    } else {
        ""
    };
    let open = format!(
        r#"<svg xmlns="http://www.w3.org/2000/svg"{xlink_attr}{class_attr} width="{width}" height="{height}" viewBox="{} {} {} {}">"#,
        js_number(transformed.left),
        js_number(transformed.top),
        js_number(transformed.width),
        js_number(transformed.height),
    );
    let projected_body_bytes = transformed.projected_body_bytes(&icon.body)?;
    let projected_svg_bytes = open
        .len()
        .checked_add(projected_body_bytes)
        .and_then(|bytes| bytes.checked_add("</svg>".len()))
        .ok_or_else(|| crate::Error::icon_processing("projected icon SVG size overflowed"))?;
    let sanitizer_ceiling =
        sanitizer_output_ceiling(projected_svg_bytes, icon.body.element_count())?;
    let work_units = icon_render_work_units(icon, projected_svg_bytes, sanitizer_ceiling)?;

    // Both ledgers are charged before cloning the retained body or allocating the assembled SVG.
    request.work_meter.charge(work_units)?;
    request.work_meter.charge_svg_bytes(projected_svg_bytes)?;

    let body = transformed.apply(&icon.body, request.id_scope)?;
    let mut raw_svg = String::with_capacity(projected_svg_bytes);
    raw_svg.push_str(&open);
    raw_svg.push_str(&body);
    raw_svg.push_str("</svg>");
    if raw_svg.len() > projected_svg_bytes {
        return Err(crate::Error::icon_processing(
            "assembled icon SVG exceeded its precharged size",
        ));
    }

    // Mermaid performs sanitization after transforms, deterministic ID replacement, and outer SVG
    // assembly. The effective request config must therefore be applied on every render.
    let requested_growth = sanitizer_ceiling - projected_svg_bytes;
    let growth_reservation = request
        .work_meter
        .reserve_svg_bytes_up_to(requested_growth)?;
    let reserved_svg_bytes = projected_svg_bytes
        .checked_add(growth_reservation.additional_bytes)
        .ok_or_else(|| crate::Error::icon_processing("sanitizer reservation overflowed"))?;
    let sanitize_sink = BoundedSanitizeSink::new(reserved_svg_bytes);
    let sanitized = match merman_core::sanitize::sanitize_text_with_sink(
        &raw_svg,
        request.effective_config,
        &sanitize_sink,
    ) {
        Ok(sanitized) => sanitized,
        Err(SanitizeFailure::Output(BoundedSanitizeError::OutputLimitExceeded { .. })) => {
            request
                .work_meter
                .reconcile_svg_bytes(reserved_svg_bytes, 0)?;
            if let Some(error) = growth_reservation.limit_error {
                return Err(request
                    .work_meter
                    .terminate_absolute_resource_error(error, OperationPhase::Emit)
                    .into());
            }
            return Err(crate::Error::icon_processing(
                "icon SVG sanitizer exceeded its fixed expansion ceiling",
            ));
        }
        Err(SanitizeFailure::RejectedInput) => {
            request
                .work_meter
                .reconcile_svg_bytes(reserved_svg_bytes, 0)?;
            return Err(crate::Error::invalid_icon_output(
                "icon SVG sanitizer rejected the output",
            ));
        }
        Err(
            SanitizeFailure::Output(BoundedSanitizeError::AllocationFailed)
            | SanitizeFailure::InvalidUtf8Output,
        ) => {
            request
                .work_meter
                .reconcile_svg_bytes(reserved_svg_bytes, 0)?;
            return Err(crate::Error::icon_processing(
                "icon SVG sanitizer failed internally",
            ));
        }
        Err(_) => {
            request
                .work_meter
                .reconcile_svg_bytes(reserved_svg_bytes, 0)?;
            return Err(crate::Error::icon_processing(
                "icon SVG sanitizer returned an unknown failure",
            ));
        }
    };
    if sanitized.is_empty() {
        request
            .work_meter
            .reconcile_svg_bytes(reserved_svg_bytes, 0)?;
        return Err(crate::Error::invalid_icon_output(
            "sanitization removed the complete icon SVG",
        ));
    }
    let validation = validate_well_formed_svg_with_controls(
        &sanitized,
        &mut || {
            request
                .work_meter
                .checkpoint(OperationPhase::Postprocess)
                .map_err(crate::Error::from)
        },
        &mut |elements, tree_depth| {
            request
                .work_meter
                .preflight_svg_structure(elements, tree_depth, OperationPhase::Postprocess)
                .map_err(crate::Error::from)
        },
    );
    if let Err(error) = validation {
        if matches!(
            &error,
            crate::Error::Cancelled(_)
                | crate::Error::ResourceLimitExceeded(_)
                | crate::Error::OperationResourceTerminal(_)
        ) {
            return Err(error);
        }
        request.work_meter.checkpoint(OperationPhase::Postprocess)?;
        request
            .work_meter
            .reconcile_svg_bytes(reserved_svg_bytes, 0)?;
        return Err(crate::Error::icon_processing(
            "sanitizer produced invalid SVG XML",
        ));
    }
    request
        .work_meter
        .reconcile_svg_bytes(reserved_svg_bytes, sanitized.len())?;
    Ok(sanitized)
}

fn sanitizer_output_ceiling(
    projected_svg_bytes: usize,
    element_count: usize,
) -> crate::Result<usize> {
    projected_svg_bytes
        .checked_mul(SANITIZER_MAX_BYTE_EXPANSION)
        .and_then(|bytes| {
            element_count
                .checked_mul(SANITIZER_MAX_ADDED_BYTES_PER_ELEMENT)
                .and_then(|overhead| bytes.checked_add(overhead))
        })
        .and_then(|bytes| bytes.checked_add(SANITIZER_FIXED_OVERHEAD_BYTES))
        .ok_or_else(|| crate::Error::icon_processing("sanitizer output limit overflowed"))
}

fn icon_render_work_units(
    icon: &ResolvedIcon,
    projected_svg_bytes: usize,
    sanitizer_ceiling: usize,
) -> crate::Result<usize> {
    let edit_work = icon
        .body
        .edit_count()
        .checked_mul(4)
        .ok_or_else(|| crate::Error::icon_processing("icon edit work estimate overflowed"))?;
    let projected_work = ceil_div(projected_svg_bytes, WORK_BYTES_PER_UNIT);
    let sanitizer_work = ceil_div(sanitizer_ceiling, WORK_BYTES_PER_UNIT);
    let element_work = icon.body.element_count().checked_mul(3).ok_or_else(|| {
        crate::Error::icon_processing("icon sanitizer element work estimate overflowed")
    })?;
    ceil_div(icon.body.source_len(), WORK_BYTES_PER_UNIT)
        // Assembly plus the first sanitizer input traversal.
        .checked_add(projected_work.checked_mul(2).ok_or_else(|| {
            crate::Error::icon_processing("icon projected work estimate overflowed")
        })?)
        // The second sanitizer pass and final XML validation are bounded by the admitted output.
        .and_then(|work| work.checked_add(sanitizer_work.checked_mul(2)?))
        .and_then(|work| work.checked_add(element_work))
        .and_then(|work| work.checked_add(edit_work))
        .and_then(|work| work.checked_add(1))
        .ok_or_else(|| crate::Error::icon_processing("icon render work estimate overflowed"))
}

const fn ceil_div(value: usize, divisor: usize) -> usize {
    value / divisor + if value.is_multiple_of(divisor) { 0 } else { 1 }
}

fn render_dimension(value: f64, name: &'static str) -> crate::Result<String> {
    if !value.is_finite() || value <= 0.0 {
        return Err(crate::Error::invalid_icon_output(format!(
            "requested icon {name} is not finite and positive"
        )));
    }
    Ok(js_number(value))
}

struct TransformedIcon {
    left: f64,
    top: f64,
    width: f64,
    height: f64,
    wrapper: Option<(String, &'static str)>,
}

impl TransformedIcon {
    fn projected_body_bytes(&self, body: &ValidatedIconBody) -> crate::Result<usize> {
        let Some((start, end)) = &self.wrapper else {
            return Ok(body.scoped_len());
        };
        body.transformed_scoped_len(start.len(), end.len())
            .map_err(|_| crate::Error::icon_processing("transformed icon size overflowed"))
    }

    fn apply(&self, body: &ValidatedIconBody, scope: super::IconIdScope) -> crate::Result<String> {
        let Some((start, end)) = &self.wrapper else {
            return body
                .scope(scope)
                .map_err(|_| crate::Error::icon_processing("validated icon ID scoping failed"));
        };
        body.scope_transformed(scope, start, end)
            .map_err(|_| crate::Error::icon_processing("validated icon transform assembly failed"))
    }
}

fn transformed_icon(icon: &ResolvedIcon) -> crate::Result<TransformedIcon> {
    let mut left = icon.left;
    let mut top = icon.top;
    let mut width = icon.width;
    let mut height = icon.height;
    let mut transformations = Vec::with_capacity(3);
    let mut rotation = i32::from(icon.rotate);

    if icon.h_flip {
        if icon.v_flip {
            rotation += 2;
        } else {
            transformations.push(format!(
                "translate({} {})",
                js_number(width + left),
                js_number(-top)
            ));
            transformations.push("scale(-1 1)".to_string());
            left = 0.0;
            top = 0.0;
        }
    } else if icon.v_flip {
        transformations.push(format!(
            "translate({} {})",
            js_number(-left),
            js_number(height + top)
        ));
        transformations.push("scale(1 -1)".to_string());
        left = 0.0;
        top = 0.0;
    }

    rotation = rotation.rem_euclid(4);
    match rotation {
        1 => {
            let center = height / 2.0 + top;
            transformations.insert(
                0,
                format!("rotate(90 {} {})", js_number(center), js_number(center)),
            );
        }
        2 => transformations.insert(
            0,
            format!(
                "rotate(180 {} {})",
                js_number(width / 2.0 + left),
                js_number(height / 2.0 + top)
            ),
        ),
        3 => {
            let center = width / 2.0 + left;
            transformations.insert(
                0,
                format!("rotate(-90 {} {})", js_number(center), js_number(center)),
            );
        }
        _ => {}
    }
    if rotation % 2 == 1 {
        std::mem::swap(&mut left, &mut top);
        std::mem::swap(&mut width, &mut height);
    }

    for value in [left, top, width, height] {
        if !value.is_finite() {
            return Err(crate::Error::invalid_icon_output(
                "icon transformation produced non-finite geometry",
            ));
        }
    }
    let wrapper = (!transformations.is_empty()).then(|| {
        (
            format!(r#"<g transform="{}">"#, transformations.join(" ")),
            "</g>",
        )
    });
    Ok(TransformedIcon {
        left,
        top,
        width,
        height,
        wrapper,
    })
}

fn escape_xml_attr(value: &str) -> String {
    crate::xml::strip_forbidden_xml_1_0_chars(value)
        .replace('&', "&amp;")
        .replace('"', "&quot;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

fn js_number(mut value: f64) -> String {
    if value == -0.0 {
        value = 0.0;
    }
    let mut buffer = ryu_js::Buffer::new();
    buffer.format_finite(value).to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::resources::{OperationWorkMeter, RenderResourcePolicy, ResourceLimitId};
    use crate::svg::icon_registry::{icon_id_scope_for_test, limits::IconRegistryBuildLimits};
    use serde_json::json;
    use std::sync::Arc;

    #[derive(Debug, Clone, Copy)]
    struct InvalidUtf8Sink(BoundedSanitizeSink);

    impl SanitizeOutputSink for InvalidUtf8Sink {
        type Error = BoundedSanitizeError;

        fn checked_output_len(
            &self,
            current: usize,
            additional: usize,
        ) -> Result<usize, Self::Error> {
            self.0.checked_output_len(current, additional)
        }

        fn string_with_capacity(&self, capacity: usize) -> Result<String, Self::Error> {
            self.0.string_with_capacity(capacity)
        }

        fn output_buffer(&self, input_len: usize) -> Result<Vec<u8>, Self::Error> {
            self.0.output_buffer(input_len)
        }

        fn push_output_chunk(&self, output: &mut Vec<u8>, chunk: &[u8]) -> Result<(), Self::Error> {
            self.0.push_output_chunk(output, chunk)?;
            if let Some(first) = output.first_mut() {
                *first = u8::MAX;
            }
            Ok(())
        }
    }

    fn resolved_icon(body: &str) -> ResolvedIcon {
        ResolvedIcon {
            body: Arc::new(
                ValidatedIconBody::parse(body.to_owned(), 0, &IconRegistryBuildLimits::fixed())
                    .expect("test icon body must be admitted"),
            ),
            left: 0.0,
            top: 0.0,
            width: 16.0,
            height: 16.0,
            h_flip: false,
            v_flip: false,
            rotate: 0,
        }
    }

    fn render(
        body: &str,
        security_level: &str,
        policy: RenderResourcePolicy,
    ) -> crate::Result<String> {
        render_with_work_usage(body, security_level, policy).0
    }

    fn render_with_work_usage(
        body: &str,
        security_level: &str,
        policy: RenderResourcePolicy,
    ) -> (crate::Result<String>, usize) {
        let icon = resolved_icon(body);
        let config = merman_core::MermaidConfig::from_value(json!({
            "securityLevel": security_level,
            "htmlLabels": true
        }));
        let work_meter = OperationWorkMeter::new(policy);
        let result = render_resolved_icon(
            &icon,
            &IconRenderRequest {
                icon_name: "test:icon",
                width_px: 16.0,
                height_px: 16.0,
                fallback_prefix: None,
                extra_class: None,
                id_scope: icon_id_scope_for_test("icon-scope"),
                effective_config: &config,
                work_meter: &work_meter,
            },
        );
        (result, work_meter.used())
    }

    fn render_repeated_with_usage(
        body: &str,
        security_level: &str,
        repetitions: usize,
        policy: RenderResourcePolicy,
    ) -> (crate::Result<Vec<String>>, usize, usize) {
        let icon = resolved_icon(body);
        let config = merman_core::MermaidConfig::from_value(json!({
            "securityLevel": security_level,
            "htmlLabels": true
        }));
        let work_meter = OperationWorkMeter::new(policy);
        let mut rendered = Vec::with_capacity(repetitions);
        let result = (|| {
            for index in 0..repetitions {
                let scope = format!("icon-scope-{index}");
                rendered.push(render_resolved_icon(
                    &icon,
                    &IconRenderRequest {
                        icon_name: "test:icon",
                        width_px: 16.0,
                        height_px: 16.0,
                        fallback_prefix: None,
                        extra_class: None,
                        id_scope: icon_id_scope_for_test(&scope),
                        effective_config: &config,
                        work_meter: &work_meter,
                    },
                )?);
            }
            Ok(rendered)
        })();
        (result, work_meter.used(), work_meter.projected_svg_bytes())
    }

    #[test]
    fn ambiguous_icon_markup_is_rejected_in_strict_and_loose_modes() {
        let body = "<select><xmp><script>alert(1)</script></xmp></select>";
        for security_level in ["strict", "loose"] {
            let error = render(
                body,
                security_level,
                RenderResourcePolicy::unbounded_for_trusted_input(),
            )
            .expect_err("ambiguous sanitizer input must fail closed");
            assert!(matches!(error, crate::Error::InvalidIconOutput { .. }));
        }
    }

    #[test]
    fn bounded_sanitizer_accepts_exact_output_limit_and_rejects_one_less() {
        let input = r#"<a href="https://example.com" target="_blank">example</a>"#;
        let expected =
            r#"<a href="https://example.com" rel="noopener" target="_blank">example</a>"#;
        let config = merman_core::MermaidConfig::from_value(json!({
            "securityLevel": "strict",
            "htmlLabels": true
        }));

        assert_eq!(
            merman_core::sanitize::sanitize_text_with_sink(
                input,
                &config,
                &BoundedSanitizeSink::new(expected.len()),
            )
            .unwrap(),
            expected
        );
        assert_eq!(
            merman_core::sanitize::sanitize_text_with_sink(
                input,
                &config,
                &BoundedSanitizeSink::new(expected.len() - 1),
            ),
            Err(SanitizeFailure::Output(
                BoundedSanitizeError::OutputLimitExceeded {
                    max_bytes: expected.len() - 1,
                },
            ))
        );
    }

    #[test]
    fn bounded_sanitizer_classifies_allocation_and_invalid_utf8_failures() {
        assert_eq!(
            BoundedSanitizeSink::new(usize::MAX).string_with_capacity(usize::MAX),
            Err(BoundedSanitizeError::AllocationFailed)
        );

        let config = merman_core::MermaidConfig::from_value(json!({
            "securityLevel": "strict",
            "htmlLabels": true
        }));
        assert_eq!(
            merman_core::sanitize::sanitize_text_with_sink(
                "<b>ok</b>",
                &config,
                &InvalidUtf8Sink(BoundedSanitizeSink::new(4096)),
            ),
            Err(SanitizeFailure::InvalidUtf8Output)
        );
    }

    #[test]
    fn active_external_icon_content_is_sanitized_in_strict_and_loose_modes() {
        let cases = [
            ("script", "<script>alert(1)</script><path d=\"M0 0h1v1z\"/>"),
            ("event", "<path onload=\"alert(1)\" d=\"M0 0h1v1z\"/>"),
            (
                "style",
                "<style>.owned{fill:red}</style><path class=\"owned\" d=\"M0 0h1v1z\"/>",
            ),
            (
                "foreign-object",
                "<foreignObject><div onclick=\"alert(1)\">owned</div></foreignObject><path d=\"M0 0h1v1z\"/>",
            ),
            (
                "href",
                "<a href=\"javascript:alert(1)\"><path d=\"M0 0h1v1z\"/></a>",
            ),
            (
                "xlink-href",
                "<a xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:href=\"javascript:alert(1)\"><path d=\"M0 0h1v1z\"/></a>",
            ),
        ];

        for security_level in ["strict", "loose"] {
            for (name, body) in cases {
                let rendered = render(
                    body,
                    security_level,
                    RenderResourcePolicy::unbounded_for_trusted_input(),
                )
                .unwrap_or_else(|error| {
                    panic!("{name} must sanitize safely in {security_level} mode: {error}")
                });
                let lower = rendered.to_ascii_lowercase();
                for forbidden in [
                    "<script",
                    "onload=",
                    "onclick=",
                    "<style",
                    "<foreignobject",
                    "javascript:",
                    "xlink:href=",
                ] {
                    assert!(
                        !lower.contains(forbidden),
                        "{name} retained {forbidden:?} in {security_level} mode: {rendered}"
                    );
                }
                assert!(lower.starts_with("<svg"), "{rendered}");
                assert!(lower.ends_with("</svg>"), "{rendered}");
            }
        }
    }

    #[test]
    fn safe_sanitizer_growth_is_accounted_at_the_svg_limit() {
        let body = r#"<a href="https://example.com" target="_blank"><path d="M0 0h16v16H0z"/></a>"#;
        let rendered = render(
            body,
            "strict",
            RenderResourcePolicy::unbounded_for_trusted_input(),
        )
        .expect("safe sanitizer growth is accepted");
        assert!(rendered.contains(r#"rel="noopener""#));

        let exact = RenderResourcePolicy::unbounded_for_trusted_input()
            .with_limit(ResourceLimitId::MaxSvgBytes, rendered.len())
            .unwrap();
        assert_eq!(render(body, "strict", exact).unwrap(), rendered);

        let one_less = RenderResourcePolicy::unbounded_for_trusted_input()
            .with_limit(ResourceLimitId::MaxSvgBytes, rendered.len() - 1)
            .unwrap();
        let error = render(body, "strict", one_less)
            .expect_err("sanitized output one byte above the SVG budget must be rejected");
        assert!(matches!(error, crate::Error::ResourceLimitExceeded(_)));
    }

    #[test]
    fn icon_xml_validation_preserves_resource_error_classification() {
        let policy = RenderResourcePolicy::unbounded_for_trusted_input()
            .with_limit(ResourceLimitId::MaxSvgElements, 1)
            .unwrap();

        let error = render("<path d=\"M0 0h16v16H0z\"/>", "strict", policy)
            .expect_err("the generated svg and path exceed the one-element validation limit");

        assert!(matches!(error, crate::Error::ResourceLimitExceeded(_)));
    }

    #[test]
    fn sanitizer_ceiling_covers_worst_case_attribute_quote_serialization() {
        let quote_count = 70 * 1024;
        let quotes = "\"".repeat(quote_count);
        let body = format!(r#"<a target='{quotes}'><path d="M0 0h16v16H0z"/></a>"#);

        let rendered = render(
            &body,
            "strict",
            RenderResourcePolicy::unbounded_for_trusted_input(),
        )
        .expect("a valid admitted attribute must fit the source-backed sanitizer ceiling");

        assert_eq!(rendered.matches("&quot;").count(), quote_count);
    }

    #[test]
    fn icon_render_work_precharge_is_exact_and_includes_sanitizer_passes() {
        let body = r#"<a target="_blank"><path d="M0 0h16v16H0z"/></a>"#;
        let (_, used) = render_with_work_usage(
            body,
            "strict",
            RenderResourcePolicy::unbounded_for_trusted_input(),
        );
        assert!(used > 0);

        let exact = RenderResourcePolicy::unbounded_for_trusted_input()
            .with_limit(ResourceLimitId::MaxLayoutWorkUnits, used)
            .unwrap();
        assert!(render(body, "strict", exact).is_ok());

        let one_less = RenderResourcePolicy::unbounded_for_trusted_input()
            .with_limit(ResourceLimitId::MaxLayoutWorkUnits, used - 1)
            .unwrap();
        let error = render(body, "strict", one_less)
            .expect_err("one work unit below the precharge must reject before rendering");
        assert!(matches!(error, crate::Error::ResourceLimitExceeded(_)));
    }

    #[test]
    fn repeated_maximum_body_precharges_aggregate_svg_and_work_exactly() {
        let maximum =
            usize::try_from(crate::svg::IconRegistryResourceLimitId::MaxBodyBytes.fixed_value())
                .expect("maximum icon body bytes fit usize");
        let prefix = r#"<path data-padding=""#;
        let suffix = r#"" d="M0 0H16V16H0z"/>"#;
        let body = format!(
            "{prefix}{}{suffix}",
            "x".repeat(maximum - prefix.len() - suffix.len())
        );
        assert_eq!(body.len(), maximum);

        let repetitions = 4;
        let (baseline, work, svg_bytes) = render_repeated_with_usage(
            &body,
            "strict",
            repetitions,
            RenderResourcePolicy::unbounded_for_trusted_input(),
        );
        assert_eq!(
            baseline.expect("maximum icon body renders").len(),
            repetitions
        );
        assert!(work > 0);
        assert!(svg_bytes >= maximum.checked_mul(repetitions).unwrap());

        let exact = RenderResourcePolicy::unbounded_for_trusted_input()
            .with_limit(ResourceLimitId::MaxLayoutWorkUnits, work)
            .unwrap()
            .with_limit(ResourceLimitId::MaxSvgBytes, svg_bytes)
            .unwrap();
        assert!(
            render_repeated_with_usage(&body, "strict", repetitions, exact)
                .0
                .is_ok()
        );

        let svg_one_less = RenderResourcePolicy::unbounded_for_trusted_input()
            .with_limit(ResourceLimitId::MaxSvgBytes, svg_bytes - 1)
            .unwrap();
        let error = render_repeated_with_usage(&body, "strict", repetitions, svg_one_less)
            .0
            .expect_err("aggregate SVG bytes plus one must fail before the final expansion");
        assert!(matches!(error, crate::Error::ResourceLimitExceeded(_)));

        let work_one_less = RenderResourcePolicy::unbounded_for_trusted_input()
            .with_limit(ResourceLimitId::MaxLayoutWorkUnits, work - 1)
            .unwrap();
        let error = render_repeated_with_usage(&body, "strict", repetitions, work_one_less)
            .0
            .expect_err("aggregate icon work plus one must fail before the final expansion");
        assert!(matches!(error, crate::Error::ResourceLimitExceeded(_)));
    }
}