revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
//! CSS parser for TUI styling

use crate::constants::{MAX_COMMENT_LENGTH, MAX_CSS_FILE_SIZE};
use crate::style::{Declaration, ErrorCode, ParseError, Rule, StyleSheet};

/// Create a ParseError at the given position
fn make_error(css: &str, pos: usize, message: &str, code: ErrorCode) -> ParseError {
    ParseError::at_offset(message, css, pos).with_code(code)
}

/// Create a ParseError for missing brace
fn missing_brace_error(css: &str, pos: usize, expected: char) -> ParseError {
    make_error(
        css,
        pos,
        &format!("expected '{}' but found end of input", expected),
        ErrorCode::MissingBrace,
    )
}

/// Maximum allowed CSS size to prevent memory exhaustion
const MAX_CSS_SIZE: usize = MAX_CSS_FILE_SIZE as usize;
/// Maximum number of rules to prevent excessive memory usage
const MAX_RULES: usize = 10_000;
/// Maximum number of total declarations across all rules
const MAX_DECLARATIONS: usize = 10_000; // Lowered for testing
/// Maximum number of @keyframes definitions
const MAX_KEYFRAMES: usize = 100;
/// Maximum number of keyframe blocks per @keyframes
const MAX_KEYFRAME_BLOCKS: usize = 50;

pub fn parse(css: &str) -> Result<StyleSheet, ParseError> {
    // Check CSS size limit before parsing
    if css.len() > MAX_CSS_SIZE {
        return Err(make_error(
            css,
            css.len().min(css.len()),
            &format!(
                "CSS input too large: {} bytes (max: {} bytes). Consider splitting into multiple files.",
                css.len(),
                MAX_CSS_SIZE
            ),
            ErrorCode::InvalidValue,
        ));
    }

    let mut sheet = StyleSheet::new();
    let bytes = css.as_bytes();
    let mut pos = 0;
    let mut total_declarations = 0;

    while pos < bytes.len() {
        // Check rule limit
        if sheet.rules.len() >= MAX_RULES {
            return Err(make_error(
                css,
                pos,
                &format!(
                    "Too many CSS rules: {} (max: {}). Consider simplifying your styles.",
                    sheet.rules.len(),
                    MAX_RULES
                ),
                ErrorCode::InvalidValue,
            ));
        }
        // Skip whitespace and comments
        pos = skip_whitespace_bytes(bytes, pos);
        if pos >= bytes.len() {
            break;
        }

        // Check for CSS variable definition (in :root)
        if bytes[pos..].starts_with(b":root") {
            pos = parse_root_variables_str(css, pos, &mut sheet)?;
            continue;
        }

        // Check for @keyframes definition
        if bytes[pos..].starts_with(b"@keyframes") {
            if sheet.keyframes.len() >= MAX_KEYFRAMES {
                return Err(make_error(
                    css,
                    pos,
                    &format!(
                        "Too many @keyframes definitions: {} (max: {})",
                        sheet.keyframes.len(),
                        MAX_KEYFRAMES
                    ),
                    ErrorCode::InvalidValue,
                ));
            }
            pos = parse_keyframes_block(css, pos, &mut sheet)?;
            continue;
        }

        // Parse selector
        let (selector, new_pos) = parse_selector_str(css, pos)?;
        pos = new_pos;

        // Skip whitespace
        pos = skip_whitespace_bytes(bytes, pos);

        // Expect '{'
        if pos >= bytes.len() || bytes[pos] != b'{' {
            return Err(make_error(
                css,
                pos,
                &format!(
                    "expected '{{' after selector '{}', found '{}'",
                    selector,
                    if pos < bytes.len() {
                        bytes[pos] as char
                    } else {
                        ' '
                    }
                ),
                ErrorCode::MissingBrace,
            ));
        }
        pos += 1;

        // Parse declarations
        let (declarations, new_pos) = parse_declarations_str(css, pos)?;
        pos = new_pos;

        // Check total declaration limit
        total_declarations += declarations.len();
        if total_declarations > MAX_DECLARATIONS {
            return Err(make_error(
                css,
                pos,
                &format!(
                    "Too many CSS declarations: {} (max: {}). Consider simplifying your styles.",
                    total_declarations, MAX_DECLARATIONS
                ),
                ErrorCode::InvalidValue,
            ));
        }

        // Expect '}'
        if pos >= bytes.len() || bytes[pos] != b'}' {
            return Err(missing_brace_error(css, pos, '}'));
        }
        pos += 1;

        sheet.rules.push(Rule {
            selector,
            declarations,
        });
    }

    Ok(sheet)
}

