word_paste.min.js 16 KB

1234567
  1. /*!
  2. * froala_editor v3.0.3 (https://www.froala.com/wysiwyg-editor)
  3. * License https://froala.com/wysiwyg-editor/terms/
  4. * Copyright 2014-2019 Froala Labs
  5. */
  6. !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(require("froala-editor")):"function"==typeof define&&define.amd?define(["froala-editor"],t):t(e.FroalaEditor)}(this,function(M){"use strict";M=M&&M.hasOwnProperty("default")?M["default"]:M,Object.assign(M.DEFAULTS,{wordDeniedTags:[],wordDeniedAttrs:[],wordAllowedStyleProps:["font-family","font-size","background","color","width","text-align","vertical-align","background-color","padding","margin","height","margin-top","margin-left","margin-right","margin-bottom","text-decoration","font-weight","font-style","text-indent","border","border-.*","line-height","list-style-type"],wordPasteModal:!0,wordPasteKeepFormatting:!0}),M.PLUGINS.wordPaste=function(y){var l,n,p=y.$,o="word_paste",c={};function t(e){var t=y.opts.wordAllowedStyleProps;e||(y.opts.wordAllowedStyleProps=[]),0===n.indexOf("<colgroup>")&&(n="<table>"+n+"</table>"),n=function g(e,t){0<=e.indexOf("<html")&&(e=e.replace(/[.\s\S\w\W<>]*(<html[^>]*>[.\s\S\w\W<>]*<\/html>)[.\s\S\w\W<>]*/i,"$1"));!function f(e){for(var t=e.split("v:shape"),r=1;r<t.length;r++){var i=t[r],n=i.split(' id="')[1];if(n&&1<n.length){n=n.split('"')[0];var a=i.split(' o:spid="')[1];a&&1<a.length&&(a=a.split('"')[0],c[n]=a)}}}(e);var r=(new DOMParser).parseFromString(e,"text/html"),i=r.head,n=r.body,l=function u(e){var t={},r=e.getElementsByTagName("style");if(r.length){var i=r[0],n=i.innerHTML.match(/[\S ]+\s+{[\s\S]+?}/gi);if(n)for(var a=0;a<n.length;a++){var l=n[a],o=l.replace(/([\S ]+\s+){[\s\S]+?}/gi,"$1"),s=l.replace(/[\S ]+\s+{([\s\S]+?)}/gi,"$1");o=o.replace(/^[\s]|[\s]$/gm,""),s=s.replace(/^[\s]|[\s]$/gm,""),o=o.replace(/\n|\r|\n\r/g,""),s=s.replace(/\n|\r|\n\r/g,"");for(var d=o.split(", "),f=0;f<d.length;f++)t[d[f]]=s}}return t}(i);h(n,function(e){if(e.nodeType===Node.TEXT_NODE&&/\n|\u00a0|\r/.test(e.data)){if(!/\S| /.test(e.data)&&!/[\u00a0]+/.test(e.data))return e.data===M.UNICODE_NBSP?(e.data="\u200b",!0):1===e.data.length&&10===e.data.charCodeAt(0)?(e.data=" ",!0):(C(e),!1);e.data=e.data.replace(/\n|\r/gi," ")}return!0}),h(n,function(e){return e.nodeType!==Node.ELEMENT_NODE||"V:IMAGEDATA"!==e.tagName&&"IMG"!==e.tagName||function d(e,t){if(!t)return;var r;if("IMG"===e.tagName){var i=e.getAttribute("src");if(!i||-1===i.indexOf("file://"))return;if(0===i.indexOf("file://")&&y.helpers.isURL(e.getAttribute("alt")))return void e.setAttribute("src",e.getAttribute("alt"));(r=c[e.getAttribute("v:shapes")])||(r=e.getAttribute("v:shapes"),e.parentNode&&e.parentNode.parentNode&&0<=e.parentNode.parentNode.innerHTML.indexOf("msEquation")&&(r=null))}else r=e.parentNode.getAttribute("o:spid");if(e.removeAttribute("height"),!r)return;!function o(e){m={},v(e,"i","\\shppict"),v(e,"s","\\shp{")}(t);var n=m[r.substring(7)];if(n){var a=function s(e){for(var t=e.match(/[0-9a-f]{2}/gi),r=[],i=0;i<t.length;i++)r.push(String.fromCharCode(parseInt(t[i],16)));var n=r.join("");return btoa(n)}(n.image_hex),l="data:"+n.image_type+";base64,"+a;"IMG"===e.tagName?(e.src=l,e.setAttribute("data-fr-image-pasted",!0)):p(e.parentNode).before('<img data-fr-image-pasted="true" src="'+l+'" style="'+e.parentNode.getAttribute("style")+'">').remove()}}(e,t),!0});for(var a=n.querySelectorAll("ul > ul, ul > ol, ol > ul, ol > ol"),o=a.length-1;0<=o;o--)a[o].previousElementSibling&&"LI"===a[o].previousElementSibling.tagName&&a[o].previousElementSibling.appendChild(a[o]);h(n,function(e){if(e.nodeType===Node.TEXT_NODE)return e.data=e.data.replace(/<br>(\n|\r)/gi,"<br>"),!1;if(e.nodeType===Node.ELEMENT_NODE){if(A(e)){var t=e.parentNode,r=e.previousSibling,i=function E(e,t,r){var i=/[0-9a-zA-Z]./gi;var n=!1;var a;var l;var o;var s=e.querySelector('span[style="mso-list:Ignore"]');var d;var f;var u;var g;s&&(n=n||i.test(s.textContent));if(1==n){var p=s.textContent.trim().split(".")[0];"a"==p?g="lower-alpha;":"A"==p?g="upper-alpha;":"1"==p?g="decimal;":"i"==p?g="lower-roman;":"I"==p?g="upper-roman;":"o"==p&&(g="circle;"),g="list-style-type: "+g,u="ol"}else u="ul";var c=g?"<"+u+' style = "'+g+'">':"<"+u+">";for(;e;){if(!A(e)){if(e.outerHTML&&0<e.outerHTML.indexOf("mso-bookmark")&&0==(e.textContent||"").length){e=e.nextElementSibling;continue}break}var h=e.getAttribute("style").replace(/\n/gi,"").replace(/.*level([0-9]+?).*/gi,"$1");if((r=r||h)<h)f=E(e,t,h),c+=f.el.outerHTML,e=f.currentNode;else{if(h<r)break;e.firstElementChild&&e.firstElementChild.firstElementChild&&e.firstElementChild.firstElementChild.firstChild&&(i.lastIndex=0,a=i.test(e.firstElementChild.firstElementChild.firstChild.data||e.firstElementChild.firstElementChild.firstChild.firstChild.data||"")),o&&o.firstElementChild&&o.firstElementChild.firstElementChild&&o.firstElementChild.firstElementChild.firstChild&&(i.lastIndex=0,l=i.test(o.firstElementChild.firstElementChild.firstChild.data||o.firstElementChild.firstElementChild.firstChild.firstChild.data||"")),l===undefined||l===a?(d=N(e,t),c+="<li>"+d+"</li>"):(f=E(e,t,h),c+=f.el.outerHTML,e=f.currentNode);var m=e&&e.nextElementSibling;m&&(o=m.previousElementSibling),e&&e.parentNode&&e.parentNode.removeChild(e),e=m}}c+="</"+u+">";var v=document.createElement("div");v.innerHTML=c;var b=v.firstElementChild;return{el:b,currentNode:e}}(e,l).el,n=null;return(n=r?r.nextSibling:t.firstChild)?t.insertBefore(i,n):t.appendChild(i),!1}return"FONT"===e.tagName&&l["."+e.getAttribute("class")]&&(e=x(e,"span")),b(e,l)}if(e.nodeType!==Node.COMMENT_NODE)return!0;if(-1<e.data.indexOf("[if !supportLineBreakNewLine]"))for(var a=e.nextSibling;a;)(a=e.nextSibling)&&C(a),a.data&&-1<a.data.indexOf("[endif]")&&(a=null);return C(e),!1}),h(n,function(e){if(e.nodeType===Node.ELEMENT_NODE){var t=e.tagName;if(!e.innerHTML&&-1===["BR","IMG"].indexOf(t)){for(var r=e.parentNode;r&&(C(e),!(e=r).innerHTML);)r=e.parentNode;return!1}!function u(e){var t=e.getAttribute("style");if(!t)return;(t=w(t))&&";"!==t.slice(-1)&&(t+=";");var r=t.match(/(^|\S+?):.+?;{1,1}/gi);if(!r)return;for(var i={},n=0;n<r.length;n++){var a=r[n],l=a.split(":");2===l.length&&("text-align"===l[0]&&"SPAN"===e.tagName||(i[l[0]]=l[1]))}var o="";for(var s in i)if(i.hasOwnProperty(s)){if("font-size"===s&&"pt;"===i[s].slice(-3)){var d=null;try{d=parseFloat(i[s].slice(0,-3),10)}catch(f){d=null}d&&(d=Math.round(1.33*d),i[s]=d+"px;")}o+=s+":"+i[s]}o&&e.setAttribute("style",o)}(e)}return!0}),h(n,function(e){if(e&&"A"===e.nodeName&&""===e.href){for(var t=document.createDocumentFragment();e.firstChild;)t.appendChild(e.firstChild);e.parentNode.replaceChild(t,e)}return!0});var s=n.outerHTML,d=y.opts.htmlAllowedStyleProps;return y.opts.htmlAllowedStyleProps=y.opts.wordAllowedStyleProps,s=y.clean.html(s,y.opts.wordDeniedTags,y.opts.wordDeniedAttrs,!1),y.opts.htmlAllowedStyleProps=d,s}(n=n.replace(/<span[\n\r ]*style='mso-spacerun:yes'>([\r\n\u00a0 ]*)<\/span>/g,function(e,t){for(var r="",i=0;i++<t.length;)r+="&nbsp;";return r}),y.paste.getRtfClipboard());var r=y.doc.createElement("DIV");r.innerHTML=n,y.html.cleanBlankSpaces(r),n=r.innerHTML,n=(n=y.paste.cleanEmptyTagsAndDivs(n)).replace(/\u200b/g,""),function i(){y.modals.hide(o)}(),y.paste.clean(n,!0,!0),y.opts.wordAllowedStyleProps=t}function C(e){e.parentNode&&e.parentNode.removeChild(e)}function h(e,t){if(t(e))for(var r=e.firstChild;r;){var i=r,n=r.previousSibling;r=r.nextSibling,h(i,t),i.previousSibling||i.nextSibling||i.parentNode||!r||n===r.previousSibling||!r.parentNode?i.previousSibling||i.nextSibling||i.parentNode||!r||r.previousSibling||r.nextSibling||r.parentNode||(n?r=n.nextSibling?n.nextSibling.nextSibling:null:e.firstChild&&(r=e.firstChild.nextSibling)):r=n?n.nextSibling:e.firstChild}}function A(e){if(!e.getAttribute("style")||!/mso-list:[\s]*l/gi.test(e.getAttribute("style").replace(/\n/gi,"")))return!1;try{if(!e.querySelector('[style="mso-list:Ignore"]'))return!!(e.outerHTML&&0<=e.outerHTML.indexOf("\x3c!--[if !supportLists]--\x3e"))}catch(t){return!1}return!0}function N(e,i){var t=e.cloneNode(!0);if(-1!==["H1","H2","H3","H4","H5","H6"].indexOf(e.tagName)){var r=document.createElement(e.tagName.toLowerCase());r.setAttribute("style",e.getAttribute("style")),r.innerHTML=t.innerHTML,t.innerHTML=r.outerHTML}h(t,function(e){if(e.nodeType==Node.COMMENT_NODE&&(y.browser.msie||y.browser.safari||y.browser.edge))try{if("[if !supportLists]"===e.data){for(e=e.nextSibling;e&&e.nodeType!==Node.COMMENT_NODE;){var t=e.nextSibling;e.parentNode.removeChild(e),e=t}e&&e.nodeType==Node.COMMENT_NODE&&e.parentNode.removeChild(e)}}catch(r){}return e.nodeType===Node.ELEMENT_NODE&&("mso-list:Ignore"===e.getAttribute("style")&&e.parentNode.removeChild(e),e.setAttribute("style",function n(e){var r="",i=e.getAttribute("style");i&&["line-height","font-family","font-size","color","background"].forEach(function(e){var t=i.match(new RegExp(e+":.*;"));t&&(r+=t[0]+";")});return r}(e)),b(e,i)),!0});var n=t.innerHTML;return n=n.replace(/<!--[\s\S]*?-->/gi,"")}function x(e,t){for(var r=document.createElement(t),i=0;i<e.attributes.length;i++){var n=e.attributes[i].name;r.setAttribute(n,e.getAttribute(n))}return r.innerHTML=e.innerHTML,e.parentNode.replaceChild(r,e),r}function T(e){var t=e.getAttribute("align");t&&(e.style["text-align"]=t,e.removeAttribute("align"))}function w(e){return e.replace(/\n|\r|\n\r|&quot;/g,"")}function S(e,t,r){if(t){var i=e.getAttribute("style");i&&";"!==i.slice(-1)&&(i+=";"),t&&";"!==t.slice(-1)&&(t+=";"),t=t.replace(/\n/gi,"");var n=null;n=r?(i||"")+t:t+(i||""),e.setAttribute("style",n)}}var m=null;function v(e,t,r){for(var i=e.split(r),n=1;n<i.length;n++){var a=i[n];if(1<(a=a.split("shplid")).length){a=a[1];for(var l="",o=0;o<a.length&&"\\"!==a[o]&&"{"!==a[o]&&" "!==a[o]&&"\r"!==a[o]&&"\n"!==a[o];)l+=a[o],o++;var s=a.split("bliptag");if(s&&s.length<2)continue;var d=null;if(-1!==s[0].indexOf("pngblip")?d="image/png":-1!==s[0].indexOf("jpegblip")&&(d="image/jpeg"),!d)continue;var f=s[1].split("}");if(f&&f.length<2)continue;var u=void 0;if(2<f.length&&-1!==f[0].indexOf("blipuid"))u=f[1].split(" ");else{if((u=f[0].split(" "))&&u.length<2)continue;u.shift()}var g=u.join("");m[t+l]={image_hex:g,image_type:d}}}}function b(e,t){var r=e.tagName,n=r.toLowerCase();e.firstElementChild&&("I"===e.firstElementChild.tagName?x(e.firstElementChild,"em"):"B"===e.firstElementChild.tagName&&x(e.firstElementChild,"strong"));if(-1!==["SCRIPT","APPLET","EMBED","NOFRAMES","NOSCRIPT"].indexOf(r))return C(e),!1;for(var a=["META","LINK","XML","ST1:","O:","W:","FONT"],l=0;l<a.length;l++)if(-1!==r.indexOf(a[l]))return e.innerHTML&&(e.outerHTML=e.innerHTML),C(e),!1;if("TD"!==r){var o=e.getAttribute("class")||"MsoNormal";if(t&&o){for(var s=(o=w(o)).split(" "),d=0;d<s.length;d++){var f=[],u="."+s[d];f.push(u),u=n+u,f.push(u);for(var g=0;g<f.length;g++)t[f[g]]&&S(e,t[f[g]])}e.removeAttribute("class")}t&&t[n]&&S(e,t[n])}if(-1!==["P","H1","H2","H3","H4","H5","H6","PRE"].indexOf(r)){var p=e.getAttribute("class");if(p&&(t&&t[r.toLowerCase()+"."+p]&&S(e,t[r.toLowerCase()+"."+p]),-1!==p.toLowerCase().indexOf("mso"))){var c=w(p);(c=c.replace(/[0-9a-z-_]*mso[0-9a-z-_]*/gi,""))?e.setAttribute("class",c):e.removeAttribute("class")}var h=e.getAttribute("style");if(h){var m=h.match(/text-align:.+?[; "]{1,1}/gi);m&&m[m.length-1].replace(/(text-align:.+?[; "]{1,1})/gi,"$1")}T(e)}if("TR"===r&&function N(e,t){y.node.clearAttributes(e);for(var r=e.firstElementChild,i=0,n=!1,a=null;r;){r.firstElementChild&&-1!==r.firstElementChild.tagName.indexOf("W:")&&(r.innerHTML=r.firstElementChild.innerHTML),(a=r.getAttribute("width"))||n||(n=!0),i+=parseInt(a,10),(!r.firstChild||r.firstChild&&r.firstChild.data===M.UNICODE_NBSP)&&(r.firstChild&&C(r.firstChild),r.innerHTML="<br>");for(var l=r.firstElementChild,o=1===r.children.length;l;)"P"!==l.tagName||A(l)||o&&T(l),l=l.nextElementSibling;if(t){var s=r.getAttribute("class");if(s){var d=(s=w(s)).match(/xl[0-9]+/gi);if(d){var f="."+d[0];t[f]&&S(r,t[f])}}t.td&&S(r,t.td)}var u=r.getAttribute("style");u&&(u=w(u))&&";"!==u.slice(-1)&&(u+=";");var g=r.getAttribute("valign");if(!g&&u){var p=u.match(/vertical-align:.+?[; "]{1,1}/gi);p&&(g=p[p.length-1].replace(/vertical-align:(.+?)[; "]{1,1}/gi,"$1"))}var c=null;if(u){var h=u.match(/text-align:.+?[; "]{1,1}/gi);h&&(c=h[h.length-1].replace(/text-align:(.+?)[; "]{1,1}/gi,"$1")),"general"===c&&(c=null)}var m=null;if(u){var v=u.match(/background:.+?[; "]{1,1}/gi);v&&(m=v[v.length-1].replace(/background:(.+?)[; "]{1,1}/gi,"$1"))}var b=r.getAttribute("colspan"),E=r.getAttribute("rowspan");b&&r.setAttribute("colspan",b),E&&r.setAttribute("rowspan",E),g&&(r.style["vertical-align"]=g),c&&(r.style["text-align"]=c),m&&(r.style["background-color"]=m),a&&r.setAttribute("width",a),r=r.nextElementSibling}for(r=e.firstElementChild;r;)a=r.getAttribute("width"),n?r.removeAttribute("width"):r.setAttribute("width",100*parseInt(a,10)/i+"%"),r=r.nextElementSibling}(e,t),"A"!==r||e.attributes.getNamedItem("href")||e.attributes.getNamedItem("name")||!e.innerHTML||(e.outerHTML=e.innerHTML),"A"==r&&e.getAttribute("href")&&e.querySelector("img")){var v=e.querySelectorAll("span");for(i=0;i<v.length;i++)v[i].innerText||(v[i].outerHTML=v[i].innerHTML)}if("TD"!==r&&"TH"!==r||e.innerHTML||(e.innerHTML="<br>"),"TABLE"===r&&(e.style.width=e.style.width),e.getAttribute("lang")&&e.removeAttribute("lang"),e.getAttribute("style")&&-1!==e.getAttribute("style").toLowerCase().indexOf("mso")){var b=w(e.getAttribute("style"));(b=b.replace(/[0-9a-z-_]*mso[0-9a-z-_]*:.+?(;{1,1}|$)/gi,""))?e.setAttribute("style",b):e.removeAttribute("style")}return!0}return{_init:function e(){y.events.on("paste.wordPaste",function(e){return n=e,y.opts.wordPasteModal?function a(){if(!l){var e='<h4><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 74.95 73.23" style="height: 25px; vertical-align: text-bottom; margin-right: 5px; display: inline-block"><defs><style>.a{fill:#2a5699;}.b{fill:#fff;}</style></defs><path class="a" d="M615.15,827.22h5.09V834c9.11.05,18.21-.09,27.32.05a2.93,2.93,0,0,1,3.29,3.25c.14,16.77,0,33.56.09,50.33-.09,1.72.17,3.63-.83,5.15-1.24.89-2.85.78-4.3.84-8.52,0-17,0-25.56,0v6.81h-5.32c-13-2.37-26-4.54-38.94-6.81q0-29.8,0-59.59c13.05-2.28,26.11-4.5,39.17-6.83Z" transform="translate(-575.97 -827.22)"/><path class="b" d="M620.24,836.59h28.1v54.49h-28.1v-6.81h22.14v-3.41H620.24v-4.26h22.14V873.2H620.24v-4.26h22.14v-3.41H620.24v-4.26h22.14v-3.41H620.24v-4.26h22.14v-3.41H620.24V846h22.14v-3.41H620.24Zm-26.67,15c1.62-.09,3.24-.16,4.85-.25,1.13,5.75,2.29,11.49,3.52,17.21,1-5.91,2-11.8,3.06-17.7,1.7-.06,3.41-.15,5.1-.26-1.92,8.25-3.61,16.57-5.71,24.77-1.42.74-3.55,0-5.24.09-1.13-5.64-2.45-11.24-3.47-16.9-1,5.5-2.29,10.95-3.43,16.42q-2.45-.13-4.92-.3c-1.41-7.49-3.07-14.93-4.39-22.44l4.38-.18c.88,5.42,1.87,10.82,2.64,16.25,1.2-5.57,2.43-11.14,3.62-16.71Z" transform="translate(-575.97 -827.22)"/></svg> '+y.language.translate("Word Paste Detected")+"</h4>",t=function n(){var e='<div class="fr-word-paste-modal" style="padding: 20px 20px 10px 20px;">';return e+='<p style="text-align: left;">'+y.language.translate("The pasted content is coming from a Microsoft Word document. Do you want to keep the format or clean it up?")+"</p>",e+='<div style="text-align: right; margin-top: 50px;"><button class="fr-remove-word fr-command">'+y.language.translate("Clean")+'</button> <button class="fr-keep-word fr-command">'+y.language.translate("Keep")+"</button></div>",e+="</div>"}(),r=y.modals.create(o,e,t),i=r.$body;l=r.$modal,r.$modal.addClass("fr-middle"),y.events.bindClick(i,"button.fr-remove-word",function(){var e=l.data("instance")||y;e.wordPaste.clean()}),y.events.bindClick(i,"button.fr-keep-word",function(){var e=l.data("instance")||y;e.wordPaste.clean(!0)}),y.events.$on(p(y.o_win),"resize",function(){y.modals.resize(o)})}y.modals.show(o),y.modals.resize(o)}():t(y.opts.wordPasteKeepFormatting),!1})},clean:t}}});