MsoParagraphList is sometimes used when there is no list.
[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.replaceDocBullets(cfg.node);
16     
17     // this is disabled as the removal is done by other filters;
18    // this.walk(cfg.node);
19     
20     
21 }
22
23 Roo.extend(Roo.htmleditor.FilterWord, Roo.htmleditor.Filter,
24 {
25     tag: true,
26      
27     
28     /**
29      * Clean up MS wordisms...
30      */
31     replaceTag : function(node)
32     {
33          
34         // no idea what this does - span with text, replaceds with just text.
35         if(
36                 node.nodeName == 'SPAN' &&
37                 !node.hasAttributes() &&
38                 node.childNodes.length == 1 &&
39                 node.firstChild.nodeName == "#text"  
40         ) {
41             var textNode = node.firstChild;
42             node.removeChild(textNode);
43             if (node.getAttribute('lang') != 'zh-CN') {   // do not space pad on chinese characters..
44                 node.parentNode.insertBefore(node.ownerDocument.createTextNode(" "), node);
45             }
46             node.parentNode.insertBefore(textNode, node);
47             if (node.getAttribute('lang') != 'zh-CN') {   // do not space pad on chinese characters..
48                 node.parentNode.insertBefore(node.ownerDocument.createTextNode(" ") , node);
49             }
50             
51             node.parentNode.removeChild(node);
52             return false; // dont do chidren - we have remove our node - so no need to do chdhilren?
53         }
54         
55    
56         
57         if (node.tagName.toLowerCase().match(/^(style|script|applet|embed|noframes|noscript)$/)) {
58             node.parentNode.removeChild(node);
59             return false; // dont do chidlren
60         }
61         //Roo.log(node.tagName);
62         // remove - but keep children..
63         if (node.tagName.toLowerCase().match(/^(meta|link|\\?xml:|st1:|o:|v:|font)/)) {
64             //Roo.log('-- removed');
65             while (node.childNodes.length) {
66                 var cn = node.childNodes[0];
67                 node.removeChild(cn);
68                 node.parentNode.insertBefore(cn, node);
69                 // move node to parent - and clean it..
70                 if (cn.nodeType == 1) {
71                     this.replaceTag(cn);
72                 }
73                 
74             }
75             node.parentNode.removeChild(node);
76             /// no need to iterate chidlren = it's got none..
77             //this.iterateChildren(node, this.cleanWord);
78             return false; // no need to iterate children.
79         }
80         // clean styles
81         if (node.className.length) {
82             
83             var cn = node.className.split(/\W+/);
84             var cna = [];
85             Roo.each(cn, function(cls) {
86                 if (cls.match(/Mso[a-zA-Z]+/)) {
87                     return;
88                 }
89                 cna.push(cls);
90             });
91             node.className = cna.length ? cna.join(' ') : '';
92             if (!cna.length) {
93                 node.removeAttribute("class");
94             }
95         }
96         
97         if (node.hasAttribute("lang")) {
98             node.removeAttribute("lang");
99         }
100         
101         if (node.hasAttribute("style")) {
102             
103             var styles = node.getAttribute("style").split(";");
104             var nstyle = [];
105             Roo.each(styles, function(s) {
106                 if (!s.match(/:/)) {
107                     return;
108                 }
109                 var kv = s.split(":");
110                 if (kv[0].match(/^(mso-|line|font|background|margin|padding|color)/)) {
111                     return;
112                 }
113                 // what ever is left... we allow.
114                 nstyle.push(s);
115             });
116             node.setAttribute("style", nstyle.length ? nstyle.join(';') : '');
117             if (!nstyle.length) {
118                 node.removeAttribute('style');
119             }
120         }
121         return true; // do children
122         
123         
124         
125     },
126     
127     styleToObject: function(node)
128     {
129         var styles = (node.getAttribute("style") || '').split(";");
130         var ret = {};
131         Roo.each(styles, function(s) {
132             if (!s.match(/:/)) {
133                 return;
134             }
135             var kv = s.split(":");
136              
137             // what ever is left... we allow.
138             ret[kv[0].trim()] = kv[1];
139         });
140         return ret;
141     },
142     
143     
144     replaceDocBullets : function(doc)
145     {
146         // this is a bit odd - but it appears some indents use ql-indent-1
147          //Roo.log(doc.innerHTML);
148         
149         var listpara = doc.getElementsByClassName('MsoListParagraphCxSpFirst');
150         for( var i = 0; i < listpara.length; i ++) {
151             listpara.item(i).className = "MsoListParagraph";
152         }
153         // this is a bit hacky - we had one word document where h2 had a miso-list attribute.
154         var htwo = doc.getElementsByTagName('h2');
155         for( var i = 0; i < htwo.length; i ++) {
156             if (htwo.item(i).getAttribute('style').match(/mso-list:/)) {
157                 htwo.item(i).className = "MsoListParagraph";
158             }
159         }
160         
161         listpara = doc.getElementsByClassName('ql-indent-1');
162         while(listpara.length) {
163             this.replaceDocBullet(listpara.item(0));
164         }
165         listpara = doc.getElementsByClassName('MsoListParagraph');
166         while(listpara.length) {
167             
168             this.replaceDocBullet(listpara.item(0));
169         }
170       
171     },
172     
173      
174     
175     replaceDocBullet : function(p)
176     {
177         // gather all the siblings.
178         var ns = p,
179             parent = p.parentNode,
180             doc = parent.ownerDocument,
181             items = [];
182             
183             
184         while (ns) {
185             if (ns.nodeType != 1) {
186                 ns = ns.nextSibling;
187                 continue;
188             }
189             if (!ns.className.match(/(MsoListParagraph|ql-indent-1)/i)) {
190                 break;
191             }
192             var spans = ns.getElementsByTagName('span');
193             if (!spans.length) {
194                 break;
195             }
196             var has_list  = false;
197             for(var i = 0; i < spans.length; i++) {
198                 if (spans[i].getAttribute('style').match(/mso-list/)) {
199                     has_list = true;
200                     break;
201                 }
202             }
203             if (!has_list) {
204                 break;
205             }
206             
207             
208             items.push(ns);
209             ns = ns.nextSibling;
210         }
211         if (!items.length) {
212             ns.className = "";
213             return;
214         }
215         
216         var ul = parent.ownerDocument.createElement('ul'); // what about number lists...
217         parent.insertBefore(ul, p);
218         var lvl = 0;
219         var stack = [ ul ];
220         var last_li = false;
221         
222          
223         items.forEach(function(n, ipos) {
224             //Roo.log("got innertHMLT=" + n.innerHTML);
225             
226             var spans = n.getElementsByTagName('span');
227             if (!spans.length) {
228                 //Roo.log("No spans found");
229                  
230                 parent.removeChild(n);
231                 
232                 
233                 return; // skip it...
234             }
235            
236                 
237             
238             var style = {};
239             for(var i = 0; i < spans.length; i++) {
240             
241                 style = this.styleToObject(spans[i]);
242                 if (typeof(style['mso-list']) == 'undefined') {
243                     continue;
244                 }
245                 
246                 spans[i].parentNode.removeChild(spans[i]); // remove the fake bullet.
247                 break;
248             }
249             //Roo.log("NOW GOT innertHMLT=" + n.innerHTML);
250             style = this.styleToObject(n); // mo-list is from the parent node.
251             if (typeof(style['mso-list']) == 'undefined') {
252                 //Roo.log("parent is missing level");
253                 
254                  
255                 parent.removeChild(n);
256                  
257                 return;
258             }
259             
260             var nlvl =   (style['mso-list'].split(' ')[1].replace(/level/,'') *1) - 1  ;
261             
262              
263             if (nlvl > lvl) {
264                 //new indent
265                 var nul = doc.createElement('ul'); // what about number lists...
266                 if (!last_li) {
267                     last_li = doc.createElement('li');
268                     stack[lvl].appendChild(last_li);
269                 }
270                 last_li.appendChild(nul);
271                 stack[nlvl] = nul;
272                 
273             }
274             lvl = nlvl;
275             
276             var nli = stack[nlvl].appendChild(doc.createElement('li'));
277             last_li = nli;
278             nli.innerHTML = n.innerHTML;
279             //Roo.log("innerHTML = " + n.innerHTML);
280             parent.removeChild(n);
281             
282              
283              
284             
285         },this);
286         
287         
288         
289         
290     }
291     
292     
293     
294 });