crcbl-ui 0.2.0

Crucible immediate-mode GUI
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
//! Selectors: which nodes a rule applies to.
//!
//! The grammar is UI section 3's (`docs/notes/tooling.md`), and no more:
//!
//! ```text
//! selector-list  = selector ("," selector)*
//! selector       = compound ((" " | ">") compound)*
//! compound       = (type | "*")? ("#" id | "." class | ":" pseudo-class)*
//! pseudo-class   = hover | active | focus | disabled | engaged | checked | open
//!                | refused
//! ```
//!
//! A type is `block`, `span` or a widget name a node declares; type names and
//! pseudo-classes are ASCII-case-insensitive, ids and classes are not, as in
//! CSS. Sibling combinators, attribute selectors, pseudo-elements and every
//! other pseudo-class are refused — the whole rule is dropped with an error
//! naming what was not understood, rather than a selector quietly matching
//! something narrower or wider than it says.
//!
//! # Specificity
//!
//! Simplified, and predictable over spec-faithful: a selector's [`Specificity`]
//! is the highest tier of anything in it — an id anywhere makes it an id
//! selector, else a class or pseudo-class makes it a class selector, else it is
//! a type selector. Two selectors of one tier do not count components against
//! each other; the later rule wins.
//!
//! # Matching
//!
//! Right to left, as browsers and RmlUi match: the rightmost compound against
//! the node, then each combinator walks up. Every pseudo-class matches against
//! the [`PseudoClasses`] the tree resolved for the node; see
//! [`crate::tree::focus`] for when `:focus`, `:engaged` and `:disabled` are
//! set, and [`crate::tree::widgets`] for `:checked` and `:open`.

use core::ops::{BitAnd, BitOr};

use cssparser::{ParseError, Parser, Token};

/// A set of pseudo-classes, as bits: a node's state, or what a selector
/// tests.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct PseudoClasses(u8);

impl PseudoClasses {
    /// The empty set.
    pub const NONE: Self = Self(0);
    /// `:hover`: the pointer is over the node or something inside it, while
    /// the pointer is the device driving.
    pub const HOVER: Self = Self(1);
    /// `:active`: the node holds the pointer's press.
    pub const ACTIVE: Self = Self(1 << 1);
    /// `:focus`: the node holds the focus, while the pad or the keyboard is
    /// the device driving.
    pub const FOCUS: Self = Self(1 << 2);
    /// `:disabled`: the node's behavior is disabled.
    pub const DISABLED: Self = Self(1 << 3);
    /// `:engaged`: the focused widget is taking the navigation input.
    pub const ENGAGED: Self = Self(1 << 4);
    /// `:checked`: a checkbox whose value is on — Selectors Level 4's input
    /// value state.
    pub const CHECKED: Self = Self(1 << 5);
    /// `:open`: a collapsing header or a tree node showing its children —
    /// Selectors Level 4's collapse state.
    pub const OPEN: Self = Self(1 << 6);
    /// `:refused`: a text input whose last clipboard offer or read the
    /// platform refused, until its next edit or until it is no longer engaged.
    /// This crate's own, like `:engaged`.
    pub const REFUSED: Self = Self(1 << 7);

    /// The pseudo-class `name` spells, ignoring ASCII case.
    #[must_use]
    pub fn from_name(name: &str) -> Option<Self> {
        Some(cssparser::match_ignore_ascii_case! { name,
            "hover" => Self::HOVER,
            "active" => Self::ACTIVE,
            "focus" => Self::FOCUS,
            "disabled" => Self::DISABLED,
            "engaged" => Self::ENGAGED,
            "checked" => Self::CHECKED,
            "open" => Self::OPEN,
            "refused" => Self::REFUSED,
            _ => return None,
        })
    }

    /// Whether every class in `other` is in `self`.
    #[must_use]
    pub const fn contains(self, other: Self) -> bool {
        self.0 & other.0 == other.0
    }

    /// Whether the set is empty.
    #[must_use]
    pub const fn is_empty(self) -> bool {
        self.0 == 0
    }
}

impl BitOr for PseudoClasses {
    type Output = Self;

    fn bitor(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }
}

impl BitAnd for PseudoClasses {
    type Output = Self;

