vize_atelier_sfc 0.69.0

Atelier SFC - The Single File Component workshop for Vize
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
use memchr::{memchr, memchr_iter, memmem};
use std::borrow::Cow;
use vize_carton::{cstr, FxHashMap, String};

// Tag name bytes for fast comparison
const TAG_TEMPLATE: &[u8] = b"template";
const TAG_SCRIPT: &[u8] = b"script";
const TAG_STYLE: &[u8] = b"style";

type BlockAttrs<'a> = FxHashMap<Cow<'a, str>, Cow<'a, str>>;
type BlockParseOutput<'a> = (
    &'a [u8],       // tag name as bytes
    BlockAttrs<'a>, // attrs with borrowed strings
    Cow<'a, str>,   // content as borrowed string
    usize,          // content start
    usize,          // content end
    usize,          // end position
    usize,          // end line
    usize,          // end column
);
type BlockParseError = (&'static str, String);
type BlockParseResult<'a> = Result<Option<BlockParseOutput<'a>>, BlockParseError>;

struct BlockEndSearch<'a> {
    bytes: &'a [u8],
    source: &'a str,
    tag_name: &'a [u8],
    pos: usize,
    content_start: usize,
    start_line: usize,
    initial_last_newline: usize,
    attrs: BlockAttrs<'a>,
}

/// Build a uniform `(code, message)` error for any malformed block.
fn build_malformed_error(tag_name: &[u8], reason: &str) -> BlockParseError {
    let tag_str = std::str::from_utf8(tag_name).unwrap_or("unknown");
    (
        "MALFORMED_BLOCK",
        cstr!("Malformed <{tag_str}> block: {reason}."),
    )
}

/// Fast tag name comparison using byte slices
#[inline(always)]
pub(super) fn tag_name_eq(name: &[u8], expected: &[u8]) -> bool {
    name.len() == expected.len() && name.eq_ignore_ascii_case(expected)
}

/// Fast byte slice prefix check
#[inline(always)]
fn starts_with_bytes(haystack: &[u8], needle: &[u8]) -> bool {
    haystack.len() >= needle.len() && haystack[..needle.len()].eq_ignore_ascii_case(needle)
}

/// Fast tag name character check
#[inline(always)]
fn is_tag_name_char_fast(b: u8) -> bool {
    matches!(b, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'-' | b'_')
}

/// Fast whitespace check
#[inline(always)]
fn is_whitespace_fast(b: u8) -> bool {
    matches!(b, b' ' | b'\t' | b'\n' | b'\r')
}

#[inline]
fn advance_line(bytes: &[u8], base: usize, line: &mut usize, last_newline: &mut usize) {
    for offset in memchr_iter(b'\n', bytes) {
        *line += 1;
        *last_newline = base + offset;
    }
}

fn can_start_regex_literal(prev_significant_char: u8) -> bool {
    matches!(
        prev_significant_char,
        b'=' | b'('
            | b'['
            | b','
            | b':'
            | b'{'
            | b';'
            | b'\n'
            | b'?'
            | b'&'
            | b'|'
            | b'+'
            | b'-'
            | b'*'
            | b'!'
            | b'>'
            | b'<'
            | b'%'
            | b'^'
    )
}

fn skip_regex_literal(
    bytes: &[u8],
    mut pos: usize,
    len: usize,
    line: &mut usize,
    last_newline: &mut usize,
) -> Option<usize> {
    debug_assert_eq!(bytes[pos], b'/');
    pos += 1;
    let mut in_character_class = false;

    while pos < len {
        let c = bytes[pos];

        if c == b'\n' {
            // An unescaped newline terminates JavaScript regex literals. Stop
            // treating this as regex so normal malformed-block handling wins.
            return None;
        }

        if c == b'\\' {
            if pos + 1 < len && bytes[pos + 1] == b'\n' {
                *line += 1;
                *last_newline = pos + 1;
            }
            pos = (pos + 2).min(len);
            continue;
        }

        if in_character_class {
            if c == b']' {
                in_character_class = false;
            }
            pos += 1;
            continue;
        }

        match c {
            b'[' => {
                in_character_class = true;
                pos += 1;
            }
            b'/' => {
                pos += 1;
                while pos < len && (bytes[pos].is_ascii_alphanumeric() || bytes[pos] == b'_') {
                    pos += 1;
                }
                return Some(pos);
            }
            _ => pos += 1,
        }
    }

    None
}

