mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 15:23:29 -05:00
1 line
No EOL
51 KiB
JavaScript
1 line
No EOL
51 KiB
JavaScript
!function(e,t){"use strict";var n=1,i=3,o=9,r=11,a=1,s="",d=e.defaultView,l=navigator.userAgent,c=(/Android/.test(l),/Mac OS X/.test(l)),h=/Windows NT/.test(l),f=/iP(?:ad|hone|od)/.test(l)||c&&!!navigator.maxTouchPoints,u=/Gecko\//.test(l),p=/Edge\//.test(l),g=!p&&/WebKit\//.test(l),m=/Trident\/[4567]\./.test(l),v=c?"meta-":"ctrl-",C=g,_="undefined"!=typeof MutationObserver,N="undefined"!=typeof WeakMap,S=/[^ \t\r\n]/,y=Array.prototype.indexOf,T={1:1,2:2,3:4,8:128,9:256,11:1024},E=function(){return!0};function L(e,t,n){this.root=this.currentNode=e,this.nodeType=t,this.filter=n||E}L.prototype.nextNode=function(){for(var e,t=this.currentNode,n=this.root,i=this.nodeType,o=this.filter;;){for(e=t.firstChild;!e&&t&&t!==n;)(e=t.nextSibling)||(t=t.parentNode);if(!e)return null;if(T[e.nodeType]&i&&o(e))return this.currentNode=e,e;t=e}},L.prototype.previousNode=function(){for(var e,t=this.currentNode,n=this.root,i=this.nodeType,o=this.filter;;){if(t===n)return null;if(e=t.previousSibling)for(;t=e.lastChild;)e=t;else e=t.parentNode;if(!e)return null;if(T[e.nodeType]&i&&o(e))return this.currentNode=e,e;t=e}},L.prototype.previousPONode=function(){for(var e,t=this.currentNode,n=this.root,i=this.nodeType,o=this.filter;;){for(e=t.lastChild;!e&&t&&t!==n;)(e=t.previousSibling)||(t=t.parentNode);if(!e)return null;if(T[e.nodeType]&i&&o(e))return this.currentNode=e,e;t=e}};var b=/^(?:#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)$/,k={BR:1,HR:1,IFRAME:1,IMG:1,INPUT:1};var x=0,O=1,A=2,B=3,D=N?new WeakMap:null;function R(e){return e.nodeType===n&&!!k[e.nodeName]}function P(e){switch(e.nodeType){case i:return O;case n:case r:if(N&&D.has(e))return D.get(e);break;default:return x}var t;return t=function(e,t){for(var n=e.length;n--;)if(!t(e[n]))return!1;return!0}(e.childNodes,U)?b.test(e.nodeName)?O:A:B,N&&D.set(e,t),t}function U(e){return P(e)===O}function I(e){return P(e)===A}function F(e){return P(e)===B}function w(e,t){var n=new L(t,a,I);return n.currentNode=e,n}function M(e,t){return(e=w(e,t).previousNode())!==t?e:null}function H(e,t){return(e=w(e,t).nextNode())!==t?e:null}function z(e){return!e.textContent&&!e.querySelector("IMG")}function W(e,t){return!R(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 q(e,t,n){if(e.nodeName!==t)return!1;for(var i in n)if(e.getAttribute(i)!==n[i])return!1;return!0}function K(e,t,n,i){for(;e&&e!==t;){if(q(e,n,i))return e;e=e.parentNode}return null}function G(e,t){for(;t;){if(t===e)return!0;t=t.parentNode}return!1}function Z(e){var t=e.nodeType;return t===n||t===r?e.childNodes.length:e.length||0}function Q(e){var t=e.parentNode;return t&&t.removeChild(e),e}function $(e,t){var n=e.parentNode;n&&n.replaceChild(t,e)}function j(e){for(var t=e.ownerDocument.createDocumentFragment(),n=e.childNodes,i=n?n.length:0;i--;)t.appendChild(e.firstChild);return t}function V(e,n,i,o){var r,a,s,d,l=e.createElement(n);if(i instanceof Array&&(o=i,i=null),i)for(r in i)(a=i[r])!==t&&l.setAttribute(r,a);if(o)for(s=0,d=o.length;s<d;s+=1)l.appendChild(o[s]);return l}function Y(e,t){var n,o,r=t.__squire__,a=e.ownerDocument,d=e;if(e===t&&((o=e.firstChild)&&"BR"!==o.nodeName||(n=r.createDefaultBlock(),o?e.replaceChild(n,o):e.appendChild(n),e=n,n=null)),e.nodeType===i)return d;if(U(e)){for(o=e.firstChild;C&&o&&o.nodeType===i&&!o.data;)e.removeChild(o),o=e.firstChild;o||(C?(n=a.createTextNode(s),r._didAddZWS()):n=a.createTextNode(""))}else if(!e.querySelector("BR"))for(n=V(a,"BR");(o=e.lastElementChild)&&!U(o);)e=o;if(n)try{e.appendChild(n)}catch(t){r.didError({name:"Squire: fixCursor – "+t,message:"Parent: "+e.nodeName+"/"+e.innerHTML+" appendChild: "+n.nodeName})}return d}function X(e,t){var n,i,o,r,a=e.childNodes,s=e.ownerDocument,d=null;for(n=0,i=a.length;n<i;n+=1)!(r="BR"===(o=a[n]).nodeName)&&U(o)?(d||(d=V(s,"div")),d.appendChild(o),n-=1,i-=1):(r||d)&&(d||(d=V(s,"div")),Y(d,t),r?e.replaceChild(d,o):(e.insertBefore(d,o),n+=1,i+=1),d=null),F(o)&&X(o,t);return d&&e.appendChild(Y(d,t)),e}function J(e,t,o,r){var a,s,d,l=e.nodeType;if(l===i&&e!==o)return J(e.parentNode,e.splitText(t),o,r);if(l===n){if("number"==typeof t&&(t=t<e.childNodes.length?e.childNodes[t]:null),e===o)return t;for(a=e.parentNode,s=e.cloneNode(!1);t;)d=t.nextSibling,s.appendChild(t),t=d;return"OL"===e.nodeName&&K(e,r,"BLOCKQUOTE")&&(s.start=(+e.start||1)+e.childNodes.length-1),Y(e,r),Y(s,r),(d=e.nextSibling)?a.insertBefore(s,d):a.appendChild(s),J(a,s,o,r)}return t}function ee(e,t){if(e.nodeType===i&&(e=e.parentNode),e.nodeType===n){var o={startContainer:t.startContainer,startOffset:t.startOffset,endContainer:t.endContainer,endOffset:t.endOffset};!function e(t,o){for(var r,a,s,d=t.childNodes,l=d.length,c=[];l--;)if(r=d[l],a=l&&d[l-1],l&&U(r)&&W(r,a)&&!k[r.nodeName])o.startContainer===r&&(o.startContainer=a,o.startOffset+=Z(a)),o.endContainer===r&&(o.endContainer=a,o.endOffset+=Z(a)),o.startContainer===t&&(o.startOffset>l?o.startOffset-=1:o.startOffset===l&&(o.startContainer=a,o.startOffset=Z(a))),o.endContainer===t&&(o.endOffset>l?o.endOffset-=1:o.endOffset===l&&(o.endContainer=a,o.endOffset=Z(a))),Q(r),r.nodeType===i?a.appendData(r.data):c.push(j(r));else if(r.nodeType===n){for(s=c.length;s--;)r.appendChild(c.pop());e(r,o)}}(e,o),t.setStart(o.startContainer,o.startOffset),t.setEnd(o.endContainer,o.endOffset)}}function te(e,t,i,o){for(var r,a,s,d=t;(r=d.parentNode)&&r!==o&&r.nodeType===n&&1===r.childNodes.length;)d=r;Q(d),s=e.childNodes.length,(a=e.lastChild)&&"BR"===a.nodeName&&(e.removeChild(a),s-=1),e.appendChild(j(t)),i.setStart(e,s),i.collapse(!0),ee(e,i)}function ne(e,t){var n,i,o=e.previousSibling,r=e.firstChild,a=e.ownerDocument,s="LI"===e.nodeName;if(!s||r&&/^[OU]L$/.test(r.nodeName))if(o&&W(o,e)){if(!F(o)){if(!s)return;(i=V(a,"DIV")).appendChild(j(o)),o.appendChild(i)}Q(e),n=!F(e),o.appendChild(j(e)),n&&X(o,t),r&&ne(r,t)}else s&&(o=V(a,"DIV"),e.insertBefore(o,r),Y(o,t))}var ie=function(e,t){for(var i=e.childNodes;t&&e.nodeType===n;)t=(i=(e=i[t-1]).childNodes).length;return e},oe=function(e,t){if(e.nodeType===n){var i=e.childNodes;if(t<i.length)e=i[t];else{for(;e&&!e.nextSibling;)e=e.parentNode;e&&(e=e.nextSibling)}}return e},re=function(e,t){var n,o,r,a,s=e.startContainer,d=e.startOffset,l=e.endContainer,c=e.endOffset;s.nodeType===i?(o=(n=s.parentNode).childNodes,d===s.length?(d=y.call(o,s)+1,e.collapsed&&(l=n,c=d)):(d&&(a=s.splitText(d),l===s?(c-=d,l=a):l===n&&(c+=1),s=a),d=y.call(o,s)),s=n):o=s.childNodes,d===(r=o.length)?s.appendChild(t):s.insertBefore(t,o[d]),s===l&&(c+=o.length-r),e.setStart(s,d),e.setEnd(l,c)},ae=function(e,t,n){var o=e.startContainer,r=e.startOffset,a=e.endContainer,s=e.endOffset;t||(t=e.commonAncestorContainer),t.nodeType===i&&(t=t.parentNode);for(var d,l,c,h,f,u=J(a,s,t,n),p=J(o,r,t,n),g=t.ownerDocument.createDocumentFragment();p!==u;)d=p.nextSibling,g.appendChild(p),p=d;return o=t,r=u?y.call(t.childNodes,u):t.childNodes.length,(l=(c=t.childNodes[r])&&c.previousSibling)&&l.nodeType===i&&c.nodeType===i&&(o=l,r=l.length,h=l.data,f=c.data," "===h.charAt(h.length-1)&&" "===f.charAt(0)&&(f=" "+f.slice(1)),l.appendData(f),Q(c)),e.setStart(o,r),e.collapse(!0),Y(t,n),g},se=function(e,t){var n,i,o=ue(e,t),r=pe(e,t),a=o!==r;return ce(e),he(e,o,r,t),n=ae(e,null,t),ce(e),a&&(r=pe(e,t),o&&r&&o!==r&&te(o,r,e,t)),o&&Y(o,t),(i=t.firstChild)&&"BR"!==i.nodeName?e.collapse(!0):(Y(t,t),e.selectNodeContents(t.firstChild)),n},de=function(e,t,n){var i,o,r,a,s,d,l,c,h,f,u,p=t.firstChild&&U(t.firstChild);for(X(t,n),i=t;i=H(i,n);)Y(i,n);if(e.collapsed||se(e,n),ce(e),e.collapse(!1),a=K(e.endContainer,n,"BLOCKQUOTE")||n,o=ue(e,n),c=H(t,t),l=!p&&!!o&&z(o),o&&c&&!l&&!K(c,t,"PRE")&&!K(c,t,"TABLE")){if(he(e,o,o,n),e.collapse(!0),s=e.endContainer,d=e.endOffset,He(o,n,!1),U(s)&&(s=(h=J(s,d,M(s,n),n)).parentNode,d=y.call(s.childNodes,h)),d!==Z(s))for(r=n.ownerDocument.createDocumentFragment();i=s.childNodes[d];)r.appendChild(i);te(s,c,e,n),d=y.call(s.parentNode.childNodes,s)+1,s=s.parentNode,e.setEnd(s,d)}Z(t)&&(l&&(e.setEndBefore(o),e.collapse(!1),Q(o)),he(e,a,a,n),f=(h=J(e.endContainer,e.endOffset,a,n))?h.previousSibling:a.lastChild,a.insertBefore(t,h),h?e.setEndBefore(h):e.setEnd(a,Z(a)),o=pe(e,n),ce(e),s=e.endContainer,d=e.endOffset,h&&F(h)&&ne(h,n),(h=f&&f.nextSibling)&&F(h)&&ne(h,n),e.setEnd(s,d)),r&&(te(o,r,u=e.cloneRange(),n),e.setEnd(u.endContainer,u.endOffset)),ce(e)},le=function(e,t,n){var i=t.ownerDocument.createRange();if(i.selectNode(t),n){var o=e.compareBoundaryPoints(3,i)>-1,r=e.compareBoundaryPoints(1,i)<1;return!o&&!r}var a=e.compareBoundaryPoints(0,i)<1,s=e.compareBoundaryPoints(2,i)>-1;return a&&s},ce=function(e){for(var t,n=e.startContainer,o=e.startOffset,r=e.endContainer,a=e.endOffset,s=!0;n.nodeType!==i&&(t=n.childNodes[o])&&!R(t);)n=t,o=0;if(a)for(;r.nodeType!==i;){if(!(t=r.childNodes[a-1])||R(t)){if(s&&t&&"BR"===t.nodeName){a-=1,s=!1;continue}break}a=Z(r=t)}else for(;r.nodeType!==i&&(t=r.firstChild)&&!R(t);)r=t;e.collapsed?(e.setStart(r,a),e.setEnd(n,o)):(e.setStart(n,o),e.setEnd(r,a))},he=function(e,t,n,o){var r,a=e.startContainer,s=e.startOffset,d=e.endContainer,l=e.endOffset,c=!0;for(t||(t=e.commonAncestorContainer),n||(n=t);!s&&a!==t&&a!==o;)r=a.parentNode,s=y.call(r.childNodes,a),a=r;for(;d!==n&&d!==o&&(c&&d.nodeType!==i&&d.childNodes[l]&&"BR"===d.childNodes[l].nodeName&&(l+=1,c=!1),l===Z(d));)r=d.parentNode,l=y.call(r.childNodes,d)+1,d=r;e.setStart(a,s),e.setEnd(d,l)},fe=function(e,t,n){var i=K(e.endContainer,n,"A");if(i){var o=e.cloneRange();i=i.parentNode,he(o,i,i,n),o.endContainer===i&&(e.setStart(o.endContainer,o.endOffset),e.setEnd(o.endContainer,o.endOffset))}return e},ue=function(e,t){var n,i=e.startContainer;return(n=U(i)?M(i,t):i!==t&&I(i)?i:H(n=ie(i,e.startOffset),t))&&le(e,n,!0)?n:null},pe=function(e,t){var n,i,o=e.endContainer;if(U(o))n=M(o,t);else if(o!==t&&I(o))n=o;else{if(!(n=oe(o,e.endOffset))||!G(t,n))for(n=t;i=n.lastChild;)n=i;n=M(n,t)}return n&&le(e,n,!0)?n:null},ge=new L(null,4|a,function(e){return e.nodeType===i?S.test(e.data):"IMG"===e.nodeName}),me=function(e,t){var n,o=e.startContainer,r=e.startOffset;if(ge.root=null,o.nodeType===i){if(r)return!1;n=o}else if((n=oe(o,r))&&!G(t,n)&&(n=null),!n&&(n=ie(o,r)).nodeType===i&&n.length)return!1;return ge.currentNode=n,ge.root=ue(e,t),!ge.previousNode()},ve=function(e,t){var n,o=e.endContainer,r=e.endOffset;if(ge.root=null,o.nodeType===i){if((n=o.data.length)&&r<n)return!1;ge.currentNode=o}else ge.currentNode=ie(o,r);return ge.root=pe(e,t),!ge.nextNode()},Ce=function(e,t){var n,i=ue(e,t),o=pe(e,t);i&&o&&(n=i.parentNode,e.setStart(n,y.call(n.childNodes,i)),n=o.parentNode,e.setEnd(n,y.call(n.childNodes,o)+1))},_e={8:"backspace",9:"tab",13:"enter",32:"space",33:"pageup",34:"pagedown",37:"left",39:"right",46:"delete",191:"/",219:"[",220:"\\",221:"]"},Ne=function(e){var t=e.keyCode,n=_e[t],i="",o=this.getSelection();e.defaultPrevented||(n||(n=String.fromCharCode(t).toLowerCase(),/^[A-Za-z0-9]$/.test(n)||(n="")),111<t&&t<124&&(n="f"+(t-111)),"backspace"!==n&&"delete"!==n&&(e.altKey&&(i+="alt-"),e.ctrlKey&&(i+="ctrl-"),e.metaKey&&(i+="meta-"),e.shiftKey&&(i+="shift-")),h&&e.shiftKey&&"delete"===n&&(i+="shift-"),n=i+n,this._keyHandlers[n]?this._keyHandlers[n](this,e,o):o.collapsed||e.isComposing||e.ctrlKey||e.metaKey||1!==(e.key||n).length||(this.saveUndoState(o),se(o,this._root),this._ensureBottomLine(),this.setSelection(o),this._updatePath(o,!0)))},Se=function(e){return function(t,n){n.preventDefault(),t[e]()}},ye=function(e,t){return t=t||null,function(n,i){i.preventDefault();var o=n.getSelection();n.hasFormat(e,null,o)?n.changeFormat(null,{tag:e},o):n.changeFormat({tag:e},t,o)}},Te=function(e,t){try{t||(t=e.getSelection());var n,o=t.startContainer;for(o.nodeType===i&&(o=o.parentNode),n=o;U(n)&&(!n.textContent||n.textContent===s);)n=(o=n).parentNode;o!==n&&(t.setStart(n,y.call(n.childNodes,o)),t.collapse(!0),n.removeChild(o),I(n)||(n=M(n,e._root)),Y(n,e._root),ce(t)),o===e._root&&(o=o.firstChild)&&"BR"===o.nodeName&&Q(o),e._ensureBottomLine(),e.setSelection(t),e._updatePath(t,!0)}catch(t){e.didError(t)}},Ee=function(e,t){for(var n;(n=e.parentNode)&&n!==t&&!n.isContentEditable;)e=n;Q(e)},Le=function(e,t,o){var r,a,d,l,c,h=e._root;if(e._recordUndoState(o),e._config.addLinks&&ht(o.startContainer,h,e),e._removeZWS(),e._getRangeAndRemoveBookmark(o),o.collapsed||se(o,h),(r=ue(o,h))&&(a=K(r,h,"PRE")))return ce(o),d=o.startContainer,l=o.startOffset,d.nodeType!==i&&(d=e._doc.createTextNode(""),a.insertBefore(d,a.firstChild)),t||"\n"!==d.data.charAt(l-1)&&!me(o,h)||"\n"!==d.data.charAt(l)&&!ve(o,h)?(d.insertData(l,"\n"),Y(a,h),d.length===l+1?o.setStartAfter(d):o.setStart(d,l+1)):(d.deleteData(l&&l-1,l?2:1),(d=(c=J(d,l&&l-1,h,h)).previousSibling).textContent||Q(d),d=e.createDefaultBlock(),c.parentNode.insertBefore(d,c),c.textContent||Q(c),o.setStart(d,0)),o.collapse(!0),e.setSelection(o),e._updatePath(o,!0),void e._docWasChanged();if(!r||t||/^T[HD]$/.test(r.nodeName))return fe(o,0,h),re(o,e.createElement("BR")),o.collapse(!1),e.setSelection(o),void e._updatePath(o,!0);if((a=K(r,h,"LI"))&&(r=a),z(r)){if(K(r,h,"UL")||K(r,h,"OL"))return e.decreaseListLevel(o);if(K(r,h,"BLOCKQUOTE"))return e.modifyBlocks(dt,o)}for(c=at(e,r,o.startContainer,o.startOffset),it(r),Fe(r),Y(r,h);c.nodeType===n;){var f,u=c.firstChild;if("A"===c.nodeName&&(!c.textContent||c.textContent===s)){$(c,u=e._doc.createTextNode("")),c=u;break}for(;u&&u.nodeType===i&&!u.data&&(f=u.nextSibling)&&"BR"!==f.nodeName;)Q(u),u=f;if(!u||"BR"===u.nodeName||u.nodeType===i)break;c=u}o=e.createRange(c,0),e.setSelection(o),e._updatePath(o,!0)},be={enter:f?function(e,t,n){e._saveRangeToBookmark(n);var i=e._getHTML(),o=function(){e.removeEventListener("keyup",o),e._setHTML(i),n=e._getRangeAndRemoveBookmark(),Le(e,!1,n)};e.addEventListener("keyup",o)}:function(e,t,n){t.preventDefault(),Le(e,t.shiftKey,n)},"shift-enter":function(e,t,n){return e._keyHandlers.enter(e,t,n)},backspace:function(e,t,n){var i=e._root;if(e._removeZWS(),e.saveUndoState(n),n.collapsed)if(me(n,i)){t.preventDefault();var o,r=ue(n,i);if(!r)return;if(X(r.parentNode,i),o=M(r,i)){if(!o.isContentEditable)return void Ee(o,i);for(te(o,r,n,i),r=o.parentNode;r!==i&&!r.nextSibling;)r=r.parentNode;r!==i&&(r=r.nextSibling)&&ne(r,i),e.setSelection(n)}else if(r){if(K(r,i,"UL")||K(r,i,"OL"))return e.decreaseListLevel(n);if(K(r,i,"BLOCKQUOTE"))return e.modifyBlocks(st,n);e.setSelection(n),e._updatePath(n,!0)}}else e.setSelection(n),setTimeout(function(){Te(e)},0);else t.preventDefault(),se(n,i),Te(e,n)},delete:function(e,t,i){var o,r,a,s,d,l,c=e._root;if(e._removeZWS(),e.saveUndoState(i),i.collapsed)if(ve(i,c)){if(t.preventDefault(),!(o=ue(i,c)))return;if(X(o.parentNode,c),r=H(o,c)){if(!r.isContentEditable)return void Ee(r,c);for(te(o,r,i,c),r=o.parentNode;r!==c&&!r.nextSibling;)r=r.parentNode;r!==c&&(r=r.nextSibling)&&ne(r,c),e.setSelection(i),e._updatePath(i,!0)}}else{if(a=i.cloneRange(),he(i,c,c,c),s=i.endContainer,d=i.endOffset,s.nodeType===n&&(l=s.childNodes[d])&&"IMG"===l.nodeName)return t.preventDefault(),Q(l),ce(i),void Te(e,i);e.setSelection(a),setTimeout(function(){Te(e)},0)}else t.preventDefault(),se(i,c),Te(e,i)},tab:function(e,t,n){var i,o,r=e._root;if(e._removeZWS(),n.collapsed&&me(n,r))for(i=ue(n,r);o=i.parentNode;){if("UL"===o.nodeName||"OL"===o.nodeName){t.preventDefault(),e.increaseListLevel(n);break}i=o}},"shift-tab":function(e,t,n){var i,o=e._root;e._removeZWS(),n.collapsed&&me(n,o)&&(K(i=n.startContainer,o,"UL")||K(i,o,"OL"))&&(t.preventDefault(),e.decreaseListLevel(n))},space:function(e,t,n){var i,o=e._root;if(e._recordUndoState(n),e._config.addLinks&&ht(n.startContainer,o,e),e._getRangeAndRemoveBookmark(n),i=n.endContainer,n.collapsed&&n.endOffset===Z(i))do{if("A"===i.nodeName){n.setStartAfter(i);break}}while(!i.nextSibling&&(i=i.parentNode)&&i!==o);n.collapsed||(se(n,o),e._ensureBottomLine(),e.setSelection(n),e._updatePath(n,!0)),e.setSelection(n)},left:function(e){e._removeZWS()},right:function(e){e._removeZWS()}};c&&u&&(be["meta-left"]=function(e,t){t.preventDefault();var n=Je(e);n&&n.modify&&n.modify("move","backward","lineboundary")},be["meta-right"]=function(e,t){t.preventDefault();var n=Je(e);n&&n.modify&&n.modify("move","forward","lineboundary")}),c||(be.pageup=function(e){e.moveCursorToStart()},be.pagedown=function(e){e.moveCursorToEnd()});const ke=function(e,t){return function(n,i){i.preventDefault();var o=n.getPath();/(?:^|>)BLOCKQUOTE/.test(o)||!/(?:^|>)[OU]L/.test(o)?n[e]():n[t]()}},xe=function(e,t){return function(n,i){i.preventDefault();var o=n.getPath();e.test(o)?n.removeList():n[t]()}};be[v+"b"]=ye("B"),be[v+"i"]=ye("I"),be[v+"u"]=ye("U"),be[v+"shift-7"]=ye("S"),be[v+"shift-5"]=ye("SUB",{tag:"SUP"}),be[v+"shift-6"]=ye("SUP",{tag:"SUB"}),be[v+"shift-8"]=xe(/(?:^|>)UL/,"makeUnorderedList"),be[v+"shift-9"]=xe(/(?:^|>)OL/,"makeOrderedList"),be[v+"["]=ke("decreaseQuoteLevel","decreaseListLevel"),be[v+"]"]=ke("increaseQuoteLevel","increaseListLevel"),be[v+"d"]=Se("toggleCode"),be[v+"y"]=Se("redo"),be[v+"z"]=Se("undo"),be[v+"shift-z"]=Se("redo");var Oe={1:10,2:13,3:16,4:18,5:24,6:32,7:48},Ae={fontWeight:{regexp:/^bold|^700/i,replace:function(e){return V(e,"B")}},fontStyle:{regexp:/^italic/i,replace:function(e){return V(e,"I")}},fontFamily:{regexp:S,replace:function(e,t,n){return V(e,"SPAN",{class:t.fontFamily,style:"font-family:"+n})}},fontSize:{regexp:S,replace:function(e,t,n){return V(e,"SPAN",{class:t.fontSize,style:"font-size:"+n})}},textDecoration:{regexp:/^underline/i,replace:function(e){return V(e,"U")}}},Be=function(e){return function(t,n){var i,o,r,a=V(t.ownerDocument,e),s=t.attributes;for(i=0,o=s.length;i<o;i+=1)r=s[i],a.setAttribute(r.name,r.value);return n.replaceChild(a,t),a.appendChild(j(t)),a}},De={SPAN:function(e,t,n){var i,o,r,a,s,d,l=e.style,c=e.ownerDocument;for(i in Ae)if(o=Ae[i],(r=l[i])&&o.regexp.test(r)){if((d=o.replace(c,n.classNames,r)).nodeName===e.nodeName&&d.className===e.className)continue;s||(s=d),a&&a.appendChild(d),a=d,e.style[i]=""}return s&&(a.appendChild(j(e)),e.appendChild(s)),a||e},STRONG:Be("B"),EM:Be("I"),INS:Be("U"),STRIKE:Be("S"),FONT:function(e,t,n){var i,o,r,a,s,d=e.face,l=e.size,c=e.color,h=e.ownerDocument,f=n.classNames;return d&&(s=i=V(h,"SPAN",{class:f.fontFamily,style:"font-family:"+d}),a=i),l&&(o=V(h,"SPAN",{class:f.fontSize,style:"font-size:"+Oe[l]+"px"}),s||(s=o),a&&a.appendChild(o),a=o),c&&/^#?([\dA-F]{3}){1,2}$/i.test(c)&&("#"!==c.charAt(0)&&(c="#"+c),r=V(h,"SPAN",{class:f.colour,style:"color:"+c}),s||(s=r),a&&a.appendChild(r),a=r),s||(s=a=V(h,"SPAN")),t.replaceChild(s,e),a.appendChild(j(e)),a},TT:function(e,t,n){var i=V(e.ownerDocument,"SPAN",{class:n.classNames.fontFamily,style:'font-family:menlo,consolas,"courier new",monospace'});return t.replaceChild(i,e),i.appendChild(j(e)),i}},Re=/^(?: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)|COL(?:GROUP)?|UL)$/,Pe=/^(?:HEAD|META|STYLE)/,Ue=new L(null,4|a),Ie=function e(t,o,r){var a,s,d,l,c,h,f,u,p,g,m,v,C=t.childNodes;for(a=t;U(a);)a=a.parentNode;for(Ue.root=a,s=0,d=C.length;s<d;s+=1)if(c=(l=C[s]).nodeName,h=l.nodeType,f=De[c],h===n){if(u=l.childNodes.length,f)l=f(l,t,o);else{if(Pe.test(c)){t.removeChild(l),s-=1,d-=1;continue}if(!Re.test(c)&&!U(l)){s-=1,d+=u-1,t.replaceChild(j(l),l);continue}}u&&e(l,o,r||"PRE"===c)}else{if(h===i){if(m=l.data,p=!S.test(m.charAt(0)),g=!S.test(m.charAt(m.length-1)),r||!p&&!g)continue;if(p){for(Ue.currentNode=l;(v=Ue.previousPONode())&&!("IMG"===(c=v.nodeName)||"#text"===c&&S.test(v.data));)if(!U(v)){v=null;break}m=m.replace(/^[ \r\n]+/g,v?" ":"")}if(g){for(Ue.currentNode=l;(v=Ue.nextNode())&&!("IMG"===c||"#text"===c&&S.test(v.data));)if(!U(v)){v=null;break}m=m.replace(/[ \r\n]+$/g,v?" ":"")}if(m){l.data=m;continue}}t.removeChild(l),s-=1,d-=1}return t},Fe=function e(t){for(var o,r=t.childNodes,a=r.length;a--;)(o=r[a]).nodeType!==n||R(o)?o.nodeType!==i||o.data||t.removeChild(o):(e(o),U(o)&&!o.firstChild&&t.removeChild(o))},we=function(e){return e.nodeType===n?"BR"===e.nodeName:S.test(e.data)},Me=function(e,t){for(var n,i=e.parentNode;U(i);)i=i.parentNode;return(n=new L(i,4|a,we)).currentNode=e,!!n.nextNode()||t&&!n.previousNode()},He=function(e,t,n){var i,o,r,a=e.querySelectorAll("BR"),s=[],d=a.length;for(i=0;i<d;i+=1)s[i]=Me(a[i],n);for(;d--;)(r=(o=a[d]).parentNode)&&(s[d]?U(r)||X(r,t):Q(o))},ze=function(e,t,n,i,o,r){var a,s,d=e.clipboardData,l=e.target.ownerDocument,c=l.body,f=V(l,"div");f.appendChild(t),a=f.innerHTML,i&&(a=i(a)),o?s=o(a):(He(f,n,!0),f.setAttribute("style","position:fixed;overflow:hidden;bottom:100%;right:100%;"),c.appendChild(f),s=(s=f.innerText||f.textContent).replace(/ /g," "),c.removeChild(f)),h&&(s=s.replace(/\r?\n/g,"\r\n")),r||s===a||d.setData("text/html",a),d.setData("text/plain",s),e.preventDefault()},We=function(e){var t,n,o,r,a,s=this.getSelection(),d=this._root,l=this;if(s.collapsed)e.preventDefault();else{if(this.saveUndoState(s),!p&&e.clipboardData){for(n=(t=ue(s,d))===pe(s,d)&&t||d,o=se(s,d),(r=s.commonAncestorContainer).nodeType===i&&(r=r.parentNode);r&&r!==n;)(a=r.cloneNode(!1)).appendChild(o),o=a,r=r.parentNode;ze(e,o,d,this._config.willCutCopy,null,!1)}else setTimeout(function(){try{l._ensureBottomLine()}catch(e){l.didError(e)}},0);this.setSelection(s)}},qe=function(e,t,n,o,r,a){var s,d,l,c,h;if(!p&&e.clipboardData){for(d=(s=ue(t,n))===pe(t,n)&&s||n,t=t.cloneRange(),ce(t),he(t,d,d,n),l=t.cloneContents(),(c=t.commonAncestorContainer).nodeType===i&&(c=c.parentNode);c&&c!==d;)(h=c.cloneNode(!1)).appendChild(l),l=h,c=c.parentNode;ze(e,l,n,o,r,a)}},Ke=function(e){qe(e,this.getSelection(),this._root,this._config.willCutCopy,null,!1)};function Ge(e){this.isShiftDown=e.shiftKey}var Ze=function(e){var t,n,i,o,r,a=e.clipboardData,s=a&&a.items,d=this.isShiftDown,l=!1,c=!1,h=!1,f=null,g=null,m=this;if(s){for(t=s.length;t--;)"text/html"===(i=(n=s[t]).type)?g=n:"text/plain"===i||"text/uri-list"===i?f=n:"text/rtf"===i?c=!0:/^image\/.*/.test(i)&&(h=!0);if(h&&(!c||!g))return e.preventDefault(),this.fireEvent("dragover",{dataTransfer:a,preventDefault:function(){l=!0}}),void(l&&this.fireEvent("drop",{dataTransfer:a}));if(!p)return e.preventDefault(),void(!g||d&&f?f&&f.getAsString(function(e){m.insertPlainText(e,!0)}):g.getAsString(function(e){m.insertHTML(e,!0)}))}if(o=a&&a.types,!p&&o&&(y.call(o,"text/html")>-1||!u&&y.call(o,"text/plain")>-1&&y.call(o,"text/rtf")<0))return e.preventDefault(),void(!d&&(r=a.getData("text/html"))?this.insertHTML(r,!0):((r=a.getData("text/plain"))||(r=a.getData("text/uri-list")))&&this.insertPlainText(r,!0));this._awaitingPaste=!0;var v=this._doc.body,C=this.getSelection(),_=C.startContainer,N=C.startOffset,S=C.endContainer,T=C.endOffset,E=this.createElement("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});v.appendChild(E),C.selectNodeContents(E),this.setSelection(C),setTimeout(function(){try{m._awaitingPaste=!1;for(var e,t,n="",i=E;E=i;)i=E.nextSibling,Q(E),(e=E.firstChild)&&e===E.lastChild&&"DIV"===e.nodeName&&(E=e),n+=E.innerHTML;t=m.createRange(_,N,S,T),m.setSelection(t),n&&m.insertHTML(n,!0)}catch(e){m.didError(e)}},0)},Qe=function(e){for(var t=e.dataTransfer.types,n=t.length,i=!1,o=!1;n--;)switch(t[n]){case"text/plain":i=!0;break;case"text/html":o=!0;break;default:return}(o||i)&&this.saveUndoState()};function $e(e,t,n){var i,o;if(e||(e={}),t)for(i in t)!n&&i in e||(o=t[i],e[i]=o&&o.constructor===Object?$e(e[i],o,n):o);return e}function je(e,t){e.nodeType===o&&(e=e.body);var n,i=e.ownerDocument,r=i.defaultView;this._win=r,this._doc=i,this._root=e,this._events={},this._isFocused=!1,this._lastSelection=null,this._hasZWS=!1,this._lastAnchorNode=null,this._lastFocusNode=null,this._path="",this._willUpdatePath=!1,"onselectionchange"in i?this.addEventListener("selectionchange",this._updatePathOnEvent):(this.addEventListener("keyup",this._updatePathOnEvent),this.addEventListener("mouseup",this._updatePathOnEvent)),this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0,this._isInUndoState=!1,this._ignoreChange=!1,this._ignoreAllChanges=!1,_?((n=new MutationObserver(this._docWasChanged.bind(this))).observe(e,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._mutation=n):this.addEventListener("keyup",this._keyUpDetectChange),this._restoreSelection=!1,this.addEventListener("blur",et),this.addEventListener("mousedown",tt),this.addEventListener("touchstart",tt),this.addEventListener("focus",nt),this._awaitingPaste=!1,this.addEventListener("cut",We),this.addEventListener("copy",Ke),this.addEventListener("keydown",Ge),this.addEventListener("keyup",Ge),this.addEventListener("paste",Ze),this.addEventListener("drop",Qe),this.addEventListener("keydown",Ne),this._keyHandlers=Object.create(be),this.setConfig(t),e.setAttribute("contenteditable","true"),e.setAttribute("data-gramm","false");try{i.execCommand("enableObjectResizing",!1,"false"),i.execCommand("enableInlineTableEditing",!1,"false")}catch(e){}e.__squire__=this,this.setHTML("")}var Ve=je.prototype,Ye=function(e,t,n){var i=n._doc,o=e?DOMPurify.sanitize(e,{ALLOW_UNKNOWN_PROTOCOLS:!0,WHOLE_DOCUMENT:!1,RETURN_DOM:!0,RETURN_DOM_FRAGMENT:!0,FORCE_BODY:!1}):null;return o?i.importNode(o,!0):i.createDocumentFragment()};Ve.setConfig=function(e){return(e=$e({blockTag:"DIV",blockAttributes:null,tagAttributes:{blockquote:null,ul:null,ol:null,li:null,a:null},classNames:{colour:"colour",fontFamily:"font",fontSize:"size",highlight:"highlight"},leafNodeNames:k,undo:{documentSizeThreshold:-1,undoLimit:-1},isInsertedHTMLSanitized:!0,isSetHTMLSanitized:!0,sanitizeToDOMFragment:"undefined"!=typeof DOMPurify&&DOMPurify.isSupported?Ye:null,willCutCopy:null,addLinks:!0},e,!0)).blockTag=e.blockTag.toUpperCase(),this._config=e,this},Ve.createElement=function(e,t,n){return V(this._doc,e,t,n)},Ve.createDefaultBlock=function(e){var t=this._config;return Y(this.createElement(t.blockTag,t.blockAttributes,e),this._root)},Ve.didError=function(e){console.log(e)},Ve.getDocument=function(){return this._doc},Ve.getRoot=function(){return this._root},Ve.modifyDocument=function(e){var t=this._mutation;t&&(t.takeRecords().length&&this._docWasChanged(),t.disconnect()),this._ignoreAllChanges=!0,e(),this._ignoreAllChanges=!1,t&&(t.observe(this._root,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._ignoreChange=!1)};var Xe={pathChange:1,select:1,input:1,undoStateChange:1};Ve.fireEvent=function(e,t){var n,i,o,r=this._events[e];if(/^(?:focus|blur)/.test(e))if(n=this._root===this._doc.activeElement,"focus"===e){if(!n||this._isFocused)return this;this._isFocused=!0}else{if(n||!this._isFocused)return this;this._isFocused=!1}if(r)for(t||(t={}),t.type!==e&&(t.type=e),i=(r=r.slice()).length;i--;){o=r[i];try{o.handleEvent?o.handleEvent(t):o.call(this,t)}catch(t){t.details="Squire: fireEvent error. Event type: "+e,this.didError(t)}}return this},Ve.destroy=function(){var e,t=this._events;for(e in t)this.removeEventListener(e);this._mutation&&this._mutation.disconnect(),delete this._root.__squire__,this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0},Ve.handleEvent=function(e){this.fireEvent(e.type,e)},Ve.addEventListener=function(e,t){var n=this._events[e],i=this._root;return t?(n||(n=this._events[e]=[],Xe[e]||("selectionchange"===e&&(i=this._doc),i.addEventListener(e,this,!0))),n.push(t),this):(this.didError({name:"Squire: addEventListener with null or undefined fn",message:"Event type: "+e}),this)},Ve.removeEventListener=function(e,t){var n,i=this._events[e],o=this._root;if(i){if(t)for(n=i.length;n--;)i[n]===t&&i.splice(n,1);else i.length=0;i.length||(delete this._events[e],Xe[e]||("selectionchange"===e&&(o=this._doc),o.removeEventListener(e,this,!0)))}return this},Ve.createRange=function(e,t,n,i){if(e instanceof this._win.Range)return e.cloneRange();var o=this._doc.createRange();return o.setStart(e,t),n?o.setEnd(n,i):o.setEnd(e,t),o},Ve.getCursorPosition=function(e){if(!e&&!(e=this.getSelection())||!e.getBoundingClientRect)return null;var t,n,i=e.getBoundingClientRect();return i&&!i.top&&(this._ignoreChange=!0,(t=this._doc.createElement("SPAN")).textContent=s,re(e,t),i=t.getBoundingClientRect(),(n=t.parentNode).removeChild(t),ee(n,e)),i},Ve._moveCursorTo=function(e){var t=this._root,n=this.createRange(t,e?0:t.childNodes.length);return ce(n),this.setSelection(n),this},Ve.moveCursorToStart=function(){return this._moveCursorTo(!0)},Ve.moveCursorToEnd=function(){return this._moveCursorTo(!1)};var Je=function(e){return e._win.getSelection()||null};function et(){this._restoreSelection=!0}function tt(){this._restoreSelection=!1}function nt(){this._restoreSelection&&this.setSelection(this._lastSelection)}Ve.setSelection=function(e){if(e)if(this._lastSelection=e,this._isFocused){f&&this._win.focus();var t=Je(this);t&&t.setBaseAndExtent?t.setBaseAndExtent(e.startContainer,e.startOffset,e.endContainer,e.endOffset):t&&(t.removeAllRanges(),t.addRange(e))}else et.call(this);return this},Ve.getSelection=function(){var e,t,n,i,o=Je(this),r=this._root;return this._isFocused&&o&&o.rangeCount&&(t=(e=o.getRangeAt(0).cloneRange()).startContainer,n=e.endContainer,t&&R(t)&&e.setStartBefore(t),n&&R(n)&&e.setEndBefore(n)),e&&G(r,e.commonAncestorContainer)?this._lastSelection=e:G((i=(e=this._lastSelection).commonAncestorContainer).ownerDocument,i)||(e=null),e||(e=this.createRange(r.firstChild,0)),e},Ve.getSelectedText=function(){var e=this.getSelection();if(!e||e.collapsed)return"";var t,n=new L(e.commonAncestorContainer,4|a,function(t){return le(e,t,!0)}),o=e.startContainer,r=e.endContainer,s=n.currentNode=o,d="",l=!1;for(n.filter(s)||(s=n.nextNode());s;)s.nodeType===i?(t=s.data)&&/\S/.test(t)&&(s===r&&(t=t.slice(0,e.endOffset)),s===o&&(t=t.slice(e.startOffset)),d+=t,l=!0):("BR"===s.nodeName||l&&!U(s))&&(d+="\n",l=!1),s=n.nextNode();return d},Ve.getPath=function(){return this._path};var it=function(e,t){for(var n,i,o,r=new L(e,4);i=r.nextNode();)for(;(o=i.data.indexOf(s))>-1&&(!t||i.parentNode!==t);){if(1===i.length){do{(n=i.parentNode).removeChild(i),i=n,r.currentNode=n}while(U(i)&&!Z(i));break}i.deleteData(o,1)}};Ve._didAddZWS=function(){this._hasZWS=!0},Ve._removeZWS=function(){this._hasZWS&&(it(this._root),this._hasZWS=!1)},Ve._updatePath=function(e,t){if(e){var i,o=e.startContainer,r=e.endContainer;(t||o!==this._lastAnchorNode||r!==this._lastFocusNode)&&(this._lastAnchorNode=o,this._lastFocusNode=r,i=o&&r?o===r?function e(t,i,o){var r,a,s,d,l,c="";return t&&t!==i&&(c=e(t.parentNode,i,o),t.nodeType===n&&(c+=(c?">":"")+t.nodeName,(r=t.id)&&(c+="#"+r),(a=t.className.trim())&&((s=a.split(/\s\s*/)).sort(),c+=".",c+=s.join(".")),(d=t.dir)&&(c+="[dir="+d+"]"),s&&(l=o.classNames,y.call(s,l.highlight)>-1&&(c+="[backgroundColor="+t.style.backgroundColor.replace(/ /g,"")+"]"),y.call(s,l.colour)>-1&&(c+="[color="+t.style.color.replace(/ /g,"")+"]"),y.call(s,l.fontFamily)>-1&&(c+="[fontFamily="+t.style.fontFamily.replace(/ /g,"")+"]"),y.call(s,l.fontSize)>-1&&(c+="[fontSize="+t.style.fontSize+"]")))),c}(r,this._root,this._config):"(selection)":"",this._path!==i&&(this._path=i,this.fireEvent("pathChange",{path:i}))),this.fireEvent(e.collapsed?"cursor":"select",{range:e})}},Ve._updatePathOnEvent=function(){var e=this;e._isFocused&&!e._willUpdatePath&&(e._willUpdatePath=!0,setTimeout(function(){e._willUpdatePath=!1,e._updatePath(e.getSelection())},0))},Ve.focus=function(){return this._root.focus({preventScroll:!0}),m&&this.fireEvent("focus"),this},Ve.blur=function(){return this._root.blur(),m&&this.fireEvent("blur"),this};var ot="squire-selection-end";Ve._saveRangeToBookmark=function(e){var t,n=this.createElement("INPUT",{id:"squire-selection-start",type:"hidden"}),i=this.createElement("INPUT",{id:ot,type:"hidden"});re(e,n),e.collapse(!1),re(e,i),2&n.compareDocumentPosition(i)&&(n.id=ot,i.id="squire-selection-start",t=n,n=i,i=t),e.setStartAfter(n),e.setEndBefore(i)},Ve._getRangeAndRemoveBookmark=function(e){var t=this._root,n=t.querySelector("#squire-selection-start"),o=t.querySelector("#"+ot);if(n&&o){var r=n.parentNode,a=o.parentNode,s=y.call(r.childNodes,n),d=y.call(a.childNodes,o);r===a&&(d-=1),Q(n),Q(o),e||(e=this._doc.createRange()),e.setStart(r,s),e.setEnd(a,d),ee(r,e),r!==a&&ee(a,e),e.collapsed&&(r=e.startContainer).nodeType===i&&((a=r.childNodes[e.startOffset])&&a.nodeType===i||(a=r.childNodes[e.startOffset-1]),a&&a.nodeType===i&&(e.setStart(a,0),e.collapse(!0)))}return e||null},Ve._keyUpDetectChange=function(e){var t=e.keyCode;e.ctrlKey||e.metaKey||e.altKey||!(t<16||t>20)||!(t<33||t>45)||this._docWasChanged()},Ve._docWasChanged=function(){N&&(D=new WeakMap),this._ignoreAllChanges||(_&&this._ignoreChange?this._ignoreChange=!1:(this._isInUndoState&&(this._isInUndoState=!1,this.fireEvent("undoStateChange",{canUndo:!0,canRedo:!1})),this.fireEvent("input")))},Ve._recordUndoState=function(e,t){if(!this._isInUndoState||t){var n,i=this._undoIndex,o=this._undoStack,r=this._config.undo,a=r.documentSizeThreshold,s=r.undoLimit;t||(i+=1),i<this._undoStackLength&&(o.length=this._undoStackLength=i),e&&this._saveRangeToBookmark(e),n=this._getHTML(),a>-1&&2*n.length>a&&s>-1&&i>s&&(o.splice(0,i-s),i=s,this._undoStackLength=s),o[i]=n,this._undoIndex=i,this._undoStackLength+=1,this._isInUndoState=!0}},Ve.saveUndoState=function(e){return e===t&&(e=this.getSelection()),this._recordUndoState(e,this._isInUndoState),this._getRangeAndRemoveBookmark(e),this},Ve.undo=function(){if(0!==this._undoIndex||!this._isInUndoState){this._recordUndoState(this.getSelection(),!1),this._undoIndex-=1,this._setHTML(this._undoStack[this._undoIndex]);var e=this._getRangeAndRemoveBookmark();e&&this.setSelection(e),this._isInUndoState=!0,this.fireEvent("undoStateChange",{canUndo:0!==this._undoIndex,canRedo:!0}),this.fireEvent("input")}return this},Ve.redo=function(){var e=this._undoIndex,t=this._undoStackLength;if(e+1<t&&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:e+2<t}),this.fireEvent("input")}return this},Ve.hasFormat=function(e,t,n){if(e=e.toUpperCase(),t||(t={}),!n&&!(n=this.getSelection()))return!1;!n.collapsed&&n.startContainer.nodeType===i&&n.startOffset===n.startContainer.length&&n.startContainer.nextSibling&&n.setStartBefore(n.startContainer.nextSibling),!n.collapsed&&n.endContainer.nodeType===i&&0===n.endOffset&&n.endContainer.previousSibling&&n.setEndAfter(n.endContainer.previousSibling);var o,r,a=this._root,s=n.commonAncestorContainer;if(K(s,a,e,t))return!0;if(s.nodeType===i)return!1;o=new L(s,4,function(e){return le(n,e,!0)});for(var d=!1;r=o.nextNode();){if(!K(r,a,e,t))return!1;d=!0}return d},Ve.getFontInfo=function(e){var n,o,r,a={color:t,backgroundColor:t,family:t,size:t},s=0;if(!e&&!(e=this.getSelection()))return a;if(n=e.commonAncestorContainer,e.collapsed||n.nodeType===i)for(n.nodeType===i&&(n=n.parentNode);s<4&&n;)(o=n.style)&&(!a.color&&(r=o.color)&&(a.color=r,s+=1),!a.backgroundColor&&(r=o.backgroundColor)&&(a.backgroundColor=r,s+=1),!a.family&&(r=o.fontFamily)&&(a.family=r,s+=1),!a.size&&(r=o.fontSize)&&(a.size=r,s+=1)),n=n.parentNode;return a},Ve._addFormat=function(e,t,n){var o,r,s,d,l,c,h,f,u=this._root;if(n.collapsed){for(o=Y(this.createElement(e,t),u),re(n,o),n.setStart(o.firstChild,o.firstChild.length),n.collapse(!0),f=o;U(f);)f=f.parentNode;it(f,o)}else{if(r=new L(n.commonAncestorContainer,4|a,function(e){return(e.nodeType===i||"BR"===e.nodeName||"IMG"===e.nodeName)&&le(n,e,!0)}),s=n.startContainer,l=n.startOffset,d=n.endContainer,c=n.endOffset,r.currentNode=s,r.filter(s)||(s=r.nextNode(),l=0),!s)return n;do{!K(h=r.currentNode,u,e,t)&&(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),$(h,o=this.createElement(e,t)),o.appendChild(h))}while(r.nextNode());d.nodeType!==i&&(h.nodeType===i?(d=h,c=h.length):(d=h.parentNode,c=1)),n=this.createRange(s,l,d,c)}return n},Ve._removeFormat=function(e,t,n,o){this._saveRangeToBookmark(n);var r,a=this._doc;n.collapsed&&(C?(r=a.createTextNode(s),this._didAddZWS()):r=a.createTextNode(""),re(n,r));for(var d=n.commonAncestorContainer;U(d);)d=d.parentNode;var l=n.startContainer,c=n.startOffset,h=n.endContainer,f=n.endOffset,u=[],p=function(e,t){if(!le(n,e,!1)){var o,r,a=e.nodeType===i;if(le(n,e,!0))if(a)e===h&&f!==e.length&&u.push([t,e.splitText(f)]),e===l&&c&&(e.splitText(c),u.push([t,e]));else for(o=e.firstChild;o;o=r)r=o.nextSibling,p(o,t);else"INPUT"===e.nodeName||a&&!e.data||u.push([t,e])}},g=Array.prototype.filter.call(d.getElementsByTagName(e),function(i){return le(n,i,!0)&&q(i,e,t)});return o||g.forEach(function(e){p(e,e)}),u.forEach(function(e){var t=e[0].cloneNode(!1),n=e[1];$(n,t),t.appendChild(n)}),g.forEach(function(e){$(e,j(e))}),this._getRangeAndRemoveBookmark(n),r&&n.collapse(!1),ee(d,n),n},Ve.changeFormat=function(e,t,n,i){return n||(n=this.getSelection())?(this.saveUndoState(n),t&&(n=this._removeFormat(t.tag.toUpperCase(),t.attributes||{},n,i)),e&&(n=this._addFormat(e.tag.toUpperCase(),e.attributes||{},n)),this.setSelection(n),this._updatePath(n,!0),_||this._docWasChanged(),this):this};var rt={DT:"DD",DD:"DT",LI:"LI",PRE:"PRE"},at=function(e,t,n,i){var o=rt[t.nodeName],r=null,a=J(n,i,t.parentNode,e._root),s=e._config;return o||(o=s.blockTag,r=s.blockAttributes),q(a,o,r)||(t=V(a.ownerDocument,o,r),a.dir&&(t.dir=a.dir),$(a,t),t.appendChild(j(a)),a=t),a};Ve.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var i=this._root,o=ue(n,i),r=pe(n,i);if(o&&r)do{if(e(o)||o===r)break}while(o=H(o,i));return t&&(this.setSelection(n),this._updatePath(n,!0),_||this._docWasChanged()),this},Ve.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._recordUndoState(t,this._isInUndoState);var n,i=this._root;return Ce(t,i),he(t,i,i,i),n=ae(t,i,i),re(t,e.call(this,n)),t.endOffset<t.endContainer.childNodes.length&&ne(t.endContainer.childNodes[t.endOffset],i),ne(t.startContainer.childNodes[t.startOffset],i),this._getRangeAndRemoveBookmark(t),this.setSelection(t),this._updatePath(t,!0),_||this._docWasChanged(),this};var st=function(e){var t=e.querySelectorAll("blockquote");return Array.prototype.filter.call(t,function(t){return!K(t.parentNode,e,"BLOCKQUOTE")}).forEach(function(e){$(e,j(e))}),e},dt=function(){return this.createDefaultBlock([this.createElement("INPUT",{id:"squire-selection-start",type:"hidden"}),this.createElement("INPUT",{id:ot,type:"hidden"})])},lt=function(e,t,n){for(var i,o,r,a,s=w(t,e._root),d=e._config.tagAttributes,l=d[n.toLowerCase()],c=d.li;i=s.nextNode();)"LI"===i.parentNode.nodeName&&(i=i.parentNode,s.currentNode=i.lastChild),"LI"!==i.nodeName?(a=e.createElement("LI",c),i.dir&&(a.dir=i.dir),(r=i.previousSibling)&&r.nodeName===n?(r.appendChild(a),Q(i)):$(i,e.createElement(n,l,[a])),a.appendChild(j(i)),s.currentNode=a):(o=(i=i.parentNode).nodeName)!==n&&/^[OU]L$/.test(o)&&$(i,e.createElement(n,l,[j(i)]))},ct=function(e,t){for(var n=e.commonAncestorContainer,i=e.startContainer,o=e.endContainer;n&&n!==t&&!/^[OU]L$/.test(n.nodeName);)n=n.parentNode;if(!n||n===t)return null;for(i===n&&(i=i.childNodes[e.startOffset]),o===n&&(o=o.childNodes[e.endOffset]);i&&i.parentNode!==n;)i=i.parentNode;for(;o&&o.parentNode!==n;)o=o.parentNode;return[n,i,o]};Ve.increaseListLevel=function(e){if(!e&&!(e=this.getSelection()))return this.focus();var t=this._root,n=ct(e,t);if(!n)return this.focus();var i=n[0],o=n[1],r=n[2];if(!o||o===i.firstChild)return this.focus();this._recordUndoState(e,this._isInUndoState);var a,s,d=i.nodeName,l=o.previousSibling;l.nodeName!==d&&(a=this._config.tagAttributes[d.toLowerCase()],l=this.createElement(d,a),i.insertBefore(l,o));do{s=o===r?null:o.nextSibling,l.appendChild(o)}while(o=s);return(s=l.nextSibling)&&ne(s,t),this._getRangeAndRemoveBookmark(e),this.setSelection(e),this._updatePath(e,!0),_||this._docWasChanged(),this.focus()},Ve.decreaseListLevel=function(e){if(!e&&!(e=this.getSelection()))return this.focus();var t=this._root,n=ct(e,t);if(!n)return this.focus();var i,o,r,a,s=n[0],d=n[1],l=n[2];if(d||(d=s.firstChild),l||(l=s.lastChild),this._recordUndoState(e,this._isInUndoState),d){if(i=s.parentNode,r=l.nextSibling?J(s,l.nextSibling,i,t):s.nextSibling,i!==t&&"LI"===i.nodeName){for(i=i.parentNode;r;)o=r.nextSibling,l.appendChild(r),r=o;r=s.parentNode.nextSibling}a=!/^[OU]L$/.test(i.nodeName);do{o=d===l?null:d.nextSibling,s.removeChild(d),a&&"LI"===d.nodeName&&(d=this.createDefaultBlock([j(d)])),i.insertBefore(d,r)}while(d=o)}return s.firstChild||Q(s),r&&ne(r,t),this._getRangeAndRemoveBookmark(e),this.setSelection(e),this._updatePath(e,!0),_||this._docWasChanged(),this.focus()},Ve._ensureBottomLine=function(){var e=this._root,t=e.lastElementChild;t&&t.nodeName===this._config.blockTag&&I(t)||e.appendChild(this.createDefaultBlock())},Ve.setKeyHandler=function(e,t){return this._keyHandlers[e]=t,this},Ve._getHTML=function(){return this._root.innerHTML},Ve._setHTML=function(e){var t=this._root,n=t,i=this._config.sanitizeToDOMFragment;if("function"==typeof i){var o=i(e,!1,this);j(n),n.appendChild(o)}else n.innerHTML=e;do{Y(n,t)}while(n=H(n,t));this._ignoreChange=!0},Ve.getHTML=function(e){var t,n;return e&&(n=this.getSelection())&&this._saveRangeToBookmark(n),t=this._getHTML().replace(/\u200B/g,""),n&&this._getRangeAndRemoveBookmark(n),t},Ve.setHTML=function(e){var t,n,i,o=this._config,r=o.isSetHTMLSanitized?o.sanitizeToDOMFragment:null,a=this._root;"function"==typeof r?n=r(e,!1,this):((t=this.createElement("DIV")).innerHTML=e,(n=this._doc.createDocumentFragment()).appendChild(j(t))),Ie(n,o),He(n,a,!1),X(n,a);for(var s=n;s=H(s,a);)Y(s,a);for(this._ignoreChange=!0;i=a.lastChild;)a.removeChild(i);a.appendChild(n),Y(a,a),this._undoIndex=-1,this._undoStack.length=0,this._undoStackLength=0,this._isInUndoState=!1;var d=this._getRangeAndRemoveBookmark()||this.createRange(a.firstChild,0);return this.saveUndoState(d),this._lastSelection=d,et.call(this),this._updatePath(d,!0),this},Ve.insertElement=function(e,t){if(t||(t=this.getSelection()),t.collapse(!0),U(e))re(t,e),t.setStartAfter(e);else{for(var n,i=this._root,o=ue(t,i)||i;o!==i&&!o.nextSibling;)o=o.parentNode;o!==i&&(n=J(o.parentNode,o.nextSibling,i,i)),n?i.insertBefore(e,n):(i.appendChild(e),n=this.createDefaultBlock(),i.appendChild(n)),t.setStart(n,0),t.setEnd(n,0),ce(t)}return this.focus(),this.setSelection(t),this._updatePath(t),_||this._docWasChanged(),this},Ve.insertImage=function(e,t){var n=this.createElement("IMG",$e({src:e},t,!0));return this.insertElement(n),n},Ve.linkRegExp=/\b(?:((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9][a-z0-9.\-]*[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:[^\s?&`!()\[\]{};:'".,<>«»“”‘’]|\([^\s()<>]+\)))|([\w\-.%+]+@(?:[\w\-]+\.)+[a-z]{2,}\b(?:[?][^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+(?:&[^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+)*)?))/i;var ht=function(e,t,n){var i,o,r,a,s,d,l,c=e.ownerDocument,h=new L(e,4,function(e){return!K(e,t,"A")}),f=n.linkRegExp,u=n._config.tagAttributes.a;if(f)for(;i=h.nextNode();)for(o=i.data,r=i.parentNode;a=f.exec(o);)d=(s=a.index)+a[0].length,s&&(l=c.createTextNode(o.slice(0,s)),r.insertBefore(l,i)),(l=n.createElement("A",$e({href:a[1]?/^(?:ht|f)tps?:/i.test(a[1])?a[1]:"http://"+a[1]:"mailto:"+a[0]},u,!1))).textContent=o.slice(s,d),r.insertBefore(l,i),i.data=o=o.slice(d)};Ve.insertHTML=function(e,t){var n,i,o,r,a,s,d,l=this._config,c=l.isInsertedHTMLSanitized?l.sanitizeToDOMFragment:null,h=this.getSelection(),f=this._doc;"function"==typeof c?r=c(e,t,this):(t&&(n=e.indexOf("\x3c!--StartFragment--\x3e"),i=e.lastIndexOf("\x3c!--EndFragment--\x3e"),n>-1&&i>-1&&(e=e.slice(n+20,i))),/<\/td>((?!<\/tr>)[\s\S])*$/i.test(e)&&(e="<TR>"+e+"</TR>"),/<\/tr>((?!<\/table>)[\s\S])*$/i.test(e)&&(e="<TABLE>"+e+"</TABLE>"),(o=this.createElement("DIV")).innerHTML=e,(r=f.createDocumentFragment()).appendChild(j(o))),this.saveUndoState(h);try{for(a=this._root,s=r,d={fragment:r,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},ht(r,r,this),Ie(r,l),He(r,a,!1),Fe(r),r.normalize();s=H(s,r);)Y(s,a);t&&this.fireEvent("willPaste",d),d.defaultPrevented||(de(h,d.fragment,a),_||this._docWasChanged(),h.collapse(!1),fe(h,0,a),this._ensureBottomLine()),this.setSelection(h),this._updatePath(h,!0),t&&this.focus()}catch(e){this.didError(e)}return this};var ft=function(e){return e.split("&").join("&").split("<").join("<").split(">").join(">").split('"').join(""")};Ve.insertPlainText=function(e,t){var n=this.getSelection();if(n.collapsed&&K(n.startContainer,this._root,"PRE")){var o,r,a=n.startContainer,s=n.startOffset;return a&&a.nodeType===i||(o=this._doc.createTextNode(""),a.insertBefore(o,a.childNodes[s]),a=o,s=0),r={text:e,preventDefault:function(){this.defaultPrevented=!0},defaultPrevented:!1},t&&this.fireEvent("willPaste",r),r.defaultPrevented||(e=r.text,a.insertData(s,e),n.setStart(a,s+e.length),n.collapse(!0)),this.setSelection(n),this}var d,l,c,h,f=e.split("\n"),u=this._config,p=u.blockTag,g=u.blockAttributes,m="</"+p+">",v="<"+p;for(d in g)v+=" "+d+'="'+ft(g[d])+'"';for(v+=">",l=0,c=f.length;l<c;l+=1)h=f[l],h=ft(h).replace(/ (?= )/g," "),l&&(h=v+(h||"<BR>")+m),f[l]=h;return this.insertHTML(f.join(""),t)};var ut=function(e,t,n){return function(){return this[e](t,n),this.focus()}};Ve.addStyles=function(e){if(e){var t=this._doc.documentElement.firstChild,n=this.createElement("STYLE",{type:"text/css"});n.appendChild(this._doc.createTextNode(e)),t.appendChild(n)}return this},Ve.bold=ut("changeFormat",{tag:"B"}),Ve.italic=ut("changeFormat",{tag:"I"}),Ve.underline=ut("changeFormat",{tag:"U"}),Ve.strikethrough=ut("changeFormat",{tag:"S"}),Ve.subscript=ut("changeFormat",{tag:"SUB"},{tag:"SUP"}),Ve.superscript=ut("changeFormat",{tag:"SUP"},{tag:"SUB"}),Ve.removeBold=ut("changeFormat",null,{tag:"B"}),Ve.removeItalic=ut("changeFormat",null,{tag:"I"}),Ve.removeUnderline=ut("changeFormat",null,{tag:"U"}),Ve.removeStrikethrough=ut("changeFormat",null,{tag:"S"}),Ve.removeSubscript=ut("changeFormat",null,{tag:"SUB"}),Ve.removeSuperscript=ut("changeFormat",null,{tag:"SUP"}),Ve.makeLink=function(e,t){var n=this.getSelection();if(n.collapsed){var i=e.indexOf(":")+1;if(i)for(;"/"===e[i];)i+=1;re(n,this._doc.createTextNode(e.slice(i)))}return t=$e($e({href:e},t,!0),this._config.tagAttributes.a,!1),this.changeFormat({tag:"A",attributes:t},{tag:"A"},n),this.focus()},Ve.removeLink=function(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0),this.focus()},Ve.setFontFace=function(e){var t=this._config.classNames.fontFamily;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"font-family: "+e+", sans-serif;"}}:null,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ve.setFontSize=function(e){var t=this._config.classNames.fontSize;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"font-size: "+("number"==typeof e?e+"px":e)}}:null,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ve.setTextColour=function(e){var t=this._config.classNames.colour;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"color:"+e}}:null,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ve.setHighlightColour=function(e){var t=this._config.classNames.highlight;return this.changeFormat(e?{tag:"SPAN",attributes:{class:t,style:"background-color:"+e}}:e,{tag:"SPAN",attributes:{class:t}}),this.focus()},Ve.setTextAlignment=function(e){return this.forEachBlock(function(t){var n=t.className.split(/\s+/).filter(function(e){return!!e&&!/^align/.test(e)}).join(" ");e?(t.className=n+" align-"+e,t.style.textAlign=e):(t.className=n,t.style.textAlign="")},!0),this.focus()},Ve.setTextDirection=function(e){return this.forEachBlock(function(t){e?t.dir=e:t.removeAttribute("dir")},!0),this.focus()};var pt=function(e){for(var t,n=this._root,i=this._doc,o=i.createDocumentFragment(),r=w(e,n);t=r.nextNode();){var a,s,d=t.querySelectorAll("BR"),l=[],c=d.length;for(a=0;a<c;a+=1)l[a]=Me(d[a],!1);for(;c--;)s=d[c],l[c]?$(s,i.createTextNode("\n")):Q(s);for(c=(d=t.querySelectorAll("CODE")).length;c--;)Q(d[c]);o.childNodes.length&&o.appendChild(i.createTextNode("\n")),o.appendChild(j(t))}for(r=new L(o,4);t=r.nextNode();)t.data=t.data.replace(/ /g," ");return o.normalize(),Y(this.createElement("PRE",this._config.tagAttributes.pre,[o]),n)},gt=function(e){for(var t,n,i,o,r,a,s=this._doc,d=this._root,l=e.querySelectorAll("PRE"),c=l.length;c--;){for(n=new L(t=l[c],4);i=n.nextNode();){for(o=(o=i.data).replace(/ (?= )/g," "),r=s.createDocumentFragment();(a=o.indexOf("\n"))>-1;)r.appendChild(s.createTextNode(o.slice(0,a))),r.appendChild(s.createElement("BR")),o=o.slice(a+1);i.parentNode.insertBefore(r,i),i.data=o}X(t,d),$(t,j(t))}return e};Ve.code=function(){var e=this.getSelection();return e.collapsed||F(e.commonAncestorContainer)?this.modifyBlocks(pt,e):this.changeFormat({tag:"CODE",attributes:this._config.tagAttributes.code},null,e),this.focus()},Ve.removeCode=function(){var e=this.getSelection();return K(e.commonAncestorContainer,this._root,"PRE")?this.modifyBlocks(gt,e):this.changeFormat(null,{tag:"CODE"},e),this.focus()},Ve.toggleCode=function(){return this.hasFormat("PRE")||this.hasFormat("CODE")?this.removeCode():this.code(),this},Ve.removeAllFormatting=function(e){if(!e&&!(e=this.getSelection())||e.collapsed)return this;for(var t=this._root,n=e.commonAncestorContainer;n&&!I(n);)n=n.parentNode;if(n||(Ce(e,t),n=t),n.nodeType===i)return this;this.saveUndoState(e),he(e,n,n,t);for(var o,r,a=n.ownerDocument,s=e.startContainer,d=e.startOffset,l=e.endContainer,c=e.endOffset,h=a.createDocumentFragment(),f=a.createDocumentFragment(),u=J(l,c,n,t),p=J(s,d,n,t);p!==u;)o=p.nextSibling,h.appendChild(p),p=o;return function e(t,n,o){var r,a;for(r=n.firstChild;r;r=a){if(a=r.nextSibling,U(r)){if(r.nodeType===i||"BR"===r.nodeName||"IMG"===r.nodeName){o.appendChild(r);continue}}else if(I(r)){o.appendChild(t.createDefaultBlock([e(t,r,t._doc.createDocumentFragment())]));continue}e(t,r,o)}return o}(this,h,f),f.normalize(),p=f.firstChild,o=f.lastChild,r=n.childNodes,p?(n.insertBefore(f,u),d=y.call(r,p),c=y.call(r,o)+1):c=d=y.call(r,u),e.setStart(n,d),e.setEnd(n,c),ee(n,e),ce(e),this.setSelection(e),this._updatePath(e,!0),this.focus()},Ve.increaseQuoteLevel=ut("modifyBlocks",function(e){return this.createElement("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e])}),Ve.decreaseQuoteLevel=ut("modifyBlocks",st),Ve.makeUnorderedList=ut("modifyBlocks",function(e){return lt(this,e,"UL"),e}),Ve.makeOrderedList=ut("modifyBlocks",function(e){return lt(this,e,"OL"),e}),Ve.removeList=ut("modifyBlocks",function(e){var t,n,i,o,r,a=e.querySelectorAll("UL, OL"),s=e.querySelectorAll("LI"),d=this._root;for(t=0,n=a.length;t<n;t+=1)X(o=j(i=a[t]),d),$(i,o);for(t=0,n=s.length;t<n;t+=1)I(r=s[t])?$(r,this.createDefaultBlock([j(r)])):(X(r,d),$(r,j(r)));return e}),je.isInline=U,je.isBlock=I,je.isContainer=F,je.getBlockWalker=w,je.getPreviousBlock=M,je.getNextBlock=H,je.areAlike=W,je.hasTagAttributes=q,je.getNearest=K,je.isOrContains=G,je.detach=Q,je.replaceWith=$,je.empty=j,je.getNodeBefore=ie,je.getNodeAfter=oe,je.insertNodeInRange=re,je.extractContentsOfRange=ae,je.deleteContentsOfRange=se,je.insertTreeFragmentIntoRange=de,je.isNodeContainedInRange=le,je.moveRangeBoundariesDownTree=ce,je.moveRangeBoundariesUpTree=he,je.getStartBlockOfRange=ue,je.getEndBlockOfRange=pe,je.contentWalker=ge,je.rangeDoesStartAtBlockBoundary=me,je.rangeDoesEndAtBlockBoundary=ve,je.expandRangeToBlockBoundaries=Ce,je.onCopy=qe,je.onPaste=Ze,je.addLinks=ht,je.splitBlock=at,je.startSelectionId="squire-selection-start",je.endSelectionId=ot,"object"==typeof exports?module.exports=je:"function"==typeof define&&define.amd?define(function(){return je}):(d.Squire=je,top!==d&&"true"===e.documentElement.getAttribute("data-squireinit")&&(d.editor=new je(e),d.onEditorLoad&&(d.onEditorLoad(d.editor),d.onEditorLoad=null)))}(document); |