more styling
[roojs1] / Roo / htmleditor / FilterWord.js
1 /**
2  * @class Roo.htmleditor.FilterWord
3  * try and clean up all the mess that Word generates.
4  * 
5  * This is the 'nice version' - see 'Heavy' that white lists a very short list of elements, and multi-filters 
6  
7  * @constructor
8  * Run a new Span Filter
9  * @param {Object} config Configuration options
10  */
11
12 Roo.htmleditor.FilterWord = function(cfg)
13 {
14     // no need to apply config.
15     this.walk(cfg.node);
16 }
17
18 Roo.extend(Roo.htmleditor.FilterWord, Roo.htmleditor.Filter,
19 {
20     tag: true,
21      
22     
23     /**
24      * Clean up MS wordisms...
25      */
26     replaceTag : function(node)
27     {
28          
29         // no idea what this does - span with text, replaceds with just text.
30         if(
31                 node.nodeName == 'SPAN' &&
32                 !node.hasAttributes() &&
33                 node.childNodes.length == 1 &&
34                 node.firstChild.nodeName == "#text"  
35         ) {
36             var textNode = node.firstChild;
37             node.removeChild(textNode);
38             if (node.getAttribute('lang') != 'zh-CN') {   // do not space pad on chinese characters..
39                 node.parentNode.insertBefore(node.ownerDocument.createTextNode(" "), node);
40             }
41             node.parentNode.insertBefore(textNode, node);
42             if (node.getAttribute('lang') != 'zh-CN') {   // do not space pad on chinese characters..
43                 node.parentNode.insertBefore(node.ownerDocument.createTextNode(" ") , node);
44             }
45             
46             node.parentNode.removeChild(node);
47             return false; // dont do chidren - we have remove our node - so no need to do chdhilren?
48         }
49         
50    
51         
52         if (node.tagName.toLowerCase().match(/^(style|script|applet|embed|noframes|noscript)$/)) {
53             node.parentNode.removeChild(node);
54             return false; // dont do chidlren
55         }
56         //Roo.log(node.tagName);
57         // remove - but keep children..
58         if (node.tagName.toLowerCase().match(/^(meta|link|\\?xml:|st1:|o:|v:|font)/)) {
59             //Roo.log('-- removed');
60             while (node.childNodes.length) {
61                 var cn = node.childNodes[0];
62                 node.removeChild(cn);
63                 node.parentNode.insertBefore(cn, node);
64                 // move node to parent - and clean it..
65                 this.replaceTag(cn);
66             }
67             node.parentNode.removeChild(node);
68             /// no need to iterate chidlren = it's got none..
69             //this.iterateChildren(node, this.cleanWord);
70             return false; // no need to iterate children.
71         }
72         // clean styles
73         if (node.className.length) {
74             
75             var cn = node.className.split(/\W+/);
76             var cna = [];
77             Roo.each(cn, function(cls) {
78                 if (cls.match(/Mso[a-zA-Z]+/)) {
79                     return;
80                 }
81                 cna.push(cls);
82             });
83             node.className = cna.length ? cna.join(' ') : '';
84             if (!cna.length) {
85                 node.removeAttribute("class");
86             }
87         }
88         
89         if (node.hasAttribute("lang")) {
90             node.removeAttribute("lang");
91         }
92         
93         if (node.hasAttribute("style")) {
94             
95             var styles = node.getAttribute("style").split(";");
96             var nstyle = [];
97             Roo.each(styles, function(s) {
98                 if (!s.match(/:/)) {
99                     return;
100                 }
101                 var kv = s.split(":");
102                 if (kv[0].match(/^(mso-|line|font|background|margin|padding|color)/)) {
103                     return;
104                 }
105                 // what ever is left... we allow.
106                 nstyle.push(s);
107             });
108             node.setAttribute("style", nstyle.length ? nstyle.join(';') : '');
109             if (!nstyle.length) {
110                 node.removeAttribute('style');
111             }
112         }
113         return true; // do children
114         
115         
116         
117     }
118 });