From 52e517b376ac31e441fb21b5cf934f3c1118b5a4 Mon Sep 17 00:00:00 2001 From: Neil Jenkins Date: Thu, 24 Jan 2013 11:54:44 +1100 Subject: [PATCH] Catch all errors to allow logging. * All errors will be caught and passed to the editor.didError fn. This can be overridden to do something useful, like logging them to the server. --- build/ie8.js | 2 +- build/squire.js | 2 +- source/Editor.js | 170 +++++++++++++++++++++++++-------------------- source/ie8types.js | 2 +- 4 files changed, 99 insertions(+), 77 deletions(-) diff --git a/build/ie8.js b/build/ie8.js index 0d04ed1..2c07ccf 100644 --- a/build/ie8.js +++ b/build/ie8.js @@ -1 +1 @@ -/* Copyright © 2011-2012 by Neil Jenkins. Licensed under the MIT license. */(function(e){Array.prototype.indexOf=function(e,t){var n=this.length;for(var r=t<0?Math.max(0,n+t):t||0;r>>0;if(typeof e!="function")throw new TypeError;for(var r=0;rf?1:0;l=o;while(c=l.parentNode){if(c===a)return e.call(c.childNodes,l)a.sourceIndex?1:0}},document.createRange=function(){return new Range};var o=function(e,t){return e===t||i(e,t)},u=function(e){var t=e.nodeType;return t===3||t===4||t===8},a=function(e,t){this.node=e,this.offset=t},f=function(e){var t=e.parentElement(),n,r,i,o;return n=e.duplicate(),n.collapse(!0),r=n.parentElement(),n=e.duplicate(),n.collapse(!1),i=n.parentElement(),o=r===i?r:s(r,i),o===t?o:s(t,o)},l=function(t,n,r,i){var s=t.duplicate();s.collapse(r);var f=s.parentElement();o(n,f)||(f=n);if(!f.canHaveHTML)return new a(f.parentNode,e.call(f.parentNode.childNodes,f));var l=document.createElement("span"),c=r?"StartToStart":"StartToEnd",h,p,d,v,m;do f.insertBefore(l,l.previousSibling),s.moveToElementText(l),h=s.compareEndPoints(c,t);while(h>0&&l.previousSibling);m=l.nextSibling;if(h===-1&&m&&u(m)){s.setEndPoint(r?"EndToStart":"EndToEnd",t);var g;if(/[\r\n]/.test(m.data)||/[\r\n]/.test(s.text)){var y=s.duplicate(),b=y.text.replace(/\r\n/g,"\r").length;g=y.moveStart("character",b);while((h=y.compareEndPoints("StartToEnd",y))===-1)g+=1,y.moveStart("character",1)}else g=s.text.length;v=new a(m,g)}else p=(i||!r)&&l.previousSibling,d=(i||r)&&l.nextSibling,d&&u(d)?v=new a(d,0):p&&u(p)?v=new a(p,p.data.length):v=new a(f,e.call(f.childNodes,l));return l.parentNode.removeChild(l),v},c=function(e,t){var n,r,i=e.offset,s=document,o,a,f=s.body.createTextRange(),l=u(e.node);return l?(n=e.node,r=n.parentNode):(a=e.node.childNodes,n=ie?Math.max(0,n+e):e||0;n>r;r+=1)if(this[r]===t)return r;return-1},Array.prototype.forEach=function(t,e){var n=this.length>>>0;if("function"!=typeof t)throw new TypeError;for(var r=0;n>r;r+=1)t.call(e,this[r],r,this)},Array.prototype.filter=function(t,e){for(var n=[],r=0,o=this.length;o>r;r+=1){var i=this[r];t.call(e,i,r,this)&&n.push(i)}return n},Object.keyOf=function(t,e){for(var n in t)if(t[n]===e)return n},Date.now=function(){return+new Date},String.prototype.trim=function(){for(var t=this.replace(/^\s\s*/,""),e=/\s/,n=t.length;e.test(t.charAt(n-=1)););return t.slice(0,n+1)}})(),function(){var t=document;window.ie=8,t.defaultView=window;var e={focus:"focusin",blur:"focusout"},n=function(){return!0},r=function(){return!1},o="altKey ctrlKey metaKey shiftKey clientX clientY charCode keyCode".split(" "),i=function(t){for(var n,r=t.type,i=document,a=t.srcElement||i,s=(a.ownerDocument||i).documentElement,c=o.length;c--;)n=o[c],this[n]=t[n];"propertychange"===r&&(r="INPUT"===a.nodeName&&"text"!==a.type&&"password"!==a.type?"change":"input"),this.type=Object.keyOf(e,r)||r,this.target=a,this.pageX=t.clientX+s.scrollLeft,this.pageY=t.clientY+s.scrollTop,t.button&&(this.button=4&t.button?1:2&t.button?2:0,this.which=this.button+1),this.relatedTarget=t.fromElement===a?t.toElement:t.fromElement,this._event=t};i.prototype={constructor:i,isEvent:!0,preventDefault:function(){this.isDefaultPrevented=n,this._event.returnValue=!1},stopPropagation:function(){this.isPropagationStopped=n,this._event.cancelBubble=!0},isDefaultPrevented:r,isPropagationStopped:r},[t,Element.prototype].forEach(function(t){t.addEventListener=function(t,n){var r=n._ie_handleEvent||(n._ie_handleEvent=function(){var t=new i(window.event);"object"==typeof n?n.handleEvent(t):n.call(this,t)}),o=/paste|cut/.test(t)?this.body||this:this;n._ie_registeredCount=(n._ie_registeredCount||0)+1,o.attachEvent("on"+(e[t]||t),r)},t.addEventListener.isFake=!0,t.removeEventListener=function(t,n){var r=n._ie_handleEvent,o=/paste|cut/.test(t)?this.body||this:this;(n._ie_registeredCount-=1)||delete n._ie_handleEvent,r&&o.detachEvent("on"+(e[t]||t),r)},t.removeEventListener.isFake=!0}),t.defaultView.addEventListener=function(e,n,r){return t.addEventListener(e,n,r)},Object.defineProperty(Element.prototype,"textContent",{get:function(){return this.innerText},set:function(t){this.innerText=t}}),Element.prototype.compareDocumentPosition=function(t){1!==t.nodeType&&(t=t.parentNode);var e=this,n=e!==t,r=e.sourceIndex,o=t.sourceIndex;return(n&&e.contains(t)?16:0)+(n&&t.contains(e)?8:0)+(o>r?4:0)+(r>o?2:0)},HTMLDocument.prototype.normalize=function(){for(var t,e=this.childNodes,n=e.length;n--;)t=e[n],1===t.nodeType&&t.normalize()}}();var Range;(function(){var t=Array.prototype.indexOf,e=0,n=1,r=3,o=function(t,e){for(;e=e.parentNode;)if(t===e)return!0;return!1},i=function(t,e){var n,r,i,a,s;if(t===e||o(t,e))n=t;else if(o(e,t))n=e;else{for(r=[],i=[];t=t.parentNode;)r.push(t);for(;e=e.parentNode;)i.push(e);for(a=r.length,s=i.length;a--&&s--;)if(r[a]!==i[s]){n=r[a+1];break}n||(n=-1===a?r[0]:i[0])}return n};Range=function(t,e,n,r){t=t||document,e=e||0,this.startContainer=t,this.startOffset=e,this.endContainer=n||t,this.endOffset=void 0!==r?r:e,this._updateCollapsedAndAncestor()},Range.prototype={constructor:Range,_updateCollapsedAndAncestor:function(){this.collapsed=this.startContainer===this.endContainer&&this.startOffset===this.endOffset,this.commonAncestorContainer=i(this.startContainer,this.endContainer)},setStart:function(t,e){this.startContainer=t,this.startOffset=e,this._updateCollapsedAndAncestor()},setEnd:function(t,e){this.endContainer=t,this.endOffset=e,this._updateCollapsedAndAncestor()},setStartAfter:function(e){var n=e.parentNode;this.setStart(n,t.call(n.childNodes,e)+1)},setEndBefore:function(e){var n=e.parentNode;this.setEnd(n,t.call(n.childNodes,e))},selectNode:function(e){var n=e.parentNode,r=t.call(n.childNodes,e);this.setStart(n,r),this.setEnd(n,r+1)},selectNodeContents:function(t){this.setStart(t,0),this.setEnd(t,t.childNodes.length)},cloneRange:function(){return new Range(this.startContainer,this.startOffset,this.endContainer,this.endOffset)},collapse:function(t){t?this.setEnd(this.startContainer,this.startOffset):this.setStart(this.endContainer,this.endOffset)},compareBoundaryPoints:function(o,i){var a,s,c,d,f,u;if(o===e||o===r?(a=this.startContainer,s=this.startOffset):(a=this.endContainer,s=this.endOffset),o===e||o===n?(c=i.startContainer,d=i.startOffset):(c=i.endContainer,d=i.endOffset),a===c)return d>s?-1:s>d?1:0;for(f=a;u=f.parentNode;){if(u===c)return d>t.call(u.childNodes,f)?-1:1;f=u}for(f=c;u=f.parentNode;){if(u===a)return s>t.call(u.childNodes,f)?1:-1;f=u}return 1!==a.nodeType&&(a=a.parentNode),1!==c.nodeType&&(c=c.parentNode),a.sourceIndexc.sourceIndex?1:0}},document.createRange=function(){return new Range};var a=function(t,e){return t===e||o(t,e)},s=function(t){var e=t.nodeType;return 3===e||4===e||8===e},c=function(t,e){this.node=t,this.offset=e},d=function(t){var e,n,r,o,a=t.parentElement();return e=t.duplicate(),e.collapse(!0),n=e.parentElement(),e=t.duplicate(),e.collapse(!1),r=e.parentElement(),o=n===r?n:i(n,r),o===a?o:i(a,o)},f=function(e,n,r,o){var i=e.duplicate();i.collapse(r);var d=i.parentElement();if(a(n,d)||(d=n),!d.canHaveHTML)return new c(d.parentNode,t.call(d.parentNode.childNodes,d));var f,u,l,h,p,v=document.createElement("span"),m=r?"StartToStart":"StartToEnd";do d.insertBefore(v,v.previousSibling),i.moveToElementText(v),f=i.compareEndPoints(m,e);while(f>0&&v.previousSibling);if(p=v.nextSibling,-1===f&&p&&s(p)){i.setEndPoint(r?"EndToStart":"EndToEnd",e);var E;if(/[\r\n]/.test(p.data)||/[\r\n]/.test(i.text)){var g=i.duplicate(),y=g.text.replace(/\r\n/g,"\r").length;for(E=g.moveStart("character",y);-1===(f=g.compareEndPoints("StartToEnd",g));)E+=1,g.moveStart("character",1)}else E=i.text.length;h=new c(p,E)}else u=(o||!r)&&v.previousSibling,l=(o||r)&&v.nextSibling,h=l&&s(l)?new c(l,0):u&&s(u)?new c(u,u.data.length):new c(d,t.call(d.childNodes,v));return v.parentNode.removeChild(v),h},u=function(t,e){var n,r,o,i,a=t.offset,c=document,d=c.body.createTextRange(),f=s(t.node);return f?(n=t.node,r=n.parentNode):(i=t.node.childNodes,n=i.length>a?i[a]:null,r=t.node),o=c.createElement("span"),o.innerHTML="",n?r.insertBefore(o,n):r.appendChild(o),d.moveToElementText(o),d.collapse(!e),r.removeChild(o),f&&d[e?"moveStart":"moveEnd"]("character",a),d},l=function(t){var e,n,r=d(t);return 0===t.compareEndPoints("StartToEnd",t)?e=n=f(t,r,!0,!0):(e=f(t,r,!0,!1),n=f(t,r,!1,!1)),new Range(e.node,e.offset,n.node,n.offset)},h=function(t){var e,n,r;return t.collapsed?e=u(new c(t.startContainer,t.startOffset),!0):(n=u(new c(t.startContainer,t.startOffset),!0),r=u(new c(t.endContainer,t.endOffset),!1),e=document.body.createTextRange(),e.setEndPoint("StartToStart",n),e.setEndPoint("EndToEnd",r)),e},p={rangeCount:0,getRangeAt:function(t){if(0!==t)return void 0;var e=document.selection.createRange();if(e.add){var n=document.createRange();n.moveToElementText(e.item(0)),n.collapse(!1),n.select(),e=n}return l(e)},removeAllRanges:function(){},addRange:function(t){h(t).select()}};document.attachEvent("onbeforeactivate",function(){p.rangeCount=1}),document.attachEvent("ondeactivate",function(){p.rangeCount=0}),window.getSelection=function(){return p}})(); \ No newline at end of file diff --git a/build/squire.js b/build/squire.js index 9e74404..c667635 100644 --- a/build/squire.js +++ b/build/squire.js @@ -1 +1 @@ -/* Copyright © 2011-2012 by Neil Jenkins. Licensed under the MIT license. *//*global navigator, window */var UA=function(e){"use strict";var t=navigator.userAgent,n=!!e.opera,r=/Trident\//.test(t),i=/WebKit\//.test(t);return{isOpera:n,isIE8:e.ie===8,isIE:r,isGecko:/Gecko\//.test(t),isWebKit:i,isIOS:/iP(?:ad|hone|od)/.test(t),useTextFixer:r||n,cantFocusEmptyTextNodes:r||i,losesSelectionOnBlur:r}}(window),DOMTreeWalker=function(){"use strict";var e={1:1,2:2,3:4,8:128,9:256,11:1024},t=1,n=function(e,t,n){this.root=this.currentNode=e,this.nodeType=t,this.filter=n};return n.prototype.nextNode=function(){var n=this.currentNode,r=this.root,i=this.nodeType,s=this.filter,o;for(;;){o=n.firstChild;while(!o&&n){if(n===r)break;o=n.nextSibling,o||(n=n.parentNode)}if(!o)return null;if(e[o.nodeType]&i&&s(o)===t)return this.currentNode=o,o;n=o}},n.prototype.previousNode=function(){var n=this.currentNode,r=this.root,i=this.nodeType,s=this.filter,o;for(;;){if(n===r)return null;o=n.previousSibling;if(o)while(n=o.lastChild)o=n;else o=n.parentNode;if(!o)return null;if(e[o.nodeType]&i&&s(o)===t)return this.currentNode=o,o;n=o}},n}();(function(e,t){"use strict";var n=function(e,t){var n=e.length,r,i;while(n--){r=e[n].prototype;for(i in t)r[i]=t[i]}},r=function(e,t){var n=e.length;while(n--)if(!t(e[n]))return!1;return!0},i=function(){return!1},s=function(){return!0},o=/^(?: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)$/,u={BR:1,IMG:1,INPUT:1},a=function(e,t){var n=t.parentNode;return n&&n.replaceChild(e,t),e},f=1,l=3,c=1,h=1,p=3,d=function(e){return e.isBlock()?h:p};n(window.Node?[Node]:[Text,Element,HTMLDocument],{isInline:i,isBlock:i,isContainer:i,getPath:function(){var e=this.parentNode;return e?e.getPath():""},detach:function(){var e=this.parentNode;return e&&e.removeChild(this),this},replaceWith:function(e){return a(e,this),this},replaces:function(e){return a(this,e),this},nearest:function(e,t){var n=this.parentNode;return n?n.nearest(e,t):null},getPreviousBlock:function(){var e=this.ownerDocument,n=new t(e.body,c,d,!1);return n.currentNode=this,n.previousNode()},getNextBlock:function(){var e=this.ownerDocument,n=new t(e.body,c,d,!1);return n.currentNode=this,n.nextNode()},split:function(e,t){return e},mergeContainers:function(){}}),n([Text],{isLeaf:i,isInline:s,getLength:function(){return this.length},isLike:function(e){return e.nodeType===l},split:function(e,t){var n=this;return n===t?e:n.parentNode.split(n.splitText(e),t)}}),n([Element],{isLeaf:function(){return!!u[this.nodeName]},isInline:function(){return o.test(this.nodeName)},isBlock:function(){return!this.isInline()&&r(this.childNodes,function(e){return e.isInline()})},isContainer:function(){return!this.isInline()&&!this.isBlock()},getLength:function(){return this.childNodes.length},getPath:function(){var e=this.nodeName;if(e==="BODY")return e;var t=this.parentNode.getPath(),n=this.id,r=this.className.trim();return t+=">"+e,n&&(t+="#"+n),r&&(r=r.split(/\s\s*/),r.sort(),t+=".",t+=r.join(".")),t},wraps:function(e){return a(this,e).appendChild(e),this},empty:function(){var e=this.ownerDocument.createDocumentFragment(),t=this.childNodes.length;while(t--)e.appendChild(this.firstChild);return e},is:function(e,t){if(this.nodeName!==e)return!1;var n;for(n in t)if(this.getAttribute(n)!==t[n])return!1;return!0},nearest:function(e,t){var n=this;do if(n.is(e,t))return n;while((n=n.parentNode)&&n.nodeType===f);return null},isLike:function(e){return e.nodeType===f&&e.nodeName===this.nodeName&&e.className===this.className&&e.style.cssText===this.style.cssText},mergeInlines:function(e){var t=this.childNodes,n=t.length,r=[],i,s,o;while(n--){i=t[n],s=n&&t[n-1];if(n&&i.isInline()&&i.isLike(s)&&!u[i.nodeName])e.startContainer===i&&(e.startContainer=s,e.startOffset+=s.getLength()),e.endContainer===i&&(e.endContainer=s,e.endOffset+=s.getLength()),e.startContainer===this&&(e.startOffset>n?e.startOffset-=1:e.startOffset===n&&(e.startContainer=s,e.startOffset=s.getLength())),e.endContainer===this&&(e.endOffset>n?e.endOffset-=1:e.endOffset===n&&(e.endContainer=s,e.endOffset=s.getLength())),i.detach(),i.nodeType===l?s.appendData(i.data.replace(/\u200B/g,"")):r.push(i.empty());else if(i.nodeType===f){o=r.length;while(o--)i.appendChild(r.pop());i.mergeInlines(e)}}},mergeWithBlock:function(e,t){var n=this,r=e,i,s,o;while(r.parentNode.childNodes.length===1)r=r.parentNode;r.detach(),s=n.childNodes.length,i=n.lastChild,i&&i.nodeName==="BR"&&(n.removeChild(i),s-=1),o={startContainer:n,startOffset:s,endContainer:n,endOffset:s},n.appendChild(e.empty()),n.mergeInlines(o),t.setStart(o.startContainer,o.startOffset),t.collapse(!0),window.opera&&(i=n.lastChild)&&i.nodeName==="BR"&&n.removeChild(i)},mergeContainers:function(){var e=this.previousSibling,t=this.firstChild;e&&e.isLike(this)&&e.isContainer()&&(e.appendChild(this.detach().empty()),t&&t.mergeContainers())},split:function(e,t){var n=this;typeof e=="number"&&(e=e-1,s=n.compareBoundaryPoints(u,r)<1;return!i&&!s}var l=n.compareBoundaryPoints(o,r)<1,c=n.compareBoundaryPoints(a,r)>-1;return l&&c},moveBoundariesDownTree:function(){var e=this.startContainer,t=this.startOffset,n=this.endContainer,i=this.endOffset,s;while(e.nodeType!==r){s=e.childNodes[t];if(!s||s.isLeaf())break;e=s,t=0}if(i)while(n.nodeType!==r){s=n.childNodes[i-1];if(!s||s.isLeaf())break;n=s,i=n.getLength()}else while(n.nodeType!==r){s=n.firstChild;if(!s||s.isLeaf())break;n=s}return this.collapsed?(this.setStart(n,i),this.setEnd(e,t)):(this.setStart(e,t),this.setEnd(n,i)),this},moveBoundariesUpTree:function(e){var n=this.startContainer,r=this.startOffset,i=this.endContainer,s=this.endOffset,o;e||(e=this.commonAncestorContainer);while(n!==e&&!r)o=n.parentNode,r=t.call(o.childNodes,n),n=o;while(i!==e&&s===i.getLength())o=i.parentNode,s=t.call(o.childNodes,i)+1,i=o;return this.setStart(n,r),this.setEnd(i,s),this},getStartBlock:function(){var e=this.startContainer,t;return e.isInline()?t=e.getPreviousBlock():e.isBlock()?t=e:(t=l(e,this.startOffset),t=t.getNextBlock()),t&&this.containsNode(t,!0)?t:null},getEndBlock:function(){var e=this.endContainer,t,n;if(e.isInline())t=e.getPreviousBlock();else if(e.isBlock())t=e;else{t=c(e,this.endOffset);if(!t){t=e.ownerDocument.body;while(n=t.lastChild)t=n}t=t.getPreviousBlock()}return t&&this.containsNode(t,!0)?t:null},startsAtBlockBoundary:function(){var e=this.startContainer,n=this.startOffset,r,i;while(e.isInline()){if(n)return!1;r=e.parentNode,n=t.call(r.childNodes,e),e=r}while(n&&(i=e.childNodes[n-1])&&(i.data===""||i.nodeName==="BR"))n-=1;return!n},endsAtBlockBoundary:function(){var e=this.endContainer,n=this.endOffset,r=e.getLength(),i,s;while(e.isInline()){if(n!==r)return!1;i=e.parentNode,n=t.call(i.childNodes,e)+1,e=i,r=e.childNodes.length}while(n-1)e.deleteData(t,1);!e.data&&!e.nextSibling&&!e.previousSibling&&e.parentNode.isInline()&&e.parentNode.detach()}},j,F,I="",q=function(e,t){M&&!t&&B(e);var n=e.startContainer,r=e.endContainer,i;if(t||n!==j||r!==F)j=n,F=r,i=n&&r?n===r?r.getPath():"(selection)":"",I!==i&&(I=i,S("pathChange",{path:i}));n!==r&&S("select")},R=function(){q(O())};T("keyup",R),T("mouseup",R);var U=function(){h&&l.focus(),f.focus()},z=function(){h&&l.blur(),top.focus()};f.addEventListener("focus",x,!1),f.addEventListener("blur",x,!1);var W=function(){return l.innerHTML},X=function(e){var t=l;t.innerHTML=e;do t.fixCursor();while(t=t.getNextBlock())},V=function(e,t){t||(t=O()),t.collapse(!0),t._insertNode(e),t.setStartAfter(e),A(t),q(t)},$=Array.prototype.indexOf,J="squire-selection-start",K="squire-selection-end",Q=function(e){var t=b("INPUT",{id:J,type:"hidden"}),n=b("INPUT",{id:K,type:"hidden"}),i;e._insertNode(t),e.collapse(!1),e._insertNode(n),t.compareDocumentPosition(n)&r&&(t.id=K,n.id=J,i=t,t=n,n=i),e.setStartAfter(t),e.setEndBefore(n)},G=function(t){var n=e.getElementById(J),r=e.getElementById(K);if(n&&r){var i=n.parentNode,s=r.parentNode,o,u={startContainer:i,endContainer:s,startOffset:$.call(i.childNodes,n),endOffset:$.call(s.childNodes,r)};i===s&&(u.endOffset-=1),n.detach(),r.detach(),i.mergeInlines(u),i!==s&&s.mergeInlines(u),t||(t=e.createRange()),t.setStart(u.startContainer,u.startOffset),t.setEnd(u.endContainer,u.endOffset),o=t.collapsed,t.moveBoundariesDownTree(),o&&t.collapse(!0)}return t||null},Y,Z,et,tt,nt=function(){tt&&(tt=!1,S("undoStateChange",{canUndo:!0,canRedo:!1})),S("input")};T("keyup",function(e){var t=e.keyCode;!e.ctrlKey&&!e.metaKey&&!e.altKey&&(t<16||t>20)&&(t<33||t>45)&&nt()});var rt=function(e){tt||(Y+=1,Yr.endOffset?d.splitText(r.endOffset):p=r.endOffset),d===r.startContainer&&(v&&r.startOffset?d=d.splitText(r.startOffset):h=r.startOffset),v&&(b(e,t).wraps(d),p=d.length),c=d,l||(l=c);while(d=f.nextNode());r=C(l,h,c,p)}return r},at=function(t,n,r,i){Q(r);var o;r.collapsed&&(m?(o=e.createTextNode("​"),H(o)):o=e.createTextNode(""),r._insertNode(o));var u=r.commonAncestorContainer;while(u.isInline())u=u.parentNode;var a=r.startContainer,f=r.startOffset,l=r.endContainer,c=r.endOffset,h=[],p=function(e,t){if(r.containsNode(e,!1))return;var n=e.nodeType===s,i,o;if(!r.containsNode(e,!0)){e.nodeName!=="INPUT"&&(!n||e.data)&&h.push([t,e]);return}if(n)e===l&&c!==e.length&&h.push([t,e.splitText(c)]),e===a&&f&&(e.splitText(f),h.push([t,e]));else for(i=e.firstChild;i;i=o)o=i.nextSibling,p(i,t)},d=Array.prototype.filter.call(u.getElementsByTagName(t),function(e){return r.containsNode(e,!0)&&e.is(t,n)});i||d.forEach(function(e){p(e,e)}),h.forEach(function(e){e[0].cloneNode(!1).wraps(e[1])}),d.forEach(function(e){e.replaceWith(e.empty())}),G(r),o&&r.collapse(!1);var v={startContainer:r.startContainer,startOffset:r.startOffset,endContainer:r.endContainer,endOffset:r.endOffset};return u.mergeInlines(v),r.setStart(v.startContainer,v.startOffset),r.setEnd(v.endContainer,v.endOffset),r},ft=function(e,t,n,r){if(!n&&!(n=O()))return;rt(n),G(n),t&&(n=at(t.tag.toUpperCase(),t.attributes||{},n,r)),e&&(n=ut(e.tag.toUpperCase(),e.attributes||{},n)),A(n),q(n,!0),nt()},lt={DIV:"DIV",PRE:"DIV",H1:"DIV",H2:"DIV",H3:"DIV",H4:"DIV",H5:"DIV",H6:"DIV",P:"DIV",DT:"DD",DD:"DT",LI:"LI"},ct=function(e,t,n){var r=lt[e.nodeName],i=t.split(n,e.parentNode);return i.nodeName!==r&&(e=b(r),e.className=i.dir==="rtl"?"dir-rtl":"",e.dir=i.dir,e.replaces(i).appendChild(i.empty()),i=e),i},ht=function(e,t,n){if(!n&&!(n=O()))return;t&&(rt(n),G(n));var r=n.getStartBlock(),i=n.getEndBlock();if(r&&i)do if(e(r)||r===i)break;while(r=r.getNextBlock());t&&(A(n),q(n,!0),nt())},pt=function(e,t){if(!t&&!(t=O()))return;c||l.setAttribute("contenteditable","false"),tt?Q(t):rt(t),t.expandToBlockBoundaries(),t.moveBoundariesUpTree(l);var n=t._extractContents(l);t._insertNode(e(n)),t.endOffset]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])|(?:[\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,4}))/i,St=function(e){var t=e.ownerDocument,r=new n(e,o,function(e){return e.nearest("A")?a:u},!1),i,s,f,l,c,h,p;while(i=r.nextNode()){s=i.data.split(Et),l=s.length;if(l>1){h=i.parentNode,p=i.nextSibling;for(f=0;f(e.documentElement.scrollTop||l.scrollTop)+l.offsetHeight&&a.scrollIntoView(!1),nt()},backspace:function(e){var t=O();if(!t.collapsed)e.preventDefault(),t._deleteContents(),A(t),q(t,!0);else if(t.startsAtBlockBoundary()){e.preventDefault();var n=t.getStartBlock(),r=n.getPreviousBlock();if(r){if(!r.isContentEditable){r.detach();return}r.mergeWithBlock(n,t),n=r.parentNode;while(n&&!n.nextSibling)n=n.parentNode;n&&(n=n.nextSibling)&&n.mergeContainers(),A(t)}else{if(n.nearest("UL")||n.nearest("OL"))return pt(wt,t);if(n.nearest("BLOCKQUOTE"))return pt(vt,t);A(t),q(t,!0)}}else setTimeout(Pt,0)},"delete":function(e){var t=O();if(!t.collapsed)e.preventDefault(),t._deleteContents(),A(t),q(t,!0);else if(t.endsAtBlockBoundary()){e.preventDefault();var n=t.getStartBlock(),r=n.getNextBlock();if(r){if(!r.isContentEditable){r.detach();return}n.mergeWithBlock(r,t),r=n.parentNode;while(r&&!r.nextSibling)r=r.parentNode;r&&(r=r.nextSibling)&&r.mergeContainers(),A(t),q(t,!0)}}else setTimeout(Pt,0)},space:function(){var e=O();rt(e),St(e.startContainer),G(e),A(e)},"ctrl-b":Dt("B"),"ctrl-i":Dt("I"),"ctrl-u":Dt("U"),"ctrl-y":_t(st),"ctrl-z":_t(it),"ctrl-shift-z":_t(st)};T(c?"keypress":"keydown",function(e){var t=e.keyCode,n=Mt[t]||String.fromCharCode(t).toLowerCase(),r="";c&&e.which===46&&(n="."),111o?e.startOffset-=1:e.startOffset===o&&(e.startContainer=n,e.startOffset=n.getLength())),e.endContainer===this&&(e.endOffset>o?e.endOffset-=1:e.endOffset===o&&(e.endContainer=n,e.endOffset=n.getLength())),t.detach(),t.nodeType===c?n.appendData(t.data.replace(/\u200B/g,"")):s.push(t.empty());else if(t.nodeType===d){for(r=s.length;r--;)t.appendChild(s.pop());t.mergeInlines(e)}},mergeWithBlock:function(e,t){for(var n,r,i,o=this,s=e;1===s.parentNode.childNodes.length;)s=s.parentNode;s.detach(),r=o.childNodes.length,n=o.lastChild,n&&"BR"===n.nodeName&&(o.removeChild(n),r-=1),i={startContainer:o,startOffset:r,endContainer:o,endOffset:r},o.appendChild(e.empty()),o.mergeInlines(i),t.setStart(i.startContainer,i.startOffset),t.collapse(!0),window.opera&&(n=o.lastChild)&&"BR"===n.nodeName&&o.removeChild(n)},mergeContainers:function(){var e=this.previousSibling,t=this.firstChild;e&&e.isLike(this)&&e.isContainer()&&(e.appendChild(this.detach().empty()),t&&t.mergeContainers())},split:function(e,t){var n=this;if("number"==typeof e&&(e=n.childNodes.length>e?n.childNodes[e]:null),n===t)return e;for(var r,i=n.parentNode,o=n.cloneNode(!1);e;)r=e.nextSibling,o.appendChild(e),e=r;return n.fixCursor(),o.fixCursor(),(r=n.nextSibling)?i.insertBefore(o,r):i.appendChild(o),i.split(o,t)},fixCursor:function(){var t,n,r=this,i=r.ownerDocument;if("BODY"===r.nodeName&&((n=r.firstChild)&&"BR"!==n.nodeName||(t=i.createElement("DIV"),n?r.replaceChild(t,n):r.appendChild(t),r=t,t=null)),r.isInline())r.firstChild||(e.cantFocusEmptyTextNodes?(t=i.createTextNode("​"),editor._setPlaceholderTextNode(t)):t=i.createTextNode(""));else if(e.useTextFixer){for(;r.nodeType!==c&&!r.isLeaf();){if(n=r.firstChild,!n){t=i.createTextNode("");break}r=n}r.nodeType===c?/^ +$/.test(r.data)&&(r.data=""):r.isLeaf()&&r.parentNode.insertBefore(i.createTextNode(""),r)}else if(!r.querySelector("BR"))for(t=i.createElement("BR");(n=r.lastElementChild)&&!n.isInline();)r=n;return t&&r.appendChild(t),this}}),function(){var e=document.createElement("div"),t=document.createTextNode("12");return e.appendChild(t),t.splitText(2),2!==e.childNodes.length}()&&(Text.prototype.splitText=function(e){var t=this.ownerDocument.createTextNode(this.data.slice(e)),n=this.nextSibling,r=this.parentNode,i=this.length-e;return n?r.insertBefore(t,n):r.appendChild(t),i&&this.deleteData(e,i),t})})(UA,DOMTreeWalker),function(e){"use strict";var t,n=Array.prototype.indexOf,r=1,i=3,o=4,s=1,a=0,l=1,d=2,c=3,f=function(e,t){for(var n=e.childNodes;t&&e.nodeType===r;)e=n[t-1],n=e.childNodes,t=n.length;return e},u=function(e,t){if(e.nodeType===r){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},h={forEachTextNode:function(t){var n=this.cloneRange();n.moveBoundariesDownTree();for(var r=n.startContainer,i=n.endContainer,a=n.commonAncestorContainer,l=new e(a,o,function(){return s},!1),d=l.currentNode=r;!t(d,n)&&d!==i&&(d=l.nextNode()););},getTextContent:function(){var e="";return this.forEachTextNode(function(t,n){var r=t.data;r&&/\S/.test(r)&&(t===n.endContainer&&(r=r.slice(0,n.endOffset)),t===n.startContainer&&(r=r.slice(n.startOffset)),e+=r)}),e},_insertNode:function(e){var t,r,o,s,a=this.startContainer,l=this.startOffset,d=this.endContainer,c=this.endOffset;return a.nodeType===i?(t=a.parentNode,r=t.childNodes,l===a.length?(l=n.call(r,a)+1,this.collapsed&&(d=t,c=l)):(l&&(s=a.splitText(l),d===a?(c-=l,d=s):d===t&&(c+=1),a=s),l=n.call(r,a)),a=t):r=a.childNodes,o=r.length,l===o?a.appendChild(e):a.insertBefore(e,r[l]),a===d&&(c+=r.length-o),this.setStart(a,l),this.setEnd(d,c),this},_extractContents:function(e){var t=this.startContainer,r=this.startOffset,o=this.endContainer,s=this.endOffset;e||(e=this.commonAncestorContainer),e.nodeType===i&&(e=e.parentNode);for(var a,l=o.split(s,e),d=t.split(r,e),c=e.ownerDocument.createDocumentFragment();d!==l;)a=d.nextSibling,c.appendChild(d),d=a;return this.setStart(e,l?n.call(e.childNodes,l):e.childNodes.length),this.collapse(!0),e.fixCursor(),c},_deleteContents:function(){this.moveBoundariesUpTree(),this._extractContents();var e=this.getStartBlock(),t=this.getEndBlock();e&&t&&e!==t&&e.mergeWithBlock(t,this),e&&e.fixCursor();var n=this.endContainer.ownerDocument.body,r=n.firstChild;r&&"BR"!==r.nodeName||(n.fixCursor(),this.selectNodeContents(n.firstChild));var i=this.collapsed;return this.moveBoundariesDownTree(),i&&this.collapse(!0),this},insertTreeFragment:function(e){for(var t=!0,n=e.childNodes,i=n.length;i--;)if(!n[i].isInline()){t=!1;break}if(this.collapsed||this._deleteContents(),this.moveBoundariesDownTree(),t)this._insertNode(e),this.collapse(!1);else{for(var o,s,a=this.startContainer.split(this.startOffset,this.startContainer.ownerDocument.body),l=a.previousSibling,d=l,c=d.childNodes.length,f=a,u=0,h=a.parentNode;(o=d.lastChild)&&o.nodeType===r&&"BR"!==o.nodeName;)d=o,c=d.childNodes.length;for(;(o=f.firstChild)&&o.nodeType===r&&"BR"!==o.nodeName;)f=o;for(;(o=e.firstChild)&&o.isInline();)d.appendChild(o);for(;(o=e.lastChild)&&o.isInline();)f.insertBefore(o,f.firstChild),u+=1;for(s=e;s=s.getNextBlock();)s.fixCursor();h.insertBefore(e,a),a.mergeContainers(),l.nextSibling.mergeContainers(),a!==f||f.textContent||(f=f.previousSibling,u=f.getLength(),h.removeChild(a)),l!==d||d.textContent||(d=d.nextSibling,c=0,h.removeChild(l)),this.setStart(d,c),this.setEnd(f,u),this.moveBoundariesDownTree()}},containsNode:function(e,t){var n=this,r=e.ownerDocument.createRange();if(r.selectNode(e),t){var i=n.compareBoundaryPoints(c,r)>-1,o=1>n.compareBoundaryPoints(l,r);return!i&&!o}var s=1>n.compareBoundaryPoints(a,r),f=n.compareBoundaryPoints(d,r)>-1;return s&&f},moveBoundariesDownTree:function(){for(var e,t=this.startContainer,n=this.startOffset,r=this.endContainer,o=this.endOffset;t.nodeType!==i&&(e=t.childNodes[n],e&&!e.isLeaf());)t=e,n=0;if(o)for(;r.nodeType!==i&&(e=r.childNodes[o-1],e&&!e.isLeaf());)r=e,o=r.getLength();else for(;r.nodeType!==i&&(e=r.firstChild,e&&!e.isLeaf());)r=e;return this.collapsed?(this.setStart(r,o),this.setEnd(t,n)):(this.setStart(t,n),this.setEnd(r,o)),this},moveBoundariesUpTree:function(e){var t,r=this.startContainer,i=this.startOffset,o=this.endContainer,s=this.endOffset;for(e||(e=this.commonAncestorContainer);r!==e&&!i;)t=r.parentNode,i=n.call(t.childNodes,r),r=t;for(;o!==e&&s===o.getLength();)t=o.parentNode,s=n.call(t.childNodes,o)+1,o=t;return this.setStart(r,i),this.setEnd(o,s),this},getStartBlock:function(){var e,t=this.startContainer;return t.isInline()?e=t.getPreviousBlock():t.isBlock()?e=t:(e=f(t,this.startOffset),e=e.getNextBlock()),e&&this.containsNode(e,!0)?e:null},getEndBlock:function(){var e,t,n=this.endContainer;if(n.isInline())e=n.getPreviousBlock();else if(n.isBlock())e=n;else{if(e=u(n,this.endOffset),!e)for(e=n.ownerDocument.body;t=e.lastChild;)e=t;e=e.getPreviousBlock()}return e&&this.containsNode(e,!0)?e:null},startsAtBlockBoundary:function(){for(var e,t,r=this.startContainer,i=this.startOffset;r.isInline();){if(i)return!1;e=r.parentNode,i=n.call(e.childNodes,r),r=e}for(;i&&(t=r.childNodes[i-1])&&(""===t.data||"BR"===t.nodeName);)i-=1;return!i},endsAtBlockBoundary:function(){for(var e,t,r=this.endContainer,i=this.endOffset,o=r.getLength();r.isInline();){if(i!==o)return!1;e=r.parentNode,i=n.call(e.childNodes,r)+1,r=e,o=r.childNodes.length}for(;o>i&&(t=r.childNodes[i])&&(""===t.data||"BR"===t.nodeName);)i+=1;return i===o},expandToBlockBoundaries:function(){var e,t=this.getStartBlock(),r=this.getEndBlock();return t&&r&&(e=t.parentNode,this.setStart(e,n.call(e.childNodes,t)),e=r.parentNode,this.setEnd(e,n.call(e.childNodes,r)+1)),this}};for(t in h)Range.prototype[t]=h[t]}(DOMTreeWalker),function(e,t,n){"use strict";var r,i=2,o=1,s=3,a=4,l=1,d=3,c=e.defaultView,f=e.body,u=t.isOpera,h=t.isGecko,p=t.isIOS,N=t.isIE,g=t.isIE8,C=t.cantFocusEmptyTextNodes,m=t.losesSelectionOnBlur,v=t.useTextFixer,y=function(t,n,r){var i,o,s,a=e.createElement(t);if(n instanceof Array&&(r=n,n=null),n)for(i in n)a.setAttribute(i,n[i]);if(r)for(o=0,s=r.length;s>o;o+=1)a.appendChild(r[o]);return a},T={focus:1,blur:1,pathChange:1,select:1,input:1,undoStateChange:1},B={},x=function(e,t){var n,i,o,s=B[e];if(s)for(t||(t={}),t.type!==e&&(t.type=e),n=0,i=s.length;i>n;n+=1){o=s[n];try{o.handleEvent?o.handleEvent(t):o(t)}catch(a){r.didError(a)}}},S=function(e){x(e.type,e)},O=function(t,n){var r=B[t];r||(r=B[t]=[],T[t]||e.addEventListener(t,S,!1)),r.push(n)},E=function(t,n){var r,i=B[t];if(i){for(r=i.length;r--;)i[r]===n&&i.splice(r,1);i.length||(delete B[t],T[t]||e.removeEventListener(t,S,!1))}},I=function(t,n,r,i){if(t instanceof Range)return t.cloneRange();var o=e.createRange();return o.setStart(t,n),r?o.setEnd(r,i):o.setEnd(t,n),o},D=c.getSelection(),b=null,k=function(e){e&&(p&&c.focus(),D.removeAllRanges(),D.addRange(e))},L=function(){return D.rangeCount&&(b=D.getRangeAt(0).cloneRange(),b.startContainer.isLeaf()&&b.setStartBefore(b.startContainer),b.endContainer.isLeaf()&&b.setEndBefore(b.endContainer)),b};m&&c.addEventListener("beforedeactivate",L,!0);var A,w,R=null,P=!0,U=!1,V=function(){P=!0,U=!1},F=function(e){R&&(P=!0,H()),U||(setTimeout(V,0),U=!0),P=!1,R=e},H=function(){if(P){var e,t=R;if(R=null,t.parentNode){for(;(e=t.data.indexOf("​"))>-1;)t.deleteData(e,1);t.data||t.nextSibling||t.previousSibling||!t.parentNode.isInline()||t.parentNode.detach()}}},W="",_=function(e,t){R&&!t&&H(e);var n,r=e.startContainer,i=e.endContainer;(t||r!==A||i!==w)&&(A=r,w=i,n=r&&i?r===i?i.getPath():"(selection)":"",W!==n&&(W=n,x("pathChange",{path:n}))),r!==i&&x("select")},M=function(){_(L())};O("keyup",M),O("mouseup",M);var K=function(){h&&f.focus(),c.focus()},z=function(){h&&f.blur(),top.focus()};c.addEventListener("focus",S,!1),c.addEventListener("blur",S,!1);var q,G,Q,Y,$=function(){return f.innerHTML},j=function(e){var t=f;t.innerHTML=e;do t.fixCursor();while(t=t.getNextBlock())},Z=function(e,t){t||(t=L()),t.collapse(!0),t._insertNode(e),t.setStartAfter(e),k(t),_(t)},J=Array.prototype.indexOf,X="squire-selection-start",et="squire-selection-end",tt=function(e){var t,n=y("INPUT",{id:X,type:"hidden"}),r=y("INPUT",{id:et,type:"hidden"});e._insertNode(n),e.collapse(!1),e._insertNode(r),n.compareDocumentPosition(r)&i&&(n.id=et,r.id=X,t=n,n=r,r=t),e.setStartAfter(n),e.setEndBefore(r)},nt=function(t){var n=e.getElementById(X),r=e.getElementById(et);if(n&&r){var i,o=n.parentNode,s=r.parentNode,a={startContainer:o,endContainer:s,startOffset:J.call(o.childNodes,n),endOffset:J.call(s.childNodes,r)};o===s&&(a.endOffset-=1),n.detach(),r.detach(),o.mergeInlines(a),o!==s&&s.mergeInlines(a),t||(t=e.createRange()),t.setStart(a.startContainer,a.startOffset),t.setEnd(a.endContainer,a.endOffset),i=t.collapsed,t.moveBoundariesDownTree(),i&&t.collapse(!0)}return t||null},rt=function(){Y&&(Y=!1,x("undoStateChange",{canUndo:!0,canRedo:!1})),x("input")};O("keyup",function(e){var t=e.keyCode;e.ctrlKey||e.metaKey||e.altKey||!(16>t||t>20)||!(33>t||t>45)||rt()});var it=function(e){Y||(q+=1,Q>q&&(G.length=Q=q),e&&tt(e),G[q]=$(),Q+=1,Y=!0)},ot=function(){if(0!==q||!Y){it(L()),q-=1,j(G[q]);var e=nt();e&&k(e),Y=!0,x("undoStateChange",{canUndo:0!==q,canRedo:!0}),x("input")}},st=function(){if(Q>q+1&&Y){q+=1,j(G[q]);var e=nt();e&&k(e),x("undoStateChange",{canUndo:!0,canRedo:Q>q+1}),x("input")}},at=function(e,t,r){if(e=e.toUpperCase(),t||(t={}),!r&&!(r=L()))return!1;var i,o,c=r.commonAncestorContainer;if(c.nearest(e,t))return!0;if(c.nodeType===s)return!1;i=new n(c,a,function(e){return r.containsNode(e,!0)?l:d},!1);for(var f=!1;o=i.nextNode();){if(!o.nearest(e,t))return!1;f=!0}return f},lt=function(e,t,r){if(r.collapsed){var i=y(e,t).fixCursor();r._insertNode(i),r.setStart(i.firstChild,i.firstChild.length),r.collapse(!0)}else{var o,c,f,u=new n(r.commonAncestorContainer,a,function(e){return r.containsNode(e,!0)?l:d},!1),h=0,p=0,N=u.currentNode=r.startContainer;N.nodeType!==s&&(N=u.nextNode());do f=!N.nearest(e,t),N===r.endContainer&&(f&&N.length>r.endOffset?N.splitText(r.endOffset):p=r.endOffset),N===r.startContainer&&(f&&r.startOffset?N=N.splitText(r.startOffset):h=r.startOffset),f&&(y(e,t).wraps(N),p=N.length),c=N,o||(o=c);while(N=u.nextNode());r=I(o,h,c,p)}return r},dt=function(t,n,r,i){tt(r);var o;r.collapsed&&(C?(o=e.createTextNode("​"),F(o)):o=e.createTextNode(""),r._insertNode(o));for(var a=r.commonAncestorContainer;a.isInline();)a=a.parentNode;var l=r.startContainer,d=r.startOffset,c=r.endContainer,f=r.endOffset,u=[],h=function(e,t){if(!r.containsNode(e,!1)){var n,i,o=e.nodeType===s;if(!r.containsNode(e,!0))return"INPUT"===e.nodeName||o&&!e.data||u.push([t,e]),void 0;if(o)e===c&&f!==e.length&&u.push([t,e.splitText(f)]),e===l&&d&&(e.splitText(d),u.push([t,e]));else for(n=e.firstChild;n;n=i)i=n.nextSibling,h(n,t)}},p=Array.prototype.filter.call(a.getElementsByTagName(t),function(e){return r.containsNode(e,!0)&&e.is(t,n)});i||p.forEach(function(e){h(e,e)}),u.forEach(function(e){e[0].cloneNode(!1).wraps(e[1])}),p.forEach(function(e){e.replaceWith(e.empty())}),nt(r),o&&r.collapse(!1);var N={startContainer:r.startContainer,startOffset:r.startOffset,endContainer:r.endContainer,endOffset:r.endOffset};return a.mergeInlines(N),r.setStart(N.startContainer,N.startOffset),r.setEnd(N.endContainer,N.endOffset),r},ct=function(e,t,n,r){(n||(n=L()))&&(it(n),nt(n),t&&(n=dt(t.tag.toUpperCase(),t.attributes||{},n,r)),e&&(n=lt(e.tag.toUpperCase(),e.attributes||{},n)),k(n),_(n,!0),rt())},ft={DIV:"DIV",PRE:"DIV",H1:"DIV",H2:"DIV",H3:"DIV",H4:"DIV",H5:"DIV",H6:"DIV",P:"DIV",DT:"DD",DD:"DT",LI:"LI"},ut=function(e,t,n){var r=ft[e.nodeName],i=t.split(n,e.parentNode);return i.nodeName!==r&&(e=y(r),e.className="rtl"===i.dir?"dir-rtl":"",e.dir=i.dir,e.replaces(i).appendChild(i.empty()),i=e),i},ht=function(e,t,n){if(n||(n=L())){t&&(it(n),nt(n));var r=n.getStartBlock(),i=n.getEndBlock();if(r&&i)do if(e(r)||r===i)break;while(r=r.getNextBlock());t&&(k(n),_(n,!0),rt())}},pt=function(e,t){if(t||(t=L())){u||f.setAttribute("contenteditable","false"),Y?tt(t):it(t),t.expandToBlockBoundaries(),t.moveBoundariesUpTree(f);var n=t._extractContents(f);t._insertNode(e(n)),t.endOffsetn;n+=1)i=e[n],o=i.nodeName,i.isBlock()?"LI"!==o&&(a=y("LI",{"class":"rtl"===i.dir?"dir-rtl":"",dir:i.dir},[i.empty()]),i.parentNode.nodeName===t?i.replaceWith(a):(s=i.previousSibling)&&s.nodeName===t?(s.appendChild(a),i.detach(),n-=1,r-=1):i.replaceWith(y(t,[a]))):i.isContainer()&&(o!==t&&/^[DOU]L$/.test(o)?i.replaceWith(y(t,[i.empty()])):mt(i.childNodes,t))},vt=function(e){return mt(e.childNodes,"UL"),e},yt=function(e){return mt(e.childNodes,"OL"),e},Tt=function(e){var t=e.querySelectorAll("UL, OL");return Array.prototype.filter.call(t,function(e){return!e.parentNode.nearest("UL")&&!e.parentNode.nearest("OL")}).forEach(function(e){for(var t,n=e.empty(),r=n.childNodes,i=r.length;i--;)t=r[i],"LI"===t.nodeName&&n.replaceChild(y("DIV",{"class":"rtl"===t.dir?"dir-rtl":"",dir:t.dir},[t.empty()]),t);e.replaceWith(n)}),e},Bt=/\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,xt=function(e){for(var t,r,i,o,s,c,f,u=e.ownerDocument,h=new n(e,a,function(e){return e.nearest("A")?d:l},!1);t=h.nextNode();)if(r=t.data.split(Bt),o=r.length,o>1){for(c=t.parentNode,f=t.nextSibling,i=0;o>i;i+=1)s=r[i],i?(i%2?(t=u.createElement("A"),t.textContent=s,t.href=/@/.test(s)?"mailto:"+s:/^(?:ht|f)tps?:/.test(s)?s:"http://"+s):t=u.createTextNode(s),f?c.insertBefore(t,f):c.appendChild(t)):t.data=s;h.currentNode=t}},St=/^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$/,Ot={color:{regexp:/\S/,replace:function(e){return y("SPAN",{"class":"colour",style:"color:"+e})}},fontWeight:{regexp:/^bold/i,replace:function(){return y("B")}},fontStyle:{regexp:/^italic/i,replace:function(){return y("I")}},fontFamily:{regexp:/\S/,replace:function(e){return y("SPAN",{"class":"font",style:"font-family:"+e})}},fontSize:{regexp:/\S/,replace:function(e){return y("SPAN",{"class":"size",style:"font-size:"+e})}}},Et={SPAN:function(e,t){var n,r,i,o,s,a,l=e.style;for(n in Ot)r=Ot[n],i=l[n],i&&r.regexp.test(i)&&(a=r.replace(i),o&&o.appendChild(a),o=a,s||(s=a));return s&&(o.appendChild(e.empty()),t.replaceChild(s,e)),o||e},STRONG:function(e,t){var n=y("B");return t.replaceChild(n,e),n.appendChild(e.empty()),n},EM:function(e,t){var n=y("I");return t.replaceChild(n,e),n.appendChild(e.empty()),n},TT:function(e,t){var n=y("SPAN",{"class":"font",style:'font-family:menlo,consolas,"courier new",monospace'});return t.replaceChild(n,e),n.appendChild(e.empty()),n}},It=function(e,t){var n,r,i,a,l,d,c,f=e.childNodes;for(n=0,r=f.length;r>n;n+=1)if(i=f[n],a=i.nodeName,l=i.nodeType,d=Et[a],l===o){if(c=i.childNodes.length,d)i=d(i,e);else if(!St.test(a)&&!i.isInline()){n-=1,r+=c-1,e.replaceChild(i.empty(),i);continue}!t&&i.style.cssText&&i.removeAttribute("style"),c&&It(i,t)}else l===s&&/\S/.test(i.data)||(e.removeChild(i),n-=1,r-=1);return e},Dt=function(e,t){var n,r,i,o,s=e.childNodes,a=null;for(n=0,r=s.length;r>n;n+=1)i=s[n],o="BR"===i.nodeName,!o&&i.isInline()?(a||(a=y(t)),a.appendChild(i),n-=1,r-=1):(o||a)&&(a||(a=y(t)),a.fixCursor(),o?e.replaceChild(a,i):(e.insertBefore(a,i),n+=1,r+=1),a=null);return a&&e.appendChild(a.fixCursor()),e},bt=function(e){for(var t,n,r=e.querySelectorAll("BR"),i=r.length;i--;)if(t=r[i],n=t.parentNode)if(t.nextSibling&&t.previousSibling){for(;n.isInline();)n=n.parentNode;n.isBlock()?ft[n.nodeName]&&(ut(n,t.parentNode,t),t.detach()):Dt(n,"DIV")}else t.detach()},kt=function(){try{f.fixCursor()}catch(e){r.didError(e)}};O(N?"beforecut":"cut",function(){var e=L();it(e),nt(e),k(e),setTimeout(kt,0)});var Lt=!1;O(N?"beforepaste":"paste",function(e){if(!Lt){var t,n=e.clipboardData,i=n&&n.items,o=!1;if(i)for(t=i.length;t--;)if(/^image\/.*/.test(i[t].type))return e.preventDefault(),x("dragover",{dataTransfer:n,preventDefault:function(){o=!0}}),o&&x("drop",{dataTransfer:n}),void 0;Lt=!0;var s=L(),a=s.startContainer,l=s.startOffset,d=s.endContainer,c=s.endOffset,u=y("DIV",{style:"position: absolute; overflow: hidden; top:"+(f.scrollTop+30)+"px; left: 0; width: 1px; height: 1px;"});f.appendChild(u),s.selectNodeContents(u),k(s),setTimeout(function(){try{var e=u.detach().empty(),t=e.firstChild,n=I(a,l,d,c);if(t){t===e.lastChild&&"DIV"===t.nodeName&&e.replaceChild(t.empty(),t),e.normalize(),xt(e),It(e,!1),bt(e);for(var i=e,o=!0;i=i.getNextBlock();)i.fixCursor();x("willPaste",{fragment:e,preventDefault:function(){o=!1}}),o&&(n.insertTreeFragment(e),rt(),n.collapse(!1))}k(n),_(n,!0),Lt=!1}catch(s){r.didError(s)}},0)}});var At={8:"backspace",9:"tab",13:"enter",32:"space",46:"delete"},wt=function(e){return function(t){t.preventDefault(),e()}},Rt=function(e){return function(t){t.preventDefault();var n=L();at(e,null,n)?ct(null,{tag:e},n):ct({tag:e},null,n)}},Pt=function(){try{var e,t=L(),n=t.startContainer;if(n.nodeType===s&&(n=n.parentNode),n.isInline()&&!n.textContent){do e=n.parentNode;while(e.isInline()&&!e.textContent&&(n=e));t.setStart(e,J.call(e.childNodes,n)),t.collapse(!0),e.removeChild(n),e.isBlock()||(e=e.getPreviousBlock()),e.fixCursor(),t.moveBoundariesDownTree(),k(t),_(t)}}catch(i){r.didError(i)}};g&&O("keyup",function(){var e=f.firstChild;"P"===e.nodeName&&(tt(L()),e.replaceWith(y("DIV",[e.empty()])),k(nt()))});var Ut={enter:function(t){t.preventDefault();var n=L();if(n){it(n),xt(n.startContainer),nt(n),n.collapsed||n._deleteContents();var r,i=n.getStartBlock(),a=i?i.nodeName:"DIV",l=ft[a];if(!i)return n._insertNode(y("BR")),n.collapse(!1),k(n),_(n,!0),rt(),void 0;var d,c=n.startContainer,h=n.startOffset;if(l||(c===i&&(c=h?c.childNodes[h-1]:null,h=0,c&&("BR"===c.nodeName?c=c.nextSibling:h=c.getLength(),c&&"BR"!==c.nodeName||(d=y("DIV").fixCursor(),c?i.replaceChild(d,c):i.appendChild(d),c=d))),Dt(i,"DIV"),l="DIV",c||(c=i.firstChild),n.setStart(c,h),n.setEnd(c,h),i=n.getStartBlock()),!i.textContent){if(i.nearest("UL")||i.nearest("OL"))return pt(Tt,n);if(i.nearest("BLOCKQUOTE"))return pt(Ct,n)}for(r=ut(i,c,h);r.nodeType===o;){var p,N=r.firstChild;if("A"!==r.nodeName){for(;N&&N.nodeType===s&&!N.data&&(p=N.nextSibling,p&&"BR"!==p.nodeName);)N.detach(),N=p;if(!N||"BR"===N.nodeName||N.nodeType===s&&!u)break;r=N}else r.replaceWith(r.empty()),r=N}n=I(r,0),k(n),_(n,!0),r.nodeType===s&&(r=r.parentNode),r.offsetTop+r.offsetHeight>(e.documentElement.scrollTop||f.scrollTop)+f.offsetHeight&&r.scrollIntoView(!1),rt()}},backspace:function(e){var t=L();if(t.collapsed)if(t.startsAtBlockBoundary()){e.preventDefault();var n=t.getStartBlock(),r=n.getPreviousBlock();if(r){if(!r.isContentEditable)return r.detach(),void 0;for(r.mergeWithBlock(n,t),n=r.parentNode;n&&!n.nextSibling;)n=n.parentNode;n&&(n=n.nextSibling)&&n.mergeContainers(),k(t)}else{if(n.nearest("UL")||n.nearest("OL"))return pt(Tt,t);if(n.nearest("BLOCKQUOTE"))return pt(gt,t);k(t),_(t,!0)}}else setTimeout(Pt,0);else e.preventDefault(),t._deleteContents(),k(t),_(t,!0)},"delete":function(e){var t=L();if(t.collapsed)if(t.endsAtBlockBoundary()){e.preventDefault();var n=t.getStartBlock(),r=n.getNextBlock();if(r){if(!r.isContentEditable)return r.detach(),void 0;for(n.mergeWithBlock(r,t),r=n.parentNode;r&&!r.nextSibling;)r=r.parentNode;r&&(r=r.nextSibling)&&r.mergeContainers(),k(t),_(t,!0)}}else setTimeout(Pt,0);else e.preventDefault(),t._deleteContents(),k(t),_(t,!0)},space:function(){var e=L();it(e),xt(e.startContainer),nt(e),k(e)},"ctrl-b":Rt("B"),"ctrl-i":Rt("I"),"ctrl-u":Rt("U"),"ctrl-y":wt(st),"ctrl-z":wt(ot),"ctrl-shift-z":wt(st)};O(u?"keypress":"keydown",function(e){var t=e.keyCode,n=At[t]||String.fromCharCode(t).toLowerCase(),r="";u&&46===e.which&&(n="."),t>111&&124>t&&(n="f"+(t-111)),e.altKey&&(r+="alt-"),(e.ctrlKey||e.metaKey)&&(r+="ctrl-"),e.shiftKey&&(r+="shift-"),n=r+n,Ut[n]&&Ut[n](e)});var Vt=function(e){return function(){return e.apply(null,arguments),this}},Ft=function(e,t,n){return function(){return e(t,n),K(),this}};c.editor=r={didError:function(e){console.log(e)},_setPlaceholderTextNode:F,addEventListener:Vt(O),removeEventListener:Vt(E),focus:Vt(K),blur:Vt(z),getDocument:function(){return e},addStyles:function(t){if(t){var n=e.documentElement.firstChild,r=y("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,i,o,s=[];if(e&&(o=L())&&tt(o),v)for(t=f;t=t.getNextBlock();)t.textContent||t.querySelector("BR")||(n=y("BR"),t.appendChild(n),s.push(n));if(r=$(),v)for(i=s.length;i--;)s[i].detach();return o&&nt(o),r},setHTML:function(t){var n,r=e.createDocumentFragment(),i=y("DIV");i.innerHTML=t,r.appendChild(i.empty()),It(r,!0),bt(r),Dt(r,"DIV");for(var o=r;o=o.getNextBlock();)o.fixCursor();for(;n=f.lastChild;)f.removeChild(n);f.appendChild(r),f.fixCursor(),q=-1,G=[],Q=0,Y=!1;var s=nt()||I(f.firstChild,0);return it(s),nt(s),m?b=s:k(s),_(s,!0),this},getSelectedText:function(){return L().getTextContent()},insertElement:Vt(Z),insertImage:function(e){var t=y("IMG",{src:e});return Z(t),t},getPath:function(){return W},getSelection:L,setSelection:Vt(k),undo:Vt(ot),redo:Vt(st),hasFormat:at,changeFormat:Vt(ct),bold:Ft(ct,{tag:"B"}),italic:Ft(ct,{tag:"I"}),underline:Ft(ct,{tag:"U"}),removeBold:Ft(ct,null,{tag:"B"}),removeItalic:Ft(ct,null,{tag:"I"}),removeUnderline:Ft(ct,null,{tag:"U"}),makeLink:function(t){t=encodeURI(t);var n=L();if(n.collapsed){var r=t.indexOf(":")+1;if(r)for(;"/"===t[r];)r+=1;n._insertNode(e.createTextNode(t.slice(r)))}return ct({tag:"A",attributes:{href:t}},{tag:"A"},n),K(),this},removeLink:function(){return ct(null,{tag:"A"},L(),!0),K(),this},setFontFace:function(e){return ct({tag:"SPAN",attributes:{"class":"font",style:"font-family: "+e+", sans-serif;"}},{tag:"SPAN",attributes:{"class":"font"}}),K(),this},setFontSize:function(e){return ct({tag:"SPAN",attributes:{"class":"size",style:"font-size: "+("number"==typeof e?e+"px":e)}},{tag:"SPAN",attributes:{"class":"size"}}),K(),this},setTextColour:function(e){return ct({tag:"SPAN",attributes:{"class":"colour",style:"color: "+e}},{tag:"SPAN",attributes:{"class":"colour"}}),K(),this},setHighlightColour:function(e){return ct({tag:"SPAN",attributes:{"class":"highlight",style:"background-color: "+e}},{tag:"SPAN",attributes:{"class":"highlight"}}),K(),this},setTextAlignment:function(e){return ht(function(t){t.className=(t.className.split(/\s+/).filter(function(e){return!/align/.test(e)}).join(" ")+" align-"+e).trim(),t.style.textAlign=e},!0),K(),this},setTextDirection:function(e){return ht(function(t){t.className=(t.className.split(/\s+/).filter(function(e){return!/dir/.test(e)}).join(" ")+" dir-"+e).trim(),t.dir=e},!0),K(),this},forEachBlock:Vt(ht),modifyBlocks:Vt(pt),increaseQuoteLevel:Ft(pt,Nt),decreaseQuoteLevel:Ft(pt,gt),makeUnorderedList:Ft(pt,vt),makeOrderedList:Ft(pt,yt),removeList:Ft(pt,Tt)},f.setAttribute("contenteditable","true"),r.setHTML(""),c.onEditorLoad&&(c.onEditorLoad(c.editor),c.onEditorLoad=null)}(document,UA,DOMTreeWalker); \ No newline at end of file diff --git a/source/Editor.js b/source/Editor.js index aa4019f..36c93fe 100644 --- a/source/Editor.js +++ b/source/Editor.js @@ -1,6 +1,6 @@ /* Copyright © 2011-2012 by Neil Jenkins. Licensed under the MIT license. */ -/*global UA, DOMTreeWalker, Range, top, document, setTimeout */ +/*global UA, DOMTreeWalker, Range, top, document, setTimeout, console */ ( function ( doc, UA, TreeWalker ) { @@ -17,6 +17,7 @@ var win = doc.defaultView; var body = doc.body; + var editor; var isOpera = UA.isOpera; var isGecko = UA.isGecko; @@ -56,7 +57,7 @@ // document node, since these events are fired in a custom manner by the // editor code. var customEvents = { - cut: 1, paste: 1, focus: 1, blur: 1, + focus: 1, blur: 1, pathChange: 1, select: 1, input: 1, undoStateChange: 1 }; @@ -74,10 +75,14 @@ } for ( i = 0, l = handlers.length; i < l; i += 1 ) { obj = handlers[i]; - if ( obj.handleEvent ) { - obj.handleEvent( event ); - } else { - obj( event ); + try { + if ( obj.handleEvent ) { + obj.handleEvent( event ); + } else { + obj( event ); + } + } catch ( error ) { + editor.didError( error ); } } } @@ -1184,24 +1189,28 @@ // --- Cut and Paste --- var afterCut = function () { - // If all content removed, ensure div at start of body. - body.fixCursor(); + try { + // If all content removed, ensure div at start of body. + body.fixCursor(); + } catch ( error ) { + editor.didError( error ); + } }; - doc.addEventListener( isIE ? 'beforecut' : 'cut', function () { + addEventListener( isIE ? 'beforecut' : 'cut', function () { // Save undo checkpoint var range = getSelection(); recordUndoState( range ); getRangeAndRemoveBookmark( range ); setSelection( range ); setTimeout( afterCut, 0 ); - }, false ); + }); // IE sometimes fires the beforepaste event twice; make sure it is not run // again before our after paste function is called. var awaitingPaste = false; - doc.addEventListener( isIE ? 'beforepaste' : 'paste', function ( event ) { + addEventListener( isIE ? 'beforepaste' : 'paste', function ( event ) { if ( awaitingPaste ) { return; } // Treat image paste as a drop of an image file. @@ -1252,52 +1261,57 @@ // single javascript thread, so it will be executed after the // paste event. setTimeout( function () { - // Get the pasted content and clean - var frag = pasteArea.detach().empty(), - first = frag.firstChild, - range = createRange( - startContainer, startOffset, endContainer, endOffset ); + try { + // Get the pasted content and clean + var frag = pasteArea.detach().empty(), + first = frag.firstChild, + range = createRange( + startContainer, startOffset, endContainer, endOffset ); - // Was anything actually pasted? - if ( first ) { - // Safari and IE like putting extra divs around things. - if ( first === frag.lastChild && first.nodeName === 'DIV' ) { - frag.replaceChild( first.empty(), first ); - } - - frag.normalize(); - addLinks( frag ); - cleanTree( frag, false ); - cleanupBRs( frag ); - - var node = frag, - doPaste = true; - while ( node = node.getNextBlock() ) { - node.fixCursor(); - } - - fireEvent( 'willPaste', { - fragment: frag, - preventDefault: function () { - doPaste = false; + // Was anything actually pasted? + if ( first ) { + // Safari and IE like putting extra divs around things. + if ( first === frag.lastChild && + first.nodeName === 'DIV' ) { + frag.replaceChild( first.empty(), first ); } - }); - // Insert pasted data - if ( doPaste ) { - range.insertTreeFragment( frag ); - docWasChanged(); + frag.normalize(); + addLinks( frag ); + cleanTree( frag, false ); + cleanupBRs( frag ); - range.collapse( false ); + var node = frag, + doPaste = true; + while ( node = node.getNextBlock() ) { + node.fixCursor(); + } + + fireEvent( 'willPaste', { + fragment: frag, + preventDefault: function () { + doPaste = false; + } + }); + + // Insert pasted data + if ( doPaste ) { + range.insertTreeFragment( frag ); + docWasChanged(); + + range.collapse( false ); + } } + + setSelection( range ); + updatePath( range, true ); + + awaitingPaste = false; + } catch ( error ) { + editor.didError( error ); } - - setSelection( range ); - updatePath( range, true ); - - awaitingPaste = false; }, 0 ); - }, false ); + }); // --- Keyboard interaction --- @@ -1333,29 +1347,33 @@ // link in Opera, it won't delete the link. Let's make things consistent. If // you delete all text inside an inline tag, remove the inline tag. var afterDelete = function () { - var range = getSelection(), - node = range.startContainer, - parent; - if ( node.nodeType === TEXT_NODE ) { - node = node.parentNode; - } - // If focussed in empty inline element - if ( node.isInline() && !node.textContent ) { - do { - parent = node.parentNode; - } while ( parent.isInline() && - !parent.textContent && ( node = parent ) ); - range.setStart( parent, - indexOf.call( parent.childNodes, node ) ); - range.collapse( true ); - parent.removeChild( node ); - if ( !parent.isBlock() ) { - parent = parent.getPreviousBlock(); + try { + var range = getSelection(), + node = range.startContainer, + parent; + if ( node.nodeType === TEXT_NODE ) { + node = node.parentNode; } - parent.fixCursor(); - range.moveBoundariesDownTree(); - setSelection( range ); - updatePath( range ); + // If focussed in empty inline element + if ( node.isInline() && !node.textContent ) { + do { + parent = node.parentNode; + } while ( parent.isInline() && + !parent.textContent && ( node = parent ) ); + range.setStart( parent, + indexOf.call( parent.childNodes, node ) ); + range.collapse( true ); + parent.removeChild( node ); + if ( !parent.isBlock() ) { + parent = parent.getPreviousBlock(); + } + parent.fixCursor(); + range.moveBoundariesDownTree(); + setSelection( range ); + updatePath( range ); + } + } catch ( error ) { + editor.didError( error ); } }; @@ -1672,7 +1690,11 @@ }; }; - win.editor = { + win.editor = editor = { + + didError: function ( error ) { + console.log( error ); + }, _setPlaceholderTextNode: setPlaceholderTextNode, @@ -1957,7 +1979,7 @@ // --- Initialise --- body.setAttribute( 'contenteditable', 'true' ); - win.editor.setHTML( '' ); + editor.setHTML( '' ); if ( win.onEditorLoad ) { win.onEditorLoad( win.editor ); diff --git a/source/ie8types.js b/source/ie8types.js index ee1d65c..7a3361c 100644 --- a/source/ie8types.js +++ b/source/ie8types.js @@ -1,6 +1,6 @@ /* Copyright © 2011-2012 by Neil Jenkins. Licensed under the MIT license. */ -( function ( undefined ) { +( function () { /*jshint strict: false */