All files / shared/src normalizeProp.ts

100% Statements 58/58
100% Branches 46/46
100% Functions 7/7
100% Lines 53/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106              736x 134x 134x 257x 257x     257x 191x 237x       134x 602x 536x       184x 184x     184x     57x 57x 25x     97x 85x 85x     57x         184x         133x 131x   129x 129x 205x 205x 200x 200x 6x     200x     129x       525x 525x 246x 279x 140x 284x 284x 193x     139x 54x 63x 50x       525x           13x 10x 10x 2x   10x 1x   10x    
import { hyphenate, isArray, isObject, isString } from './general'
 
export type NormalizedStyle = Record<string, string | number>
 
export function normalizeStyle(
  value: unknown,
): NormalizedStyle | string | undefined {
  if (isArray(value)) {
    const res: NormalizedStyle = {}
    for (let i = 0; i < value.length; i++) {
      const item = value[i]
      const normalized = isString(item)
        ? parseStringStyle(item)
        : (normalizeStyle(item) as NormalizedStyle)
      if (normalized) {
        for (const key in normalized) {
          res[key] = normalized[key]
        }
      }
    }
    return res
  } else if (isString(value) || isObject(value)) {
    return value
  }
}
 
const listDelimiterRE = /;(?![^(]*\))/g
const propertyDelimiterRE = /:([^]+)/
// Match strings and escapes first to preserve comment-like text in CSS values.
const styleCommentRE =
  /"(?:[^"\\]|\\[^])*"|'(?:[^'\\]|\\[^])*'|\\[^]|\/\*[^]*?\*\//g
 
export function parseStringStyle(cssText: string): NormalizedStyle {
  const ret: NormalizedStyle = {}
  cssText
    .replace(styleCommentRE, match => (match.startsWith('/*') ? '' : match))
    .split(listDelimiterRE)
    .forEach(item => {
      if (item) {
        const tmp = item.split(propertyDelimiterRE)
        tmp.length > 1 && (ret[tmp[0].trim()] = tmp[1].trim())
      }
    })
  return ret
}
 
// hyphenate() does not add a dash before the first letter, so camelCase
// vendor-prefixed keys (e.g. WebkitLineClamp, msTransform) need it restored
const vendorPrefixRE = /^(?:webkit|moz|ms)-/
 
export function stringifyStyle(
  styles: NormalizedStyle | string | undefined,
): string {
  if (!styles) return ''
  if (isString(styles)) return styles
 
  let ret = ''
  for (const key in styles) {
    const value = styles[key]
    if (isString(value) || typeof value === 'number') {
      let normalizedKey = key.startsWith(`--`) ? key : hyphenate(key)
      if (vendorPrefixRE.test(normalizedKey)) {
        normalizedKey = `-${normalizedKey}`
      }
      // only render valid values
      ret += `${normalizedKey}:${value};`
    }
  }
  return ret
}
 
export function normalizeClass(value: unknown): string {
  let res = ''
  if (isString(value)) {
    res = value
  } else if (isArray(value)) {
    for (let i = 0; i < value.length; i++) {
      const normalized = normalizeClass(value[i])
      if (normalized) {
        res += normalized + ' '
      }
    }
  } else if (isObject(value)) {
    for (const name in value) {
      if (value[name]) {
        res += name + ' '
      }
    }
  }
  return res.trim()
}
 
export function normalizeProps(
  props: Record<string, any> | null,
): Record<string, any> | null {
  if (!props) return null
  let { class: klass, style } = props
  if (klass && !isString(klass)) {
    props.class = normalizeClass(klass)
  }
  if (style) {
    props.style = normalizeStyle(style)
  }
  return props
}