blade-syntax 0.1.0

Lossless parser for Laravel Blade templates
Documentation
//! HTML tags, attributes and values, and how they mix with Blade.

use blade_syntax::{SyntaxKind, SyntaxNode, debug::sexp, parse};

fn tree(t: &str) -> String {
    let p = parse(t);
    assert_eq!(p.syntax().to_string(), t, "parse must be lossless");
    sexp(&p.syntax())
}

fn errors(t: &str) -> Vec<String> {
    parse(t)
        .errors()
        .iter()
        .map(|e| e.message.clone())
        .collect()
}

fn first(node: &SyntaxNode, kind: SyntaxKind) -> SyntaxNode {
    node.descendants()
        .find(|n| n.kind() == kind)
        .unwrap_or_else(|| panic!("no {kind:?} in {}", sexp(node)))
}

#[test]
fn parses_start_and_end_tags() {
    assert_eq!(
        tree(r#"<a href="x">hi</a>"#),
        r#"(ROOT (TAG "<" "a" " " (ATTRIBUTE "href" "=" (ATTR_VALUE "\"" "x" "\"")) ">") "hi" (END_TAG "</" "a" ">"))"#
    );
}

#[test]
fn parses_component_with_bound_attribute() {
    assert_eq!(
        tree(r#"<x-alert :type="$t" />"#),
        r#"(ROOT (TAG "<" "x-alert" " " (ATTRIBUTE ":type" "=" (ATTR_VALUE "\"" "$t" "\"")) " " "/>"))"#
    );
}

#[test]
fn parses_unquoted_value_and_bare_attribute() {
    assert_eq!(
        tree("<input n=7 disabled/>"),
        r#"(ROOT (TAG "<" "input" " " (ATTRIBUTE "n" "=" "7") " " (ATTRIBUTE "disabled") "/>"))"#
    );
}

#[test]
fn blade_inside_tags() {
    let t = r#"<option {{ $attributes->merge(['class' => 'a']) }} @if($s) selected @endif {{-- note --}} value="{{ $v }}">"#;
    assert!(errors(t).is_empty(), "{:?}", errors(t));
    let root = parse(t).syntax();
    let tag = first(&root, SyntaxKind::TAG);
    let child_nodes: Vec<_> = tag.children().map(|n| n.kind()).collect();
    assert!(child_nodes.contains(&SyntaxKind::ECHO), "{}", sexp(&tag));
    assert!(child_nodes.contains(&SyntaxKind::BLOCK), "{}", sexp(&tag));
    assert!(
        tag.children_with_tokens()
            .any(|c| c.kind() == SyntaxKind::COMMENT),
        "{}",
        sexp(&tag)
    );
    let value = first(&tag, SyntaxKind::ATTR_VALUE);
    assert!(
        value.children().any(|n| n.kind() == SyntaxKind::ECHO),
        "{}",
        sexp(&value)
    );
}

#[test]
fn tags_and_blocks_interleave() {
    let t = r#"@if($a) <a href="x"> @else <span> @endif"#;
    assert!(errors(t).is_empty(), "{:?}", errors(t));
    assert_eq!(
        errors("<div @if($a) class=\"x\">"),
        ["unclosed @if: expected @endif"]
    );
}

#[test]
fn html_comment_directives_pair_with_outside() {
    let t = "<!-- @if($a) --> x <!-- @endif -->";
    assert!(errors(t).is_empty(), "{:?}", errors(t));
    let blocks = parse(t)
        .syntax()
        .descendants()
        .filter(|n| n.kind() == SyntaxKind::BLOCK)
        .count();
    assert_eq!(blocks, 1);
}

#[test]
fn reports_unclosed_tag_and_value() {
    assert_eq!(
        errors("<div class=\"x"),
        [
            "unclosed attribute value: expected \"",
            "unclosed tag <div>: expected >"
        ]
    );
}

fn count(t: &str, kind: SyntaxKind) -> usize {
    parse(t)
        .syntax()
        .descendants()
        .filter(|n| n.kind() == kind)
        .count()
}

// Blade can split a tag across branches: each branch writes a different start of the
// tag, and the shared rest follows `@endif`.
#[test]
fn tag_split_across_branches() {
    let t = "@if ($url)\n<a href=\"{{ $url }}\"\n@else\n<div\n@endif\n class=\"card\">";
    assert!(errors(t).is_empty(), "{:?}", errors(t));
    assert_eq!(count(t, SyntaxKind::TAG), 2, "{}", tree(t));
    assert_eq!(count(t, SyntaxKind::BLOCK), 1, "{}", tree(t));
}

#[test]
fn block_closes_inside_a_tag_it_opened() {
    let t = "@if($a) <div @endif>";
    assert!(errors(t).is_empty(), "{:?}", errors(t));
}

#[test]
fn tag_opened_inside_an_unfinished_tag() {
    assert_eq!(errors("<a <b>"), ["unclosed tag <a>: expected >"]);
    assert_eq!(count("<a <b>", SyntaxKind::TAG), 2);
}