2 * @class Roo.htmleditor.FilterWord
3 * try and clean up all the mess that Word generates.
5 * This is the 'nice version' - see 'Heavy' that white lists a very short list of elements, and multi-filters
8 * Run a new Span Filter
9 * @param {Object} config Configuration options
12 Roo.htmleditor.FilterWord = function(cfg)
14 // no need to apply config.
15 this.replaceDocBullets(cfg.node);
22 Roo.extend(Roo.htmleditor.FilterWord, Roo.htmleditor.Filter,
28 * Clean up MS wordisms...
30 replaceTag : function(node)
33 // no idea what this does - span with text, replaceds with just text.
35 node.nodeName == 'SPAN' &&
36 !node.hasAttributes() &&
37 node.childNodes.length == 1 &&
38 node.firstChild.nodeName == "#text"
40 var textNode = node.firstChild;
41 node.removeChild(textNode);
42 if (node.getAttribute('lang') != 'zh-CN') { // do not space pad on chinese characters..
43 node.parentNode.insertBefore(node.ownerDocument.createTextNode(" "), node);
45 node.parentNode.insertBefore(textNode, node);
46 if (node.getAttribute('lang') != 'zh-CN') { // do not space pad on chinese characters..
47 node.parentNode.insertBefore(node.ownerDocument.createTextNode(" ") , node);
50 node.parentNode.removeChild(node);
51 return false; // dont do chidren - we have remove our node - so no need to do chdhilren?
56 if (node.tagName.toLowerCase().match(/^(style|script|applet|embed|noframes|noscript)$/)) {
57 node.parentNode.removeChild(node);
58 return false; // dont do chidlren
60 //Roo.log(node.tagName);
61 // remove - but keep children..
62 if (node.tagName.toLowerCase().match(/^(meta|link|\\?xml:|st1:|o:|v:|font)/)) {
63 //Roo.log('-- removed');
64 while (node.childNodes.length) {
65 var cn = node.childNodes[0];
67 node.parentNode.insertBefore(cn, node);
68 // move node to parent - and clean it..
69 if (cn.nodeType == 1) {
74 node.parentNode.removeChild(node);
75 /// no need to iterate chidlren = it's got none..
76 //this.iterateChildren(node, this.cleanWord);
77 return false; // no need to iterate children.
80 if (node.className.length) {
82 var cn = node.className.split(/\W+/);
84 Roo.each(cn, function(cls) {
85 if (cls.match(/Mso[a-zA-Z]+/)) {
90 node.className = cna.length ? cna.join(' ') : '';
92 node.removeAttribute("class");
96 if (node.hasAttribute("lang")) {
97 node.removeAttribute("lang");
100 if (node.hasAttribute("style")) {
102 var styles = node.getAttribute("style").split(";");
104 Roo.each(styles, function(s) {
108 var kv = s.split(":");
109 if (kv[0].match(/^(mso-|line|font|background|margin|padding|color)/)) {
112 // what ever is left... we allow.
115 node.setAttribute("style", nstyle.length ? nstyle.join(';') : '');
116 if (!nstyle.length) {
117 node.removeAttribute('style');
120 return true; // do children
126 styleToObject: function(node)
128 var styles = node.getAttribute("style").split(";");
130 Roo.each(styles, function(s) {
134 var kv = s.split(":");
136 // what ever is left... we allow.
143 replaceDocBullets : function(doc)
145 var listpara = doc.getElementsByClassName('MsoListParagraph');
146 while(listpara.length) {
147 this.replaceDocBullet(listpara.item(0));
152 replaceDocBullet : function(p)
154 // gather all the siblings.
156 parent = p.parentNode,
157 doc = parent.ownerDocument,
160 if (ns.nodeType != 1) {
164 if (!ns.className.match(/MsoListParagraph/i)) {
171 var ul = parent.ownerDocument.createElement('ul'); // what about number lists...
172 parent.insertBefore(ul, p);
176 items.forEach(function(n) {
177 parent.removeChild(n);
178 var spans = n.getElementsByTagName('span');
179 if (!spans.length || !n.isEqualNode(spans.item(0).parentNode)) {
180 return; // skip it...
183 var style = this.styleToObject(n);
184 if (typeof(style['mso-list']) == 'undefined') {
187 n.removeChild(spans.item(0)); // remove the fake bullet.
188 var nlvl = (style['mso-list'].split(' ')[1].replace(/level/,'') *1) - 1;
191 var nul = doc.createElement('ul'); // what about number lists...
192 last_li.appendChild(nul);
197 var nli = stack[nlvl].appendChild(doc.createElement('li'));
199 // copy children of p into nli
200 while(n.firstChild) {
201 var fc = n.firstChild;