Roo.htmleditor.FilterWord = function(cfg)
{
this.replaceDocBullets(cfg.node);
this.replaceAname(cfg.node);
}
Roo.extend(Roo.htmleditor.FilterWord, Roo.htmleditor.Filter,
{
tag: true,
replaceTag : function(node)
{
if(
node.nodeName == 'SPAN' &&
!node.hasAttributes() &&
node.childNodes.length == 1 &&
node.firstChild.nodeName == "#text"
) {
var textNode = node.firstChild;
node.removeChild(textNode);
if (node.getAttribute('lang') != 'zh-CN') { node.parentNode.insertBefore(node.ownerDocument.createTextNode(" "), node);
}
node.parentNode.insertBefore(textNode, node);
if (node.getAttribute('lang') != 'zh-CN') { node.parentNode.insertBefore(node.ownerDocument.createTextNode(" ") , node);
}
node.parentNode.removeChild(node);
return false; }
if (node.tagName.toLowerCase().match(/^(style|script|applet|embed|noframes|noscript)$/)) {
node.parentNode.removeChild(node);
return false; }
if (node.tagName.toLowerCase().match(/^(meta|link|\\?xml:|st1:|o:|v:|font)/)) {
while (node.childNodes.length) {
var cn = node.childNodes[0];
node.removeChild(cn);
node.parentNode.insertBefore(cn, node);
if (cn.nodeType == 1) {
this.replaceTag(cn);
}
}
node.parentNode.removeChild(node);
return false; }
if (node.className.length) {
var cn = node.className.split(/\W+/);
var cna = [];
Roo.each(cn, function(cls) {
if (cls.match(/Mso[a-zA-Z]+/)) {
return;
}
cna.push(cls);
});
node.className = cna.length ? cna.join(' ') : '';
if (!cna.length) {
node.removeAttribute("class");
}
}
if (node.hasAttribute("lang")) {
node.removeAttribute("lang");
}
if (node.hasAttribute("style")) {
var styles = node.getAttribute("style").split(";");
var nstyle = [];
Roo.each(styles, function(s) {
if (!s.match(/:/)) {
return;
}
var kv = s.split(":");
if (kv[0].match(/^(mso-|line|font|background|margin|padding|color)/)) {
return;
}
nstyle.push(s);
});
node.setAttribute("style", nstyle.length ? nstyle.join(';') : '');
if (!nstyle.length) {
node.removeAttribute('style');
}
}
return true; },
styleToObject: function(node)
{
var styles = (node.getAttribute("style") || '').split(";");
var ret = {};
Roo.each(styles, function(s) {
if (!s.match(/:/)) {
return;
}
var kv = s.split(":");
ret[kv[0].trim()] = kv[1];
});
return ret;
},
replaceAname : function (doc)
{
var aa = Array.from(doc.getElementsByTagName('a'));
for (var i = 0; i < aa.length; i++) {
var a = aa[i];
if (a.hasAttribute("name")) {
a.removeAttribute("name");
}
if (a.hasAttribute("href")) {
continue;
}
this.removeNodeKeepChildren(a);
}
},
replaceDocBullets : function(doc)
{
var listpara = doc.getElementsByClassName('MsoListParagraphCxSpFirst');
for( var i = 0; i < listpara.length; i ++) {
listpara.item(i).className = "MsoListParagraph";
}
var htwo = doc.getElementsByTagName('h2');
for( var i = 0; i < htwo.length; i ++) {
if (htwo.item(i).hasAttribute('style') && htwo.item(i).getAttribute('style').match(/mso-list:/)) {
htwo.item(i).className = "MsoListParagraph";
}
}
listpara = doc.getElementsByClassName('MsoNormal');
while(listpara.length) {
if (listpara.item(0).hasAttribute('style') && listpara.item(0).getAttribute('style').match(/mso-list:/)) {
listpara.item(0).className = "MsoListParagraph";
} else {
listpara.item(0).className = "MsoNormalx";
}
}
listpara = doc.getElementsByClassName('ql-indent-1');
while(listpara.length) {
this.replaceDocBullet(listpara.item(0));
}
listpara = doc.getElementsByClassName('MsoListParagraph');
while(listpara.length) {
this.replaceDocBullet(listpara.item(0));
}
},
replaceDocBullet : function(p)
{
var ns = p,
parent = p.parentNode,
doc = parent.ownerDocument,
items = [];
while (ns) {
if (ns.nodeType != 1) {
ns = ns.nextSibling;
continue;
}
if (!ns.className.match(/(MsoListParagraph|ql-indent-1)/i)) {
break;
}
if (ns.hasAttribute('style') && ns.getAttribute('style').match(/mso-list/)) {
items.push(ns);
ns = ns.nextSibling;
has_list = true;
continue;
}
var spans = ns.getElementsByTagName('span');
if (!spans.length) {
break;
}
var has_list = false;
for(var i = 0; i < spans.length; i++) {
if (spans[i].hasAttribute('style') && spans[i].getAttribute('style').match(/mso-list/)) {
has_list = true;
break;
}
}
if (!has_list) {
break;
}
items.push(ns);
ns = ns.nextSibling;
}
if (!items.length) {
ns.className = "";
return;
}
var ul = parent.ownerDocument.createElement('ul'); parent.insertBefore(ul, p);
var lvl = 0;
var stack = [ ul ];
var last_li = false;
var margin_to_depth = {};
max_margins = -1;
items.forEach(function(n, ipos) {
var spans = n.getElementsByTagName('span');
if (!spans.length) {
parent.removeChild(n);
return; }
var style = {};
for(var i = 0; i < spans.length; i++) {
style = this.styleToObject(spans[i]);
if (typeof(style['mso-list']) == 'undefined') {
continue;
}
spans[i].parentNode.removeChild(spans[i]); break;
}
style = this.styleToObject(n); if (typeof(style['mso-list']) == 'undefined') {
parent.removeChild(n);
return;
}
var margin = style['margin-left'];
if (typeof(margin_to_depth[margin]) == 'undefined') {
max_margins++;
margin_to_depth[margin] = max_margins;
}
nlvl = margin_to_depth[margin] ;
if (nlvl > lvl) {
var nul = doc.createElement('ul'); if (!last_li) {
last_li = doc.createElement('li');
stack[lvl].appendChild(last_li);
}
last_li.appendChild(nul);
stack[nlvl] = nul;
}
lvl = nlvl;
var nli = stack[nlvl].appendChild(doc.createElement('li'));
last_li = nli;
nli.innerHTML = n.innerHTML;
parent.removeChild(n);
},this);
}
});