Skip to content

Commit f3685aa

Browse files
authored
(feat) support style modifiers (sveltejs#331)
closes sveltejs#330
1 parent d992637 commit f3685aa

File tree

6 files changed

+140
-119
lines changed

6 files changed

+140
-119
lines changed

CHANGELOG.md

Lines changed: 107 additions & 103 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"prettier": "^2.7.1",
3838
"rollup": "2.36.0",
3939
"rollup-plugin-typescript": "1.0.1",
40-
"svelte": "^3.47.0",
40+
"svelte": "^3.54.0",
4141
"ts-node": "^9.1.1",
4242
"tslib": "^2.0.3",
4343
"typescript": "4.1.3"

src/print/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -624,25 +624,33 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
624624
: concat(['=', ...printJsExpression()]),
625625
]);
626626
case 'StyleDirective':
627+
const prefix = [
628+
'style:',
629+
node.name,
630+
node.modifiers && node.modifiers.length
631+
? concat(['|', join('|', node.modifiers)])
632+
: '',
633+
];
634+
627635
if (isOrCanBeConvertedToShorthand(node)) {
628636
if (options.svelteStrictMode) {
629-
return concat(['style:', node.name, '="{', node.name, '}"']);
637+
return concat([...prefix, '="{', node.name, '}"']);
630638
} else if (options.svelteAllowShorthand) {
631-
return concat(['style:', node.name]);
639+
return concat([...prefix]);
632640
} else {
633-
return concat(['style:', node.name, '={', node.name, '}']);
641+
return concat([...prefix, '={', node.name, '}']);
634642
}
635643
} else {
636644
if (node.value === true) {
637-
return concat(['style:', node.name]);
645+
return concat([...prefix]);
638646
}
639647

640648
const quotes = !isLoneMustacheTag(node.value) || options.svelteStrictMode;
641649
const attrNodeValue = printAttributeNodeValue(path, print, quotes, node);
642650
if (quotes) {
643-
return concat(['style:', node.name, '=', '"', attrNodeValue, '"']);
651+
return concat([...prefix, '=', '"', attrNodeValue, '"']);
644652
} else {
645-
return concat(['style:', node.name, '=', attrNodeValue]);
653+
return concat([...prefix, '=', attrNodeValue]);
646654
}
647655
}
648656
case 'Let':

src/print/nodes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export interface StyleDirectiveNode extends BaseNode {
127127
type: 'StyleDirective';
128128
name: string;
129129
value: Node[] | true;
130+
modifiers?: string[];
130131
}
131132

132133
export interface LetNode extends BaseNode {

test/printer/samples/style-directive.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@
55
<div style:background="prefix{color}" />
66
<div style:background={`prefix${color}`} />
77
<div style:background />
8+
9+
<div style:background|important="green" />
10+
<div style:background|important={color} />
11+
<div style:background|important="prefix{color}suffix" />
12+
<div style:background|important="{color}suffix" />
13+
<div style:background|important="prefix{color}" />
14+
<div style:background|important={`prefix${color}`} />
15+
<div style:background|important />

0 commit comments

Comments
 (0)