asciidoc-parser 0.29.4

Parser for AsciiDoc format
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
use std::{borrow::Cow, collections::HashMap, sync::Arc};

use crate::{
    SafeMode,
    document::{InterpretedValue, TocMode},
    parser::{
        AllowableValue, AttributeValue, DatetimeContext, DatetimeInputs, ModificationContext,
        ReferenceTime,
        built_in_attrs::{
            built_in_attr, derived_backend_value, max_attribute_value_size_default,
            synthesized_attr, user_home_default,
        },
        is_datetime_attribute,
        safe_mode::masked_doc_path,
    },
};

/// Folds an attribute name to the lowercase form used as its lookup key.
///
/// Attribute names are stored lower-cased (both an attribute-entry definition
/// and an API-supplied attribute fold their name), so a case-insensitive lookup
/// folds the query name the same way. An all-lowercase ASCII name – the
/// overwhelmingly common case – is already its own lowercase form, so it is
/// borrowed unchanged rather than allocating a fresh `String`. Any name that
/// carries an ASCII uppercase letter or a non-ASCII byte falls back to the full
/// Unicode [`str::to_lowercase`], preserving the previous behavior exactly.
pub(crate) fn attribute_lookup_name(name: &str) -> Cow<'_, str> {
    if name
        .bytes()
        .all(|b| b.is_ascii() && !b.is_ascii_uppercase())
    {
        Cow::Borrowed(name)
    } else {
        Cow::Owned(name.to_lowercase())
    }
}

/// A snapshot of a [`Parser`]'s fully-resolved document-attribute state, taken
/// at the end of parsing so it can be retained on a [`Document`] and queried
/// without a [`Parser`] in hand.
///
/// The attribute tables are shared from the parser by [`Arc`] rather than
/// copied, so taking a snapshot is cheap (the large built-in attribute table is
/// never deep-cloned). Only the small set of active counter values is copied.
///
/// [`attribute_value`](Self::attribute_value),
/// [`has_attribute`](Self::has_attribute), and
/// [`is_attribute_set`](Self::is_attribute_set) mirror the identically-named
/// [`Parser`] methods exactly, so a lookup here returns the same value the
/// parser would report after `parse`.
///
/// [`Parser`]: crate::Parser
/// [`Document`]: crate::Document
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub(crate) struct ResolvedAttributes {
    /// Attribute values as of the end of parsing (shared with the parser via
    /// [`Arc`]).
    attribute_values: Arc<HashMap<String, AttributeValue>>,

    /// Default values applied to attributes that are "set" with an empty value
    /// (shared with the parser via [`Arc`]).
    default_attribute_values: Arc<HashMap<String, String>>,

    /// Current value of each counter as of the end of parsing. A counter value
    /// supersedes any like-named attribute.
    counter_values: HashMap<String, String>,

    /// The safe mode the parser ran under. It is not stored in any attribute
    /// table, so the snapshot captures it here to resolve the mode-aware
    /// intrinsics (`max-attribute-value-size`, `user-home`) and to apply the
    /// `SafeMode::Server`-and-greater masking of `docdir` / `docfile` (see
    /// [`masked_doc_path`]) exactly as the parser does. Without it,
    /// `Document::attribute_value` would leak the unmasked host path that the
    /// parser hides. Defaults to [`SafeMode::Secure`], matching the parser.
    safe: SafeMode,

    /// The parser's pinned reference-time configuration, if any, used to
    /// resolve the time-dependent attributes (`docdate` and its family) on
    /// demand. This is deterministic parser configuration (not a captured
    /// instant), so two snapshots taken from equally-configured parsers
    /// stay equal. It is boxed (and `None` unless a clock was pinned) so it
    /// costs only a pointer here – this snapshot is embedded in a
    /// size-sensitive cell enum.
    datetime_inputs: Option<Box<DatetimeInputs>>,
}

impl std::hash::Hash for ResolvedAttributes {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        // `HashMap` is neither `Hash` nor deterministically ordered, so a table
        // can not feed the hasher directly. Instead fold an order-independent
        // digest of each table's entries into `state`. This stays consistent
        // with the derived, content-based `Eq` (equal snapshots hash equally),
        // while distinguishing snapshots whose tables differ in their keys or
        // values – not merely in their entry count (hashing the count alone
        // would collide every cell in a document, since they typically share one
        // `Arc`-backed, equal-length attribute table).
        hash_table(self.attribute_values.iter(), state);
        hash_table(self.default_attribute_values.iter(), state);
        hash_table(self.counter_values.iter(), state);
        self.safe.hash(state);
        self.datetime_inputs.hash(state);
    }
}

