0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 15:23:29 -05:00
Squire/build/squire.js
Neil Jenkins 0f750d2a0e Fix Range#get(Start|End)Block.
Was still returning the wrong result in certain situations (for example when the
start boundary was just before the end of a block).
2011-11-03 12:10:16 +11:00

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