-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturnCssToInline.js
72 lines (65 loc) · 2.09 KB
/
turnCssToInline.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
function turnCssToInline(a){
var content = a;
// preserve existing inline styles
content.find('[style]').each(function() {
var that = $(this);
that.attr('data-inline-css', that.attr('style'));
});
// get external css rules and iterate over all of them(Applicable to multiple css)
$('link').each(function(a,b){
var extCssFile = b.sheet;
var extCssRules = extCssFile.cssRules;
for(var i = 0; i < extCssRules.length; i++) {
var extCssRule = extCssRules[i].cssText;
if(extCssRule.substr(0,1)=="@"){
continue;
}
var extCssSelector = $.trim(extCssRule.substring(0, extCssRule.indexOf('{'))),
extCssProps = $.trim(extCssRule.substring(extCssRule.indexOf('{') + 1, extCssRule.indexOf('}')));
if(extCssSelector.indexOf(':after') == -1 &&
extCssSelector.indexOf(':before') == -1 &&
content.find(extCssSelector).length
) {
extCssProps = extCssProps.split(';');
if($.trim(extCssProps[extCssProps.length - 1]) == '') {
extCssProps.pop();
}
content.find(extCssSelector).each(function() {
var that = $(this);
for(var a = 0; a < extCssProps.length; a++) {
var style = extCssProps[a].split(/:(?!\/\/)/),
prop = $.trim(style[0]),
val = $.trim(style[1]);
// jQuery doesn't understand css "!important" - remove it
if(val.indexOf('important') != -1) {
val = $.trim(val.replace('!', '').replace('important', ''));
}
that.css(prop, val);
}
});
}
}
// restore inline css, that already existed before applying external css
content.find('[data-inline-css]').each(function() {
var that = $(this),
inlCssProps = that.attr('data-inline-css').split(';');
if($.trim(inlCssProps[inlCssProps.length - 1]) == '') {
inlCssProps.pop();
}
for(var i = 0; i < inlCssProps.length; i++) {
var style = inlCssProps[i].split(/:(?!\/\/)/),
prop = $.trim(style[0]),
val = $.trim(style[1]);
that.css(prop, val);
}
if(a==$('link').length-1){
that.removeAttr('data-inline-css');
}
});
})
}
//the dom should add parent node
if($("#r").length == 0){
$("#report1").after("<div id='r'></div>");
$("#r").append($("#report1"));
}