mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 23:40:35 -05:00
1 line
No EOL
24 KiB
JavaScript
1 line
No EOL
24 KiB
JavaScript
/* Copyright © 2011 by Neil Jenkins. Licensed under the MIT license. */(function(a){"use strict";var b=!a.createTreeWalker;window.ie===9&&(b=!0),b||function(){var c=a.createElement("div"),d=a.createTextNode("");c.appendChild(d);var e=c.cloneNode(!0),f=c.cloneNode(!0),g=c.cloneNode(!0),h=a.createTreeWalker(c,1,function(a){return 1},!1);c.appendChild(e),c.appendChild(f),c.appendChild(g),h.currentNode=g,h.previousNode()!==f&&(b=!0)}();if(!b)return;var c={1:1,2:2,3:4,8:128,9:256,11:1024},d=1,e=function(a,b,c){this.root=this.currentNode=a,this.nodeType=b,this.filter=c};e.prototype.nextNode=function(){var a=this.currentNode,b=this.root,e=this.nodeType,f=this.filter,g;for(;;){g=a.firstChild;while(!g&&a){if(a===b)break;g=a.nextSibling,g||(a=a.parentNode)}if(!g)return null;if(c[g.nodeType]&e&&f(g)===d)return this.currentNode=g,g;a=g}},e.prototype.previousNode=function(){var a=this.currentNode,b=this.root,e=this.nodeType,f=this.filter,g;for(;;){if(a===b)return null;g=a.previousSibling;if(g)while(a=g.lastChild)g=a;else g=a.parentNode;if(!g)return null;if(c[g.nodeType]&e&&f(g)===d)return this.currentNode=g,g;a=g}},a.createTreeWalker=function(a,b,c){return new e(a,b,c)}})(document),function(){"use strict";var a=function(a,b){var c=a.prototype,d;for(d in b)c[d]=b[d]},b=function(a,b){var c=a.length;while(c--)if(!b(a[c]))return!1;return!0},c=function(){return!1},d=function(){return!0},e=/^(?:A(?:BBR|CRONYM)?|B(?:R|D[IO])?|C(?:ITE|ODE)|D(?:FN|EL)|EM|HR|I(?:NPUT|MG|NS)?|KBD|Q|R(?:P|T|UBY)|S(?:U[BP]|PAN|TRONG|AMP)|U)$/,f={BR:1,IMG:1,INPUT:1},g=function(a,b){var c=b.parentNode;return c&&c.replaceChild(a,b),a},h=1,i=3,j=1,k=1,l=3,m=function(a){return a.isBlock()?k:l},n=!!window.opera||!!window.ie;a(Node,{isInline:c,isBlock:c,isContainer:c,getPath:function(){var a=this.parentNode;return a?a.getPath():""},detach:function(){var a=this.parentNode;return a&&a.removeChild(this),this},replaceWith:function(a){return g(a,this),this},replaces:function(a){return g(this,a),this},nearest:function(a,b){var c=this.parentNode;return c?c.nearest(a,b):null},getPreviousBlock:function(){var a=this.ownerDocument,b=a.createTreeWalker(a.body,j,m,!1);return b.currentNode=this,b.previousNode()},getNextBlock:function(){var a=this.ownerDocument,b=a.createTreeWalker(a.body,j,m,!1);return b.currentNode=this,b.nextNode()},split:function(a,b){return a},mergeContainers:function(){}}),a(Text,{isInline:d,isLeaf:d,getLength:function(){return this.length},isLike:function(a){return a.nodeType===i},split:function(a,b){var c=this;return c===b?a:c.parentNode.split(c.splitText(a),b)}}),a(Element,{isLeaf:function(){return!!f[this.nodeName]},isInline:function(){return e.test(this.nodeName)},isBlock:function(){return!this.isInline()&&b(this.childNodes,function(a){return a.isInline()})},isContainer:function(){return!this.isInline()&&!this.isBlock()},getLength:function(){return this.childNodes.length},getPath:function(){var a=this.nodeName;if(a==="BODY")return a;var b=this.parentNode.getPath(),c=this.id,d=this.className.trim();return b+=">"+a,c&&(b+="#"+c),d&&(d=d.split(/\s\s*/),d.sort(),b+=".",b+=d.join(".")),b},wraps:function(a){return g(this,a).appendChild(a),this},empty:function(){var a=this.ownerDocument.createDocumentFragment(),b=this.childNodes.length;while(b--)a.appendChild(this.firstChild);return a},is:function(a,b){if(this.nodeName!==a)return!1;var c;for(c in b)if(this.getAttribute(c)!==b[c])return!1;return!0},nearest:function(a,b){var c=this;do if(c.is(a,b))return c;while((c=c.parentNode)&&c.nodeType===h);return null},isLike:function(a){return a.nodeType===h&&a.nodeName===this.nodeName&&a.className===this.className&&a.style.cssText===this.style.cssText},mergeInlines:function(a){var b=this.childNodes,c=b.length,d=[],e,g,j;while(c--){e=b[c],g=c&&b[c-1];if(c&&e.isInline()&&e.isLike(g)&&!f[e.nodeName])a.startContainer===e&&(a.startContainer=g,a.startOffset+=g.getLength()),a.endContainer===e&&(a.endContainer=g,a.endOffset+=g.getLength()),a.startContainer===this&&(a.startOffset>c?a.startOffset-=1:a.startOffset===c&&(a.startContainer=g,a.startOffset=g.getLength())),a.endContainer===this&&(a.endOffset>c?a.endOffset-=1:a.endOffset===c&&(a.endContainer=g,a.endOffset=g.getLength())),e.detach(),e.nodeType===i?g.appendData(e.data):d.push(e.empty());else if(e.nodeType===h){j=d.length;while(j--)e.appendChild(d.pop());e.mergeInlines(a)}}},mergeWithBlock:function(a,b){var c=this,d=a,e,f,g;while(d.parentNode.childNodes.length===1)d=d.parentNode;d.detach(),f=c.childNodes.length,e=c.lastChild,e&&e.nodeName==="BR"&&(c.removeChild(e),f-=1),g={startContainer:c,startOffset:f,endContainer:c,endOffset:f},c.appendChild(a.empty()),c.mergeInlines(g),b.setStart(g.startContainer,g.startOffset),b.collapse(!0),window.opera&&(e=c.lastChild)&&e.nodeName==="BR"&&c.removeChild(e)},mergeContainers:function(){var a=this.previousSibling,b=this.firstChild;a&&a.isLike(this)&&a.isContainer()&&(a.appendChild(this.detach().empty()),b&&b.mergeContainers())},split:function(a,b){var c=this;typeof a=="number"&&(a=a<c.childNodes.length?c.childNodes[a]:null);if(c===b)return a;var d=c.parentNode,e=c.cloneNode(!1),f;while(a)f=a.nextSibling,e.appendChild(a),a=f;return c.fixCursor(),e.fixCursor(),(f=c.nextSibling)?d.insertBefore(e,f):d.appendChild(e),d.split(e,b)},fixCursor:function(){var a=this,b=a.ownerDocument,c,d;a.nodeName==="BODY"&&(!(d=a.firstChild)||d.nodeName==="BR")&&(c=b.createElement("DIV"),d?a.replaceChild(c,d):a.appendChild(c),a=c,c=null);if(a.isInline())a.firstChild||(c=b.createTextNode(""));else if(n){while(!a.isLeaf()){d=a.firstChild;if(!d){c=b.createTextNode("");break}a=d}a.isLeaf()&&a.nodeType!==i&&a.parentNode.insertBefore(b.createTextNode(""),a)}else if(!a.textContent&&!a.querySelector("BR")){c=b.createElement("BR");while((d=a.lastElementChild)&&!d.isInline())a=d}return c&&a.appendChild(c),this}});if(function(){var a=document.createElement("div"),b=document.createTextNode("12");return a.appendChild(b),b.splitText(2),a.childNodes.length!==2}())Text.prototype.splitText=function(a){var b=this.ownerDocument.createTextNode(this.data.slice(a)),c=this.nextSibling,d=this.parentNode,e=this.length-a;return c?d.insertBefore(b,c):d.appendChild(b),e&&this.deleteData(a,e),b}}(),function(){"use strict";var a=function(a,b){var c=a.prototype,d;for(d in b)c[d]=b[d]},b=Array.prototype.indexOf,c=1,d=3,e=0,f=1,g=2,h=3,i=function(a,b){var d=a.childNodes;while(b&&a.nodeType===c)a=d[b-1],d=a.childNodes,b=d.length;return a},j=function(a,b){if(a.nodeType===c){var d=a.childNodes;if(b<d.length)a=d[b];else{while(a&&!a.nextSibling)a=a.parentNode;a&&(a=a.nextSibling)}}return a};a(Range,{_insertNode:function(a){var c=this.startContainer,e=this.startOffset,f=this.endContainer,g=this.endOffset,h,i,j,k;return c.nodeType===d?(h=c.parentNode,i=h.childNodes,e&&(k=c.splitText(e),f===c?(g-=e,f=k):f===h&&(g+=1),c=k),e=b.call(i,c),c=h):i=c.childNodes,j=i.length,e===j?c.appendChild(a):c.insertBefore(a,i[e]),c===f&&(g+=i.length-j),this.setStart(c,e),this.setEnd(f,g),this},_extractContents:function(a){var c=this.startContainer,e=this.startOffset,f=this.endContainer,g=this.endOffset;a||(a=this.commonAncestorContainer),a.nodeType===d&&(a=a.parentNode);var h=f.split(g,a),i=c.split(e,a),j=a.ownerDocument.createDocumentFragment(),k;while(i!==h)k=i.nextSibling,j.appendChild(i),i=k;return this.setStart(a,h?b.call(a.childNodes,h):a.childNodes.length),this.collapse(!0),a.fixCursor(),j},_deleteContents:function(){this.moveBoundariesUpTree(),this._extractContents();var a=this.getStartBlock(),b=this.getEndBlock();a&&b&&a!==b&&a.mergeWithBlock(b,this);var c=this.endContainer.ownerDocument.body,e=c.firstChild;if(!e||e.nodeName==="BR")c.fixCursor(),this.selectNodeContents(c.firstChild);var f=this.collapsed;return this.moveBoundariesDownTree(),f&&this.collapse(this.startContainer.nodeType===d),this},insertTreeFragment:function(a){var b=!0,d=a.childNodes,e=d.length;while(e--)if(!d[e].isInline()){b=!1;break}this.collapsed||this._deleteContents(),this.moveBoundariesDownTree();if(b)this._insertNode(a),this.collapse(!1);else{var f=this.startContainer.split(this.startOffset,this.startContainer.ownerDocument.body),g=f.previousSibling,h=g,i=h.childNodes.length,j=f,k=0,l=f.parentNode,m,n;while((m=h.lastChild)&&m.nodeType===c&&m.nodeName!=="BR")h=m,i=h.childNodes.length;while((m=j.firstChild)&&m.nodeType===c&&m.nodeName!=="BR")j=m;while((m=a.firstChild)&&m.isInline())h.appendChild(m);while((m=a.lastChild)&&m.isInline())j.insertBefore(m,j.firstChild),k+=1;n=a;while(n=n.getNextBlock())n.fixCursor();l.insertBefore(a,f),f.mergeContainers(),g.nextSibling.mergeContainers(),f===j&&!j.textContent&&(j=j.previousSibling,k=j.getLength(),l.removeChild(f)),g===h&&!h.textContent&&(h=h.nextSibling,i=0,l.removeChild(g)),this.setStart(h,i),this.setEnd(j,k),this.moveBoundariesDownTree()}},containsNode:function(a,b){var c=this,d=a.ownerDocument.createRange();d.selectNode(a);if(b){var i=c.compareBoundaryPoints(h,d)>-1,j=c.compareBoundaryPoints(f,d)<1;return!i&&!j}var k=c.compareBoundaryPoints(e,d)<1,l=c.compareBoundaryPoints(g,d)>-1;return k&&l},moveBoundariesDownTree:function(){var a=this.startContainer,b=this.startOffset,c=this.endContainer,e=this.endOffset,f;while(a.nodeType!==d){f=a.childNodes[b];if(!f||f.nodeName==="BR")break;a=f,b=0}if(e)while(c.nodeType!==d){f=c.childNodes[e-1];if(!f||f.nodeName==="BR")break;c=f,e=c.getLength()}else while(c.nodeType!==d){f=c.firstChild;if(!f||f.nodeName==="BR")break;c=f}return this.collapsed?(this.setStart(c,e),this.setEnd(a,b)):(this.setStart(a,b),this.setEnd(c,e)),this},moveBoundariesUpTree:function(a){var c=this.startContainer,d=this.startOffset,e=this.endContainer,f=this.endOffset,g;a||(a=this.commonAncestorContainer);while(c!==a&&!d)g=c.parentNode,d=b.call(g.childNodes,c),c=g;while(e!==a&&f===e.getLength())g=e.parentNode,f=b.call(g.childNodes,e)+1,e=g;return this.setStart(c,d),this.setEnd(e,f),this},getStartBlock:function(){var a=this.startContainer,b;return a.isInline()?b=a.getPreviousBlock():a.isBlock()?b=a:(b=i(a,this.startOffset),b=b.getNextBlock()),b&&this.containsNode(b,!0)?b:null},getEndBlock:function(){var a=this.endContainer,b,c;if(a.isInline())b=a.getPreviousBlock();else if(a.isBlock())b=a;else{b=j(a,this.endOffset);if(!b){b=a.ownerDocument.body;while(c=b.lastChild)b=c}b=b.getPreviousBlock()}return b&&this.containsNode(b,!0)?b:null},startsAtBlockBoundary:function(){var a=this.startContainer,c=this.startOffset,d,e;while(a.isInline()){if(c)return!1;d=a.parentNode,c=b.call(d.childNodes,a),a=d}while(c&&(e=a.childNodes[c-1])&&(e.data===""||e.nodeName==="BR"))c-=1;return!c},endsAtBlockBoundary:function(){var a=this.endContainer,c=this.endOffset,d=a.getLength(),e,f;while(a.isInline()){if(c!==d)return!1;e=a.parentNode,c=b.call(e.childNodes,a)+1,a=e,d=a.childNodes.length}while(c<d&&(f=a.childNodes[c])&&(f.data===""||f.nodeName==="BR"))c+=1;return c===d},expandToBlockBoundaries:function(){var a=this.getStartBlock(),c=this.getEndBlock(),d;return a&&c&&(d=a.parentNode,this.setStart(d,b.call(d.childNodes,a)),d=c.parentNode,this.setEnd(d,b.call(d.childNodes,c)+1)),this}})}(),document.addEventListener("DOMContentLoaded",function(){"use strict";var a=2,b=1,c=3,d=4,e=1,f=1,g=3,h=document,i=h.defaultView,j=h.body,k=!!i.opera,l=!!i.ie,m=k||k,n=function(a,b,c){var d=h.createElement(a),e,f,g;b instanceof Array&&(c=b,b=null);if(b)for(e in b)d.setAttribute(e,b[e]);if(c)for(f=0,g=c.length;f<g;f+=1)d.appendChild(c[f]);return d},o={},p={cut:1,paste:1,focus:1,blur:1,pathChange:1,select:1,input:1,undoStateChange:1},q=function(a,b){var c=o[a],d,e;if(c){typeof b!="object"&&(b={data:b}),b.type!==a&&(b.type=a),d=c.length;while(d--)e=c[d],e.handleEvent?e.handleEvent(b):e(b)}},r=function(a){q(a.type,a)},s=function(a,b){var c=o[a];c||(c=o[a]=[],p[a]||h.addEventListener(a,r,!1)),c.push(b)},t=function(a,b){var c=o[a],d;if(c){d=c.length;while(d--)c[d]===b&&c.splice(d,1);c.length||(delete o[a],p[a]||h.removeEventListener(a,r,!1))}},u=function(a,b,c,d){if(a instanceof Range)return a.cloneRange();var e=h.createRange();return e.setStart(a,b),c?e.setEnd(c,d):e.setEnd(a,b),e},v=i.getSelection(),w=null,x=function(){return v.rangeCount&&(w=v.getRangeAt(0).cloneRange()),w};i.ie&&i.addEventListener("beforedeactivate",x,!0);var y,z,A="",B=function(a,b){var c=v.anchorNode,d=v.focusNode,e;if(b||c!==y||d!==z)y=c,z=d,e=c&&d?c===d?d.getPath():"(selection)":"",A!==e&&(A=e,q("pathChange",e));c!==d&&q("select")};s("keyup",B),s("mouseup",B);var C=function(a){a&&(v.removeAllRanges(),v.addRange(a))},D=function(){i.focus()},E=function(){i.blur()};i.addEventListener("focus",r,!1),i.addEventListener("blur",r,!1);var F=function(){return j.innerHTML},G=function(a){j.innerHTML=a,j.fixCursor()},H=function(a,b){b||(b=x()),b.collapse(!0),b._insertNode(a),b.setStartAfter(a),C(b),B()},I="ss-"+Date.now()+"-"+Math.random(),J="es-"+Date.now()+"-"+Math.random(),K=function(b){var c=n("INPUT",{id:I,type:"hidden"}),d=n("INPUT",{id:J,type:"hidden"}),e;b._insertNode(c),b.collapse(!1),b._insertNode(d),c.compareDocumentPosition(d)&a&&(c.id=J,d.id=I,e=c,c=d,d=e),b.setStartAfter(c),b.setEndBefore(d)},L=Array.prototype.indexOf,M=function(a){var b=h.getElementById(I),c=h.getElementById(J);if(b&&c){var d=b.parentNode,e=c.parentNode,f={startContainer:d,endContainer:e,startOffset:L.call(d.childNodes,b),endOffset:L.call(e.childNodes,c)};d===e&&(f.endOffset-=1),b.detach(),c.detach(),d.mergeInlines(f),d!==e&&e.mergeInlines(f),a||(a=h.createRange()),a.setStart(f.startContainer,f.startOffset),a.setEnd(f.endContainer,f.endOffset),a.collapsed||a.moveBoundariesDownTree()}return a},N,O,P,Q,R=function(){Q&&(Q=!1,q("undoStateChange",{canUndo:!0,canRedo:!1})),q("input")};j.addEventListener("DOMCharacterDataModified",R,!1);var S=function(a){Q||(N+=1,N<P&&(O.length=P=N),a&&K(a),O[N]=F(),P+=1,Q=!0)},T=function(){if(N!==0||!Q){S(x()),N-=1,G(O[N]);var a=M();a&&C(a),Q=!0,q("undoStateChange",{canUndo:N!==0,canRedo:!0}),q("input")}},U=function(){if(N+1<P&&Q){N+=1,G(O[N]);var a=M();a&&C(a),q("undoStateChange",{canUndo:!0,canRedo:N+1<P}),q("input")}},V=function(a,b,e){a=a.toUpperCase(),b||(b={});if(!e&&!(e=x()))return!1;var i=e.commonAncestorContainer,j,k;if(i.nearest(a,b))return!0;if(i.nodeType===c)return!1;j=h.createTreeWalker(i,d,function(a){return e.containsNode(a,!0)?f:g},!1);while(k=j.nextNode())if(!k.nearest(a,b))return!1;return!0},W=function(a,b,e){if(e.collapsed){var i=n(a,b).fixCursor();e._insertNode(i),e.selectNodeContents(i)}else{var j=h.createTreeWalker(e.commonAncestorContainer,d,function(a){return e.containsNode(a,!0)?f:g},!1),k,l,m=0,o=0,p=j.currentNode=e.startContainer,q;p.nodeType!==c&&(p=j.nextNode());do q=!p.nearest(a,b),p===e.endContainer&&(q&&p.length>e.endOffset?p.splitText(e.endOffset):o=e.endOffset),p===e.startContainer&&(q&&e.startOffset?p=p.splitText(e.startOffset):m=e.startOffset),q&&(n(a,b).wraps(p),o=p.length),l=p,k||(k=l);while(p=j.nextNode());e=u(k,m,l,o)}return e},X=function(a,b,d){K(d),d.collapsed&&d._insertNode(h.createTextNode(""));var e=d.commonAncestorContainer;while(e.isInline())e=e.parentNode;var f=d.startContainer,g=d.startOffset,i=d.endContainer,j=d.endOffset,k=[],l=function o(a,b){if(d.containsNode(a,!1))return;var e=a.nodeType===c,h,l;if(!d.containsNode(a,!0)){e&&!a.length?a.detach():a.nodeName!=="INPUT"&&k.push([b,a]);return}if(e)a===i&&j!==a.length&&k.push([b,a.splitText(j)]),a===f&&g&&(a.splitText(g),k.push([b,a]));else for(h=a.firstChild;h;h=l)l=h.nextSibling,o(h,b)},m=Array.prototype.filter.call(e.getElementsByTagName(a),function(c){return d.containsNode(c,!0)&&c.is(a,b)});m.forEach(function(a){l(a,a)}),k.forEach(function(a){a[0].cloneNode(!1).wraps(a[1])}),m.forEach(function(a){a.replaceWith(a.empty())}),d=M();var n={startContainer:d.startContainer,startOffset:d.startOffset,endContainer:d.endContainer,endOffset:d.endOffset};return e.mergeInlines(n),d.setStart(n.startContainer,n.startOffset),d.setEnd(n.endContainer,n.endOffset),d},Y=function(a,b,c){if(!c&&!(c=x()))return;S(c),M(c),b&&(c=X(b.tag.toUpperCase(),b.attributes||{},c)),a&&(c=W(a.tag.toUpperCase(),a.attributes||{},c)),C(c),B(0,!0),R()},Z=function(a,b){if(!b&&!(b=x()))return;S(b),M(b);var c=b.getStartBlock(),d=b.getEndBlock();if(c&&d)for(;;){a(c);if(c===d)break;c=c.getNextBlock()}B(0,!0),R()},$=function(a,b){if(!b&&!(b=x()))return;k||j.setAttribute("contenteditable","false"),Q?K(b):S(b),b.expandToBlockBoundaries(),b.moveBoundariesUpTree(j);var c=b._extractContents(j);b._insertNode(a(c)),b.endOffset<b.endContainer.childNodes.length&&b.endContainer.childNodes[b.endOffset].mergeContainers(),b.startContainer.childNodes[b.startOffset].mergeContainers(),k||j.setAttribute("contenteditable","true"),C(M()),B(0,!0),R()},_=function(a){return n("BLOCKQUOTE",[a])},ba=function(a){var b=a.querySelectorAll("blockquote");return Array.prototype.filter.call(b,function(a){return!a.parentNode.nearest("BLOCKQUOTE")}).forEach(function(a){a.replaceWith(a.empty())}),a},bb=function bv(a,b){var c,d,e,f,g,h;for(c=0,d=a.length;c<d;c+=1)e=a[c],f=e.nodeName,e.isBlock()?f!=="LI"&&(h=n("LI",[e.empty()]),e.parentNode.nodeName===b?e.replaceWith(h):(g=e.previousSibling)&&g.nodeName===b?(g.appendChild(h),e.detach(),c-=1,d-=1):e.replaceWith(n(b,[h]))):e.isContainer()&&(f!==b&&/^[DOU]L$/.test(f)?e.replaceWith(n(b,[e.empty()])):bv(e.childNodes,b))},bc=function(a){return bb(a.childNodes,"UL"),a},bd=function(a){return bb(a.childNodes,"OL"),a},be=function(a){var b=a.querySelectorAll("UL, OL");return Array.prototype.filter.call(b,function(a){return!a.parentNode.nearest("UL")&&!a.parentNode.nearest("OL")}).forEach(function(a){var b=a.empty(),c=b.childNodes,d=c.length,e;while(d--)e=c[d],e.nodeName==="LI"&&b.replaceChild(n("DIV",[e.empty()]),e);a.replaceWith(b)}),a},bf={DIV:"DIV",PRE:"DIV",H1:"DIV",H2:"DIV",H3:"DIV",H4:"DIV",H5:"DIV",H6:"DIV",P:"DIV",DT:"DD",DD:"DT",LI:"LI"},bg=function(a,b,c){var d=bf[a.nodeName],e=b.split(c,a.parentNode);return e.nodeName!==d&&(a=n(d),a.replaces(e).appendChild(e.empty()),e=a),e},bh=/^A(?:DDRESS|RTICLE|SIDE)|BLOCKQUOTE|CAPTION|D(?:[DLT]|IV)|F(?:IGURE|OOTER)|H[1-6]|HEADER|L(?:ABEL|EGEND|I)|O(?:L|UTPUT)|P(?:RE)?|SECTION|T(?:ABLE|BODY|D|FOOT|H|HEAD|R)|UL$/,bi={color:{regexp:/\S/,replace:function(a){return n("SPAN",{"class":"colour",style:"color:"+a})}},fontWeight:{regexp:/^bold/i,replace:function(){return n("B")}},fontStyle:{regexp:/^italic/i,replace:function(){return n("I")}},fontFamily:{regexp:/\S/,replace:function(a){return n("SPAN",{"class":"font",style:"font-family:"+a})}},fontSize:{regexp:/\S/,replace:function(a){return n("SPAN",{"class":"size",style:"font-size:"+a})}}},bj={SPAN:function(a,b){var c=a.style,d,e,f,g,h,i;for(d in bi)e=bi[d],f=c[d],f&&e.regexp.test(f)&&(i=e.replace(f),g&&g.appendChild(i),g=i,h||(h=i));return h&&(g.appendChild(a.empty()),b.replaceChild(h,a)),g||a},STRONG:function(a,b){var c=n("B");return b.replaceChild(c,a),c.appendChild(a.empty()),c},EM:function(a,b){var c=n("I");return b.replaceChild(c,a),c.appendChild(a.empty()),c}},bk=function(a,d){var e=a.childNodes,f,g,h,i,j,k,l;for(f=0,g=e.length;f<g;f+=1){h=e[f],i=h.nodeName,j=h.nodeType,k=bj[i];if(k)h=k(h,a);else if(!bh.test(i)&&!h.isInline()||j===c&&!/\S/.test(h.data)){a.removeChild(h),f-=1,g-=1;continue}j===b&&(!d&&h.style.cssText&&h.removeAttribute("style"),h.childNodes.length&&bk(h,d))}return a},bl=function(a,b){var c=a.childNodes,d=null,e,f,g,h;for(e=0,f=c.length;e<f;e+=1){g=c[e],h=g.nodeName==="BR";if(!h&&g.isInline())d||(d=n(b)),d.appendChild(g),e-=1,f-=1;else if(h||d)d||(d=n(b)),d.fixCursor(),h?a.replaceChild(d,g):(a.insertBefore(d,g),e+=1,f+=1),d=null}return d&&a.appendChild(d.fixCursor()),a},bm=function(a){var b=a.querySelectorAll("BR"),c=b.length,d,e,f,g,h;while(c--){d=b[c],e=d.parentNode;if(!e)continue;if(d.nextSibling&&d.previousSibling){while(e.isInline())e=e.parentNode;e.isBlock()?bf[e.nodeName]&&(bg(e,d.parentNode,d),d.detach()):bl(e,"DIV")}else d.detach()}};h.addEventListener(l?"beforecut":"cut",function(){var a=x();S(a),M(a),setTimeout(function(){j.fixCursor()},0)},!1),h.addEventListener(l?"beforepaste":"paste",function(){var a=x(),b=a.startContainer,c=a.startOffset,d=a.endContainer,e=a.endOffset,f=n("DIV",{style:"position: absolute; overflow: hidden;top: -100px; left: -100px; width: 1px; height: 1px;"});j.appendChild(f),a.selectNodeContents(f),C(a),setTimeout(function(){var g=f.detach().empty(),h=g.firstChild;h&&h===g.lastChild&&h.nodeName==="DIV"&&g.replaceChild(h.empty(),h),g.normalize(),bk(g,!1),bm(g);var i=g;while(i=i.getNextBlock())i.fixCursor();a.setStart(b,c),a.setEnd(d,e),a.insertTreeFragment(g),R(),a.collapse(!1),C(a),B(0,!0)},0)},!1);var bn={8:"backspace",9:"tab",13:"enter",32:"space",46:"delete"},bo=function(a){return function(b){b.preventDefault(),a()}},bp=function(a){return function(b){b.preventDefault();var c=x();V(a,null,c)?Y(null,{tag:a},c):Y({tag:a},null,c)}},bq={tab:function(a){a.preventDefault()},enter:function(a){a.preventDefault();var d=x();if(!d)return;S(d),M(d),d.collapsed||d._deleteContents();var e=d.getStartBlock(),f=e?e.nodeName:"DIV",g=bf[f],h;if(!e){d._insertNode(n("BR")),d.collapse(!1),C(d),B(0,!0),R();return}var i=d.startContainer,j=d.startOffset,l;if(!g){if(i===e){i=j?i.childNodes[j-1]:null,j=0;if(i){i.nodeName==="BR"?i=i.nextSibling:j=i.getLength();if(!i||i.nodeName==="BR")l=n("DIV").fixCursor(),i?e.replaceChild(l,i):e.appendChild(l),i=l}}bl(e,"DIV"),g="DIV",i||(i=e.firstChild),d.setStart(i,j),d.setEnd(i,j),e=d.getStartBlock()}if(!e.textContent){if(e.nearest("UL")||e.nearest("OL"))return $(be,d);if(e.nearest("BLOCKQUOTE"))return $(ba,d)}h=bg(e,i,j);while(h.nodeType===b){var m=h.firstChild,o;while(m&&m.nodeType===c&&!m.data){o=m.nextSibling;if(!o||o.nodeName==="BR")break;m.detach(),m=o}if(!m||m.nodeName==="BR"||m.nodeType===c&&!k)break;h=m}C(u(h,0)),B(0,!0),R()},backspace:function(a){var b=x();if(!b.collapsed)a.preventDefault(),b._deleteContents(),C(b),B(0,!0);else if(b.startsAtBlockBoundary()){a.preventDefault();var c=b.getStartBlock(),d=c.getPreviousBlock();if(d)d.mergeWithBlock(c,b),C(b);else{if(c.nearest("UL")||c.nearest("OL"))return $(be,b);if(c.nearest("BLOCKQUOTE"))return $(ba,b);C(b),B(0,!0)}}},"delete":function(a){var b=x();if(!b.collapsed)a.preventDefault(),b._deleteContents(),C(b),B(0,!0);else if(b.endsAtBlockBoundary()){a.preventDefault();var c=b.getStartBlock(),d=c.getNextBlock();d&&(c.mergeWithBlock(d,b),C(b),B(0,!0))}},space:function(){var a=x();S(a),M(a),C(a)},"ctrl-b":bp("B"),"ctrl-i":bp("I"),"ctrl-u":bp("U"),"ctrl-y":bo(U),"ctrl-z":bo(T),"ctrl-shift-z":bo(U)};s("keydown",function(a){var b=a.keyCode||a.which,c=bn[b]||String.fromCharCode(b).toLowerCase(),d="";111<b&&b<124&&(c="f"+(b-111)),a.altKey&&(d+="alt-");if(a.ctrlKey||a.metaKey)d+="ctrl-";a.shiftKey&&(d+="shift-"),c=d+c,bq[c]&&bq[c](a)});var br=/<style[^>]*>([\s\S]*?)<\/style>/gi,bs=function(a){return function(){return a.apply(null,arguments),this}},bt=function(a,b,c){return function(){return a(b,c),D(),this}};i.editor={addEventListener:bs(s),removeEventListener:bs(t),focus:bs(D),blur:bs(E),getDocument:function(){return h},getHTML:function(){var a=[],b,c,d,e;if(m){b=j;while(b=b.getNextBlock())!b.textContent&&!b.querySelector("BR")&&(c=n("BR"),b.appendChild(c),a.push(c))}d=F();if(m){e=a.length;while(e--)a[e].detach()}return d},setHTML:function(a){var b=h.createDocumentFragment(),c=n("DIV"),d="",e;br.lastIndex=0,a=a.replace(br,function(a,b){return d+=b.replace(/<!--|-->/g,""),""}),c.innerHTML=a,b.appendChild(c.empty()),bk(b,!0),bm(b),bl(b,"DIV");var f=b;while(f=f.getNextBlock())f.fixCursor();while(e=j.lastChild)j.removeChild(e);if(d){var g=h.documentElement.firstChild,i=n("STYLE",{type:"text/css"});i.styleSheet?(g.appendChild(i),i.styleSheet.cssText=d):(i.appendChild(h.createTextNode(d)),g.appendChild(i))}j.appendChild(b),j.fixCursor(),N=-1,O=[],P=0,Q=!1;var k=u(j.firstChild,0);return S(k),C(M(k)),B(0,!0),this},insertImage:function(a){var b=n("IMG",{src:a});return H(b),b},getPath:function(){return A},getSelection:x,setSelection:bs(C),undo:bs(T),redo:bs(U),hasFormat:V,changeFormat:bs(Y),bold:bt(Y,{tag:"B"}),italic:bt(Y,{tag:"I"}),underline:bt(Y,{tag:"U"}),removeBold:bt(Y,null,{tag:"B"}),removeItalic:bt(Y,null,{tag:"I"}),removeUnderline:bt(Y,null,{tag:"U"}),makeLink:function(a){return Y({tag:"A",attributes:{href:a}},{tag:"A"}),D(),this},setFontFace:function(a){return Y({tag:"SPAN",attributes:{"class":"font",style:"font-family: "+a+", sans-serif;"}},{tag:"SPAN",attributes:{"class":"font"}}),D(),this},setFontSize:function(a){return Y({tag:"SPAN",attributes:{"class":"size",style:"font-size: "+(typeof a=="number"?a+"px":a)}},{tag:"SPAN",attributes:{"class":"size"}}),D(),this},setTextColour:function(a){return Y({tag:"SPAN",attributes:{"class":"colour",style:"color: "+a}},{tag:"SPAN",attributes:{"class":"colour"}}),D(),this},setHighlightColour:function(a){return Y({tag:"SPAN",attributes:{"class":"highlight",style:"background-color: "+a}},{tag:"SPAN",attributes:{"class":"highlight"}}),D(),this},setTextAlignment:function(a){return Z(function(b){b.className="align-"+a,b.style.textAlign=a}),D(),this},modifyBlocks:bs($),incQuoteLevel:bt($,_),decQuoteLevel:bt($,ba),makeUnorderedList:bt($,bc),makeOrderedList:bt($,bd),removeList:bt($,be)},j.setAttribute("contenteditable","true"),i.editor.setHTML("")},!1); |