0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-06 23:00:08 -05:00
Squire/build/squire.js

2 lines
43 KiB
JavaScript
Raw Normal View History

2015-11-25 09:58:51 -05: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 o(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 i(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&&!!ie[e.nodeName]}function s(e){return oe.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,P,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(o(e,t,n))return e;while(e=e.parentNode);return null}function p(e){var t,n,r,o,i,a=e.parentNode;return a&&e.nodeType===A?(t=p(a),t+=(t?">":"")+e.nodeName,(n=e.id)&&(t+="#"+n),(r=e.className.trim())&&(o=r.split(/\s\s*/),o.sort(),t+=".",t+=o.join(".")),(i=e.dir)&&(t+="[dir="+i+"]")):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,o){var i,a,s,d,l=e.createElement(n);if(r instanceof Array&&(o=r,r=null),r)for(i in r)a=r[i],a!==t&&l.setAttribute(i,r[i]);if(o)for(s=0,d=o.length;d>s;s+=1)l.appendChild(o[s]);return l}function S(e){var t,n,r=e.ownerDocument,o=e;if("BODY"===e.nodeName&&((n=e.firstChild)&&"BR"!==n.nodeName||(t=x(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),x(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),o}function y(e){var t,n,r,o,i=e.childNodes,a=e.ownerDocument,d=null,c=x(a)._config;for(t=0,n=i.length;n>t;t+=1)r=i[t],o="BR"===r.nodeName,!o&&s(r)?(d||(d=N(a,c.blockTag,c.blockAttributes)),d.appendChild(r),t-=1,n-=1):(o||d)&&(d||(d=N(a,c.blockTag,c.blockAttributes)),S(d),o?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,o,i,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,o=e.cloneNode(!1);t;)i=t.nextSibling,o.appendChild(t),t=i;return"OL"===e.nodeName&&u(e,"BLOCKQUOTE")&&(o.start=(+e.start||1)+e.childNodes.length-1),S(e),S(o),(i=e.nextSibling)?r.insertBefore(o,i):r.appendChild(o),_(r,o,n)}return t}function T(e,t){if(e.nodeType===A)for(var n,r,o,a=e.childNodes,d=a.length,l=[];d--;)if(n=a[d],r=d&&a[d-1],d&&s(n)&&i(n,r)&&!ie[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(o=l.length;o--;)n.appendChild(l.pop());T(n,t)}}function b(e,t,n){for(var r,o,i,a=t;1===a.parentNode.childNodes.length;)a=a.parentNode;m(a),o=e.childNodes.length,r=e.lastChild,r&&"BR"===r.nodeName&&(e.removeChild(r),o-=1),i={startContainer:e,startOffset:o,endContainer:e,endOffset:o},e.appendChild(C(t)),T(e,i),n.setStart(i.startContainer,i.startOffset),n.collapse(
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),ee||this._docWasChanged(),this):void 0};var Qe={DT:"DD",DD:"DT",LI:"LI"},Ye=function(e,t,n,r){var i=Qe[t.nodeName],a=null,s=_(n,r,t.parentNode),d=e._config;return i||(i=d.blockTag,a=d.blockAttributes),o(s,i,a)||(t=N(s.ownerDocument,i,a),s.dir&&(t.dir=s.dir),v(s,t),t.appendChild(C(s)),s=t),s};qe.forEachBlock=function(e,t,n){if(!n&&!(n=this.getSelection()))return this;t&&this.saveUndoState(n);var r=ge(n),o=me(n);if(r&&o)do if(e(r)||r===o)break;while(r=h(r));return t&&(this.setSelection(n),this._updatePath(n,!0),ee||this._docWasChanged()),this},qe.modifyBlocks=function(e,t){if(!t&&!(t=this.getSelection()))return this;this._isInUndoState?this._saveRangeToBookmark(t):this._recordUndoState(t),Se(t);var n,r=this._body;return pe(t,r),n=le(t,r),de(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),ee||this._docWasChanged(),this};var $e=function(e){return this.createElement("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e])},Xe=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},Je=function(){return this.createDefaultBlock([this.createElement("INPUT",{id:Ge,type:"hidden"}),this.createElement("INPUT",{id:je,type:"hidden"})])},et=function(e,t,n){for(var r,o,i,a,s=c(t),d=e._config.tagAttributes,l=d[n.toLowerCase()],f=d.li;r=s.nextNode();)o=r.parentNode.nodeName,"LI"!==o?(a=e.createElement("LI",f),r.dir&&(a.dir=r.dir),(i=r.previousSibling)&&i.nodeName===n?i.appendChild(a):v(r,e.createElement(n,l,[a])),a.appendChild(r)):(r=r.parentNode.parentNode,o=r.nodeName,o!==n&&/^[OU]L$/.test(o)&&v(r,e.createElement(n,l,[C(r)])))},tt=function(e){return et(this,e,"UL"),e},nt=function(e){return et(this,e,"OL"),e},rt=function(e){var t,n,r,o,i,a,s,d=e.querySelectorAll("UL, OL");for(t=0,n=d.length;n>t;t+=1){for(o=d[t],i=C(o),a=i.childNodes,r=a.length;r--;)s=a[r],v(s,C(s));y(i),v(o,i)}return e},ot=function(e){var t,n,r,o,i,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)||(o=r.parentNode.nodeName,i=r.previousSibling,i&&(i=i.lastChild)&&i.nodeName===o||(a=d[o.toLowerCase()],v(r,this.createElement("LI",c,[i=this.createElement(o,a)]))),i.appendChild(r));return e},it=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,o=r.parentNode,i=t.firstChild,a=i;for(t.previousSibling&&(r=_(r,t,o));a&&(n=a.nextSibling,!l(a));)o.insertBefore(a,r),a=n;for("LI"===o.nodeName&&i.previousSibling&&_(o,i,o.parentNode);t!==e&&!t.childNodes.length;)r=t.parentNode,r.removeChild(t),t=r},this),y(e),e},at=function(e,t){for(var n,r=c(t),o=[];n=r.nextNode();)o.push(n.textContent);return e._doc.createTextNode(o.join("\n")||"")},st=function(e){return this.createElement("PRE",this._config.tagAttributes.pre,[this.createElement("INPUT",{id:Ge,type:"hidden"}),at(this,e),this.createElement("INPUT",{id:je,type:"hidden"}),this._doc.createTextNode("\n")])},dt=function(e){var t=this._doc.createRange(),n=e.querySelector("#"+Ge),r=e.querySelector("#"+je);if(!n||!r)return e;t.setStartBefore(n),t.setEndAfter(r);var o=e.querySelectorAll("pre");if(0===o.length)return e;var i=o[0],a=o[o.length-1],s=t.startContainer,d=t.startOffset,l=t.endContainer,c=t.endOffset,f=[];if(n.nextSibling===r){var h,p=n.previousSibling;if(p&&p.nodeType===D){var g=p.nodeValue.lastIndexOf("\n");-1!==g?(h=p.splitText(g),p.parentNode.insertBefore(n,h)):p.parentNode.insertBefore(n,p),t.setStartBefore(n),d=t.startOffset,s=t.startContainer}var m=r.nextSibling;if(m&&m.nodeType===D){var C=m.nodeValue.indexOf("\n");-1!==C?(h=m.splitText(C+1),m.parentNode.insertBefore(r,h)):m.nextSibling?m.par