/// Feeds an order-independent digest of a table's entries into `state`: the
/// entry count together with the XOR of each entry's `(key, value)` hash.
///
/// XOR is commutative, so the digest does not depend on the `HashMap`'s
/// nondeterministic iteration order, yet it still varies with every key and
/// value present. Keys of a `HashMap` are unique, so no entry's hash cancels
/// another's.
fn hash_table<'a, K, V, H>(entries: impl Iterator<Item = (&'a K, &'a V)>, state: &mut H)
where
    K: std::hash::Hash + 'a,
    V: std::hash::Hash + 'a,
    H: std::hash::Hasher,
{
    use std::hash::{Hash, Hasher};

    let mut count: usize = 0;
    let mut combined: u64 = 0;

    for (key, value) in entries {
        let mut entry_hasher = std::hash::DefaultHasher::new();
        key.hash(&mut entry_hasher);
        value.hash(&mut entry_hasher);
        combined ^= entry_hasher.finish();
        count += 1;
    }

    count.hash(state);
    combined.hash(state);
}

impl ResolvedAttributes {
    pub(crate) fn new(
        attribute_values: Arc<HashMap<String, AttributeValue>>,
        default_attribute_values: Arc<HashMap<String, String>>,
        counter_values: HashMap<String, String>,
        safe: SafeMode,
        reference_time: Option<ReferenceTime>,
        input_mtime: Option<ReferenceTime>,
    ) -> Self {
        Self {
            attribute_values,
            default_attribute_values,
            counter_values,
            safe,
            datetime_inputs: DatetimeInputs::new(reference_time, input_mtime),
        }
    }

    /// Materializes the derived `toc-position` / `toc-placement` / `toc-class`
    /// document attributes from a resolved [`TocMode`], so they are queryable
    /// on this snapshot exactly as Asciidoctor exposes them (see the
    /// `verify toc attribute matrix` upstream test).
    ///
    /// The values are written into *this snapshot's* attribute map (via
    /// [`Arc::make_mut`], which detaches it from the parser's shared table),
    /// not into the parser – so a reused parser never carries a document's
    /// derived TOC state into the next parse, where it would otherwise
    /// perturb [`TocMode::from_parser`](crate::document::TocMode)'s reading
    /// of the raw `toc-placement`.
    ///
    /// `toc-placement` and `toc-position` are fully derived and overwrite any
    /// author-supplied value – including clearing `toc-position` for an
    /// automatic TOC whose author value resolves to no side; `toc-class`
    /// only *defaults* (to `toc2` for a positional TOC), leaving an
    /// explicit author value untouched. Nothing is materialized when no TOC
    /// is generated ([`TocMode::Disabled`], for which every derived value
    /// is `None`), matching Asciidoctor – and avoiding a map clone for the
    /// common no-TOC document.
    pub(crate) fn materialize_toc_attributes(&mut self, mode: TocMode) {
        let placement = mode.derived_toc_placement();
        let position = mode.derived_toc_position();

        // A side-column TOC only *defaults* `toc-class` to `toc2`; an explicit
        // author value wins (Asciidoctor's `attrs['toc-class'] ||= 'toc2'`).
        let class = mode
            .derived_toc_class()
            .filter(|_| !self.is_attribute_set("toc-class"));

        if placement.is_none() && position.is_none() && class.is_none() {
            return;
        }

        let derived = |value: &str| AttributeValue {
            allowable_value: AllowableValue::Any,
            modification_context: ModificationContext::ApiOnly,
            silent_when_locked: false,
            value: InterpretedValue::Value(value.to_string()),
        };

        let attrs = Arc::make_mut(&mut self.attribute_values);
        if let Some(placement) = placement {
            attrs.insert("toc-placement".to_string(), derived(placement));
        }
        if let Some(position) = position {
            attrs.insert("toc-position".to_string(), derived(position));
        } else {
            // An enabled automatic (top) TOC clears any author-supplied
            // `toc-position`: Asciidoctor's normalization falls into its `else`
            // arm and deletes the attribute, so an unrecognized author value
            // (e.g. a bogus side) does not leak into the derived state. A disabled
            // TOC returns early above and leaves the author value untouched.
            attrs.remove("toc-position");
        }
        if let Some(class) = class {
            attrs.insert("toc-class".to_string(), derived(class));
        }
    }

