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=k||!!i.ie,m=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},n={},o=function(a,b){var c=n[a]||(n[a]=[]);c.push(b)},p=function(a,b){var c=n[a],d;if(c){d=c.length;while(d--)c[d]===b&&c.splice(d,1)}},q=function(a,b){var c=n[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,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},t=i.getSelection(),u=null,v=function(){return t.rangeCount&&(u=t.getRangeAt(0).cloneRange()),u};i.ie&&i.addEventListener("beforedeactivate",v,!0);var w,x,y="",z=function(){var a=t.anchorNode,b=t.focusNode,c;if(a!==w||b!==x)w=a,x=b,c=a&&b?a===b?b.getPath():"(selection)":"",y!==c&&(y=c,q("pathChange",c)),a!==b&&q("select")};j.addEventListener("keyup",z,!1),j.addEventListener("mouseup",z,!1);var A=function(a){a&&(t.removeAllRanges(),t.addRange(a)),z()},B=function(){i.focus()},C=function(){i.blur()};i.addEventListener("focus",r,!1),i.addEventListener("blur",r,!1);var D=function(){return j.innerHTML},E=function(a){j.innerHTML=a,j.fixCursor()},F=function(a,b){b||(b=v()),b.collapse(!0),b._insertNode(a),b.setStartAfter(a),A(b)},G="ss-"+Date.now()+"-"+Math.random(),H="es-"+Date.now()+"-"+Math.random(),I=function(b){var c=m("INPUT",{id:G,type:"hidden"}),d=m("INPUT",{id:H,type:"hidden"}),e;b._insertNode(c),b.collapse(!1),b._insertNode(d),c.compareDocumentPosition(d)&a&&(c.id=H,d.id=G,e=c,c=d,d=e),b.setStartAfter(c),b.setEndBefore(d)},J=Array.prototype.indexOf,K=function(a){var b=h.getElementById(G),c=h.getElementById(H);if(b&&c){var d=b.parentNode,e=c.parentNode,f={startContainer:d,endContainer:e,startOffset:J.call(d.childNodes,b),endOffset:J.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},L,M,N,O,P=function(){O&&(O=!1,q("undoStateChange",{canUndo:!0,canRedo:!1})),q("input")};j.addEventListener("DOMCharacterDataModified",P,!1);var Q=function(a){O||(L+=1,L<N&&(M.length=N=L),a&&I(a),M[L]=D(),N+=1,O=!0)},R=function(){if(L!==0||!O){Q(v()),L-=1,E(M[L]);var a=K();a&&A(a),O=!0,q("undoStateChange",{canUndo:L!==0,canRedo:!0}),q("input")}},S=function(){if(L+1<N&&O){L+=1,E(M[L]);var a=K();a&&A(a),q("undoStateChange",{canUndo:!0,canRedo:L+1<N}),q("input")}},T=function(a,b,e){a=a.toUpperCase(),b||(b={});if(!e&&!(e=v()))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},U=function(a,b,e){if(e.collapsed){var i=m(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,n=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):n=e.startOffset),q&&(m(a,b).wraps(p),o=p.length),l=p,k||(k=l);while(p=j.nextNode());e=s(k,n,l,o)}return e},V=function(a,b,d){I(d);var e=d.commonAncestorContainer;while(e.isInline())e=e.parentNode;var f=d.startContainer,g=d.startOffset,h=d.endContainer,i=d.endOffset,j=[],k=function n(a,b){if(d.containsNode(a,!1))return;var e=a.nodeType===c,k,l;if(!d.containsNode(a,!0)){e&&!a.length?a.detach():a.nodeName!=="INPUT"&&j.push([b,a]);return}if(e)a===h&&i!==a.length&&j.push([b,a.splitText(i)]),a===f&&g&&(a.splitText(g),j.push([b,a]));else for(k=a.firstChild;k;k=l)l=k.nextSibling,n(k,b)},l=Array.prototype.filter.call(e.getElementsByTagName(a),function(c){return d.containsNode(c,!0)&&c.is(a,b)});l.forEach(function(a){k(a,a)}),j.forEach(function(a){a[0].cloneNode(!1).wraps(a[1])}),l.forEach(function(a){a.replaceWith(a.empty())}),d=K();var m={startContainer:d.startContainer,startOffset:d.startOffset,endContainer:d.endContainer,endOffset:d.endOffset};return e.mergeInlines(m),d.setStart(m.startContainer,m.startOffset),d.setEnd(m.endContainer,m.endOffset),d},W=function(a,b,c){if(!c&&!(c=v()))return;Q(c),K(c),b&&(c=V(b.tag.toUpperCase(),b.attributes||{},c)),a&&(c=U(a.tag.toUpperCase(),a.attributes||{},c)),A(c),P()},X=function(a,b){if(!b&&!(b=v()))return;Q(b),K(b);var c=b.commonAncestorContainer,d=h.createTreeWalker(c,e,function(a){return b.containsNode(a,!0)&&a.isBlock()?f:g},!1),i;while(i=d.nextNode())a(i);P()},Y=function(a,b){if(!b&&!(b=v()))return;k||j.setAttribute("contenteditable","false"),O?I(b):Q(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"),A(K()),P()},Z=function(a){return m("BLOCKQUOTE",[a])},$=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},_=function bt(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=m("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(m(b,[h]))):e.isContainer()&&(f!==b&&/^[DOU]L$/.test(f)?e.replaceWith(m(b,[e.empty()])):bt(e.childNodes,b))},ba=function(a){return _(a.childNodes,"UL"),a},bb=function(a){return _(a.childNodes,"OL"),a},bc=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(m("DIV",[e.empty()]),e);a.replaceWith(b)}),a},bd={DIV:"DIV",PRE:"DIV",H1:"DIV",H2:"DIV",H3:"DIV",H4:"DIV",H5:"DIV",H6:"DIV",P:"DIV",DT:"DD",DD:"DT",LI:"LI"},be=function(a,b,c){var d=bd[a.nodeName],e=b.split(c,a.parentNode);return e.nodeName!==d&&(a=m(d),a.replaces(e).appendChild(e.empty()),e=a),e},bf=/^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$/,bg={color:{regexp:/\S/,replace:function(a){return m("SPAN",{"class":"colour",style:"color:"+a})}},fontWeight:{regexp:/^bold/i,replace:function(){return m("B")}},fontStyle:{regexp:/^italic/i,replace:function(){return m("I")}},fontFamily:{regexp:/\S/,replace:function(a){return m("SPAN",{"class":"font",style:"font-family:"+a})}},fontSize:{regexp:/\S/,replace:function(a){return m("SPAN",{"class":"size",style:"font-size:"+a})}}},bh={SPAN:function(a,b){var c=a.style,d,e,f,g,h,i;for(d in bg)e=bg[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=m("B");return b.replaceChild(c,a),c.appendChild(a.empty()),c},EM:function(a,b){var c=m("I");return b.replaceChild(c,a),c.appendChild(a.empty()),c}},bi=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=bh[i];if(k)h=k(h,a);else if(!bf.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&&bi(h,d))}return a},bj=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=m(b)),d.appendChild(g),e-=1,f-=1;else if(h||d)d||(d=m(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},bk=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()?bd[e.nodeName]&&(be(e,d.parentNode,d),d.detach()):bj(e,"DIV")}else d.detach()}};h.addEventListener("cut",function(){setTimeout(function(){j.fixCursor()},0)}),h.addEventListener("paste",function(){var a=v(),b=a.startContainer,c=a.startOffset,d=a.endContainer,e=a.endOffset,f=m("DIV",{style:"position: absolute; overflow: hidden;top: -100px; left: -100px; width: 1px; height: 1px;"});j.appendChild(f),a.selectNodeContents(f),A(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(),bi(g,!1),bk(g);var i=g;while(i=i.getNextBlock())i.fixCursor();a.setStart(b,c),a.setEnd(d,e),a.insertTreeFragment(g),P(),a.collapse(!1),A(a)},0)},!1);var bl={8:"backspace",9:"tab",13:"enter",32:"space",46:"delete"},bm=function(a){return function(b){b.preventDefault(),a()}},bn=function(a){return function(b){b.preventDefault();var c=v();T(a,null,c)?W(null,{tag:a},c):W({tag:a},null,c)}},bo={tab:function(a){a.preventDefault()},enter:function(a){a.preventDefault();var d=v();if(!d)return;Q(d),K(d),d.collapsed||d._deleteContents();var e=d.getStartBlock(),f=e?e.nodeName:"DIV",g=bd[f],h;if(!e){d._insertNode(m("BR")),d.collapse(!1),A(d),P();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=m("DIV").fixCursor(),i?e.replaceChild(l,i):e.appendChild(l),i=l}}bj(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"))return Y(bc,d);if(e.nearest("BLOCKQUOTE"))return Y($,d)}h=be(e,i,j);while(h.nodeType===b){var n=h.firstChild,o;while(n&&n.nodeType===c&&!n.data){o=n.nextSibling;if(!o||o.nodeName==="BR")break;n.detach(),n=o}if(!n||n.nodeName==="BR"||n.nodeType===c&&!k)break;h=n}A(s(h,0)),P()},backspace:function(a){var b=v();if(!b.collapsed)a.preventDefault(),b._deleteContents(),A(b);else if(b.startsAtBlockBoundary()){a.preventDefault();var c=b.getStartBlock(),d=c.getPreviousBlock();if(d)d.mergeWithBlock(c,b),A(b);else{if(c.nearest("UL"))return Y(bc,b);if(c.nearest("BLOCKQUOTE"))return Y($,b);A(b)}}},"delete":function(a){var b=v();if(!b.collapsed)a.preventDefault(),b._deleteContents(),A(b);else if(b.endsAtBlockBoundary()){a.preventDefault();var c=b.getStartBlock(),d=c.getNextBlock();d&&(c.mergeWithBlock(d,b),A(b))}},space:function(){var a=v();Q(a),K(a),A(a)},"ctrl-b":bn("B"),"ctrl-i":bn("I"),"ctrl-u":bn("U"),"ctrl-y":bm(S),"ctrl-z":bm(R),"ctrl-shift-z":bm(S)};j.addEventListener("keydown",function(a){var b=a.keyCode||a.which,c=bl[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,bo[c]?bo[c](a):q("keydown",a)},!1),j.addEventListener("keypress",r,!1),j.addEventListener("keyup",r,!1);var bp=/<style[^>]*>([\s\S]*?)<\/style>/gi,bq=function(a){return function(){return a.apply(null,arguments),this}},br=function(a,b,c){return function(){return a(b,c),B(),this}};i.editor={addEventListener:bq(o),removeEventListener:bq(p),focus:bq(B),blur:bq(C),getDocument:function(){return h},getHTML:function(){var a=[],b,c,d,e;if(l){b=j;while(b=b.getNextBlock())!b.textContent&&!b.querySelector("BR")&&(c=m("BR"),b.appendChild(c),a.push(c))}d=D();if(l){e=a.length;while(e--)a[e].detach()}return d},setHTML:function(a){var b=h.createDocumentFragment(),c=m("DIV"),d="",e;bp.lastIndex=0,a=a.replace(bp,function(a,b){return d+=b.replace(/<!--|-->/g,""),""}),c.innerHTML=a,b.appendChild(c.empty()),bi(b,!0),bk(b),bj(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=m("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(),L=-1,M=[],N=0,O=!1;var k=s(j.firstChild,0);return Q(k),A(K(k)),this},insertImage:function(a){var b=m("IMG",{src:a});return F(b),b},getPath:function(){return y},getSelection:v,setSelection:bq(A),undo:bq(R),redo:bq(S),hasFormat:bq(T),changeFormat:bq(W),bold:br(W,{tag:"B"}),italic:br(W,{tag:"I"}),underline:br(W,{tag:"U"}),removeBold:br(W,null,{tag:"B"}),removeItalic:br(W,null,{tag:"I"}),removeUnderline:br(W,null,{tag:"U"}),makeLink:function(a){return W({tag:"A",attributes:{href:a}},{tag:"A"}),B(),this},setFontFace:function(a){return W({tag:"SPAN",attributes:{"class":"font",style:"font-family: "+a+", sans-serif;"}},{tag:"SPAN","class":"font"}),B(),this},setFontSize:function(a){return W({tag:"SPAN",attributes:{"class":"size",style:"font-size: "+(typeof a=="number"?a+"px":a)}},{tag:"SPAN","class":"size"}),B(),this},setTextAlignment:function(a){return X(function(b){b.style.textAlign=a}),B(),this},modifyBlocks:bq(Y),incQuoteLevel:br(Y,Z),decQuoteLevel:br(Y,$),makeUnorderedList:br(Y,ba),makeOrderedList:br(Y,bb),removeList:br(Y,bc)},j.setAttribute("contenteditable","true"),i.editor.setHTML("")},!1); |