rdocx 0.14.0

High-level API for reading, writing, and converting DOCX documents
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
//! Style access and manipulation for documents.

use std::collections::{HashMap, HashSet};

use rdocx_oxml::properties::{CT_PPr, CT_RPr};
use rdocx_oxml::styles::{CT_Style, CT_Styles, CT_TblStylePr, StyleType};
use rdocx_oxml::table::{CT_TblPr, CT_TcPr};

use crate::{Error, Result};

/// An immutable reference to a style definition.
pub struct Style<'a> {
    pub(crate) inner: &'a CT_Style,
}

impl<'a> Style<'a> {
    /// The style ID (used to reference this style).
    pub fn style_id(&self) -> &str {
        &self.inner.style_id
    }

    /// The display name of the style.
    pub fn name(&self) -> Option<&str> {
        self.inner.name.as_deref()
    }

    /// The style ID this style is based on.
    pub fn based_on(&self) -> Option<&str> {
        self.inner.based_on.as_deref()
    }

    /// The kind of content this style formats.
    pub fn style_type(&self) -> StyleType {
        self.inner.style_type
    }

    /// The reciprocal paragraph or character style linked to this style.
    pub fn linked_style(&self) -> Option<&str> {
        self.inner.linked_style.as_deref()
    }

    /// The style selected for the paragraph following this paragraph style.
    pub fn next_style(&self) -> Option<&str> {
        self.inner.next_style.as_deref()
    }

    /// The user-interface ordering priority for this style.
    pub fn priority(&self) -> Option<u32> {
        self.inner.ui_priority
    }

    /// Whether Word may redefine the style from direct formatting.
    pub fn auto_redefine(&self) -> Option<bool> {
        self.inner.auto_redefine
    }

    /// Whether the style is hidden from all user-interface lists.
    pub fn hidden(&self) -> Option<bool> {
        self.inner.hidden
    }

    /// Whether the style is hidden from the main style gallery.
    pub fn semi_hidden(&self) -> Option<bool> {
        self.inner.semi_hidden
    }

    /// Whether applying the style makes it visible in the user interface.
    pub fn unhide_when_used(&self) -> Option<bool> {
        self.inner.unhide_when_used
    }

    /// Whether the style appears in the quick style gallery.
    pub fn quick_format(&self) -> Option<bool> {
        self.inner.quick_format
    }

    /// Whether the style is locked against application in the user interface.
    pub fn locked(&self) -> Option<bool> {
        self.inner.locked
    }

    /// The style's paragraph properties.
    pub fn paragraph_properties(&self) -> Option<&CT_PPr> {
        self.inner.ppr.as_ref()
    }

    /// The style's run properties.
    pub fn run_properties(&self) -> Option<&CT_RPr> {
        self.inner.rpr.as_ref()
    }

    /// The style's base table properties.
    pub fn table_properties(&self) -> Option<&CT_TblPr> {
        self.inner.table_properties.as_ref()
    }

    /// Conditional table regions in source order.
    pub fn conditional_table_styles(&self) -> &[CT_TblStylePr] {
        &self.inner.conditional_table_styles
    }

    /// Whether this is the default style for its type.
    pub fn is_default(&self) -> bool {
        self.inner.is_default
    }
}

/// Builder for creating a new paragraph style.
pub struct StyleBuilder {
    style: CT_Style,
    cleared: u16,
}

pub(crate) const CLEAR_BASED_ON: u16 = 1 << 0;
pub(crate) const CLEAR_NEXT_STYLE: u16 = 1 << 1;
pub(crate) const CLEAR_LINKED_STYLE: u16 = 1 << 2;
pub(crate) const CLEAR_PRIORITY: u16 = 1 << 3;
pub(crate) const CLEAR_AUTO_REDEFINE: u16 = 1 << 4;
pub(crate) const CLEAR_HIDDEN: u16 = 1 << 5;
pub(crate) const CLEAR_SEMI_HIDDEN: u16 = 1 << 6;
pub(crate) const CLEAR_UNHIDE_WHEN_USED: u16 = 1 << 7;
pub(crate) const CLEAR_QUICK_FORMAT: u16 = 1 << 8;
pub(crate) const CLEAR_LOCKED: u16 = 1 << 9;
pub(crate) const CLEAR_PARAGRAPH_PROPERTIES: u16 = 1 << 10;
pub(crate) const CLEAR_RUN_PROPERTIES: u16 = 1 << 11;
pub(crate) const CLEAR_TABLE_PROPERTIES: u16 = 1 << 12;
pub(crate) const CLEAR_CONDITIONAL_TABLE_STYLES: u16 = 1 << 13;

