()
| 1209 | } |
| 1210 | |
| 1211 | async get_svg() { |
| 1212 | // Returns the outer html of the figure svg |
| 1213 | const replaceAll = (find, replace, str) => { |
| 1214 | return str.replace(new RegExp(find, 'g'), replace); |
| 1215 | }; |
| 1216 | |
| 1217 | const get_css = (node, regs) => { |
| 1218 | /** |
| 1219 | * Gathers all the css rules applied to elements of the svg |
| 1220 | * node. Removes the parent element selectors specified in |
| 1221 | * argument `regs`. |
| 1222 | */ |
| 1223 | let css = ''; |
| 1224 | const sheets = document.styleSheets; |
| 1225 | let selector; |
| 1226 | for (let i = 0; i < sheets.length; i++) { |
| 1227 | let rules: any = null; |
| 1228 | // due to CORS we may have some sheets we cannot access, instead of checking we always try |
| 1229 | try { |
| 1230 | rules = (sheets[i] as CSSStyleSheet).cssRules; |
| 1231 | } catch (e) { |
| 1232 | // ignore CORS errors |
| 1233 | } |
| 1234 | if (rules) { |
| 1235 | for (let j = 0; j < rules.length; j++) { |
| 1236 | const rule = rules[j]; |
| 1237 | if (typeof rule.style !== 'undefined') { |
| 1238 | let match = null; |
| 1239 | try { |
| 1240 | match = node.querySelectorAll(rule.selectorText); |
| 1241 | } catch (err) { |
| 1242 | console.warn( |
| 1243 | "Invalid CSS selector '" + rule.selectorText + "'", |
| 1244 | err |
| 1245 | ); |
| 1246 | } |
| 1247 | if (match) { |
| 1248 | const elems = node.querySelectorAll(rule.selectorText); |
| 1249 | if (elems.length > 0) { |
| 1250 | selector = rule.selectorText; |
| 1251 | for (let r = 0; r < regs.length; r++) { |
| 1252 | selector = replaceAll(regs[r], '', selector); |
| 1253 | } |
| 1254 | css += `${selector} { ${rule.style.cssText} } |
| 1255 | `; |
| 1256 | } |
| 1257 | } else if (rule.cssText.match(/^@font-face/)) { |
| 1258 | css += rule.cssText + '\n'; |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | // TODO: this is terrible. The previous loop over style sheets |
| 1265 | // does not catch document's top-level properties. |
| 1266 | css += 'svg { font-size: 10px; }\n'; |
| 1267 | return css; |
| 1268 | }; |
no test coverage detected