marco-core 1.1.0

nom-based Markdown parser, HTML renderer, and intelligence features (highlights, diagnostics, completions) for the Marco editor.
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
// CommonMark HTML Blocks Grammar
// Parses 7 types of HTML blocks per CommonMark spec
//
// HTML Block Types:
// 1. Special tags (script, pre, style, textarea) - consume until closing tag
// 2. Comments (<!-- ... -->)
// 3. Processing instructions (<? ... ?>)
// 4. Declarations (<!DOCTYPE ...>)
// 5. CDATA (<![CDATA[ ... ]]>)
// 6. Standard block tags (address, article, aside, etc.) - end at blank line
// 7. Complete tags (well-formed open/close tag on single line) - end at blank line

use crate::grammar::shared::Span;
use nom::{
    branch::alt,
    bytes::complete::{tag, take_until, take_while},
    character::complete::line_ending,
    combinator::opt,
    IResult, Input, Parser,
};

// HTML Block Type 2: Comments
// Parses <!-- ... --> on its own line(s)
/// Parse a CommonMark HTML block comment (`<!-- ... -->`).
pub fn html_comment(input: Span) -> IResult<Span, Span> {
    log::debug!(
        "Trying HTML comment at: {:?}",
        crate::logic::logger::safe_preview(input.fragment(), 40)
    );

    let start = input;

    // Optional leading spaces (0-3 allowed, just like other block elements)
    let (input, leading_spaces) = take_while(|c| c == ' ').parse(input)?;
    if leading_spaces.fragment().len() > 3 {
        return Err(nom::Err::Error(nom::error::Error::new(
            start,
            nom::error::ErrorKind::Tag,
        )));
    }

    // Parse <!-- ... -->
    let (input, (_, _content, _)) = (tag("<!--"), take_until("-->"), tag("-->")).parse(input)?;

    // Must be followed by newline or EOF
    let (input, _) = opt(line_ending).parse(input)?;

    // Return the whole comment including markers
    let consumed_len = input.location_offset() - start.location_offset();
    let comment_span = start.take(consumed_len);

    log::debug!(
        "Parsed HTML comment: {:?}",
        crate::logic::logger::safe_preview(comment_span.fragment(), 40)
    );
    Ok((input, comment_span))
}

// HTML Block Type 3: Processing Instructions
// Parses <?...?>
/// Parse an HTML processing instruction block (`<? ... ?>`).
pub fn html_processing_instruction(input: Span) -> IResult<Span, Span> {
    log::debug!(
        "Trying processing instruction at: {:?}",
        crate::logic::logger::safe_preview(input.fragment(), 40)
    );

    let start = input;

    // Optional leading spaces (0-3)
    let (input, leading_spaces) = take_while(|c| c == ' ').parse(input)?;
    if leading_spaces.fragment().len() > 3 {
        return Err(nom::Err::Error(nom::error::Error::new(
            start,
            nom::error::ErrorKind::Tag,
        )));
    }

    // Must start with <?
    let (input, _) = tag("<?").parse(input)?;

    // Consume until ?>
    let (input, _content) = take_until("?>").parse(input)?;
    let (input, _) = tag("?>").parse(input)?;

    // Must be followed by newline or EOF
    let (input, _) = opt(line_ending).parse(input)?;

    let consumed_len = input.location_offset() - start.location_offset();
    let pi_span = start.take(consumed_len);

    log::debug!(
        "Parsed processing instruction: {:?}",
        crate::logic::logger::safe_preview(pi_span.fragment(), 40)
    );
    Ok((input, pi_span))
}

