fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 3.33 kB
Source Map (JSON)
{"version":3,"file":"getCSSRules.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,YAAY,KAAe;CACzC,MAAM,SAAS,IAAI,qBAAqB,QAAQ;CAChD,MAAM,WAAqB,EAAE;AAG7B,MAAK,IAAI,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK;EACtC,MAAM,iBAAiB,OAAO,GAAG,eAAe,IAAI,QAElD,qBACA,GACD;AAED,MAAI,cAAc,MAAM,KAAK,GAC3B;AAIF,gBACG,MAAM,IAAI,CAEV,QAAQ,MAAM,OAAO,UAAU,MAAM,SAAS,KAAK,KAAK,MAAM,CAAC,CAE/D,SAAS,SAAS;AAIjB,QACG,KAAK,MAAM,KAAK,IAAI,EAAE,EAAE,SAAS,KAClC,KAAK,MAAM,CAAC,WAAW,IAAI,CAE3B;GAGF,MAAM,QAAQ,KAAK,MAAM,IAAI,EAC3B,UAAkC,EAAE,EAEpC,qBADc,MAAM,GAAG,MAAM,CACI,MAAM,IAAI,CAAC,OAAO,SAAU,MAAM;AACjE,WAAO,KAAK,MAAM;KAClB;AAEJ,QAAK,IAAI,IAAI,GAAG,IAAI,mBAAmB,QAAQ,KAAK;IAClD,MAAM,OAAO,mBAAmB,GAAG,MAAM,IAAI,EAC3C,WAAW,KAAK,GAAG,MAAM;AAE3B,YAAQ,YADE,KAAK,GAAG,MAAM;;AAG1B,UAAO,MAAM,GAAG,MAAM;AACtB,QAAK,MAAM,IAAI,CAAC,SAAS,UAAU;AACjC,YAAQ,MAAM,QAAQ,SAAS,GAAG,CAAC,MAAM;AACzC,QAAI,UAAU,GACZ;AAEF,aAAS,SAAS;KAChB,GAAI,SAAS,UAAU,EAAE;KACzB,GAAG;KACJ;KACD;IACF;;AAEN,QAAO"}