0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 23:40:35 -05:00
Squire/dist/squire.js
Neil Jenkins fe0dfdf6c4 Squire 2.0
This is a massive refactor to port Squire to TypeScript, fix a bunch of small
bugs and modernise our tooling. The development was done on an internal
repository, so apologies to anyone following externally for the commit dump;
updates from here should come as real commits again.

Co-authored-by: Joe Woods <woods@fastmailteam.com>
2023-01-23 13:18:29 +11:00

12 lines
50 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";(()=>{var ot=()=>!0,C=class{constructor(t,e,n){this.root=t,this.currentNode=t,this.nodeType=e,this.filter=n||ot}isAcceptableNode(t){let e=t.nodeType;return!!((e===Node.ELEMENT_NODE?1:e===Node.TEXT_NODE?4:0)&this.nodeType)&&this.filter(t)}nextNode(){let t=this.root,e=this.currentNode,n;for(;;){for(n=e.firstChild;!n&&e&&e!==t;)n=e.nextSibling,n||(e=e.parentNode);if(!n)return null;if(this.isAcceptableNode(n))return this.currentNode=n,n;e=n}}previousNode(){let t=this.root,e=this.currentNode,n;for(;;){if(e===t)return null;if(n=e.previousSibling,n)for(;e=n.lastChild;)n=e;else n=e.parentNode;if(!n)return null;if(this.isAcceptableNode(n))return this.currentNode=n,n;e=n}}previousPONode(){let t=this.root,e=this.currentNode,n;for(;;){for(n=e.lastChild;!n&&e&&e!==t;)n=e.previousSibling,n||(e=e.parentNode);if(!n)return null;if(this.isAcceptableNode(n))return this.currentNode=n,n;e=n}}};var W="\u200B",V=navigator.userAgent,ce=/Mac OS X/.test(V),de=/Windows NT/.test(V),Ce=/iP(?:ad|hone|od)/.test(V)||ce&&!!navigator.maxTouchPoints,Me=/Android/.test(V),Fe=/Gecko\//.test(V),se=/Edge\//.test(V),st=!se&&/WebKit\//.test(V),k=ce||Ce?"Meta-":"Ctrl-",re=st,He="onbeforeinput"in document&&"inputType"in new InputEvent("input"),w=/[^ \t\r\n]/;var at=/^(?:#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)$/,ct=new Set(["BR","HR","IFRAME","IMG","INPUT"]),dt=0,_e=1,Pe=2,Ue=3,fe=new WeakMap,qe=()=>{fe=new WeakMap},I=i=>ct.has(i.nodeName),ve=i=>{switch(i.nodeType){case 3:return _e;case 1:case 11:if(fe.has(i))return fe.get(i);break;default:return dt}let t;return Array.from(i.childNodes).every(N)?at.test(i.nodeName)?t=_e:t=Pe:t=Ue,fe.set(i,t),t},N=i=>ve(i)===_e,H=i=>ve(i)===Pe,j=i=>ve(i)===Ue;var m=(i,t,e)=>{let n=document.createElement(i);if(t instanceof Array&&(e=t,t=null),t)for(let o in t){let s=t[o];s!==void 0&&n.setAttribute(o,s)}return e&&e.forEach(o=>n.appendChild(o)),n},xe=(i,t)=>I(i)||i.nodeType!==t.nodeType||i.nodeName!==t.nodeName?!1:i instanceof HTMLElement&&t instanceof HTMLElement?i.nodeName!=="A"&&i.className===t.className&&i.style.cssText===t.style.cssText:!0,ue=(i,t,e)=>{if(i.nodeName!==t)return!1;for(let n in e)if(!("getAttribute"in i)||i.getAttribute(n)!==e[n])return!1;return!0},g=(i,t,e,n)=>{for(;i&&i!==t;){if(ue(i,e,n))return i;i=i.parentNode}return null},he=(i,t)=>{let e=i.childNodes;for(;t&&i instanceof Element;)i=e[t-1],e=i.childNodes,t=e.length;return i},Le=(i,t)=>{let e=i;if(e instanceof Element){let n=e.childNodes;if(t<n.length)e=n[t];else{for(;e&&!e.nextSibling;)e=e.parentNode;e&&(e=e.nextSibling)}}return e},O=i=>i instanceof Element||i instanceof DocumentFragment?i.childNodes.length:i instanceof CharacterData?i.length:0,S=i=>{let t=document.createDocumentFragment(),e=i.firstChild;for(;e;)t.appendChild(e),e=i.firstChild;return t},E=i=>{let t=i.parentNode;return t&&t.removeChild(i),i},y=(i,t)=>{let e=i.parentNode;e&&e.replaceChild(t,i)};var ft=i=>i instanceof Element?i.nodeName==="BR":w.test(i.data),Y=(i,t)=>{let e=i.parentNode;for(;N(e);)e=e.parentNode;let n=new C(e,5,ft);return n.currentNode=i,!!n.nextNode()||t&&!n.previousNode()},me=(i,t)=>{let e=new C(i,4),n,o;for(;n=e.nextNode();)for(;(o=n.data.indexOf(W))>-1&&(!t||n.parentNode!==t);)if(n.length===1){let s=n,r=s.parentNode;for(;r&&(r.removeChild(s),e.currentNode=r,!(!N(r)||O(r)));)s=r,r=s.parentNode;break}else n.deleteData(o,1)};var ut=0,ht=1,mt=2,pt=3,K=(i,t,e)=>{let n=document.createRange();if(n.selectNode(t),e){let o=i.compareBoundaryPoints(pt,n)>-1,s=i.compareBoundaryPoints(ht,n)<1;return!o&&!s}else{let o=i.compareBoundaryPoints(ut,n)<1,s=i.compareBoundaryPoints(mt,n)>-1;return o&&s}},v=i=>{let{startContainer:t,startOffset:e,endContainer:n,endOffset:o}=i;for(;!(t instanceof Text);){let s=t.childNodes[e];if(!s||I(s)){e&&(s=t.childNodes[e-1],s instanceof Text&&(t=s,e=s.data.length));break}t=s,e=0}if(o)for(;!(n instanceof Text);){let s=n.childNodes[o-1];if(!s||I(s)){if(s&&s.nodeName==="BR"&&!Y(s,!1)){o-=1;continue}break}n=s,o=O(n)}else for(;!(n instanceof Text);){let s=n.firstChild;if(!s||I(s))break;n=s}i.setStart(t,e),i.setEnd(n,o)},P=(i,t,e,n)=>{let o=i.startContainer,s=i.startOffset,r=i.endContainer,l=i.endOffset,a;for(t||(t=i.commonAncestorContainer),e||(e=t);!s&&o!==t&&o!==n;)a=o.parentNode,s=Array.from(a.childNodes).indexOf(o),o=a;for(;!(r===e||r===n||(r.nodeType!==3&&r.childNodes[l]&&r.childNodes[l].nodeName==="BR"&&!Y(r.childNodes[l],!1)&&(l+=1),l!==O(r)));)a=r.parentNode,l=Array.from(a.childNodes).indexOf(r)+1,r=a;i.setStart(o,s),i.setEnd(r,l)},Re=(i,t,e)=>{let n=g(i.endContainer,e,t);if(n&&(n=n.parentNode)){let o=i.cloneRange();P(o,n,n,e),o.endContainer===n&&(i.setStart(o.endContainer,o.endOffset),i.setEnd(o.endContainer,o.endOffset))}return i};var _=i=>{let t=null;if(i instanceof Text)return i;if(N(i)){let e=i.firstChild;if(re)for(;e&&e instanceof Text&&!e.data;)i.removeChild(e),e=i.firstChild;e||(re?t=document.createTextNode(W):t=document.createTextNode(""))}else if(i instanceof Element&&!i.querySelector("BR")){t=m("BR");let e=i,n;for(;(n=e.lastElementChild)&&!N(n);)e=n}if(t)try{i.appendChild(t)}catch(e){}return i},B=(i,t)=>{let e=i.childNodes,n=null;for(let o=0,s=e.length;o<s;o+=1){let r=e[o],l=r.nodeName==="BR";!l&&N(r)?(n||(n=m("DIV")),n.appendChild(r),o-=1,s-=1):(l||n)&&(n||(n=m("DIV")),_(n),l?i.replaceChild(n,r):(i.insertBefore(n,r),o+=1,s+=1),n=null),j(r)&&B(r,t)}return n&&i.appendChild(_(n)),i},D=(i,t,e,n)=>{if(i instanceof Text&&i!==e){if(typeof t!="number")throw new Error("Offset must be a number to split text node!");if(!i.parentNode)throw new Error("Cannot split text node with no parent!");return D(i.parentNode,i.splitText(t),e,n)}let o=typeof t=="number"?t<i.childNodes.length?i.childNodes[t]:null:t,s=i.parentNode;if(!s||i===e||!(i instanceof Element))return o;let r=i.cloneNode(!1);for(;o;){let l=o.nextSibling;r.appendChild(o),o=l}return i instanceof HTMLOListElement&&g(i,n,"BLOCKQUOTE")&&(r.start=(+i.start||1)+i.childNodes.length-1),_(i),_(r),s.insertBefore(r,i.nextSibling),D(s,r,e,n)},We=(i,t)=>{let e=i.childNodes,n=e.length,o=[];for(;n--;){let s=e[n],r=n?e[n-1]:null;if(r&&N(s)&&xe(s,r))t.startContainer===s&&(t.startContainer=r,t.startOffset+=O(r)),t.endContainer===s&&(t.endContainer=r,t.endOffset+=O(r)),t.startContainer===i&&(t.startOffset>n?t.startOffset-=1:t.startOffset===n&&(t.startContainer=r,t.startOffset=O(r))),t.endContainer===i&&(t.endOffset>n?t.endOffset-=1:t.endOffset===n&&(t.endContainer=r,t.endOffset=O(r))),E(s),s instanceof Text?r.appendData(s.data):o.push(S(s));else if(s instanceof Element){let l;for(;l=o.pop();)s.appendChild(l);We(s,t)}}},J=(i,t)=>{let e=i instanceof Text?i.parentNode:i;if(e instanceof Element){let n={startContainer:t.startContainer,startOffset:t.startOffset,endContainer:t.endContainer,endOffset:t.endOffset};We(e,n),t.setStart(n.startContainer,n.startOffset),t.setEnd(n.endContainer,n.endOffset)}},$=(i,t,e,n)=>{let o=t,s,r;for(;(s=o.parentNode)&&s!==n&&s instanceof Element&&s.childNodes.length===1;)o=s;E(o),r=i.childNodes.length;let l=i.lastChild;l&&l.nodeName==="BR"&&(i.removeChild(l),r-=1),i.appendChild(S(t)),e.setStart(i,r),e.collapse(!0),J(i,e)},M=(i,t)=>{let e=i.previousSibling,n=i.firstChild,o=i.nodeName==="LI";if(!(o&&(!n||!/^[OU]L$/.test(n.nodeName)))){if(e&&xe(e,i)){if(!j(e))if(o){let r=m("DIV");r.appendChild(S(e)),e.appendChild(r)}else return;E(i);let s=!j(i);e.appendChild(S(i)),s&&B(e,t),n&&M(n,t)}else if(o){let s=m("DIV");i.insertBefore(s,n),_(s)}}};var Ke={"font-weight":{regexp:/^bold|^700/i,replace(){return m("B")}},"font-style":{regexp:/^italic/i,replace(){return m("I")}},"font-family":{regexp:w,replace(i,t){return m("SPAN",{class:i.fontFamily,style:"font-family:"+t})}},"font-size":{regexp:w,replace(i,t){return m("SPAN",{class:i.fontSize,style:"font-size:"+t})}},"text-decoration":{regexp:/^underline/i,replace(){return m("U")}}},Nt=(i,t,e)=>{let n=i.style,o,s;for(let r in Ke){let l=Ke[r],a=n.getPropertyValue(r);if(a&&l.regexp.test(a)){let d=l.replace(e.classNames,a);if(d.nodeName===i.nodeName&&d.className===i.className)continue;s||(s=d),o&&o.appendChild(d),o=d,i.style.setProperty(r,a)}}return s&&o&&(o.appendChild(S(i)),i.appendChild(s)),o||i},pe=i=>(t,e)=>{let n=m(i),o=t.attributes;for(let s=0,r=o.length;s<r;s+=1){let l=o[s];n.setAttribute(l.name,l.value)}return e.replaceChild(n,t),n.appendChild(S(t)),n},gt={1:"10",2:"13",3:"16",4:"18",5:"24",6:"32",7:"48"},St={STRONG:pe("B"),EM:pe("I"),INS:pe("U"),STRIKE:pe("S"),SPAN:Nt,FONT:(i,t,e)=>{let n=i,o=n.face,s=n.size,r=n.color,l=e.classNames,a,d,c,f,u;return o&&(a=m("SPAN",{class:l.fontFamily,style:"font-family:"+o}),u=a,f=a),s&&(d=m("SPAN",{class:l.fontSize,style:"font-size:"+gt[s]+"px"}),u||(u=d),f&&f.appendChild(d),f=d),r&&/^#?([\dA-F]{3}){1,2}$/i.test(r)&&(r.charAt(0)!=="#"&&(r="#"+r),c=m("SPAN",{class:l.color,style:"color:"+r}),u||(u=c),f&&f.appendChild(c),f=c),(!u||!f)&&(u=f=m("SPAN")),t.replaceChild(u,n),f.appendChild(S(n)),f},TT:(i,t,e)=>{let n=m("SPAN",{class:e.classNames.fontFamily,style:'font-family:menlo,consolas,"courier new",monospace'});return t.replaceChild(n,i),n.appendChild(S(i)),n}},Et=/^(?: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)$/,Tt=/^(?:HEAD|META|STYLE)/,Ne=(i,t,e)=>{let n=i.childNodes,o=i;for(;N(o);)o=o.parentNode;let s=new C(o,5);for(let r=0,l=n.length;r<l;r+=1){let a=n[r],d=a.nodeName,c=St[d];if(a instanceof HTMLElement){let f=a.childNodes.length;if(c)a=c(a,i,t);else if(Tt.test(d)){i.removeChild(a),r-=1,l-=1;continue}else if(!Et.test(d)&&!N(a)){r-=1,l+=f-1,i.replaceChild(S(a),a);continue}f&&Ne(a,t,e||d==="PRE")}else{if(a instanceof Text){let f=a.data,u=!w.test(f.charAt(0)),p=!w.test(f.charAt(f.length-1));if(e||!u&&!p)continue;if(u){s.currentNode=a;let h;for(;(h=s.previousPONode())&&!(h.nodeName==="IMG"||h instanceof Text&&w.test(h.data));)if(!N(h)){h=null;break}f=f.replace(/^[ \t\r\n]+/g,h?" ":"")}if(p){s.currentNode=a;let h;for(;(h=s.nextNode())&&!(h.nodeName==="IMG"||h instanceof Text&&w.test(h.data));)if(!N(h)){h=null;break}f=f.replace(/[ \t\r\n]+$/g,h?" ":"")}if(f){a.data=f;continue}}i.removeChild(a),r-=1,l-=1}}return i},ge=i=>{let t=i.childNodes,e=t.length;for(;e--;){let n=t[e];n instanceof Element&&!I(n)?(ge(n),N(n)&&!n.firstChild&&i.removeChild(n)):n instanceof Text&&!n.data&&i.removeChild(n)}},ee=(i,t,e)=>{let n=i.querySelectorAll("BR"),o=[],s=n.length;for(let r=0;r<s;r+=1)o[r]=Y(n[r],e);for(;s--;){let r=n[s],l=r.parentNode;l&&(o[s]?N(l)||B(l,t):E(r))}},Oe=i=>i.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;").split('"').join("&quot;");var le=(i,t)=>{let e=new C(t,1,H);return e.currentNode=i,e},z=(i,t)=>{let e=le(i,t).previousNode();return e!==t?e:null},U=(i,t)=>{let e=le(i,t).nextNode();return e!==t?e:null},ae=i=>!i.textContent&&!i.querySelector("IMG");var L=(i,t)=>{let e=i.startContainer,n;if(N(e))n=z(e,t);else if(e!==t&&e instanceof HTMLElement&&H(e))n=e;else{let o=he(e,i.startOffset);n=U(o,t)}return n&&K(i,n,!0)?n:null},G=(i,t)=>{let e=i.endContainer,n;if(N(e))n=z(e,t);else if(e!==t&&e instanceof HTMLElement&&H(e))n=e;else{let o=Le(e,i.endOffset);if(!o||!t.contains(o)){o=t;let s;for(;s=o.lastChild;)o=s}n=z(o,t)}return n&&K(i,n,!0)?n:null},ze=i=>i instanceof Text?w.test(i.data):i.nodeName==="IMG",Q=(i,t)=>{let e=i.startContainer,n=i.startOffset,o;if(e instanceof Text){if(n)return!1;o=e}else if(o=Le(e,n),o&&!t.contains(o)&&(o=null),!o&&(o=he(e,n),o instanceof Text&&o.length))return!1;let s=L(i,t);if(!s)return!1;let r=new C(s,5,ze);return r.currentNode=o,!r.previousNode()},ie=(i,t)=>{let e=i.endContainer,n=i.endOffset,o;if(e instanceof Text){let l=e.data.length;if(l&&n<l)return!1;o=e}else o=he(e,n);let s=G(i,t);if(!s)return!1;let r=new C(s,5,ze);return r.currentNode=o,!r.nextNode()},ke=(i,t)=>{let e=L(i,t),n=G(i,t),o;e&&n&&(o=e.parentNode,i.setStart(o,Array.from(o.childNodes).indexOf(e)),o=n.parentNode,i.setEnd(o,Array.from(o.childNodes).indexOf(n)+1))};function X(i,t,e,n){let o=document.createRange();return o.setStart(i,t),e&&typeof n=="number"?o.setEnd(e,n):o.setEnd(i,t),o}var Z=(i,t)=>{let{startContainer:e,startOffset:n,endContainer:o,endOffset:s}=i,r;if(e instanceof Text){let a=e.parentNode;if(r=a.childNodes,n===e.length)n=Array.from(r).indexOf(e)+1,i.collapsed&&(o=a,s=n);else{if(n){let d=e.splitText(n);o===e?(s-=n,o=d):o===a&&(s+=1),e=d}n=Array.from(r).indexOf(e)}e=a}else r=e.childNodes;let l=r.length;n===l?e.appendChild(t):e.insertBefore(t,r[n]),e===o&&(s+=r.length-l),i.setStart(e,n),i.setEnd(o,s)},ye=(i,t,e)=>{let n=document.createDocumentFragment();if(i.collapsed)return n;t||(t=i.commonAncestorContainer),t instanceof Text&&(t=t.parentNode);let o=i.startContainer,s=i.startOffset,r=D(i.endContainer,i.endOffset,t,e),l=0,a=D(o,s,t,e);for(;a&&a!==r;){let d=a.nextSibling;n.appendChild(a),a=d}return o instanceof Text&&r instanceof Text&&(o.appendData(r.data),E(r),r=o,l=s),i.setStart(o,s),r?i.setEnd(r,l):i.setEnd(t,t.childNodes.length),_(t),n},Ge=(i,t,e)=>{i.currentNode=e;let n;for(;n=i[t]();){if(n instanceof Text||I(n))return n;if(!N(n))return null}return null},F=(i,t)=>{let e=L(i,t),n=G(i,t),o=e!==n;e&&n&&(v(i),P(i,e,n,t));let s=ye(i,null,t);v(i),o&&(n=G(i,t),e&&n&&e!==n&&$(e,n,i,t)),e&&_(e);let r=t.firstChild;(!r||r.nodeName==="BR")&&(_(t),t.firstChild&&i.selectNodeContents(t.firstChild)),i.collapse(!0);let l=i.startContainer,a=i.startOffset,d=new C(t,5),c=l,f=a;(!(c instanceof Text)||f===c.data.length)&&(c=Ge(d,"nextNode",c),f=0);let u=l,p=a-1;(!(u instanceof Text)||p===-1)&&(u=Ge(d,"previousPONode",c||(l instanceof Text?l:l.childNodes[a]||l)),u instanceof Text&&(p=u.data.length));let h=null,T=0;return c instanceof Text&&c.data.charAt(f)===" "&&Q(i,t)?(h=c,T=f):u instanceof Text&&u.data.charAt(p)===" "&&(c instanceof Text&&c.data.charAt(f)===" "||ie(i,t))&&(h=u,T=p),h&&h.replaceData(T,1,"\xA0"),i.setStart(l,a),i.collapse(!0),s},Ze=(i,t,e)=>{let n=t.firstChild&&N(t.firstChild),o;for(B(t,e),o=t;o=U(o,e);)_(o);i.collapsed||F(i,e),v(i),i.collapse(!1);let s=g(i.endContainer,e,"BLOCKQUOTE")||e,r=L(i,e),l=null,a=U(t,t),d=!n&&!!r&&ae(r);if(r&&a&&!d&&!g(a,t,"PRE")&&!g(a,t,"TABLE")){P(i,r,r,e),i.collapse(!0);let c=i.endContainer,f=i.endOffset;if(ee(r,e,!1),N(c)){let u=D(c,f,z(c,e)||e,e);c=u.parentNode,f=Array.from(c.childNodes).indexOf(u)}if(f!==O(c))for(l=document.createDocumentFragment();o=c.childNodes[f];)l.appendChild(o);$(c,a,i,e),f=Array.from(c.parentNode.childNodes).indexOf(c)+1,c=c.parentNode,i.setEnd(c,f)}if(O(t)){d&&r&&(i.setEndBefore(r),i.collapse(!1),E(r)),P(i,s,s,e);let c=D(i.endContainer,i.endOffset,s,e),f=c?c.previousSibling:s.lastChild;s.insertBefore(t,c),c?i.setEndBefore(c):i.setEnd(s,O(s)),r=G(i,e),v(i);let u=i.endContainer,p=i.endOffset;c&&j(c)&&M(c,e),c=f&&f.nextSibling,c&&j(c)&&M(c,e),i.setEnd(u,p)}if(l&&r){let c=i.cloneRange();$(r,l,c,e),i.setEnd(c.endContainer,c.endOffset)}v(i)};var Be=Array.prototype.indexOf,_t=(i,t,e,n,o,s)=>{let r=i.clipboardData,l=document.body,a=m("DIV"),d,c;t.childNodes.length===1&&t.childNodes[0]instanceof Text?(c=t.childNodes[0].data.replace(/ /g," "),s=!0):(a.appendChild(t),d=a.innerHTML,n&&(d=n(d))),c!==void 0||(o&&d!==void 0?c=o(d):(ee(a,e,!0),a.setAttribute("style","position:fixed;overflow:hidden;bottom:100%;right:100%;"),l.appendChild(a),c=a.innerText||a.textContent,c=c.replace(/ /g," "),l.removeChild(a))),de&&(c=c.replace(/\r?\n/g,`\r
`)),!s&&d&&c!==d&&r.setData("text/html",d),r.setData("text/plain",c),i.preventDefault()},je=(i,t,e,n,o,s,r)=>{if(!se&&i.clipboardData){let l=L(t,e),a=G(t,e),d=e;l===a&&(l!=null&&l.contains(t.commonAncestorContainer))&&(d=l);let c;n?c=F(t,e):(t=t.cloneRange(),v(t),P(t,d,d,e),c=t.cloneContents());let f=t.commonAncestorContainer;for(f instanceof Text&&(f=f.parentNode);f&&f!==d;){let u=f.cloneNode(!1);u.appendChild(c),c=u,f=f.parentNode}return _t(i,c,e,o,s,r),!0}return!1},Qe=function(i){let t=this.getSelection(),e=this._root;if(t.collapsed){i.preventDefault();return}this.saveUndoState(t),je(i,t,e,!0,this._config.willCutCopy,null,!1)||setTimeout(()=>{try{this._ensureBottomLine()}catch(o){this._config.didError(o)}},0),this.setSelection(t)},Xe=function(i){je(i,this.getSelection(),this._root,!1,this._config.willCutCopy,null,!1)},Ae=function(i){this._isShiftDown=i.shiftKey},$e=function(i){let t=i.clipboardData,e=t==null?void 0:t.items,n=this._isShiftDown,o=!1,s=!1,r=null,l=null;if(e){let b=e.length;for(;b--;){let R=e[b],A=R.type;A==="text/html"?l=R:A==="text/plain"||A==="text/uri-list"?r=R:A==="text/rtf"?o=!0:/^image\/.*/.test(A)&&(s=!0)}if(s&&!(o&&l)){i.preventDefault(),this.fireEvent("pasteImage",{clipboardData:t});return}if(!se){i.preventDefault(),l&&(!n||!r)?l.getAsString(R=>{this.insertHTML(R,!0)}):r&&r.getAsString(R=>{let A=!1,we=this.getSelection();if(!we.collapsed&&w.test(we.toString())){let Ie=this.linkRegExp.exec(R);A=!!Ie&&Ie[0].length===R.length}A?this.makeLink(R):this.insertPlainText(R,!0)});return}}let a=t==null?void 0:t.types;if(!se&&a&&(Be.call(a,"text/html")>-1||!Fe&&Be.call(a,"text/plain")>-1&&Be.call(a,"text/rtf")<0)){i.preventDefault();let b;!n&&(b=t.getData("text/html"))?this.insertHTML(b,!0):((b=t.getData("text/plain"))||(b=t.getData("text/uri-list")))&&this.insertPlainText(b,!0);return}let d=document.body,c=this.getSelection(),f=c.startContainer,u=c.startOffset,p=c.endContainer,h=c.endOffset,T=m("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});d.appendChild(T),c.selectNodeContents(T),this.setSelection(c),setTimeout(()=>{try{let b="",R=T,A;for(;T=R;)R=T.nextSibling,E(T),A=T.firstChild,A&&A===T.lastChild&&A instanceof HTMLDivElement&&(T=A),b+=T.innerHTML;this.setSelection(X(f,u,p,h)),b&&this.insertHTML(b,!0)}catch(b){this._config.didError(b)}},0)},Ve=function(i){if(!i.dataTransfer)return;let t=i.dataTransfer.types,e=t.length,n=!1,o=!1;for(;e--;)switch(t[e]){case"text/plain":n=!0;break;case"text/html":o=!0;break;default:return}(o||n&&this.saveUndoState)&&this.saveUndoState()};var De=(i,t,e)=>{t.preventDefault(),i.splitBlock(t.shiftKey,e)};var te=(i,t)=>{try{t||(t=i.getSelection());let e=t.startContainer;e instanceof Text&&(e=e.parentNode);let n=e;for(;N(n)&&(!n.textContent||n.textContent===W);)e=n,n=e.parentNode;e!==n&&(t.setStart(n,Array.from(n.childNodes).indexOf(e)),t.collapse(!0),n.removeChild(e),H(n)||(n=z(n,i._root)||i._root),_(n),v(t)),e===i._root&&(e=e.firstChild)&&e.nodeName==="BR"&&E(e),i._ensureBottomLine(),i.setSelection(t),i._updatePath(t,!0)}catch(e){i._config.didError(e)}},Se=(i,t)=>{let e;for(;(e=i.parentNode)&&!(e===t||e.isContentEditable);)i=e;E(i)},Ee=(i,t,e)=>{if(g(t,i._root,"A"))return;let n=t.data||"",o=Math.max(n.lastIndexOf(" ",e-1),n.lastIndexOf("\xA0",e-1))+1,s=n.slice(o,e),r=i.linkRegExp.exec(s);if(r){let l=i.getSelection();i._docWasChanged(),i._recordUndoState(l),i._getRangeAndRemoveBookmark(l);let a=o+r.index,d=a+r[0].length;a&&(t=t.splitText(a));let c=i._config.tagAttributes.a,f=m("A",Object.assign({href:r[1]?/^(?:ht|f)tps?:/i.test(r[1])?r[1]:"http://"+r[1]:"mailto:"+r[0]},c));f.textContent=n.slice(a,d),t.parentNode.insertBefore(f,t);let u=l.startOffset;if(t.data=n.slice(d),l.startContainer===t){let p=u-d;l.setStart(t,p),l.setEnd(t,p)}i.setSelection(l)}};var Ye=(i,t,e)=>{let n=i._root;if(i._removeZWS(),i.saveUndoState(e),!e.collapsed)t.preventDefault(),F(e,n),te(i,e);else if(Q(e,n)){t.preventDefault();let o=L(e,n);if(!o)return;let s=o;B(s.parentNode,n);let r=z(s,n);if(r){if(!r.isContentEditable){Se(r,n);return}for($(r,s,e,n),s=r.parentNode;s!==n&&!s.nextSibling;)s=s.parentNode;s!==n&&(s=s.nextSibling)&&M(s,n),i.setSelection(e)}else if(s){if(g(s,n,"UL")||g(s,n,"OL")){i.decreaseListLevel(e);return}else if(g(s,n,"BLOCKQUOTE")){i.removeQuote(e);return}i.setSelection(e),i._updatePath(e,!0)}}else i.setSelection(e),setTimeout(()=>{te(i)},0)};var Je=(i,t,e)=>{let n=i._root,o,s,r,l,a,d;if(i._removeZWS(),i.saveUndoState(e),!e.collapsed)t.preventDefault(),F(e,n),te(i,e);else if(ie(e,n)){if(t.preventDefault(),o=L(e,n),!o)return;if(B(o.parentNode,n),s=U(o,n),s){if(!s.isContentEditable){Se(s,n);return}for($(o,s,e,n),s=o.parentNode;s!==n&&!s.nextSibling;)s=s.parentNode;s!==n&&(s=s.nextSibling)&&M(s,n),i.setSelection(e),i._updatePath(e,!0)}}else{if(r=e.cloneRange(),P(e,n,n,n),l=e.endContainer,a=e.endOffset,l instanceof Element&&(d=l.childNodes[a],d&&d.nodeName==="IMG")){t.preventDefault(),E(d),v(e),te(i,e);return}i.setSelection(r),setTimeout(()=>{te(i)},0)}};var et=(i,t,e)=>{let n=i._root;if(i._removeZWS(),e.collapsed&&Q(e,n)){let o=L(e,n),s;for(;s=o.parentNode;){if(s.nodeName==="UL"||s.nodeName==="OL"){t.preventDefault(),i.increaseListLevel(e);break}o=s}}},tt=(i,t,e)=>{let n=i._root;if(i._removeZWS(),e.collapsed&&Q(e,n)){let o=e.startContainer;(g(o,n,"UL")||g(o,n,"OL"))&&(t.preventDefault(),i.decreaseListLevel(e))}};var nt=(i,t,e)=>{let n,o=i._root;if(i._recordUndoState(e),i._getRangeAndRemoveBookmark(e),e.collapsed||(F(e,o),i._ensureBottomLine(),i.setSelection(e),i._updatePath(e,!0)),n=e.endContainer,e.endOffset===O(n))do if(n.nodeName==="A"){e.setStartAfter(n);break}while(!n.nextSibling&&(n=n.parentNode)&&n!==o);if(i._config.addLinks){let s=e.cloneRange();v(s);let r=s.startContainer,l=s.startOffset;setTimeout(()=>{Ee(i,r,l)},0)}i.setSelection(e)};var bt={8:"Backspace",9:"Tab",13:"Enter",27:"Escape",32:"Space",33:"PageUp",34:"PageDown",37:"ArrowLeft",38:"ArrowUp",39:"ArrowRight",40:"ArrowDown",46:"Delete",191:"/",219:"[",220:"\\",221:"]"},it=function(i){let t=i.keyCode,e=bt[t],n="",o=this.getSelection();i.defaultPrevented||(e||(e=String.fromCharCode(t).toLowerCase(),/^[A-Za-z0-9]$/.test(e)||(e="")),111<t&&t<124&&(e="F"+(t-111)),e!=="Backspace"&&e!=="Delete"&&(i.altKey&&(n+="Alt-"),i.ctrlKey&&(n+="Ctrl-"),i.metaKey&&(n+="Meta-"),i.shiftKey&&(n+="Shift-")),de&&i.shiftKey&&e==="Delete"&&(n+="Shift-"),e=n+e,this._keyHandlers[e]?this._keyHandlers[e](this,i,o):!o.collapsed&&!i.isComposing&&!i.ctrlKey&&!i.metaKey&&(i.key||e).length===1&&(this.saveUndoState(o),F(o,this._root),this._ensureBottomLine(),this.setSelection(o),this._updatePath(o,!0)))},x={Backspace:Ye,Delete:Je,Tab:et,"Shift-Tab":tt,Space:nt,ArrowLeft(i){i._removeZWS()},ArrowRight(i){i._removeZWS()}};He||(x.Enter=De,x["Shift-Enter"]=De);!ce&&!Ce&&(x.PageUp=i=>{i.moveCursorToStart()},x.PageDown=i=>{i.moveCursorToEnd()});var oe=(i,t)=>(t=t||null,(e,n)=>{n.preventDefault();let o=e.getSelection();e.hasFormat(i,null,o)?e.changeFormat(null,{tag:i},o):e.changeFormat({tag:i},t,o)});x[k+"b"]=oe("B");x[k+"i"]=oe("I");x[k+"u"]=oe("U");x[k+"Shift-7"]=oe("S");x[k+"Shift-5"]=oe("SUB",{tag:"SUP"});x[k+"Shift-6"]=oe("SUP",{tag:"SUB"});x[k+"Shift-8"]=(i,t)=>{t.preventDefault();let e=i.getPath();/(?:^|>)UL/.test(e)?i.removeList():i.makeUnorderedList()};x[k+"Shift-9"]=(i,t)=>{t.preventDefault();let e=i.getPath();/(?:^|>)OL/.test(e)?i.removeList():i.makeOrderedList()};x[k+"["]=(i,t)=>{t.preventDefault();let e=i.getPath();/(?:^|>)BLOCKQUOTE/.test(e)||!/(?:^|>)[OU]L/.test(e)?i.decreaseQuoteLevel():i.decreaseListLevel()};x[k+"]"]=(i,t)=>{t.preventDefault();let e=i.getPath();/(?:^|>)BLOCKQUOTE/.test(e)||!/(?:^|>)[OU]L/.test(e)?i.increaseQuoteLevel():i.increaseListLevel()};x[k+"d"]=(i,t)=>{t.preventDefault(),i.toggleCode()};x[k+"z"]=(i,t)=>{t.preventDefault(),i.undo()};x[k+"y"]=x[k+"Shift-z"]=(i,t)=>{t.preventDefault(),i.redo()};var Te=class{constructor(t,e){this.customEvents=new Set(["pathChange","select","input","pasteImage","undoStateChange"]);this.startSelectionId="squire-selection-start";this.endSelectionId="squire-selection-end";this.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;this.tagAfterSplit={DT:"DD",DD:"DT",LI:"LI",PRE:"PRE"};this._root=t,this._config=this._makeConfig(e),this._isFocused=!1,this._lastSelection=X(t,0),this._willRestoreSelection=!1,this._mayHaveZWS=!1,this._lastAnchorNode=null,this._lastFocusNode=null,this._path="",this._events=new Map,this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0,this._isInUndoState=!1,this._ignoreChange=!1,this._ignoreAllChanges=!1,this.addEventListener("selectionchange",this._updatePathOnEvent),this.addEventListener("blur",this._enableRestoreSelection),this.addEventListener("mousedown",this._disableRestoreSelection),this.addEventListener("touchstart",this._disableRestoreSelection),this.addEventListener("focus",this._restoreSelection),this._isShiftDown=!1,this.addEventListener("cut",Qe),this.addEventListener("copy",Xe),this.addEventListener("paste",$e),this.addEventListener("drop",Ve),this.addEventListener("keydown",Ae),this.addEventListener("keyup",Ae),this.addEventListener("keydown",it),this._keyHandlers=Object.create(x);let n=new MutationObserver(()=>this._docWasChanged());n.observe(t,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._mutation=n,t.setAttribute("contenteditable","true");try{document.execCommand("enableObjectResizing",!1,"false"),document.execCommand("enableInlineTableEditing",!1,"false")}catch(o){}this.addEventListener("beforeinput",this._beforeInput),this.setHTML("")}destroy(){this._events.forEach((t,e)=>{this.removeEventListener(e)}),this._mutation.disconnect(),this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0}_makeConfig(t){let e={blockTag:"DIV",blockAttributes:null,tagAttributes:{},classNames:{color:"color",fontFamily:"font",fontSize:"size",highlight:"highlight"},undo:{documentSizeThreshold:-1,undoLimit:-1},addLinks:!0,willCutCopy:null,sanitizeToDOMFragment:n=>{let o=DOMPurify.sanitize(n,{ALLOW_UNKNOWN_PROTOCOLS:!0,WHOLE_DOCUMENT:!1,RETURN_DOM:!0,RETURN_DOM_FRAGMENT:!0,FORCE_BODY:!1});return o?document.importNode(o,!0):document.createDocumentFragment()},didError:n=>console.log(n)};return t&&(Object.assign(e,t),e.blockTag=e.blockTag.toUpperCase()),e}setKeyHandler(t,e){return this._keyHandlers[t]=e,this}_beforeInput(t){switch(t.inputType){case"insertText":Me&&t.data&&t.data.includes(`
`)&&t.preventDefault();break;case"insertLineBreak":t.preventDefault(),this.splitBlock(!0);break;case"insertParagraph":t.preventDefault(),this.splitBlock(!1);break;case"insertOrderedList":t.preventDefault(),this.makeOrderedList();break;case"insertUnoderedList":t.preventDefault(),this.makeUnorderedList();break;case"historyUndo":t.preventDefault(),this.undo();break;case"historyRedo":t.preventDefault(),this.redo();break;case"formatBold":t.preventDefault(),this.bold();break;case"formaItalic":t.preventDefault(),this.italic();break;case"formatUnderline":t.preventDefault(),this.underline();break;case"formatStrikeThrough":t.preventDefault(),this.strikethrough();break;case"formatSuperscript":t.preventDefault(),this.superscript();break;case"formatSubscript":t.preventDefault(),this.subscript();break;case"formatJustifyFull":case"formatJustifyCenter":case"formatJustifyRight":case"formatJustifyLeft":{t.preventDefault();let e=t.inputType.slice(13).toLowerCase();e==="full"&&(e="justify"),this.setTextAlignment(e);break}case"formatRemove":t.preventDefault(),this.removeAllFormatting();break;case"formatSetBlockTextDirection":{t.preventDefault();let e=t.data;e==="null"&&(e=null),this.setTextDirection(e);break}case"formatBackColor":t.preventDefault(),this.setHighlightColor(t.data);break;case"formatFontColor":t.preventDefault(),this.setTextColor(t.data);break;case"formatFontName":t.preventDefault(),this.setFontFace(t.data);break}}handleEvent(t){this.fireEvent(t.type,t)}fireEvent(t,e){let n=this._events.get(t);if(/^(?:focus|blur)/.test(t)){let o=this._root===document.activeElement;if(t==="focus"){if(!o||this._isFocused)return this;this._isFocused=!0}else{if(o||!this._isFocused)return this;this._isFocused=!1}}if(n){let o=e instanceof Event?e:new CustomEvent(t,{detail:e});n=n.slice();for(let s of n)try{"handleEvent"in s?s.handleEvent(o):s.call(this,o)}catch(r){this._config.didError(r)}}return this}addEventListener(t,e){let n=this._events.get(t),o=this._root;return n||(n=[],this._events.set(t,n),this.customEvents.has(t)||(t==="selectionchange"&&(o=document),o.addEventListener(t,this,!0))),n.push(e),this}removeEventListener(t,e){let n=this._events.get(t),o=this._root;if(n){if(e){let s=n.length;for(;s--;)n[s]===e&&n.splice(s,1)}else n.length=0;n.length||(this._events.delete(t),this.customEvents.has(t)||(t==="selectionchange"&&(o=document),o.removeEventListener(t,this,!0)))}return this}focus(){return this._root.focus({preventScroll:!0}),this}blur(){return this._root.blur(),this}_enableRestoreSelection(){this._willRestoreSelection=!0}_disableRestoreSelection(){this._willRestoreSelection=!1}_restoreSelection(){this._willRestoreSelection&&this.setSelection(this._lastSelection)}_removeZWS(){this._mayHaveZWS&&(me(this._root),this._mayHaveZWS=!1)}_saveRangeToBookmark(t){let e=m("INPUT",{id:this.startSelectionId,type:"hidden"}),n=m("INPUT",{id:this.endSelectionId,type:"hidden"}),o;Z(t,e),t.collapse(!1),Z(t,n),e.compareDocumentPosition(n)&Node.DOCUMENT_POSITION_PRECEDING&&(e.id=this.endSelectionId,n.id=this.startSelectionId,o=e,e=n,n=o),t.setStartAfter(e),t.setEndBefore(n)}_getRangeAndRemoveBookmark(t){let e=this._root,n=e.querySelector("#"+this.startSelectionId),o=e.querySelector("#"+this.endSelectionId);if(n&&o){let s=n.parentNode,r=o.parentNode,l=Array.from(s.childNodes).indexOf(n),a=Array.from(r.childNodes).indexOf(o);s===r&&(a-=1),n.remove(),o.remove(),t||(t=document.createRange()),t.setStart(s,l),t.setEnd(r,a),J(s,t),s!==r&&J(r,t),t.collapsed&&(s=t.startContainer,s instanceof Text&&(r=s.childNodes[t.startOffset],(!r||!(r instanceof Text))&&(r=s.childNodes[t.startOffset-1]),r&&r instanceof Text&&(t.setStart(r,0),t.collapse(!0))))}return t||null}getSelection(){let t=window.getSelection(),e=this._root,n=null;if(this._isFocused&&t&&t.rangeCount){n=t.getRangeAt(0).cloneRange();let o=n.startContainer,s=n.endContainer;o&&I(o)&&n.setStartBefore(o),s&&I(s)&&n.setEndBefore(s)}return n&&e.contains(n.commonAncestorContainer)?this._lastSelection=n:(n=this._lastSelection,document.contains(n.commonAncestorContainer)||(n=null)),n||(n=X(e.firstElementChild||e,0)),n}setSelection(t){if(this._lastSelection=t,!this._isFocused)this._enableRestoreSelection();else{let e=window.getSelection();e&&e.setBaseAndExtent(t.startContainer,t.startOffset,t.endContainer,t.endOffset)}return this}_moveCursorTo(t){let e=this._root,n=X(e,t?0:e.childNodes.length);return v(n),this.setSelection(n),this}moveCursorToStart(){return this._moveCursorTo(!0)}moveCursorToEnd(){return this._moveCursorTo(!1)}getCursorPosition(){let t=this.getSelection(),e=t.getBoundingClientRect();if(e&&!e.top){this._ignoreChange=!0;let n=m("SPAN");n.textContent=W,Z(t,n),e=n.getBoundingClientRect();let o=n.parentNode;o.removeChild(n),J(o,t)}return e}getPath(){return this._path}_updatePathOnEvent(){this._isFocused&&this._updatePath(this.getSelection())}_updatePath(t,e){let n=t.startContainer,o=t.endContainer,s;(e||n!==this._lastAnchorNode||o!==this._lastFocusNode)&&(this._lastAnchorNode=n,this._lastFocusNode=o,s=n&&o?n===o?this._getPath(o):"(selection)":"",this._path!==s&&(this._path=s,this.fireEvent("pathChange",{path:s}))),this.fireEvent(t.collapsed?"cursor":"select",{range:t})}_getPath(t){let e=this._root,n=this._config,o="";if(t&&t!==e){let s=t.parentNode;if(o=s?this._getPath(s):"",t instanceof HTMLElement){let r=t.id,l=t.classList,a=Array.from(l).sort(),d=t.dir,c=n.classNames;o+=(o?">":"")+t.nodeName,r&&(o+="#"+r),a.length&&(o+=".",o+=a.join(".")),d&&(o+="[dir="+d+"]"),l.contains(c.highlight)&&(o+="[backgroundColor="+t.style.backgroundColor.replace(/ /g,"")+"]"),l.contains(c.color)&&(o+="[color="+t.style.color.replace(/ /g,"")+"]"),l.contains(c.fontFamily)&&(o+="[fontFamily="+t.style.fontFamily.replace(/ /g,"")+"]"),l.contains(c.fontSize)&&(o+="[fontSize="+t.style.fontSize+"]")}}return o}modifyDocument(t){let e=this._mutation;return e&&(e.takeRecords().length&&this._docWasChanged(),e.disconnect()),this._ignoreAllChanges=!0,t(),this._ignoreAllChanges=!1,e&&(e.observe(this._root,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._ignoreChange=!1),this}_docWasChanged(){if(qe(),this._mayHaveZWS=!0,!this._ignoreAllChanges){if(this._ignoreChange){this._ignoreChange=!1;return}this._isInUndoState&&(this._isInUndoState=!1,this.fireEvent("undoStateChange",{canUndo:!0,canRedo:!1})),this.fireEvent("input")}}_recordUndoState(t,e){if(!this._isInUndoState||e){let n=this._undoIndex,o=this._undoStack,s=this._config.undo,r=s.documentSizeThreshold,l=s.undoLimit;e||(n+=1),n<this._undoStackLength&&(o.length=this._undoStackLength=n),t&&this._saveRangeToBookmark(t);let a=this._getRawHTML();r>-1&&a.length*2>r&&l>-1&&n>l&&(o.splice(0,n-l),n=l,this._undoStackLength=l),o[n]=a,this._undoIndex=n,this._undoStackLength+=1,this._isInUndoState=!0}return this}saveUndoState(t){return t||(t=this.getSelection()),this._recordUndoState(t,this._isInUndoState),this._getRangeAndRemoveBookmark(t),this}undo(){if(this._undoIndex!==0||!this._isInUndoState){this._recordUndoState(this.getSelection(),!1),this._undoIndex-=1,this._setRawHTML(this._undoStack[this._undoIndex]);let t=this._getRangeAndRemoveBookmark();t&&this.setSelection(t),this._isInUndoState=!0,this.fireEvent("undoStateChange",{canUndo:this._undoIndex!==0,canRedo:!0}),this.fireEvent("input")}return this}redo(){let t=this._undoIndex,e=this._undoStackLength;if(t+1<e&&this._isInUndoState){this._undoIndex+=1,this._setRawHTML(this._undoStack[this._undoIndex]);let n=this._getRangeAndRemoveBookmark();n&&this.setSelection(n),this.fireEvent("undoStateChange",{canUndo:!0,canRedo:t+2<e}),this.fireEvent("input")}return this}getRoot(){return this._root}_getRawHTML(){return this._root.innerHTML}_setRawHTML(t){let e=this._root;e.innerHTML=t;let n=e,o=n.firstChild;if(!o||o.nodeName==="BR"){let s=this.createDefaultBlock();o?n.replaceChild(s,o):n.appendChild(s)}else for(;n=U(n,e);)_(n);return this._ignoreChange=!0,this}getHTML(t){let e;t&&(e=this.getSelection(),this._saveRangeToBookmark(e));let n=this._getRawHTML().replace(/\u200B/g,"");return t&&this._getRangeAndRemoveBookmark(e),n}setHTML(t){let e=this._config.sanitizeToDOMFragment(t,this),n=this._root;Ne(e,this._config),ee(e,n,!1),B(e,n);let o=e,s=o.firstChild;if(!s||s.nodeName==="BR"){let l=this.createDefaultBlock();s?o.replaceChild(l,s):o.appendChild(l)}else for(;o=U(o,n);)_(o);for(this._ignoreChange=!0;s=n.lastChild;)n.removeChild(s);n.appendChild(e),this._undoIndex=-1,this._undoStack.length=0,this._undoStackLength=0,this._isInUndoState=!1;let r=this._getRangeAndRemoveBookmark()||X(n.firstElementChild||n,0);return this.saveUndoState(r),this.setSelection(r),this._updatePath(r,!0),this}insertHTML(t,e){let n=this._config,o=n.sanitizeToDOMFragment(t,this),s=this.getSelection();this.saveUndoState(s);try{let r=this._root;n.addLinks&&this.addDetectedLinks(o,o),Ne(o,this._config),ee(o,r,!1),ge(o),o.normalize();let l=o;for(;l=U(l,o);)_(l);let a=!0;if(e){let d=new CustomEvent("willPaste",{detail:{fragment:o}});this.fireEvent("willPaste",d),o=d.detail.fragment,a=!d.defaultPrevented}a&&(Ze(s,o,r),s.collapse(!1),Re(s,"A",r),this._ensureBottomLine()),this.setSelection(s),this._updatePath(s,!0),e&&this.focus()}catch(r){this._config.didError(r)}return this}insertElement(t,e){if(e||(e=this.getSelection()),e.collapse(!0),N(t))Z(e,t),e.setStartAfter(t);else{let n=this._root,o=L(e,n),s=o||n,r=null;for(;s!==n&&!s.nextSibling;)s=s.parentNode;if(s!==n){let a=s.parentNode;r=D(a,s.nextSibling,n,n)}o&&ae(o)&&E(o),n.insertBefore(t,r);let l=this.createDefaultBlock();n.insertBefore(l,r),e.setStart(l,0),e.setEnd(l,0),v(e)}return this.focus(),this.setSelection(e),this._updatePath(e),this}insertImage(t,e){let n=m("IMG",Object.assign({src:t},e));return this.insertElement(n),n}insertPlainText(t,e){let n=this.getSelection();if(n.collapsed&&g(n.startContainer,this._root,"PRE")){let c=n.startContainer,f=n.startOffset,u;if(!c||!(c instanceof Text)){let h=document.createTextNode("");c.insertBefore(h,c.childNodes[f]),u=h,f=0}else u=c;let p=!0;if(e){let h=new CustomEvent("willPaste",{detail:{text:t}});this.fireEvent("willPaste",h),t=h.detail.text,p=!h.defaultPrevented}return p&&(u.insertData(f,t),n.setStart(u,f+t.length),n.collapse(!0)),this.setSelection(n),this}let o=t.split(`
`),s=this._config,r=s.blockTag,l=s.blockAttributes,a="</"+r+">",d="<"+r;for(let c in l)d+=" "+c+'="'+Oe(l[c])+'"';d+=">";for(let c=0,f=o.length;c<f;c+=1){let u=o[c];u=Oe(u).replace(/ (?=(?: |$))/g,"&nbsp;"),c&&(u=d+(u||"<BR>")+a),o[c]=u}return this.insertHTML(o.join(""),e)}getSelectedText(){let t=this.getSelection();if(t.collapsed)return"";let e=t.startContainer,n=t.endContainer,o=new C(t.commonAncestorContainer,5,d=>K(t,d,!0));o.currentNode=e;let s=e,r="",l=!1,a;for((!(s instanceof Element)&&!(s instanceof Text)||!o.filter(s))&&(s=o.nextNode());s;)s instanceof Text?(a=s.data,a&&/\S/.test(a)&&(s===n&&(a=a.slice(0,t.endOffset)),s===e&&(a=a.slice(t.startOffset)),r+=a,l=!0)):(s.nodeName==="BR"||l&&!N(s))&&(r+=`
`,l=!1),s=o.nextNode();return r}getFontInfo(t){let e={color:void 0,backgroundColor:void 0,fontFamily:void 0,fontSize:void 0};t||(t=this.getSelection());let n=0,o=t.commonAncestorContainer;if(t.collapsed||o instanceof Text)for(o instanceof Text&&(o=o.parentNode);n<4&&o;){let s=o.style;if(s){let r=s.color;!e.color&&r&&(e.color=r,n+=1);let l=s.backgroundColor;!e.backgroundColor&&l&&(e.backgroundColor=l,n+=1);let a=s.fontFamily;!e.fontFamily&&a&&(e.fontFamily=a,n+=1);let d=s.fontSize;!e.fontSize&&d&&(e.fontSize=d,n+=1)}o=o.parentNode}return e}hasFormat(t,e,n){t=t.toUpperCase(),e||(e={}),n||(n=this.getSelection()),!n.collapsed&&n.startContainer instanceof Text&&n.startOffset===n.startContainer.length&&n.startContainer.nextSibling&&n.setStartBefore(n.startContainer.nextSibling),!n.collapsed&&n.endContainer instanceof Text&&n.endOffset===0&&n.endContainer.previousSibling&&n.setEndAfter(n.endContainer.previousSibling);let o=this._root,s=n.commonAncestorContainer;if(g(s,o,t,e))return!0;if(s instanceof Text)return!1;let r=new C(s,4,d=>K(n,d,!0)),l=!1,a;for(;a=r.nextNode();){if(!g(a,o,t,e))return!1;l=!0}return l}changeFormat(t,e,n,o){return n||(n=this.getSelection()),this.saveUndoState(n),e&&(n=this._removeFormat(e.tag.toUpperCase(),e.attributes||{},n,o)),t&&(n=this._addFormat(t.tag.toUpperCase(),t.attributes||{},n)),this.setSelection(n),this._updatePath(n,!0),this.focus()}_addFormat(t,e,n){let o=this._root;if(n.collapsed){let s=_(m(t,e));Z(n,s);let r=s.firstChild||s,l=r instanceof Text?r.length:0;n.setStart(r,l),n.collapse(!0);let a=s;for(;N(a);)a=a.parentNode;me(a,s)}else{let s=new C(n.commonAncestorContainer,5,c=>(c instanceof Text||c.nodeName==="BR"||c.nodeName==="IMG")&&K(n,c,!0)),{startContainer:r,startOffset:l,endContainer:a,endOffset:d}=n;if(s.currentNode=r,!(r instanceof Element)&&!(r instanceof Text)||!s.filter(r)){let c=s.nextNode();if(!c)return n;r=c,l=0}do{let c=s.currentNode;if(!g(c,o,t,e)){c===a&&c.length>d&&c.splitText(d),c===r&&l&&(c=c.splitText(l),a===r?(a=c,d-=l):a===r.parentNode&&(d+=1),r=c,l=0);let u=m(t,e);y(c,u),u.appendChild(c)}}while(s.nextNode());n=X(r,l,a,d)}return n}_removeFormat(t,e,n,o){this._saveRangeToBookmark(n);let s;n.collapsed&&(re?s=document.createTextNode(W):s=document.createTextNode(""),Z(n,s));let r=n.commonAncestorContainer;for(;N(r);)r=r.parentNode;let l=n.startContainer,a=n.startOffset,d=n.endContainer,c=n.endOffset,f=[],u=(h,T)=>{if(K(n,h,!1))return;let b,R;if(!K(n,h,!0)){!(h instanceof HTMLInputElement)&&(!(h instanceof Text)||h.data)&&f.push([T,h]);return}if(h instanceof Text)h===d&&c!==h.length&&f.push([T,h.splitText(c)]),h===l&&a&&(h.splitText(a),f.push([T,h]));else for(b=h.firstChild;b;b=R)R=b.nextSibling,u(b,T)},p=Array.from(r.getElementsByTagName(t)).filter(h=>K(n,h,!0)&&ue(h,t,e));return o||p.forEach(h=>{u(h,h)}),f.forEach(([h,T])=>{h=h.cloneNode(!1),y(T,h),h.appendChild(T)}),p.forEach(h=>{y(h,S(h))}),this._getRangeAndRemoveBookmark(n),s&&n.collapse(!1),J(r,n),n}bold(){return this.changeFormat({tag:"B"})}removeBold(){return this.changeFormat(null,{tag:"B"})}italic(){return this.changeFormat({tag:"I"})}removeItalic(){return this.changeFormat(null,{tag:"I"})}underline(){return this.changeFormat({tag:"U"})}removeUnderline(){return this.changeFormat(null,{tag:"U"})}strikethrough(){return this.changeFormat({tag:"S"})}removeStrikethrough(){return this.changeFormat(null,{tag:"S"})}subscript(){return this.changeFormat({tag:"SUB"},{tag:"SUP"})}removeSubscript(){return this.changeFormat(null,{tag:"SUB"})}superscript(){return this.changeFormat({tag:"SUP"},{tag:"SUB"})}removeSuperscript(){return this.changeFormat(null,{tag:"SUP"})}makeLink(t,e){let n=this.getSelection();if(n.collapsed){let o=t.indexOf(":")+1;if(o)for(;t[o]==="/";)o+=1;Z(n,document.createTextNode(t.slice(o)))}return e=Object.assign({href:t},this._config.tagAttributes.a,e),this.changeFormat({tag:"A",attributes:e},{tag:"A"},n)}removeLink(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0)}addDetectedLinks(t,e){let n=new C(t,4,l=>!g(l,e||this._root,"A")),o=this.linkRegExp,s=this._config.tagAttributes.a,r;for(;r=n.nextNode();){let l=r.parentNode,a=r.data,d;for(;d=o.exec(a);){let c=d.index,f=c+d[0].length;c&&l.insertBefore(document.createTextNode(a.slice(0,c)),r);let u=m("A",Object.assign({href:d[1]?/^(?:ht|f)tps?:/i.test(d[1])?d[1]:"http://"+d[1]:"mailto:"+d[0]},s));u.textContent=a.slice(c,f),l.insertBefore(u,r),r.data=a=a.slice(f)}}return this}setFontFace(t){let e=this._config.classNames.fontFamily;return this.changeFormat(t?{tag:"SPAN",attributes:{class:e,style:"font-family: "+t+", sans-serif;"}}:null,{tag:"SPAN",attributes:{class:e}})}setFontSize(t){let e=this._config.classNames.fontSize;return this.changeFormat(t?{tag:"SPAN",attributes:{class:e,style:"font-size: "+(typeof t=="number"?t+"px":t)}}:null,{tag:"SPAN",attributes:{class:e}})}setTextColor(t){let e=this._config.classNames.color;return this.changeFormat(t?{tag:"SPAN",attributes:{class:e,style:"color:"+t}}:null,{tag:"SPAN",attributes:{class:e}})}setHighlightColor(t){let e=this._config.classNames.highlight;return this.changeFormat(t?{tag:"SPAN",attributes:{class:e,style:"background-color:"+t}}:null,{tag:"SPAN",attributes:{class:e}})}_ensureBottomLine(){let t=this._root,e=t.lastElementChild;(!e||e.nodeName!==this._config.blockTag||!H(e))&&t.appendChild(this.createDefaultBlock())}createDefaultBlock(t){let e=this._config;return _(m(e.blockTag,e.blockAttributes,t))}splitBlock(t,e){e||(e=this.getSelection());let n=this._root,o,s,r,l;if(this._recordUndoState(e),this._removeZWS(),this._getRangeAndRemoveBookmark(e),e.collapsed||F(e,n),this._config.addLinks){v(e);let u=e.startContainer,p=e.startOffset;setTimeout(()=>{Ee(this,u,p)},0)}if(o=L(e,n),o&&(s=g(o,n,"PRE"))){v(e),r=e.startContainer;let u=e.startOffset;return r instanceof Text||(r=document.createTextNode(""),s.insertBefore(r,s.firstChild)),!t&&r instanceof Text&&(r.data.charAt(u-1)===`
`||Q(e,n))&&(r.data.charAt(u)===`
`||ie(e,n))?(r.deleteData(u&&u-1,u?2:1),l=D(r,u&&u-1,n,n),r=l.previousSibling,r.textContent||E(r),r=this.createDefaultBlock(),l.parentNode.insertBefore(r,l),l.textContent||E(l),e.setStart(r,0)):(r.insertData(u,`
`),_(s),r.length===u+1?e.setStartAfter(r):e.setStart(r,u+1)),e.collapse(!0),this.setSelection(e),this._updatePath(e,!0),this._docWasChanged(),this}if(!o||t||/^T[HD]$/.test(o.nodeName))return Re(e,"A",n),Z(e,m("BR")),e.collapse(!1),this.setSelection(e),this._updatePath(e,!0),this;if((s=g(o,n,"LI"))&&(o=s),ae(o)){if(g(o,n,"UL")||g(o,n,"OL"))return this.decreaseListLevel(e),this;if(g(o,n,"BLOCKQUOTE"))return this.removeQuote(e),this}r=e.startContainer;let a=e.startOffset,d=this.tagAfterSplit[o.nodeName];l=D(r,a,o.parentNode,this._root);let c=this._config,f=null;for(d||(d=c.blockTag,f=c.blockAttributes),ue(l,d,f)||(o=m(d,f),l.dir&&(o.dir=l.dir),y(l,o),o.appendChild(S(l)),l=o),me(o),ge(o),_(o);l instanceof Element;){let u=l.firstChild,p;if(l.nodeName==="A"&&(!l.textContent||l.textContent===W)){u=document.createTextNode(""),y(l,u),l=u;break}for(;u&&u instanceof Text&&!u.data&&(p=u.nextSibling,!(!p||p.nodeName==="BR"));)E(u),u=p;if(!u||u.nodeName==="BR"||u instanceof Text)break;l=u}return e=X(l,0),this.setSelection(e),this._updatePath(e,!0),this}forEachBlock(t,e,n){n||(n=this.getSelection()),e&&this.saveUndoState(n);let o=this._root,s=L(n,o),r=G(n,o);if(s&&r)do if(t(s)||s===r)break;while(s=U(s,o));return e&&(this.setSelection(n),this._updatePath(n,!0)),this}modifyBlocks(t,e){e||(e=this.getSelection()),this._recordUndoState(e,this._isInUndoState);let n=this._root;ke(e,n),P(e,n,n,n);let o=ye(e,n,n);if(!e.collapsed){let s=e.endContainer;if(s===n)e.collapse(!1);else{for(;s.parentNode!==n;)s=s.parentNode;e.setStartBefore(s),e.collapse(!0)}}return Z(e,t.call(this,o)),e.endOffset<e.endContainer.childNodes.length&&M(e.endContainer.childNodes[e.endOffset],n),M(e.startContainer.childNodes[e.startOffset],n),this._getRangeAndRemoveBookmark(e),this.setSelection(e),this._updatePath(e,!0),this}setTextAlignment(t){return this.forEachBlock(e=>{let n=e.className.split(/\s+/).filter(o=>!!o&&!/^align/.test(o)).join(" ");t?(e.className=n+" align-"+t,e.style.textAlign=t):(e.className=n,e.style.textAlign="")},!0),this.focus()}setTextDirection(t){return this.forEachBlock(e=>{t?e.dir=t:e.removeAttribute("dir")},!0),this.focus()}_getListSelection(t,e){let n=t.commonAncestorContainer,o=t.startContainer,s=t.endContainer;for(;n&&n!==e&&!/^[OU]L$/.test(n.nodeName);)n=n.parentNode;if(!n||n===e)return null;for(o===n&&(o=o.childNodes[t.startOffset]),s===n&&(s=s.childNodes[t.endOffset]);o&&o.parentNode!==n;)o=o.parentNode;for(;s&&s.parentNode!==n;)s=s.parentNode;return[n,o,s]}increaseListLevel(t){t||(t=this.getSelection());let e=this._root,n=this._getListSelection(t,e);if(!n)return this.focus();let[o,s,r]=n;if(!s||s===o.firstChild)return this.focus();this._recordUndoState(t,this._isInUndoState);let l=o.nodeName,a=s.previousSibling,d,c;a.nodeName!==l&&(d=this._config.tagAttributes[l.toLowerCase()],a=m(l,d),o.insertBefore(a,s));do c=s===r?null:s.nextSibling,a.appendChild(s);while(s=c);return c=a.nextSibling,c&&M(c,e),this._getRangeAndRemoveBookmark(t),this.setSelection(t),this._updatePath(t,!0),this.focus()}decreaseListLevel(t){t||(t=this.getSelection());let e=this._root,n=this._getListSelection(t,e);if(!n)return this.focus();let[o,s,r]=n;s||(s=o.firstChild),r||(r=o.lastChild),this._recordUndoState(t,this._isInUndoState);let l,a=null;if(s){let d=o.parentNode;if(a=r.nextSibling?D(o,r.nextSibling,d,e):o.nextSibling,d!==e&&d.nodeName==="LI"){for(d=d.parentNode;a;)l=a.nextSibling,r.appendChild(a),a=l;a=o.parentNode.nextSibling}let c=!/^[OU]L$/.test(d.nodeName);do l=s===r?null:s.nextSibling,o.removeChild(s),c&&s.nodeName==="LI"&&(s=this.createDefaultBlock([S(s)])),d.insertBefore(s,a);while(s=l)}return o.firstChild||E(o),a&&M(a,e),this._getRangeAndRemoveBookmark(t),this.setSelection(t),this._updatePath(t,!0),this.focus()}_makeList(t,e){let n=le(t,this._root),o=this._config.tagAttributes,s=o[e.toLowerCase()],r=o.li,l;for(;l=n.nextNode();)if(l.parentNode instanceof HTMLLIElement&&(l=l.parentNode,n.currentNode=l.lastChild),l instanceof HTMLLIElement){l=l.parentNode;let a=l.nodeName;a!==e&&/^[OU]L$/.test(a)&&y(l,m(e,s,[S(l)]))}else{let a=m("LI",r);l.dir&&(a.dir=l.dir);let d=l.previousSibling;d&&d.nodeName===e?(d.appendChild(a),E(l)):y(l,m(e,s,[a])),a.appendChild(S(l)),n.currentNode=a}return t}makeUnorderedList(){return this.modifyBlocks(t=>this._makeList(t,"UL")),this.focus()}makeOrderedList(){return this.modifyBlocks(t=>this._makeList(t,"OL")),this.focus()}removeList(){return this.modifyBlocks(t=>{let e=t.querySelectorAll("UL, OL"),n=t.querySelectorAll("LI"),o=this._root;for(let s=0,r=e.length;s<r;s+=1){let l=e[s],a=S(l);B(a,o),y(l,a)}for(let s=0,r=n.length;s<r;s+=1){let l=n[s];H(l)?y(l,this.createDefaultBlock([S(l)])):(B(l,o),y(l,S(l)))}return t}),this.focus()}increaseQuoteLevel(t){return this.modifyBlocks(e=>m("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e]),t),this.focus()}decreaseQuoteLevel(t){return this.modifyBlocks(e=>(Array.from(e.querySelectorAll("blockquote")).filter(n=>!g(n.parentNode,e,"BLOCKQUOTE")).forEach(n=>{y(n,S(n))}),e),t),this.focus()}removeQuote(t){return this.modifyBlocks(()=>this.createDefaultBlock([m("INPUT",{id:this.startSelectionId,type:"hidden"}),m("INPUT",{id:this.endSelectionId,type:"hidden"})]),t),this.focus()}code(){let t=this.getSelection();return t.collapsed||j(t.commonAncestorContainer)?(this.modifyBlocks(e=>{let n=this._root,o=document.createDocumentFragment(),s=le(e,n),r;for(;r=s.nextNode();){let a=r.querySelectorAll("BR"),d=[],c=a.length;for(let f=0;f<c;f+=1)d[f]=Y(a[f],!1);for(;c--;){let f=a[c];d[c]?y(f,document.createTextNode(`
`)):E(f)}for(a=r.querySelectorAll("CODE"),c=a.length;c--;)y(a[c],S(a[c]));o.childNodes.length&&o.appendChild(document.createTextNode(`
`)),o.appendChild(S(r))}let l=new C(o,4);for(;r=l.nextNode();)r.data=r.data.replace(/ /g," ");return o.normalize(),_(m("PRE",this._config.tagAttributes.pre,[o]))},t),this.focus()):this.changeFormat({tag:"CODE",attributes:this._config.tagAttributes.code},null,t),this}removeCode(){let t=this.getSelection(),e=t.commonAncestorContainer;return g(e,this._root,"PRE")?(this.modifyBlocks(o=>{let s=this._root,r=o.querySelectorAll("PRE"),l=r.length;for(;l--;){let a=r[l],d=new C(a,4),c;for(;c=d.nextNode();){let f=c.data;f=f.replace(/ (?= )/g,"\xA0");let u=document.createDocumentFragment(),p;for(;(p=f.indexOf(`
`))>-1;)u.appendChild(document.createTextNode(f.slice(0,p))),u.appendChild(m("BR")),f=f.slice(p+1);c.parentNode.insertBefore(u,c),c.data=f}B(a,s),y(a,S(a))}return o},t),this.focus()):this.changeFormat(null,{tag:"CODE"},t),this}toggleCode(){return this.hasFormat("PRE")||this.hasFormat("CODE")?this.removeCode():this.code(),this}_removeFormatting(t,e){for(let n=t.firstChild,o;n;n=o){if(o=n.nextSibling,N(n)){if(n instanceof Text||n.nodeName==="BR"||n.nodeName==="IMG"){e.appendChild(n);continue}}else if(H(n)){e.appendChild(this.createDefaultBlock([this._removeFormatting(n,document.createDocumentFragment())]));continue}this._removeFormatting(n,e)}return e}removeAllFormatting(t){if(t||(t=this.getSelection()),t.collapsed)return this;let e=this._root,n=t.commonAncestorContainer;for(;n&&!H(n);)n=n.parentNode;if(n||(ke(t,e),n=e),n instanceof Text)return this;this.saveUndoState(t),P(t,n,n,e);let o=t.startContainer,s=t.startOffset,r=t.endContainer,l=t.endOffset,a=document.createDocumentFragment(),d=document.createDocumentFragment(),c=D(r,l,n,e),f=D(o,s,n,e),u;for(;f!==c;)u=f.nextSibling,a.appendChild(f),f=u;if(this._removeFormatting(a,d),d.normalize(),f=d.firstChild,u=d.lastChild,f){n.insertBefore(d,c);let p=Array.from(n.childNodes);s=p.indexOf(f),l=u?p.indexOf(u)+1:0}else c&&(s=Array.from(n.childNodes).indexOf(c),l=s);return t.setStart(n,s),t.setEnd(n,l),J(n,t),v(t),this.setSelection(t),this._updatePath(t,!0),this.focus()}};window.Squire=Te;})();
//# sourceMappingURL=squire.js.map