UNPKG

fabric

Version:

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

1 lines 3.29 kB
{"version":3,"file":"getCSSRules.min.mjs","names":[],"sources":["../../../src/parser/getCSSRules.ts"],"sourcesContent":["import type { CSSRules } from './typedefs';\n\n/**\n * Returns CSS rules for a given SVG document\n * @param {HTMLElement} doc SVG document to parse\n * @return {Object} CSS rules of this document\n */\nexport function getCSSRules(doc: Document) {\n const styles = doc.getElementsByTagName('style');\n const allRules: CSSRules = {};\n\n // very crude parsing of style contents\n for (let i = 0; i < styles.length; i++) {\n const styleContents = (styles[i].textContent || '').replace(\n // remove comments\n /\\/\\*[\\s\\S]*?\\*\\//g,\n '',\n );\n\n if (styleContents.trim() === '') {\n continue;\n }\n // recovers all the rule in this form `body { style code... }`\n // rules = styleContents.match(/[^{]*\\{[\\s\\S]*?\\}/g);\n styleContents\n .split('}')\n // remove empty rules and remove everything if we didn't split in at least 2 pieces\n .filter((rule, index, array) => array.length > 1 && rule.trim())\n // at this point we have hopefully an array of rules `body { style code... `\n .forEach((rule) => {\n // if there is more than one opening bracket and the rule starts with '@', it is likely\n // a nested at-rule like @media, @supports, @scope, etc. Ignore these as the code below\n // can not handle it.\n if (\n (rule.match(/{/g) || []).length > 1 &&\n rule.trim().startsWith('@')\n ) {\n return;\n }\n\n const match = rule.split('{'),\n ruleObj: Record<string, string> = {},\n declaration = match[1].trim(),\n propertyValuePairs = declaration.split(';').filter(function (pair) {\n return pair.trim();\n });\n\n for (let j = 0; j < propertyValuePairs.length; j++) {\n const pair = propertyValuePairs[j].split(':'),\n property = pair[0].trim(),\n value = pair[1].trim();\n ruleObj[property] = value;\n }\n rule = match[0].trim();\n rule.split(',').forEach((_rule) => {\n _rule = _rule.replace(/^svg/i, '').trim();\n if (_rule === '') {\n return;\n }\n allRules[_rule] = {\n ...(allRules[_rule] || {}),\n ...ruleObj,\n };\n });\n });\n }\n return allRules;\n}\n"],"mappings":"AAOA,SAAgB,EAAY,EAAA,CAC1B,IAAM,EAAS,EAAI,qBAAqB,QAAA,CAClC,EAAqB,EAAA,CAG3B,IAAK,IAAI,EAAI,EAAG,EAAI,EAAO,OAAQ,IAAK,CACtC,IAAM,GAAiB,EAAO,GAAG,aAAe,IAAI,QAElD,oBACA,GAAA,CAGE,EAAc,MAAA,GAAW,IAK7B,EACG,MAAM,IAAA,CAEN,QAAQ,EAAM,EAAO,IAAU,EAAM,OAAS,GAAK,EAAK,MAAA,CAAA,CAExD,QAAS,GAAA,CAIR,IACG,EAAK,MAAM,KAAA,EAAS,EAAA,EAAI,OAAS,GAClC,EAAK,MAAA,CAAO,WAAW,IAAA,CAEvB,OAGF,IAAM,EAAQ,EAAK,MAAM,IAAA,CACvB,EAAkC,EAAA,CAElC,EADc,EAAM,GAAG,MAAA,CACU,MAAM,IAAA,CAAK,OAAO,SAAU,EAAA,CAC3D,OAAO,EAAK,MAAA,EAAA,CAGhB,IAAK,IAAI,EAAI,EAAG,EAAI,EAAmB,OAAQ,IAAK,CAClD,IAAM,EAAO,EAAmB,GAAG,MAAM,IAAA,CACvC,EAAW,EAAK,GAAG,MAAA,CAErB,EAAQ,GADE,EAAK,GAAG,MAAA,EAGpB,EAAO,EAAM,GAAG,MAAA,EACX,MAAM,IAAA,CAAK,QAAS,GAAA,EACvB,EAAQ,EAAM,QAAQ,QAAS,GAAA,CAAI,MAAA,IACrB,KAGd,EAAS,GAAS,CAAA,GACZ,EAAS,IAAU,EAAA,CAAA,GACpB,EAAA,GAAA,EAAA,CAKb,OAAO,EAAA,OAAA,KAAA"}