UNPKG

fabric

Version:

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

1 lines 5.78 kB
{"version":3,"file":"parseAttributes.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","parentAttributes","fontSize","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":";;;;;;;;;;AAWA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,eAAeA,CAC7BC,OAAwC,EACxCC,UAAoB,EACpBC,QAAmB,EACE;EACrB,IAAI,CAACF,OAAO,EAAE;AACZ,IAAA,OAAO,EAAE;AACX,EAAA;EAEA,IAAIG,gBAAwC,GAAG,EAAE;IAC/CC,QAAgB;AAChBC,IAAAA,cAAc,GAAGC,qBAAqB;;AAExC;AACA,EAAA,IACEN,OAAO,CAACO,UAAU,IAClBC,oBAAoB,CAACC,IAAI,CAACT,OAAO,CAAC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