/// Find the end of a closing tag `</tag_name` followed by optional whitespace and `>`.
/// Returns the position immediately after `>`, or `None` if no valid closing tag at `pos`.
#[inline]
fn find_closing_tag_end(bytes: &[u8], pos: usize, len: usize, tag_name: &[u8]) -> Option<usize> {
    // Need at least "</" + tag_name + ">"
    if pos + 2 + tag_name.len() >= len {
        return None;
    }
    if bytes[pos] != b'<' || bytes[pos + 1] != b'/' {
        return None;
    }
    let name_start = pos + 2;
    if !bytes[name_start..name_start + tag_name.len()].eq_ignore_ascii_case(tag_name) {
        return None;
    }
    let mut check_pos = name_start + tag_name.len();
    while check_pos < len {
        match bytes[check_pos] {
            b'>' => return Some(check_pos + 1),
            b' ' | b'\t' | b'\n' | b'\r' => check_pos += 1,
            _ => return None,
        }
    }
    None
}

/// Parse a single block from the source using byte operations
/// Returns borrowed strings using Cow for zero-copy
///
/// - `Ok(Some(...))` — successfully parsed block.
/// - `Ok(None)` — no SFC block starts at this position.
/// - `Err(...)` — a block starts here but is incomplete or malformed.
pub(super) fn parse_block_fast<'a>(
    bytes: &'a [u8],
    source: &'a str,
    start: usize,
    start_line: usize,
) -> BlockParseResult<'a> {
    let len = bytes.len();

    // Skip '<'
    let mut pos = start + 1;
    if pos >= len {
        return Ok(None);
    }

    // Parse tag name - find end of tag name
    let tag_start = pos;
    while pos < len && is_tag_name_char_fast(bytes[pos]) {
        pos += 1;
    }

    if pos == tag_start {
        return Ok(None);
    }

    let tag_name = &source.as_bytes()[tag_start..pos];

    // Parse attributes with zero-copy
    let mut attrs: BlockAttrs<'a> = FxHashMap::default();

    while pos < len && bytes[pos] != b'>' {
        // Skip whitespace
        while pos < len && is_whitespace_fast(bytes[pos]) {
            pos += 1;
        }

        if pos >= len || bytes[pos] == b'>' || bytes[pos] == b'/' {
            break;
        }

        // Parse attribute name
        let attr_start = pos;
        while pos < len {
            let c = bytes[pos];
            if c == b'='
                || c == b' '
                || c == b'>'
                || c == b'/'
                || c == b'\t'
                || c == b'\n'
                || c == b'\r'
            {
                break;
            }
            pos += 1;
        }

        if pos == attr_start {
            pos += 1;
            continue;
        }

        // Zero-copy: borrow from source
        let attr_name: Cow<'a, str> = Cow::Borrowed(&source[attr_start..pos]);

        // Skip whitespace
        while pos < len && (bytes[pos] == b' ' || bytes[pos] == b'\t') {
            pos += 1;
        }

        let attr_value: Cow<'a, str> = if pos < len && bytes[pos] == b'=' {
            pos += 1;

            // Skip whitespace
            while pos < len && (bytes[pos] == b' ' || bytes[pos] == b'\t') {
                pos += 1;
            }

            if pos < len && (bytes[pos] == b'"' || bytes[pos] == b'\'') {
                let quote_char = bytes[pos];
                pos += 1;
                let value_start = pos;

                // Use memchr for fast quote finding
                if let Some(quote_pos) = memchr(quote_char, &bytes[pos..]) {
                    pos += quote_pos;
                    let value = Cow::Borrowed(&source[value_start..pos]);
                    pos += 1; // Skip closing quote
                    value
                } else {
                    // No closing quote found
                    while pos < len && bytes[pos] != quote_char {
                        pos += 1;
                    }
                    let value = Cow::Borrowed(&source[value_start..pos]);
                    if pos < len {
                        pos += 1;
                    }
                    value
                }
            } else {
                // Unquoted value
                let value_start = pos;
                while pos < len {
                    let c = bytes[pos];
                    if c == b' ' || c == b'>' || c == b'/' || c == b'\t' || c == b'\n' {
                        break;
                    }
                    pos += 1;
                }
                Cow::Borrowed(&source[value_start..pos])
            }
        } else {
            // Boolean attribute
            Cow::Borrowed("")
        };

        if !attr_name.is_empty() {
            attrs.insert(attr_name, attr_value);
        }
    }

    // Handle self-closing tag
    let is_self_closing = pos > 0 && pos < len && bytes[pos - 1] == b'/';

    if is_self_closing {
        if pos < len && bytes[pos] == b'>' {
            pos += 1;
        }
        return Ok(Some((
            tag_name,
            attrs,
            Cow::Borrowed(""),
            pos,
            pos,
            pos,
            start_line,
            pos - start,
        )));
    }

    // Skip '>'
    if pos < len && bytes[pos] == b'>' {
        pos += 1;
    } else {
        return Err(build_malformed_error(
            tag_name,
            "the opening tag is incomplete",
        ));
    }

    let content_start = pos;

    // Find closing tag based on tag type
    let mut line = start_line;
    let mut last_newline = start;

    // Handle known tags with static closing tags
    if tag_name.eq_ignore_ascii_case(TAG_TEMPLATE) {
        // Template block: handle nested template tags
        let mut depth = 1;

        // Check for closing template tag, handling whitespace before the closing '>'
        // This handles cases like:
        //   </template>       - normal
        //   </template\n   >  - closing '>' on next line
        fn is_closing_template_tag(bytes: &[u8], pos: usize, len: usize) -> Option<usize> {
            // Check if we have "</template" (without the final ">")
            const CLOSING_TAG_PREFIX: &[u8] = b"</template";
            if pos + CLOSING_TAG_PREFIX.len() > len {
                return None;
            }
            if !bytes[pos..pos + CLOSING_TAG_PREFIX.len()].eq_ignore_ascii_case(CLOSING_TAG_PREFIX)
            {
                return None;
            }
            // Find the closing '>' allowing whitespace
            let mut check_pos = pos + CLOSING_TAG_PREFIX.len();
            while check_pos < len {
                match bytes[check_pos] {
                    b'>' => return Some(check_pos + 1), // Return position after '>'
                    b' ' | b'\t' | b'\n' | b'\r' => check_pos += 1,
                    _ => return None, // Invalid character in closing tag
                }
            }
            None
        }

        while pos < len {
            let Some(lt_offset) = memchr(b'<', &bytes[pos..]) else {
                advance_line(&bytes[pos..], pos, &mut line, &mut last_newline);
                break;
            };

            advance_line(
                &bytes[pos..pos + lt_offset],
                pos,
                &mut line,
                &mut last_newline,
            );
            pos += lt_offset;

            // Check for closing tag using byte comparison
            if let Some(end_tag_pos) = is_closing_template_tag(bytes, pos, len) {
                depth -= 1;
                if depth == 0 {
                    let content_end = pos;
                    let end_pos = end_tag_pos;
                    let col = pos - last_newline + (end_pos - pos);
                    let content = Cow::Borrowed(&source[content_start..content_end]);
                    return Ok(Some((
                        tag_name,
                        attrs,
                        content,
                        content_start,
                        content_end,
                        end_pos,
                        line,
                        col,
                    )));
                }
                pos = end_tag_pos;
                continue;
            }

            // Check for nested opening tag
            if starts_with_bytes(&bytes[pos + 1..], TAG_TEMPLATE) {
                let tag_check_pos = pos + 1 + TAG_TEMPLATE.len();
                if tag_check_pos < len {
                    let next_char = bytes[tag_check_pos];
                    if next_char == b' '
                        || next_char == b'>'
                        || next_char == b'\n'
                        || next_char == b'\t'
                        || next_char == b'\r'
                    {
                        // Check if self-closing
                        let mut check_pos = tag_check_pos;
                        let mut is_self_closing_nested = false;
                        while check_pos < len && bytes[check_pos] != b'>' {
                            if bytes[check_pos] == b'/'
                                && check_pos + 1 < len
                                && bytes[check_pos + 1] == b'>'
                            {
                                is_self_closing_nested = true;
                                break;
                            }
                            check_pos += 1;
                        }
                        if !is_self_closing_nested {
                            depth += 1;
                        }
                    }
                }
            }

            pos += 1;
        }
        return Err(build_malformed_error(
            tag_name,
            "the closing tag is missing",
        ));
    }

    if tag_name.eq_ignore_ascii_case(TAG_STYLE) {
        return find_block_end(BlockEndSearch {
            bytes,
            source,
            tag_name,
            pos,
            content_start,
            start_line,
            initial_last_newline: start,
            attrs,
        });
    }

    // Custom block: need to find closing tag dynamically
    if !tag_name.eq_ignore_ascii_case(TAG_SCRIPT) {
        return find_block_end(BlockEndSearch {
            bytes,
            source,
            tag_name,
            pos,
            content_start,
            start_line,
            initial_last_newline: content_start,
            attrs,
        });
    }

    // For script blocks, we need to be aware of string literals to avoid
    // matching closing tags inside strings like: const x = `</script>`
    let is_script = tag_name.eq_ignore_ascii_case(TAG_SCRIPT);

    // Track the previous non-whitespace character to determine string context
    let mut prev_significant_char: u8 = b'\n'; // Start as if at beginning of line

    while pos < len {
        let b = bytes[pos];

        if b == b'\n' {
            line += 1;
            last_newline = pos;
            prev_significant_char = b'\n';
            pos += 1;
            continue;
        }

        // Skip whitespace but don't update prev_significant_char
        if b == b' ' || b == b'\t' || b == b'\r' {
            pos += 1;
            continue;
        }

        // For script blocks, skip over comments and string literals
        if is_script {
            // Check for single-line comment
            if b == b'/' && pos + 1 < len && bytes[pos + 1] == b'/' {
                // Skip to end of line
                pos += 2;
                if let Some(newline_offset) = memchr(b'\n', &bytes[pos..]) {
                    pos += newline_offset;
                } else {
                    pos = len;
                }
                continue;
            }

            // Check for multi-line comment
            if b == b'/' && pos + 1 < len && bytes[pos + 1] == b'*' {
                pos += 2;
                if let Some(end_offset) = memmem::find(&bytes[pos..], b"*/") {
                    advance_line(
                        &bytes[pos..pos + end_offset],
                        pos,
                        &mut line,
                        &mut last_newline,
                    );
                    pos += end_offset + 2;
                } else {
                    advance_line(&bytes[pos..], pos, &mut line, &mut last_newline);
                    pos = len;
                }
                continue;
            }

            if b == b'/' && can_start_regex_literal(prev_significant_char) {
                if let Some(next_pos) =
                    skip_regex_literal(bytes, pos, len, &mut line, &mut last_newline)
                {
                    prev_significant_char = b'/';
                    pos = next_pos;
                    continue;
                }
            }

            // Check for string literals (', ", `)
            // Only treat as string if in a context where strings are expected
            // (after =, (, [, ,, :, {, or at start of expression)
            // This avoids treating quotes inside regex literals as strings
            //
            // For backticks specifically, also allow after alphanumeric characters
            // to handle tagged templates (e.g., html`...`) and keywords (e.g., return `...`)
            if b == b'\'' || b == b'"' || b == b'`' {
                let is_string_context = matches!(
                    prev_significant_char,
                    b'=' | b'('
                        | b'['
                        | b','
                        | b':'
                        | b'{'
                        | b';'
                        | b'\n'
                        | b'?'
                        | b'&'
                        | b'|'
                        | b'+'
                        | b'-'
                        | b'*'
                        | b'!'
                        | b'>'
                        | b'<'
                        | b'%'
                        | b'^'
                ) || (b == b'`'
                    && (prev_significant_char.is_ascii_alphanumeric()
                        || prev_significant_char == b'_'
                        || prev_significant_char == b')'));

                if is_string_context {
                    let quote = b;
                    pos += 1;

                    while pos < len {
                        let c = bytes[pos];

                        if c == b'\n' {
                            line += 1;
                            last_newline = pos;
                        }

                        // Handle escape sequences
                        if c == b'\\' && pos + 1 < len {
                            pos += 2; // Skip escaped character
                            continue;
                        }

                        // Handle template literal expressions ${...}
                        if quote == b'`' && c == b'$' && pos + 1 < len && bytes[pos + 1] == b'{' {
                            pos += 2;
                            let mut brace_depth = 1;
                            while pos < len && brace_depth > 0 {
                                let inner = bytes[pos];
                                if inner == b'\n' {
                                    line += 1;
                                    last_newline = pos;
                                }
                                if inner == b'{' {
                                    brace_depth += 1;
                                } else if inner == b'}' {
                                    brace_depth -= 1;
                                } else if inner == b'\\' && pos + 1 < len {
                                    pos += 1; // Skip escape in template expression
                                }
                                pos += 1;
                            }
                            continue;
                        }

                        // End of string
                        if c == quote {
                            pos += 1;
                            break;
                        }

                        // For non-template strings, newline ends the string (syntax error, but handle gracefully)
                        if quote != b'`' && c == b'\n' {
                            break;
                        }

                        pos += 1;
                    }
                    prev_significant_char = quote; // String ended with quote
                    continue;
                }
            }
        }

        // Check for closing tag (allows optional whitespace before '>')
        if b == b'<' {
            if let Some(end_tag_pos) = find_closing_tag_end(bytes, pos, len, tag_name) {
                let content_end = pos;
                let col = pos - last_newline + (end_tag_pos - pos);
                let content = Cow::Borrowed(&source[content_start..content_end]);
                return Ok(Some((
                    tag_name,
                    attrs,
                    content,
                    content_start,
                    content_end,
                    end_tag_pos,
                    line,
                    col,
                )));
            }
        }

        prev_significant_char = b;
        pos += 1;
    }

    Err(build_malformed_error(
        tag_name,
        "the closing tag is missing",
    ))
}

