UNPKG

fabric

Version:

Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.

1 lines 4.33 kB
{"version":3,"file":"parseAttributes.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":";;;;;;;;;;;;;;;;AAkBA,SAAgB,gBACd,SACA,YACA,UACqB;AACrB,KAAI,CAAC,QACH,QAAO,EAAE;CAGX,IAAI,mBAA2C,EAAE,EAC/C,UACA,iBAAA;AAGF,KACE,QAAQ,cACR,qBAAqB,KAAK,QAAQ,WAAW,SAAS,EACtD;AACA,qBAAmB,gBACjB,QAAQ,eACR,YACA,SACD;AACD,MAAI,iBAAiB,SACnB,YAAW,iBAAiB,UAAU,iBAAiB,SAAS;;CAIpE,MAAM,gBAAwC;EAC5C,GAAG,WAAW,QAAgC,MAAM,SAAS;GAC3D,MAAM,QAAQ,QAAQ,aAAa,KAAK;AACxC,OAAI,MACF,MAAK,QAAQ;AAEf,UAAO;KACN,EAAE,CAAC;EAGN,GAAG,0BAA0B,SAAS,SAAS;EAC/C,GAAG,oBAAoB,QAAQ;EAChC;AAED,KAAI,cAAA,aACF,SAAQ,aAAa,OAAO,cAAc,OAAO;AAEnD,KAAI,cAAA,cAAsB;AAExB,aAAW,UAAU,cAAc,QAAQ,eAAe;AAC1D,gBAAc,SAAS,GAAG;;CAI5B,MAAM,kBAGF,EAAE;AACN,MAAK,MAAM,QAAQ,eAAe;EAChC,MAAM,iBAAiB,cAAc,KAAK;AAO1C,kBAAgB,kBANQ,eACtB,gBACA,cAAc,OACd,kBACA,SACD;;AAGH,KAAI,mBAAmB,gBAAgB,KACrC,sBAAqB,gBAAgB,MAAgB,gBAAgB;CAEvE,MAAM,cAAc;EAAE,GAAG;EAAkB,GAAG;EAAiB;AAC/D,QAAO,qBAAqB,KAAK,QAAQ,SAAS,GAC9C,cACA,qBAAqB,YAAY"}