/// Skip ASCII whitespace using byte slice (no allocation)
#[inline]
fn skip_whitespace_bytes(bytes: &[u8], mut pos: usize) -> usize {
    while pos < bytes.len() && bytes[pos].is_ascii_whitespace() {
        pos += 1;
    }
    pos
}

/// Skip whitespace and block comments using byte slice (no allocation)
///
/// This function includes protection against malicious input that attempts
/// to cause denial-of-service through malformed or unterminated comments.
fn skip_whitespace_and_comments_bytes(bytes: &[u8], mut pos: usize) -> usize {
    loop {
        pos = skip_whitespace_bytes(bytes, pos);
        // Check for block comment start (/*)
        if pos + 1 < bytes.len() && bytes[pos] == b'/' && bytes[pos + 1] == b'*' {
            // Skip block comment
            pos += 2;
            let comment_start = pos;

            // Look for comment end (*/), with protection against malformed comments
            while pos + 1 < bytes.len() {
                // Check for maliciously long comments that could cause DoS
                if pos - comment_start > MAX_COMMENT_LENGTH {
                    // Return an error position that signals the comment is too long
                    return bytes.len(); // Signal error condition
                }

                if bytes[pos] == b'*' && bytes[pos + 1] == b'/' {
                    pos += 2; // Skip the closing */
                    break;
                }
                pos += 1;
            }

            // If we reached the end without finding closing */, skip to end
            if pos >= bytes.len() || pos + 1 >= bytes.len() {
                #[cfg(debug_assertions)]
                eprintln!("[revue css] warning: unterminated comment in CSS");
                return bytes.len();
            }
        } else {
            break;
        }
    }
    pos
}

/// Parse :root variables block using zero-copy str slicing
fn parse_root_variables_str(
    css: &str,
    mut pos: usize,
    sheet: &mut StyleSheet,
) -> Result<usize, ParseError> {
    let bytes = css.as_bytes();

    // Skip ":root"
    pos += 5;
    pos = skip_whitespace_bytes(bytes, pos);

    // Expect '{'
    if pos >= bytes.len() || bytes[pos] != b'{' {
        return Err(make_error(
            css,
            pos,
            "expected '{' after :root",
            ErrorCode::MissingBrace,
        ));
    }
    pos += 1;

    // Parse variable declarations
    loop {
        pos = skip_whitespace_and_comments_bytes(bytes, pos);

        if pos >= bytes.len() {
            return Err(missing_brace_error(css, pos, '}'));
        }

        if bytes[pos] == b'}' {
            pos += 1;
            break;
        }

        // Variable name starts with --
        if !bytes[pos..].starts_with(b"--") {
            return Err(make_error(
                css,
                pos,
                "CSS variables must start with '--' (e.g., --primary-color)",
                ErrorCode::InvalidSyntax,
            )
            .suggest("use '--variable-name: value;' format"));
        }

        // Read variable name (ASCII only, safe to use byte indexing)
        let start = pos;
        while pos < bytes.len() && bytes[pos] != b':' && !bytes[pos].is_ascii_whitespace() {
            pos += 1;
        }
        let name = css[start..pos].to_string();

        pos = skip_whitespace_bytes(bytes, pos);

        // Expect ':'
        if pos >= bytes.len() || bytes[pos] != b':' {
            return Err(make_error(
                css,
                pos,
                "expected ':' after variable name",
                ErrorCode::InvalidSyntax,
            )
            .suggest("format: --variable-name: value;"));
        }
        pos += 1;

        pos = skip_whitespace_bytes(bytes, pos);

        // Read value until ';' or '}'
        let start = pos;
        while pos < bytes.len() && bytes[pos] != b';' && bytes[pos] != b'}' {
            pos += 1;
        }
        let value = css[start..pos].trim().to_string();

        sheet.variables.insert(name, value);

        if pos < bytes.len() && bytes[pos] == b';' {
            pos += 1;
        }
    }

    Ok(pos)
}

