From c7c3798c78c6cbc2285da29207d8752f9f03ae9e Mon Sep 17 00:00:00 2001 From: Sahin Vardar Date: Sat, 26 Oct 2024 16:00:11 +0200 Subject: [PATCH] WIP --- src/print-html.ts | 77 ++++++++++++++++++++++++++---------- src/tests/print-html.test.ts | 76 +++++++++++++++++------------------ 2 files changed, 94 insertions(+), 59 deletions(-) diff --git a/src/print-html.ts b/src/print-html.ts index 7f2e51c..6a012ac 100644 --- a/src/print-html.ts +++ b/src/print-html.ts @@ -36,8 +36,8 @@ export abstract class BaseHtmlNodePrinter { } class FragmentPrinter extends BaseHtmlNodePrinter { - enter(_: TemplateNode, __: TemplateNode, ___: PrinterContext) {} - leave(_: TemplateNode, __: TemplateNode, ___: PrinterContext) {} + enter(_: any, __: any, ___: PrinterContext) {} + leave(_: any, __: any, ___: PrinterContext) {} } class ElementPrinter extends BaseHtmlNodePrinter { @@ -122,6 +122,44 @@ class ElementPrinter extends BaseHtmlNodePrinter { return `{...${generate(attribute.expression, context.indent)}}`; } + if (attribute.type === 'EventHandler') { + if (attribute.expression) { + return `on:${attribute.name}={${generate(attribute.expression, context.indent)}}`; + } + + return `${attribute.name}`; + } + + if (attribute.type === 'Binding') { + return `bind:${attribute.name}={${generate(attribute.expression, context.indent)}}`; + } + + if (attribute.type === 'Class') { + return `class:${attribute.name}={${generate(attribute.expression, context.indent)}}`; + } + + if (attribute.type === 'StyleDirective') { + if (attribute.value === true) { + return `style:${attribute.name}`; + } + + const [value] = attribute.value; + + if (value.type === 'Text') { + return `style:${attribute.name}="${value.data}"`; + } + + if (value.type === 'ExpressionTag') { + return `style:${attribute.name}={${generate(value.expression, context.indent)}}`; + } + + if(value.type === 'MustacheTag') { + return `style:${attribute.name}={${generate(value.expression, context.indent)}}`; + } + + + } + // if (attribute?.expression) { // return `${attributeName}={${generate(attribute.expression, context.indent)}}`; // } @@ -171,13 +209,6 @@ class ElementPrinter extends BaseHtmlNodePrinter { } } -class AttributePrinter extends BaseHtmlNodePrinter { - enter(node: TemplateNode, parent: TemplateNode, context: PrinterContext) { - context._this.skip(); - } - leave(_: TemplateNode, __: TemplateNode, ___: PrinterContext) {} -} - class TextPrinter extends BaseHtmlNodePrinter { enter(node: AST.Text, _: any, context: PrinterContext) { const { write } = context; @@ -402,7 +433,7 @@ const PRINTERS: PrinterCollection = { Text: new TextPrinter(), MustacheTag: new MustacheTagPrinter(), Comment: new CommentPrinter(), - Slot: new ElementPrinter() + Slot: new ElementPrinter(), // SlotTemplate: new ElementPrinter(), // Title: new ElementPrinter(), // Slot: new ElementPrinter(), @@ -411,18 +442,16 @@ const PRINTERS: PrinterCollection = { // Window: new ElementPrinter(), // Document: new ElementPrinter(), // Body: new ElementPrinter(), - - // Attribute: new AttributePrinter(), - // Text: new TextPrinter(), - // EventHandler: NoOp, - // Identifier: NoOp, - // Binding: NoOp, - // Class: NoOp, - // StyleDirective: NoOp, - // Action: NoOp, - // Transition: NoOp, - // Animation: NoOp, + Attribute: NoOp, + EventHandler: NoOp, + Identifier: NoOp, + Binding: NoOp, + Class: NoOp, + StyleDirective: NoOp, + Action: NoOp, + Transition: NoOp, + Animation: NoOp // Comment: new CommentPrinter(), // IfBlock: new IfBlockPrinter(), // ElseBlock: new ElseBlockPrinter(), @@ -470,6 +499,9 @@ export default function printHtml(params: PrintHtmlParams) { walk(copy, { enter: function (node: SvelteNode | LegacySvelteNode, parent: SvelteNode | LegacySvelteNode) { + if (node.skip === true) { + return; + } const printer = printers[node.type]; if (printer === undefined) { throw new Error(`Could not find printer for ${node.type}`); @@ -484,6 +516,9 @@ export default function printHtml(params: PrintHtmlParams) { nestingLevel++; }, leave: function (node: any, parent: any) { + if (node.skip === true) { + return; + } const printer = printers[node.type]; printer.leave(node, parent, { _this: this, diff --git a/src/tests/print-html.test.ts b/src/tests/print-html.test.ts index 062a462..216f292 100644 --- a/src/tests/print-html.test.ts +++ b/src/tests/print-html.test.ts @@ -43,48 +43,48 @@ describe('Attributes', () => { test('inline component element with attribute', () => testHtmlPrinter('')); }); -// describe('Element directives', () => { -// test('on:eventname', () => testHtmlPrinter('')); -// test('bind:property', () => testHtmlPrinter('', '')); -// test('bind:group', () => testHtmlPrinter('')); +describe('Element directives', () => { + test('on:eventname', () => testHtmlPrinter('')); + test('bind:property', () => testHtmlPrinter('', '')); + test('bind:group', () => testHtmlPrinter('')); -// test('bind:this', () => testHtmlPrinter('')); + test('bind:this', () => testHtmlPrinter('')); -// test('class:name', () => { -// testHtmlPrinter('
'); -// testHtmlPrinter('
', '
'); -// }); - -// test('style:property', () => { -// testHtmlPrinter('
'); -// testHtmlPrinter('
'); -// testHtmlPrinter('
'); -// }); - -// test('use:action', () => { -// testHtmlPrinter('
'); -// testHtmlPrinter('
'); -// }); - -// test('transition:fn', () => { -// testHtmlPrinter('
'); -// testHtmlPrinter('
'); -// }); - -// test('in/out:fn', () => { -// testHtmlPrinter('
flies in, fades out
'); -// testHtmlPrinter('
flies in, fades out
'); -// }); + test('class:name', () => { + testHtmlPrinter('
'); + testHtmlPrinter('
', '
'); + }); -// test('animate:fn', () => { -// testHtmlPrinter('
  • {item}
  • '); -// testHtmlPrinter('
    {item}
    '); -// }); + test('style:property', () => { + testHtmlPrinter('
    '); + testHtmlPrinter('
    '); + testHtmlPrinter('
    '); + }); -// test('let:variable', () => { -// testHtmlPrinter(''); -// }); -// }); + // test('use:action', () => { + // testHtmlPrinter('
    '); + // testHtmlPrinter('
    '); + // }); + + // test('transition:fn', () => { + // testHtmlPrinter('
    '); + // testHtmlPrinter('
    '); + // }); + + // test('in/out:fn', () => { + // testHtmlPrinter('
    flies in, fades out
    '); + // testHtmlPrinter('
    flies in, fades out
    '); + // }); + + // test('animate:fn', () => { + // testHtmlPrinter('
  • {item}
  • '); + // testHtmlPrinter('
    {item}
    '); + // }); + + // test('let:variable', () => { + // testHtmlPrinter(''); + // }); +}); // describe('Template', () => { // test('{#if ...}', () => {