All files / compiler-sfc/src/style cssVars.ts

96.89% Statements 125/129
92.07% Branches 93/101
100% Functions 20/20
97.52% Lines 118/121

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349                            17x               15x               24x                     31x   4x         27x         46x 46x       6x   40x     17x     430x 430x   33x 33x 39x 39x 39x 39x 39x 36x         430x                                                           17x           33x 33x 33x 33x 33x 33x 83x 83x 83x 13x 13x 4x 4x 4x 9x 6x 6x 85x 6x   3x   70x 14x 56x 1x 55x 4x   51x   83x   33x           14x 132x 132x 118x 118x 2x           116x                 55x 55x                     4x 4x 4x 2x 48x 48x 46x           5x                     213x       6x                             46x 46x   46x 357x 357x   283x 8x 275x 2x 273x 5x 268x 51x 5x   46x     237x   55x 8x   55x   19x 2x   19x                       17x 58x 58x       97x 97x 7x 7x 7x   7x 7x 7x 7x 7x 7x     7x     7x         17x               14x 14x 14x         14x   14x       66x           14x                       7x                  
import {
  type BindingMetadata,
  NodeTypes,
  type SimpleExpressionNode,
  createRoot,
  createSimpleExpression,
  createTransformContext,
  processExpression,
} from '@vue/compiler-dom'
import type { SFCDescriptor } from '../parse'
import type { PluginCreator } from 'postcss'
import hash from 'hash-sum'
import { getEscapedCssVarName } from '@vue/shared'
 
export const CSS_VARS_HELPER = `useCssVars`
 
export function genCssVarsFromList(
  vars: string[],
  id: string,
  isProd: boolean,
  isSSR = false,
): string {
  return `{\n  ${vars
    .map(
      key =>
        // The `:` prefix here is used in `ssrRenderStyle` to distinguish whether
        // a custom property comes from `ssrCssVars`. If it does, we need to reset
        // its value to `initial` on the component instance to avoid unintentionally
        // inheriting the same property value from a different instance of the same
        // component in the outer scope.
        `"${isSSR ? `:--` : ``}${genVarName(id, key, isProd, isSSR)}": (${key})`,
    )
    .join(',\n  ')}\n}`
}
 
function genVarName(
  id: string,
  raw: string,
  isProd: boolean,
  isSSR = false,
): string {
  if (isProd) {
    // hash must not start with a digit to comply with CSS custom property naming rules
    return hash(id + raw).replace(/^\d/, r => `v${r}`)
  } else {
    // escape ASCII Punctuation & Symbols
    // #7823 need to double-escape in SSR because the attributes are rendered
    // into an HTML string
    return `${id}-${getEscapedCssVarName(raw, isSSR)}`
  }
}
 
function normalizeExpression(exp: string) {
  exp = exp.trim()
  if (
    (exp[0] === `'` && exp[exp.length - 1] === `'`) ||
    (exp[0] === `"` && exp[exp.length - 1] === `"`)
  ) {
    return exp.slice(1, -1)
  }
  return exp
}
 
