verter_core 0.0.1-beta.1

Vue 3 SFC compiler - transforms Vue Single File Components to render functions with TypeScript support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
//! CSS style processor using lightningcss.
//!
//! Provides `process_style()` — a standalone function for processing Vue SFC style blocks.
//! Uses lightningcss for correct CSS parsing and transformation.
//!
//! ## Processing Pipeline
//!
//! 1. **Pre-pass** (`prepass.rs`): Replace Vue-specific syntax with valid CSS markers
//!    - `v-bind(expr)` → `var(--{scopeId}-{sanitized})`
//!    - `:deep(.inner)` → `[__v_deep__] .inner`
//!    - `:slotted(.inner)` → `.inner[__v_slotted__]`
//!
//! 2. **lightningcss parse + transform** (`scoped.rs`, `modules.rs`):
//!    - Scoped: insert `[data-v-{scopeId}]` attribute selectors
//!    - Modules: hash class names and build mapping
//!
//! 3. **Serialization**: lightningcss serializes the AST back to clean CSS

pub mod modules;
pub mod prepass;
pub mod scoped;
pub mod types;
mod walk;

pub use types::{CssError, ProcessStyleOptions, ProcessStyleResult, VBindVar};

use lightningcss::stylesheet::{ParserOptions, PrinterOptions, StyleSheet};

/// Parse CSS with lightningcss and serialize back to normalize it.
///
/// This normalizes comments, strings, at-rules, and nesting so downstream
/// string-level transforms (scoped, modules) see well-formed CSS.
fn normalize_css(css: &str) -> Result<String, CssError> {
    let stylesheet = StyleSheet::parse(css, ParserOptions::default())
        .map_err(|e| CssError::Parse(e.to_string()))?;
    let result = stylesheet
        .to_css(PrinterOptions::default())
        .map_err(|e| CssError::Serialize(e.to_string()))?;
    Ok(result.code)
}

/// Process a CSS style block: apply scoping, CSS modules, and v-bind replacement.
///
/// This is the main entry point, called from:
/// - The Rust `StyleCodegenPlugin` for plain CSS blocks (inline in compileForVite)
/// - The NAPI `processStyle()` binding for preprocessed CSS (from vite-plugin)
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn process_style(
    css: &str,
    options: &ProcessStyleOptions<'_>,
) -> Result<ProcessStyleResult, CssError> {
    // Step 1: Pre-pass — replace v-bind() and Vue pseudo-selectors with valid CSS
    let prepass_result = prepass::prepass(css, options.scope_id);
    let mut current_css = prepass_result.css;
    let v_bind_vars = prepass_result.v_bind_vars;

    // Step 2: Normalize CSS once (if any transform needs it)
    let needs_transform = options.is_module || options.scoped;
    if needs_transform {
        current_css = normalize_css(&current_css)?;
    }

    // Step 3: Apply CSS modules (class name hashing) on normalized CSS
    let mut module_classes = Vec::new();
    if options.is_module {
        let (modules_css, mapping) =
            modules::apply_css_modules_normalized(&current_css, options.scope_id);
        current_css = modules_css;
        module_classes = mapping;
    }

    // Step 4: Apply scoped selectors on normalized CSS
    if options.scoped {
        current_css = scoped::apply_scoped_normalized(&current_css, options.scope_id);
    }

    let module_name = if options.is_module {
        Some(options.module_name.unwrap_or("$style").to_string())
    } else {
        None
    };

    Ok(ProcessStyleResult {
        code: current_css,
        source_map: None, // TODO: source map support
        module_classes,
        module_name,
        v_bind_vars,
    })
}

