From bb3cd05c64f42795ed8ca6216b2e961f991a10fa Mon Sep 17 00:00:00 2001 From: Nicholas Wylie Date: Wed, 3 Jun 2020 14:53:05 +1000 Subject: [PATCH] Move cursor outside of when inserting HTML When links are pasted into the editor the cursor ends up at the end of the text node inside the parent element. Any text entered is then appended to the end of the link text. Chrome automatically moves the cursor after the end of elements when additional text is inserted, so this change enforces the same behaviour in other browsers. Resolves LP 55607264 https://app.liquidplanner.com/space/14822/projects/show/55607264 --- build/squire-raw.js | 34 +++++++++++++++++++++++++--------- build/squire.js | 3 +-- source/Editor.js | 6 ++++++ source/KeyHandlers.js | 13 ++++--------- source/Range.js | 21 ++++++++++++++++++--- 5 files changed, 54 insertions(+), 23 deletions(-) diff --git a/build/squire-raw.js b/build/squire-raw.js index 6087ce6..9a97beb 100644 --- a/build/squire-raw.js +++ b/build/squire-raw.js @@ -1096,6 +1096,9 @@ var moveRangeBoundariesUpTree = function ( range, startMax, endMax, root ) { } while ( true ) { + if ( endContainer === endMax || endContainer === root ) { + break; + } if ( maySkipBR && endContainer.nodeType !== TEXT_NODE && endContainer.childNodes[ endOffset ] && @@ -1103,9 +1106,7 @@ var moveRangeBoundariesUpTree = function ( range, startMax, endMax, root ) { endOffset += 1; maySkipBR = false; } - if ( endContainer === endMax || - endContainer === root || - endOffset !== getLength( endContainer ) ) { + if ( endOffset !== getLength( endContainer ) ) { break; } parent = endContainer.parentNode; @@ -1117,6 +1118,20 @@ var moveRangeBoundariesUpTree = function ( range, startMax, endMax, root ) { range.setEnd( endContainer, endOffset ); }; +var moveRangeBoundaryOutOf = function ( range, nodeName, root ) { + var parent = getNearest( range.endContainer, root, 'A' ); + if ( parent ) { + var clone = range.cloneRange(); + parent = parent.parentNode; + moveRangeBoundariesUpTree( clone, parent, parent, root ); + if ( clone.endContainer === parent ) { + range.setStart( clone.endContainer, clone.endOffset ); + range.setEnd( clone.endContainer, clone.endOffset ); + } + } + return range; +}; + // Returns the first block at least partially contained by the range, // or null if no block is contained by the range. var getStartBlockOfRange = function ( range, root ) { @@ -1468,12 +1483,7 @@ var handleEnter = function ( self, shiftKey, range ) { // 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 ); - } + moveRangeBoundaryOutOf( range, 'A', root ); insertNodeInRange( range, self.createElement( 'BR' ) ); range.collapse( false ); self.setSelection( range ); @@ -4435,6 +4445,12 @@ proto.insertHTML = function ( html, isPaste ) { this._docWasChanged(); } range.collapse( false ); + + // After inserting the fragment, check whether the cursor is inside + // an element and if so if there is an equivalent cursor + // position after the element. If there is, move it there. + moveRangeBoundaryOutOf( range, 'A', root ); + this._ensureBottomLine(); } diff --git a/build/squire.js b/build/squire.js index 37967b4..be69060 100644 --- a/build/squire.js +++ b/build/squire.js @@ -1,2 +1 @@ -!function(e,t){"use strict";function n(e,t,n){this.root=this.currentNode=e,this.nodeType=t,this.filter=n||ae}function i(e,t){for(var n=e.length;n--;)if(!t(e[n]))return!1;return!0}function o(e){return e.nodeType===F&&!!de[e.nodeName]}function r(e){switch(e.nodeType){case w:return ce;case F:case H:if(ne&&fe.has(e))return fe.get(e);break;default:return le}var t;return t=i(e.childNodes,a)?se.test(e.nodeName)?ce:he:ue,ne&&fe.set(e,t),t}function a(e){return r(e)===ce}function s(e){return r(e)===he}function d(e){return r(e)===ue}function l(e,t){var i=new n(t,W,s);return i.currentNode=e,i}function c(e,t){return e=l(e,t).previousNode(),e!==t?e:null}function h(e,t){return e=l(e,t).nextNode(),e!==t?e:null}function u(e){return!e.textContent&&!e.querySelector("IMG")}function f(e,t){return!o(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 p(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(p(e,n,i))return e;e=e.parentNode}return null}function m(e,t){for(;t;){if(t===e)return!0;t=t.parentNode}return!1}function v(e,t,n){var i,o,r,a,s,d="";return e&&e!==t&&(d=v(e.parentNode,t,n),e.nodeType===F&&(d+=(d?">":"")+e.nodeName,(i=e.id)&&(d+="#"+i),(o=e.className.trim())&&(r=o.split(/\s\s*/),r.sort(),d+=".",d+=r.join(".")),(a=e.dir)&&(d+="[dir="+a+"]"),r&&(s=n.classNames,oe.call(r,s.highlight)>-1&&(d+="[backgroundColor="+e.style.backgroundColor.replace(/ /g,"")+"]"),oe.call(r,s.colour)>-1&&(d+="[color="+e.style.color.replace(/ /g,"")+"]"),oe.call(r,s.fontFamily)>-1&&(d+="[fontFamily="+e.style.fontFamily.replace(/ /g,"")+"]"),oe.call(r,s.fontSize)>-1&&(d+="[fontSize="+e.style.fontSize+"]")))),d}function _(e){var t=e.nodeType;return t===F||t===H?e.childNodes.length:e.length||0}function C(e){var t=e.parentNode;return t&&t.removeChild(e),e}function N(e,t){var n=e.parentNode;n&&n.replaceChild(t,e)}function S(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;ss?t.startOffset-=1:t.startOffset===s&&(t.startContainer=i,t.startOffset=_(i))),t.endContainer===e&&(t.endOffset>s?t.endOffset-=1:t.endOffset===s&&(t.endContainer=i,t.endOffset=_(i))),C(n),n.nodeType===w?i.appendData(n.data):d.push(S(n));else if(n.nodeType===F){for(o=d.length;o--;)n.appendChild(d.pop());L(n,t)}}function k(e,t){if(e.nodeType===w&&(e=e.parentNode),e.nodeType===F){var n={startContainer:t.startContainer,startOffset:t.startOffset,endContainer:t.endContainer,endOffset:t.endOffset};L(e,n),t.setStart(n.startContainer,n.startOffset),t.setEnd(n.endContainer,n.endOffset)}}function x(e,t,n,i){for(var o,r,a,s=t;(o=s.parentNode)&&o!==i&&o.nodeType===F&&1===o.childNodes.length;)s=o;C(s),a=e.childNodes.length,r=e.lastChild,r&&"BR"===r.nodeName&&(e.removeChild(r),a-=1),e.appendChild(S(t)),n.setStart(e,a),n.collapse(!0),k(e,n)}function A(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&&f(o,e)){if(!d(o)){if(!s)return;i=y(a,"DIV"),i.appendChild(S(o)),o.appendChild(i)}C(e),n=!d(e),o.appendChild(S(e)),n&&E(o,t),r&&A(r,t)}else s&&(o=y(a,"DIV"),e.insertBefore(o,r),T(o,t))}function O(e){this.isShiftDown=e.shiftKey}function B(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?B(e[i],o,n):o);return e}function R(e,t){e.nodeType===M&&(e=e.body);var n,i=e.ownerDocument,o=i.defaultView;this._win=o,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,te?(n=new MutationObserver(this._docWasChanged.bind(this)),n.observe(e,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._mutation=n):this.addEventListener("keyup",this._keyUpDetectChange),this._restoreSelection=!1,this.addEventListener("blur",D),this.addEventListener("mousedown",P),this.addEventListener("touchstart",P),this.addEventListener("focus",U),this._awaitingPaste=!1,this.addEventListener("cut",Xe),this.addEventListener("copy",Je),this.addEventListener("keydown",O),this.addEventListener("keyup",O),this.addEventListener("paste",et),this.addEventListener("drop",tt),this.addEventListener("keydown",Oe),this._keyHandlers=Object.create(Ie),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("")}function D(){this._restoreSelection=!0}function P(){this._restoreSelection=!1}function U(){this._restoreSelection&&this.setSelection(this._lastSelection)}function I(e,t,n){var i,o;for(i=t.firstChild;i;i=o){if(o=i.nextSibling,a(i)){if(i.nodeType===w||"BR"===i.nodeName||"IMG"===i.nodeName){n.appendChild(i);continue}}else if(s(i)){n.appendChild(e.createDefaultBlock([I(e,i,e._doc.createDocumentFragment())]));continue}I(e,i,n)}return n}var F=1,w=3,M=9,H=11,W=1,z="​",q=e.defaultView,K=navigator.userAgent,G=/Android/.test(K),Z=/Mac OS X/.test(K),Q=/Windows NT/.test(K),$=/iP(?:ad|hone|od)/.test(K)||Z&&!!navigator.maxTouchPoints,j=/Gecko\//.test(K),V=/Edge\//.test(K),Y=!V&&/WebKit\//.test(K),X=/Trident\/[4567]\./.test(K),J=Z?"meta-":"ctrl-",ee=Y,te="undefined"!=typeof MutationObserver,ne="undefined"!=typeof WeakMap,ie=/[^ \t\r\n]/,oe=Array.prototype.indexOf,re={1:1,2:2,3:4,8:128,9:256,11:1024},ae=function(){return!0};n.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(re[e.nodeType]&i&&o(e))return this.currentNode=e,e;t=e}},n.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(re[e.nodeType]&i&&o(e))return this.currentNode=e,e;t=e}},n.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(re[e.nodeType]&i&&o(e))return this.currentNode=e,e;t=e}};var se=/^(?:#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)$/,de={BR:1,HR:1,IFRAME:1,IMG:1,INPUT:1},le=0,ce=1,he=2,ue=3,fe=ne?new WeakMap:null,pe=function(e,t){for(var n=e.childNodes;t&&e.nodeType===F;)e=n[t-1],n=e.childNodes,t=n.length;return e},ge=function(e,t){if(e.nodeType===F){var n=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},Se=function(e){for(var t,n=e.startContainer,i=e.startOffset,r=e.endContainer,a=e.endOffset,s=!0;n.nodeType!==w&&(t=n.childNodes[i])&&!o(t);)n=t,i=0;if(a)for(;r.nodeType!==w;){if(!(t=r.childNodes[a-1])||o(t)){if(s&&t&&"BR"===t.nodeName){a-=1,s=!1;continue}break}r=t,a=_(r)}else for(;r.nodeType!==w&&(t=r.firstChild)&&!o(t);)r=t;e.collapsed?(e.setStart(r,a),e.setEnd(n,i)):(e.setStart(n,i),e.setEnd(r,a))},ye=function(e,t,n,i){var o,r=e.startContainer,a=e.startOffset,s=e.endContainer,d=e.endOffset,l=!0;for(t||(t=e.commonAncestorContainer),n||(n=t);!a&&r!==t&&r!==i;)o=r.parentNode,a=oe.call(o.childNodes,r),r=o;for(;;){if(l&&s.nodeType!==w&&s.childNodes[d]&&"BR"===s.childNodes[d].nodeName&&(d+=1,l=!1),s===n||s===i||d!==_(s))break;o=s.parentNode,d=oe.call(o.childNodes,s)+1,s=o}e.setStart(r,a),e.setEnd(s,d)},Te=function(e,t){var n,i=e.startContainer;return a(i)?n=c(i,t):i!==t&&s(i)?n=i:(n=pe(i,e.startOffset),n=h(n,t)),n&&Ne(e,n,!0)?n:null},Ee=function(e,t){var n,i,o=e.endContainer;if(a(o))n=c(o,t);else if(o!==t&&s(o))n=o;else{if(!(n=ge(o,e.endOffset))||!m(t,n))for(n=t;i=n.lastChild;)n=i;n=c(n,t)}return n&&Ne(e,n,!0)?n:null},be=new n(null,4|W,function(e){return e.nodeType===w?ie.test(e.data):"IMG"===e.nodeName}),Le=function(e,t){var n,i=e.startContainer,o=e.startOffset;if(be.root=null,i.nodeType===w){if(o)return!1;n=i}else if(n=ge(i,o),n&&!m(t,n)&&(n=null),!n&&(n=pe(i,o),n.nodeType===w&&n.length))return!1;return be.currentNode=n,be.root=Te(e,t),!be.previousNode()},ke=function(e,t){var n,i=e.endContainer,o=e.endOffset;if(be.root=null,i.nodeType===w){if((n=i.data.length)&&o)BLOCKQUOTE/.test(o)||!/(?:^|>)[OU]L/.test(o)?n[e]():n[t]()}};Ie[J+"b"]=Re("B"),Ie[J+"i"]=Re("I"),Ie[J+"u"]=Re("U"),Ie[J+"shift-7"]=Re("S"),Ie[J+"shift-5"]=Re("SUB",{tag:"SUP"}),Ie[J+"shift-6"]=Re("SUP",{tag:"SUB"}),Ie[J+"shift-8"]=Be("makeUnorderedList"),Ie[J+"shift-9"]=Be("makeOrderedList"),Ie[J+"["]=Fe("decreaseQuoteLevel","decreaseListLevel"),Ie[J+"]"]=Fe("increaseQuoteLevel","increaseListLevel"),Ie[J+"d"]=Be("toggleCode"),Ie[J+"y"]=Be("redo"),Ie[J+"z"]=Be("undo"),Ie[J+"shift-z"]=Be("redo");var we={1:10,2:13,3:16,4:18,5:24,6:32,7:48},Me={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:ie,replace:function(e,t,n){return y(e,"SPAN",{class:t.fontFamily,style:"font-family:"+n})}},fontSize:{regexp:ie,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")}}},He=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||!j&&oe.call(o,"text/plain")>-1&&oe.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 g=this._doc.body,m=this.getSelection(),v=m.startContainer,_=m.startOffset,N=m.endContainer,S=m.endOffset,y=this.createElement("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});g.appendChild(y),m.selectNodeContents(y),this.setSelection(m),setTimeout(function(){try{p._awaitingPaste=!1;for(var e,t,n="",i=y;y=i;)i=y.nextSibling,C(y),e=y.firstChild,e&&e===y.lastChild&&"DIV"===e.nodeName&&(y=e),n+=y.innerHTML;t=p.createRange(v,_,N,S),p.setSelection(t),n&&p.insertHTML(n,!0)}catch(e){p.didError(e)}},0)},tt=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()},nt=R.prototype,it=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()};nt.setConfig=function(e){return e=B({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:de,undo:{documentSizeThreshold:-1,undoLimit:-1},isInsertedHTMLSanitized:!0,isSetHTMLSanitized:!0,sanitizeToDOMFragment:"undefined"!=typeof DOMPurify&&DOMPurify.isSupported?it:null,willCutCopy:null,addLinks:!0},e,!0),e.blockTag=e.blockTag.toUpperCase(),this._config=e,this},nt.createElement=function(e,t,n){return y(this._doc,e,t,n)},nt.createDefaultBlock=function(e){var t=this._config;return T(this.createElement(t.blockTag,t.blockAttributes,e),this._root)},nt.didError=function(e){console.log(e)},nt.getDocument=function(){return this._doc},nt.getRoot=function(){return this._root},nt.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 ot={pathChange:1,select:1,input:1,undoStateChange:1};nt.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),r=r.slice(),i=r.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},nt.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},nt.handleEvent=function(e){this.fireEvent(e.type,e)},nt.addEventListener=function(e,t){var n=this._events[e],i=this._root;return t?(n||(n=this._events[e]=[],ot[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)},nt.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],ot[e]||("selectionchange"===e&&(o=this._doc),o.removeEventListener(e,this,!0)))}return this},nt.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},nt.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"),t.textContent=z,me(e,t),i=t.getBoundingClientRect(),n=t.parentNode,n.removeChild(t),k(n,e)),i},nt._moveCursorTo=function(e){var t=this._root,n=this.createRange(t,e?0:t.childNodes.length);return Se(n),this.setSelection(n),this},nt.moveCursorToStart=function(){return this._moveCursorTo(!0)},nt.moveCursorToEnd=function(){return this._moveCursorTo(!1)};var rt=function(e){return e._win.getSelection()||null};nt.setSelection=function(e){if(e)if(this._lastSelection=e,this._isFocused)if(G&&!this._restoreSelection)D.call(this),this.blur(),this.focus();else{$&&this._win.focus();var t=rt(this);t&&(t.removeAllRanges(),t.addRange(e))}else D.call(this);return this},nt.getSelection=function(){var e,t,n,i,r=rt(this),a=this._root;return this._isFocused&&r&&r.rangeCount&&(e=r.getRangeAt(0).cloneRange(),t=e.startContainer,n=e.endContainer,t&&o(t)&&e.setStartBefore(t),n&&o(n)&&e.setEndBefore(n)),e&&m(a,e.commonAncestorContainer)?this._lastSelection=e:(e=this._lastSelection,i=e.commonAncestorContainer,m(i.ownerDocument,i)||(e=null)),e||(e=this.createRange(a.firstChild,0)),e},nt.getSelectedText=function(){var e=this.getSelection();if(!e||e.collapsed)return"";var t,i=new n(e.commonAncestorContainer,4|W,function(t){return Ne(e,t,!0)}),o=e.startContainer,r=e.endContainer,s=i.currentNode=o,d="",l=!1;for(i.filter(s)||(s=i.nextNode());s;)s.nodeType===w?(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&&!a(s))&&(d+="\n",l=!1),s=i.nextNode();return d},nt.getPath=function(){return this._path};var at=function(e,t){for(var i,o,r,s=new n(e,4);o=s.nextNode();)for(;(r=o.data.indexOf(z))>-1&&(!t||o.parentNode!==t);){if(1===o.length){do{i=o.parentNode,i.removeChild(o),o=i,s.currentNode=i}while(a(o)&&!_(o));break}o.deleteData(r,1)}};nt._didAddZWS=function(){this._hasZWS=!0},nt._removeZWS=function(){this._hasZWS&&(at(this._root),this._hasZWS=!1)},nt._updatePath=function(e,t){if(e){var n,i=e.startContainer,o=e.endContainer;(t||i!==this._lastAnchorNode||o!==this._lastFocusNode)&&(this._lastAnchorNode=i,this._lastFocusNode=o,n=i&&o?i===o?v(o,this._root,this._config):"(selection)":"",this._path!==n&&(this._path=n,this.fireEvent("pathChange",{path:n}))),this.fireEvent(e.collapsed?"cursor":"select",{range:e})}},nt._updatePathOnEvent=function(e){var t=this;t._isFocused&&!t._willUpdatePath&&(t._willUpdatePath=!0,setTimeout(function(){t._willUpdatePath=!1,t._updatePath(t.getSelection())},0))},nt.focus=function(){return this._root.focus({preventScroll:!0}),X&&this.fireEvent("focus"),this},nt.blur=function(){return this._root.blur(),X&&this.fireEvent("blur"),this};var st="squire-selection-end";nt._saveRangeToBookmark=function(e){var t,n=this.createElement("INPUT",{id:"squire-selection-start",type:"hidden"}),i=this.createElement("INPUT",{id:st,type:"hidden"});me(e,n),e.collapse(!1),me(e,i),2&n.compareDocumentPosition(i)&&(n.id=st, -i.id="squire-selection-start",t=n,n=i,i=t),e.setStartAfter(n),e.setEndBefore(i)},nt._getRangeAndRemoveBookmark=function(e){var t=this._root,n=t.querySelector("#squire-selection-start"),i=t.querySelector("#"+st);if(n&&i){var o=n.parentNode,r=i.parentNode,a=oe.call(o.childNodes,n),s=oe.call(r.childNodes,i);o===r&&(s-=1),C(n),C(i),e||(e=this._doc.createRange()),e.setStart(o,a),e.setEnd(r,s),k(o,e),o!==r&&k(r,e),e.collapsed&&(o=e.startContainer,o.nodeType===w&&(r=o.childNodes[e.startOffset],r&&r.nodeType===w||(r=o.childNodes[e.startOffset-1]),r&&r.nodeType===w&&(e.setStart(r,0),e.collapse(!0))))}return e||null},nt._keyUpDetectChange=function(e){var t=e.keyCode;e.ctrlKey||e.metaKey||e.altKey||!(t<16||t>20)||!(t<33||t>45)||this._docWasChanged()},nt._docWasChanged=function(){if(ne&&(fe=new WeakMap),!this._ignoreAllChanges){if(te&&this._ignoreChange)return void(this._ignoreChange=!1);this._isInUndoState&&(this._isInUndoState=!1,this.fireEvent("undoStateChange",{canUndo:!0,canRedo:!1})),this.fireEvent("input")}},nt._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}},nt.saveUndoState=function(e){return e===t&&(e=this.getSelection()),this._recordUndoState(e,this._isInUndoState),this._getRangeAndRemoveBookmark(e),this},nt.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},nt.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),o=this.createElement(e,t),N(h,o),o.appendChild(h))}while(r.nextNode());d.nodeType!==w&&(h.nodeType===w?(d=h,c=h.length):(d=h.parentNode,c=1)),i=this.createRange(s,l,d,c)}return i},nt._removeFormat=function(e,t,n,i){this._saveRangeToBookmark(n);var o,r=this._doc;n.collapsed&&(ee?(o=r.createTextNode(z),this._didAddZWS()):o=r.createTextNode(""),me(n,o));for(var s=n.commonAncestorContainer;a(s);)s=s.parentNode;var d=n.startContainer,l=n.startOffset,c=n.endContainer,h=n.endOffset,u=[],f=function(e,t){if(!Ne(n,e,!1)){var i,o,r=e.nodeType===w;if(!Ne(n,e,!0))return void("INPUT"===e.nodeName||r&&!e.data||u.push([t,e]));if(r)e===c&&h!==e.length&&u.push([t,e.splitText(h)]),e===d&&l&&(e.splitText(l),u.push([t,e]));else for(i=e.firstChild;i;i=o)o=i.nextSibling,f(i,t)}},g=Array.prototype.filter.call(s.getElementsByTagName(e),function(i){return Ne(n,i,!0)&&p(i,e,t)});return i||g.forEach(function(e){f(e,e)}),u.forEach(function(e){var t=e[0].cloneNode(!1),n=e[1];N(n,t),t.appendChild(n)}),g.forEach(function(e){N(e,S(e))}),this._getRangeAndRemoveBookmark(n),o&&n.collapse(!1),k(s,n),n},nt.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),te||this._docWasChanged(),this):this};var dt={DT:"DD",DD:"DT",LI:"LI",PRE:"PRE"},lt=function(e,t,n,i){var o=dt[t.nodeName],r=null,a=b(n,i,t.parentNode,e._root),s=e._config;return o||(o=s.blockTag,r=s.blockAttributes),p(a,o,r)||(t=y(a.ownerDocument,o,r),a.dir&&(t.dir=a.dir),N(a,t),t.appendChild(S(a)),a=t),a};nt.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var i=this._root,o=Te(n,i),r=Ee(n,i);if(o&&r)do{if(e(o)||o===r)break}while(o=h(o,i));return t&&(this.setSelection(n),this._updatePath(n,!0),te||this._docWasChanged()),this},nt.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._recordUndoState(t,this._isInUndoState);var n,i=this._root;return xe(t,i),ye(t,i,i,i),n=ve(t,i,i),me(t,e.call(this,n)),t.endOffset]+|\([^\s()<>]+\))+(?:[^\s?&`!()\[\]{};:'".,<>«»“”‘’]|\([^\s()<>]+\)))|([\w\-.%+]+@(?:[\w\-]+\.)+[a-z]{2,}\b(?:[?][^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+(?:&[^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+)*)?))/i;var _t=function(e,t,i){var o,r,a,s,d,l,c,h=e.ownerDocument,u=new n(e,4,function(e){return!g(e,t,"A")}),f=i.linkRegExp,p=i._config.tagAttributes.a;if(f)for(;o=u.nextNode();)for(r=o.data,a=o.parentNode;s=f.exec(r);)d=s.index,l=d+s[0].length,d&&(c=h.createTextNode(r.slice(0,d)),a.insertBefore(c,o)),c=i.createElement("A",B({href:s[1]?/^(?:ht|f)tps?:/i.test(s[1])?s[1]:"http://"+s[1]:"mailto:"+s[0]},p,!1)),c.textContent=r.slice(d,l),a.insertBefore(c,o),o.data=r=r.slice(l)};nt.insertHTML=function(e,t){var n,i,o,r,a,s,d,l=this._config,c=l.isInsertedHTMLSanitized?l.sanitizeToDOMFragment:null,u=this.getSelection(),f=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"),o.innerHTML=e,r=f.createDocumentFragment(),r.appendChild(S(o))),this.saveUndoState(u);try{for(a=this._root,s=r,d={fragment:r,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},_t(r,r,this),Ze(r,l),Ve(r,a,!1),Qe(r),r.normalize();s=h(s,r);)T(s,a);t&&this.fireEvent("willPaste",d),d.defaultPrevented||(Ce(u,d.fragment,a),te||this._docWasChanged(),u.collapse(!1),this._ensureBottomLine()),this.setSelection(u),this._updatePath(u,!0),t&&this.focus()}catch(e){this.didError(e)}return this};var Ct=function(e){return e.split("&").join("&").split("<").join("<").split(">").join(">").split('"').join(""")};nt.insertPlainText=function(e,t){var n=this.getSelection();if(n.collapsed&&g(n.startContainer,this._root,"PRE")){var i,o,r=n.startContainer,a=n.startOffset;return r&&r.nodeType===w||(i=this._doc.createTextNode(""),r.insertBefore(i,r.childNodes[a]),r=i,a=0),o={text:e,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},t&&this.fireEvent("willPaste",o),o.defaultPrevented||(e=o.text,r.insertData(a,e),n.setStart(r,a+e.length),n.collapse(!0)),this.setSelection(n),this}var s,d,l,c,h=e.split("\n"),u=this._config,f=u.blockTag,p=u.blockAttributes,m="",v="<"+f;for(s in p)v+=" "+s+'="'+Ct(p[s])+'"';for(v+=">",d=0,l=h.length;d")+m),h[d]=c;return this.insertHTML(h.join(""),t)};var Nt=function(e,t,n){return function(){return this[e](t,n),this.focus()}};nt.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},nt.bold=Nt("changeFormat",{tag:"B"}),nt.italic=Nt("changeFormat",{tag:"I"}),nt.underline=Nt("changeFormat",{tag:"U"}),nt.strikethrough=Nt("changeFormat",{tag:"S"}),nt.subscript=Nt("changeFormat",{tag:"SUB"},{tag:"SUP"}),nt.superscript=Nt("changeFormat",{tag:"SUP"},{tag:"SUB"}),nt.removeBold=Nt("changeFormat",null,{tag:"B"}),nt.removeItalic=Nt("changeFormat",null,{tag:"I"}),nt.removeUnderline=Nt("changeFormat",null,{tag:"U"}),nt.removeStrikethrough=Nt("changeFormat",null,{tag:"S"}),nt.removeSubscript=Nt("changeFormat",null,{tag:"SUB"}),nt.removeSuperscript=Nt("changeFormat",null,{tag:"SUP"}),nt.makeLink=function(e,t){var n=this.getSelection();if(n.collapsed){var i=e.indexOf(":")+1;if(i)for(;"/"===e[i];)i+=1;me(n,this._doc.createTextNode(e.slice(i)))}return t=B(B({href:e},t,!0),this._config.tagAttributes.a,!1),this.changeFormat({tag:"A",attributes:t},{tag:"A"},n),this.focus()},nt.removeLink=function(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0),this.focus()},nt.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()},nt.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()},nt.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()},nt.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()},nt.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()},nt.setTextDirection=function(e){return this.forEachBlock(function(t){e?t.dir=e:t.removeAttribute("dir")},!0),this.focus()};var St=function(e){for(var t,i=this._root,o=this._doc,r=o.createDocumentFragment(),a=l(e,i);t=a.nextNode();){var s,d,c=t.querySelectorAll("BR"),h=[],u=c.length;for(s=0;s-1;)a.appendChild(d.createTextNode(r.slice(0,s))),a.appendChild(d.createElement("BR")),r=r.slice(s+1);o.parentNode.insertBefore(a,o),o.data=r}E(t,l),N(t,S(t))}return e};nt.code=function(){var e=this.getSelection();return e.collapsed||d(e.commonAncestorContainer)?this.modifyBlocks(St,e):this.changeFormat({tag:"CODE",attributes:this._config.tagAttributes.code},null,e),this.focus()},nt.removeCode=function(){var e=this.getSelection();return g(e.commonAncestorContainer,this._root,"PRE")?this.modifyBlocks(yt,e):this.changeFormat(null,{tag:"CODE"},e),this.focus()},nt.toggleCode=function(){return this.hasFormat("PRE")||this.hasFormat("CODE")?this.removeCode():this.code(),this},nt.removeAllFormatting=function(e){if(!e&&!(e=this.getSelection())||e.collapsed)return this;for(var t=this._root,n=e.commonAncestorContainer;n&&!s(n);)n=n.parentNode;if(n||(xe(e,t),n=t),n.nodeType===w)return this;this.saveUndoState(e),ye(e,n,n,t);for(var i,o,r=n.ownerDocument,a=e.startContainer,d=e.startOffset,l=e.endContainer,c=e.endOffset,h=r.createDocumentFragment(),u=r.createDocumentFragment(),f=b(l,c,n,t),p=b(a,d,n,t);p!==f;)i=p.nextSibling,h.appendChild(p),p=i;return I(this,h,u),u.normalize(),p=u.firstChild,i=u.lastChild,o=n.childNodes,p?(n.insertBefore(u,f),d=oe.call(o,p),c=oe.call(o,i)+1):(d=oe.call(o,f),c=d),e.setStart(n,d),e.setEnd(n,c),k(n,e),Se(e),this.setSelection(e),this._updatePath(e,!0),this.focus()},nt.increaseQuoteLevel=Nt("modifyBlocks",ct),nt.decreaseQuoteLevel=Nt("modifyBlocks",ht),nt.makeUnorderedList=Nt("modifyBlocks",pt),nt.makeOrderedList=Nt("modifyBlocks",gt),nt.removeList=Nt("modifyBlocks",mt),R.isInline=a,R.isBlock=s,R.isContainer=d,R.getBlockWalker=l,R.getPreviousBlock=c,R.getNextBlock=h,R.areAlike=f,R.hasTagAttributes=p,R.getNearest=g,R.isOrContains=m,R.detach=C,R.replaceWith=N,R.empty=S,R.getNodeBefore=pe,R.getNodeAfter=ge,R.insertNodeInRange=me,R.extractContentsOfRange=ve,R.deleteContentsOfRange=_e,R.insertTreeFragmentIntoRange=Ce,R.isNodeContainedInRange=Ne,R.moveRangeBoundariesDownTree=Se,R.moveRangeBoundariesUpTree=ye,R.getStartBlockOfRange=Te,R.getEndBlockOfRange=Ee,R.contentWalker=be,R.rangeDoesStartAtBlockBoundary=Le,R.rangeDoesEndAtBlockBoundary=ke,R.expandRangeToBlockBoundaries=xe,R.onPaste=et,R.addLinks=_t,R.splitBlock=lt,R.startSelectionId="squire-selection-start",R.endSelectionId=st,"object"==typeof exports?module.exports=R:"function"==typeof define&&define.amd?define(function(){return R}):(q.Squire=R,top!==q&&"true"===e.documentElement.getAttribute("data-squireinit")&&(q.editor=new R(e),q.onEditorLoad&&(q.onEditorLoad(q.editor),q.onEditorLoad=null)))}(document); \ No newline at end of file +!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),f=/Windows NT/.test(l),u=/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),_=h?"meta-":"ctrl-",C=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},b=function(){return!0};function L(e,t,n){this.root=this.currentNode=e,this.nodeType=t,this.filter=n||b}L.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}},L.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}},L.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 F(e){return U(e)===B}function w(e){return U(e)===R}function M(e,t){var n=new L(t,a,F);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(!w(o)){if(!s)return;(i=Y(a,"DIV")).appendChild(V(o)),o.appendChild(i)}$(e),n=!w(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))},fe=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)},ue=function(e,t,n){var i=G(e.endContainer,n,"A");if(i){var o=e.cloneRange();i=i.parentNode,fe(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&&F(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&&F(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 L(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()},_e=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]()}};ke[_+"b"]=Te("B"),ke[_+"i"]=Te("I"),ke[_+"u"]=Te("U"),ke[_+"shift-7"]=Te("S"),ke[_+"shift-5"]=Te("SUB",{tag:"SUP"}),ke[_+"shift-6"]=Te("SUP",{tag:"SUB"}),ke[_+"shift-8"]=ye("makeUnorderedList"),ke[_+"shift-9"]=ye("makeOrderedList"),ke[_+"["]=xe("decreaseQuoteLevel","decreaseListLevel"),ke[_+"]"]=xe("increaseQuoteLevel","increaseListLevel"),ke[_+"d"]=ye("toggleCode"),ke[_+"y"]=ye("redo"),ke[_+"z"]=ye("undo"),ke[_+"shift-z"]=ye("redo");var Oe={1:10,2:13,3:16,4:18,5:24,6:32,7:48},Ae={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")}}},Be=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,_=this.getSelection(),C=_.startContainer,N=_.startOffset,S=_.endContainer,y=_.endOffset,E=this.createElement("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});v.appendChild(E),_.selectNodeContents(E),this.setSelection(_),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(C,N,S,y),m.setSelection(t),n&&m.insertHTML(n,!0)}catch(e){m.didError(e)}},0)},Ze=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 Qe(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?Qe(e[i],o,n):o);return e}function $e(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",Je),this.addEventListener("mousedown",et),this.addEventListener("touchstart",et),this.addEventListener("focus",tt),this._awaitingPaste=!1,this.addEventListener("cut",ze),this.addEventListener("copy",qe),this.addEventListener("keydown",Ke),this.addEventListener("keyup",Ke),this.addEventListener("paste",Ge),this.addEventListener("drop",Ze),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 je=$e.prototype,Ve=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()};je.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:x,undo:{documentSizeThreshold:-1,undoLimit:-1},isInsertedHTMLSanitized:!0,isSetHTMLSanitized:!0,sanitizeToDOMFragment:"undefined"!=typeof DOMPurify&&DOMPurify.isSupported?Ve:null,willCutCopy:null,addLinks:!0},e,!0)).blockTag=e.blockTag.toUpperCase(),this._config=e,this},je.createElement=function(e,t,n){return Y(this._doc,e,t,n)},je.createDefaultBlock=function(e){var t=this._config;return X(this.createElement(t.blockTag,t.blockAttributes,e),this._root)},je.didError=function(e){console.log(e)},je.getDocument=function(){return this._doc},je.getRoot=function(){return this._root},je.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 Ye={pathChange:1,select:1,input:1,undoStateChange:1};je.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},je.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},je.handleEvent=function(e){this.fireEvent(e.type,e)},je.addEventListener=function(e,t){var n=this._events[e],i=this._root;return t?(n||(n=this._events[e]=[],Ye[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)},je.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],Ye[e]||("selectionchange"===e&&(o=this._doc),o.removeEventListener(e,this,!0)))}return this},je.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},je.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},je._moveCursorTo=function(e){var t=this._root,n=this.createRange(t,e?0:t.childNodes.length);return he(n),this.setSelection(n),this},je.moveCursorToStart=function(){return this._moveCursorTo(!0)},je.moveCursorToEnd=function(){return this._moveCursorTo(!1)};var Xe=function(e){return e._win.getSelection()||null};function Je(){this._restoreSelection=!0}function et(){this._restoreSelection=!1}function tt(){this._restoreSelection&&this.setSelection(this._lastSelection)}je.setSelection=function(e){if(e)if(this._lastSelection=e,this._isFocused)if(c&&!this._restoreSelection)Je.call(this),this.blur(),this.focus();else{u&&this._win.focus();var t=Xe(this);t&&(t.removeAllRanges(),t.addRange(e))}else Je.call(this);return this},je.getSelection=function(){var e,t,n,i,o=Xe(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},je.getSelectedText=function(){var e=this.getSelection();if(!e||e.collapsed)return"";var t,n=new L(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},je.getPath=function(){return this._path};var nt=function(e,t){for(var n,i,o,r=new L(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)}};je._didAddZWS=function(){this._hasZWS=!0},je._removeZWS=function(){this._hasZWS&&(nt(this._root),this._hasZWS=!1)},je._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})}},je._updatePathOnEvent=function(e){var t=this;t._isFocused&&!t._willUpdatePath&&(t._willUpdatePath=!0,setTimeout(function(){t._willUpdatePath=!1,t._updatePath(t.getSelection())},0))},je.focus=function(){return this._root.focus({preventScroll:!0}),v&&this.fireEvent("focus"),this},je.blur=function(){return this._root.blur(),v&&this.fireEvent("blur"),this};var it="squire-selection-end";je._saveRangeToBookmark=function(e){var t,n=this.createElement("INPUT",{id:"squire-selection-start",type:"hidden"}),i=this.createElement("INPUT",{id:it,type:"hidden"});ae(e,n),e.collapse(!1),ae(e,i),2&n.compareDocumentPosition(i)&&(n.id=it,i.id="squire-selection-start",t=n,n=i,i=t),e.setStartAfter(n),e.setEndBefore(i)},je._getRangeAndRemoveBookmark=function(e){var t=this._root,n=t.querySelector("#squire-selection-start"),o=t.querySelector("#"+it);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},je._keyUpDetectChange=function(e){var t=e.keyCode;e.ctrlKey||e.metaKey||e.altKey||!(t<16||t>20)||!(t<33||t>45)||this._docWasChanged()},je._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")))},je._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}},je.saveUndoState=function(e){return e===t&&(e=this.getSelection()),this._recordUndoState(e,this._isInUndoState),this._getRangeAndRemoveBookmark(e),this},je.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},je.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},je._removeFormat=function(e,t,n,o){this._saveRangeToBookmark(n);var r,a=this._doc;n.collapsed&&(C?(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,f=n.endOffset,u=[],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&&f!==e.length&&u.push([t,e.splitText(f)]),e===l&&c&&(e.splitText(c),u.push([t,e]));else for(o=e.firstChild;o;o=r)r=o.nextSibling,p(o,t);else"INPUT"===e.nodeName||a&&!e.data||u.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)}),u.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},je.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 ot={DT:"DD",DD:"DT",LI:"LI",PRE:"PRE"},rt=function(e,t,n,i){var o=ot[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};je.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},je.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._recordUndoState(t,this._isInUndoState);var n,i=this._root;return Ce(t,i),fe(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 ct=function(e,t,n){var i,o,r,a,s,d,l,c=e.ownerDocument,h=new L(e,4,function(e){return!G(e,t,"A")}),f=n.linkRegExp,u=n._config.tagAttributes.a;if(f)for(;i=h.nextNode();)for(o=i.data,r=i.parentNode;a=f.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",Qe({href:a[1]?/^(?:ht|f)tps?:/i.test(a[1])?a[1]:"http://"+a[1]:"mailto:"+a[0]},u,!1))).textContent=o.slice(s,d),r.insertBefore(l,i),i.data=o=o.slice(d)};je.insertHTML=function(e,t){var n,i,o,r,a,s,d,l=this._config,c=l.isInsertedHTMLSanitized?l.sanitizeToDOMFragment:null,h=this.getSelection(),f=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=f.createDocumentFragment()).appendChild(V(o))),this.saveUndoState(h);try{for(a=this._root,s=r,d={fragment:r,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},ct(r,r,this),Ie(r,l),He(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),ue(h,0,a),this._ensureBottomLine()),this.setSelection(h),this._updatePath(h,!0),t&&this.focus()}catch(e){this.didError(e)}return this};var ht=function(e){return e.split("&").join("&").split("<").join("<").split(">").join(">").split('"').join(""")};je.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,f=e.split("\n"),u=this._config,p=u.blockTag,g=u.blockAttributes,m="",v="<"+p;for(d in g)v+=" "+d+'="'+ht(g[d])+'"';for(v+=">",l=0,c=f.length;l")+m),f[l]=h;return this.insertHTML(f.join(""),t)};var ft=function(e,t,n){return function(){return this[e](t,n),this.focus()}};je.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},je.bold=ft("changeFormat",{tag:"B"}),je.italic=ft("changeFormat",{tag:"I"}),je.underline=ft("changeFormat",{tag:"U"}),je.strikethrough=ft("changeFormat",{tag:"S"}),je.subscript=ft("changeFormat",{tag:"SUB"},{tag:"SUP"}),je.superscript=ft("changeFormat",{tag:"SUP"},{tag:"SUB"}),je.removeBold=ft("changeFormat",null,{tag:"B"}),je.removeItalic=ft("changeFormat",null,{tag:"I"}),je.removeUnderline=ft("changeFormat",null,{tag:"U"}),je.removeStrikethrough=ft("changeFormat",null,{tag:"S"}),je.removeSubscript=ft("changeFormat",null,{tag:"SUB"}),je.removeSuperscript=ft("changeFormat",null,{tag:"SUP"}),je.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=Qe(Qe({href:e},t,!0),this._config.tagAttributes.a,!1),this.changeFormat({tag:"A",attributes:t},{tag:"A"},n),this.focus()},je.removeLink=function(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0),this.focus()},je.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()},je.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()},je.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()},je.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()},je.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()},je.setTextDirection=function(e){return this.forEachBlock(function(t){e?t.dir=e:t.removeAttribute("dir")},!0),this.focus()};var ut=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};je.code=function(){var e=this.getSelection();return e.collapsed||w(e.commonAncestorContainer)?this.modifyBlocks(ut,e):this.changeFormat({tag:"CODE",attributes:this._config.tagAttributes.code},null,e),this.focus()},je.removeCode=function(){var e=this.getSelection();return G(e.commonAncestorContainer,this._root,"PRE")?this.modifyBlocks(pt,e):this.changeFormat(null,{tag:"CODE"},e),this.focus()},je.toggleCode=function(){return this.hasFormat("PRE")||this.hasFormat("CODE")?this.removeCode():this.code(),this},je.removeAllFormatting=function(e){if(!e&&!(e=this.getSelection())||e.collapsed)return this;for(var t=this._root,n=e.commonAncestorContainer;n&&!F(n);)n=n.parentNode;if(n||(Ce(e,t),n=t),n.nodeType===i)return this;this.saveUndoState(e),fe(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(),f=a.createDocumentFragment(),u=ee(l,c,n,t),p=ee(s,d,n,t);p!==u;)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(F(r)){o.appendChild(t.createDefaultBlock([e(t,r,t._doc.createDocumentFragment())]));continue}e(t,r,o)}return o}(this,h,f),f.normalize(),p=f.firstChild,o=f.lastChild,r=n.childNodes,p?(n.insertBefore(f,u),d=T.call(r,p),c=T.call(r,o)+1):c=d=T.call(r,u),e.setStart(n,d),e.setEnd(n,c),te(n,e),he(e),this.setSelection(e),this._updatePath(e,!0),this.focus()},je.increaseQuoteLevel=ft("modifyBlocks",function(e){return this.createElement("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e])}),je.decreaseQuoteLevel=ft("modifyBlocks",at),je.makeUnorderedList=ft("modifyBlocks",function(e){return dt(this,e,"UL"),e}),je.makeOrderedList=ft("modifyBlocks",function(e){return dt(this,e,"OL"),e}),je.removeList=ft("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 element and if so if there is an equivalent cursor + // position after the
element. If there is, move it there. + moveRangeBoundaryOutOf( range, 'A', root ); + this._ensureBottomLine(); } diff --git a/source/KeyHandlers.js b/source/KeyHandlers.js index f8327bd..69e0ddd 100644 --- a/source/KeyHandlers.js +++ b/source/KeyHandlers.js @@ -227,12 +227,7 @@ var handleEnter = function ( self, shiftKey, range ) { // 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 ); - } + moveRangeBoundaryOutOf( range, 'A', root ); insertNodeInRange( range, self.createElement( 'BR' ) ); range.collapse( false ); self.setSelection( range ); @@ -587,7 +582,7 @@ const changeIndentationLevel = function ( methodIfInQuote, methodIfInList ) { return function ( self, event ) { event.preventDefault(); var path = self.getPath(); - if ( /(?:^|>)BLOCKQUOTE/.test( path ) || + if ( /(?:^|>)BLOCKQUOTE/.test( path ) || !/(?:^|>)[OU]L/.test( path ) ) { self[ methodIfInQuote ](); } else { @@ -604,9 +599,9 @@ keyHandlers[ ctrlKey + 'shift-5' ] = mapKeyToFormat( 'SUB', { tag: 'SUP' } ); keyHandlers[ ctrlKey + 'shift-6' ] = mapKeyToFormat( 'SUP', { tag: 'SUB' } ); keyHandlers[ ctrlKey + 'shift-8' ] = mapKeyTo( 'makeUnorderedList' ); keyHandlers[ ctrlKey + 'shift-9' ] = mapKeyTo( 'makeOrderedList' ); -keyHandlers[ ctrlKey + '[' ] = +keyHandlers[ ctrlKey + '[' ] = changeIndentationLevel( 'decreaseQuoteLevel', 'decreaseListLevel' ); -keyHandlers[ ctrlKey + ']' ] = +keyHandlers[ ctrlKey + ']' ] = changeIndentationLevel( 'increaseQuoteLevel', 'increaseListLevel' ); keyHandlers[ ctrlKey + 'd' ] = mapKeyTo( 'toggleCode' ); keyHandlers[ ctrlKey + 'y' ] = mapKeyTo( 'redo' ); diff --git a/source/Range.js b/source/Range.js index f4e166e..bfe763a 100644 --- a/source/Range.js +++ b/source/Range.js @@ -411,6 +411,9 @@ var moveRangeBoundariesUpTree = function ( range, startMax, endMax, root ) { } while ( true ) { + if ( endContainer === endMax || endContainer === root ) { + break; + } if ( maySkipBR && endContainer.nodeType !== TEXT_NODE && endContainer.childNodes[ endOffset ] && @@ -418,9 +421,7 @@ var moveRangeBoundariesUpTree = function ( range, startMax, endMax, root ) { endOffset += 1; maySkipBR = false; } - if ( endContainer === endMax || - endContainer === root || - endOffset !== getLength( endContainer ) ) { + if ( endOffset !== getLength( endContainer ) ) { break; } parent = endContainer.parentNode; @@ -432,6 +433,20 @@ var moveRangeBoundariesUpTree = function ( range, startMax, endMax, root ) { range.setEnd( endContainer, endOffset ); }; +var moveRangeBoundaryOutOf = function ( range, nodeName, root ) { + var parent = getNearest( range.endContainer, root, 'A' ); + if ( parent ) { + var clone = range.cloneRange(); + parent = parent.parentNode; + moveRangeBoundariesUpTree( clone, parent, parent, root ); + if ( clone.endContainer === parent ) { + range.setStart( clone.endContainer, clone.endOffset ); + range.setEnd( clone.endContainer, clone.endOffset ); + } + } + return range; +}; + // Returns the first block at least partially contained by the range, // or null if no block is contained by the range. var getStartBlockOfRange = function ( range, root ) {