(
style: Record<string, string>,
{ toJS = false, useRem, rootFontSize, scale, variableDisplay }: SerializeOptions,
{ transform, transformVariable, transformPx }: TransformOptions = {},
{ variableSyntax }: SerializeContext = {}
)
| 762 | } |
| 763 | |
| 764 | export function serializeCSS( |
| 765 | style: Record<string, string>, |
| 766 | { toJS = false, useRem, rootFontSize, scale, variableDisplay }: SerializeOptions, |
| 767 | { transform, transformVariable, transformPx }: TransformOptions = {}, |
| 768 | { variableSyntax }: SerializeContext = {} |
| 769 | ) { |
| 770 | const options = { useRem, rootFontSize, scale, variableDisplay } |
| 771 | const displayMode = variableDisplay |
| 772 | const variableSyntaxMap = |
| 773 | variableSyntax instanceof Map ? variableSyntax : new Map(Object.entries(variableSyntax ?? {})) |
| 774 | |
| 775 | function getVariableSyntax(name: string): string | undefined { |
| 776 | return variableSyntaxMap.get(normalizeCustomPropertyName(name.trim())) |
| 777 | } |
| 778 | |
| 779 | function applyDisplayMode(value: string): string { |
| 780 | if (!displayMode || displayMode === 'reference') { |
| 781 | return stripFallback(value) |
| 782 | } |
| 783 | if (displayMode === 'resolved') { |
| 784 | return replaceVarFunctions(value, ({ full, fallback }) => fallback ?? full) |
| 785 | } |
| 786 | return value |
| 787 | } |
| 788 | |
| 789 | function processValue(key: string, value: string) { |
| 790 | const protectedFragments = createProtectedFragments() |
| 791 | let current = value |
| 792 | const useDisplayMode = displayMode != null |
| 793 | |
| 794 | if (useDisplayMode) { |
| 795 | current = preprocessCssValue(current) |
| 796 | current = current.replace(ZERO_UNITS_RE, '$10').trim() |
| 797 | } else { |
| 798 | current = normalizeStyleValue(current) |
| 799 | } |
| 800 | |
| 801 | if (typeof scale === 'number' && scale !== 1) { |
| 802 | current = scalePxValue(current, scale) |
| 803 | } |
| 804 | |
| 805 | if (variableSyntaxMap.size || typeof transformVariable === 'function') { |
| 806 | current = replaceVarFunctions(current, ({ full, name, fallback }) => { |
| 807 | const syntax = getVariableSyntax(name) |
| 808 | if (syntax) { |
| 809 | return protectedFragments.protect(syntax) |
| 810 | } |
| 811 | |
| 812 | if (typeof transformVariable !== 'function') { |
| 813 | return full |
| 814 | } |
| 815 | |
| 816 | const trimmed = name.trim() |
| 817 | const normalizedName = trimmed.startsWith('--') ? trimmed.slice(2) : trimmed |
| 818 | return transformVariable({ |
| 819 | code: current, |
| 820 | name: normalizedName, |
| 821 | value: fallback, |
no test coverage detected