impl StyleBuilder {
    /// Create a new paragraph style builder.
    pub fn paragraph(style_id: &str, name: &str) -> Self {
        StyleBuilder {
            style: CT_Style {
                style_id: style_id.to_string(),
                style_type: StyleType::Paragraph,
                name: Some(name.to_string()),
                based_on: None,
                next_style: None,
                linked_style: None,
                auto_redefine: None,
                hidden: None,
                ui_priority: None,
                semi_hidden: None,
                unhide_when_used: None,
                quick_format: None,
                locked: None,
                is_default: false,
                ppr: None,
                rpr: None,
                table_properties: None,
                table_properties_original: None,
                table_properties_xml: None,
                conditional_table_styles: Vec::new(),
                extra_attributes: Vec::new(),
                modeled_xml: Vec::new(),
                extra_xml: Vec::new(),
            },
            cleared: 0,
        }
    }

    /// Create a new character style builder.
    pub fn character(style_id: &str, name: &str) -> Self {
        StyleBuilder {
            style: CT_Style {
                style_id: style_id.to_string(),
                style_type: StyleType::Character,
                name: Some(name.to_string()),
                based_on: None,
                next_style: None,
                linked_style: None,
                auto_redefine: None,
                hidden: None,
                ui_priority: None,
                semi_hidden: None,
                unhide_when_used: None,
                quick_format: None,
                locked: None,
                is_default: false,
                ppr: None,
                rpr: None,
                table_properties: None,
                table_properties_original: None,
                table_properties_xml: None,
                conditional_table_styles: Vec::new(),
                extra_attributes: Vec::new(),
                modeled_xml: Vec::new(),
                extra_xml: Vec::new(),
            },
            cleared: 0,
        }
    }

    /// Create a new table style builder.
    pub fn table(style_id: &str, name: &str) -> Self {
        let mut builder = Self::paragraph(style_id, name);
        builder.style.style_type = StyleType::Table;
        builder
    }

    /// Set the parent style this one inherits from.
    pub fn based_on(mut self, style_id: &str) -> Self {
        self.style.based_on = Some(style_id.to_string());
        self.cleared &= !CLEAR_BASED_ON;
        self
    }

    /// Remove the parent style during an update.
    pub fn clear_based_on(mut self) -> Self {
        self.style.based_on = None;
        self.cleared |= CLEAR_BASED_ON;
        self
    }

    /// Set the next style (applied to the following paragraph after pressing Enter).
    pub fn next_style(mut self, style_id: &str) -> Self {
        self.style.next_style = Some(style_id.to_string());
        self.cleared &= !CLEAR_NEXT_STYLE;
        self
    }

    /// Remove the following-paragraph style during an update.
    pub fn clear_next_style(mut self) -> Self {
        self.style.next_style = None;
        self.cleared |= CLEAR_NEXT_STYLE;
        self
    }

    /// Link this paragraph style to a character style, or the reverse.
    pub fn linked_style(mut self, style_id: &str) -> Self {
        self.style.linked_style = Some(style_id.to_string());
        self.cleared &= !CLEAR_LINKED_STYLE;
        self
    }

    /// Remove the reciprocal paragraph or character link during an update.
    pub fn clear_linked_style(mut self) -> Self {
        self.style.linked_style = None;
        self.cleared |= CLEAR_LINKED_STYLE;
        self
    }

    /// Set the style's user-interface ordering priority.
    pub fn priority(mut self, priority: u32) -> Self {
        self.style.ui_priority = Some(priority);
        self.cleared &= !CLEAR_PRIORITY;
        self
    }

    /// Remove the user-interface ordering priority during an update.
    pub fn clear_priority(mut self) -> Self {
        self.style.ui_priority = None;
        self.cleared |= CLEAR_PRIORITY;
        self
    }

