Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/melody/melody-extension-core/parser/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
3 changes: 3 additions & 0 deletions src/melody/melody-parser/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment on lines +448 to +449
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think storing twig tags as html attribute is correct. They have different data structure that might be incompatible with each other. Maybe we can introduce new property into Element node. I'm not sure what we should named it (statements, tags).

} else if ((twigComment = tokens.nextIf(Types.COMMENT))) {
const twigCommentValue = new n.StringLiteral(twigComment.text);
const twigCommentNode = new n.TwigComment(twigCommentValue);
Expand Down
9 changes: 9 additions & 0 deletions tests/Element/__snapshots__/attribute_twig_tag.snap.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div
{% block class %}
contenteditable="true"
{% endblock %}
></div>

{% block class %}
class="hidden"
{% endblock %}
4 changes: 4 additions & 0 deletions tests/Element/attribute_twig_tag.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div {% block class %}contenteditable="true"{% endblock class %}>
</div>

{% block class %}class="hidden"{% endblock class %}
7 changes: 7 additions & 0 deletions tests/Element/jsfmt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading