markupsth 0.2.0

Crate to define and print out formatted Markup Languages.
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
//! Module contains the pre-implemented formatters of this crate. Suche as the `NoFormatting`, the
//! `AlwaysIndentAlwaysLf`, and the `AutoIndent`, which are ready to be used without any effort.
//!
//! All of the three pre-implemented formatters implement for sure the trait `Formatter`. Formatter
//! `AutoIndent` implements the additional feature trait `FixedRuleset` as well. Have a look at
//! their further documentation to get an overview.
//!
//! To overwrite the default formatter apply:
//! ```
//! use markupsth::{Formatter, Language, MarkupSth, NoFormatting};
//! // use markupsth::{AlwaysIndentAlwaysLf, Formatter, Language, MarkupSth};
//!
//! let mut doc = String::new();
//! let mut mus = MarkupSth::new(&mut doc, Language::Html).unwrap();
//!
//! mus.set_formatter(Box::new(NoFormatting::new()));
//! // mus.set_formatter(Box::new(AlwaysIndentAlwaysLf::new()));
//! ```
//!
//! To modify the default `AutoIndent` formatter use either the trait methods from trait
//! `Formatter` or `FixedRuleset` defined in module `format`.
//!
//! ### `NoFormatting`
//!
//! A pre-implemented formatter for havin a strict indenting and always linefeeds between tags.
//!
//! You want to have the clearest readable Markup file you can imagine, then this formatter is
//! yours. Output files may be suitable for debugging and error search, but maybe too pendantic.
//!
//! ### `AlwaysIndentAlwaysLf`
//!
//! A pre-implemented formatter for havin a strict indenting and always linefeeds between tags.
//!
//! You want to have the clearest readable Markup file you can imagine, then this formatter is
//! yours. Output files may be suitable for debugging and error search, but maybe too pendantic.
//!
//! ### `AutoIndent`
//!
//! A pre-implemented formatter which applies the fixed ruleset and auto-detects additional
//! indenting.
//!
//! This formatter applies the three fixed rules described in the Fixed Ruleset in module `format`,
//! but does also detect indenting-changes, when adding manual linefeeds. In general this formatter
//! tries to hit a very good and readable output, especially HTML and XML files.
//!
//! Inserting a linefeed by using `MarkupSth::new_line()` after an opening tag, will cause an
//! increase on indenting. It will automatically decreased when closing the pair by inserting the
//! closing tag. Tags who have been added to the ruleset will be treated by rule without any
//! further input.
//!
//! Depending on your personal taste, a good setup for HTML might be:
//! ```
//! # use markupsth::{FixedRule, FixedRuleset, Language, MarkupSth};
//! # let mut doc = String::new();
//! # let mut mus = MarkupSth::new(&mut doc, Language::Html).unwrap();
//! # let fmtr = mus.formatter.optional_fixed_ruleset().unwrap();
//! fmtr.add_tags_to_rule(
//!     &["head", "body", "header", "nav", "section", "footer"],
//!     FixedRule::IndentAlways
//!     ).unwrap();
//! fmtr.add_tags_to_rule(
//!     &["html"],
//!     FixedRule::LfAlways
//!     ).unwrap();
//! fmtr.add_tags_to_rule(
//!     &["p", "div", "link"],
//!     FixedRule::LfClosing
//!     ).unwrap();
//! ```

use crate::{format::*, Result};

/// A pre-implemented formatter for having no formatting at all. No linefeeds, no indenting at all.
///
/// You want no linefeeds, no indenting at all, this is your formatter! Suitable use cases may be
/// to generate a pure HTML file, which will only read by browsers for pure optimization.
#[derive(Debug)]
pub struct NoFormatting;

impl Formatter for NoFormatting {
    fn new() -> NoFormatting {
        NoFormatting
    }

    fn check(&mut self, _: &SequenceState) -> FormatChanges {
        FormatChanges::nothing()
    }
}

/// A pre-implemented formatter for havin a strict indenting and always linefeeds between tags.
///
/// You want to have the clearest readable Markup file you can imagine, then this formatter is
/// yours. Output files may be suitable for debugging and error search, but maybe too pendantic.
#[derive(Debug)]
pub struct AlwaysIndentAlwaysLf(usize);