    /// Set whether Word may redefine the style from direct formatting.
    pub fn auto_redefine(mut self, value: bool) -> Self {
        self.style.auto_redefine = Some(value);
        self.cleared &= !CLEAR_AUTO_REDEFINE;
        self
    }

    /// Remove the automatic-redefinition setting during an update.
    pub fn clear_auto_redefine(mut self) -> Self {
        self.style.auto_redefine = None;
        self.cleared |= CLEAR_AUTO_REDEFINE;
        self
    }

    /// Set whether the style is hidden from all user-interface lists.
    pub fn hidden(mut self, value: bool) -> Self {
        self.style.hidden = Some(value);
        self.cleared &= !CLEAR_HIDDEN;
        self
    }

    /// Remove the hidden setting during an update.
    pub fn clear_hidden(mut self) -> Self {
        self.style.hidden = None;
        self.cleared |= CLEAR_HIDDEN;
        self
    }

    /// Set whether the style is hidden from the main style gallery.
    pub fn semi_hidden(mut self, value: bool) -> Self {
        self.style.semi_hidden = Some(value);
        self.cleared &= !CLEAR_SEMI_HIDDEN;
        self
    }

    /// Remove the semi-hidden setting during an update.
    pub fn clear_semi_hidden(mut self) -> Self {
        self.style.semi_hidden = None;
        self.cleared |= CLEAR_SEMI_HIDDEN;
        self
    }

    /// Set whether applying the style makes it visible in the user interface.
    pub fn unhide_when_used(mut self, value: bool) -> Self {
        self.style.unhide_when_used = Some(value);
        self.cleared &= !CLEAR_UNHIDE_WHEN_USED;
        self
    }

    /// Remove the unhide-when-used setting during an update.
    pub fn clear_unhide_when_used(mut self) -> Self {
        self.style.unhide_when_used = None;
        self.cleared |= CLEAR_UNHIDE_WHEN_USED;
        self
    }

    /// Set whether the style appears in the quick style gallery.
    pub fn quick_format(mut self, value: bool) -> Self {
        self.style.quick_format = Some(value);
        self.cleared &= !CLEAR_QUICK_FORMAT;
        self
    }

    /// Remove the quick-format setting during an update.
    pub fn clear_quick_format(mut self) -> Self {
        self.style.quick_format = None;
        self.cleared |= CLEAR_QUICK_FORMAT;
        self
    }

    /// Set whether the style is locked against application in the user interface.
    pub fn locked(mut self, value: bool) -> Self {
        self.style.locked = Some(value);
        self.cleared &= !CLEAR_LOCKED;
        self
    }

    /// Remove the locked setting during an update.
    pub fn clear_locked(mut self) -> Self {
        self.style.locked = None;
        self.cleared |= CLEAR_LOCKED;
        self
    }

    /// Set paragraph properties for this style.
    pub fn paragraph_properties(mut self, ppr: CT_PPr) -> Self {
        self.style.ppr = Some(ppr);
        self.cleared &= !CLEAR_PARAGRAPH_PROPERTIES;
        self
    }

    /// Remove all paragraph properties during an update.
    pub fn clear_paragraph_properties(mut self) -> Self {
        self.style.ppr = None;
        self.cleared |= CLEAR_PARAGRAPH_PROPERTIES;
        self
    }

    /// Set run properties for this style.
    pub fn run_properties(mut self, rpr: CT_RPr) -> Self {
        self.style.rpr = Some(rpr);
        self.cleared &= !CLEAR_RUN_PROPERTIES;
        self
    }

    /// Remove all run properties during an update.
    pub fn clear_run_properties(mut self) -> Self {
        self.style.rpr = None;
        self.cleared |= CLEAR_RUN_PROPERTIES;
        self
    }

    /// Set base table properties for a table style.
    pub fn table_properties(mut self, properties: CT_TblPr) -> Self {
        self.style.table_properties = Some(properties);
        self.cleared &= !CLEAR_TABLE_PROPERTIES;
        self
    }

    /// Remove all base table properties during an update.
    pub fn clear_table_properties(mut self) -> Self {
        self.style.table_properties = None;
        self.cleared |= CLEAR_TABLE_PROPERTIES;
        self
    }

