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
46 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){switch(e.nodeType){case F:return St;case w:case H:if(pt&&bt.has(e))return bt.get(e);break;default:return Nt}var t;return t=o(e.childNodes,a)?_t.test(e.nodeName)?St:yt:Tt,pt&&bt.set(e,t),t}function a(e){return r(e)===St}function s(e){return r(e)===yt}function d(e){return r(e)===Tt}function l(e,t){var o=new n(t,z,s);return o.currentNode=e,o}function c(e,t){return e=l(e,t).previousNode(),e!==t?e:null}function h(e,t){return e=l(e,t).nextNode(),e!==t?e:null}function u(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 f(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 p(e,t,n,o){for(;e&&e!==t;){if(f(e,n,o))return e;e=e.parentNode}return null}function g(e,t){for(;t;){if(t===e)return!0;t=t.parentNode}return!1}function m(e,t){var n,o,i,r,a="";return e&&e!==t&&(a=m(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&&(mt.call(i,j)>-1&&(a+="[backgroundColor="+e.style.backgroundColor.replace(/ /g,"")+"]"),mt.call(i,Q)>-1&&(a+="[color="+e.style.color.replace(/ /g,"")+"]"),mt.call(i,V)>-1&&(a+="[fontFamily="+e.style.fontFamily.replace(/ /g,"")+"]"),mt.call(i,$)>-1&&(a+="[fontSize="+e.style.fontSize+"]")))),a}function v(e){var t=e.nodeType;return t===w?e.childNodes.length:e.length||0}function _(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 S(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 y(e,t){var n,o,r=t.__squire__,s=e.ownerDocument,d=e;if(e===t&&((o=e.firstChild)&&"BR"!==o.nodeName||(n=r.createDefaultBlock(),o?e.replaceChild(n,o):e.appendChild(n),e=n,n=null)),e.nodeType===F)return d;if(a(e)){for(o=e.firstChild;ht&&o&&o.nodeType===F&&!o.data;)e.removeChild(o),o=e.firstChild;o||(ht?(n=s.createTextNode(Y),r._didAddZWS()):n=s.createTextNode(""))}else if(ct){for(;e.nodeType!==F&&!i(e);){if(o=e.firstChild,!o){n=s.createTextNode("");break}e=o}e.nodeType===F?/^ +$/.test(e.data)&&(e.data=""):i(e)&&e.parentNode.insertBefore(s.createTextNode(""),e)}else if(!e.querySelector("BR"))for(n=S(s,"BR");(o=e.lastElementChild)&&!a(o);)e=o;if(n)try{e.appendChild(n)}catch(l){r.didError({name:"Squire: fixCursor  "+l,message:"Parent: "+e.nodeName+"/"+e.innerHTML+" appendChild: "+n.nodeName})}return d}function T(e,t){var n,o,i,r,s=e.childNodes,l=e.ownerDocument,c=null,h=t.__squire__._config;for(n=0,o=s.length;o>n;n+=1)i=s[n],r="BR"===i.nodeName,!r&&a(i)?(c||(c=S(l,h.blockTag,h.blockAttributes)),c.appendChild(i),n-=1,o-=1):(r||c)&&(c||(c=S(l,h.blockTag,h.blockAttributes)),y(c,t),r?e.replaceChild(c,i):(e.insertBefore(c,i),n+=1,o+=1),c=null),d(i)&&T(i,t);return c&&e.appendChild(y(c,t)),e}function b(e,t,n,o){var i,r,a,s=e.nodeType;if(s===F&&e!==n)return b(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&&p(e,o,"BLOCKQUOTE")&&(r.start=(+e.start||1)+e.childNodes.length-1),y(e,o),y(r,o),(a=e.nextSibling)?i.insertBefore(r,a):i.appendChild(r),b(i,r,n,o)}return t}function E(e,t){for(var n,o,i,r=e.childNodes,s=r.length,d=[];s--;)if(n=r[s],o=s&&r[s-1],s&&a(n)&&u(n,o)&&!Ct[n.nodeName])t.startContainer===n&&(t.startContainer=o,t.startOffset+=v(o)),t.endContainer===n&&(t.endContainer=o,t
2016-12-07 03:08:52 -05:00
}},hn.saveUndoState=function(e){return e===t&&(e=this.getSelection()),this._isInUndoState||(this._recordUndoState(e),this._getRangeAndRemoveBookmark(e)),this},hn.undo=function(){if(0!==this._undoIndex||!this._isInUndoState){this._recordUndoState(this.getSelection()),this._undoIndex-=1,this._setHTML(this._undoStack[this._undoIndex]);var e=this._getRangeAndRemoveBookmark();e&&this.setSelection(e),this._isInUndoState=!0,this.fireEvent("undoStateChange",{canUndo:0!==this._undoIndex,canRedo:!0}),this.fireEvent("input")}return this},hn.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},hn.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(p(s,a,e,t))return!0;if(s.nodeType===F)return!1;i=new n(s,W,function(e){return Ot(o,e,!0)},!1);for(var d=!1;r=i.nextNode();){if(!p(r,a,e,t))return!1;d=!0}return d},hn.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},hn._addFormat=function(e,t,o){var i,r,s,d,l,c,h,u,f,g=this._root;if(o.collapsed){for(i=y(this.createElement(e,t),g),Bt(o,i),o.setStart(i.firstChild,i.firstChild.length),o.collapse(!0),f=i;a(f);)f=f.parentNode;gn(f,i)}else{if(r=new n(o.commonAncestorContainer,W|z,function(e){return(e.nodeType===F||"BR"===e.nodeName||"IMG"===e.nodeName)&&Ot(o,e,!0)},!1),s=o.startContainer,l=o.startOffset,d=o.endContainer,c=o.endOffset,r.currentNode=s,r.filter(s)||(s=r.nextNode(),l=0),!s)return o;do h=r.currentNode,u=!p(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(r.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},hn._removeFormat=function(e,t,n,o){this._saveRangeToBookmark(n);var i,r=this._doc;n.collapsed&&(ht?(i=r.createTextNode(Y),this._didAddZWS()):i=r.createTextNode(""),Bt(n,i));for(var s=n.commonAncestorContainer;a(s);)s=s.parentNode;var d=n.startContainer,l=n.startOffset,c=n.endContainer,h=n.endOffset,u=[],p=function(e,t){if(!Ot(n,e,!1)){var o,i,r=e.nodeType===F;if(!Ot(n,e,!0))return void("INPUT"===e.nodeName||r&&!e.data||u.push([t,e]));if(r)e===c&&h!==e.length&&u.push([t,e.splitText(h)]),e===d&&l&&(e.splitText(l),u.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 Ot(n,o,!0)&&f(o,e,t)});return o||g.forEach(function(e){p(e,e)}),u.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),k(s,n),n},hn.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 _n={DT:"DD",DD:"DT",LI:"LI"},Cn=function(e,t,n,o){var i=_n[t.nodeName],r=null,a=b(n,o,t.parentNode,e._root),s=e._config;return i||(i=s.blockTag,r=s.block