0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-23 07:43:52 -05:00
Squire/build/squire.js

2 lines
44 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 i(e){return e.nodeType===P&&!!ct[e.nodeName]}function o(e){return lt.test(e.nodeName)}function a(e){var t=e.nodeType;return(t===P||t===F)&&!o(e)&&r(e.childNodes,o)}function s(e){var t=e.nodeType;return!(t!==P&&t!==F||o(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 h(e,t){return!i(e)&&e.nodeType===t.nodeType&&e.nodeName===t.nodeName&&"A"!==e.nodeName&&e.className===t.className&&(!e.style&&!t.style||e.style.cssText===t.style.cssText)}function f(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(f(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,i,o,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())&&(i=r.split(/\s\s*/),i.sort(),a+=".",a+=i.join(".")),(o=e.dir)&&(a+="[dir="+o+"]"))),a}function m(e){var t=e.nodeType;return t===P?e.childNodes.length:e.length||0}function v(e){var t=e.parentNode;return t&&t.removeChild(e),e}function C(e,t){var n=e.parentNode;n&&n.replaceChild(t,e)}function N(e){for(var t=e.ownerDocument.createDocumentFragment(),n=e.childNodes,r=n?n.length:0;r--;)t.appendChild(e.firstChild);return t}function _(e,n,r,i){var o,a,s,d,l=e.createElement(n);if(r instanceof Array&&(i=r,r=null),r)for(o in r)a=r[o],a!==t&&l.setAttribute(o,r[o]);if(i)for(s=0,d=i.length;d>s;s+=1)l.appendChild(i[s]);return l}function S(e,t){var n,r,a=e.ownerDocument,s=e;if(e===t&&((r=e.firstChild)&&"BR"!==r.nodeName||(n=O(a).createDefaultBlock(),r?e.replaceChild(n,r):e.appendChild(n),e=n,n=null)),e.nodeType===I)return s;if(o(e)){for(r=e.firstChild;rt&&r&&r.nodeType===I&&!r.data;)e.removeChild(r),r=e.firstChild;r||(rt?(n=a.createTextNode(Z),O(a)._didAddZWS()):n=a.createTextNode(""))}else if(nt){for(;e.nodeType!==I&&!i(e);){if(r=e.firstChild,!r){n=a.createTextNode("");break}e=r}e.nodeType===I?/^ +$/.test(e.data)&&(e.data=""):i(e)&&e.parentNode.insertBefore(a.createTextNode(""),e)}else if(!e.querySelector("BR"))for(n=_(a,"BR");(r=e.lastElementChild)&&!o(r);)e=r;if(n)try{e.appendChild(n)}catch(d){O(a).didError({name:"Squire: fixCursor  "+d,message:"Parent: "+e.nodeName+"/"+e.innerHTML+" appendChild: "+n.nodeName})}return s}function y(e,t){var n,r,i,a,d=e.childNodes,l=e.ownerDocument,c=null,h=O(l)._config;for(n=0,r=d.length;r>n;n+=1)i=d[n],a="BR"===i.nodeName,!a&&o(i)?(c||(c=_(l,h.blockTag,h.blockAttributes)),c.appendChild(i),n-=1,r-=1):(a||c)&&(c||(c=_(l,h.blockTag,h.blockAttributes)),S(c,t),a?e.replaceChild(c,i):(e.insertBefore(c,i),n+=1,r+=1),c=null),s(i)&&y(i,t);return c&&e.appendChild(S(c,t)),e}function T(e,t,n,r){var i,o,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(i=e.parentNode,o=e.cloneNode(!1);t;)a=t.nextSibling,o.appendChild(t),t=a;return"OL"===e.nodeName&&u(e,r,"BLOCKQUOTE")&&(o.start=(+e.start||1)+e.childNodes.length-1),S(e,r),S(o,r),(a=e.nextSibling)?i.insertBefore(o,a):i.appendChild(o),T(i,o,n,r)}return t}function E(e,t){if(e.nodeType===P)for(var n,r,i,a=e.childNodes,s=a.length,d=[];s--;)if(n=a[s],r=s&&a[s-1],s&&o(n)&&h(n,r)&&!ct[n.nodeName])t.startContainer===n&&(t.startContainer=r,t.startOffset+=m(r)),t.endContainer===n&&(t.endContainer=r,t.endOffset+=m(r)),t.startContainer===e&&(t.startOffset>s?t.startOffset-=1:t.startOffset===s&&(t.startContainer=r,t.startOffset=m(r))),t.endContainer===e&&(t.endOffset>s?t.endOffset-=1:t.endOffset===s&&(t.endContainer=r,t.endOffset=m(r))),v(n),n.nodeType===I?r.appendData(n.data):d.push(N(n));else if(n.nodeType===P){for(i=d.length;i--;)n.appendChild(d.pop());E(n,t)}}function b(e,t,n){for(var r,i,o,
return o},$t._addFormat=function(e,t,r){var i,o,a,s,d,l,c,h,f=this._root;if(r.collapsed)i=S(this.createElement(e,t),f),ut(r,i),r.setStart(i.firstChild,i.firstChild.length),r.collapse(!0);else{if(o=new n(r.commonAncestorContainer,H|M,function(e){return(e.nodeType===I||"BR"===e.nodeName||"IMG"===e.nodeName)&&vt(r,e,!0)},!1),a=r.startContainer,d=r.startOffset,s=r.endContainer,l=r.endOffset,o.currentNode=a,o.filter(a)||(a=o.nextNode(),d=0),!a)return r;do c=o.currentNode,h=!u(c,f,e,t),h&&(c===s&&c.length>l&&c.splitText(l),c===a&&d&&(c=c.splitText(d),s===a&&(s=c,l-=d),a=c,d=0),i=this.createElement(e,t),C(c,i),i.appendChild(c));while(o.nextNode());s.nodeType!==I&&(c.nodeType===I?(s=c,l=c.length):(s=c.parentNode,l=1)),r=this._createRange(a,d,s,l)}return r},$t._removeFormat=function(e,t,n,r){this._saveRangeToBookmark(n);var i,a=this._doc;n.collapsed&&(rt?(i=a.createTextNode(Z),this._didAddZWS()):i=a.createTextNode(""),ut(n,i));for(var s=n.commonAncestorContainer;o(s);)s=s.parentNode;var d=n.startContainer,l=n.startOffset,c=n.endContainer,h=n.endOffset,u=[],p=function(e,t){if(!vt(n,e,!1)){var r,i,o=e.nodeType===I;if(!vt(n,e,!0))return void("INPUT"===e.nodeName||o&&!e.data||u.push([t,e]));if(o)e===c&&h!==e.length&&u.push([t,e.splitText(h)]),e===d&&l&&(e.splitText(l),u.push([t,e]));else for(r=e.firstChild;r;r=i)i=r.nextSibling,p(r,t)}},g=Array.prototype.filter.call(s.getElementsByTagName(e),function(r){return vt(n,r,!0)&&f(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),i&&n.collapse(!1);var m={startContainer:n.startContainer,startOffset:n.startOffset,endContainer:n.endContainer,endOffset:n.endOffset};return E(s,m),n.setStart(m.startContainer,m.startOffset),n.setEnd(m.endContainer,m.endOffset),n},$t.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),ot||this._docWasChanged(),this):this};var nn={DT:"DD",DD:"DT",LI:"LI"},rn=function(e,t,n,r){var i=nn[t.nodeName],o=null,a=T(n,r,t.parentNode,e._root),s=e._config;return i||(i=s.blockTag,o=s.blockAttributes),f(a,i,o)||(t=_(a.ownerDocument,i,o),a.dir&&(t.dir=a.dir),C(a,t),t.appendChild(N(a)),a=t),a};$t.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var r=this._root,i=_t(n,r),o=St(n,r);if(i&&o)do if(e(i)||i===o)break;while(i=c(i,r));return t&&(this.setSelection(n),this._updatePath(n,!0),ot||this._docWasChanged()),this},$t.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),ot||this._docWasChanged(),this};var on=function(e){return this.createElement("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e])},an=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},sn=function(){return this.createDefaultBlock([this.createElement("INPUT",{id:en,type:"hidden"}),this.createElement("INPUT",{id:tn,type:"hidden"})])},dn=function(e,t,n){for(var r,i,o,a,s=d(t,e._root),l=e._config.tagAttributes,c=l[n.toLowerCase()],h=l.li;r=s.nextNode();)i=r.parentNode.nodeName,"LI"!==i?(a=e.createElement("LI",h),r.dir&&(a.dir=r.dir),(o=r.previousSibling)&&o.nodeName===n?o.appendChild(a):C(r,e.createElement(n,c,[a])),a.appendChild(r)):(r=r.parentNode.parentNode,i=r.nodeName,i!==n&&/^[OU]L$/.test(i)&&C(r,e.createElement(n,c,[N(r)])))},ln=function(e){return dn(this,e,"UL"),e},cn=function(e){return dn(this,e,"OL"),