    fn bitand(self, other: Self) -> Self {
        Self(self.0 & other.0)
    }
}

/// A selector's tier in the cascade; see the module docs. Ordered, so a higher
/// tier compares greater.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Specificity {
    /// Types and `*` only.
    Type,
    /// A class or a pseudo-class somewhere in it.
    Class,
    /// An id somewhere in it.
    Id,
}

/// One compound selector: everything between two combinators.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct Compound {
    /// Lowercased; `None` for `*` or no type at all.
    pub type_name: Option<Box<str>>,
    pub id: Option<Box<str>>,
    pub classes: Vec<Box<str>>,
    pub pseudo: PseudoClasses,
}

/// How a compound relates to the one on its right.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Combinator {
    /// Whitespace: any ancestor.
    Descendant,
    /// `>`: the parent.
    Child,
}

/// A parsed selector, stored right to left.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Selector {
    /// The rightmost compound, which the node itself must match.
    pub(crate) subject: Compound,
    /// Every compound to its left, nearest first, each with the combinator
    /// that joins it to the compound on its right.
    pub(crate) ancestors: Vec<(Combinator, Compound)>,
}

/// What the rule index files a selector under: its subject's id, else its
/// first class, else its type, else nothing.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Bucket<'a> {
    Id(&'a str),
    Class(&'a str),
    Type(&'a str),
    Universal,
}

/// The view of a node that matching reads. The tree implements it over its
/// frame; the tests implement it over a table.
pub(crate) trait Element: Sized {
    /// `block`, `span` or a widget name, as the node declared it.
    fn type_name(&self) -> &str;
    fn id(&self) -> Option<&str>;
    fn has_class(&self, class: &str) -> bool;
    fn pseudo(&self) -> PseudoClasses;
    fn parent(&self) -> Option<Self>;
}

impl Compound {
    fn matches(&self, element: &impl Element) -> bool {
        self.type_name
            .as_deref()
            .is_none_or(|name| element.type_name().eq_ignore_ascii_case(name))
            && self.id.as_deref().is_none_or(|id| element.id() == Some(id))
            && self.classes.iter().all(|class| element.has_class(class))
            && element.pseudo().contains(self.pseudo)
    }

    const fn is_empty(&self) -> bool {
        self.type_name.is_none()
            && self.id.is_none()
            && self.classes.is_empty()
            && self.pseudo.is_empty()
    }
}

impl Selector {
    /// Parses one selector list — the part of a rule before its `{` — from
    /// `css`, for a caller outside a stylesheet.
    ///
    /// # Errors
    ///
    /// A message naming what was not understood.
    pub fn parse_list(css: &str) -> Result<Vec<Self>, String> {
        let mut parser = Parser::new(css);
        parse_selector_list(&mut parser).map_err(|error| match error.kind {
            cssparser::ParseErrorKind::Custom(message) => message,
            cssparser::ParseErrorKind::Basic(kind) => kind.to_string(),
        })
    }

    /// The tier this selector cascades in.
    #[must_use]
    pub fn specificity(&self) -> Specificity {
        let compounds =
            || core::iter::once(&self.subject).chain(self.ancestors.iter().map(|(_, c)| c));
        if compounds().any(|compound| compound.id.is_some()) {
            Specificity::Id
        } else if compounds()
            .any(|compound| !compound.classes.is_empty() || !compound.pseudo.is_empty())
        {
            Specificity::Class
        } else {
            Specificity::Type
        }
    }

    /// The pseudo-classes the node itself is tested for.
    pub(crate) fn subject_pseudo(&self) -> PseudoClasses {
        self.subject.pseudo
    }

    /// The pseudo-classes some ancestor is tested for.
    pub(crate) fn ancestor_pseudo(&self) -> PseudoClasses {
        self.ancestors
            .iter()
            .fold(PseudoClasses::NONE, |all, (_, compound)| {
                all | compound.pseudo
            })
    }

    /// Whether matching this selector reads anything above the node.
    pub(crate) fn has_ancestors(&self) -> bool {
        !self.ancestors.is_empty()
    }

    pub(crate) fn bucket(&self) -> Bucket<'_> {
        let subject = &self.subject;
        if let Some(id) = subject.id.as_deref() {
            Bucket::Id(id)
        } else if let Some(class) = subject.classes.first() {
            Bucket::Class(class)
        } else if let Some(name) = subject.type_name.as_deref() {
            Bucket::Type(name)
        } else {
            Bucket::Universal
        }
    }

    /// Whether `element` matches, right to left.
    pub(crate) fn matches(&self, element: &impl Element) -> bool {
        self.subject.matches(element) && self.ancestors_match(0, element)
    }

    /// Whether `ancestors[from..]` match above `element`, trying every
    /// ancestor a descendant combinator could mean before giving up.
    fn ancestors_match(&self, from: usize, element: &impl Element) -> bool {
        let Some((combinator, compound)) = self.ancestors.get(from) else {
            return true;
        };
        match combinator {
            Combinator::Child => element.parent().is_some_and(|parent| {
                compound.matches(&parent) && self.ancestors_match(from + 1, &parent)
            }),
            Combinator::Descendant => {
                let mut next = element.parent();
                while let Some(ancestor) = next {
                    if compound.matches(&ancestor) && self.ancestors_match(from + 1, &ancestor) {
                        return true;
                    }
                    next = ancestor.parent();
                }
                false
            }
        }
    }
}

/// The error a selector refuses with: a message for the diagnostic.
pub(crate) type SelectorError = ParseError<String>;

fn refuse<T>(message: impl Into<String>) -> Result<T, SelectorError> {
    Err(ParseError::custom(message.into()))
}

/// A comma-separated list of selectors, which must use the whole of `input`.
pub(crate) fn parse_selector_list<'i>(
    input: &mut Parser<'i>,
) -> Result<Vec<Selector>, SelectorError> {
    input.parse_comma_separated(parse_selector)
}

