next-rust-view 0.1.11

The Next Rust view layer: an escaping-by-default HTML DSL, components, metadata and a streaming renderer.
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
//! Per-page CSS.
//!
//! An app-wide stylesheet (the Tailwind build, with the hand-written CSS
//! imported into it, or the UI components' styles) holds every rule the
//! whole app uses. A page needs only the rules for the classes it renders,
//! so the renderer splits the stylesheet into pieces once and sends, per
//! document, the pieces that page needs:
//!
//! * a rule whose selectors all start with a class is sent when one of
//!   those classes is on the page, wherever it is in the sheet: Tailwind's
//!   utilities, a component's `@layer components`, or a plain `.card{…}`
//!   from an imported stylesheet;
//! * everything else (layer order, theme variables, the base reset,
//!   element and attribute selectors, `@font-face`) is always sent, once;
//! * `@property` registrations, their `@layer properties` fallbacks and
//!   `@keyframes` are sent once something sent refers to them.
//!
//! Each `<style>` gets the id `<sheet id>~<bitset of pieces>`, so the client
//! runtime can report what it already has and later navigations and
//! streamed chunks send only what is missing. Utilities depend on their
//! order (`px-2` must follow `p-4`), so when new rules are sent, already-sent
//! rules that come after them are sent again to keep the order intact.

use std::collections::{HashMap, HashSet};
use std::sync::{Mutex, OnceLock};

use crate::style::Stylesheet;

/// One piece of a split stylesheet.
#[derive(Debug)]
enum Item {
    /// Outside the utilities layer: always sent.
    Text { index: usize, css: String },
    /// A utility rule, sent when one of `owners` is used (always when empty).
    /// `props`: the property families it sets (see [`families`]).
    Rule { index: usize, owners: Vec<String>, css: String, props: Vec<String> },
    /// `@property --x{…}`, a `--x:…;` fallback or `@keyframes x{…}`: sent
    /// when a sent piece refers to `name`.
    Var { index: usize, name: String, css: String },
    /// `@layer utilities{…}` or an at-rule inside it: written only around
    /// pieces that are sent.
    Group { open: String, items: Vec<Item> },
}

#[derive(Debug)]
pub(crate) struct Split {
    items: Vec<Item>,
    len: usize,
    /// For each `Var` piece, the pieces that refer to it.
    refs: Vec<(usize, Vec<usize>)>,
}

/// Pieces of a split stylesheet, as a bit set.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Pieces(Vec<u8>);

impl Pieces {
    fn with_len(len: usize) -> Self {
        Pieces(vec![0; len.div_ceil(8)])
    }

    fn insert(&mut self, i: usize) {
        if let Some(b) = self.0.get_mut(i / 8) {
            *b |= 1 << (i % 8);
        }
    }

    fn contains(&self, i: usize) -> bool {
        self.0.get(i / 8).is_some_and(|b| b & (1 << (i % 8)) != 0)
    }

    fn union(&mut self, other: &Pieces) {
        for (a, b) in self.0.iter_mut().zip(&other.0) {
            *a |= b;
        }
    }

    fn is_empty(&self) -> bool {
        self.0.iter().all(|b| *b == 0)
    }

    fn encode(&self) -> String {
        const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
        let mut bytes = self.0.as_slice();
        while let [rest @ .., 0] = bytes {
            bytes = rest;
        }
        let mut out = String::with_capacity(bytes.len() * 4 / 3 + 2);
        for chunk in bytes.chunks(3) {
            let n = chunk.iter().enumerate().fold(0u32, |acc, (i, &b)| acc | (u32::from(b) << (16 - 8 * i)));
            for i in 0..=chunk.len() {
                out.push(B64[((n >> (18 - 6 * i)) & 63) as usize] as char);
            }
        }
        out
    }

    fn decode(s: &str, len: usize) -> Option<Pieces> {
        let mut bytes = Vec::with_capacity(s.len() * 3 / 4);
        for chunk in s.as_bytes().chunks(4) {
            if chunk.len() == 1 {
                return None;
            }
            let mut n = 0u32;
            for (i, &c) in chunk.iter().enumerate() {
                let v = match c {
                    b'A'..=b'Z' => c - b'A',
                    b'a'..=b'z' => c - b'a' + 26,
                    b'0'..=b'9' => c - b'0' + 52,
                    b'-' => 62,
                    b'_' => 63,
                    _ => return None,
                };
                n |= u32::from(v) << (18 - 6 * i);
            }
            for i in 0..chunk.len() - 1 {
                bytes.push((n >> (16 - 8 * i)) as u8);
            }
        }
        let mut pieces = Pieces::with_len(len);
        for (a, b) in pieces.0.iter_mut().zip(bytes) {
            *a = b;
        }
        Some(pieces)
    }
}

