MCPcopy Create free account
hub / github.com/ecomfe/tempad-dev / serializeCSS

Function serializeCSS

packages/extension/utils/css.ts:764–933  ·  view source on GitHub ↗
(
  style: Record<string, string>,
  { toJS = false, useRem, rootFontSize, scale, variableDisplay }: SerializeOptions,
  { transform, transformVariable, transformPx }: TransformOptions = {},
  { variableSyntax }: SerializeContext = {}
)

Source from the content-addressed store, hash-verified

762}
763
764export 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,

Callers 3

css.test.tsFile · 0.90
serializeBlockFunction · 0.90

Calls 8

kebabToCamelFunction · 0.90
processValueFunction · 0.85
resolveGradientBorderFunction · 0.85
formatJsObjectKeyFunction · 0.85
stringifyValueFunction · 0.85
transformFunction · 0.50

Tested by

no test coverage detected