fn parse_selector<'i>(input: &mut Parser<'i>) -> Result<Selector, SelectorError> {
    // Left to right as written, reversed at the end.
    let mut compounds = vec![parse_compound(input)?];
    let mut combinators = Vec::new();
    loop {
        let mut spaced = false;
        loop {
            let before = input.state();
            match input.next_including_whitespace() {
                Ok(Token::WhiteSpace(_)) => spaced = true,
                Ok(_) => {
                    input.reset(&before);
                    break;
                }
                Err(_) => break,
            }
        }
        let before = input.state();
        let combinator = match input.next_including_whitespace() {
            Err(_) => break,
            Ok(Token::Delim('>')) => {
                input.skip_whitespace();
                Combinator::Child
            }
            Ok(Token::Delim(delim @ ('+' | '~'))) => {
                return refuse(format!("the `{delim}` sibling combinator is not supported"));
            }
            Ok(_) if spaced => {
                input.reset(&before);
                Combinator::Descendant
            }
            Ok(token) => {
                let token = token.clone();
                return refuse(format!("unexpected {token:?} in a selector"));
            }
        };
        combinators.push(combinator);
        compounds.push(parse_compound(input)?);
    }

    let subject = compounds
        .pop()
        .expect("a selector has at least one compound");
    let ancestors = combinators
        .into_iter()
        .rev()
        .zip(compounds.into_iter().rev())
        .collect();
    Ok(Selector { subject, ancestors })
}

