inliner.js
[app.webkitpdf] / inliner.js
1
2
3
4 (function(){
5     
6     var camelize = function(a,b){
7         return b.toUpperCase();
8     }
9     
10     var keys = ['fontFamily','fontSize','fontWeight','fontStyle','color',
11         'textTransform','textDecoration','letterSpacing','wordSpacing',
12         'lineHeight','textAlign','verticalAlign','direction','backgroundColor',
13         'backgroundImage','backgroundRepeat','backgroundposition',
14         'backgroundAttachment','opacity','width','height','top','right','bottom',
15         'left','marginTop','marginRight','marginBottom','marginLeft',
16         'paddingTop','paddingRight','paddingBottom','paddingLeft',
17         'borderTopWidth','borderRightWidth','borderBottomWidth',
18         'borderLeftWidth','borderTopColor','borderRightColor',
19         'borderBottomColor','borderLeftColor','borderTopStyle',
20         'borderRightStyle','borderBottomStyle','borderLeftStyle','position',
21         'display','visibility','zIndex','overflowX','overflowY','whiteSpace',
22         'clip','float','clear','cursor','listStyleImage','listStylePosition',
23         'listStyleType','markerOffset'
24     ];
25     
26     var fillStyle = function(dom)
27     {
28         if (!dom || dom.nodeType != 1) {
29             console.log("SKIP: " + dom.nodeName);
30             return;
31         }
32         console.log(dom.nodeName);
33         
34         var style = window.getComputedStyle(dom, null);
35         if (style.display == 'none') {
36             dom.parentElement.removeChild(dom);
37             return;
38         }
39         
40         for (var i = dom.childNodes.length -1; i--; i> -1) {
41             fillStyle(dom.childNodes[i]);
42         }
43         
44         
45         
46         //var pstyle = false;
47         //if (dom.nodeName != 'BODY') {
48         //    pstyle = window.getComputedStyle(dom.parentElement, null);
49         //}
50          
51         
52         
53         //if (dom.nodeName == 'LI') {            throw 'done';        }
54         
55         
56         for(var i=0;i<style.length;i++){
57             var prop = style[i];
58             var camel = prop.replace(/\-([a-z])/g, camelize);
59             var val = style.getPropertyValue(prop);
60             //returns[camel] = val;
61             
62             // idea... if the parent has the same style.. then do not apply it to the child?
63             //if (pstyle && pstyle[prop] == val) {
64             //    continue;
65             //}
66             console.log(prop + '=' + val);
67             
68             dom.style[camel] = val;
69          
70             
71         }
72         var es = dom.getAttribute('style');
73         console.log(dom.nodeName + '::' + es);
74         dom.setAttribute('style', es);
75         
76         //return returns;
77     }
78     var el = document.querySelector('body');
79     fillStyle(el);
80     
81     var matches = document.getElementsByTagName('style');
82     for(var i =0;i < matches.length;i++) {
83         matches[i].parentElement.removeChild(matches[i]);
84     }
85     var matches = document.getElementsByTagName('link');
86     for(var i =0;i < matches.length;i++) {
87     //    matches[i].parentElement.removeChild(matches[i]);
88     }
89      
90     
91 })();