From aa41badbe027b682d2bf3a2998391f2a4261c443 Mon Sep 17 00:00:00 2001 From: Robert O'Leary Date: Wed, 1 Feb 2017 18:29:15 +0100 Subject: [PATCH] Fix issue with focusing image within editor --- build/squire-raw.js | 14 ++++++++++++-- build/squire.js | 4 ++-- source/Editor.js | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/build/squire-raw.js b/build/squire-raw.js index b769bcf..0ed36bc 100644 --- a/build/squire-raw.js +++ b/build/squire-raw.js @@ -2934,8 +2934,18 @@ proto.getSelection = function () { return selection; }; -function enableRestoreSelection () { - this._restoreSelection = true; +function enableRestoreSelection (ev) { + // Blur can be called when you are focusing on other elements within the + // editor. We should only want to restore the selection if we are coming + // from outside of the editor. We need to do this on a timeout because we + // cannot determine what the new focused element is until after the blur. + var self = this; + setTimeout(function() { + var target = document.activeElement; + if( !isOrContains( self._root, document.activeElement ) ) { + this._restoreSelection = true; + } + }, 1); } function disableRestoreSelection () { this._restoreSelection = false; diff --git a/build/squire.js b/build/squire.js index ab6361e..548ae32 100644 --- a/build/squire.js +++ b/build/squire.js @@ -1,2 +1,2 @@ -!function(e,t){"use strict";function n(e,t,n){this.root=this.currentNode=e,this.nodeType=t,this.filter=n}function o(e,t){for(var n=e.length;n--;)if(!t(e[n]))return!1;return!0}function i(e){return e.nodeType===w&&!!_e[e.nodeName]}function r(e){switch(e.nodeType){case F:return ye;case w:case H:if(ge&&Ee.has(e))return Ee.get(e);break;default:return Se}var t;return t=o(e.childNodes,a)?Ce.test(e.nodeName)?ye:Te:be,ge&&Ee.set(e,t),t}function a(e){return r(e)===ye}function s(e){return r(e)===Te}function d(e){return r(e)===be}function l(e,t){var o=new n(t,W,s);return o.currentNode=e,o}function c(e,t){return e=l(e,t).previousNode(),e!==t?e:null}function h(e,t){return e=l(e,t).nextNode(),e!==t?e:null}function u(e,t){return!i(e)&&e.nodeType===t.nodeType&&e.nodeName===t.nodeName&&"A"!==e.nodeName&&e.className===t.className&&(!e.style&&!t.style||e.style.cssText===t.style.cssText)}function f(e,t,n){if(e.nodeName!==t)return!1;for(var o in n)if(e.getAttribute(o)!==n[o])return!1;return!0}function p(e,t,n,o){for(;e&&e!==t;){if(f(e,n,o))return e;e=e.parentNode}return null}function g(e,t){for(;t;){if(t===e)return!0;t=t.parentNode}return!1}function m(e,t){var n,o,i,r,a="";return e&&e!==t&&(a=m(e.parentNode,t),e.nodeType===w&&(a+=(a?">":"")+e.nodeName,(n=e.id)&&(a+="#"+n),(o=e.className.trim())&&(i=o.split(/\s\s*/),i.sort(),a+=".",a+=i.join(".")),(r=e.dir)&&(a+="[dir="+r+"]"),i&&(ve.call(i,j)>-1&&(a+="[backgroundColor="+e.style.backgroundColor.replace(/ /g,"")+"]"),ve.call(i,Q)>-1&&(a+="[color="+e.style.color.replace(/ /g,"")+"]"),ve.call(i,V)>-1&&(a+="[fontFamily="+e.style.fontFamily.replace(/ /g,"")+"]"),ve.call(i,$)>-1&&(a+="[fontSize="+e.style.fontSize+"]")))),a}function v(e){var t=e.nodeType;return t===w?e.childNodes.length:e.length||0}function N(e){var t=e.parentNode;return t&&t.removeChild(e),e}function C(e,t){var n=e.parentNode;n&&n.replaceChild(t,e)}function _(e){for(var t=e.ownerDocument.createDocumentFragment(),n=e.childNodes,o=n?n.length:0;o--;)t.appendChild(e.firstChild);return t}function S(e,n,o,i){var r,a,s,d,l=e.createElement(n);if(o instanceof Array&&(i=o,o=null),o)for(r in o)a=o[r],a!==t&&l.setAttribute(r,o[r]);if(i)for(s=0,d=i.length;ss?t.startOffset-=1:t.startOffset===s&&(t.startContainer=o,t.startOffset=v(o))),t.endContainer===e&&(t.endOffset>s?t.endOffset-=1:t.endOffset===s&&(t.endContainer=o,t.endOffset=v(o))),N(n),n.nodeType===F?o.appendData(n.data):d.push(_(n));else if(n.nodeType===w){for(i=d.length;i--;)n.appendChild(d.pop());E(n,t)}}function k(e,t){if(e.nodeType===F&&(e=e.parentNode),e.nodeType===w){var n={startContainer:t.startContainer,startOffset:t.startOffset,endContainer:t.endContainer,endOffset:t.endOffset};E(e,n),t.setStart(n.startContainer,n.startOffset),t.setEnd(n.endContainer,n.endOffset)}}function B(e,t,n){for(var o,i,r=t;1===r.parentNode.childNodes.length;)r=r.parentNode;N(r),i=e.childNodes.length,o=e.lastChild,o&&"BR"===o.nodeName&&(e.removeChild(o),i-=1),e.appendChild(_(t)),n.setStart(e,i),n.collapse(!0),k(e,n),ae&&(o=e.lastChild)&&"BR"===o.nodeName&&e.removeChild(o)}function x(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&&u(i,e)){if(!d(i)){if(!s)return;o=S(a,"DIV"),o.appendChild(_(i)),i.appendChild(o)}N(e),n=!d(e),i.appendChild(_(e)),n&&T(i,t),r&&x(r,t)}else s&&(i=S(a,"DIV"),e.insertBefore(i,r),y(i,t))}function L(e){this.isShiftDown=e.shiftKey}function A(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?A(e[o],i,n):i);return e}function O(e,t){e.nodeType===M&&(e=e.body);var n,o=e.ownerDocument,i=o.defaultView;this._win=i,this._doc=o,this._root=e,this._events={},this._isFocused=!1,this._lastSelection=null,fe&&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,pe?(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",R),this.addEventListener("touchstart",R),this.addEventListener("focus",P),this._awaitingPaste=!1,this.addEventListener(re?"beforecut":"cut",st),this.addEventListener("copy",dt),this.addEventListener("keydown",L),this.addEventListener("keyup",L),this.addEventListener(re?"beforepaste":"paste",lt),this.addEventListener("drop",ct),this.addEventListener(ae?"keypress":"keydown",ze),this._keyHandlers=Object.create(Ze),this.setConfig(t),re&&(i.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("")}function D(){this._restoreSelection=!0}function R(){this._restoreSelection=!1}function P(){this._restoreSelection&&this.setSelection(this._lastSelection)}function U(e,t,n){var o,i;for(o=t.firstChild;o;o=i){if(i=o.nextSibling,a(o)){if(o.nodeType===F||"BR"===o.nodeName||"IMG"===o.nodeName){n.appendChild(o);continue}}else if(s(o)){n.appendChild(e.createDefaultBlock([U(e,o,e._doc.createDocumentFragment())]));continue}U(e,o,n)}return n}var I=2,w=1,F=3,M=9,H=11,W=1,z=4,q=0,K=1,G=2,Z=3,j="highlight",Q="colour",V="font",$="size",Y="​",X=e.defaultView,J=navigator.userAgent,ee=/Android/.test(J),te=/iP(?:ad|hone|od)/.test(J),ne=/Mac OS X/.test(J),oe=/Windows NT/.test(J),ie=/Gecko\//.test(J),re=/Trident\/[456]\./.test(J),ae=!!X.opera,se=/Edge\//.test(J),de=!se&&/WebKit\//.test(J),le=/Trident\/[4567]\./.test(J),ce=ne?"meta-":"ctrl-",he=re||ae,ue=re||de,fe=re,pe="undefined"!=typeof MutationObserver,ge="undefined"!=typeof WeakMap,me=/[^ \t\r\n]/,ve=Array.prototype.indexOf;Object.create||(Object.create=function(e){var t=function(){};return t.prototype=e,new t});var Ne={1:1,2:2,3:4,8:128,9:256,11:1024};n.prototype.nextNode=function(){for(var e,t=this.currentNode,n=this.root,o=this.nodeType,i=this.filter;;){for(e=t.firstChild;!e&&t&&t!==n;)e=t.nextSibling,e||(t=t.parentNode);if(!e)return null;if(Ne[e.nodeType]&o&&i(e))return this.currentNode=e,e;t=e}},n.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(Ne[e.nodeType]&o&&i(e))return this.currentNode=e,e;t=e}},n.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,e||(t=t.parentNode);if(!e)return null;if(Ne[e.nodeType]&o&&i(e))return this.currentNode=e,e;t=e}};var Ce=/^(?:#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)$/,_e={BR:1,HR:1,IFRAME:1,IMG:1,INPUT:1},Se=0,ye=1,Te=2,be=3,Ee=ge?new WeakMap:null,ke=function(e,t){for(var n=e.childNodes;t&&e.nodeType===w;)e=n[t-1],n=e.childNodes,t=n.length;return e},Be=function(e,t){if(e.nodeType===w){var n=e.childNodes;if(t-1,r=e.compareBoundaryPoints(K,o)<1;return!i&&!r}var a=e.compareBoundaryPoints(q,o)<1,s=e.compareBoundaryPoints(G,o)>-1;return a&&s},Re=function(e){for(var t,n=e.startContainer,o=e.startOffset,r=e.endContainer,a=e.endOffset,s=!0;n.nodeType!==F&&(t=n.childNodes[o],t&&!i(t));)n=t,o=0;if(a)for(;r.nodeType!==F;){if(t=r.childNodes[a-1],!t||i(t)){if(s&&t&&"BR"===t.nodeName){a-=1,s=!1;continue}break}r=t,a=v(r)}else for(;r.nodeType!==F&&(t=r.firstChild,t&&!i(t));)r=t;e.collapsed?(e.setStart(r,a),e.setEnd(n,o)):(e.setStart(n,o),e.setEnd(r,a))},Pe=function(e,t,n,o){var i,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!==o;)i=r.parentNode,a=ve.call(i.childNodes,r),r=i;for(;;){if(l&&s.nodeType!==F&&s.childNodes[d]&&"BR"===s.childNodes[d].nodeName&&(d+=1,l=!1),s===n||s===o||d!==v(s))break;i=s.parentNode,d=ve.call(i.childNodes,s)+1,s=i}e.setStart(r,a),e.setEnd(s,d)},Ue=function(e,t){var n,o=e.startContainer;return a(o)?n=c(o,t):o!==t&&s(o)?n=o:(n=ke(o,e.startOffset),n=h(n,t)),n&&De(e,n,!0)?n:null},Ie=function(e,t){var n,o,i=e.endContainer;if(a(i))n=c(i,t);else if(i!==t&&s(i))n=i;else{if(n=Be(i,e.endOffset),!n||!g(t,n))for(n=t;o=n.lastChild;)n=o;n=c(n,t)}return n&&De(e,n,!0)?n:null},we=new n(null,z|W,function(e){return e.nodeType===F?me.test(e.data):"IMG"===e.nodeName}),Fe=function(e,t){var n,o=e.startContainer,i=e.startOffset;if(we.root=null,o.nodeType===F){if(i)return!1;n=o}else if(n=Be(o,i),n&&!g(t,n)&&(n=null),!n&&(n=ke(o,i),n.nodeType===F&&n.length))return!1;return we.currentNode=n,we.root=Ue(e,t),!we.previousNode()},Me=function(e,t){var n,o=e.endContainer,i=e.endOffset;if(we.root=null,o.nodeType===F){if(n=o.data.length,n&&i-1||!ie&&ve.call(i,"text/plain")>-1))return e.preventDefault(),void(d||this._config.usePlainTextOnPaste||!(r=a.getData("text/html"))?((r=a.getData("text/plain"))||(r=a.getData("text/uri-list")))&&this.insertPlainText(r,!0):this.insertHTML(r,!0));this._awaitingPaste=!0;var f=this._doc.body,p=this.getSelection(),g=p.startContainer,m=p.startOffset,v=p.endContainer,C=p.endOffset,_=this.createElement("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});f.appendChild(_),p.selectNodeContents(_),this.setSelection(p),setTimeout(function(){try{u._awaitingPaste=!1;for(var e,t,n="",o=_;_=o;)o=_.nextSibling,N(_),e=_.firstChild,e&&e===_.lastChild&&"DIV"===e.nodeName&&(_=e),n+=_.innerHTML;t=u._createRange(g,m,v,C),u.setSelection(t),n&&u.insertHTML(n,!0)}catch(e){u.didError(e)}},0)},ct=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()},ht=O.prototype,ut=function(e,t,n){var o=n._doc,i=e?DOMPurify.sanitize(e,{WHOLE_DOCUMENT:!1,RETURN_DOM:!0,RETURN_DOM_FRAGMENT:!0}):null;return i?o.importNode(i,!0):o.createDocumentFragment()};ht.setConfig=function(e){return e=A({blockTag:"DIV",blockAttributes:null,usePlainTextOnPaste:!1,tagAttributes:{blockquote:null,ul:null,ol:null,li:null,a:null},leafNodeNames:_e,undo:{documentSizeThreshold:-1,undoLimit:-1},isInsertedHTMLSanitized:!0,isSetHTMLSanitized:!0,sanitizeToDOMFragment:"undefined"!=typeof DOMPurify&&DOMPurify.isSupported?ut:null},e,!0),e.blockTag=e.blockTag.toUpperCase(),this._config=e,this},ht.createElement=function(e,t,n){return S(this._doc,e,t,n)},ht.createDefaultBlock=function(e){var t=this._config;return y(this.createElement(t.blockTag,t.blockAttributes,e),this._root)},ht.didError=function(e){console.log(e)},ht.getDocument=function(){return this._doc},ht.getRoot=function(){return this._root},ht.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 ft={pathChange:1,select:1,input:1,undoStateChange:1};ht.fireEvent=function(e,t){var n,o,i,r=this._events[e];if(/^(?:focus|blur)/.test(e))if(n=g(this._root,this._doc.activeElement),"focus"===e){if(!n||this._isFocused)return this;this._isFocused=!0}else{if(n||!this._isFocused)return this;this._isFocused=!1}if(r)for(t||(t={}),t.type!==e&&(t.type=e),r=r.slice(),o=r.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},ht.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},ht.handleEvent=function(e){this.fireEvent(e.type,e)},ht.addEventListener=function(e,t){var n=this._events[e],o=this._root;return t?(n||(n=this._events[e]=[],ft[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)},ht.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],ft[e]||("selectionchange"===e&&(i=this._doc),i.removeEventListener(e,this,!0)))}return this},ht._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},ht.getCursorPosition=function(e){if(!e&&!(e=this.getSelection())||!e.getBoundingClientRect)return null;var t,n,o=e.getBoundingClientRect();return o&&!o.top&&(this._ignoreChange=!0,t=this._doc.createElement("SPAN"),t.textContent=Y,xe(e,t),o=t.getBoundingClientRect(),n=t.parentNode,n.removeChild(t),k(n,e)),o},ht._moveCursorTo=function(e){var t=this._root,n=this._createRange(t,e?0:t.childNodes.length);return Re(n),this.setSelection(n),this},ht.moveCursorToStart=function(){return this._moveCursorTo(!0)},ht.moveCursorToEnd=function(){return this._moveCursorTo(!1)};var pt=function(e){return e._win.getSelection()||null};ht.setSelection=function(e){if(e)if(this._lastSelection=e,this._isFocused)if(ee&&!this._restoreSelection)D.call(this),this.blur(),this.focus();else{te&&this._win.focus();var t=pt(this);t&&(t.removeAllRanges(),t.addRange(e))}else D.call(this);return this},ht.getSelection=function(){var e,t,n,o=pt(this),r=this._root;return o&&o.rangeCount&&(e=o.getRangeAt(0).cloneRange(),t=e.startContainer,n=e.endContainer,t&&i(t)&&e.setStartBefore(t),n&&i(n)&&e.setEndBefore(n)),e&&g(r,e.commonAncestorContainer)?this._lastSelection=e:e=this._lastSelection,e||(e=this._createRange(r.firstChild,0)),e},ht.getSelectedText=function(){var e,t=this.getSelection(),o=new n(t.commonAncestorContainer,z|W,function(e){return De(t,e,!0)}),i=t.startContainer,r=t.endContainer,s=o.currentNode=i,d="",l=!1;for(o.filter(s)||(s=o.nextNode());s;)s.nodeType===F?(e=s.data,e&&/\S/.test(e)&&(s===r&&(e=e.slice(0,t.endOffset)),s===i&&(e=e.slice(t.startOffset)),d+=e,l=!0)):("BR"===s.nodeName||l&&!a(s))&&(d+="\n",l=!1),s=o.nextNode();return d},ht.getPath=function(){return this._path};var gt=function(e,t){for(var o,i,r,s=new n(e,z,function(){return!0},!1);i=s.nextNode();)for(;(r=i.data.indexOf(Y))>-1&&(!t||i.parentNode!==t);){if(1===i.length){do o=i.parentNode,o.removeChild(i),i=o,s.currentNode=o;while(a(i)&&!v(i));break}i.deleteData(r,1)}};ht._didAddZWS=function(){this._hasZWS=!0},ht._removeZWS=function(){this._hasZWS&&(gt(this._root),this._hasZWS=!1)},ht._updatePath=function(e,t){var n,o=e.startContainer,i=e.endContainer;(t||o!==this._lastAnchorNode||i!==this._lastFocusNode)&&(this._lastAnchorNode=o,this._lastFocusNode=i,n=o&&i?o===i?m(i,this._root):"(selection)":"",t||this._path===n||(this._path=n,this.fireEvent("pathChange",{path:n}))),e.collapsed||this.fireEvent("select")},ht._updatePathOnEvent=function(){var e=this;e._willUpdatePath||(e._willUpdatePath=!0,setTimeout(function(){e._willUpdatePath=!1,e._updatePath(e.getSelection())},0))},ht.focus=function(){return this._root.focus(),le&&this.fireEvent("focus"),this},ht.blur=function(){return this._root.blur(),le&&this.fireEvent("blur"),this};var mt="squire-selection-start",vt="squire-selection-end";ht._saveRangeToBookmark=function(e){var t,n=this.createElement("INPUT",{id:mt,type:"hidden"}),o=this.createElement("INPUT",{id:vt,type:"hidden"});xe(e,n),e.collapse(!1),xe(e,o),n.compareDocumentPosition(o)&I&&(n.id=vt,o.id=mt,t=n,n=o,o=t),e.setStartAfter(n),e.setEndBefore(o)},ht._getRangeAndRemoveBookmark=function(e){var t=this._root,n=t.querySelector("#"+mt),o=t.querySelector("#"+vt);if(n&&o){var i=n.parentNode,r=o.parentNode,a=ve.call(i.childNodes,n),s=ve.call(r.childNodes,o);i===r&&(s-=1),N(n),N(o),e||(e=this._doc.createRange()),e.setStart(i,a),e.setEnd(r,s),k(i,e),i!==r&&k(r,e),e.collapsed&&(i=e.startContainer,i.nodeType===F&&(r=i.childNodes[e.startOffset],r&&r.nodeType===F||(r=i.childNodes[e.startOffset-1]),r&&r.nodeType===F&&(e.setStart(r,0),e.collapse(!0))))}return e||null},ht._keyUpDetectChange=function(e){var t=e.keyCode;e.ctrlKey||e.metaKey||e.altKey||!(t<16||t>20)||!(t<33||t>45)||this._docWasChanged()},ht._docWasChanged=function(){if(ge&&(Ee=new WeakMap),!this._ignoreAllChanges){if(pe&&this._ignoreChange)return void(this._ignoreChange=!1);this._isInUndoState&&(this._isInUndoState=!1,this.fireEvent("undoStateChange",{ -canUndo:!0,canRedo:!1})),this.fireEvent("input")}},ht._recordUndoState=function(e){if(!this._isInUndoState){var t,n=this._undoIndex+=1,o=this._undoStack,i=this._config.undo,r=i.documentSizeThreshold,a=i.undoLimit;n-1&&2*t.length>r&&a>-1&&n>a&&(o.splice(0,n-a),n=this._undoIndex=a,this._undoStackLength=a),o[n]=t,this._undoStackLength+=1,this._isInUndoState=!0}},ht.saveUndoState=function(e){return e===t&&(e=this.getSelection()),this._isInUndoState||(this._recordUndoState(e),this._getRangeAndRemoveBookmark(e)),this},ht.undo=function(){if(0!==this._undoIndex||!this._isInUndoState){this._recordUndoState(this.getSelection()),this._undoIndex-=1,this._setHTML(this._undoStack[this._undoIndex]);var e=this._getRangeAndRemoveBookmark();e&&this.setSelection(e),this._isInUndoState=!0,this.fireEvent("undoStateChange",{canUndo:0!==this._undoIndex,canRedo:!0}),this.fireEvent("input")}return this},ht.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),i=this.createElement(e,t),C(h,i),i.appendChild(h));while(r.nextNode());d.nodeType!==F&&(h.nodeType===F?(d=h,c=h.length):(d=h.parentNode,c=1)),o=this._createRange(s,l,d,c)}return o},ht._removeFormat=function(e,t,n,o){this._saveRangeToBookmark(n);var i,r=this._doc;n.collapsed&&(ue?(i=r.createTextNode(Y),this._didAddZWS()):i=r.createTextNode(""),xe(n,i));for(var s=n.commonAncestorContainer;a(s);)s=s.parentNode;var d=n.startContainer,l=n.startOffset,c=n.endContainer,h=n.endOffset,u=[],p=function(e,t){if(!De(n,e,!1)){var o,i,r=e.nodeType===F;if(!De(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(o=e.firstChild;o;o=i)i=o.nextSibling,p(o,t)}},g=Array.prototype.filter.call(s.getElementsByTagName(e),function(o){return De(n,o,!0)&&f(o,e,t)});return o||g.forEach(function(e){p(e,e)}),u.forEach(function(e){var t=e[0].cloneNode(!1),n=e[1];C(n,t),t.appendChild(n)}),g.forEach(function(e){C(e,_(e))}),this._getRangeAndRemoveBookmark(n),i&&n.collapse(!1),k(s,n),n},ht.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),pe||this._docWasChanged(),this):this};var Nt={DT:"DD",DD:"DT",LI:"LI"},Ct=function(e,t,n,o){var i=Nt[t.nodeName],r=null,a=b(n,o,t.parentNode,e._root),s=e._config;return i||(i=s.blockTag,r=s.blockAttributes),f(a,i,r)||(t=S(a.ownerDocument,i,r),a.dir&&(t.dir=a.dir),C(a,t),t.appendChild(_(a)),a=t),a};ht.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var o=this._root,i=Ue(n,o),r=Ie(n,o);if(i&&r)do if(e(i)||i===r)break;while(i=h(i,o));return t&&(this.setSelection(n),this._updatePath(n,!0),pe||this._docWasChanged()),this},ht.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._isInUndoState?this._saveRangeToBookmark(t):this._recordUndoState(t);var n,o=this._root;return He(t,o),Pe(t,o,o,o),n=Le(t,o,o),xe(t,e.call(this,n)),t.endOffset]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))|([\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,}\b)/i,At=function(e,t,o){for(var i,r,a,s,d,l,c,h=e.ownerDocument,u=new n(e,z,function(e){return!p(e,t,"A")},!1),f=o._config.tagAttributes.a;i=u.nextNode();)for(r=i.data,a=i.parentNode;s=Lt.exec(r);)d=s.index,l=d+s[0].length,d&&(c=h.createTextNode(r.slice(0,d)),a.insertBefore(c,i)),c=o.createElement("A",A({href:s[1]?/^(?:ht|f)tps?:/.test(s[1])?s[1]:"http://"+s[1]:"mailto:"+s[2]},f,!1)),c.textContent=r.slice(d,l),a.insertBefore(c,i),i.data=r=r.slice(l)};ht.insertHTML=function(e,t){var n,o,i,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(""),o=e.lastIndexOf(""),n>-1&&o>-1&&(e=e.slice(n+20,o))),i=this.createElement("DIV"),i.innerHTML=e,r=f.createDocumentFragment(),r.appendChild(_(i))),this.saveUndoState(u);try{for(a=this._root,s=r,d={fragment:r,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},At(r,r,this),tt(r),rt(r,a,!1),nt(r),r.normalize();s=h(s,r);)y(s,a);t&&this.fireEvent("willPaste",d),d.defaultPrevented||(Oe(u,d.fragment,a),pe||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 Ot=function(e){return e.split("&").join("&").split("<").join("<").split(">").join(">").split('"').join(""")};ht.insertPlainText=function(e,t){var n,o,i,r,a=e.split("\n"),s=this._config,d=s.blockTag,l=s.blockAttributes,c="",h="<"+d;for(n in l)h+=" "+n+'="'+Ot(l[n])+'"';for(h+=">",o=0,i=a.length;o")+c),a[o]=r;return this.insertHTML(a.join(""),t)};var Dt=function(e,t,n){return function(){return this[e](t,n),this.focus()}};ht.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},ht.bold=Dt("changeFormat",{tag:"B"}),ht.italic=Dt("changeFormat",{tag:"I"}),ht.underline=Dt("changeFormat",{tag:"U"}),ht.strikethrough=Dt("changeFormat",{tag:"S"}),ht.subscript=Dt("changeFormat",{tag:"SUB"},{tag:"SUP"}),ht.superscript=Dt("changeFormat",{tag:"SUP"},{tag:"SUB"}),ht.removeBold=Dt("changeFormat",null,{tag:"B"}),ht.removeItalic=Dt("changeFormat",null,{tag:"I"}),ht.removeUnderline=Dt("changeFormat",null,{tag:"U"}),ht.removeStrikethrough=Dt("changeFormat",null,{tag:"S"}),ht.removeSubscript=Dt("changeFormat",null,{tag:"SUB"}),ht.removeSuperscript=Dt("changeFormat",null,{tag:"SUP"}),ht.makeLink=function(e,t){var n=this.getSelection();if(n.collapsed){var o=e.indexOf(":")+1;if(o)for(;"/"===e[o];)o+=1;xe(n,this._doc.createTextNode(e.slice(o)))}return t=A(A({href:e},t,!0),this._config.tagAttributes.a,!1),this.changeFormat({tag:"A",attributes:t},{tag:"A"},n),this.focus()},ht.removeLink=function(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0),this.focus()},ht.setFontFace=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{class:V,style:"font-family: "+e+", sans-serif;"}}:null,{tag:"SPAN",attributes:{class:V}}),this.focus()},ht.setFontSize=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{class:$,style:"font-size: "+("number"==typeof e?e+"px":e)}}:null,{tag:"SPAN",attributes:{class:$}}),this.focus()},ht.setTextColour=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{class:Q,style:"color:"+e}}:null,{tag:"SPAN",attributes:{class:Q}}),this.focus()},ht.setHighlightColour=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{class:j,style:"background-color:"+e}}:e,{tag:"SPAN",attributes:{class:j}}),this.focus()},ht.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()},ht.setTextDirection=function(e){return this.forEachBlock(function(t){e?t.dir=e:t.removeAttribute("dir")},!0),this.focus()},ht.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||(He(e,t),n=t),n.nodeType===F)return this;this.saveUndoState(e),Pe(e,n,n,t);for(var o,i,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;)o=p.nextSibling,h.appendChild(p),p=o;return U(this,h,u),u.normalize(),p=u.firstChild,o=u.lastChild,i=n.childNodes,p?(n.insertBefore(u,f),d=ve.call(i,p),c=ve.call(i,o)+1):(d=ve.call(i,f),c=d),e.setStart(n,d),e.setEnd(n,c),k(n,e),Re(e),this.setSelection(e),this._updatePath(e,!0),this.focus()},ht.increaseQuoteLevel=Dt("modifyBlocks",_t),ht.decreaseQuoteLevel=Dt("modifyBlocks",St),ht.makeUnorderedList=Dt("modifyBlocks",bt),ht.makeOrderedList=Dt("modifyBlocks",Et),ht.removeList=Dt("modifyBlocks",kt),ht.increaseListLevel=Dt("modifyBlocks",Bt),ht.decreaseListLevel=Dt("modifyBlocks",xt),O.isInline=a,O.isBlock=s,O.isContainer=d,O.getBlockWalker=l,O.getPreviousBlock=c,O.getNextBlock=h,O.areAlike=u,O.hasTagAttributes=f,O.getNearest=p,O.isOrContains=g,O.detach=N,O.replaceWith=C,O.empty=_,O.getNodeBefore=ke,O.getNodeAfter=Be,O.insertNodeInRange=xe,O.extractContentsOfRange=Le,O.deleteContentsOfRange=Ae,O.insertTreeFragmentIntoRange=Oe,O.isNodeContainedInRange=De,O.moveRangeBoundariesDownTree=Re,O.moveRangeBoundariesUpTree=Pe,O.getStartBlockOfRange=Ue,O.getEndBlockOfRange=Ie,O.contentWalker=we,O.rangeDoesStartAtBlockBoundary=Fe,O.rangeDoesEndAtBlockBoundary=Me,O.expandRangeToBlockBoundaries=He,O.onPaste=lt,O.addLinks=At,O.splitBlock=Ct,O.startSelectionId=mt,O.endSelectionId=vt,"object"==typeof exports?module.exports=O:"function"==typeof define&&define.amd?define(function(){return O}):(X.Squire=O,top!==X&&"true"===e.documentElement.getAttribute("data-squireinit")&&(X.editor=new O(e),X.onEditorLoad&&(X.onEditorLoad(X.editor),X.onEditorLoad=null)))}(document); \ No newline at end of file +!function(e,t){"use strict";function n(e,t,n){this.root=this.currentNode=e,this.nodeType=t,this.filter=n}function o(e,t){for(var n=e.length;n--;)if(!t(e[n]))return!1;return!0}function i(e){return e.nodeType===w&&!!Ce[e.nodeName]}function r(e){switch(e.nodeType){case F:return ye;case w:case H:if(ge&&Ee.has(e))return Ee.get(e);break;default:return Se}var t;return t=o(e.childNodes,a)?_e.test(e.nodeName)?ye:Te:be,ge&&Ee.set(e,t),t}function a(e){return r(e)===ye}function s(e){return r(e)===Te}function d(e){return r(e)===be}function l(e,t){var o=new n(t,W,s);return o.currentNode=e,o}function c(e,t){return e=l(e,t).previousNode(),e!==t?e:null}function h(e,t){return e=l(e,t).nextNode(),e!==t?e:null}function u(e,t){return!i(e)&&e.nodeType===t.nodeType&&e.nodeName===t.nodeName&&"A"!==e.nodeName&&e.className===t.className&&(!e.style&&!t.style||e.style.cssText===t.style.cssText)}function f(e,t,n){if(e.nodeName!==t)return!1;for(var o in n)if(e.getAttribute(o)!==n[o])return!1;return!0}function p(e,t,n,o){for(;e&&e!==t;){if(f(e,n,o))return e;e=e.parentNode}return null}function g(e,t){for(;t;){if(t===e)return!0;t=t.parentNode}return!1}function m(e,t){var n,o,i,r,a="";return e&&e!==t&&(a=m(e.parentNode,t),e.nodeType===w&&(a+=(a?">":"")+e.nodeName,(n=e.id)&&(a+="#"+n),(o=e.className.trim())&&(i=o.split(/\s\s*/),i.sort(),a+=".",a+=i.join(".")),(r=e.dir)&&(a+="[dir="+r+"]"),i&&(ve.call(i,j)>-1&&(a+="[backgroundColor="+e.style.backgroundColor.replace(/ /g,"")+"]"),ve.call(i,Q)>-1&&(a+="[color="+e.style.color.replace(/ /g,"")+"]"),ve.call(i,V)>-1&&(a+="[fontFamily="+e.style.fontFamily.replace(/ /g,"")+"]"),ve.call(i,$)>-1&&(a+="[fontSize="+e.style.fontSize+"]")))),a}function v(e){var t=e.nodeType;return t===w?e.childNodes.length:e.length||0}function N(e){var t=e.parentNode;return t&&t.removeChild(e),e}function _(e,t){var n=e.parentNode;n&&n.replaceChild(t,e)}function C(e){for(var t=e.ownerDocument.createDocumentFragment(),n=e.childNodes,o=n?n.length:0;o--;)t.appendChild(e.firstChild);return t}function S(e,n,o,i){var r,a,s,d,l=e.createElement(n);if(o instanceof Array&&(i=o,o=null),o)for(r in o)a=o[r],a!==t&&l.setAttribute(r,o[r]);if(i)for(s=0,d=i.length;ss?t.startOffset-=1:t.startOffset===s&&(t.startContainer=o,t.startOffset=v(o))),t.endContainer===e&&(t.endOffset>s?t.endOffset-=1:t.endOffset===s&&(t.endContainer=o,t.endOffset=v(o))),N(n),n.nodeType===F?o.appendData(n.data):d.push(C(n));else if(n.nodeType===w){for(i=d.length;i--;)n.appendChild(d.pop());E(n,t)}}function k(e,t){if(e.nodeType===F&&(e=e.parentNode),e.nodeType===w){var n={startContainer:t.startContainer,startOffset:t.startOffset,endContainer:t.endContainer,endOffset:t.endOffset};E(e,n),t.setStart(n.startContainer,n.startOffset),t.setEnd(n.endContainer,n.endOffset)}}function B(e,t,n){for(var o,i,r=t;1===r.parentNode.childNodes.length;)r=r.parentNode;N(r),i=e.childNodes.length,o=e.lastChild,o&&"BR"===o.nodeName&&(e.removeChild(o),i-=1),e.appendChild(C(t)),n.setStart(e,i),n.collapse(!0),k(e,n),ae&&(o=e.lastChild)&&"BR"===o.nodeName&&e.removeChild(o)}function x(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&&u(i,e)){if(!d(i)){if(!s)return;o=S(a,"DIV"),o.appendChild(C(i)),i.appendChild(o)}N(e),n=!d(e),i.appendChild(C(e)),n&&T(i,t),r&&x(r,t)}else s&&(i=S(a,"DIV"),e.insertBefore(i,r),y(i,t))}function L(e){this.isShiftDown=e.shiftKey}function A(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?A(e[o],i,n):i);return e}function O(e,t){e.nodeType===M&&(e=e.body);var n,o=e.ownerDocument,i=o.defaultView;this._win=i,this._doc=o,this._root=e,this._events={},this._isFocused=!1,this._lastSelection=null,fe&&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,pe?(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",R),this.addEventListener("touchstart",R),this.addEventListener("focus",P),this._awaitingPaste=!1,this.addEventListener(re?"beforecut":"cut",st),this.addEventListener("copy",dt),this.addEventListener("keydown",L),this.addEventListener("keyup",L),this.addEventListener(re?"beforepaste":"paste",lt),this.addEventListener("drop",ct),this.addEventListener(ae?"keypress":"keydown",ze),this._keyHandlers=Object.create(Ze),this.setConfig(t),re&&(i.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("")}function D(e){var t=this;setTimeout(function(){document.activeElement;g(t._root,document.activeElement)||(this._restoreSelection=!0)},1)}function R(){this._restoreSelection=!1}function P(){this._restoreSelection&&this.setSelection(this._lastSelection)}function U(e,t,n){var o,i;for(o=t.firstChild;o;o=i){if(i=o.nextSibling,a(o)){if(o.nodeType===F||"BR"===o.nodeName||"IMG"===o.nodeName){n.appendChild(o);continue}}else if(s(o)){n.appendChild(e.createDefaultBlock([U(e,o,e._doc.createDocumentFragment())]));continue}U(e,o,n)}return n}var I=2,w=1,F=3,M=9,H=11,W=1,z=4,q=0,K=1,G=2,Z=3,j="highlight",Q="colour",V="font",$="size",Y="​",X=e.defaultView,J=navigator.userAgent,ee=/Android/.test(J),te=/iP(?:ad|hone|od)/.test(J),ne=/Mac OS X/.test(J),oe=/Windows NT/.test(J),ie=/Gecko\//.test(J),re=/Trident\/[456]\./.test(J),ae=!!X.opera,se=/Edge\//.test(J),de=!se&&/WebKit\//.test(J),le=/Trident\/[4567]\./.test(J),ce=ne?"meta-":"ctrl-",he=re||ae,ue=re||de,fe=re,pe="undefined"!=typeof MutationObserver,ge="undefined"!=typeof WeakMap,me=/[^ \t\r\n]/,ve=Array.prototype.indexOf;Object.create||(Object.create=function(e){var t=function(){};return t.prototype=e,new t});var Ne={1:1,2:2,3:4,8:128,9:256,11:1024};n.prototype.nextNode=function(){for(var e,t=this.currentNode,n=this.root,o=this.nodeType,i=this.filter;;){for(e=t.firstChild;!e&&t&&t!==n;)e=t.nextSibling,e||(t=t.parentNode);if(!e)return null;if(Ne[e.nodeType]&o&&i(e))return this.currentNode=e,e;t=e}},n.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(Ne[e.nodeType]&o&&i(e))return this.currentNode=e,e;t=e}},n.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,e||(t=t.parentNode);if(!e)return null;if(Ne[e.nodeType]&o&&i(e))return this.currentNode=e,e;t=e}};var _e=/^(?:#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)$/,Ce={BR:1,HR:1,IFRAME:1,IMG:1,INPUT:1},Se=0,ye=1,Te=2,be=3,Ee=ge?new WeakMap:null,ke=function(e,t){for(var n=e.childNodes;t&&e.nodeType===w;)e=n[t-1],n=e.childNodes,t=n.length;return e},Be=function(e,t){if(e.nodeType===w){var n=e.childNodes;if(t-1,r=e.compareBoundaryPoints(K,o)<1;return!i&&!r}var a=e.compareBoundaryPoints(q,o)<1,s=e.compareBoundaryPoints(G,o)>-1;return a&&s},Re=function(e){for(var t,n=e.startContainer,o=e.startOffset,r=e.endContainer,a=e.endOffset,s=!0;n.nodeType!==F&&(t=n.childNodes[o],t&&!i(t));)n=t,o=0;if(a)for(;r.nodeType!==F;){if(t=r.childNodes[a-1],!t||i(t)){if(s&&t&&"BR"===t.nodeName){a-=1,s=!1;continue}break}r=t,a=v(r)}else for(;r.nodeType!==F&&(t=r.firstChild,t&&!i(t));)r=t;e.collapsed?(e.setStart(r,a),e.setEnd(n,o)):(e.setStart(n,o),e.setEnd(r,a))},Pe=function(e,t,n,o){var i,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!==o;)i=r.parentNode,a=ve.call(i.childNodes,r),r=i;for(;;){if(l&&s.nodeType!==F&&s.childNodes[d]&&"BR"===s.childNodes[d].nodeName&&(d+=1,l=!1),s===n||s===o||d!==v(s))break;i=s.parentNode,d=ve.call(i.childNodes,s)+1,s=i}e.setStart(r,a),e.setEnd(s,d)},Ue=function(e,t){var n,o=e.startContainer;return a(o)?n=c(o,t):o!==t&&s(o)?n=o:(n=ke(o,e.startOffset),n=h(n,t)),n&&De(e,n,!0)?n:null},Ie=function(e,t){var n,o,i=e.endContainer;if(a(i))n=c(i,t);else if(i!==t&&s(i))n=i;else{if(n=Be(i,e.endOffset),!n||!g(t,n))for(n=t;o=n.lastChild;)n=o;n=c(n,t)}return n&&De(e,n,!0)?n:null},we=new n(null,z|W,function(e){return e.nodeType===F?me.test(e.data):"IMG"===e.nodeName}),Fe=function(e,t){var n,o=e.startContainer,i=e.startOffset;if(we.root=null,o.nodeType===F){if(i)return!1;n=o}else if(n=Be(o,i),n&&!g(t,n)&&(n=null),!n&&(n=ke(o,i),n.nodeType===F&&n.length))return!1;return we.currentNode=n,we.root=Ue(e,t),!we.previousNode()},Me=function(e,t){var n,o=e.endContainer,i=e.endOffset;if(we.root=null,o.nodeType===F){if(n=o.data.length,n&&i-1||!ie&&ve.call(i,"text/plain")>-1))return e.preventDefault(),void(d||this._config.usePlainTextOnPaste||!(r=a.getData("text/html"))?((r=a.getData("text/plain"))||(r=a.getData("text/uri-list")))&&this.insertPlainText(r,!0):this.insertHTML(r,!0));this._awaitingPaste=!0;var f=this._doc.body,p=this.getSelection(),g=p.startContainer,m=p.startOffset,v=p.endContainer,_=p.endOffset,C=this.createElement("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});f.appendChild(C),p.selectNodeContents(C),this.setSelection(p),setTimeout(function(){try{u._awaitingPaste=!1;for(var e,t,n="",o=C;C=o;)o=C.nextSibling,N(C),e=C.firstChild,e&&e===C.lastChild&&"DIV"===e.nodeName&&(C=e),n+=C.innerHTML;t=u._createRange(g,m,v,_),u.setSelection(t),n&&u.insertHTML(n,!0)}catch(e){u.didError(e)}},0)},ct=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()},ht=O.prototype,ut=function(e,t,n){var o=n._doc,i=e?DOMPurify.sanitize(e,{WHOLE_DOCUMENT:!1,RETURN_DOM:!0,RETURN_DOM_FRAGMENT:!0}):null;return i?o.importNode(i,!0):o.createDocumentFragment()};ht.setConfig=function(e){return e=A({blockTag:"DIV",blockAttributes:null,usePlainTextOnPaste:!1,tagAttributes:{blockquote:null,ul:null,ol:null,li:null,a:null},leafNodeNames:Ce,undo:{documentSizeThreshold:-1,undoLimit:-1},isInsertedHTMLSanitized:!0,isSetHTMLSanitized:!0,sanitizeToDOMFragment:"undefined"!=typeof DOMPurify&&DOMPurify.isSupported?ut:null},e,!0),e.blockTag=e.blockTag.toUpperCase(),this._config=e,this},ht.createElement=function(e,t,n){return S(this._doc,e,t,n)},ht.createDefaultBlock=function(e){var t=this._config;return y(this.createElement(t.blockTag,t.blockAttributes,e),this._root)},ht.didError=function(e){console.log(e)},ht.getDocument=function(){return this._doc},ht.getRoot=function(){return this._root},ht.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 ft={pathChange:1,select:1,input:1,undoStateChange:1};ht.fireEvent=function(e,t){var n,o,i,r=this._events[e];if(/^(?:focus|blur)/.test(e))if(n=g(this._root,this._doc.activeElement),"focus"===e){if(!n||this._isFocused)return this;this._isFocused=!0}else{if(n||!this._isFocused)return this;this._isFocused=!1}if(r)for(t||(t={}),t.type!==e&&(t.type=e),r=r.slice(),o=r.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},ht.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},ht.handleEvent=function(e){this.fireEvent(e.type,e)},ht.addEventListener=function(e,t){var n=this._events[e],o=this._root;return t?(n||(n=this._events[e]=[],ft[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)},ht.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],ft[e]||("selectionchange"===e&&(i=this._doc),i.removeEventListener(e,this,!0)))}return this},ht._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},ht.getCursorPosition=function(e){if(!e&&!(e=this.getSelection())||!e.getBoundingClientRect)return null;var t,n,o=e.getBoundingClientRect();return o&&!o.top&&(this._ignoreChange=!0,t=this._doc.createElement("SPAN"),t.textContent=Y,xe(e,t),o=t.getBoundingClientRect(),n=t.parentNode,n.removeChild(t),k(n,e)),o},ht._moveCursorTo=function(e){var t=this._root,n=this._createRange(t,e?0:t.childNodes.length);return Re(n),this.setSelection(n),this},ht.moveCursorToStart=function(){return this._moveCursorTo(!0)},ht.moveCursorToEnd=function(){return this._moveCursorTo(!1)};var pt=function(e){return e._win.getSelection()||null};ht.setSelection=function(e){if(e)if(this._lastSelection=e,this._isFocused)if(ee&&!this._restoreSelection)D.call(this),this.blur(),this.focus();else{te&&this._win.focus();var t=pt(this);t&&(t.removeAllRanges(),t.addRange(e))}else D.call(this);return this},ht.getSelection=function(){var e,t,n,o=pt(this),r=this._root;return o&&o.rangeCount&&(e=o.getRangeAt(0).cloneRange(),t=e.startContainer,n=e.endContainer,t&&i(t)&&e.setStartBefore(t),n&&i(n)&&e.setEndBefore(n)),e&&g(r,e.commonAncestorContainer)?this._lastSelection=e:e=this._lastSelection,e||(e=this._createRange(r.firstChild,0)),e},ht.getSelectedText=function(){var e,t=this.getSelection(),o=new n(t.commonAncestorContainer,z|W,function(e){return De(t,e,!0)}),i=t.startContainer,r=t.endContainer,s=o.currentNode=i,d="",l=!1;for(o.filter(s)||(s=o.nextNode());s;)s.nodeType===F?(e=s.data,e&&/\S/.test(e)&&(s===r&&(e=e.slice(0,t.endOffset)),s===i&&(e=e.slice(t.startOffset)),d+=e,l=!0)):("BR"===s.nodeName||l&&!a(s))&&(d+="\n",l=!1),s=o.nextNode();return d},ht.getPath=function(){return this._path};var gt=function(e,t){for(var o,i,r,s=new n(e,z,function(){return!0},!1);i=s.nextNode();)for(;(r=i.data.indexOf(Y))>-1&&(!t||i.parentNode!==t);){if(1===i.length){do o=i.parentNode,o.removeChild(i),i=o,s.currentNode=o;while(a(i)&&!v(i));break}i.deleteData(r,1)}};ht._didAddZWS=function(){this._hasZWS=!0},ht._removeZWS=function(){this._hasZWS&&(gt(this._root),this._hasZWS=!1)},ht._updatePath=function(e,t){var n,o=e.startContainer,i=e.endContainer;(t||o!==this._lastAnchorNode||i!==this._lastFocusNode)&&(this._lastAnchorNode=o,this._lastFocusNode=i,n=o&&i?o===i?m(i,this._root):"(selection)":"",t||this._path===n||(this._path=n,this.fireEvent("pathChange",{path:n}))),e.collapsed||this.fireEvent("select")},ht._updatePathOnEvent=function(){var e=this;e._willUpdatePath||(e._willUpdatePath=!0,setTimeout(function(){e._willUpdatePath=!1,e._updatePath(e.getSelection())},0))},ht.focus=function(){return this._root.focus(),le&&this.fireEvent("focus"),this},ht.blur=function(){return this._root.blur(),le&&this.fireEvent("blur"),this};var mt="squire-selection-start",vt="squire-selection-end";ht._saveRangeToBookmark=function(e){var t,n=this.createElement("INPUT",{id:mt,type:"hidden"}),o=this.createElement("INPUT",{id:vt,type:"hidden"});xe(e,n),e.collapse(!1),xe(e,o),n.compareDocumentPosition(o)&I&&(n.id=vt,o.id=mt,t=n,n=o,o=t),e.setStartAfter(n),e.setEndBefore(o)},ht._getRangeAndRemoveBookmark=function(e){var t=this._root,n=t.querySelector("#"+mt),o=t.querySelector("#"+vt);if(n&&o){var i=n.parentNode,r=o.parentNode,a=ve.call(i.childNodes,n),s=ve.call(r.childNodes,o);i===r&&(s-=1),N(n),N(o),e||(e=this._doc.createRange()),e.setStart(i,a),e.setEnd(r,s),k(i,e),i!==r&&k(r,e),e.collapsed&&(i=e.startContainer,i.nodeType===F&&(r=i.childNodes[e.startOffset],r&&r.nodeType===F||(r=i.childNodes[e.startOffset-1]),r&&r.nodeType===F&&(e.setStart(r,0),e.collapse(!0))))}return e||null},ht._keyUpDetectChange=function(e){var t=e.keyCode;e.ctrlKey||e.metaKey||e.altKey||!(t<16||t>20)||!(t<33||t>45)||this._docWasChanged()},ht._docWasChanged=function(){if(ge&&(Ee=new WeakMap),!this._ignoreAllChanges){if(pe&&this._ignoreChange)return void(this._ignoreChange=!1); +this._isInUndoState&&(this._isInUndoState=!1,this.fireEvent("undoStateChange",{canUndo:!0,canRedo:!1})),this.fireEvent("input")}},ht._recordUndoState=function(e){if(!this._isInUndoState){var t,n=this._undoIndex+=1,o=this._undoStack,i=this._config.undo,r=i.documentSizeThreshold,a=i.undoLimit;n-1&&2*t.length>r&&a>-1&&n>a&&(o.splice(0,n-a),n=this._undoIndex=a,this._undoStackLength=a),o[n]=t,this._undoStackLength+=1,this._isInUndoState=!0}},ht.saveUndoState=function(e){return e===t&&(e=this.getSelection()),this._isInUndoState||(this._recordUndoState(e),this._getRangeAndRemoveBookmark(e)),this},ht.undo=function(){if(0!==this._undoIndex||!this._isInUndoState){this._recordUndoState(this.getSelection()),this._undoIndex-=1,this._setHTML(this._undoStack[this._undoIndex]);var e=this._getRangeAndRemoveBookmark();e&&this.setSelection(e),this._isInUndoState=!0,this.fireEvent("undoStateChange",{canUndo:0!==this._undoIndex,canRedo:!0}),this.fireEvent("input")}return this},ht.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),i=this.createElement(e,t),_(h,i),i.appendChild(h));while(r.nextNode());d.nodeType!==F&&(h.nodeType===F?(d=h,c=h.length):(d=h.parentNode,c=1)),o=this._createRange(s,l,d,c)}return o},ht._removeFormat=function(e,t,n,o){this._saveRangeToBookmark(n);var i,r=this._doc;n.collapsed&&(ue?(i=r.createTextNode(Y),this._didAddZWS()):i=r.createTextNode(""),xe(n,i));for(var s=n.commonAncestorContainer;a(s);)s=s.parentNode;var d=n.startContainer,l=n.startOffset,c=n.endContainer,h=n.endOffset,u=[],p=function(e,t){if(!De(n,e,!1)){var o,i,r=e.nodeType===F;if(!De(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(o=e.firstChild;o;o=i)i=o.nextSibling,p(o,t)}},g=Array.prototype.filter.call(s.getElementsByTagName(e),function(o){return De(n,o,!0)&&f(o,e,t)});return o||g.forEach(function(e){p(e,e)}),u.forEach(function(e){var t=e[0].cloneNode(!1),n=e[1];_(n,t),t.appendChild(n)}),g.forEach(function(e){_(e,C(e))}),this._getRangeAndRemoveBookmark(n),i&&n.collapse(!1),k(s,n),n},ht.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),pe||this._docWasChanged(),this):this};var Nt={DT:"DD",DD:"DT",LI:"LI"},_t=function(e,t,n,o){var i=Nt[t.nodeName],r=null,a=b(n,o,t.parentNode,e._root),s=e._config;return i||(i=s.blockTag,r=s.blockAttributes),f(a,i,r)||(t=S(a.ownerDocument,i,r),a.dir&&(t.dir=a.dir),_(a,t),t.appendChild(C(a)),a=t),a};ht.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var o=this._root,i=Ue(n,o),r=Ie(n,o);if(i&&r)do if(e(i)||i===r)break;while(i=h(i,o));return t&&(this.setSelection(n),this._updatePath(n,!0),pe||this._docWasChanged()),this},ht.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._isInUndoState?this._saveRangeToBookmark(t):this._recordUndoState(t);var n,o=this._root;return He(t,o),Pe(t,o,o,o),n=Le(t,o,o),xe(t,e.call(this,n)),t.endOffset]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))|([\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,}\b)/i,At=function(e,t,o){for(var i,r,a,s,d,l,c,h=e.ownerDocument,u=new n(e,z,function(e){return!p(e,t,"A")},!1),f=o._config.tagAttributes.a;i=u.nextNode();)for(r=i.data,a=i.parentNode;s=Lt.exec(r);)d=s.index,l=d+s[0].length,d&&(c=h.createTextNode(r.slice(0,d)),a.insertBefore(c,i)),c=o.createElement("A",A({href:s[1]?/^(?:ht|f)tps?:/.test(s[1])?s[1]:"http://"+s[1]:"mailto:"+s[2]},f,!1)),c.textContent=r.slice(d,l),a.insertBefore(c,i),i.data=r=r.slice(l)};ht.insertHTML=function(e,t){var n,o,i,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(""),o=e.lastIndexOf(""),n>-1&&o>-1&&(e=e.slice(n+20,o))),i=this.createElement("DIV"),i.innerHTML=e,r=f.createDocumentFragment(),r.appendChild(C(i))),this.saveUndoState(u);try{for(a=this._root,s=r,d={fragment:r,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},At(r,r,this),tt(r),rt(r,a,!1),nt(r),r.normalize();s=h(s,r);)y(s,a);t&&this.fireEvent("willPaste",d),d.defaultPrevented||(Oe(u,d.fragment,a),pe||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 Ot=function(e){return e.split("&").join("&").split("<").join("<").split(">").join(">").split('"').join(""")};ht.insertPlainText=function(e,t){var n,o,i,r,a=e.split("\n"),s=this._config,d=s.blockTag,l=s.blockAttributes,c="",h="<"+d;for(n in l)h+=" "+n+'="'+Ot(l[n])+'"';for(h+=">",o=0,i=a.length;o")+c),a[o]=r;return this.insertHTML(a.join(""),t)};var Dt=function(e,t,n){return function(){return this[e](t,n),this.focus()}};ht.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},ht.bold=Dt("changeFormat",{tag:"B"}),ht.italic=Dt("changeFormat",{tag:"I"}),ht.underline=Dt("changeFormat",{tag:"U"}),ht.strikethrough=Dt("changeFormat",{tag:"S"}),ht.subscript=Dt("changeFormat",{tag:"SUB"},{tag:"SUP"}),ht.superscript=Dt("changeFormat",{tag:"SUP"},{tag:"SUB"}),ht.removeBold=Dt("changeFormat",null,{tag:"B"}),ht.removeItalic=Dt("changeFormat",null,{tag:"I"}),ht.removeUnderline=Dt("changeFormat",null,{tag:"U"}),ht.removeStrikethrough=Dt("changeFormat",null,{tag:"S"}),ht.removeSubscript=Dt("changeFormat",null,{tag:"SUB"}),ht.removeSuperscript=Dt("changeFormat",null,{tag:"SUP"}),ht.makeLink=function(e,t){var n=this.getSelection();if(n.collapsed){var o=e.indexOf(":")+1;if(o)for(;"/"===e[o];)o+=1;xe(n,this._doc.createTextNode(e.slice(o)))}return t=A(A({href:e},t,!0),this._config.tagAttributes.a,!1),this.changeFormat({tag:"A",attributes:t},{tag:"A"},n),this.focus()},ht.removeLink=function(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0),this.focus()},ht.setFontFace=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{class:V,style:"font-family: "+e+", sans-serif;"}}:null,{tag:"SPAN",attributes:{class:V}}),this.focus()},ht.setFontSize=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{class:$,style:"font-size: "+("number"==typeof e?e+"px":e)}}:null,{tag:"SPAN",attributes:{class:$}}),this.focus()},ht.setTextColour=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{class:Q,style:"color:"+e}}:null,{tag:"SPAN",attributes:{class:Q}}),this.focus()},ht.setHighlightColour=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{class:j,style:"background-color:"+e}}:e,{tag:"SPAN",attributes:{class:j}}),this.focus()},ht.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()},ht.setTextDirection=function(e){return this.forEachBlock(function(t){e?t.dir=e:t.removeAttribute("dir")},!0),this.focus()},ht.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||(He(e,t),n=t),n.nodeType===F)return this;this.saveUndoState(e),Pe(e,n,n,t);for(var o,i,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;)o=p.nextSibling,h.appendChild(p),p=o;return U(this,h,u),u.normalize(),p=u.firstChild,o=u.lastChild,i=n.childNodes,p?(n.insertBefore(u,f),d=ve.call(i,p),c=ve.call(i,o)+1):(d=ve.call(i,f),c=d),e.setStart(n,d),e.setEnd(n,c),k(n,e),Re(e),this.setSelection(e),this._updatePath(e,!0),this.focus()},ht.increaseQuoteLevel=Dt("modifyBlocks",Ct),ht.decreaseQuoteLevel=Dt("modifyBlocks",St),ht.makeUnorderedList=Dt("modifyBlocks",bt),ht.makeOrderedList=Dt("modifyBlocks",Et),ht.removeList=Dt("modifyBlocks",kt),ht.increaseListLevel=Dt("modifyBlocks",Bt),ht.decreaseListLevel=Dt("modifyBlocks",xt),O.isInline=a,O.isBlock=s,O.isContainer=d,O.getBlockWalker=l,O.getPreviousBlock=c,O.getNextBlock=h,O.areAlike=u,O.hasTagAttributes=f,O.getNearest=p,O.isOrContains=g,O.detach=N,O.replaceWith=_,O.empty=C,O.getNodeBefore=ke,O.getNodeAfter=Be,O.insertNodeInRange=xe,O.extractContentsOfRange=Le,O.deleteContentsOfRange=Ae,O.insertTreeFragmentIntoRange=Oe,O.isNodeContainedInRange=De,O.moveRangeBoundariesDownTree=Re,O.moveRangeBoundariesUpTree=Pe,O.getStartBlockOfRange=Ue,O.getEndBlockOfRange=Ie,O.contentWalker=we,O.rangeDoesStartAtBlockBoundary=Fe,O.rangeDoesEndAtBlockBoundary=Me,O.expandRangeToBlockBoundaries=He,O.onPaste=lt,O.addLinks=At,O.splitBlock=_t,O.startSelectionId=mt,O.endSelectionId=vt,"object"==typeof exports?module.exports=O:"function"==typeof define&&define.amd?define(function(){return O}):(X.Squire=O,top!==X&&"true"===e.documentElement.getAttribute("data-squireinit")&&(X.editor=new O(e),X.onEditorLoad&&(X.onEditorLoad(X.editor),X.onEditorLoad=null)))}(document); \ No newline at end of file diff --git a/source/Editor.js b/source/Editor.js index e804fe8..0e39140 100644 --- a/source/Editor.js +++ b/source/Editor.js @@ -488,8 +488,18 @@ proto.getSelection = function () { return selection; }; -function enableRestoreSelection () { - this._restoreSelection = true; +function enableRestoreSelection (ev) { + // Blur can be called when you are focusing on other elements within the + // editor. We should only want to restore the selection if we are coming + // from outside of the editor. We need to do this on a timeout because we + // cannot determine what the new focused element is until after the blur. + var self = this; + setTimeout(function() { + var target = document.activeElement; + if( !isOrContains( self._root, document.activeElement ) ) { + this._restoreSelection = true; + } + }, 1); } function disableRestoreSelection () { this._restoreSelection = false;