/// Fast CSS style processing: skip lightningcss normalization.
///
/// Uses the same walker-based scoping but directly on raw CSS (after prepass).
/// Faster than [`process_style`] since it avoids the full CSS parse/serialize
/// cycle. CSS values are not normalized — colors, units, and shorthand remain
/// as authored.
///
/// **Trade-off:** CSS nesting (`&`) is not flattened. If the input uses native
/// CSS nesting, the output may contain `&` selectors that need browser support.
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn process_style_fast(
    css: &str,
    options: &ProcessStyleOptions<'_>,
) -> Result<ProcessStyleResult, CssError> {
    // Step 1: Pre-pass — replace v-bind() and Vue pseudo-selectors with valid CSS
    let prepass_result = prepass::prepass(css, options.scope_id);
    let mut current_css = prepass_result.css;
    let v_bind_vars = prepass_result.v_bind_vars;

    // Step 2: Apply CSS modules directly on raw CSS (no normalization)
    let mut module_classes = Vec::new();
    if options.is_module {
        let (modules_css, mapping) =
            modules::apply_css_modules_normalized(&current_css, options.scope_id);
        current_css = modules_css;
        module_classes = mapping;
    }

    // Step 3: Apply scoped selectors directly on raw CSS (no normalization)
    if options.scoped {
        current_css = scoped::apply_scoped_raw(&current_css, options.scope_id);
    }

    let module_name = if options.is_module {
        Some(options.module_name.unwrap_or("$style").to_string())
    } else {
        None
    };

    Ok(ProcessStyleResult {
        code: current_css,
        source_map: None,
        module_classes,
        module_name,
        v_bind_vars,
    })
}

