diff --git a/build/squire-raw.js b/build/squire-raw.js index 5e3611d..2f76cda 100644 --- a/build/squire-raw.js +++ b/build/squire-raw.js @@ -1426,161 +1426,184 @@ var afterDelete = function ( self, range ) { } }; -var keyHandlers = { - enter: function ( self, event, range ) { - var root = self._root; - var block, parent, node, offset, nodeAfterSplit; +var handleEnter = function ( self, shiftKey, range ) { + var root = self._root; + var block, parent, node, offset, nodeAfterSplit; - // We handle this ourselves - event.preventDefault(); + // Save undo checkpoint and add any links in the preceding section. + // Remove any zws so we don't think there's content in an empty + // block. + self._recordUndoState( range ); + addLinks( range.startContainer, root, self ); + self._removeZWS(); + self._getRangeAndRemoveBookmark( range ); - // Save undo checkpoint and add any links in the preceding section. - // Remove any zws so we don't think there's content in an empty - // block. - self._recordUndoState( range ); - addLinks( range.startContainer, root, self ); - self._removeZWS(); - self._getRangeAndRemoveBookmark( range ); + // Selected text is overwritten, therefore delete the contents + // to collapse selection. + if ( !range.collapsed ) { + deleteContentsOfRange( range, root ); + } - // Selected text is overwritten, therefore delete the contents - // to collapse selection. - if ( !range.collapsed ) { - deleteContentsOfRange( range, root ); + block = getStartBlockOfRange( range, root ); + + // Inside a PRE, insert literal newline, unless on blank line. + if ( block && ( parent = getNearest( block, root, 'PRE' ) ) ) { + moveRangeBoundariesDownTree( range ); + node = range.startContainer; + offset = range.startOffset; + if ( node.nodeType !== TEXT_NODE ) { + node = self._doc.createTextNode( '' ); + parent.insertBefore( node, parent.firstChild ); } - - block = getStartBlockOfRange( range, root ); - - // Inside a PRE, insert literal newline, unless on blank line. - if ( block && ( parent = getNearest( block, root, 'PRE' ) ) ) { - moveRangeBoundariesDownTree( range ); - node = range.startContainer; - offset = range.startOffset; - if ( node.nodeType !== TEXT_NODE ) { - node = self._doc.createTextNode( '' ); - parent.insertBefore( node, parent.firstChild ); + // If blank line: split and insert default block + if ( !shiftKey && + ( node.data.charAt( offset - 1 ) === '\n' || + rangeDoesStartAtBlockBoundary( range, root ) ) && + ( node.data.charAt( offset ) === '\n' || + rangeDoesEndAtBlockBoundary( range, root ) ) ) { + node.deleteData( offset && offset - 1, offset ? 2 : 1 ); + nodeAfterSplit = + split( node, offset && offset - 1, root, root ); + node = nodeAfterSplit.previousSibling; + if ( !node.textContent ) { + detach( node ); } - // If blank line: split and insert default block - if ( !event.shiftKey && - ( node.data.charAt( offset - 1 ) === '\n' || - rangeDoesStartAtBlockBoundary( range, root ) ) && - ( node.data.charAt( offset ) === '\n' || - rangeDoesEndAtBlockBoundary( range, root ) ) ) { - node.deleteData( offset && offset - 1, offset ? 2 : 1 ); - nodeAfterSplit = - split( node, offset && offset - 1, root, root ); - node = nodeAfterSplit.previousSibling; - if ( !node.textContent ) { - detach( node ); - } - node = self.createDefaultBlock(); - nodeAfterSplit.parentNode.insertBefore( node, nodeAfterSplit ); - if ( !nodeAfterSplit.textContent ) { - detach( nodeAfterSplit ); - } - range.setStart( node, 0 ); + node = self.createDefaultBlock(); + nodeAfterSplit.parentNode.insertBefore( node, nodeAfterSplit ); + if ( !nodeAfterSplit.textContent ) { + detach( nodeAfterSplit ); + } + range.setStart( node, 0 ); + } else { + node.insertData( offset, '\n' ); + fixCursor( parent, root ); + // Firefox bug: if you set the selection in the text node after + // the new line, it draws the cursor before the line break still + // but if you set the selection to the equivalent position + // in the parent, it works. + if ( node.length === offset + 1 ) { + range.setStartAfter( node ); } else { - node.insertData( offset, '\n' ); - fixCursor( parent, root ); - // Firefox bug: if you set the selection in the text node after - // the new line, it draws the cursor before the line break still - // but if you set the selection to the equivalent position - // in the parent, it works. - if ( node.length === offset + 1 ) { - range.setStartAfter( node ); - } else { - range.setStart( node, offset + 1 ); - } - } - range.collapse( true ); - self.setSelection( range ); - self._updatePath( range, true ); - self._docWasChanged(); - return; - } - - // If this is a malformed bit of document or in a table; - // just play it safe and insert a
. - if ( !block || event.shiftKey || /^T[HD]$/.test( block.nodeName ) ) { - // If inside an , move focus out - parent = getNearest( range.endContainer, root, 'A' ); - if ( parent ) { - parent = parent.parentNode; - moveRangeBoundariesUpTree( range, parent, parent, root ); - range.collapse( false ); - } - insertNodeInRange( range, self.createElement( 'BR' ) ); - range.collapse( false ); - self.setSelection( range ); - self._updatePath( range, true ); - return; - } - - // If in a list, we'll split the LI instead. - if ( parent = getNearest( block, root, 'LI' ) ) { - block = parent; - } - - if ( isEmptyBlock( block ) ) { - // Break list - if ( getNearest( block, root, 'UL' ) || - getNearest( block, root, 'OL' ) ) { - return self.decreaseListLevel( range ); - } - // Break blockquote - else if ( getNearest( block, root, 'BLOCKQUOTE' ) ) { - return self.modifyBlocks( removeBlockQuote, range ); + range.setStart( node, offset + 1 ); } } - - // Otherwise, split at cursor point. - nodeAfterSplit = splitBlock( self, block, - range.startContainer, range.startOffset ); - - // Clean up any empty inlines if we hit enter at the beginning of the - // block - removeZWS( block ); - removeEmptyInlines( block ); - fixCursor( block, root ); - - // Focus cursor - // If there's a / etc. at the beginning of the split - // make sure we focus inside it. - while ( nodeAfterSplit.nodeType === ELEMENT_NODE ) { - var child = nodeAfterSplit.firstChild, - next; - - // Don't continue links over a block break; unlikely to be the - // desired outcome. - if ( nodeAfterSplit.nodeName === 'A' && - ( !nodeAfterSplit.textContent || - nodeAfterSplit.textContent === ZWS ) ) { - child = self._doc.createTextNode( '' ); - replaceWith( nodeAfterSplit, child ); - nodeAfterSplit = child; - break; - } - - while ( child && child.nodeType === TEXT_NODE && !child.data ) { - next = child.nextSibling; - if ( !next || next.nodeName === 'BR' ) { - break; - } - detach( child ); - child = next; - } - - // 'BR's essentially don't count; they're a browser hack. - // If you try to select the contents of a 'BR', FF will not let - // you type anything! - if ( !child || child.nodeName === 'BR' || - ( child.nodeType === TEXT_NODE && !isPresto ) ) { - break; - } - nodeAfterSplit = child; - } - range = self.createRange( nodeAfterSplit, 0 ); + range.collapse( true ); self.setSelection( range ); self._updatePath( range, true ); + self._docWasChanged(); + return; + } + + // If this is a malformed bit of document or in a table; + // just play it safe and insert a
. + if ( !block || shiftKey || /^T[HD]$/.test( block.nodeName ) ) { + // If inside an
, move focus out + parent = getNearest( range.endContainer, root, 'A' ); + if ( parent ) { + parent = parent.parentNode; + moveRangeBoundariesUpTree( range, parent, parent, root ); + range.collapse( false ); + } + insertNodeInRange( range, self.createElement( 'BR' ) ); + range.collapse( false ); + self.setSelection( range ); + self._updatePath( range, true ); + return; + } + + // If in a list, we'll split the LI instead. + if ( parent = getNearest( block, root, 'LI' ) ) { + block = parent; + } + + if ( isEmptyBlock( block ) ) { + // Break list + if ( getNearest( block, root, 'UL' ) || + getNearest( block, root, 'OL' ) ) { + return self.decreaseListLevel( range ); + } + // Break blockquote + else if ( getNearest( block, root, 'BLOCKQUOTE' ) ) { + return self.modifyBlocks( removeBlockQuote, range ); + } + } + + // Otherwise, split at cursor point. + nodeAfterSplit = splitBlock( self, block, + range.startContainer, range.startOffset ); + + // Clean up any empty inlines if we hit enter at the beginning of the + // block + removeZWS( block ); + removeEmptyInlines( block ); + fixCursor( block, root ); + + // Focus cursor + // If there's a / etc. at the beginning of the split + // make sure we focus inside it. + while ( nodeAfterSplit.nodeType === ELEMENT_NODE ) { + var child = nodeAfterSplit.firstChild, + next; + + // Don't continue links over a block break; unlikely to be the + // desired outcome. + if ( nodeAfterSplit.nodeName === 'A' && + ( !nodeAfterSplit.textContent || + nodeAfterSplit.textContent === ZWS ) ) { + child = self._doc.createTextNode( '' ); + replaceWith( nodeAfterSplit, child ); + nodeAfterSplit = child; + break; + } + + while ( child && child.nodeType === TEXT_NODE && !child.data ) { + next = child.nextSibling; + if ( !next || next.nodeName === 'BR' ) { + break; + } + detach( child ); + child = next; + } + + // 'BR's essentially don't count; they're a browser hack. + // If you try to select the contents of a 'BR', FF will not let + // you type anything! + if ( !child || child.nodeName === 'BR' || + ( child.nodeType === TEXT_NODE && !isPresto ) ) { + break; + } + nodeAfterSplit = child; + } + range = self.createRange( nodeAfterSplit, 0 ); + self.setSelection( range ); + self._updatePath( range, true ); +}; + +var keyHandlers = { + // This song and dance is to force iOS to do enable the shift key + // automatically on enter. When you do the DOM split manipulation yourself, + // WebKit doesn't reset the IME state and so presents auto-complete options + // as though you were continuing to type on the previous line, and doesn't + // auto-enable the shift key. The old trick of blurring and focussing + // again no longer works in iOS 13, and I tried various execCommand options + // but they didn't seem to do anything. The only solution I've found is to + // let iOS handle the enter key, then after it's done that reset the HTML + // to what it was before and handle it properly in Squire; the IME state of + // course doesn't reset so you end up in the correct state! + enter: isIOS ? function ( self, event, range ) { + self._saveRangeToBookmark( range ); + var html = self._getHTML(); + var restoreAndDoEnter = function () { + self.removeEventListener( 'keyup', restoreAndDoEnter ); + self._setHTML( html ); + range = self._getRangeAndRemoveBookmark(); + // Ignore the shift key on iOS, as this is for auto-capitalisation. + handleEnter( self, false, range ); + }; + self.addEventListener( 'keyup', restoreAndDoEnter ); + } : function ( self, event, range ) { + event.preventDefault(); + handleEnter( self, event.shiftKey, range ); }, 'shift-enter': function ( self, event, range ) { @@ -2254,9 +2277,7 @@ var onCut = function ( event ) { this.saveUndoState( range ); // Edge only seems to support setting plain text as of 2016-03-11. - // Mobile Safari flat out doesn't work: - // https://bugs.webkit.org/show_bug.cgi?id=143776 - if ( !isEdge && !isIOS && clipboardData ) { + if ( !isEdge && clipboardData ) { // Clipboard content should include all parents within block, or all // parents up to root if selection across blocks startBlock = getStartBlockOfRange( range, root ); @@ -2301,9 +2322,7 @@ var onCopy = function ( event ) { var startBlock, endBlock, copyRoot, contents, parent, newContents, node; // Edge only seems to support setting plain text as of 2016-03-11. - // Mobile Safari flat out doesn't work: - // https://bugs.webkit.org/show_bug.cgi?id=143776 - if ( !isEdge && !isIOS && clipboardData ) { + if ( !isEdge && clipboardData ) { // Clipboard content should include all parents within block, or all // parents up to root if selection across blocks startBlock = getStartBlockOfRange( range, root ); diff --git a/build/squire.js b/build/squire.js index b25f2fb..3be0a12 100644 --- a/build/squire.js +++ b/build/squire.js @@ -1 +1 @@ -!function(e,t){"use strict";var n=1,o=3,i=9,r=11,a=1,s="​",d=e.defaultView,l=navigator.userAgent,c=/Android/.test(l),h=/iP(?:ad|hone|od)/.test(l),u=/Mac OS X/.test(l),f=/Windows NT/.test(l),p=/Gecko\//.test(l),g=/Trident\/[456]\./.test(l),m=!!d.opera,v=/Edge\//.test(l),C=!v&&/WebKit\//.test(l),_=/Trident\/[4567]\./.test(l),N=u?"meta-":"ctrl-",S=g||m,y=g||C,T=g,E="undefined"!=typeof MutationObserver,b="undefined"!=typeof WeakMap,x=/[^ \t\r\n]/,k=Array.prototype.indexOf;Object.create||(Object.create=function(e){var t=function(){};return t.prototype=e,new t});var A={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,o=this.nodeType,i=this.filter;;){for(e=t.firstChild;!e&&t&&t!==n;)(e=t.nextSibling)||(t=t.parentNode);if(!e)return null;if(A[e.nodeType]&o&&i(e))return this.currentNode=e,e;t=e}},B.prototype.previousNode=function(){for(var e,t=this.currentNode,n=this.root,o=this.nodeType,i=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(A[e.nodeType]&o&&i(e))return this.currentNode=e,e;t=e}},B.prototype.previousPONode=function(){for(var e,t=this.currentNode,n=this.root,o=this.nodeType,i=this.filter;;){for(e=t.lastChild;!e&&t&&t!==n;)(e=t.previousSibling)||(t=t.parentNode);if(!e)return null;if(A[e.nodeType]&o&&i(e))return this.currentNode=e,e;t=e}};var O=/^(?:#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)$/,D={BR:1,HR:1,IFRAME:1,IMG:1,INPUT:1};var R=0,P=1,U=2,I=3,w=b?new WeakMap:null;function F(e){return e.nodeType===n&&!!D[e.nodeName]}function M(e){switch(e.nodeType){case o:return P;case n:case r:if(b&&w.has(e))return w.get(e);break;default:return R}var t;return t=function(e,t){for(var n=e.length;n--;)if(!t(e[n]))return!1;return!0}(e.childNodes,H)?O.test(e.nodeName)?P:U:I,b&&w.set(e,t),t}function H(e){return M(e)===P}function W(e){return M(e)===U}function z(e){return M(e)===I}function q(e,t){var n=new B(t,a,W);return n.currentNode=e,n}function K(e,t){return(e=q(e,t).previousNode())!==t?e:null}function G(e,t){return(e=q(e,t).nextNode())!==t?e:null}function Z(e){return!e.textContent&&!e.querySelector("IMG")}function j(e,t){return!F(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 $(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 Q(e,t,n,o){for(;e&&e!==t;){if($(e,n,o))return e;e=e.parentNode}return null}function V(e,t){for(;t;){if(t===e)return!0;t=t.parentNode}return!1}function Y(e){var t=e.nodeType;return t===n||t===r?e.childNodes.length:e.length||0}function X(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 ee(e){for(var t=e.ownerDocument.createDocumentFragment(),n=e.childNodes,o=n?n.length:0;o--;)t.appendChild(e.firstChild);return t}function te(e,n,o,i){var r,a,s,d=e.createElement(n);if(o instanceof Array&&(i=o,o=null),o)for(r in o)o[r]!==t&&d.setAttribute(r,o[r]);if(i)for(a=0,s=i.length;al?i.startOffset-=1:i.startOffset===l&&(i.startContainer=a,i.startOffset=Y(a))),i.endContainer===t&&(i.endOffset>l?i.endOffset-=1:i.endOffset===l&&(i.endContainer=a,i.endOffset=Y(a))),X(r),r.nodeType===o?a.appendData(r.data):c.push(ee(r));else if(r.nodeType===n){for(s=c.length;s--;)r.appendChild(c.pop());e(r,i)}}(e,i),t.setStart(i.startContainer,i.startOffset),t.setEnd(i.endContainer,i.endOffset)}}function ae(e,t,o,i){for(var r,a,s,d=t;(r=d.parentNode)&&r!==i&&r.nodeType===n&&1===r.childNodes.length;)d=r;X(d),s=e.childNodes.length,(a=e.lastChild)&&"BR"===a.nodeName&&(e.removeChild(a),s-=1),e.appendChild(ee(t)),o.setStart(e,s),o.collapse(!0),re(e,o),m&&(a=e.lastChild)&&"BR"===a.nodeName&&e.removeChild(a)}function se(e,t){var n,o,i=e.previousSibling,r=e.firstChild,a=e.ownerDocument,s="LI"===e.nodeName;if(!s||r&&/^[OU]L$/.test(r.nodeName))if(i&&j(i,e)){if(!z(i)){if(!s)return;(o=te(a,"DIV")).appendChild(ee(i)),i.appendChild(o)}X(e),n=!z(e),i.appendChild(ee(e)),n&&oe(i,t),r&&se(r,t)}else s&&(i=te(a,"DIV"),e.insertBefore(i,r),ne(i,t))}var de=function(e,t){for(var o=e.childNodes;t&&e.nodeType===n;)t=(o=(e=o[t-1]).childNodes).length;return e},le=function(e,t){if(e.nodeType===n){var o=e.childNodes;if(t-1,r=e.compareBoundaryPoints(1,o)<1;return!i&&!r}var a=e.compareBoundaryPoints(0,o)<1,s=e.compareBoundaryPoints(2,o)>-1;return a&&s},ge=function(e){for(var t,n=e.startContainer,i=e.startOffset,r=e.endContainer,a=e.endOffset,s=!0;n.nodeType!==o&&(t=n.childNodes[i])&&!F(t);)n=t,i=0;if(a)for(;r.nodeType!==o;){if(!(t=r.childNodes[a-1])||F(t)){if(s&&t&&"BR"===t.nodeName){a-=1,s=!1;continue}break}a=Y(r=t)}else for(;r.nodeType!==o&&(t=r.firstChild)&&!F(t);)r=t;e.collapsed?(e.setStart(r,a),e.setEnd(n,i)):(e.setStart(n,i),e.setEnd(r,a))},me=function(e,t,n,i){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!==i;)r=a.parentNode,s=k.call(r.childNodes,a),a=r;for(;c&&d.nodeType!==o&&d.childNodes[l]&&"BR"===d.childNodes[l].nodeName&&(l+=1,c=!1),d!==n&&d!==i&&l===Y(d);)r=d.parentNode,l=k.call(r.childNodes,d)+1,d=r;e.setStart(a,s),e.setEnd(d,l)},ve=function(e,t){var n,o=e.startContainer;return(n=H(o)?K(o,t):o!==t&&W(o)?o:G(n=de(o,e.startOffset),t))&&pe(e,n,!0)?n:null},Ce=function(e,t){var n,o,i=e.endContainer;if(H(i))n=K(i,t);else if(i!==t&&W(i))n=i;else{if(!(n=le(i,e.endOffset))||!V(t,n))for(n=t;o=n.lastChild;)n=o;n=K(n,t)}return n&&pe(e,n,!0)?n:null},_e=new B(null,4|a,function(e){return e.nodeType===o?x.test(e.data):"IMG"===e.nodeName}),Ne=function(e,t){var n,i=e.startContainer,r=e.startOffset;if(_e.root=null,i.nodeType===o){if(r)return!1;n=i}else if((n=le(i,r))&&!V(t,n)&&(n=null),!n&&(n=de(i,r)).nodeType===o&&n.length)return!1;return _e.currentNode=n,_e.root=ve(e,t),!_e.previousNode()},Se=function(e,t){var n,i=e.endContainer,r=e.endOffset;if(_e.root=null,i.nodeType===o){if((n=i.data.length)&&r-1||!p&&k.call(i,"text/plain")>-1&&k.call(i,"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 m=this._doc.body,C=this.getSelection(),_=C.startContainer,N=C.startOffset,S=C.endContainer,y=C.endOffset,T=this.createElement("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});m.appendChild(T),C.selectNodeContents(T),this.setSelection(C),setTimeout(function(){try{g._awaitingPaste=!1;for(var e,t,n="",o=T;T=o;)o=T.nextSibling,X(T),(e=T.firstChild)&&e===T.lastChild&&"DIV"===e.nodeName&&(T=e),n+=T.innerHTML;t=g.createRange(_,N,S,y),g.setSelection(t),n&&g.insertHTML(n,!0)}catch(e){g.didError(e)}},0)},je=function(e){for(var t=e.dataTransfer.types,n=t.length,o=!1,i=!1;n--;)switch(t[n]){case"text/plain":o=!0;break;case"text/html":i=!0;break;default:return}(i||o)&&this.saveUndoState()};function $e(e,t,n){var o,i;if(e||(e={}),t)for(o in t)!n&&o in e||(i=t[o],e[o]=i&&i.constructor===Object?$e(e[o],i,n):i);return e}function Qe(e,t){e.nodeType===i&&(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,T&&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,E?((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",et),this.addEventListener("mousedown",tt),this.addEventListener("touchstart",tt),this.addEventListener("focus",nt),this._awaitingPaste=!1,this.addEventListener(g?"beforecut":"cut",qe),this.addEventListener("copy",Ke),this.addEventListener("keydown",Ge),this.addEventListener("keyup",Ge),this.addEventListener(g?"beforepaste":"paste",Ze),this.addEventListener("drop",je),this.addEventListener(m?"keypress":"keydown",Ee),this._keyHandlers=Object.create(Ae),this.setConfig(t),g&&(r.Text.prototype.splitText=function(e){var t=this.ownerDocument.createTextNode(this.data.slice(e)),n=this.nextSibling,o=this.parentNode,i=this.length-e;return n?o.insertBefore(t,n):o.appendChild(t),i&&this.deleteData(e,i),t}),e.setAttribute("contenteditable","true");try{o.execCommand("enableObjectResizing",!1,"false"),o.execCommand("enableInlineTableEditing",!1,"false")}catch(e){}e.__squire__=this,this.setHTML("")}var Ve=Qe.prototype,Ye=function(e,t,n){var o=n._doc,i=e?DOMPurify.sanitize(e,{ALLOW_UNKNOWN_PROTOCOLS:!0,WHOLE_DOCUMENT:!1,RETURN_DOM:!0,RETURN_DOM_FRAGMENT:!0}):null;return i?o.importNode(i,!0):o.createDocumentFragment()};Ve.setConfig=function(e){return(e=$e({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:D,undo:{documentSizeThreshold:-1,undoLimit:-1},isInsertedHTMLSanitized:!0,isSetHTMLSanitized:!0,sanitizeToDOMFragment:"undefined"!=typeof DOMPurify&&DOMPurify.isSupported?Ye:null,willCutCopy:null},e,!0)).blockTag=e.blockTag.toUpperCase(),this._config=e,this},Ve.createElement=function(e,t,n){return te(this._doc,e,t,n)},Ve.createDefaultBlock=function(e){var t=this._config;return ne(this.createElement(t.blockTag,t.blockAttributes,e),this._root)},Ve.didError=function(e){console.log(e)},Ve.getDocument=function(){return this._doc},Ve.getRoot=function(){return this._root},Ve.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 Xe={pathChange:1,select:1,input:1,undoStateChange:1};Ve.fireEvent=function(e,t){var n,o,i,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),o=(r=r.slice()).length;o--;){i=r[o];try{i.handleEvent?i.handleEvent(t):i.call(this,t)}catch(t){t.details="Squire: fireEvent error. Event type: "+e,this.didError(t)}}return this},Ve.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},Ve.handleEvent=function(e){this.fireEvent(e.type,e)},Ve.addEventListener=function(e,t){var n=this._events[e],o=this._root;return t?(n||(n=this._events[e]=[],Xe[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)},Ve.removeEventListener=function(e,t){var n,o=this._events[e],i=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],Xe[e]||("selectionchange"===e&&(i=this._doc),i.removeEventListener(e,this,!0)))}return this},Ve.createRange=function(e,t,n,o){if(e instanceof this._win.Range)return e.cloneRange();var i=this._doc.createRange();return i.setStart(e,t),n?i.setEnd(n,o):i.setEnd(e,t),i},Ve.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")).textContent=s,ce(e,t),o=t.getBoundingClientRect(),(n=t.parentNode).removeChild(t),re(n,e)),o},Ve._moveCursorTo=function(e){var t=this._root,n=this.createRange(t,e?0:t.childNodes.length);return ge(n),this.setSelection(n),this},Ve.moveCursorToStart=function(){return this._moveCursorTo(!0)},Ve.moveCursorToEnd=function(){return this._moveCursorTo(!1)};var Je=function(e){return e._win.getSelection()||null};function et(){this._restoreSelection=!0}function tt(){this._restoreSelection=!1}function nt(){this._restoreSelection&&this.setSelection(this._lastSelection)}Ve.setSelection=function(e){if(e)if(this._lastSelection=e,this._isFocused)if(c&&!this._restoreSelection)et.call(this),this.blur(),this.focus();else{h&&this._win.focus();var t=Je(this);t&&(t.removeAllRanges(),t.addRange(e))}else et.call(this);return this},Ve.getSelection=function(){var e,t,n,o,i=Je(this),r=this._root;return this._isFocused&&i&&i.rangeCount&&(t=(e=i.getRangeAt(0).cloneRange()).startContainer,n=e.endContainer,t&&F(t)&&e.setStartBefore(t),n&&F(n)&&e.setEndBefore(n)),e&&V(r,e.commonAncestorContainer)?this._lastSelection=e:V((o=(e=this._lastSelection).commonAncestorContainer).ownerDocument,o)||(e=null),e||(e=this.createRange(r.firstChild,0)),e},Ve.getSelectedText=function(){var e=this.getSelection();if(!e||e.collapsed)return"";var t,n=new B(e.commonAncestorContainer,4|a,function(t){return pe(e,t,!0)}),i=e.startContainer,r=e.endContainer,s=n.currentNode=i,d="",l=!1;for(n.filter(s)||(s=n.nextNode());s;)s.nodeType===o?(t=s.data)&&/\S/.test(t)&&(s===r&&(t=t.slice(0,e.endOffset)),s===i&&(t=t.slice(e.startOffset)),d+=t,l=!0):("BR"===s.nodeName||l&&!H(s))&&(d+="\n",l=!1),s=n.nextNode();return d},Ve.getPath=function(){return this._path};var ot=function(e,t){for(var n,o,i,r=new B(e,4);o=r.nextNode();)for(;(i=o.data.indexOf(s))>-1&&(!t||o.parentNode!==t);){if(1===o.length){do{(n=o.parentNode).removeChild(o),o=n,r.currentNode=n}while(H(o)&&!Y(o));break}o.deleteData(i,1)}};Ve._didAddZWS=function(){this._hasZWS=!0},Ve._removeZWS=function(){this._hasZWS&&(ot(this._root),this._hasZWS=!1)},Ve._updatePath=function(e,t){if(e){var o,i=e.startContainer,r=e.endContainer;(t||i!==this._lastAnchorNode||r!==this._lastFocusNode)&&(this._lastAnchorNode=i,this._lastFocusNode=r,o=i&&r?i===r?function e(t,o,i){var r,a,s,d,l,c="";return t&&t!==o&&(c=e(t.parentNode,o,i),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=i.classNames,k.call(s,l.highlight)>-1&&(c+="[backgroundColor="+t.style.backgroundColor.replace(/ /g,"")+"]"),k.call(s,l.colour)>-1&&(c+="[color="+t.style.color.replace(/ /g,"")+"]"),k.call(s,l.fontFamily)>-1&&(c+="[fontFamily="+t.style.fontFamily.replace(/ /g,"")+"]"),k.call(s,l.fontSize)>-1&&(c+="[fontSize="+t.style.fontSize+"]")))),c}(r,this._root,this._config):"(selection)":"",this._path!==o&&(this._path=o,this.fireEvent("pathChange",{path:o}))),this.fireEvent(e.collapsed?"cursor":"select",{range:e})}},Ve._updatePathOnEvent=function(e){var t=this;t._isFocused&&!t._willUpdatePath&&(t._willUpdatePath=!0,setTimeout(function(){t._willUpdatePath=!1,t._updatePath(t.getSelection())},0))},Ve.focus=function(){return this._root.focus({preventScroll:!0}),_&&this.fireEvent("focus"),this},Ve.blur=function(){return this._root.blur(),_&&this.fireEvent("blur"),this};var it="squire-selection-end";Ve._saveRangeToBookmark=function(e){var t,n=this.createElement("INPUT",{id:"squire-selection-start",type:"hidden"}),o=this.createElement("INPUT",{id:it,type:"hidden"});ce(e,n),e.collapse(!1),ce(e,o),2&n.compareDocumentPosition(o)&&(n.id=it,o.id="squire-selection-start",t=n,n=o,o=t),e.setStartAfter(n),e.setEndBefore(o)},Ve._getRangeAndRemoveBookmark=function(e){var t=this._root,n=t.querySelector("#squire-selection-start"),i=t.querySelector("#"+it);if(n&&i){var r=n.parentNode,a=i.parentNode,s=k.call(r.childNodes,n),d=k.call(a.childNodes,i);r===a&&(d-=1),X(n),X(i),e||(e=this._doc.createRange()),e.setStart(r,s),e.setEnd(a,d),re(r,e),r!==a&&re(a,e),e.collapsed&&(r=e.startContainer).nodeType===o&&((a=r.childNodes[e.startOffset])&&a.nodeType===o||(a=r.childNodes[e.startOffset-1]),a&&a.nodeType===o&&(e.setStart(a,0),e.collapse(!0)))}return e||null},Ve._keyUpDetectChange=function(e){var t=e.keyCode;e.ctrlKey||e.metaKey||e.altKey||!(t<16||t>20)||!(t<33||t>45)||this._docWasChanged()},Ve._docWasChanged=function(){b&&(w=new WeakMap),this._ignoreAllChanges||(E&&this._ignoreChange?this._ignoreChange=!1:(this._isInUndoState&&(this._isInUndoState=!1,this.fireEvent("undoStateChange",{canUndo:!0,canRedo:!1})),this.fireEvent("input")))},Ve._recordUndoState=function(e,t){if(!this._isInUndoState||t){var n,o=this._undoIndex,i=this._undoStack,r=this._config.undo,a=r.documentSizeThreshold,s=r.undoLimit;t||(o+=1),o-1&&2*n.length>a&&s>-1&&o>s&&(i.splice(0,o-s),o=s,this._undoStackLength=s),i[o]=n,this._undoIndex=o,this._undoStackLength+=1,this._isInUndoState=!0}},Ve.saveUndoState=function(e){return e===t&&(e=this.getSelection()),this._recordUndoState(e,this._isInUndoState),this._getRangeAndRemoveBookmark(e),this},Ve.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},Ve.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,i=this.createElement(e,t)),i.appendChild(h))}while(r.nextNode());d.nodeType!==o&&(h.nodeType===o?(d=h,c=h.length):(d=h.parentNode,c=1)),n=this.createRange(s,l,d,c)}return n},Ve._removeFormat=function(e,t,n,i){this._saveRangeToBookmark(n);var r,a=this._doc;n.collapsed&&(y?(r=a.createTextNode(s),this._didAddZWS()):r=a.createTextNode(""),ce(n,r));for(var d=n.commonAncestorContainer;H(d);)d=d.parentNode;var l=n.startContainer,c=n.startOffset,h=n.endContainer,u=n.endOffset,f=[],p=function(e,t){if(!pe(n,e,!1)){var i,r,a=e.nodeType===o;if(pe(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(i=e.firstChild;i;i=r)r=i.nextSibling,p(i,t);else"INPUT"===e.nodeName||a&&!e.data||f.push([t,e])}},g=Array.prototype.filter.call(d.getElementsByTagName(e),function(o){return pe(n,o,!0)&&$(o,e,t)});return i||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,ee(e))}),this._getRangeAndRemoveBookmark(n),r&&n.collapse(!1),re(d,n),n},Ve.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),E||this._docWasChanged(),this):this};var rt={DT:"DD",DD:"DT",LI:"LI",PRE:"PRE"},at=function(e,t,n,o){var i=rt[t.nodeName],r=null,a=ie(n,o,t.parentNode,e._root),s=e._config;return i||(i=s.blockTag,r=s.blockAttributes),$(a,i,r)||(t=te(a.ownerDocument,i,r),a.dir&&(t.dir=a.dir),J(a,t),t.appendChild(ee(a)),a=t),a};Ve.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var o=this._root,i=ve(n,o),r=Ce(n,o);if(i&&r)do{if(e(i)||i===r)break}while(i=G(i,o));return t&&(this.setSelection(n),this._updatePath(n,!0),E||this._docWasChanged()),this},Ve.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._recordUndoState(t,this._isInUndoState);var n,o=this._root;return ye(t,o),me(t,o,o,o),n=he(t,o,o),ce(t,e.call(this,n)),t.endOffset]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))|([\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,}\b)(?:\?[^&?\s]+=[^&?\s]+(?:&[^&?\s]+=[^&?\s]+)*)?/i;var ht=function(e,t,n){var o,i,r,a,s,d,l,c=e.ownerDocument,h=new B(e,4,function(e){return!Q(e,t,"A")}),u=n.linkRegExp,f=n._config.tagAttributes.a;if(u)for(;o=h.nextNode();)for(i=o.data,r=o.parentNode;a=u.exec(i);)d=(s=a.index)+a[0].length,s&&(l=c.createTextNode(i.slice(0,s)),r.insertBefore(l,o)),(l=n.createElement("A",$e({href:a[1]?/^(?:ht|f)tps?:/i.test(a[1])?a[1]:"http://"+a[1]:"mailto:"+a[0]},f,!1))).textContent=i.slice(s,d),r.insertBefore(l,o),o.data=i=i.slice(d)};Ve.insertHTML=function(e,t){var n,o,i,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"),o=e.lastIndexOf("\x3c!--EndFragment--\x3e"),n>-1&&o>-1&&(e=e.slice(n+20,o))),/<\/td>((?!<\/tr>)[\s\S])*$/i.test(e)&&(e=""+e+""),/<\/tr>((?!<\/table>)[\s\S])*$/i.test(e)&&(e=""+e+"
"),(i=this.createElement("DIV")).innerHTML=e,(r=u.createDocumentFragment()).appendChild(ee(i))),this.saveUndoState(h);try{for(a=this._root,s=r,d={fragment:r,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},ht(r,r,this),we(r,l),We(r,a,!1),Fe(r),r.normalize();s=G(s,r);)ne(s,a);t&&this.fireEvent("willPaste",d),d.defaultPrevented||(fe(h,d.fragment,a),E||this._docWasChanged(),h.collapse(!1),this._ensureBottomLine()),this.setSelection(h),this._updatePath(h,!0),t&&this.focus()}catch(e){this.didError(e)}return this};var ut=function(e){return e.split("&").join("&").split("<").join("<").split(">").join(">").split('"').join(""")};Ve.insertPlainText=function(e,t){var n=this.getSelection();if(n.collapsed&&Q(n.startContainer,this._root,"PRE")){var i,r,a=n.startContainer,s=n.startOffset;return a&&a.nodeType===o||(i=this._doc.createTextNode(""),a.insertBefore(i,a.childNodes[s]),a=i,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+'="'+ut(g[d])+'"';for(v+=">",l=0,c=u.length;l")+m;return this.insertHTML(u.join(""),t)};var ft=function(e,t,n){return function(){return this[e](t,n),this.focus()}};Ve.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},Ve.bold=ft("changeFormat",{tag:"B"}),Ve.italic=ft("changeFormat",{tag:"I"}),Ve.underline=ft("changeFormat",{tag:"U"}),Ve.strikethrough=ft("changeFormat",{tag:"S"}),Ve.subscript=ft("changeFormat",{tag:"SUB"},{tag:"SUP"}),Ve.superscript=ft("changeFormat",{tag:"SUP"},{tag:"SUB"}),Ve.removeBold=ft("changeFormat",null,{tag:"B"}),Ve.removeItalic=ft("changeFormat",null,{tag:"I"}),Ve.removeUnderline=ft("changeFormat",null,{tag:"U"}),Ve.removeStrikethrough=ft("changeFormat",null,{tag:"S"}),Ve.removeSubscript=ft("changeFormat",null,{tag:"SUB"}),Ve.removeSuperscript=ft("changeFormat",null,{tag:"SUP"}),Ve.makeLink=function(e,t){var n=this.getSelection();if(n.collapsed){var o=e.indexOf(":")+1;if(o)for(;"/"===e[o];)o+=1;ce(n,this._doc.createTextNode(e.slice(o)))}return t=$e($e({href:e},t,!0),this._config.tagAttributes.a,!1),this.changeFormat({tag:"A",attributes:t},{tag:"A"},n),this.focus()},Ve.removeLink=function(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0),this.focus()},Ve.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()},Ve.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()},Ve.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()},Ve.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()},Ve.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()},Ve.setTextDirection=function(e){return this.forEachBlock(function(t){e?t.dir=e:t.removeAttribute("dir")},!0),this.focus()};var pt=function(e){for(var t,n=this._root,o=this._doc,i=o.createDocumentFragment(),r=q(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(i.slice(0,a))),r.appendChild(s.createElement("BR")),i=i.slice(a+1);o.parentNode.insertBefore(r,o),o.data=i}oe(t,d),J(t,ee(t))}return e};Ve.code=function(){var e=this.getSelection();return e.collapsed||z(e.commonAncestorContainer)?this.modifyBlocks(pt,e):this.changeFormat({tag:"CODE",attributes:this._config.tagAttributes.code},null,e),this.focus()},Ve.removeCode=function(){var e=this.getSelection();return Q(e.commonAncestorContainer,this._root,"PRE")?this.modifyBlocks(gt,e):this.changeFormat(null,{tag:"CODE"},e),this.focus()},Ve.toggleCode=function(){return this.hasFormat("PRE")||this.hasFormat("CODE")?this.removeCode():this.code(),this},Ve.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||(ye(e,t),n=t),n.nodeType===o)return this;this.saveUndoState(e),me(e,n,n,t);for(var i,r,a=n.ownerDocument,s=e.startContainer,d=e.startOffset,l=e.endContainer,c=e.endOffset,h=a.createDocumentFragment(),u=a.createDocumentFragment(),f=ie(l,c,n,t),p=ie(s,d,n,t);p!==f;)i=p.nextSibling,h.appendChild(p),p=i;return function e(t,n,i){var r,a;for(r=n.firstChild;r;r=a){if(a=r.nextSibling,H(r)){if(r.nodeType===o||"BR"===r.nodeName||"IMG"===r.nodeName){i.appendChild(r);continue}}else if(W(r)){i.appendChild(t.createDefaultBlock([e(t,r,t._doc.createDocumentFragment())]));continue}e(t,r,i)}return i}(this,h,u),u.normalize(),p=u.firstChild,i=u.lastChild,r=n.childNodes,p?(n.insertBefore(u,f),d=k.call(r,p),c=k.call(r,i)+1):c=d=k.call(r,f),e.setStart(n,d),e.setEnd(n,c),re(n,e),ge(e),this.setSelection(e),this._updatePath(e,!0),this.focus()},Ve.increaseQuoteLevel=ft("modifyBlocks",function(e){return this.createElement("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e])}),Ve.decreaseQuoteLevel=ft("modifyBlocks",st),Ve.makeUnorderedList=ft("modifyBlocks",function(e){return lt(this,e,"UL"),e}),Ve.makeOrderedList=ft("modifyBlocks",function(e){return lt(this,e,"OL"),e}),Ve.removeList=ft("modifyBlocks",function(e){var t,n,o,i,r,a=e.querySelectorAll("UL, OL"),s=e.querySelectorAll("LI"),d=this._root;for(t=0,n=a.length;tl?i.startOffset-=1:i.startOffset===l&&(i.startContainer=a,i.startOffset=Y(a))),i.endContainer===t&&(i.endOffset>l?i.endOffset-=1:i.endOffset===l&&(i.endContainer=a,i.endOffset=Y(a))),X(r),r.nodeType===o?a.appendData(r.data):c.push(ee(r));else if(r.nodeType===n){for(s=c.length;s--;)r.appendChild(c.pop());e(r,i)}}(e,i),t.setStart(i.startContainer,i.startOffset),t.setEnd(i.endContainer,i.endOffset)}}function ae(e,t,o,i){for(var r,a,s,d=t;(r=d.parentNode)&&r!==i&&r.nodeType===n&&1===r.childNodes.length;)d=r;X(d),s=e.childNodes.length,(a=e.lastChild)&&"BR"===a.nodeName&&(e.removeChild(a),s-=1),e.appendChild(ee(t)),o.setStart(e,s),o.collapse(!0),re(e,o),m&&(a=e.lastChild)&&"BR"===a.nodeName&&e.removeChild(a)}function se(e,t){var n,o,i=e.previousSibling,r=e.firstChild,a=e.ownerDocument,s="LI"===e.nodeName;if(!s||r&&/^[OU]L$/.test(r.nodeName))if(i&&j(i,e)){if(!z(i)){if(!s)return;(o=te(a,"DIV")).appendChild(ee(i)),i.appendChild(o)}X(e),n=!z(e),i.appendChild(ee(e)),n&&oe(i,t),r&&se(r,t)}else s&&(i=te(a,"DIV"),e.insertBefore(i,r),ne(i,t))}var de=function(e,t){for(var o=e.childNodes;t&&e.nodeType===n;)t=(o=(e=o[t-1]).childNodes).length;return e},le=function(e,t){if(e.nodeType===n){var o=e.childNodes;if(t-1,r=e.compareBoundaryPoints(1,o)<1;return!i&&!r}var a=e.compareBoundaryPoints(0,o)<1,s=e.compareBoundaryPoints(2,o)>-1;return a&&s},ge=function(e){for(var t,n=e.startContainer,i=e.startOffset,r=e.endContainer,a=e.endOffset,s=!0;n.nodeType!==o&&(t=n.childNodes[i])&&!F(t);)n=t,i=0;if(a)for(;r.nodeType!==o;){if(!(t=r.childNodes[a-1])||F(t)){if(s&&t&&"BR"===t.nodeName){a-=1,s=!1;continue}break}a=Y(r=t)}else for(;r.nodeType!==o&&(t=r.firstChild)&&!F(t);)r=t;e.collapsed?(e.setStart(r,a),e.setEnd(n,i)):(e.setStart(n,i),e.setEnd(r,a))},me=function(e,t,n,i){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!==i;)r=a.parentNode,s=x.call(r.childNodes,a),a=r;for(;c&&d.nodeType!==o&&d.childNodes[l]&&"BR"===d.childNodes[l].nodeName&&(l+=1,c=!1),d!==n&&d!==i&&l===Y(d);)r=d.parentNode,l=x.call(r.childNodes,d)+1,d=r;e.setStart(a,s),e.setEnd(d,l)},ve=function(e,t){var n,o=e.startContainer;return(n=H(o)?K(o,t):o!==t&&W(o)?o:G(n=de(o,e.startOffset),t))&&pe(e,n,!0)?n:null},_e=function(e,t){var n,o,i=e.endContainer;if(H(i))n=K(i,t);else if(i!==t&&W(i))n=i;else{if(!(n=le(i,e.endOffset))||!V(t,n))for(n=t;o=n.lastChild;)n=o;n=K(n,t)}return n&&pe(e,n,!0)?n:null},Ce=new B(null,4|a,function(e){return e.nodeType===o?k.test(e.data):"IMG"===e.nodeName}),Ne=function(e,t){var n,i=e.startContainer,r=e.startOffset;if(Ce.root=null,i.nodeType===o){if(r)return!1;n=i}else if((n=le(i,r))&&!V(t,n)&&(n=null),!n&&(n=de(i,r)).nodeType===o&&n.length)return!1;return Ce.currentNode=n,Ce.root=ve(e,t),!Ce.previousNode()},Se=function(e,t){var n,i=e.endContainer,r=e.endOffset;if(Ce.root=null,i.nodeType===o){if((n=i.data.length)&&r-1||!p&&x.call(i,"text/plain")>-1&&x.call(i,"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 m=this._doc.body,_=this.getSelection(),C=_.startContainer,N=_.startOffset,S=_.endContainer,y=_.endOffset,T=this.createElement("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});m.appendChild(T),_.selectNodeContents(T),this.setSelection(_),setTimeout(function(){try{g._awaitingPaste=!1;for(var e,t,n="",o=T;T=o;)o=T.nextSibling,X(T),(e=T.firstChild)&&e===T.lastChild&&"DIV"===e.nodeName&&(T=e),n+=T.innerHTML;t=g.createRange(C,N,S,y),g.setSelection(t),n&&g.insertHTML(n,!0)}catch(e){g.didError(e)}},0)},$e=function(e){for(var t=e.dataTransfer.types,n=t.length,o=!1,i=!1;n--;)switch(t[n]){case"text/plain":o=!0;break;case"text/html":i=!0;break;default:return}(i||o)&&this.saveUndoState()};function Qe(e,t,n){var o,i;if(e||(e={}),t)for(o in t)!n&&o in e||(i=t[o],e[o]=i&&i.constructor===Object?Qe(e[o],i,n):i);return e}function Ve(e,t){e.nodeType===i&&(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,T&&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,E?((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",ot),this._awaitingPaste=!1,this.addEventListener(g?"beforecut":"cut",Ke),this.addEventListener("copy",Ge),this.addEventListener("keydown",Ze),this.addEventListener("keyup",Ze),this.addEventListener(g?"beforepaste":"paste",je),this.addEventListener("drop",$e),this.addEventListener(m?"keypress":"keydown",Ee),this._keyHandlers=Object.create(Ae),this.setConfig(t),g&&(r.Text.prototype.splitText=function(e){var t=this.ownerDocument.createTextNode(this.data.slice(e)),n=this.nextSibling,o=this.parentNode,i=this.length-e;return n?o.insertBefore(t,n):o.appendChild(t),i&&this.deleteData(e,i),t}),e.setAttribute("contenteditable","true");try{o.execCommand("enableObjectResizing",!1,"false"),o.execCommand("enableInlineTableEditing",!1,"false")}catch(e){}e.__squire__=this,this.setHTML("")}var Ye=Ve.prototype,Xe=function(e,t,n){var o=n._doc,i=e?DOMPurify.sanitize(e,{ALLOW_UNKNOWN_PROTOCOLS:!0,WHOLE_DOCUMENT:!1,RETURN_DOM:!0,RETURN_DOM_FRAGMENT:!0}):null;return i?o.importNode(i,!0):o.createDocumentFragment()};Ye.setConfig=function(e){return(e=Qe({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:R,undo:{documentSizeThreshold:-1,undoLimit:-1},isInsertedHTMLSanitized:!0,isSetHTMLSanitized:!0,sanitizeToDOMFragment:"undefined"!=typeof DOMPurify&&DOMPurify.isSupported?Xe:null,willCutCopy:null},e,!0)).blockTag=e.blockTag.toUpperCase(),this._config=e,this},Ye.createElement=function(e,t,n){return te(this._doc,e,t,n)},Ye.createDefaultBlock=function(e){var t=this._config;return ne(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,o,i,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),o=(r=r.slice()).length;o--;){i=r[o];try{i.handleEvent?i.handleEvent(t):i.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],o=this._root;return t?(n||(n=this._events[e]=[],Je[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)},Ye.removeEventListener=function(e,t){var n,o=this._events[e],i=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],Je[e]||("selectionchange"===e&&(i=this._doc),i.removeEventListener(e,this,!0)))}return this},Ye.createRange=function(e,t,n,o){if(e instanceof this._win.Range)return e.cloneRange();var i=this._doc.createRange();return i.setStart(e,t),n?i.setEnd(n,o):i.setEnd(e,t),i},Ye.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")).textContent=s,ce(e,t),o=t.getBoundingClientRect(),(n=t.parentNode).removeChild(t),re(n,e)),o},Ye._moveCursorTo=function(e){var t=this._root,n=this.createRange(t,e?0:t.childNodes.length);return ge(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 ot(){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{h&&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,o,i=et(this),r=this._root;return this._isFocused&&i&&i.rangeCount&&(t=(e=i.getRangeAt(0).cloneRange()).startContainer,n=e.endContainer,t&&F(t)&&e.setStartBefore(t),n&&F(n)&&e.setEndBefore(n)),e&&V(r,e.commonAncestorContainer)?this._lastSelection=e:V((o=(e=this._lastSelection).commonAncestorContainer).ownerDocument,o)||(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 pe(e,t,!0)}),i=e.startContainer,r=e.endContainer,s=n.currentNode=i,d="",l=!1;for(n.filter(s)||(s=n.nextNode());s;)s.nodeType===o?(t=s.data)&&/\S/.test(t)&&(s===r&&(t=t.slice(0,e.endOffset)),s===i&&(t=t.slice(e.startOffset)),d+=t,l=!0):("BR"===s.nodeName||l&&!H(s))&&(d+="\n",l=!1),s=n.nextNode();return d},Ye.getPath=function(){return this._path};var it=function(e,t){for(var n,o,i,r=new B(e,4);o=r.nextNode();)for(;(i=o.data.indexOf(s))>-1&&(!t||o.parentNode!==t);){if(1===o.length){do{(n=o.parentNode).removeChild(o),o=n,r.currentNode=n}while(H(o)&&!Y(o));break}o.deleteData(i,1)}};Ye._didAddZWS=function(){this._hasZWS=!0},Ye._removeZWS=function(){this._hasZWS&&(it(this._root),this._hasZWS=!1)},Ye._updatePath=function(e,t){if(e){var o,i=e.startContainer,r=e.endContainer;(t||i!==this._lastAnchorNode||r!==this._lastFocusNode)&&(this._lastAnchorNode=i,this._lastFocusNode=r,o=i&&r?i===r?function e(t,o,i){var r,a,s,d,l,c="";return t&&t!==o&&(c=e(t.parentNode,o,i),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=i.classNames,x.call(s,l.highlight)>-1&&(c+="[backgroundColor="+t.style.backgroundColor.replace(/ /g,"")+"]"),x.call(s,l.colour)>-1&&(c+="[color="+t.style.color.replace(/ /g,"")+"]"),x.call(s,l.fontFamily)>-1&&(c+="[fontFamily="+t.style.fontFamily.replace(/ /g,"")+"]"),x.call(s,l.fontSize)>-1&&(c+="[fontSize="+t.style.fontSize+"]")))),c}(r,this._root,this._config):"(selection)":"",this._path!==o&&(this._path=o,this.fireEvent("pathChange",{path:o}))),this.fireEvent(e.collapsed?"cursor":"select",{range:e})}},Ye._updatePathOnEvent=function(e){var t=this;t._isFocused&&!t._willUpdatePath&&(t._willUpdatePath=!0,setTimeout(function(){t._willUpdatePath=!1,t._updatePath(t.getSelection())},0))},Ye.focus=function(){return this._root.focus({preventScroll:!0}),C&&this.fireEvent("focus"),this},Ye.blur=function(){return this._root.blur(),C&&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"}),o=this.createElement("INPUT",{id:rt,type:"hidden"});ce(e,n),e.collapse(!1),ce(e,o),2&n.compareDocumentPosition(o)&&(n.id=rt,o.id="squire-selection-start",t=n,n=o,o=t),e.setStartAfter(n),e.setEndBefore(o)},Ye._getRangeAndRemoveBookmark=function(e){var t=this._root,n=t.querySelector("#squire-selection-start"),i=t.querySelector("#"+rt);if(n&&i){var r=n.parentNode,a=i.parentNode,s=x.call(r.childNodes,n),d=x.call(a.childNodes,i);r===a&&(d-=1),X(n),X(i),e||(e=this._doc.createRange()),e.setStart(r,s),e.setEnd(a,d),re(r,e),r!==a&&re(a,e),e.collapsed&&(r=e.startContainer).nodeType===o&&((a=r.childNodes[e.startOffset])&&a.nodeType===o||(a=r.childNodes[e.startOffset-1]),a&&a.nodeType===o&&(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(){b&&(w=new WeakMap),this._ignoreAllChanges||(E&&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,o=this._undoIndex,i=this._undoStack,r=this._config.undo,a=r.documentSizeThreshold,s=r.undoLimit;t||(o+=1),o-1&&2*n.length>a&&s>-1&&o>s&&(i.splice(0,o-s),o=s,this._undoStackLength=s),i[o]=n,this._undoIndex=o,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,i=this.createElement(e,t)),i.appendChild(h))}while(r.nextNode());d.nodeType!==o&&(h.nodeType===o?(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,i){this._saveRangeToBookmark(n);var r,a=this._doc;n.collapsed&&(y?(r=a.createTextNode(s),this._didAddZWS()):r=a.createTextNode(""),ce(n,r));for(var d=n.commonAncestorContainer;H(d);)d=d.parentNode;var l=n.startContainer,c=n.startOffset,h=n.endContainer,u=n.endOffset,f=[],p=function(e,t){if(!pe(n,e,!1)){var i,r,a=e.nodeType===o;if(pe(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(i=e.firstChild;i;i=r)r=i.nextSibling,p(i,t);else"INPUT"===e.nodeName||a&&!e.data||f.push([t,e])}},g=Array.prototype.filter.call(d.getElementsByTagName(e),function(o){return pe(n,o,!0)&&$(o,e,t)});return i||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,ee(e))}),this._getRangeAndRemoveBookmark(n),r&&n.collapse(!1),re(d,n),n},Ye.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),E||this._docWasChanged(),this):this};var at={DT:"DD",DD:"DT",LI:"LI",PRE:"PRE"},st=function(e,t,n,o){var i=at[t.nodeName],r=null,a=ie(n,o,t.parentNode,e._root),s=e._config;return i||(i=s.blockTag,r=s.blockAttributes),$(a,i,r)||(t=te(a.ownerDocument,i,r),a.dir&&(t.dir=a.dir),J(a,t),t.appendChild(ee(a)),a=t),a};Ye.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var o=this._root,i=ve(n,o),r=_e(n,o);if(i&&r)do{if(e(i)||i===r)break}while(i=G(i,o));return t&&(this.setSelection(n),this._updatePath(n,!0),E||this._docWasChanged()),this},Ye.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._recordUndoState(t,this._isInUndoState);var n,o=this._root;return ye(t,o),me(t,o,o,o),n=he(t,o,o),ce(t,e.call(this,n)),t.endOffset]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))|([\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,}\b)(?:\?[^&?\s]+=[^&?\s]+(?:&[^&?\s]+=[^&?\s]+)*)?/i;var ut=function(e,t,n){var o,i,r,a,s,d,l,c=e.ownerDocument,h=new B(e,4,function(e){return!Q(e,t,"A")}),u=n.linkRegExp,f=n._config.tagAttributes.a;if(u)for(;o=h.nextNode();)for(i=o.data,r=o.parentNode;a=u.exec(i);)d=(s=a.index)+a[0].length,s&&(l=c.createTextNode(i.slice(0,s)),r.insertBefore(l,o)),(l=n.createElement("A",Qe({href:a[1]?/^(?:ht|f)tps?:/i.test(a[1])?a[1]:"http://"+a[1]:"mailto:"+a[0]},f,!1))).textContent=i.slice(s,d),r.insertBefore(l,o),o.data=i=i.slice(d)};Ye.insertHTML=function(e,t){var n,o,i,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"),o=e.lastIndexOf("\x3c!--EndFragment--\x3e"),n>-1&&o>-1&&(e=e.slice(n+20,o))),/<\/td>((?!<\/tr>)[\s\S])*$/i.test(e)&&(e=""+e+""),/<\/tr>((?!<\/table>)[\s\S])*$/i.test(e)&&(e=""+e+"
"),(i=this.createElement("DIV")).innerHTML=e,(r=u.createDocumentFragment()).appendChild(ee(i))),this.saveUndoState(h);try{for(a=this._root,s=r,d={fragment:r,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},ut(r,r,this),Fe(r,l),ze(r,a,!1),Me(r),r.normalize();s=G(s,r);)ne(s,a);t&&this.fireEvent("willPaste",d),d.defaultPrevented||(fe(h,d.fragment,a),E||this._docWasChanged(),h.collapse(!1),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&&Q(n.startContainer,this._root,"PRE")){var i,r,a=n.startContainer,s=n.startOffset;return a&&a.nodeType===o||(i=this._doc.createTextNode(""),a.insertBefore(i,a.childNodes[s]),a=i,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;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 o=e.indexOf(":")+1;if(o)for(;"/"===e[o];)o+=1;ce(n,this._doc.createTextNode(e.slice(o)))}return t=Qe(Qe({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,o=this._doc,i=o.createDocumentFragment(),r=q(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(i.slice(0,a))),r.appendChild(s.createElement("BR")),i=i.slice(a+1);o.parentNode.insertBefore(r,o),o.data=i}oe(t,d),J(t,ee(t))}return e};Ye.code=function(){var e=this.getSelection();return e.collapsed||z(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 Q(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||(ye(e,t),n=t),n.nodeType===o)return this;this.saveUndoState(e),me(e,n,n,t);for(var i,r,a=n.ownerDocument,s=e.startContainer,d=e.startOffset,l=e.endContainer,c=e.endOffset,h=a.createDocumentFragment(),u=a.createDocumentFragment(),f=ie(l,c,n,t),p=ie(s,d,n,t);p!==f;)i=p.nextSibling,h.appendChild(p),p=i;return function e(t,n,i){var r,a;for(r=n.firstChild;r;r=a){if(a=r.nextSibling,H(r)){if(r.nodeType===o||"BR"===r.nodeName||"IMG"===r.nodeName){i.appendChild(r);continue}}else if(W(r)){i.appendChild(t.createDefaultBlock([e(t,r,t._doc.createDocumentFragment())]));continue}e(t,r,i)}return i}(this,h,u),u.normalize(),p=u.firstChild,i=u.lastChild,r=n.childNodes,p?(n.insertBefore(u,f),d=x.call(r,p),c=x.call(r,i)+1):c=d=x.call(r,f),e.setStart(n,d),e.setEnd(n,c),re(n,e),ge(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,o,i,r,a=e.querySelectorAll("UL, OL"),s=e.querySelectorAll("LI"),d=this._root;for(t=0,n=a.length;t. - if ( !block || shiftKey || /^T[HD]$/.test( block.nodeName ) ) { - // If inside an
, move focus out - parent = getNearest( range.endContainer, root, 'A' ); - if ( parent ) { - parent = parent.parentNode; - moveRangeBoundariesUpTree( range, parent, parent, root ); - range.collapse( false ); - } - insertNodeInRange( range, self.createElement( 'BR' ) ); - range.collapse( false ); - self.setSelection( range ); - self._updatePath( range, true ); - return; - } - - // If in a list, we'll split the LI instead. - if ( parent = getNearest( block, root, 'LI' ) ) { - block = parent; - } - - if ( isEmptyBlock( block ) ) { - // Break list - if ( getNearest( block, root, 'UL' ) || - getNearest( block, root, 'OL' ) ) { - return self.decreaseListLevel( range ); - } - // Break blockquote - else if ( getNearest( block, root, 'BLOCKQUOTE' ) ) { - return self.modifyBlocks( removeBlockQuote, range ); + range.setStart( node, offset + 1 ); } } - - // Otherwise, split at cursor point. - nodeAfterSplit = splitBlock( self, block, - range.startContainer, range.startOffset ); - - // Clean up any empty inlines if we hit enter at the beginning of the - // block - removeZWS( block ); - removeEmptyInlines( block ); - fixCursor( block, root ); - - // Focus cursor - // If there's a / etc. at the beginning of the split - // make sure we focus inside it. - while ( nodeAfterSplit.nodeType === ELEMENT_NODE ) { - var child = nodeAfterSplit.firstChild, - next; - - // Don't continue links over a block break; unlikely to be the - // desired outcome. - if ( nodeAfterSplit.nodeName === 'A' && - ( !nodeAfterSplit.textContent || - nodeAfterSplit.textContent === ZWS ) ) { - child = self._doc.createTextNode( '' ); - replaceWith( nodeAfterSplit, child ); - nodeAfterSplit = child; - break; - } - - while ( child && child.nodeType === TEXT_NODE && !child.data ) { - next = child.nextSibling; - if ( !next || next.nodeName === 'BR' ) { - break; - } - detach( child ); - child = next; - } - - // 'BR's essentially don't count; they're a browser hack. - // If you try to select the contents of a 'BR', FF will not let - // you type anything! - if ( !child || child.nodeName === 'BR' || - ( child.nodeType === TEXT_NODE && !isPresto ) ) { - break; - } - nodeAfterSplit = child; - } - range = self.createRange( nodeAfterSplit, 0 ); + range.collapse( true ); self.setSelection( range ); self._updatePath( range, true ); + self._docWasChanged(); + return; + } + + // If this is a malformed bit of document or in a table; + // just play it safe and insert a
. + if ( !block || shiftKey || /^T[HD]$/.test( block.nodeName ) ) { + // If inside an
, move focus out + parent = getNearest( range.endContainer, root, 'A' ); + if ( parent ) { + parent = parent.parentNode; + moveRangeBoundariesUpTree( range, parent, parent, root ); + range.collapse( false ); + } + insertNodeInRange( range, self.createElement( 'BR' ) ); + range.collapse( false ); + self.setSelection( range ); + self._updatePath( range, true ); + return; + } + + // If in a list, we'll split the LI instead. + if ( parent = getNearest( block, root, 'LI' ) ) { + block = parent; + } + + if ( isEmptyBlock( block ) ) { + // Break list + if ( getNearest( block, root, 'UL' ) || + getNearest( block, root, 'OL' ) ) { + return self.decreaseListLevel( range ); + } + // Break blockquote + else if ( getNearest( block, root, 'BLOCKQUOTE' ) ) { + return self.modifyBlocks( removeBlockQuote, range ); + } + } + + // Otherwise, split at cursor point. + nodeAfterSplit = splitBlock( self, block, + range.startContainer, range.startOffset ); + + // Clean up any empty inlines if we hit enter at the beginning of the + // block + removeZWS( block ); + removeEmptyInlines( block ); + fixCursor( block, root ); + + // Focus cursor + // If there's a / etc. at the beginning of the split + // make sure we focus inside it. + while ( nodeAfterSplit.nodeType === ELEMENT_NODE ) { + var child = nodeAfterSplit.firstChild, + next; + + // Don't continue links over a block break; unlikely to be the + // desired outcome. + if ( nodeAfterSplit.nodeName === 'A' && + ( !nodeAfterSplit.textContent || + nodeAfterSplit.textContent === ZWS ) ) { + child = self._doc.createTextNode( '' ); + replaceWith( nodeAfterSplit, child ); + nodeAfterSplit = child; + break; + } + + while ( child && child.nodeType === TEXT_NODE && !child.data ) { + next = child.nextSibling; + if ( !next || next.nodeName === 'BR' ) { + break; + } + detach( child ); + child = next; + } + + // 'BR's essentially don't count; they're a browser hack. + // If you try to select the contents of a 'BR', FF will not let + // you type anything! + if ( !child || child.nodeName === 'BR' || + ( child.nodeType === TEXT_NODE && !isPresto ) ) { + break; + } + nodeAfterSplit = child; + } + range = self.createRange( nodeAfterSplit, 0 ); + self.setSelection( range ); + self._updatePath( range, true ); +}; + +var keyHandlers = { + // This song and dance is to force iOS to do enable the shift key + // automatically on enter. When you do the DOM split manipulation yourself, + // WebKit doesn't reset the IME state and so presents auto-complete options + // as though you were continuing to type on the previous line, and doesn't + // auto-enable the shift key. The old trick of blurring and focussing + // again no longer works in iOS 13, and I tried various execCommand options + // but they didn't seem to do anything. The only solution I've found is to + // let iOS handle the enter key, then after it's done that reset the HTML + // to what it was before and handle it properly in Squire; the IME state of + // course doesn't reset so you end up in the correct state! + enter: isIOS ? function ( self, event, range ) { + self._saveRangeToBookmark( range ); + var html = self._getHTML(); + var restoreAndDoEnter = function () { + self.removeEventListener( 'keyup', restoreAndDoEnter ); + self._setHTML( html ); + range = self._getRangeAndRemoveBookmark(); + // Ignore the shift key on iOS, as this is for auto-capitalisation. + handleEnter( self, false, range ); + }; + self.addEventListener( 'keyup', restoreAndDoEnter ); + } : function ( self, event, range ) { + event.preventDefault(); + handleEnter( self, event.shiftKey, range ); }, 'shift-enter': function ( self, event, range ) {