0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 15:23:29 -05:00
Squire/build/squire.js
2016-11-21 10:59:44 +11:00

2 lines
No EOL
46 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

!function(e,t){"use strict";function n(e,t,n){this.root=this.currentNode=e,this.nodeType=t,this.filter=n}function o(e,t){for(var n=e.length;n--;)if(!t(e[n]))return!1;return!0}function r(e){return e.nodeType===F&&!!_t[e.nodeName]}function i(e){switch(e.nodeType){case M:return yt;case F:case W:if(gt&&Et.has(e))return Et.get(e);break;default:return St}var t;return t=o(e.childNodes,a)?Nt.test(e.nodeName)?yt:Tt:bt,gt&&Et.set(e,t),t}function a(e){return i(e)===yt}function s(e){return i(e)===Tt}function d(e){return i(e)===bt}function l(e,t){var o=new n(t,z,s);return o.currentNode=e,o}function c(e,t){return e=l(e,t).previousNode(),e!==t?e:null}function h(e,t){return e=l(e,t).nextNode(),e!==t?e:null}function u(e,t){return!r(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)}function f(e,t,n){if(e.nodeName!==t)return!1;for(var o in n)if(e.getAttribute(o)!==n[o])return!1;return!0}function p(e,t,n,o){for(;e&&e!==t;){if(f(e,n,o))return e;e=e.parentNode}return null}function g(e,t){for(;t;){if(t===e)return!0;t=t.parentNode}return!1}function m(e,t){var n,o,r,i,a="";return e&&e!==t&&(a=m(e.parentNode,t),e.nodeType===F&&(a+=(a?">":"")+e.nodeName,(n=e.id)&&(a+="#"+n),(o=e.className.trim())&&(r=o.split(/\s\s*/),r.sort(),a+=".",a+=r.join(".")),(i=e.dir)&&(a+="[dir="+i+"]"),r&&(vt.call(r,Q)>-1&&(a+="[backgroundColor="+e.style.backgroundColor.replace(/ /g,"")+"]"),vt.call(r,V)>-1&&(a+="[color="+e.style.color.replace(/ /g,"")+"]"),vt.call(r,$)>-1&&(a+="[fontFamily="+e.style.fontFamily.replace(/ /g,"")+"]"),vt.call(r,Y)>-1&&(a+="[fontSize="+e.style.fontSize+"]")))),a}function v(e){var t=e.nodeType;return t===F?e.childNodes.length:e.length||0}function C(e){var t=e.parentNode;return t&&t.removeChild(e),e}function N(e,t){var n=e.parentNode;n&&n.replaceChild(t,e)}function _(e){for(var t=e.ownerDocument.createDocumentFragment(),n=e.childNodes,o=n?n.length:0;o--;)t.appendChild(e.firstChild);return t}function S(e,n,o,r){var i,a,s,d,l=e.createElement(n);if(o instanceof Array&&(r=o,o=null),o)for(i in o)a=o[i],a!==t&&l.setAttribute(i,o[i]);if(r)for(s=0,d=r.length;d>s;s+=1)l.appendChild(r[s]);return l}function y(e,t){var n,o,i=e.ownerDocument,s=e;if(e===t&&((o=e.firstChild)&&"BR"!==o.nodeName||(n=A(i).createDefaultBlock(),o?e.replaceChild(n,o):e.appendChild(n),e=n,n=null)),e.nodeType===M)return s;if(a(e)){for(o=e.firstChild;ut&&o&&o.nodeType===M&&!o.data;)e.removeChild(o),o=e.firstChild;o||(ut?(n=i.createTextNode(X),A(i)._didAddZWS()):n=i.createTextNode(""))}else if(ht){for(;e.nodeType!==M&&!r(e);){if(o=e.firstChild,!o){n=i.createTextNode("");break}e=o}e.nodeType===M?/^ +$/.test(e.data)&&(e.data=""):r(e)&&e.parentNode.insertBefore(i.createTextNode(""),e)}else if(!e.querySelector("BR"))for(n=S(i,"BR");(o=e.lastElementChild)&&!a(o);)e=o;if(n)try{e.appendChild(n)}catch(d){A(i).didError({name:"Squire: fixCursor  "+d,message:"Parent: "+e.nodeName+"/"+e.innerHTML+" appendChild: "+n.nodeName})}return s}function T(e,t){var n,o,r,i,s=e.childNodes,l=e.ownerDocument,c=null,h=A(l)._config;for(n=0,o=s.length;o>n;n+=1)r=s[n],i="BR"===r.nodeName,!i&&a(r)?(c||(c=S(l,h.blockTag,h.blockAttributes)),c.appendChild(r),n-=1,o-=1):(i||c)&&(c||(c=S(l,h.blockTag,h.blockAttributes)),y(c,t),i?e.replaceChild(c,r):(e.insertBefore(c,r),n+=1,o+=1),c=null),d(r)&&T(r,t);return c&&e.appendChild(y(c,t)),e}function b(e,t,n,o){var r,i,a,s=e.nodeType;if(s===M&&e!==n)return b(e.parentNode,e.splitText(t),n,o);if(s===F){if("number"==typeof t&&(t=t<e.childNodes.length?e.childNodes[t]:null),e===n)return t;for(r=e.parentNode,i=e.cloneNode(!1);t;)a=t.nextSibling,i.appendChild(t),t=a;return"OL"===e.nodeName&&p(e,o,"BLOCKQUOTE")&&(i.start=(+e.start||1)+e.childNodes.length-1),y(e,o),y(i,o),(a=e.nextSibling)?r.insertBefore(i,a):r.appendChild(i),b(r,i,n,o)}return t}function E(e,t){for(var n,o,r,i=e.childNodes,s=i.length,d=[];s--;)if(n=i[s],o=s&&i[s-1],s&&a(n)&&u(n,o)&&!_t[n.nodeName])t.startContainer===n&&(t.startContainer=o,t.startOffset+=v(o)),t.endContainer===n&&(t.endContainer=o,t.endOffset+=v(o)),t.startContainer===e&&(t.startOffset>s?t.startOffset-=1:t.startOffset===s&&(t.startContainer=o,t.startOffset=v(o))),t.endContainer===e&&(t.endOffset>s?t.endOffset-=1:t.endOffset===s&&(t.endContainer=o,t.endOffset=v(o))),C(n),n.nodeType===M?o.appendData(n.data):d.push(_(n));else if(n.nodeType===F){for(r=d.length;r--;)n.appendChild(d.pop());E(n,t)}}function k(e,t){if(e.nodeType===M&&(e=e.parentNode),e.nodeType===F){var n={startContainer:t.startContainer,startOffset:t.startOffset,endContainer:t.endContainer,endOffset:t.endOffset};E(e,n),t.setStart(n.startContainer,n.startOffset),t.setEnd(n.endContainer,n.endOffset)}}function B(e,t,n){for(var o,r,i=t;1===i.parentNode.childNodes.length;)i=i.parentNode;C(i),r=e.childNodes.length,o=e.lastChild,o&&"BR"===o.nodeName&&(e.removeChild(o),r-=1),e.appendChild(_(t)),n.setStart(e,r),n.collapse(!0),k(e,n),at&&(o=e.lastChild)&&"BR"===o.nodeName&&e.removeChild(o)}function x(e,t){var n,o,r=e.previousSibling,i=e.firstChild,a=e.ownerDocument,s="LI"===e.nodeName;if(!s||i&&/^[OU]L$/.test(i.nodeName))if(r&&u(r,e)){if(!d(r)){if(!s)return;o=S(a,"DIV"),o.appendChild(_(r)),r.appendChild(o)}C(e),n=!d(e),r.appendChild(_(e)),n&&T(r,t),i&&x(i,t)}else s&&(r=S(a,"DIV"),e.insertBefore(r,i),y(r,t))}function L(e){this.isShiftDown=e.shiftKey}function A(e){for(var t,n=un.length;n--;)if(t=un[n],t._doc===e)return t;return null}function O(e,t,n){var o,r;if(e||(e={}),t)for(o in t)!n&&o in e||(r=t[o],e[o]=r&&r.constructor===Object?O(e[o],r,n):r);return e}function R(e,t){e.nodeType===H&&(e=e.body);var n,o=e.ownerDocument,r=o.defaultView;this._win=r,this._doc=o,this._root=e,this._events={},this._isFocused=!1,this._lastSelection=null,ft&&this.addEventListener("beforedeactivate",this.getSelection),this._hasZWS=!1,this._lastAnchorNode=null,this._lastFocusNode=null,this._path="",this._willUpdatePath=!1,"onselectionchange"in o?this.addEventListener("selectionchange",this._updatePathOnEvent):(this.addEventListener("keyup",this._updatePathOnEvent),this.addEventListener("mouseup",this._updatePathOnEvent)),this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0,this._isInUndoState=!1,this._ignoreChange=!1,this._ignoreAllChanges=!1,pt?(n=new MutationObserver(this._docWasChanged.bind(this)),n.observe(e,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._mutation=n):this.addEventListener("keyup",this._keyUpDetectChange),this._restoreSelection=!1,this.addEventListener("blur",D),this.addEventListener("mousedown",U),this.addEventListener("touchstart",U),this.addEventListener("focus",P),this._awaitingPaste=!1,this.addEventListener(it?"beforecut":"cut",dn),this.addEventListener("copy",ln),this.addEventListener("keydown",L),this.addEventListener("keyup",L),this.addEventListener(it?"beforepaste":"paste",cn),this.addEventListener("drop",hn),this.addEventListener(at?"keypress":"keydown",zt),this._keyHandlers=Object.create(Zt),this.setConfig(t),it&&(r.Text.prototype.splitText=function(e){var t=this.ownerDocument.createTextNode(this.data.slice(e)),n=this.nextSibling,o=this.parentNode,r=this.length-e;return n?o.insertBefore(t,n):o.appendChild(t),r&&this.deleteData(e,r),t}),e.setAttribute("contenteditable","true");try{o.execCommand("enableObjectResizing",!1,"false"),o.execCommand("enableInlineTableEditing",!1,"false")}catch(i){}un.push(this),this.setHTML("")}function D(){this._restoreSelection=!0}function U(){this._restoreSelection=!1}function P(){this._restoreSelection&&this.setSelection(this._lastSelection)}function I(e,t,n){var o,r;for(o=t.firstChild;o;o=r){if(r=o.nextSibling,a(o)){if(o.nodeType===M||"BR"===o.nodeName||"IMG"===o.nodeName){n.appendChild(o);continue}}else if(s(o)){n.appendChild(e.createDefaultBlock([I(e,o,e._doc.createDocumentFragment())]));continue}I(e,o,n)}return n}var w=2,F=1,M=3,H=9,W=11,z=1,q=4,K=0,G=1,Z=2,j=3,Q="highlight",V="colour",$="font",Y="size",X="",J=e.defaultView,et=navigator.userAgent,tt=/iP(?:ad|hone|od)/.test(et),nt=/Mac OS X/.test(et),ot=/Android/.test(et),rt=/Gecko\//.test(et),it=/Trident\/[456]\./.test(et),at=!!J.opera,st=/Edge\//.test(et),dt=!st&&/WebKit\//.test(et),lt=/Trident\/[4567]\./.test(et),ct=nt?"meta-":"ctrl-",ht=it||at,ut=it||dt,ft=it,pt="undefined"!=typeof MutationObserver,gt="undefined"!=typeof WeakMap,mt=/[^ \t\r\n]/,vt=Array.prototype.indexOf;Object.create||(Object.create=function(e){var t=function(){};return t.prototype=e,new t});var Ct={1:1,2:2,3:4,8:128,9:256,11:1024};n.prototype.nextNode=function(){for(var e,t=this.currentNode,n=this.root,o=this.nodeType,r=this.filter;;){for(e=t.firstChild;!e&&t&&t!==n;)e=t.nextSibling,e||(t=t.parentNode);if(!e)return null;if(Ct[e.nodeType]&o&&r(e))return this.currentNode=e,e;t=e}},n.prototype.previousNode=function(){for(var e,t=this.currentNode,n=this.root,o=this.nodeType,r=this.filter;;){if(t===n)return null;if(e=t.previousSibling)for(;t=e.lastChild;)e=t;else e=t.parentNode;if(!e)return null;if(Ct[e.nodeType]&o&&r(e))return this.currentNode=e,e;t=e}},n.prototype.previousPONode=function(){for(var e,t=this.currentNode,n=this.root,o=this.nodeType,r=this.filter;;){for(e=t.lastChild;!e&&t&&t!==n;)e=t.previousSibling,e||(t=t.parentNode);if(!e)return null;if(Ct[e.nodeType]&o&&r(e))return this.currentNode=e,e;t=e}};var Nt=/^(?:#text|A(?:BBR|CRONYM)?|B(?:R|D[IO])?|C(?:ITE|ODE)|D(?:ATA|EL|FN)|EM|FONT|HR|I(?:FRAME|MG|NPUT|NS)?|KBD|Q|R(?:P|T|UBY)|S(?:AMP|MALL|PAN|TR(?:IKE|ONG)|U[BP])?|TIME|U|VAR|WBR)$/,_t={BR:1,HR:1,IFRAME:1,IMG:1,INPUT:1},St=0,yt=1,Tt=2,bt=3,Et=gt?new WeakMap:null,kt=function(e,t){for(var n=e.childNodes;t&&e.nodeType===F;)e=n[t-1],n=e.childNodes,t=n.length;return e},Bt=function(e,t){if(e.nodeType===F){var n=e.childNodes;if(t<n.length)e=n[t];else{for(;e&&!e.nextSibling;)e=e.parentNode;e&&(e=e.nextSibling)}}return e},xt=function(e,t){var n,o,r,i,a=e.startContainer,s=e.startOffset,d=e.endContainer,l=e.endOffset;a.nodeType===M?(n=a.parentNode,o=n.childNodes,s===a.length?(s=vt.call(o,a)+1,e.collapsed&&(d=n,l=s)):(s&&(i=a.splitText(s),d===a?(l-=s,d=i):d===n&&(l+=1),a=i),s=vt.call(o,a)),a=n):o=a.childNodes,r=o.length,s===r?a.appendChild(t):a.insertBefore(t,o[s]),a===d&&(l+=o.length-r),e.setStart(a,s),e.setEnd(d,l)},Lt=function(e,t,n){var o=e.startContainer,r=e.startOffset,i=e.endContainer,a=e.endOffset;t||(t=e.commonAncestorContainer),t.nodeType===M&&(t=t.parentNode);for(var s,d,l,c=b(i,a,t,n),h=b(o,r,t,n),u=t.ownerDocument.createDocumentFragment();h!==c;)s=h.nextSibling,u.appendChild(h),h=s;return o=t,r=c?vt.call(t.childNodes,c):t.childNodes.length,l=t.childNodes[r],d=l&&l.previousSibling,d&&d.nodeType===M&&l.nodeType===M&&(o=d,r=d.length,d.appendData(l.data),C(l)),e.setStart(o,r),e.collapse(!0),y(t,n),u},At=function(e,t){Ut(e);var n=e.startContainer,o=e.endContainer,r=(a(n)||s(n))&&(a(o)||s(o)),i=Lt(e,null,t);Dt(e),n=Pt(e,t),r&&(o=It(e,t),n&&o&&n!==o&&B(n,o,e)),n&&y(n,t);var d=t.firstChild;return d&&"BR"!==d.nodeName?e.collapse(!1):(y(t,t),e.selectNodeContents(t.firstChild)),i},Ot=function(e,t,n){for(var o=!0,r=t.childNodes,i=r.length;i--;)if(!a(r[i])){o=!1;break}if(e.collapsed||At(e,n),Dt(e),o)xt(e,t),e.startContainer!==e.endContainer&&k(e.endContainer,e),k(e.startContainer,e),e.collapse(!1);else{for(var l,u,f,g,m,v=e.startContainer,C=b(v,e.startOffset,p(v.parentNode,n,"BLOCKQUOTE")||n,n),N=C.previousSibling,_=N,S=_.childNodes.length,T=C,E=0,B=C.parentNode;(l=_.lastChild)&&l.nodeType===F;){if("BR"===l.nodeName){S-=1;break}_=l,S=_.childNodes.length}for(;(l=T.firstChild)&&l.nodeType===F&&"BR"!==l.nodeName;)T=l;for(m=_.childNodes[S]||null;(l=t.firstChild)&&a(l);)_.insertBefore(l,m);for(;(l=t.lastChild)&&a(l);)T.insertBefore(l,T.firstChild),E+=1;for(u=t;u=h(u,n);)y(u,n);if(B.insertBefore(t,C),g=N.nextSibling,u=c(g,n),u&&!/\S/.test(u.textContent))do B=u.parentNode,B.removeChild(u),u=B;while(u&&!u.lastChild&&u!==n);if(N.parentNode||(N=g.previousSibling),_.parentNode||(_=N||g.parentNode,S=N?N.childNodes.length:0),d(g)&&x(g,n),f=C.previousSibling,u=s(C)?C:h(C,n),u&&!/\S/.test(u.textContent))do B=u.parentNode,B.removeChild(u),u=B;while(u&&!u.lastChild&&u!==n);C.parentNode||(C=f.nextSibling),E||(T=f,E=f.childNodes.length),C&&d(C)&&x(C,n),e.setStart(_,S),e.setEnd(T,E),Dt(e)}},Rt=function(e,t,n){var o=t.ownerDocument.createRange();if(o.selectNode(t),n){var r=e.compareBoundaryPoints(j,o)>-1,i=e.compareBoundaryPoints(G,o)<1;return!r&&!i}var a=e.compareBoundaryPoints(K,o)<1,s=e.compareBoundaryPoints(Z,o)>-1;return a&&s},Dt=function(e){for(var t,n=e.startContainer,o=e.startOffset,i=e.endContainer,a=e.endOffset,s=!0;n.nodeType!==M&&(t=n.childNodes[o],t&&!r(t));)n=t,o=0;if(a)for(;i.nodeType!==M;){if(t=i.childNodes[a-1],!t||r(t)){if(s&&t&&"BR"===t.nodeName){a-=1,s=!1;continue}break}i=t,a=v(i)}else for(;i.nodeType!==M&&(t=i.firstChild,t&&!r(t));)i=t;e.collapsed?(e.setStart(i,a),e.setEnd(n,o)):(e.setStart(n,o),e.setEnd(i,a))},Ut=function(e,t){var n,o=e.startContainer,r=e.startOffset,i=e.endContainer,a=e.endOffset,s=!0;for(t||(t=e.commonAncestorContainer);o!==t&&!r;)n=o.parentNode,r=vt.call(n.childNodes,o),o=n;for(;;){if(s&&i.nodeType!==M&&i.childNodes[a]&&"BR"===i.childNodes[a].nodeName&&(a+=1,s=!1),i===t||a!==v(i))break;n=i.parentNode,a=vt.call(n.childNodes,i)+1,i=n}e.setStart(o,r),e.setEnd(i,a)},Pt=function(e,t){var n,o=e.startContainer;return a(o)?n=c(o,t):o!==t&&s(o)?n=o:(n=kt(o,e.startOffset),n=h(n,t)),n&&Rt(e,n,!0)?n:null},It=function(e,t){var n,o,r=e.endContainer;if(a(r))n=c(r,t);else if(r!==t&&s(r))n=r;else{if(n=Bt(r,e.endOffset),!n||!g(t,n))for(n=t;o=n.lastChild;)n=o;n=c(n,t)}return n&&Rt(e,n,!0)?n:null},wt=new n(null,q|z,function(e){return e.nodeType===M?mt.test(e.data):"IMG"===e.nodeName}),Ft=function(e,t){var n,o=e.startContainer,r=e.startOffset;if(wt.root=null,o.nodeType===M){if(r)return!1;n=o}else if(n=Bt(o,r),n&&!g(t,n)&&(n=null),!n&&(n=kt(o,r),n.nodeType===M&&n.length))return!1;return wt.currentNode=n,wt.root=Pt(e,t),!wt.previousNode()},Mt=function(e,t){var n,o=e.endContainer,r=e.endOffset;if(wt.root=null,o.nodeType===M){if(n=o.data.length,n&&n>r)return!1;wt.currentNode=o}else wt.currentNode=kt(o,r);return wt.root=It(e,t),!wt.nextNode()},Ht=function(e,t){var n,o=Pt(e,t),r=It(e,t);o&&r&&(n=o.parentNode,e.setStart(n,vt.call(n.childNodes,o)),n=r.parentNode,e.setEnd(n,vt.call(n.childNodes,r)+1))},Wt={8:"backspace",9:"tab",13:"enter",32:"space",33:"pageup",34:"pagedown",37:"left",39:"right",46:"delete",219:"[",221:"]"},zt=function(e){var t=e.keyCode,n=Wt[t],o="",r=this.getSelection();e.defaultPrevented||(n||(n=String.fromCharCode(t).toLowerCase(),/^[A-Za-z0-9]$/.test(n)||(n="")),at&&46===e.which&&(n="."),t>111&&124>t&&(n="f"+(t-111)),"backspace"!==n&&"delete"!==n&&(e.altKey&&(o+="alt-"),e.ctrlKey&&(o+="ctrl-"),e.metaKey&&(o+="meta-")),e.shiftKey&&(o+="shift-"),n=o+n,this._keyHandlers[n]?this._keyHandlers[n](this,e,r):1!==n.length||r.collapsed||(this.saveUndoState(r),At(r,this._root),this._ensureBottomLine(),this.setSelection(r),this._updatePath(r,!0)))},qt=function(e){return function(t,n){n.preventDefault(),t[e]()}},Kt=function(e,t){return t=t||null,function(n,o){o.preventDefault();var r=n.getSelection();n.hasFormat(e,null,r)?n.changeFormat(null,{tag:e},r):n.changeFormat({tag:e},t,r)}},Gt=function(e,t){try{t||(t=e.getSelection());var n,o=t.startContainer;for(o.nodeType===M&&(o=o.parentNode),n=o;a(n)&&(!n.textContent||n.textContent===X);)o=n,n=o.parentNode;o!==n&&(t.setStart(n,vt.call(n.childNodes,o)),t.collapse(!0),n.removeChild(o),s(n)||(n=c(n,e._root)),y(n,e._root),Dt(t)),o===e._root&&(o=o.firstChild)&&"BR"===o.nodeName&&C(o),e._ensureBottomLine(),e.setSelection(t),e._updatePath(t,!0)}catch(r){e.didError(r)}},Zt={enter:function(e,t,n){var o,r,i,a=e._root;if(t.preventDefault(),e._recordUndoState(n),On(n.startContainer,a,e),e._removeZWS(),e._getRangeAndRemoveBookmark(n),n.collapsed||At(n,a),o=Pt(n,a),!o||/^T[HD]$/.test(o.nodeName))return xt(n,e.createElement("BR")),n.collapse(!1),e.setSelection(n),void e._updatePath(n,!0);if((r=p(o,a,"LI"))&&(o=r),!o.textContent){if(p(o,a,"UL")||p(o,a,"OL"))return e.modifyBlocks(Ln,n);if(p(o,a,"BLOCKQUOTE"))return e.modifyBlocks(Tn,n)}for(i=_n(e,o,n.startContainer,n.startOffset),mn(o),nn(o),y(o,a);i.nodeType===F;){var s,d=i.firstChild;if("A"===i.nodeName&&(!i.textContent||i.textContent===X)){d=e._doc.createTextNode(""),N(i,d),i=d;break}for(;d&&d.nodeType===M&&!d.data&&(s=d.nextSibling,s&&"BR"!==s.nodeName);)C(d),d=s;if(!d||"BR"===d.nodeName||d.nodeType===M&&!at)break;i=d}n=e._createRange(i,0),e.setSelection(n),e._updatePath(n,!0)},backspace:function(e,t,n){var o=e._root;if(e._removeZWS(),e.saveUndoState(n),n.collapsed)if(Ft(n,o)){t.preventDefault();var r,i=Pt(n,o);if(!i)return;if(T(i.parentNode,o),r=c(i,o)){if(!r.isContentEditable)return void C(r);for(B(r,i,n),i=r.parentNode;i!==o&&!i.nextSibling;)i=i.parentNode;i!==o&&(i=i.nextSibling)&&x(i,o),e.setSelection(n)}else if(i){if(p(i,o,"UL")||p(i,o,"OL"))return e.modifyBlocks(Ln,n);if(p(i,o,"BLOCKQUOTE"))return e.modifyBlocks(yn,n);e.setSelection(n),e._updatePath(n,!0)}}else e.setSelection(n),setTimeout(function(){Gt(e)},0);else t.preventDefault(),At(n,o),Gt(e,n)},"delete":function(e,t,n){var o,r,i,a,s,d,l=e._root;if(e._removeZWS(),e.saveUndoState(n),n.collapsed)if(Mt(n,l)){if(t.preventDefault(),o=Pt(n,l),!o)return;if(T(o.parentNode,l),r=h(o,l)){if(!r.isContentEditable)return void C(r);for(B(o,r,n),r=o.parentNode;r!==l&&!r.nextSibling;)r=r.parentNode;r!==l&&(r=r.nextSibling)&&x(r,l),e.setSelection(n),e._updatePath(n,!0)}}else{if(i=n.cloneRange(),Ut(n,e._root),a=n.endContainer,s=n.endOffset,a.nodeType===F&&(d=a.childNodes[s],d&&"IMG"===d.nodeName))return t.preventDefault(),C(d),Dt(n),void Gt(e,n);e.setSelection(i),setTimeout(function(){Gt(e)},0)}else t.preventDefault(),At(n,l),Gt(e,n)},tab:function(e,t,n){var o,r,i=e._root;if(e._removeZWS(),n.collapsed&&Ft(n,i))for(o=Pt(n,i);r=o.parentNode;){if("UL"===r.nodeName||"OL"===r.nodeName){t.preventDefault(),e.modifyBlocks(xn,n);break}o=r}},"shift-tab":function(e,t,n){var o,r=e._root;e._removeZWS(),n.collapsed&&Ft(n,r)&&(o=n.startContainer,(p(o,r,"UL")||p(o,r,"OL"))&&(t.preventDefault(),e.modifyBlocks(Ln,n)))},space:function(e,t,n){var o,r;e._recordUndoState(n),On(n.startContainer,e._root,e),e._getRangeAndRemoveBookmark(n),o=n.endContainer,r=o.parentNode,n.collapsed&&"A"===r.nodeName&&!o.nextSibling&&n.endOffset===v(o)?n.setStartAfter(r):n.collapsed||(At(n,e._root),e._ensureBottomLine(),e.setSelection(n),e._updatePath(n,!0)),e.setSelection(n)},left:function(e){e._removeZWS()},right:function(e){e._removeZWS()}};nt&&rt&&(Zt["meta-left"]=function(e,t){t.preventDefault();var n=gn(e);n&&n.modify&&n.modify("move","backward","lineboundary")},Zt["meta-right"]=function(e,t){t.preventDefault();var n=gn(e);n&&n.modify&&n.modify("move","forward","lineboundary")}),nt||(Zt.pageup=function(e){e.moveCursorToStart()},Zt.pagedown=function(e){e.moveCursorToEnd()}),Zt[ct+"b"]=Kt("B"),Zt[ct+"i"]=Kt("I"),Zt[ct+"u"]=Kt("U"),Zt[ct+"shift-7"]=Kt("S"),Zt[ct+"shift-5"]=Kt("SUB",{tag:"SUP"}),Zt[ct+"shift-6"]=Kt("SUP",{tag:"SUB"}),Zt[ct+"shift-8"]=qt("makeUnorderedList"),Zt[ct+"shift-9"]=qt("makeOrderedList"),Zt[ct+"["]=qt("decreaseQuoteLevel"),Zt[ct+"]"]=qt("increaseQuoteLevel"),Zt[ct+"y"]=qt("redo"),Zt[ct+"z"]=qt("undo"),Zt[ct+"shift-z"]=qt("redo");var jt={1:10,2:13,3:16,4:18,5:24,6:32,7:48},Qt={backgroundColor:{regexp:mt,replace:function(e,t){return S(e,"SPAN",{"class":Q,style:"background-color:"+t})}},color:{regexp:mt,replace:function(e,t){return S(e,"SPAN",{"class":V,style:"color:"+t})}},fontWeight:{regexp:/^bold|^700/i,replace:function(e){return S(e,"B")}},fontStyle:{regexp:/^italic/i,replace:function(e){return S(e,"I")}},fontFamily:{regexp:mt,replace:function(e,t){return S(e,"SPAN",{"class":$,style:"font-family:"+t})}},fontSize:{regexp:mt,replace:function(e,t){return S(e,"SPAN",{"class":Y,style:"font-size:"+t})}},textDecoration:{regexp:/^underline/i,replace:function(e){return S(e,"U")}}},Vt=function(e){return function(t,n){var o=S(t.ownerDocument,e);return n.replaceChild(o,t),o.appendChild(_(t)),o}},$t=function(e,t){var n,o,r,i,a,s,d=e.style,l=e.ownerDocument;for(n in Qt)o=Qt[n],r=d[n],r&&o.regexp.test(r)&&(s=o.replace(l,r),a||(a=s),i&&i.appendChild(s),i=s,e.style[n]="");return a&&(i.appendChild(_(e)),"SPAN"===e.nodeName?t.replaceChild(a,e):e.appendChild(a)),i||e},Yt={P:$t,SPAN:$t,STRONG:Vt("B"),EM:Vt("I"),INS:Vt("U"),STRIKE:Vt("S"),FONT:function(e,t){var n,o,r,i,a,s=e.face,d=e.size,l=e.color,c=e.ownerDocument;return s&&(n=S(c,"SPAN",{"class":"font",style:"font-family:"+s}),a=n,i=n),d&&(o=S(c,"SPAN",{"class":"size",style:"font-size:"+jt[d]+"px"}),a||(a=o),i&&i.appendChild(o),i=o),l&&/^#?([\dA-F]{3}){1,2}$/i.test(l)&&("#"!==l.charAt(0)&&(l="#"+l),r=S(c,"SPAN",{"class":"colour",style:"color:"+l}),a||(a=r),i&&i.appendChild(r),i=r),a||(a=i=S(c,"SPAN")),t.replaceChild(a,e),i.appendChild(_(e)),i},TT:function(e,t){var n=S(e.ownerDocument,"SPAN",{"class":"font",style:'font-family:menlo,consolas,"courier new",monospace'});return t.replaceChild(n,e),n.appendChild(_(e)),n}},Xt=/^(?: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)$/,Jt=/^(?:HEAD|META|STYLE)/,en=new n(null,q|z,function(){return!0}),tn=function Un(e,t){var n,o,r,i,s,d,l,c,h,u,f,p,g=e.childNodes;for(n=e;a(n);)n=n.parentNode;for(en.root=n,o=0,r=g.length;r>o;o+=1)if(i=g[o],s=i.nodeName,d=i.nodeType,l=Yt[s],d===F){if(c=i.childNodes.length,l)i=l(i,e);else{if(Jt.test(s)){e.removeChild(i),o-=1,r-=1;continue}if(!Xt.test(s)&&!a(i)){o-=1,r+=c-1,e.replaceChild(_(i),i);continue}}c&&Un(i,t||"PRE"===s)}else{if(d===M){if(f=i.data,h=!mt.test(f.charAt(0)),u=!mt.test(f.charAt(f.length-1)),t||!h&&!u)continue;if(h){for(en.currentNode=i;(p=en.previousPONode())&&(s=p.nodeName,!("IMG"===s||"#text"===s&&mt.test(p.data)));)if(!a(p)){p=null;break}f=f.replace(/^[ \t\r\n]+/g,p?" ":"")}if(u){for(en.currentNode=i;(p=en.nextNode())&&!("IMG"===s||"#text"===s&&mt.test(p.data));)if(!a(p)){p=null;break}f=f.replace(/[ \t\r\n]+$/g,p?" ":"")}if(f){i.data=f;continue}}e.removeChild(i),o-=1,r-=1}return e},nn=function Pn(e){for(var t,n=e.childNodes,o=n.length;o--;)t=n[o],t.nodeType!==F||r(t)?t.nodeType!==M||t.data||e.removeChild(t):(Pn(t),a(t)&&!t.firstChild&&e.removeChild(t))},on=function(e){return e.nodeType===F?"BR"===e.nodeName:mt.test(e.data)},rn=function(e){for(var t,o=e.parentNode;a(o);)o=o.parentNode;return t=new n(o,z|q,on),t.currentNode=e,!!t.nextNode()},an=function(e,t){var n,o,r,i=e.querySelectorAll("BR"),s=[],d=i.length;for(n=0;d>n;n+=1)s[n]=rn(i[n]);for(;d--;)o=i[d],r=o.parentNode,r&&(s[d]?a(r)||T(r,t):C(o))},sn=function(e,t){var n=t.ownerDocument.body;t.setAttribute("style","position:fixed;overflow:hidden;bottom:100%;right:100%;"),n.appendChild(t),e.setData("text/html",t.innerHTML),e.setData("text/plain",t.innerText||t.textContent),n.removeChild(t)},dn=function(e){var t=e.clipboardData,n=this.getSelection(),o=this.createElement("div"),r=this._root,i=this;this.saveUndoState(n),st||tt||!t?setTimeout(function(){try{i._ensureBottomLine()}catch(e){i.didError(e)}},0):(Ut(n,r),o.appendChild(At(n,r)),sn(t,o),e.preventDefault()),this.setSelection(n)},ln=function(e){var t,n,o,r,i,a,s=e.clipboardData,d=this.getSelection(),l=this.createElement("div"),c=this._root;if(!st&&!tt&&s){for(d=d.cloneRange(),t=Pt(d,c),n=It(d,c),o=t===n?t:c,Dt(d),Ut(d,o),r=d.cloneContents(),i=d.commonAncestorContainer,i.nodeType===M&&(i=i.parentNode);i&&i!==o;)a=i.cloneNode(!1),a.appendChild(r),r=a,i=i.parentNode;l.appendChild(r),sn(s,l),e.preventDefault()}},cn=function(e){var t,n,o,r,i,a=e.clipboardData,s=a&&a.items,d=this.isShiftDown,l=!1,c=!1,h=null,u=this;if(!st&&s){for(e.preventDefault(),t=s.length;t--;){if(n=s[t],o=n.type,!d&&"text/html"===o)return void n.getAsString(function(e){u.insertHTML(e,!0)});"text/plain"===o&&(h=n),!d&&/^image\/.*/.test(o)&&(c=!0)}return void(c?(this.fireEvent("dragover",{dataTransfer:a,preventDefault:function(){l=!0}}),l&&this.fireEvent("drop",{dataTransfer:a})):h&&h.getAsString(function(e){u.insertPlainText(e,!0)}))}if(r=a&&a.types,!st&&r&&(vt.call(r,"text/html")>-1||!rt&&vt.call(r,"text/plain")>-1&&vt.call(r,"text/rtf")<0))return e.preventDefault(),void(!d&&(i=a.getData("text/html"))?this.insertHTML(i,!0):((i=a.getData("text/plain"))||(i=a.getData("text/uri-list")))&&this.insertPlainText(i,!0));this._awaitingPaste=!0;var f=this._doc.body,p=this.getSelection(),g=p.startContainer,m=p.startOffset,v=p.endContainer,N=p.endOffset,_=this.createElement("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});f.appendChild(_),p.selectNodeContents(_),this.setSelection(p),setTimeout(function(){try{u._awaitingPaste=!1;for(var e,t,n="",o=_;_=o;)o=_.nextSibling,C(_),e=_.firstChild,e&&e===_.lastChild&&"DIV"===e.nodeName&&(_=e),n+=_.innerHTML;t=u._createRange(g,m,v,N),u.setSelection(t),n&&u.insertHTML(n,!0)}catch(r){u.didError(r)}},0)},hn=function(e){for(var t=e.dataTransfer.types,n=t.length,o=!1,r=!1;n--;)switch(t[n]){case"text/plain":o=!0;break;case"text/html":r=!0;break;default:return}(r||o)&&this.saveUndoState()},un=[],fn=R.prototype;fn.setConfig=function(e){return e=O({blockTag:"DIV",blockAttributes:null,tagAttributes:{blockquote:null,ul:null,ol:null,li:null,a:null},leafNodeNames:_t,undo:{documentSizeThreshold:-1,undoLimit:-1}},e,!0),e.blockTag=e.blockTag.toUpperCase(),this._config=e,this},fn.createElement=function(e,t,n){return S(this._doc,e,t,n)},fn.createDefaultBlock=function(e){var t=this._config;return y(this.createElement(t.blockTag,t.blockAttributes,e),this._root)},fn.didError=function(e){console.log(e)},fn.getDocument=function(){return this._doc},fn.getRoot=function(){return this._root},fn.modifyDocument=function(e){this._ignoreAllChanges=!0,this._mutation&&this._mutation.disconnect(),e(),this._ignoreAllChanges=!1,this._mutation&&(this._mutation.observe(this._root,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._ignoreChange=!1)};var pn={pathChange:1,select:1,input:1,undoStateChange:1};fn.fireEvent=function(e,t){var n,o,r,i=this._events[e];if(/^(?:focus|blur)/.test(e))if(n=g(this._root,this._doc.activeElement),"focus"===e){if(!n||this._isFocused)return this;this._isFocused=!0}else{if(n||!this._isFocused)return this;this._isFocused=!1}if(i)for(t||(t={}),t.type!==e&&(t.type=e),i=i.slice(),o=i.length;o--;){r=i[o];try{r.handleEvent?r.handleEvent(t):r.call(this,t)}catch(a){a.details="Squire: fireEvent error. Event type: "+e,this.didError(a)}}return this},fn.destroy=function(){var e,t=un.length,n=this._events;for(e in n)this.removeEventListener(e);for(this._mutation&&this._mutation.disconnect();t--;)un[t]===this&&un.splice(t,1);this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0},fn.handleEvent=function(e){this.fireEvent(e.type,e)},fn.addEventListener=function(e,t){var n=this._events[e],o=this._root;return t?(n||(n=this._events[e]=[],pn[e]||("selectionchange"===e&&(o=this._doc),o.addEventListener(e,this,!0))),n.push(t),this):(this.didError({name:"Squire: addEventListener with null or undefined fn",message:"Event type: "+e}),this)},fn.removeEventListener=function(e,t){var n,o=this._events[e],r=this._root;if(o){if(t)for(n=o.length;n--;)o[n]===t&&o.splice(n,1);else o.length=0;o.length||(delete this._events[e],pn[e]||("selectionchange"===e&&(r=this._doc),r.removeEventListener(e,this,!0)))}return this},fn._createRange=function(e,t,n,o){if(e instanceof this._win.Range)return e.cloneRange();var r=this._doc.createRange();return r.setStart(e,t),n?r.setEnd(n,o):r.setEnd(e,t),r},fn.getCursorPosition=function(e){if(!e&&!(e=this.getSelection())||!e.getBoundingClientRect)return null;var t,n,o=e.getBoundingClientRect();return o&&!o.top&&(this._ignoreChange=!0,t=this._doc.createElement("SPAN"),t.textContent=X,xt(e,t),o=t.getBoundingClientRect(),n=t.parentNode,n.removeChild(t),k(n,e)),o},fn._moveCursorTo=function(e){var t=this._root,n=this._createRange(t,e?0:t.childNodes.length);return Dt(n),this.setSelection(n),this},fn.moveCursorToStart=function(){return this._moveCursorTo(!0)},fn.moveCursorToEnd=function(){return this._moveCursorTo(!1)};var gn=function(e){return e._win.getSelection()||null};fn.setSelection=function(e){if(e)if(this._lastSelection=e,this._isFocused)if(ot&&!this._restoreSelection)D.call(this),this.blur(),this.focus();else{tt&&this._win.focus();var t=gn(this);t&&(t.removeAllRanges(),t.addRange(e))}else D.call(this);return this},fn.getSelection=function(){var e,t,n,o=gn(this),i=this._root;return o&&o.rangeCount&&(e=o.getRangeAt(0).cloneRange(),t=e.startContainer,n=e.endContainer,t&&r(t)&&e.setStartBefore(t),n&&r(n)&&e.setEndBefore(n)),e&&g(i,e.commonAncestorContainer)?this._lastSelection=e:e=this._lastSelection,e||(e=this._createRange(i.firstChild,0)),e},fn.getSelectedText=function(){var e,t=this.getSelection(),o=new n(t.commonAncestorContainer,q|z,function(e){return Rt(t,e,!0)}),r=t.startContainer,i=t.endContainer,s=o.currentNode=r,d="",l=!1;for(o.filter(s)||(s=o.nextNode());s;)s.nodeType===M?(e=s.data,e&&/\S/.test(e)&&(s===i&&(e=e.slice(0,t.endOffset)),s===r&&(e=e.slice(t.startOffset)),d+=e,l=!0)):("BR"===s.nodeName||l&&!a(s))&&(d+="\n",l=!1),s=o.nextNode();return d},fn.getPath=function(){return this._path};var mn=function(e,t){for(var o,r,i,s=new n(e,q,function(){return!0},!1);r=s.nextNode();)for(;(i=r.data.indexOf(X))>-1&&(!t||r.parentNode!==t);){if(1===r.length){do o=r.parentNode,o.removeChild(r),r=o,s.currentNode=o;while(a(r)&&!v(r));break}r.deleteData(i,1)}};fn._didAddZWS=function(){this._hasZWS=!0},fn._removeZWS=function(){this._hasZWS&&(mn(this._root),this._hasZWS=!1)},fn._updatePath=function(e,t){var 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?m(r,this._root):"(selection)":"",this._path!==n&&(this._path=n,this.fireEvent("pathChange",{path:n}))),e.collapsed||this.fireEvent("select")},fn._updatePathOnEvent=function(){var e=this;e._willUpdatePath||(e._willUpdatePath=!0,setTimeout(function(){e._willUpdatePath=!1,e._updatePath(e.getSelection())},0))},fn.focus=function(){return this._root.focus(),lt&&this.fireEvent("focus"),this},fn.blur=function(){return this._root.blur(),lt&&this.fireEvent("blur"),this};var vn="squire-selection-start",Cn="squire-selection-end";fn._saveRangeToBookmark=function(e){var t,n=this.createElement("INPUT",{id:vn,type:"hidden"}),o=this.createElement("INPUT",{id:Cn,type:"hidden"});xt(e,n),e.collapse(!1),xt(e,o),n.compareDocumentPosition(o)&w&&(n.id=Cn,o.id=vn,t=n,n=o,o=t),e.setStartAfter(n),e.setEndBefore(o)},fn._getRangeAndRemoveBookmark=function(e){var t=this._root,n=t.querySelector("#"+vn),o=t.querySelector("#"+Cn);if(n&&o){var r=n.parentNode,i=o.parentNode,a=vt.call(r.childNodes,n),s=vt.call(i.childNodes,o);r===i&&(s-=1),C(n),C(o),e||(e=this._doc.createRange()),e.setStart(r,a),e.setEnd(i,s),k(r,e),r!==i&&k(i,e),e.collapsed&&(r=e.startContainer,r.nodeType===M&&(i=r.childNodes[e.startOffset],i&&i.nodeType===M||(i=r.childNodes[e.startOffset-1]),i&&i.nodeType===M&&(e.setStart(i,0),e.collapse(!0))))}return e||null},fn._keyUpDetectChange=function(e){var t=e.keyCode;e.ctrlKey||e.metaKey||e.altKey||!(16>t||t>20)||!(33>t||t>45)||this._docWasChanged()},fn._docWasChanged=function(){if(gt&&(Et=new WeakMap),!this._ignoreAllChanges){if(pt&&this._ignoreChange)return void(this._ignoreChange=!1);this._isInUndoState&&(this._isInUndoState=!1,this.fireEvent("undoStateChange",{canUndo:!0,canRedo:!1})),this.fireEvent("input")}},fn._recordUndoState=function(e){if(!this._isInUndoState){var t,n=this._undoIndex+=1,o=this._undoStack,r=this._config.undo,i=r.documentSizeThreshold,a=r.undoLimit;n<this._undoStackLength&&(o.length=this._undoStackLength=n),e&&this._saveRangeToBookmark(e),t=this._getHTML(),i>-1&&2*t.length>i&&a>-1&&n>a&&(o.splice(0,n-a),n=this._undoIndex=a,this._undoStackLength=a),o[n]=t,this._undoStackLength+=1,this._isInUndoState=!0}},fn.saveUndoState=function(e){return e===t&&(e=this.getSelection()),this._isInUndoState||(this._recordUndoState(e),this._getRangeAndRemoveBookmark(e)),this
},fn.undo=function(){if(0!==this._undoIndex||!this._isInUndoState){this._recordUndoState(this.getSelection()),this._undoIndex-=1,this._setHTML(this._undoStack[this._undoIndex]);var e=this._getRangeAndRemoveBookmark();e&&this.setSelection(e),this._isInUndoState=!0,this.fireEvent("undoStateChange",{canUndo:0!==this._undoIndex,canRedo:!0}),this.fireEvent("input")}return this},fn.redo=function(){var e=this._undoIndex,t=this._undoStackLength;if(t>e+1&&this._isInUndoState){this._undoIndex+=1,this._setHTML(this._undoStack[this._undoIndex]);var n=this._getRangeAndRemoveBookmark();n&&this.setSelection(n),this.fireEvent("undoStateChange",{canUndo:!0,canRedo:t>e+2}),this.fireEvent("input")}return this},fn.hasFormat=function(e,t,o){if(e=e.toUpperCase(),t||(t={}),!o&&!(o=this.getSelection()))return!1;!o.collapsed&&o.startContainer.nodeType===M&&o.startOffset===o.startContainer.length&&o.startContainer.nextSibling&&o.setStartBefore(o.startContainer.nextSibling),!o.collapsed&&o.endContainer.nodeType===M&&0===o.endOffset&&o.endContainer.previousSibling&&o.setEndAfter(o.endContainer.previousSibling);var r,i,a=this._root,s=o.commonAncestorContainer;if(p(s,a,e,t))return!0;if(s.nodeType===M)return!1;r=new n(s,q,function(e){return Rt(o,e,!0)},!1);for(var d=!1;i=r.nextNode();){if(!p(i,a,e,t))return!1;d=!0}return d},fn.getFontInfo=function(e){var n,o,r,i={color:t,backgroundColor:t,family:t,size:t},a=0;if(!e&&!(e=this.getSelection()))return i;if(n=e.commonAncestorContainer,e.collapsed||n.nodeType===M)for(n.nodeType===M&&(n=n.parentNode);4>a&&n;)(o=n.style)&&(!i.color&&(r=o.color)&&(i.color=r,a+=1),!i.backgroundColor&&(r=o.backgroundColor)&&(i.backgroundColor=r,a+=1),!i.family&&(r=o.fontFamily)&&(i.family=r,a+=1),!i.size&&(r=o.fontSize)&&(i.size=r,a+=1)),n=n.parentNode;return i},fn._addFormat=function(e,t,o){var r,i,s,d,l,c,h,u,f,g=this._root;if(o.collapsed){for(r=y(this.createElement(e,t),g),xt(o,r),o.setStart(r.firstChild,r.firstChild.length),o.collapse(!0),f=r;a(f);)f=f.parentNode;mn(f,r)}else{if(i=new n(o.commonAncestorContainer,q|z,function(e){return(e.nodeType===M||"BR"===e.nodeName||"IMG"===e.nodeName)&&Rt(o,e,!0)},!1),s=o.startContainer,l=o.startOffset,d=o.endContainer,c=o.endOffset,i.currentNode=s,i.filter(s)||(s=i.nextNode(),l=0),!s)return o;do h=i.currentNode,u=!p(h,g,e,t),u&&(h===d&&h.length>c&&h.splitText(c),h===s&&l&&(h=h.splitText(l),d===s&&(d=h,c-=l),s=h,l=0),r=this.createElement(e,t),N(h,r),r.appendChild(h));while(i.nextNode());d.nodeType!==M&&(h.nodeType===M?(d=h,c=h.length):(d=h.parentNode,c=1)),o=this._createRange(s,l,d,c)}return o},fn._removeFormat=function(e,t,n,o){this._saveRangeToBookmark(n);var r,i=this._doc;n.collapsed&&(ut?(r=i.createTextNode(X),this._didAddZWS()):r=i.createTextNode(""),xt(n,r));for(var s=n.commonAncestorContainer;a(s);)s=s.parentNode;var d=n.startContainer,l=n.startOffset,c=n.endContainer,h=n.endOffset,u=[],p=function(e,t){if(!Rt(n,e,!1)){var o,r,i=e.nodeType===M;if(!Rt(n,e,!0))return void("INPUT"===e.nodeName||i&&!e.data||u.push([t,e]));if(i)e===c&&h!==e.length&&u.push([t,e.splitText(h)]),e===d&&l&&(e.splitText(l),u.push([t,e]));else for(o=e.firstChild;o;o=r)r=o.nextSibling,p(o,t)}},g=Array.prototype.filter.call(s.getElementsByTagName(e),function(o){return Rt(n,o,!0)&&f(o,e,t)});return o||g.forEach(function(e){p(e,e)}),u.forEach(function(e){var t=e[0].cloneNode(!1),n=e[1];N(n,t),t.appendChild(n)}),g.forEach(function(e){N(e,_(e))}),this._getRangeAndRemoveBookmark(n),r&&n.collapse(!1),k(s,n),n},fn.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),pt||this._docWasChanged(),this):this};var Nn={DT:"DD",DD:"DT",LI:"LI"},_n=function(e,t,n,o){var r=Nn[t.nodeName],i=null,a=b(n,o,t.parentNode,e._root),s=e._config;return r||(r=s.blockTag,i=s.blockAttributes),f(a,r,i)||(t=S(a.ownerDocument,r,i),a.dir&&(t.dir=a.dir),N(a,t),t.appendChild(_(a)),a=t),a};fn.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var o=this._root,r=Pt(n,o),i=It(n,o);if(r&&i)do if(e(r)||r===i)break;while(r=h(r,o));return t&&(this.setSelection(n),this._updatePath(n,!0),pt||this._docWasChanged()),this},fn.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._isInUndoState?this._saveRangeToBookmark(t):this._recordUndoState(t);var n,o=this._root;return Ht(t,o),Ut(t,o),n=Lt(t,o,o),xt(t,e.call(this,n)),t.endOffset<t.endContainer.childNodes.length&&x(t.endContainer.childNodes[t.endOffset],o),x(t.startContainer.childNodes[t.startOffset],o),this._getRangeAndRemoveBookmark(t),this.setSelection(t),this._updatePath(t,!0),pt||this._docWasChanged(),this};var Sn=function(e){return this.createElement("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e])},yn=function(e){var t=this._root,n=e.querySelectorAll("blockquote");return Array.prototype.filter.call(n,function(e){return!p(e.parentNode,t,"BLOCKQUOTE")}).forEach(function(e){N(e,_(e))}),e},Tn=function(){return this.createDefaultBlock([this.createElement("INPUT",{id:vn,type:"hidden"}),this.createElement("INPUT",{id:Cn,type:"hidden"})])},bn=function(e,t,n){for(var o,r,i,a,s=l(t,e._root),d=e._config.tagAttributes,c=d[n.toLowerCase()],h=d.li;o=s.nextNode();)"LI"===o.parentNode.nodeName&&(o=o.parentNode,s.currentNode=o.lastChild),"LI"!==o.nodeName?(a=e.createElement("LI",h),o.dir&&(a.dir=o.dir),(i=o.previousSibling)&&i.nodeName===n?(i.appendChild(a),C(o)):N(o,e.createElement(n,c,[a])),a.appendChild(_(o)),s.currentNode=a):(o=o.parentNode,r=o.nodeName,r!==n&&/^[OU]L$/.test(r)&&N(o,e.createElement(n,c,[_(o)])))},En=function(e){return bn(this,e,"UL"),e},kn=function(e){return bn(this,e,"OL"),e},Bn=function(e){var t,n,o,r,i,a=e.querySelectorAll("UL, OL"),d=e.querySelectorAll("LI"),l=this._root;for(t=0,n=a.length;n>t;t+=1)o=a[t],r=_(o),T(r,l),N(o,r);for(t=0,n=d.length;n>t;t+=1)i=d[t],s(i)?N(i,this.createDefaultBlock([_(i)])):(T(i,l),N(i,_(i)));return e},xn=function(e){var t,n,o,r,i,a,s=e.querySelectorAll("LI"),l=this._config.tagAttributes;for(t=0,n=s.length;n>t;t+=1)o=s[t],d(o.firstChild)||(r=o.parentNode.nodeName,i=o.previousSibling,i&&(i=i.lastChild)&&i.nodeName===r||(a=l[r.toLowerCase()],i=this.createElement(r,a),N(o,i)),i.appendChild(o));return e},Ln=function(e){var t=this._root,n=e.querySelectorAll("LI");return Array.prototype.filter.call(n,function(e){return!d(e.firstChild)}).forEach(function(n){var o,r=n.parentNode,i=r.parentNode,a=n.firstChild,s=a;if(n.previousSibling&&(r=b(r,n,i,t)),/^[OU]L$/.test(i.nodeName))i.insertBefore(n,r),r.firstChild||i.removeChild(r);else for(;s&&(o=s.nextSibling,!d(s));)i.insertBefore(s,r),s=o;for("LI"===i.nodeName&&a.previousSibling&&b(i,a,i.parentNode,t);n!==e&&!n.childNodes.length;)r=n.parentNode,r.removeChild(n),n=r},this),T(e,t),e};fn._ensureBottomLine=function(){var e=this._root,t=e.lastElementChild;t&&t.nodeName===this._config.blockTag&&s(t)||e.appendChild(this.createDefaultBlock())},fn.setKeyHandler=function(e,t){return this._keyHandlers[e]=t,this},fn._getHTML=function(){return this._root.innerHTML},fn._setHTML=function(e){var t=this._root,n=t;n.innerHTML=e;do y(n,t);while(n=h(n,t));this._ignoreChange=!0},fn.getHTML=function(e){var t,n,o,r,i,a,s=[];if(e&&(a=this.getSelection())&&this._saveRangeToBookmark(a),ht)for(t=this._root,n=t;n=h(n,t);)n.textContent||n.querySelector("BR")||(o=this.createElement("BR"),n.appendChild(o),s.push(o));if(r=this._getHTML().replace(/\u200B/g,""),ht)for(i=s.length;i--;)C(s[i]);return a&&this._getRangeAndRemoveBookmark(a),r},fn.setHTML=function(e){var t,n=this._doc.createDocumentFragment(),o=this.createElement("DIV"),r=this._root;o.innerHTML=e,n.appendChild(_(o)),tn(n),an(n,r),T(n,r);for(var i=n;i=h(i,r);)y(i,r);for(this._ignoreChange=!0;t=r.lastChild;)r.removeChild(t);r.appendChild(n),y(r,r),this._undoIndex=-1,this._undoStack.length=0,this._undoStackLength=0,this._isInUndoState=!1;var a=this._getRangeAndRemoveBookmark()||this._createRange(r.firstChild,0);return this.saveUndoState(a),this._lastSelection=a,D.call(this),this._updatePath(a,!0),this},fn.insertElement=function(e,t){if(t||(t=this.getSelection()),t.collapse(!0),a(e))xt(t,e),t.setStartAfter(e);else{for(var n,o,r=this._root,i=Pt(t,r)||r;i!==r&&!i.nextSibling;)i=i.parentNode;i!==r&&(n=i.parentNode,o=b(n,i.nextSibling,r,r)),o?r.insertBefore(e,o):(r.appendChild(e),o=this.createDefaultBlock(),r.appendChild(o)),t.setStart(o,0),t.setEnd(o,0),Dt(t)}return this.focus(),this.setSelection(t),this._updatePath(t),pt||this._docWasChanged(),this},fn.insertImage=function(e,t){var n=this.createElement("IMG",O({src:e},t,!0));return this.insertElement(n),n};var An=/\b((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))|([\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,}\b)/i,On=function(e,t,o){for(var r,i,a,s,d,l,c,h=e.ownerDocument,u=new n(e,q,function(e){return!p(e,t,"A")},!1),f=o._config.tagAttributes.a;r=u.nextNode();)for(i=r.data,a=r.parentNode;s=An.exec(i);)d=s.index,l=d+s[0].length,d&&(c=h.createTextNode(i.slice(0,d)),a.insertBefore(c,r)),c=o.createElement("A",O({href:s[1]?/^(?:ht|f)tps?:/.test(s[1])?s[1]:"http://"+s[1]:"mailto:"+s[2]},f,!1)),c.textContent=i.slice(d,l),a.insertBefore(c,r),r.data=i=i.slice(l)};fn.insertHTML=function(e,t){var n,o,r,i,a,s,d,l=this.getSelection(),c=this._doc;"undefined"!=typeof DOMPurify&&DOMPurify.isSupported?(i=DOMPurify.sanitize(e,{WHOLE_DOCUMENT:!1,RETURN_DOM:!0,RETURN_DOM_FRAGMENT:!0}),i=c.importNode(i,!0)):(t&&(n=e.indexOf("<!--StartFragment-->"),o=e.lastIndexOf("<!--EndFragment-->"),n>-1&&o>-1&&(e=e.slice(n+20,o))),r=this.createElement("DIV"),r.innerHTML=e,i=c.createDocumentFragment(),i.appendChild(_(r))),this.saveUndoState(l);try{for(a=this._root,s=i,d={fragment:i,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},On(i,i,this),tn(i),an(i,null),nn(i),i.normalize();s=h(s,i);)y(s,null);t&&this.fireEvent("willPaste",d),d.defaultPrevented||(Ot(l,d.fragment,a),pt||this._docWasChanged(),l.collapse(!1),this._ensureBottomLine()),this.setSelection(l),this._updatePath(l,!0),t&&this.focus()}catch(u){this.didError(u)}return this};var Rn=function(e){return e.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;").split('"').join("&quot;")};fn.insertPlainText=function(e,t){var n,o,r,i,a=e.split("\n"),s=this._config,d=s.blockTag,l=s.blockAttributes,c="</"+d+">",h="<"+d;for(n in l)h+=" "+n+'="'+Rn(l[n])+'"';for(h+=">",o=0,r=a.length;r>o;o+=1)i=a[o],i=Rn(i).replace(/ (?= )/g,"&nbsp;"),o&&r>o+1&&(i=h+(i||"<BR>")+c),a[o]=i;return this.insertHTML(a.join(""),t)};var Dn=function(e,t,n){return function(){return this[e](t,n),this.focus()}};fn.addStyles=function(e){if(e){var t=this._doc.documentElement.firstChild,n=this.createElement("STYLE",{type:"text/css"});n.appendChild(this._doc.createTextNode(e)),t.appendChild(n)}return this},fn.bold=Dn("changeFormat",{tag:"B"}),fn.italic=Dn("changeFormat",{tag:"I"}),fn.underline=Dn("changeFormat",{tag:"U"}),fn.strikethrough=Dn("changeFormat",{tag:"S"}),fn.subscript=Dn("changeFormat",{tag:"SUB"},{tag:"SUP"}),fn.superscript=Dn("changeFormat",{tag:"SUP"},{tag:"SUB"}),fn.removeBold=Dn("changeFormat",null,{tag:"B"}),fn.removeItalic=Dn("changeFormat",null,{tag:"I"}),fn.removeUnderline=Dn("changeFormat",null,{tag:"U"}),fn.removeStrikethrough=Dn("changeFormat",null,{tag:"S"}),fn.removeSubscript=Dn("changeFormat",null,{tag:"SUB"}),fn.removeSuperscript=Dn("changeFormat",null,{tag:"SUP"}),fn.makeLink=function(e,t){var n=this.getSelection();if(n.collapsed){var o=e.indexOf(":")+1;if(o)for(;"/"===e[o];)o+=1;xt(n,this._doc.createTextNode(e.slice(o)))}return t=O(O({href:e},t,!0),this._config.tagAttributes.a,!1),this.changeFormat({tag:"A",attributes:t},{tag:"A"},n),this.focus()},fn.removeLink=function(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0),this.focus()},fn.setFontFace=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{"class":"font",style:"font-family: "+e+", sans-serif;"}}:null,{tag:"SPAN",attributes:{"class":"font"}}),this.focus()},fn.setFontSize=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{"class":"size",style:"font-size: "+("number"==typeof e?e+"px":e)}}:null,{tag:"SPAN",attributes:{"class":"size"}}),this.focus()},fn.setTextColour=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{"class":"colour",style:"color:"+e}}:null,{tag:"SPAN",attributes:{"class":"colour"}}),this.focus()},fn.setHighlightColour=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{"class":"highlight",style:"background-color:"+e}}:e,{tag:"SPAN",attributes:{"class":"highlight"}}),this.focus()},fn.setTextAlignment=function(e){return this.forEachBlock(function(t){t.className=(t.className.split(/\s+/).filter(function(e){return!/align/.test(e)}).join(" ")+" align-"+e).trim(),t.style.textAlign=e},!0),this.focus()},fn.setTextDirection=function(e){return this.forEachBlock(function(t){t.dir=e},!0),this.focus()},fn.removeAllFormatting=function(e){if(!e&&!(e=this.getSelection())||e.collapsed)return this;for(var t=this._root,n=e.commonAncestorContainer;n&&!s(n);)n=n.parentNode;if(n||(Ht(e,t),n=t),n.nodeType===M)return this;this.saveUndoState(e),Ut(e,n);for(var o,r,i=n.ownerDocument,a=e.startContainer,d=e.startOffset,l=e.endContainer,c=e.endOffset,h=i.createDocumentFragment(),u=i.createDocumentFragment(),f=b(l,c,n,t),p=b(a,d,n,t);p!==f;)o=p.nextSibling,h.appendChild(p),p=o;return I(this,h,u),u.normalize(),p=u.firstChild,o=u.lastChild,r=n.childNodes,p?(n.insertBefore(u,f),d=vt.call(r,p),c=vt.call(r,o)+1):(d=vt.call(r,f),c=d),e.setStart(n,d),e.setEnd(n,c),k(n,e),Dt(e),this.setSelection(e),this._updatePath(e,!0),this.focus()},fn.increaseQuoteLevel=Dn("modifyBlocks",Sn),fn.decreaseQuoteLevel=Dn("modifyBlocks",yn),fn.makeUnorderedList=Dn("modifyBlocks",En),fn.makeOrderedList=Dn("modifyBlocks",kn),fn.removeList=Dn("modifyBlocks",Bn),fn.increaseListLevel=Dn("modifyBlocks",xn),fn.decreaseListLevel=Dn("modifyBlocks",Ln),R.getNodeBefore=kt,R.getNodeAfter=Bt,R.insertNodeInRange=xt,R.extractContentsOfRange=Lt,R.deleteContentsOfRange=At,R.insertTreeFragmentIntoRange=Ot,R.isNodeContainedInRange=Rt,R.moveRangeBoundariesDownTree=Dt,R.moveRangeBoundariesUpTree=Ut,R.getStartBlockOfRange=Pt,R.getEndBlockOfRange=It,R.contentWalker=wt,R.rangeDoesStartAtBlockBoundary=Ft,R.rangeDoesEndAtBlockBoundary=Mt,R.expandRangeToBlockBoundaries=Ht,R.onPaste=cn,R.addLinks=On,R.splitBlock=_n,R.startSelectionId=vn,R.endSelectionId=Cn,"object"==typeof exports?module.exports=R:"function"==typeof define&&define.amd?define(function(){return R}):(J.Squire=R,top!==J&&"true"===e.documentElement.getAttribute("data-squireinit")&&(J.editor=new R(e),J.onEditorLoad&&(J.onEditorLoad(J.editor),J.onEditorLoad=null)))}(document);