/// Split form of `sheet`, parsed on first use.
pub(crate) fn split(sheet: &'static Stylesheet) -> &'static Split {
    let mut cache = SPLITS.get_or_init(Default::default).lock().unwrap_or_else(|e| e.into_inner());
    // Keyed by address: stylesheets are statics, and ids may repeat in tests.
    cache
        .entry(sheet as *const Stylesheet as usize)
        .or_insert_with(|| (sheet, Box::leak(Box::new(Split::parse(sheet.css)))))
        .1
}

type SplitCache = Mutex<HashMap<usize, (&'static Stylesheet, &'static Split)>>;

/// Every per-class stylesheet rendered so far, split.
static SPLITS: OnceLock<SplitCache> = OnceLock::new();

impl Split {
    fn parse(css: &str) -> Split {
        let mut len = 0;
        let items = parse_rules(css, &mut len, true);
        let refs = references(&items);
        Split { items, len, refs }
    }

    /// The bit set encoded in a known `<style>` id for `sheet`, if it is one.
    pub(crate) fn known(&self, sheet: &Stylesheet, ids: &HashSet<String>) -> Pieces {
        let mut known = Pieces::with_len(self.len);
        let prefix = format!("{}~", sheet.id);
        for id in ids {
            if id == sheet.id {
                // The whole sheet (an older page, or pruning was off).
                (0..self.len).for_each(|i| known.insert(i));
            } else if let Some(bits) = id.strip_prefix(&prefix).and_then(|b| Pieces::decode(b, self.len)) {
                known.union(&bits);
            }
        }
        known
    }

    /// Pieces needed for `used` classes that are not in `sent`, plus sent
    /// rules that must be repeated to keep the order.
    pub(crate) fn delta(&self, used: &dyn Fn(&str) -> bool, sent: &Pieces) -> Pieces {
        let mut wanted = Pieces::with_len(self.len);
        visit(&self.items, &mut |item| match item {
            Item::Text { index, .. } => wanted.insert(*index),
            Item::Rule { index, owners, .. } if owners.is_empty() || owners.iter().any(|o| used(o)) => {
                wanted.insert(*index)
            }
            _ => {}
        });
        for (var, by) in &self.refs {
            if by.iter().any(|p| wanted.contains(*p) || sent.contains(*p)) {
                wanted.insert(*var);
            }
        }
        self.missing(&wanted, sent)
    }

    /// Pieces of `wanted` not in `sent`, plus the sent rules that must be
    /// repeated after them. Invariant kept for the browser: for any two rules
    /// it has that set the same property, the last copies are in the
    /// stylesheet's own order. So a sent rule is repeated only when it
    /// follows (in stylesheet order) a rule being sent that it could conflict with.
    fn missing(&self, wanted: &Pieces, sent: &Pieces) -> Pieces {
        let mut needed = Pieces::with_len(self.len);
        // Property families of the rules sent in this piece, so far.
        let mut sending: HashSet<&str> = HashSet::new();
        visit(&self.items, &mut |item| match item {
            Item::Text { index, .. } | Item::Var { index, .. } if wanted.contains(*index) && !sent.contains(*index) => {
                needed.insert(*index)
            }
            Item::Rule { index, props, .. } => {
                let new = wanted.contains(*index) && !sent.contains(*index);
                let conflicts =
                    || sending.contains("*") || props.iter().any(|p| p == "*" || sending.contains(p.as_str()));
                if new || (sent.contains(*index) && !sending.is_empty() && conflicts()) {
                    needed.insert(*index);
                    sending.extend(props.iter().map(String::as_str));
                }
            }
            _ => {}
        });
        needed
    }

    /// `<style>` element with the given pieces, or nothing when empty.
    pub(crate) fn style(&self, sheet: &Stylesheet, pieces: &Pieces) -> String {
        if pieces.is_empty() {
            return String::new();
        }
        let mut css = String::new();
        write_items(&self.items, pieces, &mut css);
        format!(
            "<style data-nr-css=\"{}~{}\">{}</style>",
            sheet.id,
            pieces.encode(),
            crate::escape::escape_raw_text(&css)
        )
    }

    #[cfg(test)]
    fn empty(&self) -> Pieces {
        Pieces::with_len(self.len)
    }

    pub(crate) fn record(&self, sent: &mut Pieces, pieces: &Pieces) {
        sent.union(pieces);
    }
}

/// Rewrite the per-class `<style>` elements of a rendered document (a cached
/// static page) for a browser that already has the style ids `known`: only
/// what it is missing is kept.
/// Every per-class stylesheet rendered so far (component libraries' too) is
/// considered, besides `sheets`.
pub fn restyle_document(html: &str, sheets: &[&'static Stylesheet], known: &[&str]) -> String {
    let known: HashSet<String> = known.iter().map(|s| (*s).to_owned()).collect();
    let mut all: Vec<&'static Stylesheet> = sheets.iter().map(|s| crate::style::resolve(s)).collect();
    if let Some(cache) = SPLITS.get() {
        for (sheet, _) in cache.lock().unwrap_or_else(|e| e.into_inner()).values() {
            if !all.iter().any(|s| std::ptr::eq(*s, *sheet)) {
                all.push(sheet);
            }
        }
    }
    let mut out = html.to_owned();
    for sheet in all.into_iter().filter(|s| s.per_class.is_some()) {
        let open = format!("<style data-nr-css=\"{}~", sheet.id);
        let Some(at) = out.find(&open) else { continue };
        let bits_start = at + open.len();
        let Some(bits_len) = out[bits_start..].find('"') else { continue };
        let Some(close) = out[at..].find("</style>").map(|c| at + c + "</style>".len()) else { continue };
        let split = split(sheet);
        let Some(page) = Pieces::decode(&out[bits_start..bits_start + bits_len], split.len) else { continue };
        let sent = split.known(sheet, &known);
        let style = split.style(sheet, &split.missing(&page, &sent));
        out.replace_range(at..close, &style);
    }
    out
}

fn visit<'a>(items: &'a [Item], f: &mut dyn FnMut(&'a Item)) {
    for item in items {
        match item {
            Item::Group { items, .. } => visit(items, f),
            other => f(other),
        }
    }
}

/// Write the selected pieces; returns whether anything was written.
fn write_items(items: &[Item], pieces: &Pieces, out: &mut String) -> bool {
    let start = out.len();
    for item in items {
        match item {
            Item::Text { index, css } | Item::Rule { index, css, .. } | Item::Var { index, css, .. } => {
                if pieces.contains(*index) {
                    out.push_str(css);
                }
            }
            Item::Group { open, items } => {
                let at = out.len();
                out.push_str(open);
                if write_items(items, pieces, out) {
                    out.push('}');
                } else {
                    out.truncate(at);
                }
            }
        }
    }
    out.len() > start
}

/// `@layer properties{@supports …{*,…{--tw-x:initial;…}}}`: each custom
/// property fallback becomes a `Var` piece.
fn parse_fallbacks(css: &str, len: &mut usize) -> Vec<Item> {
    let mut items = Vec::new();
    let mut rest = css;
    while !rest.trim().is_empty() {
        let (prelude, block, after) = next_block(rest);
        match block {
            Some(body) => {
                items.push(Item::Group { open: format!("{}{{", prelude.trim()), items: parse_fallbacks(body, len) })
            }
            None => {
                let decl = prelude.trim().trim_end_matches(';');
                let name = decl.split_once(':').map(|(n, _)| n.trim()).filter(|n| n.starts_with("--"));
                let css = format!("{decl};");
                items.push(match name {
                    Some(name) => Item::Var { index: *len, name: name.to_owned(), css },
                    None => Item::Text { index: *len, css },
                });
                *len += 1;
            }
        }
        rest = after;
    }
    items
}

/// For each `Var`, the other pieces whose CSS mentions its name.
fn references(items: &[Item]) -> Vec<(usize, Vec<usize>)> {
    let mut vars = Vec::new();
    let mut others = Vec::new();
    visit(items, &mut |item| match item {
        Item::Var { index, name, .. } => vars.push((*index, name.as_str())),
        Item::Text { index, css } | Item::Rule { index, css, .. } => others.push((*index, css.as_str())),
        Item::Group { .. } => {}
    });
    vars.into_iter()
        .map(|(var, name)| (var, others.iter().filter(|(_, css)| mentions(css, name)).map(|(i, _)| *i).collect()))
        .collect()
}

/// Whether `css` contains `name` as a whole identifier.
fn mentions(css: &str, name: &str) -> bool {
    let ident = |c: Option<char>| c.is_some_and(|c| c.is_alphanumeric() || c == '-' || c == '_');
    css.match_indices(name)
        .any(|(at, _)| !ident(css[..at].chars().next_back()) && !ident(css[at + name.len()..].chars().next()))
}

/// Leading `/* … */` comments of a prelude, and the rest.
fn split_comments(prelude: &str) -> (&str, &str) {
    let mut end = 0;
    loop {
        let rest = &prelude[end..];
        let trimmed = rest.trim_start();
        match trimmed.strip_prefix("/*").and_then(|c| c.find("*/")) {
            Some(close) => end += rest.len() - trimmed.len() + 2 + close + 2,
            None => return prelude.split_at(end),
        }
    }
}

/// Every rule of `css` as pieces. A rule whose selectors all start with a
/// class is owned by those classes and sent when one of them is on the
/// page; anything else (element and attribute selectors, `:root`,
/// `@font-face`, statements such as `@import`) is always sent. Grouping
/// at-rules (`@media`, `@supports`, `@layer`, `@container`) are parsed
/// inside; `@property`, `@keyframes` and `@layer properties` become
/// registrations sent when something refers to them. Only a leading
/// comment of the whole sheet (`top`), such as a license, is kept.
fn parse_rules(css: &str, len: &mut usize, top: bool) -> Vec<Item> {
    let mut items = Vec::new();
    let mut rest = css;
    while !rest.trim().is_empty() {
        let (prelude, block, after) = next_block(rest);
        let (comment, prelude) = split_comments(prelude);
        if top && !comment.trim().is_empty() {
            items.push(Item::Text { index: *len, css: comment.trim().to_owned() });
            *len += 1;
        }
        let head = normalize(prelude);
        match block {
            Some(body) if head == "@layer properties" => {
                items.push(Item::Group { open: format!("{}{{", prelude.trim()), items: parse_fallbacks(body, len) });
            }
            Some(body)
                if head.starts_with("@property --")
                    || head.starts_with("@keyframes ")
                    || head.starts_with("@-webkit-keyframes ") =>
            {
                let name = head.split_once(' ').map(|(_, n)| n.trim().to_owned()).unwrap_or_default();
                items.push(Item::Var { index: *len, name, css: format!("{}{{{}}}", prelude.trim(), body) });
                *len += 1;
            }
            Some(body) if head.starts_with('@') && !head.starts_with("@font-face") && !head.starts_with("@page") => {
                items.push(Item::Group { open: format!("{}{{", prelude.trim()), items: parse_rules(body, len, false) });
            }
            Some(body) => {
                items.push(Item::Rule {
                    index: *len,
                    owners: owner_classes(head.as_str()),
                    css: format!("{}{{{}}}", prelude.trim(), body),
                    props: families(body),
                });
                *len += 1;
            }
            None => {
                // A statement (`@import …;`, `@layer a,b;`, `@apply …;`): keep it.
                let css = prelude.trim();
                if !css.is_empty() {
                    items.push(Item::Rule {
                        index: *len,
                        owners: Vec::new(),
                        css: css.to_owned(),
                        props: vec!["*".into()],
                    });
                    *len += 1;
                }
            }
        }
        rest = after;
    }
    items
}

/// Split off the next `prelude{body}` (or `statement;`) from `css`.
/// Returns (prelude, body, rest). Escapes and strings are respected.
fn next_block(css: &str) -> (&str, Option<&str>, &str) {
    let bytes = css.as_bytes();
    let mut i = 0;
    let mut quote = None;
    let mut open = None;
    let mut depth = 0usize;
    while i < bytes.len() {
        let b = bytes[i];
        match (quote, b) {
            (_, b'\\') => i += 1,
            (None, b'/') if bytes.get(i + 1) == Some(&b'*') => {
                i += css[i + 2..].find("*/").map_or(bytes.len(), |end| end + 3);
                continue;
            }
            (Some(q), _) if b == q => quote = None,
            (Some(_), _) => {}
            (None, b'"' | b'\'') => quote = Some(b),
            (None, b'{') => {
                if depth == 0 {
                    open = Some(i);
                }
                depth += 1;
            }
            (None, b'}') => {
                depth = depth.saturating_sub(1);
                if depth == 0
                    && let Some(o) = open
                {
                    return (&css[..o], Some(&css[o + 1..i]), &css[i + 1..]);
                }
            }
            (None, b';') if depth == 0 => return (&css[..=i], None, &css[i + 1..]),
            _ => {}
        }
        i += 1;
    }
    (css, None, "")
}

/// Owners of a selector list: the first class of each selector. Empty (the
/// rule is always needed) when one of them has no class.
fn owner_classes(selectors: &str) -> Vec<String> {
    let mut parts = Vec::new();
    let (mut depth, mut start) = (0i32, 0);
    let mut chars = selectors.char_indices();
    while let Some((i, c)) = chars.next() {
        match c {
            '\\' => {
                chars.next();
            }
            '(' | '[' => depth += 1,
            ')' | ']' => depth -= 1,
            ',' if depth == 0 => {
                parts.push(&selectors[start..i]);
                start = i + 1;
            }
            _ => {}
        }
    }
    parts.push(&selectors[start..]);
    parts.into_iter().map(owner_class).collect::<Option<Vec<_>>>().unwrap_or_default()
}

/// The class a utility selector starts with, unescaped (`.sm\:p-4:hover` → `sm:p-4`).
fn owner_class(selector: &str) -> Option<String> {
    let bytes = selector.as_bytes();
    let dot = (0..bytes.len()).find(|&i| bytes[i] == b'.' && (i == 0 || bytes[i - 1] != b'\\'))?;
    let mut name = String::new();
    let mut chars = selector[dot + 1..].chars();
    while let Some(c) = chars.next() {
        match c {
            '\\' => name.push(chars.next()?),
            c if c.is_alphanumeric() || c == '-' || c == '_' || !c.is_ascii() => name.push(c),
            _ => break,
        }
    }
    (!name.is_empty()).then_some(name)
}

/// Property families a rule body sets, for deciding which rules can
/// override each other: `padding-inline` → `padding`, `-webkit-mask` →
/// `mask`; custom properties by full name. `*` when nothing was recognized.
fn families(body: &str) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    let mut rest = body;
    while !rest.trim().is_empty() {
        let (prelude, block, after) = next_block(rest);
        match block {
            // A nested rule or at-rule: its declarations count too.
            Some(inner) => {
                for f in families(inner) {
                    if !out.contains(&f) {
                        out.push(f);
                    }
                }
            }
            None => {
                for decl in prelude.split(';') {
                    let Some((name, _)) = decl.split_once(':') else { continue };
                    let name = name.trim().to_ascii_lowercase();
                    let family = if name.starts_with("--") {
                        name
                    } else {
                        let bare =
                            ["-webkit-", "-moz-", "-ms-"].iter().find_map(|v| name.strip_prefix(v)).unwrap_or(&name);
                        match bare {
                            // Shorthands whose longhands are named differently.
                            "all" => "*".to_owned(),
                            "row-gap" | "column-gap" => "gap".to_owned(),
                            "column-count" | "column-width" => "columns".to_owned(),
                            _ => match bare.split('-').next().unwrap_or(bare) {
                                "top" | "right" | "bottom" | "left" => "inset".to_owned(),
                                "align" | "justify" => "place".to_owned(),
                                first => first.to_owned(),
                            },
                        }
                    };
                    if !family.is_empty() && !out.contains(&family) {
                        out.push(family);
                    }
                }
            }
        }
        rest = after;
    }
    if out.is_empty() {
        out.push("*".into());
    }
    out
}

fn normalize(prelude: &str) -> String {
    prelude.split_whitespace().collect::<Vec<_>>().join(" ")
}

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

    const CSS: &str = "@layer theme,base,components,utilities;@layer theme{:root{--c:red}}@layer base{*{margin:0}}\
@layer utilities{.p-4{padding:1rem}.px-2{padding-inline:.5rem}.hover\\:underline{&:hover{@media (hover:hover){text-decoration:underline}}}\
@media (width>=40rem){.sm\\:p-10{padding:2.5rem}.sm\\:w-\\[1\\.5rem\\]{width:1.5rem}}\
:where(.space-y-2>:not(:last-child)){margin-block:.5rem}}@property --tw-x{syntax:\"*\";inherits:false}@keyframes spin{to{rotate:1turn}}";

    static SHEET: Stylesheet = Stylesheet { id: "tw", css: CSS, per_class: Some(&[]), scripts: &[] };

    fn css_of(style: &str) -> &str {
        &style[style.find('>').unwrap() + 1..style.rfind("</style>").unwrap()]
    }

    #[test]
    fn owners() {
        assert_eq!(owner_class(".p-4").as_deref(), Some("p-4"));
        assert_eq!(owner_class(".sm\\:w-\\[1\\.5rem\\]").as_deref(), Some("sm:w-[1.5rem]"));
        assert_eq!(owner_class(":where(.space-y-2>:not(:last-child))").as_deref(), Some("space-y-2"));
        assert_eq!(owner_class(".dark\\:x:where(.dark,.dark *)").as_deref(), Some("dark:x"));
        assert_eq!(owner_class("input"), None);
        assert_eq!(owner_classes(".a:hover,.b\\,c:where(.x,.y)"), ["a", "b,c"]);
        assert!(owner_classes(".a,input").is_empty(), "a part without a class: always needed");
    }

    #[test]
    fn sends_only_used_rules_and_everything_outside_utilities() {
        let split = Split::parse(CSS);
        let used = |c: &str| ["px-2", "sm:p-10"].contains(&c);
        let pieces = split.delta(&used, &split.empty());
        let css = split.style(&SHEET, &pieces);
        assert_eq!(
            css_of(&css),
            "@layer theme,base,components,utilities;@layer theme{:root{--c:red}}@layer base{*{margin:0}}\
@layer utilities{.px-2{padding-inline:.5rem}@media (width>=40rem){.sm\\:p-10{padding:2.5rem}}}",
            "`@property` and `@keyframes` nothing refers to are left out"
        );
        // Every class used: the original stylesheet, minus the registrations
        // nothing refers to.
        let all = split.delta(&|_| true, &split.empty());
        let (rules, _) = CSS.split_once("@property").unwrap();
        assert_eq!(css_of(&split.style(&SHEET, &all)), rules);
    }

    #[test]
    fn property_families() {
        assert_eq!(families("padding-inline:.5rem"), ["padding"]);
        assert_eq!(families("--tw-shadow:0 0;box-shadow:var(--tw-shadow)"), ["--tw-shadow", "box"]);
        assert_eq!(families("&:hover{@media (hover:hover){-webkit-text-decoration:underline}}"), ["text"]);
        assert_eq!(families(""), ["*"]);
        assert_eq!(families("top:0;align-items:center;row-gap:1rem"), ["inset", "place", "gap"]);
    }

    #[test]
    fn unrelated_rules_are_not_repeated() {
        // `.p-4` is new; `.px-2` (same family) is repeated, `.mt-2` isn't.
        let css = "@layer utilities{.p-4{padding:1rem}.mt-2{margin-top:.5rem}.px-2{padding-inline:.5rem}}";
        let split = Split::parse(css);
        let mut sent = split.empty();
        let first = split.delta(&|c| c == "px-2" || c == "mt-2", &sent);
        split.record(&mut sent, &first);
        let next = split.delta(&|c| c == "p-4", &sent);
        assert_eq!(
            css_of(&split.style(&SHEET, &next)),
            "@layer utilities{.p-4{padding:1rem}.px-2{padding-inline:.5rem}}"
        );
    }

    #[test]
    fn registrations_follow_the_rules_that_use_them() {
        let css = "/*! notice */@layer properties{@supports (x:y){*,:before{--tw-a:initial;--tw-b:0}}}\
@layer utilities{.a{--tw-a:1px;width:var(--tw-a)}.b{height:var(--tw-b)}.c{color:red}}\
@property --tw-a{syntax:\"*\";inherits:false}@property --tw-b{syntax:\"*\";inherits:false}@keyframes spin{to{rotate:1turn}}";
        let split = Split::parse(css);
        let mut sent = split.empty();
        let first = split.delta(&|c| c == "a" || c == "c", &sent);
        assert_eq!(
            css_of(&split.style(&SHEET, &first)),
            "/*! notice */@layer properties{@supports (x:y){*,:before{--tw-a:initial;}}}\
@layer utilities{.a{--tw-a:1px;width:var(--tw-a)}.c{color:red}}@property --tw-a{syntax:\"*\";inherits:false}"
        );
        split.record(&mut sent, &first);
        let next = split.delta(&|c| c == "b", &sent);
        assert_eq!(
            css_of(&split.style(&SHEET, &next)),
            "@layer properties{@supports (x:y){*,:before{--tw-b:0;}}}@layer utilities{.b{height:var(--tw-b)}}\
@property --tw-b{syntax:\"*\";inherits:false}"
        );
        assert!(mentions("a{animation:spin 1s}", "spin") && !mentions("a{animation:spinner}", "spin"));
    }

    #[test]
    fn later_chunks_send_what_is_missing_in_order() {
        let split = Split::parse(CSS);
        let mut sent = split.empty();
        let first = split.delta(&|c| c == "px-2", &sent);
        split.record(&mut sent, &first);
        // `p-4` comes before `px-2`: `px-2` is repeated after it so it still wins.
        let next = split.delta(&|c| c == "p-4" || c == "px-2", &sent);
        assert_eq!(
            css_of(&split.style(&SHEET, &next)),
            "@layer utilities{.p-4{padding:1rem}.px-2{padding-inline:.5rem}}"
        );
        split.record(&mut sent, &next);
        assert!(split.delta(&|c| c == "p-4", &sent).is_empty(), "nothing new");
    }

    #[test]
    fn cached_documents_are_restyled_for_the_browser() {
        let split = Split::parse(CSS);
        let page = split.delta(&|c| c == "p-4" || c == "px-2", &split.empty());
        let html = format!("<head>{}</head>", split.style(&SHEET, &page));
        let had = split.delta(&|c| c == "px-2", &split.empty());
        let had_id = split.style(&SHEET, &had).split('"').nth(1).unwrap().to_owned();
        let out = restyle_document(&html, &[&SHEET], &[&had_id]);
        assert_eq!(
            css_of(&out.replace("<head>", "").replace("</head>", "")),
            "@layer utilities{.p-4{padding:1rem}.px-2{padding-inline:.5rem}}"
        );
        let all_id = split.style(&SHEET, &page).split('"').nth(1).unwrap().to_owned();
        assert_eq!(restyle_document(&html, &[&SHEET], &[&all_id]), "<head></head>", "nothing missing");
    }

    #[test]
    fn ids_round_trip_through_the_client() {
        let split = Split::parse(CSS);
        let pieces = split.delta(&|c| c == "sm:w-[1.5rem]" || c == "hover:underline", &split.empty());
        let style = split.style(&SHEET, &pieces);
        let id = style.split('"').nth(1).unwrap().to_owned();
        let known = split.known(&SHEET, &HashSet::from([id]));
        assert_eq!(known, pieces);
        let whole = split.known(&SHEET, &HashSet::from(["tw".to_owned()]));
        assert!((0..split.len).all(|i| whole.contains(i)), "the whole sheet");
        assert_eq!(split.known(&SHEET, &HashSet::from(["tw~!!".to_owned()])), split.empty(), "garbage ignored");
    }

    #[test]
    fn plain_rules_are_split_by_their_classes() {
        // Hand-written CSS imported into the build: class rules go out per
        // page; everything without a class owner is always sent.
        let css = "@import url(x.css);:root{--c:red}h1{margin:0}.card{padding:1rem}.card .title{font-weight:700}\
@media (width>=40rem){.card{padding:2rem}.wide{width:100%}}@font-face{font-family:F;src:url(f.woff2)}\
.spin{animation:spin 1s}@keyframes spin{to{rotate:1turn}}[data-theme=dark]{--c:blue}";
        let split = Split::parse(css);
        let used = |c: &str| c == "card";
        let pieces = split.delta(&used, &split.empty());
        assert_eq!(
            css_of(&split.style(&SHEET, &pieces)),
            "@import url(x.css);:root{--c:red}h1{margin:0}.card{padding:1rem}.card .title{font-weight:700}\
@media (width>=40rem){.card{padding:2rem}}@font-face{font-family:F;src:url(f.woff2)}[data-theme=dark]{--c:blue}"
        );
        let spin = split.delta(&|c| c == "spin", &split.empty());
        let out = split.style(&SHEET, &spin);
        assert!(
            out.contains(".spin{animation:spin 1s}@keyframes spin{to{rotate:1turn}}") && !out.contains(".card"),
            "{out}"
        );
        let all = split.delta(&|_| true, &split.empty());
        assert_eq!(css_of(&split.style(&SHEET, &all)), css, "all pieces: the sheet byte for byte");
    }
}