strata-dom 0.1.2

Core DOM, reactive signals, and synthetic event routing for Strata
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
//! Zero-allocation compile-time bitmasked and static class lists.

use std::fmt;

/// 256-bit bitmask representation of standard Tailwind utility classes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct ClassBitmask(pub [u64; 4]);

impl ClassBitmask {
    /// An empty 256-bit class mask.
    pub const fn empty() -> Self {
        Self([0, 0, 0, 0])
    }

    /// Construct mask from 4 64-bit integer words.
    pub const fn from_raw(raw: [u64; 4]) -> Self {
        Self(raw)
    }

    /// Returns the raw 4 64-bit words.
    pub const fn raw(&self) -> [u64; 4] {
        self.0
    }

    /// Returns true if no utility bits are set.
    pub const fn is_empty(&self) -> bool {
        self.0[0] == 0 && self.0[1] == 0 && self.0[2] == 0 && self.0[3] == 0
    }

    /// Checks if the utility at the given standard index is active.
    pub const fn contains_index(&self, index: usize) -> bool {
        if index >= 256 {
            return false;
        }
        let word = index / 64;
        let bit = index % 64;
        (self.0[word] & (1u64 << bit)) != 0
    }

    /// Sets the bit at the given index and returns the new mask.
    pub const fn with_bit(mut self, index: usize) -> Self {
        if index < 256 {
            let word = index / 64;
            let bit = index % 64;
            self.0[word] |= 1u64 << bit;
        }
        self
    }

    /// Sets the bit at the given index in place.
    pub fn set_bit(&mut self, index: usize) {
        if index < 256 {
            let word = index / 64;
            let bit = index % 64;
            self.0[word] |= 1u64 << bit;
        }
    }

    /// Returns the total number of utility classes encoded in this bitmask.
    pub fn count_ones(&self) -> u32 {
        self.0[0].count_ones()
            + self.0[1].count_ones()
            + self.0[2].count_ones()
            + self.0[3].count_ones()
    }
}

