///import core ///plugin 编辑器默认的过滤转换机制 ue.plugins['defaultfilter'] = function () { var me = this; me.setopt({ 'allowdivtranstop':true, 'disabledtableintable':true, 'rgb2hex':true }); //默认的过滤处理 //进入编辑器的内容处理 me.addinputrule(function (root) { var allowdivtranstop = this.options.allowdivtranstop; var val; function tdparent(node){ while(node && node.type == 'element'){ if(node.tagname == 'td'){ return true; } node = node.parentnode; } return false; } //进行默认的处理 root.traversal(function (node) { if (node.type == 'element') { if (!ue.dom.dtd.$cdata[node.tagname] && me.options.autoclearemptynode && ue.dom.dtd.$inline[node.tagname] && !ue.dom.dtd.$empty[node.tagname] && (!node.attrs || ue.utils.isemptyobject(node.attrs))) { if (!node.firstchild()) node.parentnode.removechild(node); else if (node.tagname == 'span' && (!node.attrs || ue.utils.isemptyobject(node.attrs))) { node.parentnode.removechild(node, true) } return; } switch (node.tagname) { case 'style': case 'script': node.setattr({ cdata_tag: node.tagname, cdata_data: (node.innerhtml() || ''), '_ue_custom_node_':'true' }); node.tagname = 'div'; node.innerhtml(''); break; case 'a': if (val = node.getattr('href')) { node.setattr('_href', val) } break; case 'img': //todo base64暂时去掉,后边做远程图片上传后,干掉这个 // if (val = node.getattr('src')) { // if (/^data:/.test(val)) { // node.parentnode.removechild(node); // break; // } // } node.setattr('onerror', ''); node.setattr('_src', node.getattr('src')); break; case 'span': if (ue.browser.webkit && (val = node.getstyle('white-space'))) { if (/nowrap|normal/.test(val)) { node.setstyle('white-space', ''); if (me.options.autoclearemptynode && ue.utils.isemptyobject(node.attrs)) { node.parentnode.removechild(node, true) } } } val = node.getattr('id'); if(val && /^_baidu_bookmark_/i.test(val)){ node.parentnode.removechild(node) } break; case 'p': if (val = node.getattr('align')) { node.setattr('align'); node.setstyle('text-align', val) } //trace:3431 // var cssstyle = node.getattr('style'); // if (cssstyle) { // cssstyle = cssstyle.replace(/(margin|padding)[^;]+/g, ''); // node.setattr('style', cssstyle) // // } //p标签不允许嵌套 ue.utils.each(node.children,function(n){ if(n.type == 'element' && n.tagname == 'p'){ var next = n.nextsibling(); node.parentnode.insertafter(n,node); var last = n; while(next){ var tmp = next.nextsibling(); node.parentnode.insertafter(next,last); last = next; next = tmp; } return false; } }); if (!node.firstchild()) { node.innerhtml(ue.browser.ie ? ' ' : '
') } break; case 'div': if(node.getattr('cdata_tag')){ break; } //针对代码这里不处理插入代码的div val = node.getattr('class'); if(val && /^line number\d+/.test(val)){ break; } if(!allowdivtranstop){ break; } var tmpnode, p = ue.unode.createelement('p'); while (tmpnode = node.firstchild()) { if (tmpnode.type == 'text' || !ue.dom.dtd.$block[tmpnode.tagname]) { p.appendchild(tmpnode); } else { if (p.firstchild()) { node.parentnode.insertbefore(p, node); p = ue.unode.createelement('p'); } else { node.parentnode.insertbefore(tmpnode, node); } } } if (p.firstchild()) { node.parentnode.insertbefore(p, node); } node.parentnode.removechild(node); break; case 'dl': node.tagname = 'ul'; break; case 'dt': case 'dd': node.tagname = 'li'; break; case 'li': var classname = node.getattr('class'); if (!classname || !/list\-/.test(classname)) { node.setattr() } var tmpnodes = node.getnodesbytagname('ol ul'); ue.utils.each(tmpnodes, function (n) { node.parentnode.insertafter(n, node); }); break; case 'td': case 'th': case 'caption': if(!node.children || !node.children.length){ node.appendchild(ue.browser.ie11below ? ue.unode.createtext(' ') : ue.unode.createelement('br')) } break; case 'table': if(me.options.disabledtableintable && tdparent(node)){ node.parentnode.insertbefore(ue.unode.createtext(node.innertext()),node); node.parentnode.removechild(node) } } } // if(node.type == 'comment'){ // node.parentnode.removechild(node); // } }) }); //从编辑器出去的内容处理 me.addoutputrule(function (root) { var val; root.traversal(function (node) { if (node.type == 'element') { if (me.options.autoclearemptynode && ue.dom.dtd.$inline[node.tagname] && !ue.dom.dtd.$empty[node.tagname] && (!node.attrs || ue.utils.isemptyobject(node.attrs))) { if (!node.firstchild()) node.parentnode.removechild(node); else if (node.tagname == 'span' && (!node.attrs || ue.utils.isemptyobject(node.attrs))) { node.parentnode.removechild(node, true) } return; } switch (node.tagname) { case 'div': if (val = node.getattr('cdata_tag')) { node.tagname = val; node.appendchild(ue.unode.createtext(node.getattr('cdata_data'))); node.setattr({cdata_tag: '', cdata_data: '','_ue_custom_node_':''}); } break; case 'a': if (val = node.getattr('_href')) { node.setattr({ 'href': ue.utils.html(val), '_href': '' }) } break; break; case 'span': val = node.getattr('id'); if(val && /^_baidu_bookmark_/i.test(val)){ node.parentnode.removechild(node) } //将color的rgb格式转换为#16进制格式 if (me.getopt('rgb2hex')) { var cssstyle = node.getattr('style'); if (cssstyle) { node.setattr('style', cssstyle.replace(/rgba?\(([\d,\s]+)\)/g, function (a, value) { var values = value .replace(/rgba?\(/, '') .replace(/\)/, '') .replace(/[\s+]/g, '') .split(','); var a = parsefloat(values[3] || 1), r = math.floor(a * parseint(values[0]) + (1 - a) * 255), g = math.floor(a * parseint(values[1]) + (1 - a) * 255), b = math.floor(a * parseint(values[2]) + (1 - a) * 255); value = "#" + ("0" + r.tostring(16)).slice(-2) + ("0" + g.tostring(16)).slice(-2) + ("0" + b.tostring(16)).slice(-2); return value.touppercase() + ";"; })) } } break; case 'img': if (val = node.getattr('_src')) { node.setattr({ 'src': node.getattr('_src'), '_src': '' }) } } } }) }); };