{"version":3,"file":"utils.mjs","names":[],"sources":["../../../../../../packages/components/input/src/utils.ts"],"sourcesContent":["import { isFirefox, isNumber } from '@element-plus/utils'\n\nlet hiddenTextarea: HTMLTextAreaElement | undefined = undefined\n\nconst HIDDEN_STYLE = {\n  height: '0',\n  visibility: 'hidden',\n  overflow: isFirefox() ? '' : 'hidden',\n  position: 'absolute',\n  'z-index': '-1000',\n  top: '0',\n  right: '0',\n}\n\nconst CONTEXT_STYLE = [\n  'letter-spacing',\n  'line-height',\n  'padding-top',\n  'padding-bottom',\n  'font-family',\n  'font-weight',\n  'font-size',\n  'text-rendering',\n  'text-transform',\n  'width',\n  'text-indent',\n  'padding-left',\n  'padding-right',\n  'border-width',\n  'box-sizing',\n  'word-break',\n]\n\ntype NodeStyle = {\n  contextStyle: string[][]\n  boxSizing: string\n  paddingSize: number\n  borderSize: number\n}\n\ntype TextAreaHeight = {\n  height: string\n  minHeight?: string\n}\n\nexport const looseToNumber = (val: any): any => {\n  const n = Number.parseFloat(val)\n  return Number.isNaN(n) ? val : n\n}\n\nfunction calculateNodeStyling(targetElement: Element): NodeStyle {\n  const style = window.getComputedStyle(targetElement)\n\n  const boxSizing = style.getPropertyValue('box-sizing')\n\n  const paddingSize =\n    Number.parseFloat(style.getPropertyValue('padding-bottom')) +\n    Number.parseFloat(style.getPropertyValue('padding-top'))\n\n  const borderSize =\n    Number.parseFloat(style.getPropertyValue('border-bottom-width')) +\n    Number.parseFloat(style.getPropertyValue('border-top-width'))\n\n  const contextStyle = CONTEXT_STYLE.map((name) => [\n    name,\n    style.getPropertyValue(name),\n  ])\n\n  return { contextStyle, paddingSize, borderSize, boxSizing }\n}\n\nexport function calcTextareaHeight(\n  targetElement: HTMLTextAreaElement,\n  minRows = 1,\n  maxRows?: number\n): TextAreaHeight {\n  if (!hiddenTextarea) {\n    hiddenTextarea = document.createElement('textarea')\n    let hostNode = document.body\n    // #23575\n    if (!isFirefox() && targetElement.parentNode) {\n      hostNode = targetElement.parentNode as HTMLElement\n    }\n    hostNode.appendChild(hiddenTextarea)\n  }\n\n  const { paddingSize, borderSize, boxSizing, contextStyle } =\n    calculateNodeStyling(targetElement)\n\n  contextStyle.forEach(([key, value]) =>\n    hiddenTextarea?.style.setProperty(key, value)\n  )\n\n  Object.entries(HIDDEN_STYLE).forEach(([key, value]) =>\n    hiddenTextarea?.style.setProperty(key, value, 'important')\n  )\n\n  hiddenTextarea.value = targetElement.value || targetElement.placeholder || ''\n\n  let height = hiddenTextarea.scrollHeight\n  const result = {} as TextAreaHeight\n\n  if (boxSizing === 'border-box') {\n    height = height + borderSize\n  } else if (boxSizing === 'content-box') {\n    height = height - paddingSize\n  }\n\n  hiddenTextarea.value = ''\n  const singleRowHeight = hiddenTextarea.scrollHeight - paddingSize\n\n  if (isNumber(minRows)) {\n    let minHeight = singleRowHeight * minRows\n    if (boxSizing === 'border-box') {\n      minHeight = minHeight + paddingSize + borderSize\n    }\n    height = Math.max(minHeight, height)\n    result.minHeight = `${minHeight}px`\n  }\n  if (isNumber(maxRows)) {\n    let maxHeight = singleRowHeight * maxRows\n    if (boxSizing === 'border-box') {\n      maxHeight = maxHeight + paddingSize + borderSize\n    }\n    height = Math.min(maxHeight, height)\n  }\n  result.height = `${height}px`\n  hiddenTextarea.parentNode?.removeChild(hiddenTextarea)\n  hiddenTextarea = undefined\n\n  return result\n}\n"],"mappings":";;;AAEA,IAAI,iBAAkD,KAAA;AAEtD,MAAM,eAAe;CACnB,QAAQ;CACR,YAAY;CACZ,UAAU,UAAU,IAAI,KAAK;CAC7B,UAAU;CACV,WAAW;CACX,KAAK;CACL,OAAO;AACT;AAEA,MAAM,gBAAgB;CACpB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAcA,MAAa,iBAAiB,QAAkB;CAC9C,MAAM,IAAI,OAAO,WAAW,GAAG;CAC/B,OAAO,OAAO,MAAM,CAAC,IAAI,MAAM;AACjC;AAEA,SAAS,qB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