    /// Remove all conditional table regions during an update.
    pub fn clear_conditional_table_styles(mut self) -> Self {
        self.style.conditional_table_styles.clear();
        self.cleared |= CLEAR_CONDITIONAL_TABLE_STYLES;
        self
    }

    /// Add one conditional table style region in source order.
    pub fn conditional_table_style(
        mut self,
        region: &str,
        paragraph_properties: Option<CT_PPr>,
        table_properties: Option<CT_TblPr>,
        cell_properties: Option<CT_TcPr>,
    ) -> Self {
        self.style.conditional_table_styles.push(CT_TblStylePr {
            region: region.to_string(),
            paragraph_properties,
            table_properties,
            cell_properties,
            extra_attributes: Vec::new(),
            raw_xml: Vec::new(),
        });
        self
    }

    /// Build the style (consumed by Document::add_style).
    pub(crate) fn build(self) -> (CT_Style, u16) {
        (self.style, self.cleared)
    }
}

pub(crate) fn validate_style_graph(styles: &CT_Styles) -> Result<()> {
    let mut by_id = HashMap::new();
    for style in &styles.styles {
        if style.style_id.is_empty() {
            return Err(style_graph_error("style IDs cannot be empty"));
        }
        if by_id.insert(style.style_id.as_str(), style).is_some() {
            return Err(style_graph_error(format!(
                "duplicate style ID '{}'",
                style.style_id
            )));
        }
    }

    for style_type in [
        StyleType::Paragraph,
        StyleType::Character,
        StyleType::Table,
        StyleType::Numbering,
    ] {
        let defaults = styles
            .styles
            .iter()
            .filter(|style| style.style_type == style_type && style.is_default)
            .count();
        if defaults > 1 {
            return Err(style_graph_error(format!(
                "style type '{}' has more than one default",
                style_type.to_str()
            )));
        }
    }

    for style in &styles.styles {
        if style.style_type == StyleType::Character && style.ppr.is_some() {
            return Err(style_graph_error(format!(
                "character style '{}' cannot contain paragraph properties",
                style.style_id
            )));
        }
        if style.style_type != StyleType::Table
            && (style.table_properties.is_some() || !style.conditional_table_styles.is_empty())
        {
            return Err(style_graph_error(format!(
                "{} style '{}' cannot contain table properties",
                style.style_type.to_str(),
                style.style_id
            )));
        }
        let mut conditional_regions = HashSet::new();
        for conditional in &style.conditional_table_styles {
            if !matches!(
                conditional.region.as_str(),
                "wholeTable"
                    | "firstRow"
                    | "lastRow"
                    | "firstCol"
                    | "lastCol"
                    | "band1Vert"
                    | "band2Vert"
                    | "band1Horz"
                    | "band2Horz"
                    | "neCell"
                    | "nwCell"
                    | "seCell"
                    | "swCell"
            ) {
                return Err(style_graph_error(format!(
                    "table style '{}' has invalid conditional region '{}'",
                    style.style_id, conditional.region
                )));
            }
            if !conditional_regions.insert(conditional.region.as_str()) {
                return Err(style_graph_error(format!(
                    "table style '{}' repeats conditional region '{}'",
                    style.style_id, conditional.region
                )));
            }
        }

        if let Some(parent_id) = style.based_on.as_deref() {
            let parent = by_id.get(parent_id).ok_or_else(|| {
                style_graph_error(format!(
                    "style '{}' is based on missing style '{parent_id}'",
                    style.style_id
                ))
            })?;
            if parent.style_type != style.style_type {
                return Err(style_graph_error(format!(
                    "style '{}' cannot be based on {} style '{parent_id}'",
                    style.style_id,
                    parent.style_type.to_str()
                )));
            }
        }

        if let Some(next_id) = style.next_style.as_deref() {
            if style.style_type != StyleType::Paragraph {
                return Err(style_graph_error(format!(
                    "{} style '{}' cannot declare a next style",
                    style.style_type.to_str(),
                    style.style_id
                )));
            }
            let next = by_id.get(next_id).ok_or_else(|| {
                style_graph_error(format!(
                    "style '{}' names missing next style '{next_id}'",
                    style.style_id
                ))
            })?;
            if next.style_type != StyleType::Paragraph {
                return Err(style_graph_error(format!(
                    "paragraph style '{}' has non-paragraph next style '{next_id}'",
                    style.style_id
                )));
            }
        }

        if let Some(linked_id) = style.linked_style.as_deref() {
            let linked = by_id.get(linked_id).ok_or_else(|| {
                style_graph_error(format!(
                    "style '{}' links to missing style '{linked_id}'",
                    style.style_id
                ))
            })?;
            let legal_types = matches!(
                (style.style_type, linked.style_type),
                (StyleType::Paragraph, StyleType::Character)
                    | (StyleType::Character, StyleType::Paragraph)
            );
            if !legal_types {
                return Err(style_graph_error(format!(
                    "{} style '{}' cannot link to {} style '{linked_id}'",
                    style.style_type.to_str(),
                    style.style_id,
                    linked.style_type.to_str()
                )));
            }
            if linked.linked_style.as_deref() != Some(style.style_id.as_str()) {
                return Err(style_graph_error(format!(
                    "linked styles '{}' and '{linked_id}' are not reciprocal",
                    style.style_id
                )));
            }
        }
    }

    for style in &styles.styles {
        let mut seen = HashSet::new();
        let mut current = Some(style.style_id.as_str());
        while let Some(style_id) = current {
            if !seen.insert(style_id) {
                return Err(style_graph_error(format!(
                    "based-on cycle contains style '{style_id}'"
                )));
            }
            current = by_id
                .get(style_id)
                .and_then(|current_style| current_style.based_on.as_deref());
        }
    }

    Ok(())
}