// HTML Block Type 4: Declarations
// Parses <!X...> where X is ASCII letter
/// Parse an HTML declaration block (`<!DOCTYPE ...>`-style).
pub fn html_declaration(input: Span) -> IResult<Span, Span> {
    log::debug!(
        "Trying HTML declaration at: {:?}",
        crate::logic::logger::safe_preview(input.fragment(), 40)
    );

    let start = input;

    // Optional leading spaces (0-3)
    let (input, leading_spaces) = take_while(|c| c == ' ').parse(input)?;
    if leading_spaces.fragment().len() > 3 {
        return Err(nom::Err::Error(nom::error::Error::new(
            start,
            nom::error::ErrorKind::Tag,
        )));
    }

    // Must start with <! followed by ASCII letter
    let (input, _) = tag("<!").parse(input)?;

    // Next character must be ASCII letter
    let bytes = input.fragment().as_bytes();
    if bytes.is_empty() || !bytes[0].is_ascii_alphabetic() {
        return Err(nom::Err::Error(nom::error::Error::new(
            input,
            nom::error::ErrorKind::Alpha,
        )));
    }

    // Consume until >
    let (input, _content) = take_until(">").parse(input)?;
    let (input, _) = tag(">").parse(input)?;

    // Must be followed by newline or EOF
    let (input, _) = opt(line_ending).parse(input)?;

    let consumed_len = input.location_offset() - start.location_offset();
    let decl_span = start.take(consumed_len);

    log::debug!(
        "Parsed HTML declaration: {:?}",
        crate::logic::logger::safe_preview(decl_span.fragment(), 40)
    );
    Ok((input, decl_span))
}

// HTML Block Type 5: CDATA Sections
// Parses <![CDATA[...]]>
/// Parse an HTML CDATA block (`<![CDATA[ ... ]]>`).
pub fn html_cdata(input: Span) -> IResult<Span, Span> {
    log::debug!(
        "Trying CDATA section at: {:?}",
        crate::logic::logger::safe_preview(input.fragment(), 40)
    );

    let start = input;

    // Optional leading spaces (0-3)
    let (input, leading_spaces) = take_while(|c| c == ' ').parse(input)?;
    if leading_spaces.fragment().len() > 3 {
        return Err(nom::Err::Error(nom::error::Error::new(
            start,
            nom::error::ErrorKind::Tag,
        )));
    }

    // Must start with <![CDATA[
    let (input, _) = tag("<![CDATA[").parse(input)?;

    // Consume until ]]>
    let (input, _content) = take_until("]]>").parse(input)?;
    let (input, _) = tag("]]>").parse(input)?;

    // Must be followed by newline or EOF
    let (input, _) = opt(line_ending).parse(input)?;

    let consumed_len = input.location_offset() - start.location_offset();
    let cdata_span = start.take(consumed_len);

    log::debug!(
        "Parsed CDATA section: {:?}",
        crate::logic::logger::safe_preview(cdata_span.fragment(), 40)
    );
    Ok((input, cdata_span))
}

// HTML Block Type 1: Special Raw Content Tags (script, pre, style, textarea)
// These consume content until closing tag, can contain blank lines
/// Parse special raw HTML blocks (`script`, `pre`, `style`, `textarea`).
pub fn html_special_tag(input: Span) -> IResult<Span, Span> {
    log::debug!(
        "Trying special HTML tag at: {:?}",
        crate::logic::logger::safe_preview(input.fragment(), 40)
    );

    let start = input;

    // Optional leading spaces (0-3)
    let (input, leading_spaces) = take_while(|c| c == ' ').parse(input)?;
    if leading_spaces.fragment().len() > 3 {
        return Err(nom::Err::Error(nom::error::Error::new(
            start,
            nom::error::ErrorKind::Tag,
        )));
    }

    // Try to parse opening tag: <pre, <script, <style, <textarea (case-insensitive)
    let lower_input = input.fragment().to_lowercase();
    let tag_name = if lower_input.starts_with("<script") {
        "script"
    } else if lower_input.starts_with("<pre") {
        "pre"
    } else if lower_input.starts_with("<style") {
        "style"
    } else if lower_input.starts_with("<textarea") {
        "textarea"
    } else {
        return Err(nom::Err::Error(nom::error::Error::new(
            input,
            nom::error::ErrorKind::Tag,
        )));
    };

    // Check that after tag name there's space, tab, >, or EOL
    let tag_len = tag_name.len() + 1; // +1 for '<'
    if input.fragment().len() > tag_len {
        let next_char = input.fragment().chars().nth(tag_len);
        match next_char {
            Some(' ') | Some('\t') | Some('>') | Some('\n') | Some('\r') => {}
            Some(_) => {
                return Err(nom::Err::Error(nom::error::Error::new(
                    input,
                    nom::error::ErrorKind::Tag,
                )))
            }
            None => {} // EOF is OK
        }
    }

    // Build closing tag pattern (case-insensitive)
    let closing_tag = format!("</{}>", tag_name);

    // Consume until we find the closing tag
    let mut remaining = input;

    while !remaining.fragment().is_empty() {
        // Check if current position contains closing tag (case-insensitive)
        if remaining.fragment().to_lowercase().contains(&closing_tag) {
            // Find exact position of closing tag
            if let Some(pos) = remaining.fragment().to_lowercase().find(&closing_tag) {
                // Advance to after the closing tag
                let bytes_to_consume = pos + closing_tag.len();
                remaining = remaining.take_from(bytes_to_consume);
                break;
            }
        }

        // Advance to next line
        if let Some(newline_pos) = remaining.fragment().find('\n') {
            remaining = remaining.take_from(newline_pos + 1);
        } else {
            // No more newlines, consume rest
            remaining = remaining.take_from(remaining.fragment().len());
            break;
        }
    }

    // Return the entire block (from start to after closing tag or EOF)
    let consumed_len = remaining.location_offset() - start.location_offset();
    let block_span = start.take(consumed_len);

    log::debug!(
        "Parsed special HTML tag ({}): {:?}",
        tag_name,
        crate::logic::logger::safe_preview(block_span.fragment(), 40)
    );
    Ok((remaining, block_span))
}

