0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 23:40:35 -05:00
Squire/build/squire.js

2 lines
42 KiB
JavaScript
Raw Normal View History

!function(e,t){"use strict";function n(e,t,n){this.root=this.currentNode=e,this.nodeType=t,this.filter=n}function r(e,t){for(var n=e.length;n--;)if(!t(e[n]))return!1;return!0}function o(e){return e.nodeType===P&&!!ct[e.nodeName]}function i(e){return lt.test(e.nodeName)}function a(e){var t=e.nodeType;return(t===P||t===F)&&!i(e)&&r(e.childNodes,i)}function s(e){var t=e.nodeType;return!(t!==P&&t!==F||i(e)||a(e))}function d(e,t){var r=new n(t,M,a);return r.currentNode=e,r}function l(e,t){return e=d(e,t).previousNode(),e!==t?e:null}function c(e,t){return e=d(e,t).nextNode(),e!==t?e:null}function f(e,t){return!o(e)&&e.nodeType===t.nodeType&&e.nodeName===t.nodeName&&e.className===t.className&&(!e.style&&!t.style||e.style.cssText===t.style.cssText)}function h(e,t,n){if(e.nodeName!==t)return!1;for(var r in n)if(e.getAttribute(r)!==n[r])return!1;return!0}function u(e,t,n,r){for(;e&&e!==t;){if(h(e,n,r))return e;e=e.parentNode}return null}function p(e,t){for(;t;){if(t===e)return!0;t=t.parentNode}return!1}function g(e,t){var n,r,o,i,a="";return e&&e!==t&&(a=g(e.parentNode,t),e.nodeType===P&&(a+=(a?">":"")+e.nodeName,(n=e.id)&&(a+="#"+n),(r=e.className.trim())&&(o=r.split(/\s\s*/),o.sort(),a+=".",a+=o.join(".")),(i=e.dir)&&(a+="[dir="+i+"]"))),a}function v(e){var t=e.nodeType;return t===P?e.childNodes.length:e.length||0}function m(e){var t=e.parentNode;return t&&t.removeChild(e),e}function C(e,t){var n=e.parentNode;n&&n.replaceChild(t,e)}function N(e){for(var t=e.ownerDocument.createDocumentFragment(),n=e.childNodes,r=n?n.length:0;r--;)t.appendChild(e.firstChild);return t}function S(e,n,r,o){var i,a,s,d,l=e.createElement(n);if(r instanceof Array&&(o=r,r=null),r)for(i in r)a=r[i],a!==t&&l.setAttribute(i,r[i]);if(o)for(s=0,d=o.length;d>s;s+=1)l.appendChild(o[s]);return l}function _(e,t){var n,r,a=e.ownerDocument,s=e;if(e===t&&((r=e.firstChild)&&"BR"!==r.nodeName||(n=k(a).createDefaultBlock(),r?e.replaceChild(n,r):e.appendChild(n),e=n,n=null)),e.nodeType===I)return s;if(i(e)){for(r=e.firstChild;rt&&r&&r.nodeType===I&&!r.data;)e.removeChild(r),r=e.firstChild;r||(rt?(n=a.createTextNode(Z),k(a)._didAddZWS()):n=a.createTextNode(""))}else if(nt){for(;e.nodeType!==I&&!o(e);){if(r=e.firstChild,!r){n=a.createTextNode("");break}e=r}e.nodeType===I?/^ +$/.test(e.data)&&(e.data=""):o(e)&&e.parentNode.insertBefore(a.createTextNode(""),e)}else if(!e.querySelector("BR"))for(n=S(a,"BR");(r=e.lastElementChild)&&!i(r);)e=r;if(n)try{e.appendChild(n)}catch(d){k(a).didError({name:"Squire: fixCursor  "+d,message:"Parent: "+e.nodeName+"/"+e.innerHTML+" appendChild: "+n.nodeName})}return s}function y(e,t){var n,r,o,a,d=e.childNodes,l=e.ownerDocument,c=null,f=k(l)._config;for(n=0,r=d.length;r>n;n+=1)o=d[n],a="BR"===o.nodeName,!a&&i(o)?(c||(c=S(l,f.blockTag,f.blockAttributes)),c.appendChild(o),n-=1,r-=1):(a||c)&&(c||(c=S(l,f.blockTag,f.blockAttributes)),_(c,t),a?e.replaceChild(c,o):(e.insertBefore(c,o),n+=1,r+=1),c=null),s(o)&&y(o,t);return c&&e.appendChild(_(c,t)),e}function T(e,t,n,r){var o,i,a,s=e.nodeType;if(s===I&&e!==n)return T(e.parentNode,e.splitText(t),n,r);if(s===P){if("number"==typeof t&&(t=t<e.childNodes.length?e.childNodes[t]:null),e===n)return t;for(o=e.parentNode,i=e.cloneNode(!1);t;)a=t.nextSibling,i.appendChild(t),t=a;return"OL"===e.nodeName&&u(e,r,"BLOCKQUOTE")&&(i.start=(+e.start||1)+e.childNodes.length-1),_(e,r),_(i,r),(a=e.nextSibling)?o.insertBefore(i,a):o.appendChild(i),T(o,i,n,r)}return t}function E(e,t){if(e.nodeType===P)for(var n,r,o,a=e.childNodes,s=a.length,d=[];s--;)if(n=a[s],r=s&&a[s-1],s&&i(n)&&f(n,r)&&!ct[n.nodeName])t.startContainer===n&&(t.startContainer=r,t.startOffset+=v(r)),t.endContainer===n&&(t.endContainer=r,t.endOffset+=v(r)),t.startContainer===e&&(t.startOffset>s?t.startOffset-=1:t.startOffset===s&&(t.startContainer=r,t.startOffset=v(r))),t.endContainer===e&&(t.endOffset>s?t.endOffset-=1:t.endOffset===s&&(t.endContainer=r,t.endOffset=v(r))),m(n),n.nodeType===I?r.appendData(n.data):d.push(N(n));else if(n.nodeType===P){for(o=d.length;o--;)n.appendChild(d.pop());E(n,t)}}function b(e,t,n){for(var r,o,i,a=t;1===a.parentNo
else for(r=e.firstChild;r;r=o)o=r.nextSibling,p(r,t)}},g=Array.prototype.filter.call(s.getElementsByTagName(e),function(r){return mt(n,r,!0)&&h(r,e,t)});r||g.forEach(function(e){p(e,e)}),u.forEach(function(e){var t=e[0].cloneNode(!1),n=e[1];C(n,t),t.appendChild(n)}),g.forEach(function(e){C(e,N(e))}),this._getRangeAndRemoveBookmark(n),o&&n.collapse(!1);var v={startContainer:n.startContainer,startOffset:n.startOffset,endContainer:n.endContainer,endOffset:n.endOffset};return E(s,v),n.setStart(v.startContainer,v.startOffset),n.setEnd(v.endContainer,v.endOffset),n},jt.changeFormat=function(e,t,n,r){return n||(n=this.getSelection())?(this.saveUndoState(n),t&&(n=this._removeFormat(t.tag.toUpperCase(),t.attributes||{},n,r)),e&&(n=this._addFormat(e.tag.toUpperCase(),e.attributes||{},n)),this.setSelection(n),this._updatePath(n,!0),it||this._docWasChanged(),this):void 0};var en={DT:"DD",DD:"DT",LI:"LI"},tn=function(e,t,n,r){var o=en[t.nodeName],i=null,a=T(n,r,t.parentNode,e._root),s=e._config;return o||(o=s.blockTag,i=s.blockAttributes),h(a,o,i)||(t=S(a.ownerDocument,o,i),a.dir&&(t.dir=a.dir),C(a,t),t.appendChild(N(a)),a=t),a};jt.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var r=this._root,o=St(n,r),i=_t(n,r);if(o&&i)do if(e(o)||o===i)break;while(o=c(o,r));return t&&(this.setSelection(n),this._updatePath(n,!0),it||this._docWasChanged()),this},jt.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._isInUndoState?this._saveRangeToBookmark(t):this._recordUndoState(t);var n,r=this._root;return bt(t,r),Nt(t,r),n=pt(t,r,r),ut(t,e.call(this,n)),t.endOffset<t.endContainer.childNodes.length&&x(t.endContainer.childNodes[t.endOffset],r),x(t.startContainer.childNodes[t.startOffset],r),this._getRangeAndRemoveBookmark(t),this.setSelection(t),this._updatePath(t,!0),it||this._docWasChanged(),this};var nn=function(e){return this.createElement("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e])},rn=function(e){var t=this._root,n=e.querySelectorAll("blockquote");return Array.prototype.filter.call(n,function(e){return!u(e.parentNode,t,"BLOCKQUOTE")}).forEach(function(e){C(e,N(e))}),e},on=function(){return this.createDefaultBlock([this.createElement("INPUT",{id:Xt,type:"hidden"}),this.createElement("INPUT",{id:Jt,type:"hidden"})])},an=function(e,t,n){for(var r,o,i,a,s=d(t,e._root),l=e._config.tagAttributes,c=l[n.toLowerCase()],f=l.li;r=s.nextNode();)o=r.parentNode.nodeName,"LI"!==o?(a=e.createElement("LI",f),r.dir&&(a.dir=r.dir),(i=r.previousSibling)&&i.nodeName===n?i.appendChild(a):C(r,e.createElement(n,c,[a])),a.appendChild(r)):(r=r.parentNode.parentNode,o=r.nodeName,o!==n&&/^[OU]L$/.test(o)&&C(r,e.createElement(n,c,[N(r)])))},sn=function(e){return an(this,e,"UL"),e},dn=function(e){return an(this,e,"OL"),e},ln=function(e){var t,n,r,o,i,a,s,d=e.querySelectorAll("UL, OL");for(t=0,n=d.length;n>t;t+=1){for(o=d[t],i=N(o),a=i.childNodes,r=a.length;r--;)s=a[r],C(s,N(s));y(i,this._root),C(o,i)}return e},cn=function(e){var t,n,r,o,i,a,d=e.querySelectorAll("LI"),l=this._config.tagAttributes,c=l.li;for(t=0,n=d.length;n>t;t+=1)r=d[t],s(r.firstChild)||(o=r.parentNode.nodeName,i=r.previousSibling,i&&(i=i.lastChild)&&i.nodeName===o||(a=l[o.toLowerCase()],C(r,this.createElement("LI",c,[i=this.createElement(o,a)]))),i.appendChild(r));return e},fn=function(e){var t=this._root,n=e.querySelectorAll("LI");return Array.prototype.filter.call(n,function(e){return!s(e.firstChild)}).forEach(function(n){var r,o=n.parentNode,i=o.parentNode,a=n.firstChild,d=a;for(n.previousSibling&&(o=T(o,n,i,t));d&&(r=d.nextSibling,!s(d));)i.insertBefore(d,o),d=r;for("LI"===i.nodeName&&a.previousSibling&&T(i,a,i.parentNode,t);n!==e&&!n.childNodes.length;)o=n.parentNode,o.removeChild(n),n=o},this),y(e,t),e};jt._ensureBottomLine=function(){var e=this._root,t=e.lastElementChild;t&&t.nodeName===this._config.blockTag&&a(t)||e.appendChild(this.createDefaultBlock())},jt.setKeyHandler=function(e,t){return this._keyHandlers[e]=t,this},jt._getHTML=function(){return this._root.innerHTML},jt._setHTML=function(e){var t=this._