From ffcaaa665acfd85b11962d05d899e56db1f3764f Mon Sep 17 00:00:00 2001 From: Alan Date: Wed, 18 Jan 2023 16:23:15 +0800 Subject: [PATCH] support empty tables wrapping images on word paste --- Roo/htmleditor/FilterWord.js | 60 ++++++++++++++++++++++++++++++++++-- docs/src/Roo.js.html | 21 +++++++------ roojs-bootstrap-debug.js | 60 ++++++++++++++++++++++++++++++++++-- roojs-bootstrap.js | 6 ++-- 4 files changed, 130 insertions(+), 17 deletions(-) diff --git a/Roo/htmleditor/FilterWord.js b/Roo/htmleditor/FilterWord.js index f8e695d124..a1705af2cb 100644 --- a/Roo/htmleditor/FilterWord.js +++ b/Roo/htmleditor/FilterWord.js @@ -17,7 +17,7 @@ Roo.htmleditor.FilterWord = function(cfg) this.replaceAname(cfg.node); // this is disabled as the removal is done by other filters; // this.walk(cfg.node); - + this.replaceImageTable(cfg.node); } @@ -371,8 +371,62 @@ Roo.extend(Roo.htmleditor.FilterWord, Roo.htmleditor.Filter, - } - + }, + replaceImageTable : function(doc) + { + /* + + + + + + + + +
+ */ + var imgs = Array.from(doc.getElementsByTagName('img')); + Roo.each(imgs, function(img) { + var td = img.parentNode; + if (td.nodeName != 'TD') { + return; + } + var tr = td.parentNode; + if (tr.nodeName != 'TR') { + return; + } + var tbody = tr.parentNode; + if (tbody.nodeName != 'TBODY') { + return; + } + var table = tbody.parentNode; + if (table.nodeName != 'TABLE') { + return; + } + // first row.. + + if (table.getElementsByTagName('tr').length != 2) { + return; + } + if (table.getElementsByTagName('td').length != 3) { + return; + } + if (table.innerText.trim() != '') { + return; + } + var p = table.parentNode; + img.parentNode.removeChild(img); + p.insertBefore(img, table); + p.removeChild(table); + + + + }); + + + } }); \ No newline at end of file diff --git a/docs/src/Roo.js.html b/docs/src/Roo.js.html index 4ebc34f3f5..0c87de8acd 100644 --- a/docs/src/Roo.js.html +++ b/docs/src/Roo.js.html @@ -505,15 +505,18 @@ Roo.factory(conf, Roo.data); return s.replace(/([.*+?^${}()|[\]\/\\])/g, "\\$1"); }, - // internal - callback : function(cb, scope, args, delay){ - if(typeof cb == "function"){ - if(delay){ - cb.defer(delay, scope, args || []); - }else{ - cb.apply(scope, args || []); - } - } + // internal (non-delayed, will get a return value..) + callback : function(cb, scope, args, delay) + { + if(typeof cb != "function"){ + return false; + } + if(delay){ + cb.defer(delay, scope, args || []); + return false + } + return cb.apply(scope, args || []); + }, /** diff --git a/roojs-bootstrap-debug.js b/roojs-bootstrap-debug.js index a4afb8e9e5..ab789813d1 100644 --- a/roojs-bootstrap-debug.js +++ b/roojs-bootstrap-debug.js @@ -26996,7 +26996,7 @@ Roo.htmleditor.FilterWord = function(cfg) this.replaceAname(cfg.node); // this is disabled as the removal is done by other filters; // this.walk(cfg.node); - + this.replaceImageTable(cfg.node); } @@ -27350,9 +27350,63 @@ Roo.extend(Roo.htmleditor.FilterWord, Roo.htmleditor.Filter, - } - + }, + replaceImageTable : function(doc) + { + /* + + + + + + + + +
+ */ + var imgs = Array.from(doc.getElementsByTagName('img')); + Roo.each(imgs, function(img) { + var td = img.parentNode; + if (td.nodeName != 'TD') { + return; + } + var tr = td.parentNode; + if (tr.nodeName != 'TR') { + return; + } + var tbody = tr.parentNode; + if (tbody.nodeName != 'TBODY') { + return; + } + var table = tbody.parentNode; + if (table.nodeName != 'TABLE') { + return; + } + // first row.. + + if (table.getElementsByTagName('tr').length != 2) { + return; + } + if (table.getElementsByTagName('td').length != 3) { + return; + } + if (table.innerText.trim() != '') { + return; + } + var p = table.parentNode; + img.parentNode.removeChild(img); + p.insertBefore(img, table); + p.removeChild(table); + + + + }); + + + } }); /** diff --git a/roojs-bootstrap.js b/roojs-bootstrap.js index dbad11c1fc..dd8aed631d 100644 --- a/roojs-bootstrap.js +++ b/roojs-bootstrap.js @@ -1182,7 +1182,7 @@ Roo.htmleditor.FilterTableWidth=function(A){this.tag=['TABLE','TD','TR','TH','TH }if(A.hasAttribute("style")){var B=A.getAttribute("style").split(";");var C=[];Roo.each(B,function(s){if(!s.match(/:/)){return;}var kv=s.split(":");if(kv[0].match(/^\s*(width|min-width)\s*$/)){return;}C.push(s);});A.setAttribute("style",C.length?C.join(';'):''); if(!C.length){A.removeAttribute('style');}}return true;}}); // Roo/htmleditor/FilterWord.js -Roo.htmleditor.FilterWord=function(A){this.replaceDocBullets(A.node);this.replaceAname(A.node);};Roo.extend(Roo.htmleditor.FilterWord,Roo.htmleditor.Filter,{tag:true,replaceTag:function(A){if(A.nodeName=='SPAN'&&!A.hasAttributes()&&A.childNodes.length==1&&A.firstChild.nodeName=="#text"){var B=A.firstChild; +Roo.htmleditor.FilterWord=function(A){this.replaceDocBullets(A.node);this.replaceAname(A.node);this.replaceImageTable(A.node);};Roo.extend(Roo.htmleditor.FilterWord,Roo.htmleditor.Filter,{tag:true,replaceTag:function(A){if(A.nodeName=='SPAN'&&!A.hasAttributes()&&A.childNodes.length==1&&A.firstChild.nodeName=="#text"){var B=A.firstChild; A.removeChild(B);if(A.getAttribute('lang')!='zh-CN'){A.parentNode.insertBefore(A.ownerDocument.createTextNode(" "),A);}A.parentNode.insertBefore(B,A);if(A.getAttribute('lang')!='zh-CN'){A.parentNode.insertBefore(A.ownerDocument.createTextNode(" "),A);}A.parentNode.removeChild(A); return false;}if(A.tagName.toLowerCase().match(/^(style|script|applet|embed|noframes|noscript)$/)){A.parentNode.removeChild(A);return false;}if(A.tagName.toLowerCase().match(/^(meta|link|\\?xml:|st1:|o:|v:|font)/)){while(A.childNodes.length){var cn=A.childNodes[0]; A.removeChild(cn);A.parentNode.insertBefore(cn,A);if(cn.nodeType==1){this.replaceTag(cn);}}A.parentNode.removeChild(A);return false;}if(A.className.length){var cn=A.className.split(/\W+/);var C=[];Roo.each(cn,function(F){if(F.match(/Mso[a-zA-Z]+/)){return; @@ -1199,7 +1199,9 @@ if(!E.length){break;}var F=false;for(var i=0;iG){var P=B.createElement(D);if(!I){I=B.createElement('li');H[G].appendChild(I);}I.appendChild(P);H[nlvl]=P;}G=nlvl;if(!H[nlvl].hasAttribute("start")&&D=="ol"){H[nlvl].setAttribute("start",M);}var Q=H[nlvl].appendChild(B.createElement('li')); -I=Q;Q.innerHTML=n.innerHTML;A.removeChild(n);},this);}}); +I=Q;Q.innerHTML=n.innerHTML;A.removeChild(n);},this);},replaceImageTable:function(A){var B=Array.from(A.getElementsByTagName('img'));Roo.each(B,function(C){var td=C.parentNode;if(td.nodeName!='TD'){return;}var tr=td.parentNode;if(tr.nodeName!='TR'){return; +}var D=tr.parentNode;if(D.nodeName!='TBODY'){return;}var E=D.parentNode;if(E.nodeName!='TABLE'){return;}if(E.getElementsByTagName('tr').length!=2){return;}if(E.getElementsByTagName('td').length!=3){return;}if(E.innerText.trim()!=''){return;}var p=E.parentNode; +C.parentNode.removeChild(C);p.insertBefore(C,E);p.removeChild(E);});}}); // Roo/htmleditor/FilterStyleToTag.js Roo.htmleditor.FilterStyleToTag=function(A){this.tags={B:['fontWeight','bold'],I:['fontStyle','italic'],SUP:['verticalAlign','super'],SUB:['verticalAlign','sub']};Roo.apply(this,A);this.walk(A.node);};Roo.extend(Roo.htmleditor.FilterStyleToTag,Roo.htmleditor.Filter,{tag:true,tags:false,replaceTag:function(A){if(A.getAttribute("style")===null){return true; }var B=[];for(var k in this.tags){if(A.style[this.tags[k][0]]==this.tags[k][1]){B.push(k);A.style.removeProperty(this.tags[k][0]);}}if(!B.length){return true;}var cn=Array.from(A.childNodes);var nn=A;Roo.each(B,function(t){var nc=A.ownerDocument.createElement(t); -- 2.39.2