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
45 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 o(e,t){for(var n=e.length;n--;)if(!t(e[n]))return!1;return!0}function i(e){return e.nodeType===w&&!!Ct[e.nodeName]}function r(e){return vt.test(e.nodeName)&&(e.nodeType===F||o(e.childNodes,r))}function a(e){var t=e.nodeType;return(t===w||t===H)&&!r(e)&&o(e.childNodes,r)}function s(e){var t=e.nodeType;return!(t!==w&&t!==H||r(e)||a(e))}function d(e,t){var o=new n(t,W,a);return o.currentNode=e,o}function l(e,t){return e=d(e,t).previousNode(),e!==t?e:null}function c(e,t){return e=d(e,t).nextNode(),e!==t?e:null}function h(e,t){return!i(e)&&e.nodeType===t.nodeType&&e.nodeName===t.nodeName&&"A"!==e.nodeName&&e.className===t.className&&(!e.style&&!t.style||e.style.cssText===t.style.cssText)}function u(e,t,n){if(e.nodeName!==t)return!1;for(var o in n)if(e.getAttribute(o)!==n[o])return!1;return!0}function f(e,t,n,o){for(;e&&e!==t;){if(u(e,n,o))return e;e=e.parentNode}return null}function p(e,t){for(;t;){if(t===e)return!0;t=t.parentNode}return!1}function g(e,t){var n,o,i,r,a="";return e&&e!==t&&(a=g(e.parentNode,t),e.nodeType===w&&(a+=(a?">":"")+e.nodeName,(n=e.id)&&(a+="#"+n),(o=e.className.trim())&&(i=o.split(/\s\s*/),i.sort(),a+=".",a+=i.join(".")),(r=e.dir)&&(a+="[dir="+r+"]"),i&&(gt.call(i,j)>-1&&(a+="[backgroundColor="+e.style.backgroundColor.replace(/ /g,"")+"]"),gt.call(i,Q)>-1&&(a+="[color="+e.style.color.replace(/ /g,"")+"]"),gt.call(i,V)>-1&&(a+="[fontFamily="+e.style.fontFamily.replace(/ /g,"")+"]"),gt.call(i,$)>-1&&(a+="[fontSize="+e.style.fontSize+"]")))),a}function m(e){var t=e.nodeType;return t===w?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,o=n?n.length:0;o--;)t.appendChild(e.firstChild);return t}function _(e,n,o,i){var r,a,s,d,l=e.createElement(n);if(o instanceof Array&&(i=o,o=null),o)for(r in o)a=o[r],a!==t&&l.setAttribute(r,o[r]);if(i)for(s=0,d=i.length;d>s;s+=1)l.appendChild(i[s]);return l}function S(e,t){var n,o,a=e.ownerDocument,s=e;if(e===t&&((o=e.firstChild)&&"BR"!==o.nodeName||(n=L(a).createDefaultBlock(),o?e.replaceChild(n,o):e.appendChild(n),e=n,n=null)),e.nodeType===F)return s;if(r(e)){for(o=e.firstChild;ht&&o&&o.nodeType===F&&!o.data;)e.removeChild(o),o=e.firstChild;o||(ht?(n=a.createTextNode(Y),L(a)._didAddZWS()):n=a.createTextNode(""))}else if(ct){for(;e.nodeType!==F&&!i(e);){if(o=e.firstChild,!o){n=a.createTextNode("");break}e=o}e.nodeType===F?/^ +$/.test(e.data)&&(e.data=""):i(e)&&e.parentNode.insertBefore(a.createTextNode(""),e)}else if(!e.querySelector("BR"))for(n=_(a,"BR");(o=e.lastElementChild)&&!r(o);)e=o;if(n)try{e.appendChild(n)}catch(d){L(a).didError({name:"Squire: fixCursor  "+d,message:"Parent: "+e.nodeName+"/"+e.innerHTML+" appendChild: "+n.nodeName})}return s}function y(e,t){var n,o,i,a,d=e.childNodes,l=e.ownerDocument,c=null,h=L(l)._config;for(n=0,o=d.length;o>n;n+=1)i=d[n],a="BR"===i.nodeName,!a&&r(i)?(c||(c=_(l,h.blockTag,h.blockAttributes)),c.appendChild(i),n-=1,o-=1):(a||c)&&(c||(c=_(l,h.blockTag,h.blockAttributes)),S(c,t),a?e.replaceChild(c,i):(e.insertBefore(c,i),n+=1,o+=1),c=null),s(i)&&y(i,t);return c&&e.appendChild(S(c,t)),e}function T(e,t,n,o){var i,r,a,s=e.nodeType;if(s===F&&e!==n)return T(e.parentNode,e.splitText(t),n,o);if(s===w){if("number"==typeof t&&(t=t<e.childNodes.length?e.childNodes[t]:null),e===n)return t;for(i=e.parentNode,r=e.cloneNode(!1);t;)a=t.nextSibling,r.appendChild(t),t=a;return"OL"===e.nodeName&&f(e,o,"BLOCKQUOTE")&&(r.start=(+e.start||1)+e.childNodes.length-1),S(e,o),S(r,o),(a=e.nextSibling)?i.insertBefore(r,a):i.appendChild(r),T(i,r,n,o)}return t}function E(e,t){for(var n,o,i,a=e.childNodes,s=a.length,d=[];s--;)if(n=a[s],o=s&&a[s-1],s&&r(n)&&h(n,o)&&!Ct[n.nodeName])t.startContainer===n&&(t.startContainer=o,t.startOffset+=m(o)),t.endContainer===n&&(t.endContainer=o,t.endOffset+=m(o)),t.startContainer===e&&(t.startOffset>s?t.startOffset-=1:t.startOffset=
}return this},rn.redo=function(){var e=this._undoIndex,t=this._undoStackLength;if(t>e+1&&this._isInUndoState){this._undoIndex+=1,this._setHTML(this._undoStack[this._undoIndex]);var n=this._getRangeAndRemoveBookmark();n&&this.setSelection(n),this.fireEvent("undoStateChange",{canUndo:!0,canRedo:t>e+2}),this.fireEvent("input")}return this},rn.hasFormat=function(e,t,o){if(e=e.toUpperCase(),t||(t={}),!o&&!(o=this.getSelection()))return!1;!o.collapsed&&o.startContainer.nodeType===F&&o.startOffset===o.startContainer.length&&o.startContainer.nextSibling&&o.setStartBefore(o.startContainer.nextSibling),!o.collapsed&&o.endContainer.nodeType===F&&0===o.endOffset&&o.endContainer.previousSibling&&o.setEndAfter(o.endContainer.previousSibling);var i,r,a=this._root,s=o.commonAncestorContainer;if(f(s,a,e,t))return!0;if(s.nodeType===F)return!1;i=new n(s,z,function(e){return bt(o,e,!0)},!1);for(var d=!1;r=i.nextNode();){if(!f(r,a,e,t))return!1;d=!0}return d},rn.getFontInfo=function(e){var n,o,i,r={color:t,backgroundColor:t,family:t,size:t},a=0;if(!e&&!(e=this.getSelection()))return r;if(n=e.commonAncestorContainer,e.collapsed||n.nodeType===F)for(n.nodeType===F&&(n=n.parentNode);4>a&&n;)(o=n.style)&&(!r.color&&(i=o.color)&&(r.color=i,a+=1),!r.backgroundColor&&(i=o.backgroundColor)&&(r.backgroundColor=i,a+=1),!r.family&&(i=o.fontFamily)&&(r.family=i,a+=1),!r.size&&(i=o.fontSize)&&(r.size=i,a+=1)),n=n.parentNode;return r},rn._addFormat=function(e,t,o){var i,a,s,d,l,c,h,u,p,g=this._root;if(o.collapsed){for(i=S(this.createElement(e,t),g),St(o,i),o.setStart(i.firstChild,i.firstChild.length),o.collapse(!0),p=i;r(p);)p=p.parentNode;dn(p,i)}else{if(a=new n(o.commonAncestorContainer,z|W,function(e){return(e.nodeType===F||"BR"===e.nodeName||"IMG"===e.nodeName)&&bt(o,e,!0)},!1),s=o.startContainer,l=o.startOffset,d=o.endContainer,c=o.endOffset,a.currentNode=s,a.filter(s)||(s=a.nextNode(),l=0),!s)return o;do h=a.currentNode,u=!f(h,g,e,t),u&&(h===d&&h.length>c&&h.splitText(c),h===s&&l&&(h=h.splitText(l),d===s&&(d=h,c-=l),s=h,l=0),i=this.createElement(e,t),C(h,i),i.appendChild(h));while(a.nextNode());d.nodeType!==F&&(h.nodeType===F?(d=h,c=h.length):(d=h.parentNode,c=1)),o=this._createRange(s,l,d,c)}return o},rn._removeFormat=function(e,t,n,o){this._saveRangeToBookmark(n);var i,a=this._doc;n.collapsed&&(ht?(i=a.createTextNode(Y),this._didAddZWS()):i=a.createTextNode(""),St(n,i));for(var s=n.commonAncestorContainer;r(s);)s=s.parentNode;var d=n.startContainer,l=n.startOffset,c=n.endContainer,h=n.endOffset,f=[],p=function(e,t){if(!bt(n,e,!1)){var o,i,r=e.nodeType===F;if(!bt(n,e,!0))return void("INPUT"===e.nodeName||r&&!e.data||f.push([t,e]));if(r)e===c&&h!==e.length&&f.push([t,e.splitText(h)]),e===d&&l&&(e.splitText(l),f.push([t,e]));else for(o=e.firstChild;o;o=i)i=o.nextSibling,p(o,t)}},g=Array.prototype.filter.call(s.getElementsByTagName(e),function(o){return bt(n,o,!0)&&u(o,e,t)});return o||g.forEach(function(e){p(e,e)}),f.forEach(function(e){var t=e[0].cloneNode(!1),n=e[1];C(n,t),t.appendChild(n)}),g.forEach(function(e){C(e,N(e))}),this._getRangeAndRemoveBookmark(n),i&&n.collapse(!1),b(s,n),n},rn.changeFormat=function(e,t,n,o){return n||(n=this.getSelection())?(this.saveUndoState(n),t&&(n=this._removeFormat(t.tag.toUpperCase(),t.attributes||{},n,o)),e&&(n=this._addFormat(e.tag.toUpperCase(),e.attributes||{},n)),this.setSelection(n),this._updatePath(n,!0),ft||this._docWasChanged(),this):this};var hn={DT:"DD",DD:"DT",LI:"LI"},un=function(e,t,n,o){var i=hn[t.nodeName],r=null,a=T(n,o,t.parentNode,e._root),s=e._config;return i||(i=s.blockTag,r=s.blockAttributes),u(a,i,r)||(t=_(a.ownerDocument,i,r),a.dir&&(t.dir=a.dir),C(a,t),t.appendChild(N(a)),a=t),a};rn.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var o=this._root,i=Bt(n,o),r=Lt(n,o);if(i&&r)do if(e(i)||i===r)break;while(i=c(i,o));return t&&(this.setSelection(n),this._updatePath(n,!0),ft||this._docWasChanged()),this},rn.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._isInUndoState?this._saveRangeToBookmark(t):this._recordUndoSt