fn parse_compound<'i>(input: &mut Parser<'i>) -> Result<Compound, SelectorError> {
    let mut compound = Compound::default();
    let mut universal = false;
    let mut first = true;
    loop {
        let before = input.state();
        let Ok(token) = input.next_including_whitespace() else {
            break;
        };
        match token.clone() {
            Token::Ident(name) if first => {
                compound.type_name = Some(name.to_ascii_lowercase().into_boxed_str());
            }
            Token::Delim('*') if first => universal = true,
            Token::IDHash(id) => {
                if compound.id.is_some() {
                    return refuse(format!(
                        "a compound selector names two ids, the second `#{id}`"
                    ));
                }
                compound.id = Some(Box::from(&*id));
            }
            Token::Hash(hash) => return refuse(format!("`#{hash}` is not a valid id")),
            Token::Delim('.') => match input.next_including_whitespace() {
                Ok(Token::Ident(class)) => compound.classes.push(Box::from(&**class)),
                _ => return refuse("`.` is not followed by a class name"),
            },
            Token::Colon => match input.next_including_whitespace() {
                Ok(Token::Ident(name)) => match PseudoClasses::from_name(name) {
                    Some(pseudo) => compound.pseudo = compound.pseudo | pseudo,
                    None => return refuse(format!("the pseudo-class `:{name}` is not supported")),
                },
                Ok(Token::Colon) => return refuse("pseudo-elements are not supported"),
                Ok(Token::Function(name)) => {
                    return refuse(format!("the pseudo-class `:{name}()` is not supported"));
                }
                _ => return refuse("`:` is not followed by a pseudo-class"),
            },
            Token::SquareBracketBlock => return refuse("attribute selectors are not supported"),
            _ => {
                input.reset(&before);
                break;
            }
        }
        first = false;
    }
    if compound.is_empty() && !universal {
        let found = input.next().cloned();
        return match found {
            Ok(token) => refuse(format!("expected a selector, found {token:?}")),
            Err(_) => refuse("expected a selector"),
        };
    }
    Ok(compound)
}

/// A node's own selector as a builder took it — `type#id.class.class`, every
/// part optional — split into its parts without allocating.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct NodeSelector<'a> {
    pub type_name: Option<&'a str>,
    pub id: Option<&'a str>,
    /// Everything after the type: each id and class with its marker.
    tail: &'a str,
    text: &'a str,
}

impl<'a> NodeSelector<'a> {
    /// No type, id or class.
    pub const EMPTY: Self = Self {
        type_name: None,
        id: None,
        tail: "",
        text: "",
    };

    /// The selector as written.
    pub const fn text(self) -> &'a str {
        self.text
    }

    /// Splits `text`, or refuses it when a part is empty, two ids are named, or
    /// a name holds a character outside `[A-Za-z0-9_-]` and non-ASCII.
    pub fn parse(text: &'a str) -> Result<Self, ()> {
        let is_name = |name: &str| {
            !name.is_empty()
                && name.chars().all(|ch| {
                    ch.is_ascii_alphanumeric() || ch == '-' || ch == '_' || !ch.is_ascii()
                })
        };
        let (head, tail) = text.split_at(text.find(['#', '.']).unwrap_or(text.len()));
        let type_name = match head {
            "" => None,
            head if is_name(head) => Some(head),
            _ => return Err(()),
        };
        let mut id = None;
        for (marker, name) in segments(tail) {
            if !is_name(name) || marker == b'#' && id.replace(name).is_some() {
                return Err(());
            }
        }
        Ok(Self {
            type_name,
            id,
            tail,
            text,
        })
    }

    /// Every class the node declares.
    pub fn classes(self) -> impl Iterator<Item = &'a str> {
        segments(self.tail).filter_map(|(marker, name)| (marker == b'.').then_some(name))
    }
}

