mirror of
https://github.com/the-djmaze/snappymail.git
synced 2025-01-09 00:08:18 +08:00
1 line
46 KiB
JavaScript
1 line
46 KiB
JavaScript
(e=>{"use strict";const t=3,n=9,o=e.defaultView,r=navigator.userAgent,i=/Mac OS X/.test(r),s=/Windows NT/.test(r),a=/iP(?:ad|hone|od)/.test(r)||i&&!!navigator.maxTouchPoints,l=/Gecko\//.test(r),d=/Edge\//.test(r),c=!d&&/WebKit\//.test(r),h=i?"meta-":"ctrl-",f=i?"metaKey":"ctrlKey",u=/[^ \t\r\n]/,p=(e,t)=>Array.prototype.indexOf.call(e,t),g={1:1,2:2,3:4,8:128,9:256,11:1024},m=/^(?:#text|A|ABBR|ACRONYM|B|BR|BD[IO]|CITE|CODE|DATA|DEL|DFN|EM|FONT|HR|IMG|INPUT|INS|KBD|Q|RP|RT|RUBY|SAMP|SMALL|SPAN|STR(IKE|ONG)|SU[BP]|TIME|U|VAR|WBR)$/,S={BR:1,HR:1,IMG:1},N=e=>1===e.nodeType&&!!S[e.nodeName],_=e=>{switch(e.nodeType){case t:return 1;case 1:case 11:if(re.has(e))return re.get(e);break;default:return 0}let n;return n=Array.prototype.every.call(e.childNodes,C)?m.test(e.nodeName)?1:2:3,re.set(e,n),n},C=e=>1===_(e),v=e=>2===_(e),T=e=>3===_(e),y=(t,n)=>{let o=e.createTreeWalker(n,1,v);return o.currentNode=t,o},E=(e,t)=>(e=y(e,t).previousNode())!==t?e:null,L=(e,t)=>(e=y(e,t).nextNode())!==t?e:null,b=e=>!e.textContent&&!e.querySelector("IMG"),k=(e,t)=>!N(e)&&e.nodeType===t.nodeType&&e.nodeName===t.nodeName&&"A"!==e.nodeName&&e.className===t.className&&(!e.style&&!t.style||e.style.cssText===t.style.cssText),O=(e,t,n)=>{if(e.nodeName!==t)return!1;for(let t in n)if(e.getAttribute(t)!==n[t])return!1;return!0},x=(e,t,n)=>(e=(e=!e||e.closest?e:e.parentElement)&&e.closest(n))&&t.contains(e)?e:null,A=(e,t,n,o)=>{for(;e&&e!==t;){if(O(e,n,o))return e;e=e.parentNode}return null},R=(e,t,n)=>{let o,r,i,s,a,l="";return e&&e!==t&&(l=R(e.parentNode,t,n),1===e.nodeType&&(l+=(l?">":"")+e.nodeName,(o=e.id)&&(l+="#"+o),(r=e.className.trim())&&((i=r.split(/\s+/)).sort(),l+=".",l+=i.join(".")),(s=e.dir)&&(l+="[dir="+s+"]"),i&&(a=n.classNames,p(i,a.highlight)>-1&&(l+="[backgroundColor="+e.style.backgroundColor.replace(/ /g,"")+"]"),p(i,a.colour)>-1&&(l+="[color="+e.style.color.replace(/ /g,"")+"]"),p(i,a.fontFamily)>-1&&(l+="[fontFamily="+e.style.fontFamily.replace(/ /g,"")+"]"),p(i,a.fontSize)>-1&&(l+="[fontSize="+e.style.fontSize+"]")))),l},B=e=>{let t=e.nodeType;return 1===t||11===t?e.childNodes.length:e.length||0},D=t=>{let n=e.createDocumentFragment(),o=t.childNodes;return o&&n.append(...o),n},U=(e,t,n,o)=>{let r,i,s=e.createElement(t);if(n instanceof Array&&(o=n,n=null),n)for(r in n)void 0!==(i=n[r])&&s.setAttribute(r,i);return o&&s.append(...o),s},P=(n,o)=>{let r,i,s=o.__squire__,a=n;if(n===o&&((i=n.firstChild)&&"BR"!==i.nodeName||(r=s.createDefaultBlock(),i?i.replaceWith(r):n.append(r),n=r,r=null)),n.nodeType===t)return a;if(C(n)){for(i=n.firstChild;c&&i&&i.nodeType===t&&!i.data;)i.remove(),i=n.firstChild;i||(c?(r=e.createTextNode(""),s._didAddZWS()):r=e.createTextNode(""))}else if(!n.querySelector("BR"))for(r=U(e,"BR");(i=n.lastElementChild)&&!C(i);)n=i;if(r)try{n.append(r)}catch(e){s.didError({name:"Squire: fixCursor – "+e,message:"Parent: "+n.nodeName+"/"+n.innerHTML+" appendChild: "+r.nodeName})}return a},I=(t,n)=>{let o,r,i,s,a=t.childNodes,l=null;for(o=0,r=a.length;o<r;++o)!(s="BR"===(i=a[o]).nodeName)&&C(i)?(l||(l=U(e,"div")),l.append(i),--o,--r):(s||l)&&(l||(l=U(e,"div")),P(l,n),s?i.replaceWith(l):(i.before(l),++o,++r),l=null),T(i)&&I(i,n);return l&&t.append(P(l,n)),t},F=(e,n,o,r)=>{let i,s,a,l=e.nodeType;if(l===t&&e!==o)return F(e.parentNode,e.splitText(n),o,r);if(1===l){if("number"==typeof n&&(n=n<e.childNodes.length?e.childNodes[n]:null),e===o)return n;for(i=e.parentNode,s=e.cloneNode(!1);n;)a=n.nextSibling,s.append(n),n=a;return"OL"===e.nodeName&&x(e,r,"BLOCKQUOTE")&&(s.start=(+e.start||1)+e.childNodes.length-1),P(e,r),P(s,r),e.after(s),F(i,s,o,r)}return n},W=(e,n)=>{let o,r,i=e.childNodes,s=i.length,a=[];for(;s--;)o=i[s],r=s&&i[s-1],s&&C(o)&&k(o,r)&&!S[o.nodeName]?(n.startContainer===o&&(n.startContainer=r,n.startOffset+=B(r)),n.endContainer===o&&(n.endContainer=r,n.endOffset+=B(r)),n.startContainer===e&&(n.startOffset>s?--n.startOffset:n.startOffset===s&&(n.startContainer=r,n.startOffset=B(r))),n.endContainer===e&&(n.endOffset>s?--n.endOffset:n.endOffset===s&&(n.endContainer=r,n.endOffset=B(r))),o.remove(),o.nodeType===t?r.appendData(o.data):a.push(D(o))):1===o.nodeType&&(o.append(...a.reverse()),a=[],W(o,n))},M=(e,n)=>{if(e.nodeType===t&&(e=e.parentNode),1===e.nodeType){let t={startContainer:n.startContainer,startOffset:n.startOffset,endContainer:n.endContainer,endOffset:n.endOffset};W(e,t),n.setStart(t.startContainer,t.startOffset),n.setEnd(t.endContainer,t.endOffset)}},w=(e,t,n,o)=>{let r,i,s,a=t;for(;(r=a.parentNode)&&r!==o&&1===r.nodeType&&1===r.childNodes.length;)a=r;a.remove(),s=e.childNodes.length,(i=e.lastChild)&&"BR"===i.nodeName&&(i.remove(),--s),e.append(D(t)),n.setStart(e,s),n.collapse(!0),M(e,n)},H=(t,n)=>{let o,r,i=t.previousSibling,s=t.firstChild,a="LI"===t.nodeName;if(!a||s&&/^[OU]L$/.test(s.nodeName))if(i&&k(i,t)){if(!T(i)){if(!a)return;(r=U(e,"DIV")).append(D(i)),i.append(r)}t.remove(),o=!T(t),i.append(D(t)),o&&I(i,n),s&&H(s,n)}else a&&(i=U(e,"DIV"),s.before(i),P(i,n))},z=(e,t)=>{let n=e.childNodes;for(;t&&1===e.nodeType;)t=(n=(e=n[t-1]).childNodes).length;return e},q=(e,t)=>{if(1===e.nodeType){let n=e.childNodes;if(t<n.length)e=n[t];else{for(;e&&!e.nextSibling;)e=e.parentNode;e&&(e=e.nextSibling)}}return e},K=(e,n)=>{let o,r,i,s,a=e.startContainer,l=e.startOffset,d=e.endContainer,c=e.endOffset;a.nodeType===t?(r=(o=a.parentNode).childNodes,l===a.length?(l=p(r,a)+1,e.collapsed&&(d=o,c=l)):(l&&(s=a.splitText(l),d===a?(c-=l,d=s):d===o&&++c,a=s),l=p(r,a)),a=o):r=a.childNodes,l===(i=r.length)?a.append(n):r[l].before(n),a===d&&(c+=r.length-i),e.setStart(a,l),e.setEnd(d,c)},G=(n,o,r)=>{let i=n.startContainer,s=n.startOffset,a=n.endContainer,l=n.endOffset;o||(o=n.commonAncestorContainer),o.nodeType===t&&(o=o.parentNode);let d,c,h,f,u,g=F(a,l,o,r),m=F(i,s,o,r),S=e.createDocumentFragment();for(;m!==g;)d=m.nextSibling,S.append(m),m=d;return i=o,s=g?p(o.childNodes,g):o.childNodes.length,(c=(h=o.childNodes[s])&&h.previousSibling)&&c.nodeType===t&&h.nodeType===t&&(i=c,s=c.length,f=c.data,u=h.data," "===f.charAt(f.length-1)&&" "===u.charAt(0)&&(u=" "+u.slice(1)),c.appendData(u),h.remove()),n.setStart(i,s),n.collapse(!0),P(o,r),S},Q=(e,t)=>{let n,o,r=Y(e,t),i=X(e,t),s=r!==i;return $(e),V(e,r,i,t),n=G(e,null,t),$(e),s&&(i=X(e,t),r&&i&&r!==i&&w(r,i,e,t)),r&&P(r,t),(o=t.firstChild)&&"BR"!==o.nodeName?e.collapse(!0):(P(t,t),e.selectNodeContents(t.firstChild)),n},Z=(t,n,o=!0)=>{let r=e.createRange();if(r.selectNode(n),o){let e=t.compareBoundaryPoints(3,r)>-1,n=t.compareBoundaryPoints(1,r)<1;return!e&&!n}{let e=t.compareBoundaryPoints(0,r)<1,n=t.compareBoundaryPoints(2,r)>-1;return e&&n}},$=e=>{let n,o=e.startContainer,r=e.startOffset,i=e.endContainer,s=e.endOffset,a=!0;for(;o.nodeType!==t&&(n=o.childNodes[r])&&!N(n);)o=n,r=0;if(s)for(;i.nodeType!==t;){if(!(n=i.childNodes[s-1])||N(n)){if(a&&n&&"BR"===n.nodeName){--s,a=!1;continue}break}s=B(i=n)}else for(;i.nodeType!==t&&(n=i.firstChild)&&!N(n);)i=n;e.collapsed?(e.setStart(i,s),e.setEnd(o,r)):(e.setStart(o,r),e.setEnd(i,s))},V=(e,n,o,r)=>{let i,s=e.startContainer,a=e.startOffset,l=e.endContainer,d=e.endOffset,c=!0;for(n||(n=e.commonAncestorContainer),o||(o=n);!a&&s!==n&&s!==r;)i=s.parentNode,a=p(i.childNodes,s),s=i;for(;l!==o&&l!==r&&(c&&l.nodeType!==t&&l.childNodes[d]&&"BR"===l.childNodes[d].nodeName&&(++d,c=!1),d===B(l));)i=l.parentNode,d=p(i.childNodes,l)+1,l=i;e.setStart(s,a),e.setEnd(l,d)},j=(e,t,n)=>{let o=x(e.endContainer,n,"A");if(o){let t=e.cloneRange();o=o.parentNode,V(t,o,o,n),t.endContainer===o&&(e.setStart(t.endContainer,t.endOffset),e.setEnd(t.endContainer,t.endOffset))}return e},Y=(e,t)=>{let n,o=e.startContainer;return C(o)?n=E(o,t):o!==t&&v(o)?n=o:(n=z(o,e.startOffset),n=L(n,t)),n&&Z(e,n)?n:null},X=(e,t)=>{let n,o,r=e.endContainer;if(C(r))n=E(r,t);else if(r!==t&&v(r))n=r;else{if(!(n=q(r,e.endOffset))||!t.contains(n))for(n=t;o=n.lastChild;)n=o;n=E(n,t)}return n&&Z(e,n)?n:null},J=n=>e.createTreeWalker(n,5,e=>e.nodeType===t?u.test(e.data):"IMG"===e.nodeName),ee=(e,n)=>{let o,r=e.startContainer,i=e.startOffset;if(r.nodeType===t){if(i)return!1;o=r}else if((o=q(r,i))&&!n.contains(o)&&(o=null),!o&&(o=z(r,i)).nodeType===t&&o.length)return!1;return(oe=J(Y(e,n))).currentNode=o,!oe.previousNode()},te=(e,n)=>{let o,r=e.endContainer,i=e.endOffset;if(oe=J(Y(e,n)),r.nodeType===t){if((o=r.data.length)&&i<o)return!1;oe.currentNode=r}else oe.currentNode=z(r,i);return!oe.nextNode()},ne=(e,t)=>{let n=Y(e,t),o=X(e,t);n&&o&&(e.setStart(n,0),e.setEnd(o,o.childNodes.length))};let oe,re=new WeakMap;TreeWalker.prototype.previousPONode=function(){let e,t=this.currentNode,n=this.root,o=this.nodeType,r=this.filter;for(;;){for(e=t.lastChild;!e&&t&&t!==n;)(e=t.previousSibling)||(t=t.parentNode);if(!e)return null;if(g[e.nodeType]&o&&r(e))return this.currentNode=e,e;t=e}};let ie=function(e){if(e.defaultPrevented)return;let t=e.key.toLowerCase(),n="",o=this.getSelection();"backspace"!==t&&"delete"!==t&&(e.altKey&&(n+="alt-"),e[f]&&(n+=h),e.shiftKey&&(n+="shift-")),s&&e.shiftKey&&"delete"===t&&(n+="shift-"),t=n+t,this._keyHandlers[t]?this._keyHandlers[t](this,e,o):o.collapsed||e.isComposing||e[f]||1!==t.length||(this.saveUndoState(o),Q(o,this._root),this._ensureBottomLine(),this.setSelection(o),this._updatePath(o,!0))},se=e=>(t,n)=>{n.preventDefault(),t[e]()},ae=(e,t)=>(t=t||null,(n,o)=>{o.preventDefault();let r=n.getSelection();n.hasFormat(e,null,r)?n.changeFormat(null,{tag:e},r):n.changeFormat({tag:e},t,r)}),le=(e,n)=>{try{n||(n=e.getSelection());let o,r=n.startContainer;for(r.nodeType===t&&(r=r.parentNode),o=r;C(o)&&(!o.textContent||""===o.textContent);)o=(r=o).parentNode;r!==o&&(n.setStart(o,p(o.childNodes,r)),n.collapse(!0),r.remove(),v(o)||(o=E(o,e._root)),P(o,e._root),$(n)),r===e._root&&(r=r.firstChild)&&"BR"===r.nodeName&&r.remove(),e._ensureBottomLine(),e.setSelection(n),e._updatePath(n,!0)}catch(t){e.didError(t)}},de=(e,t)=>{let n;for(;(n=e.parentNode)&&n!==t&&!n.isContentEditable;)e=n;e.remove()},ce=(n,o,r)=>{let i,s,a,l,d,c=n._root;if(n._recordUndoState(r),n._config.addLinks&&Ke(r.startContainer,c,n),n._removeZWS(),n._getRangeAndRemoveBookmark(r),r.collapsed||Q(r,c),(i=Y(r,c))&&(s=x(i,c,"PRE")))return $(r),a=r.startContainer,l=r.startOffset,a.nodeType!==t&&(a=e.createTextNode(""),s.insertBefore(a,s.firstChild)),o||"\n"!==a.data.charAt(l-1)&&!ee(r,c)||"\n"!==a.data.charAt(l)&&!te(r,c)?(a.insertData(l,"\n"),P(s,c),a.length===l+1?r.setStartAfter(a):r.setStart(a,l+1)):(a.deleteData(l&&l-1,l?2:1),(a=(d=F(a,l&&l-1,c,c)).previousSibling).textContent||a.remove(),a=n.createDefaultBlock(),d.before(a),d.textContent||d.remove(),r.setStart(a,0)),r.collapse(!0),n.setSelection(r),n._updatePath(r,!0),void n._docWasChanged();if(!i||o||/^T[HD]$/.test(i.nodeName))return j(r,0,c),K(r,n.createElement("BR")),r.collapse(!1),n.setSelection(r),void n._updatePath(r,!0);if(i=x(i,c,"LI")||i,b(i)&&(s=x(i,c,"UL,OL,BLOCKQUOTE")))return"BLOCKQUOTE"===s.nodeName?n.modifyBlocks(()=>n.createDefaultBlock(We(n)),r):n.decreaseListLevel(r);for(d=we(n,i,r.startContainer,r.startOffset),Fe(i),Te(i),P(i,c);1===d.nodeType;){let n,o=d.firstChild;if("A"===d.nodeName&&(!d.textContent||""===d.textContent)){o=e.createTextNode(""),d.replaceWith(o),d=o;break}for(;o&&o.nodeType===t&&!o.data&&(n=o.nextSibling)&&"BR"!==n.nodeName;)o.remove(),o=n;if(!o||"BR"===o.nodeName||o.nodeType===t)break;d=o}r=n.createRange(d,0),n.setSelection(r),n._updatePath(r,!0)},he={enter:a?(e,t,n)=>{e._saveRangeToBookmark(n);let o=e._getHTML(),r=()=>{e.removeEventListener("keyup",r),e._setHTML(o),n=e._getRangeAndRemoveBookmark(),ce(e,!1,n)};e.addEventListener("keyup",r)}:(e,t,n)=>{t.preventDefault(),ce(e,t.shiftKey,n)},"shift-enter":(e,t,n)=>e._keyHandlers.enter(e,t,n),backspace:(e,t,n)=>{let o=e._root;if(e._removeZWS(),e.saveUndoState(n),n.collapsed)if(ee(n,o)){t.preventDefault();let r,i=Y(n,o);if(!i)return;if(I(i.parentNode,o),r=E(i,o)){if(!r.isContentEditable)return void de(r,o);for(w(r,i,n,o),i=r.parentNode;i!==o&&!i.nextSibling;)i=i.parentNode;i!==o&&(i=i.nextSibling)&&H(i,o),e.setSelection(n)}else if(i){let t=x(i,o,"UL,OL,BLOCKQUOTE");if(t)return"BLOCKQUOTE"===t.nodeName?e.modifyBlocks(He,n):e.decreaseListLevel(n);e.setSelection(n),e._updatePath(n,!0)}}else e.setSelection(n),setTimeout(()=>le(e),0);else t.preventDefault(),Q(n,o),le(e,n)},delete:(e,t,n)=>{let o,r,i,s,a,l,d=e._root;if(e._removeZWS(),e.saveUndoState(n),n.collapsed)if(te(n,d)){if(t.preventDefault(),!(o=Y(n,d)))return;if(I(o.parentNode,d),r=L(o,d)){if(!r.isContentEditable)return void de(r,d);for(w(o,r,n,d),r=o.parentNode;r!==d&&!r.nextSibling;)r=r.parentNode;r!==d&&(r=r.nextSibling)&&H(r,d),e.setSelection(n),e._updatePath(n,!0)}}else{if(i=n.cloneRange(),V(n,d,d,d),s=n.endContainer,a=n.endOffset,1===s.nodeType&&(l=s.childNodes[a])&&"IMG"===l.nodeName)return t.preventDefault(),l.remove(),$(n),void le(e,n);e.setSelection(i),setTimeout(()=>le(e),0)}else t.preventDefault(),Q(n,d),le(e,n)},tab:(e,t,n)=>{let o,r,i=e._root;if(e._removeZWS(),n.collapsed&&ee(n,i))for(o=Y(n,i);r=o.parentNode;){if("UL"===r.nodeName||"OL"===r.nodeName){t.preventDefault(),e.increaseListLevel(n);break}o=r}},"shift-tab":(e,t,n)=>{let o,r=e._root;e._removeZWS(),n.collapsed&&ee(n,r)&&(o=n.startContainer,x(o,r,"UL,OL")&&(t.preventDefault(),e.decreaseListLevel(n)))},space:(e,t,n)=>{let o,r=e._root;if(e._recordUndoState(n),e._config.addLinks&&Ke(n.startContainer,r,e),e._getRangeAndRemoveBookmark(n),o=n.endContainer,n.collapsed&&n.endOffset===B(o))do{if("A"===o.nodeName){n.setStartAfter(o);break}}while(!o.nextSibling&&(o=o.parentNode)&&o!==r);n.collapsed||(Q(n,r),e._ensureBottomLine(),e.setSelection(n),e._updatePath(n,!0)),e.setSelection(n)},arrowleft:e=>e._removeZWS(),arrowright:e=>e._removeZWS()};function fe(e,t){let n=e._root,o=e.createRange(n,t?0:n.childNodes.length);return $(o),e.setSelection(o),e}i||(he.pageup=(e=>fe(e,!0)),he.pagedown=(e=>fe(e,!1)));const ue=e=>(t,n)=>{n.preventDefault(),t.changeIndentationLevel(e)},pe=(e,t)=>(n,o)=>{o.preventDefault();let r=n.getSelectionClosest("UL,OL");r&&e==r.nodeName?n.removeList():n[t]()};he[h+"b"]=ae("B"),he[h+"i"]=ae("I"),he[h+"u"]=ae("U"),he[h+"shift-7"]=ae("S"),he[h+"shift-5"]=ae("SUB",{tag:"SUP"}),he[h+"shift-6"]=ae("SUP",{tag:"SUB"}),he[h+"shift-8"]=pe("UL","makeUnorderedList"),he[h+"shift-9"]=pe("OL","makeOrderedList"),he[h+"["]=ue("decrease"),he[h+"]"]=ue("increase"),he[h+"d"]=se("toggleCode"),he[h+"y"]=se("redo"),he[h+"z"]=se("undo"),he[h+"shift-z"]=se("redo");let ge={1:10,2:13,3:16,4:18,5:24,6:32,7:48},me={fontWeight:{regexp:/^bold|^700/i,replace:e=>U(e,"B")},fontStyle:{regexp:/^italic/i,replace:e=>U(e,"I")},fontFamily:{regexp:u,replace:(e,t,n)=>U(e,"SPAN",{class:t.fontFamily,style:"font-family:"+n})},fontSize:{regexp:u,replace:(e,t,n)=>U(e,"SPAN",{class:t.fontSize,style:"font-size:"+n})},textDecoration:{regexp:/^underline/i,replace:e=>U(e,"U")}},Se=t=>n=>{let o=U(e,t);return Array.prototype.forEach.call(n.attributes,e=>o.setAttribute(e.name,e.value)),n.replaceWith(o),o.append(D(n)),o},Ne={SPAN:(t,n,o)=>{let r,i,s,a,l,d,c=t.style;for(r in me)if(i=me[r],(s=c[r])&&i.regexp.test(s)){if((d=i.replace(e,o.classNames,s)).nodeName===t.nodeName&&d.className===t.className)continue;l||(l=d),a&&a.append(d),a=d,t.style[r]=""}return l&&(a.append(D(t)),t.append(l)),a||t},STRONG:Se("B"),EM:Se("I"),INS:Se("U"),STRIKE:Se("S"),FONT:(t,n,o)=>{let r,i,s,a,l,d=t.face,c=t.size,h=t.color,f=o.classNames;return d&&(l=r=U(e,"SPAN",{class:f.fontFamily,style:"font-family:"+d}),a=r),c&&(i=U(e,"SPAN",{class:f.fontSize,style:"font-size:"+ge[c]+"px"}),l||(l=i),a&&a.append(i),a=i),h&&/^#?([\dA-F]{3}){1,2}$/i.test(h)&&("#"!==h.charAt(0)&&(h="#"+h),s=U(e,"SPAN",{class:f.colour,style:"color:"+h}),l||(l=s),a&&a.append(s),a=s),l||(l=a=U(e,"SPAN")),t.replaceWith(l),a.append(D(t)),a},TT:(t,n,o)=>{let r=U(e,"SPAN",{class:o.classNames.fontFamily,style:'font-family:menlo,consolas,"courier new",monospace'});return t.replaceWith(r),r.append(D(t)),r}},_e=/^(?:A(?:DDRESS|RTICLE|SIDE|UDIO)|BLOCKQUOTE|CAPTION|D(?:[DLT]|IV)|F(?:IGURE|IGCAPTION|OOTER)|H[1-6]|HEADER|L(?:ABEL|EGEND|I)|O(?:L|UTPUT)|P(?:RE)?|SECTION|T(?:ABLE|BODY|D|FOOT|H|HEAD|R)|COL(?:GROUP)?|UL)$/,Ce=/^(?:HEAD|META|STYLE)/,ve=(n,o,r)=>{let i,s,a,l,d,c,h,f,p,g,m,S,N=n.childNodes;for(i=n;C(i);)i=i.parentNode;let _=e.createTreeWalker(i,5);for(s=0,a=N.length;s<a;++s)if(d=(l=N[s]).nodeName,1===(c=l.nodeType)){if(h=Ne[d],f=l.childNodes.length,h)l=h(l,n,o);else{if(Ce.test(d)){l.remove(),--s,--a;continue}if(!_e.test(d)&&!C(l)){--s,a+=f-1,l.replaceWith(D(l));continue}}f&&ve(l,o,r||"PRE"===d)}else{if(c===t){if(m=l.data,p=!u.test(m.charAt(0)),g=!u.test(m.charAt(m.length-1)),r||!p&&!g)continue;if(p){for(_.currentNode=l;(S=_.previousPONode())&&!("IMG"===(d=S.nodeName)||"#text"===d&&u.test(S.data));)if(!C(S)){S=null;break}m=m.replace(/^[ \t\r\n]+/g,S?" ":"")}if(g){for(_.currentNode=l;(S=_.nextNode())&&!("IMG"===d||"#text"===d&&u.test(S.data));)if(!C(S)){S=null;break}m=m.replace(/[ \t\r\n]+$/g,S?" ":"")}if(m){l.data=m;continue}}l.remove(),--s,--a}return n},Te=e=>{let n,o=e.childNodes,r=o.length;for(;r--;)1!==(n=o[r]).nodeType||N(n)?n.nodeType!==t||n.data||n.remove():(Te(n),C(n)&&!n.firstChild&&n.remove())},ye=e=>1===e.nodeType?"BR"===e.nodeName:u.test(e.data),Ee=(t,n)=>{let o,r=t.parentNode;for(;C(r);)r=r.parentNode;return(o=e.createTreeWalker(r,5,ye)).currentNode=t,!!o.nextNode()||n&&!o.previousNode()},Le=(e,t,n)=>{let o,r,i,s=e.querySelectorAll("BR"),a=[],l=s.length;for(o=0;o<l;++o)a[o]=Ee(s[o],n);for(;l--;)(i=(r=s[l]).parentNode)&&(a[l]?C(i)||I(i,t):r.remove())},be=(t,n,o,r,i,a)=>{let l,d,c=t.clipboardData,h=e.body,f=U(e,"div");f.append(n),l=f.innerHTML,r&&(l=r(l)),i?d=i(l):(Le(f,o,!0),f.setAttribute("style","position:fixed;overflow:hidden;bottom:100%;right:100%;"),h.append(f),d=(d=f.innerText||f.textContent).replace(" "," "),f.remove()),s&&(d=d.replace(/\r?\n/g,"\r\n")),a||d===l||c.setData("text/html",l),c.setData("text/plain",d),t.preventDefault()},ke=function(e){let n,o,r,i,s,a,l=this.getSelection(),c=this._root,h=this;if(l.collapsed)e.preventDefault();else{if(this.saveUndoState(l),!d&&e.clipboardData){for(r=(n=Y(l,c))===(o=X(l,c))&&n||c,i=Q(l,c),(s=l.commonAncestorContainer).nodeType===t&&(s=s.parentNode);s&&s!==r;)(a=s.cloneNode(!1)).append(i),i=a,s=s.parentNode;be(e,i,c,this._config.willCutCopy,null,!1)}else setTimeout(()=>{try{h._ensureBottomLine()}catch(e){h.didError(e)}},0);this.setSelection(l)}},Oe=function(e){((e,n,o,r,i,s)=>{let a,l,c,h,f,u;if(!d&&e.clipboardData){for(c=(a=Y(n,o))===(l=X(n,o))&&a||o,n=n.cloneRange(),$(n),V(n,c,c,o),h=n.cloneContents(),(f=n.commonAncestorContainer).nodeType===t&&(f=f.parentNode);f&&f!==c;)(u=f.cloneNode(!1)).append(h),h=u,f=f.parentNode;be(e,h,o,r,i,s)}})(e,this.getSelection(),this._root,this._config.willCutCopy,null,!1)};function xe(e){this.isShiftDown=e.shiftKey}let Ae=function(t){let n,o,r,i,s,a=t.clipboardData,c=a&&a.items,h=this.isShiftDown,f=!1,u=!1,g=!1,m=null,S=null,N=this;if(c){for(n=c.length;n--;)"text/html"===(r=(o=c[n]).type)?S=o:"text/plain"===r||"text/uri-list"===r?m=o:"text/rtf"===r?u=!0:/^image\/.*/.test(r)&&(g=!0);if(g&&(!u||!S))return t.preventDefault(),this.fireEvent("dragover",{dataTransfer:a,preventDefault:()=>f=!0}),void(f&&this.fireEvent("drop",{dataTransfer:a}));if(!d)return t.preventDefault(),void(!S||h&&m?m&&m.getAsString(e=>N.insertPlainText(e,!0)):S.getAsString(e=>N.insertHTML(e,!0)))}if(i=a&&a.types,!d&&i&&(p(i,"text/html")>-1||!l&&p(i,"text/plain")>-1&&p(i,"text/rtf")<0))return t.preventDefault(),void(!h&&(s=a.getData("text/html"))?this.insertHTML(s,!0):((s=a.getData("text/plain"))||(s=a.getData("text/uri-list")))&&this.insertPlainText(s,!0));let _=e.body,C=this.getSelection(),v=C.startContainer,T=C.startOffset,y=C.endContainer,E=C.endOffset,L=this.createElement("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});_.append(L),C.selectNodeContents(L),this.setSelection(C),setTimeout(()=>{try{let e,t,n="",o=L;for(;L=o;)o=L.nextSibling,L.remove(),(e=L.firstChild)&&e===L.lastChild&&"DIV"===e.nodeName&&(L=e),n+=L.innerHTML;t=N.createRange(v,T,y,E),N.setSelection(t),n&&N.insertHTML(n,!0)}catch(e){N.didError(e)}},0)},Re=function(e){let t=e.dataTransfer.types,n=t.length,o=!1,r=!1;for(;n--;)switch(t[n]){case"text/plain":o=!0;break;case"text/html":r=!0;break;default:return}(r||o)&&this.saveUndoState()};function Be(e,t,n){let o,r;if(e||(e={}),t)for(o in t)!n&&o in e||(r=t[o],e[o]=r&&r.constructor===Object?Be(e[o],r,n):r);return e}function De(t,o){let r;t.nodeType===n&&(t=t.body),this._root=t,this._events={},this._isFocused=!1,this._lastRange=null,this._hasZWS=!1,this._lastAnchorNode=null,this._lastFocusNode=null,this._path="",this._willUpdatePath=!1;const i=()=>{if(t.contains(e.activeElement)){let e=this;e._isFocused&&!e._willUpdatePath&&(e._willUpdatePath=!0,setTimeout(function(){e._willUpdatePath=!1,e._updatePath(e.getSelection())},0))}else this.removeEventListener("selectionchange",i)};this.addEventListener("selectstart",()=>this.addEventListener("selectionchange",i)),this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0,this._isInUndoState=!1,this._ignoreChange=!1,this._ignoreAllChanges=!1,(r=new MutationObserver(()=>this._docWasChanged())).observe(t,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._mutation=r,this._restoreSelection=!1,this.addEventListener("blur",()=>this._restoreSelection=!0).addEventListener("pointerdown mousedown touchstart",()=>this._restoreSelection=!1).addEventListener("focus",()=>this._restoreSelection&&this.setSelection(this._lastRange)).addEventListener("cut",ke).addEventListener("copy",Oe).addEventListener("keydown keyup",xe).addEventListener("paste",Ae).addEventListener("drop",Re).addEventListener("keydown",ie).addEventListener("pointerup keyup mouseup touchend",()=>this.getSelection()),this._keyHandlers=Object.create(he),this.setConfig(o),t.setAttribute("contenteditable","true"),t.setAttribute("data-gramm","false");try{e.execCommand("enableObjectResizing",!1,"false"),e.execCommand("enableInlineTableEditing",!1,"false")}catch(e){}t.__squire__=this,this.setHTML("")}let Ue=De.prototype,Pe=t=>{let n=t?o.DOMPurify.sanitize(t,{ALLOW_UNKNOWN_PROTOCOLS:!0,WHOLE_DOCUMENT:!1,RETURN_DOM:!0,RETURN_DOM_FRAGMENT:!0}):null;return n?e.importNode(n,!0):e.createDocumentFragment()};Ue.setConfig=function(e){return(e=Be({blockTag:"DIV",blockAttributes:null,tagAttributes:{blockquote:null,ul:null,ol:null,li:null,a:null},classNames:{colour:"colour",fontFamily:"font",fontSize:"size",highlight:"highlight"},leafNodeNames:S,undo:{documentSizeThreshold:-1,undoLimit:-1},isInsertedHTMLSanitized:!0,isSetHTMLSanitized:!0,sanitizeToDOMFragment:o.DOMPurify&&o.DOMPurify.isSupported?Pe:null,willCutCopy:null,addLinks:!0},e,!0)).blockTag=e.blockTag.toUpperCase(),this._config=e,this},Ue.createElement=function(t,n,o){return U(e,t,n,o)},Ue.createDefaultBlock=function(e){let t=this._config;return P(this.createElement(t.blockTag,t.blockAttributes,e),this._root)},Ue.didError=(e=>console.error(e)),Ue.getRoot=function(){return this._root},Ue.modifyDocument=function(e){let t=this._mutation;t&&(t.takeRecords().length&&this._docWasChanged(),t.disconnect()),this._ignoreAllChanges=!0,e(),this._ignoreAllChanges=!1,t&&(t.observe(this._root,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._ignoreChange=!1)};let Ie={pathChange:1,select:1,input:1,undoStateChange:1};Ue.fireEvent=function(t,n){let o,r,i,s=this._events[t];if(/^(?:focus|blur)/.test(t))if(o=this._root===e.activeElement,"focus"===t){if(!o||this._isFocused)return this;this._isFocused=!0}else{if(o||!this._isFocused)return this;this._isFocused=!1}if(s)for(n||(n={}),n.type!==t&&(n.type=t),r=(s=s.slice()).length;r--;){i=s[r];try{i.handleEvent?i.handleEvent(n):i.call(this,n)}catch(e){e.details="Squire: fireEvent error. Event type: "+t,this.didError(e)}}return this},Ue.destroy=function(){let e,t=this._events;for(e in t)this.removeEventListener(e);this._mutation&&this._mutation.disconnect(),delete this._root.__squire__,this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0},Ue.handleEvent=function(e){this.fireEvent(e.type,e)},Ue.addEventListener=function(t,n){return t.split(/\s+/).forEach(t=>{let o=this._events[t],r=this._root;if(!n)return this.didError({name:"Squire: addEventListener with null or undefined fn",message:"Event type: "+t}),this;o||(o=this._events[t]=[],Ie[t]||("selectionchange"===t&&(r=e),r.addEventListener(t,this,!0))),o.push(n)}),this},Ue.removeEventListener=function(t,n){let o,r=this._events[t],i=this._root;if(r){if(n)for(o=r.length;o--;)r[o]===n&&r.splice(o,1);else r.length=0;r.length||(delete this._events[t],Ie[t]||("selectionchange"===t&&(i=e),i.removeEventListener(t,this,!0)))}return this},Ue.createRange=function(t,n,r,i){if(t instanceof o.Range)return t.cloneRange();let s=e.createRange();return s.setStart(t,n),r?s.setEnd(r,i):s.setEnd(t,n),s},Ue.getCursorPosition=function(t){if(!t&&!(t=this.getSelection())||!t.getBoundingClientRect)return null;let n,o,r=t.getBoundingClientRect();return r&&!r.top&&(this._ignoreChange=!0,(n=e.createElement("SPAN")).textContent="",K(t,n),r=n.getBoundingClientRect(),o=n.parentNode,n.remove(),M(o,t)),r},Ue.setSelection=function(e){if(e)if(this._lastRange=e,this._isFocused){a&&o.focus();let t=o.getSelection();t&&t.setBaseAndExtent?t.setBaseAndExtent(e.startContainer,e.startOffset,e.endContainer,e.endOffset):t&&(t.removeAllRanges(),t.addRange(e))}else this._restoreSelection=!0;return this},Ue.getSelection=function(){let t,n,r,i,s=o.getSelection(),a=this._root;return this._isFocused&&s&&s.rangeCount&&(n=(t=s.getRangeAt(0).cloneRange()).startContainer,r=t.endContainer,n&&N(n)&&t.setStartBefore(n),r&&N(r)&&t.setEndBefore(r)),t&&a.contains(t.commonAncestorContainer)?this._lastRange=t:(i=(t=this._lastRange).commonAncestorContainer,e.contains(i)||(t=null)),t||this.createRange(a.firstChild,0)},Ue.getSelectionClosest=function(e){let t=this.getSelection();return t&&x(t.commonAncestorContainer,this._root,e)},Ue.selectionContains=function(e){let t=this.getSelection(),n=t&&t.commonAncestorContainer;return!(!n||t.collapsed)&&!(!(n=n.querySelector?n:n.parentElement)||!n.querySelector(e))},Ue.getSelectedText=function(){let n=this.getSelection();if(!n||n.collapsed)return"";let o,r=e.createTreeWalker(n.commonAncestorContainer,5,e=>Z(n,e)),i=n.startContainer,s=n.endContainer,a=r.currentNode=i,l="",d=!1;for(r.filter(a)||(a=r.nextNode());a;)a.nodeType===t?(o=a.data)&&/\S/.test(o)&&(a===s&&(o=o.slice(0,n.endOffset)),a===i&&(o=o.slice(n.startOffset)),l+=o,d=!0):("BR"===a.nodeName||d&&!C(a))&&(l+="\n",d=!1),a=r.nextNode();return l},Ue.getPath=function(){return this._path};let Fe=(t,n)=>{let o,r,i,s=e.createTreeWalker(t,4);for(;r=s.nextNode();)for(;(i=r.data.indexOf(""))>-1&&(!n||r.parentNode!==n);){if(1===r.length){do{o=r.parentNode,r.remove(),r=o,s.currentNode=o}while(C(r)&&!B(r));break}r.deleteData(i,1)}};Ue._didAddZWS=function(){this._hasZWS=!0},Ue._removeZWS=function(){this._hasZWS&&(Fe(this._root),this._hasZWS=!1)},Ue._updatePath=function(e,t){if(!e)return;let n,o=e.startContainer,r=e.endContainer;(t||o!==this._lastAnchorNode||r!==this._lastFocusNode)&&(this._lastAnchorNode=o,this._lastFocusNode=r,n=o&&r?o===r?R(r,this._root,this._config):"(selection)":"",this._path!==n&&(this._path=n,this.fireEvent("pathChange",{path:n}))),this.fireEvent(e.collapsed?"cursor":"select",{range:e})},Ue.focus=function(){return this._root.focus({preventScroll:!0}),this},Ue.blur=function(){return this._root.blur(),this};const We=e=>[e.createElement("INPUT",{id:"squire-selection-start",type:"hidden"}),e.createElement("INPUT",{id:"squire-selection-end",type:"hidden"})];Ue._saveRangeToBookmark=function(e){let t,[n,o]=We(this);K(e,n),e.collapse(!1),K(e,o),2&n.compareDocumentPosition(o)&&(n.id="squire-selection-end",o.id="squire-selection-start",t=n,n=o,o=t),e.setStartAfter(n),e.setEndBefore(o)},Ue._getRangeAndRemoveBookmark=function(n){let o=this._root,r=o.querySelector("#squire-selection-start"),i=o.querySelector("#squire-selection-end");if(r&&i){let o=r.parentNode,s=i.parentNode,a=p(o.childNodes,r),l=p(s.childNodes,i);o===s&&--l,r.remove(),i.remove(),n||(n=e.createRange()),n.setStart(o,a),n.setEnd(s,l),M(o,n),o!==s&&M(s,n),n.collapsed&&(o=n.startContainer).nodeType===t&&((s=o.childNodes[n.startOffset])&&s.nodeType===t||(s=o.childNodes[n.startOffset-1]),s&&s.nodeType===t&&(n.setStart(s,0),n.collapse(!0)))}return n||null},Ue._keyUpDetectChange=(e=>{let t=e.keyCode;!e[f]&&!e.altKey&&(t<16||t>20)&&(t<33||t>45)&&this._docWasChanged()}),Ue._docWasChanged=function(){re=new WeakMap,this._ignoreAllChanges||(this._ignoreChange?this._ignoreChange=!1:(this._isInUndoState&&(this._isInUndoState=!1,this.fireEvent("undoStateChange",{canUndo:!0,canRedo:!1})),this.fireEvent("input")))},Ue._recordUndoState=function(e,t){if(!this._isInUndoState||t){let n,o=this._undoIndex,r=this._undoStack,i=this._config.undo,s=i.documentSizeThreshold,a=i.undoLimit;t||++o,o<this._undoStackLength&&(r.length=this._undoStackLength=o),e&&this._saveRangeToBookmark(e),n=this._getHTML(),s>-1&&2*n.length>s&&a>-1&&o>a&&(r.splice(0,o-a),o=a,this._undoStackLength=a),r[o]=n,this._undoIndex=o,++this._undoStackLength,this._isInUndoState=!0}},Ue.saveUndoState=function(e){return void 0===e&&(e=this.getSelection()),this._recordUndoState(e,this._isInUndoState),this._getRangeAndRemoveBookmark(e),this},Ue.undo=function(){if(0!==this._undoIndex||!this._isInUndoState){this._recordUndoState(this.getSelection(),!1),--this._undoIndex,this._setHTML(this._undoStack[this._undoIndex]);let e=this._getRangeAndRemoveBookmark();e&&this.setSelection(e),this._isInUndoState=!0,this.fireEvent("undoStateChange",{canUndo:0!==this._undoIndex,canRedo:!0}),this.fireEvent("input")}return this},Ue.redo=function(){let e=this._undoIndex,t=this._undoStackLength;if(e+1<t&&this._isInUndoState){++this._undoIndex,this._setHTML(this._undoStack[this._undoIndex]);let n=this._getRangeAndRemoveBookmark();n&&this.setSelection(n),this.fireEvent("undoStateChange",{canUndo:!0,canRedo:e+2<t}),this.fireEvent("input")}return this},Ue.hasFormat=function(n,o,r){if(n=n.toUpperCase(),o||(o={}),!r&&!(r=this.getSelection()))return!1;!r.collapsed&&r.startContainer.nodeType===t&&r.startOffset===r.startContainer.length&&r.startContainer.nextSibling&&r.setStartBefore(r.startContainer.nextSibling),!r.collapsed&&r.endContainer.nodeType===t&&0===r.endOffset&&r.endContainer.previousSibling&&r.setEndAfter(r.endContainer.previousSibling);let i,s,a=this._root,l=r.commonAncestorContainer;if(A(l,a,n,o))return!0;if(l.nodeType===t)return!1;i=e.createTreeWalker(l,4,e=>Z(r,e));let d=!1;for(;s=i.nextNode();){if(!A(s,a,n,o))return!1;d=!0}return d},Ue.getFontInfo=function(e){let n,o,r,i={color:void 0,backgroundColor:void 0,family:void 0,size:void 0},s=0;if(!e&&!(e=this.getSelection()))return i;if(n=e.commonAncestorContainer,e.collapsed||n.nodeType===t)for(n.nodeType===t&&(n=n.parentNode);s<4&&n;)(o=n.style)&&(!i.color&&(r=o.color)&&(i.color=r,++s),!i.backgroundColor&&(r=o.backgroundColor)&&(i.backgroundColor=r,++s),!i.family&&(r=o.fontFamily)&&(i.family=r,++s),!i.size&&(r=o.fontSize)&&(i.size=r,++s)),n=n.parentNode;return i},Ue._addFormat=function(n,o,r){let i,s,a,l,d,c,h,f,u,p=this._root;if(r.collapsed){for(i=P(this.createElement(n,o),p),K(r,i),r.setStart(i.firstChild,i.firstChild.length),r.collapse(!0),u=i;C(u);)u=u.parentNode;Fe(u,i)}else{if(s=e.createTreeWalker(r.commonAncestorContainer,5,e=>(e.nodeType===t||"BR"===e.nodeName||"IMG"===e.nodeName)&&Z(r,e)),a=r.startContainer,d=r.startOffset,l=r.endContainer,c=r.endOffset,s.currentNode=a,s.filter(a)||(a=s.nextNode(),d=0),!a)return r;do{h=s.currentNode,(f=!A(h,p,n,o))&&(h===l&&h.length>c&&h.splitText(c),h===a&&d&&(h=h.splitText(d),l===a&&(l=h,c-=d),a=h,d=0),i=this.createElement(n,o),h.replaceWith(i),i.append(h))}while(s.nextNode());l.nodeType!==t&&(h.nodeType===t?(l=h,c=h.length):(l=h.parentNode,c=1)),r=this.createRange(a,d,l,c)}return r},Ue._removeFormat=function(n,o,r,i){let s;this._saveRangeToBookmark(r),r.collapsed&&(c?(s=e.createTextNode(""),this._didAddZWS()):s=e.createTextNode(""),K(r,s));let a=r.commonAncestorContainer;for(;C(a);)a=a.parentNode;let l=r.startContainer,d=r.startOffset,h=r.endContainer,f=r.endOffset,u=[],p=function(e,n){if(Z(r,e,!1))return;let o,i,s=e.nodeType===t;if(Z(r,e))if(s)e===h&&f!==e.length&&u.push([n,e.splitText(f)]),e===l&&d&&(e.splitText(d),u.push([n,e]));else for(o=e.firstChild;o;o=i)i=o.nextSibling,p(o,n);else"INPUT"===e.nodeName||s&&!e.data||u.push([n,e])},g=Array.prototype.filter.call(a.getElementsByTagName(n),function(e){return Z(r,e)&&O(e,n,o)});return i||g.forEach(function(e){p(e,e)}),u.forEach(function(e){let t=e[0].cloneNode(!1),n=e[1];n.replaceWith(t),t.append(n)}),g.forEach(function(e){e.replaceWith(D(e))}),this._getRangeAndRemoveBookmark(r),s&&r.collapse(!1),M(a,r),r},Ue.changeFormat=function(e,t,n,o){return n||(n=this.getSelection())?(this.saveUndoState(n),t&&(n=this._removeFormat(t.tag.toUpperCase(),t.attributes||{},n,o)),e&&(n=this._addFormat(e.tag.toUpperCase(),e.attributes||{},n)),this.setSelection(n),this._updatePath(n,!0),this):this};let Me={DT:"DD",DD:"DT",LI:"LI",PRE:"PRE"},we=(t,n,o,r)=>{let i=Me[n.nodeName],s=null,a=F(o,r,n.parentNode,t._root),l=t._config;return i||(i=l.blockTag,s=l.blockAttributes),O(a,i,s)||(n=U(e,i,s),a.dir&&(n.dir=a.dir),a.replaceWith(n),n.append(D(a)),a=n),a};Ue.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);let o=this._root,r=Y(n,o),i=X(n,o);if(r&&i)do{if(e(r)||r===i)break}while(r=L(r,o));return t&&(this.setSelection(n),this._updatePath(n,!0)),this},Ue.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._recordUndoState(t,this._isInUndoState);let n,o=this._root;return ne(t,o),V(t,o,o,o),n=G(t,o,o),K(t,e.call(this,n)),t.endOffset<t.endContainer.childNodes.length&&H(t.endContainer.childNodes[t.endOffset],o),H(t.startContainer.childNodes[t.startOffset],o),this._getRangeAndRemoveBookmark(t),this.setSelection(t),this._updatePath(t,!0),this};let He=function(e){var t=e.querySelectorAll("blockquote");return Array.prototype.filter.call(t,t=>!x(t.parentNode,e,"BLOCKQUOTE")).forEach(e=>e.replaceWith(D(e))),e},ze=(e,t,n)=>{let o,r,i,s,a=y(t,e._root),l=e._config.tagAttributes,d=l[n.toLowerCase()],c=l.li;for(;o=a.nextNode();)"LI"===o.parentNode.nodeName&&(o=o.parentNode,a.currentNode=o.lastChild),"LI"!==o.nodeName?(s=e.createElement("LI",c),o.dir&&(s.dir=o.dir),(i=o.previousSibling)&&i.nodeName===n?(i.append(s),o.remove()):o.replaceWith(e.createElement(n,d,[s])),s.append(D(o)),a.currentNode=s):(r=(o=o.parentNode).nodeName)!==n&&/^[OU]L$/.test(r)&&o.replaceWith(e.createElement(n,d,[D(o)]))},qe=(e,t)=>{let n=e.commonAncestorContainer,o=e.startContainer,r=e.endContainer;for(;n&&n!==t&&!/^[OU]L$/.test(n.nodeName);)n=n.parentNode;if(!n||n===t)return null;for(o===n&&(o=o.childNodes[e.startOffset]),r===n&&(r=r.childNodes[e.endOffset]);o&&o.parentNode!==n;)o=o.parentNode;for(;r&&r.parentNode!==n;)r=r.parentNode;return[n,o,r]};Ue.increaseListLevel=function(e){if(!e&&!(e=this.getSelection()))return this.focus();let t=this._root,n=qe(e,t);if(!n)return this.focus();let o=n[0],r=n[1],i=n[2];if(!r||r===o.firstChild)return this.focus();this._recordUndoState(e,this._isInUndoState);let s,a,l=o.nodeName,d=r.previousSibling;d.nodeName!==l&&(s=this._config.tagAttributes[l.toLowerCase()],d=this.createElement(l,s),r.before(d));do{a=r===i?null:r.nextSibling,d.append(r)}while(r=a);return(a=d.nextSibling)&&H(a,t),this._getRangeAndRemoveBookmark(e),this.setSelection(e),this._updatePath(e,!0),this.focus()},Ue.decreaseListLevel=function(e){if(!e&&!(e=this.getSelection()))return this.focus();let t=this._root,n=qe(e,t);if(!n)return this.focus();let o,r,i,s,a=n[0],l=n[1],d=n[2];if(l||(l=a.firstChild),d||(d=a.lastChild),this._recordUndoState(e,this._isInUndoState),l){if(o=a.parentNode,i=d.nextSibling?F(a,d.nextSibling,o,t):a.nextSibling,o!==t&&"LI"===o.nodeName){for(o=o.parentNode;i;)r=i.nextSibling,d.append(i),i=r;i=a.parentNode.nextSibling}s=!/^[OU]L$/.test(o.nodeName);do{r=l===d?null:l.nextSibling,l.remove(),s&&"LI"===l.nodeName&&(l=this.createDefaultBlock([D(l)])),o.insertBefore(l,i)}while(l=r)}return a.firstChild||a.remove(),i&&H(i,t),this._getRangeAndRemoveBookmark(e),this.setSelection(e),this._updatePath(e,!0),this.focus()},Ue._ensureBottomLine=function(){let e=this._root,t=e.lastElementChild;t&&t.nodeName===this._config.blockTag&&v(t)||e.append(this.createDefaultBlock())},Ue.setKeyHandler=function(e,t){return this._keyHandlers[e]=t,this},Ue._getHTML=function(){return this._root.innerHTML},Ue._setHTML=function(e){let t=this._root,n=t;n.innerHTML=e;do{P(n,t)}while(n=L(n,t));this._ignoreChange=!0},Ue.getHTML=function(e){let t,n;return e&&(n=this.getSelection())&&this._saveRangeToBookmark(n),t=this._getHTML().replace(/\u200B/g,""),n&&this._getRangeAndRemoveBookmark(n),t},Ue.setHTML=function(t){let n,o,r,i=this._config,s=i.isSetHTMLSanitized?i.sanitizeToDOMFragment:null,a=this._root;"function"==typeof s?o=s(t,!1,this):((n=this.createElement("DIV")).innerHTML=t,(o=e.createDocumentFragment()).append(D(n))),ve(o,i),Le(o,a,!1),I(o,a);let l=o;for(;l=L(l,a);)P(l,a);for(this._ignoreChange=!0;r=a.lastChild;)r.remove();a.append(o),P(a,a),this._undoIndex=-1,this._undoStack.length=0,this._undoStackLength=0,this._isInUndoState=!1;let d=this._getRangeAndRemoveBookmark()||this.createRange(a.firstChild,0);return this.saveUndoState(d),this._lastRange=d,this._restoreSelection=!0,this._updatePath(d,!0),this},Ue.insertElement=function(e,t){if(t||(t=this.getSelection()),t.collapse(!0),C(e))K(t,e),t.setStartAfter(e);else{let n,o,r=this._root,i=Y(t,r)||r;for(;i!==r&&!i.nextSibling;)i=i.parentNode;i!==r&&(n=i.parentNode,o=F(n,i.nextSibling,r,r)),o?o.before(e):(r.append(e),o=this.createDefaultBlock(),r.append(o)),t.setStart(o,0),t.setEnd(o,0),$(t)}return this.focus(),this.setSelection(t),this._updatePath(t),this},Ue.insertImage=function(e,t){let n=this.createElement("IMG",Be({src:e},t,!0));return this.insertElement(n),n},Ue.linkRegExp=/\b(?:((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9][a-z0-9.-]*[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:[^\s?&`!()[\]{};:'".,<>«»“”‘’]|\([^\s()<>]+\)))|([\w\-.%+]+@(?:[\w-]+\.)+[a-z]{2,}\b(?:[?][^&?\s]+=[^\s?&`!()[\]{};:'".,<>«»“”‘’]+(?:&[^&?\s]+=[^\s?&`!()[\]{};:'".,<>«»“”‘’]+)*)?))/i;let Ke=(t,n,o)=>{let r,i,s,a,l,d,c=e.createTreeWalker(t,4,e=>!x(e,n,"A")),h=o.linkRegExp,f=o._config.tagAttributes.a;if(h)for(;r=c.nextNode();)for(i=r.data;s=h.exec(i);)l=(a=s.index)+s[0].length,a&&(d=e.createTextNode(i.slice(0,a)),r.before(d)),(d=o.createElement("A",Be({href:s[1]?/^(?:ht|f)tps?:/i.test(s[1])?s[1]:"http://"+s[1]:"mailto:"+s[0]},f,!1))).textContent=i.slice(a,l),r.before(d),r.data=i=i.slice(l)};Ue.insertHTML=function(t,n){let o,r,i,s,a,l,d,c=this._config,h=c.isInsertedHTMLSanitized?c.sanitizeToDOMFragment:null,f=this.getSelection();"function"==typeof h?s=h(t,n,this):(n&&(o=t.indexOf("\x3c!--StartFragment--\x3e"),r=t.lastIndexOf("\x3c!--EndFragment--\x3e"),o>-1&&r>-1&&(t=t.slice(o+20,r))),/<\/td>((?!<\/tr>)[\s\S])*$/i.test(t)&&(t="<TR>"+t+"</TR>"),/<\/tr>((?!<\/table>)[\s\S])*$/i.test(t)&&(t="<TABLE>"+t+"</TABLE>"),(i=this.createElement("DIV")).innerHTML=t,(s=e.createDocumentFragment()).append(D(i))),this.saveUndoState(f);try{for(a=this._root,l=s,d={fragment:s,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},Ke(s,s,this),ve(s,c),Le(s,a,!1),Te(s),s.normalize();l=L(l,s);)P(l,a);n&&this.fireEvent("willPaste",d),d.defaultPrevented||(((t,n,o)=>{let r,i,s,a,l,d,c,h,f,u,g,m=n.firstChild&&C(n.firstChild);for(I(n,o),r=n;r=L(r,o);)P(r,o);if(t.collapsed||Q(t,o),$(t),t.collapse(!1),a=x(t.endContainer,o,"BLOCKQUOTE")||o,i=Y(t,o),h=L(n,n),c=!m&&!!i&&b(i),i&&h&&!c&&!x(h,n,"PRE,TABLE")){if(V(t,i,i,o),t.collapse(!0),l=t.endContainer,d=t.endOffset,Le(i,o,!1),C(l)&&(l=(f=F(l,d,E(l,o),o)).parentNode,d=p(l.childNodes,f)),d!==B(l))for(s=e.createDocumentFragment();r=l.childNodes[d];)s.append(r);w(l,h,t,o),d=p(l.parentNode.childNodes,l)+1,l=l.parentNode,t.setEnd(l,d)}B(n)&&(c&&(t.setEndBefore(i),t.collapse(!1),i.remove()),V(t,a,a,o),u=(f=F(t.endContainer,t.endOffset,a,o))?f.previousSibling:a.lastChild,f.before(n),f?t.setEndBefore(f):t.setEnd(a,B(a)),i=X(t,o),$(t),l=t.endContainer,d=t.endOffset,f&&T(f)&&H(f,o),(f=u&&u.nextSibling)&&T(f)&&H(f,o),t.setEnd(l,d)),s&&(g=t.cloneRange(),w(i,s,g,o),t.setEnd(g.endContainer,g.endOffset)),$(t)})(f,d.fragment,a),f.collapse(!1),j(f,0,a),this._ensureBottomLine()),this.setSelection(f),this._updatePath(f,!0),n&&this.focus()}catch(e){this.didError(e)}return this};let Ge=e=>e.replace("&","&").replace("<","<").replace(">",">").replace('"',""");Ue.insertPlainText=function(n,o){let r=this.getSelection();if(r.collapsed&&x(r.startContainer,this._root,"PRE")){let i,s,a=r.startContainer,l=r.startOffset;return a&&a.nodeType===t||(i=e.createTextNode(""),a.childNodes[l].before(i),a=i,l=0),s={text:n,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},o&&this.fireEvent("willPaste",s),s.defaultPrevented||(n=s.text,a.insertData(l,n),r.setStart(a,l+n.length),r.collapse(!0)),this.setSelection(r),this}let i,s,a,l,d=n.split("\n"),c=this._config,h=c.blockTag,f=c.blockAttributes,u="</"+h+">",p="<"+h;for(i in f)p+=" "+i+'="'+Ge(f[i])+'"';for(p+=">",s=0,a=d.length;s<a;++s)l=d[s],l=Ge(l).replace(/ (?= )/g," "),s&&(l=p+(l||"<BR>")+u),d[s]=l;return this.insertHTML(d.join(""),o)};let Qe=(e,t,n)=>(function(){return this[e](t,n),this.focus()});Ue.addStyles=function(t){if(t){let n=e.documentElement.firstChild,o=this.createElement("STYLE",{type:"text/css"});o.append(e.createTextNode(t)),n.append(o)}return this},Ue.bold=Qe("changeFormat",{tag:"B"}),Ue.italic=Qe("changeFormat",{tag:"I"}),Ue.underline=Qe("changeFormat",{tag:"U"}),Ue.strikethrough=Qe("changeFormat",{tag:"S"}),Ue.subscript=Qe("changeFormat",{tag:"SUB"},{tag:"SUP"}),Ue.superscript=Qe("changeFormat",{tag:"SUP"},{tag:"SUB"}),Ue.removeBold=Qe("changeFormat",null,{tag:"B"}),Ue.removeItalic=Qe("changeFormat",null,{tag:"I"}),Ue.removeUnderline=Qe("changeFormat",null,{tag:"U"}),Ue.removeStrikethrough=Qe("changeFormat",null,{tag:"S"}),Ue.removeSubscript=Qe("changeFormat",null,{tag:"SUB"}),Ue.removeSuperscript=Qe("changeFormat",null,{tag:"SUP"}),Ue.makeLink=function(t,n){let o=this.getSelection();if(o.collapsed){let n=t.indexOf(":")+1;if(n)for(;"/"===t[n];)++n;K(o,e.createTextNode(t.slice(n)))}return n=Be(Be({href:t},n,!0),this._config.tagAttributes.a,!1),this.changeFormat({tag:"A",attributes:n},{tag:"A"},o),this.focus()},Ue.removeLink=function(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0),this.focus()},Ue.setFontFace=function(e){let t=this._config.classNames.fontFamily;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"font-family: "+e+", sans-serif;"}}:null,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ue.setFontSize=function(e){let t=this._config.classNames.fontSize;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"font-size: "+("number"==typeof e?e+"px":e)}}:null,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ue.setTextColour=function(e){let t=this._config.classNames.colour;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"color:"+e}}:null,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ue.setHighlightColour=function(e){let t=this._config.classNames.highlight;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"background-color:"+e}}:e,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ue.setTextAlignment=function(e){return this.forEachBlock(function(t){let n=t.className.split(/\s+/).filter(function(e){return!!e&&!/^align/.test(e)}).join(" ");e?(t.className=n+" align-"+e,t.style.textAlign=e):(t.className=n,t.style.textAlign="")},!0),this.focus()},Ue.setTextDirection=function(e){return this.forEachBlock(function(t){e?t.dir=e:t.removeAttribute("dir")},!0),this.focus()};let Ze=function(t){let n,o=this._root,r=e.createDocumentFragment(),i=y(t,o);for(;n=i.nextNode();){let t,o,i=n.querySelectorAll("BR"),s=[],a=i.length;for(t=0;t<a;++t)s[t]=Ee(i[t],!1);for(;a--;)o=i[a],s[a]?o.replaceWith(e.createTextNode("\n")):o.remove();for(a=(i=n.querySelectorAll("CODE")).length;a--;)i[a].remove();r.childNodes.length&&r.append(e.createTextNode("\n")),r.append(D(n))}for(i=e.createTreeWalker(r,4);n=i.nextNode();)n.data=n.data.replace(" "," ");return r.normalize(),P(this.createElement("PRE",this._config.tagAttributes.pre,[r]),o)},$e=function(t){let n,o,r,i,s,a,l=this._root,d=t.querySelectorAll("PRE"),c=d.length;for(;c--;){for(n=d[c],o=e.createTreeWalker(n,4);r=o.nextNode();){for(i=(i=r.data).replace(/ (?= )/g," "),s=e.createDocumentFragment();(a=i.indexOf("\n"))>-1;)s.append(e.createTextNode(i.slice(0,a))),s.append(e.createElement("BR")),i=i.slice(a+1);r.before(s),r.data=i}I(n,l),n.replaceWith(D(n))}return t};Ue.code=function(){let e=this.getSelection();return e.collapsed||T(e.commonAncestorContainer)?this.modifyBlocks(Ze,e):this.changeFormat({tag:"CODE",attributes:this._config.tagAttributes.code},null,e),this.focus()},Ue.removeCode=function(){let e=this.getSelection(),t=e.commonAncestorContainer;return x(t,this._root,"PRE")?this.modifyBlocks($e,e):this.changeFormat(null,{tag:"CODE"},e),this.focus()},Ue.toggleCode=function(){return this.hasFormat("PRE")||this.hasFormat("CODE")?this.removeCode():this.code(),this},Ue.removeAllFormatting=function(n){if(!n&&!(n=this.getSelection())||n.collapsed)return this;let o=this._root,r=n.commonAncestorContainer;for(;r&&!v(r);)r=r.parentNode;if(r||(ne(n,o),r=o),r.nodeType===t)return this;this.saveUndoState(n),V(n,r,r,o);let i,s,a=n.startContainer,l=n.startOffset,d=n.endContainer,c=n.endOffset,h=e.createDocumentFragment(),f=e.createDocumentFragment(),u=F(d,c,r,o),g=F(a,l,r,o);for(;g!==u;)i=g.nextSibling,h.append(g),g=i;return function n(o,r,i){let s,a;for(s=r.firstChild;s;s=a){if(a=s.nextSibling,C(s)){if(s.nodeType===t||"BR"===s.nodeName||"IMG"===s.nodeName){i.append(s);continue}}else if(v(s)){i.append(o.createDefaultBlock([n(o,s,e.createDocumentFragment())]));continue}n(o,s,i)}return i}(this,h,f),f.normalize(),g=f.firstChild,i=f.lastChild,s=r.childNodes,g?(u.before(f),l=p(s,g),c=p(s,i)+1):c=l=p(s,u),n.setStart(r,l),n.setEnd(r,c),M(r,n),$(n),this.setSelection(n),this._updatePath(n,!0),this.focus()},Ue.increaseQuoteLevel=Qe("modifyBlocks",function(e){return this.createElement("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e])}),Ue.decreaseQuoteLevel=Qe("modifyBlocks",He),Ue.changeIndentationLevel=function(e){let t=this.getSelectionClosest("UL,OL,BLOCKQUOTE");if(t||"increase"===e){this[e+(t&&"BLOCKQUOTE"!==t.nodeName?"List":"Quote")+"Level"]()}},Ue.makeUnorderedList=Qe("modifyBlocks",function(e){return ze(this,e,"UL"),e}),Ue.makeOrderedList=Qe("modifyBlocks",function(e){return ze(this,e,"OL"),e}),Ue.removeList=Qe("modifyBlocks",function(e){let t,n,o,r,i,s=e.querySelectorAll("UL, OL"),a=e.querySelectorAll("LI"),l=this._root;for(t=0,n=s.length;t<n;++t)o=s[t],r=D(o),I(r,l),o.replaceWith(r);for(t=0,n=a.length;t<n;++t)i=a[t],v(i)?i.replaceWith(this.createDefaultBlock([D(i)])):(I(i,l),i.replaceWith(D(i)));return e}),o.Squire=De})(document);
|