"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = __importDefault(require("assert"));
const parseHtml_1 = require("../../../src/lib/documents/parseHtml");
const documents_1 = require("../../../src/lib/documents");
describe('parseHtml', () => {
    const testRootElements = (document) => {
        assert_1.default.deepStrictEqual(document.roots.map((r) => r.tag), ['Foo', 'style']);
    };
    it('ignore arrow inside moustache', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`<Foo on:click={() => console.log('ya!!!')} />
                <style></style>`));
    });
    it('ignore greater than operator inside moustache', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`<Foo checked={a > 1} />
                <style></style>`));
    });
    it('ignore less than operator inside moustache', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`<Foo checked={a < 1} />
                <style></style>`));
    });
    it('ignore binary operator inside @const', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`{#if foo}
                  {@const bar = 1 << 2}
                  <Foo  />
                {/if}
                <style></style>`));
    });
    it('ignore less than operator inside control flow moustache', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`<Foo>
                    {#if 1 < 2 && innWidth <= 700}
                        <Foo>
                            <SelfClosing />
                        </Foo>
                        <div>hi</div>
                    {/if}
                </Foo>
                <style></style>`));
    });
    it('ignore less than operator inside moustache with tag not self closed', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`<Foo checked={a < 1}>
                </Foo>
                <style></style>`));
    });
    it('parse baseline html', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`<Foo checked />
                <style></style>`));
    });
    it('parse baseline html with moustache', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`<Foo checked={a} />
                <style></style>`));
    });
    it('parse baseline html with control flow moustache', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`<Foo>
                    {#if true}
                        foo
                    {/if}
                </Foo>
                <style></style>`));
    });
    it('parse baseline html with possibly un-closed start tag', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`<Foo checked={a}
                <style></style>`));
    });
    it('can parse html with destructured snippet and type annotation', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`{#snippet foo({ props }: { props?: Record<string, unknown> })}{/snippet}
                <Foo checked={a} />
                <style></style>`));
    });
    it('can parse html with destructured event handler', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`<Foo on:click={({ detail }) => handleClick(detail)} />
                <style></style>`));
    });
    it('can parse html with object literal in event handler', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`<Foo on:click={(e) => { if ({ x }.x <= 0) {} }} />
                <style></style>`));
    });
    it('ignore { inside string', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`<Foo title={foo("}") > 1} />
                <style></style>`));
    });
    it('ignore } inside template string', () => {
        testRootElements((0, parseHtml_1.parseHtml)('<Foo title={foo(`${a}}`) < 1} />\n<style></style>'));
    });
    it('ignore } inside template string (nested)', () => {
        testRootElements((0, parseHtml_1.parseHtml)('<Foo title={foo(`${hi(`${a}}`)}`) < 1} />\n<style></style>'));
    });
    it('parse attribute short-hand', () => {
        const document = (0, parseHtml_1.parseHtml)(`<Foo disabled {ariaLabel} />
            <style></style>`);
        const fooNode = document.roots.find((r) => r.tag === 'Foo');
        assert_1.default.ok(fooNode);
        const ariaLabelValue = fooNode.attributes?.['ariaLabel'];
        assert_1.default.strictEqual(ariaLabelValue, '{ariaLabel}');
    });
    it('parse expression', () => {
        const document = (0, parseHtml_1.parseHtml)(`<Foo disabled ariaLabel={""} />
            <style></style>`);
        const fooNode = document.roots.find((r) => r.tag === 'Foo');
        assert_1.default.ok(fooNode);
        const ariaLabelValue = fooNode.attributes?.['ariaLabel'];
        assert_1.default.strictEqual(ariaLabelValue, '{""}');
    });
    it('parse expression with spaces around equals', () => {
        const document = (0, parseHtml_1.parseHtml)(`<Foo disabled ariaLabel = {""} />`);
        const fooNode = document.roots.find((r) => r.tag === 'Foo');
        assert_1.default.ok(fooNode);
        const ariaLabelValue = fooNode.attributes?.['ariaLabel'];
        assert_1.default.strictEqual(ariaLabelValue, '{""}');
    });
    it('parse attributes with interpolation', () => {
        const document = (0, parseHtml_1.parseHtml)(`<Foo ariaLabel="a{b > c ? "": ""} c" />`);
        const fooNode = document.roots.find((r) => r.tag === 'Foo');
        assert_1.default.ok(fooNode);
        const ariaLabelValue = fooNode.attributes?.['ariaLabel'];
        assert_1.default.strictEqual(ariaLabelValue, `"a{b > c ? "": ""} c"`);
    });
    it('parse comments in attributes', () => {
        testRootElements((0, parseHtml_1.parseHtml)(`<Foo
                // comment/>
                checked={a}
                /* another comment/> <div>ignore me</div> */
                hello="bar"
                />
                <style></style>`));
    });
});
describe('getAttributeContextAtPosition', () => {
    it('extract attribute name', () => {
        const document = setupDocument('<div disabled />');
        const result = (0, parseHtml_1.getAttributeContextAtPosition)(document, { line: 0, character: 6 });
        assert_1.default.strictEqual(result?.name, 'disabled');
        assert_1.default.strictEqual(result.inValue, false);
    });
    it('extract attribute after interpolated attribute', () => {
        const document = setupDocument('<Foo a={a > b} b= />');
        const result = (0, parseHtml_1.getAttributeContextAtPosition)(document, { line: 0, character: 17 });
        assert_1.default.strictEqual(result?.name, 'b');
        assert_1.default.strictEqual(result.inValue, true);
    });
    it('extract attribute value range', () => {
        const document = setupDocument('<Foo a="a > b" />');
        const result = (0, parseHtml_1.getAttributeContextAtPosition)(document, { line: 0, character: 8 });
        assert_1.default.strictEqual(result?.name, 'a');
        assert_1.default.strictEqual(result.inValue, true);
        assert_1.default.deepStrictEqual(result?.valueRange, [8, 13]);
    });
    function setupDocument(content) {
        return new documents_1.Document('file:///test/Test.svelte', content);
    }
});
//# sourceMappingURL=parseHtml.test.js.map