1 | export function cssToObject(css) {
|
2 | let o = {};
|
3 | let elements = css.split(';');
|
4 | elements
|
5 | .filter(i => !!i)
|
6 | .map(function(i) {
|
7 | let s = i.split(':');
|
8 | let key = s.shift().trim();
|
9 | let value = s.join(':').trim();
|
10 | o[key] = value;
|
11 | });
|
12 | return o;
|
13 | }
|
14 |
|
15 | export function cssToJsxStr(css) {
|
16 | let o = cssToObject(css);
|
17 | return `{${JSON.stringify(o)}}`;
|
18 | }
|
19 |
|
20 |
|
21 |
|
22 | export function styleAttrToJsx(xml) {
|
23 | let rx = / style="([^"]*)"/g;
|
24 | let arr = rx.exec(xml);
|
25 | if (!arr) return xml;
|
26 | return xml.replace(rx, ' style=' + cssToJsxStr(arr[1]));
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | export function attrsToObj(attrs) {
|
35 | let o = {};
|
36 |
|
37 | let rx = / (\S+)="/g;
|
38 | let elements = [], tmp;
|
39 | while(tmp = rx.exec(attrs)) elements.push(tmp[1]);
|
40 |
|
41 | elements
|
42 | .map(function(i) {
|
43 |
|
44 | let rx2 = new RegExp(` ${i}="([^\"]*)"`, 'g');
|
45 | let arr = rx2.exec(attrs);
|
46 | o[i] = arr[1];
|
47 | });
|
48 | return o;
|
49 | }
|
50 |
|
51 | export function convertRootToProps(xml) {
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | let rx = /<svg (.*)>/;
|
58 | let arr = rx.exec(xml);
|
59 | let o = attrsToObj(arr[1]);
|
60 | let keys = Object.keys(o);
|
61 | keys.map(function(key) {
|
62 | if (key === 'style') return;
|
63 | o[key] = `{'undefined' === typeof this.props['${key}'] ? ${JSON.stringify(o[key])} : this.props['${key}']}`;
|
64 | });
|
65 | let proped = keys.map(function(key) {
|
66 | return `${key}=${o[key]}`;
|
67 | }).join(' ');
|
68 | return xml.replace(arr[1], proped);
|
69 | }
|
70 |
|
71 | export function hyphenToCamel(name) {
|
72 | return name.replace(/-([a-z])/g, g => g[1].toUpperCase());
|
73 | }
|