{"version":3,"file":"parseAttributes.min.mjs","sources":["../../../src/parser/parseAttributes.ts"],"sourcesContent":["import { DEFAULT_SVG_FONT_SIZE } from '../constants';\nimport { parseUnit } from '../util/misc/svgParsing';\nimport { cPath, fSize, svgValidParentsRegEx } from './constants';\nimport { getGlobalStylesForElement } from './getGlobalStylesForElement';\nimport { normalizeAttr } from './normalizeAttr';\nimport { normalizeValue } from './normalizeValue';\nimport { parseFontDeclaration } from './parseFontDeclaration';\nimport { parseStyleAttribute } from './parseStyleAttribute';\nimport { setStrokeFillOpacity } from './setStrokeFillOpacity';\nimport type { CSSRules } from './typedefs';\n\n/**\n * Returns an object of attributes' name/value, given element and an array of attribute names;\n * Parses parent \"g\" nodes recursively upwards.\n * @param {SVGElement | HTMLElement} element Element to parse\n * @param {Array} attributes Array of attributes to parse\n * @return {Object} object containing parsed attributes' names/values\n */\nexport function parseAttributes(\n  element: HTMLElement | null,\n  attributes: string[],\n  cssRules?: CSSRules\n): Record<string, any> {\n  if (!element) {\n    return {};\n  }\n\n  let parentAttributes: Record<string, string> = {},\n    fontSize: number,\n    parentFontSize = DEFAULT_SVG_FONT_SIZE;\n\n  // if there's a parent container (`g` or `a` or `symbol` node), parse its attributes recursively upwards\n  if (\n    element.parentNode &&\n    svgValidParentsRegEx.test(element.parentNode.nodeName)\n  ) {\n    parentAttributes = parseAttributes(\n      element.parentElement,\n      attributes,\n      cssRules\n    );\n    if (parentAttributes.fontSize) {\n      fontSize = parentFontSize = parseUnit(parentAttributes.fontSize);\n    }\n  }\n\n  const ownAttributes: Record<string, string> = {\n    ...attributes.reduce<Record<string, string>>((memo, attr) => {\n      const value = element.getAttribute(attr);\n      if (value) {\n        memo[attr] = value;\n      }\n      return memo;\n    }, {}),\n    // add values parsed from style, which take precedence over attributes\n    // (see: http://www.w3.org/TR/SVG/styling.html#UsingPresentationAttributes)\n    ...getGlobalStylesForElement(element, cssRules),\n    ...parseStyleAttribute(element),\n  };\n\n  if (ownAttributes[cPath]) {\n    element.setAttribute(cPath, ownAttributes[cPath]);\n  }\n  if (ownAttributes[fSize]) {\n    // looks like the minimum should be 9px when dealing with ems. this is what looks like in browsers.\n    fontSize = parseUnit(ownAttributes[fSize], parentFontSize);\n    ownAttributes[fSize] = `${fontSize}`;\n  }\n\n  // this should have its own complex type\n  const normalizedStyle: Record<\n    string,\n    string | boolean | number | number[] | null\n  > = {};\n  for (const attr in ownAttributes) {\n    const normalizedAttr = normalizeAttr(attr);\n    const normalizedValue = normalizeValue(\n      normalizedAttr,\n      ownAttributes[attr],\n      parentAttributes,\n      fontSize!\n    );\n    normalizedStyle[normalizedAttr] = normalizedValue;\n  }\n  if (normalizedStyle && normalizedStyle.font) {\n    parseFontDeclaration(normalizedStyle.font as string, normalizedStyle);\n  }\n  const mergedAttrs = { ...parentAttributes, ...normalizedStyle };\n  return svgValidParentsRegEx.test(element.nodeName)\n    ? mergedAttrs\n    : setStrokeFillOpacity(mergedAttrs);\n}\n"],"names":["parseAttributes","element","attributes","cssRules","fontSize","parentAttributes","parentFontSize","DEFAULT_SVG_FONT_SIZE","parentNode","svgValidParentsRegEx","test","nodeName","parentElement","parseUnit","ownAttributes","_objectSpread","reduce","memo","attr","value","getAttribute","getGlobalStylesForElement","parseStyleAttribute","cPath","setAttribute","fSize","concat","normalizedStyle","normalizedAttr","normalizeAttr","normalizedValue","normalizeValue","font","parseFontDeclaration","mergedAttrs","setStrokeFillOpacity"],"mappings":"6qBAkBO,SAASA,EACdC,EACAC,EACAC,GAEA,IAAKF,EACH,MAAO,GAGT,IACEG,EADEC,EAA2C,CAAE,EAE/CC,EAAiBC,EAIjBN,EAAQO,YACRC,EAAqBC,KAAKT,EAAQO,WAAWG,YAE7CN,EAAmBL,EACjBC,EAAQW,cACRV,EACAC,GAEEE,EAAiBD,WACnBA,EAAWE,EAAiBO,EAAUR,EAAiBD,YAI3D,MAAMU,EAAqCC,EAAAA,EAAAA,EAAA,GACtCb,EAAWc,QAA+B,CAACC,EAAMC,KAClD,MAAMC,EAAQlB,EAAQmB,aAAaF,GAInC,OAHIC,IACFF,EAAKC,GAAQC,GAERF,CAAI,GACV,CAAE,IAGFI,EAA0BpB,EAASE,IACnCmB,EAAoBrB,IAGrBa,EAAcS,IAChBtB,EAAQuB,aAAaD,EAAOT,EAAcS,IAExCT,EAAcW,KAEhBrB,EAAWS,EAAUC,EAAcW,GAAQnB,GAC3CQ,EAAcW,MAAMC,OAAMtB,IAI5B,MAAMuB,EAGF,CAAA,EACJ,IAAK,MAAMT,KAAQJ,EAAe,CAChC,MAAMc,EAAiBC,EAAcX,GAC/BY,EAAkBC,EACtBH,EACAd,EAAcI,GACdb,EACAD,GAEFuB,EAAgBC,GAAkBE,CACpC,CACIH,GAAmBA,EAAgBK,MACrCC,EAAqBN,EAAgBK,KAAgBL,GAEvD,MAAMO,EAAWnB,EAAAA,EAAQV,CAAAA,EAAAA,GAAqBsB,GAC9C,OAAOlB,EAAqBC,KAAKT,EAAQU,UACrCuB,EACAC,EAAqBD,EAC3B"}