fn style_graph_error(message: impl Into<String>) -> Error {
    Error::Other(format!("invalid style graph: {}", message.into()))
}

/// Resolve the effective paragraph properties by walking the style inheritance chain.
pub fn resolve_paragraph_properties(style_id: Option<&str>, styles: &CT_Styles) -> CT_PPr {
    let mut effective = CT_PPr::default();

    // Start from docDefaults
    if let Some(ref defaults) = styles.doc_defaults
        && let Some(ref ppr) = defaults.ppr
    {
        effective.merge_from(ppr);
    }

    // Walk the selected style's basedOn chain.
    let selected_style_id = style_id.or_else(|| {
        styles
            .get_default(StyleType::Paragraph)
            .map(|style| style.style_id.as_str())
    });
    if let Some(sid) = selected_style_id {
        let chain = collect_style_chain(sid, styles);
        // Apply from most-base to most-derived
        for style in chain.iter().rev() {
            if let Some(ref ppr) = style.ppr {
                effective.merge_from(ppr);
            }
        }
    }

    effective
}

/// Resolve the effective run properties by walking the style inheritance chain.
pub fn resolve_run_properties(
    para_style_id: Option<&str>,
    run_style_id: Option<&str>,
    styles: &CT_Styles,
) -> CT_RPr {
    let mut effective = CT_RPr::default();

    // Start from docDefaults
    if let Some(ref defaults) = styles.doc_defaults
        && let Some(ref rpr) = defaults.rpr
    {
        effective.merge_from(rpr);
    }

    // Apply paragraph style's rpr
    let para_sid = para_style_id.or_else(|| {
        styles
            .get_default(StyleType::Paragraph)
            .map(|s| s.style_id.as_str())
    });
    if let Some(sid) = para_sid {
        let chain = collect_style_chain(sid, styles);
        for style in chain.iter().rev() {
            if let Some(ref rpr) = style.rpr {
                effective.merge_from(rpr);
            }
        }
    }

    // Apply character style's rpr
    if let Some(sid) = run_style_id.and_then(|style_id| character_style_id(style_id, styles)) {
        let chain = collect_style_chain(sid, styles);
        for style in chain.iter().rev() {
            if let Some(ref rpr) = style.rpr {
                effective.merge_from(rpr);
            }
        }
    }

    effective
}

fn character_style_id<'a>(style_id: &'a str, styles: &'a CT_Styles) -> Option<&'a str> {
    let style = styles.get_by_id(style_id)?;
    match style.style_type {
        StyleType::Character => Some(style.style_id.as_str()),
        StyleType::Paragraph => style.linked_style.as_deref().filter(|linked_id| {
            styles
                .get_by_id(linked_id)
                .is_some_and(|linked| linked.style_type == StyleType::Character)
        }),
        StyleType::Table | StyleType::Numbering => None,
    }
}