/// Exactly 256 well-known standard Tailwind utility classes mapped to bit indices 0..255.
pub const STANDARD_CLASSES: [&str; 256] = [
    // 0..11: Display
    "flex",
    "grid",
    "inline",
    "inline-flex",
    "inline-grid",
    "block",
    "inline-block",
    "hidden",
    "contents",
    "table",
    "table-row",
    "table-cell",
    // 12..26: Flexbox Direction, Wrap, Sizing
    "flex-row",
    "flex-row-reverse",
    "flex-col",
    "flex-col-reverse",
    "flex-wrap",
    "flex-wrap-reverse",
    "flex-nowrap",
    "flex-1",
    "flex-auto",
    "flex-initial",
    "flex-none",
    "grow",
    "grow-0",
    "shrink",
    "shrink-0",
    // 27..31: Align Items
    "items-start",
    "items-end",
    "items-center",
    "items-baseline",
    "items-stretch",
    // 32..37: Justify Content
    "justify-start",
    "justify-end",
    "justify-center",
    "justify-between",
    "justify-around",
    "justify-evenly",
    // 38..41: Justify Items
    "justify-items-start",
    "justify-items-end",
    "justify-items-center",
    "justify-items-stretch",
    // 42..46: Justify Self
    "justify-self-auto",
    "justify-self-start",
    "justify-self-end",
    "justify-self-center",
    "justify-self-stretch",
    // 47..54: Align Content
    "content-center",
    "content-start",
    "content-end",
    "content-between",
    "content-around",
    "content-evenly",
    "content-baseline",
    "content-stretch",
    // 55..60: Align Self
    "self-auto",
    "self-start",
    "self-end",
    "self-center",
    "self-stretch",
    "self-baseline",
    // 61..65: Positioning
    "relative",
    "absolute",
    "fixed",
    "sticky",
    "static",
    // 66..70: Inset
    "inset-0",
    "top-0",
    "bottom-0",
    "left-0",
    "right-0",
    // 71..97: Width
    "w-full",
    "w-auto",
    "w-screen",
    "w-min",
    "w-max",
    "w-fit",
    "w-0",
    "w-1",
    "w-2",
    "w-3",
    "w-4",
    "w-5",
    "w-6",
    "w-8",
    "w-10",
    "w-12",
    "w-16",
    "w-20",
    "w-24",
    "w-32",
    "w-40",
    "w-48",
    "w-56",
    "w-64",
    "w-72",
    "w-80",
    "w-96",
    // 98..124: Height
    "h-full",
    "h-auto",
    "h-screen",
    "h-min",
    "h-max",
    "h-fit",
    "h-0",
    "h-1",
    "h-2",
    "h-3",
    "h-4",
    "h-5",
    "h-6",
    "h-8",
    "h-10",
    "h-12",
    "h-16",
    "h-20",
    "h-24",
    "h-32",
    "h-40",
    "h-48",
    "h-56",
    "h-64",
    "h-72",
    "h-80",
    "h-96",
    // 125..133: Min/Max Dimensions
    "min-w-0",
    "min-w-full",
    "max-w-none",
    "max-w-full",
    "min-h-0",
    "min-h-full",
    "min-h-screen",
    "max-h-full",
    "max-h-screen",
    // 134..144: Overflow
    "overflow-auto",
    "overflow-hidden",
    "overflow-clip",
    "overflow-visible",
    "overflow-scroll",
    "overflow-x-auto",
    "overflow-x-hidden",
    "overflow-x-scroll",
    "overflow-y-auto",
    "overflow-y-hidden",
    "overflow-y-scroll",
    // 145..168: Padding
    "p-0",
    "p-1",
    "p-2",
    "p-3",
    "p-4",
    "p-5",
    "p-6",
    "p-8",
    "p-10",
    "p-12",
    "px-0",
    "px-1",
    "px-2",
    "px-3",
    "px-4",
    "px-6",
    "px-8",
    "py-0",
    "py-1",
    "py-2",
    "py-3",
    "py-4",
    "py-6",
    "py-8",
    // 169..187: Margin
    "m-0",
    "m-1",
    "m-2",
    "m-3",
    "m-4",
    "m-5",
    "m-6",
    "m-8",
    "m-auto",
    "mx-auto",
    "my-auto",
    "mx-0",
    "mx-1",
    "mx-2",
    "mx-4",
    "my-0",
    "my-1",
    "my-2",
    "my-4",
    // 188..194: Gap
    "gap-0",
    "gap-1",
    "gap-2",
    "gap-3",
    "gap-4",
    "gap-6",
    "gap-8",
    // 195..202: Text Size
    "text-xs",
    "text-sm",
    "text-base",
    "text-lg",
    "text-xl",
    "text-2xl",
    "text-3xl",
    "text-4xl",
    // 203..208: Font Weight
    "font-thin",
    "font-normal",
    "font-medium",
    "font-semibold",
    "font-bold",
    "font-extrabold",
    // 209..212: Text Align
    "text-left",
    "text-center",
    "text-right",
    "text-justify",
    // 213..216: Text Color & Truncate
    "text-white",
    "text-black",
    "text-transparent",
    "truncate",
    // 217..224: Background Color
    "bg-white",
    "bg-black",
    "bg-transparent",
    "bg-neutral-900",
    "bg-neutral-800",
    "bg-neutral-700",
    "bg-neutral-100",
    "bg-neutral-50",
    // 225..231: Border
    "border",
    "border-0",
    "border-2",
    "border-4",
    "border-neutral-800",
    "border-neutral-700",
    "border-transparent",
    // 232..239: Rounded (Border Radius)
    "rounded-none",
    "rounded-sm",
    "rounded",
    "rounded-md",
    "rounded-lg",
    "rounded-xl",
    "rounded-2xl",
    "rounded-full",
    // 240..246: Box Shadow
    "shadow-none",
    "shadow-sm",
    "shadow",
    "shadow-md",
    "shadow-lg",
    "shadow-xl",
    "shadow-2xl",
    // 247..253: Cursor
    "cursor-auto",
    "cursor-default",
    "cursor-pointer",
    "cursor-wait",
    "cursor-text",
    "cursor-move",
    "cursor-not-allowed",
    // 254..255: CSS Containment
    "contain-strict",
    "contain-paint",
];