    /// Returns the resolved interpreted value of the named document attribute.
    ///
    /// Mirrors [`Parser::attribute_value`](crate::Parser::attribute_value).
    pub(crate) fn attribute_value<N: AsRef<str>>(&self, name: N) -> InterpretedValue {
        let name = name.as_ref();

        // A counter's current value supersedes any earlier value of the
        // attribute of the same name.
        if let Some(value) = self.counter_values.get(name) {
            return InterpretedValue::Value(value.clone());
        }

        // An unset `relfilesuffix` reads as the *effective* value of
        // `outfilesuffix` – routed through this same reader so an
        // `outfilesuffix` counter overlay is honored too (see
        // [`tracks_outfilesuffix`](Self::tracks_outfilesuffix)).
        if self.tracks_outfilesuffix(name) {
            return self.attribute_value("outfilesuffix");
        }

        // Under `SafeMode::Server` or greater, `docdir` reads as empty and
        // `docfile` is relativized, exactly as the parser reports it (see
        // [`masked_doc_path`]).
        if self.safe >= SafeMode::Server
            && let Some(masked) = masked_doc_path(name, |n| self.raw_set_value(n))
        {
            return masked;
        }

        // `basebackend` / `filetype` are derived on the fly from the current
        // `backend` (see [`derived_backend_value`]) rather than stored.
        if let Some(value) = derived_backend_value(name, &self.attribute_values) {
            return value;
        }

        match self.effective_attribute(name) {
            Some(av) => {
                if let InterpretedValue::Set = av.value
                    && let Some(default) = self.default_attribute_values.get(name)
                {
                    InterpretedValue::Value(default.clone())
                } else {
                    av.value.clone()
                }
            }

            // A time-dependent attribute is resolved on demand from the
            // reference-time configuration rather than stored in a table.
            None => self
                .resolve_datetime_attribute(name)
                .unwrap_or(InterpretedValue::Unset),
        }
    }

    /// Resolves a time-dependent document attribute (`docdate` and its family)
    /// on demand from the snapshot's reference-time configuration, mirroring
    /// [`Parser::attribute_value`](crate::Parser::attribute_value). Returns
    /// `None` for any other name.
    ///
    /// An explicit value stored during parsing still wins, since the readers
    /// consult [`effective_attribute`](Self::effective_attribute) first.
    ///
    /// # Consistency of an unpinned clock
    ///
    /// The reference instant is captured per call (these attributes are read
    /// rarely from a snapshot) rather than cached; within a single call it is
    /// consistent. When the clock is *pinned* – a
    /// [`reference_time`](crate::Parser::with_reference_time), an
    /// [`input_mtime`](crate::Parser::with_input_mtime), or `SOURCE_DATE_EPOCH`
    /// – every capture yields the same instant, so a snapshot lookup always
    /// agrees with the value substituted into content during parsing. This is
    /// the reproducible-build path and the intended way to consume these
    /// attributes.
    ///
    /// When the clock is *not* pinned, each capture reads the real wall clock
    /// (or a since-changed `SOURCE_DATE_EPOCH`) afresh, so a post-parse lookup
    /// can disagree with content the parser already rendered – e.g. `{docdate}`
    /// substituted just before midnight, then read back just after. The
    /// snapshot deliberately does *not* freeze the parser's capture here: doing
    /// so would either make snapshot equality depend on a wall-clock reading or
    /// require the parser to capture (a clock/environment read plus an
    /// allocation) on *every* parse, defeating the laziness that keeps a parse
    /// which never references one of these attributes free of that cost. The
    /// unpinned clock is inherently non-reproducible, so pin it (or set
    /// `SOURCE_DATE_EPOCH`) whenever stable, self-consistent output matters.
    fn resolve_datetime_attribute(&self, name: &str) -> Option<InterpretedValue> {
        if !is_datetime_attribute(name) {
            return None;
        }

        // Absent any pinned clock the inputs box is `None`; capture from the
        // defaults (SOURCE_DATE_EPOCH, then the real wall clock) in that case.
        // See the doc comment above on why an unpinned capture is intentionally
        // taken fresh here rather than frozen from the parse.
        let context = match &self.datetime_inputs {
            Some(inputs) => inputs.capture(),
            None => DatetimeContext::capture(None, None),
        };

        context
            .resolve(name, |sibling| self.stored_datetime_override(sibling))
            .map(InterpretedValue::Value)
    }

