0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-10 17:00:11 -05:00
Squire/build/squire.js
Neil Jenkins 7812b8db23 Turn Range prototype extensions into functions.
A step towards being able to share code between multiple concurrent instances of
the editor. Also reduces minified size slightly.
2013-06-20 21:03:01 +10:00

1 line
No EOL
27 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

(function(e){"use strict";function t(e,t,n){this.root=this.currentNode=e,this.nodeType=t,this.filter=n}function n(e,t){for(var n=e.length;n--;)if(!t(e[n]))return!1;return!0}function r(e,t,n){if(e.nodeName!==t)return!1;for(var r in n)if(e.getAttribute(r)!==n[r])return!1;return!0}function o(e,t){return e.nodeType===t.nodeType&&e.nodeName===t.nodeName&&e.className===t.className&&(!e.style&&!t.style||e.style.cssText===t.style.cssText)}function i(e){return e.nodeType===x&&!!et[e.nodeName]}function a(e){return J.test(e.nodeName)}function d(e){return e.nodeType===x&&!a(e)&&n(e.childNodes,a)}function l(e){return e.nodeType===x&&!a(e)&&!d(e)}function s(e){return d(e)?I:L}function f(e){var n=e.ownerDocument,r=new t(n.body,B,s,!1);return r.currentNode=e,r}function c(e){return f(e).previousNode()}function u(e){return f(e).nextNode()}function p(e,t,n){do if(r(e,t,n))return e;while(e=e.parentNode);return null}function h(e){var t,n,r,o,i=e.parentNode;return i&&e.nodeType===x?(t=h(i),t+=(t?">":"")+e.nodeName,(n=e.id)&&(t+="#"+n),(r=e.className.trim())&&(o=r.split(/\s\s*/),o.sort(),t+=".",t+=o.join("."))):t=i?h(i):"",t}function N(e){var t=e.nodeType;return t===x?e.childNodes.length:e.length||0}function C(e){var t=e.parentNode;return t&&t.removeChild(e),e}function v(e,t){var n=e.parentNode;n&&n.replaceChild(t,e)}function m(e){for(var t=e.ownerDocument.createDocumentFragment(),n=e.childNodes,r=n?n.length:0;r--;)t.appendChild(e.firstChild);return t}function g(e){var t,n,r=e.ownerDocument,o=e;if("BODY"===e.nodeName&&((n=e.firstChild)&&"BR"!==n.nodeName||(t=r.createElement("DIV"),n?e.replaceChild(t,n):e.appendChild(t),e=t,t=null)),a(e))e.firstChild||(j?(t=r.createTextNode(""),kt(t)):t=r.createTextNode(""));else if($){for(;e.nodeType!==D&&!i(e);){if(n=e.firstChild,!n){t=r.createTextNode("");break}e=n}e.nodeType===D?/^ +$/.test(e.data)&&(e.data=""):i(e)&&e.parentNode.insertBefore(r.createTextNode(""),e)}else if(!e.querySelector("BR"))for(t=r.createElement("BR");(n=e.lastElementChild)&&!a(n);)e=n;return t&&e.appendChild(t),o}function y(e,t,n){var r,o,i,a=e.nodeType;if(a===D&&e!==n)return y(e.parentNode,e.splitText(t),n);if(a===x){if("number"==typeof t&&(t=e.childNodes.length>t?e.childNodes[t]:null),e===n)return t;for(r=e.parentNode,o=e.cloneNode(!1);t;)i=t.nextSibling,o.appendChild(t),t=i;return g(e),g(o),(i=e.nextSibling)?r.insertBefore(o,i):r.appendChild(o),y(r,o,n)}return t}function T(e,t){if(e.nodeType===x)for(var n,r,i,d=e.childNodes,l=d.length,s=[];l--;)if(n=d[l],r=l&&d[l-1],l&&a(n)&&o(n,r)&&!et[n.nodeName])t.startContainer===n&&(t.startContainer=r,t.startOffset+=N(r)),t.endContainer===n&&(t.endContainer=r,t.endOffset+=N(r)),t.startContainer===e&&(t.startOffset>l?t.startOffset-=1:t.startOffset===l&&(t.startContainer=r,t.startOffset=N(r))),t.endContainer===e&&(t.endOffset>l?t.endOffset-=1:t.endOffset===l&&(t.endContainer=r,t.endOffset=N(r))),C(n),n.nodeType===D?r.appendData(n.data.replace(/\u200B/g,"")):s.push(m(n));else if(n.nodeType===x){for(i=s.length;i--;)n.appendChild(s.pop());T(n,t)}}function S(e,t,n){for(var r,o,i,a=t;1===a.parentNode.childNodes.length;)a=a.parentNode;C(a),o=e.childNodes.length,r=e.lastChild,r&&"BR"===r.nodeName&&(e.removeChild(r),o-=1),i={startContainer:e,startOffset:o,endContainer:e,endOffset:o},e.appendChild(m(t)),T(e,i),n.setStart(i.startContainer,i.startOffset),n.collapse(!0),G&&(r=e.lastChild)&&"BR"===r.nodeName&&e.removeChild(r)}function O(e){var t=e.previousSibling,n=e.firstChild;t&&o(t,e)&&l(t)&&(C(e),t.appendChild(m(e)),n&&O(n))}function b(t,n,r){var o,i,a,d=e.createElement(t);if(n instanceof Array&&(r=n,n=null),n)for(o in n)d.setAttribute(o,n[o]);if(r)for(i=0,a=r.length;a>i;i+=1)d.appendChild(r[i]);return d}var E=2,x=1,D=3,B=1,A=4,I=1,L=3,R=0,w=1,P=2,U=3,k=e.defaultView,V=e.body,H=navigator.userAgent,z=/iP(?:ad|hone|od)/.test(H),M=/Mac OS X/.test(H),F=/Gecko\//.test(H),K=/Trident\//.test(H),q=8===k.ie,G=!!k.opera,Q=/WebKit\//.test(H),Y=M?"meta-":"ctrl-",$=K||G,j=K||Q,W=K,X=/\S/,Z=Array.prototype.indexOf,_={1:1,2:2,3:4,8:128,9:256,11:1024};t.prototype.nextNode=function(){for(var e,t=this.currentNode,n=this.root,r=this.nodeType,o=this.filter;;){for(e=t.firstChild;!e&&t&&t!==n;)e=t.nextSibling,e||(t=t.parentNode);if(!e)return null;if(_[e.nodeType]&r&&o(e)===I)return this.currentNode=e,e;t=e}},t.prototype.previousNode=function(){for(var e,t=this.currentNode,n=this.root,r=this.nodeType,o=this.filter;;){if(t===n)return null;if(e=t.previousSibling)for(;t=e.lastChild;)e=t;else e=t.parentNode;if(!e)return null;if(_[e.nodeType]&r&&o(e)===I)return this.currentNode=e,e;t=e}};var J=/^(?:#text|A(?:BBR|CRONYM)?|B(?:R|D[IO])?|C(?:ITE|ODE)|D(?:FN|EL)|EM|FONT|HR|I(?:NPUT|MG|NS)?|KBD|Q|R(?:P|T|UBY)|S(?:U[BP]|PAN|TRONG|AMP)|U)$/,et={BR:1,IMG:1,INPUT:1};(function(){var t=e.createElement("div"),n=e.createTextNode("12");return t.appendChild(n),n.splitText(2),2!==t.childNodes.length})()&&(Text.prototype.splitText=function(e){var t=this.ownerDocument.createTextNode(this.data.slice(e)),n=this.nextSibling,r=this.parentNode,o=this.length-e;return n?r.insertBefore(t,n):r.appendChild(t),o&&this.deleteData(e,o),t});var tt,nt=function(e,t){for(var n=e.childNodes;t&&e.nodeType===x;)e=n[t-1],n=e.childNodes,t=n.length;return e},rt=function(e,t){if(e.nodeType===x){var n=e.childNodes;if(n.length>t)e=n[t];else{for(;e&&!e.nextSibling;)e=e.parentNode;e&&(e=e.nextSibling)}}return e},ot=function(e,n){e=e.cloneRange(),ct(e);for(var r=e.startContainer,o=e.endContainer,i=e.commonAncestorContainer,a=new t(i,A,function(){return I},!1),d=a.currentNode=r;!n(d,e)&&d!==o&&(d=a.nextNode()););},it=function(e){var t="";return ot(e,function(e,n){var r=e.data;r&&/\S/.test(r)&&(e===n.endContainer&&(r=r.slice(0,n.endOffset)),e===n.startContainer&&(r=r.slice(n.startOffset)),t+=r)}),t},at=function(e,t){var n,r,o,i,a=e.startContainer,d=e.startOffset,l=e.endContainer,s=e.endOffset;a.nodeType===D?(n=a.parentNode,r=n.childNodes,d===a.length?(d=Z.call(r,a)+1,e.collapsed&&(l=n,s=d)):(d&&(i=a.splitText(d),l===a?(s-=d,l=i):l===n&&(s+=1),a=i),d=Z.call(r,a)),a=n):r=a.childNodes,o=r.length,d===o?a.appendChild(t):a.insertBefore(t,r[d]),a===l&&(s+=r.length-o),e.setStart(a,d),e.setEnd(l,s)},dt=function(e,t){var n=e.startContainer,r=e.startOffset,o=e.endContainer,i=e.endOffset;t||(t=e.commonAncestorContainer),t.nodeType===D&&(t=t.parentNode);for(var a,d=y(o,i,t),l=y(n,r,t),s=t.ownerDocument.createDocumentFragment();l!==d;)a=l.nextSibling,s.appendChild(l),l=a;return e.setStart(t,d?Z.call(t.childNodes,d):t.childNodes.length),e.collapse(!0),g(t),s},lt=function(e){ut(e),dt(e);var t=pt(e),n=ht(e);t&&n&&t!==n&&S(t,n,e),t&&g(t);var r=e.endContainer.ownerDocument.body,o=r.firstChild;o&&"BR"!==o.nodeName||(g(r),e.selectNodeContents(r.firstChild));var i=e.collapsed;ct(e),i&&e.collapse(!0)},st=function(e,t){for(var n=!0,r=t.childNodes,o=r.length;o--;)if(!a(r[o])){n=!1;break}if(e.collapsed||lt(e),ct(e),n)at(e,t),e.collapse(!1);else{for(var i,d,l=y(e.startContainer,e.startOffset,e.startContainer.ownerDocument.body),s=l.previousSibling,f=s,c=f.childNodes.length,p=l,h=0,C=l.parentNode;(i=f.lastChild)&&i.nodeType===x&&"BR"!==i.nodeName;)f=i,c=f.childNodes.length;for(;(i=p.firstChild)&&i.nodeType===x&&"BR"!==i.nodeName;)p=i;for(;(i=t.firstChild)&&a(i);)f.appendChild(i);for(;(i=t.lastChild)&&a(i);)p.insertBefore(i,p.firstChild),h+=1;for(d=t;d=u(d);)g(d);C.insertBefore(t,l),d=l.previousSibling,l.textContent?O(l):C.removeChild(l),l.parentNode||(p=d,h=N(p)),s.textContent?O(s):(f=s.nextSibling,c=0,C.removeChild(s)),e.setStart(f,c),e.setEnd(p,h),ct(e)}},ft=function(e,t,n){var r=t.ownerDocument.createRange();if(r.selectNode(t),n){var o=e.compareBoundaryPoints(U,r)>-1,i=1>e.compareBoundaryPoints(w,r);return!o&&!i}var a=1>e.compareBoundaryPoints(R,r),d=e.compareBoundaryPoints(P,r)>-1;return a&&d},ct=function(e){for(var t,n=e.startContainer,r=e.startOffset,o=e.endContainer,a=e.endOffset;n.nodeType!==D&&(t=n.childNodes[r],t&&!i(t));)n=t,r=0;if(a)for(;o.nodeType!==D&&(t=o.childNodes[a-1],t&&!i(t));)o=t,a=N(o);else for(;o.nodeType!==D&&(t=o.firstChild,t&&!i(t));)o=t;e.collapsed?(e.setStart(o,a),e.setEnd(n,r)):(e.setStart(n,r),e.setEnd(o,a))},ut=function(e,t){var n,r=e.startContainer,o=e.startOffset,i=e.endContainer,a=e.endOffset;for(t||(t=e.commonAncestorContainer);r!==t&&!o;)n=r.parentNode,o=Z.call(n.childNodes,r),r=n;for(;i!==t&&a===N(i);)n=i.parentNode,a=Z.call(n.childNodes,i)+1,i=n;e.setStart(r,o),e.setEnd(i,a)},pt=function(e){var t,n=e.startContainer;return a(n)?t=c(n):d(n)?t=n:(t=nt(n,e.startOffset),t=u(t)),t&&ft(e,t,!0)?t:null},ht=function(e){var t,n,r=e.endContainer;if(a(r))t=c(r);else if(d(r))t=r;else{if(t=rt(r,e.endOffset),!t)for(t=r.ownerDocument.body;n=t.lastChild;)t=n;t=c(t)}return t&&ft(e,t,!0)?t:null},Nt=function(e){for(var t,n,r=e.startContainer,o=e.startOffset;a(r);){if(o)return!1;t=r.parentNode,o=Z.call(t.childNodes,r),r=t}for(;o&&(n=r.childNodes[o-1])&&(""===n.data||"BR"===n.nodeName);)o-=1;return!o},Ct=function(e){for(var t,n,r=e.endContainer,o=e.endOffset,i=N(r);a(r);){if(o!==i)return!1;t=r.parentNode,o=Z.call(t.childNodes,r)+1,r=t,i=r.childNodes.length}for(;i>o&&(n=r.childNodes[o])&&(""===n.data||"BR"===n.nodeName);)o+=1;return o===i},vt=function(e){var t,n=pt(e),r=ht(e);n&&r&&(t=n.parentNode,e.setStart(t,Z.call(t.childNodes,n)),t=r.parentNode,e.setEnd(t,Z.call(t.childNodes,r)+1))},mt={focus:1,blur:1,pathChange:1,select:1,input:1,undoStateChange:1},gt={},yt=function(e,t){var n,r,o,i=gt[e];if(i)for(t||(t={}),t.type!==e&&(t.type=e),i=i.slice(),n=0,r=i.length;r>n;n+=1){o=i[n];try{o.handleEvent?o.handleEvent(t):o(t)}catch(a){a.details="Squire: fireEvent error. Event type: "+e,tt.didError(a)}}},Tt=function(e){yt(e.type,e)},St=function(t,n){var r=gt[t];return n?(r||(r=gt[t]=[],mt[t]||e.addEventListener(t,Tt,!1)),r.push(n),void 0):(tt.didError({name:"Squire: addEventListener with null or undefined fn",message:"Event type: "+t}),void 0)},Ot=function(t,n){var r,o=gt[t];if(o){for(r=o.length;r--;)o[r]===n&&o.splice(r,1);o.length||(delete gt[t],mt[t]||e.removeEventListener(t,Tt,!1))}},bt=function(t,n,r,o){if(t instanceof Range)return t.cloneRange();var i=e.createRange();return i.setStart(t,n),r?i.setEnd(r,o):i.setEnd(t,n),i},Et=k.getSelection(),xt=null,Dt=function(e){e&&(z&&k.focus(),Et.removeAllRanges(),Et.addRange(e))},Bt=function(){if(Et.rangeCount){xt=Et.getRangeAt(0).cloneRange();var e=xt.startContainer,t=xt.endContainer;try{e&&i(e)&&xt.setStartBefore(e),t&&i(t)&&xt.setEndBefore(t)}catch(n){tt.didError({name:"Squire#getSelection error",message:"Starts: "+e.nodeName+"\nEnds: "+t.nodeName})}}return xt};W&&k.addEventListener("beforedeactivate",Bt,!0);var At,It,Lt=null,Rt=!0,wt=!1,Pt=function(){Rt=!0,wt=!1,Ot("keydown",Pt)},Ut=function(){if(Rt){var e,t=Lt;if(Lt=null,t.parentNode){for(;(e=t.data.indexOf(""))>-1;)t.deleteData(e,1);t.data||t.nextSibling||t.previousSibling||!a(t.parentNode)||C(t.parentNode)}}},kt=function(e){Lt&&(Rt=!0,Ut()),wt||(St("keydown",Pt),wt=!0),Rt=!1,Lt=e},Vt="",Ht=function(e,t){Lt&&!t&&Ut(e);var n,r=e.startContainer,o=e.endContainer;(t||r!==At||o!==It)&&(At=r,It=o,n=r&&o?r===o?h(o):"(selection)":"",Vt!==n&&(Vt=n,yt("pathChange",{path:n}))),r!==o&&yt("select")},zt=function(){Ht(Bt())};St("keyup",zt),St("mouseup",zt);var Mt=function(){F&&V.focus(),k.focus()},Ft=function(){F&&V.blur(),top.focus()};k.addEventListener("focus",Tt,!1),k.addEventListener("blur",Tt,!1);var Kt,qt,Gt,Qt,Yt=function(){return V.innerHTML},$t=function(e){var t=V;t.innerHTML=e;do g(t);while(t=u(t))},jt=function(e,t){if(t||(t=Bt()),t.collapse(!0),a(e))at(t,e),t.setStartAfter(e);else{for(var n,r,o=pt(t)||V;o!==V&&!o.nextSibling;)o=o.parentNode;o!==V&&(n=o.parentNode,r=y(n,o.nextSibling,V)),r?(V.insertBefore(e,r),t.setStart(r,0),t.setStart(r,0),ct(t)):(V.appendChild(e),V.appendChild(g(b("div"))),t.setStart(e,0),t.setEnd(e,0)),Mt(),Dt(t),Ht(t)}},Wt="squire-selection-start",Xt="squire-selection-end",Zt=function(e){var t,n=b("INPUT",{id:Wt,type:"hidden"}),r=b("INPUT",{id:Xt,type:"hidden"});at(e,n),e.collapse(!1),at(e,r),n.compareDocumentPosition(r)&E&&(n.id=Xt,r.id=Wt,t=n,n=r,r=t),e.setStartAfter(n),e.setEndBefore(r)},_t=function(t){var n=e.getElementById(Wt),r=e.getElementById(Xt);if(n&&r){var o,i=n.parentNode,a=r.parentNode,d={startContainer:i,endContainer:a,startOffset:Z.call(i.childNodes,n),endOffset:Z.call(a.childNodes,r)};i===a&&(d.endOffset-=1),C(n),C(r),T(i,d),i!==a&&T(a,d),t||(t=e.createRange()),t.setStart(d.startContainer,d.startOffset),t.setEnd(d.endContainer,d.endOffset),o=t.collapsed,ct(t),o&&t.collapse(!0)}return t||null},Jt=function(){Qt&&(Qt=!1,yt("undoStateChange",{canUndo:!0,canRedo:!1})),yt("input")};St("keyup",function(e){var t=e.keyCode;e.ctrlKey||e.metaKey||e.altKey||!(16>t||t>20)||!(33>t||t>45)||Jt()});var en=function(e){Qt||(Kt+=1,Gt>Kt&&(qt.length=Gt=Kt),e&&Zt(e),qt[Kt]=Yt(),Gt+=1,Qt=!0)},tn=function(){if(0!==Kt||!Qt){en(Bt()),Kt-=1,$t(qt[Kt]);var e=_t();e&&Dt(e),Qt=!0,yt("undoStateChange",{canUndo:0!==Kt,canRedo:!0}),yt("input")}},nn=function(){if(Gt>Kt+1&&Qt){Kt+=1,$t(qt[Kt]);var e=_t();e&&Dt(e),yt("undoStateChange",{canUndo:!0,canRedo:Gt>Kt+1}),yt("input")}},rn=function(e,n,r){if(e=e.toUpperCase(),n||(n={}),!r&&!(r=Bt()))return!1;var o,i,a=r.commonAncestorContainer;if(p(a,e,n))return!0;if(a.nodeType===D)return!1;o=new t(a,A,function(e){return ft(r,e,!0)?I:L},!1);for(var d=!1;i=o.nextNode();){if(!p(i,e,n))return!1;d=!0}return d},on=function(e,n,r){var o,i,a,d,l,s,f,c;if(r.collapsed)o=g(b(e,n)),at(r,o),r.setStart(o.firstChild,o.firstChild.length),r.collapse(!0);else{i=new t(r.commonAncestorContainer,A,function(e){return ft(r,e,!0)?I:L},!1),l=0,s=0,f=i.currentNode=r.startContainer,f.nodeType!==D&&(f=i.nextNode());do c=!p(f,e,n),f===r.endContainer&&(c&&f.length>r.endOffset?f.splitText(r.endOffset):s=r.endOffset),f===r.startContainer&&(c&&r.startOffset?f=f.splitText(r.startOffset):l=r.startOffset),c&&(o=b(e,n),v(f,o),o.appendChild(f),s=f.length),d=f,a||(a=d);while(f=i.nextNode());r=bt(a,l,d,s)}return r},an=function(t,n,o,i){Zt(o);var d;o.collapsed&&(j?(d=e.createTextNode(""),kt(d)):d=e.createTextNode(""),at(o,d));for(var l=o.commonAncestorContainer;a(l);)l=l.parentNode;var s=o.startContainer,f=o.startOffset,c=o.endContainer,u=o.endOffset,p=[],h=function(e,t){if(!ft(o,e,!1)){var n,r,i=e.nodeType===D;if(!ft(o,e,!0))return"INPUT"===e.nodeName||i&&!e.data||p.push([t,e]),void 0;if(i)e===c&&u!==e.length&&p.push([t,e.splitText(u)]),e===s&&f&&(e.splitText(f),p.push([t,e]));else for(n=e.firstChild;n;n=r)r=n.nextSibling,h(n,t)}},N=Array.prototype.filter.call(l.getElementsByTagName(t),function(e){return ft(o,e,!0)&&r(e,t,n)});i||N.forEach(function(e){h(e,e)}),p.forEach(function(e){var t=e[0].cloneNode(!1),n=e[1];v(n,t),t.appendChild(n)}),N.forEach(function(e){v(e,m(e))}),_t(o),d&&o.collapse(!1);var C={startContainer:o.startContainer,startOffset:o.startOffset,endContainer:o.endContainer,endOffset:o.endOffset};return T(l,C),o.setStart(C.startContainer,C.startOffset),o.setEnd(C.endContainer,C.endOffset),o},dn=function(e,t,n,r){(n||(n=Bt()))&&(en(n),_t(n),t&&(n=an(t.tag.toUpperCase(),t.attributes||{},n,r)),e&&(n=on(e.tag.toUpperCase(),e.attributes||{},n)),Dt(n),Ht(n,!0),Jt())},ln={DIV:"DIV",PRE:"DIV",H1:"DIV",H2:"DIV",H3:"DIV",H4:"DIV",H5:"DIV",H6:"DIV",P:"DIV",DT:"DD",DD:"DT",LI:"LI"},sn=function(e,t,n){var r=ln[e.nodeName],o=y(t,n,e.parentNode);return o.nodeName!==r&&(e=b(r),e.className="rtl"===o.dir?"dir-rtl":"",e.dir=o.dir,v(o,e),e.appendChild(m(o)),o=e),o},fn=function(e,t,n){if(n||(n=Bt())){t&&(en(n),_t(n));var r=pt(n),o=ht(n);if(r&&o)do if(e(r)||r===o)break;while(r=u(r));t&&(Dt(n),Ht(n,!0),Jt())}},cn=function(e,t){if(t||(t=Bt())){G||V.setAttribute("contenteditable","false"),Qt?Zt(t):en(t),vt(t),ut(t,V);var n=dt(t,V);at(t,e(n)),t.endOffset<t.endContainer.childNodes.length&&O(t.endContainer.childNodes[t.endOffset]),O(t.startContainer.childNodes[t.startOffset]),G||V.setAttribute("contenteditable","true"),_t(t),Dt(t),Ht(t,!0),Jt()}},un=function(e){return b("BLOCKQUOTE",[e])},pn=function(e){var t=e.querySelectorAll("blockquote");return Array.prototype.filter.call(t,function(e){return!p(e.parentNode,"BLOCKQUOTE")}).forEach(function(e){v(e,m(e))}),e},hn=function(e){for(var t,n=e.querySelectorAll("blockquote"),r=n.length;r--;)t=n[r],v(t,m(t));return e},Nn=function(e,t){var n,r,o,i,a,s;for(n=0,r=e.length;r>n;n+=1)o=e[n],i=o.nodeName,d(o)?"LI"!==i&&(s=b("LI",{"class":"rtl"===o.dir?"dir-rtl":"",dir:o.dir},[m(o)]),o.parentNode.nodeName===t?v(o,s):(a=o.previousSibling)&&a.nodeName===t?(a.appendChild(s),C(o),n-=1,r-=1):v(o,b(t,[s]))):l(o)&&(i!==t&&/^[DOU]L$/.test(i)?v(o,b(t,[m(o)])):Nn(o.childNodes,t))},Cn=function(e){return Nn(e.childNodes,"UL"),e},vn=function(e){return Nn(e.childNodes,"OL"),e},mn=function(e){var t=e.querySelectorAll("UL, OL");return Array.prototype.filter.call(t,function(e){return!p(e.parentNode,"UL")&&!p(e.parentNode,"OL")}).forEach(function(e){for(var t,n=m(e),r=n.childNodes,o=r.length;o--;)t=r[o],"LI"===t.nodeName&&n.replaceChild(b("DIV",{"class":"rtl"===t.dir?"dir-rtl":"",dir:t.dir},[m(t)]),t);v(e,n)}),e},gn=/\b((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])|(?:[\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,4}))/i,yn=function(e){for(var n,r,o,i,a,d,l,s=e.ownerDocument,f=new t(e,A,function(e){return p(e,"A")?L:I},!1);n=f.nextNode();)if(r=n.data.split(gn),i=r.length,i>1){for(d=n.parentNode,l=n.nextSibling,o=0;i>o;o+=1)a=r[o],o?(o%2?(n=s.createElement("A"),n.textContent=a,n.href=/@/.test(a)?"mailto:"+a:/^(?:ht|f)tps?:/.test(a)?a:"http://"+a):n=s.createTextNode(a),l?d.insertBefore(n,l):d.appendChild(n)):n.data=a;f.currentNode=n}},Tn=/^(?: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)$/,Sn={1:10,2:13,3:16,4:18,5:24,6:32,7:48},On={backgroundColor:{regexp:X,replace:function(e){return b("SPAN",{"class":"highlight",style:"background-color: "+e})}},color:{regexp:X,replace:function(e){return b("SPAN",{"class":"colour",style:"color:"+e})}},fontWeight:{regexp:/^bold/i,replace:function(){return b("B")}},fontStyle:{regexp:/^italic/i,replace:function(){return b("I")}},fontFamily:{regexp:X,replace:function(e){return b("SPAN",{"class":"font",style:"font-family:"+e})}},fontSize:{regexp:X,replace:function(e){return b("SPAN",{"class":"size",style:"font-size:"+e})}}},bn={SPAN:function(e,t){var n,r,o,i,a,d,l=e.style;for(n in On)r=On[n],o=l[n],o&&r.regexp.test(o)&&(d=r.replace(o),i&&i.appendChild(d),i=d,a||(a=d));return a&&(i.appendChild(m(e)),t.replaceChild(a,e)),i||e},STRONG:function(e,t){var n=b("B");return t.replaceChild(n,e),n.appendChild(m(e)),n},EM:function(e,t){var n=b("I");return t.replaceChild(n,e),n.appendChild(m(e)),n},FONT:function(e,t){var n,r,o,i,a=e.face,d=e.size;return a&&(n=b("SPAN",{"class":"font",style:"font-family:"+a})),d&&(r=b("SPAN",{"class":"size",style:"font-size:"+Sn[d]+"px"}),n&&n.appendChild(r)),i=n||r||b("SPAN"),o=r||n||i,t.replaceChild(i,e),o.appendChild(m(e)),o},TT:function(e,t){var n=b("SPAN",{"class":"font",style:'font-family:menlo,consolas,"courier new",monospace'});return t.replaceChild(n,e),n.appendChild(m(e)),n}},En=function(e){for(var t,n=e.childNodes,r=n.length;r--;)t=n[r],t.nodeType===x&&"IMG"!==t.nodeName&&(En(t),a(t)&&!t.firstChild&&e.removeChild(t))},xn=function(e,t){var n,r,o,i,d,l,s,f=e.childNodes;for(n=0,r=f.length;r>n;n+=1)if(o=f[n],i=o.nodeName,d=o.nodeType,l=bn[i],d===x){if(s=o.childNodes.length,l)o=l(o,e);else{if(!Tn.test(i)&&!a(o)){n-=1,r+=s-1,e.replaceChild(m(o),o);continue}!t&&o.style.cssText&&o.removeAttribute("style")}s&&xn(o,t)}else d===D&&(X.test(o.data)||n>0&&a(f[n-1])||r>n+1&&a(f[n+1]))||(e.removeChild(o),n-=1,r-=1);return e},Dn=function(e,t){var n,r,o,i,d=e.childNodes,l=null;for(n=0,r=d.length;r>n;n+=1)o=d[n],i="BR"===o.nodeName,!i&&a(o)?(l||(l=b(t)),l.appendChild(o),n-=1,r-=1):(i||l)&&(l||(l=b(t)),g(l),i?e.replaceChild(l,o):(e.insertBefore(l,o),n+=1,r+=1),l=null);return l&&e.appendChild(g(l)),e},Bn=function(e){return(e.nodeType===x?"BR"===e.nodeName:X.test(e.data))?I:L},An=function(e){for(var n,r=e.parentNode;a(r);)r=r.parentNode;return n=new t(r,B|A,Bn),n.currentNode=e,!!n.nextNode()},In=function(e){var t,n,r,o=e.querySelectorAll("BR"),i=[],l=o.length;for(t=0;l>t;t+=1)i[t]=An(o[t]);for(;l--;)if(n=o[l],r=n.parentNode){for(;a(r);)r=r.parentNode;d(r)&&ln[r.nodeName]?(i[l]&&sn(r,n.parentNode,n),C(n)):Dn(r,"DIV")}},Ln=function(){try{g(V)}catch(e){tt.didError(e)}};St(K?"beforecut":"cut",function(){var e=Bt();en(e),_t(e),Dt(e),setTimeout(Ln,0)});var Rn=!1;St(K?"beforepaste":"paste",function(e){if(!Rn){var t,n,r=e.clipboardData,o=r&&r.items,i=!1,a=!1;if(o){for(t=o.length;t--;){if(n=o[t].type,"text/html"===n){a=!1;break}/^image\/.*/.test(n)&&(a=!0)}if(a)return e.preventDefault(),yt("dragover",{dataTransfer:r,preventDefault:function(){i=!0}}),i&&yt("drop",{dataTransfer:r}),void 0}Rn=!0;var d=Bt(),l=d.startContainer,s=d.startOffset,f=d.endContainer,c=d.endOffset,p=b("DIV",{style:"position: absolute; overflow: hidden; top:"+(V.scrollTop+30)+"px; left: 0; width: 1px; height: 1px;"});V.appendChild(p),d.selectNodeContents(p),Dt(d),setTimeout(function(){try{var e=m(C(p)),t=e.firstChild,n=bt(l,s,f,c);if(t){t===e.lastChild&&"DIV"===t.nodeName&&e.replaceChild(m(t),t),e.normalize(),yn(e),xn(e,!1),In(e),En(e);for(var r=e,o=!0;r=u(r);)g(r);yt("willPaste",{fragment:e,preventDefault:function(){o=!1}}),o&&(st(n,e),Jt(),n.collapse(!1))}Dt(n),Ht(n,!0),Rn=!1}catch(i){tt.didError(i)}},0)}});var wn={8:"backspace",9:"tab",13:"enter",32:"space",37:"left",39:"right",46:"delete"},Pn=function(e){return function(t){t.preventDefault(),e()}},Un=function(e){return function(t){t.preventDefault();var n=Bt();rn(e,null,n)?dn(null,{tag:e},n):dn({tag:e},null,n)}},kn=function(){try{var e,t=Bt(),n=t.startContainer;if(n.nodeType===D&&(n=n.parentNode),a(n)&&!n.textContent){do e=n.parentNode;while(a(e)&&!e.textContent&&(n=e));t.setStart(e,Z.call(e.childNodes,n)),t.collapse(!0),e.removeChild(n),d(e)||(e=c(e)),g(e),ct(t),Dt(t),Ht(t)}}catch(r){tt.didError(r)}};q&&St("keyup",function(){var e=V.firstChild;"P"===e.nodeName&&(Zt(Bt()),v(e,b("DIV",[m(e)])),Dt(_t()))});var Vn={enter:function(t){t.preventDefault();var n=Bt();if(n){en(n),yn(n.startContainer),_t(n),n.collapsed||lt(n);var r,o=pt(n),i=o?o.nodeName:"DIV",a=ln[i];if(!o)return at(n,b("BR")),n.collapse(!1),Dt(n),Ht(n,!0),Jt(),void 0;var d,l=n.startContainer,s=n.startOffset;if(a||(l===o&&(l=s?l.childNodes[s-1]:null,s=0,l&&("BR"===l.nodeName?l=l.nextSibling:s=N(l),l&&"BR"!==l.nodeName||(d=g(b("DIV")),l?o.replaceChild(d,l):o.appendChild(d),l=d))),Dn(o,"DIV"),a="DIV",l||(l=o.firstChild),n.setStart(l,s),n.setEnd(l,s),o=pt(n)),!o.textContent){if(p(o,"UL")||p(o,"OL"))return cn(mn,n);if(p(o,"BLOCKQUOTE"))return cn(hn,n)}for(r=sn(o,l,s);r.nodeType===x;){var f,c=r.firstChild;if("A"!==r.nodeName){for(;c&&c.nodeType===D&&!c.data&&(f=c.nextSibling,f&&"BR"!==f.nodeName);)C(c),c=f;if(!c||"BR"===c.nodeName||c.nodeType===D&&!G)break;r=c}else v(r,m(r)),r=c}n=bt(r,0),Dt(n),Ht(n,!0),r.nodeType===D&&(r=r.parentNode),r.offsetTop+r.offsetHeight>(e.documentElement.scrollTop||V.scrollTop)+V.offsetHeight&&r.scrollIntoView(!1),Jt()}},backspace:function(e){var t=Bt();if(t.collapsed)if(Nt(t)){en(t),_t(t),e.preventDefault();var n=pt(t),r=n&&c(n);if(r){if(!r.isContentEditable)return C(r),void 0;for(S(r,n,t),n=r.parentNode;n&&!n.nextSibling;)n=n.parentNode;n&&(n=n.nextSibling)&&O(n),Dt(t)}else if(n){if(p(n,"UL")||p(n,"OL"))return cn(mn,t);if(p(n,"BLOCKQUOTE"))return cn(pn,t);Dt(t),Ht(t,!0)}}else{var o=t.startContainer.data||"";X.test(o.charAt(t.startOffset-1))||(en(t),_t(t),Dt(t)),setTimeout(kn,0)}else en(t),_t(t),e.preventDefault(),lt(t),Dt(t),Ht(t,!0)},"delete":function(e){var t=Bt();if(t.collapsed)if(Ct(t)){en(t),_t(t),e.preventDefault();var n=pt(t),r=n&&u(n);if(r){if(!r.isContentEditable)return C(r),void 0;for(S(n,r,t),r=n.parentNode;r&&!r.nextSibling;)r=r.parentNode;r&&(r=r.nextSibling)&&O(r),Dt(t),Ht(t,!0)}}else{var o=t.startContainer.data||"";X.test(o.charAt(t.startOffset))||(en(t),_t(t),Dt(t)),setTimeout(kn,0)}else en(t),_t(t),e.preventDefault(),lt(t),Dt(t),Ht(t,!0)},space:function(){var e=Bt();en(e),yn(e.startContainer),_t(e),Dt(e)}};M&&F&&Et.modify&&(Vn["meta-left"]=function(e){e.preventDefault(),Et.modify("move","backward","lineboundary")},Vn["meta-right"]=function(e){e.preventDefault(),Et.modify("move","forward","lineboundary")}),Vn[Y+"b"]=Un("B"),Vn[Y+"i"]=Un("I"),Vn[Y+"u"]=Un("U"),Vn[Y+"y"]=Pn(nn),Vn[Y+"z"]=Pn(tn),Vn[Y+"shift-z"]=Pn(nn),St(G?"keypress":"keydown",function(e){var t=e.keyCode,n=wn[t]||String.fromCharCode(t).toLowerCase(),r="";G&&46===e.which&&(n="."),t>111&&124>t&&(n="f"+(t-111)),e.altKey&&(r+="alt-"),e.ctrlKey&&(r+="ctrl-"),e.metaKey&&(r+="meta-"),e.shiftKey&&(r+="shift-"),n=r+n,Vn[n]&&Vn[n](e)});var Hn=function(e){return function(){return e.apply(null,arguments),this}},zn=function(e,t,n){return function(){return e(t,n),Mt(),this}};tt=k.editor={didError:function(e){console.log(e)},addEventListener:Hn(St),removeEventListener:Hn(Ot),focus:Hn(Mt),blur:Hn(Ft),getDocument:function(){return e},addStyles:function(t){if(t){var n=e.documentElement.firstChild,r=b("STYLE",{type:"text/css"});r.styleSheet?(n.appendChild(r),r.styleSheet.cssText=t):(r.appendChild(e.createTextNode(t)),n.appendChild(r))}return this},getHTML:function(e){var t,n,r,o,i,a=[];if(e&&(i=Bt())&&Zt(i),$)for(t=V;t=u(t);)t.textContent||t.querySelector("BR")||(n=b("BR"),t.appendChild(n),a.push(n));if(r=Yt(),$)for(o=a.length;o--;)C(a[o]);return i&&_t(i),r},setHTML:function(t){var n,r=e.createDocumentFragment(),o=b("DIV");o.innerHTML=t,r.appendChild(m(o)),xn(r,!0),In(r),Dn(r,"DIV");for(var i=r;i=u(i);)g(i);for(;n=V.lastChild;)V.removeChild(n);V.appendChild(r),g(V),Kt=-1,qt=[],Gt=0,Qt=!1;var a=_t()||bt(V.firstChild,0);return en(a),_t(a),W?xt=a:Dt(a),Ht(a,!0),this},getSelectedText:function(){return it(Bt())},insertElement:Hn(jt),insertImage:function(e){var t=b("IMG",{src:e});return jt(t),t},getPath:function(){return Vt},getSelection:Bt,setSelection:Hn(Dt),undo:Hn(tn),redo:Hn(nn),hasFormat:rn,changeFormat:Hn(dn),bold:zn(dn,{tag:"B"}),italic:zn(dn,{tag:"I"}),underline:zn(dn,{tag:"U"}),removeBold:zn(dn,null,{tag:"B"}),removeItalic:zn(dn,null,{tag:"I"}),removeUnderline:zn(dn,null,{tag:"U"}),makeLink:function(t){t=encodeURI(t);var n=Bt();if(n.collapsed){var r=t.indexOf(":")+1;if(r)for(;"/"===t[r];)r+=1;n._insertNode(e.createTextNode(t.slice(r)))}return dn({tag:"A",attributes:{href:t}},{tag:"A"},n),Mt(),this},removeLink:function(){return dn(null,{tag:"A"},Bt(),!0),Mt(),this},setFontFace:function(e){return dn({tag:"SPAN",attributes:{"class":"font",style:"font-family: "+e+", sans-serif;"}},{tag:"SPAN",attributes:{"class":"font"}}),Mt(),this},setFontSize:function(e){return dn({tag:"SPAN",attributes:{"class":"size",style:"font-size: "+("number"==typeof e?e+"px":e)}},{tag:"SPAN",attributes:{"class":"size"}}),Mt(),this},setTextColour:function(e){return dn({tag:"SPAN",attributes:{"class":"colour",style:"color: "+e}},{tag:"SPAN",attributes:{"class":"colour"}}),Mt(),this},setHighlightColour:function(e){return dn({tag:"SPAN",attributes:{"class":"highlight",style:"background-color: "+e}},{tag:"SPAN",attributes:{"class":"highlight"}}),Mt(),this},setTextAlignment:function(e){return fn(function(t){t.className=(t.className.split(/\s+/).filter(function(e){return!/align/.test(e)}).join(" ")+" align-"+e).trim(),t.style.textAlign=e},!0),Mt(),this},setTextDirection:function(e){return fn(function(t){t.className=(t.className.split(/\s+/).filter(function(e){return!/dir/.test(e)}).join(" ")+" dir-"+e).trim(),t.dir=e},!0),Mt(),this},forEachBlock:Hn(fn),modifyBlocks:Hn(cn),increaseQuoteLevel:zn(cn,un),decreaseQuoteLevel:zn(cn,pn),makeUnorderedList:zn(cn,Cn),makeOrderedList:zn(cn,vn),removeList:zn(cn,mn)},V.setAttribute("contenteditable","true"),tt.setHTML(""),k.onEditorLoad&&(k.onEditorLoad(k.editor),k.onEditorLoad=null)})(document);