// HTML Block Type 6: Standard Block Tags
// CommonMark spec lists these specific tags
const BLOCK_TAGS: &[&str] = &[
    "address",
    "article",
    "aside",
    "base",
    "basefont",
    "blockquote",
    "body",
    "caption",
    "center",
    "col",
    "colgroup",
    "dd",
    "details",
    "dialog",
    "dir",
    "div",
    "dl",
    "dt",
    "fieldset",
    "figcaption",
    "figure",
    "footer",
    "form",
    "frame",
    "frameset",
    "h1",
    "h2",
    "h3",
    "h4",
    "h5",
    "h6",
    "head",
    "header",
    "hr",
    "html",
    "iframe",
    "legend",
    "li",
    "link",
    "main",
    "menu",
    "menuitem",
    "nav",
    "noframes",
    "ol",
    "optgroup",
    "option",
    "p",
    "param",
    "search",
    "section",
    "summary",
    "table",
    "tbody",
    "td",
    "tfoot",
    "th",
    "thead",
    "title",
    "tr",
    "track",
    "ul",
];

/// Parse a standard block-tag HTML block (CommonMark type 6).
pub fn html_block_tag(input: Span) -> IResult<Span, Span> {
    log::debug!(
        "Trying block HTML tag at: {:?}",
        crate::logic::logger::safe_preview(input.fragment(), 40)
    );

    let start = input;

    // Optional leading spaces (0-3)
    let (input, leading_spaces) = take_while(|c| c == ' ').parse(input)?;
    if leading_spaces.fragment().len() > 3 {
        return Err(nom::Err::Error(nom::error::Error::new(
            start,
            nom::error::ErrorKind::Tag,
        )));
    }

    // Must start with < or </
    let (input, _opening) = alt((tag("</"), tag("<"))).parse(input)?;

    // Try to match one of the block tag names (case-insensitive)
    let lower_input = input.fragment().to_lowercase();
    let mut matched_tag: Option<&str> = None;

    for tag_name in BLOCK_TAGS {
        if lower_input.starts_with(tag_name) {
            // Check what follows the tag name
            let tag_len = tag_name.len();
            if lower_input.len() == tag_len {
                // Tag name at EOF is valid
                matched_tag = Some(tag_name);
                break;
            }

            let next_char = lower_input.chars().nth(tag_len);
            match next_char {
                // Valid: space, tab, >, newline, or / followed by >
                Some(' ') | Some('\t') | Some('>') | Some('\n') | Some('\r') => {
                    matched_tag = Some(tag_name);
                    break;
                }
                Some('/') => {
                    // Check if followed by >
                    if lower_input.len() > tag_len + 1
                        && lower_input.chars().nth(tag_len + 1) == Some('>')
                    {
                        matched_tag = Some(tag_name);
                        break;
                    }
                }
                _ => continue, // Not a match, try next tag
            }
        }
    }

    let tag_name = matched_tag.ok_or_else(|| {
        nom::Err::Error(nom::error::Error::new(input, nom::error::ErrorKind::Tag))
    })?;

    // User-friendly deviation from strict CommonMark:
    // If the opening line contains a matching closing tag for the same element,
    // treat this as a single-line HTML block (only that line), instead of a
    // multi-line HTML block that runs until the next blank line.
    //
    // Why:
    // - In editor UX, a single-line `<div>...</div>` is typically expected to not
    //   swallow subsequent Markdown lines into a raw HTML block.
    // - Keeping it as a block (not inline) avoids wrapping block-level tags
    //   inside `<p>` which would be invalid HTML.
    let first_line = if let Some(newline_pos) = start.fragment().find('\n') {
        &start.fragment()[..newline_pos]
    } else {
        start.fragment()
    };
    let first_line_lower = first_line.to_lowercase();
    let closing_tag = format!("</{}>", tag_name);
    let closes_on_same_line = first_line_lower.contains(&closing_tag);

    // Consume rest of current line
    let mut remaining = input;
    if let Some(newline_pos) = remaining.fragment().find('\n') {
        remaining = remaining.take_from(newline_pos + 1);
    } else {
        // No newline, consume rest
        remaining = remaining.take_from(remaining.fragment().len());
    }

    if closes_on_same_line {
        let consumed_len = remaining.location_offset() - start.location_offset();
        let block_span = start.take(consumed_len);

        log::debug!(
            "Parsed single-line block HTML tag ({}): {:?}",
            tag_name,
            crate::logic::logger::safe_preview(block_span.fragment(), 40)
        );
        return Ok((remaining, block_span));
    }

    // Type 6 blocks end at next blank line
    // Consume lines until blank line or EOF
    while !remaining.fragment().is_empty() {
        // Check if this line is blank
        let line_content = if let Some(newline_pos) = remaining.fragment().find('\n') {
            &remaining.fragment()[..newline_pos]
        } else {
            remaining.fragment()
        };

        // If line is blank (only whitespace), end here
        if line_content.trim().is_empty() {
            break;
        }

        // Not blank, consume this line
        if let Some(newline_pos) = remaining.fragment().find('\n') {
            remaining = remaining.take_from(newline_pos + 1);
        } else {
            // No more newlines, consume rest
            remaining = remaining.take_from(remaining.fragment().len());
            break;
        }
    }

    // Return block from start to current position (before blank line)
    let consumed_len = remaining.location_offset() - start.location_offset();
    let block_span = start.take(consumed_len);

    log::debug!(
        "Parsed block HTML tag ({}): {:?}",
        tag_name,
        crate::logic::logger::safe_preview(block_span.fragment(), 40)
    );
    Ok((remaining, block_span))
}

