-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathsetTableAttrs.js
76 lines (66 loc) · 1.83 KB
/
setTableAttrs.js
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
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, styleVal, attributeValue;
for (style in styleToAttrMap) {
styleVal = $el.css(style);
if (styleVal !== undefined) {
if (attributesToRemovePxFrom.indexOf(style) > -1) {
attributeValue = styleVal.replace(/px$/i, '');
} else {
attributeValue = styleVal;
}
$el.attr(styleToAttrMap[style], attributeValue);
$el.css(style, '');
}
}
};
const batchApplyStylesAsProps = ($el, sel, $) => {
$el.find(sel).each((i, childEl) => {
applyStylesAsProps($(childEl), tableStyleAttrMap[sel]);
});
};
function resetAttr(node, attribute) {
if (node.attr(attribute)) {
return;
}
node.attr(attribute, 0);
}
module.exports = (el, $) => {
let selector;
let $el = $(el);
resetAttr($el, 'border');
resetAttr($el, 'cellpadding');
resetAttr($el, 'cellspacing');
for (selector in tableStyleAttrMap) {
if (selector === 'table') {
applyStylesAsProps($el, tableStyleAttrMap.table);
} else {
batchApplyStylesAsProps($el, selector, $);
}
}
};