    /// Returns the *explicitly-set* value of `name` from the stored attribute
    /// map (a value-less "set" reads as an empty string), or `None`.
    ///
    /// Reads only the stored overrides – never the on-the-fly datetime
    /// resolution – so it can feed the explicit sibling values
    /// [`resolve_datetime_attribute`](Self::resolve_datetime_attribute) needs
    /// without recursing.
    fn stored_datetime_override(&self, name: &str) -> Option<String> {
        self.attribute_values
            .get(name)
            .and_then(|av| match &av.value {
                InterpretedValue::Value(value) => Some(value.clone()),
                InterpretedValue::Set => Some(String::new()),
                InterpretedValue::Unset => None,
            })
    }

    /// Returns the raw stored string value of `name` if it currently resolves
    /// to a plain [`Value`](InterpretedValue::Value), *before* any safe-mode
    /// masking is applied. Mirrors `Parser::raw_set_value`, so the `docfile`
    /// relativization is computed from the *original* API-provided `docdir`.
    fn raw_set_value(&self, name: &str) -> Option<String> {
        match self.effective_attribute(name)?.value {
            InterpretedValue::Value(ref v) => Some(v.clone()),
            _ => None,
        }
    }

    /// Returns the effective attribute definition for `name`, falling back to
    /// the mode-aware intrinsics (`max-attribute-value-size`, `user-home`), the
    /// shared built-in defaults, and the synthesized derived attributes exactly
    /// as [`Parser::effective_attribute`] does.
    ///
    /// [`Parser::effective_attribute`]: crate::Parser::effective_attribute
    fn effective_attribute(&self, name: &str) -> Option<&AttributeValue> {
        if let Some(av) = self.attribute_values.get(name) {
            return Some(av);
        }

        // Mirror the parser's mode-aware resolution of the two intrinsics whose
        // default depends on the safe mode rather than on either attribute
        // table (see [`Parser::effective_attribute`]). Consulted after the
        // per-parser map so a caller-supplied value still wins.
        if name == "max-attribute-value-size" {
            return Some(max_attribute_value_size_default(
                self.safe == SafeMode::Secure,
            ));
        }
        if name == "user-home" {
            return Some(user_home_default(self.safe < SafeMode::Server));
        }
        if let Some(av) = built_in_attr(name) {
            return Some(av);
        }
        synthesized_attr(name, &self.attribute_values)
    }

    /// Reports whether `name` is `relfilesuffix` in its unset state, in which
    /// case a *read* resolves it to the current value of `outfilesuffix`.
    /// Mirrors [`Parser::tracks_outfilesuffix`], so a lookup here returns the
    /// same value the parser would report after `parse`. Callers apply a
    /// like-named counter overlay first, so a `{counter:relfilesuffix}` still
    /// wins over the tracked default.
    ///
    /// [`Parser::tracks_outfilesuffix`]: crate::Parser::tracks_outfilesuffix
    fn tracks_outfilesuffix(&self, name: &str) -> bool {
        name == "relfilesuffix" && !self.attribute_values.contains_key(name)
    }

    /// Returns `true` if the named document attribute is present (whether or
    /// not it is set).
    ///
    /// Mirrors [`Parser::has_attribute`](crate::Parser::has_attribute).
    pub(crate) fn has_attribute<N: AsRef<str>>(&self, name: N) -> bool {
        let name = name.as_ref();
        if self.counter_values.contains_key(name) {
            return true;
        }
        if self.tracks_outfilesuffix(name) {
            return self.has_attribute("outfilesuffix");
        }

        // A derived `basebackend` / `filetype` is present only while `backend`
        // resolves to a non-empty value (see [`derived_backend_value`]).
        if derived_backend_value(name, &self.attribute_values).is_some() {
            return true;
        }
        self.effective_attribute(name).is_some() || self.resolve_datetime_attribute(name).is_some()
    }

