mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 23:40:35 -05:00
1 line
No EOL
7.4 KiB
JavaScript
1 line
No EOL
7.4 KiB
JavaScript
/* Copyright © 2011-2012 by Neil Jenkins. Licensed under the MIT license. */(function(a){Array.prototype.indexOf=function(a,b){var c=this.length;for(var d=b<0?Math.max(0,c+b):b||0;d<c;d+=1)if(this[d]===a)return d;return-1},Array.prototype.forEach=function(a,b){var c=this.length>>>0;if(typeof a!="function")throw new TypeError;for(var d=0;d<c;d+=1)a.call(b,this[d],d,this)},Array.prototype.filter=function(a,b){var c=[];for(var d=0,e=this.length;d<e;d+=1){var f=this[d];a.call(b,f,d,this)&&c.push(f)}return c},Object.keyOf=function(a,b){for(var c in a)if(a[c]===b)return c},Date.now=function(){return+(new Date)},String.prototype.trim=function(){var a=this.replace(/^\s\s*/,""),b=/\s/,c=a.length;while(b.test(a.charAt(c-=1)));return a.slice(0,c+1)}})(),function(){var a=document;window.ie=8,a.defaultView=window;var b={focus:"focusin",blur:"focusout"},c=function(){return!0},d=function(){return!1},e="altKey ctrlKey metaKey shiftKey clientX clientY charCode keyCode".split(" "),f=function(a){var c=a.type,d=document,f=a.srcElement||d,g=(f.ownerDocument||d).documentElement,h=e.length,i;while(h--)i=e[h],this[i]=a[i];c==="propertychange"&&(c=f.nodeName==="INPUT"&&f.type!=="text"&&f.type!=="password"?"change":"input"),this.type=Object.keyOf(b,c)||c,this.target=f,this.pageX=a.clientX+g.scrollLeft,this.pageY=a.clientY+g.scrollTop,a.button&&(this.button=a.button&4?1:a.button&2?2:0,this.which=this.button+1),this.relatedTarget=a.fromElement===f?a.toElement:a.fromElement,this._event=a};f.prototype={constructor:f,isEvent:!0,preventDefault:function(){this.isDefaultPrevented=c,this._event.returnValue=!1},stopPropagation:function(){this.isPropagationStopped=c,this._event.cancelBubble=!0},isDefaultPrevented:d,isPropagationStopped:d},[a,Element.prototype].forEach(function(a){a.addEventListener=function(a,c,d){var e=c._ie_handleEvent||(c._ie_handleEvent=function(){var a=new f(window.event);typeof c=="object"?c.handleEvent(a):c.call(this,a)}),g=/paste|cut/.test(a)?this.body||this:this;c._ie_registeredCount=(c._ie_registeredCount||0)+1,g.attachEvent("on"+(b[a]||a),e)},a.addEventListener.isFake=!0,a.removeEventListener=function(a,c,d){var e=c._ie_handleEvent,f=/paste|cut/.test(a)?this.body||this:this;(c._ie_registeredCount-=1)||delete c._ie_handleEvent,e&&f.detachEvent("on"+(b[a]||a),e)},a.removeEventListener.isFake=!0}),a.defaultView.addEventListener=function(b,c,d){return a.addEventListener(b,c,d)},Object.defineProperty(Element.prototype,"textContent",{get:function(){return this.innerText},set:function(a){this.innerText=a}}),Element.prototype.compareDocumentPosition=function(a){a.nodeType!==1&&(a=a.parentNode);var b=this,c=b!==a,d=b.sourceIndex,e=a.sourceIndex;return(c&&b.contains(a)?16:0)+(c&&a.contains(b)?8:0)+(d<e?4:0)+(e<d?2:0)},HTMLDocument.prototype.normalize=function(){var a=this.childNodes,b=a.length,c;while(b--)c=a[b],c.nodeType===1&&c.normalize()}}();var Range;(function(){var a=Array.prototype.indexOf,b=0,c=1,d=2,e=3,f=function(a,b){while(b=b.parentNode)if(a===b)return!0;return!1},g=function(a,b){var c,d,e,g,h;if(a===b||f(a,b))c=a;else if(f(b,a))c=b;else{d=[],e=[];while(a=a.parentNode)d.push(a);while(b=b.parentNode)e.push(b);g=d.length,h=e.length;while(g--&&h--)if(d[g]!==e[h]){c=d[g+1];break}c||(c=g===-1?d[0]:e[0])}return c};Range=function(a,b,c,d){a=a||document,b=b||0,this.startContainer=a,this.startOffset=b,this.endContainer=c||a,this.endOffset=d!==undefined?d:b,this._updateCollapsedAndAncestor()},Range.prototype={constructor:Range,_updateCollapsedAndAncestor:function(){this.collapsed=this.startContainer===this.endContainer&&this.startOffset===this.endOffset,this.commonAncestorContainer=g(this.startContainer,this.endContainer)},setStart:function(a,b){this.startContainer=a,this.startOffset=b,this._updateCollapsedAndAncestor()},setEnd:function(a,b){this.endContainer=a,this.endOffset=b,this._updateCollapsedAndAncestor()},setStartAfter:function(b){var c=b.parentNode;this.setStart(c,a.call(c.childNodes,b)+1)},setEndBefore:function(b){var c=b.parentNode;this.setEnd(c,a.call(c.childNodes,b))},selectNode:function(b){var c=b.parentNode,d=a.call(c.childNodes,b);this.setStart(c,d),this.setEnd(c,d+1)},selectNodeContents:function(a){this.setStart(a,0),this.setEnd(a,a.childNodes.length)},cloneRange:function(){return new Range(this.startContainer,this.startOffset,this.endContainer,this.endOffset)},collapse:function(a){a?this.setEnd(this.startContainer,this.startOffset):this.setStart(this.endContainer,this.endOffset)},compareBoundaryPoints:function(d,f){var g,h,i,j,k,l;d===b||d===e?(g=this.startContainer,h=this.startOffset):(g=this.endContainer,h=this.endOffset),d===b||d===c?(i=f.startContainer,j=f.startOffset):(i=f.endContainer,j=f.endOffset);if(g===i)return h<j?-1:h>j?1:0;k=g;while(l=k.parentNode){if(l===i)return a.call(l.childNodes,k)<j?-1:1;k=l}k=i;while(l=k.parentNode){if(l===g)return a.call(l.childNodes,k)<h?1:-1;k=l}return g.nodeType!==1&&(g=g.parentNode),i.nodeType!==1&&(i=i.parentNode),g.sourceIndex<i.sourceIndex?-1:g.sourceIndex>i.sourceIndex?1:0}},document.createRange=function(){return new Range};var h=function(a,b){return a===b||f(a,b)},i=function(a){var b=a.nodeType;return b===3||b===4||b===8},j=function(a,b){this.node=a,this.offset=b},k=function(a){var b=a.parentElement(),c,d,e,f;return c=a.duplicate(),c.collapse(!0),d=c.parentElement(),c=a.duplicate(),c.collapse(!1),e=c.parentElement(),f=d===e?d:g(d,e),f===b?f:g(b,f)},l=function(b,c,d,e){var f=b.duplicate();f.collapse(d);var g=f.parentElement();h(c,g)||(g=c);if(!g.canHaveHTML)return new j(g.parentNode,a.call(g.parentNode.childNodes,g));var k=document.createElement("span"),l=d?"StartToStart":"StartToEnd",m,n,o,p,q;do g.insertBefore(k,k.previousSibling),f.moveToElementText(k),m=f.compareEndPoints(l,b);while(m>0&&k.previousSibling);q=k.nextSibling;if(m===-1&&q&&i(q)){f.setEndPoint(d?"EndToStart":"EndToEnd",b);var r;if(/[\r\n]/.test(q.data)||/[\r\n]/.test(f.text)){var s=f.duplicate(),t=s.text.replace(/\r\n/g,"\r").length;r=s.moveStart("character",t);while((m=s.compareEndPoints("StartToEnd",s))===-1)r+=1,s.moveStart("character",1)}else r=f.text.length;p=new j(q,r)}else n=(e||!d)&&k.previousSibling,o=(e||d)&&k.nextSibling,o&&i(o)?p=new j(o,0):n&&i(n)?(n.data,p=new j(n,n.length)):p=new j(g,a.call(g.childNodes,k));return k.parentNode.removeChild(k),p},m=function(a,b){var c,d,e=a.offset,f=document,g,h,j=f.body.createTextRange(),k=i(a.node);return k?(c=a.node,d=c.parentNode):(h=a.node.childNodes,c=e<h.length?h[e]:null,d=a.node),g=f.createElement("span"),g.innerHTML="",c?d.insertBefore(g,c):d.appendChild(g),j.moveToElementText(g),j.collapse(!b),d.removeChild(g),k&&j[b?"moveStart":"moveEnd"]("character",e),j},n=function(a){var b=k(a),c,d;return a.compareEndPoints("StartToEnd",a)===0?c=d=l(a,b,!0,!0):(c=l(a,b,!0,!1),d=l(a,b,!1,!1)),new Range(c.node,c.offset,d.node,d.offset)},o=function(a){var b,c,d;return a.collapsed?b=m(new j(a.startContainer,a.startOffset),!0):(c=m(new j(a.startContainer,a.startOffset),!0),d=m(new j(a.endContainer,a.endOffset),!1),b=document.body.createTextRange(),b.setEndPoint("StartToStart",c),b.setEndPoint("EndToEnd",d)),b},p={rangeCount:1,getRangeAt:function(a){if(a!==0)return undefined;var b=document.selection.createRange();if(b.add){var c=document.createRange();c.moveToElementText(b.item(0)),c.collapse(!1),c.select(),b=c}return n(b)},removeAllRanges:function(){},addRange:function(a){o(a).select()}};document.attachEvent("onbeforeactivate",function(){p.rangeCount=1}),document.attachEvent("ondeactivate",function(){p.rangeCount=0}),window.getSelection=function(){return p}})(); |