0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-18 12:42:37 -05:00
Squire/build/squire.js

2 lines
41 KiB
JavaScript
Raw Permalink Normal View History

2016-03-14 15:22:40 +11:00
!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,t,n){if(e.nodeName!==t)return!1;for(var r in n)if(e.getAttribute(r)!==n[r])return!1;return!0}function o(e,t){return!a(e)&&e.nodeType===t.nodeType&&e.nodeName===t.nodeName&&e.className===t.className&&(!e.style&&!t.style||e.style.cssText===t.style.cssText)}function a(e){return e.nodeType===A&&!!ot[e.nodeName]}function s(e){return it.test(e.nodeName)}function d(e){var t=e.nodeType;return(t===A||t===R)&&!s(e)&&r(e.childNodes,s)}function l(e){var t=e.nodeType;return!(t!==A&&t!==R||s(e)||d(e))}function c(e){var t=e.ownerDocument,r=new n(t.body,I,d,!1);return r.currentNode=e,r}function f(e){return c(e).previousNode()}function h(e){return c(e).nextNode()}function u(e,t,n){do if(i(e,t,n))return e;while(e=e.parentNode);return null}function p(e){var t,n,r,i,o,a=e.parentNode;return a&&e.nodeType===A?(t=p(a),t+=(t?">":"")+e.nodeName,(n=e.id)&&(t+="#"+n),(r=e.className.trim())&&(i=r.split(/\s\s*/),i.sort(),t+=".",t+=i.join(".")),(o=e.dir)&&(t+="[dir="+o+"]")):t=a?p(a):"",t}function g(e){var t=e.nodeType;return t===A?e.childNodes.length:e.length||0}function m(e){var t=e.parentNode;return t&&t.removeChild(e),e}function v(e,t){var n=e.parentNode;n&&n.replaceChild(t,e)}function C(e){for(var t=e.ownerDocument.createDocumentFragment(),n=e.childNodes,r=n?n.length:0;r--;)t.appendChild(e.firstChild);return t}function N(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){var t,n,r=e.ownerDocument,i=e;if("BODY"===e.nodeName&&((n=e.firstChild)&&"BR"!==n.nodeName||(t=B(r).createDefaultBlock(),n?e.replaceChild(t,n):e.appendChild(t),e=t,t=null)),s(e)){for(n=e.firstChild;X&&n&&n.nodeType===D&&!n.data;)e.removeChild(n),n=e.firstChild;n||(X?(t=r.createTextNode(H),B(r)._didAddZWS()):t=r.createTextNode(""))}else if($){for(;e.nodeType!==D&&!a(e);){if(n=e.firstChild,!n){t=r.createTextNode("");break}e=n}e.nodeType===D?/^ +$/.test(e.data)&&(e.data=""):a(e)&&e.parentNode.insertBefore(r.createTextNode(""),e)}else if(!e.querySelector("BR"))for(t=N(r,"BR");(n=e.lastElementChild)&&!s(n);)e=n;return t&&e.appendChild(t),i}function y(e){var t,n,r,i,o=e.childNodes,a=e.ownerDocument,d=null,c=B(a)._config;for(t=0,n=o.length;n>t;t+=1)r=o[t],i="BR"===r.nodeName,!i&&s(r)?(d||(d=N(a,c.blockTag,c.blockAttributes)),d.appendChild(r),t-=1,n-=1):(i||d)&&(d||(d=N(a,c.blockTag,c.blockAttributes)),S(d),i?e.replaceChild(d,r):(e.insertBefore(d,r),t+=1,n+=1),d=null),l(r)&&y(r);return d&&e.appendChild(S(d)),e}function _(e,t,n){var r,i,o,a=e.nodeType;if(a===D&&e!==n)return _(e.parentNode,e.splitText(t),n);if(a===A){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;)o=t.nextSibling,i.appendChild(t),t=o;return"OL"===e.nodeName&&u(e,"BLOCKQUOTE")&&(i.start=(+e.start||1)+e.childNodes.length-1),S(e),S(i),(o=e.nextSibling)?r.insertBefore(i,o):r.appendChild(i),_(r,i,n)}return t}function T(e,t){if(e.nodeType===A)for(var n,r,i,a=e.childNodes,d=a.length,l=[];d--;)if(n=a[d],r=d&&a[d-1],d&&s(n)&&o(n,r)&&!ot[n.nodeName])t.startContainer===n&&(t.startContainer=r,t.startOffset+=g(r)),t.endContainer===n&&(t.endContainer=r,t.endOffset+=g(r)),t.startContainer===e&&(t.startOffset>d?t.startOffset-=1:t.startOffset===d&&(t.startContainer=r,t.startOffset=g(r))),t.endContainer===e&&(t.endOffset>d?t.endOffset-=1:t.endOffset===d&&(t.endContainer=r,t.endOffset=g(r))),m(n),n.nodeType===D?r.appendData(n.data):l.push(C(n));else if(n.nodeType===A){for(i=l.length;i--;)n.appendChild(l.pop());T(n,t)}}function b(e,t,n){for(var r,i,o,a=t;1===a.parentNode.childNodes.length;)a=a.parentNode;m(a),i=e.childNodes.length,r=e.lastChild,r&&"BR"===r.nodeName&&(e.removeChild(r),i-=1),o={startContainer:e,startOffset:i,endContainer:e,endOffset:i},e.appendChild(C(t)),T(e,o),n.setStart(o.startContainer,o.startOffset),n.collapse(
return o||(o=d.blockTag,a=d.blockAttributes),i(s,o,a)||(t=N(s.ownerDocument,o,a),s.dir&&(t.dir=s.dir),v(s,t),t.appendChild(C(s)),s=t),s};Kt.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var r=gt(n),i=mt(n);if(r&&i)do if(e(r)||r===i)break;while(r=h(r));return t&&(this.setSelection(n),this._updatePath(n,!0),et||this._docWasChanged()),this},Kt.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._isInUndoState?this._saveRangeToBookmark(t):this._recordUndoState(t),St(t);var n,r=this._body;return pt(t,r),n=lt(t,r),dt(t,e.call(this,n)),t.endOffset<t.endContainer.childNodes.length&&E(t.endContainer.childNodes[t.endOffset]),E(t.startContainer.childNodes[t.startOffset]),this._getRangeAndRemoveBookmark(t),this.setSelection(t),this._updatePath(t,!0),et||this._docWasChanged(),this};var $t=function(e){return this.createElement("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e])},Xt=function(e){var t=e.querySelectorAll("blockquote");return Array.prototype.filter.call(t,function(e){return!u(e.parentNode,"BLOCKQUOTE")}).forEach(function(e){v(e,C(e))}),e},Jt=function(){return this.createDefaultBlock([this.createElement("INPUT",{id:Gt,type:"hidden"}),this.createElement("INPUT",{id:jt,type:"hidden"})])},en=function(e,t,n){for(var r,i,o,a,s=c(t),d=e._config.tagAttributes,l=d[n.toLowerCase()],f=d.li;r=s.nextNode();)i=r.parentNode.nodeName,"LI"!==i?(a=e.createElement("LI",f),r.dir&&(a.dir=r.dir),(o=r.previousSibling)&&o.nodeName===n?o.appendChild(a):v(r,e.createElement(n,l,[a])),a.appendChild(r)):(r=r.parentNode.parentNode,i=r.nodeName,i!==n&&/^[OU]L$/.test(i)&&v(r,e.createElement(n,l,[C(r)])))},tn=function(e){return en(this,e,"UL"),e},nn=function(e){return en(this,e,"OL"),e},rn=function(e){var t,n,r,i,o,a,s,d=e.querySelectorAll("UL, OL");for(t=0,n=d.length;n>t;t+=1){for(i=d[t],o=C(i),a=o.childNodes,r=a.length;r--;)s=a[r],v(s,C(s));y(o),v(i,o)}return e},on=function(e){var t,n,r,i,o,a,s=e.querySelectorAll("LI"),d=this._config.tagAttributes,c=d.li;for(t=0,n=s.length;n>t;t+=1)r=s[t],l(r.firstChild)||(i=r.parentNode.nodeName,o=r.previousSibling,o&&(o=o.lastChild)&&o.nodeName===i||(a=d[i.toLowerCase()],v(r,this.createElement("LI",c,[o=this.createElement(i,a)]))),o.appendChild(r));return e},an=function(e){var t=e.querySelectorAll("LI");return Array.prototype.filter.call(t,function(e){return!l(e.firstChild)}).forEach(function(t){var n,r=t.parentNode,i=r.parentNode,o=t.firstChild,a=o;for(t.previousSibling&&(r=_(r,t,i));a&&(n=a.nextSibling,!l(a));)i.insertBefore(a,r),a=n;for("LI"===i.nodeName&&o.previousSibling&&_(i,o,i.parentNode);t!==e&&!t.childNodes.length;)r=t.parentNode,r.removeChild(t),t=r},this),y(e),e};Kt._ensureBottomLine=function(){var e=this._body,t=e.lastElementChild;t&&t.nodeName===this._config.blockTag&&d(t)||e.appendChild(this.createDefaultBlock())},Kt.setKeyHandler=function(e,t){return this._keyHandlers[e]=t,this},Kt._getHTML=function(){return this._body.innerHTML},Kt._setHTML=function(e){var t=this._body;t.innerHTML=e;do S(t);while(t=h(t));this._ignoreChange=!0},Kt.getHTML=function(e){var t,n,r,i,o,a=[];if(e&&(o=this.getSelection())&&this._saveRangeToBookmark(o),$)for(t=this._body;t=h(t);)t.textContent||t.querySelector("BR")||(n=this.createElement("BR"),t.appendChild(n),a.push(n));if(r=this._getHTML().replace(/\u200B/g,""),$)for(i=a.length;i--;)m(a[i]);return o&&this._getRangeAndRemoveBookmark(o),r},Kt.setHTML=function(e){var t,n=this._doc.createDocumentFragment(),r=this.createElement("DIV");r.innerHTML=e,n.appendChild(C(r)),It(n),Ft(n),y(n);for(var i=n;i=h(i);)S(i);this._ignoreChange=!0;for(var o=this._body;t=o.lastChild;)o.removeChild(t);o.appendChild(n),S(o),this._undoIndex=-1,this._undoStack.length=0,this._undoStackLength=0,this._isInUndoState=!1;var a=this._getRangeAndRemoveBookmark()||this._createRange(o.firstChild,0);return this.saveUndoState(a),J?this._lastSelection=a:this.setSelection(a),this._updatePath(a,!0),this},Kt.insertElement=function(e,t){if(t||(t=this.getSelection()),t.collapse(!0),s(e))dt(t,e),t.setStartAfter(e);else{for(var n,r,i=this._