/// Find the end of a raw block by jumping between `<` bytes.
fn find_block_end<'a>(search: BlockEndSearch<'a>) -> BlockParseResult<'a> {
    let BlockEndSearch {
        bytes,
        source,
        tag_name,
        mut pos,
        content_start,
        start_line,
        initial_last_newline,
        attrs,
    } = search;
    let len = bytes.len();
    let mut line = start_line;
    let mut last_newline = initial_last_newline;

    while pos < len {
        if let Some(lt_offset) = memchr(b'<', &bytes[pos..]) {
            // Count newlines
            advance_line(
                &bytes[pos..pos + lt_offset],
                pos,
                &mut line,
                &mut last_newline,
            );
            pos += lt_offset;

            // Check for closing tag (allows optional whitespace before '>')
            if bytes[pos] == b'<' {
                if let Some(end_tag_pos) = find_closing_tag_end(bytes, pos, len, tag_name) {
                    let content_end = pos;
                    let col = pos - last_newline + (end_tag_pos - pos);
                    let content = Cow::Borrowed(&source[content_start..content_end]);
                    return Ok(Some((
                        tag_name,
                        attrs,
                        content,
                        content_start,
                        content_end,
                        end_tag_pos,
                        line,
                        col,
                    )));
                }
            }
            pos += 1;
        } else {
            break;
        }
    }

    Err(build_malformed_error(
        tag_name,
        "the closing tag is missing",
    ))
}