/* Copyright © 2011 by Neil Jenkins. Licensed under the MIT license. */(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=function(a,b){var c=b.parentNode;return c&&c.replaceChild(a,b),a},g=1,h=3,i=1,j=1,k=3,l=function(a,b){var c;for(;;){c=a.firstChild;while(!c&&a)c=a.nextSibling,c||(a=a.parentNode);if(c.nodeName==="BODY")return null;if(b(c))return c;a=c}},m=function(a,b){var c;for(;;){c=a.previousSibling;if(c)while(a=c.lastChild)c=a;else c=a.parentNode;if(c.nodeName==="BODY")return null;if(b(c))return c;a=c}},n=function(a){return a.isBlock()},o=!!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 f(a,this),this},replaces:function(a){return f(this,a),this},nearest:function(a,b){var c=this.parentNode;return c?c.nearest(a,b):null},getPreviousBlock:function(){return m(this,n)},getNextBlock:function(){return l(this,n)},split:function(a,b){return a},mergeContainers:function(){}}),a(Text,{isInline:d,getLength:function(){return this.length},isLike:function(a){return a.nodeType===h},split:function(a,b){var c=this;return b(c)?a:c.parentNode.split(c.splitText(a),b)}}),a(Element,{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 f(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===g);return null},isLike:function(a){return a.nodeType===g&&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,f,i;while(c--){e=b[c],f=c&&b[c-1];if(c&&e.isInline()&&e.isLike(f))a.startContainer===e&&(a.startContainer=f,a.startOffset+=f.getLength()),a.endContainer===e&&(a.endContainer=f,a.endOffset+=f.getLength()),a.startContainer===this&&(a.startOffset>c?a.startOffset-=1:a.startOffset===c&&(a.startContainer=f,a.startOffset=f.getLength())),a.endContainer===this&&(a.endOffset>c?a.endOffset-=1:a.endOffset===c&&(a.endContainer=f,a.endOffset=f.getLength())),e.detach(),e.nodeType===h?f.appendData(e.data):d.push(e.empty());else if(e.nodeType===g){i=d.length;while(i--)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(o)while(a.nodeType!==h){d=a.firstChild;if(!d){c=b.createTextNode("");break}a=d}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;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(),this.moveBoundariesDownTree();var a=this.getStartBlock(),b=this.getEndBlock();a!==b&&a.mergeWithBlock(b,this);var c=a.ownerDocument,d=c.body,e=d.firstChild;if(!e||e.nodeName==="BR")a=c.createElement("DIV").fixCursor(),e?d.replaceChild(a,e):d.appendChild(a),this.selectNodeContents(a);return 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;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;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.selectNodeContents(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=this.startOffset,d=a.childNodes;return a.nodeType===c&&be.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]*>([\s\S]*?)<\/style>/gi,bn=function(a){return function(){return a.apply(null,arguments),this}},bo=function(a,b,c){return function(){return a(b,c),A(),this}};i.editor={addEventListener:bn(n),removeEventListener:bn(o),focus:bn(A),blur:bn(B),getDocument:function(){return h},getHTML:C,setHTML:function(a){var b=[];bm.lastIndex=0,a=a.replace(bm,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),e=c.childNodes,d=null;for(i=0,g=e.length;i