/// Returns the index of a well-known standard Tailwind class, if recognized.
pub fn standard_class_index(name: &str) -> Option<usize> {
    // Fast lookup using linear scan over static slice (or binary if sorted, but 256 strings is trivial)
    STANDARD_CLASSES.iter().position(|&s| s == name)
}

/// Zero-allocation class list representing compile-time bitmasks, static slices, or dynamic vectors.
#[derive(Clone, PartialEq, Eq, Default)]
pub enum ClassList {
    /// Completely empty class list.
    #[default]
    Empty,
    /// Single static class token. Zero allocations.
    Single(&'static str),
    /// Static slice of string literals in read-only program memory. Zero allocations.
    Static(&'static [&'static str]),
    /// Compile-time 256-bit bitmask of standard Tailwind utilities plus optional static tail. Zero allocations.
    Masked {
        mask: ClassBitmask,
        tail: &'static [&'static str],
    },
    /// Dynamic owned list for runtime-concatenated strings.
    Dynamic(Vec<String>),
}

impl ClassList {
    /// Returns an empty class list.
    pub const fn empty() -> Self {
        Self::Empty
    }

    /// Construct a zero-allocation `ClassList` from a 256-bit bitmask of standard utilities.
    pub const fn from_mask(raw: [u64; 4]) -> Self {
        Self::Masked {
            mask: ClassBitmask::from_raw(raw),
            tail: &[],
        }
    }

    /// Construct a zero-allocation `ClassList` from a single static string.
    pub const fn from_single(s: &'static str) -> Self {
        Self::Single(s)
    }

    /// Construct a zero-allocation `ClassList` from a static slice of string literals.
    pub const fn from_static(slice: &'static [&'static str]) -> Self {
        Self::Static(slice)
    }

    /// Construct a zero-allocation `ClassList` with both a bitmask and custom static tail.
    pub const fn from_masked(mask: ClassBitmask, tail: &'static [&'static str]) -> Self {
        Self::Masked { mask, tail }
    }

    /// Returns true if the class list has zero classes.
    pub fn is_empty(&self) -> bool {
        match self {
            Self::Empty => true,
            Self::Single(s) => s.is_empty(),
            Self::Static(s) => s.is_empty(),
            Self::Masked { mask, tail } => mask.is_empty() && tail.is_empty(),
            Self::Dynamic(v) => v.is_empty(),
        }
    }

    /// Returns the number of distinct classes in the list.
    pub fn len(&self) -> usize {
        match self {
            Self::Empty => 0,
            Self::Single(_) => 1,
            Self::Static(s) => s.len(),
            Self::Masked { mask, tail } => mask.count_ones() as usize + tail.len(),
            Self::Dynamic(v) => v.len(),
        }
    }

    /// Checks if the specified class name exists in the list.
    /// Standard utility classes in masked lists are checked in O(1) bitwise operations.
    pub fn contains(&self, class: &str) -> bool {
        match self {
            Self::Empty => false,
            Self::Single(s) => *s == class,
            Self::Static(s) => s.contains(&class),
            Self::Masked { mask, tail } => {
                if let Some(idx) = standard_class_index(class) {
                    if mask.contains_index(idx) {
                        return true;
                    }
                }
                tail.contains(&class)
            }
            Self::Dynamic(v) => v.iter().any(|c| c == class),
        }
    }

    /// Appends a class to the list, promoting it to dynamic allocation if needed.
    pub fn push(&mut self, class: impl Into<String>) {
        self.as_mut_vec().push(class.into());
    }

    /// Extends the class list, promoting it to dynamic allocation if needed.
    pub fn extend(&mut self, iter: impl IntoIterator<Item = impl Into<String>>) {
        let vec = self.as_mut_vec();
        for item in iter {
            vec.push(item.into());
        }
    }

    fn as_mut_vec(&mut self) -> &mut Vec<String> {
        if let Self::Dynamic(ref mut v) = self {
            return v;
        }
        let owned: Vec<String> = self.iter().map(|s| s.to_string()).collect();
        *self = Self::Dynamic(owned);
        if let Self::Dynamic(ref mut v) = self {
            v
        } else {
            unreachable!()
        }
    }

    /// Converts this class list to an owned `Vec<String>`.
    pub fn to_vec(&self) -> Vec<String> {
        self.iter().map(|s| s.to_string()).collect()
    }

    /// Concatenates all classes in this list with the given separator string.
    pub fn join(&self, sep: &str) -> String {
        let mut out = String::new();
        for (i, c) in self.iter().enumerate() {
            if i > 0 {
                out.push_str(sep);
            }
            out.push_str(c);
        }
        out
    }

    /// Returns an iterator yielding `&str` references over all classes in the list.
    pub fn iter(&self) -> ClassIter<'_> {
        match self {
            Self::Empty => ClassIter::Empty,
            Self::Single(item) => ClassIter::Single { item: Some(item) },
            Self::Static(slice) => ClassIter::Static { slice, index: 0 },
            Self::Masked { mask, tail } => ClassIter::Masked {
                mask: *mask,
                current_bit: 0,
                tail,
                tail_index: 0,
            },
            Self::Dynamic(slice) => ClassIter::Dynamic {
                slice: slice.as_slice(),
                index: 0,
            },
        }
    }
}