const vBindRE = /v-bind\s*\(/g
 
export function parseCssVars(sfc: SFCDescriptor): string[] {
  const vars: string[] = []
  sfc.styles.forEach(style => {
    let match
    const content = stripComments(style.content)
    while ((match = vBindRE.exec(content))) {
      const start = match.index + match[0].length
      const end = lexBinding(content, start)
      Eif (end !== null) {
        const variable = normalizeExpression(content.slice(start, end))
        if (!vars.includes(variable)) {
          vars.push(variable)
        }
      }
    }
  })
  return vars
}
 
enum CharCodes {
  Tab = 0x9,
  NewLine = 0xa,
  FormFeed = 0xc,
  CarriageReturn = 0xd,
  Space = 0x20,
  DoubleQuote = 0x22,
  SingleQuote = 0x27,
  LeftParen = 0x28,
  RightParen = 0x29,
  Asterisk = 0x2a,
  Dash = 0x2d,
  Slash = 0x2f,
  Zero = 0x30,
  Nine = 0x39,
  UpperA = 0x41,
  UpperZ = 0x5a,
  Backslash = 0x5c,
  Underscore = 0x5f,
  LowerA = 0x61,
  LowerL = 0x6c,
  LowerR = 0x72,
  LowerU = 0x75,
  LowerZ = 0x7a,
}
 
// chars that can start a comment, a string, an escape or close `url(`
const cssSpecialRE = /[/"'\\(]/g
 
// Removes block comments and `//` comments (Less, Sass and Stylus all support
// `//`) so v-bind() inside them is ignored. Strings and unquoted url() are
// kept verbatim since they may legitimately contain comment delimiters.
function stripComments(content: string): string {
  const len = content.length
  let out = ''
  let last = 0
  let i = 0
  cssSpecialRE.lastIndex = 0
  while (cssSpecialRE.test(content)) {
    i = cssSpecialRE.lastIndex - 1
    const c = content.charCodeAt(i)
    if (c === CharCodes.Slash) {
      const next = content.charCodeAt(i + 1)
      if (next === CharCodes.Asterisk) {
        out += content.slice(last, i)
        const end = content.indexOf('*/', i + 2)
        i = last = end === -1 ? len : end + 2
      } else if (next === CharCodes.Slash) {
        out += content.slice(last, i)
        i += 2
        while (i < len && !isNewline(content.charCodeAt(i))) i++
        last = i
      } else {
        i++
      }
    } else if (c === CharCodes.DoubleQuote || c === CharCodes.SingleQuote) {
      i = skipString(content, i + 1, c)
    } else if (c === CharCodes.Backslash) {
      i += 2
    } else if (isUrlFunction(content, i)) {
      i = skipUrl(content, i + 1)
    } else {
      i++
    }
    cssSpecialRE.lastIndex = i
  }
  return last === 0 ? content : out + content.slice(last)
}
 
// an unterminated string ends at the newline, matching CSS bad-string;
// an escaped newline (including CRLF) is a continuation
function skipString(s: string, i: number, quote: number): number {
  while (i < s.length) {
    const c = s.charCodeAt(i)
    if (c === quote) return i + 1
    Iif (isNewline(c)) return i
    if (c === CharCodes.Backslash) {
      i +=
        s.charCodeAt(i + 1) === CharCodes.CarriageReturn &&
        s.charCodeAt(i + 2) === CharCodes.NewLine
          ? 3
          : 2
    } else {
      i++
    }
  }
  return i
}
 
// i is at `(`; `url` must be a whole identifier so that e.g. `my-url(` and
// `my\url(` are not mistaken for url()
function isUrlFunction(s: string, i: number): boolean {
  const prev = s.charCodeAt(i - 4)
  return (
    (s.charCodeAt(i - 3) | 0x20) === CharCodes.LowerU &&
    (s.charCodeAt(i - 2) | 0x20) === CharCodes.LowerR &&
    (s.charCodeAt(i - 1) | 0x20) === CharCodes.LowerL &&
    prev !== CharCodes.Backslash &&
    !isIdentChar(prev)
  )
}
 
// i is right after `url(`; a quoted url is left to the string scanner
function skipUrl(s: string, i: number): number {
  while (isWhitespace(s.charCodeAt(i))) i++
  const c = s.charCodeAt(i)
  if (c === CharCodes.DoubleQuote || c === CharCodes.SingleQuote) return i
  while (i < s.length) {
    const c = s.charCodeAt(i)
    if (c === CharCodes.RightParen) return i + 1
    i += c === CharCodes.Backslash ? 2 : 1
  }
  return i
}
 
function isIdentChar(c: number): boolean {
  return (
    (c >= CharCodes.LowerA && c <= CharCodes.LowerZ) ||
    (c >= CharCodes.UpperA && c <= CharCodes.UpperZ) ||
    (c >= CharCodes.Zero && c <= CharCodes.Nine) ||
    c === CharCodes.Dash ||
    c === CharCodes.Underscore ||
    c >= 0x80
  )
}
 
function isNewline(c: number): boolean {
  return c === CharCodes.NewLine || c === CharCodes.CarriageReturn
}
 
function isWhitespace(c: number): boolean {
  return (
    c === CharCodes.Space ||
    c === CharCodes.Tab ||
    c === CharCodes.FormFeed ||
    isNewline(c)
  )
}
 
enum LexerState {
  inParens,
  inSingleQuoteString,
  inDoubleQuoteString,
}
 
function lexBinding(content: string, start: number): number | null {
  let state: LexerState = LexerState.inParens
  let parenDepth = 0
 
  for (let i = start; i < content.length; i++) {
    const char = content.charAt(i)
    switch (state) {
      case LexerState.inParens:
        if (char === `'`) {
          state = LexerState.inSingleQuoteString
        } else if (char === `"`) {
          state = LexerState.inDoubleQuoteString
        } else if (char === `(`) {
          parenDepth++
        } else if (char === `)`) {
          if (parenDepth > 0) {
            parenDepth--
          } else {
            return i
          }
        }
        break
      case LexerState.inSingleQuoteString:
        if (char === `'`) {
          state = LexerState.inParens
        }
        break
      case LexerState.inDoubleQuoteString:
        if (char === `"`) {
          state = LexerState.inParens
        }
        break
    }
  }
  return null
}
 
// for compileStyle
export interface CssVarsPluginOptions {
  id: string
  isProd: boolean
}
 
export const cssVarsPlugin: PluginCreator<CssVarsPluginOptions> = opts => {
  const { id, isProd } = opts!
  return {
    postcssPlugin: 'vue-sfc-vars',
    Declaration(decl) {
      // rewrite CSS variables
      const value = decl.value
      if (vBindRE.test(value)) {
        vBindRE.lastIndex = 0
        let transformed = ''
        let lastIndex = 0
        let match
        while ((match = vBindRE.exec(value))) {
          const start = match.index + match[0].length
          const end = lexBinding(value, start)
          Eif (end !== null) {
            const variable = normalizeExpression(value.slice(start, end))
            transformed +=
              value.slice(lastIndex, match.index) +
              `var(--${genVarName(id, variable, isProd)})`
            lastIndex = end + 1
          }
        }
        decl.value = transformed + value.slice(lastIndex)
      }
    },
  }
}
cssVarsPlugin.postcss = true
 
export function genCssVarsCode(
  vars: string[],
  bindings: BindingMetadata,
  id: string,
  isProd: boolean,
) {
  const varsExp = genCssVarsFromList(vars, id, isProd)
  const exp = createSimpleExpression(varsExp, false)
  const context = createTransformContext(createRoot([]), {
    prefixIdentifiers: true,
    inline: true,
    bindingMetadata: bindings.__isScriptSetup === false ? undefined : bindings,
  })
  const transformed = processExpression(exp, context)
  const transformedString =
    transformed.type === NodeTypes.SIMPLE_EXPRESSION
      ? transformed.content
      : transformed.children
          .map(c => {
            return typeof c === 'string'
              ? c
              : (c as SimpleExpressionNode).content
          })
          .join('')
 
  return `_${CSS_VARS_HELPER}(_ctx => (${transformedString}))`
}
 
// <script setup> already gets the calls injected as part of the transform
// this is only for single normal <script>
export function genNormalScriptCssVarsCode(
  cssVars: string[],
  bindings: BindingMetadata,
  id: string,
  isProd: boolean,
  defaultVar: string,
): string {
  return (
    `\nimport { ${CSS_VARS_HELPER} as _${CSS_VARS_HELPER} } from 'vue'\n` +
    `const __injectCSSVars__ = () => {\n${genCssVarsCode(cssVars, bindings, id, isProd)}}\n` +
    `const __setup__ = ${defaultVar}.setup\n` +
    `${defaultVar}.setup = __setup__\n` +
    `  ? (props, ctx) => { __injectCSSVars__();return __setup__(props, ctx) }\n` +
    `  : __injectCSSVars__\n`
  )
}