    /// Returns `true` if the named document attribute is present and set (i.e.
    /// not [unset]).
    ///
    /// Mirrors [`Parser::is_attribute_set`](crate::Parser::is_attribute_set).
    ///
    /// [unset]: https://docs.asciidoctor.org/asciidoc/latest/attributes/unset-attributes/
    pub(crate) fn is_attribute_set<N: AsRef<str>>(&self, name: N) -> bool {
        let name = name.as_ref();

        // A counter always holds a concrete (set) value.
        if self.counter_values.contains_key(name) {
            return true;
        }

        if self.tracks_outfilesuffix(name) {
            return self.is_attribute_set("outfilesuffix");
        }

        // A derived `basebackend` / `filetype` holds a concrete (set) value
        // whenever it is present, i.e. while `backend` is non-empty (see
        // [`derived_backend_value`]).
        if derived_backend_value(name, &self.attribute_values).is_some() {
            return true;
        }

        self.effective_attribute(name)
            .map(|a| a.value != InterpretedValue::Unset)
            .unwrap_or_else(|| self.resolve_datetime_attribute(name).is_some())
    }
}

#[cfg(test)]
mod tests {
    use std::{collections::HashMap, sync::Arc};

    use crate::{
        SafeMode,
        document::InterpretedValue,
        parser::{AllowableValue, AttributeValue, ModificationContext, ResolvedAttributes},
    };

    fn attr(value: InterpretedValue) -> AttributeValue {
        AttributeValue {
            allowable_value: AllowableValue::Any,
            modification_context: ModificationContext::Anywhere,
            silent_when_locked: false,
            value,
        }
    }

    /// Builds a snapshot exercising each attribute shape – an explicit value, a
    /// `Set` with a registered default, a `Set` with no default, and an
    /// explicitly unset attribute – plus a counter that shadows a like-named
    /// attribute.
    fn sample() -> ResolvedAttributes {
        let mut attribute_values: HashMap<String, AttributeValue> = HashMap::new();
        attribute_values.insert(
            "value".to_string(),
            attr(InterpretedValue::Value("v".to_string())),
        );
        attribute_values.insert("set-with-default".to_string(), attr(InterpretedValue::Set));
        attribute_values.insert("set-no-default".to_string(), attr(InterpretedValue::Set));
        attribute_values.insert("unset".to_string(), attr(InterpretedValue::Unset));
        attribute_values.insert(
            "shadowed".to_string(),
            attr(InterpretedValue::Value("attr".to_string())),
        );

        let mut default_attribute_values: HashMap<String, String> = HashMap::new();
        default_attribute_values.insert("set-with-default".to_string(), "d".to_string());

        let mut counter_values: HashMap<String, String> = HashMap::new();
        counter_values.insert("count".to_string(), "3".to_string());
        counter_values.insert("shadowed".to_string(), "counter".to_string());

        ResolvedAttributes::new(
            Arc::new(attribute_values),
            Arc::new(default_attribute_values),
            counter_values,
            SafeMode::Secure,
            None,
            None,
        )
    }

    #[test]
    fn attribute_value_resolves_each_shape() {
        let attrs = sample();

        // Explicit value.
        assert_eq!(
            attrs.attribute_value("value"),
            InterpretedValue::Value("v".to_string())
        );

        // `Set` with a default resolves to that default.
        assert_eq!(
            attrs.attribute_value("set-with-default"),
            InterpretedValue::Value("d".to_string())
        );

        // `Set` with no default stays `Set`.
        assert_eq!(
            attrs.attribute_value("set-no-default"),
            InterpretedValue::Set
        );

        // Explicitly unset resolves to `Unset`.
        assert_eq!(attrs.attribute_value("unset"), InterpretedValue::Unset);

        // Absent resolves to `Unset`.
        assert_eq!(attrs.attribute_value("absent"), InterpretedValue::Unset);

        // A counter reads back its current value.
        assert_eq!(
            attrs.attribute_value("count"),
            InterpretedValue::Value("3".to_string())
        );

        // A counter supersedes a like-named attribute.
        assert_eq!(
            attrs.attribute_value("shadowed"),
            InterpretedValue::Value("counter".to_string())
        );
    }

