diff --git a/build/squire-raw.js b/build/squire-raw.js index c79b823..45033e9 100644 --- a/build/squire-raw.js +++ b/build/squire-raw.js @@ -2233,10 +2233,6 @@ var setClipboardData = var html, text; node.appendChild( contents ); - // Firefox will add an extra new line for BRs at the end of block when - // calculating innerText, even though they don't actually affect display. - // So we need to remove them first. - cleanupBRs( node, root, true ); html = node.innerHTML; if ( willCutCopy ) { @@ -2246,6 +2242,10 @@ var setClipboardData = if ( toPlainText ) { text = toPlainText( html ); } else { + // Firefox will add an extra new line for BRs at the end of block when + // calculating innerText, even though they don't actually affect + // display, so we need to remove them first. + cleanupBRs( node, root, true ); node.setAttribute( 'style', 'position:fixed;overflow:hidden;bottom:100%;right:100%;' ); body.appendChild( node ); diff --git a/build/squire.js b/build/squire.js index c5b4789..e819b86 100644 --- a/build/squire.js +++ b/build/squire.js @@ -1 +1 @@ -!function(e,t){"use strict";var n=1,i=3,o=9,r=11,a=1,s="​",d=e.defaultView,l=navigator.userAgent,c=/Android/.test(l),h=/Mac OS X/.test(l),u=/Windows NT/.test(l),f=/iP(?:ad|hone|od)/.test(l)||h&&!!navigator.maxTouchPoints,p=/Gecko\//.test(l),g=/Edge\//.test(l),m=!g&&/WebKit\//.test(l),v=/Trident\/[4567]\./.test(l),C=h?"meta-":"ctrl-",_=m,N="undefined"!=typeof MutationObserver,S="undefined"!=typeof WeakMap,y=/[^ \t\r\n]/,T=Array.prototype.indexOf,E={1:1,2:2,3:4,8:128,9:256,11:1024},L=function(){return!0};function b(e,t,n){this.root=this.currentNode=e,this.nodeType=t,this.filter=n||L}b.prototype.nextNode=function(){for(var e,t=this.currentNode,n=this.root,i=this.nodeType,o=this.filter;;){for(e=t.firstChild;!e&&t&&t!==n;)(e=t.nextSibling)||(t=t.parentNode);if(!e)return null;if(E[e.nodeType]&i&&o(e))return this.currentNode=e,e;t=e}},b.prototype.previousNode=function(){for(var e,t=this.currentNode,n=this.root,i=this.nodeType,o=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(E[e.nodeType]&i&&o(e))return this.currentNode=e,e;t=e}},b.prototype.previousPONode=function(){for(var e,t=this.currentNode,n=this.root,i=this.nodeType,o=this.filter;;){for(e=t.lastChild;!e&&t&&t!==n;)(e=t.previousSibling)||(t=t.parentNode);if(!e)return null;if(E[e.nodeType]&i&&o(e))return this.currentNode=e,e;t=e}};var k=/^(?:#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)$/,x={BR:1,HR:1,IFRAME:1,IMG:1,INPUT:1};var O=0,A=1,B=2,R=3,D=S?new WeakMap:null;function P(e){return e.nodeType===n&&!!x[e.nodeName]}function U(e){switch(e.nodeType){case i:return A;case n:case r:if(S&&D.has(e))return D.get(e);break;default:return O}var t;return t=function(e,t){for(var n=e.length;n--;)if(!t(e[n]))return!1;return!0}(e.childNodes,I)?k.test(e.nodeName)?A:B:R,S&&D.set(e,t),t}function I(e){return U(e)===A}function w(e){return U(e)===B}function F(e){return U(e)===R}function M(e,t){var n=new b(t,a,w);return n.currentNode=e,n}function H(e,t){return(e=M(e,t).previousNode())!==t?e:null}function W(e,t){return(e=M(e,t).nextNode())!==t?e:null}function z(e){return!e.textContent&&!e.querySelector("IMG")}function q(e,t){return!P(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 K(e,t,n){if(e.nodeName!==t)return!1;for(var i in n)if(e.getAttribute(i)!==n[i])return!1;return!0}function G(e,t,n,i){for(;e&&e!==t;){if(K(e,n,i))return e;e=e.parentNode}return null}function Z(e,t){for(;t;){if(t===e)return!0;t=t.parentNode}return!1}function Q(e){var t=e.nodeType;return t===n||t===r?e.childNodes.length:e.length||0}function $(e){var t=e.parentNode;return t&&t.removeChild(e),e}function j(e,t){var n=e.parentNode;n&&n.replaceChild(t,e)}function V(e){for(var t=e.ownerDocument.createDocumentFragment(),n=e.childNodes,i=n?n.length:0;i--;)t.appendChild(e.firstChild);return t}function Y(e,n,i,o){var r,a,s,d,l=e.createElement(n);if(i instanceof Array&&(o=i,i=null),i)for(r in i)(a=i[r])!==t&&l.setAttribute(r,a);if(o)for(s=0,d=o.length;sl?o.startOffset-=1:o.startOffset===l&&(o.startContainer=a,o.startOffset=Q(a))),o.endContainer===t&&(o.endOffset>l?o.endOffset-=1:o.endOffset===l&&(o.endContainer=a,o.endOffset=Q(a))),$(r),r.nodeType===i?a.appendData(r.data):c.push(V(r));else if(r.nodeType===n){for(s=c.length;s--;)r.appendChild(c.pop());e(r,o)}}(e,o),t.setStart(o.startContainer,o.startOffset),t.setEnd(o.endContainer,o.endOffset)}}function ne(e,t,i,o){for(var r,a,s,d=t;(r=d.parentNode)&&r!==o&&r.nodeType===n&&1===r.childNodes.length;)d=r;$(d),s=e.childNodes.length,(a=e.lastChild)&&"BR"===a.nodeName&&(e.removeChild(a),s-=1),e.appendChild(V(t)),i.setStart(e,s),i.collapse(!0),te(e,i)}function ie(e,t){var n,i,o=e.previousSibling,r=e.firstChild,a=e.ownerDocument,s="LI"===e.nodeName;if(!s||r&&/^[OU]L$/.test(r.nodeName))if(o&&q(o,e)){if(!F(o)){if(!s)return;(i=Y(a,"DIV")).appendChild(V(o)),o.appendChild(i)}$(e),n=!F(e),o.appendChild(V(e)),n&&J(o,t),r&&ie(r,t)}else s&&(o=Y(a,"DIV"),e.insertBefore(o,r),X(o,t))}var oe=function(e,t){for(var i=e.childNodes;t&&e.nodeType===n;)t=(i=(e=i[t-1]).childNodes).length;return e},re=function(e,t){if(e.nodeType===n){var i=e.childNodes;if(t-1,r=e.compareBoundaryPoints(1,i)<1;return!o&&!r}var a=e.compareBoundaryPoints(0,i)<1,s=e.compareBoundaryPoints(2,i)>-1;return a&&s},he=function(e){for(var t,n=e.startContainer,o=e.startOffset,r=e.endContainer,a=e.endOffset,s=!0;n.nodeType!==i&&(t=n.childNodes[o])&&!P(t);)n=t,o=0;if(a)for(;r.nodeType!==i;){if(!(t=r.childNodes[a-1])||P(t)){if(s&&t&&"BR"===t.nodeName){a-=1,s=!1;continue}break}a=Q(r=t)}else for(;r.nodeType!==i&&(t=r.firstChild)&&!P(t);)r=t;e.collapsed?(e.setStart(r,a),e.setEnd(n,o)):(e.setStart(n,o),e.setEnd(r,a))},ue=function(e,t,n,o){var r,a=e.startContainer,s=e.startOffset,d=e.endContainer,l=e.endOffset,c=!0;for(t||(t=e.commonAncestorContainer),n||(n=t);!s&&a!==t&&a!==o;)r=a.parentNode,s=T.call(r.childNodes,a),a=r;for(;d!==n&&d!==o&&(c&&d.nodeType!==i&&d.childNodes[l]&&"BR"===d.childNodes[l].nodeName&&(l+=1,c=!1),l===Q(d));)r=d.parentNode,l=T.call(r.childNodes,d)+1,d=r;e.setStart(a,s),e.setEnd(d,l)},fe=function(e,t,n){var i=G(e.endContainer,n,"A");if(i){var o=e.cloneRange();i=i.parentNode,ue(o,i,i,n),o.endContainer===i&&(e.setStart(o.endContainer,o.endOffset),e.setEnd(o.endContainer,o.endOffset))}return e},pe=function(e,t){var n,i=e.startContainer;return(n=I(i)?H(i,t):i!==t&&w(i)?i:W(n=oe(i,e.startOffset),t))&&ce(e,n,!0)?n:null},ge=function(e,t){var n,i,o=e.endContainer;if(I(o))n=H(o,t);else if(o!==t&&w(o))n=o;else{if(!(n=re(o,e.endOffset))||!Z(t,n))for(n=t;i=n.lastChild;)n=i;n=H(n,t)}return n&&ce(e,n,!0)?n:null},me=new b(null,4|a,function(e){return e.nodeType===i?y.test(e.data):"IMG"===e.nodeName}),ve=function(e,t){var n,o=e.startContainer,r=e.startOffset;if(me.root=null,o.nodeType===i){if(r)return!1;n=o}else if((n=re(o,r))&&!Z(t,n)&&(n=null),!n&&(n=oe(o,r)).nodeType===i&&n.length)return!1;return me.currentNode=n,me.root=pe(e,t),!me.previousNode()},Ce=function(e,t){var n,o=e.endContainer,r=e.endOffset;if(me.root=null,o.nodeType===i){if((n=o.data.length)&&r)BLOCKQUOTE/.test(o)||!/(?:^|>)[OU]L/.test(o)?n[e]():n[t]()}},Oe=function(e,t){return function(n,i){i.preventDefault();var o=n.getPath();e.test(o)?n.removeList():n[t]()}};ke[C+"b"]=Te("B"),ke[C+"i"]=Te("I"),ke[C+"u"]=Te("U"),ke[C+"shift-7"]=Te("S"),ke[C+"shift-5"]=Te("SUB",{tag:"SUP"}),ke[C+"shift-6"]=Te("SUP",{tag:"SUB"}),ke[C+"shift-8"]=Oe(/(?:^|>)UL/,"makeUnorderedList"),ke[C+"shift-9"]=Oe(/(?:^|>)OL/,"makeOrderedList"),ke[C+"["]=xe("decreaseQuoteLevel","decreaseListLevel"),ke[C+"]"]=xe("increaseQuoteLevel","increaseListLevel"),ke[C+"d"]=ye("toggleCode"),ke[C+"y"]=ye("redo"),ke[C+"z"]=ye("undo"),ke[C+"shift-z"]=ye("redo");var Ae={1:10,2:13,3:16,4:18,5:24,6:32,7:48},Be={fontWeight:{regexp:/^bold|^700/i,replace:function(e){return Y(e,"B")}},fontStyle:{regexp:/^italic/i,replace:function(e){return Y(e,"I")}},fontFamily:{regexp:y,replace:function(e,t,n){return Y(e,"SPAN",{class:t.fontFamily,style:"font-family:"+n})}},fontSize:{regexp:y,replace:function(e,t,n){return Y(e,"SPAN",{class:t.fontSize,style:"font-size:"+n})}},textDecoration:{regexp:/^underline/i,replace:function(e){return Y(e,"U")}}},Re=function(e){return function(t,n){var i,o,r,a=Y(t.ownerDocument,e),s=t.attributes;for(i=0,o=s.length;i-1||!p&&T.call(o,"text/plain")>-1&&T.call(o,"text/rtf")<0))return e.preventDefault(),void(!d&&(r=a.getData("text/html"))?this.insertHTML(r,!0):((r=a.getData("text/plain"))||(r=a.getData("text/uri-list")))&&this.insertPlainText(r,!0));this._awaitingPaste=!0;var v=this._doc.body,C=this.getSelection(),_=C.startContainer,N=C.startOffset,S=C.endContainer,y=C.endOffset,E=this.createElement("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});v.appendChild(E),C.selectNodeContents(E),this.setSelection(C),setTimeout(function(){try{m._awaitingPaste=!1;for(var e,t,n="",i=E;E=i;)i=E.nextSibling,$(E),(e=E.firstChild)&&e===E.lastChild&&"DIV"===e.nodeName&&(E=e),n+=E.innerHTML;t=m.createRange(_,N,S,y),m.setSelection(t),n&&m.insertHTML(n,!0)}catch(e){m.didError(e)}},0)},$e=function(e){for(var t=e.dataTransfer.types,n=t.length,i=!1,o=!1;n--;)switch(t[n]){case"text/plain":i=!0;break;case"text/html":o=!0;break;default:return}(o||i)&&this.saveUndoState()};function je(e,t,n){var i,o;if(e||(e={}),t)for(i in t)!n&&i in e||(o=t[i],e[i]=o&&o.constructor===Object?je(e[i],o,n):o);return e}function Ve(e,t){e.nodeType===o&&(e=e.body);var n,i=e.ownerDocument,r=i.defaultView;this._win=r,this._doc=i,this._root=e,this._events={},this._isFocused=!1,this._lastSelection=null,this._hasZWS=!1,this._lastAnchorNode=null,this._lastFocusNode=null,this._path="",this._willUpdatePath=!1,"onselectionchange"in i?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,N?((n=new MutationObserver(this._docWasChanged.bind(this))).observe(e,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._mutation=n):this.addEventListener("keyup",this._keyUpDetectChange),this._restoreSelection=!1,this.addEventListener("blur",tt),this.addEventListener("mousedown",nt),this.addEventListener("touchstart",nt),this.addEventListener("focus",it),this._awaitingPaste=!1,this.addEventListener("cut",qe),this.addEventListener("copy",Ge),this.addEventListener("keydown",Ze),this.addEventListener("keyup",Ze),this.addEventListener("paste",Qe),this.addEventListener("drop",$e),this.addEventListener("keydown",Se),this._keyHandlers=Object.create(ke),this.setConfig(t),e.setAttribute("contenteditable","true"),e.setAttribute("data-gramm","false");try{i.execCommand("enableObjectResizing",!1,"false"),i.execCommand("enableInlineTableEditing",!1,"false")}catch(e){}e.__squire__=this,this.setHTML("")}var Ye=Ve.prototype,Xe=function(e,t,n){var i=n._doc,o=e?DOMPurify.sanitize(e,{ALLOW_UNKNOWN_PROTOCOLS:!0,WHOLE_DOCUMENT:!1,RETURN_DOM:!0,RETURN_DOM_FRAGMENT:!0}):null;return o?i.importNode(o,!0):i.createDocumentFragment()};Ye.setConfig=function(e){return(e=je({blockTag:"DIV",blockAttributes:null,tagAttributes:{blockquote:null,ul:null,ol:null,li:null,a:null},classNames:{colour:"colour",fontFamily:"font",fontSize:"size",highlight:"highlight"},leafNodeNames:x,undo:{documentSizeThreshold:-1,undoLimit:-1},isInsertedHTMLSanitized:!0,isSetHTMLSanitized:!0,sanitizeToDOMFragment:"undefined"!=typeof DOMPurify&&DOMPurify.isSupported?Xe:null,willCutCopy:null,addLinks:!0},e,!0)).blockTag=e.blockTag.toUpperCase(),this._config=e,this},Ye.createElement=function(e,t,n){return Y(this._doc,e,t,n)},Ye.createDefaultBlock=function(e){var t=this._config;return X(this.createElement(t.blockTag,t.blockAttributes,e),this._root)},Ye.didError=function(e){console.log(e)},Ye.getDocument=function(){return this._doc},Ye.getRoot=function(){return this._root},Ye.modifyDocument=function(e){var t=this._mutation;t&&(t.takeRecords().length&&this._docWasChanged(),t.disconnect()),this._ignoreAllChanges=!0,e(),this._ignoreAllChanges=!1,t&&(t.observe(this._root,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._ignoreChange=!1)};var Je={pathChange:1,select:1,input:1,undoStateChange:1};Ye.fireEvent=function(e,t){var n,i,o,r=this._events[e];if(/^(?:focus|blur)/.test(e))if(n=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(r)for(t||(t={}),t.type!==e&&(t.type=e),i=(r=r.slice()).length;i--;){o=r[i];try{o.handleEvent?o.handleEvent(t):o.call(this,t)}catch(t){t.details="Squire: fireEvent error. Event type: "+e,this.didError(t)}}return this},Ye.destroy=function(){var e,t=this._events;for(e in t)this.removeEventListener(e);this._mutation&&this._mutation.disconnect(),delete this._root.__squire__,this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0},Ye.handleEvent=function(e){this.fireEvent(e.type,e)},Ye.addEventListener=function(e,t){var n=this._events[e],i=this._root;return t?(n||(n=this._events[e]=[],Je[e]||("selectionchange"===e&&(i=this._doc),i.addEventListener(e,this,!0))),n.push(t),this):(this.didError({name:"Squire: addEventListener with null or undefined fn",message:"Event type: "+e}),this)},Ye.removeEventListener=function(e,t){var n,i=this._events[e],o=this._root;if(i){if(t)for(n=i.length;n--;)i[n]===t&&i.splice(n,1);else i.length=0;i.length||(delete this._events[e],Je[e]||("selectionchange"===e&&(o=this._doc),o.removeEventListener(e,this,!0)))}return this},Ye.createRange=function(e,t,n,i){if(e instanceof this._win.Range)return e.cloneRange();var o=this._doc.createRange();return o.setStart(e,t),n?o.setEnd(n,i):o.setEnd(e,t),o},Ye.getCursorPosition=function(e){if(!e&&!(e=this.getSelection())||!e.getBoundingClientRect)return null;var t,n,i=e.getBoundingClientRect();return i&&!i.top&&(this._ignoreChange=!0,(t=this._doc.createElement("SPAN")).textContent=s,ae(e,t),i=t.getBoundingClientRect(),(n=t.parentNode).removeChild(t),te(n,e)),i},Ye._moveCursorTo=function(e){var t=this._root,n=this.createRange(t,e?0:t.childNodes.length);return he(n),this.setSelection(n),this},Ye.moveCursorToStart=function(){return this._moveCursorTo(!0)},Ye.moveCursorToEnd=function(){return this._moveCursorTo(!1)};var et=function(e){return e._win.getSelection()||null};function tt(){this._restoreSelection=!0}function nt(){this._restoreSelection=!1}function it(){this._restoreSelection&&this.setSelection(this._lastSelection)}Ye.setSelection=function(e){if(e)if(this._lastSelection=e,this._isFocused)if(c&&!this._restoreSelection)tt.call(this),this.blur(),this.focus();else{f&&this._win.focus();var t=et(this);t&&(t.removeAllRanges(),t.addRange(e))}else tt.call(this);return this},Ye.getSelection=function(){var e,t,n,i,o=et(this),r=this._root;return this._isFocused&&o&&o.rangeCount&&(t=(e=o.getRangeAt(0).cloneRange()).startContainer,n=e.endContainer,t&&P(t)&&e.setStartBefore(t),n&&P(n)&&e.setEndBefore(n)),e&&Z(r,e.commonAncestorContainer)?this._lastSelection=e:Z((i=(e=this._lastSelection).commonAncestorContainer).ownerDocument,i)||(e=null),e||(e=this.createRange(r.firstChild,0)),e},Ye.getSelectedText=function(){var e=this.getSelection();if(!e||e.collapsed)return"";var t,n=new b(e.commonAncestorContainer,4|a,function(t){return ce(e,t,!0)}),o=e.startContainer,r=e.endContainer,s=n.currentNode=o,d="",l=!1;for(n.filter(s)||(s=n.nextNode());s;)s.nodeType===i?(t=s.data)&&/\S/.test(t)&&(s===r&&(t=t.slice(0,e.endOffset)),s===o&&(t=t.slice(e.startOffset)),d+=t,l=!0):("BR"===s.nodeName||l&&!I(s))&&(d+="\n",l=!1),s=n.nextNode();return d},Ye.getPath=function(){return this._path};var ot=function(e,t){for(var n,i,o,r=new b(e,4);i=r.nextNode();)for(;(o=i.data.indexOf(s))>-1&&(!t||i.parentNode!==t);){if(1===i.length){do{(n=i.parentNode).removeChild(i),i=n,r.currentNode=n}while(I(i)&&!Q(i));break}i.deleteData(o,1)}};Ye._didAddZWS=function(){this._hasZWS=!0},Ye._removeZWS=function(){this._hasZWS&&(ot(this._root),this._hasZWS=!1)},Ye._updatePath=function(e,t){if(e){var i,o=e.startContainer,r=e.endContainer;(t||o!==this._lastAnchorNode||r!==this._lastFocusNode)&&(this._lastAnchorNode=o,this._lastFocusNode=r,i=o&&r?o===r?function e(t,i,o){var r,a,s,d,l,c="";return t&&t!==i&&(c=e(t.parentNode,i,o),t.nodeType===n&&(c+=(c?">":"")+t.nodeName,(r=t.id)&&(c+="#"+r),(a=t.className.trim())&&((s=a.split(/\s\s*/)).sort(),c+=".",c+=s.join(".")),(d=t.dir)&&(c+="[dir="+d+"]"),s&&(l=o.classNames,T.call(s,l.highlight)>-1&&(c+="[backgroundColor="+t.style.backgroundColor.replace(/ /g,"")+"]"),T.call(s,l.colour)>-1&&(c+="[color="+t.style.color.replace(/ /g,"")+"]"),T.call(s,l.fontFamily)>-1&&(c+="[fontFamily="+t.style.fontFamily.replace(/ /g,"")+"]"),T.call(s,l.fontSize)>-1&&(c+="[fontSize="+t.style.fontSize+"]")))),c}(r,this._root,this._config):"(selection)":"",this._path!==i&&(this._path=i,this.fireEvent("pathChange",{path:i}))),this.fireEvent(e.collapsed?"cursor":"select",{range:e})}},Ye._updatePathOnEvent=function(){var e=this;e._isFocused&&!e._willUpdatePath&&(e._willUpdatePath=!0,setTimeout(function(){e._willUpdatePath=!1,e._updatePath(e.getSelection())},0))},Ye.focus=function(){return this._root.focus({preventScroll:!0}),v&&this.fireEvent("focus"),this},Ye.blur=function(){return this._root.blur(),v&&this.fireEvent("blur"),this};var rt="squire-selection-end";Ye._saveRangeToBookmark=function(e){var t,n=this.createElement("INPUT",{id:"squire-selection-start",type:"hidden"}),i=this.createElement("INPUT",{id:rt,type:"hidden"});ae(e,n),e.collapse(!1),ae(e,i),2&n.compareDocumentPosition(i)&&(n.id=rt,i.id="squire-selection-start",t=n,n=i,i=t),e.setStartAfter(n),e.setEndBefore(i)},Ye._getRangeAndRemoveBookmark=function(e){var t=this._root,n=t.querySelector("#squire-selection-start"),o=t.querySelector("#"+rt);if(n&&o){var r=n.parentNode,a=o.parentNode,s=T.call(r.childNodes,n),d=T.call(a.childNodes,o);r===a&&(d-=1),$(n),$(o),e||(e=this._doc.createRange()),e.setStart(r,s),e.setEnd(a,d),te(r,e),r!==a&&te(a,e),e.collapsed&&(r=e.startContainer).nodeType===i&&((a=r.childNodes[e.startOffset])&&a.nodeType===i||(a=r.childNodes[e.startOffset-1]),a&&a.nodeType===i&&(e.setStart(a,0),e.collapse(!0)))}return e||null},Ye._keyUpDetectChange=function(e){var t=e.keyCode;e.ctrlKey||e.metaKey||e.altKey||!(t<16||t>20)||!(t<33||t>45)||this._docWasChanged()},Ye._docWasChanged=function(){S&&(D=new WeakMap),this._ignoreAllChanges||(N&&this._ignoreChange?this._ignoreChange=!1:(this._isInUndoState&&(this._isInUndoState=!1,this.fireEvent("undoStateChange",{canUndo:!0,canRedo:!1})),this.fireEvent("input")))},Ye._recordUndoState=function(e,t){if(!this._isInUndoState||t){var n,i=this._undoIndex,o=this._undoStack,r=this._config.undo,a=r.documentSizeThreshold,s=r.undoLimit;t||(i+=1),i-1&&2*n.length>a&&s>-1&&i>s&&(o.splice(0,i-s),i=s,this._undoStackLength=s),o[i]=n,this._undoIndex=i,this._undoStackLength+=1,this._isInUndoState=!0}},Ye.saveUndoState=function(e){return e===t&&(e=this.getSelection()),this._recordUndoState(e,this._isInUndoState),this._getRangeAndRemoveBookmark(e),this},Ye.undo=function(){if(0!==this._undoIndex||!this._isInUndoState){this._recordUndoState(this.getSelection(),!1),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},Ye.redo=function(){var e=this._undoIndex,t=this._undoStackLength;if(e+1c&&h.splitText(c),h===s&&l&&(h=h.splitText(l),d===s&&(d=h,c-=l),s=h,l=0),j(h,o=this.createElement(e,t)),o.appendChild(h))}while(r.nextNode());d.nodeType!==i&&(h.nodeType===i?(d=h,c=h.length):(d=h.parentNode,c=1)),n=this.createRange(s,l,d,c)}return n},Ye._removeFormat=function(e,t,n,o){this._saveRangeToBookmark(n);var r,a=this._doc;n.collapsed&&(_?(r=a.createTextNode(s),this._didAddZWS()):r=a.createTextNode(""),ae(n,r));for(var d=n.commonAncestorContainer;I(d);)d=d.parentNode;var l=n.startContainer,c=n.startOffset,h=n.endContainer,u=n.endOffset,f=[],p=function(e,t){if(!ce(n,e,!1)){var o,r,a=e.nodeType===i;if(ce(n,e,!0))if(a)e===h&&u!==e.length&&f.push([t,e.splitText(u)]),e===l&&c&&(e.splitText(c),f.push([t,e]));else for(o=e.firstChild;o;o=r)r=o.nextSibling,p(o,t);else"INPUT"===e.nodeName||a&&!e.data||f.push([t,e])}},g=Array.prototype.filter.call(d.getElementsByTagName(e),function(i){return ce(n,i,!0)&&K(i,e,t)});return o||g.forEach(function(e){p(e,e)}),f.forEach(function(e){var t=e[0].cloneNode(!1),n=e[1];j(n,t),t.appendChild(n)}),g.forEach(function(e){j(e,V(e))}),this._getRangeAndRemoveBookmark(n),r&&n.collapse(!1),te(d,n),n},Ye.changeFormat=function(e,t,n,i){return n||(n=this.getSelection())?(this.saveUndoState(n),t&&(n=this._removeFormat(t.tag.toUpperCase(),t.attributes||{},n,i)),e&&(n=this._addFormat(e.tag.toUpperCase(),e.attributes||{},n)),this.setSelection(n),this._updatePath(n,!0),N||this._docWasChanged(),this):this};var at={DT:"DD",DD:"DT",LI:"LI",PRE:"PRE"},st=function(e,t,n,i){var o=at[t.nodeName],r=null,a=ee(n,i,t.parentNode,e._root),s=e._config;return o||(o=s.blockTag,r=s.blockAttributes),K(a,o,r)||(t=Y(a.ownerDocument,o,r),a.dir&&(t.dir=a.dir),j(a,t),t.appendChild(V(a)),a=t),a};Ye.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var i=this._root,o=pe(n,i),r=ge(n,i);if(o&&r)do{if(e(o)||o===r)break}while(o=W(o,i));return t&&(this.setSelection(n),this._updatePath(n,!0),N||this._docWasChanged()),this},Ye.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._recordUndoState(t,this._isInUndoState);var n,i=this._root;return _e(t,i),ue(t,i,i,i),n=se(t,i,i),ae(t,e.call(this,n)),t.endOffset]+|\([^\s()<>]+\))+(?:[^\s?&`!()\[\]{};:'".,<>«»“”‘’]|\([^\s()<>]+\)))|([\w\-.%+]+@(?:[\w\-]+\.)+[a-z]{2,}\b(?:[?][^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+(?:&[^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+)*)?))/i;var ut=function(e,t,n){var i,o,r,a,s,d,l,c=e.ownerDocument,h=new b(e,4,function(e){return!G(e,t,"A")}),u=n.linkRegExp,f=n._config.tagAttributes.a;if(u)for(;i=h.nextNode();)for(o=i.data,r=i.parentNode;a=u.exec(o);)d=(s=a.index)+a[0].length,s&&(l=c.createTextNode(o.slice(0,s)),r.insertBefore(l,i)),(l=n.createElement("A",je({href:a[1]?/^(?:ht|f)tps?:/i.test(a[1])?a[1]:"http://"+a[1]:"mailto:"+a[0]},f,!1))).textContent=o.slice(s,d),r.insertBefore(l,i),i.data=o=o.slice(d)};Ye.insertHTML=function(e,t){var n,i,o,r,a,s,d,l=this._config,c=l.isInsertedHTMLSanitized?l.sanitizeToDOMFragment:null,h=this.getSelection(),u=this._doc;"function"==typeof c?r=c(e,t,this):(t&&(n=e.indexOf("\x3c!--StartFragment--\x3e"),i=e.lastIndexOf("\x3c!--EndFragment--\x3e"),n>-1&&i>-1&&(e=e.slice(n+20,i))),/<\/td>((?!<\/tr>)[\s\S])*$/i.test(e)&&(e=""+e+""),/<\/tr>((?!<\/table>)[\s\S])*$/i.test(e)&&(e=""+e+"
"),(o=this.createElement("DIV")).innerHTML=e,(r=u.createDocumentFragment()).appendChild(V(o))),this.saveUndoState(h);try{for(a=this._root,s=r,d={fragment:r,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},ut(r,r,this),we(r,l),We(r,a,!1),Fe(r),r.normalize();s=W(s,r);)X(s,a);t&&this.fireEvent("willPaste",d),d.defaultPrevented||(le(h,d.fragment,a),N||this._docWasChanged(),h.collapse(!1),fe(h,0,a),this._ensureBottomLine()),this.setSelection(h),this._updatePath(h,!0),t&&this.focus()}catch(e){this.didError(e)}return this};var ft=function(e){return e.split("&").join("&").split("<").join("<").split(">").join(">").split('"').join(""")};Ye.insertPlainText=function(e,t){var n=this.getSelection();if(n.collapsed&&G(n.startContainer,this._root,"PRE")){var o,r,a=n.startContainer,s=n.startOffset;return a&&a.nodeType===i||(o=this._doc.createTextNode(""),a.insertBefore(o,a.childNodes[s]),a=o,s=0),r={text:e,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},t&&this.fireEvent("willPaste",r),r.defaultPrevented||(e=r.text,a.insertData(s,e),n.setStart(a,s+e.length),n.collapse(!0)),this.setSelection(n),this}var d,l,c,h,u=e.split("\n"),f=this._config,p=f.blockTag,g=f.blockAttributes,m="",v="<"+p;for(d in g)v+=" "+d+'="'+ft(g[d])+'"';for(v+=">",l=0,c=u.length;l")+m),u[l]=h;return this.insertHTML(u.join(""),t)};var pt=function(e,t,n){return function(){return this[e](t,n),this.focus()}};Ye.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},Ye.bold=pt("changeFormat",{tag:"B"}),Ye.italic=pt("changeFormat",{tag:"I"}),Ye.underline=pt("changeFormat",{tag:"U"}),Ye.strikethrough=pt("changeFormat",{tag:"S"}),Ye.subscript=pt("changeFormat",{tag:"SUB"},{tag:"SUP"}),Ye.superscript=pt("changeFormat",{tag:"SUP"},{tag:"SUB"}),Ye.removeBold=pt("changeFormat",null,{tag:"B"}),Ye.removeItalic=pt("changeFormat",null,{tag:"I"}),Ye.removeUnderline=pt("changeFormat",null,{tag:"U"}),Ye.removeStrikethrough=pt("changeFormat",null,{tag:"S"}),Ye.removeSubscript=pt("changeFormat",null,{tag:"SUB"}),Ye.removeSuperscript=pt("changeFormat",null,{tag:"SUP"}),Ye.makeLink=function(e,t){var n=this.getSelection();if(n.collapsed){var i=e.indexOf(":")+1;if(i)for(;"/"===e[i];)i+=1;ae(n,this._doc.createTextNode(e.slice(i)))}return t=je(je({href:e},t,!0),this._config.tagAttributes.a,!1),this.changeFormat({tag:"A",attributes:t},{tag:"A"},n),this.focus()},Ye.removeLink=function(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0),this.focus()},Ye.setFontFace=function(e){var t=this._config.classNames.fontFamily;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"font-family: "+e+", sans-serif;"}}:null,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ye.setFontSize=function(e){var t=this._config.classNames.fontSize;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"font-size: "+("number"==typeof e?e+"px":e)}}:null,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ye.setTextColour=function(e){var t=this._config.classNames.colour;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"color:"+e}}:null,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ye.setHighlightColour=function(e){var t=this._config.classNames.highlight;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"background-color:"+e}}:e,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ye.setTextAlignment=function(e){return this.forEachBlock(function(t){var n=t.className.split(/\s+/).filter(function(e){return!!e&&!/^align/.test(e)}).join(" ");e?(t.className=n+" align-"+e,t.style.textAlign=e):(t.className=n,t.style.textAlign="")},!0),this.focus()},Ye.setTextDirection=function(e){return this.forEachBlock(function(t){e?t.dir=e:t.removeAttribute("dir")},!0),this.focus()};var gt=function(e){for(var t,n=this._root,i=this._doc,o=i.createDocumentFragment(),r=M(e,n);t=r.nextNode();){var a,s,d=t.querySelectorAll("BR"),l=[],c=d.length;for(a=0;a-1;)r.appendChild(s.createTextNode(o.slice(0,a))),r.appendChild(s.createElement("BR")),o=o.slice(a+1);i.parentNode.insertBefore(r,i),i.data=o}J(t,d),j(t,V(t))}return e};Ye.code=function(){var e=this.getSelection();return e.collapsed||F(e.commonAncestorContainer)?this.modifyBlocks(gt,e):this.changeFormat({tag:"CODE",attributes:this._config.tagAttributes.code},null,e),this.focus()},Ye.removeCode=function(){var e=this.getSelection();return G(e.commonAncestorContainer,this._root,"PRE")?this.modifyBlocks(mt,e):this.changeFormat(null,{tag:"CODE"},e),this.focus()},Ye.toggleCode=function(){return this.hasFormat("PRE")||this.hasFormat("CODE")?this.removeCode():this.code(),this},Ye.removeAllFormatting=function(e){if(!e&&!(e=this.getSelection())||e.collapsed)return this;for(var t=this._root,n=e.commonAncestorContainer;n&&!w(n);)n=n.parentNode;if(n||(_e(e,t),n=t),n.nodeType===i)return this;this.saveUndoState(e),ue(e,n,n,t);for(var o,r,a=n.ownerDocument,s=e.startContainer,d=e.startOffset,l=e.endContainer,c=e.endOffset,h=a.createDocumentFragment(),u=a.createDocumentFragment(),f=ee(l,c,n,t),p=ee(s,d,n,t);p!==f;)o=p.nextSibling,h.appendChild(p),p=o;return function e(t,n,o){var r,a;for(r=n.firstChild;r;r=a){if(a=r.nextSibling,I(r)){if(r.nodeType===i||"BR"===r.nodeName||"IMG"===r.nodeName){o.appendChild(r);continue}}else if(w(r)){o.appendChild(t.createDefaultBlock([e(t,r,t._doc.createDocumentFragment())]));continue}e(t,r,o)}return o}(this,h,u),u.normalize(),p=u.firstChild,o=u.lastChild,r=n.childNodes,p?(n.insertBefore(u,f),d=T.call(r,p),c=T.call(r,o)+1):c=d=T.call(r,f),e.setStart(n,d),e.setEnd(n,c),te(n,e),he(e),this.setSelection(e),this._updatePath(e,!0),this.focus()},Ye.increaseQuoteLevel=pt("modifyBlocks",function(e){return this.createElement("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e])}),Ye.decreaseQuoteLevel=pt("modifyBlocks",dt),Ye.makeUnorderedList=pt("modifyBlocks",function(e){return ct(this,e,"UL"),e}),Ye.makeOrderedList=pt("modifyBlocks",function(e){return ct(this,e,"OL"),e}),Ye.removeList=pt("modifyBlocks",function(e){var t,n,i,o,r,a=e.querySelectorAll("UL, OL"),s=e.querySelectorAll("LI"),d=this._root;for(t=0,n=a.length;tl?o.startOffset-=1:o.startOffset===l&&(o.startContainer=a,o.startOffset=Q(a))),o.endContainer===t&&(o.endOffset>l?o.endOffset-=1:o.endOffset===l&&(o.endContainer=a,o.endOffset=Q(a))),$(r),r.nodeType===i?a.appendData(r.data):c.push(V(r));else if(r.nodeType===n){for(s=c.length;s--;)r.appendChild(c.pop());e(r,o)}}(e,o),t.setStart(o.startContainer,o.startOffset),t.setEnd(o.endContainer,o.endOffset)}}function ne(e,t,i,o){for(var r,a,s,d=t;(r=d.parentNode)&&r!==o&&r.nodeType===n&&1===r.childNodes.length;)d=r;$(d),s=e.childNodes.length,(a=e.lastChild)&&"BR"===a.nodeName&&(e.removeChild(a),s-=1),e.appendChild(V(t)),i.setStart(e,s),i.collapse(!0),te(e,i)}function ie(e,t){var n,i,o=e.previousSibling,r=e.firstChild,a=e.ownerDocument,s="LI"===e.nodeName;if(!s||r&&/^[OU]L$/.test(r.nodeName))if(o&&q(o,e)){if(!F(o)){if(!s)return;(i=Y(a,"DIV")).appendChild(V(o)),o.appendChild(i)}$(e),n=!F(e),o.appendChild(V(e)),n&&J(o,t),r&&ie(r,t)}else s&&(o=Y(a,"DIV"),e.insertBefore(o,r),X(o,t))}var oe=function(e,t){for(var i=e.childNodes;t&&e.nodeType===n;)t=(i=(e=i[t-1]).childNodes).length;return e},re=function(e,t){if(e.nodeType===n){var i=e.childNodes;if(t-1,r=e.compareBoundaryPoints(1,i)<1;return!o&&!r}var a=e.compareBoundaryPoints(0,i)<1,s=e.compareBoundaryPoints(2,i)>-1;return a&&s},he=function(e){for(var t,n=e.startContainer,o=e.startOffset,r=e.endContainer,a=e.endOffset,s=!0;n.nodeType!==i&&(t=n.childNodes[o])&&!P(t);)n=t,o=0;if(a)for(;r.nodeType!==i;){if(!(t=r.childNodes[a-1])||P(t)){if(s&&t&&"BR"===t.nodeName){a-=1,s=!1;continue}break}a=Q(r=t)}else for(;r.nodeType!==i&&(t=r.firstChild)&&!P(t);)r=t;e.collapsed?(e.setStart(r,a),e.setEnd(n,o)):(e.setStart(n,o),e.setEnd(r,a))},ue=function(e,t,n,o){var r,a=e.startContainer,s=e.startOffset,d=e.endContainer,l=e.endOffset,c=!0;for(t||(t=e.commonAncestorContainer),n||(n=t);!s&&a!==t&&a!==o;)r=a.parentNode,s=T.call(r.childNodes,a),a=r;for(;d!==n&&d!==o&&(c&&d.nodeType!==i&&d.childNodes[l]&&"BR"===d.childNodes[l].nodeName&&(l+=1,c=!1),l===Q(d));)r=d.parentNode,l=T.call(r.childNodes,d)+1,d=r;e.setStart(a,s),e.setEnd(d,l)},fe=function(e,t,n){var i=G(e.endContainer,n,"A");if(i){var o=e.cloneRange();i=i.parentNode,ue(o,i,i,n),o.endContainer===i&&(e.setStart(o.endContainer,o.endOffset),e.setEnd(o.endContainer,o.endOffset))}return e},pe=function(e,t){var n,i=e.startContainer;return(n=I(i)?H(i,t):i!==t&&w(i)?i:W(n=oe(i,e.startOffset),t))&&ce(e,n,!0)?n:null},ge=function(e,t){var n,i,o=e.endContainer;if(I(o))n=H(o,t);else if(o!==t&&w(o))n=o;else{if(!(n=re(o,e.endOffset))||!Z(t,n))for(n=t;i=n.lastChild;)n=i;n=H(n,t)}return n&&ce(e,n,!0)?n:null},me=new b(null,4|a,function(e){return e.nodeType===i?y.test(e.data):"IMG"===e.nodeName}),ve=function(e,t){var n,o=e.startContainer,r=e.startOffset;if(me.root=null,o.nodeType===i){if(r)return!1;n=o}else if((n=re(o,r))&&!Z(t,n)&&(n=null),!n&&(n=oe(o,r)).nodeType===i&&n.length)return!1;return me.currentNode=n,me.root=pe(e,t),!me.previousNode()},Ce=function(e,t){var n,o=e.endContainer,r=e.endOffset;if(me.root=null,o.nodeType===i){if((n=o.data.length)&&r)BLOCKQUOTE/.test(o)||!/(?:^|>)[OU]L/.test(o)?n[e]():n[t]()}},Oe=function(e,t){return function(n,i){i.preventDefault();var o=n.getPath();e.test(o)?n.removeList():n[t]()}};ke[C+"b"]=Te("B"),ke[C+"i"]=Te("I"),ke[C+"u"]=Te("U"),ke[C+"shift-7"]=Te("S"),ke[C+"shift-5"]=Te("SUB",{tag:"SUP"}),ke[C+"shift-6"]=Te("SUP",{tag:"SUB"}),ke[C+"shift-8"]=Oe(/(?:^|>)UL/,"makeUnorderedList"),ke[C+"shift-9"]=Oe(/(?:^|>)OL/,"makeOrderedList"),ke[C+"["]=xe("decreaseQuoteLevel","decreaseListLevel"),ke[C+"]"]=xe("increaseQuoteLevel","increaseListLevel"),ke[C+"d"]=ye("toggleCode"),ke[C+"y"]=ye("redo"),ke[C+"z"]=ye("undo"),ke[C+"shift-z"]=ye("redo");var Ae={1:10,2:13,3:16,4:18,5:24,6:32,7:48},Be={fontWeight:{regexp:/^bold|^700/i,replace:function(e){return Y(e,"B")}},fontStyle:{regexp:/^italic/i,replace:function(e){return Y(e,"I")}},fontFamily:{regexp:y,replace:function(e,t,n){return Y(e,"SPAN",{class:t.fontFamily,style:"font-family:"+n})}},fontSize:{regexp:y,replace:function(e,t,n){return Y(e,"SPAN",{class:t.fontSize,style:"font-size:"+n})}},textDecoration:{regexp:/^underline/i,replace:function(e){return Y(e,"U")}}},Re=function(e){return function(t,n){var i,o,r,a=Y(t.ownerDocument,e),s=t.attributes;for(i=0,o=s.length;i-1||!p&&T.call(o,"text/plain")>-1&&T.call(o,"text/rtf")<0))return e.preventDefault(),void(!d&&(r=a.getData("text/html"))?this.insertHTML(r,!0):((r=a.getData("text/plain"))||(r=a.getData("text/uri-list")))&&this.insertPlainText(r,!0));this._awaitingPaste=!0;var v=this._doc.body,C=this.getSelection(),_=C.startContainer,N=C.startOffset,S=C.endContainer,y=C.endOffset,E=this.createElement("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});v.appendChild(E),C.selectNodeContents(E),this.setSelection(C),setTimeout(function(){try{m._awaitingPaste=!1;for(var e,t,n="",i=E;E=i;)i=E.nextSibling,$(E),(e=E.firstChild)&&e===E.lastChild&&"DIV"===e.nodeName&&(E=e),n+=E.innerHTML;t=m.createRange(_,N,S,y),m.setSelection(t),n&&m.insertHTML(n,!0)}catch(e){m.didError(e)}},0)},$e=function(e){for(var t=e.dataTransfer.types,n=t.length,i=!1,o=!1;n--;)switch(t[n]){case"text/plain":i=!0;break;case"text/html":o=!0;break;default:return}(o||i)&&this.saveUndoState()};function je(e,t,n){var i,o;if(e||(e={}),t)for(i in t)!n&&i in e||(o=t[i],e[i]=o&&o.constructor===Object?je(e[i],o,n):o);return e}function Ve(e,t){e.nodeType===o&&(e=e.body);var n,i=e.ownerDocument,r=i.defaultView;this._win=r,this._doc=i,this._root=e,this._events={},this._isFocused=!1,this._lastSelection=null,this._hasZWS=!1,this._lastAnchorNode=null,this._lastFocusNode=null,this._path="",this._willUpdatePath=!1,"onselectionchange"in i?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,N?((n=new MutationObserver(this._docWasChanged.bind(this))).observe(e,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._mutation=n):this.addEventListener("keyup",this._keyUpDetectChange),this._restoreSelection=!1,this.addEventListener("blur",tt),this.addEventListener("mousedown",nt),this.addEventListener("touchstart",nt),this.addEventListener("focus",it),this._awaitingPaste=!1,this.addEventListener("cut",qe),this.addEventListener("copy",Ge),this.addEventListener("keydown",Ze),this.addEventListener("keyup",Ze),this.addEventListener("paste",Qe),this.addEventListener("drop",$e),this.addEventListener("keydown",Se),this._keyHandlers=Object.create(ke),this.setConfig(t),e.setAttribute("contenteditable","true"),e.setAttribute("data-gramm","false");try{i.execCommand("enableObjectResizing",!1,"false"),i.execCommand("enableInlineTableEditing",!1,"false")}catch(e){}e.__squire__=this,this.setHTML("")}var Ye=Ve.prototype,Xe=function(e,t,n){var i=n._doc,o=e?DOMPurify.sanitize(e,{ALLOW_UNKNOWN_PROTOCOLS:!0,WHOLE_DOCUMENT:!1,RETURN_DOM:!0,RETURN_DOM_FRAGMENT:!0}):null;return o?i.importNode(o,!0):i.createDocumentFragment()};Ye.setConfig=function(e){return(e=je({blockTag:"DIV",blockAttributes:null,tagAttributes:{blockquote:null,ul:null,ol:null,li:null,a:null},classNames:{colour:"colour",fontFamily:"font",fontSize:"size",highlight:"highlight"},leafNodeNames:x,undo:{documentSizeThreshold:-1,undoLimit:-1},isInsertedHTMLSanitized:!0,isSetHTMLSanitized:!0,sanitizeToDOMFragment:"undefined"!=typeof DOMPurify&&DOMPurify.isSupported?Xe:null,willCutCopy:null,addLinks:!0},e,!0)).blockTag=e.blockTag.toUpperCase(),this._config=e,this},Ye.createElement=function(e,t,n){return Y(this._doc,e,t,n)},Ye.createDefaultBlock=function(e){var t=this._config;return X(this.createElement(t.blockTag,t.blockAttributes,e),this._root)},Ye.didError=function(e){console.log(e)},Ye.getDocument=function(){return this._doc},Ye.getRoot=function(){return this._root},Ye.modifyDocument=function(e){var t=this._mutation;t&&(t.takeRecords().length&&this._docWasChanged(),t.disconnect()),this._ignoreAllChanges=!0,e(),this._ignoreAllChanges=!1,t&&(t.observe(this._root,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._ignoreChange=!1)};var Je={pathChange:1,select:1,input:1,undoStateChange:1};Ye.fireEvent=function(e,t){var n,i,o,r=this._events[e];if(/^(?:focus|blur)/.test(e))if(n=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(r)for(t||(t={}),t.type!==e&&(t.type=e),i=(r=r.slice()).length;i--;){o=r[i];try{o.handleEvent?o.handleEvent(t):o.call(this,t)}catch(t){t.details="Squire: fireEvent error. Event type: "+e,this.didError(t)}}return this},Ye.destroy=function(){var e,t=this._events;for(e in t)this.removeEventListener(e);this._mutation&&this._mutation.disconnect(),delete this._root.__squire__,this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0},Ye.handleEvent=function(e){this.fireEvent(e.type,e)},Ye.addEventListener=function(e,t){var n=this._events[e],i=this._root;return t?(n||(n=this._events[e]=[],Je[e]||("selectionchange"===e&&(i=this._doc),i.addEventListener(e,this,!0))),n.push(t),this):(this.didError({name:"Squire: addEventListener with null or undefined fn",message:"Event type: "+e}),this)},Ye.removeEventListener=function(e,t){var n,i=this._events[e],o=this._root;if(i){if(t)for(n=i.length;n--;)i[n]===t&&i.splice(n,1);else i.length=0;i.length||(delete this._events[e],Je[e]||("selectionchange"===e&&(o=this._doc),o.removeEventListener(e,this,!0)))}return this},Ye.createRange=function(e,t,n,i){if(e instanceof this._win.Range)return e.cloneRange();var o=this._doc.createRange();return o.setStart(e,t),n?o.setEnd(n,i):o.setEnd(e,t),o},Ye.getCursorPosition=function(e){if(!e&&!(e=this.getSelection())||!e.getBoundingClientRect)return null;var t,n,i=e.getBoundingClientRect();return i&&!i.top&&(this._ignoreChange=!0,(t=this._doc.createElement("SPAN")).textContent=s,ae(e,t),i=t.getBoundingClientRect(),(n=t.parentNode).removeChild(t),te(n,e)),i},Ye._moveCursorTo=function(e){var t=this._root,n=this.createRange(t,e?0:t.childNodes.length);return he(n),this.setSelection(n),this},Ye.moveCursorToStart=function(){return this._moveCursorTo(!0)},Ye.moveCursorToEnd=function(){return this._moveCursorTo(!1)};var et=function(e){return e._win.getSelection()||null};function tt(){this._restoreSelection=!0}function nt(){this._restoreSelection=!1}function it(){this._restoreSelection&&this.setSelection(this._lastSelection)}Ye.setSelection=function(e){if(e)if(this._lastSelection=e,this._isFocused)if(c&&!this._restoreSelection)tt.call(this),this.blur(),this.focus();else{f&&this._win.focus();var t=et(this);t&&(t.removeAllRanges(),t.addRange(e))}else tt.call(this);return this},Ye.getSelection=function(){var e,t,n,i,o=et(this),r=this._root;return this._isFocused&&o&&o.rangeCount&&(t=(e=o.getRangeAt(0).cloneRange()).startContainer,n=e.endContainer,t&&P(t)&&e.setStartBefore(t),n&&P(n)&&e.setEndBefore(n)),e&&Z(r,e.commonAncestorContainer)?this._lastSelection=e:Z((i=(e=this._lastSelection).commonAncestorContainer).ownerDocument,i)||(e=null),e||(e=this.createRange(r.firstChild,0)),e},Ye.getSelectedText=function(){var e=this.getSelection();if(!e||e.collapsed)return"";var t,n=new b(e.commonAncestorContainer,4|a,function(t){return ce(e,t,!0)}),o=e.startContainer,r=e.endContainer,s=n.currentNode=o,d="",l=!1;for(n.filter(s)||(s=n.nextNode());s;)s.nodeType===i?(t=s.data)&&/\S/.test(t)&&(s===r&&(t=t.slice(0,e.endOffset)),s===o&&(t=t.slice(e.startOffset)),d+=t,l=!0):("BR"===s.nodeName||l&&!I(s))&&(d+="\n",l=!1),s=n.nextNode();return d},Ye.getPath=function(){return this._path};var ot=function(e,t){for(var n,i,o,r=new b(e,4);i=r.nextNode();)for(;(o=i.data.indexOf(s))>-1&&(!t||i.parentNode!==t);){if(1===i.length){do{(n=i.parentNode).removeChild(i),i=n,r.currentNode=n}while(I(i)&&!Q(i));break}i.deleteData(o,1)}};Ye._didAddZWS=function(){this._hasZWS=!0},Ye._removeZWS=function(){this._hasZWS&&(ot(this._root),this._hasZWS=!1)},Ye._updatePath=function(e,t){if(e){var i,o=e.startContainer,r=e.endContainer;(t||o!==this._lastAnchorNode||r!==this._lastFocusNode)&&(this._lastAnchorNode=o,this._lastFocusNode=r,i=o&&r?o===r?function e(t,i,o){var r,a,s,d,l,c="";return t&&t!==i&&(c=e(t.parentNode,i,o),t.nodeType===n&&(c+=(c?">":"")+t.nodeName,(r=t.id)&&(c+="#"+r),(a=t.className.trim())&&((s=a.split(/\s\s*/)).sort(),c+=".",c+=s.join(".")),(d=t.dir)&&(c+="[dir="+d+"]"),s&&(l=o.classNames,T.call(s,l.highlight)>-1&&(c+="[backgroundColor="+t.style.backgroundColor.replace(/ /g,"")+"]"),T.call(s,l.colour)>-1&&(c+="[color="+t.style.color.replace(/ /g,"")+"]"),T.call(s,l.fontFamily)>-1&&(c+="[fontFamily="+t.style.fontFamily.replace(/ /g,"")+"]"),T.call(s,l.fontSize)>-1&&(c+="[fontSize="+t.style.fontSize+"]")))),c}(r,this._root,this._config):"(selection)":"",this._path!==i&&(this._path=i,this.fireEvent("pathChange",{path:i}))),this.fireEvent(e.collapsed?"cursor":"select",{range:e})}},Ye._updatePathOnEvent=function(){var e=this;e._isFocused&&!e._willUpdatePath&&(e._willUpdatePath=!0,setTimeout(function(){e._willUpdatePath=!1,e._updatePath(e.getSelection())},0))},Ye.focus=function(){return this._root.focus({preventScroll:!0}),v&&this.fireEvent("focus"),this},Ye.blur=function(){return this._root.blur(),v&&this.fireEvent("blur"),this};var rt="squire-selection-end";Ye._saveRangeToBookmark=function(e){var t,n=this.createElement("INPUT",{id:"squire-selection-start",type:"hidden"}),i=this.createElement("INPUT",{id:rt,type:"hidden"});ae(e,n),e.collapse(!1),ae(e,i),2&n.compareDocumentPosition(i)&&(n.id=rt,i.id="squire-selection-start",t=n,n=i,i=t),e.setStartAfter(n),e.setEndBefore(i)},Ye._getRangeAndRemoveBookmark=function(e){var t=this._root,n=t.querySelector("#squire-selection-start"),o=t.querySelector("#"+rt);if(n&&o){var r=n.parentNode,a=o.parentNode,s=T.call(r.childNodes,n),d=T.call(a.childNodes,o);r===a&&(d-=1),$(n),$(o),e||(e=this._doc.createRange()),e.setStart(r,s),e.setEnd(a,d),te(r,e),r!==a&&te(a,e),e.collapsed&&(r=e.startContainer).nodeType===i&&((a=r.childNodes[e.startOffset])&&a.nodeType===i||(a=r.childNodes[e.startOffset-1]),a&&a.nodeType===i&&(e.setStart(a,0),e.collapse(!0)))}return e||null},Ye._keyUpDetectChange=function(e){var t=e.keyCode;e.ctrlKey||e.metaKey||e.altKey||!(t<16||t>20)||!(t<33||t>45)||this._docWasChanged()},Ye._docWasChanged=function(){S&&(D=new WeakMap),this._ignoreAllChanges||(N&&this._ignoreChange?this._ignoreChange=!1:(this._isInUndoState&&(this._isInUndoState=!1,this.fireEvent("undoStateChange",{canUndo:!0,canRedo:!1})),this.fireEvent("input")))},Ye._recordUndoState=function(e,t){if(!this._isInUndoState||t){var n,i=this._undoIndex,o=this._undoStack,r=this._config.undo,a=r.documentSizeThreshold,s=r.undoLimit;t||(i+=1),i-1&&2*n.length>a&&s>-1&&i>s&&(o.splice(0,i-s),i=s,this._undoStackLength=s),o[i]=n,this._undoIndex=i,this._undoStackLength+=1,this._isInUndoState=!0}},Ye.saveUndoState=function(e){return e===t&&(e=this.getSelection()),this._recordUndoState(e,this._isInUndoState),this._getRangeAndRemoveBookmark(e),this},Ye.undo=function(){if(0!==this._undoIndex||!this._isInUndoState){this._recordUndoState(this.getSelection(),!1),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},Ye.redo=function(){var e=this._undoIndex,t=this._undoStackLength;if(e+1c&&h.splitText(c),h===s&&l&&(h=h.splitText(l),d===s&&(d=h,c-=l),s=h,l=0),j(h,o=this.createElement(e,t)),o.appendChild(h))}while(r.nextNode());d.nodeType!==i&&(h.nodeType===i?(d=h,c=h.length):(d=h.parentNode,c=1)),n=this.createRange(s,l,d,c)}return n},Ye._removeFormat=function(e,t,n,o){this._saveRangeToBookmark(n);var r,a=this._doc;n.collapsed&&(_?(r=a.createTextNode(s),this._didAddZWS()):r=a.createTextNode(""),ae(n,r));for(var d=n.commonAncestorContainer;I(d);)d=d.parentNode;var l=n.startContainer,c=n.startOffset,h=n.endContainer,u=n.endOffset,f=[],p=function(e,t){if(!ce(n,e,!1)){var o,r,a=e.nodeType===i;if(ce(n,e,!0))if(a)e===h&&u!==e.length&&f.push([t,e.splitText(u)]),e===l&&c&&(e.splitText(c),f.push([t,e]));else for(o=e.firstChild;o;o=r)r=o.nextSibling,p(o,t);else"INPUT"===e.nodeName||a&&!e.data||f.push([t,e])}},g=Array.prototype.filter.call(d.getElementsByTagName(e),function(i){return ce(n,i,!0)&&K(i,e,t)});return o||g.forEach(function(e){p(e,e)}),f.forEach(function(e){var t=e[0].cloneNode(!1),n=e[1];j(n,t),t.appendChild(n)}),g.forEach(function(e){j(e,V(e))}),this._getRangeAndRemoveBookmark(n),r&&n.collapse(!1),te(d,n),n},Ye.changeFormat=function(e,t,n,i){return n||(n=this.getSelection())?(this.saveUndoState(n),t&&(n=this._removeFormat(t.tag.toUpperCase(),t.attributes||{},n,i)),e&&(n=this._addFormat(e.tag.toUpperCase(),e.attributes||{},n)),this.setSelection(n),this._updatePath(n,!0),N||this._docWasChanged(),this):this};var at={DT:"DD",DD:"DT",LI:"LI",PRE:"PRE"},st=function(e,t,n,i){var o=at[t.nodeName],r=null,a=ee(n,i,t.parentNode,e._root),s=e._config;return o||(o=s.blockTag,r=s.blockAttributes),K(a,o,r)||(t=Y(a.ownerDocument,o,r),a.dir&&(t.dir=a.dir),j(a,t),t.appendChild(V(a)),a=t),a};Ye.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var i=this._root,o=pe(n,i),r=ge(n,i);if(o&&r)do{if(e(o)||o===r)break}while(o=W(o,i));return t&&(this.setSelection(n),this._updatePath(n,!0),N||this._docWasChanged()),this},Ye.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._recordUndoState(t,this._isInUndoState);var n,i=this._root;return _e(t,i),ue(t,i,i,i),n=se(t,i,i),ae(t,e.call(this,n)),t.endOffset]+|\([^\s()<>]+\))+(?:[^\s?&`!()\[\]{};:'".,<>«»“”‘’]|\([^\s()<>]+\)))|([\w\-.%+]+@(?:[\w\-]+\.)+[a-z]{2,}\b(?:[?][^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+(?:&[^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+)*)?))/i;var ut=function(e,t,n){var i,o,r,a,s,d,l,c=e.ownerDocument,h=new b(e,4,function(e){return!G(e,t,"A")}),u=n.linkRegExp,f=n._config.tagAttributes.a;if(u)for(;i=h.nextNode();)for(o=i.data,r=i.parentNode;a=u.exec(o);)d=(s=a.index)+a[0].length,s&&(l=c.createTextNode(o.slice(0,s)),r.insertBefore(l,i)),(l=n.createElement("A",je({href:a[1]?/^(?:ht|f)tps?:/i.test(a[1])?a[1]:"http://"+a[1]:"mailto:"+a[0]},f,!1))).textContent=o.slice(s,d),r.insertBefore(l,i),i.data=o=o.slice(d)};Ye.insertHTML=function(e,t){var n,i,o,r,a,s,d,l=this._config,c=l.isInsertedHTMLSanitized?l.sanitizeToDOMFragment:null,h=this.getSelection(),u=this._doc;"function"==typeof c?r=c(e,t,this):(t&&(n=e.indexOf("\x3c!--StartFragment--\x3e"),i=e.lastIndexOf("\x3c!--EndFragment--\x3e"),n>-1&&i>-1&&(e=e.slice(n+20,i))),/<\/td>((?!<\/tr>)[\s\S])*$/i.test(e)&&(e=""+e+""),/<\/tr>((?!<\/table>)[\s\S])*$/i.test(e)&&(e=""+e+"
"),(o=this.createElement("DIV")).innerHTML=e,(r=u.createDocumentFragment()).appendChild(V(o))),this.saveUndoState(h);try{for(a=this._root,s=r,d={fragment:r,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},ut(r,r,this),we(r,l),We(r,a,!1),Fe(r),r.normalize();s=W(s,r);)X(s,a);t&&this.fireEvent("willPaste",d),d.defaultPrevented||(le(h,d.fragment,a),N||this._docWasChanged(),h.collapse(!1),fe(h,0,a),this._ensureBottomLine()),this.setSelection(h),this._updatePath(h,!0),t&&this.focus()}catch(e){this.didError(e)}return this};var ft=function(e){return e.split("&").join("&").split("<").join("<").split(">").join(">").split('"').join(""")};Ye.insertPlainText=function(e,t){var n=this.getSelection();if(n.collapsed&&G(n.startContainer,this._root,"PRE")){var o,r,a=n.startContainer,s=n.startOffset;return a&&a.nodeType===i||(o=this._doc.createTextNode(""),a.insertBefore(o,a.childNodes[s]),a=o,s=0),r={text:e,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},t&&this.fireEvent("willPaste",r),r.defaultPrevented||(e=r.text,a.insertData(s,e),n.setStart(a,s+e.length),n.collapse(!0)),this.setSelection(n),this}var d,l,c,h,u=e.split("\n"),f=this._config,p=f.blockTag,g=f.blockAttributes,m="",v="<"+p;for(d in g)v+=" "+d+'="'+ft(g[d])+'"';for(v+=">",l=0,c=u.length;l")+m),u[l]=h;return this.insertHTML(u.join(""),t)};var pt=function(e,t,n){return function(){return this[e](t,n),this.focus()}};Ye.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},Ye.bold=pt("changeFormat",{tag:"B"}),Ye.italic=pt("changeFormat",{tag:"I"}),Ye.underline=pt("changeFormat",{tag:"U"}),Ye.strikethrough=pt("changeFormat",{tag:"S"}),Ye.subscript=pt("changeFormat",{tag:"SUB"},{tag:"SUP"}),Ye.superscript=pt("changeFormat",{tag:"SUP"},{tag:"SUB"}),Ye.removeBold=pt("changeFormat",null,{tag:"B"}),Ye.removeItalic=pt("changeFormat",null,{tag:"I"}),Ye.removeUnderline=pt("changeFormat",null,{tag:"U"}),Ye.removeStrikethrough=pt("changeFormat",null,{tag:"S"}),Ye.removeSubscript=pt("changeFormat",null,{tag:"SUB"}),Ye.removeSuperscript=pt("changeFormat",null,{tag:"SUP"}),Ye.makeLink=function(e,t){var n=this.getSelection();if(n.collapsed){var i=e.indexOf(":")+1;if(i)for(;"/"===e[i];)i+=1;ae(n,this._doc.createTextNode(e.slice(i)))}return t=je(je({href:e},t,!0),this._config.tagAttributes.a,!1),this.changeFormat({tag:"A",attributes:t},{tag:"A"},n),this.focus()},Ye.removeLink=function(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0),this.focus()},Ye.setFontFace=function(e){var t=this._config.classNames.fontFamily;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"font-family: "+e+", sans-serif;"}}:null,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ye.setFontSize=function(e){var t=this._config.classNames.fontSize;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"font-size: "+("number"==typeof e?e+"px":e)}}:null,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ye.setTextColour=function(e){var t=this._config.classNames.colour;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"color:"+e}}:null,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ye.setHighlightColour=function(e){var t=this._config.classNames.highlight;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"background-color:"+e}}:e,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ye.setTextAlignment=function(e){return this.forEachBlock(function(t){var n=t.className.split(/\s+/).filter(function(e){return!!e&&!/^align/.test(e)}).join(" ");e?(t.className=n+" align-"+e,t.style.textAlign=e):(t.className=n,t.style.textAlign="")},!0),this.focus()},Ye.setTextDirection=function(e){return this.forEachBlock(function(t){e?t.dir=e:t.removeAttribute("dir")},!0),this.focus()};var gt=function(e){for(var t,n=this._root,i=this._doc,o=i.createDocumentFragment(),r=M(e,n);t=r.nextNode();){var a,s,d=t.querySelectorAll("BR"),l=[],c=d.length;for(a=0;a-1;)r.appendChild(s.createTextNode(o.slice(0,a))),r.appendChild(s.createElement("BR")),o=o.slice(a+1);i.parentNode.insertBefore(r,i),i.data=o}J(t,d),j(t,V(t))}return e};Ye.code=function(){var e=this.getSelection();return e.collapsed||F(e.commonAncestorContainer)?this.modifyBlocks(gt,e):this.changeFormat({tag:"CODE",attributes:this._config.tagAttributes.code},null,e),this.focus()},Ye.removeCode=function(){var e=this.getSelection();return G(e.commonAncestorContainer,this._root,"PRE")?this.modifyBlocks(mt,e):this.changeFormat(null,{tag:"CODE"},e),this.focus()},Ye.toggleCode=function(){return this.hasFormat("PRE")||this.hasFormat("CODE")?this.removeCode():this.code(),this},Ye.removeAllFormatting=function(e){if(!e&&!(e=this.getSelection())||e.collapsed)return this;for(var t=this._root,n=e.commonAncestorContainer;n&&!w(n);)n=n.parentNode;if(n||(_e(e,t),n=t),n.nodeType===i)return this;this.saveUndoState(e),ue(e,n,n,t);for(var o,r,a=n.ownerDocument,s=e.startContainer,d=e.startOffset,l=e.endContainer,c=e.endOffset,h=a.createDocumentFragment(),u=a.createDocumentFragment(),f=ee(l,c,n,t),p=ee(s,d,n,t);p!==f;)o=p.nextSibling,h.appendChild(p),p=o;return function e(t,n,o){var r,a;for(r=n.firstChild;r;r=a){if(a=r.nextSibling,I(r)){if(r.nodeType===i||"BR"===r.nodeName||"IMG"===r.nodeName){o.appendChild(r);continue}}else if(w(r)){o.appendChild(t.createDefaultBlock([e(t,r,t._doc.createDocumentFragment())]));continue}e(t,r,o)}return o}(this,h,u),u.normalize(),p=u.firstChild,o=u.lastChild,r=n.childNodes,p?(n.insertBefore(u,f),d=T.call(r,p),c=T.call(r,o)+1):c=d=T.call(r,f),e.setStart(n,d),e.setEnd(n,c),te(n,e),he(e),this.setSelection(e),this._updatePath(e,!0),this.focus()},Ye.increaseQuoteLevel=pt("modifyBlocks",function(e){return this.createElement("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e])}),Ye.decreaseQuoteLevel=pt("modifyBlocks",dt),Ye.makeUnorderedList=pt("modifyBlocks",function(e){return ct(this,e,"UL"),e}),Ye.makeOrderedList=pt("modifyBlocks",function(e){return ct(this,e,"OL"),e}),Ye.removeList=pt("modifyBlocks",function(e){var t,n,i,o,r,a=e.querySelectorAll("UL, OL"),s=e.querySelectorAll("LI"),d=this._root;for(t=0,n=a.length;t