{"version":3,"file":"parseAttributes.min.mjs","names":[],"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 | SVGElement | 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"],"mappings":"+hBAkBA,SAAgB,EACd,EACA,EACA,EAAA,CAEA,GAAA,CAAK,EACH,MAAO,EAAA,CAGT,IACE,EADE,EAA2C,EAAA,CAE7C,EAAA,GAIA,EAAQ,YACR,EAAqB,KAAK,EAAQ,WAAW,SAAA,GAE7C,EAAmB,EACjB,EAAQ,cACR,EACA,EAAA,CAEE,EAAiB,WACnB,EAAW,EAAiB,EAAU,EAAiB,SAAA,GAI3D,IAAM,EAAwC,CAAA,GACzC,EAAW,QAAgC,EAAM,IAAA,CAClD,IAAM,EAAQ,EAAQ,aAAa,EAAA,CAInC,OAHI,IACF,EAAK,GAAQ,GAER,GACN,EAAA,CAAA,CAAA,GAGA,EAA0B,EAAS,EAAA,CAAA,GACnC,EAAoB,EAAA,CAAA,CAGrB,EAAA,cACF,EAAQ,aAAa,EAAO,EAAc,GAAA,CAExC,EAAA,eAEF,EAAW,EAAU,EAAc,GAAQ,EAAA,CAC3C,EAAc,GAAS,GAAG,KAI5B,IAAM,EAGF,EAAA,CACJ,IAAK,IAAM,KAAQ,EAAe,CAChC,IAAM,EAAiB,EAAc,EAAA,CAOrC,EAAgB,GANQ,EACtB,EACA,EAAc,GACd,EACA,EAAA,CAIA,GAAmB,EAAgB,MACrC,EAAqB,EAAgB,KAAgB,EAAA,CAEvD,IAAM,EAAc,CAAA,GAAK,EAAA,GAAqB,EAAA,CAC9C,OAAO,EAAqB,KAAK,EAAQ,SAAA,CACrC,EACA,EAAqB,EAAA,CAAA,OAAA,KAAA"}