    #[test]
    fn has_attribute_reports_presence() {
        let attrs = sample();

        assert!(attrs.has_attribute("value"));
        assert!(attrs.has_attribute("unset"));
        assert!(attrs.has_attribute("count"));
        assert!(!attrs.has_attribute("absent"));
    }

    #[test]
    fn is_attribute_set_reports_set_state() {
        let attrs = sample();

        assert!(attrs.is_attribute_set("value"));
        assert!(attrs.is_attribute_set("set-no-default"));
        assert!(!attrs.is_attribute_set("unset"));
        assert!(!attrs.is_attribute_set("absent"));

        // A counter always holds a concrete (set) value.
        assert!(attrs.is_attribute_set("count"));
    }

    #[test]
    fn masks_docdir_and_docfile_under_server_safe_mode() {
        // A snapshot taken from a `SafeMode::Server` parser masks `docdir`
        // (blanked) and `docfile` (relativized) exactly as the parser does, so
        // `Document::attribute_value` never leaks the host path.
        let mut attribute_values: HashMap<String, AttributeValue> = HashMap::new();
        attribute_values.insert(
            "docdir".to_string(),
            attr(InterpretedValue::Value("/some/dir".to_string())),
        );
        attribute_values.insert(
            "docfile".to_string(),
            attr(InterpretedValue::Value("/some/dir/sample.adoc".to_string())),
        );

        let attrs = ResolvedAttributes::new(
            Arc::new(attribute_values),
            Arc::new(HashMap::new()),
            HashMap::new(),
            SafeMode::Server,
            None,
            None,
        );

        assert_eq!(
            attrs.attribute_value("docdir"),
            InterpretedValue::Value(String::new())
        );
        assert_eq!(
            attrs.attribute_value("docfile"),
            InterpretedValue::Value("sample.adoc".to_string())
        );

        // Masking changes only the value; the attributes stay present and set.
        assert!(attrs.has_attribute("docdir"));
        assert!(attrs.is_attribute_set("docfile"));
    }

    #[test]
    fn absent_docdir_and_docfile_stay_missing_under_server_safe_mode() {
        // With no `docdir` / `docfile` stored, the masking finds nothing to mask
        // (`raw_set_value` short-circuits on the absent attribute) and they stay
        // missing – mirroring the parser.
        let attrs = ResolvedAttributes::new(
            Arc::new(HashMap::new()),
            Arc::new(HashMap::new()),
            HashMap::new(),
            SafeMode::Server,
            None,
            None,
        );

        assert_eq!(attrs.attribute_value("docdir"), InterpretedValue::Unset);
        assert_eq!(attrs.attribute_value("docfile"), InterpretedValue::Unset);
        assert!(!attrs.has_attribute("docdir"));
        assert!(!attrs.has_attribute("docfile"));
    }

    #[test]
    fn leaves_non_string_docdir_and_docfile_untouched_under_server_safe_mode() {
        // A `docdir` / `docfile` present as a value-less `Set` flag carries no
        // path to relativize, so the masking leaves it untouched (mirroring
        // `Parser`).
        let mut attribute_values: HashMap<String, AttributeValue> = HashMap::new();
        attribute_values.insert("docdir".to_string(), attr(InterpretedValue::Set));
        attribute_values.insert("docfile".to_string(), attr(InterpretedValue::Set));

        let attrs = ResolvedAttributes::new(
            Arc::new(attribute_values),
            Arc::new(HashMap::new()),
            HashMap::new(),
            SafeMode::Server,
            None,
            None,
        );

        assert_eq!(attrs.attribute_value("docdir"), InterpretedValue::Set);
        assert_eq!(attrs.attribute_value("docfile"), InterpretedValue::Set);
    }

    #[test]
    fn does_not_mask_docdir_and_docfile_below_server_safe_mode() {
        let mut attribute_values: HashMap<String, AttributeValue> = HashMap::new();
        attribute_values.insert(
            "docdir".to_string(),
            attr(InterpretedValue::Value("/some/dir".to_string())),
        );

        let attrs = ResolvedAttributes::new(
            Arc::new(attribute_values),
            Arc::new(HashMap::new()),
            HashMap::new(),
            SafeMode::Safe,
            None,
            None,
        );

        assert_eq!(
            attrs.attribute_value("docdir"),
            InterpretedValue::Value("/some/dir".to_string())
        );
    }
}