From a5d4bfb7d6eb38776377edaeea172f3769084327 Mon Sep 17 00:00:00 2001 From: Andy Kauffman Date: Mon, 25 Jul 2016 12:24:51 -0400 Subject: [PATCH] Remove Unneeded ZWS ZWS are added when formats are added, however these are not cleaned up if the format is changed. Now they will be cleaned up if they only contain ZWS and no text. --- build/squire-raw.js | 18 ++++++++++++++++-- build/squire.js | 4 ++-- source/Editor.js | 18 ++++++++++++++++-- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/build/squire-raw.js b/build/squire-raw.js index fa0090c..62e0ff1 100644 --- a/build/squire-raw.js +++ b/build/squire-raw.js @@ -2856,13 +2856,18 @@ proto.getPath = function () { // WebKit bug: https://bugs.webkit.org/show_bug.cgi?id=15256 -var removeZWS = function ( root ) { +// Walk down the tree starting at the root and remove any ZWS. If the node only contained +// ZWS space then remove it too. +// +// We may want to keep one ZWS node at the bottom of the tree so the block can be selected. Define that +// node as the keepNode. +var removeZWS = function ( root, keepNode ) { var walker = new TreeWalker( root, SHOW_TEXT, function () { return true; }, false ), parent, node, index; while ( node = walker.nextNode() ) { - while ( ( index = node.data.indexOf( ZWS ) ) > -1 ) { + while ( ( index = node.data.indexOf( ZWS ) ) > -1 && node.parentNode !== keepNode ) { if ( node.length === 1 ) { do { parent = node.parentNode; @@ -3267,6 +3272,15 @@ proto._addFormat = function ( tag, attributes, range ) { insertNodeInRange( range, el ); range.setStart( el.firstChild, el.firstChild.length ); range.collapse( true ); + + // Clean up any previous formats that may have been set on this block + // that are unused. + var block = el; + while ( isInline( block ) ) { + block = block.parentNode; + } + + removeZWS( block, el ); } // Otherwise we find all the textnodes in the range (splitting // partially selected nodes) and if they're not already formatted diff --git a/build/squire.js b/build/squire.js index 794a6d2..185a112 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===I&&!!mt[e.nodeName]}function r(e){return gt.test(e.nodeName)}function a(e){var t=e.nodeType;return(t===I||t===M)&&!r(e)&&o(e.childNodes,r)}function s(e){var t=e.nodeType;return!(t!==I&&t!==M||r(e)||a(e))}function d(e,t){var o=new n(t,H,a);return o.currentNode=e,o}function l(e,t){return e=d(e,t).previousNode(),e!==t?e:null}function c(e,t){return e=d(e,t).nextNode(),e!==t?e:null}function h(e,t){return!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 u(e,t,n){if(e.nodeName!==t)return!1;for(var o in n)if(e.getAttribute(o)!==n[o])return!1;return!0}function f(e,t,n,o){for(;e&&e!==t;){if(u(e,n,o))return e;e=e.parentNode}return null}function p(e,t){for(;t;){if(t===e)return!0;t=t.parentNode}return!1}function g(e,t){var n,o,i,r,a="";return e&&e!==t&&(a=g(e.parentNode,t),e.nodeType===I&&(a+=(a?">":"")+e.nodeName,(n=e.id)&&(a+="#"+n),(o=e.className.trim())&&(i=o.split(/\s\s*/),i.sort(),a+=".",a+=i.join(".")),(r=e.dir)&&(a+="[dir="+r+"]"),i&&(ft.call(i,G)>-1&&(a+="[backgroundColor="+e.style.backgroundColor.replace(/ /g,"")+"]"),ft.call(i,j)>-1&&(a+="[color="+e.style.color.replace(/ /g,"")+"]"),ft.call(i,Q)>-1&&(a+="[fontFamily="+e.style.fontFamily.replace(/ /g,"")+"]"),ft.call(i,V)>-1&&(a+="[fontSize="+e.style.fontSize+"]")))),a}function m(e){var t=e.nodeType;return t===I?e.childNodes.length:e.length||0}function v(e){var t=e.parentNode;return t&&t.removeChild(e),e}function C(e,t){var n=e.parentNode;n&&n.replaceChild(t,e)}function _(e){for(var t=e.ownerDocument.createDocumentFragment(),n=e.childNodes,o=n?n.length:0;o--;)t.appendChild(e.firstChild);return t}function N(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;d>s;s+=1)l.appendChild(i[s]);return l}function S(e,t){var n,o,a=e.ownerDocument,s=e;if(e===t&&((o=e.firstChild)&&"BR"!==o.nodeName||(n=L(a).createDefaultBlock(),o?e.replaceChild(n,o):e.appendChild(n),e=n,n=null)),e.nodeType===w)return s;if(r(e)){for(o=e.firstChild;lt&&o&&o.nodeType===w&&!o.data;)e.removeChild(o),o=e.firstChild;o||(lt?(n=a.createTextNode($),L(a)._didAddZWS()):n=a.createTextNode(""))}else if(dt){for(;e.nodeType!==w&&!i(e);){if(o=e.firstChild,!o){n=a.createTextNode("");break}e=o}e.nodeType===w?/^ +$/.test(e.data)&&(e.data=""):i(e)&&e.parentNode.insertBefore(a.createTextNode(""),e)}else if(!e.querySelector("BR"))for(n=N(a,"BR");(o=e.lastElementChild)&&!r(o);)e=o;if(n)try{e.appendChild(n)}catch(d){L(a).didError({name:"Squire: fixCursor – "+d,message:"Parent: "+e.nodeName+"/"+e.innerHTML+" appendChild: "+n.nodeName})}return s}function y(e,t){var n,o,i,a,d=e.childNodes,l=e.ownerDocument,c=null,h=L(l)._config;for(n=0,o=d.length;o>n;n+=1)i=d[n],a="BR"===i.nodeName,!a&&r(i)?(c||(c=N(l,h.blockTag,h.blockAttributes)),c.appendChild(i),n-=1,o-=1):(a||c)&&(c||(c=N(l,h.blockTag,h.blockAttributes)),S(c,t),a?e.replaceChild(c,i):(e.insertBefore(c,i),n+=1,o+=1),c=null),s(i)&&y(i,t);return c&&e.appendChild(S(c,t)),e}function T(e,t,n,o){var i,r,a,s=e.nodeType;if(s===w&&e!==n)return T(e.parentNode,e.splitText(t),n,o);if(s===I){if("number"==typeof t&&(t=ts?t.startOffset-=1:t.startOffset===s&&(t.startContainer=o,t.startOffset=m(o))),t.endContainer===e&&(t.endOffset>s?t.endOffset-=1:t.endOffset===s&&(t.endContainer=o,t.endOffset=m(o))),v(n),n.nodeType===w?o.appendData(n.data):d.push(_(n));else if(n.nodeType===I){for(i=d.length;i--;)n.appendChild(d.pop());b(n,t)}}function E(e,t){if(e.nodeType===w&&(e=e.parentNode),e.nodeType===I){var n={startContainer:t.startContainer,startOffset:t.startOffset,endContainer:t.endContainer,endOffset:t.endOffset};b(e,n),t.setStart(n.startContainer,n.startOffset),t.setEnd(n.endContainer,n.endOffset)}}function x(e,t,n){for(var o,i,r=t;1===r.parentNode.childNodes.length;)r=r.parentNode;v(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),E(e,n),it&&(o=e.lastChild)&&"BR"===o.nodeName&&e.removeChild(o)}function k(e,t){var n,o,i=e.previousSibling,r=e.firstChild,a=e.ownerDocument,d="LI"===e.nodeName;if(!d||r&&/^[OU]L$/.test(r.nodeName))if(i&&h(i,e)){if(!s(i)){if(!d)return;o=N(a,"DIV"),o.appendChild(_(i)),i.appendChild(o)}v(e),n=!s(e),i.appendChild(_(e)),n&&y(i,t),r&&k(r,t)}else d&&(i=N(a,"DIV"),e.insertBefore(i,r),S(i,t))}function L(e){for(var t,n=tn.length;n--;)if(t=tn[n],t._doc===e)return t;return null}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 B(e,t){e.nodeType===F&&(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,ct&&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,ht?(n=new MutationObserver(this._docWasChanged.bind(this)),n.observe(e,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._mutation=n):this.addEventListener("keyup",this._keyUpDetectChange),this._restoreSelection=!1,this.addEventListener("blur",O),this.addEventListener("mousedown",D),this.addEventListener("touchstart",D),this.addEventListener("focus",R),this._awaitingPaste=!1,this.addEventListener(ot?"beforecut":"cut",Yt),this.addEventListener("copy",Xt),this.addEventListener(ot?"beforepaste":"paste",Jt),this.addEventListener("drop",en),this.addEventListener(it?"keypress":"keydown",Rt),this._keyHandlers=Object.create(wt),this.setConfig(t),ot&&(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(r){}tn.push(this),this.setHTML("")}function O(){this._restoreSelection=!0}function D(){this._restoreSelection=!1}function R(){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,r(o)){if(o.nodeType===w||"BR"===o.nodeName||"IMG"===o.nodeName){n.appendChild(o);continue}}else if(a(o)){n.appendChild(e.createDefaultBlock([U(e,o,e._doc.createDocumentFragment())]));continue}U(e,o,n)}return n}var P=2,I=1,w=3,F=9,M=11,H=1,z=4,W=0,q=1,K=2,Z=3,G="highlight",j="colour",Q="font",V="size",$="​",Y=e.defaultView,X=navigator.userAgent,J=/iP(?:ad|hone|od)/.test(X),et=/Mac OS X/.test(X),tt=/Android/.test(X),nt=/Gecko\//.test(X),ot=/Trident\/[456]\./.test(X),it=!!Y.opera,rt=/Edge\//.test(X),at=!rt&&/WebKit\//.test(X),st=et?"meta-":"ctrl-",dt=ot||it,lt=ot||at,ct=ot,ht="undefined"!=typeof MutationObserver,ut=/[^ \t\r\n]/,ft=Array.prototype.indexOf;Object.create||(Object.create=function(e){var t=function(){};return t.prototype=e,new t});var pt={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(pt[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(pt[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(pt[e.nodeType]&o&&i(e))return this.currentNode=e,e;t=e}};var gt=/^(?:#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])?|U|VAR|WBR)$/,mt={BR:1,HR:1,IFRAME:1,IMG:1,INPUT:1},vt=function(e,t){for(var n=e.childNodes;t&&e.nodeType===I;)e=n[t-1],n=e.childNodes,t=n.length;return e},Ct=function(e,t){if(e.nodeType===I){var n=e.childNodes;if(t-1,r=e.compareBoundaryPoints(q,o)<1;return!i&&!r}var a=e.compareBoundaryPoints(W,o)<1,s=e.compareBoundaryPoints(K,o)>-1;return a&&s},bt=function(e){for(var t,n=e.startContainer,o=e.startOffset,r=e.endContainer,a=e.endOffset;n.nodeType!==w&&(t=n.childNodes[o],t&&!i(t));)n=t,o=0;if(a)for(;r.nodeType!==w&&(t=r.childNodes[a-1],t&&!i(t));)r=t,a=m(r);else for(;r.nodeType!==w&&(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))},Et=function(e,t){var n,o=e.startContainer,i=e.startOffset,r=e.endContainer,a=e.endOffset;for(t||(t=e.commonAncestorContainer);o!==t&&!i;)n=o.parentNode,i=ft.call(n.childNodes,o),o=n;for(;r!==t&&a===m(r);)n=r.parentNode,a=ft.call(n.childNodes,r)+1,r=n;e.setStart(o,i),e.setEnd(r,a)},xt=function(e,t){var n,o=e.startContainer;return r(o)?n=l(o,t):a(o)?n=o:(n=vt(o,e.startOffset),n=c(n,t)),n&&Tt(e,n,!0)?n:null},kt=function(e,t){var n,o,i=e.endContainer;if(r(i))n=l(i,t);else if(a(i))n=i;else{if(n=Ct(i,e.endOffset),!n||!p(t,n))for(n=t;o=n.lastChild;)n=o;n=l(n,t)}return n&&Tt(e,n,!0)?n:null},Lt=new n(null,z|H,function(e){return e.nodeType===w?ut.test(e.data):"IMG"===e.nodeName}),At=function(e,t){var n,o=e.startContainer,i=e.startOffset;if(Lt.root=null,o.nodeType===w){if(i)return!1;n=o}else if(n=Ct(o,i),n&&!p(t,n)&&(n=null),!n&&(n=vt(o,i),n.nodeType===w&&n.length))return!1;return Lt.currentNode=n,Lt.root=xt(e,t),!Lt.previousNode()},Bt=function(e,t){var n,o=e.endContainer,i=e.endOffset;if(Lt.root=null,o.nodeType===w){if(n=o.data.length,n&&n>i)return!1;Lt.currentNode=o}else Lt.currentNode=vt(o,i);return Lt.root=kt(e,t),!Lt.nextNode()},Ot=function(e,t){var n,o=xt(e,t),i=kt(e,t);o&&i&&(n=o.parentNode,e.setStart(n,ft.call(n.childNodes,o)),n=i.parentNode,e.setEnd(n,ft.call(n.childNodes,i)+1))},Dt={8:"backspace",9:"tab",13:"enter",32:"space",33:"pageup",34:"pagedown",37:"left",39:"right",46:"delete",219:"[",221:"]"},Rt=function(e){var t=e.keyCode,n=Dt[t],o="",i=this.getSelection();e.defaultPrevented||(n||(n=String.fromCharCode(t).toLowerCase(),/^[A-Za-z0-9]$/.test(n)||(n="")),it&&46===e.which&&(n="."),t>111&&124>t&&(n="f"+(t-111)),"backspace"!==n&&"delete"!==n&&(e.altKey&&(o+="alt-"),e.ctrlKey&&(o+="ctrl-"),e.metaKey&&(o+="meta-")),e.shiftKey&&(o+="shift-"),n=o+n,this._keyHandlers[n]?this._keyHandlers[n](this,e,i):1!==n.length||i.collapsed||(this.saveUndoState(i),St(i,this._root),this._ensureBottomLine(),this.setSelection(i),this._updatePath(i,!0)))},Ut=function(e){return function(t,n){n.preventDefault(),t[e]()}},Pt=function(e,t){return t=t||null,function(n,o){o.preventDefault();var i=n.getSelection();n.hasFormat(e,null,i)?n.changeFormat(null,{tag:e},i):n.changeFormat({tag:e},t,i)}},It=function(e,t){try{t||(t=e.getSelection());var n,o=t.startContainer;for(o.nodeType===w&&(o=o.parentNode),n=o;r(n)&&(!n.textContent||n.textContent===$);)o=n,n=o.parentNode;o!==n&&(t.setStart(n,ft.call(n.childNodes,o)),t.collapse(!0),n.removeChild(o),a(n)||(n=l(n,e._root)),S(n,e._root),bt(t)),o===e._root&&(o=o.firstChild)&&"BR"===o.nodeName&&v(o),e._ensureBottomLine(),e.setSelection(t),e._updatePath(t,!0)}catch(i){e.didError(i)}},wt={enter:function(e,t,n){var o,i,r,a=e._root;if(t.preventDefault(),e._recordUndoState(n),Sn(n.startContainer,a,e),e._removeZWS(),e._getRangeAndRemoveBookmark(n),n.collapsed||St(n,a),o=xt(n,a),!o||/^T[HD]$/.test(o.nodeName))return _t(n,e.createElement("BR")),n.collapse(!1),e.setSelection(n),void e._updatePath(n,!0);if((i=f(o,a,"LI"))&&(o=i),!o.textContent){if(f(o,a,"UL")||f(o,a,"OL"))return e.modifyBlocks(_n,n);if(f(o,a,"BLOCKQUOTE"))return e.modifyBlocks(fn,n)}for(r=cn(e,o,n.startContainer,n.startOffset),an(o),jt(o),S(o,a);r.nodeType===I;){var s,d=r.firstChild;if("A"===r.nodeName&&(!r.textContent||r.textContent===$)){d=e._doc.createTextNode(""),C(r,d),r=d;break}for(;d&&d.nodeType===w&&!d.data&&(s=d.nextSibling,s&&"BR"!==s.nodeName);)v(d),d=s;if(!d||"BR"===d.nodeName||d.nodeType===w&&!it)break;r=d}n=e._createRange(r,0),e.setSelection(n),e._updatePath(n,!0)},backspace:function(e,t,n){var o=e._root;if(e._removeZWS(),e.saveUndoState(n),n.collapsed)if(At(n,o)){t.preventDefault();var i,r=xt(n,o);if(!r)return;if(y(r.parentNode,o),i=l(r,o)){if(!i.isContentEditable)return void v(i);for(x(i,r,n),r=i.parentNode;r!==o&&!r.nextSibling;)r=r.parentNode;r!==o&&(r=r.nextSibling)&&k(r,o),e.setSelection(n)}else if(r){if(f(r,o,"UL")||f(r,o,"OL"))return e.modifyBlocks(_n,n);if(f(r,o,"BLOCKQUOTE"))return e.modifyBlocks(un,n);e.setSelection(n),e._updatePath(n,!0)}}else e.setSelection(n),setTimeout(function(){It(e)},0);else t.preventDefault(),St(n,o),It(e,n)},"delete":function(e,t,n){var o,i,r,a,s,d,l=e._root;if(e._removeZWS(),e.saveUndoState(n),n.collapsed)if(Bt(n,l)){if(t.preventDefault(),o=xt(n,l),!o)return;if(y(o.parentNode,l),i=c(o,l)){if(!i.isContentEditable)return void v(i);for(x(o,i,n),i=o.parentNode;i!==l&&!i.nextSibling;)i=i.parentNode;i!==l&&(i=i.nextSibling)&&k(i,l),e.setSelection(n),e._updatePath(n,!0)}}else{if(r=n.cloneRange(),Et(n,e._root),a=n.endContainer,s=n.endOffset,a.nodeType===I&&(d=a.childNodes[s],d&&"IMG"===d.nodeName))return t.preventDefault(),v(d),bt(n),void It(e,n);e.setSelection(r),setTimeout(function(){It(e)},0)}else t.preventDefault(),St(n,l),It(e,n)},tab:function(e,t,n){var o,i,r=e._root;if(e._removeZWS(),n.collapsed&&At(n,r))for(o=xt(n,r);i=o.parentNode;){if("UL"===i.nodeName||"OL"===i.nodeName){o.previousSibling&&(t.preventDefault(),e.modifyBlocks(Cn,n));break}o=i}},"shift-tab":function(e,t,n){var o,i=e._root;e._removeZWS(),n.collapsed&&At(n,i)&&(o=n.startContainer,(f(o,i,"UL")||f(o,i,"OL"))&&(t.preventDefault(),e.modifyBlocks(_n,n)))},space:function(e,t,n){var o,i;e._recordUndoState(n),Sn(n.startContainer,e._root,e),e._getRangeAndRemoveBookmark(n),o=n.endContainer,i=o.parentNode,n.collapsed&&"A"===i.nodeName&&!o.nextSibling&&n.endOffset===m(o)?n.setStartAfter(i):n.collapsed||(St(n,e._root),e._ensureBottomLine(),e.setSelection(n),e._updatePath(n,!0)),e.setSelection(n)},left:function(e){e._removeZWS()},right:function(e){e._removeZWS()}};et&&nt&&(wt["meta-left"]=function(e,t){t.preventDefault();var n=rn(e);n&&n.modify&&n.modify("move","backward","lineboundary")},wt["meta-right"]=function(e,t){t.preventDefault();var n=rn(e);n&&n.modify&&n.modify("move","forward","lineboundary")}),et||(wt.pageup=function(e){e.moveCursorToStart()},wt.pagedown=function(e){e.moveCursorToEnd()}),wt[st+"b"]=Pt("B"),wt[st+"i"]=Pt("I"),wt[st+"u"]=Pt("U"),wt[st+"shift-7"]=Pt("S"),wt[st+"shift-5"]=Pt("SUB",{tag:"SUP"}),wt[st+"shift-6"]=Pt("SUP",{tag:"SUB"}),wt[st+"shift-8"]=Ut("makeUnorderedList"),wt[st+"shift-9"]=Ut("makeOrderedList"),wt[st+"["]=Ut("decreaseQuoteLevel"),wt[st+"]"]=Ut("increaseQuoteLevel"),wt[st+"y"]=Ut("redo"),wt[st+"z"]=Ut("undo"),wt[st+"shift-z"]=Ut("redo");var Ft={1:10,2:13,3:16,4:18,5:24,6:32,7:48},Mt={backgroundColor:{regexp:ut,replace:function(e,t){return N(e,"SPAN",{"class":G,style:"background-color:"+t})}},color:{regexp:ut,replace:function(e,t){return N(e,"SPAN",{"class":j,style:"color:"+t})}},fontWeight:{regexp:/^bold/i,replace:function(e){return N(e,"B")}},fontStyle:{regexp:/^italic/i,replace:function(e){return N(e,"I")}},fontFamily:{regexp:ut,replace:function(e,t){return N(e,"SPAN",{"class":Q,style:"font-family:"+t})}},fontSize:{regexp:ut,replace:function(e,t){return N(e,"SPAN",{"class":V,style:"font-size:"+t})}},textDecoration:{regexp:/^underline/i,replace:function(e){return N(e,"U")}}},Ht=function(e){return function(t,n){var o=N(t.ownerDocument,e);return n.replaceChild(o,t),o.appendChild(_(t)),o}},zt=function(e,t){var n,o,i,r,a,s,d=e.style,l=e.ownerDocument;for(n in Mt)o=Mt[n],i=d[n],i&&o.regexp.test(i)&&(s=o.replace(l,i),a||(a=s),r&&r.appendChild(s),r=s,e.style[n]="");return a&&(r.appendChild(_(e)),"SPAN"===e.nodeName?t.replaceChild(a,e):e.appendChild(a)),r||e},Wt={P:zt,SPAN:zt,STRONG:Ht("B"),EM:Ht("I"),INS:Ht("U"),STRIKE:Ht("S"),FONT:function(e,t){var n,o,i,r,a,s=e.face,d=e.size,l=e.color,c=e.ownerDocument;return s&&(n=N(c,"SPAN",{"class":"font",style:"font-family:"+s}),a=n,r=n),d&&(o=N(c,"SPAN",{"class":"size",style:"font-size:"+Ft[d]+"px"}),a||(a=o),r&&r.appendChild(o),r=o),l&&/^#?([\dA-F]{3}){1,2}$/i.test(l)&&("#"!==l.charAt(0)&&(l="#"+l),i=N(c,"SPAN",{"class":"colour",style:"color:"+l}),a||(a=i),r&&r.appendChild(i),r=i),a||(a=r=N(c,"SPAN")),t.replaceChild(a,e),r.appendChild(_(e)),r},TT:function(e,t){var n=N(e.ownerDocument,"SPAN",{"class":"font",style:'font-family:menlo,consolas,"courier new",monospace'});return t.replaceChild(n,e),n.appendChild(_(e)),n}},qt=/^(?:A(?:DDRESS|RTICLE|SIDE|UDIO)|BLOCKQUOTE|CAPTION|D(?:[DLT]|IV)|F(?:IGURE|IGCAPTION|OOTER)|H[1-6]|HEADER|L(?:ABEL|EGEND|I)|O(?:L|UTPUT)|P(?:RE)?|SECTION|T(?:ABLE|BODY|D|FOOT|H|HEAD|R)|UL)$/,Kt=/^(?:HEAD|META|STYLE)/,Zt=new n(null,z|H,function(){return!0}),Gt=function bn(e,t){var n,o,i,a,s,d,l,c,h,u,f,p,g=e.childNodes;for(n=e;r(n);)n=n.parentNode;for(Zt.root=n,o=0,i=g.length;i>o;o+=1)if(a=g[o],s=a.nodeName,d=a.nodeType,l=Wt[s],d===I){if(c=a.childNodes.length,l)a=l(a,e);else{if(Kt.test(s)){e.removeChild(a),o-=1,i-=1;continue}if(!qt.test(s)&&!r(a)){o-=1,i+=c-1,e.replaceChild(_(a),a);continue}}c&&bn(a,t||"PRE"===s)}else{if(d===w){if(f=a.data,h=!ut.test(f.charAt(0)),u=!ut.test(f.charAt(f.length-1)),t||!h&&!u)continue;if(h){for(Zt.currentNode=a;(p=Zt.previousPONode())&&(s=p.nodeName,!("IMG"===s||"#text"===s&&/\S/.test(p.data)));)if(!r(p)){p=null;break}f=f.replace(/^\s+/g,p?" ":"")}if(u){for(Zt.currentNode=a;(p=Zt.nextNode())&&!("IMG"===s||"#text"===s&&/\S/.test(p.data));)if(!r(p)){p=null;break}f=f.replace(/\s+$/g,p?" ":"")}if(f){a.data=f;continue}}e.removeChild(a),o-=1,i-=1}return e},jt=function En(e){for(var t,n=e.childNodes,o=n.length;o--;)t=n[o],t.nodeType!==I||i(t)?t.nodeType!==w||t.data||e.removeChild(t):(En(t),r(t)&&!t.firstChild&&e.removeChild(t))},Qt=function(e){return e.nodeType===I?"BR"===e.nodeName:ut.test(e.data)},Vt=function(e){for(var t,o=e.parentNode;r(o);)o=o.parentNode;return t=new n(o,H|z,Qt),t.currentNode=e,!!t.nextNode()},$t=function(e,t){var n,o,i,a=e.querySelectorAll("BR"),s=[],d=a.length;for(n=0;d>n;n+=1)s[n]=Vt(a[n]);for(;d--;)o=a[d],i=o.parentNode,i&&(s[d]?r(i)||y(i,t):v(o))},Yt=function(e){var t=e.clipboardData,n=this.getSelection(),o=this.createElement("div"),i=this._root,r=this;this.saveUndoState(n),rt||J||!t?setTimeout(function(){try{r._ensureBottomLine()}catch(e){r.didError(e)}},0):(Et(n,i),o.appendChild(St(n,i)),t.setData("text/html",o.innerHTML),t.setData("text/plain",o.innerText||o.textContent),e.preventDefault()),this.setSelection(n)},Xt=function(e){var t=e.clipboardData,n=this.getSelection(),o=this.createElement("div");rt||J||!t||(o.appendChild(n.cloneContents()),t.setData("text/html",o.innerHTML),t.setData("text/plain",o.innerText||o.textContent),e.preventDefault())},Jt=function(e){var t,n,o,i,r,a=e.clipboardData,s=a&&a.items,d=!1,l=!1,c=null,h=this;if(!rt&&s){for(e.preventDefault(),t=s.length;t--;){if(n=s[t],o=n.type,"text/html"===o)return void n.getAsString(function(e){h.insertHTML(e,!0)});"text/plain"===o&&(c=n),/^image\/.*/.test(o)&&(l=!0)}return void(l?(this.fireEvent("dragover",{dataTransfer:a,preventDefault:function(){d=!0}}),d&&this.fireEvent("drop",{dataTransfer:a})):c&&n.getAsString(function(e){h.insertPlainText(e,!0)}))}if(i=a&&a.types,!rt&&i&&(ft.call(i,"text/html")>-1||!nt&&ft.call(i,"text/plain")>-1&&ft.call(i,"text/rtf")<0))return e.preventDefault(),void((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 u=this._doc.body,f=this.getSelection(),p=f.startContainer,g=f.startOffset,m=f.endContainer,C=f.endOffset,_=this.createElement("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});u.appendChild(_),f.selectNodeContents(_),this.setSelection(f),setTimeout(function(){try{h._awaitingPaste=!1;for(var e,t,n="",o=_;_=o;)o=_.nextSibling,v(_),e=_.firstChild,e&&e===_.lastChild&&"DIV"===e.nodeName&&(_=e),n+=_.innerHTML;t=h._createRange(p,g,m,C),h.setSelection(t),n&&h.insertHTML(n,!0)}catch(i){h.didError(i)}},0)},en=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()},tn=[],nn=B.prototype;nn.setConfig=function(e){return e=A({blockTag:"DIV",blockAttributes:null,tagAttributes:{blockquote:null,ul:null,ol:null,li:null,a:null},undo:{documentSizeThreshold:-1,undoLimit:-1}},e,!0),e.blockTag=e.blockTag.toUpperCase(),this._config=e,this},nn.createElement=function(e,t,n){return N(this._doc,e,t,n)},nn.createDefaultBlock=function(e){var t=this._config;return S(this.createElement(t.blockTag,t.blockAttributes,e),this._root)},nn.didError=function(e){console.log(e)},nn.getDocument=function(){return this._doc},nn.getRoot=function(){return this._root},nn.modifyDocument=function(e){this._ignoreAllChanges=!0,this._mutation&&this._mutation.disconnect(),e(),this._ignoreAllChanges=!1,this._mutation&&this._mutation.observe(this._root,{childList:!0,attributes:!0,characterData:!0,subtree:!0})};var on={pathChange:1,select:1,input:1,undoStateChange:1};nn.fireEvent=function(e,t){var n,o,i,r=this._events[e];if(/^(?:focus|blur)/.test(e))if(n=p(this._root,this._doc.activeElement),"focus"===e){if(!n||this._isFocused)return this;this._isFocused=!0}else{if(n||!this._isFocused)return this;this._isFocused=!1}if(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(a){a.details="Squire: fireEvent error. Event type: "+e,this.didError(a)}}return this},nn.destroy=function(){var e,t=tn.length,n=this._events;for(e in n)this.removeEventListener(e);for(this._mutation&&this._mutation.disconnect();t--;)tn[t]===this&&tn.splice(t,1);this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0},nn.handleEvent=function(e){this.fireEvent(e.type,e)},nn.addEventListener=function(e,t){var n=this._events[e],o=this._root;return t?(n||(n=this._events[e]=[],on[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)},nn.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],on[e]||("selectionchange"===e&&(i=this._doc),i.removeEventListener(e,this,!0)))}return this},nn._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},nn.getCursorPosition=function(e){if(!e&&!(e=this.getSelection())||!e.getBoundingClientRect)return null;var t,n,o=e.getBoundingClientRect();return o&&!o.top&&(this._ignoreChange=!0,t=this._doc.createElement("SPAN"),t.textContent=$,_t(e,t),o=t.getBoundingClientRect(),n=t.parentNode,n.removeChild(t),E(n,e)),o},nn._moveCursorTo=function(e){var t=this._root,n=this._createRange(t,e?0:t.childNodes.length);return bt(n),this.setSelection(n),this},nn.moveCursorToStart=function(){return this._moveCursorTo(!0)},nn.moveCursorToEnd=function(){return this._moveCursorTo(!1)};var rn=function(e){return e._win.getSelection()||null};nn.setSelection=function(e){if(e)if(this._lastSelection=e,this._isFocused)if(tt&&!this._restoreSelection)O.call(this),this.blur(),this.focus();else{J&&this._win.focus();var t=rn(this);t&&(t.removeAllRanges(),t.addRange(e))}else O.call(this);return this},nn.getSelection=function(){var e,t,n,o=rn(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&&p(r,e.commonAncestorContainer)?this._lastSelection=e:e=this._lastSelection,e||(e=this._createRange(r.firstChild,0)),e},nn.getSelectedText=function(){var e,t=this.getSelection(),o=new n(t.commonAncestorContainer,z|H,function(e){return Tt(t,e,!0)}),i=t.startContainer,a=t.endContainer,s=o.currentNode=i,d="",l=!1;for(o.filter(s)||(s=o.nextNode());s;)s.nodeType===w?(e=s.data,e&&/\S/.test(e)&&(s===a&&(e=e.slice(0,t.endOffset)),s===i&&(e=e.slice(t.startOffset)),d+=e,l=!0)):("BR"===s.nodeName||l&&!r(s))&&(d+="\n",l=!1),s=o.nextNode();return d},nn.getPath=function(){return this._path};var an=function(e){for(var t,o,i,a=new n(e,z,function(){return!0},!1);o=a.nextNode();)for(;(i=o.data.indexOf($))>-1;){if(1===o.length){do t=o.parentNode,t.removeChild(o),o=t,a.currentNode=t;while(r(o)&&!m(o));break}o.deleteData(i,1)}};nn._didAddZWS=function(){this._hasZWS=!0},nn._removeZWS=function(){this._hasZWS&&(an(this._root),this._hasZWS=!1)},nn._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?g(i,this._root):"(selection)":"",this._path!==n&&(this._path=n,this.fireEvent("pathChange",{path:n}))),e.collapsed||this.fireEvent("select")},nn._updatePathOnEvent=function(){var e=this;e._willUpdatePath||(e._willUpdatePath=!0,setTimeout(function(){e._willUpdatePath=!1,e._updatePath(e.getSelection())},0))},nn.focus=function(){return this._root.focus(),this},nn.blur=function(){return this._root.blur(),this};var sn="squire-selection-start",dn="squire-selection-end";nn._saveRangeToBookmark=function(e){var t,n=this.createElement("INPUT",{id:sn,type:"hidden"}),o=this.createElement("INPUT",{id:dn,type:"hidden"});_t(e,n),e.collapse(!1),_t(e,o),n.compareDocumentPosition(o)&P&&(n.id=dn,o.id=sn,t=n,n=o,o=t),e.setStartAfter(n),e.setEndBefore(o)},nn._getRangeAndRemoveBookmark=function(e){var t=this._root,n=t.querySelector("#"+sn),o=t.querySelector("#"+dn);if(n&&o){var i=n.parentNode,r=o.parentNode,a=ft.call(i.childNodes,n),s=ft.call(r.childNodes,o);i===r&&(s-=1),v(n),v(o),e||(e=this._doc.createRange()),e.setStart(i,a),e.setEnd(r,s),E(i,e),i!==r&&E(r,e),e.collapsed&&(i=e.startContainer,i.nodeType===w&&(r=i.childNodes[e.startOffset],r&&r.nodeType===w||(r=i.childNodes[e.startOffset-1]),r&&r.nodeType===w&&(e.setStart(r,0),e.collapse(!0))))}return e||null},nn._keyUpDetectChange=function(e){var t=e.keyCode;e.ctrlKey||e.metaKey||e.altKey||!(16>t||t>20)||!(33>t||t>45)||this._docWasChanged()},nn._docWasChanged=function(){if(!this._ignoreAllChanges){if(ht&&this._ignoreChange)return void(this._ignoreChange=!1);this._isInUndoState&&(this._isInUndoState=!1,this.fireEvent("undoStateChange",{canUndo:!0,canRedo:!1})),this.fireEvent("input")}},nn._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}},nn.saveUndoState=function(e){return e===t&&(e=this.getSelection()),this._isInUndoState||(this._recordUndoState(e),this._getRangeAndRemoveBookmark(e)),this},nn.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},nn.redo=function(){var e=this._undoIndex,t=this._undoStackLength;if(t>e+1&&this._isInUndoState){this._undoIndex+=1,this._setHTML(this._undoStack[this._undoIndex]);var n=this._getRangeAndRemoveBookmark();n&&this.setSelection(n),this.fireEvent("undoStateChange",{canUndo:!0,canRedo:t>e+2}),this.fireEvent("input")}return this},nn.hasFormat=function(e,t,o){if(e=e.toUpperCase(),t||(t={}),!o&&!(o=this.getSelection()))return!1;!o.collapsed&&o.startContainer.nodeType===w&&o.startOffset===o.startContainer.length&&o.startContainer.nextSibling&&o.setStartBefore(o.startContainer.nextSibling),!o.collapsed&&o.endContainer.nodeType===w&&0===o.endOffset&&o.endContainer.previousSibling&&o.setEndAfter(o.endContainer.previousSibling); -var i,r,a=this._root,s=o.commonAncestorContainer;if(f(s,a,e,t))return!0;if(s.nodeType===w)return!1;i=new n(s,z,function(e){return Tt(o,e,!0)},!1);for(var d=!1;r=i.nextNode();){if(!f(r,a,e,t))return!1;d=!0}return d},nn.getFontInfo=function(e){var n,o,i,r={color:t,backgroundColor:t,family:t,size:t},a=0;if(!e&&!(e=this.getSelection()))return r;if(n=e.commonAncestorContainer,e.collapsed||n.nodeType===w)for(n.nodeType===w&&(n=n.parentNode);4>a&&n;)(o=n.style)&&(!r.color&&(i=o.color)&&(r.color=i,a+=1),!r.backgroundColor&&(i=o.backgroundColor)&&(r.backgroundColor=i,a+=1),!r.family&&(i=o.fontFamily)&&(r.family=i,a+=1),!r.size&&(i=o.fontSize)&&(r.size=i,a+=1)),n=n.parentNode;return r},nn._addFormat=function(e,t,o){var i,r,a,s,d,l,c,h,u=this._root;if(o.collapsed)i=S(this.createElement(e,t),u),_t(o,i),o.setStart(i.firstChild,i.firstChild.length),o.collapse(!0);else{if(r=new n(o.commonAncestorContainer,z|H,function(e){return(e.nodeType===w||"BR"===e.nodeName||"IMG"===e.nodeName)&&Tt(o,e,!0)},!1),a=o.startContainer,d=o.startOffset,s=o.endContainer,l=o.endOffset,r.currentNode=a,r.filter(a)||(a=r.nextNode(),d=0),!a)return o;do c=r.currentNode,h=!f(c,u,e,t),h&&(c===s&&c.length>l&&c.splitText(l),c===a&&d&&(c=c.splitText(d),s===a&&(s=c,l-=d),a=c,d=0),i=this.createElement(e,t),C(c,i),i.appendChild(c));while(r.nextNode());s.nodeType!==w&&(c.nodeType===w?(s=c,l=c.length):(s=c.parentNode,l=1)),o=this._createRange(a,d,s,l)}return o},nn._removeFormat=function(e,t,n,o){this._saveRangeToBookmark(n);var i,a=this._doc;n.collapsed&&(lt?(i=a.createTextNode($),this._didAddZWS()):i=a.createTextNode(""),_t(n,i));for(var s=n.commonAncestorContainer;r(s);)s=s.parentNode;var d=n.startContainer,l=n.startOffset,c=n.endContainer,h=n.endOffset,f=[],p=function(e,t){if(!Tt(n,e,!1)){var o,i,r=e.nodeType===w;if(!Tt(n,e,!0))return void("INPUT"===e.nodeName||r&&!e.data||f.push([t,e]));if(r)e===c&&h!==e.length&&f.push([t,e.splitText(h)]),e===d&&l&&(e.splitText(l),f.push([t,e]));else for(o=e.firstChild;o;o=i)i=o.nextSibling,p(o,t)}},g=Array.prototype.filter.call(s.getElementsByTagName(e),function(o){return Tt(n,o,!0)&&u(o,e,t)});return o||g.forEach(function(e){p(e,e)}),f.forEach(function(e){var t=e[0].cloneNode(!1),n=e[1];C(n,t),t.appendChild(n)}),g.forEach(function(e){C(e,_(e))}),this._getRangeAndRemoveBookmark(n),i&&n.collapse(!1),E(s,n),n},nn.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),ht||this._docWasChanged(),this):this};var ln={DT:"DD",DD:"DT",LI:"LI"},cn=function(e,t,n,o){var i=ln[t.nodeName],r=null,a=T(n,o,t.parentNode,e._root),s=e._config;return i||(i=s.blockTag,r=s.blockAttributes),u(a,i,r)||(t=N(a.ownerDocument,i,r),a.dir&&(t.dir=a.dir),C(a,t),t.appendChild(_(a)),a=t),a};nn.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var o=this._root,i=xt(n,o),r=kt(n,o);if(i&&r)do if(e(i)||i===r)break;while(i=c(i,o));return t&&(this.setSelection(n),this._updatePath(n,!0),ht||this._docWasChanged()),this},nn.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 Ot(t,o),Et(t,o),n=Nt(t,o,o),_t(t,e.call(this,n)),t.endOffsett;t+=1){for(i=d[t],r=_(i),a=r.childNodes,o=a.length;o--;)s=a[o],C(s,_(s));y(r,this._root),C(i,r)}return e},Cn=function(e){var t,n,o,i,r,a,d=e.querySelectorAll("LI"),l=this._config.tagAttributes,c=l.li;for(t=0,n=d.length;n>t;t+=1)o=d[t],s(o.firstChild)||(i=o.parentNode.nodeName,r=o.previousSibling,r&&(r=r.lastChild)&&r.nodeName===i||(a=l[i.toLowerCase()],C(o,this.createElement("LI",c,[r=this.createElement(i,a)]))),r.appendChild(o));return e},_n=function(e){var t=this._root,n=e.querySelectorAll("LI");return Array.prototype.filter.call(n,function(e){return!s(e.firstChild)}).forEach(function(n){var o,i=n.parentNode,r=i.parentNode,a=n.firstChild,d=a;for(n.previousSibling&&(i=T(i,n,r,t));d&&(o=d.nextSibling,!s(d));)r.insertBefore(d,i),d=o;for("LI"===r.nodeName&&a.previousSibling&&T(r,a,r.parentNode,t);n!==e&&!n.childNodes.length;)i=n.parentNode,i.removeChild(n),n=i},this),y(e,t),e};nn._ensureBottomLine=function(){var e=this._root,t=e.lastElementChild;t&&t.nodeName===this._config.blockTag&&a(t)||e.appendChild(this.createDefaultBlock())},nn.setKeyHandler=function(e,t){return this._keyHandlers[e]=t,this},nn._getHTML=function(){return this._root.innerHTML},nn._setHTML=function(e){var t=this._root,n=t;n.innerHTML=e;do S(n,t);while(n=c(n,t));this._ignoreChange=!0},nn.getHTML=function(e){var t,n,o,i,r,a,s=[];if(e&&(a=this.getSelection())&&this._saveRangeToBookmark(a),dt)for(t=this._root,n=t;n=c(n,t);)n.textContent||n.querySelector("BR")||(o=this.createElement("BR"),n.appendChild(o),s.push(o));if(i=this._getHTML().replace(/\u200B/g,""),dt)for(r=s.length;r--;)v(s[r]);return a&&this._getRangeAndRemoveBookmark(a),i},nn.setHTML=function(e){var t,n=this._doc.createDocumentFragment(),o=this.createElement("DIV"),i=this._root;o.innerHTML=e,n.appendChild(_(o)),Gt(n),$t(n,i),y(n,i);for(var r=n;r=c(r,i);)S(r,i);for(this._ignoreChange=!0;t=i.lastChild;)i.removeChild(t);i.appendChild(n),S(i,i),this._undoIndex=-1,this._undoStack.length=0,this._undoStackLength=0,this._isInUndoState=!1;var a=this._getRangeAndRemoveBookmark()||this._createRange(i.firstChild,0);return this.saveUndoState(a),this._lastSelection=a,O.call(this),this._updatePath(a,!0),this},nn.insertElement=function(e,t){if(t||(t=this.getSelection()),t.collapse(!0),r(e))_t(t,e),t.setStartAfter(e);else{for(var n,o,i=this._root,a=xt(t,i)||i;a!==i&&!a.nextSibling;)a=a.parentNode;a!==i&&(n=a.parentNode,o=T(n,a.nextSibling,i,i)),o?i.insertBefore(e,o):(i.appendChild(e),o=this.createDefaultBlock(),i.appendChild(o)),t.setStart(o,0),t.setEnd(o,0),bt(t)}return this.focus(),this.setSelection(t),this._updatePath(t),ht||this._docWasChanged(),this},nn.insertImage=function(e,t){var n=this.createElement("IMG",A({src:e},t,!0));return this.insertElement(n),n};var Nn=/\b((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))|([\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,}\b)/i,Sn=function(e,t,o){for(var i,r,a,s,d,l,c,h=e.ownerDocument,u=new n(e,z,function(e){return!f(e,t,"A")},!1),p=o._config.tagAttributes.a;i=u.nextNode();)for(r=i.data,a=i.parentNode;s=Nn.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]},p,!1)),c.textContent=r.slice(d,l),a.insertBefore(c,i),i.data=r=r.slice(l)};nn.insertHTML=function(e,t){var n,o,i,r,a,s,d,l=this.getSelection(),h=this._doc;"undefined"!=typeof DOMPurify&&DOMPurify.isSupported?(r=DOMPurify.sanitize(e,{WHOLE_DOCUMENT:!1,RETURN_DOM:!0,RETURN_DOM_FRAGMENT:!0}),r=h.importNode(r,!0)):(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=h.createDocumentFragment(),r.appendChild(_(i))),this.saveUndoState(l);try{for(a=this._root,s=r,d={fragment:r,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},Sn(r,r,this),Gt(r),$t(r,null),jt(r),r.normalize();s=c(s,r);)S(s,null);t&&this.fireEvent("willPaste",d),d.defaultPrevented||(yt(l,d.fragment,a),ht||this._docWasChanged(),l.collapse(!1),this._ensureBottomLine()),this.setSelection(l),this._updatePath(l,!0),t&&this.focus()}catch(u){this.didError(u)}return this};var yn=function(e){return e.split("&").join("&").split("<").join("<").split(">").join(">").split('"').join(""")};nn.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+'="'+yn(l[n])+'"';for(h+=">",o=0,i=a.length;i>o;o+=1)r=a[o],r=yn(r).replace(/ (?= )/g," "),o&&i>o+1&&(r=h+(r||"
")+c),a[o]=r;return this.insertHTML(a.join(""),t)};var Tn=function(e,t,n){return function(){return this[e](t,n),this.focus()}};nn.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},nn.bold=Tn("changeFormat",{tag:"B"}),nn.italic=Tn("changeFormat",{tag:"I"}),nn.underline=Tn("changeFormat",{tag:"U"}),nn.strikethrough=Tn("changeFormat",{tag:"S"}),nn.subscript=Tn("changeFormat",{tag:"SUB"},{tag:"SUP"}),nn.superscript=Tn("changeFormat",{tag:"SUP"},{tag:"SUB"}),nn.removeBold=Tn("changeFormat",null,{tag:"B"}),nn.removeItalic=Tn("changeFormat",null,{tag:"I"}),nn.removeUnderline=Tn("changeFormat",null,{tag:"U"}),nn.removeStrikethrough=Tn("changeFormat",null,{tag:"S"}),nn.removeSubscript=Tn("changeFormat",null,{tag:"SUB"}),nn.removeSuperscript=Tn("changeFormat",null,{tag:"SUP"}),nn.makeLink=function(e,t){var n=this.getSelection();if(n.collapsed){var o=e.indexOf(":")+1;if(o)for(;"/"===e[o];)o+=1;_t(n,this._doc.createTextNode(e.slice(o)))}return t=A(A({href:e},t,!0),this._config.tagAttributes.a,!1),this.changeFormat({tag:"A",attributes:t},{tag:"A"},n),this.focus()},nn.removeLink=function(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0),this.focus()},nn.setFontFace=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{"class":"font",style:"font-family: "+e+", sans-serif;"}}:null,{tag:"SPAN",attributes:{"class":"font"}}),this.focus()},nn.setFontSize=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{"class":"size",style:"font-size: "+("number"==typeof e?e+"px":e)}}:null,{tag:"SPAN",attributes:{"class":"size"}}),this.focus()},nn.setTextColour=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{"class":"colour",style:"color:"+e}}:null,{tag:"SPAN",attributes:{"class":"colour"}}),this.focus()},nn.setHighlightColour=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{"class":"highlight",style:"background-color:"+e}}:e,{tag:"SPAN",attributes:{"class":"highlight"}}),this.focus()},nn.setTextAlignment=function(e){return this.forEachBlock(function(t){t.className=(t.className.split(/\s+/).filter(function(e){return!/align/.test(e)}).join(" ")+" align-"+e).trim(),t.style.textAlign=e},!0),this.focus()},nn.setTextDirection=function(e){return this.forEachBlock(function(t){t.dir=e},!0),this.focus()},nn.removeAllFormatting=function(e){if(!e&&!(e=this.getSelection())||e.collapsed)return this;for(var t=this._root,n=e.commonAncestorContainer;n&&!a(n);)n=n.parentNode;if(n||(Ot(e,t),n=t),n.nodeType===w)return this;this.saveUndoState(e),Et(e,n);for(var o,i,r=n.ownerDocument,s=e.startContainer,d=e.startOffset,l=e.endContainer,c=e.endOffset,h=r.createDocumentFragment(),u=r.createDocumentFragment(),f=T(l,c,n,t),p=T(s,d,n,t);p!==f;)o=p.nextSibling,h.appendChild(p),p=o;return U(this,h,u),u.normalize(),p=u.firstChild,o=u.lastChild,i=n.childNodes,p?(n.insertBefore(u,f),d=ft.call(i,p),c=ft.call(i,o)+1):(d=ft.call(i,f),c=d),e.setStart(n,d),e.setEnd(n,c),E(n,e),bt(e),this.setSelection(e),this._updatePath(e,!0),this.focus()},nn.increaseQuoteLevel=Tn("modifyBlocks",hn),nn.decreaseQuoteLevel=Tn("modifyBlocks",un),nn.makeUnorderedList=Tn("modifyBlocks",gn),nn.makeOrderedList=Tn("modifyBlocks",mn),nn.removeList=Tn("modifyBlocks",vn),nn.increaseListLevel=Tn("modifyBlocks",Cn),nn.decreaseListLevel=Tn("modifyBlocks",_n),"object"==typeof exports?module.exports=B:"function"==typeof define&&define.amd?define(function(){return B}):(Y.Squire=B,top!==Y&&"true"===e.documentElement.getAttribute("data-squireinit")&&(Y.editor=new B(e),Y.onEditorLoad&&(Y.onEditorLoad(Y.editor),Y.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===I&&!!me[e.nodeName]}function r(e){return ge.test(e.nodeName)}function a(e){var t=e.nodeType;return(t===I||t===M)&&!r(e)&&o(e.childNodes,r)}function s(e){var t=e.nodeType;return!(t!==I&&t!==M||r(e)||a(e))}function d(e,t){var o=new n(t,H,a);return o.currentNode=e,o}function l(e,t){return e=d(e,t).previousNode(),e!==t?e:null}function c(e,t){return e=d(e,t).nextNode(),e!==t?e:null}function h(e,t){return!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 u(e,t,n){if(e.nodeName!==t)return!1;for(var o in n)if(e.getAttribute(o)!==n[o])return!1;return!0}function f(e,t,n,o){for(;e&&e!==t;){if(u(e,n,o))return e;e=e.parentNode}return null}function p(e,t){for(;t;){if(t===e)return!0;t=t.parentNode}return!1}function g(e,t){var n,o,i,r,a="";return e&&e!==t&&(a=g(e.parentNode,t),e.nodeType===I&&(a+=(a?">":"")+e.nodeName,(n=e.id)&&(a+="#"+n),(o=e.className.trim())&&(i=o.split(/\s\s*/),i.sort(),a+=".",a+=i.join(".")),(r=e.dir)&&(a+="[dir="+r+"]"),i&&(fe.call(i,G)>-1&&(a+="[backgroundColor="+e.style.backgroundColor.replace(/ /g,"")+"]"),fe.call(i,j)>-1&&(a+="[color="+e.style.color.replace(/ /g,"")+"]"),fe.call(i,Q)>-1&&(a+="[fontFamily="+e.style.fontFamily.replace(/ /g,"")+"]"),fe.call(i,V)>-1&&(a+="[fontSize="+e.style.fontSize+"]")))),a}function m(e){var t=e.nodeType;return t===I?e.childNodes.length:e.length||0}function v(e){var t=e.parentNode;return t&&t.removeChild(e),e}function C(e,t){var n=e.parentNode;n&&n.replaceChild(t,e)}function _(e){for(var t=e.ownerDocument.createDocumentFragment(),n=e.childNodes,o=n?n.length:0;o--;)t.appendChild(e.firstChild);return t}function N(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;d>s;s+=1)l.appendChild(i[s]);return l}function S(e,t){var n,o,a=e.ownerDocument,s=e;if(e===t&&((o=e.firstChild)&&"BR"!==o.nodeName||(n=L(a).createDefaultBlock(),o?e.replaceChild(n,o):e.appendChild(n),e=n,n=null)),e.nodeType===w)return s;if(r(e)){for(o=e.firstChild;le&&o&&o.nodeType===w&&!o.data;)e.removeChild(o),o=e.firstChild;o||(le?(n=a.createTextNode($),L(a)._didAddZWS()):n=a.createTextNode(""))}else if(de){for(;e.nodeType!==w&&!i(e);){if(o=e.firstChild,!o){n=a.createTextNode("");break}e=o}e.nodeType===w?/^ +$/.test(e.data)&&(e.data=""):i(e)&&e.parentNode.insertBefore(a.createTextNode(""),e)}else if(!e.querySelector("BR"))for(n=N(a,"BR");(o=e.lastElementChild)&&!r(o);)e=o;if(n)try{e.appendChild(n)}catch(d){L(a).didError({name:"Squire: fixCursor – "+d,message:"Parent: "+e.nodeName+"/"+e.innerHTML+" appendChild: "+n.nodeName})}return s}function y(e,t){var n,o,i,a,d=e.childNodes,l=e.ownerDocument,c=null,h=L(l)._config;for(n=0,o=d.length;o>n;n+=1)i=d[n],a="BR"===i.nodeName,!a&&r(i)?(c||(c=N(l,h.blockTag,h.blockAttributes)),c.appendChild(i),n-=1,o-=1):(a||c)&&(c||(c=N(l,h.blockTag,h.blockAttributes)),S(c,t),a?e.replaceChild(c,i):(e.insertBefore(c,i),n+=1,o+=1),c=null),s(i)&&y(i,t);return c&&e.appendChild(S(c,t)),e}function T(e,t,n,o){var i,r,a,s=e.nodeType;if(s===w&&e!==n)return T(e.parentNode,e.splitText(t),n,o);if(s===I){if("number"==typeof t&&(t=ts?t.startOffset-=1:t.startOffset===s&&(t.startContainer=o,t.startOffset=m(o))),t.endContainer===e&&(t.endOffset>s?t.endOffset-=1:t.endOffset===s&&(t.endContainer=o,t.endOffset=m(o))),v(n),n.nodeType===w?o.appendData(n.data):d.push(_(n));else if(n.nodeType===I){for(i=d.length;i--;)n.appendChild(d.pop());b(n,t)}}function E(e,t){if(e.nodeType===w&&(e=e.parentNode),e.nodeType===I){var n={startContainer:t.startContainer,startOffset:t.startOffset,endContainer:t.endContainer,endOffset:t.endOffset};b(e,n),t.setStart(n.startContainer,n.startOffset),t.setEnd(n.endContainer,n.endOffset)}}function x(e,t,n){for(var o,i,r=t;1===r.parentNode.childNodes.length;)r=r.parentNode;v(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),E(e,n),ie&&(o=e.lastChild)&&"BR"===o.nodeName&&e.removeChild(o)}function k(e,t){var n,o,i=e.previousSibling,r=e.firstChild,a=e.ownerDocument,d="LI"===e.nodeName;if(!d||r&&/^[OU]L$/.test(r.nodeName))if(i&&h(i,e)){if(!s(i)){if(!d)return;o=N(a,"DIV"),o.appendChild(_(i)),i.appendChild(o)}v(e),n=!s(e),i.appendChild(_(e)),n&&y(i,t),r&&k(r,t)}else d&&(i=N(a,"DIV"),e.insertBefore(i,r),S(i,t))}function L(e){for(var t,n=tt.length;n--;)if(t=tt[n],t._doc===e)return t;return null}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 B(e,t){e.nodeType===F&&(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,ce&&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,he?(n=new MutationObserver(this._docWasChanged.bind(this)),n.observe(e,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._mutation=n):this.addEventListener("keyup",this._keyUpDetectChange),this._restoreSelection=!1,this.addEventListener("blur",O),this.addEventListener("mousedown",D),this.addEventListener("touchstart",D),this.addEventListener("focus",R),this._awaitingPaste=!1,this.addEventListener(oe?"beforecut":"cut",Ye),this.addEventListener("copy",Xe),this.addEventListener(oe?"beforepaste":"paste",Je),this.addEventListener("drop",et),this.addEventListener(ie?"keypress":"keydown",Re),this._keyHandlers=Object.create(we),this.setConfig(t),oe&&(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(r){}tt.push(this),this.setHTML("")}function O(){this._restoreSelection=!0}function D(){this._restoreSelection=!1}function R(){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,r(o)){if(o.nodeType===w||"BR"===o.nodeName||"IMG"===o.nodeName){n.appendChild(o);continue}}else if(a(o)){n.appendChild(e.createDefaultBlock([U(e,o,e._doc.createDocumentFragment())]));continue}U(e,o,n)}return n}var P=2,I=1,w=3,F=9,M=11,H=1,z=4,W=0,q=1,K=2,Z=3,G="highlight",j="colour",Q="font",V="size",$="​",Y=e.defaultView,X=navigator.userAgent,J=/iP(?:ad|hone|od)/.test(X),ee=/Mac OS X/.test(X),te=/Android/.test(X),ne=/Gecko\//.test(X),oe=/Trident\/[456]\./.test(X),ie=!!Y.opera,re=/Edge\//.test(X),ae=!re&&/WebKit\//.test(X),se=ee?"meta-":"ctrl-",de=oe||ie,le=oe||ae,ce=oe,he="undefined"!=typeof MutationObserver,ue=/[^ \t\r\n]/,fe=Array.prototype.indexOf;Object.create||(Object.create=function(e){var t=function(){};return t.prototype=e,new t});var pe={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(pe[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(pe[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(pe[e.nodeType]&o&&i(e))return this.currentNode=e,e;t=e}};var ge=/^(?:#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])?|U|VAR|WBR)$/,me={BR:1,HR:1,IFRAME:1,IMG:1,INPUT:1},ve=function(e,t){for(var n=e.childNodes;t&&e.nodeType===I;)e=n[t-1],n=e.childNodes,t=n.length;return e},Ce=function(e,t){if(e.nodeType===I){var n=e.childNodes;if(t-1,r=e.compareBoundaryPoints(q,o)<1;return!i&&!r}var a=e.compareBoundaryPoints(W,o)<1,s=e.compareBoundaryPoints(K,o)>-1;return a&&s},be=function(e){for(var t,n=e.startContainer,o=e.startOffset,r=e.endContainer,a=e.endOffset;n.nodeType!==w&&(t=n.childNodes[o],t&&!i(t));)n=t,o=0;if(a)for(;r.nodeType!==w&&(t=r.childNodes[a-1],t&&!i(t));)r=t,a=m(r);else for(;r.nodeType!==w&&(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))},Ee=function(e,t){var n,o=e.startContainer,i=e.startOffset,r=e.endContainer,a=e.endOffset;for(t||(t=e.commonAncestorContainer);o!==t&&!i;)n=o.parentNode,i=fe.call(n.childNodes,o),o=n;for(;r!==t&&a===m(r);)n=r.parentNode,a=fe.call(n.childNodes,r)+1,r=n;e.setStart(o,i),e.setEnd(r,a)},xe=function(e,t){var n,o=e.startContainer;return r(o)?n=l(o,t):a(o)?n=o:(n=ve(o,e.startOffset),n=c(n,t)),n&&Te(e,n,!0)?n:null},ke=function(e,t){var n,o,i=e.endContainer;if(r(i))n=l(i,t);else if(a(i))n=i;else{if(n=Ce(i,e.endOffset),!n||!p(t,n))for(n=t;o=n.lastChild;)n=o;n=l(n,t)}return n&&Te(e,n,!0)?n:null},Le=new n(null,z|H,function(e){return e.nodeType===w?ue.test(e.data):"IMG"===e.nodeName}),Ae=function(e,t){var n,o=e.startContainer,i=e.startOffset;if(Le.root=null,o.nodeType===w){if(i)return!1;n=o}else if(n=Ce(o,i),n&&!p(t,n)&&(n=null),!n&&(n=ve(o,i),n.nodeType===w&&n.length))return!1;return Le.currentNode=n,Le.root=xe(e,t),!Le.previousNode()},Be=function(e,t){var n,o=e.endContainer,i=e.endOffset;if(Le.root=null,o.nodeType===w){if(n=o.data.length,n&&n>i)return!1;Le.currentNode=o}else Le.currentNode=ve(o,i);return Le.root=ke(e,t),!Le.nextNode()},Oe=function(e,t){var n,o=xe(e,t),i=ke(e,t);o&&i&&(n=o.parentNode,e.setStart(n,fe.call(n.childNodes,o)),n=i.parentNode,e.setEnd(n,fe.call(n.childNodes,i)+1))},De={8:"backspace",9:"tab",13:"enter",32:"space",33:"pageup",34:"pagedown",37:"left",39:"right",46:"delete",219:"[",221:"]"},Re=function(e){var t=e.keyCode,n=De[t],o="",i=this.getSelection();e.defaultPrevented||(n||(n=String.fromCharCode(t).toLowerCase(),/^[A-Za-z0-9]$/.test(n)||(n="")),ie&&46===e.which&&(n="."),t>111&&124>t&&(n="f"+(t-111)),"backspace"!==n&&"delete"!==n&&(e.altKey&&(o+="alt-"),e.ctrlKey&&(o+="ctrl-"),e.metaKey&&(o+="meta-")),e.shiftKey&&(o+="shift-"),n=o+n,this._keyHandlers[n]?this._keyHandlers[n](this,e,i):1!==n.length||i.collapsed||(this.saveUndoState(i),Se(i,this._root),this._ensureBottomLine(),this.setSelection(i),this._updatePath(i,!0)))},Ue=function(e){return function(t,n){n.preventDefault(),t[e]()}},Pe=function(e,t){return t=t||null,function(n,o){o.preventDefault();var i=n.getSelection();n.hasFormat(e,null,i)?n.changeFormat(null,{tag:e},i):n.changeFormat({tag:e},t,i)}},Ie=function(e,t){try{t||(t=e.getSelection());var n,o=t.startContainer;for(o.nodeType===w&&(o=o.parentNode),n=o;r(n)&&(!n.textContent||n.textContent===$);)o=n,n=o.parentNode;o!==n&&(t.setStart(n,fe.call(n.childNodes,o)),t.collapse(!0),n.removeChild(o),a(n)||(n=l(n,e._root)),S(n,e._root),be(t)),o===e._root&&(o=o.firstChild)&&"BR"===o.nodeName&&v(o),e._ensureBottomLine(),e.setSelection(t),e._updatePath(t,!0)}catch(i){e.didError(i)}},we={enter:function(e,t,n){var o,i,r,a=e._root;if(t.preventDefault(),e._recordUndoState(n),Nt(n.startContainer,a,e),e._removeZWS(),e._getRangeAndRemoveBookmark(n),n.collapsed||Se(n,a),o=xe(n,a),!o||/^T[HD]$/.test(o.nodeName))return _e(n,e.createElement("BR")),n.collapse(!1),e.setSelection(n),void e._updatePath(n,!0);if((i=f(o,a,"LI"))&&(o=i),!o.textContent){if(f(o,a,"UL")||f(o,a,"OL"))return e.modifyBlocks(Ct,n);if(f(o,a,"BLOCKQUOTE"))return e.modifyBlocks(ut,n)}for(r=lt(e,o,n.startContainer,n.startOffset),rt(o),je(o),S(o,a);r.nodeType===I;){var s,d=r.firstChild;if("A"===r.nodeName&&(!r.textContent||r.textContent===$)){d=e._doc.createTextNode(""),C(r,d),r=d;break}for(;d&&d.nodeType===w&&!d.data&&(s=d.nextSibling,s&&"BR"!==s.nodeName);)v(d),d=s;if(!d||"BR"===d.nodeName||d.nodeType===w&&!ie)break;r=d}n=e._createRange(r,0),e.setSelection(n),e._updatePath(n,!0)},backspace:function(e,t,n){var o=e._root;if(e._removeZWS(),e.saveUndoState(n),n.collapsed)if(Ae(n,o)){t.preventDefault();var i,r=xe(n,o);if(!r)return;if(y(r.parentNode,o),i=l(r,o)){if(!i.isContentEditable)return void v(i);for(x(i,r,n),r=i.parentNode;r!==o&&!r.nextSibling;)r=r.parentNode;r!==o&&(r=r.nextSibling)&&k(r,o),e.setSelection(n)}else if(r){if(f(r,o,"UL")||f(r,o,"OL"))return e.modifyBlocks(Ct,n);if(f(r,o,"BLOCKQUOTE"))return e.modifyBlocks(ht,n);e.setSelection(n),e._updatePath(n,!0)}}else e.setSelection(n),setTimeout(function(){Ie(e)},0);else t.preventDefault(),Se(n,o),Ie(e,n)},"delete":function(e,t,n){var o,i,r,a,s,d,l=e._root;if(e._removeZWS(),e.saveUndoState(n),n.collapsed)if(Be(n,l)){if(t.preventDefault(),o=xe(n,l),!o)return;if(y(o.parentNode,l),i=c(o,l)){if(!i.isContentEditable)return void v(i);for(x(o,i,n),i=o.parentNode;i!==l&&!i.nextSibling;)i=i.parentNode;i!==l&&(i=i.nextSibling)&&k(i,l),e.setSelection(n),e._updatePath(n,!0)}}else{if(r=n.cloneRange(),Ee(n,e._root),a=n.endContainer,s=n.endOffset,a.nodeType===I&&(d=a.childNodes[s],d&&"IMG"===d.nodeName))return t.preventDefault(),v(d),be(n),void Ie(e,n);e.setSelection(r),setTimeout(function(){Ie(e)},0)}else t.preventDefault(),Se(n,l),Ie(e,n)},tab:function(e,t,n){var o,i,r=e._root;if(e._removeZWS(),n.collapsed&&Ae(n,r))for(o=xe(n,r);i=o.parentNode;){if("UL"===i.nodeName||"OL"===i.nodeName){o.previousSibling&&(t.preventDefault(),e.modifyBlocks(vt,n));break}o=i}},"shift-tab":function(e,t,n){var o,i=e._root;e._removeZWS(),n.collapsed&&Ae(n,i)&&(o=n.startContainer,(f(o,i,"UL")||f(o,i,"OL"))&&(t.preventDefault(),e.modifyBlocks(Ct,n)))},space:function(e,t,n){var o,i;e._recordUndoState(n),Nt(n.startContainer,e._root,e),e._getRangeAndRemoveBookmark(n),o=n.endContainer,i=o.parentNode,n.collapsed&&"A"===i.nodeName&&!o.nextSibling&&n.endOffset===m(o)?n.setStartAfter(i):n.collapsed||(Se(n,e._root),e._ensureBottomLine(),e.setSelection(n),e._updatePath(n,!0)),e.setSelection(n)},left:function(e){e._removeZWS()},right:function(e){e._removeZWS()}};ee&&ne&&(we["meta-left"]=function(e,t){t.preventDefault();var n=it(e);n&&n.modify&&n.modify("move","backward","lineboundary")},we["meta-right"]=function(e,t){t.preventDefault();var n=it(e);n&&n.modify&&n.modify("move","forward","lineboundary")}),ee||(we.pageup=function(e){e.moveCursorToStart()},we.pagedown=function(e){e.moveCursorToEnd()}),we[se+"b"]=Pe("B"),we[se+"i"]=Pe("I"),we[se+"u"]=Pe("U"),we[se+"shift-7"]=Pe("S"),we[se+"shift-5"]=Pe("SUB",{tag:"SUP"}),we[se+"shift-6"]=Pe("SUP",{tag:"SUB"}),we[se+"shift-8"]=Ue("makeUnorderedList"),we[se+"shift-9"]=Ue("makeOrderedList"),we[se+"["]=Ue("decreaseQuoteLevel"),we[se+"]"]=Ue("increaseQuoteLevel"),we[se+"y"]=Ue("redo"),we[se+"z"]=Ue("undo"),we[se+"shift-z"]=Ue("redo");var Fe={1:10,2:13,3:16,4:18,5:24,6:32,7:48},Me={backgroundColor:{regexp:ue,replace:function(e,t){return N(e,"SPAN",{"class":G,style:"background-color:"+t})}},color:{regexp:ue,replace:function(e,t){return N(e,"SPAN",{"class":j,style:"color:"+t})}},fontWeight:{regexp:/^bold/i,replace:function(e){return N(e,"B")}},fontStyle:{regexp:/^italic/i,replace:function(e){return N(e,"I")}},fontFamily:{regexp:ue,replace:function(e,t){return N(e,"SPAN",{"class":Q,style:"font-family:"+t})}},fontSize:{regexp:ue,replace:function(e,t){return N(e,"SPAN",{"class":V,style:"font-size:"+t})}},textDecoration:{regexp:/^underline/i,replace:function(e){return N(e,"U")}}},He=function(e){return function(t,n){var o=N(t.ownerDocument,e);return n.replaceChild(o,t),o.appendChild(_(t)),o}},ze=function(e,t){var n,o,i,r,a,s,d=e.style,l=e.ownerDocument;for(n in Me)o=Me[n],i=d[n],i&&o.regexp.test(i)&&(s=o.replace(l,i),a||(a=s),r&&r.appendChild(s),r=s,e.style[n]="");return a&&(r.appendChild(_(e)),"SPAN"===e.nodeName?t.replaceChild(a,e):e.appendChild(a)),r||e},We={P:ze,SPAN:ze,STRONG:He("B"),EM:He("I"),INS:He("U"),STRIKE:He("S"),FONT:function(e,t){var n,o,i,r,a,s=e.face,d=e.size,l=e.color,c=e.ownerDocument;return s&&(n=N(c,"SPAN",{"class":"font",style:"font-family:"+s}),a=n,r=n),d&&(o=N(c,"SPAN",{"class":"size",style:"font-size:"+Fe[d]+"px"}),a||(a=o),r&&r.appendChild(o),r=o),l&&/^#?([\dA-F]{3}){1,2}$/i.test(l)&&("#"!==l.charAt(0)&&(l="#"+l),i=N(c,"SPAN",{"class":"colour",style:"color:"+l}),a||(a=i),r&&r.appendChild(i),r=i),a||(a=r=N(c,"SPAN")),t.replaceChild(a,e),r.appendChild(_(e)),r},TT:function(e,t){var n=N(e.ownerDocument,"SPAN",{"class":"font",style:'font-family:menlo,consolas,"courier new",monospace'});return t.replaceChild(n,e),n.appendChild(_(e)),n}},qe=/^(?:A(?:DDRESS|RTICLE|SIDE|UDIO)|BLOCKQUOTE|CAPTION|D(?:[DLT]|IV)|F(?:IGURE|IGCAPTION|OOTER)|H[1-6]|HEADER|L(?:ABEL|EGEND|I)|O(?:L|UTPUT)|P(?:RE)?|SECTION|T(?:ABLE|BODY|D|FOOT|H|HEAD|R)|UL)$/,Ke=/^(?:HEAD|META|STYLE)/,Ze=new n(null,z|H,function(){return!0}),Ge=function Tt(e,t){var n,o,i,a,s,d,l,c,h,u,f,p,g=e.childNodes;for(n=e;r(n);)n=n.parentNode;for(Ze.root=n,o=0,i=g.length;i>o;o+=1)if(a=g[o],s=a.nodeName,d=a.nodeType,l=We[s],d===I){if(c=a.childNodes.length,l)a=l(a,e);else{if(Ke.test(s)){e.removeChild(a),o-=1,i-=1;continue}if(!qe.test(s)&&!r(a)){o-=1,i+=c-1,e.replaceChild(_(a),a);continue}}c&&Tt(a,t||"PRE"===s)}else{if(d===w){if(f=a.data,h=!ue.test(f.charAt(0)),u=!ue.test(f.charAt(f.length-1)),t||!h&&!u)continue;if(h){for(Ze.currentNode=a;(p=Ze.previousPONode())&&(s=p.nodeName,!("IMG"===s||"#text"===s&&/\S/.test(p.data)));)if(!r(p)){p=null;break}f=f.replace(/^\s+/g,p?" ":"")}if(u){for(Ze.currentNode=a;(p=Ze.nextNode())&&!("IMG"===s||"#text"===s&&/\S/.test(p.data));)if(!r(p)){p=null;break}f=f.replace(/\s+$/g,p?" ":"")}if(f){a.data=f;continue}}e.removeChild(a),o-=1,i-=1}return e},je=function bt(e){for(var t,n=e.childNodes,o=n.length;o--;)t=n[o],t.nodeType!==I||i(t)?t.nodeType!==w||t.data||e.removeChild(t):(bt(t),r(t)&&!t.firstChild&&e.removeChild(t))},Qe=function(e){return e.nodeType===I?"BR"===e.nodeName:ue.test(e.data)},Ve=function(e){for(var t,o=e.parentNode;r(o);)o=o.parentNode;return t=new n(o,H|z,Qe),t.currentNode=e,!!t.nextNode()},$e=function(e,t){var n,o,i,a=e.querySelectorAll("BR"),s=[],d=a.length;for(n=0;d>n;n+=1)s[n]=Ve(a[n]);for(;d--;)o=a[d],i=o.parentNode,i&&(s[d]?r(i)||y(i,t):v(o))},Ye=function(e){var t=e.clipboardData,n=this.getSelection(),o=this.createElement("div"),i=this._root,r=this;this.saveUndoState(n),re||J||!t?setTimeout(function(){try{r._ensureBottomLine()}catch(e){r.didError(e)}},0):(Ee(n,i),o.appendChild(Se(n,i)),t.setData("text/html",o.innerHTML),t.setData("text/plain",o.innerText||o.textContent),e.preventDefault()),this.setSelection(n)},Xe=function(e){var t=e.clipboardData,n=this.getSelection(),o=this.createElement("div");re||J||!t||(o.appendChild(n.cloneContents()),t.setData("text/html",o.innerHTML),t.setData("text/plain",o.innerText||o.textContent),e.preventDefault())},Je=function(e){var t,n,o,i,r,a=e.clipboardData,s=a&&a.items,d=!1,l=!1,c=null,h=this;if(!re&&s){for(e.preventDefault(),t=s.length;t--;){if(n=s[t],o=n.type,"text/html"===o)return void n.getAsString(function(e){h.insertHTML(e,!0)});"text/plain"===o&&(c=n),/^image\/.*/.test(o)&&(l=!0)}return void(l?(this.fireEvent("dragover",{dataTransfer:a,preventDefault:function(){d=!0}}),d&&this.fireEvent("drop",{dataTransfer:a})):c&&n.getAsString(function(e){h.insertPlainText(e,!0)}))}if(i=a&&a.types,!re&&i&&(fe.call(i,"text/html")>-1||!ne&&fe.call(i,"text/plain")>-1&&fe.call(i,"text/rtf")<0))return e.preventDefault(),void((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 u=this._doc.body,f=this.getSelection(),p=f.startContainer,g=f.startOffset,m=f.endContainer,C=f.endOffset,_=this.createElement("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});u.appendChild(_),f.selectNodeContents(_),this.setSelection(f),setTimeout(function(){try{h._awaitingPaste=!1;for(var e,t,n="",o=_;_=o;)o=_.nextSibling,v(_),e=_.firstChild,e&&e===_.lastChild&&"DIV"===e.nodeName&&(_=e),n+=_.innerHTML;t=h._createRange(p,g,m,C),h.setSelection(t),n&&h.insertHTML(n,!0)}catch(i){h.didError(i)}},0)},et=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()},tt=[],nt=B.prototype;nt.setConfig=function(e){return e=A({blockTag:"DIV",blockAttributes:null,tagAttributes:{blockquote:null,ul:null,ol:null,li:null,a:null},undo:{documentSizeThreshold:-1,undoLimit:-1}},e,!0),e.blockTag=e.blockTag.toUpperCase(),this._config=e,this},nt.createElement=function(e,t,n){return N(this._doc,e,t,n)},nt.createDefaultBlock=function(e){var t=this._config;return S(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){this._ignoreAllChanges=!0,this._mutation&&this._mutation.disconnect(),e(),this._ignoreAllChanges=!1,this._mutation&&this._mutation.observe(this._root,{childList:!0,attributes:!0,characterData:!0,subtree:!0})};var ot={pathChange:1,select:1,input:1,undoStateChange:1};nt.fireEvent=function(e,t){var n,o,i,r=this._events[e];if(/^(?:focus|blur)/.test(e))if(n=p(this._root,this._doc.activeElement),"focus"===e){if(!n||this._isFocused)return this;this._isFocused=!0}else{if(n||!this._isFocused)return this;this._isFocused=!1}if(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(a){a.details="Squire: fireEvent error. Event type: "+e,this.didError(a)}}return this},nt.destroy=function(){var e,t=tt.length,n=this._events;for(e in n)this.removeEventListener(e);for(this._mutation&&this._mutation.disconnect();t--;)tt[t]===this&&tt.splice(t,1);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],o=this._root;return t?(n||(n=this._events[e]=[],ot[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)},nt.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],ot[e]||("selectionchange"===e&&(i=this._doc),i.removeEventListener(e,this,!0)))}return this},nt._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},nt.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=$,_e(e,t),o=t.getBoundingClientRect(),n=t.parentNode,n.removeChild(t),E(n,e)),o},nt._moveCursorTo=function(e){var t=this._root,n=this._createRange(t,e?0:t.childNodes.length);return be(n),this.setSelection(n),this},nt.moveCursorToStart=function(){return this._moveCursorTo(!0)},nt.moveCursorToEnd=function(){return this._moveCursorTo(!1)};var it=function(e){return e._win.getSelection()||null};nt.setSelection=function(e){if(e)if(this._lastSelection=e,this._isFocused)if(te&&!this._restoreSelection)O.call(this),this.blur(),this.focus();else{J&&this._win.focus();var t=it(this);t&&(t.removeAllRanges(),t.addRange(e))}else O.call(this);return this},nt.getSelection=function(){var e,t,n,o=it(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&&p(r,e.commonAncestorContainer)?this._lastSelection=e:e=this._lastSelection,e||(e=this._createRange(r.firstChild,0)),e},nt.getSelectedText=function(){var e,t=this.getSelection(),o=new n(t.commonAncestorContainer,z|H,function(e){return Te(t,e,!0)}),i=t.startContainer,a=t.endContainer,s=o.currentNode=i,d="",l=!1;for(o.filter(s)||(s=o.nextNode());s;)s.nodeType===w?(e=s.data,e&&/\S/.test(e)&&(s===a&&(e=e.slice(0,t.endOffset)),s===i&&(e=e.slice(t.startOffset)),d+=e,l=!0)):("BR"===s.nodeName||l&&!r(s))&&(d+="\n",l=!1),s=o.nextNode();return d},nt.getPath=function(){return this._path};var rt=function(e,t){for(var o,i,a,s=new n(e,z,function(){return!0},!1);i=s.nextNode();)for(;(a=i.data.indexOf($))>-1&&i.parentNode!==t;){if(1===i.length){do o=i.parentNode,o.removeChild(i),i=o,s.currentNode=o;while(r(i)&&!m(i));break}i.deleteData(a,1)}};nt._didAddZWS=function(){this._hasZWS=!0},nt._removeZWS=function(){this._hasZWS&&(rt(this._root),this._hasZWS=!1)},nt._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?g(i,this._root):"(selection)":"",this._path!==n&&(this._path=n,this.fireEvent("pathChange",{path:n}))),e.collapsed||this.fireEvent("select")},nt._updatePathOnEvent=function(){var e=this;e._willUpdatePath||(e._willUpdatePath=!0,setTimeout(function(){e._willUpdatePath=!1,e._updatePath(e.getSelection())},0))},nt.focus=function(){return this._root.focus(),this},nt.blur=function(){return this._root.blur(),this};var at="squire-selection-start",st="squire-selection-end";nt._saveRangeToBookmark=function(e){var t,n=this.createElement("INPUT",{id:at,type:"hidden"}),o=this.createElement("INPUT",{id:st,type:"hidden"});_e(e,n),e.collapse(!1),_e(e,o),n.compareDocumentPosition(o)&P&&(n.id=st,o.id=at,t=n,n=o,o=t),e.setStartAfter(n),e.setEndBefore(o)},nt._getRangeAndRemoveBookmark=function(e){var t=this._root,n=t.querySelector("#"+at),o=t.querySelector("#"+st);if(n&&o){var i=n.parentNode,r=o.parentNode,a=fe.call(i.childNodes,n),s=fe.call(r.childNodes,o);i===r&&(s-=1),v(n),v(o),e||(e=this._doc.createRange()),e.setStart(i,a),e.setEnd(r,s),E(i,e),i!==r&&E(r,e),e.collapsed&&(i=e.startContainer,i.nodeType===w&&(r=i.childNodes[e.startOffset],r&&r.nodeType===w||(r=i.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||!(16>t||t>20)||!(33>t||t>45)||this._docWasChanged()},nt._docWasChanged=function(){if(!this._ignoreAllChanges){if(he&&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){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}},nt.saveUndoState=function(e){return e===t&&(e=this.getSelection()),this._isInUndoState||(this._recordUndoState(e),this._getRangeAndRemoveBookmark(e)),this},nt.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},nt.redo=function(){var e=this._undoIndex,t=this._undoStackLength;if(t>e+1&&this._isInUndoState){this._undoIndex+=1,this._setHTML(this._undoStack[this._undoIndex]);var n=this._getRangeAndRemoveBookmark();n&&this.setSelection(n),this.fireEvent("undoStateChange",{canUndo:!0,canRedo:t>e+2}),this.fireEvent("input")}return this},nt.hasFormat=function(e,t,o){if(e=e.toUpperCase(),t||(t={}),!o&&!(o=this.getSelection()))return!1;!o.collapsed&&o.startContainer.nodeType===w&&o.startOffset===o.startContainer.length&&o.startContainer.nextSibling&&o.setStartBefore(o.startContainer.nextSibling), +!o.collapsed&&o.endContainer.nodeType===w&&0===o.endOffset&&o.endContainer.previousSibling&&o.setEndAfter(o.endContainer.previousSibling);var i,r,a=this._root,s=o.commonAncestorContainer;if(f(s,a,e,t))return!0;if(s.nodeType===w)return!1;i=new n(s,z,function(e){return Te(o,e,!0)},!1);for(var d=!1;r=i.nextNode();){if(!f(r,a,e,t))return!1;d=!0}return d},nt.getFontInfo=function(e){var n,o,i,r={color:t,backgroundColor:t,family:t,size:t},a=0;if(!e&&!(e=this.getSelection()))return r;if(n=e.commonAncestorContainer,e.collapsed||n.nodeType===w)for(n.nodeType===w&&(n=n.parentNode);4>a&&n;)(o=n.style)&&(!r.color&&(i=o.color)&&(r.color=i,a+=1),!r.backgroundColor&&(i=o.backgroundColor)&&(r.backgroundColor=i,a+=1),!r.family&&(i=o.fontFamily)&&(r.family=i,a+=1),!r.size&&(i=o.fontSize)&&(r.size=i,a+=1)),n=n.parentNode;return r},nt._addFormat=function(e,t,o){var i,a,s,d,l,c,h,u,p=this._root;if(o.collapsed){i=S(this.createElement(e,t),p),_e(o,i),o.setStart(i.firstChild,i.firstChild.length),o.collapse(!0);for(var g=i;r(g);)g=g.parentNode;rt(g,i)}else{if(a=new n(o.commonAncestorContainer,z|H,function(e){return(e.nodeType===w||"BR"===e.nodeName||"IMG"===e.nodeName)&&Te(o,e,!0)},!1),s=o.startContainer,l=o.startOffset,d=o.endContainer,c=o.endOffset,a.currentNode=s,a.filter(s)||(s=a.nextNode(),l=0),!s)return o;do h=a.currentNode,u=!f(h,p,e,t),u&&(h===d&&h.length>c&&h.splitText(c),h===s&&l&&(h=h.splitText(l),d===s&&(d=h,c-=l),s=h,l=0),i=this.createElement(e,t),C(h,i),i.appendChild(h));while(a.nextNode());d.nodeType!==w&&(h.nodeType===w?(d=h,c=h.length):(d=h.parentNode,c=1)),o=this._createRange(s,l,d,c)}return o},nt._removeFormat=function(e,t,n,o){this._saveRangeToBookmark(n);var i,a=this._doc;n.collapsed&&(le?(i=a.createTextNode($),this._didAddZWS()):i=a.createTextNode(""),_e(n,i));for(var s=n.commonAncestorContainer;r(s);)s=s.parentNode;var d=n.startContainer,l=n.startOffset,c=n.endContainer,h=n.endOffset,f=[],p=function(e,t){if(!Te(n,e,!1)){var o,i,r=e.nodeType===w;if(!Te(n,e,!0))return void("INPUT"===e.nodeName||r&&!e.data||f.push([t,e]));if(r)e===c&&h!==e.length&&f.push([t,e.splitText(h)]),e===d&&l&&(e.splitText(l),f.push([t,e]));else for(o=e.firstChild;o;o=i)i=o.nextSibling,p(o,t)}},g=Array.prototype.filter.call(s.getElementsByTagName(e),function(o){return Te(n,o,!0)&&u(o,e,t)});return o||g.forEach(function(e){p(e,e)}),f.forEach(function(e){var t=e[0].cloneNode(!1),n=e[1];C(n,t),t.appendChild(n)}),g.forEach(function(e){C(e,_(e))}),this._getRangeAndRemoveBookmark(n),i&&n.collapse(!1),E(s,n),n},nt.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),he||this._docWasChanged(),this):this};var dt={DT:"DD",DD:"DT",LI:"LI"},lt=function(e,t,n,o){var i=dt[t.nodeName],r=null,a=T(n,o,t.parentNode,e._root),s=e._config;return i||(i=s.blockTag,r=s.blockAttributes),u(a,i,r)||(t=N(a.ownerDocument,i,r),a.dir&&(t.dir=a.dir),C(a,t),t.appendChild(_(a)),a=t),a};nt.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var o=this._root,i=xe(n,o),r=ke(n,o);if(i&&r)do if(e(i)||i===r)break;while(i=c(i,o));return t&&(this.setSelection(n),this._updatePath(n,!0),he||this._docWasChanged()),this},nt.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 Oe(t,o),Ee(t,o),n=Ne(t,o,o),_e(t,e.call(this,n)),t.endOffsett;t+=1){for(i=d[t],r=_(i),a=r.childNodes,o=a.length;o--;)s=a[o],C(s,_(s));y(r,this._root),C(i,r)}return e},vt=function(e){var t,n,o,i,r,a,d=e.querySelectorAll("LI"),l=this._config.tagAttributes,c=l.li;for(t=0,n=d.length;n>t;t+=1)o=d[t],s(o.firstChild)||(i=o.parentNode.nodeName,r=o.previousSibling,r&&(r=r.lastChild)&&r.nodeName===i||(a=l[i.toLowerCase()],C(o,this.createElement("LI",c,[r=this.createElement(i,a)]))),r.appendChild(o));return e},Ct=function(e){var t=this._root,n=e.querySelectorAll("LI");return Array.prototype.filter.call(n,function(e){return!s(e.firstChild)}).forEach(function(n){var o,i=n.parentNode,r=i.parentNode,a=n.firstChild,d=a;for(n.previousSibling&&(i=T(i,n,r,t));d&&(o=d.nextSibling,!s(d));)r.insertBefore(d,i),d=o;for("LI"===r.nodeName&&a.previousSibling&&T(r,a,r.parentNode,t);n!==e&&!n.childNodes.length;)i=n.parentNode,i.removeChild(n),n=i},this),y(e,t),e};nt._ensureBottomLine=function(){var e=this._root,t=e.lastElementChild;t&&t.nodeName===this._config.blockTag&&a(t)||e.appendChild(this.createDefaultBlock())},nt.setKeyHandler=function(e,t){return this._keyHandlers[e]=t,this},nt._getHTML=function(){return this._root.innerHTML},nt._setHTML=function(e){var t=this._root,n=t;n.innerHTML=e;do S(n,t);while(n=c(n,t));this._ignoreChange=!0},nt.getHTML=function(e){var t,n,o,i,r,a,s=[];if(e&&(a=this.getSelection())&&this._saveRangeToBookmark(a),de)for(t=this._root,n=t;n=c(n,t);)n.textContent||n.querySelector("BR")||(o=this.createElement("BR"),n.appendChild(o),s.push(o));if(i=this._getHTML().replace(/\u200B/g,""),de)for(r=s.length;r--;)v(s[r]);return a&&this._getRangeAndRemoveBookmark(a),i},nt.setHTML=function(e){var t,n=this._doc.createDocumentFragment(),o=this.createElement("DIV"),i=this._root;o.innerHTML=e,n.appendChild(_(o)),Ge(n),$e(n,i),y(n,i);for(var r=n;r=c(r,i);)S(r,i);for(this._ignoreChange=!0;t=i.lastChild;)i.removeChild(t);i.appendChild(n),S(i,i),this._undoIndex=-1,this._undoStack.length=0,this._undoStackLength=0,this._isInUndoState=!1;var a=this._getRangeAndRemoveBookmark()||this._createRange(i.firstChild,0);return this.saveUndoState(a),this._lastSelection=a,O.call(this),this._updatePath(a,!0),this},nt.insertElement=function(e,t){if(t||(t=this.getSelection()),t.collapse(!0),r(e))_e(t,e),t.setStartAfter(e);else{for(var n,o,i=this._root,a=xe(t,i)||i;a!==i&&!a.nextSibling;)a=a.parentNode;a!==i&&(n=a.parentNode,o=T(n,a.nextSibling,i,i)),o?i.insertBefore(e,o):(i.appendChild(e),o=this.createDefaultBlock(),i.appendChild(o)),t.setStart(o,0),t.setEnd(o,0),be(t)}return this.focus(),this.setSelection(t),this._updatePath(t),he||this._docWasChanged(),this},nt.insertImage=function(e,t){var n=this.createElement("IMG",A({src:e},t,!0));return this.insertElement(n),n};var _t=/\b((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))|([\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,}\b)/i,Nt=function(e,t,o){for(var i,r,a,s,d,l,c,h=e.ownerDocument,u=new n(e,z,function(e){return!f(e,t,"A")},!1),p=o._config.tagAttributes.a;i=u.nextNode();)for(r=i.data,a=i.parentNode;s=_t.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]},p,!1)),c.textContent=r.slice(d,l),a.insertBefore(c,i),i.data=r=r.slice(l)};nt.insertHTML=function(e,t){var n,o,i,r,a,s,d,l=this.getSelection(),h=this._doc;"undefined"!=typeof DOMPurify&&DOMPurify.isSupported?(r=DOMPurify.sanitize(e,{WHOLE_DOCUMENT:!1,RETURN_DOM:!0,RETURN_DOM_FRAGMENT:!0}),r=h.importNode(r,!0)):(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=h.createDocumentFragment(),r.appendChild(_(i))),this.saveUndoState(l);try{for(a=this._root,s=r,d={fragment:r,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},Nt(r,r,this),Ge(r),$e(r,null),je(r),r.normalize();s=c(s,r);)S(s,null);t&&this.fireEvent("willPaste",d),d.defaultPrevented||(ye(l,d.fragment,a),he||this._docWasChanged(),l.collapse(!1),this._ensureBottomLine()),this.setSelection(l),this._updatePath(l,!0),t&&this.focus()}catch(u){this.didError(u)}return this};var St=function(e){return e.split("&").join("&").split("<").join("<").split(">").join(">").split('"').join(""")};nt.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+'="'+St(l[n])+'"';for(h+=">",o=0,i=a.length;i>o;o+=1)r=a[o],r=St(r).replace(/ (?= )/g," "),o&&i>o+1&&(r=h+(r||"
")+c),a[o]=r;return this.insertHTML(a.join(""),t)};var yt=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=yt("changeFormat",{tag:"B"}),nt.italic=yt("changeFormat",{tag:"I"}),nt.underline=yt("changeFormat",{tag:"U"}),nt.strikethrough=yt("changeFormat",{tag:"S"}),nt.subscript=yt("changeFormat",{tag:"SUB"},{tag:"SUP"}),nt.superscript=yt("changeFormat",{tag:"SUP"},{tag:"SUB"}),nt.removeBold=yt("changeFormat",null,{tag:"B"}),nt.removeItalic=yt("changeFormat",null,{tag:"I"}),nt.removeUnderline=yt("changeFormat",null,{tag:"U"}),nt.removeStrikethrough=yt("changeFormat",null,{tag:"S"}),nt.removeSubscript=yt("changeFormat",null,{tag:"SUB"}),nt.removeSuperscript=yt("changeFormat",null,{tag:"SUP"}),nt.makeLink=function(e,t){var n=this.getSelection();if(n.collapsed){var o=e.indexOf(":")+1;if(o)for(;"/"===e[o];)o+=1;_e(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()},nt.removeLink=function(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0),this.focus()},nt.setFontFace=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{"class":"font",style:"font-family: "+e+", sans-serif;"}}:null,{tag:"SPAN",attributes:{"class":"font"}}),this.focus()},nt.setFontSize=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{"class":"size",style:"font-size: "+("number"==typeof e?e+"px":e)}}:null,{tag:"SPAN",attributes:{"class":"size"}}),this.focus()},nt.setTextColour=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{"class":"colour",style:"color:"+e}}:null,{tag:"SPAN",attributes:{"class":"colour"}}),this.focus()},nt.setHighlightColour=function(e){return this.changeFormat(e?{tag:"SPAN",attributes:{"class":"highlight",style:"background-color:"+e}}:e,{tag:"SPAN",attributes:{"class":"highlight"}}),this.focus()},nt.setTextAlignment=function(e){return this.forEachBlock(function(t){t.className=(t.className.split(/\s+/).filter(function(e){return!/align/.test(e)}).join(" ")+" align-"+e).trim(),t.style.textAlign=e},!0),this.focus()},nt.setTextDirection=function(e){return this.forEachBlock(function(t){t.dir=e},!0),this.focus()},nt.removeAllFormatting=function(e){if(!e&&!(e=this.getSelection())||e.collapsed)return this;for(var t=this._root,n=e.commonAncestorContainer;n&&!a(n);)n=n.parentNode;if(n||(Oe(e,t),n=t),n.nodeType===w)return this;this.saveUndoState(e),Ee(e,n);for(var o,i,r=n.ownerDocument,s=e.startContainer,d=e.startOffset,l=e.endContainer,c=e.endOffset,h=r.createDocumentFragment(),u=r.createDocumentFragment(),f=T(l,c,n,t),p=T(s,d,n,t);p!==f;)o=p.nextSibling,h.appendChild(p),p=o;return U(this,h,u),u.normalize(),p=u.firstChild,o=u.lastChild,i=n.childNodes,p?(n.insertBefore(u,f),d=fe.call(i,p),c=fe.call(i,o)+1):(d=fe.call(i,f),c=d),e.setStart(n,d),e.setEnd(n,c),E(n,e),be(e),this.setSelection(e),this._updatePath(e,!0),this.focus()},nt.increaseQuoteLevel=yt("modifyBlocks",ct),nt.decreaseQuoteLevel=yt("modifyBlocks",ht),nt.makeUnorderedList=yt("modifyBlocks",pt),nt.makeOrderedList=yt("modifyBlocks",gt),nt.removeList=yt("modifyBlocks",mt),nt.increaseListLevel=yt("modifyBlocks",vt),nt.decreaseListLevel=yt("modifyBlocks",Ct),"object"==typeof exports?module.exports=B:"function"==typeof define&&define.amd?define(function(){return B}):(Y.Squire=B,top!==Y&&"true"===e.documentElement.getAttribute("data-squireinit")&&(Y.editor=new B(e),Y.onEditorLoad&&(Y.onEditorLoad(Y.editor),Y.onEditorLoad=null)))}(document); \ No newline at end of file diff --git a/source/Editor.js b/source/Editor.js index 66a2229..aaad5e2 100644 --- a/source/Editor.js +++ b/source/Editor.js @@ -546,13 +546,18 @@ proto.getPath = function () { // WebKit bug: https://bugs.webkit.org/show_bug.cgi?id=15256 -var removeZWS = function ( root ) { +// Walk down the tree starting at the root and remove any ZWS. If the node only contained +// ZWS space then remove it too. +// +// We may want to keep one ZWS node at the bottom of the tree so the block can be selected. Define that +// node as the keepNode. +var removeZWS = function ( root, keepNode ) { var walker = new TreeWalker( root, SHOW_TEXT, function () { return true; }, false ), parent, node, index; while ( node = walker.nextNode() ) { - while ( ( index = node.data.indexOf( ZWS ) ) > -1 ) { + while ( ( index = node.data.indexOf( ZWS ) ) > -1 && node.parentNode !== keepNode ) { if ( node.length === 1 ) { do { parent = node.parentNode; @@ -957,6 +962,15 @@ proto._addFormat = function ( tag, attributes, range ) { insertNodeInRange( range, el ); range.setStart( el.firstChild, el.firstChild.length ); range.collapse( true ); + + // Clean up any previous formats that may have been set on this block + // that are unused. + var block = el; + while ( isInline( block ) ) { + block = block.parentNode; + } + + removeZWS( block, el ); } // Otherwise we find all the textnodes in the range (splitting // partially selected nodes) and if they're not already formatted