/// `#a.b.c` as `(b'#', "a")`, `(b'.', "b")`, `(b'.', "c")`.
fn segments(tail: &str) -> impl Iterator<Item = (u8, &str)> {
    let mut rest = tail;
    core::iter::from_fn(move || {
        let marker = *rest.as_bytes().first()?;
        let body = &rest[1..];
        let end = body.find(['#', '.']).unwrap_or(body.len());
        rest = &body[end..];
        Some((marker, &body[..end]))
    })
}

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

    /// A node in a table: its selector text and its parent's row.
    struct Row {
        kind: &'static str,
        selector: &'static str,
        pseudo: PseudoClasses,
        parent: Option<usize>,
    }

    #[derive(Clone, Copy)]
    struct TableElement<'a> {
        rows: &'a [Row],
        at: usize,
    }

    impl Element for TableElement<'_> {
        fn type_name(&self) -> &str {
            let row = &self.rows[self.at];
            NodeSelector::parse(row.selector)
                .expect("valid")
                .type_name
                .unwrap_or(row.kind)
        }
        fn id(&self) -> Option<&str> {
            NodeSelector::parse(self.rows[self.at].selector)
                .expect("valid")
                .id
        }
        fn has_class(&self, class: &str) -> bool {
            NodeSelector::parse(self.rows[self.at].selector)
                .expect("valid")
                .classes()
                .any(|own| own == class)
        }
        fn pseudo(&self) -> PseudoClasses {
            self.rows[self.at].pseudo
        }
        fn parent(&self) -> Option<Self> {
            self.rows[self.at].parent.map(|at| Self {
                rows: self.rows,
                at,
            })
        }
    }

    fn one(css: &str) -> Selector {
        let mut list = Selector::parse_list(css).unwrap_or_else(|why| panic!("{css}: {why}"));
        assert_eq!(list.len(), 1, "{css}");
        list.remove(0)
    }

    /// `panel > row.selected:hover`'s shape, with a `#stats` block above.
    const TREE: &[Row] = &[
        Row {
            kind: "block",
            selector: "#stats.panel",
            pseudo: PseudoClasses::NONE,
            parent: None,
        },
        Row {
            kind: "block",
            selector: "list.rows",
            pseudo: PseudoClasses(PseudoClasses::HOVER.0 | PseudoClasses::OPEN.0),
            parent: Some(0),
        },
        Row {
            kind: "block",
            selector: ".row.selected",
            pseudo: PseudoClasses::HOVER,
            parent: Some(1),
        },
        Row {
            kind: "span",
            selector: ".label",
            pseudo: PseudoClasses::CHECKED,
            parent: Some(2),
        },
        Row {
            kind: "span",
            selector: "#value",
            pseudo: PseudoClasses::ACTIVE,
            parent: Some(2),
        },
    ];

    /// **Every selector form matches exactly the nodes it names**, across
    /// types, ids, classes, pseudo-classes and both combinators — a table of
    /// selectors against the five nodes above, each row the set it must match.
    #[test]
    fn each_selector_matches_exactly_the_nodes_it_names() {
        let cases: &[(&str, &[usize])] = &[
            // A declared type replaces `block`: `list.rows` is not a block.
            ("block", &[0, 2]),
            ("span", &[3, 4]),
            ("LIST", &[1]),
            ("*", &[0, 1, 2, 3, 4]),
            ("#stats", &[0]),
            ("block#stats.panel", &[0]),
            ("span#stats", &[]),
            (".row", &[2]),
            (".row.selected", &[2]),
            (".row.missing", &[]),
            (":hover", &[1, 2]),
            (".row:hover", &[2]),
            (".label:hover", &[]),
            ("span:active", &[4]),
            (":focus", &[]),
            (":disabled", &[]),
            (":engaged", &[]),
            (":checked", &[3]),
            ("span:checked", &[3]),
            (".row:checked", &[]),
            (":open", &[1]),
            ("list:open > .row", &[2]),
            (":open:checked", &[]),
            ("#stats span", &[3, 4]),
            ("#stats > span", &[]),
            (".row > span", &[3, 4]),
            ("list > .row > #value", &[4]),
            (".panel .rows .selected .label", &[3]),
            // The descendant combinator must try every ancestor, not just the
            // nearest one that matches the compound.
            (".panel block span", &[3, 4]),
            // `*` first meets `.row`, whose parent is not `.panel`; only the
            // list above it is.
            (".panel > * span", &[3, 4]),
            (".panel > * > * > span", &[3, 4]),
            (".panel > block span", &[]),
            ("list:hover .label", &[3]),
            (".panel:hover span", &[]),
        ];
        for (css, want) in cases {
            let selector = one(css);
            let got: Vec<usize> = (0..TREE.len())
                .filter(|&at| selector.matches(&TableElement { rows: TREE, at }))
                .collect();
            assert_eq!(&got, want, "`{css}`");
        }
    }

    /// **Specificity is the tier of the strongest part**: an id anywhere beats
    /// any count of classes, and a pseudo-class is a class.
    #[test]
    fn specificity_is_the_tier_of_the_strongest_component() {
        let cases = [
            ("block", Specificity::Type),
            ("block span", Specificity::Type),
            ("*", Specificity::Type),
            (".a", Specificity::Class),
            (".a.b.c.d", Specificity::Class),
            (":hover", Specificity::Class),
            ("block > span:active", Specificity::Class),
            ("#x", Specificity::Id),
            ("#x span", Specificity::Id),
            ("block .a #x", Specificity::Id),
        ];
        for (css, want) in cases {
            assert_eq!(one(css).specificity(), want, "`{css}`");
        }
        assert!(Specificity::Id > Specificity::Class && Specificity::Class > Specificity::Type);
    }

    /// **A list is one selector per comma, whitespace and comments around
    /// the parts are ignored, and an escape is decoded** by the tokenizer.
    #[test]
    fn lists_whitespace_comments_and_escapes_parse() {
        let list = Selector::parse_list(" .a ,/* note */#b>span , c  d ").expect("parses");
        assert_eq!(list.len(), 3);
        assert_eq!(list[1].subject.type_name.as_deref(), Some("span"));
        assert_eq!(list[1].ancestors[0].0, Combinator::Child);
        assert_eq!(list[2].ancestors[0].0, Combinator::Descendant);

        let escaped = one(r".a\:b");
        assert_eq!(escaped.subject.classes, [Box::from("a:b")]);
        let escaped = one(r"#\31 23");
        assert_eq!(escaped.subject.id.as_deref(), Some("123"));
    }

    /// **Everything outside the grammar is refused with a message saying
    /// what**, never parsed into a selector that means something else.
    #[test]
    fn everything_outside_the_grammar_is_refused_by_name() {
        let cases = [
            ("a + b", "sibling"),
            ("a ~ b", "sibling"),
            (":nth-child(2)", "not supported"),
            (":visited", "`:visited`"),
            ("::before", "pseudo-elements"),
            ("[href]", "attribute"),
            ("#1a", "not a valid id"),
            ("#a#b", "two ids"),
            (". a", "class name"),
            ("a,", "expected a selector"),
            ("a >", "expected a selector"),
            ("> a", "expected a selector"),
            ("", "expected a selector"),
        ];
        for (css, message) in cases {
            let error = Selector::parse_list(css).expect_err(css);
            assert!(
                error.contains(message),
                "`{css}` refused with {error:?}, not {message:?}"
            );
        }
    }

    /// **A rule depends on the pseudo-classes it names, split by where**:
    /// what the node is tested for, and what an ancestor is.
    #[test]
    fn pseudo_dependencies_are_split_between_the_node_and_its_ancestors() {
        let selector = one(".list:hover > .row:active:focus");
        assert_eq!(
            selector.subject_pseudo(),
            PseudoClasses::ACTIVE | PseudoClasses::FOCUS
        );
        assert_eq!(selector.ancestor_pseudo(), PseudoClasses::HOVER);
        assert!(selector.has_ancestors());
        assert!(!one(".row").has_ancestors());
        assert!(one(".row").subject_pseudo().is_empty());
    }

    /// The bucket is the subject's id, else its first class, else its type.
    #[test]
    fn a_selector_is_bucketed_by_its_subjects_strongest_part() {
        assert_eq!(one("block .a #x.b").bucket(), Bucket::Id("x"));
        assert_eq!(one("#x .b.c").bucket(), Bucket::Class("b"));
        assert_eq!(one(".a SPAN").bucket(), Bucket::Type("span"));
        assert_eq!(one(".a :hover").bucket(), Bucket::Universal);
    }

    /// **A builder's selector splits into type, id and classes**, and a
    /// malformed one is refused rather than guessed at.
    #[test]
    fn a_node_selector_splits_into_its_parts() {
        let parsed = NodeSelector::parse("button#ok.primary.wide").expect("valid");
        assert_eq!(parsed.type_name, Some("button"));
        assert_eq!(parsed.id, Some("ok"));
        assert_eq!(parsed.classes().collect::<Vec<_>>(), ["primary", "wide"]);

        let parsed = NodeSelector::parse(".a#id.b").expect("valid");
        assert_eq!((parsed.type_name, parsed.id), (None, Some("id")));
        assert_eq!(parsed.classes().collect::<Vec<_>>(), ["a", "b"]);

        let empty = NodeSelector::parse("").expect("valid");
        assert_eq!(
            (empty.type_name, empty.id, empty.classes().count()),
            (None, None, 0)
        );

        for bad in [
            "#", ".", "a b", "#a#b", ".a#b#c", "a:hover", "..a", "#a.", "a>b",
        ] {
            assert!(NodeSelector::parse(bad).is_err(), "`{bad}` was accepted");
        }
    }
}