asciidoc-parser 0.28.0

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
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
use super::{MatchedItem, Span};
use crate::internal::is_word_char;

impl<'src> Span<'src> {
    /// Split the span, consuming a [block macro] name if found.
    ///
    /// A macro name begins with a word character (a letter, numeral, or
    /// underscore) and continues with any number of word or `-` characters
    /// (e.g., `image`, `toc`, or `foo-bar`). This mirrors the name capture of
    /// Asciidoctor's `BlockMacroRx` (`#{CG_WORD}[#{CC_WORD}-]*`), except that –
    /// as elsewhere in this crate – a "word character" is restricted to ASCII
    /// (`[A-Za-z0-9_]`); accepting the full Unicode word class is tracked
    /// separately. (The same name grammar governs inline macros, but the sole
    /// caller today parses block macros.)
    ///
    /// [block macro]: https://docs.asciidoctor.org/asciidoc/latest/macros/block-macro-processor/
    pub(crate) fn take_block_macro_name(self) -> Option<MatchedItem<'src, Self>> {
        let mut chars = self.data.char_indices();

        let (_, c) = chars.next()?;
        if !is_attr_name_start(c) {
            return None;
        }

        for (index, c) in chars {
            if !is_block_macro_name_continuation(c) {
                return Some(self.into_parse_result(index));
            }
        }

        Some(self.into_parse_result(self.len()))
    }

    /// Split the span, consuming an attribute name if found.
    ///
    /// An [attribute name] begins with a word character (a letter, numeral, or
    /// underscore) and continues with any number of word, `-`, or `.`
    /// characters (e.g., `see-also`, `_foo`, or `foo.foo`). This mirrors
    /// Asciidoctor's `AttributeList::NameRx` (`#{CG_WORD}[#{CC_WORD}\-.]*`),
    /// except that – as elsewhere in this crate – a "word character" is
    /// restricted to ASCII (`[A-Za-z0-9_]`); accepting the full Unicode word
    /// class is tracked separately.
    ///
    /// [attribute name]: https://docs.asciidoctor.org/asciidoc/latest/attributes/positional-and-named-attributes/#attribute-list-parsing
    pub(crate) fn take_attr_name(self) -> Option<MatchedItem<'src, Self>> {
        let mut chars = self.data.char_indices();

        let (_, c) = chars.next()?;
        if !is_attr_name_start(c) {
            return None;
        }

        for (index, c) in chars {
            if !is_attr_name_continuation(c) {
                return Some(self.into_parse_result(index));
            }
        }

        Some(self.into_parse_result(self.len()))
    }

    /// Split the span, consuming the raw name of an [attribute entry].
    ///
    /// The name of an attribute entry begins with a [word character] (any
    /// Unicode letter, digit, or `_`, matching Asciidoctor's `\p{Word}`) and
    /// runs up to – but not including – the closing colon (`:`) that terminates
    /// it. Mirroring Asciidoctor's
    /// `AttributeEntryRx` (whose capture is `!?#{CG_WORD}[^:]*`), the
    /// characters after the first are otherwise unconstrained: the raw name may
    /// contain spaces and other characters that are not valid in a canonical
    /// attribute name (e.g. `Author Initials` or `Foo 3^ # - Bar[`).
    ///
    /// Returns `None` when the span is empty or does not begin with a word
    /// character. When the span contains no colon, the whole span is consumed
    /// as the name (the caller then fails to find the terminating colon).
    ///
    /// IMPORTANT: This function performs no normalization. The captured name is
    /// sanitized – lower-cased, with every character that is not a word
    /// character or `-` stripped – before it is used as an attribute key, so
    /// `URL-Repo` becomes `url-repo` and `Author Initials` becomes
    /// `authorinitials`. See `remap_attr_name` in the parser.
    ///
    /// [attribute entry]: https://docs.asciidoctor.org/asciidoc/latest/attributes/names-and-values/#user-defined
    /// [word character]: crate::internal::is_word_char
    pub(crate) fn take_user_attr_name(self) -> Option<MatchedItem<'src, Self>> {
        let first = self.data.chars().next()?;
        if !is_word_char(first) {
            return None;
        }

        match self.position(|c| c == ':') {
            Some(index) => Some(self.into_parse_result(index)),
            None => Some(self.into_parse_result(self.len())),
        }
    }

    /// Returns [`true`] if the span properly forms an [XML Name].
    ///
    /// [XML Name]: https://www.w3.org/TR/REC-xml/#NT-Name
    pub(crate) fn is_xml_name(self) -> bool {
        let mut chars = self.data.chars();

        if let Some(c) = chars.next() {
            match c {
                ':' | '_' => (),
                '\u{037e}' => {
                    return false;
                }
                _ => {
                    if !c.is_alphabetic() {
                        return false;
                    }
                }
            }
        } else {
            return false;
        }

        chars.all(|c| match c {
            ':' | '_' | '.' | '-' | '\u{00b7}' => true,
            '\u{037e}' => false,
            _ => c.is_alphanumeric(),
        })
    }

    /// Split the span, consuming one quoted string if found.
    ///
    /// A string is defined as a single quote or double quote character,
    /// followed by any number of other characters, and terminated by a matching
    /// single or double quote character.
    ///
    /// The single or double quote may be included in the string by preceding it
    /// with a backslash. No other backslash escape sequences are recognized.
    ///
    /// **IMPORTANT:** The [`Span`] that is returned does not include the start
    /// or ending quote, but _does_ include (without transformation) any
    /// escaped quotes.
    pub(crate) fn take_quoted_string(self) -> Option<MatchedItem<'src, Self>> {
        let mut chars = self.data.char_indices();

        let delimiter = match chars.next() {
            Some((_, '\'')) => '\'',
            Some((_, '"')) => '"',
            _ => {
                return None;
            }
        };

        let mut prev_was_backslash = false;

        for (index, c) in chars {
            if c == delimiter && !prev_was_backslash {
                return Some(MatchedItem {
                    item: self.slice(1..index),
                    after: self.slice_from(index + 1..),
                });
            }
            prev_was_backslash = c == '\\';
        }

        // Didn't find closing delimiter.
        None
    }

    /// Given a second [`Span`], which must be a trailing remainder of `self`,
    /// return the portion of `self` that excludes the second (remainder).
    ///
    /// Note that the trailing remainder condition is not enforced.
    pub(crate) fn trim_remainder(self, after: Span<'src>) -> Span<'src> {
        let offset = (self.offset + self.len()).min(after.offset);

        if offset <= self.offset {
            // Invalid input: We'll respond with an empty slice.
            self.slice(0..0)
        } else {
            let trim_len = offset - self.offset;
            self.slice(0..trim_len)
        }
    }
}

