From 43096db81f2b7f14c56bbbffc296c18e5d226427 Mon Sep 17 00:00:00 2001 From: Alan Date: Wed, 18 Jan 2023 16:23:37 +0800 Subject: [PATCH] support empty tables wrapping images on word paste --- docs/src/Roo_htmleditor_FilterWord.js.html | 60 ++++++++++++++++++++-- roojs-all.js | 6 ++- roojs-debug.js | 60 ++++++++++++++++++++-- roojs-ui-debug.js | 60 ++++++++++++++++++++-- roojs-ui.js | 6 ++- 5 files changed, 179 insertions(+), 13 deletions(-) diff --git a/docs/src/Roo_htmleditor_FilterWord.js.html b/docs/src/Roo_htmleditor_FilterWord.js.html index bc4414698b..2bb90c315f 100644 --- a/docs/src/Roo_htmleditor_FilterWord.js.html +++ b/docs/src/Roo_htmleditor_FilterWord.js.html @@ -17,9 +17,9 @@ this.replaceAname(cfg.node); // this is disabled as the removal is done by other filters; // this.walk(cfg.node); + this.replaceImageTable(cfg.node); - -} +} Roo.extend(Roo.htmleditor.FilterWord, Roo.htmleditor.Filter, { @@ -371,8 +371,62 @@ - } + }, + + replaceImageTable : function(doc) + { + /* + <table cellpadding=0 cellspacing=0 align=left> + <tr> + <td width=423 height=0></td> + </tr> + <tr> + <td></td> + <td><img width=601 height=401 + src="file:///C:/Users/Alan/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg" + v:shapes="Picture_x0020_2"></td> + </tr> + </table> + */ + 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/roojs-all.js b/roojs-all.js index f722969845..08e4b493b7 100644 --- a/roojs-all.js +++ b/roojs-all.js @@ -1927,7 +1927,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; @@ -1944,7 +1944,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); diff --git a/roojs-debug.js b/roojs-debug.js index 7658466239..be87bffa10 100644 --- a/roojs-debug.js +++ b/roojs-debug.js @@ -46163,7 +46163,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); } @@ -46517,9 +46517,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-ui-debug.js b/roojs-ui-debug.js index 55c765320b..1fff0c8ad2 100644 --- a/roojs-ui-debug.js +++ b/roojs-ui-debug.js @@ -21661,7 +21661,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); } @@ -22015,9 +22015,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-ui.js b/roojs-ui.js index b24d21446a..220dacbadb 100644 --- a/roojs-ui.js +++ b/roojs-ui.js @@ -980,7 +980,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; @@ -997,7 +997,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