// HTML Block Type 7: Complete Tags
// Must be a complete open or closing tag on a line by itself (followed only by spaces/tabs)
// Cannot interrupt paragraphs (must be handled specially by caller)
// IMPORTANT: This must validate that the tag is well-formed per CommonMark spec
/// Parse a complete-tag HTML block line per CommonMark type 7 rules.
pub fn html_complete_tag(input: Span) -> IResult<Span, Span> {
    log::debug!(
        "Trying complete HTML tag at: {:?}",
        crate::logic::logger::safe_preview(input.fragment(), 40)
    );

    let start = input;

    // Optional leading spaces (0-3)
    let (input, leading_spaces) = take_while(|c| c == ' ').parse(input)?;
    if leading_spaces.fragment().len() > 3 {
        return Err(nom::Err::Error(nom::error::Error::new(
            start,
            nom::error::ErrorKind::Tag,
        )));
    }

    // Try to parse complete tag (open or closing)
    let line_content = if let Some(newline_pos) = input.fragment().find('\n') {
        &input.fragment()[..newline_pos]
    } else {
        input.fragment()
    };

    // Must start with < and contain >
    if !line_content.starts_with('<') || !line_content.contains('>') {
        return Err(nom::Err::Error(nom::error::Error::new(
            input,
            nom::error::ErrorKind::Tag,
        )));
    }

    // Find the > position
    let gt_pos = line_content.find('>').unwrap();

    // After >, rest of line must be only spaces/tabs
    let after_tag = &line_content[(gt_pos + 1)..];
    if !after_tag.chars().all(|c| c == ' ' || c == '\t') {
        return Err(nom::Err::Error(nom::error::Error::new(
            input,
            nom::error::ErrorKind::Tag,
        )));
    }

    // Check if it's a closing tag or opening tag
    let tag_content = &line_content[..=gt_pos];
    let is_closing = tag_content.starts_with("</");

    if is_closing {
        // Closing tag: </tagname>
        if !tag_content.starts_with("</") || tag_content.contains(' ') || tag_content.contains('\t')
        {
            return Err(nom::Err::Error(nom::error::Error::new(
                input,
                nom::error::ErrorKind::Tag,
            )));
        }

        // Extract tag name (between </ and >)
        let tag_name = &tag_content[2..(tag_content.len() - 1)];

        // Tag name must start with ASCII letter
        if tag_name.is_empty() || !tag_name.chars().next().unwrap().is_ascii_alphabetic() {
            return Err(nom::Err::Error(nom::error::Error::new(
                input,
                nom::error::ErrorKind::Tag,
            )));
        }

        // Rest of tag name must be alphanumeric or hyphen
        if !tag_name
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-')
        {
            return Err(nom::Err::Error(nom::error::Error::new(
                input,
                nom::error::ErrorKind::Tag,
            )));
        }
    } else {
        // Opening tag: <tagname ...>
        // Exclude special tags (pre, script, style, textarea) - those are Type 1
        let lower_tag = tag_content.to_lowercase();
        if lower_tag.starts_with("<pre")
            || lower_tag.starts_with("<script")
            || lower_tag.starts_with("<style")
            || lower_tag.starts_with("<textarea")
        {
            return Err(nom::Err::Error(nom::error::Error::new(
                input,
                nom::error::ErrorKind::Tag,
            )));
        }

        // Extract tag name (from < to first space, /, or >)
        let after_lt = &tag_content[1..];
        let tag_name_end = after_lt
            .find([' ', '\t', '/', '>'])
            .unwrap_or(after_lt.len());
        let tag_name = &after_lt[..tag_name_end];

        // Tag name must start with ASCII letter
        if tag_name.is_empty() || !tag_name.chars().next().unwrap().is_ascii_alphabetic() {
            return Err(nom::Err::Error(nom::error::Error::new(
                input,
                nom::error::ErrorKind::Tag,
            )));
        }

        // Rest of tag name must be alphanumeric or hyphen
        if !tag_name
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-')
        {
            return Err(nom::Err::Error(nom::error::Error::new(
                input,
                nom::error::ErrorKind::Tag,
            )));
        }

        // If there are attributes, validate them
        let after_tag_name = &after_lt[tag_name_end..];
        if !after_tag_name.is_empty()
            && !after_tag_name.starts_with('>')
            && !after_tag_name.starts_with("/>")
        {
            let trimmed = after_tag_name.trim_start();
            if trimmed.starts_with('/') && trimmed.len() == 2 && trimmed == "/>" {
                // Self-closing tag, OK
            } else {
                // Validate attribute format
                if after_tag_name.contains("*") || after_tag_name.contains("#") {
                    return Err(nom::Err::Error(nom::error::Error::new(
                        input,
                        nom::error::ErrorKind::Tag,
                    )));
                }

                if !after_tag_name.starts_with(' ') && !after_tag_name.starts_with('\t') {
                    return Err(nom::Err::Error(nom::error::Error::new(
                        input,
                        nom::error::ErrorKind::Tag,
                    )));
                }

                // Check for common malformed patterns
                if after_tag_name.contains("'") {
                    let parts: Vec<&str> = after_tag_name.split('\'').collect();
                    for (i, part) in parts.iter().enumerate().skip(1) {
                        if i % 2 == 0 && !part.is_empty() {
                            let first_char = part.chars().next().unwrap();
                            if first_char.is_alphabetic() {
                                return Err(nom::Err::Error(nom::error::Error::new(
                                    input,
                                    nom::error::ErrorKind::Tag,
                                )));
                            }
                        }
                    }
                }

                if after_tag_name.contains("\\\"") {
                    return Err(nom::Err::Error(nom::error::Error::new(
                        input,
                        nom::error::ErrorKind::Tag,
                    )));
                }

                if !trimmed.starts_with(char::is_alphabetic)
                    && !trimmed.starts_with('/')
                    && !trimmed.starts_with('>')
                {
                    return Err(nom::Err::Error(nom::error::Error::new(
                        input,
                        nom::error::ErrorKind::Tag,
                    )));
                }
            }
        }
    }

    // Consume current line
    let mut remaining = input;
    if let Some(newline_pos) = remaining.fragment().find('\n') {
        remaining = remaining.take_from(newline_pos + 1);
    } else {
        remaining = remaining.take_from(remaining.fragment().len());
    }

    // Type 7 blocks end at next blank line
    while !remaining.fragment().is_empty() {
        let line_content = if let Some(newline_pos) = remaining.fragment().find('\n') {
            &remaining.fragment()[..newline_pos]
        } else {
            remaining.fragment()
        };

        if line_content.trim().is_empty() {
            break;
        }

        if let Some(newline_pos) = remaining.fragment().find('\n') {
            remaining = remaining.take_from(newline_pos + 1);
        } else {
            remaining = remaining.take_from(remaining.fragment().len());
            break;
        }
    }

    // Return block from start to current position (before blank line)
    let consumed_len = remaining.location_offset() - start.location_offset();
    let block_span = start.take(consumed_len);

    log::debug!(
        "Parsed complete HTML tag: {:?}",
        crate::logic::logger::safe_preview(block_span.fragment(), 40)
    );
    Ok((remaining, block_span))
}

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

    #[test]
    fn smoke_test_html_comment() {
        let input = Span::new("<!-- Comment -->");
        let result = html_comment(input);
        assert!(result.is_ok());
        let (_, content) = result.unwrap();
        assert!(content.fragment().contains("Comment"));
    }

    #[test]
    fn smoke_test_html_processing_instruction() {
        let input = Span::new("<?xml version=\"1.0\"?>");
        let result = html_processing_instruction(input);
        assert!(result.is_ok());
        let (_, content) = result.unwrap();
        assert!(content.fragment().contains("xml"));
    }

    #[test]
    fn smoke_test_html_declaration() {
        let input = Span::new("<!DOCTYPE html>");
        let result = html_declaration(input);
        assert!(result.is_ok());
        let (_, content) = result.unwrap();
        assert!(content.fragment().contains("DOCTYPE"));
    }

    #[test]
    fn smoke_test_html_cdata() {
        let input = Span::new("<![CDATA[data here]]>");
        let result = html_cdata(input);
        assert!(result.is_ok());
        let (_, content) = result.unwrap();
        assert!(content.fragment().contains("data here"));
    }

    #[test]
    fn smoke_test_html_special_tag_script() {
        let input = Span::new("<script>\nalert('hi');\n</script>");
        let result = html_special_tag(input);
        assert!(result.is_ok());
        let (_, content) = result.unwrap();
        assert!(content.fragment().contains("alert"));
    }

    #[test]
    fn smoke_test_html_block_tag() {
        let input = Span::new("<div>\nContent\n</div>");
        let result = html_block_tag(input);
        assert!(result.is_ok());
        let (_, content) = result.unwrap();
        assert!(content.fragment().contains("Content"));
    }

    #[test]
    fn smoke_test_html_complete_tag_open() {
        let input = Span::new("<div>");
        let result = html_complete_tag(input);
        assert!(result.is_ok());
    }

    #[test]
    fn smoke_test_html_complete_tag_close() {
        let input = Span::new("</div>");
        let result = html_complete_tag(input);
        assert!(result.is_ok());
    }

    #[test]
    fn smoke_test_html_comment_fails_without_closing() {
        let input = Span::new("<!-- Comment");
        let result = html_comment(input);
        assert!(result.is_err());
    }

    #[test]
    fn smoke_test_html_block_tag_ends_at_blank() {
        let input = Span::new("<div>\nLine 1\n\nAfter blank");
        let result = html_block_tag(input);
        assert!(result.is_ok());
        let (remaining, content) = result.unwrap();
        assert!(content.fragment().contains("Line 1"));
        assert!(remaining.fragment().trim_start().starts_with("After blank"));
    }

    #[test]
    fn smoke_test_html_block_tag_single_line_closed_does_not_swallow_next_line() {
        let input = Span::new("<div>html</div>\n`www.example.com`\n");
        let result = html_block_tag(input);
        assert!(result.is_ok());
        let (remaining, content) = result.unwrap();

        assert!(content.fragment().contains("<div>html</div>"));
        assert!(
            remaining.fragment().starts_with("`www.example.com`"),
            "following line should remain to be parsed as Markdown"
        );
    }
}