0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 23:40:35 -05:00
Squire/build/squire.js
2016-09-04 20:44:36 -04:00

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