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
44 KiB
JavaScript
Raw Permalink 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===I&&!!vt[e.nodeName]}function r(e){return mt.test(e.nodeName)}function a(e){var t=e.nodeType;return(t===I||t===M)&&!r(e)&&o(e.childNodes,r)}function s(e){var t=e.nodeType;return!(t!==I&&t!==M||r(e)||a(e))}function d(e,t){var o=new n(t,H,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===I&&(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&&(pt.call(i,G)>-1&&(a+="[backgroundColor="+e.style.backgroundColor.replace(/ /g,"")+"]"),pt.call(i,j)>-1&&(a+="[color="+e.style.color.replace(/ /g,"")+"]"),pt.call(i,Q)>-1&&(a+="[fontFamily="+e.style.fontFamily.replace(/ /g,"")+"]"),pt.call(i,V)>-1&&(a+="[fontSize="+e.style.fontSize+"]")))),a}function m(e){var t=e.nodeType;return t===I?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===w)return s;if(r(e)){for(o=e.firstChild;ct&&o&&o.nodeType===w&&!o.data;)e.removeChild(o),o=e.firstChild;o||(ct?(n=a.createTextNode($),L(a)._didAddZWS()):n=a.createTextNode(""))}else if(lt){for(;e.nodeType!==w&&!i(e);){if(o=e.firstChild,!o){n=a.createTextNode("");break}e=o}e.nodeType===w?/^ +$/.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===w&&e!==n)return T(e.parentNode,e.splitText(t),n,o);if(s===I){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 b(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)&&!vt[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===s&&(t.startContainer=o,t.startOffse
}return this},on.hasFormat=function(e,t,o){if(e=e.toUpperCase(),t||(t={}),!o&&!(o=this.getSelection()))return!1;!o.collapsed&&o.startContainer.nodeType===w&&o.startOffset===o.startContainer.length&&o.startContainer.nextSibling&&o.setStartBefore(o.startContainer.nextSibling),!o.collapsed&&o.endContainer.nodeType===w&&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===w)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},on.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===w)for(n.nodeType===w&&(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},on._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),_t(o,i),o.setStart(i.firstChild,i.firstChild.length),o.collapse(!0),p=i;r(p);)p=p.parentNode;sn(p,i)}else{if(a=new n(o.commonAncestorContainer,z|H,function(e){return(e.nodeType===w||"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!==w&&(h.nodeType===w?(d=h,c=h.length):(d=h.parentNode,c=1)),o=this._createRange(s,l,d,c)}return o},on._removeFormat=function(e,t,n,o){this._saveRangeToBookmark(n);var i,a=this._doc;n.collapsed&&(ct?(i=a.createTextNode($),this._didAddZWS()):i=a.createTextNode(""),_t(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===w;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),E(s,n),n},on.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),ut||this._docWasChanged(),this):this};var cn={DT:"DD",DD:"DT",LI:"LI"},hn=function(e,t,n,o){var i=cn[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};on.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var o=this._root,i=kt(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),ut||this._docWasChanged()),this},on.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._isInUndoState?this._saveRangeToBookmark(t):this._recordUndoState(t);var n,o=this._root;return Dt(t,o),xt(t,o),n=St(t,o,o),_t(t,e.call(this,n)),t.endOffset<t.endContainer.childNodes.length&&k(t.endContainer.childNodes[t.endOffset],o),k(t.startContainer.childNodes[t.startOffset],o),this._getRangeAndRemoveBookmark(t),this.setSelection(t),this._updatePath(t,!0),ut||this._docWasChanged(),