/// Extract CSS class names from a style block (lightweight scan, no lightningcss).
///
/// Scans for `.className` patterns in CSS selectors. Handles basic cases:
/// - `.btn { ... }` → `"btn"`
/// - `.card-title, .card-body { ... }` → `"card-title"`, `"card-body"`
/// - `@media (...) { .responsive { ... } }` → `"responsive"`
///
/// Skips class-like patterns inside strings, comments, and `v-bind()`.
/// May produce false positives from comments, but that's acceptable for IDE completions.
pub fn extract_css_class_names(css: &str) -> Vec<String> {
    let mut classes = Vec::new();
    let mut seen = std::collections::HashSet::new();
    let bytes = css.as_bytes();
    let len = bytes.len();
    let mut i = 0;

    while i < len {
        // Skip block comments
        if i + 1 < len && bytes[i] == b'/' && bytes[i + 1] == b'*' {
            i += 2;
            while i + 1 < len && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
                i += 1;
            }
            i += 2;
            continue;
        }
        // Skip line comments (SCSS)
        if i + 1 < len && bytes[i] == b'/' && bytes[i + 1] == b'/' {
            while i < len && bytes[i] != b'\n' {
                i += 1;
            }
            continue;
        }
        // Skip strings
        if bytes[i] == b'"' || bytes[i] == b'\'' {
            let quote = bytes[i];
            i += 1;
            while i < len && bytes[i] != quote {
                if bytes[i] == b'\\' {
                    i += 1;
                }
                i += 1;
            }
            i += 1;
            continue;
        }
        // Skip rule bodies (between { and })
        if bytes[i] == b'{' {
            let mut depth = 1;
            i += 1;
            while i < len && depth > 0 {
                match bytes[i] {
                    b'{' => depth += 1,
                    b'}' => depth -= 1,
                    b'"' | b'\'' => {
                        let q = bytes[i];
                        i += 1;
                        while i < len && bytes[i] != q {
                            if bytes[i] == b'\\' {
                                i += 1;
                            }
                            i += 1;
                        }
                    }
                    b'/' if i + 1 < len && bytes[i + 1] == b'*' => {
                        i += 2;
                        while i + 1 < len && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
                            i += 1;
                        }
                        i += 1; // skip past '*'
                    }
                    b'.' => {
                        // Nested selector (e.g. @media { .foo { } })
                        i += 1;
                        let start = i;
                        while i < len
                            && (bytes[i].is_ascii_alphanumeric()
                                || bytes[i] == b'-'
                                || bytes[i] == b'_')
                        {
                            i += 1;
                        }
                        if i > start {
                            let name = &css[start..i];
                            if seen.insert(name.to_string()) {
                                classes.push(name.to_string());
                            }
                        }
                        continue;
                    }
                    _ => {}
                }
                i += 1;
            }
            continue;
        }
        // Detect class selector
        if bytes[i] == b'.' {
            i += 1;
            let start = i;
            while i < len
                && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'-' || bytes[i] == b'_')
            {
                i += 1;
            }
            if i > start {
                let name = &css[start..i];
                if seen.insert(name.to_string()) {
                    classes.push(name.to_string());
                }
            }
            continue;
        }
        i += 1;
    }

    classes
}

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

    #[test]
    fn test_process_style_scoped_basic() {
        let result = process_style(
            ".box { color: red; }",
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: true,
                is_module: false,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        assert!(
            result.code.contains(".box[data-v-a4f2eed6]"),
            "Got: {}",
            result.code
        );
    }

    #[test]
    fn test_process_style_scoped_with_v_bind() {
        let result = process_style(
            ".box { color: v-bind(primary); }",
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: true,
                is_module: false,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        assert!(
            result.code.contains("var(--a4f2eed6-primary)"),
            "Got: {}",
            result.code
        );
        assert!(
            result.code.contains("[data-v-a4f2eed6]"),
            "Got: {}",
            result.code
        );
        assert_eq!(result.v_bind_vars.len(), 1);
        assert_eq!(result.v_bind_vars[0].expression, "primary");
    }

    #[test]
    fn test_process_style_deep() {
        let result = process_style(
            ":deep(.inner) { color: red; }",
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: true,
                is_module: false,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        assert!(
            result.code.contains("[data-v-a4f2eed6]"),
            "Got: {}",
            result.code
        );
        assert!(result.code.contains(".inner"), "Got: {}", result.code);
        // Inner should NOT have scope attr
        assert!(
            !result.code.contains(".inner[data-v"),
            "Inner should not be scoped. Got: {}",
            result.code
        );
    }

    #[test]
    fn test_process_style_slotted() {
        let result = process_style(
            ":slotted(.slot) { color: red; }",
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: true,
                is_module: false,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        assert!(
            result.code.contains("[data-v-a4f2eed6-s]"),
            "Got: {}",
            result.code
        );
    }

    #[test]
    fn test_process_style_global() {
        let result = process_style(
            ":global(.reset) { margin: 0; }",
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: true,
                is_module: false,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        assert!(result.code.contains(".reset"), "Got: {}", result.code);
        assert!(
            !result.code.contains("[data-v"),
            "Should not have scope attr. Got: {}",
            result.code
        );
    }

    #[test]
    fn test_process_style_modules() {
        let result = process_style(
            ".btn { color: red; } .card { display: flex; }",
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: false,
                is_module: true,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        // Class names should be content-hashed (format: {name}_{8-hex-chars})
        assert!(
            result.code.contains("btn_")
                && !result.code.contains(".btn{")
                && !result.code.contains(".btn "),
            "btn should be hashed. Got: {}",
            result.code
        );
        assert!(
            result.code.contains("card_")
                && !result.code.contains(".card{")
                && !result.code.contains(".card "),
            "card should be hashed. Got: {}",
            result.code
        );
        assert_eq!(result.module_classes.len(), 2);
    }

    #[test]
    fn test_process_style_no_transform() {
        let result = process_style(
            ".box { color: red; }",
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: false,
                is_module: false,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        // No scoping, no modules — CSS should pass through (possibly normalized)
        assert!(result.code.contains(".box"), "Got: {}", result.code);
        assert!(
            !result.code.contains("[data-v"),
            "Should not have scope attr. Got: {}",
            result.code
        );
    }

    #[test]
    fn test_process_style_scoped_and_modules() {
        let result = process_style(
            ".btn { color: red; } .card { display: flex; }",
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: true,
                is_module: true,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        // Classes should be hashed AND scoped
        assert_eq!(result.module_classes.len(), 2);
        assert!(
            result.code.contains("[data-v-a4f2eed6]"),
            "Should have scope attr. Got: {}",
            result.code
        );
        // Hashed class names should be present (content-hash format: {name}_{8-hex-chars})
        assert!(
            result.code.contains("btn_") && !result.code.contains(".btn["),
            "Should have hashed btn. Got: {}",
            result.code
        );
        assert!(
            result.code.contains("card_") && !result.code.contains(".card["),
            "Should have hashed card. Got: {}",
            result.code
        );
    }

    #[test]
    fn test_process_style_pseudo_class_ordering() {
        let result = process_style(
            ".btn:hover { color: red; }",
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: true,
                is_module: false,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        assert!(
            result.code.contains(".btn[data-v-a4f2eed6]:hover"),
            "Scope should be before :hover. Got: {}",
            result.code
        );
    }

    #[test]
    fn test_process_style_pseudo_class_and_pseudo_element() {
        let result = process_style(
            ".btn:hover::before { content: ''; }",
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: true,
                is_module: false,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        assert!(
            result.code.contains(".btn[data-v-a4f2eed6]:hover:before")
                || result.code.contains(".btn[data-v-a4f2eed6]:hover::before"),
            "Scope should be before :hover. Got: {}",
            result.code
        );
    }

    #[test]
    fn test_process_style_pseudo_element_ordering() {
        let result = process_style(
            ".text::before { content: ''; }",
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: true,
                is_module: false,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        // lightningcss may normalize ::before to :before
        assert!(
            result.code.contains(".text[data-v-a4f2eed6]:before")
                || result.code.contains(".text[data-v-a4f2eed6]::before"),
            "Scope should be before ::before. Got: {}",
            result.code
        );
    }

    /// @ai-generated - Test lightningcss normalization on grid layout CSS
    /// to verify that grid-template-areas/columns/rows are preserved
    /// and not collapsed into shorthand that could change behavior.
    #[test]
    fn test_process_style_grid_layout_normalization() {
        let css = r#"
.dashboard {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
  grid-template-columns: 250px 1fr;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}
.stat-card {
  background: white;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.badge.success {
  background: #d4edda;
  color: #155724;
}
.indicator.online {
  background: #4CAF50;
}
"#;
        let result = process_style(
            css,
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: true,
                is_module: false,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        eprintln!("=== NORMALIZED+SCOPED CSS ===\n{}", result.code);

        // grid-template-areas must be preserved (not collapsed into shorthand)
        assert!(
            result.code.contains("grid-template-areas"),
            "grid-template-areas must be preserved after normalization. Got:\n{}",
            result.code
        );
        // grid-template-columns and rows must be preserved
        assert!(
            result.code.contains("grid-template-columns"),
            "grid-template-columns must be preserved. Got:\n{}",
            result.code
        );
        assert!(
            result.code.contains("grid-template-rows"),
            "grid-template-rows must be preserved. Got:\n{}",
            result.code
        );
        // Compound selectors must be scoped correctly
        assert!(
            result.code.contains(".badge.success[data-v-a4f2eed6]"),
            "Compound selector must be scoped. Got:\n{}",
            result.code
        );
        assert!(
            result.code.contains(".indicator.online[data-v-a4f2eed6]"),
            "Compound selector must be scoped. Got:\n{}",
            result.code
        );
        // box-shadow with rgba must still be valid
        assert!(
            result.code.contains("box-shadow"),
            "box-shadow must be preserved. Got:\n{}",
            result.code
        );
    }

    // ===================================================================
    // @ai-generated - Comparison tests: process_style vs process_style_fast
    // Both paths must produce the same scoped selectors. Values may differ
    // (lightningcss normalizes colors/units), but selectors must match.
    // ===================================================================

    /// Helper: count the number of scoped attribute occurrences in CSS.
    fn count_scope_attrs(css: &str, scope_id: &str) -> usize {
        let attr = format!("[data-v-{}]", scope_id);
        css.matches(&attr).count()
    }

    /// Both paths scope simple classes identically.
    #[test]
    fn test_fast_vs_normal_simple_classes() {
        let css = ".box { color: red; } .card { display: flex; }";
        let opts = ProcessStyleOptions {
            scope_id: "a4f2eed6",
            scoped: true,
            is_module: false,
            module_name: None,
            filename: None,
            sourcemap: false,
        };
        let normal = process_style(css, &opts).unwrap();
        let fast = process_style_fast(css, &opts).unwrap();

        // Both must produce exactly 2 scoped selectors
        assert_eq!(count_scope_attrs(&normal.code, "a4f2eed6"), 2);
        assert_eq!(count_scope_attrs(&fast.code, "a4f2eed6"), 2);
        // Both must scope the same selectors
        assert!(normal.code.contains(".box[data-v-a4f2eed6]"));
        assert!(fast.code.contains(".box[data-v-a4f2eed6]"));
        assert!(normal.code.contains(".card[data-v-a4f2eed6]"));
        assert!(fast.code.contains(".card[data-v-a4f2eed6]"));
    }

    /// Both paths scope descendant selectors identically.
    #[test]
    fn test_fast_vs_normal_descendant() {
        let css = ".parent .child { color: red; } .a > .b { color: blue; }";
        let opts = ProcessStyleOptions {
            scope_id: "test1234",
            scoped: true,
            is_module: false,
            module_name: None,
            filename: None,
            sourcemap: false,
        };
        let normal = process_style(css, &opts).unwrap();
        let fast = process_style_fast(css, &opts).unwrap();

        // Both should scope only the last compound selector
        assert!(normal.code.contains(".child[data-v-test1234]"));
        assert!(fast.code.contains(".child[data-v-test1234]"));
        assert!(normal.code.contains(".b[data-v-test1234]"));
        assert!(fast.code.contains(".b[data-v-test1234]"));
        // Parent should NOT be scoped in either
        assert!(!normal.code.contains(".parent[data-v-test1234]"));
        assert!(!fast.code.contains(".parent[data-v-test1234]"));
    }

    /// Both paths scope selectors inside @media correctly.
    #[test]
    fn test_fast_vs_normal_media() {
        let css = ".top { color: red; } @media (max-width: 768px) { .inner { color: blue; } } .bottom { color: green; }";
        let opts = ProcessStyleOptions {
            scope_id: "a4f2eed6",
            scoped: true,
            is_module: false,
            module_name: None,
            filename: None,
            sourcemap: false,
        };
        let normal = process_style(css, &opts).unwrap();
        let fast = process_style_fast(css, &opts).unwrap();

        for (label, code) in [("normal", &normal.code), ("fast", &fast.code)] {
            assert!(
                code.contains(".top[data-v-a4f2eed6]"),
                "{}: .top must be scoped. Got: {}",
                label,
                code
            );
            assert!(
                code.contains(".inner[data-v-a4f2eed6]"),
                "{}: .inner inside @media must be scoped. Got: {}",
                label,
                code
            );
            assert!(
                code.contains(".bottom[data-v-a4f2eed6]"),
                "{}: .bottom must be scoped. Got: {}",
                label,
                code
            );
        }
    }

    /// Both paths skip @keyframes selectors.
    #[test]
    fn test_fast_vs_normal_keyframes() {
        let css =
            "@keyframes fade { from { opacity: 1; } to { opacity: 0; } } .box { color: red; }";
        let opts = ProcessStyleOptions {
            scope_id: "a4f2eed6",
            scoped: true,
            is_module: false,
            module_name: None,
            filename: None,
            sourcemap: false,
        };
        let normal = process_style(css, &opts).unwrap();
        let fast = process_style_fast(css, &opts).unwrap();

        for (label, code) in [("normal", &normal.code), ("fast", &fast.code)] {
            assert!(
                code.contains(".box[data-v-a4f2eed6]"),
                "{}: .box must be scoped. Got: {}",
                label,
                code
            );
            assert!(
                !code.contains("from[data-v"),
                "{}: keyframe 'from' must NOT be scoped. Got: {}",
                label,
                code
            );
            assert!(
                !code.contains("to[data-v"),
                "{}: keyframe 'to' must NOT be scoped. Got: {}",
                label,
                code
            );
        }
    }

    /// Both paths handle pseudo-classes correctly.
    #[test]
    fn test_fast_vs_normal_pseudo() {
        let css = ".btn:hover { color: red; } .item:not(.active) { opacity: 0.5; }";
        let opts = ProcessStyleOptions {
            scope_id: "a4f2eed6",
            scoped: true,
            is_module: false,
            module_name: None,
            filename: None,
            sourcemap: false,
        };
        let normal = process_style(css, &opts).unwrap();
        let fast = process_style_fast(css, &opts).unwrap();

        for (label, code) in [("normal", &normal.code), ("fast", &fast.code)] {
            assert!(
                code.contains(".btn[data-v-a4f2eed6]:hover"),
                "{}: .btn:hover must be scoped before :hover. Got: {}",
                label,
                code
            );
            assert!(
                code.contains(".item[data-v-a4f2eed6]:not(.active)"),
                "{}: .item:not must be scoped before :not. Got: {}",
                label,
                code
            );
        }
    }

    /// Both paths handle v-bind() replacement.
    #[test]
    fn test_fast_vs_normal_v_bind() {
        let css = ".box { color: v-bind(primary); font-size: v-bind('theme.size'); }";
        let opts = ProcessStyleOptions {
            scope_id: "a4f2eed6",
            scoped: true,
            is_module: false,
            module_name: None,
            filename: None,
            sourcemap: false,
        };
        let normal = process_style(css, &opts).unwrap();
        let fast = process_style_fast(css, &opts).unwrap();

        for (label, result) in [("normal", &normal), ("fast", &fast)] {
            assert!(
                result.code.contains("var(--a4f2eed6-primary)"),
                "{}: v-bind(primary) must be replaced. Got: {}",
                label,
                result.code
            );
            assert!(
                result.code.contains("[data-v-a4f2eed6]"),
                "{}: must be scoped. Got: {}",
                label,
                result.code
            );
            assert_eq!(
                result.v_bind_vars.len(),
                2,
                "{}: must have 2 v-bind vars",
                label
            );
        }
    }

    /// Both paths handle CSS modules.
    #[test]
    fn test_fast_vs_normal_modules() {
        let css = ".btn { color: red; } .card { display: flex; }";
        let opts = ProcessStyleOptions {
            scope_id: "a4f2eed6",
            scoped: false,
            is_module: true,
            module_name: None,
            filename: None,
            sourcemap: false,
        };
        let normal = process_style(css, &opts).unwrap();
        let fast = process_style_fast(css, &opts).unwrap();

        for (label, result) in [("normal", &normal), ("fast", &fast)] {
            assert_eq!(
                result.module_classes.len(),
                2,
                "{}: must have 2 module classes",
                label
            );
            // Content-hash format: {name}_{8-hex-chars}
            assert!(
                result.code.contains("btn_") && !result.code.contains(".btn{"),
                "{}: btn must be hashed. Got: {}",
                label,
                result.code
            );
            assert!(
                result.code.contains("card_") && !result.code.contains(".card{"),
                "{}: card must be hashed. Got: {}",
                label,
                result.code
            );
        }
        // Module class mappings should be identical
        assert_eq!(
            normal.module_classes, fast.module_classes,
            "Module class mappings must match"
        );
    }

    /// Both paths handle real-world template-heavy.vue CSS.
    #[test]
    fn test_fast_vs_normal_template_heavy() {
        let sfc_source =
            include_str!("../../../../packages/benchmark/src/fixtures/template-heavy.vue");
        // Extract CSS from the style block
        let style_start = sfc_source
            .find("<style scoped>")
            .expect("must have <style scoped>");
        let css_start = style_start + "<style scoped>".len();
        let css_end = sfc_source[css_start..]
            .find("</style>")
            .expect("must have </style>");
        let css = &sfc_source[css_start..css_start + css_end];

        let opts = ProcessStyleOptions {
            scope_id: "0d04bfeb",
            scoped: true,
            is_module: false,
            module_name: None,
            filename: None,
            sourcemap: false,
        };
        let normal = process_style(css, &opts).unwrap();
        let fast = process_style_fast(css, &opts).unwrap();

        let normal_count = count_scope_attrs(&normal.code, "0d04bfeb");
        let fast_count = count_scope_attrs(&fast.code, "0d04bfeb");

        eprintln!("Normal scoped attrs: {}", normal_count);
        eprintln!("Fast scoped attrs: {}", fast_count);

        assert_eq!(
            normal_count, fast_count,
            "Both paths must produce the same number of scoped attributes.\nNormal: {}\nFast: {}",
            normal_count, fast_count
        );

        // Spot-check key selectors from template-heavy.vue
        for sel in [
            ".dashboard[data-v-0d04bfeb]",
            ".stat-card[data-v-0d04bfeb]",
            ".badge.success[data-v-0d04bfeb]",
            ".indicator.online[data-v-0d04bfeb]",
        ] {
            assert!(
                normal.code.contains(sel),
                "Normal must contain {}. Got:\n{}",
                sel,
                normal.code
            );
            assert!(
                fast.code.contains(sel),
                "Fast must contain {}. Got:\n{}",
                sel,
                fast.code
            );
        }
    }

    /// @ai-generated - Selectors inside @media blocks MUST be scoped.
    /// This is a regression test for the walker bug where selectors inside
    /// @media were skipped because `last_block_end` tracking caused the
    /// inner selector text to include the @media prefix.
    #[test]
    fn test_process_style_media_query_selectors_scoped() {
        let css = r#"
.box { color: red; }
@media (min-width: 768px) {
  .box { color: blue; }
  .container { display: flex; }
}
.footer { color: green; }
"#;
        let result = process_style(
            css,
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: true,
                is_module: false,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        eprintln!("=== @media SCOPED CSS ===\n{}", result.code);

        // Top-level selectors must be scoped
        assert!(
            result.code.contains(".box[data-v-a4f2eed6]"),
            "Top-level .box must be scoped. Got:\n{}",
            result.code
        );
        assert!(
            result.code.contains(".footer[data-v-a4f2eed6]"),
            "Top-level .footer must be scoped. Got:\n{}",
            result.code
        );

        // Selectors INSIDE @media must also be scoped
        // Find the @media block and check its selectors
        let media_start = result.code.find("@media").expect("@media must be present");
        let media_section = &result.code[media_start..];
        assert!(
            media_section.contains(".box[data-v-a4f2eed6]"),
            "@media .box must be scoped. Got:\n{}",
            result.code
        );
        assert!(
            media_section.contains(".container[data-v-a4f2eed6]"),
            "@media .container must be scoped. Got:\n{}",
            result.code
        );
    }

    /// @ai-generated - Selectors inside nested @supports blocks must be scoped.
    #[test]
    fn test_process_style_supports_query_selectors_scoped() {
        let css = r#"
.box { display: grid; }
@supports (display: grid) {
  .grid-item { grid-column: span 2; }
}
"#;
        let result = process_style(
            css,
            &ProcessStyleOptions {
                scope_id: "a4f2eed6",
                scoped: true,
                is_module: false,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        eprintln!("=== @supports SCOPED CSS ===\n{}", result.code);

        assert!(
            result.code.contains(".box[data-v-a4f2eed6]"),
            "Top-level .box must be scoped. Got:\n{}",
            result.code
        );

        // Selector inside @supports must also be scoped
        if result.code.contains("@supports") {
            let supports_start = result.code.find("@supports").unwrap();
            let supports_section = &result.code[supports_start..];
            assert!(
                supports_section.contains(".grid-item[data-v-a4f2eed6]"),
                "@supports .grid-item must be scoped. Got:\n{}",
                result.code
            );
        }
    }

    /// @ai-generated - @media with multiple nested selectors and combined rules.
    #[test]
    fn test_process_style_media_complex_selectors_scoped() {
        let css = r#"
.sidebar { width: 250px; }
@media (max-width: 768px) {
  .sidebar { display: none; }
  .content { width: 100%; }
  .nav .link { color: blue; }
}
@media (min-width: 1200px) {
  .sidebar { width: 300px; }
}
.content { padding: 1rem; }
"#;
        let result = process_style(
            css,
            &ProcessStyleOptions {
                scope_id: "test123",
                scoped: true,
                is_module: false,
                module_name: None,
                filename: None,
                sourcemap: false,
            },
        )
        .unwrap();

        eprintln!("=== complex @media SCOPED CSS ===\n{}", result.code);

        // All selectors must be scoped, both inside and outside @media
        let scope = "[data-v-test123]";

        // Top-level
        assert!(
            result.code.contains(&format!(".sidebar{}", scope)),
            "Top-level .sidebar must be scoped. Got:\n{}",
            result.code
        );
        assert!(
            result.code.contains(&format!(".content{}", scope)),
            "Top-level .content must be scoped. Got:\n{}",
            result.code
        );

        // Count scoped selectors - should be at least 6 (2 top + 3 in first @media + 1 in second)
        let scope_count = result.code.matches(scope).count();
        assert!(
            scope_count >= 6,
            "Expected at least 6 scoped selectors, found {}. Got:\n{}",
            scope_count,
            result.code
        );
    }

    // ── extract_css_class_names tests ───────────────────────────

    #[test]
    fn extract_basic_classes() {
        let css = ".btn { color: red; } .card { padding: 1rem; }";
        let classes = extract_css_class_names(css);
        assert_eq!(classes, vec!["btn", "card"]);
    }

    #[test]
    fn extract_classes_with_media_query() {
        let css = ".mobile { display: block; } @media (min-width: 768px) { .desktop { display: block; } }";
        let classes = extract_css_class_names(css);
        assert!(classes.contains(&"mobile".to_string()));
        assert!(classes.contains(&"desktop".to_string()));
    }

    #[test]
    fn extract_deduplicates() {
        let css = ".btn { color: red; } .btn:hover { color: blue; }";
        let classes = extract_css_class_names(css);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0], "btn");
    }

    #[test]
    fn extract_kebab_case_classes() {
        let css = ".card-title { font-weight: bold; } .card-body { padding: 1rem; }";
        let classes = extract_css_class_names(css);
        assert_eq!(classes, vec!["card-title", "card-body"]);
    }

    #[test]
    fn extract_empty_css() {
        let classes = extract_css_class_names("");
        assert!(classes.is_empty());
    }

    #[test]
    fn extract_no_classes() {
        let css = "div { color: red; } p { margin: 0; }";
        let classes = extract_css_class_names(css);
        assert!(classes.is_empty());
    }
}