Skip to content

Commit

Permalink
(feat) support style modifiers (#331)
Browse files Browse the repository at this point in the history
closes #330
  • Loading branch information
dummdidumm committed Dec 9, 2022
1 parent d992637 commit f3685aa
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 119 deletions.
210 changes: 107 additions & 103 deletions CHANGELOG.md

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -37,7 +37,7 @@
"prettier": "^2.7.1",
"rollup": "2.36.0",
"rollup-plugin-typescript": "1.0.1",
"svelte": "^3.47.0",
"svelte": "^3.54.0",
"ts-node": "^9.1.1",
"tslib": "^2.0.3",
"typescript": "4.1.3"
Expand Down
20 changes: 14 additions & 6 deletions src/print/index.ts
Expand Up @@ -624,25 +624,33 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
: concat(['=', ...printJsExpression()]),
]);
case 'StyleDirective':
const prefix = [
'style:',
node.name,
node.modifiers && node.modifiers.length
? concat(['|', join('|', node.modifiers)])
: '',
];

if (isOrCanBeConvertedToShorthand(node)) {
if (options.svelteStrictMode) {
return concat(['style:', node.name, '="{', node.name, '}"']);
return concat([...prefix, '="{', node.name, '}"']);
} else if (options.svelteAllowShorthand) {
return concat(['style:', node.name]);
return concat([...prefix]);
} else {
return concat(['style:', node.name, '={', node.name, '}']);
return concat([...prefix, '={', node.name, '}']);
}
} else {
if (node.value === true) {
return concat(['style:', node.name]);
return concat([...prefix]);
}

const quotes = !isLoneMustacheTag(node.value) || options.svelteStrictMode;
const attrNodeValue = printAttributeNodeValue(path, print, quotes, node);
if (quotes) {
return concat(['style:', node.name, '=', '"', attrNodeValue, '"']);
return concat([...prefix, '=', '"', attrNodeValue, '"']);
} else {
return concat(['style:', node.name, '=', attrNodeValue]);
return concat([...prefix, '=', attrNodeValue]);
}
}
case 'Let':
Expand Down
1 change: 1 addition & 0 deletions src/print/nodes.ts
Expand Up @@ -127,6 +127,7 @@ export interface StyleDirectiveNode extends BaseNode {
type: 'StyleDirective';
name: string;
value: Node[] | true;
modifiers?: string[];
}

export interface LetNode extends BaseNode {
Expand Down
8 changes: 8 additions & 0 deletions test/printer/samples/style-directive.html
Expand Up @@ -5,3 +5,11 @@
<div style:background="prefix{color}" />
<div style:background={`prefix${color}`} />
<div style:background />

<div style:background|important="green" />
<div style:background|important={color} />
<div style:background|important="prefix{color}suffix" />
<div style:background|important="{color}suffix" />
<div style:background|important="prefix{color}" />
<div style:background|important={`prefix${color}`} />
<div style:background|important />

0 comments on commit f3685aa

Please sign in to comment.