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 r(e){return e.nodeType===I&&!!vt[e.nodeName]}function i(e){return mt.test(e.nodeName)}function a(e){var t=e.nodeType;return(t===I||t===M)&&!i(e)&&o(e.childNodes,i)}function s(e){var t=e.nodeType;return!(t!==I&&t!==M||i(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!r(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,r,i,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())&&(r=o.split(/\s\s*/),r.sort(),a+=".",a+=r.join(".")),(i=e.dir)&&(a+="[dir="+i+"]"),r&&(pt.call(r,G)>-1&&(a+="[backgroundColor="+e.style.backgroundColor.replace(/ /g,"")+"]"),pt.call(r,j)>-1&&(a+="[color="+e.style.color.replace(/ /g,"")+"]"),pt.call(r,Q)>-1&&(a+="[fontFamily="+e.style.fontFamily.replace(/ /g,"")+"]"),pt.call(r,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,r){var i,a,s,d,l=e.createElement(n);if(o instanceof Array&&(r=o,o=null),o)for(i in o)a=o[i],a!==t&&l.setAttribute(i,o[i]);if(r)for(s=0,d=r.length;d>s;s+=1)l.appendChild(r[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=B(a).createDefaultBlock(),o?e.replaceChild(n,o):e.appendChild(n),e=n,n=null)),e.nodeType===w)return s;if(i(e)){for(o=e.firstChild;ct&&o&&o.nodeType===w&&!o.data;)e.removeChild(o),o=e.firstChild;o||(ct?(n=a.createTextNode($),B(a)._didAddZWS()):n=a.createTextNode(""))}else if(lt){for(;e.nodeType!==w&&!r(e);){if(o=e.firstChild,!o){n=a.createTextNode("");break}e=o}e.nodeType===w?/^ +$/.test(e.data)&&(e.data=""):r(e)&&e.parentNode.insertBefore(a.createTextNode(""),e)}else if(!e.querySelector("BR"))for(n=_(a,"BR");(o=e.lastElementChild)&&!i(o);)e=o;if(n)try{e.appendChild(n)}catch(d){B(a).didError({name:"Squire: fixCursor  "+d,message:"Parent: "+e.nodeName+"/"+e.innerHTML+" appendChild: "+n.nodeName})}return s}function y(e,t){var n,o,r,a,d=e.childNodes,l=e.ownerDocument,c=null,h=B(l)._config;for(n=0,o=d.length;o>n;n+=1)r=d[n],a="BR"===r.nodeName,!a&&i(r)?(c||(c=_(l,h.blockTag,h.blockAttributes)),c.appendChild(r),n-=1,o-=1):(a||c)&&(c||(c=_(l,h.blockTag,h.blockAttributes)),S(c,t),a?e.replaceChild(c,r):(e.insertBefore(c,r),n+=1,o+=1),c=null),s(r)&&y(r,t);return c&&e.appendChild(S(c,t)),e}function T(e,t,n,o){var r,i,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(r=e.parentNode,i=e.cloneNode(!1);t;)a=t.nextSibling,i.appendChild(t),t=a;return"OL"===e.nodeName&&f(e,o,"BLOCKQUOTE")&&(i.start=(+e.start||1)+e.childNodes.length-1),S(e,o),S(i,o),(a=e.nextSibling)?r.insertBefore(i,a):r.appendChild(i),T(r,i,n,o)}return t}function E(e,t){for(var n,o,r,a=e.childNodes,s=a.length,d=[];s--;)if(n=a[s],o=s&&a[s-1],s&&i(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 r,i,a=this._root,s=o.commonAncestorContainer;if(f(s,a,e,t))return!0;if(s.nodeType===w)return!1;r=new n(s,W,function(e){return Et(o,e,!0)},!1);for(var d=!1;i=r.nextNode();){if(!f(i,a,e,t))return!1;d=!0}return d},on.getFontInfo=function(e){var n,o,r,i={color:t,backgroundColor:t,family:t,size:t},a=0;if(!e&&!(e=this.getSelection()))return i;if(n=e.commonAncestorContainer,e.collapsed||n.nodeType===w)for(n.nodeType===w&&(n=n.parentNode);4>a&&n;)(o=n.style)&&(!i.color&&(r=o.color)&&(i.color=r,a+=1),!i.backgroundColor&&(r=o.backgroundColor)&&(i.backgroundColor=r,a+=1),!i.family&&(r=o.fontFamily)&&(i.family=r,a+=1),!i.size&&(r=o.fontSize)&&(i.size=r,a+=1)),n=n.parentNode;return i},on._addFormat=function(e,t,o){var r,a,s,d,l,c,h,u,p,g=this._root;if(o.collapsed){for(r=S(this.createElement(e,t),g),_t(o,r),o.setStart(r.firstChild,r.firstChild.length),o.collapse(!0),p=r;i(p);)p=p.parentNode;sn(p,r)}else{if(a=new n(o.commonAncestorContainer,W|H,function(e){return(e.nodeType===w||"BR"===e.nodeName||"IMG"===e.nodeName)&&Et(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),r=this.createElement(e,t),C(h,r),r.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 r,a=this._doc;n.collapsed&&(ct?(r=a.createTextNode($),this._didAddZWS()):r=a.createTextNode(""),_t(n,r));for(var s=n.commonAncestorContainer;i(s);)s=s.parentNode;var d=n.startContainer,l=n.startOffset,c=n.endContainer,h=n.endOffset,f=[],p=function(e,t){if(!Et(n,e,!1)){var o,r,i=e.nodeType===w;if(!Et(n,e,!0))return void("INPUT"===e.nodeName||i&&!e.data||f.push([t,e]));if(i)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=r)r=o.nextSibling,p(o,t)}},g=Array.prototype.filter.call(s.getElementsByTagName(e),function(o){return Et(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),r&&n.collapse(!1),b(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 r=cn[t.nodeName],i=null,a=T(n,o,t.parentNode,e._root),s=e._config;return r||(r=s.blockTag,i=s.blockAttributes),u(a,r,i)||(t=_(a.ownerDocument,r,i),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,r=xt(n,o),i=Bt(n,o);if(r&&i)do if(e(r)||r===i)break;while(r=c(r,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 Rt(t,o),kt(t,o),n=St(t,o,o),_t(t,e.call(this,n)),t.endOffset<t.endContainer.childNodes.length&&x(t.endContainer.childNodes[t.endOffset],o),x(t.startContainer.childNodes[t.startOffset],o),this._getRangeAndRemoveBookmark(t),this.setSelection(t),this._updatePath(t,!0),ut||this._docWasChanged(),