/// Returns `true` when `c` may begin an attribute-list name token: an ASCII
/// word character (letter, digit, or underscore). See [`Span::take_attr_name`].
fn is_attr_name_start(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '_'
}

/// Returns `true` when `c` may continue an attribute-list name token: an ASCII
/// word character, a hyphen, or a dot. See [`Span::take_attr_name`].
fn is_attr_name_continuation(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == '.'
}

/// Returns `true` when `c` may continue a macro name token: an ASCII word
/// character or a hyphen. See [`Span::take_block_macro_name`].
fn is_block_macro_name_continuation(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '_' || c == '-'
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]

    mod take_block_macro_name {
        use crate::tests::prelude::*;

        #[test]
        fn empty_source() {
            let span = crate::Span::default();
            assert!(span.take_block_macro_name().is_none());
        }

        #[test]
        fn starts_with_non_word() {
            let span = crate::Span::new("#not-a-proper-name");
            assert!(span.take_block_macro_name().is_none());
        }

        #[test]
        fn starts_with_hyphen() {
            let span = crate::Span::new("-not-a-proper-name");
            assert!(span.take_block_macro_name().is_none());
        }

        #[test]
        fn starts_with_number() {
            // A numeral is a word character, so it may begin a macro name
            // (Asciidoctor's `BlockMacroRx` accepts a leading digit).
            let span = crate::Span::new("9x!");
            let mi = span.take_block_macro_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "9x",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "!",
                    line: 1,
                    col: 3,
                    offset: 2
                }
            );
        }

        #[test]
        fn stops_at_non_name_char() {
            let span = crate::Span::new("x#");
            let mi = span.take_block_macro_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "x",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "#",
                    line: 1,
                    col: 2,
                    offset: 1
                }
            );
        }

        #[test]
        fn stops_at_dot() {
            // Unlike an attribute name, a macro name does not include `.`, so
            // the dot terminates the name.
            let span = crate::Span::new("foo.bar::baz[]");
            let mi = span.take_block_macro_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "foo",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: ".bar::baz[]",
                    line: 1,
                    col: 4,
                    offset: 3
                }
            );
        }

        #[test]
        fn alpha_numeric() {
            let span = crate::Span::new("i94!");
            let mi = span.take_block_macro_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "i94",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "!",
                    line: 1,
                    col: 4,
                    offset: 3
                }
            );
        }

        #[test]
        fn starts_with_underscore() {
            let span = crate::Span::new("_i94!");
            let mi = span.take_block_macro_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "_i94",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "!",
                    line: 1,
                    col: 5,
                    offset: 4
                }
            );
        }

        #[test]
        fn contains_underscores() {
            let span = crate::Span::new("blah_blah_94 = foo");
            let mi = span.take_block_macro_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "blah_blah_94",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: " = foo",
                    line: 1,
                    col: 13,
                    offset: 12
                }
            );
        }

        #[test]
        fn contains_hyphens() {
            // Hyphens are valid continuation characters, so `blah-blah-94` is a
            // single macro name token.
            let span = crate::Span::new("blah-blah-94 = foo");
            let mi = span.take_block_macro_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "blah-blah-94",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: " = foo",
                    line: 1,
                    col: 13,
                    offset: 12
                }
            );
        }

        #[test]
        fn stops_at_eof() {
            let span = crate::Span::new("xyz");
            let mi = span.take_block_macro_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "xyz",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "",
                    line: 1,
                    col: 4,
                    offset: 3
                }
            );
        }
    }

    mod take_attr_name {
        use crate::tests::prelude::*;

        #[test]
        fn empty_source() {
            let span = crate::Span::default();
            assert!(span.take_attr_name().is_none());
        }

        #[test]
        fn starts_with_non_word() {
            let span = crate::Span::new("#not-a-proper-name");
            assert!(span.take_attr_name().is_none());
        }

        #[test]
        fn starts_with_hyphen() {
            let span = crate::Span::new("-not-a-proper-name");
            assert!(span.take_attr_name().is_none());
        }

        #[test]
        fn stops_at_non_ident() {
            let span = crate::Span::new("x#");
            let mi = span.take_attr_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "x",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "#",
                    line: 1,
                    col: 2,
                    offset: 1
                }
            );
        }

        #[test]
        fn numeric() {
            let span = crate::Span::new("94!");
            let mi = span.take_attr_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "94",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "!",
                    line: 1,
                    col: 3,
                    offset: 2
                }
            );
        }

        #[test]
        fn contains_hyphens() {
            let span = crate::Span::new("blah-blah-94 = foo");
            let mi = span.take_attr_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "blah-blah-94",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: " = foo",
                    line: 1,
                    col: 13,
                    offset: 12
                }
            );
        }

        #[test]
        fn stops_at_eof() {
            let span = crate::Span::new("xyz");
            let mi = span.take_attr_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "xyz",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "",
                    line: 1,
                    col: 4,
                    offset: 3
                }
            );
        }

        #[test]
        fn starts_with_underscore() {
            // A leading underscore is a word character, so it may begin an
            // attribute name (Asciidoctor's `NameRx` accepts `_foo`).
            let span = crate::Span::new("_foo = bar");
            let mi = span.take_attr_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "_foo",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: " = bar",
                    line: 1,
                    col: 5,
                    offset: 4
                }
            );
        }

        #[test]
        fn contains_dots_and_underscores() {
            // Dots and (non-leading) underscores are valid continuation
            // characters, so `foo.foo` and `foo_foo` are single name tokens.
            let span = crate::Span::new("foo.foo_bar = baz");
            let mi = span.take_attr_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "foo.foo_bar",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: " = baz",
                    line: 1,
                    col: 12,
                    offset: 11
                }
            );
        }
    }

    mod take_user_attr_name {
        use crate::tests::prelude::*;

        #[test]
        fn empty_source() {
            let span = crate::Span::default();
            assert!(span.take_user_attr_name().is_none());
        }

        #[test]
        fn starts_with_non_word() {
            let span = crate::Span::new("#not-a-proper-name");
            assert!(span.take_user_attr_name().is_none());
        }

        #[test]
        fn starts_with_hyphen() {
            let span = crate::Span::new("-not-a-proper-name");
            assert!(span.take_user_attr_name().is_none());
        }

        #[test]
        fn stops_at_colon() {
            let span = crate::Span::new("foo: bar");
            let mi = span.take_user_attr_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "foo",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: ": bar",
                    line: 1,
                    col: 4,
                    offset: 3
                }
            );
        }

        #[test]
        fn captures_non_word_chars() {
            // The raw name runs up to the closing colon and may contain spaces
            // and other characters that are not valid in a canonical attribute
            // name; sanitization happens later (see `remap_attr_name`).
            let span = crate::Span::new("Foo 3^ # - Bar[: x");
            let mi = span.take_user_attr_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "Foo 3^ # - Bar[",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: ": x",
                    line: 1,
                    col: 16,
                    offset: 15
                }
            );
        }

        #[test]
        fn numeric() {
            let span = crate::Span::new("94: x");
            let mi = span.take_user_attr_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "94",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: ": x",
                    line: 1,
                    col: 3,
                    offset: 2
                }
            );
        }

        #[test]
        fn unicode_word_chars() {
            // A name may begin with (and contain) any Unicode word character,
            // not just ASCII, so `:café:` is recognized as an attribute entry.
            let span = crate::Span::new("café: x");
            let mi = span.take_user_attr_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "café",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: ": x",
                    line: 1,
                    col: 5,
                    offset: 5
                }
            );
        }

        #[test]
        fn contains_hyphens() {
            let span = crate::Span::new("blah-blah-94: foo");
            let mi = span.take_user_attr_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "blah-blah-94",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: ": foo",
                    line: 1,
                    col: 13,
                    offset: 12
                }
            );
        }

        #[test]
        fn stops_at_eof() {
            let span = crate::Span::new("xyz");
            let mi = span.take_user_attr_name().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "xyz",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "",
                    line: 1,
                    col: 4,
                    offset: 3
                }
            );
        }
    }

    #[test]
    fn is_xml_name() {
        assert!(!crate::Span::default().is_xml_name());
        assert!(crate::Span::new("a").is_xml_name());
        assert!(crate::Span::new("a9").is_xml_name());

        assert!(crate::Span::new("install").is_xml_name());
        assert!(crate::Span::new("data-structures").is_xml_name());
        assert!(crate::Span::new("error-handling").is_xml_name());
        assert!(crate::Span::new("subject-and-body").is_xml_name());
        assert!(crate::Span::new("unset_an_attribute").is_xml_name());
        assert!(crate::Span::new(":a").is_xml_name());
        assert!(crate::Span::new("_a").is_xml_name());

        assert!(!crate::Span::new("install the gem").is_xml_name());
        assert!(!crate::Span::new("3 blind mice").is_xml_name());
        assert!(!crate::Span::new("-about-the-author").is_xml_name());
        assert!(!crate::Span::new("\u{037e}abc").is_xml_name());
        assert!(!crate::Span::new("ab\u{037e}c").is_xml_name());
    }

    mod take_quoted_string {
        use crate::tests::prelude::*;

        #[test]
        fn empty_source() {
            let span = crate::Span::default();
            assert!(span.take_quoted_string().is_none());
        }

        #[test]
        fn unterminated_double_quote() {
            let span = crate::Span::new("\"xxx");
            assert!(span.take_quoted_string().is_none());
        }

        #[test]
        fn double_quoted_string() {
            let span = crate::Span::new("\"abc\"def");
            let mi = span.take_quoted_string().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "abc",
                    line: 1,
                    col: 2,
                    offset: 1
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "def",
                    line: 1,
                    col: 6,
                    offset: 5
                }
            );
        }

        #[test]
        fn double_quoted_with_escape() {
            let span = crate::Span::new("\"a\\\"bc\"def");
            let mi = span.take_quoted_string().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "a\\\"bc",
                    line: 1,
                    col: 2,
                    offset: 1
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "def",
                    line: 1,
                    col: 8,
                    offset: 7
                }
            );
        }

        #[test]
        fn double_quoted_with_single_quote() {
            let span = crate::Span::new("\"a'bc\"def");
            let mi = span.take_quoted_string().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "a'bc",
                    line: 1,
                    col: 2,
                    offset: 1
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "def",
                    line: 1,
                    col: 7,
                    offset: 6
                }
            );
        }

        #[test]
        fn unterminated_single_quote() {
            let span = crate::Span::new("'xxx");
            assert!(span.take_quoted_string().is_none());
        }

        #[test]
        fn single_quoted_string() {
            let span = crate::Span::new("'abc'def");
            let mi = span.take_quoted_string().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "abc",
                    line: 1,
                    col: 2,
                    offset: 1
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "def",
                    line: 1,
                    col: 6,
                    offset: 5
                }
            );
        }

        #[test]
        fn single_quoted_with_escape() {
            let span = crate::Span::new("'a\\'bc'def");
            let mi = span.take_quoted_string().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "a\\'bc",
                    line: 1,
                    col: 2,
                    offset: 1
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "def",
                    line: 1,
                    col: 8,
                    offset: 7
                }
            );
        }

        #[test]
        fn single_quoted_with_double_quote() {
            let span = crate::Span::new("'a\"bc'def");
            let mi = span.take_quoted_string().unwrap();

            assert_eq!(
                mi.item,
                Span {
                    data: "a\"bc",
                    line: 1,
                    col: 2,
                    offset: 1
                }
            );

            assert_eq!(
                mi.after,
                Span {
                    data: "def",
                    line: 1,
                    col: 7,
                    offset: 6
                }
            );
        }
    }

    mod trim_remainder {
        use crate::tests::prelude::*;

        fn advanced_span(source: &'static str, skip: usize) -> crate::Span<'static> {
            let span = crate::Span::new(source);
            span.slice_from(skip..)
        }

        #[test]
        fn empty_spans() {
            let source = advanced_span("abcdef", 6);
            let after = crate::Span::default();

            assert_eq!(
                source.trim_remainder(after),
                Span {
                    data: "",
                    line: 1,
                    col: 7,
                    offset: 6
                }
            );
        }

        #[test]
        fn rem_equals_source() {
            let source = advanced_span("abcdef", 6);
            let after = crate::Span::new("abcdef");

            assert_eq!(
                source.trim_remainder(after),
                Span {
                    data: "",
                    line: 1,
                    col: 7,
                    offset: 6
                }
            );
        }

        #[test]
        fn rem_too_long() {
            // This is nonsense input, but we should at least not panic in this case.

            let source = advanced_span("abcdef", 6);
            let after = crate::Span::new("abcdef_bogus_bogus");

            assert_eq!(
                source.trim_remainder(after),
                Span {
                    data: "",
                    line: 1,
                    col: 7,
                    offset: 6
                }
            );
        }

        #[test]
        fn rem_is_subset_of_source() {
            let source = advanced_span("abcdef", 2);
            let after = advanced_span("abcdef", 4);

            assert_eq!(
                source.trim_remainder(after),
                Span {
                    data: "cd",
                    line: 1,
                    col: 3,
                    offset: 2
                }
            );
        }

        #[test]
        fn rem_is_incomplete_subset_of_source() {
            let source = crate::Span::new("abc\ndef\n");
            let line1 = source.take_normalized_line();
            let line2 = line1.after.take_line();

            assert_eq!(
                source.trim_remainder(line2.item),
                Span {
                    data: "abc\n",
                    line: 1,
                    col: 1,
                    offset: 0
                }
            );
        }
    }
}