import { describe, it, expect } from 'vitest'; import { toLiquidHtmlAST } from '../ast'; import type { DocumentNode, LiquidHtmlNode } from '../ast'; import { NodeTypes } from '../types'; const PAIRING = { mode: 'strict', allowUnclosedDocumentNode: false, pairDanglingCloseTags: true, } as const; const STRICT = { mode: 'strict', allowUnclosedDocumentNode: false } as const; /** One line per node: its type, its plain name, and whether it is left open. */ function outline(nodes: LiquidHtmlNode[], depth = 0): string[] { const lines: string[] = []; for (const node of nodes) { if (node.type === NodeTypes.TextNode) continue; const pad = ' '.repeat(depth); if (node.type === NodeTypes.HtmlElement) { const name = node.name.map((s) => ('value' in s ? s.value : '?')).join(''); const open = node.blockEndPosition.start === node.blockEndPosition.end ? ' (open)' : ''; lines.push(`${pad}<${name}>${open}`); lines.push(...outline(node.children, depth + 1)); } else if (node.type === NodeTypes.HtmlDanglingMarkerClose) { const name = node.name.map((s) => ('value' in s ? s.value : '?')).join(''); lines.push(`${pad} (dangling)`); } else if (node.type === NodeTypes.LiquidTag) { lines.push(`${pad}{% ${node.name} %}`); if (Array.isArray(node.children)) lines.push(...outline(node.children, depth + 1)); } else if (node.type === NodeTypes.LiquidBranch) { lines.push(`${pad}branch ${node.name ?? '(first)'}`); lines.push(...outline(node.children, depth + 1)); } else if (node.type === NodeTypes.LiquidVariableOutput) { lines.push(`${pad}{{ }}`); } } return lines; } function parseOutline(source: string): string[] { const ast: DocumentNode = toLiquidHtmlAST(source, PAIRING); return outline(ast.children); } describe('pairDanglingCloseTags', () => { const LAYOUT = [ '', '
', '{% if a %}', '
', '{% elsif b %}', '
', '{% else %}', '
', '{% endif %}', '{{ content_for_layout }}', '
', '
', '', '', ].join('\n'); it('throws on an element opened in every branch and closed after endif, without the option', () => { expect(() => toLiquidHtmlAST(LAYOUT, STRICT)).toThrow( "Attempting to close HtmlElement 'div' before HtmlElement 'body' was closed", ); }); it('parses the close as a dangling marker inside the enclosing element', () => { expect(parseOutline(LAYOUT)).toEqual([ '', '
', ' {% if %}', ' branch (first)', '
(open)', ' branch elsif', '
(open)', ' branch else', '
(open)', ' {{ }}', '
(dangling)', '
', ]); }); it('parses a close at the document level the same way', () => { const source = '{% if a %}
{% else %}
{% endif %}hi
'; expect(() => toLiquidHtmlAST(source, STRICT)).toThrow( "Attempting to close HtmlElement 'div' before it was opened", ); expect(parseOutline(source)).toEqual([ '{% if %}', ' branch (first)', '
(open)', ' branch else', '
(open)', '
(dangling)', ]); }); it('parses a close in a loop body the same way', () => { const source = '
    {% for i in (1..2) %}{% if s %}
    {% else %}
    {% endif %}{{ i }}
    {% endfor %}
'; expect(() => toLiquidHtmlAST(source, STRICT)).toThrow(); expect(parseOutline(source)).toEqual([ '
    ', ' {% for %}', ' branch (first)', ' {% if %}', ' branch (first)', '
    (open)', ' branch else', '
    (open)', ' {{ }}', '
    (dangling)', ]); }); it('pairs one close per element every branch leaves open', () => { const source = '
    {% if a %}

    {% else %}

    {% endif %}x

    '; expect(parseOutline(source)).toEqual([ '
    ', ' {% if %}', ' branch (first)', '
    (open)', '

    (open)', ' branch else', '

    (open)', '

    (open)', '

    (dangling)', '
    (dangling)', ]); }); it('counts a case by its when and else branches, not the text before the first when', () => { const source = '
    {% case t %}{% when \'a\' %}
    {% else %}
    {% endcase %}x
    '; expect(parseOutline(source)).toContain('
    (dangling)'); }); it('does not count a conditional that leaves the element open on only some paths', () => { // `{% if c %}
    {% endif %}` has an empty path, so the `
    ` after it // keeps closing `
    `'s sibling structure as before, and this still throws. const source = '
    {% if c %}
    {% endif %}x
    '; expect(() => toLiquidHtmlAST(source, PAIRING)).toThrow(); }); it('keeps closing the enclosing element once the branch-opened elements are closed', () => { const source = '
    {% if a %}
    {% else %}
    {% endif %}x

    after

    '; expect(parseOutline(source)).toEqual([ '
    ', ' {% if %}', ' branch (first)', '
    (open)', ' branch else', '
    (open)', '
    (dangling)', '

    ', ]); }); it('does not let an unmatched earlier close cancel an element opened later', () => { const source = '{% if a %}

    {% else %}
    {% endif %}{% if b %}
    {% else %}
    {% endif %}x
    '; expect(parseOutline(source)).toEqual([ '{% if %}', ' branch (first)', '
    (dangling)', ' branch else', '
(dangling)', '{% if %}', ' branch (first)', '
(open)', ' branch else', '
(open)', '
(dangling)', ]); }); it('does not convert a close that crosses the innermost branch-opened element', () => { const source = '{% if a %}
{% else %}
{% endif %}
'; expect(() => toLiquidHtmlAST(source, PAIRING)).toThrow( "Attempting to close HtmlElement 'section' before it was opened", ); }); it('does not convert a close for an element a branch-opened element sits inside of', () => { // The innermost open is `

`, so `

` is not taken from the branches and // closes `
`'s `
` sibling structure as before, which throws here. const source = '
{% if a %}

{% else %}

{% endif %}x

'; expect(() => toLiquidHtmlAST(source, PAIRING)).toThrow(); }); it('reads each node of a scope once, however many closes the scope meets', () => { const count = 2000; const source = `
${'{% if a %}
{% else %}
{% endif %}'.repeat(count)}x${'
'.repeat(count)}
`; const started = performance.now(); const ast = toLiquidHtmlAST(source, PAIRING); expect(performance.now() - started).toBeLessThan(2000); const main = ast.children[0] as { children: LiquidHtmlNode[] }; const closes = main.children.filter((n) => n.type === NodeTypes.HtmlDanglingMarkerClose); expect(closes).toHaveLength(count); }); it('leaves a conditional open and close pair to the existing branch markers', () => { const source = '
{% if c %}{% endif %}x{% if c %}{% endif %}
'; expect(toLiquidHtmlAST(source, PAIRING)).toEqual(toLiquidHtmlAST(source, STRICT)); }); it('does not change the AST of a template the strict parse accepts', () => { const sources = [ '
a
', '{% if a %}
{% else %}
{% endif %}', '
{% if a %}

{% else %}

{% endif %}
', '{% for x in y %}
  • {{ x }}
  • {% endfor %}', ]; for (const source of sources) { expect(toLiquidHtmlAST(source, PAIRING)).toEqual(toLiquidHtmlAST(source, STRICT)); } }); });