/// Parse selector using zero-copy str slicing
fn parse_selector_str(css: &str, mut pos: usize) -> Result<(String, usize), ParseError> {
    let bytes = css.as_bytes();
    let start = pos;
    while pos < bytes.len() && bytes[pos] != b'{' {
        pos += 1;
    }
    Ok((css[start..pos].trim().to_string(), pos))
}

/// Parse declarations block using zero-copy str slicing
fn parse_declarations_str(
    css: &str,
    mut pos: usize,
) -> Result<(Vec<Declaration>, usize), ParseError> {
    let bytes = css.as_bytes();
    let mut declarations = Vec::new();

    loop {
        pos = skip_whitespace_and_comments_bytes(bytes, pos);

        if pos >= bytes.len() || bytes[pos] == b'}' {
            break;
        }

        // Read property name
        let start = pos;
        while pos < bytes.len() && bytes[pos] != b':' && bytes[pos] != b'}' {
            pos += 1;
        }
        let property = css[start..pos].trim().to_string();

        if pos >= bytes.len() || bytes[pos] == b'}' {
            break;
        }

        // Skip ':'
        pos += 1;
        pos = skip_whitespace_bytes(bytes, pos);

        // Read value until ';' or '}'
        let start = pos;
        let mut paren_depth: i32 = 0;
        while pos < bytes.len() {
            match bytes[pos] {
                b'(' => paren_depth += 1,
                b')' => paren_depth = paren_depth.saturating_sub(1),
                b';' | b'}' if paren_depth == 0 => break,
                _ => {}
            }
            pos += 1;
        }
        let value = css[start..pos].trim().to_string();

        if !property.is_empty() {
            declarations.push(Declaration { property, value });
        }

        if pos < bytes.len() && bytes[pos] == b';' {
            pos += 1;
        }
    }

    Ok((declarations, pos))
}

/// Parse a @keyframes block
fn parse_keyframes_block(
    css: &str,
    mut pos: usize,
    sheet: &mut StyleSheet,
) -> Result<usize, ParseError> {
    let bytes = css.as_bytes();

    // Skip "@keyframes"
    pos += 10;
    pos = skip_whitespace_bytes(bytes, pos);

    // Parse animation name
    let name_start = pos;
    while pos < bytes.len() && !bytes[pos].is_ascii_whitespace() && bytes[pos] != b'{' {
        pos += 1;
    }

    let name = css[name_start..pos].trim().to_string();
    if name.is_empty() {
        return Err(make_error(
            css,
            name_start,
            "expected name after @keyframes",
            ErrorCode::InvalidSyntax,
        ));
    }

    pos = skip_whitespace_bytes(bytes, pos);

    // Expect outer '{'
    if pos >= bytes.len() || bytes[pos] != b'{' {
        return Err(make_error(
            css,
            pos,
            "expected '{' after @keyframes name",
            ErrorCode::MissingBrace,
        ));
    }
    pos += 1;

    let mut keyframe_blocks = Vec::new();

    // Parse keyframe blocks
    loop {
        pos = skip_whitespace_and_comments_bytes(bytes, pos);

        if pos >= bytes.len() {
            return Err(missing_brace_error(css, pos, '}'));
        }

        if bytes[pos] == b'}' {
            pos += 1;
            break;
        }

        if keyframe_blocks.len() >= MAX_KEYFRAME_BLOCKS {
            return Err(make_error(
                css,
                pos,
                &format!(
                    "Too many keyframe blocks in @keyframes '{}': {} (max: {})",
                    name,
                    keyframe_blocks.len(),
                    MAX_KEYFRAME_BLOCKS
                ),
                ErrorCode::InvalidValue,
            ));
        }

        // Parse keyframe selector (from, to, N%)
        let (percent, new_pos) = parse_keyframe_selector(css, pos)?;
        pos = new_pos;

        pos = skip_whitespace_bytes(bytes, pos);

        // Expect '{'
        if pos >= bytes.len() || bytes[pos] != b'{' {
            return Err(make_error(
                css,
                pos,
                "expected '{' after keyframe selector",
                ErrorCode::MissingBrace,
            ));
        }
        pos += 1;

        // Parse declarations
        let (declarations, new_pos) = parse_declarations_str(css, pos)?;
        pos = new_pos;

        // Expect '}'
        if pos >= bytes.len() || bytes[pos] != b'}' {
            return Err(missing_brace_error(css, pos, '}'));
        }
        pos += 1;

        keyframe_blocks.push(crate::style::KeyframeBlock {
            percent,
            declarations,
        });
    }

    sheet.keyframes.insert(
        name.clone(),
        crate::style::KeyframesDefinition {
            name,
            keyframes: keyframe_blocks,
        },
    );

    Ok(pos)
}

