|
| 1 | +import MagicString from 'magic-string'; |
| 2 | +import { StyleDirective } from '../../interfaces'; |
| 3 | +import { buildTemplateString } from '../utils/node-utils'; |
| 4 | + |
| 5 | +/** |
| 6 | + * style:xx ---> __sveltets_1_ensureType(String, xx); |
| 7 | + * style:xx={yy} ---> __sveltets_1_ensureType(String, yy); |
| 8 | + * style:xx="yy" ---> __sveltets_1_ensureType(String, "yy"); |
| 9 | + * style:xx="a{b}" ---> __sveltets_1_ensureType(String, `a${b}`); |
| 10 | + */ |
| 11 | +export function handleStyleDirective(str: MagicString, style: StyleDirective): void { |
| 12 | + const htmlx = str.original; |
| 13 | + if (style.value === true || style.value.length === 0) { |
| 14 | + str.overwrite( |
| 15 | + style.start, |
| 16 | + htmlx.indexOf(':', style.start) + 1, |
| 17 | + '{...__sveltets_1_ensureType(String, ' |
| 18 | + ); |
| 19 | + str.appendLeft(style.end, ')}'); |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + if (style.value.length > 1) { |
| 24 | + buildTemplateString( |
| 25 | + style, |
| 26 | + str, |
| 27 | + htmlx, |
| 28 | + '{...__sveltets_1_ensureType(String, `', |
| 29 | + '`)}', |
| 30 | + style.start |
| 31 | + ); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const styleVal = style.value[0]; |
| 36 | + if (styleVal.type === 'Text') { |
| 37 | + str.overwrite(style.start, styleVal.start, '{...__sveltets_1_ensureType(String, "'); |
| 38 | + if (styleVal.end === style.end) { |
| 39 | + str.appendLeft(style.end, '")}'); |
| 40 | + } else { |
| 41 | + str.overwrite(styleVal.end, style.end, '")}'); |
| 42 | + } |
| 43 | + } else { |
| 44 | + // MustacheTag |
| 45 | + str.overwrite(style.start, styleVal.start + 1, '{...__sveltets_1_ensureType(String, '); |
| 46 | + str.overwrite(styleVal.end - 1, style.end, ')}'); |
| 47 | + } |
| 48 | +} |
0 commit comments