/// Collect the chain of styles from the given style up through basedOn ancestors.
fn collect_style_chain<'a>(style_id: &str, styles: &'a CT_Styles) -> Vec<&'a CT_Style> {
    let mut chain = Vec::new();
    let mut current_id = Some(style_id.to_string());
    let mut seen = std::collections::HashSet::new();

    while let Some(ref sid) = current_id {
        if !seen.insert(sid.clone()) {
            break; // Prevent cycles
        }
        if let Some(style) = styles.get_by_id(sid) {
            chain.push(style);
            current_id = style.based_on.clone();
        } else {
            break;
        }
    }

    chain
}

#[cfg(test)]
mod tests {
    use super::*;
    use rdocx_oxml::units::{HalfPoint, Twips};

    fn test_styles() -> CT_Styles {
        let mut styles = CT_Styles::new_default();

        // Add a Heading2 based on Heading1
        styles.styles.push(CT_Style {
            style_id: "Heading2".to_string(),
            style_type: StyleType::Paragraph,
            name: Some("heading 2".to_string()),
            based_on: Some("Heading1".to_string()),
            next_style: Some("Normal".to_string()),
            linked_style: None,
            auto_redefine: None,
            hidden: None,
            ui_priority: None,
            semi_hidden: None,
            unhide_when_used: None,
            quick_format: None,
            locked: None,
            is_default: false,
            ppr: Some(CT_PPr {
                space_before: Some(Twips(40)), // Override Heading1's 240
                ..Default::default()
            }),
            rpr: Some(CT_RPr {
                sz: Some(HalfPoint(26)), // Override Heading1's 32
                color: Some("2E74B5".to_string()),
                ..Default::default()
            }),
            table_properties: None,
            table_properties_original: None,
            table_properties_xml: None,
            conditional_table_styles: Vec::new(),
            extra_attributes: Vec::new(),
            modeled_xml: Vec::new(),
            extra_xml: Vec::new(),
        });

        styles
    }

    #[test]
    fn resolve_normal_paragraph() {
        let styles = test_styles();
        let ppr = resolve_paragraph_properties(Some("Normal"), &styles);
        // Should have docDefaults' spacing
        assert_eq!(ppr.space_after, Some(Twips(160)));
    }

    #[test]
    fn resolve_heading1() {
        let styles = test_styles();
        let ppr = resolve_paragraph_properties(Some("Heading1"), &styles);
        // keepNext from Heading1
        assert_eq!(ppr.keep_next, Some(true));
        // spaceBefore from Heading1 (overrides docDefaults which has none)
        assert_eq!(ppr.space_before, Some(Twips(240)));
        // spaceAfter from Heading1 overrides docDefaults
        assert_eq!(ppr.space_after, Some(Twips(0)));
    }

    #[test]
    fn resolve_heading2_inherits_heading1() {
        let styles = test_styles();
        let ppr = resolve_paragraph_properties(Some("Heading2"), &styles);
        // keepNext inherited from Heading1
        assert_eq!(ppr.keep_next, Some(true));
        // spaceBefore overridden by Heading2
        assert_eq!(ppr.space_before, Some(Twips(40)));
    }

    #[test]
    fn resolve_heading2_rpr() {
        let styles = test_styles();
        let rpr = resolve_run_properties(Some("Heading2"), None, &styles);
        // Font from docDefaults
        assert_eq!(rpr.font_ascii, Some("Calibri".to_string()));
        // Size overridden by Heading2 (not Heading1's 32)
        assert_eq!(rpr.sz, Some(HalfPoint(26)));
        // Bold inherited from Heading1
        assert_eq!(rpr.bold, Some(true));
        // Color from Heading2
        assert_eq!(rpr.color, Some("2E74B5".to_string()));
    }

    #[test]
    fn resolve_default_when_no_style() {
        let mut styles = test_styles();
        for style in &mut styles.styles {
            style.is_default = style.style_id == "Heading2";
        }
        let ppr = resolve_paragraph_properties(None, &styles);
        assert_eq!(ppr.keep_next, Some(true));
        assert_eq!(ppr.space_before, Some(Twips(40)));
        assert_eq!(ppr.space_after, Some(Twips(0)));
    }
}