impl Formatter for AlwaysIndentAlwaysLf {
    fn new() -> AlwaysIndentAlwaysLf {
        AlwaysIndentAlwaysLf(DEFAULT_INDENT)
    }

    fn set_indent_step_size(&mut self, step_size: usize) {
        self.0 = step_size;
    }

    fn get_indent_step_size(&self) -> usize {
        self.0
    }

    fn reset_to_defaults(&mut self) {
        self.0 = DEFAULT_INDENT;
    }

    fn check(&mut self, state: &SequenceState) -> FormatChanges {
        if matches!(state.next.0, Sequence::Closing) {
            match state.last.0 {
                Sequence::Opening => FormatChanges::may_lf(true),
                _ => FormatChanges::indent_less(state.indent, self.0),
            }
        } else {
            match state.last.0 {
                Sequence::Initial => FormatChanges::may_lf(true),
                Sequence::Opening => match state.next.0 {
                    Sequence::SelfClosing | Sequence::Text | Sequence::Opening => {
                        FormatChanges::indent_more(state.indent, self.0)
                    }
                    _ => FormatChanges::nothing(),
                },
                Sequence::Closing => FormatChanges::may_lf(true),
                Sequence::SelfClosing => FormatChanges::may_lf(true),
                _ => FormatChanges::nothing(),
            }
        }
    }
}

/// A pre-implemented formatter which applies the fixed ruleset and auto-detects additional
/// indenting.
///
/// This formatter applies the three fixed rules described in the Fixed Ruleset in module `format`,
/// but does also detect indenting-changes, when adding manual linefeeds. In general this formatter
/// tries to hit a very good and readable output, especially HTML and XML files.
///
/// Inserting a linefeed by using `MarkupSth::new_line()` after an opening tag, will cause an
/// increase on indenting. It will automatically decreased when closing the pair by inserting the
/// closing tag. Tags who have been added to the ruleset will be treated by rule without any
/// further input.
///
/// Depending on your personal taste, a good setup for HTML might be:
/// ```
/// # use markupsth::{FixedRule, Language, MarkupSth};
/// # let mut doc = String::new();
/// # let mut mus = MarkupSth::new(&mut doc, Language::Html).unwrap();
/// # let fmtr = mus.formatter.optional_fixed_ruleset().unwrap();
/// fmtr.add_tags_to_rule(
///     &["head", "body", "header", "nav", "section", "footer"],
///     FixedRule::IndentAlways
///     ).unwrap();
/// fmtr.add_tags_to_rule(
///     &["html"],
///     FixedRule::LfAlways
///     ).unwrap();
/// fmtr.add_tags_to_rule(
///     &["p", "div", "link"],
///     FixedRule::LfClosing
///     ).unwrap();
/// ```
#[derive(Debug)]
pub struct AutoIndent {
    /// List for tags, where content has always to be indented.
    pub fltr_indent_always: Vec<String>,
    /// List for tags, where a LINEFEED shall always be inserted.
    pub fltr_lf_always: Vec<String>,
    /// List for tags, where a LINEFEED shall inserted after closing tags.
    pub fltr_lf_closing: Vec<String>,
    /// Internal, operational, for tracking whether indented or not.
    indent_stack: Vec<bool>,
    /// The indenting step size.
    indent_step: usize,
}

impl AutoIndent {
    // Internal method to check if tags are in another filter too.
    fn check_other_filter(&self, tags: &[&str], fltr: FixedRule, other: FixedRule) -> Result<()> {
        let errtags: Vec<String> = tags
            .iter()
            .filter(|t| self.is_ts_in_filter(&TagSequence::opening(t), other))
            .map(|t| t.to_string())
            .collect();
        if errtags.is_empty() {
            Ok(())
        } else {
            Err(format!(
                "{}({:?}), {} {:?} too: {:?}",
                "AutoIndent::add_tags_to_rule",
                fltr,
                "trying to add tags which have already been added to filter",
                other,
                errtags
            )
            .into())
        }
    }

    /// Internal check method, if tag is contained in filter `fltr`.
    fn is_ts_in_filter(&self, tagseq: &TagSequence, fltr: FixedRule) -> bool {
        let fltr: &Vec<String> = match fltr {
            FixedRule::IndentAlways => &self.fltr_indent_always,
            FixedRule::LfAlways => &self.fltr_lf_always,
            FixedRule::LfClosing => &self.fltr_lf_closing,
        };
        for tf in fltr.iter() {
            if tf == &tagseq.1 {
                return true;
            }
        }
        false
    }

