From 3301aa7cc57988505f5cfef1ea93db822a63f58a Mon Sep 17 00:00:00 2001 From: zackad Date: Fri, 17 Jan 2025 14:24:26 +0700 Subject: [PATCH 1/3] test: add case for twig tag as html element attribute --- tests/Element/__snapshots__/attribute_twig_tag.snap.twig | 9 +++++++++ tests/Element/attribute_twig_tag.twig | 4 ++++ tests/Element/jsfmt.spec.js | 7 +++++++ 3 files changed, 20 insertions(+) create mode 100644 tests/Element/__snapshots__/attribute_twig_tag.snap.twig create mode 100644 tests/Element/attribute_twig_tag.twig diff --git a/tests/Element/__snapshots__/attribute_twig_tag.snap.twig b/tests/Element/__snapshots__/attribute_twig_tag.snap.twig new file mode 100644 index 0000000..843528d --- /dev/null +++ b/tests/Element/__snapshots__/attribute_twig_tag.snap.twig @@ -0,0 +1,9 @@ +
+ +{% block class %} + class="hidden" +{% endblock %} diff --git a/tests/Element/attribute_twig_tag.twig b/tests/Element/attribute_twig_tag.twig new file mode 100644 index 0000000..7053cd1 --- /dev/null +++ b/tests/Element/attribute_twig_tag.twig @@ -0,0 +1,4 @@ +
+
+ +{% block class %}class="hidden"{% endblock class %} diff --git a/tests/Element/jsfmt.spec.js b/tests/Element/jsfmt.spec.js index 7e5156f..7aad249 100644 --- a/tests/Element/jsfmt.spec.js +++ b/tests/Element/jsfmt.spec.js @@ -16,6 +16,13 @@ describe("Elements", () => { expect(actual).toMatchFileSnapshot(snapshotFile); }); + it("should handle attribute with twig tag", async () => { + const { actual, snapshotFile } = await run_spec(import.meta.url, { + source: "attribute_twig_tag.twig" + }); + expect(actual).toMatchFileSnapshot(snapshotFile); + }); + it("should handle attributes", async () => { const { actual, snapshotFile } = await run_spec(import.meta.url, { source: "attributes.twig" From 2e39bdd7b6a3c4f5fd13cc0bb820a5c9e5cc87c3 Mon Sep 17 00:00:00 2001 From: zackad Date: Fri, 17 Jan 2025 14:26:00 +0700 Subject: [PATCH 2/3] refactor: break down declaration statement to make it easier to debug --- .../melody-extension-core/parser/block.js | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/melody/melody-extension-core/parser/block.js b/src/melody/melody-extension-core/parser/block.js index db6236b..e5665d8 100644 --- a/src/melody/melody-extension-core/parser/block.js +++ b/src/melody/melody-extension-core/parser/block.js @@ -37,18 +37,20 @@ export const BlockParser = { let openingTagEndToken; let closingTagStartToken; if ((openingTagEndToken = tokens.nextIf(Types.TAG_END))) { + const blockName = createNode(Identifier, nameToken, nameToken.text); + const blockBody = parser.parse((tokenText, token, tokens) => { + const result = !!( + token.type === Types.TAG_START && + tokens.nextIf(Types.SYMBOL, "endblock") + ); + if (result) { + closingTagStartToken = token; + } + return result; + }); blockStatement = new BlockStatement( - createNode(Identifier, nameToken, nameToken.text), - parser.parse((tokenText, token, tokens) => { - const result = !!( - token.type === Types.TAG_START && - tokens.nextIf(Types.SYMBOL, "endblock") - ); - if (result) { - closingTagStartToken = token; - } - return result; - }).expressions + blockName, + blockBody.expressions ); if (tokens.nextIf(Types.SYMBOL, nameToken.text)) { From e75d4c0db4fe17c20fa93d545e1e3b86d7ead6f3 Mon Sep 17 00:00:00 2001 From: zackad Date: Fri, 17 Jan 2025 14:27:08 +0700 Subject: [PATCH 3/3] feat(wip): allow twig tag as html element attribute --- src/melody/melody-parser/Parser.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/melody/melody-parser/Parser.js b/src/melody/melody-parser/Parser.js index 4797ff1..1b179f3 100644 --- a/src/melody/melody-parser/Parser.js +++ b/src/melody/melody-parser/Parser.js @@ -444,6 +444,9 @@ export default class Parser { } else if (tokens.nextIf(Types.EXPRESSION_START)) { element.attributes.push(this.matchExpression()); tokens.expect(Types.EXPRESSION_END); + } else if (tokens.nextIf(Types.TAG_START)) { + const tagExpression = this.matchTag(); + element.attributes.push(tagExpression); } else if ((twigComment = tokens.nextIf(Types.COMMENT))) { const twigCommentValue = new n.StringLiteral(twigComment.text); const twigCommentNode = new n.TwigComment(twigCommentValue);