-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathsetTableAttrs.js
More file actions
84 lines (73 loc) · 2.08 KB
/
Copy pathsetTableAttrs.js
File metadata and controls
84 lines (73 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const tableStyleAttrMap = {
table: {
float: 'align',
'background-color': 'bgcolor',
width: 'width',
height: 'height'
},
tr: {
'background-color': 'bgcolor',
'vertical-align': 'valign',
'text-align': 'align'
},
'td,th': {
'background-color': 'bgcolor',
width: 'width',
height: 'height',
'vertical-align': 'valign',
'text-align': 'align',
'white-space': 'nowrap'
},
'tbody,thead,tfoot': {
'vertical-align': 'valign',
'text-align': 'align'
}
};
const attributesToRemovePxFrom = [ 'height', 'width' ];
const applyStylesAsProps = (el, styleToAttrMap) => {
let style;
let styleVal;
let attributeValue;
for (style in styleToAttrMap) {
styleVal = el.style[style];
if (styleVal !== undefined) {
if (attributesToRemovePxFrom.includes(style)) {
attributeValue = styleVal.replace(/px$/i, '');
} else {
attributeValue = styleVal;
}
if (attributeValue.length > 0) {
el.setAttribute(styleToAttrMap[style], attributeValue);
} else {
el.removeAttribute(styleToAttrMap[style]);
}
el.style[style] = '';
}
}
};
const batchApplyStylesAsProps = ($el, sel) => {
const elements = $el.querySelectorAll(sel);
Array.prototype.forEach.call(elements, el => {
applyStylesAsProps(el, tableStyleAttrMap[sel]);
});
};
const resetAttribute = (el, attribute) => {
if (!el.getAttribute(attribute)) {
el.setAttribute(attribute, 0);
}
return el;
};
module.exports = (el) => {
let selector;
let $el = el;
$el = resetAttribute($el, 'border');
$el = resetAttribute($el, 'cellpadding');
$el = resetAttribute($el, 'cellspacing');
for (selector in tableStyleAttrMap) {
if (selector === 'table') {
applyStylesAsProps($el, tableStyleAttrMap.table);
} else {
batchApplyStylesAsProps($el, selector);
}
}
};