    /// Internal check method, if tag is contained in filter `fltr` and of type `seq`.
    fn is_ts_in_fltr_aot(&self, tagseq: &TagSequence, fltr: FixedRule, seq: Sequence) -> bool {
        if tagseq.0 != seq {
            return false;
        }
        self.is_ts_in_filter(tagseq, fltr)
    }
}

impl Formatter for AutoIndent {
    fn new() -> AutoIndent {
        AutoIndent {
            fltr_indent_always: Vec::new(),
            fltr_lf_always: Vec::new(),
            fltr_lf_closing: Vec::new(),
            indent_stack: Vec::new(),
            indent_step: DEFAULT_INDENT,
        }
    }

    fn set_indent_step_size(&mut self, step_size: usize) {
        self.indent_step = step_size;
    }

    fn get_indent_step_size(&self) -> usize {
        self.indent_step
    }

    fn reset_to_defaults(&mut self) {
        self.fltr_indent_always.clear();
        self.fltr_lf_always.clear();
        self.fltr_lf_closing.clear();
        self.indent_step = DEFAULT_INDENT;
    }

    fn optional_fixed_ruleset(&mut self) -> Option<&mut dyn FixedRuleset> {
        Some(self)
    }

    fn check(&mut self, state: &SequenceState) -> FormatChanges {
        let mut changes = FormatChanges::nothing();

        if matches!(state.next.0, Sequence::Closing) {
            // In case of a following closing tag, everything behaves a little different,
            // because of optional less-indenting.
            if matches!(state.last.0, Sequence::Opening)
                && self.is_ts_in_filter(&state.last, FixedRule::LfAlways)
            {
                // Detect if lf or not...
                changes = FormatChanges::lf();
            } else if let Some(true) = self.indent_stack.pop() {
                // otherwise we can check for a less-indenting on indent_stack!
                changes = FormatChanges::indent_less(state.indent, self.indent_step);
            } else if self.is_ts_in_fltr_aot(
                &state.last,
                FixedRule::IndentAlways,
                Sequence::Closing,
            ) || self.is_ts_in_filter(&state.last, FixedRule::LfAlways)
                || self.is_ts_in_fltr_aot(&state.last, FixedRule::LfClosing, Sequence::Closing)
                || self.is_ts_in_fltr_aot(&state.last, FixedRule::LfClosing, Sequence::SelfClosing)
            {
                changes = FormatChanges::lf();
            }
        } else {
            match state.last.0 {
                Sequence::Opening => {
                    // After an opening-tag LINEFEED and optional indenting can be desired
                    // Anyway, for each opening tag we add a flag for indenting on the internal stack.
                    let do_indent = (matches!(state.next.0, Sequence::LineFeed)
                        && !self.is_ts_in_filter(&state.last, FixedRule::LfAlways))
                        || self.is_ts_in_filter(&state.last, FixedRule::IndentAlways);
                    self.indent_stack.push(do_indent);
                    if do_indent {
                        changes = FormatChanges::indent_more(state.indent, self.indent_step);
                    } else if self.is_ts_in_filter(&state.last, FixedRule::LfAlways) {
                        changes = FormatChanges::lf();
                    }
                }
                Sequence::Closing => {
                    // After a closing-tag a LINEFEED can be desired
                    if self.is_ts_in_filter(&state.last, FixedRule::IndentAlways)
                        || self.is_ts_in_filter(&state.last, FixedRule::LfAlways)
                        || self.is_ts_in_filter(&state.last, FixedRule::LfClosing)
                    {
                        changes = FormatChanges::lf();
                    }
                }
                Sequence::SelfClosing => {
                    if self.is_ts_in_fltr_aot(
                        &state.last,
                        FixedRule::LfClosing,
                        Sequence::SelfClosing,
                    ) {
                        changes = FormatChanges::lf();
                    }
                }
                Sequence::Initial => {
                    // If last tag was the initial document sequence, also line feed always!
                    changes = FormatChanges::lf()
                }
                _ => {}
            }
        }
        changes
    }
}

