fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 4.77 kB
Source Map (JSON)
{"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 | 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"],"names":["parseAttributes","element","attributes","cssRules","fontSize","parentAttributes","parentFontSize","DEFAULT_SVG_FONT_SIZE","parentNode","svgValidParentsRegEx","test","nodeName","parentElement","parseUnit","ownAttributes","reduce","memo","attr","value","getAttribute","getGlobalStylesForElement","parseStyleAttribute","cPath","setAttribute","fSize","normalizedStyle","normalizedAttr","normalizeAttr","normalizedValue","normalizeValue","font","parseFontDeclaration","mergedAttrs","setStrokeFillOpacity"],"mappings":"4lBAkBO,SAASA,EACdC,EACAC,EACAC,GAEA,IAAKF,EACH,MAAO,CAAA,EAGT,IACEG,EADEC,EAA2C,CAAA,EAE7CC,EAAiBC,EAIjBN,EAAQO,YACRC,EAAqBC,KAAKT,EAAQO,WAAWG,YAE7CN,EAAmBL,EACjBC,EAAQW,cACRV,EACAC,GAEEE,EAAiBD,WACnBA,EAAWE,EAAiBO,EAAUR,EAAiBD,YAI3D,MAAMU,EAAwC,IACzCZ,EAAWa,OAA+B,CAACC,EAAMC,KAClD,MAAMC,EAAQjB,EAAQkB,aAAaF,GAInC,OAHIC,IACFF,EAAKC,GAAQC,GAERF,GACN,CAAA,MAGAI,EAA0BnB,EAASE,MACnCkB,EAAoBpB,IAGrBa,EAAcQ,IAChBrB,EAAQsB,aAAaD,EAAOR,EAAcQ,IAExCR,EAAcU,KAEhBpB,EAAWS,EAAUC,EAAcU,GAAQlB,GAC3CQ,EAAcU,GAAS,GAAGpB,KAI5B,MAAMqB,EAGF,CAAA,EACJ,IAAK,MAAMR,KAAQH,EAAe,CAChC,MAAMY,EAAiBC,EAAcV,GAC/BW,EAAkBC,EACtBH,EACAZ,EAAcG,GACdZ,EACAD,GAEFqB,EAAgBC,GAAkBE,CACpC,CACIH,GAAmBA,EAAgBK,MACrCC,EAAqBN,EAAgBK,KAAgBL,GAEvD,MAAMO,EAAc,IAAK3B,KAAqBoB,GAC9C,OAAOhB,EAAqBC,KAAKT,EAAQU,UACrCqB,EACAC,EAAqBD,EAC3B"}