/// Parse a keyframe selector: `from` → 0, `to` → 100, `50%` → 50
fn parse_keyframe_selector(css: &str, mut pos: usize) -> Result<(u8, usize), ParseError> {
    let bytes = css.as_bytes();
    let start = pos;

    while pos < bytes.len()
        && !bytes[pos].is_ascii_whitespace()
        && bytes[pos] != b'{'
        && bytes[pos] != b','
    {
        pos += 1;
    }

    let selector = css[start..pos].trim();
    let percent = match selector {
        "from" => 0,
        "to" => 100,
        s if s.ends_with('%') => {
            let num_str = &s[..s.len() - 1];
            num_str
                .parse::<u8>()
                .map_err(|_| {
                    make_error(
                        css,
                        start,
                        &format!("invalid keyframe percentage: '{}'", s),
                        ErrorCode::InvalidValue,
                    )
                })?
                .min(100)
        }
        _ => {
            return Err(make_error(
                css,
                start,
                &format!(
                    "invalid keyframe selector '{}': expected 'from', 'to', or 'N%'",
                    selector
                ),
                ErrorCode::InvalidSyntax,
            ));
        }
    };

    Ok((percent, pos))
}

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

    #[test]
    fn test_parse_normal_css() {
        let css = r#"
            .button {
                background: blue;
                color: white;
            }
        "#;
        assert!(parse(css).is_ok());
    }

    #[test]
    fn test_css_size_limit() {
        // Create CSS that exceeds 1MB
        let mut large_css = String::new();
        large_css.push_str(".test { content: ");
        for _ in 0..1_200_000 {
            large_css.push('x');
        }
        large_css.push_str("; }");

        let result = parse(&large_css);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.message.contains("too large"));
    }

    #[test]
    fn test_css_rules_limit() {
        // Create CSS with many rules
        let mut css = String::new();
        for i in 0..10_001 {
            css.push_str(&format!(".class{} {{ color: red; }}", i));
        }

        let result = parse(&css);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.message.contains("Too many CSS rules"));
    }

    #[test]
    fn test_css_declarations_limit() {
        // Create CSS with many declarations (exceeds 10,000 limit)
        let mut css = String::new();
        for rule in 0..2 {
            css.push_str(&format!(".rule{} {{ ", rule));
            for i in 0..5_001 {
                css.push_str(&format!("prop{}: val{}; ", i, i));
            }
            css.push_str("} ");
        }

        let result = parse(&css);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.message.contains("CSS declarations") || err.message.contains("declarations"));
    }

    #[test]
    fn test_css_within_limits() {
        // CSS within all limits should parse fine
        let mut css = String::new();
        for i in 0..100 {
            css.push_str(&format!(".class{} {{ ", i));
            for j in 0..10 {
                css.push_str(&format!("prop{}: val{}; ", j, j));
            }
            css.push_str("}");
        }

        assert!(parse(&css).is_ok());
    }

    // Security tests for comment parsing
    #[test]
    fn test_css_normal_comments() {
        // Normal comments should work fine
        let css = r#"
        /* This is a normal comment */
        .box { width: 100; }
        /* Another comment */
        .text { color: red; }
        "#;
        assert!(parse(&css).is_ok());
    }

    #[test]
    fn test_css_multiline_comment() {
        // Multi-line comments should work
        let css = r#"
        /* This is a
           multi-line
           comment */
        .box { width: 100; }
        "#;
        assert!(parse(&css).is_ok());
    }

    #[test]
    fn test_css_nested_comments_wont_hang() {
        // CSS doesn't support nested comments - this should parse but not hang
        let css = "/* outer /* inner */ comment */ .box { width: 100; }";
        // The parser will handle this as: /* outer /* inner */ then "comment */" as text
        // It won't hang or crash
        let _ = parse(&css);
    }

    #[test]
    fn test_css_unterminated_comment_is_safe() {
        // Unterminated comment should not cause infinite loop
        let css = "/* This comment is never closed .box { width: 100; }";
        // The scanner should handle this safely without infinite loop
        let result = parse(&css);
        // Should either error or parse what it can, but never hang
        assert!(result.is_ok() || result.is_err());
    }

    #[test]
    fn test_css_comment_after_property() {
        // Comment after property value
        let css = ".box { width: 100; /* comment after */ }";
        assert!(parse(&css).is_ok());
    }

    #[test]
    fn test_css_empty_comment() {
        // Empty comment should work
        let css = "/**/ .box { width: 100; }";
        assert!(parse(&css).is_ok());
    }

    // @keyframes parsing tests
    #[test]
    fn test_keyframes_from_to() {
        let css = r#"
            @keyframes fadeIn {
                from { opacity: 0; }
                to { opacity: 1; }
            }
        "#;
        let sheet = parse(css).unwrap();
        assert!(sheet.keyframes.contains_key("fadeIn"));
        let def = &sheet.keyframes["fadeIn"];
        assert_eq!(def.keyframes.len(), 2);
        assert_eq!(def.keyframes[0].percent, 0);
        assert_eq!(def.keyframes[1].percent, 100);
    }

    #[test]
    fn test_keyframes_percentages() {
        let css = r#"
            @keyframes slide {
                0% { x: 0; }
                50% { x: 50; }
                100% { x: 100; }
            }
        "#;
        let sheet = parse(css).unwrap();
        let def = &sheet.keyframes["slide"];
        assert_eq!(def.keyframes.len(), 3);
        assert_eq!(def.keyframes[0].percent, 0);
        assert_eq!(def.keyframes[1].percent, 50);
        assert_eq!(def.keyframes[2].percent, 100);
    }

    #[test]
    fn test_keyframes_empty_body() {
        let css = "@keyframes empty {}";
        let sheet = parse(css).unwrap();
        assert_eq!(sheet.keyframes["empty"].keyframes.len(), 0);
    }

    #[test]
    fn test_keyframes_missing_name_error() {
        let css = "@keyframes { from { opacity: 0; } }";
        assert!(parse(css).is_err());
    }

    #[test]
    fn test_keyframes_with_regular_rules() {
        let css = r#"
            .btn { color: red; }
            @keyframes fadeIn {
                from { opacity: 0; }
                to { opacity: 1; }
            }
            .text { color: blue; }
        "#;
        let sheet = parse(css).unwrap();
        assert_eq!(sheet.rules.len(), 2);
        assert!(sheet.keyframes.contains_key("fadeIn"));
    }

    #[test]
    fn test_keyframes_invalid_selector() {
        let css = r#"
            @keyframes test {
                invalid { opacity: 0; }
            }
        "#;
        assert!(parse(css).is_err());
    }
}