impl FixedRuleset for AutoIndent {
    fn add_tags_to_rule(&mut self, tags: &[&str], rule: FixedRule) -> Result<()> {
        match rule {
            FixedRule::IndentAlways => {
                self.check_other_filter(tags, FixedRule::IndentAlways, FixedRule::LfAlways)?;
                self.fltr_indent_always = tags.iter().map(|s| s.to_string()).collect();
            }
            FixedRule::LfAlways => {
                self.check_other_filter(tags, FixedRule::LfAlways, FixedRule::IndentAlways)?;
                self.check_other_filter(tags, FixedRule::LfAlways, FixedRule::LfClosing)?;
                self.fltr_lf_always = tags.iter().map(|s| s.to_string()).collect();
            }
            FixedRule::LfClosing => {
                self.check_other_filter(tags, FixedRule::LfClosing, FixedRule::LfAlways)?;
                self.fltr_lf_closing = tags.iter().map(|s| s.to_string()).collect();
            }
        }
        Ok(())
    }

    fn reset_ruleset(&mut self) -> Result<()> {
        self.fltr_indent_always.clear();
        self.fltr_lf_always.clear();
        self.fltr_lf_closing.clear();
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use totems::assert_err;

    const NOTHING: FormatChanges = FormatChanges {
        new_line: false,
        new_indent: None,
    };
    const LINEFEED: FormatChanges = FormatChanges {
        new_line: true,
        new_indent: None,
    };
    const INDENT_LESS: FormatChanges = FormatChanges {
        new_line: true,
        new_indent: Some(0),
    };
    const INDENT_MORE: FormatChanges = FormatChanges {
        new_line: true,
        new_indent: Some(8),
    };

    fn get_formatters_list() -> Vec<Box<dyn Formatter>> {
        vec![
            Box::new(NoFormatting::new()),
            Box::new(AlwaysIndentAlwaysLf::new()),
            Box::new(AutoIndent::new()),
        ]
    }

    #[test]
    fn all_initially_default() {
        for fmt in get_formatters_list().iter_mut() {
            assert_eq!(fmt.get_indent_step_size(), DEFAULT_INDENT);
        }
    }

    #[test]
    fn after_reset_default_again() {
        for fmt in get_formatters_list().iter_mut() {
            fmt.set_indent_step_size(DEFAULT_INDENT + 1);
            fmt.reset_to_defaults();
            assert_eq!(fmt.get_indent_step_size(), DEFAULT_INDENT);
        }
    }

    // Because opening tags are influencing the AutoIndent's state, consider open tags!!!
    // Meaningful to test in rows of three, e.g. <div><img></div>, <p>text</p>, except for
    // special cases like <div></div>.

    #[test]
    fn auto_indenting_after_initial() {
        let mut fmtr = Box::new(AutoIndent::new());
        assert_eq!(fmtr.check(&SequenceState::initial_open("html")), LINEFEED);
    }

    #[test]
    fn auto_indenting_rule_always_indent() {
        let mut fmtr = Box::new(AutoIndent::new());
        fmtr.add_tags_to_rule(&["html"], FixedRule::IndentAlways)
            .unwrap();

        // Test: Auto-indenting works on non-registered tags with manual LF.
        // <body>\n<img></body>
        assert_eq!(fmtr.check(&SequenceState::open_lf("body")), INDENT_MORE);
        assert_eq!(fmtr.check(&SequenceState::lf_self_closing("img")), NOTHING);
        assert_eq!(
            fmtr.check(&SequenceState::self_closing_close("img", "body")),
            INDENT_LESS
        );

        // Test: Auto-indenting works on registered tags with manual LF.
        // <html>\n<img></html>
        assert_eq!(fmtr.check(&SequenceState::open_lf("html")), INDENT_MORE);
        assert_eq!(fmtr.check(&SequenceState::lf_self_closing("img")), NOTHING);
        assert_eq!(
            fmtr.check(&SequenceState::self_closing_close("img", "html")),
            INDENT_LESS
        );

        // Test: Auto-indenting works on registered tags without input.
        // <html><img></html>
        assert_eq!(
            fmtr.check(&SequenceState::open_self_closing("html", "img")),
            INDENT_MORE
        );
        assert_eq!(
            fmtr.check(&SequenceState::self_closing_close("img", "html")),
            INDENT_LESS
        );

        // Test: Multiple stages with manual and auto-indenting mixed tags.
        assert_eq!(
            fmtr.check(&SequenceState::open_open("html", "body")),
            INDENT_MORE
        );
        assert_eq!(fmtr.check(&SequenceState::open_lf("body")), INDENT_MORE);
        assert_eq!(fmtr.check(&SequenceState::lf_self_closing("img")), NOTHING);
        assert_eq!(
            fmtr.check(&SequenceState::self_closing_close("img", "body")),
            INDENT_LESS
        );
        assert_eq!(
            fmtr.check(&SequenceState::close_close("body", "html")),
            INDENT_LESS
        );

        // ? Error on self-closing tags if added to always-indent ?
    }

    #[test]
    fn auto_indenting_rule_lf_always() {
        let mut fmtr = Box::new(AutoIndent::new());
        fmtr.add_tags_to_rule(&["html"], FixedRule::LfAlways)
            .unwrap();

        // Test: Auto-LF after opening-tag and after closing-tag of registered tags.
        // <html><img></html>
        assert_eq!(
            fmtr.check(&SequenceState::open_self_closing("html", "img")),
            LINEFEED
        );
        assert_eq!(
            fmtr.check(&SequenceState::self_closing_close("img", "html")),
            NOTHING
        );
        assert_eq!(fmtr.check(&SequenceState::close_text("html")), LINEFEED);

        // Test: No auto-LF after opening- and closing-tags of non-registered tags.
        // <body><img></body>
        assert_eq!(
            fmtr.check(&SequenceState::open_self_closing("body", "img")),
            NOTHING
        );
        assert_eq!(
            fmtr.check(&SequenceState::self_closing_close("img", "body")),
            NOTHING
        );
        assert_eq!(fmtr.check(&SequenceState::close_text("body")), NOTHING);

        // Test: Auto-indenting after registered tags and opening-tag folowed by LF.
        // <html>\n<img></html>
        assert_eq!(fmtr.check(&SequenceState::open_lf("html")), LINEFEED);
        assert_eq!(fmtr.check(&SequenceState::lf_self_closing("img")), NOTHING);
        assert_eq!(
            fmtr.check(&SequenceState::self_closing_close("img", "html")),
            NOTHING
        );
        assert_eq!(fmtr.check(&SequenceState::close_text("html")), LINEFEED);

        // Test: Auto-indenting after non-registered tags and following LF.
        // <body>\n<img></body>
        assert_eq!(fmtr.check(&SequenceState::open_lf("body")), INDENT_MORE);
        assert_eq!(fmtr.check(&SequenceState::lf_self_closing("img")), NOTHING);
        assert_eq!(
            fmtr.check(&SequenceState::self_closing_close("img", "body")),
            INDENT_LESS
        );
        assert_eq!(fmtr.check(&SequenceState::close_text("body")), NOTHING);

        // ? Error on self-closing tags if added to lf-always ?
    }

    #[test]
    fn auto_indenting_rule_lf_closing() {
        let mut fmtr = Box::new(AutoIndent::new());
        fmtr.add_tags_to_rule(&["html", "img"], FixedRule::LfClosing)
            .unwrap();

        // Test: open-close works without problems, no formatting at all in between.
        // <html></html>
        assert_eq!(
            fmtr.check(&SequenceState::open_close("html", "html")),
            NOTHING
        );

        // Test: Auto-LF after closing block will be inserted.
        // <html>Text</html>
        assert_eq!(fmtr.check(&SequenceState::open_text("html")), NOTHING);
        assert_eq!(fmtr.check(&SequenceState::text_close("html")), NOTHING);
        assert_eq!(fmtr.check(&SequenceState::close_text("html")), LINEFEED);

        // Test: No auto-LF after non-listed tags.
        // <body>Text</body>
        assert_eq!(fmtr.check(&SequenceState::open_text("body")), NOTHING);
        assert_eq!(fmtr.check(&SequenceState::text_close("body")), NOTHING);
        assert_eq!(fmtr.check(&SequenceState::close_text("body")), NOTHING);

        // Test: Auto-indenting on open-lf for non-listed tags.
        // <body>\n<link></body>
        assert_eq!(fmtr.check(&SequenceState::open_lf("body")), INDENT_MORE);
        assert_eq!(fmtr.check(&SequenceState::lf_self_closing("link")), NOTHING);
        assert_eq!(
            fmtr.check(&SequenceState::self_closing_close("link", "body")),
            INDENT_LESS
        );
        assert_eq!(fmtr.check(&SequenceState::close_text("body")), NOTHING);

        // Test: Auto-indenting on open-lf for listed tags.
        // <html>\n<link></html>
        assert_eq!(fmtr.check(&SequenceState::open_lf("html")), INDENT_MORE);
        assert_eq!(fmtr.check(&SequenceState::lf_self_closing("link")), NOTHING);
        assert_eq!(
            fmtr.check(&SequenceState::self_closing_close("link", "html")),
            INDENT_LESS
        );
        assert_eq!(fmtr.check(&SequenceState::close_text("html")), LINEFEED);

        // Test: Auto-LF after listed self-closing tags.
        // <div><img></div>
        assert_eq!(
            fmtr.check(&SequenceState::open_self_closing("div", "img")),
            NOTHING
        );
        assert_eq!(
            fmtr.check(&SequenceState::self_closing_close("img", "div")),
            LINEFEED
        );
        assert_eq!(fmtr.check(&SequenceState::close_text("div")), NOTHING);

        // Test: No auto-LF after non-listed self-clsoing tags.
        // already tested that before two times.
    }

    #[test]
    fn auto_indenting_mixed_rules() {
        let mut fmtr = Box::new(AutoIndent::new());

        // Test some error messages
        fmtr.add_tags_to_rule(&["html", "body"], FixedRule::IndentAlways)
            .unwrap();
        assert_err!(fmtr.add_tags_to_rule(&["html", "head"], FixedRule::LfAlways));
        fmtr.add_tags_to_rule(&["html", "body"], FixedRule::LfClosing)
            .unwrap();
        fmtr.reset_to_defaults();

        fmtr.add_tags_to_rule(&["html", "body"], FixedRule::LfAlways)
            .unwrap();
        assert_err!(fmtr.add_tags_to_rule(&["html", "head"], FixedRule::IndentAlways));
        assert_err!(fmtr.add_tags_to_rule(&["body", "header"], FixedRule::LfClosing));
        fmtr.reset_to_defaults();

        // Now, test setup for the operational test cases.
        fmtr.add_tags_to_rule(&["head", "body"], FixedRule::IndentAlways)
            .unwrap();
        fmtr.add_tags_to_rule(&["html"], FixedRule::LfAlways)
            .unwrap();
        fmtr.add_tags_to_rule(&["body", "div"], FixedRule::LfClosing)
            .unwrap();

        // Test: Auto-indenting combined with lf-always.
        // <html><head><link></head></html>
        assert_eq!(
            fmtr.check(&SequenceState::open_open("html", "head")),
            LINEFEED
        );
        assert_eq!(
            fmtr.check(&SequenceState::open_self_closing("head", "link")),
            INDENT_MORE
        );
        assert_eq!(
            fmtr.check(&SequenceState::self_closing_close("link", "head")),
            INDENT_LESS
        );
        assert_eq!(
            fmtr.check(&SequenceState::close_close("head", "html")),
            LINEFEED
        );
        assert_eq!(fmtr.check(&SequenceState::close_text("html")), LINEFEED);

        // Test: Auto-indenting works also if lf-closing activated too.
        // <body><div>Text</div></body>
        assert_eq!(
            fmtr.check(&SequenceState::open_open("body", "div")),
            INDENT_MORE
        );
        assert_eq!(fmtr.check(&SequenceState::open_text("div")), NOTHING);
        assert_eq!(fmtr.check(&SequenceState::text_close("div")), NOTHING);
        assert_eq!(
            fmtr.check(&SequenceState::close_close("div", "body")),
            INDENT_LESS
        );
        assert_eq!(fmtr.check(&SequenceState::close_text("body")), LINEFEED);

        // Test: Auto-indenting works also if lf-closing on self-closing tags activated.
        // <body><img></body>
        assert_eq!(
            fmtr.check(&SequenceState::open_self_closing("body", "img")),
            INDENT_MORE
        );
        assert_eq!(
            fmtr.check(&SequenceState::self_closing_close("img", "body")),
            INDENT_LESS
        );
        assert_eq!(fmtr.check(&SequenceState::close_text("body")), LINEFEED);
    }
}