Skip to content

Commit c72dc44

Browse files
committed
Fix if statement block
1 parent 6369a7c commit c72dc44

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/parsers/template.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ const addDynamicVariablesToElements = (
5454
cssVar: CssVariables
5555
): void => {
5656
node.children?.forEach((childNode) => {
57+
// console.log(childNode);
58+
5759
if (childNode.type === 'InlineComponent') {
5860
addDynamicVariablesToElements(processor, childNode, cssVar);
5961
} else if (childNode.type === 'Element') {
@@ -72,6 +74,9 @@ const addDynamicVariablesToElements = (
7274
` ${cssVar.styleAttribute}`
7375
);
7476
}
77+
} else if (childNode.type === 'IfBlock') {
78+
addDynamicVariablesToElements(processor, childNode, cssVar);
79+
addDynamicVariablesToElements(processor, childNode.else, cssVar);
7580
}
7681
});
7782
};

test/globalFixtures/bindVariable.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,24 @@ describe('Bind variable to CSS', () => {
113113
</style>`
114114
);
115115
});
116+
117+
test('root elements has if statement', async () => {
118+
const output = await compiler({
119+
source: `${script}` +
120+
`{#if color === 'blue'}<div>blue</div>` +
121+
`{:else if color === 'red'}<div>red</div>` +
122+
`{:else}<div>none</div>` +
123+
`{/if}<style module>div{color:bind(color)}</style>`,
124+
}, {
125+
cssVariableHash: '123',
126+
});
127+
128+
expect(output).toBe(
129+
`${script}` +
130+
`{#if color === 'blue'}<div style="--color-123:{color};">blue</div>` +
131+
`{:else if color === 'red'}<div style="--color-123:{color};">red</div>` +
132+
`{:else}<div style="--color-123:{color};">none</div>`+
133+
`{/if}<style module>:global(div){color:var(--color-123)}</style>`
134+
);
135+
});
116136
});

0 commit comments

Comments
 (0)