impl<'a> IntoIterator for &'a ClassList {
    type Item = &'a str;
    type IntoIter = ClassIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl From<Vec<String>> for ClassList {
    fn from(v: Vec<String>) -> Self {
        Self::Dynamic(v)
    }
}

impl From<&'static [&'static str]> for ClassList {
    fn from(slice: &'static [&'static str]) -> Self {
        Self::Static(slice)
    }
}

impl From<&'static str> for ClassList {
    fn from(s: &'static str) -> Self {
        Self::Single(s)
    }
}

impl From<[u64; 4]> for ClassList {
    fn from(raw: [u64; 4]) -> Self {
        Self::from_mask(raw)
    }
}

impl PartialEq<Vec<String>> for ClassList {
    fn eq(&self, other: &Vec<String>) -> bool {
        self.len() == other.len() && self.iter().zip(other.iter()).all(|(a, b)| a == b.as_str())
    }
}

impl PartialEq<ClassList> for Vec<String> {
    fn eq(&self, other: &ClassList) -> bool {
        other == self
    }
}

impl PartialEq<[&str]> for ClassList {
    fn eq(&self, other: &[&str]) -> bool {
        self.len() == other.len() && self.iter().zip(other.iter()).all(|(a, b)| a == *b)
    }
}

impl fmt::Debug for ClassList {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}

/// Iterator over classes in a `ClassList`.
#[derive(Clone)]
pub enum ClassIter<'a> {
    Empty,
    Single {
        item: Option<&'a str>,
    },
    Static {
        slice: &'a [&'static str],
        index: usize,
    },
    Masked {
        mask: ClassBitmask,
        current_bit: usize,
        tail: &'a [&'static str],
        tail_index: usize,
    },
    Dynamic {
        slice: &'a [String],
        index: usize,
    },
}

impl<'a> Iterator for ClassIter<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::Empty => None,
            Self::Single { item } => item.take(),
            Self::Static { slice, index } => {
                if *index < slice.len() {
                    let item = slice[*index];
                    *index += 1;
                    Some(item)
                } else {
                    None
                }
            }
            Self::Dynamic { slice, index } => {
                if *index < slice.len() {
                    let item = slice[*index].as_str();
                    *index += 1;
                    Some(item)
                } else {
                    None
                }
            }
            Self::Masked {
                mask,
                current_bit,
                tail,
                tail_index,
            } => {
                while *current_bit < 256 {
                    let bit = *current_bit;
                    *current_bit += 1;
                    if mask.contains_index(bit) {
                        return Some(STANDARD_CLASSES[bit]);
                    }
                }
                if *tail_index < tail.len() {
                    let item = tail[*tail_index];
                    *tail_index += 1;
                    Some(item)
                } else {
                    None
                }
            }
        }
    }
}