openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

combined.min.js (1533256B)


      1 
      2 /* ./modules/cms/ui/themes/default/script/jquery.min.js *//*! jQuery v3.4.1 | (c) JS Foundation and other contributors | jquery.org/license */
      3 !function(e,t){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e,!0):function(e){if(!e.document)throw new Error("jQuery requires a window with a document");return t(e)}:t(e)}("undefined"!=typeof window?window:this,function(C,e){"use strict";var t=[],E=C.document,r=Object.getPrototypeOf,s=t.slice,g=t.concat,u=t.push,i=t.indexOf,n={},o=n.toString,v=n.hasOwnProperty,a=v.toString,l=a.call(Object),y={},m=function(e){return"function"==typeof e&&"number"!=typeof e.nodeType},x=function(e){return null!=e&&e===e.window},c={type:!0,src:!0,nonce:!0,noModule:!0};function b(e,t,n){var r,i,o=(n=n||E).createElement("script");if(o.text=e,t)for(r in c)(i=t[r]||t.getAttribute&&t.getAttribute(r))&&o.setAttribute(r,i);n.head.appendChild(o).parentNode.removeChild(o)}function w(e){return null==e?e+"":"object"==typeof e||"function"==typeof e?n[o.call(e)]||"object":typeof e}var f="3.4.1",k=function(e,t){return new k.fn.init(e,t)},p=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;function d(e){var t=!!e&&"length"in e&&e.length,n=w(e);return!m(e)&&!x(e)&&("array"===n||0===t||"number"==typeof t&&0<t&&t-1 in e)}k.fn=k.prototype={jquery:f,constructor:k,length:0,toArray:function(){return s.call(this)},get:function(e){return null==e?s.call(this):e<0?this[e+this.length]:this[e]},pushStack:function(e){var t=k.merge(this.constructor(),e);return t.prevObject=this,t},each:function(e){return k.each(this,e)},map:function(n){return this.pushStack(k.map(this,function(e,t){return n.call(e,t,e)}))},slice:function(){return this.pushStack(s.apply(this,arguments))},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},eq:function(e){var t=this.length,n=+e+(e<0?t:0);return this.pushStack(0<=n&&n<t?[this[n]]:[])},end:function(){return this.prevObject||this.constructor()},push:u,sort:t.sort,splice:t.splice},k.extend=k.fn.extend=function(){var e,t,n,r,i,o,a=arguments[0]||{},s=1,u=arguments.length,l=!1;for("boolean"==typeof a&&(l=a,a=arguments[s]||{},s++),"object"==typeof a||m(a)||(a={}),s===u&&(a=this,s--);s<u;s++)if(null!=(e=arguments[s]))for(t in e)r=e[t],"__proto__"!==t&&a!==r&&(l&&r&&(k.isPlainObject(r)||(i=Array.isArray(r)))?(n=a[t],o=i&&!Array.isArray(n)?[]:i||k.isPlainObject(n)?n:{},i=!1,a[t]=k.extend(l,o,r)):void 0!==r&&(a[t]=r));return a},k.extend({expando:"jQuery"+(f+Math.random()).replace(/\D/g,""),isReady:!0,error:function(e){throw new Error(e)},noop:function(){},isPlainObject:function(e){var t,n;return!(!e||"[object Object]"!==o.call(e))&&(!(t=r(e))||"function"==typeof(n=v.call(t,"constructor")&&t.constructor)&&a.call(n)===l)},isEmptyObject:function(e){var t;for(t in e)return!1;return!0},globalEval:function(e,t){b(e,{nonce:t&&t.nonce})},each:function(e,t){var n,r=0;if(d(e)){for(n=e.length;r<n;r++)if(!1===t.call(e[r],r,e[r]))break}else for(r in e)if(!1===t.call(e[r],r,e[r]))break;return e},trim:function(e){return null==e?"":(e+"").replace(p,"")},makeArray:function(e,t){var n=t||[];return null!=e&&(d(Object(e))?k.merge(n,"string"==typeof e?[e]:e):u.call(n,e)),n},inArray:function(e,t,n){return null==t?-1:i.call(t,e,n)},merge:function(e,t){for(var n=+t.length,r=0,i=e.length;r<n;r++)e[i++]=t[r];return e.length=i,e},grep:function(e,t,n){for(var r=[],i=0,o=e.length,a=!n;i<o;i++)!t(e[i],i)!==a&&r.push(e[i]);return r},map:function(e,t,n){var r,i,o=0,a=[];if(d(e))for(r=e.length;o<r;o++)null!=(i=t(e[o],o,n))&&a.push(i);else for(o in e)null!=(i=t(e[o],o,n))&&a.push(i);return g.apply([],a)},guid:1,support:y}),"function"==typeof Symbol&&(k.fn[Symbol.iterator]=t[Symbol.iterator]),k.each("Boolean Number String Function Array Date RegExp Object Error Symbol".split(" "),function(e,t){n["[object "+t+"]"]=t.toLowerCase()});var h=function(n){var e,d,b,o,i,h,f,g,w,u,l,T,C,a,E,v,s,c,y,k="sizzle"+1*new Date,m=n.document,S=0,r=0,p=ue(),x=ue(),N=ue(),A=ue(),D=function(e,t){return e===t&&(l=!0),0},j={}.hasOwnProperty,t=[],q=t.pop,L=t.push,H=t.push,O=t.slice,P=function(e,t){for(var n=0,r=e.length;n<r;n++)if(e[n]===t)return n;return-1},R="checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped",M="[\\x20\\t\\r\\n\\f]",I="(?:\\\\.|[\\w-]|[^\0-\\xa0])+",W="\\["+M+"*("+I+")(?:"+M+"*([*^$|!~]?=)"+M+"*(?:'((?:\\\\.|[^\\\\'])*)'|\"((?:\\\\.|[^\\\\\"])*)\"|("+I+"))|)"+M+"*\\]",$=":("+I+")(?:\\((('((?:\\\\.|[^\\\\'])*)'|\"((?:\\\\.|[^\\\\\"])*)\")|((?:\\\\.|[^\\\\()[\\]]|"+W+")*)|.*)\\)|)",F=new RegExp(M+"+","g"),B=new RegExp("^"+M+"+|((?:^|[^\\\\])(?:\\\\.)*)"+M+"+$","g"),_=new RegExp("^"+M+"*,"+M+"*"),z=new RegExp("^"+M+"*([>+~]|"+M+")"+M+"*"),U=new RegExp(M+"|>"),X=new RegExp($),V=new RegExp("^"+I+"$"),G={ID:new RegExp("^#("+I+")"),CLASS:new RegExp("^\\.("+I+")"),TAG:new RegExp("^("+I+"|[*])"),ATTR:new RegExp("^"+W),PSEUDO:new RegExp("^"+$),CHILD:new RegExp("^:(only|first|last|nth|nth-last)-(child|of-type)(?:\\("+M+"*(even|odd|(([+-]|)(\\d*)n|)"+M+"*(?:([+-]|)"+M+"*(\\d+)|))"+M+"*\\)|)","i"),bool:new RegExp("^(?:"+R+")$","i"),needsContext:new RegExp("^"+M+"*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\("+M+"*((?:-\\d)?\\d*)"+M+"*\\)|)(?=[^-]|$)","i")},Y=/HTML$/i,Q=/^(?:input|select|textarea|button)$/i,J=/^h\d$/i,K=/^[^{]+\{\s*\[native \w/,Z=/^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,ee=/[+~]/,te=new RegExp("\\\\([\\da-f]{1,6}"+M+"?|("+M+")|.)","ig"),ne=function(e,t,n){var r="0x"+t-65536;return r!=r||n?t:r<0?String.fromCharCode(r+65536):String.fromCharCode(r>>10|55296,1023&r|56320)},re=/([\0-\x1f\x7f]|^-?\d)|^-$|[^\0-\x1f\x7f-\uFFFF\w-]/g,ie=function(e,t){return t?"\0"===e?"\ufffd":e.slice(0,-1)+"\\"+e.charCodeAt(e.length-1).toString(16)+" ":"\\"+e},oe=function(){T()},ae=be(function(e){return!0===e.disabled&&"fieldset"===e.nodeName.toLowerCase()},{dir:"parentNode",next:"legend"});try{H.apply(t=O.call(m.childNodes),m.childNodes),t[m.childNodes.length].nodeType}catch(e){H={apply:t.length?function(e,t){L.apply(e,O.call(t))}:function(e,t){var n=e.length,r=0;while(e[n++]=t[r++]);e.length=n-1}}}function se(t,e,n,r){var i,o,a,s,u,l,c,f=e&&e.ownerDocument,p=e?e.nodeType:9;if(n=n||[],"string"!=typeof t||!t||1!==p&&9!==p&&11!==p)return n;if(!r&&((e?e.ownerDocument||e:m)!==C&&T(e),e=e||C,E)){if(11!==p&&(u=Z.exec(t)))if(i=u[1]){if(9===p){if(!(a=e.getElementById(i)))return n;if(a.id===i)return n.push(a),n}else if(f&&(a=f.getElementById(i))&&y(e,a)&&a.id===i)return n.push(a),n}else{if(u[2])return H.apply(n,e.getElementsByTagName(t)),n;if((i=u[3])&&d.getElementsByClassName&&e.getElementsByClassName)return H.apply(n,e.getElementsByClassName(i)),n}if(d.qsa&&!A[t+" "]&&(!v||!v.test(t))&&(1!==p||"object"!==e.nodeName.toLowerCase())){if(c=t,f=e,1===p&&U.test(t)){(s=e.getAttribute("id"))?s=s.replace(re,ie):e.setAttribute("id",s=k),o=(l=h(t)).length;while(o--)l[o]="#"+s+" "+xe(l[o]);c=l.join(","),f=ee.test(t)&&ye(e.parentNode)||e}try{return H.apply(n,f.querySelectorAll(c)),n}catch(e){A(t,!0)}finally{s===k&&e.removeAttribute("id")}}}return g(t.replace(B,"$1"),e,n,r)}function ue(){var r=[];return function e(t,n){return r.push(t+" ")>b.cacheLength&&delete e[r.shift()],e[t+" "]=n}}function le(e){return e[k]=!0,e}function ce(e){var t=C.createElement("fieldset");try{return!!e(t)}catch(e){return!1}finally{t.parentNode&&t.parentNode.removeChild(t),t=null}}function fe(e,t){var n=e.split("|"),r=n.length;while(r--)b.attrHandle[n[r]]=t}function pe(e,t){var n=t&&e,r=n&&1===e.nodeType&&1===t.nodeType&&e.sourceIndex-t.sourceIndex;if(r)return r;if(n)while(n=n.nextSibling)if(n===t)return-1;return e?1:-1}function de(t){return function(e){return"input"===e.nodeName.toLowerCase()&&e.type===t}}function he(n){return function(e){var t=e.nodeName.toLowerCase();return("input"===t||"button"===t)&&e.type===n}}function ge(t){return function(e){return"form"in e?e.parentNode&&!1===e.disabled?"label"in e?"label"in e.parentNode?e.parentNode.disabled===t:e.disabled===t:e.isDisabled===t||e.isDisabled!==!t&&ae(e)===t:e.disabled===t:"label"in e&&e.disabled===t}}function ve(a){return le(function(o){return o=+o,le(function(e,t){var n,r=a([],e.length,o),i=r.length;while(i--)e[n=r[i]]&&(e[n]=!(t[n]=e[n]))})})}function ye(e){return e&&"undefined"!=typeof e.getElementsByTagName&&e}for(e in d=se.support={},i=se.isXML=function(e){var t=e.namespaceURI,n=(e.ownerDocument||e).documentElement;return!Y.test(t||n&&n.nodeName||"HTML")},T=se.setDocument=function(e){var t,n,r=e?e.ownerDocument||e:m;return r!==C&&9===r.nodeType&&r.documentElement&&(a=(C=r).documentElement,E=!i(C),m!==C&&(n=C.defaultView)&&n.top!==n&&(n.addEventListener?n.addEventListener("unload",oe,!1):n.attachEvent&&n.attachEvent("onunload",oe)),d.attributes=ce(function(e){return e.className="i",!e.getAttribute("className")}),d.getElementsByTagName=ce(function(e){return e.appendChild(C.createComment("")),!e.getElementsByTagName("*").length}),d.getElementsByClassName=K.test(C.getElementsByClassName),d.getById=ce(function(e){return a.appendChild(e).id=k,!C.getElementsByName||!C.getElementsByName(k).length}),d.getById?(b.filter.ID=function(e){var t=e.replace(te,ne);return function(e){return e.getAttribute("id")===t}},b.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&E){var n=t.getElementById(e);return n?[n]:[]}}):(b.filter.ID=function(e){var n=e.replace(te,ne);return function(e){var t="undefined"!=typeof e.getAttributeNode&&e.getAttributeNode("id");return t&&t.value===n}},b.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&E){var n,r,i,o=t.getElementById(e);if(o){if((n=o.getAttributeNode("id"))&&n.value===e)return[o];i=t.getElementsByName(e),r=0;while(o=i[r++])if((n=o.getAttributeNode("id"))&&n.value===e)return[o]}return[]}}),b.find.TAG=d.getElementsByTagName?function(e,t){return"undefined"!=typeof t.getElementsByTagName?t.getElementsByTagName(e):d.qsa?t.querySelectorAll(e):void 0}:function(e,t){var n,r=[],i=0,o=t.getElementsByTagName(e);if("*"===e){while(n=o[i++])1===n.nodeType&&r.push(n);return r}return o},b.find.CLASS=d.getElementsByClassName&&function(e,t){if("undefined"!=typeof t.getElementsByClassName&&E)return t.getElementsByClassName(e)},s=[],v=[],(d.qsa=K.test(C.querySelectorAll))&&(ce(function(e){a.appendChild(e).innerHTML="<a id='"+k+"'></a><select id='"+k+"-\r\\' msallowcapture=''><option selected=''></option></select>",e.querySelectorAll("[msallowcapture^='']").length&&v.push("[*^$]="+M+"*(?:''|\"\")"),e.querySelectorAll("[selected]").length||v.push("\\["+M+"*(?:value|"+R+")"),e.querySelectorAll("[id~="+k+"-]").length||v.push("~="),e.querySelectorAll(":checked").length||v.push(":checked"),e.querySelectorAll("a#"+k+"+*").length||v.push(".#.+[+~]")}),ce(function(e){e.innerHTML="<a href='' disabled='disabled'></a><select disabled='disabled'><option/></select>";var t=C.createElement("input");t.setAttribute("type","hidden"),e.appendChild(t).setAttribute("name","D"),e.querySelectorAll("[name=d]").length&&v.push("name"+M+"*[*^$|!~]?="),2!==e.querySelectorAll(":enabled").length&&v.push(":enabled",":disabled"),a.appendChild(e).disabled=!0,2!==e.querySelectorAll(":disabled").length&&v.push(":enabled",":disabled"),e.querySelectorAll("*,:x"),v.push(",.*:")})),(d.matchesSelector=K.test(c=a.matches||a.webkitMatchesSelector||a.mozMatchesSelector||a.oMatchesSelector||a.msMatchesSelector))&&ce(function(e){d.disconnectedMatch=c.call(e,"*"),c.call(e,"[s!='']:x"),s.push("!=",$)}),v=v.length&&new RegExp(v.join("|")),s=s.length&&new RegExp(s.join("|")),t=K.test(a.compareDocumentPosition),y=t||K.test(a.contains)?function(e,t){var n=9===e.nodeType?e.documentElement:e,r=t&&t.parentNode;return e===r||!(!r||1!==r.nodeType||!(n.contains?n.contains(r):e.compareDocumentPosition&&16&e.compareDocumentPosition(r)))}:function(e,t){if(t)while(t=t.parentNode)if(t===e)return!0;return!1},D=t?function(e,t){if(e===t)return l=!0,0;var n=!e.compareDocumentPosition-!t.compareDocumentPosition;return n||(1&(n=(e.ownerDocument||e)===(t.ownerDocument||t)?e.compareDocumentPosition(t):1)||!d.sortDetached&&t.compareDocumentPosition(e)===n?e===C||e.ownerDocument===m&&y(m,e)?-1:t===C||t.ownerDocument===m&&y(m,t)?1:u?P(u,e)-P(u,t):0:4&n?-1:1)}:function(e,t){if(e===t)return l=!0,0;var n,r=0,i=e.parentNode,o=t.parentNode,a=[e],s=[t];if(!i||!o)return e===C?-1:t===C?1:i?-1:o?1:u?P(u,e)-P(u,t):0;if(i===o)return pe(e,t);n=e;while(n=n.parentNode)a.unshift(n);n=t;while(n=n.parentNode)s.unshift(n);while(a[r]===s[r])r++;return r?pe(a[r],s[r]):a[r]===m?-1:s[r]===m?1:0}),C},se.matches=function(e,t){return se(e,null,null,t)},se.matchesSelector=function(e,t){if((e.ownerDocument||e)!==C&&T(e),d.matchesSelector&&E&&!A[t+" "]&&(!s||!s.test(t))&&(!v||!v.test(t)))try{var n=c.call(e,t);if(n||d.disconnectedMatch||e.document&&11!==e.document.nodeType)return n}catch(e){A(t,!0)}return 0<se(t,C,null,[e]).length},se.contains=function(e,t){return(e.ownerDocument||e)!==C&&T(e),y(e,t)},se.attr=function(e,t){(e.ownerDocument||e)!==C&&T(e);var n=b.attrHandle[t.toLowerCase()],r=n&&j.call(b.attrHandle,t.toLowerCase())?n(e,t,!E):void 0;return void 0!==r?r:d.attributes||!E?e.getAttribute(t):(r=e.getAttributeNode(t))&&r.specified?r.value:null},se.escape=function(e){return(e+"").replace(re,ie)},se.error=function(e){throw new Error("Syntax error, unrecognized expression: "+e)},se.uniqueSort=function(e){var t,n=[],r=0,i=0;if(l=!d.detectDuplicates,u=!d.sortStable&&e.slice(0),e.sort(D),l){while(t=e[i++])t===e[i]&&(r=n.push(i));while(r--)e.splice(n[r],1)}return u=null,e},o=se.getText=function(e){var t,n="",r=0,i=e.nodeType;if(i){if(1===i||9===i||11===i){if("string"==typeof e.textContent)return e.textContent;for(e=e.firstChild;e;e=e.nextSibling)n+=o(e)}else if(3===i||4===i)return e.nodeValue}else while(t=e[r++])n+=o(t);return n},(b=se.selectors={cacheLength:50,createPseudo:le,match:G,attrHandle:{},find:{},relative:{">":{dir:"parentNode",first:!0}," ":{dir:"parentNode"},"+":{dir:"previousSibling",first:!0},"~":{dir:"previousSibling"}},preFilter:{ATTR:function(e){return e[1]=e[1].replace(te,ne),e[3]=(e[3]||e[4]||e[5]||"").replace(te,ne),"~="===e[2]&&(e[3]=" "+e[3]+" "),e.slice(0,4)},CHILD:function(e){return e[1]=e[1].toLowerCase(),"nth"===e[1].slice(0,3)?(e[3]||se.error(e[0]),e[4]=+(e[4]?e[5]+(e[6]||1):2*("even"===e[3]||"odd"===e[3])),e[5]=+(e[7]+e[8]||"odd"===e[3])):e[3]&&se.error(e[0]),e},PSEUDO:function(e){var t,n=!e[6]&&e[2];return G.CHILD.test(e[0])?null:(e[3]?e[2]=e[4]||e[5]||"":n&&X.test(n)&&(t=h(n,!0))&&(t=n.indexOf(")",n.length-t)-n.length)&&(e[0]=e[0].slice(0,t),e[2]=n.slice(0,t)),e.slice(0,3))}},filter:{TAG:function(e){var t=e.replace(te,ne).toLowerCase();return"*"===e?function(){return!0}:function(e){return e.nodeName&&e.nodeName.toLowerCase()===t}},CLASS:function(e){var t=p[e+" "];return t||(t=new RegExp("(^|"+M+")"+e+"("+M+"|$)"))&&p(e,function(e){return t.test("string"==typeof e.className&&e.className||"undefined"!=typeof e.getAttribute&&e.getAttribute("class")||"")})},ATTR:function(n,r,i){return function(e){var t=se.attr(e,n);return null==t?"!="===r:!r||(t+="","="===r?t===i:"!="===r?t!==i:"^="===r?i&&0===t.indexOf(i):"*="===r?i&&-1<t.indexOf(i):"$="===r?i&&t.slice(-i.length)===i:"~="===r?-1<(" "+t.replace(F," ")+" ").indexOf(i):"|="===r&&(t===i||t.slice(0,i.length+1)===i+"-"))}},CHILD:function(h,e,t,g,v){var y="nth"!==h.slice(0,3),m="last"!==h.slice(-4),x="of-type"===e;return 1===g&&0===v?function(e){return!!e.parentNode}:function(e,t,n){var r,i,o,a,s,u,l=y!==m?"nextSibling":"previousSibling",c=e.parentNode,f=x&&e.nodeName.toLowerCase(),p=!n&&!x,d=!1;if(c){if(y){while(l){a=e;while(a=a[l])if(x?a.nodeName.toLowerCase()===f:1===a.nodeType)return!1;u=l="only"===h&&!u&&"nextSibling"}return!0}if(u=[m?c.firstChild:c.lastChild],m&&p){d=(s=(r=(i=(o=(a=c)[k]||(a[k]={}))[a.uniqueID]||(o[a.uniqueID]={}))[h]||[])[0]===S&&r[1])&&r[2],a=s&&c.childNodes[s];while(a=++s&&a&&a[l]||(d=s=0)||u.pop())if(1===a.nodeType&&++d&&a===e){i[h]=[S,s,d];break}}else if(p&&(d=s=(r=(i=(o=(a=e)[k]||(a[k]={}))[a.uniqueID]||(o[a.uniqueID]={}))[h]||[])[0]===S&&r[1]),!1===d)while(a=++s&&a&&a[l]||(d=s=0)||u.pop())if((x?a.nodeName.toLowerCase()===f:1===a.nodeType)&&++d&&(p&&((i=(o=a[k]||(a[k]={}))[a.uniqueID]||(o[a.uniqueID]={}))[h]=[S,d]),a===e))break;return(d-=v)===g||d%g==0&&0<=d/g}}},PSEUDO:function(e,o){var t,a=b.pseudos[e]||b.setFilters[e.toLowerCase()]||se.error("unsupported pseudo: "+e);return a[k]?a(o):1<a.length?(t=[e,e,"",o],b.setFilters.hasOwnProperty(e.toLowerCase())?le(function(e,t){var n,r=a(e,o),i=r.length;while(i--)e[n=P(e,r[i])]=!(t[n]=r[i])}):function(e){return a(e,0,t)}):a}},pseudos:{not:le(function(e){var r=[],i=[],s=f(e.replace(B,"$1"));return s[k]?le(function(e,t,n,r){var i,o=s(e,null,r,[]),a=e.length;while(a--)(i=o[a])&&(e[a]=!(t[a]=i))}):function(e,t,n){return r[0]=e,s(r,null,n,i),r[0]=null,!i.pop()}}),has:le(function(t){return function(e){return 0<se(t,e).length}}),contains:le(function(t){return t=t.replace(te,ne),function(e){return-1<(e.textContent||o(e)).indexOf(t)}}),lang:le(function(n){return V.test(n||"")||se.error("unsupported lang: "+n),n=n.replace(te,ne).toLowerCase(),function(e){var t;do{if(t=E?e.lang:e.getAttribute("xml:lang")||e.getAttribute("lang"))return(t=t.toLowerCase())===n||0===t.indexOf(n+"-")}while((e=e.parentNode)&&1===e.nodeType);return!1}}),target:function(e){var t=n.location&&n.location.hash;return t&&t.slice(1)===e.id},root:function(e){return e===a},focus:function(e){return e===C.activeElement&&(!C.hasFocus||C.hasFocus())&&!!(e.type||e.href||~e.tabIndex)},enabled:ge(!1),disabled:ge(!0),checked:function(e){var t=e.nodeName.toLowerCase();return"input"===t&&!!e.checked||"option"===t&&!!e.selected},selected:function(e){return e.parentNode&&e.parentNode.selectedIndex,!0===e.selected},empty:function(e){for(e=e.firstChild;e;e=e.nextSibling)if(e.nodeType<6)return!1;return!0},parent:function(e){return!b.pseudos.empty(e)},header:function(e){return J.test(e.nodeName)},input:function(e){return Q.test(e.nodeName)},button:function(e){var t=e.nodeName.toLowerCase();return"input"===t&&"button"===e.type||"button"===t},text:function(e){var t;return"input"===e.nodeName.toLowerCase()&&"text"===e.type&&(null==(t=e.getAttribute("type"))||"text"===t.toLowerCase())},first:ve(function(){return[0]}),last:ve(function(e,t){return[t-1]}),eq:ve(function(e,t,n){return[n<0?n+t:n]}),even:ve(function(e,t){for(var n=0;n<t;n+=2)e.push(n);return e}),odd:ve(function(e,t){for(var n=1;n<t;n+=2)e.push(n);return e}),lt:ve(function(e,t,n){for(var r=n<0?n+t:t<n?t:n;0<=--r;)e.push(r);return e}),gt:ve(function(e,t,n){for(var r=n<0?n+t:n;++r<t;)e.push(r);return e})}}).pseudos.nth=b.pseudos.eq,{radio:!0,checkbox:!0,file:!0,password:!0,image:!0})b.pseudos[e]=de(e);for(e in{submit:!0,reset:!0})b.pseudos[e]=he(e);function me(){}function xe(e){for(var t=0,n=e.length,r="";t<n;t++)r+=e[t].value;return r}function be(s,e,t){var u=e.dir,l=e.next,c=l||u,f=t&&"parentNode"===c,p=r++;return e.first?function(e,t,n){while(e=e[u])if(1===e.nodeType||f)return s(e,t,n);return!1}:function(e,t,n){var r,i,o,a=[S,p];if(n){while(e=e[u])if((1===e.nodeType||f)&&s(e,t,n))return!0}else while(e=e[u])if(1===e.nodeType||f)if(i=(o=e[k]||(e[k]={}))[e.uniqueID]||(o[e.uniqueID]={}),l&&l===e.nodeName.toLowerCase())e=e[u]||e;else{if((r=i[c])&&r[0]===S&&r[1]===p)return a[2]=r[2];if((i[c]=a)[2]=s(e,t,n))return!0}return!1}}function we(i){return 1<i.length?function(e,t,n){var r=i.length;while(r--)if(!i[r](e,t,n))return!1;return!0}:i[0]}function Te(e,t,n,r,i){for(var o,a=[],s=0,u=e.length,l=null!=t;s<u;s++)(o=e[s])&&(n&&!n(o,r,i)||(a.push(o),l&&t.push(s)));return a}function Ce(d,h,g,v,y,e){return v&&!v[k]&&(v=Ce(v)),y&&!y[k]&&(y=Ce(y,e)),le(function(e,t,n,r){var i,o,a,s=[],u=[],l=t.length,c=e||function(e,t,n){for(var r=0,i=t.length;r<i;r++)se(e,t[r],n);return n}(h||"*",n.nodeType?[n]:n,[]),f=!d||!e&&h?c:Te(c,s,d,n,r),p=g?y||(e?d:l||v)?[]:t:f;if(g&&g(f,p,n,r),v){i=Te(p,u),v(i,[],n,r),o=i.length;while(o--)(a=i[o])&&(p[u[o]]=!(f[u[o]]=a))}if(e){if(y||d){if(y){i=[],o=p.length;while(o--)(a=p[o])&&i.push(f[o]=a);y(null,p=[],i,r)}o=p.length;while(o--)(a=p[o])&&-1<(i=y?P(e,a):s[o])&&(e[i]=!(t[i]=a))}}else p=Te(p===t?p.splice(l,p.length):p),y?y(null,t,p,r):H.apply(t,p)})}function Ee(e){for(var i,t,n,r=e.length,o=b.relative[e[0].type],a=o||b.relative[" "],s=o?1:0,u=be(function(e){return e===i},a,!0),l=be(function(e){return-1<P(i,e)},a,!0),c=[function(e,t,n){var r=!o&&(n||t!==w)||((i=t).nodeType?u(e,t,n):l(e,t,n));return i=null,r}];s<r;s++)if(t=b.relative[e[s].type])c=[be(we(c),t)];else{if((t=b.filter[e[s].type].apply(null,e[s].matches))[k]){for(n=++s;n<r;n++)if(b.relative[e[n].type])break;return Ce(1<s&&we(c),1<s&&xe(e.slice(0,s-1).concat({value:" "===e[s-2].type?"*":""})).replace(B,"$1"),t,s<n&&Ee(e.slice(s,n)),n<r&&Ee(e=e.slice(n)),n<r&&xe(e))}c.push(t)}return we(c)}return me.prototype=b.filters=b.pseudos,b.setFilters=new me,h=se.tokenize=function(e,t){var n,r,i,o,a,s,u,l=x[e+" "];if(l)return t?0:l.slice(0);a=e,s=[],u=b.preFilter;while(a){for(o in n&&!(r=_.exec(a))||(r&&(a=a.slice(r[0].length)||a),s.push(i=[])),n=!1,(r=z.exec(a))&&(n=r.shift(),i.push({value:n,type:r[0].replace(B," ")}),a=a.slice(n.length)),b.filter)!(r=G[o].exec(a))||u[o]&&!(r=u[o](r))||(n=r.shift(),i.push({value:n,type:o,matches:r}),a=a.slice(n.length));if(!n)break}return t?a.length:a?se.error(e):x(e,s).slice(0)},f=se.compile=function(e,t){var n,v,y,m,x,r,i=[],o=[],a=N[e+" "];if(!a){t||(t=h(e)),n=t.length;while(n--)(a=Ee(t[n]))[k]?i.push(a):o.push(a);(a=N(e,(v=o,m=0<(y=i).length,x=0<v.length,r=function(e,t,n,r,i){var o,a,s,u=0,l="0",c=e&&[],f=[],p=w,d=e||x&&b.find.TAG("*",i),h=S+=null==p?1:Math.random()||.1,g=d.length;for(i&&(w=t===C||t||i);l!==g&&null!=(o=d[l]);l++){if(x&&o){a=0,t||o.ownerDocument===C||(T(o),n=!E);while(s=v[a++])if(s(o,t||C,n)){r.push(o);break}i&&(S=h)}m&&((o=!s&&o)&&u--,e&&c.push(o))}if(u+=l,m&&l!==u){a=0;while(s=y[a++])s(c,f,t,n);if(e){if(0<u)while(l--)c[l]||f[l]||(f[l]=q.call(r));f=Te(f)}H.apply(r,f),i&&!e&&0<f.length&&1<u+y.length&&se.uniqueSort(r)}return i&&(S=h,w=p),c},m?le(r):r))).selector=e}return a},g=se.select=function(e,t,n,r){var i,o,a,s,u,l="function"==typeof e&&e,c=!r&&h(e=l.selector||e);if(n=n||[],1===c.length){if(2<(o=c[0]=c[0].slice(0)).length&&"ID"===(a=o[0]).type&&9===t.nodeType&&E&&b.relative[o[1].type]){if(!(t=(b.find.ID(a.matches[0].replace(te,ne),t)||[])[0]))return n;l&&(t=t.parentNode),e=e.slice(o.shift().value.length)}i=G.needsContext.test(e)?0:o.length;while(i--){if(a=o[i],b.relative[s=a.type])break;if((u=b.find[s])&&(r=u(a.matches[0].replace(te,ne),ee.test(o[0].type)&&ye(t.parentNode)||t))){if(o.splice(i,1),!(e=r.length&&xe(o)))return H.apply(n,r),n;break}}}return(l||f(e,c))(r,t,!E,n,!t||ee.test(e)&&ye(t.parentNode)||t),n},d.sortStable=k.split("").sort(D).join("")===k,d.detectDuplicates=!!l,T(),d.sortDetached=ce(function(e){return 1&e.compareDocumentPosition(C.createElement("fieldset"))}),ce(function(e){return e.innerHTML="<a href='#'></a>","#"===e.firstChild.getAttribute("href")})||fe("type|href|height|width",function(e,t,n){if(!n)return e.getAttribute(t,"type"===t.toLowerCase()?1:2)}),d.attributes&&ce(function(e){return e.innerHTML="<input/>",e.firstChild.setAttribute("value",""),""===e.firstChild.getAttribute("value")})||fe("value",function(e,t,n){if(!n&&"input"===e.nodeName.toLowerCase())return e.defaultValue}),ce(function(e){return null==e.getAttribute("disabled")})||fe(R,function(e,t,n){var r;if(!n)return!0===e[t]?t.toLowerCase():(r=e.getAttributeNode(t))&&r.specified?r.value:null}),se}(C);k.find=h,k.expr=h.selectors,k.expr[":"]=k.expr.pseudos,k.uniqueSort=k.unique=h.uniqueSort,k.text=h.getText,k.isXMLDoc=h.isXML,k.contains=h.contains,k.escapeSelector=h.escape;var T=function(e,t,n){var r=[],i=void 0!==n;while((e=e[t])&&9!==e.nodeType)if(1===e.nodeType){if(i&&k(e).is(n))break;r.push(e)}return r},S=function(e,t){for(var n=[];e;e=e.nextSibling)1===e.nodeType&&e!==t&&n.push(e);return n},N=k.expr.match.needsContext;function A(e,t){return e.nodeName&&e.nodeName.toLowerCase()===t.toLowerCase()}var D=/^<([a-z][^\/\0>:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)$/i;function j(e,n,r){return m(n)?k.grep(e,function(e,t){return!!n.call(e,t,e)!==r}):n.nodeType?k.grep(e,function(e){return e===n!==r}):"string"!=typeof n?k.grep(e,function(e){return-1<i.call(n,e)!==r}):k.filter(n,e,r)}k.filter=function(e,t,n){var r=t[0];return n&&(e=":not("+e+")"),1===t.length&&1===r.nodeType?k.find.matchesSelector(r,e)?[r]:[]:k.find.matches(e,k.grep(t,function(e){return 1===e.nodeType}))},k.fn.extend({find:function(e){var t,n,r=this.length,i=this;if("string"!=typeof e)return this.pushStack(k(e).filter(function(){for(t=0;t<r;t++)if(k.contains(i[t],this))return!0}));for(n=this.pushStack([]),t=0;t<r;t++)k.find(e,i[t],n);return 1<r?k.uniqueSort(n):n},filter:function(e){return this.pushStack(j(this,e||[],!1))},not:function(e){return this.pushStack(j(this,e||[],!0))},is:function(e){return!!j(this,"string"==typeof e&&N.test(e)?k(e):e||[],!1).length}});var q,L=/^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]+))$/;(k.fn.init=function(e,t,n){var r,i;if(!e)return this;if(n=n||q,"string"==typeof e){if(!(r="<"===e[0]&&">"===e[e.length-1]&&3<=e.length?[null,e,null]:L.exec(e))||!r[1]&&t)return!t||t.jquery?(t||n).find(e):this.constructor(t).find(e);if(r[1]){if(t=t instanceof k?t[0]:t,k.merge(this,k.parseHTML(r[1],t&&t.nodeType?t.ownerDocument||t:E,!0)),D.test(r[1])&&k.isPlainObject(t))for(r in t)m(this[r])?this[r](t[r]):this.attr(r,t[r]);return this}return(i=E.getElementById(r[2]))&&(this[0]=i,this.length=1),this}return e.nodeType?(this[0]=e,this.length=1,this):m(e)?void 0!==n.ready?n.ready(e):e(k):k.makeArray(e,this)}).prototype=k.fn,q=k(E);var H=/^(?:parents|prev(?:Until|All))/,O={children:!0,contents:!0,next:!0,prev:!0};function P(e,t){while((e=e[t])&&1!==e.nodeType);return e}k.fn.extend({has:function(e){var t=k(e,this),n=t.length;return this.filter(function(){for(var e=0;e<n;e++)if(k.contains(this,t[e]))return!0})},closest:function(e,t){var n,r=0,i=this.length,o=[],a="string"!=typeof e&&k(e);if(!N.test(e))for(;r<i;r++)for(n=this[r];n&&n!==t;n=n.parentNode)if(n.nodeType<11&&(a?-1<a.index(n):1===n.nodeType&&k.find.matchesSelector(n,e))){o.push(n);break}return this.pushStack(1<o.length?k.uniqueSort(o):o)},index:function(e){return e?"string"==typeof e?i.call(k(e),this[0]):i.call(this,e.jquery?e[0]:e):this[0]&&this[0].parentNode?this.first().prevAll().length:-1},add:function(e,t){return this.pushStack(k.uniqueSort(k.merge(this.get(),k(e,t))))},addBack:function(e){return this.add(null==e?this.prevObject:this.prevObject.filter(e))}}),k.each({parent:function(e){var t=e.parentNode;return t&&11!==t.nodeType?t:null},parents:function(e){return T(e,"parentNode")},parentsUntil:function(e,t,n){return T(e,"parentNode",n)},next:function(e){return P(e,"nextSibling")},prev:function(e){return P(e,"previousSibling")},nextAll:function(e){return T(e,"nextSibling")},prevAll:function(e){return T(e,"previousSibling")},nextUntil:function(e,t,n){return T(e,"nextSibling",n)},prevUntil:function(e,t,n){return T(e,"previousSibling",n)},siblings:function(e){return S((e.parentNode||{}).firstChild,e)},children:function(e){return S(e.firstChild)},contents:function(e){return"undefined"!=typeof e.contentDocument?e.contentDocument:(A(e,"template")&&(e=e.content||e),k.merge([],e.childNodes))}},function(r,i){k.fn[r]=function(e,t){var n=k.map(this,i,e);return"Until"!==r.slice(-5)&&(t=e),t&&"string"==typeof t&&(n=k.filter(t,n)),1<this.length&&(O[r]||k.uniqueSort(n),H.test(r)&&n.reverse()),this.pushStack(n)}});var R=/[^\x20\t\r\n\f]+/g;function M(e){return e}function I(e){throw e}function W(e,t,n,r){var i;try{e&&m(i=e.promise)?i.call(e).done(t).fail(n):e&&m(i=e.then)?i.call(e,t,n):t.apply(void 0,[e].slice(r))}catch(e){n.apply(void 0,[e])}}k.Callbacks=function(r){var e,n;r="string"==typeof r?(e=r,n={},k.each(e.match(R)||[],function(e,t){n[t]=!0}),n):k.extend({},r);var i,t,o,a,s=[],u=[],l=-1,c=function(){for(a=a||r.once,o=i=!0;u.length;l=-1){t=u.shift();while(++l<s.length)!1===s[l].apply(t[0],t[1])&&r.stopOnFalse&&(l=s.length,t=!1)}r.memory||(t=!1),i=!1,a&&(s=t?[]:"")},f={add:function(){return s&&(t&&!i&&(l=s.length-1,u.push(t)),function n(e){k.each(e,function(e,t){m(t)?r.unique&&f.has(t)||s.push(t):t&&t.length&&"string"!==w(t)&&n(t)})}(arguments),t&&!i&&c()),this},remove:function(){return k.each(arguments,function(e,t){var n;while(-1<(n=k.inArray(t,s,n)))s.splice(n,1),n<=l&&l--}),this},has:function(e){return e?-1<k.inArray(e,s):0<s.length},empty:function(){return s&&(s=[]),this},disable:function(){return a=u=[],s=t="",this},disabled:function(){return!s},lock:function(){return a=u=[],t||i||(s=t=""),this},locked:function(){return!!a},fireWith:function(e,t){return a||(t=[e,(t=t||[]).slice?t.slice():t],u.push(t),i||c()),this},fire:function(){return f.fireWith(this,arguments),this},fired:function(){return!!o}};return f},k.extend({Deferred:function(e){var o=[["notify","progress",k.Callbacks("memory"),k.Callbacks("memory"),2],["resolve","done",k.Callbacks("once memory"),k.Callbacks("once memory"),0,"resolved"],["reject","fail",k.Callbacks("once memory"),k.Callbacks("once memory"),1,"rejected"]],i="pending",a={state:function(){return i},always:function(){return s.done(arguments).fail(arguments),this},"catch":function(e){return a.then(null,e)},pipe:function(){var i=arguments;return k.Deferred(function(r){k.each(o,function(e,t){var n=m(i[t[4]])&&i[t[4]];s[t[1]](function(){var e=n&&n.apply(this,arguments);e&&m(e.promise)?e.promise().progress(r.notify).done(r.resolve).fail(r.reject):r[t[0]+"With"](this,n?[e]:arguments)})}),i=null}).promise()},then:function(t,n,r){var u=0;function l(i,o,a,s){return function(){var n=this,r=arguments,e=function(){var e,t;if(!(i<u)){if((e=a.apply(n,r))===o.promise())throw new TypeError("Thenable self-resolution");t=e&&("object"==typeof e||"function"==typeof e)&&e.then,m(t)?s?t.call(e,l(u,o,M,s),l(u,o,I,s)):(u++,t.call(e,l(u,o,M,s),l(u,o,I,s),l(u,o,M,o.notifyWith))):(a!==M&&(n=void 0,r=[e]),(s||o.resolveWith)(n,r))}},t=s?e:function(){try{e()}catch(e){k.Deferred.exceptionHook&&k.Deferred.exceptionHook(e,t.stackTrace),u<=i+1&&(a!==I&&(n=void 0,r=[e]),o.rejectWith(n,r))}};i?t():(k.Deferred.getStackHook&&(t.stackTrace=k.Deferred.getStackHook()),C.setTimeout(t))}}return k.Deferred(function(e){o[0][3].add(l(0,e,m(r)?r:M,e.notifyWith)),o[1][3].add(l(0,e,m(t)?t:M)),o[2][3].add(l(0,e,m(n)?n:I))}).promise()},promise:function(e){return null!=e?k.extend(e,a):a}},s={};return k.each(o,function(e,t){var n=t[2],r=t[5];a[t[1]]=n.add,r&&n.add(function(){i=r},o[3-e][2].disable,o[3-e][3].disable,o[0][2].lock,o[0][3].lock),n.add(t[3].fire),s[t[0]]=function(){return s[t[0]+"With"](this===s?void 0:this,arguments),this},s[t[0]+"With"]=n.fireWith}),a.promise(s),e&&e.call(s,s),s},when:function(e){var n=arguments.length,t=n,r=Array(t),i=s.call(arguments),o=k.Deferred(),a=function(t){return function(e){r[t]=this,i[t]=1<arguments.length?s.call(arguments):e,--n||o.resolveWith(r,i)}};if(n<=1&&(W(e,o.done(a(t)).resolve,o.reject,!n),"pending"===o.state()||m(i[t]&&i[t].then)))return o.then();while(t--)W(i[t],a(t),o.reject);return o.promise()}});var $=/^(Eval|Internal|Range|Reference|Syntax|Type|URI)Error$/;k.Deferred.exceptionHook=function(e,t){C.console&&C.console.warn&&e&&$.test(e.name)&&C.console.warn("jQuery.Deferred exception: "+e.message,e.stack,t)},k.readyException=function(e){C.setTimeout(function(){throw e})};var F=k.Deferred();function B(){E.removeEventListener("DOMContentLoaded",B),C.removeEventListener("load",B),k.ready()}k.fn.ready=function(e){return F.then(e)["catch"](function(e){k.readyException(e)}),this},k.extend({isReady:!1,readyWait:1,ready:function(e){(!0===e?--k.readyWait:k.isReady)||(k.isReady=!0)!==e&&0<--k.readyWait||F.resolveWith(E,[k])}}),k.ready.then=F.then,"complete"===E.readyState||"loading"!==E.readyState&&!E.documentElement.doScroll?C.setTimeout(k.ready):(E.addEventListener("DOMContentLoaded",B),C.addEventListener("load",B));var _=function(e,t,n,r,i,o,a){var s=0,u=e.length,l=null==n;if("object"===w(n))for(s in i=!0,n)_(e,t,s,n[s],!0,o,a);else if(void 0!==r&&(i=!0,m(r)||(a=!0),l&&(a?(t.call(e,r),t=null):(l=t,t=function(e,t,n){return l.call(k(e),n)})),t))for(;s<u;s++)t(e[s],n,a?r:r.call(e[s],s,t(e[s],n)));return i?e:l?t.call(e):u?t(e[0],n):o},z=/^-ms-/,U=/-([a-z])/g;function X(e,t){return t.toUpperCase()}function V(e){return e.replace(z,"ms-").replace(U,X)}var G=function(e){return 1===e.nodeType||9===e.nodeType||!+e.nodeType};function Y(){this.expando=k.expando+Y.uid++}Y.uid=1,Y.prototype={cache:function(e){var t=e[this.expando];return t||(t={},G(e)&&(e.nodeType?e[this.expando]=t:Object.defineProperty(e,this.expando,{value:t,configurable:!0}))),t},set:function(e,t,n){var r,i=this.cache(e);if("string"==typeof t)i[V(t)]=n;else for(r in t)i[V(r)]=t[r];return i},get:function(e,t){return void 0===t?this.cache(e):e[this.expando]&&e[this.expando][V(t)]},access:function(e,t,n){return void 0===t||t&&"string"==typeof t&&void 0===n?this.get(e,t):(this.set(e,t,n),void 0!==n?n:t)},remove:function(e,t){var n,r=e[this.expando];if(void 0!==r){if(void 0!==t){n=(t=Array.isArray(t)?t.map(V):(t=V(t))in r?[t]:t.match(R)||[]).length;while(n--)delete r[t[n]]}(void 0===t||k.isEmptyObject(r))&&(e.nodeType?e[this.expando]=void 0:delete e[this.expando])}},hasData:function(e){var t=e[this.expando];return void 0!==t&&!k.isEmptyObject(t)}};var Q=new Y,J=new Y,K=/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,Z=/[A-Z]/g;function ee(e,t,n){var r,i;if(void 0===n&&1===e.nodeType)if(r="data-"+t.replace(Z,"-$&").toLowerCase(),"string"==typeof(n=e.getAttribute(r))){try{n="true"===(i=n)||"false"!==i&&("null"===i?null:i===+i+""?+i:K.test(i)?JSON.parse(i):i)}catch(e){}J.set(e,t,n)}else n=void 0;return n}k.extend({hasData:function(e){return J.hasData(e)||Q.hasData(e)},data:function(e,t,n){return J.access(e,t,n)},removeData:function(e,t){J.remove(e,t)},_data:function(e,t,n){return Q.access(e,t,n)},_removeData:function(e,t){Q.remove(e,t)}}),k.fn.extend({data:function(n,e){var t,r,i,o=this[0],a=o&&o.attributes;if(void 0===n){if(this.length&&(i=J.get(o),1===o.nodeType&&!Q.get(o,"hasDataAttrs"))){t=a.length;while(t--)a[t]&&0===(r=a[t].name).indexOf("data-")&&(r=V(r.slice(5)),ee(o,r,i[r]));Q.set(o,"hasDataAttrs",!0)}return i}return"object"==typeof n?this.each(function(){J.set(this,n)}):_(this,function(e){var t;if(o&&void 0===e)return void 0!==(t=J.get(o,n))?t:void 0!==(t=ee(o,n))?t:void 0;this.each(function(){J.set(this,n,e)})},null,e,1<arguments.length,null,!0)},removeData:function(e){return this.each(function(){J.remove(this,e)})}}),k.extend({queue:function(e,t,n){var r;if(e)return t=(t||"fx")+"queue",r=Q.get(e,t),n&&(!r||Array.isArray(n)?r=Q.access(e,t,k.makeArray(n)):r.push(n)),r||[]},dequeue:function(e,t){t=t||"fx";var n=k.queue(e,t),r=n.length,i=n.shift(),o=k._queueHooks(e,t);"inprogress"===i&&(i=n.shift(),r--),i&&("fx"===t&&n.unshift("inprogress"),delete o.stop,i.call(e,function(){k.dequeue(e,t)},o)),!r&&o&&o.empty.fire()},_queueHooks:function(e,t){var n=t+"queueHooks";return Q.get(e,n)||Q.access(e,n,{empty:k.Callbacks("once memory").add(function(){Q.remove(e,[t+"queue",n])})})}}),k.fn.extend({queue:function(t,n){var e=2;return"string"!=typeof t&&(n=t,t="fx",e--),arguments.length<e?k.queue(this[0],t):void 0===n?this:this.each(function(){var e=k.queue(this,t,n);k._queueHooks(this,t),"fx"===t&&"inprogress"!==e[0]&&k.dequeue(this,t)})},dequeue:function(e){return this.each(function(){k.dequeue(this,e)})},clearQueue:function(e){return this.queue(e||"fx",[])},promise:function(e,t){var n,r=1,i=k.Deferred(),o=this,a=this.length,s=function(){--r||i.resolveWith(o,[o])};"string"!=typeof e&&(t=e,e=void 0),e=e||"fx";while(a--)(n=Q.get(o[a],e+"queueHooks"))&&n.empty&&(r++,n.empty.add(s));return s(),i.promise(t)}});var te=/[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source,ne=new RegExp("^(?:([+-])=|)("+te+")([a-z%]*)$","i"),re=["Top","Right","Bottom","Left"],ie=E.documentElement,oe=function(e){return k.contains(e.ownerDocument,e)},ae={composed:!0};ie.getRootNode&&(oe=function(e){return k.contains(e.ownerDocument,e)||e.getRootNode(ae)===e.ownerDocument});var se=function(e,t){return"none"===(e=t||e).style.display||""===e.style.display&&oe(e)&&"none"===k.css(e,"display")},ue=function(e,t,n,r){var i,o,a={};for(o in t)a[o]=e.style[o],e.style[o]=t[o];for(o in i=n.apply(e,r||[]),t)e.style[o]=a[o];return i};function le(e,t,n,r){var i,o,a=20,s=r?function(){return r.cur()}:function(){return k.css(e,t,"")},u=s(),l=n&&n[3]||(k.cssNumber[t]?"":"px"),c=e.nodeType&&(k.cssNumber[t]||"px"!==l&&+u)&&ne.exec(k.css(e,t));if(c&&c[3]!==l){u/=2,l=l||c[3],c=+u||1;while(a--)k.style(e,t,c+l),(1-o)*(1-(o=s()/u||.5))<=0&&(a=0),c/=o;c*=2,k.style(e,t,c+l),n=n||[]}return n&&(c=+c||+u||0,i=n[1]?c+(n[1]+1)*n[2]:+n[2],r&&(r.unit=l,r.start=c,r.end=i)),i}var ce={};function fe(e,t){for(var n,r,i,o,a,s,u,l=[],c=0,f=e.length;c<f;c++)(r=e[c]).style&&(n=r.style.display,t?("none"===n&&(l[c]=Q.get(r,"display")||null,l[c]||(r.style.display="")),""===r.style.display&&se(r)&&(l[c]=(u=a=o=void 0,a=(i=r).ownerDocument,s=i.nodeName,(u=ce[s])||(o=a.body.appendChild(a.createElement(s)),u=k.css(o,"display"),o.parentNode.removeChild(o),"none"===u&&(u="block"),ce[s]=u)))):"none"!==n&&(l[c]="none",Q.set(r,"display",n)));for(c=0;c<f;c++)null!=l[c]&&(e[c].style.display=l[c]);return e}k.fn.extend({show:function(){return fe(this,!0)},hide:function(){return fe(this)},toggle:function(e){return"boolean"==typeof e?e?this.show():this.hide():this.each(function(){se(this)?k(this).show():k(this).hide()})}});var pe=/^(?:checkbox|radio)$/i,de=/<([a-z][^\/\0>\x20\t\r\n\f]*)/i,he=/^$|^module$|\/(?:java|ecma)script/i,ge={option:[1,"<select multiple='multiple'>","</select>"],thead:[1,"<table>","</table>"],col:[2,"<table><colgroup>","</colgroup></table>"],tr:[2,"<table><tbody>","</tbody></table>"],td:[3,"<table><tbody><tr>","</tr></tbody></table>"],_default:[0,"",""]};function ve(e,t){var n;return n="undefined"!=typeof e.getElementsByTagName?e.getElementsByTagName(t||"*"):"undefined"!=typeof e.querySelectorAll?e.querySelectorAll(t||"*"):[],void 0===t||t&&A(e,t)?k.merge([e],n):n}function ye(e,t){for(var n=0,r=e.length;n<r;n++)Q.set(e[n],"globalEval",!t||Q.get(t[n],"globalEval"))}ge.optgroup=ge.option,ge.tbody=ge.tfoot=ge.colgroup=ge.caption=ge.thead,ge.th=ge.td;var me,xe,be=/<|&#?\w+;/;function we(e,t,n,r,i){for(var o,a,s,u,l,c,f=t.createDocumentFragment(),p=[],d=0,h=e.length;d<h;d++)if((o=e[d])||0===o)if("object"===w(o))k.merge(p,o.nodeType?[o]:o);else if(be.test(o)){a=a||f.appendChild(t.createElement("div")),s=(de.exec(o)||["",""])[1].toLowerCase(),u=ge[s]||ge._default,a.innerHTML=u[1]+k.htmlPrefilter(o)+u[2],c=u[0];while(c--)a=a.lastChild;k.merge(p,a.childNodes),(a=f.firstChild).textContent=""}else p.push(t.createTextNode(o));f.textContent="",d=0;while(o=p[d++])if(r&&-1<k.inArray(o,r))i&&i.push(o);else if(l=oe(o),a=ve(f.appendChild(o),"script"),l&&ye(a),n){c=0;while(o=a[c++])he.test(o.type||"")&&n.push(o)}return f}me=E.createDocumentFragment().appendChild(E.createElement("div")),(xe=E.createElement("input")).setAttribute("type","radio"),xe.setAttribute("checked","checked"),xe.setAttribute("name","t"),me.appendChild(xe),y.checkClone=me.cloneNode(!0).cloneNode(!0).lastChild.checked,me.innerHTML="<textarea>x</textarea>",y.noCloneChecked=!!me.cloneNode(!0).lastChild.defaultValue;var Te=/^key/,Ce=/^(?:mouse|pointer|contextmenu|drag|drop)|click/,Ee=/^([^.]*)(?:\.(.+)|)/;function ke(){return!0}function Se(){return!1}function Ne(e,t){return e===function(){try{return E.activeElement}catch(e){}}()==("focus"===t)}function Ae(e,t,n,r,i,o){var a,s;if("object"==typeof t){for(s in"string"!=typeof n&&(r=r||n,n=void 0),t)Ae(e,s,n,r,t[s],o);return e}if(null==r&&null==i?(i=n,r=n=void 0):null==i&&("string"==typeof n?(i=r,r=void 0):(i=r,r=n,n=void 0)),!1===i)i=Se;else if(!i)return e;return 1===o&&(a=i,(i=function(e){return k().off(e),a.apply(this,arguments)}).guid=a.guid||(a.guid=k.guid++)),e.each(function(){k.event.add(this,t,i,r,n)})}function De(e,i,o){o?(Q.set(e,i,!1),k.event.add(e,i,{namespace:!1,handler:function(e){var t,n,r=Q.get(this,i);if(1&e.isTrigger&&this[i]){if(r.length)(k.event.special[i]||{}).delegateType&&e.stopPropagation();else if(r=s.call(arguments),Q.set(this,i,r),t=o(this,i),this[i](),r!==(n=Q.get(this,i))||t?Q.set(this,i,!1):n={},r!==n)return e.stopImmediatePropagation(),e.preventDefault(),n.value}else r.length&&(Q.set(this,i,{value:k.event.trigger(k.extend(r[0],k.Event.prototype),r.slice(1),this)}),e.stopImmediatePropagation())}})):void 0===Q.get(e,i)&&k.event.add(e,i,ke)}k.event={global:{},add:function(t,e,n,r,i){var o,a,s,u,l,c,f,p,d,h,g,v=Q.get(t);if(v){n.handler&&(n=(o=n).handler,i=o.selector),i&&k.find.matchesSelector(ie,i),n.guid||(n.guid=k.guid++),(u=v.events)||(u=v.events={}),(a=v.handle)||(a=v.handle=function(e){return"undefined"!=typeof k&&k.event.triggered!==e.type?k.event.dispatch.apply(t,arguments):void 0}),l=(e=(e||"").match(R)||[""]).length;while(l--)d=g=(s=Ee.exec(e[l])||[])[1],h=(s[2]||"").split(".").sort(),d&&(f=k.event.special[d]||{},d=(i?f.delegateType:f.bindType)||d,f=k.event.special[d]||{},c=k.extend({type:d,origType:g,data:r,handler:n,guid:n.guid,selector:i,needsContext:i&&k.expr.match.needsContext.test(i),namespace:h.join(".")},o),(p=u[d])||((p=u[d]=[]).delegateCount=0,f.setup&&!1!==f.setup.call(t,r,h,a)||t.addEventListener&&t.addEventListener(d,a)),f.add&&(f.add.call(t,c),c.handler.guid||(c.handler.guid=n.guid)),i?p.splice(p.delegateCount++,0,c):p.push(c),k.event.global[d]=!0)}},remove:function(e,t,n,r,i){var o,a,s,u,l,c,f,p,d,h,g,v=Q.hasData(e)&&Q.get(e);if(v&&(u=v.events)){l=(t=(t||"").match(R)||[""]).length;while(l--)if(d=g=(s=Ee.exec(t[l])||[])[1],h=(s[2]||"").split(".").sort(),d){f=k.event.special[d]||{},p=u[d=(r?f.delegateType:f.bindType)||d]||[],s=s[2]&&new RegExp("(^|\\.)"+h.join("\\.(?:.*\\.|)")+"(\\.|$)"),a=o=p.length;while(o--)c=p[o],!i&&g!==c.origType||n&&n.guid!==c.guid||s&&!s.test(c.namespace)||r&&r!==c.selector&&("**"!==r||!c.selector)||(p.splice(o,1),c.selector&&p.delegateCount--,f.remove&&f.remove.call(e,c));a&&!p.length&&(f.teardown&&!1!==f.teardown.call(e,h,v.handle)||k.removeEvent(e,d,v.handle),delete u[d])}else for(d in u)k.event.remove(e,d+t[l],n,r,!0);k.isEmptyObject(u)&&Q.remove(e,"handle events")}},dispatch:function(e){var t,n,r,i,o,a,s=k.event.fix(e),u=new Array(arguments.length),l=(Q.get(this,"events")||{})[s.type]||[],c=k.event.special[s.type]||{};for(u[0]=s,t=1;t<arguments.length;t++)u[t]=arguments[t];if(s.delegateTarget=this,!c.preDispatch||!1!==c.preDispatch.call(this,s)){a=k.event.handlers.call(this,s,l),t=0;while((i=a[t++])&&!s.isPropagationStopped()){s.currentTarget=i.elem,n=0;while((o=i.handlers[n++])&&!s.isImmediatePropagationStopped())s.rnamespace&&!1!==o.namespace&&!s.rnamespace.test(o.namespace)||(s.handleObj=o,s.data=o.data,void 0!==(r=((k.event.special[o.origType]||{}).handle||o.handler).apply(i.elem,u))&&!1===(s.result=r)&&(s.preventDefault(),s.stopPropagation()))}return c.postDispatch&&c.postDispatch.call(this,s),s.result}},handlers:function(e,t){var n,r,i,o,a,s=[],u=t.delegateCount,l=e.target;if(u&&l.nodeType&&!("click"===e.type&&1<=e.button))for(;l!==this;l=l.parentNode||this)if(1===l.nodeType&&("click"!==e.type||!0!==l.disabled)){for(o=[],a={},n=0;n<u;n++)void 0===a[i=(r=t[n]).selector+" "]&&(a[i]=r.needsContext?-1<k(i,this).index(l):k.find(i,this,null,[l]).length),a[i]&&o.push(r);o.length&&s.push({elem:l,handlers:o})}return l=this,u<t.length&&s.push({elem:l,handlers:t.slice(u)}),s},addProp:function(t,e){Object.defineProperty(k.Event.prototype,t,{enumerable:!0,configurable:!0,get:m(e)?function(){if(this.originalEvent)return e(this.originalEvent)}:function(){if(this.originalEvent)return this.originalEvent[t]},set:function(e){Object.defineProperty(this,t,{enumerable:!0,configurable:!0,writable:!0,value:e})}})},fix:function(e){return e[k.expando]?e:new k.Event(e)},special:{load:{noBubble:!0},click:{setup:function(e){var t=this||e;return pe.test(t.type)&&t.click&&A(t,"input")&&De(t,"click",ke),!1},trigger:function(e){var t=this||e;return pe.test(t.type)&&t.click&&A(t,"input")&&De(t,"click"),!0},_default:function(e){var t=e.target;return pe.test(t.type)&&t.click&&A(t,"input")&&Q.get(t,"click")||A(t,"a")}},beforeunload:{postDispatch:function(e){void 0!==e.result&&e.originalEvent&&(e.originalEvent.returnValue=e.result)}}}},k.removeEvent=function(e,t,n){e.removeEventListener&&e.removeEventListener(t,n)},k.Event=function(e,t){if(!(this instanceof k.Event))return new k.Event(e,t);e&&e.type?(this.originalEvent=e,this.type=e.type,this.isDefaultPrevented=e.defaultPrevented||void 0===e.defaultPrevented&&!1===e.returnValue?ke:Se,this.target=e.target&&3===e.target.nodeType?e.target.parentNode:e.target,this.currentTarget=e.currentTarget,this.relatedTarget=e.relatedTarget):this.type=e,t&&k.extend(this,t),this.timeStamp=e&&e.timeStamp||Date.now(),this[k.expando]=!0},k.Event.prototype={constructor:k.Event,isDefaultPrevented:Se,isPropagationStopped:Se,isImmediatePropagationStopped:Se,isSimulated:!1,preventDefault:function(){var e=this.originalEvent;this.isDefaultPrevented=ke,e&&!this.isSimulated&&e.preventDefault()},stopPropagation:function(){var e=this.originalEvent;this.isPropagationStopped=ke,e&&!this.isSimulated&&e.stopPropagation()},stopImmediatePropagation:function(){var e=this.originalEvent;this.isImmediatePropagationStopped=ke,e&&!this.isSimulated&&e.stopImmediatePropagation(),this.stopPropagation()}},k.each({altKey:!0,bubbles:!0,cancelable:!0,changedTouches:!0,ctrlKey:!0,detail:!0,eventPhase:!0,metaKey:!0,pageX:!0,pageY:!0,shiftKey:!0,view:!0,"char":!0,code:!0,charCode:!0,key:!0,keyCode:!0,button:!0,buttons:!0,clientX:!0,clientY:!0,offsetX:!0,offsetY:!0,pointerId:!0,pointerType:!0,screenX:!0,screenY:!0,targetTouches:!0,toElement:!0,touches:!0,which:function(e){var t=e.button;return null==e.which&&Te.test(e.type)?null!=e.charCode?e.charCode:e.keyCode:!e.which&&void 0!==t&&Ce.test(e.type)?1&t?1:2&t?3:4&t?2:0:e.which}},k.event.addProp),k.each({focus:"focusin",blur:"focusout"},function(e,t){k.event.special[e]={setup:function(){return De(this,e,Ne),!1},trigger:function(){return De(this,e),!0},delegateType:t}}),k.each({mouseenter:"mouseover",mouseleave:"mouseout",pointerenter:"pointerover",pointerleave:"pointerout"},function(e,i){k.event.special[e]={delegateType:i,bindType:i,handle:function(e){var t,n=e.relatedTarget,r=e.handleObj;return n&&(n===this||k.contains(this,n))||(e.type=r.origType,t=r.handler.apply(this,arguments),e.type=i),t}}}),k.fn.extend({on:function(e,t,n,r){return Ae(this,e,t,n,r)},one:function(e,t,n,r){return Ae(this,e,t,n,r,1)},off:function(e,t,n){var r,i;if(e&&e.preventDefault&&e.handleObj)return r=e.handleObj,k(e.delegateTarget).off(r.namespace?r.origType+"."+r.namespace:r.origType,r.selector,r.handler),this;if("object"==typeof e){for(i in e)this.off(i,t,e[i]);return this}return!1!==t&&"function"!=typeof t||(n=t,t=void 0),!1===n&&(n=Se),this.each(function(){k.event.remove(this,e,n,t)})}});var je=/<(?!area|br|col|embed|hr|img|input|link|meta|param)(([a-z][^\/\0>\x20\t\r\n\f]*)[^>]*)\/>/gi,qe=/<script|<style|<link/i,Le=/checked\s*(?:[^=]|=\s*.checked.)/i,He=/^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g;function Oe(e,t){return A(e,"table")&&A(11!==t.nodeType?t:t.firstChild,"tr")&&k(e).children("tbody")[0]||e}function Pe(e){return e.type=(null!==e.getAttribute("type"))+"/"+e.type,e}function Re(e){return"true/"===(e.type||"").slice(0,5)?e.type=e.type.slice(5):e.removeAttribute("type"),e}function Me(e,t){var n,r,i,o,a,s,u,l;if(1===t.nodeType){if(Q.hasData(e)&&(o=Q.access(e),a=Q.set(t,o),l=o.events))for(i in delete a.handle,a.events={},l)for(n=0,r=l[i].length;n<r;n++)k.event.add(t,i,l[i][n]);J.hasData(e)&&(s=J.access(e),u=k.extend({},s),J.set(t,u))}}function Ie(n,r,i,o){r=g.apply([],r);var e,t,a,s,u,l,c=0,f=n.length,p=f-1,d=r[0],h=m(d);if(h||1<f&&"string"==typeof d&&!y.checkClone&&Le.test(d))return n.each(function(e){var t=n.eq(e);h&&(r[0]=d.call(this,e,t.html())),Ie(t,r,i,o)});if(f&&(t=(e=we(r,n[0].ownerDocument,!1,n,o)).firstChild,1===e.childNodes.length&&(e=t),t||o)){for(s=(a=k.map(ve(e,"script"),Pe)).length;c<f;c++)u=e,c!==p&&(u=k.clone(u,!0,!0),s&&k.merge(a,ve(u,"script"))),i.call(n[c],u,c);if(s)for(l=a[a.length-1].ownerDocument,k.map(a,Re),c=0;c<s;c++)u=a[c],he.test(u.type||"")&&!Q.access(u,"globalEval")&&k.contains(l,u)&&(u.src&&"module"!==(u.type||"").toLowerCase()?k._evalUrl&&!u.noModule&&k._evalUrl(u.src,{nonce:u.nonce||u.getAttribute("nonce")}):b(u.textContent.replace(He,""),u,l))}return n}function We(e,t,n){for(var r,i=t?k.filter(t,e):e,o=0;null!=(r=i[o]);o++)n||1!==r.nodeType||k.cleanData(ve(r)),r.parentNode&&(n&&oe(r)&&ye(ve(r,"script")),r.parentNode.removeChild(r));return e}k.extend({htmlPrefilter:function(e){return e.replace(je,"<$1></$2>")},clone:function(e,t,n){var r,i,o,a,s,u,l,c=e.cloneNode(!0),f=oe(e);if(!(y.noCloneChecked||1!==e.nodeType&&11!==e.nodeType||k.isXMLDoc(e)))for(a=ve(c),r=0,i=(o=ve(e)).length;r<i;r++)s=o[r],u=a[r],void 0,"input"===(l=u.nodeName.toLowerCase())&&pe.test(s.type)?u.checked=s.checked:"input"!==l&&"textarea"!==l||(u.defaultValue=s.defaultValue);if(t)if(n)for(o=o||ve(e),a=a||ve(c),r=0,i=o.length;r<i;r++)Me(o[r],a[r]);else Me(e,c);return 0<(a=ve(c,"script")).length&&ye(a,!f&&ve(e,"script")),c},cleanData:function(e){for(var t,n,r,i=k.event.special,o=0;void 0!==(n=e[o]);o++)if(G(n)){if(t=n[Q.expando]){if(t.events)for(r in t.events)i[r]?k.event.remove(n,r):k.removeEvent(n,r,t.handle);n[Q.expando]=void 0}n[J.expando]&&(n[J.expando]=void 0)}}}),k.fn.extend({detach:function(e){return We(this,e,!0)},remove:function(e){return We(this,e)},text:function(e){return _(this,function(e){return void 0===e?k.text(this):this.empty().each(function(){1!==this.nodeType&&11!==this.nodeType&&9!==this.nodeType||(this.textContent=e)})},null,e,arguments.length)},append:function(){return Ie(this,arguments,function(e){1!==this.nodeType&&11!==this.nodeType&&9!==this.nodeType||Oe(this,e).appendChild(e)})},prepend:function(){return Ie(this,arguments,function(e){if(1===this.nodeType||11===this.nodeType||9===this.nodeType){var t=Oe(this,e);t.insertBefore(e,t.firstChild)}})},before:function(){return Ie(this,arguments,function(e){this.parentNode&&this.parentNode.insertBefore(e,this)})},after:function(){return Ie(this,arguments,function(e){this.parentNode&&this.parentNode.insertBefore(e,this.nextSibling)})},empty:function(){for(var e,t=0;null!=(e=this[t]);t++)1===e.nodeType&&(k.cleanData(ve(e,!1)),e.textContent="");return this},clone:function(e,t){return e=null!=e&&e,t=null==t?e:t,this.map(function(){return k.clone(this,e,t)})},html:function(e){return _(this,function(e){var t=this[0]||{},n=0,r=this.length;if(void 0===e&&1===t.nodeType)return t.innerHTML;if("string"==typeof e&&!qe.test(e)&&!ge[(de.exec(e)||["",""])[1].toLowerCase()]){e=k.htmlPrefilter(e);try{for(;n<r;n++)1===(t=this[n]||{}).nodeType&&(k.cleanData(ve(t,!1)),t.innerHTML=e);t=0}catch(e){}}t&&this.empty().append(e)},null,e,arguments.length)},replaceWith:function(){var n=[];return Ie(this,arguments,function(e){var t=this.parentNode;k.inArray(this,n)<0&&(k.cleanData(ve(this)),t&&t.replaceChild(e,this))},n)}}),k.each({appendTo:"append",prependTo:"prepend",insertBefore:"before",insertAfter:"after",replaceAll:"replaceWith"},function(e,a){k.fn[e]=function(e){for(var t,n=[],r=k(e),i=r.length-1,o=0;o<=i;o++)t=o===i?this:this.clone(!0),k(r[o])[a](t),u.apply(n,t.get());return this.pushStack(n)}});var $e=new RegExp("^("+te+")(?!px)[a-z%]+$","i"),Fe=function(e){var t=e.ownerDocument.defaultView;return t&&t.opener||(t=C),t.getComputedStyle(e)},Be=new RegExp(re.join("|"),"i");function _e(e,t,n){var r,i,o,a,s=e.style;return(n=n||Fe(e))&&(""!==(a=n.getPropertyValue(t)||n[t])||oe(e)||(a=k.style(e,t)),!y.pixelBoxStyles()&&$e.test(a)&&Be.test(t)&&(r=s.width,i=s.minWidth,o=s.maxWidth,s.minWidth=s.maxWidth=s.width=a,a=n.width,s.width=r,s.minWidth=i,s.maxWidth=o)),void 0!==a?a+"":a}function ze(e,t){return{get:function(){if(!e())return(this.get=t).apply(this,arguments);delete this.get}}}!function(){function e(){if(u){s.style.cssText="position:absolute;left:-11111px;width:60px;margin-top:1px;padding:0;border:0",u.style.cssText="position:relative;display:block;box-sizing:border-box;overflow:scroll;margin:auto;border:1px;padding:1px;width:60%;top:1%",ie.appendChild(s).appendChild(u);var e=C.getComputedStyle(u);n="1%"!==e.top,a=12===t(e.marginLeft),u.style.right="60%",o=36===t(e.right),r=36===t(e.width),u.style.position="absolute",i=12===t(u.offsetWidth/3),ie.removeChild(s),u=null}}function t(e){return Math.round(parseFloat(e))}var n,r,i,o,a,s=E.createElement("div"),u=E.createElement("div");u.style&&(u.style.backgroundClip="content-box",u.cloneNode(!0).style.backgroundClip="",y.clearCloneStyle="content-box"===u.style.backgroundClip,k.extend(y,{boxSizingReliable:function(){return e(),r},pixelBoxStyles:function(){return e(),o},pixelPosition:function(){return e(),n},reliableMarginLeft:function(){return e(),a},scrollboxSize:function(){return e(),i}}))}();var Ue=["Webkit","Moz","ms"],Xe=E.createElement("div").style,Ve={};function Ge(e){var t=k.cssProps[e]||Ve[e];return t||(e in Xe?e:Ve[e]=function(e){var t=e[0].toUpperCase()+e.slice(1),n=Ue.length;while(n--)if((e=Ue[n]+t)in Xe)return e}(e)||e)}var Ye=/^(none|table(?!-c[ea]).+)/,Qe=/^--/,Je={position:"absolute",visibility:"hidden",display:"block"},Ke={letterSpacing:"0",fontWeight:"400"};function Ze(e,t,n){var r=ne.exec(t);return r?Math.max(0,r[2]-(n||0))+(r[3]||"px"):t}function et(e,t,n,r,i,o){var a="width"===t?1:0,s=0,u=0;if(n===(r?"border":"content"))return 0;for(;a<4;a+=2)"margin"===n&&(u+=k.css(e,n+re[a],!0,i)),r?("content"===n&&(u-=k.css(e,"padding"+re[a],!0,i)),"margin"!==n&&(u-=k.css(e,"border"+re[a]+"Width",!0,i))):(u+=k.css(e,"padding"+re[a],!0,i),"padding"!==n?u+=k.css(e,"border"+re[a]+"Width",!0,i):s+=k.css(e,"border"+re[a]+"Width",!0,i));return!r&&0<=o&&(u+=Math.max(0,Math.ceil(e["offset"+t[0].toUpperCase()+t.slice(1)]-o-u-s-.5))||0),u}function tt(e,t,n){var r=Fe(e),i=(!y.boxSizingReliable()||n)&&"border-box"===k.css(e,"boxSizing",!1,r),o=i,a=_e(e,t,r),s="offset"+t[0].toUpperCase()+t.slice(1);if($e.test(a)){if(!n)return a;a="auto"}return(!y.boxSizingReliable()&&i||"auto"===a||!parseFloat(a)&&"inline"===k.css(e,"display",!1,r))&&e.getClientRects().length&&(i="border-box"===k.css(e,"boxSizing",!1,r),(o=s in e)&&(a=e[s])),(a=parseFloat(a)||0)+et(e,t,n||(i?"border":"content"),o,r,a)+"px"}function nt(e,t,n,r,i){return new nt.prototype.init(e,t,n,r,i)}k.extend({cssHooks:{opacity:{get:function(e,t){if(t){var n=_e(e,"opacity");return""===n?"1":n}}}},cssNumber:{animationIterationCount:!0,columnCount:!0,fillOpacity:!0,flexGrow:!0,flexShrink:!0,fontWeight:!0,gridArea:!0,gridColumn:!0,gridColumnEnd:!0,gridColumnStart:!0,gridRow:!0,gridRowEnd:!0,gridRowStart:!0,lineHeight:!0,opacity:!0,order:!0,orphans:!0,widows:!0,zIndex:!0,zoom:!0},cssProps:{},style:function(e,t,n,r){if(e&&3!==e.nodeType&&8!==e.nodeType&&e.style){var i,o,a,s=V(t),u=Qe.test(t),l=e.style;if(u||(t=Ge(s)),a=k.cssHooks[t]||k.cssHooks[s],void 0===n)return a&&"get"in a&&void 0!==(i=a.get(e,!1,r))?i:l[t];"string"===(o=typeof n)&&(i=ne.exec(n))&&i[1]&&(n=le(e,t,i),o="number"),null!=n&&n==n&&("number"!==o||u||(n+=i&&i[3]||(k.cssNumber[s]?"":"px")),y.clearCloneStyle||""!==n||0!==t.indexOf("background")||(l[t]="inherit"),a&&"set"in a&&void 0===(n=a.set(e,n,r))||(u?l.setProperty(t,n):l[t]=n))}},css:function(e,t,n,r){var i,o,a,s=V(t);return Qe.test(t)||(t=Ge(s)),(a=k.cssHooks[t]||k.cssHooks[s])&&"get"in a&&(i=a.get(e,!0,n)),void 0===i&&(i=_e(e,t,r)),"normal"===i&&t in Ke&&(i=Ke[t]),""===n||n?(o=parseFloat(i),!0===n||isFinite(o)?o||0:i):i}}),k.each(["height","width"],function(e,u){k.cssHooks[u]={get:function(e,t,n){if(t)return!Ye.test(k.css(e,"display"))||e.getClientRects().length&&e.getBoundingClientRect().width?tt(e,u,n):ue(e,Je,function(){return tt(e,u,n)})},set:function(e,t,n){var r,i=Fe(e),o=!y.scrollboxSize()&&"absolute"===i.position,a=(o||n)&&"border-box"===k.css(e,"boxSizing",!1,i),s=n?et(e,u,n,a,i):0;return a&&o&&(s-=Math.ceil(e["offset"+u[0].toUpperCase()+u.slice(1)]-parseFloat(i[u])-et(e,u,"border",!1,i)-.5)),s&&(r=ne.exec(t))&&"px"!==(r[3]||"px")&&(e.style[u]=t,t=k.css(e,u)),Ze(0,t,s)}}}),k.cssHooks.marginLeft=ze(y.reliableMarginLeft,function(e,t){if(t)return(parseFloat(_e(e,"marginLeft"))||e.getBoundingClientRect().left-ue(e,{marginLeft:0},function(){return e.getBoundingClientRect().left}))+"px"}),k.each({margin:"",padding:"",border:"Width"},function(i,o){k.cssHooks[i+o]={expand:function(e){for(var t=0,n={},r="string"==typeof e?e.split(" "):[e];t<4;t++)n[i+re[t]+o]=r[t]||r[t-2]||r[0];return n}},"margin"!==i&&(k.cssHooks[i+o].set=Ze)}),k.fn.extend({css:function(e,t){return _(this,function(e,t,n){var r,i,o={},a=0;if(Array.isArray(t)){for(r=Fe(e),i=t.length;a<i;a++)o[t[a]]=k.css(e,t[a],!1,r);return o}return void 0!==n?k.style(e,t,n):k.css(e,t)},e,t,1<arguments.length)}}),((k.Tween=nt).prototype={constructor:nt,init:function(e,t,n,r,i,o){this.elem=e,this.prop=n,this.easing=i||k.easing._default,this.options=t,this.start=this.now=this.cur(),this.end=r,this.unit=o||(k.cssNumber[n]?"":"px")},cur:function(){var e=nt.propHooks[this.prop];return e&&e.get?e.get(this):nt.propHooks._default.get(this)},run:function(e){var t,n=nt.propHooks[this.prop];return this.options.duration?this.pos=t=k.easing[this.easing](e,this.options.duration*e,0,1,this.options.duration):this.pos=t=e,this.now=(this.end-this.start)*t+this.start,this.options.step&&this.options.step.call(this.elem,this.now,this),n&&n.set?n.set(this):nt.propHooks._default.set(this),this}}).init.prototype=nt.prototype,(nt.propHooks={_default:{get:function(e){var t;return 1!==e.elem.nodeType||null!=e.elem[e.prop]&&null==e.elem.style[e.prop]?e.elem[e.prop]:(t=k.css(e.elem,e.prop,""))&&"auto"!==t?t:0},set:function(e){k.fx.step[e.prop]?k.fx.step[e.prop](e):1!==e.elem.nodeType||!k.cssHooks[e.prop]&&null==e.elem.style[Ge(e.prop)]?e.elem[e.prop]=e.now:k.style(e.elem,e.prop,e.now+e.unit)}}}).scrollTop=nt.propHooks.scrollLeft={set:function(e){e.elem.nodeType&&e.elem.parentNode&&(e.elem[e.prop]=e.now)}},k.easing={linear:function(e){return e},swing:function(e){return.5-Math.cos(e*Math.PI)/2},_default:"swing"},k.fx=nt.prototype.init,k.fx.step={};var rt,it,ot,at,st=/^(?:toggle|show|hide)$/,ut=/queueHooks$/;function lt(){it&&(!1===E.hidden&&C.requestAnimationFrame?C.requestAnimationFrame(lt):C.setTimeout(lt,k.fx.interval),k.fx.tick())}function ct(){return C.setTimeout(function(){rt=void 0}),rt=Date.now()}function ft(e,t){var n,r=0,i={height:e};for(t=t?1:0;r<4;r+=2-t)i["margin"+(n=re[r])]=i["padding"+n]=e;return t&&(i.opacity=i.width=e),i}function pt(e,t,n){for(var r,i=(dt.tweeners[t]||[]).concat(dt.tweeners["*"]),o=0,a=i.length;o<a;o++)if(r=i[o].call(n,t,e))return r}function dt(o,e,t){var n,a,r=0,i=dt.prefilters.length,s=k.Deferred().always(function(){delete u.elem}),u=function(){if(a)return!1;for(var e=rt||ct(),t=Math.max(0,l.startTime+l.duration-e),n=1-(t/l.duration||0),r=0,i=l.tweens.length;r<i;r++)l.tweens[r].run(n);return s.notifyWith(o,[l,n,t]),n<1&&i?t:(i||s.notifyWith(o,[l,1,0]),s.resolveWith(o,[l]),!1)},l=s.promise({elem:o,props:k.extend({},e),opts:k.extend(!0,{specialEasing:{},easing:k.easing._default},t),originalProperties:e,originalOptions:t,startTime:rt||ct(),duration:t.duration,tweens:[],createTween:function(e,t){var n=k.Tween(o,l.opts,e,t,l.opts.specialEasing[e]||l.opts.easing);return l.tweens.push(n),n},stop:function(e){var t=0,n=e?l.tweens.length:0;if(a)return this;for(a=!0;t<n;t++)l.tweens[t].run(1);return e?(s.notifyWith(o,[l,1,0]),s.resolveWith(o,[l,e])):s.rejectWith(o,[l,e]),this}}),c=l.props;for(!function(e,t){var n,r,i,o,a;for(n in e)if(i=t[r=V(n)],o=e[n],Array.isArray(o)&&(i=o[1],o=e[n]=o[0]),n!==r&&(e[r]=o,delete e[n]),(a=k.cssHooks[r])&&"expand"in a)for(n in o=a.expand(o),delete e[r],o)n in e||(e[n]=o[n],t[n]=i);else t[r]=i}(c,l.opts.specialEasing);r<i;r++)if(n=dt.prefilters[r].call(l,o,c,l.opts))return m(n.stop)&&(k._queueHooks(l.elem,l.opts.queue).stop=n.stop.bind(n)),n;return k.map(c,pt,l),m(l.opts.start)&&l.opts.start.call(o,l),l.progress(l.opts.progress).done(l.opts.done,l.opts.complete).fail(l.opts.fail).always(l.opts.always),k.fx.timer(k.extend(u,{elem:o,anim:l,queue:l.opts.queue})),l}k.Animation=k.extend(dt,{tweeners:{"*":[function(e,t){var n=this.createTween(e,t);return le(n.elem,e,ne.exec(t),n),n}]},tweener:function(e,t){m(e)?(t=e,e=["*"]):e=e.match(R);for(var n,r=0,i=e.length;r<i;r++)n=e[r],dt.tweeners[n]=dt.tweeners[n]||[],dt.tweeners[n].unshift(t)},prefilters:[function(e,t,n){var r,i,o,a,s,u,l,c,f="width"in t||"height"in t,p=this,d={},h=e.style,g=e.nodeType&&se(e),v=Q.get(e,"fxshow");for(r in n.queue||(null==(a=k._queueHooks(e,"fx")).unqueued&&(a.unqueued=0,s=a.empty.fire,a.empty.fire=function(){a.unqueued||s()}),a.unqueued++,p.always(function(){p.always(function(){a.unqueued--,k.queue(e,"fx").length||a.empty.fire()})})),t)if(i=t[r],st.test(i)){if(delete t[r],o=o||"toggle"===i,i===(g?"hide":"show")){if("show"!==i||!v||void 0===v[r])continue;g=!0}d[r]=v&&v[r]||k.style(e,r)}if((u=!k.isEmptyObject(t))||!k.isEmptyObject(d))for(r in f&&1===e.nodeType&&(n.overflow=[h.overflow,h.overflowX,h.overflowY],null==(l=v&&v.display)&&(l=Q.get(e,"display")),"none"===(c=k.css(e,"display"))&&(l?c=l:(fe([e],!0),l=e.style.display||l,c=k.css(e,"display"),fe([e]))),("inline"===c||"inline-block"===c&&null!=l)&&"none"===k.css(e,"float")&&(u||(p.done(function(){h.display=l}),null==l&&(c=h.display,l="none"===c?"":c)),h.display="inline-block")),n.overflow&&(h.overflow="hidden",p.always(function(){h.overflow=n.overflow[0],h.overflowX=n.overflow[1],h.overflowY=n.overflow[2]})),u=!1,d)u||(v?"hidden"in v&&(g=v.hidden):v=Q.access(e,"fxshow",{display:l}),o&&(v.hidden=!g),g&&fe([e],!0),p.done(function(){for(r in g||fe([e]),Q.remove(e,"fxshow"),d)k.style(e,r,d[r])})),u=pt(g?v[r]:0,r,p),r in v||(v[r]=u.start,g&&(u.end=u.start,u.start=0))}],prefilter:function(e,t){t?dt.prefilters.unshift(e):dt.prefilters.push(e)}}),k.speed=function(e,t,n){var r=e&&"object"==typeof e?k.extend({},e):{complete:n||!n&&t||m(e)&&e,duration:e,easing:n&&t||t&&!m(t)&&t};return k.fx.off?r.duration=0:"number"!=typeof r.duration&&(r.duration in k.fx.speeds?r.duration=k.fx.speeds[r.duration]:r.duration=k.fx.speeds._default),null!=r.queue&&!0!==r.queue||(r.queue="fx"),r.old=r.complete,r.complete=function(){m(r.old)&&r.old.call(this),r.queue&&k.dequeue(this,r.queue)},r},k.fn.extend({fadeTo:function(e,t,n,r){return this.filter(se).css("opacity",0).show().end().animate({opacity:t},e,n,r)},animate:function(t,e,n,r){var i=k.isEmptyObject(t),o=k.speed(e,n,r),a=function(){var e=dt(this,k.extend({},t),o);(i||Q.get(this,"finish"))&&e.stop(!0)};return a.finish=a,i||!1===o.queue?this.each(a):this.queue(o.queue,a)},stop:function(i,e,o){var a=function(e){var t=e.stop;delete e.stop,t(o)};return"string"!=typeof i&&(o=e,e=i,i=void 0),e&&!1!==i&&this.queue(i||"fx",[]),this.each(function(){var e=!0,t=null!=i&&i+"queueHooks",n=k.timers,r=Q.get(this);if(t)r[t]&&r[t].stop&&a(r[t]);else for(t in r)r[t]&&r[t].stop&&ut.test(t)&&a(r[t]);for(t=n.length;t--;)n[t].elem!==this||null!=i&&n[t].queue!==i||(n[t].anim.stop(o),e=!1,n.splice(t,1));!e&&o||k.dequeue(this,i)})},finish:function(a){return!1!==a&&(a=a||"fx"),this.each(function(){var e,t=Q.get(this),n=t[a+"queue"],r=t[a+"queueHooks"],i=k.timers,o=n?n.length:0;for(t.finish=!0,k.queue(this,a,[]),r&&r.stop&&r.stop.call(this,!0),e=i.length;e--;)i[e].elem===this&&i[e].queue===a&&(i[e].anim.stop(!0),i.splice(e,1));for(e=0;e<o;e++)n[e]&&n[e].finish&&n[e].finish.call(this);delete t.finish})}}),k.each(["toggle","show","hide"],function(e,r){var i=k.fn[r];k.fn[r]=function(e,t,n){return null==e||"boolean"==typeof e?i.apply(this,arguments):this.animate(ft(r,!0),e,t,n)}}),k.each({slideDown:ft("show"),slideUp:ft("hide"),slideToggle:ft("toggle"),fadeIn:{opacity:"show"},fadeOut:{opacity:"hide"},fadeToggle:{opacity:"toggle"}},function(e,r){k.fn[e]=function(e,t,n){return this.animate(r,e,t,n)}}),k.timers=[],k.fx.tick=function(){var e,t=0,n=k.timers;for(rt=Date.now();t<n.length;t++)(e=n[t])()||n[t]!==e||n.splice(t--,1);n.length||k.fx.stop(),rt=void 0},k.fx.timer=function(e){k.timers.push(e),k.fx.start()},k.fx.interval=13,k.fx.start=function(){it||(it=!0,lt())},k.fx.stop=function(){it=null},k.fx.speeds={slow:600,fast:200,_default:400},k.fn.delay=function(r,e){return r=k.fx&&k.fx.speeds[r]||r,e=e||"fx",this.queue(e,function(e,t){var n=C.setTimeout(e,r);t.stop=function(){C.clearTimeout(n)}})},ot=E.createElement("input"),at=E.createElement("select").appendChild(E.createElement("option")),ot.type="checkbox",y.checkOn=""!==ot.value,y.optSelected=at.selected,(ot=E.createElement("input")).value="t",ot.type="radio",y.radioValue="t"===ot.value;var ht,gt=k.expr.attrHandle;k.fn.extend({attr:function(e,t){return _(this,k.attr,e,t,1<arguments.length)},removeAttr:function(e){return this.each(function(){k.removeAttr(this,e)})}}),k.extend({attr:function(e,t,n){var r,i,o=e.nodeType;if(3!==o&&8!==o&&2!==o)return"undefined"==typeof e.getAttribute?k.prop(e,t,n):(1===o&&k.isXMLDoc(e)||(i=k.attrHooks[t.toLowerCase()]||(k.expr.match.bool.test(t)?ht:void 0)),void 0!==n?null===n?void k.removeAttr(e,t):i&&"set"in i&&void 0!==(r=i.set(e,n,t))?r:(e.setAttribute(t,n+""),n):i&&"get"in i&&null!==(r=i.get(e,t))?r:null==(r=k.find.attr(e,t))?void 0:r)},attrHooks:{type:{set:function(e,t){if(!y.radioValue&&"radio"===t&&A(e,"input")){var n=e.value;return e.setAttribute("type",t),n&&(e.value=n),t}}}},removeAttr:function(e,t){var n,r=0,i=t&&t.match(R);if(i&&1===e.nodeType)while(n=i[r++])e.removeAttribute(n)}}),ht={set:function(e,t,n){return!1===t?k.removeAttr(e,n):e.setAttribute(n,n),n}},k.each(k.expr.match.bool.source.match(/\w+/g),function(e,t){var a=gt[t]||k.find.attr;gt[t]=function(e,t,n){var r,i,o=t.toLowerCase();return n||(i=gt[o],gt[o]=r,r=null!=a(e,t,n)?o:null,gt[o]=i),r}});var vt=/^(?:input|select|textarea|button)$/i,yt=/^(?:a|area)$/i;function mt(e){return(e.match(R)||[]).join(" ")}function xt(e){return e.getAttribute&&e.getAttribute("class")||""}function bt(e){return Array.isArray(e)?e:"string"==typeof e&&e.match(R)||[]}k.fn.extend({prop:function(e,t){return _(this,k.prop,e,t,1<arguments.length)},removeProp:function(e){return this.each(function(){delete this[k.propFix[e]||e]})}}),k.extend({prop:function(e,t,n){var r,i,o=e.nodeType;if(3!==o&&8!==o&&2!==o)return 1===o&&k.isXMLDoc(e)||(t=k.propFix[t]||t,i=k.propHooks[t]),void 0!==n?i&&"set"in i&&void 0!==(r=i.set(e,n,t))?r:e[t]=n:i&&"get"in i&&null!==(r=i.get(e,t))?r:e[t]},propHooks:{tabIndex:{get:function(e){var t=k.find.attr(e,"tabindex");return t?parseInt(t,10):vt.test(e.nodeName)||yt.test(e.nodeName)&&e.href?0:-1}}},propFix:{"for":"htmlFor","class":"className"}}),y.optSelected||(k.propHooks.selected={get:function(e){var t=e.parentNode;return t&&t.parentNode&&t.parentNode.selectedIndex,null},set:function(e){var t=e.parentNode;t&&(t.selectedIndex,t.parentNode&&t.parentNode.selectedIndex)}}),k.each(["tabIndex","readOnly","maxLength","cellSpacing","cellPadding","rowSpan","colSpan","useMap","frameBorder","contentEditable"],function(){k.propFix[this.toLowerCase()]=this}),k.fn.extend({addClass:function(t){var e,n,r,i,o,a,s,u=0;if(m(t))return this.each(function(e){k(this).addClass(t.call(this,e,xt(this)))});if((e=bt(t)).length)while(n=this[u++])if(i=xt(n),r=1===n.nodeType&&" "+mt(i)+" "){a=0;while(o=e[a++])r.indexOf(" "+o+" ")<0&&(r+=o+" ");i!==(s=mt(r))&&n.setAttribute("class",s)}return this},removeClass:function(t){var e,n,r,i,o,a,s,u=0;if(m(t))return this.each(function(e){k(this).removeClass(t.call(this,e,xt(this)))});if(!arguments.length)return this.attr("class","");if((e=bt(t)).length)while(n=this[u++])if(i=xt(n),r=1===n.nodeType&&" "+mt(i)+" "){a=0;while(o=e[a++])while(-1<r.indexOf(" "+o+" "))r=r.replace(" "+o+" "," ");i!==(s=mt(r))&&n.setAttribute("class",s)}return this},toggleClass:function(i,t){var o=typeof i,a="string"===o||Array.isArray(i);return"boolean"==typeof t&&a?t?this.addClass(i):this.removeClass(i):m(i)?this.each(function(e){k(this).toggleClass(i.call(this,e,xt(this),t),t)}):this.each(function(){var e,t,n,r;if(a){t=0,n=k(this),r=bt(i);while(e=r[t++])n.hasClass(e)?n.removeClass(e):n.addClass(e)}else void 0!==i&&"boolean"!==o||((e=xt(this))&&Q.set(this,"__className__",e),this.setAttribute&&this.setAttribute("class",e||!1===i?"":Q.get(this,"__className__")||""))})},hasClass:function(e){var t,n,r=0;t=" "+e+" ";while(n=this[r++])if(1===n.nodeType&&-1<(" "+mt(xt(n))+" ").indexOf(t))return!0;return!1}});var wt=/\r/g;k.fn.extend({val:function(n){var r,e,i,t=this[0];return arguments.length?(i=m(n),this.each(function(e){var t;1===this.nodeType&&(null==(t=i?n.call(this,e,k(this).val()):n)?t="":"number"==typeof t?t+="":Array.isArray(t)&&(t=k.map(t,function(e){return null==e?"":e+""})),(r=k.valHooks[this.type]||k.valHooks[this.nodeName.toLowerCase()])&&"set"in r&&void 0!==r.set(this,t,"value")||(this.value=t))})):t?(r=k.valHooks[t.type]||k.valHooks[t.nodeName.toLowerCase()])&&"get"in r&&void 0!==(e=r.get(t,"value"))?e:"string"==typeof(e=t.value)?e.replace(wt,""):null==e?"":e:void 0}}),k.extend({valHooks:{option:{get:function(e){var t=k.find.attr(e,"value");return null!=t?t:mt(k.text(e))}},select:{get:function(e){var t,n,r,i=e.options,o=e.selectedIndex,a="select-one"===e.type,s=a?null:[],u=a?o+1:i.length;for(r=o<0?u:a?o:0;r<u;r++)if(((n=i[r]).selected||r===o)&&!n.disabled&&(!n.parentNode.disabled||!A(n.parentNode,"optgroup"))){if(t=k(n).val(),a)return t;s.push(t)}return s},set:function(e,t){var n,r,i=e.options,o=k.makeArray(t),a=i.length;while(a--)((r=i[a]).selected=-1<k.inArray(k.valHooks.option.get(r),o))&&(n=!0);return n||(e.selectedIndex=-1),o}}}}),k.each(["radio","checkbox"],function(){k.valHooks[this]={set:function(e,t){if(Array.isArray(t))return e.checked=-1<k.inArray(k(e).val(),t)}},y.checkOn||(k.valHooks[this].get=function(e){return null===e.getAttribute("value")?"on":e.value})}),y.focusin="onfocusin"in C;var Tt=/^(?:focusinfocus|focusoutblur)$/,Ct=function(e){e.stopPropagation()};k.extend(k.event,{trigger:function(e,t,n,r){var i,o,a,s,u,l,c,f,p=[n||E],d=v.call(e,"type")?e.type:e,h=v.call(e,"namespace")?e.namespace.split("."):[];if(o=f=a=n=n||E,3!==n.nodeType&&8!==n.nodeType&&!Tt.test(d+k.event.triggered)&&(-1<d.indexOf(".")&&(d=(h=d.split(".")).shift(),h.sort()),u=d.indexOf(":")<0&&"on"+d,(e=e[k.expando]?e:new k.Event(d,"object"==typeof e&&e)).isTrigger=r?2:3,e.namespace=h.join("."),e.rnamespace=e.namespace?new RegExp("(^|\\.)"+h.join("\\.(?:.*\\.|)")+"(\\.|$)"):null,e.result=void 0,e.target||(e.target=n),t=null==t?[e]:k.makeArray(t,[e]),c=k.event.special[d]||{},r||!c.trigger||!1!==c.trigger.apply(n,t))){if(!r&&!c.noBubble&&!x(n)){for(s=c.delegateType||d,Tt.test(s+d)||(o=o.parentNode);o;o=o.parentNode)p.push(o),a=o;a===(n.ownerDocument||E)&&p.push(a.defaultView||a.parentWindow||C)}i=0;while((o=p[i++])&&!e.isPropagationStopped())f=o,e.type=1<i?s:c.bindType||d,(l=(Q.get(o,"events")||{})[e.type]&&Q.get(o,"handle"))&&l.apply(o,t),(l=u&&o[u])&&l.apply&&G(o)&&(e.result=l.apply(o,t),!1===e.result&&e.preventDefault());return e.type=d,r||e.isDefaultPrevented()||c._default&&!1!==c._default.apply(p.pop(),t)||!G(n)||u&&m(n[d])&&!x(n)&&((a=n[u])&&(n[u]=null),k.event.triggered=d,e.isPropagationStopped()&&f.addEventListener(d,Ct),n[d](),e.isPropagationStopped()&&f.removeEventListener(d,Ct),k.event.triggered=void 0,a&&(n[u]=a)),e.result}},simulate:function(e,t,n){var r=k.extend(new k.Event,n,{type:e,isSimulated:!0});k.event.trigger(r,null,t)}}),k.fn.extend({trigger:function(e,t){return this.each(function(){k.event.trigger(e,t,this)})},triggerHandler:function(e,t){var n=this[0];if(n)return k.event.trigger(e,t,n,!0)}}),y.focusin||k.each({focus:"focusin",blur:"focusout"},function(n,r){var i=function(e){k.event.simulate(r,e.target,k.event.fix(e))};k.event.special[r]={setup:function(){var e=this.ownerDocument||this,t=Q.access(e,r);t||e.addEventListener(n,i,!0),Q.access(e,r,(t||0)+1)},teardown:function(){var e=this.ownerDocument||this,t=Q.access(e,r)-1;t?Q.access(e,r,t):(e.removeEventListener(n,i,!0),Q.remove(e,r))}}});var Et=C.location,kt=Date.now(),St=/\?/;k.parseXML=function(e){var t;if(!e||"string"!=typeof e)return null;try{t=(new C.DOMParser).parseFromString(e,"text/xml")}catch(e){t=void 0}return t&&!t.getElementsByTagName("parsererror").length||k.error("Invalid XML: "+e),t};var Nt=/\[\]$/,At=/\r?\n/g,Dt=/^(?:submit|button|image|reset|file)$/i,jt=/^(?:input|select|textarea|keygen)/i;function qt(n,e,r,i){var t;if(Array.isArray(e))k.each(e,function(e,t){r||Nt.test(n)?i(n,t):qt(n+"["+("object"==typeof t&&null!=t?e:"")+"]",t,r,i)});else if(r||"object"!==w(e))i(n,e);else for(t in e)qt(n+"["+t+"]",e[t],r,i)}k.param=function(e,t){var n,r=[],i=function(e,t){var n=m(t)?t():t;r[r.length]=encodeURIComponent(e)+"="+encodeURIComponent(null==n?"":n)};if(null==e)return"";if(Array.isArray(e)||e.jquery&&!k.isPlainObject(e))k.each(e,function(){i(this.name,this.value)});else for(n in e)qt(n,e[n],t,i);return r.join("&")},k.fn.extend({serialize:function(){return k.param(this.serializeArray())},serializeArray:function(){return this.map(function(){var e=k.prop(this,"elements");return e?k.makeArray(e):this}).filter(function(){var e=this.type;return this.name&&!k(this).is(":disabled")&&jt.test(this.nodeName)&&!Dt.test(e)&&(this.checked||!pe.test(e))}).map(function(e,t){var n=k(this).val();return null==n?null:Array.isArray(n)?k.map(n,function(e){return{name:t.name,value:e.replace(At,"\r\n")}}):{name:t.name,value:n.replace(At,"\r\n")}}).get()}});var Lt=/%20/g,Ht=/#.*$/,Ot=/([?&])_=[^&]*/,Pt=/^(.*?):[ \t]*([^\r\n]*)$/gm,Rt=/^(?:GET|HEAD)$/,Mt=/^\/\//,It={},Wt={},$t="*/".concat("*"),Ft=E.createElement("a");function Bt(o){return function(e,t){"string"!=typeof e&&(t=e,e="*");var n,r=0,i=e.toLowerCase().match(R)||[];if(m(t))while(n=i[r++])"+"===n[0]?(n=n.slice(1)||"*",(o[n]=o[n]||[]).unshift(t)):(o[n]=o[n]||[]).push(t)}}function _t(t,i,o,a){var s={},u=t===Wt;function l(e){var r;return s[e]=!0,k.each(t[e]||[],function(e,t){var n=t(i,o,a);return"string"!=typeof n||u||s[n]?u?!(r=n):void 0:(i.dataTypes.unshift(n),l(n),!1)}),r}return l(i.dataTypes[0])||!s["*"]&&l("*")}function zt(e,t){var n,r,i=k.ajaxSettings.flatOptions||{};for(n in t)void 0!==t[n]&&((i[n]?e:r||(r={}))[n]=t[n]);return r&&k.extend(!0,e,r),e}Ft.href=Et.href,k.extend({active:0,lastModified:{},etag:{},ajaxSettings:{url:Et.href,type:"GET",isLocal:/^(?:about|app|app-storage|.+-extension|file|res|widget):$/.test(Et.protocol),global:!0,processData:!0,async:!0,contentType:"application/x-www-form-urlencoded; charset=UTF-8",accepts:{"*":$t,text:"text/plain",html:"text/html",xml:"application/xml, text/xml",json:"application/json, text/javascript"},contents:{xml:/\bxml\b/,html:/\bhtml/,json:/\bjson\b/},responseFields:{xml:"responseXML",text:"responseText",json:"responseJSON"},converters:{"* text":String,"text html":!0,"text json":JSON.parse,"text xml":k.parseXML},flatOptions:{url:!0,context:!0}},ajaxSetup:function(e,t){return t?zt(zt(e,k.ajaxSettings),t):zt(k.ajaxSettings,e)},ajaxPrefilter:Bt(It),ajaxTransport:Bt(Wt),ajax:function(e,t){"object"==typeof e&&(t=e,e=void 0),t=t||{};var c,f,p,n,d,r,h,g,i,o,v=k.ajaxSetup({},t),y=v.context||v,m=v.context&&(y.nodeType||y.jquery)?k(y):k.event,x=k.Deferred(),b=k.Callbacks("once memory"),w=v.statusCode||{},a={},s={},u="canceled",T={readyState:0,getResponseHeader:function(e){var t;if(h){if(!n){n={};while(t=Pt.exec(p))n[t[1].toLowerCase()+" "]=(n[t[1].toLowerCase()+" "]||[]).concat(t[2])}t=n[e.toLowerCase()+" "]}return null==t?null:t.join(", ")},getAllResponseHeaders:function(){return h?p:null},setRequestHeader:function(e,t){return null==h&&(e=s[e.toLowerCase()]=s[e.toLowerCase()]||e,a[e]=t),this},overrideMimeType:function(e){return null==h&&(v.mimeType=e),this},statusCode:function(e){var t;if(e)if(h)T.always(e[T.status]);else for(t in e)w[t]=[w[t],e[t]];return this},abort:function(e){var t=e||u;return c&&c.abort(t),l(0,t),this}};if(x.promise(T),v.url=((e||v.url||Et.href)+"").replace(Mt,Et.protocol+"//"),v.type=t.method||t.type||v.method||v.type,v.dataTypes=(v.dataType||"*").toLowerCase().match(R)||[""],null==v.crossDomain){r=E.createElement("a");try{r.href=v.url,r.href=r.href,v.crossDomain=Ft.protocol+"//"+Ft.host!=r.protocol+"//"+r.host}catch(e){v.crossDomain=!0}}if(v.data&&v.processData&&"string"!=typeof v.data&&(v.data=k.param(v.data,v.traditional)),_t(It,v,t,T),h)return T;for(i in(g=k.event&&v.global)&&0==k.active++&&k.event.trigger("ajaxStart"),v.type=v.type.toUpperCase(),v.hasContent=!Rt.test(v.type),f=v.url.replace(Ht,""),v.hasContent?v.data&&v.processData&&0===(v.contentType||"").indexOf("application/x-www-form-urlencoded")&&(v.data=v.data.replace(Lt,"+")):(o=v.url.slice(f.length),v.data&&(v.processData||"string"==typeof v.data)&&(f+=(St.test(f)?"&":"?")+v.data,delete v.data),!1===v.cache&&(f=f.replace(Ot,"$1"),o=(St.test(f)?"&":"?")+"_="+kt+++o),v.url=f+o),v.ifModified&&(k.lastModified[f]&&T.setRequestHeader("If-Modified-Since",k.lastModified[f]),k.etag[f]&&T.setRequestHeader("If-None-Match",k.etag[f])),(v.data&&v.hasContent&&!1!==v.contentType||t.contentType)&&T.setRequestHeader("Content-Type",v.contentType),T.setRequestHeader("Accept",v.dataTypes[0]&&v.accepts[v.dataTypes[0]]?v.accepts[v.dataTypes[0]]+("*"!==v.dataTypes[0]?", "+$t+"; q=0.01":""):v.accepts["*"]),v.headers)T.setRequestHeader(i,v.headers[i]);if(v.beforeSend&&(!1===v.beforeSend.call(y,T,v)||h))return T.abort();if(u="abort",b.add(v.complete),T.done(v.success),T.fail(v.error),c=_t(Wt,v,t,T)){if(T.readyState=1,g&&m.trigger("ajaxSend",[T,v]),h)return T;v.async&&0<v.timeout&&(d=C.setTimeout(function(){T.abort("timeout")},v.timeout));try{h=!1,c.send(a,l)}catch(e){if(h)throw e;l(-1,e)}}else l(-1,"No Transport");function l(e,t,n,r){var i,o,a,s,u,l=t;h||(h=!0,d&&C.clearTimeout(d),c=void 0,p=r||"",T.readyState=0<e?4:0,i=200<=e&&e<300||304===e,n&&(s=function(e,t,n){var r,i,o,a,s=e.contents,u=e.dataTypes;while("*"===u[0])u.shift(),void 0===r&&(r=e.mimeType||t.getResponseHeader("Content-Type"));if(r)for(i in s)if(s[i]&&s[i].test(r)){u.unshift(i);break}if(u[0]in n)o=u[0];else{for(i in n){if(!u[0]||e.converters[i+" "+u[0]]){o=i;break}a||(a=i)}o=o||a}if(o)return o!==u[0]&&u.unshift(o),n[o]}(v,T,n)),s=function(e,t,n,r){var i,o,a,s,u,l={},c=e.dataTypes.slice();if(c[1])for(a in e.converters)l[a.toLowerCase()]=e.converters[a];o=c.shift();while(o)if(e.responseFields[o]&&(n[e.responseFields[o]]=t),!u&&r&&e.dataFilter&&(t=e.dataFilter(t,e.dataType)),u=o,o=c.shift())if("*"===o)o=u;else if("*"!==u&&u!==o){if(!(a=l[u+" "+o]||l["* "+o]))for(i in l)if((s=i.split(" "))[1]===o&&(a=l[u+" "+s[0]]||l["* "+s[0]])){!0===a?a=l[i]:!0!==l[i]&&(o=s[0],c.unshift(s[1]));break}if(!0!==a)if(a&&e["throws"])t=a(t);else try{t=a(t)}catch(e){return{state:"parsererror",error:a?e:"No conversion from "+u+" to "+o}}}return{state:"success",data:t}}(v,s,T,i),i?(v.ifModified&&((u=T.getResponseHeader("Last-Modified"))&&(k.lastModified[f]=u),(u=T.getResponseHeader("etag"))&&(k.etag[f]=u)),204===e||"HEAD"===v.type?l="nocontent":304===e?l="notmodified":(l=s.state,o=s.data,i=!(a=s.error))):(a=l,!e&&l||(l="error",e<0&&(e=0))),T.status=e,T.statusText=(t||l)+"",i?x.resolveWith(y,[o,l,T]):x.rejectWith(y,[T,l,a]),T.statusCode(w),w=void 0,g&&m.trigger(i?"ajaxSuccess":"ajaxError",[T,v,i?o:a]),b.fireWith(y,[T,l]),g&&(m.trigger("ajaxComplete",[T,v]),--k.active||k.event.trigger("ajaxStop")))}return T},getJSON:function(e,t,n){return k.get(e,t,n,"json")},getScript:function(e,t){return k.get(e,void 0,t,"script")}}),k.each(["get","post"],function(e,i){k[i]=function(e,t,n,r){return m(t)&&(r=r||n,n=t,t=void 0),k.ajax(k.extend({url:e,type:i,dataType:r,data:t,success:n},k.isPlainObject(e)&&e))}}),k._evalUrl=function(e,t){return k.ajax({url:e,type:"GET",dataType:"script",cache:!0,async:!1,global:!1,converters:{"text script":function(){}},dataFilter:function(e){k.globalEval(e,t)}})},k.fn.extend({wrapAll:function(e){var t;return this[0]&&(m(e)&&(e=e.call(this[0])),t=k(e,this[0].ownerDocument).eq(0).clone(!0),this[0].parentNode&&t.insertBefore(this[0]),t.map(function(){var e=this;while(e.firstElementChild)e=e.firstElementChild;return e}).append(this)),this},wrapInner:function(n){return m(n)?this.each(function(e){k(this).wrapInner(n.call(this,e))}):this.each(function(){var e=k(this),t=e.contents();t.length?t.wrapAll(n):e.append(n)})},wrap:function(t){var n=m(t);return this.each(function(e){k(this).wrapAll(n?t.call(this,e):t)})},unwrap:function(e){return this.parent(e).not("body").each(function(){k(this).replaceWith(this.childNodes)}),this}}),k.expr.pseudos.hidden=function(e){return!k.expr.pseudos.visible(e)},k.expr.pseudos.visible=function(e){return!!(e.offsetWidth||e.offsetHeight||e.getClientRects().length)},k.ajaxSettings.xhr=function(){try{return new C.XMLHttpRequest}catch(e){}};var Ut={0:200,1223:204},Xt=k.ajaxSettings.xhr();y.cors=!!Xt&&"withCredentials"in Xt,y.ajax=Xt=!!Xt,k.ajaxTransport(function(i){var o,a;if(y.cors||Xt&&!i.crossDomain)return{send:function(e,t){var n,r=i.xhr();if(r.open(i.type,i.url,i.async,i.username,i.password),i.xhrFields)for(n in i.xhrFields)r[n]=i.xhrFields[n];for(n in i.mimeType&&r.overrideMimeType&&r.overrideMimeType(i.mimeType),i.crossDomain||e["X-Requested-With"]||(e["X-Requested-With"]="XMLHttpRequest"),e)r.setRequestHeader(n,e[n]);o=function(e){return function(){o&&(o=a=r.onload=r.onerror=r.onabort=r.ontimeout=r.onreadystatechange=null,"abort"===e?r.abort():"error"===e?"number"!=typeof r.status?t(0,"error"):t(r.status,r.statusText):t(Ut[r.status]||r.status,r.statusText,"text"!==(r.responseType||"text")||"string"!=typeof r.responseText?{binary:r.response}:{text:r.responseText},r.getAllResponseHeaders()))}},r.onload=o(),a=r.onerror=r.ontimeout=o("error"),void 0!==r.onabort?r.onabort=a:r.onreadystatechange=function(){4===r.readyState&&C.setTimeout(function(){o&&a()})},o=o("abort");try{r.send(i.hasContent&&i.data||null)}catch(e){if(o)throw e}},abort:function(){o&&o()}}}),k.ajaxPrefilter(function(e){e.crossDomain&&(e.contents.script=!1)}),k.ajaxSetup({accepts:{script:"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript"},contents:{script:/\b(?:java|ecma)script\b/},converters:{"text script":function(e){return k.globalEval(e),e}}}),k.ajaxPrefilter("script",function(e){void 0===e.cache&&(e.cache=!1),e.crossDomain&&(e.type="GET")}),k.ajaxTransport("script",function(n){var r,i;if(n.crossDomain||n.scriptAttrs)return{send:function(e,t){r=k("<script>").attr(n.scriptAttrs||{}).prop({charset:n.scriptCharset,src:n.url}).on("load error",i=function(e){r.remove(),i=null,e&&t("error"===e.type?404:200,e.type)}),E.head.appendChild(r[0])},abort:function(){i&&i()}}});var Vt,Gt=[],Yt=/(=)\?(?=&|$)|\?\?/;k.ajaxSetup({jsonp:"callback",jsonpCallback:function(){var e=Gt.pop()||k.expando+"_"+kt++;return this[e]=!0,e}}),k.ajaxPrefilter("json jsonp",function(e,t,n){var r,i,o,a=!1!==e.jsonp&&(Yt.test(e.url)?"url":"string"==typeof e.data&&0===(e.contentType||"").indexOf("application/x-www-form-urlencoded")&&Yt.test(e.data)&&"data");if(a||"jsonp"===e.dataTypes[0])return r=e.jsonpCallback=m(e.jsonpCallback)?e.jsonpCallback():e.jsonpCallback,a?e[a]=e[a].replace(Yt,"$1"+r):!1!==e.jsonp&&(e.url+=(St.test(e.url)?"&":"?")+e.jsonp+"="+r),e.converters["script json"]=function(){return o||k.error(r+" was not called"),o[0]},e.dataTypes[0]="json",i=C[r],C[r]=function(){o=arguments},n.always(function(){void 0===i?k(C).removeProp(r):C[r]=i,e[r]&&(e.jsonpCallback=t.jsonpCallback,Gt.push(r)),o&&m(i)&&i(o[0]),o=i=void 0}),"script"}),y.createHTMLDocument=((Vt=E.implementation.createHTMLDocument("").body).innerHTML="<form></form><form></form>",2===Vt.childNodes.length),k.parseHTML=function(e,t,n){return"string"!=typeof e?[]:("boolean"==typeof t&&(n=t,t=!1),t||(y.createHTMLDocument?((r=(t=E.implementation.createHTMLDocument("")).createElement("base")).href=E.location.href,t.head.appendChild(r)):t=E),o=!n&&[],(i=D.exec(e))?[t.createElement(i[1])]:(i=we([e],t,o),o&&o.length&&k(o).remove(),k.merge([],i.childNodes)));var r,i,o},k.fn.load=function(e,t,n){var r,i,o,a=this,s=e.indexOf(" ");return-1<s&&(r=mt(e.slice(s)),e=e.slice(0,s)),m(t)?(n=t,t=void 0):t&&"object"==typeof t&&(i="POST"),0<a.length&&k.ajax({url:e,type:i||"GET",dataType:"html",data:t}).done(function(e){o=arguments,a.html(r?k("<div>").append(k.parseHTML(e)).find(r):e)}).always(n&&function(e,t){a.each(function(){n.apply(this,o||[e.responseText,t,e])})}),this},k.each(["ajaxStart","ajaxStop","ajaxComplete","ajaxError","ajaxSuccess","ajaxSend"],function(e,t){k.fn[t]=function(e){return this.on(t,e)}}),k.expr.pseudos.animated=function(t){return k.grep(k.timers,function(e){return t===e.elem}).length},k.offset={setOffset:function(e,t,n){var r,i,o,a,s,u,l=k.css(e,"position"),c=k(e),f={};"static"===l&&(e.style.position="relative"),s=c.offset(),o=k.css(e,"top"),u=k.css(e,"left"),("absolute"===l||"fixed"===l)&&-1<(o+u).indexOf("auto")?(a=(r=c.position()).top,i=r.left):(a=parseFloat(o)||0,i=parseFloat(u)||0),m(t)&&(t=t.call(e,n,k.extend({},s))),null!=t.top&&(f.top=t.top-s.top+a),null!=t.left&&(f.left=t.left-s.left+i),"using"in t?t.using.call(e,f):c.css(f)}},k.fn.extend({offset:function(t){if(arguments.length)return void 0===t?this:this.each(function(e){k.offset.setOffset(this,t,e)});var e,n,r=this[0];return r?r.getClientRects().length?(e=r.getBoundingClientRect(),n=r.ownerDocument.defaultView,{top:e.top+n.pageYOffset,left:e.left+n.pageXOffset}):{top:0,left:0}:void 0},position:function(){if(this[0]){var e,t,n,r=this[0],i={top:0,left:0};if("fixed"===k.css(r,"position"))t=r.getBoundingClientRect();else{t=this.offset(),n=r.ownerDocument,e=r.offsetParent||n.documentElement;while(e&&(e===n.body||e===n.documentElement)&&"static"===k.css(e,"position"))e=e.parentNode;e&&e!==r&&1===e.nodeType&&((i=k(e).offset()).top+=k.css(e,"borderTopWidth",!0),i.left+=k.css(e,"borderLeftWidth",!0))}return{top:t.top-i.top-k.css(r,"marginTop",!0),left:t.left-i.left-k.css(r,"marginLeft",!0)}}},offsetParent:function(){return this.map(function(){var e=this.offsetParent;while(e&&"static"===k.css(e,"position"))e=e.offsetParent;return e||ie})}}),k.each({scrollLeft:"pageXOffset",scrollTop:"pageYOffset"},function(t,i){var o="pageYOffset"===i;k.fn[t]=function(e){return _(this,function(e,t,n){var r;if(x(e)?r=e:9===e.nodeType&&(r=e.defaultView),void 0===n)return r?r[i]:e[t];r?r.scrollTo(o?r.pageXOffset:n,o?n:r.pageYOffset):e[t]=n},t,e,arguments.length)}}),k.each(["top","left"],function(e,n){k.cssHooks[n]=ze(y.pixelPosition,function(e,t){if(t)return t=_e(e,n),$e.test(t)?k(e).position()[n]+"px":t})}),k.each({Height:"height",Width:"width"},function(a,s){k.each({padding:"inner"+a,content:s,"":"outer"+a},function(r,o){k.fn[o]=function(e,t){var n=arguments.length&&(r||"boolean"!=typeof e),i=r||(!0===e||!0===t?"margin":"border");return _(this,function(e,t,n){var r;return x(e)?0===o.indexOf("outer")?e["inner"+a]:e.document.documentElement["client"+a]:9===e.nodeType?(r=e.documentElement,Math.max(e.body["scroll"+a],r["scroll"+a],e.body["offset"+a],r["offset"+a],r["client"+a])):void 0===n?k.css(e,t,i):k.style(e,t,n,i)},s,n?e:void 0,n)}})}),k.each("blur focus focusin focusout resize scroll click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup contextmenu".split(" "),function(e,n){k.fn[n]=function(e,t){return 0<arguments.length?this.on(n,null,e,t):this.trigger(n)}}),k.fn.extend({hover:function(e,t){return this.mouseenter(e).mouseleave(t||e)}}),k.fn.extend({bind:function(e,t,n){return this.on(e,null,t,n)},unbind:function(e,t){return this.off(e,null,t)},delegate:function(e,t,n,r){return this.on(t,e,n,r)},undelegate:function(e,t,n){return 1===arguments.length?this.off(e,"**"):this.off(t,e||"**",n)}}),k.proxy=function(e,t){var n,r,i;if("string"==typeof t&&(n=e[t],t=e,e=n),m(e))return r=s.call(arguments,2),(i=function(){return e.apply(t||this,r.concat(s.call(arguments)))}).guid=e.guid=e.guid||k.guid++,i},k.holdReady=function(e){e?k.readyWait++:k.ready(!0)},k.isArray=Array.isArray,k.parseJSON=JSON.parse,k.nodeName=A,k.isFunction=m,k.isWindow=x,k.camelCase=V,k.type=w,k.now=Date.now,k.isNumeric=function(e){var t=k.type(e);return("number"===t||"string"===t)&&!isNaN(e-parseFloat(e))},"function"==typeof define&&define.amd&&define("jquery",[],function(){return k});var Qt=C.jQuery,Jt=C.$;return k.noConflict=function(e){return C.$===k&&(C.$=Jt),e&&C.jQuery===k&&(C.jQuery=Qt),k},e||(C.jQuery=C.$=k),k});
      4 
      5 /* ./modules/cms/ui/themes/default/script/jquery-ui.min.js *//*! jQuery UI - v1.12.1 - 2018-09-03
      6 * http://jqueryui.com
      7 * Includes: widget.js, data.js, scroll-parent.js, widgets/draggable.js, widgets/droppable.js, widgets/sortable.js, widgets/mouse.js
      8 * Copyright jQuery Foundation and other contributors; Licensed MIT */
      9 (function(t){if(typeof define==="function"&&define.amd){define(["jquery"],t)}
     10 else{t(jQuery)}}(function(t){t.ui=t.ui||{};var g=t.ui.version="1.12.1";
     11 /*!
     12  * jQuery UI Widget 1.12.1
     13  * http://jqueryui.com
     14  *
     15  * Copyright jQuery Foundation and other contributors
     16  * Released under the MIT license.
     17  * http://jquery.org/license
     18  */
     19 ;var o=0,s=Array.prototype.slice;t.cleanData=(function(e){return function(i){var s,o,n;for(n=0;(o=i[n])!=null;n++){try{s=t._data(o,"events");if(s&&s.remove){t(o).triggerHandler("remove")}}catch(r){}};e(i)}})(t.cleanData);t.widget=function(e,i,s){var r,o,a,l={};var n=e.split(".")[0];e=e.split(".")[1];var h=n+"-"+e;if(!s){s=i;i=t.Widget};if(t.isArray(s)){s=t.extend.apply(null,[{}].concat(s))};t.expr[":"][h.toLowerCase()]=function(e){return!!t.data(e,h)};t[n]=t[n]||{};r=t[n][e];o=t[n][e]=function(t,e){if(!this._createWidget){return new o(t,e)};if(arguments.length){this._createWidget(t,e)}};t.extend(o,r,{version:s.version,_proto:t.extend({},s),_childConstructors:[]});a=new i();a.options=t.widget.extend({},a.options);t.each(s,function(e,s){if(!t.isFunction(s)){l[e]=s;return};l[e]=(function(){function t(){return i.prototype[e].apply(this,arguments)};function o(t){return i.prototype[e].apply(this,t)};return function(){var i=this._super,n=this._superApply,e;this._super=t;this._superApply=o;e=s.apply(this,arguments);this._super=i;this._superApply=n;return e}})()});o.prototype=t.widget.extend(a,{widgetEventPrefix:r?(a.widgetEventPrefix||e):e},l,{constructor:o,namespace:n,widgetName:e,widgetFullName:h});if(r){t.each(r._childConstructors,function(e,i){var s=i.prototype;t.widget(s.namespace+"."+s.widgetName,o,i._proto)});delete r._childConstructors}
     20 else{i._childConstructors.push(o)};t.widget.bridge(e,o);return o};t.widget.extend=function(e){var r=s.call(arguments,1),n=0,a=r.length,i,o;for(;n<a;n++){for(i in r[n]){o=r[n][i];if(r[n].hasOwnProperty(i)&&o!==undefined){if(t.isPlainObject(o)){e[i]=t.isPlainObject(e[i])?t.widget.extend({},e[i],o):t.widget.extend({},o)}
     21 else{e[i]=o}}}};return e};t.widget.bridge=function(e,i){var o=i.prototype.widgetFullName||e;t.fn[e]=function(n){var h=typeof n==="string",a=s.call(arguments,1),r=this;if(h){if(!this.length&&n==="instance"){r=undefined}
     22 else{this.each(function(){var i,s=t.data(this,o);if(n==="instance"){r=s;return!1};if(!s){return t.error("cannot call methods on "+e+" prior to initialization; attempted to call method '"+n+"'")};if(!t.isFunction(s[n])||n.charAt(0)==="_"){return t.error("no such method '"+n+"' for "+e+" widget instance")};i=s[n].apply(s,a);if(i!==s&&i!==undefined){r=i&&i.jquery?r.pushStack(i.get()):i;return!1}})}}
     23 else{if(a.length){n=t.widget.extend.apply(null,[n].concat(a))};this.each(function(){var e=t.data(this,o);if(e){e.option(n||{});if(e._init){e._init()}}
     24 else{t.data(this,o,new i(n,this))}})};return r}};t.Widget=function(){};t.Widget._childConstructors=[];t.Widget.prototype={widgetName:"widget",widgetEventPrefix:"",defaultElement:"<div>",options:{classes:{},disabled:!1,create:null},_createWidget:function(e,i){i=t(i||this.defaultElement||this)[0];this.element=t(i);this.uuid=o++;this.eventNamespace="."+this.widgetName+this.uuid;this.bindings=t();this.hoverable=t();this.focusable=t();this.classesElementLookup={};if(i!==this){t.data(i,this.widgetFullName,this);this._on(!0,this.element,{remove:function(t){if(t.target===i){this.destroy()}}});this.document=t(i.style?i.ownerDocument:i.document||i);this.window=t(this.document[0].defaultView||this.document[0].parentWindow)};this.options=t.widget.extend({},this.options,this._getCreateOptions(),e);this._create();if(this.options.disabled){this._setOptionDisabled(this.options.disabled)};this._trigger("create",null,this._getCreateEventData());this._init()},_getCreateOptions:function(){return{}},_getCreateEventData:t.noop,_create:t.noop,_init:t.noop,destroy:function(){var e=this;this._destroy();t.each(this.classesElementLookup,function(t,i){e._removeClass(i,t)});this.element.off(this.eventNamespace).removeData(this.widgetFullName);this.widget().off(this.eventNamespace).removeAttr("aria-disabled");this.bindings.off(this.eventNamespace)},_destroy:t.noop,widget:function(){return this.element},option:function(e,i){var r=e,s,o,n;if(arguments.length===0){return t.widget.extend({},this.options)};if(typeof e==="string"){r={};s=e.split(".");e=s.shift();if(s.length){o=r[e]=t.widget.extend({},this.options[e]);for(n=0;n<s.length-1;n++){o[s[n]]=o[s[n]]||{};o=o[s[n]]};e=s.pop();if(arguments.length===1){return o[e]===undefined?null:o[e]};o[e]=i}
     25 else{if(arguments.length===1){return this.options[e]===undefined?null:this.options[e]};r[e]=i}};this._setOptions(r);return this},_setOptions:function(t){var e;for(e in t){this._setOption(e,t[e])};return this},_setOption:function(t,e){if(t==="classes"){this._setOptionClasses(e)};this.options[t]=e;if(t==="disabled"){this._setOptionDisabled(e)};return this},_setOptionClasses:function(e){var i,o,s;for(i in e){s=this.classesElementLookup[i];if(e[i]===this.options.classes[i]||!s||!s.length){continue};o=t(s.get());this._removeClass(s,i);o.addClass(this._classes({element:o,keys:i,classes:e,add:!0}))}},_setOptionDisabled:function(t){this._toggleClass(this.widget(),this.widgetFullName+"-disabled",null,!!t);if(t){this._removeClass(this.hoverable,null,"ui-state-hover");this._removeClass(this.focusable,null,"ui-state-focus")}},enable:function(){return this._setOptions({disabled:!1})},disable:function(){return this._setOptions({disabled:!0})},_classes:function(e){var i=[],s=this;e=t.extend({element:this.element,classes:this.options.classes||{}},e);function o(o,n){var a,r;for(r=0;r<o.length;r++){a=s.classesElementLookup[o[r]]||t();if(e.add){a=t(t.unique(a.get().concat(e.element.get())))}
     26 else{a=t(a.not(e.element).get())};s.classesElementLookup[o[r]]=a;i.push(o[r]);if(n&&e.classes[o[r]]){i.push(e.classes[o[r]])}}};this._on(e.element,{"remove":"_untrackClassesElement"});if(e.keys){o(e.keys.match(/\S+/g)||[],!0)};if(e.extra){o(e.extra.match(/\S+/g)||[])};return i.join(" ")},_untrackClassesElement:function(e){var i=this;t.each(i.classesElementLookup,function(s,o){if(t.inArray(e.target,o)!==-1){i.classesElementLookup[s]=t(o.not(e.target).get())}})},_removeClass:function(t,e,i){return this._toggleClass(t,e,i,!1)},_addClass:function(t,e,i){return this._toggleClass(t,e,i,!0)},_toggleClass:function(t,e,i,s){s=(typeof s==="boolean")?s:i;var o=(typeof t==="string"||t===null),n={extra:o?e:i,keys:o?t:e,element:o?this.element:t,add:s};n.element.toggleClass(this._classes(n),s);return this},_on:function(e,i,s){var n,o=this;if(typeof e!=="boolean"){s=i;i=e;e=!1};if(!s){s=i;i=this.element;n=this.widget()}
     27 else{i=n=t(i);this.bindings=this.bindings.add(i)};t.each(s,function(s,r){function a(){if(!e&&(o.options.disabled===!0||t(this).hasClass("ui-state-disabled"))){return};return(typeof r==="string"?o[r]:r).apply(o,arguments)};if(typeof r!=="string"){a.guid=r.guid=r.guid||a.guid||t.guid++};var h=s.match(/^([\w:-]*)\s*(.*)$/),l=h[1]+o.eventNamespace,c=h[2];if(c){n.on(l,c,a)}
     28 else{i.on(l,a)}})},_off:function(e,i){i=(i||"").split(" ").join(this.eventNamespace+" ")+this.eventNamespace;e.off(i).off(i);this.bindings=t(this.bindings.not(e).get());this.focusable=t(this.focusable.not(e).get());this.hoverable=t(this.hoverable.not(e).get())},_delay:function(t,e){function s(){return(typeof t==="string"?i[t]:t).apply(i,arguments)};var i=this;return setTimeout(s,e||0)},_hoverable:function(e){this.hoverable=this.hoverable.add(e);this._on(e,{mouseenter:function(e){this._addClass(t(e.currentTarget),null,"ui-state-hover")},mouseleave:function(e){this._removeClass(t(e.currentTarget),null,"ui-state-hover")}})},_focusable:function(e){this.focusable=this.focusable.add(e);this._on(e,{focusin:function(e){this._addClass(t(e.currentTarget),null,"ui-state-focus")},focusout:function(e){this._removeClass(t(e.currentTarget),null,"ui-state-focus")}})},_trigger:function(e,i,s){var o,n,r=this.options[e];s=s||{};i=t.Event(i);i.type=(e===this.widgetEventPrefix?e:this.widgetEventPrefix+e).toLowerCase();i.target=this.element[0];n=i.originalEvent;if(n){for(o in n){if(!(o in i)){i[o]=n[o]}}};this.element.trigger(i,s);return!(t.isFunction(r)&&r.apply(this.element[0],[i].concat(s))===!1||i.isDefaultPrevented())}};t.each({show:"fadeIn",hide:"fadeOut"},function(e,i){t.Widget.prototype["_"+e]=function(s,o,n){if(typeof o==="string"){o={effect:o}};var a,r=!o?e:o===!0||typeof o==="number"?i:o.effect||i;o=o||{};if(typeof o==="number"){o={duration:o}};a=!t.isEmptyObject(o);o.complete=n;if(o.delay){s.delay(o.delay)};if(a&&t.effects&&t.effects.effect[r]){s[e](o)}
     29 else if(r!==e&&s[r]){s[r](o.duration,o.easing,n)}
     30 else{s.queue(function(i){t(this)[e]();if(n){n.call(s[0])};i()})}}});var m=t.widget;
     31 /*!
     32  * jQuery UI :data 1.12.1
     33  * http://jqueryui.com
     34  *
     35  * Copyright jQuery Foundation and other contributors
     36  * Released under the MIT license.
     37  * http://jquery.org/license
     38  */
     39 ;var d=t.extend(t.expr[":"],{data:t.expr.createPseudo?t.expr.createPseudo(function(e){return function(i){return!!t.data(i,e)}}):function(e,i,s){return!!t.data(e,s[3])}});
     40 /*!
     41  * jQuery UI Scroll Parent 1.12.1
     42  * http://jqueryui.com
     43  *
     44  * Copyright jQuery Foundation and other contributors
     45  * Released under the MIT license.
     46  * http://jquery.org/license
     47  */
     48 ;var p=t.fn.scrollParent=function(e){var i=this.css("position"),o=i==="absolute",n=e?/(auto|scroll|hidden)/:/(auto|scroll)/,s=this.parents().filter(function(){var e=t(this);if(o&&e.css("position")==="static"){return!1};return n.test(e.css("overflow")+e.css("overflow-y")+e.css("overflow-x"))}).eq(0);return i==="fixed"||!s.length?t(this[0].ownerDocument||document):s},u=t.ui.ie=!!/msie [\w.]+/.exec(navigator.userAgent.toLowerCase());
     49 /*!
     50  * jQuery UI Mouse 1.12.1
     51  * http://jqueryui.com
     52  *
     53  * Copyright jQuery Foundation and other contributors
     54  * Released under the MIT license.
     55  * http://jquery.org/license
     56  */
     57 ;var e=!1;t(document).on("mouseup",function(){e=!1});var f=t.widget("ui.mouse",{version:"1.12.1",options:{cancel:"input, textarea, button, select, option",distance:1,delay:0},_mouseInit:function(){var e=this;this.element.on("mousedown."+this.widgetName,function(t){return e._mouseDown(t)}).on("click."+this.widgetName,function(i){if(!0===t.data(i.target,e.widgetName+".preventClickEvent")){t.removeData(i.target,e.widgetName+".preventClickEvent");i.stopImmediatePropagation();return!1}});this.started=!1},_mouseDestroy:function(){this.element.off("."+this.widgetName);if(this._mouseMoveDelegate){this.document.off("mousemove."+this.widgetName,this._mouseMoveDelegate).off("mouseup."+this.widgetName,this._mouseUpDelegate)}},_mouseDown:function(i){if(e){return};this._mouseMoved=!1;(this._mouseStarted&&this._mouseUp(i));this._mouseDownEvent=i;var s=this,o=(i.which===1),n=(typeof this.options.cancel==="string"&&i.target.nodeName?t(i.target).closest(this.options.cancel).length:!1);if(!o||n||!this._mouseCapture(i)){return!0};this.mouseDelayMet=!this.options.delay;if(!this.mouseDelayMet){this._mouseDelayTimer=setTimeout(function(){s.mouseDelayMet=!0},this.options.delay)};if(this._mouseDistanceMet(i)&&this._mouseDelayMet(i)){this._mouseStarted=(this._mouseStart(i)!==!1);if(!this._mouseStarted){i.preventDefault();return!0}};if(!0===t.data(i.target,this.widgetName+".preventClickEvent")){t.removeData(i.target,this.widgetName+".preventClickEvent")};this._mouseMoveDelegate=function(t){return s._mouseMove(t)};this._mouseUpDelegate=function(t){return s._mouseUp(t)};this.document.on("mousemove."+this.widgetName,this._mouseMoveDelegate).on("mouseup."+this.widgetName,this._mouseUpDelegate);i.preventDefault();e=!0;return!0},_mouseMove:function(e){if(this._mouseMoved){if(t.ui.ie&&(!document.documentMode||document.documentMode<9)&&!e.button){return this._mouseUp(e)}
     58 else if(!e.which){if(e.originalEvent.altKey||e.originalEvent.ctrlKey||e.originalEvent.metaKey||e.originalEvent.shiftKey){this.ignoreMissingWhich=!0}
     59 else if(!this.ignoreMissingWhich){return this._mouseUp(e)}}};if(e.which||e.button){this._mouseMoved=!0};if(this._mouseStarted){this._mouseDrag(e);return e.preventDefault()};if(this._mouseDistanceMet(e)&&this._mouseDelayMet(e)){this._mouseStarted=(this._mouseStart(this._mouseDownEvent,e)!==!1);(this._mouseStarted?this._mouseDrag(e):this._mouseUp(e))};return!this._mouseStarted},_mouseUp:function(i){this.document.off("mousemove."+this.widgetName,this._mouseMoveDelegate).off("mouseup."+this.widgetName,this._mouseUpDelegate);if(this._mouseStarted){this._mouseStarted=!1;if(i.target===this._mouseDownEvent.target){t.data(i.target,this.widgetName+".preventClickEvent",!0)};this._mouseStop(i)};if(this._mouseDelayTimer){clearTimeout(this._mouseDelayTimer);delete this._mouseDelayTimer};this.ignoreMissingWhich=!1;e=!1;i.preventDefault()},_mouseDistanceMet:function(t){return(Math.max(Math.abs(this._mouseDownEvent.pageX-t.pageX),Math.abs(this._mouseDownEvent.pageY-t.pageY))>=this.options.distance)},_mouseDelayMet:function(){return this.mouseDelayMet},_mouseStart:function(){},_mouseDrag:function(){},_mouseStop:function(){},_mouseCapture:function(){return!0}});var c=t.ui.plugin={add:function(e,i,s){var o,n=t.ui[e].prototype;for(o in s){n.plugins[o]=n.plugins[o]||[];n.plugins[o].push([i,s[o]])}},call:function(t,e,i,o){var s,n=t.plugins[e];if(!n){return};if(!o&&(!t.element[0].parentNode||t.element[0].parentNode.nodeType===11)){return};for(s=0;s<n.length;s++){if(t.options[n[s][0]]){n[s][1].apply(t.element,i)}}}};var h=t.ui.safeActiveElement=function(t){var e;try{e=t.activeElement}catch(i){e=t.body};if(!e){e=t.body};if(!e.nodeName){e=t.body};return e},l=t.ui.safeBlur=function(e){if(e&&e.nodeName.toLowerCase()!=="body"){t(e).trigger("blur")}};
     60 /*!
     61  * jQuery UI Draggable 1.12.1
     62  * http://jqueryui.com
     63  *
     64  * Copyright jQuery Foundation and other contributors
     65  * Released under the MIT license.
     66  * http://jquery.org/license
     67  */
     68 ;t.widget("ui.draggable",t.ui.mouse,{version:"1.12.1",widgetEventPrefix:"drag",options:{addClasses:!0,appendTo:"parent",axis:!1,connectToSortable:!1,containment:!1,cursor:"auto",cursorAt:!1,grid:!1,handle:!1,helper:"original",iframeFix:!1,opacity:!1,refreshPositions:!1,revert:!1,revertDuration:500,scope:"default",scroll:!0,scrollSensitivity:20,scrollSpeed:20,snap:!1,snapMode:"both",snapTolerance:20,stack:!1,zIndex:!1,drag:null,start:null,stop:null},_create:function(){if(this.options.helper==="original"){this._setPositionRelative()};if(this.options.addClasses){this._addClass("ui-draggable")};this._setHandleClassName();this._mouseInit()},_setOption:function(t,e){this._super(t,e);if(t==="handle"){this._removeHandleClassName();this._setHandleClassName()}},_destroy:function(){if((this.helper||this.element).is(".ui-draggable-dragging")){this.destroyOnClear=!0;return};this._removeHandleClassName();this._mouseDestroy()},_mouseCapture:function(e){var i=this.options;if(this.helper||i.disabled||t(e.target).closest(".ui-resizable-handle").length>0){return!1};this.handle=this._getHandle(e);if(!this.handle){return!1};this._blurActiveElement(e);this._blockFrames(i.iframeFix===!0?"iframe":i.iframeFix);return!0},_blockFrames:function(e){this.iframeBlocks=this.document.find(e).map(function(){var e=t(this);return t("<div>").css("position","absolute").appendTo(e.parent()).outerWidth(e.outerWidth()).outerHeight(e.outerHeight()).offset(e.offset())[0]})},_unblockFrames:function(){if(this.iframeBlocks){this.iframeBlocks.remove();delete this.iframeBlocks}},_blurActiveElement:function(e){var i=t.ui.safeActiveElement(this.document[0]),s=t(e.target);if(s.closest(i).length){return};t.ui.safeBlur(i)},_mouseStart:function(e){var i=this.options;this.helper=this._createHelper(e);this._addClass(this.helper,"ui-draggable-dragging");this._cacheHelperProportions();if(t.ui.ddmanager){t.ui.ddmanager.current=this};this._cacheMargins();this.cssPosition=this.helper.css("position");this.scrollParent=this.helper.scrollParent(!0);this.offsetParent=this.helper.offsetParent();this.hasFixedAncestor=this.helper.parents().filter(function(){return t(this).css("position")==="fixed"}).length>0;this.positionAbs=this.element.offset();this._refreshOffsets(e);this.originalPosition=this.position=this._generatePosition(e,!1);this.originalPageX=e.pageX;this.originalPageY=e.pageY;(i.cursorAt&&this._adjustOffsetFromHelper(i.cursorAt));this._setContainment();if(this._trigger("start",e)===!1){this._clear();return!1};this._cacheHelperProportions();if(t.ui.ddmanager&&!i.dropBehaviour){t.ui.ddmanager.prepareOffsets(this,e)};this._mouseDrag(e,!0);if(t.ui.ddmanager){t.ui.ddmanager.dragStart(this,e)};return!0},_refreshOffsets:function(t){this.offset={top:this.positionAbs.top-this.margins.top,left:this.positionAbs.left-this.margins.left,scroll:!1,parent:this._getParentOffset(),relative:this._getRelativeOffset()};this.offset.click={left:t.pageX-this.offset.left,top:t.pageY-this.offset.top}},_mouseDrag:function(e,i){if(this.hasFixedAncestor){this.offset.parent=this._getParentOffset()};this.position=this._generatePosition(e,!0);this.positionAbs=this._convertPositionTo("absolute");if(!i){var s=this._uiHash();if(this._trigger("drag",e,s)===!1){this._mouseUp(new t.Event("mouseup",e));return!1};this.position=s.position};this.helper[0].style.left=this.position.left+"px";this.helper[0].style.top=this.position.top+"px";if(t.ui.ddmanager){t.ui.ddmanager.drag(this,e)};return!1},_mouseStop:function(e){var s=this,i=!1;if(t.ui.ddmanager&&!this.options.dropBehaviour){i=t.ui.ddmanager.drop(this,e)};if(this.dropped){i=this.dropped;this.dropped=!1};if((this.options.revert==="invalid"&&!i)||(this.options.revert==="valid"&&i)||this.options.revert===!0||(t.isFunction(this.options.revert)&&this.options.revert.call(this.element,i))){t(this.helper).animate(this.originalPosition,parseInt(this.options.revertDuration,10),function(){if(s._trigger("stop",e)!==!1){s._clear()}})}
     69 else{if(this._trigger("stop",e)!==!1){this._clear()}};return!1},_mouseUp:function(e){this._unblockFrames();if(t.ui.ddmanager){t.ui.ddmanager.dragStop(this,e)};if(this.handleElement.is(e.target)){this.element.trigger("focus")};return t.ui.mouse.prototype._mouseUp.call(this,e)},cancel:function(){if(this.helper.is(".ui-draggable-dragging")){this._mouseUp(new t.Event("mouseup",{target:this.element[0]}))}
     70 else{this._clear()};return this},_getHandle:function(e){return this.options.handle?!!t(e.target).closest(this.element.find(this.options.handle)).length:!0},_setHandleClassName:function(){this.handleElement=this.options.handle?this.element.find(this.options.handle):this.element;this._addClass(this.handleElement,"ui-draggable-handle")},_removeHandleClassName:function(){this._removeClass(this.handleElement,"ui-draggable-handle")},_createHelper:function(e){var s=this.options,o=t.isFunction(s.helper),i=o?t(s.helper.apply(this.element[0],[e])):(s.helper==="clone"?this.element.clone().removeAttr("id"):this.element);if(!i.parents("body").length){i.appendTo((s.appendTo==="parent"?this.element[0].parentNode:s.appendTo))};if(o&&i[0]===this.element[0]){this._setPositionRelative()};if(i[0]!==this.element[0]&&!(/(fixed|absolute)/).test(i.css("position"))){i.css("position","absolute")};return i},_setPositionRelative:function(){if(!(/^(?:r|a|f)/).test(this.element.css("position"))){this.element[0].style.position="relative"}},_adjustOffsetFromHelper:function(e){if(typeof e==="string"){e=e.split(" ")};if(t.isArray(e)){e={left:+e[0],top:+e[1]||0}};if("left" in e){this.offset.click.left=e.left+this.margins.left};if("right" in e){this.offset.click.left=this.helperProportions.width-e.right+this.margins.left};if("top" in e){this.offset.click.top=e.top+this.margins.top};if("bottom" in e){this.offset.click.top=this.helperProportions.height-e.bottom+this.margins.top}},_isRootNode:function(t){return(/(html|body)/i).test(t.tagName)||t===this.document[0]},_getParentOffset:function(){var e=this.offsetParent.offset(),i=this.document[0];if(this.cssPosition==="absolute"&&this.scrollParent[0]!==i&&t.contains(this.scrollParent[0],this.offsetParent[0])){e.left+=this.scrollParent.scrollLeft();e.top+=this.scrollParent.scrollTop()};if(this._isRootNode(this.offsetParent[0])){e={top:0,left:0}};return{top:e.top+(parseInt(this.offsetParent.css("borderTopWidth"),10)||0),left:e.left+(parseInt(this.offsetParent.css("borderLeftWidth"),10)||0)}},_getRelativeOffset:function(){if(this.cssPosition!=="relative"){return{top:0,left:0}};var t=this.element.position(),e=this._isRootNode(this.scrollParent[0]);return{top:t.top-(parseInt(this.helper.css("top"),10)||0)+(!e?this.scrollParent.scrollTop():0),left:t.left-(parseInt(this.helper.css("left"),10)||0)+(!e?this.scrollParent.scrollLeft():0)}},_cacheMargins:function(){this.margins={left:(parseInt(this.element.css("marginLeft"),10)||0),top:(parseInt(this.element.css("marginTop"),10)||0),right:(parseInt(this.element.css("marginRight"),10)||0),bottom:(parseInt(this.element.css("marginBottom"),10)||0)}},_cacheHelperProportions:function(){this.helperProportions={width:this.helper.outerWidth(),height:this.helper.outerHeight()}},_setContainment:function(){var n,e,i,s=this.options,o=this.document[0];this.relativeContainer=null;if(!s.containment){this.containment=null;return};if(s.containment==="window"){this.containment=[t(window).scrollLeft()-this.offset.relative.left-this.offset.parent.left,t(window).scrollTop()-this.offset.relative.top-this.offset.parent.top,t(window).scrollLeft()+t(window).width()-this.helperProportions.width-this.margins.left,t(window).scrollTop()+(t(window).height()||o.body.parentNode.scrollHeight)-this.helperProportions.height-this.margins.top];return};if(s.containment==="document"){this.containment=[0,0,t(o).width()-this.helperProportions.width-this.margins.left,(t(o).height()||o.body.parentNode.scrollHeight)-this.helperProportions.height-this.margins.top];return};if(s.containment.constructor===Array){this.containment=s.containment;return};if(s.containment==="parent"){s.containment=this.helper[0].parentNode};e=t(s.containment);i=e[0];if(!i){return};n=/(scroll|auto)/.test(e.css("overflow"));this.containment=[(parseInt(e.css("borderLeftWidth"),10)||0)+(parseInt(e.css("paddingLeft"),10)||0),(parseInt(e.css("borderTopWidth"),10)||0)+(parseInt(e.css("paddingTop"),10)||0),(n?Math.max(i.scrollWidth,i.offsetWidth):i.offsetWidth)-(parseInt(e.css("borderRightWidth"),10)||0)-(parseInt(e.css("paddingRight"),10)||0)-this.helperProportions.width-this.margins.left-this.margins.right,(n?Math.max(i.scrollHeight,i.offsetHeight):i.offsetHeight)-(parseInt(e.css("borderBottomWidth"),10)||0)-(parseInt(e.css("paddingBottom"),10)||0)-this.helperProportions.height-this.margins.top-this.margins.bottom];this.relativeContainer=e},_convertPositionTo:function(t,e){if(!e){e=this.position};var i=t==="absolute"?1:-1,s=this._isRootNode(this.scrollParent[0]);return{top:(e.top+this.offset.relative.top*i+this.offset.parent.top*i-((this.cssPosition==="fixed"?-this.offset.scroll.top:(s?0:this.offset.scroll.top))*i)),left:(e.left+this.offset.relative.left*i+this.offset.parent.left*i-((this.cssPosition==="fixed"?-this.offset.scroll.left:(s?0:this.offset.scroll.left))*i))}},_generatePosition:function(t,e){var i,h,o,n,s=this.options,l=this._isRootNode(this.scrollParent[0]),r=t.pageX,a=t.pageY;if(!l||!this.offset.scroll){this.offset.scroll={top:this.scrollParent.scrollTop(),left:this.scrollParent.scrollLeft()}};if(e){if(this.containment){if(this.relativeContainer){h=this.relativeContainer.offset();i=[this.containment[0]+h.left,this.containment[1]+h.top,this.containment[2]+h.left,this.containment[3]+h.top]}
     71 else{i=this.containment};if(t.pageX-this.offset.click.left<i[0]){r=i[0]+this.offset.click.left};if(t.pageY-this.offset.click.top<i[1]){a=i[1]+this.offset.click.top};if(t.pageX-this.offset.click.left>i[2]){r=i[2]+this.offset.click.left};if(t.pageY-this.offset.click.top>i[3]){a=i[3]+this.offset.click.top}};if(s.grid){o=s.grid[1]?this.originalPageY+Math.round((a-this.originalPageY)/s.grid[1])*s.grid[1]:this.originalPageY;a=i?((o-this.offset.click.top>=i[1]||o-this.offset.click.top>i[3])?o:((o-this.offset.click.top>=i[1])?o-s.grid[1]:o+s.grid[1])):o;n=s.grid[0]?this.originalPageX+Math.round((r-this.originalPageX)/s.grid[0])*s.grid[0]:this.originalPageX;r=i?((n-this.offset.click.left>=i[0]||n-this.offset.click.left>i[2])?n:((n-this.offset.click.left>=i[0])?n-s.grid[0]:n+s.grid[0])):n};if(s.axis==="y"){r=this.originalPageX};if(s.axis==="x"){a=this.originalPageY}};return{top:(a-this.offset.click.top-this.offset.relative.top-this.offset.parent.top+(this.cssPosition==="fixed"?-this.offset.scroll.top:(l?0:this.offset.scroll.top))),left:(r-this.offset.click.left-this.offset.relative.left-this.offset.parent.left+(this.cssPosition==="fixed"?-this.offset.scroll.left:(l?0:this.offset.scroll.left)))}},_clear:function(){this._removeClass(this.helper,"ui-draggable-dragging");if(this.helper[0]!==this.element[0]&&!this.cancelHelperRemoval){this.helper.remove()};this.helper=null;this.cancelHelperRemoval=!1;if(this.destroyOnClear){this.destroy()}},_trigger:function(e,i,s){s=s||this._uiHash();t.ui.plugin.call(this,e,[i,s,this],!0);if(/^(drag|start|stop)/.test(e)){this.positionAbs=this._convertPositionTo("absolute");s.offset=this.positionAbs};return t.Widget.prototype._trigger.call(this,e,i,s)},plugins:{},_uiHash:function(){return{helper:this.helper,position:this.position,originalPosition:this.originalPosition,offset:this.positionAbs}}});t.ui.plugin.add("draggable","connectToSortable",{start:function(e,i,s){var o=t.extend({},i,{item:s.element});s.sortables=[];t(s.options.connectToSortable).each(function(){var i=t(this).sortable("instance");if(i&&!i.options.disabled){s.sortables.push(i);i.refreshPositions();i._trigger("activate",e,o)}})},stop:function(e,i,s){var o=t.extend({},i,{item:s.element});s.cancelHelperRemoval=!1;t.each(s.sortables,function(){var t=this;if(t.isOver){t.isOver=0;s.cancelHelperRemoval=!0;t.cancelHelperRemoval=!1;t._storedCSS={position:t.placeholder.css("position"),top:t.placeholder.css("top"),left:t.placeholder.css("left")};t._mouseStop(e);t.options.helper=t.options._helper}
     72 else{t.cancelHelperRemoval=!0;t._trigger("deactivate",e,o)}})},drag:function(e,i,s){t.each(s.sortables,function(){var n=!1,o=this;o.positionAbs=s.positionAbs;o.helperProportions=s.helperProportions;o.offset.click=s.offset.click;if(o._intersectsWith(o.containerCache)){n=!0;t.each(s.sortables,function(){this.positionAbs=s.positionAbs;this.helperProportions=s.helperProportions;this.offset.click=s.offset.click;if(this!==o&&this._intersectsWith(this.containerCache)&&t.contains(o.element[0],this.element[0])){n=!1};return n})};if(n){if(!o.isOver){o.isOver=1;s._parent=i.helper.parent();o.currentItem=i.helper.appendTo(o.element).data("ui-sortable-item",!0);o.options._helper=o.options.helper;o.options.helper=function(){return i.helper[0]};e.target=o.currentItem[0];o._mouseCapture(e,!0);o._mouseStart(e,!0,!0);o.offset.click.top=s.offset.click.top;o.offset.click.left=s.offset.click.left;o.offset.parent.left-=s.offset.parent.left-o.offset.parent.left;o.offset.parent.top-=s.offset.parent.top-o.offset.parent.top;s._trigger("toSortable",e);s.dropped=o.element;t.each(s.sortables,function(){this.refreshPositions()});s.currentItem=s.element;o.fromOutside=s};if(o.currentItem){o._mouseDrag(e);i.position=o.position}}
     73 else{if(o.isOver){o.isOver=0;o.cancelHelperRemoval=!0;o.options._revert=o.options.revert;o.options.revert=!1;o._trigger("out",e,o._uiHash(o));o._mouseStop(e,!0);o.options.revert=o.options._revert;o.options.helper=o.options._helper;if(o.placeholder){o.placeholder.remove()};i.helper.appendTo(s._parent);s._refreshOffsets(e);i.position=s._generatePosition(e,!0);s._trigger("fromSortable",e);s.dropped=!1;t.each(s.sortables,function(){this.refreshPositions()})}}})}});t.ui.plugin.add("draggable","cursor",{start:function(e,i,s){var o=t("body"),n=s.options;if(o.css("cursor")){n._cursor=o.css("cursor")};o.css("cursor",n.cursor)},stop:function(e,i,s){var o=s.options;if(o._cursor){t("body").css("cursor",o._cursor)}}});t.ui.plugin.add("draggable","opacity",{start:function(e,i,s){var o=t(i.helper),n=s.options;if(o.css("opacity")){n._opacity=o.css("opacity")};o.css("opacity",n.opacity)},stop:function(e,i,s){var o=s.options;if(o._opacity){t(i.helper).css("opacity",o._opacity)}}});t.ui.plugin.add("draggable","scroll",{start:function(t,e,i){if(!i.scrollParentNotHidden){i.scrollParentNotHidden=i.helper.scrollParent(!1)};if(i.scrollParentNotHidden[0]!==i.document[0]&&i.scrollParentNotHidden[0].tagName!=="HTML"){i.overflowOffset=i.scrollParentNotHidden.offset()}},drag:function(e,i,s){var o=s.options,a=!1,r=s.scrollParentNotHidden[0],n=s.document[0];if(r!==n&&r.tagName!=="HTML"){if(!o.axis||o.axis!=="x"){if((s.overflowOffset.top+r.offsetHeight)-e.pageY<o.scrollSensitivity){r.scrollTop=a=r.scrollTop+o.scrollSpeed}
     74 else if(e.pageY-s.overflowOffset.top<o.scrollSensitivity){r.scrollTop=a=r.scrollTop-o.scrollSpeed}};if(!o.axis||o.axis!=="y"){if((s.overflowOffset.left+r.offsetWidth)-e.pageX<o.scrollSensitivity){r.scrollLeft=a=r.scrollLeft+o.scrollSpeed}
     75 else if(e.pageX-s.overflowOffset.left<o.scrollSensitivity){r.scrollLeft=a=r.scrollLeft-o.scrollSpeed}}}
     76 else{if(!o.axis||o.axis!=="x"){if(e.pageY-t(n).scrollTop()<o.scrollSensitivity){a=t(n).scrollTop(t(n).scrollTop()-o.scrollSpeed)}
     77 else if(t(window).height()-(e.pageY-t(n).scrollTop())<o.scrollSensitivity){a=t(n).scrollTop(t(n).scrollTop()+o.scrollSpeed)}};if(!o.axis||o.axis!=="y"){if(e.pageX-t(n).scrollLeft()<o.scrollSensitivity){a=t(n).scrollLeft(t(n).scrollLeft()-o.scrollSpeed)}
     78 else if(t(window).width()-(e.pageX-t(n).scrollLeft())<o.scrollSensitivity){a=t(n).scrollLeft(t(n).scrollLeft()+o.scrollSpeed)}}};if(a!==!1&&t.ui.ddmanager&&!o.dropBehaviour){t.ui.ddmanager.prepareOffsets(s,e)}}});t.ui.plugin.add("draggable","snap",{start:function(e,i,s){var o=s.options;s.snapElements=[];t(o.snap.constructor!==String?(o.snap.items||":data(ui-draggable)"):o.snap).each(function(){var e=t(this),i=e.offset();if(this!==s.element[0]){s.snapElements.push({item:this,width:e.outerWidth(),height:e.outerHeight(),top:i.top,left:i.left})}})},drag:function(e,i,s){var r,a,h,l,c,p,f,u,o,g,v=s.options,n=v.snapTolerance,d=i.offset.left,b=d+s.helperProportions.width,m=i.offset.top,P=m+s.helperProportions.height;for(o=s.snapElements.length-1;o>=0;o--){c=s.snapElements[o].left-s.margins.left;p=c+s.snapElements[o].width;f=s.snapElements[o].top-s.margins.top;u=f+s.snapElements[o].height;if(b<c-n||d>p+n||P<f-n||m>u+n||!t.contains(s.snapElements[o].item.ownerDocument,s.snapElements[o].item)){if(s.snapElements[o].snapping){(s.options.snap.release&&s.options.snap.release.call(s.element,e,t.extend(s._uiHash(),{snapItem:s.snapElements[o].item})))};s.snapElements[o].snapping=!1;continue};if(v.snapMode!=="inner"){r=Math.abs(f-P)<=n;a=Math.abs(u-m)<=n;h=Math.abs(c-b)<=n;l=Math.abs(p-d)<=n;if(r){i.position.top=s._convertPositionTo("relative",{top:f-s.helperProportions.height,left:0}).top};if(a){i.position.top=s._convertPositionTo("relative",{top:u,left:0}).top};if(h){i.position.left=s._convertPositionTo("relative",{top:0,left:c-s.helperProportions.width}).left};if(l){i.position.left=s._convertPositionTo("relative",{top:0,left:p}).left}};g=(r||a||h||l);if(v.snapMode!=="outer"){r=Math.abs(f-m)<=n;a=Math.abs(u-P)<=n;h=Math.abs(c-d)<=n;l=Math.abs(p-b)<=n;if(r){i.position.top=s._convertPositionTo("relative",{top:f,left:0}).top};if(a){i.position.top=s._convertPositionTo("relative",{top:u-s.helperProportions.height,left:0}).top};if(h){i.position.left=s._convertPositionTo("relative",{top:0,left:c}).left};if(l){i.position.left=s._convertPositionTo("relative",{top:0,left:p-s.helperProportions.width}).left}};if(!s.snapElements[o].snapping&&(r||a||h||l||g)){(s.options.snap.snap&&s.options.snap.snap.call(s.element,e,t.extend(s._uiHash(),{snapItem:s.snapElements[o].item})))};s.snapElements[o].snapping=(r||a||h||l||g)}}});t.ui.plugin.add("draggable","stack",{start:function(e,i,s){var n,r=s.options,o=t.makeArray(t(r.stack)).sort(function(e,i){return(parseInt(t(e).css("zIndex"),10)||0)-(parseInt(t(i).css("zIndex"),10)||0)});if(!o.length){return};n=parseInt(t(o[0]).css("zIndex"),10)||0;t(o).each(function(e){t(this).css("zIndex",n+e)});this.css("zIndex",(n+o.length))}});t.ui.plugin.add("draggable","zIndex",{start:function(e,i,s){var o=t(i.helper),n=s.options;if(o.css("zIndex")){n._zIndex=o.css("zIndex")};o.css("zIndex",n.zIndex)},stop:function(e,i,s){var o=s.options;if(o._zIndex){t(i.helper).css("zIndex",o._zIndex)}}});var a=t.ui.draggable;
     79 /*!
     80  * jQuery UI Droppable 1.12.1
     81  * http://jqueryui.com
     82  *
     83  * Copyright jQuery Foundation and other contributors
     84  * Released under the MIT license.
     85  * http://jquery.org/license
     86  */
     87 ;t.widget("ui.droppable",{version:"1.12.1",widgetEventPrefix:"drop",options:{accept:"*",addClasses:!0,greedy:!1,scope:"default",tolerance:"intersect",activate:null,deactivate:null,drop:null,out:null,over:null},_create:function(){var e,i=this.options,s=i.accept;this.isover=!1;this.isout=!0;this.accept=t.isFunction(s)?s:function(t){return t.is(s)};this.proportions=function(){if(arguments.length){e=arguments[0]}
     88 else{return e?e:e={width:this.element[0].offsetWidth,height:this.element[0].offsetHeight}}};this._addToManager(i.scope);i.addClasses&&this._addClass("ui-droppable")},_addToManager:function(e){t.ui.ddmanager.droppables[e]=t.ui.ddmanager.droppables[e]||[];t.ui.ddmanager.droppables[e].push(this)},_splice:function(t){var e=0;for(;e<t.length;e++){if(t[e]===this){t.splice(e,1)}}},_destroy:function(){var e=t.ui.ddmanager.droppables[this.options.scope];this._splice(e)},_setOption:function(e,i){if(e==="accept"){this.accept=t.isFunction(i)?i:function(t){return t.is(i)}}
     89 else if(e==="scope"){var s=t.ui.ddmanager.droppables[this.options.scope];this._splice(s);this._addToManager(i)};this._super(e,i)},_activate:function(e){var i=t.ui.ddmanager.current;this._addActiveClass();if(i){this._trigger("activate",e,this.ui(i))}},_deactivate:function(e){var i=t.ui.ddmanager.current;this._removeActiveClass();if(i){this._trigger("deactivate",e,this.ui(i))}},_over:function(e){var i=t.ui.ddmanager.current;if(!i||(i.currentItem||i.element)[0]===this.element[0]){return};if(this.accept.call(this.element[0],(i.currentItem||i.element))){this._addHoverClass();this._trigger("over",e,this.ui(i))}},_out:function(e){var i=t.ui.ddmanager.current;if(!i||(i.currentItem||i.element)[0]===this.element[0]){return};if(this.accept.call(this.element[0],(i.currentItem||i.element))){this._removeHoverClass();this._trigger("out",e,this.ui(i))}},_drop:function(e,s){var o=s||t.ui.ddmanager.current,n=!1;if(!o||(o.currentItem||o.element)[0]===this.element[0]){return!1};this.element.find(":data(ui-droppable)").not(".ui-draggable-dragging").each(function(){var s=t(this).droppable("instance");if(s.options.greedy&&!s.options.disabled&&s.options.scope===o.options.scope&&s.accept.call(s.element[0],(o.currentItem||o.element))&&i(o,t.extend(s,{offset:s.element.offset()}),s.options.tolerance,e)){n=!0;return!1}});if(n){return!1};if(this.accept.call(this.element[0],(o.currentItem||o.element))){this._removeActiveClass();this._removeHoverClass();this._trigger("drop",e,this.ui(o));return this.element};return!1},ui:function(t){return{draggable:(t.currentItem||t.element),helper:t.helper,position:t.position,offset:t.positionAbs}},_addHoverClass:function(){this._addClass("ui-droppable-hover")},_removeHoverClass:function(){this._removeClass("ui-droppable-hover")},_addActiveClass:function(){this._addClass("ui-droppable-active")},_removeActiveClass:function(){this._removeClass("ui-droppable-active")}});var i=t.ui.intersect=(function(){function t(t,e,i){return(t>=e)&&(t<(e+i))};return function(e,i,s,h){if(!i.offset){return!1};var r=(e.positionAbs||e.position.absolute).left+e.margins.left,a=(e.positionAbs||e.position.absolute).top+e.margins.top,l=r+e.helperProportions.width,c=a+e.helperProportions.height,o=i.offset.left,n=i.offset.top,f=o+i.proportions().width,p=n+i.proportions().height;switch(s){case"fit":return(o<=r&&l<=f&&n<=a&&c<=p);case"intersect":return(o<r+(e.helperProportions.width/2)&&l-(e.helperProportions.width/2)<f&&n<a+(e.helperProportions.height/2)&&c-(e.helperProportions.height/2)<p);case"pointer":return t(h.pageY,n,i.proportions().height)&&t(h.pageX,o,i.proportions().width);case"touch":return((a>=n&&a<=p)||(c>=n&&c<=p)||(a<n&&c>p))&&((r>=o&&r<=f)||(l>=o&&l<=f)||(r<o&&l>f));default:return!1}}})();t.ui.ddmanager={current:null,droppables:{"default":[]},prepareOffsets:function(e,i){var s,n,o=t.ui.ddmanager.droppables[e.options.scope]||[],a=i?i.type:null,r=(e.currentItem||e.element).find(":data(ui-droppable)").addBack();droppablesLoop:for(s=0;s<o.length;s++){if(o[s].options.disabled||(e&&!o[s].accept.call(o[s].element[0],(e.currentItem||e.element)))){continue};for(n=0;n<r.length;n++){if(r[n]===o[s].element[0]){o[s].proportions().height=0;continue;droppablesLoop}};o[s].visible=o[s].element.css("display")!=="none";if(!o[s].visible){continue};if(a==="mousedown"){o[s]._activate.call(o[s],i)};o[s].offset=o[s].element.offset();o[s].proportions({width:o[s].element[0].offsetWidth,height:o[s].element[0].offsetHeight})}},drop:function(e,s){var o=!1;t.each((t.ui.ddmanager.droppables[e.options.scope]||[]).slice(),function(){if(!this.options){return};if(!this.options.disabled&&this.visible&&i(e,this,this.options.tolerance,s)){o=this._drop.call(this,s)||o};if(!this.options.disabled&&this.visible&&this.accept.call(this.element[0],(e.currentItem||e.element))){this.isout=!0;this.isover=!1;this._deactivate.call(this,s)}});return o},dragStart:function(e,i){e.element.parentsUntil("body").on("scroll.droppable",function(){if(!e.options.refreshPositions){t.ui.ddmanager.prepareOffsets(e,i)}})},drag:function(e,s){if(e.options.refreshPositions){t.ui.ddmanager.prepareOffsets(e,s)};t.each(t.ui.ddmanager.droppables[e.options.scope]||[],function(){if(this.options.disabled||this.greedyChild||!this.visible){return};var o,a,r,h=i(e,this,this.options.tolerance,s),n=!h&&this.isover?"isout":(h&&!this.isover?"isover":null);if(!n){return};if(this.options.greedy){a=this.options.scope;r=this.element.parents(":data(ui-droppable)").filter(function(){return t(this).droppable("instance").options.scope===a});if(r.length){o=t(r[0]).droppable("instance");o.greedyChild=(n==="isover")}};if(o&&n==="isover"){o.isover=!1;o.isout=!0;o._out.call(o,s)};this[n]=!0;this[n==="isout"?"isover":"isout"]=!1;this[n==="isover"?"_over":"_out"].call(this,s);if(o&&n==="isout"){o.isout=!1;o.isover=!0;o._over.call(o,s)}})},dragStop:function(e,i){e.element.parentsUntil("body").off("scroll.droppable");if(!e.options.refreshPositions){t.ui.ddmanager.prepareOffsets(e,i)}}};if(t.uiBackCompat!==!1){t.widget("ui.droppable",t.ui.droppable,{options:{hoverClass:!1,activeClass:!1},_addActiveClass:function(){this._super();if(this.options.activeClass){this.element.addClass(this.options.activeClass)}},_removeActiveClass:function(){this._super();if(this.options.activeClass){this.element.removeClass(this.options.activeClass)}},_addHoverClass:function(){this._super();if(this.options.hoverClass){this.element.addClass(this.options.hoverClass)}},_removeHoverClass:function(){this._super();if(this.options.hoverClass){this.element.removeClass(this.options.hoverClass)}}})};var r=t.ui.droppable;
     90 /*!
     91  * jQuery UI Sortable 1.12.1
     92  * http://jqueryui.com
     93  *
     94  * Copyright jQuery Foundation and other contributors
     95  * Released under the MIT license.
     96  * http://jquery.org/license
     97  */
     98 ;var n=t.widget("ui.sortable",t.ui.mouse,{version:"1.12.1",widgetEventPrefix:"sort",ready:!1,options:{appendTo:"parent",axis:!1,connectWith:!1,containment:!1,cursor:"auto",cursorAt:!1,dropOnEmpty:!0,forcePlaceholderSize:!1,forceHelperSize:!1,grid:!1,handle:!1,helper:"original",items:"> *",opacity:!1,placeholder:!1,revert:!1,scroll:!0,scrollSensitivity:20,scrollSpeed:20,scope:"default",tolerance:"intersect",zIndex:1000,activate:null,beforeStop:null,change:null,deactivate:null,out:null,over:null,receive:null,remove:null,sort:null,start:null,stop:null,update:null},_isOverAxis:function(t,e,i){return(t>=e)&&(t<(e+i))},_isFloating:function(t){return(/left|right/).test(t.css("float"))||(/inline|table-cell/).test(t.css("display"))},_create:function(){this.containerCache={};this._addClass("ui-sortable");this.refresh();this.offset=this.element.offset();this._mouseInit();this._setHandleClassName();this.ready=!0},_setOption:function(t,e){this._super(t,e);if(t==="handle"){this._setHandleClassName()}},_setHandleClassName:function(){var e=this;this._removeClass(this.element.find(".ui-sortable-handle"),"ui-sortable-handle");t.each(this.items,function(){e._addClass(this.instance.options.handle?this.item.find(this.instance.options.handle):this.item,"ui-sortable-handle")})},_destroy:function(){this._mouseDestroy();for(var t=this.items.length-1;t>=0;t--){this.items[t].item.removeData(this.widgetName+"-item")};return this},_mouseCapture:function(e,i){var s=null,n=!1,o=this;if(this.reverting){return!1};if(this.options.disabled||this.options.type==="static"){return!1};this._refreshItems(e);t(e.target).parents().each(function(){if(t.data(this,o.widgetName+"-item")===o){s=t(this);return!1}});if(t.data(e.target,o.widgetName+"-item")===o){s=t(e.target)};if(!s){return!1};if(this.options.handle&&!i){t(this.options.handle,s).find("*").addBack().each(function(){if(this===e.target){n=!0}});if(!n){return!1}};this.currentItem=s;this._removeCurrentsFromItems();return!0},_mouseStart:function(e,i,s){var n,r,o=this.options;this.currentContainer=this;this.refreshPositions();this.helper=this._createHelper(e);this._cacheHelperProportions();this._cacheMargins();this.scrollParent=this.helper.scrollParent();this.offset=this.currentItem.offset();this.offset={top:this.offset.top-this.margins.top,left:this.offset.left-this.margins.left};t.extend(this.offset,{click:{left:e.pageX-this.offset.left,top:e.pageY-this.offset.top},parent:this._getParentOffset(),relative:this._getRelativeOffset()});this.helper.css("position","absolute");this.cssPosition=this.helper.css("position");this.originalPosition=this._generatePosition(e);this.originalPageX=e.pageX;this.originalPageY=e.pageY;(o.cursorAt&&this._adjustOffsetFromHelper(o.cursorAt));this.domPosition={prev:this.currentItem.prev()[0],parent:this.currentItem.parent()[0]};if(this.helper[0]!==this.currentItem[0]){this.currentItem.hide()};this._createPlaceholder();if(o.containment){this._setContainment()};if(o.cursor&&o.cursor!=="auto"){r=this.document.find("body");this.storedCursor=r.css("cursor");r.css("cursor",o.cursor);this.storedStylesheet=t("<style>*{ cursor: "+o.cursor+" !important; }</style>").appendTo(r)};if(o.opacity){if(this.helper.css("opacity")){this._storedOpacity=this.helper.css("opacity")};this.helper.css("opacity",o.opacity)};if(o.zIndex){if(this.helper.css("zIndex")){this._storedZIndex=this.helper.css("zIndex")};this.helper.css("zIndex",o.zIndex)};if(this.scrollParent[0]!==this.document[0]&&this.scrollParent[0].tagName!=="HTML"){this.overflowOffset=this.scrollParent.offset()};this._trigger("start",e,this._uiHash());if(!this._preserveHelperProportions){this._cacheHelperProportions()};if(!s){for(n=this.containers.length-1;n>=0;n--){this.containers[n]._trigger("activate",e,this._uiHash(this))}};if(t.ui.ddmanager){t.ui.ddmanager.current=this};if(t.ui.ddmanager&&!o.dropBehaviour){t.ui.ddmanager.prepareOffsets(this,e)};this.dragging=!0;this._addClass(this.helper,"ui-sortable-helper");this._mouseDrag(e);return!0},_mouseDrag:function(e){var r,o,n,a,i=this.options,s=!1;this.position=this._generatePosition(e);this.positionAbs=this._convertPositionTo("absolute");if(!this.lastPositionAbs){this.lastPositionAbs=this.positionAbs};if(this.options.scroll){if(this.scrollParent[0]!==this.document[0]&&this.scrollParent[0].tagName!=="HTML"){if((this.overflowOffset.top+this.scrollParent[0].offsetHeight)-e.pageY<i.scrollSensitivity){this.scrollParent[0].scrollTop=s=this.scrollParent[0].scrollTop+i.scrollSpeed}
     99 else if(e.pageY-this.overflowOffset.top<i.scrollSensitivity){this.scrollParent[0].scrollTop=s=this.scrollParent[0].scrollTop-i.scrollSpeed};if((this.overflowOffset.left+this.scrollParent[0].offsetWidth)-e.pageX<i.scrollSensitivity){this.scrollParent[0].scrollLeft=s=this.scrollParent[0].scrollLeft+i.scrollSpeed}
    100 else if(e.pageX-this.overflowOffset.left<i.scrollSensitivity){this.scrollParent[0].scrollLeft=s=this.scrollParent[0].scrollLeft-i.scrollSpeed}}
    101 else{if(e.pageY-this.document.scrollTop()<i.scrollSensitivity){s=this.document.scrollTop(this.document.scrollTop()-i.scrollSpeed)}
    102 else if(this.window.height()-(e.pageY-this.document.scrollTop())<i.scrollSensitivity){s=this.document.scrollTop(this.document.scrollTop()+i.scrollSpeed)};if(e.pageX-this.document.scrollLeft()<i.scrollSensitivity){s=this.document.scrollLeft(this.document.scrollLeft()-i.scrollSpeed)}
    103 else if(this.window.width()-(e.pageX-this.document.scrollLeft())<i.scrollSensitivity){s=this.document.scrollLeft(this.document.scrollLeft()+i.scrollSpeed)}};if(s!==!1&&t.ui.ddmanager&&!i.dropBehaviour){t.ui.ddmanager.prepareOffsets(this,e)}};this.positionAbs=this._convertPositionTo("absolute");if(!this.options.axis||this.options.axis!=="y"){this.helper[0].style.left=this.position.left+"px"};if(!this.options.axis||this.options.axis!=="x"){this.helper[0].style.top=this.position.top+"px"};for(r=this.items.length-1;r>=0;r--){o=this.items[r];n=o.item[0];a=this._intersectsWithPointer(o);if(!a){continue};if(o.instance!==this.currentContainer){continue};if(n!==this.currentItem[0]&&this.placeholder[a===1?"next":"prev"]()[0]!==n&&!t.contains(this.placeholder[0],n)&&(this.options.type==="semi-dynamic"?!t.contains(this.element[0],n):!0)){this.direction=a===1?"down":"up";if(this.options.tolerance==="pointer"||this._intersectsWithSides(o)){this._rearrange(e,o)}
    104 else{break};this._trigger("change",e,this._uiHash());break}};this._contactContainers(e);if(t.ui.ddmanager){t.ui.ddmanager.drag(this,e)};this._trigger("sort",e,this._uiHash());this.lastPositionAbs=this.positionAbs;return!1},_mouseStop:function(e,i){if(!e){return};if(t.ui.ddmanager&&!this.options.dropBehaviour){t.ui.ddmanager.drop(this,e)};if(this.options.revert){var r=this,n=this.placeholder.offset(),s=this.options.axis,o={};if(!s||s==="x"){o.left=n.left-this.offset.parent.left-this.margins.left+(this.offsetParent[0]===this.document[0].body?0:this.offsetParent[0].scrollLeft)};if(!s||s==="y"){o.top=n.top-this.offset.parent.top-this.margins.top+(this.offsetParent[0]===this.document[0].body?0:this.offsetParent[0].scrollTop)};this.reverting=!0;t(this.helper).animate(o,parseInt(this.options.revert,10)||500,function(){r._clear(e)})}
    105 else{this._clear(e,i)};return!1},cancel:function(){if(this.dragging){this._mouseUp(new t.Event("mouseup",{target:null}));if(this.options.helper==="original"){this.currentItem.css(this._storedCSS);this._removeClass(this.currentItem,"ui-sortable-helper")}
    106 else{this.currentItem.show()};for(var e=this.containers.length-1;e>=0;e--){this.containers[e]._trigger("deactivate",null,this._uiHash(this));if(this.containers[e].containerCache.over){this.containers[e]._trigger("out",null,this._uiHash(this));this.containers[e].containerCache.over=0}}};if(this.placeholder){if(this.placeholder[0].parentNode){this.placeholder[0].parentNode.removeChild(this.placeholder[0])};if(this.options.helper!=="original"&&this.helper&&this.helper[0].parentNode){this.helper.remove()};t.extend(this,{helper:null,dragging:!1,reverting:!1,_noFinalSort:null});if(this.domPosition.prev){t(this.domPosition.prev).after(this.currentItem)}
    107 else{t(this.domPosition.parent).prepend(this.currentItem)}};return this},serialize:function(e){var s=this._getItemsAsjQuery(e&&e.connected),i=[];e=e||{};t(s).each(function(){var s=(t(e.item||this).attr(e.attribute||"id")||"").match(e.expression||(/(.+)[\-=_](.+)/));if(s){i.push((e.key||s[1]+"[]")+"="+(e.key&&e.expression?s[1]:s[2]))}});if(!i.length&&e.key){i.push(e.key+"=")};return i.join("&")},toArray:function(e){var s=this._getItemsAsjQuery(e&&e.connected),i=[];e=e||{};s.each(function(){i.push(t(e.item||this).attr(e.attribute||"id")||"")});return i},_intersectsWith:function(t){var e=this.positionAbs.left,l=e+this.helperProportions.width,i=this.positionAbs.top,c=i+this.helperProportions.height,s=t.left,n=s+t.width,o=t.top,r=o+t.height,a=this.offset.click.top,h=this.offset.click.left,f=(this.options.axis==="x")||((i+a)>o&&(i+a)<r),p=(this.options.axis==="y")||((e+h)>s&&(e+h)<n),u=f&&p;if(this.options.tolerance==="pointer"||this.options.forcePointerForContainers||(this.options.tolerance!=="pointer"&&this.helperProportions[this.floating?"width":"height"]>t[this.floating?"width":"height"])){return u}
    108 else{return(s<e+(this.helperProportions.width/2)&&l-(this.helperProportions.width/2)<n&&o<i+(this.helperProportions.height/2)&&c-(this.helperProportions.height/2)<r)}},_intersectsWithPointer:function(t){var e,i,s=(this.options.axis==="x")||this._isOverAxis(this.positionAbs.top+this.offset.click.top,t.top,t.height),o=(this.options.axis==="y")||this._isOverAxis(this.positionAbs.left+this.offset.click.left,t.left,t.width),n=s&&o;if(!n){return!1};e=this._getDragVerticalDirection();i=this._getDragHorizontalDirection();return this.floating?((i==="right"||e==="down")?2:1):(e&&(e==="down"?2:1))},_intersectsWithSides:function(t){var s=this._isOverAxis(this.positionAbs.top+this.offset.click.top,t.top+(t.height/2),t.height),o=this._isOverAxis(this.positionAbs.left+this.offset.click.left,t.left+(t.width/2),t.width),e=this._getDragVerticalDirection(),i=this._getDragHorizontalDirection();if(this.floating&&i){return((i==="right"&&o)||(i==="left"&&!o))}
    109 else{return e&&((e==="down"&&s)||(e==="up"&&!s))}},_getDragVerticalDirection:function(){var t=this.positionAbs.top-this.lastPositionAbs.top;return t!==0&&(t>0?"down":"up")},_getDragHorizontalDirection:function(){var t=this.positionAbs.left-this.lastPositionAbs.left;return t!==0&&(t>0?"right":"left")},refresh:function(t){this._refreshItems(t);this._setHandleClassName();this.refreshPositions();return this},_connectWith:function(){var t=this.options;return t.connectWith.constructor===String?[t.connectWith]:t.connectWith},_getItemsAsjQuery:function(e){var s,o,r,i,h=[],n=[],a=this._connectWith();if(a&&e){for(s=a.length-1;s>=0;s--){r=t(a[s],this.document[0]);for(o=r.length-1;o>=0;o--){i=t.data(r[o],this.widgetFullName);if(i&&i!==this&&!i.options.disabled){n.push([t.isFunction(i.options.items)?i.options.items.call(i.element):t(i.options.items,i.element).not(".ui-sortable-helper").not(".ui-sortable-placeholder"),i])}}}};n.push([t.isFunction(this.options.items)?this.options.items.call(this.element,null,{options:this.options,item:this.currentItem}):t(this.options.items,this.element).not(".ui-sortable-helper").not(".ui-sortable-placeholder"),this]);function l(){h.push(this)};for(s=n.length-1;s>=0;s--){n[s][0].each(l)};return t(h)},_removeCurrentsFromItems:function(){var e=this.currentItem.find(":data("+this.widgetName+"-item)");this.items=t.grep(this.items,function(t){for(var i=0;i<e.length;i++){if(e[i]===t.item[0]){return!1}};return!0})},_refreshItems:function(e){this.items=[];this.containers=[this];var s,o,r,i,a,h,l,f,p=this.items,n=[[t.isFunction(this.options.items)?this.options.items.call(this.element[0],e,{item:this.currentItem}):t(this.options.items,this.element),this]],c=this._connectWith();if(c&&this.ready){for(s=c.length-1;s>=0;s--){r=t(c[s],this.document[0]);for(o=r.length-1;o>=0;o--){i=t.data(r[o],this.widgetFullName);if(i&&i!==this&&!i.options.disabled){n.push([t.isFunction(i.options.items)?i.options.items.call(i.element[0],e,{item:this.currentItem}):t(i.options.items,i.element),i]);this.containers.push(i)}}}};for(s=n.length-1;s>=0;s--){a=n[s][1];h=n[s][0];for(o=0,f=h.length;o<f;o++){l=t(h[o]);l.data(this.widgetName+"-item",a);p.push({item:l,instance:a,width:0,height:0,left:0,top:0})}}},refreshPositions:function(e){this.floating=this.items.length?this.options.axis==="x"||this._isFloating(this.items[0].item):!1;if(this.offsetParent&&this.helper){this.offset.parent=this._getParentOffset()};var i,s,n,o;for(i=this.items.length-1;i>=0;i--){s=this.items[i];if(s.instance!==this.currentContainer&&this.currentContainer&&s.item[0]!==this.currentItem[0]){continue};n=this.options.toleranceElement?t(this.options.toleranceElement,s.item):s.item;if(!e){s.width=n.outerWidth();s.height=n.outerHeight()};o=n.offset();s.left=o.left;s.top=o.top};if(this.options.custom&&this.options.custom.refreshContainers){this.options.custom.refreshContainers.call(this)}
    110 else{for(i=this.containers.length-1;i>=0;i--){o=this.containers[i].element.offset();this.containers[i].containerCache.left=o.left;this.containers[i].containerCache.top=o.top;this.containers[i].containerCache.width=this.containers[i].element.outerWidth();this.containers[i].containerCache.height=this.containers[i].element.outerHeight()}};return this},_createPlaceholder:function(e){e=e||this;var s,i=e.options;if(!i.placeholder||i.placeholder.constructor===String){s=i.placeholder;i.placeholder={element:function(){var o=e.currentItem[0].nodeName.toLowerCase(),i=t("<"+o+">",e.document[0]);e._addClass(i,"ui-sortable-placeholder",s||e.currentItem[0].className)._removeClass(i,"ui-sortable-helper");if(o==="tbody"){e._createTrPlaceholder(e.currentItem.find("tr").eq(0),t("<tr>",e.document[0]).appendTo(i))}
    111 else if(o==="tr"){e._createTrPlaceholder(e.currentItem,i)}
    112 else if(o==="img"){i.attr("src",e.currentItem.attr("src"))};if(!s){i.css("visibility","hidden")};return i},update:function(t,o){if(s&&!i.forcePlaceholderSize){return};if(!o.height()){o.height(e.currentItem.innerHeight()-parseInt(e.currentItem.css("paddingTop")||0,10)-parseInt(e.currentItem.css("paddingBottom")||0,10))};if(!o.width()){o.width(e.currentItem.innerWidth()-parseInt(e.currentItem.css("paddingLeft")||0,10)-parseInt(e.currentItem.css("paddingRight")||0,10))}}}};e.placeholder=t(i.placeholder.element.call(e.element,e.currentItem));e.currentItem.after(e.placeholder);i.placeholder.update(e,e.placeholder)},_createTrPlaceholder:function(e,i){var s=this;e.children().each(function(){t("<td>&#160;</td>",s.document[0]).attr("colspan",t(this).attr("colspan")||1).appendTo(i)})},_contactContainers:function(e){var s,o,c,n,p,u,a,f,h,l,r=null,i=null;for(s=this.containers.length-1;s>=0;s--){if(t.contains(this.currentItem[0],this.containers[s].element[0])){continue};if(this._intersectsWith(this.containers[s].containerCache)){if(r&&t.contains(this.containers[s].element[0],r.element[0])){continue};r=this.containers[s];i=s}
    113 else{if(this.containers[s].containerCache.over){this.containers[s]._trigger("out",e,this._uiHash(this));this.containers[s].containerCache.over=0}}};if(!r){return};if(this.containers.length===1){if(!this.containers[i].containerCache.over){this.containers[i]._trigger("over",e,this._uiHash(this));this.containers[i].containerCache.over=1}}
    114 else{c=10000;n=null;h=r.floating||this._isFloating(this.currentItem);p=h?"left":"top";u=h?"width":"height";l=h?"pageX":"pageY";for(o=this.items.length-1;o>=0;o--){if(!t.contains(this.containers[i].element[0],this.items[o].item[0])){continue};if(this.items[o].item[0]===this.currentItem[0]){continue};a=this.items[o].item.offset()[p];f=!1;if(e[l]-a>this.items[o][u]/2){f=!0};if(Math.abs(e[l]-a)<c){c=Math.abs(e[l]-a);n=this.items[o];this.direction=f?"up":"down"}};if(!n&&!this.options.dropOnEmpty){return};if(this.currentContainer===this.containers[i]){if(!this.currentContainer.containerCache.over){this.containers[i]._trigger("over",e,this._uiHash());this.currentContainer.containerCache.over=1};return};n?this._rearrange(e,n,null,!0):this._rearrange(e,null,this.containers[i].element,!0);this._trigger("change",e,this._uiHash());this.containers[i]._trigger("change",e,this._uiHash(this));this.currentContainer=this.containers[i];this.options.placeholder.update(this.currentContainer,this.placeholder);this.containers[i]._trigger("over",e,this._uiHash(this));this.containers[i].containerCache.over=1}},_createHelper:function(e){var s=this.options,i=t.isFunction(s.helper)?t(s.helper.apply(this.element[0],[e,this.currentItem])):(s.helper==="clone"?this.currentItem.clone():this.currentItem);if(!i.parents("body").length){t(s.appendTo!=="parent"?s.appendTo:this.currentItem[0].parentNode)[0].appendChild(i[0])};if(i[0]===this.currentItem[0]){this._storedCSS={width:this.currentItem[0].style.width,height:this.currentItem[0].style.height,position:this.currentItem.css("position"),top:this.currentItem.css("top"),left:this.currentItem.css("left")}};if(!i[0].style.width||s.forceHelperSize){i.width(this.currentItem.width())};if(!i[0].style.height||s.forceHelperSize){i.height(this.currentItem.height())};return i},_adjustOffsetFromHelper:function(e){if(typeof e==="string"){e=e.split(" ")};if(t.isArray(e)){e={left:+e[0],top:+e[1]||0}};if("left" in e){this.offset.click.left=e.left+this.margins.left};if("right" in e){this.offset.click.left=this.helperProportions.width-e.right+this.margins.left};if("top" in e){this.offset.click.top=e.top+this.margins.top};if("bottom" in e){this.offset.click.top=this.helperProportions.height-e.bottom+this.margins.top}},_getParentOffset:function(){this.offsetParent=this.helper.offsetParent();var e=this.offsetParent.offset();if(this.cssPosition==="absolute"&&this.scrollParent[0]!==this.document[0]&&t.contains(this.scrollParent[0],this.offsetParent[0])){e.left+=this.scrollParent.scrollLeft();e.top+=this.scrollParent.scrollTop()};if(this.offsetParent[0]===this.document[0].body||(this.offsetParent[0].tagName&&this.offsetParent[0].tagName.toLowerCase()==="html"&&t.ui.ie)){e={top:0,left:0}};return{top:e.top+(parseInt(this.offsetParent.css("borderTopWidth"),10)||0),left:e.left+(parseInt(this.offsetParent.css("borderLeftWidth"),10)||0)}},_getRelativeOffset:function(){if(this.cssPosition==="relative"){var t=this.currentItem.position();return{top:t.top-(parseInt(this.helper.css("top"),10)||0)+this.scrollParent.scrollTop(),left:t.left-(parseInt(this.helper.css("left"),10)||0)+this.scrollParent.scrollLeft()}}
    115 else{return{top:0,left:0}}},_cacheMargins:function(){this.margins={left:(parseInt(this.currentItem.css("marginLeft"),10)||0),top:(parseInt(this.currentItem.css("marginTop"),10)||0)}},_cacheHelperProportions:function(){this.helperProportions={width:this.helper.outerWidth(),height:this.helper.outerHeight()}},_setContainment:function(){var e,s,o,i=this.options;if(i.containment==="parent"){i.containment=this.helper[0].parentNode};if(i.containment==="document"||i.containment==="window"){this.containment=[0-this.offset.relative.left-this.offset.parent.left,0-this.offset.relative.top-this.offset.parent.top,i.containment==="document"?this.document.width():this.window.width()-this.helperProportions.width-this.margins.left,(i.containment==="document"?(this.document.height()||document.body.parentNode.scrollHeight):this.window.height()||this.document[0].body.parentNode.scrollHeight)-this.helperProportions.height-this.margins.top]};if(!(/^(document|window|parent)$/).test(i.containment)){e=t(i.containment)[0];s=t(i.containment).offset();o=(t(e).css("overflow")!=="hidden");this.containment=[s.left+(parseInt(t(e).css("borderLeftWidth"),10)||0)+(parseInt(t(e).css("paddingLeft"),10)||0)-this.margins.left,s.top+(parseInt(t(e).css("borderTopWidth"),10)||0)+(parseInt(t(e).css("paddingTop"),10)||0)-this.margins.top,s.left+(o?Math.max(e.scrollWidth,e.offsetWidth):e.offsetWidth)-(parseInt(t(e).css("borderLeftWidth"),10)||0)-(parseInt(t(e).css("paddingRight"),10)||0)-this.helperProportions.width-this.margins.left,s.top+(o?Math.max(e.scrollHeight,e.offsetHeight):e.offsetHeight)-(parseInt(t(e).css("borderTopWidth"),10)||0)-(parseInt(t(e).css("paddingBottom"),10)||0)-this.helperProportions.height-this.margins.top]}},_convertPositionTo:function(e,i){if(!i){i=this.position};var s=e==="absolute"?1:-1,o=this.cssPosition==="absolute"&&!(this.scrollParent[0]!==this.document[0]&&t.contains(this.scrollParent[0],this.offsetParent[0]))?this.offsetParent:this.scrollParent,n=(/(html|body)/i).test(o[0].tagName);return{top:(i.top+this.offset.relative.top*s+this.offset.parent.top*s-((this.cssPosition==="fixed"?-this.scrollParent.scrollTop():(n?0:o.scrollTop()))*s)),left:(i.left+this.offset.relative.left*s+this.offset.parent.left*s-((this.cssPosition==="fixed"?-this.scrollParent.scrollLeft():n?0:o.scrollLeft())*s))}},_generatePosition:function(e){var s,o,i=this.options,n=e.pageX,r=e.pageY,a=this.cssPosition==="absolute"&&!(this.scrollParent[0]!==this.document[0]&&t.contains(this.scrollParent[0],this.offsetParent[0]))?this.offsetParent:this.scrollParent,h=(/(html|body)/i).test(a[0].tagName);if(this.cssPosition==="relative"&&!(this.scrollParent[0]!==this.document[0]&&this.scrollParent[0]!==this.offsetParent[0])){this.offset.relative=this._getRelativeOffset()};if(this.originalPosition){if(this.containment){if(e.pageX-this.offset.click.left<this.containment[0]){n=this.containment[0]+this.offset.click.left};if(e.pageY-this.offset.click.top<this.containment[1]){r=this.containment[1]+this.offset.click.top};if(e.pageX-this.offset.click.left>this.containment[2]){n=this.containment[2]+this.offset.click.left};if(e.pageY-this.offset.click.top>this.containment[3]){r=this.containment[3]+this.offset.click.top}};if(i.grid){s=this.originalPageY+Math.round((r-this.originalPageY)/i.grid[1])*i.grid[1];r=this.containment?((s-this.offset.click.top>=this.containment[1]&&s-this.offset.click.top<=this.containment[3])?s:((s-this.offset.click.top>=this.containment[1])?s-i.grid[1]:s+i.grid[1])):s;o=this.originalPageX+Math.round((n-this.originalPageX)/i.grid[0])*i.grid[0];n=this.containment?((o-this.offset.click.left>=this.containment[0]&&o-this.offset.click.left<=this.containment[2])?o:((o-this.offset.click.left>=this.containment[0])?o-i.grid[0]:o+i.grid[0])):o}};return{top:(r-this.offset.click.top-this.offset.relative.top-this.offset.parent.top+((this.cssPosition==="fixed"?-this.scrollParent.scrollTop():(h?0:a.scrollTop())))),left:(n-this.offset.click.left-this.offset.relative.left-this.offset.parent.left+((this.cssPosition==="fixed"?-this.scrollParent.scrollLeft():h?0:a.scrollLeft())))}},_rearrange:function(t,e,i,s){i?i[0].appendChild(this.placeholder[0]):e.item[0].parentNode.insertBefore(this.placeholder[0],(this.direction==="down"?e.item[0]:e.item[0].nextSibling));this.counter=this.counter?++this.counter:1;var o=this.counter;this._delay(function(){if(o===this.counter){this.refreshPositions(!s)}})},_clear:function(t,e){this.reverting=!1;var i,s=[];if(!this._noFinalSort&&this.currentItem.parent().length){this.placeholder.before(this.currentItem)};this._noFinalSort=null;if(this.helper[0]===this.currentItem[0]){for(i in this._storedCSS){if(this._storedCSS[i]==="auto"||this._storedCSS[i]==="static"){this._storedCSS[i]=""}};this.currentItem.css(this._storedCSS);this._removeClass(this.currentItem,"ui-sortable-helper")}
    116 else{this.currentItem.show()};if(this.fromOutside&&!e){s.push(function(t){this._trigger("receive",t,this._uiHash(this.fromOutside))})};if((this.fromOutside||this.domPosition.prev!==this.currentItem.prev().not(".ui-sortable-helper")[0]||this.domPosition.parent!==this.currentItem.parent()[0])&&!e){s.push(function(t){this._trigger("update",t,this._uiHash())})};if(this!==this.currentContainer){if(!e){s.push(function(t){this._trigger("remove",t,this._uiHash())});s.push((function(t){return function(e){t._trigger("receive",e,this._uiHash(this))}}).call(this,this.currentContainer));s.push((function(t){return function(e){t._trigger("update",e,this._uiHash(this))}}).call(this,this.currentContainer))}};function o(t,e,i){return function(s){i._trigger(t,s,e._uiHash(e))}};for(i=this.containers.length-1;i>=0;i--){if(!e){s.push(o("deactivate",this,this.containers[i]))};if(this.containers[i].containerCache.over){s.push(o("out",this,this.containers[i]));this.containers[i].containerCache.over=0}};if(this.storedCursor){this.document.find("body").css("cursor",this.storedCursor);this.storedStylesheet.remove()};if(this._storedOpacity){this.helper.css("opacity",this._storedOpacity)};if(this._storedZIndex){this.helper.css("zIndex",this._storedZIndex==="auto"?"":this._storedZIndex)};this.dragging=!1;if(!e){this._trigger("beforeStop",t,this._uiHash())};this.placeholder[0].parentNode.removeChild(this.placeholder[0]);if(!this.cancelHelperRemoval){if(this.helper[0]!==this.currentItem[0]){this.helper.remove()};this.helper=null};if(!e){for(i=0;i<s.length;i++){s[i].call(this,t)};this._trigger("stop",t,this._uiHash())};this.fromOutside=!1;return!this.cancelHelperRemoval},_trigger:function(){if(t.Widget.prototype._trigger.apply(this,arguments)===!1){this.cancel()}},_uiHash:function(e){var i=e||this;return{helper:i.helper,placeholder:i.placeholder||t([]),position:i.position,originalPosition:i.originalPosition,offset:i.positionAbs,item:i.currentItem,sender:e?e.element:null}}})}));
    117 /* ./modules/cms/ui/themes/default/script/plugin/jquery-plugin-orSearch.min.js */;jQuery.fn.orSearch=function(e){var t=$.extend({'dropdown':$(),'select':function(e){}},e);return $(this).on('input',function(){let searchArgument=$(this).val();let dropdownEl=$(t.dropdown);if(searchArgument.length>3){$(dropdownEl).empty();$.ajax({'type':'GET',url:'./api/?action=search&subaction=quicksearch&output=json&search='+searchArgument,data:null,success:function(e,n,r){for(id in e.output.result){let result=e.output.result[id];let div=$('<div class="entry or-search-result" title="'+result.desc+'"></div>');div.data('object',{'name':result.name,'action':result.type,'id':result.id});let link=$('<a />').attr('href',Openrat.Navigator.createShortUrl(result.type,result.id));link.click(function(e){e.preventDefault()});$(link).append('<i class="image-icon image-icon--action-'+result.type+'" />');$(link).append('<span>'+result.name+'</span>');$(div).append(link);$(dropdownEl).append(div)};$(dropdownEl).closest('.or-menu').addClass('open');$(dropdownEl).find('.or-search-result').click(function(e){t.select($(this).data('object'))})}})}
    118 else{$(dropdownEl).empty()}})};
    119 /* ./modules/cms/ui/themes/default/script/plugin/jquery-plugin-orLinkify.min.js */;var popupWindow;jQuery.fn.orLinkify=function(){$(this).find('a').click(function(t){t.preventDefault()});return $(this).click(function(){$(this).find('a').first().each(function(){let type=$(this).attr('data-type');if($(this).parent().hasClass('inactive'))return;switch(type){case'post':$form=$('<form />').attr('method','POST').addClass('invisible');$form.data('afterSuccess',$(this).data('afterSuccess'));let params=jQuery.parseJSON($(this).attr('data-data'));params.output='json';$.each(params,function(t,a){let $input=$('<input />').attr('type','hidden').attr('name',t).attr('value',a);$form.append($input)});let form=new Openrat.Form();form.initOnElement($form);form.submit();break;case'edit':case'dialog':startDialog($(this).attr('data-name'),$(this).attr('data-action'),$(this).attr('data-method'),$(this).attr('data-id'),$(this).attr('data-extra'));break;case'external':window.open($(this).attr('data-url'),' _blank');break;case'popup':popupWindow=window.open($(this).attr('data-url'),'Popup','location=no,menubar=no,scrollbars=yes,toolbar=no,resizable=yes');break;case'help':help(this,$(this).attr('data-url'),$(this).attr('data-suffix'));break;case'fullscreen':fullscreen(this);break;case'open':openNewAction($(this).attr('data-name'),$(this).attr('data-action'),$(this).attr('data-id'));break;default:throw'UI error: Unknown link type: '+type+' in link '+$(this).html()}})})};
    120 /* ./modules/cms/ui/themes/default/script/plugin/jquery-plugin-orTree.min.js */;jQuery.fn.orTree=function(){$(this).each(function(a,e){$(e).children('.or-navtree-node-control').click(function(){var a=$(this).parent('.or-navtree-node');if($(a).is('.or-navtree-node--is-open')){$(a).children('ul').slideUp('fast').remove();$(a).removeClass('or-navtree-node--is-open').addClass('or-navtree-node--is-closed').find('.tree-icon').removeClass('image-icon--node-open').addClass('image-icon--node-closed')}
    121 else{$(e).closest('div.view').addClass('loader');var i=$(a).data('type'),o=$(a).data('id'),t=$(a).data('extra'),n='./api/?action=tree&subaction=loadBranch&id='+o+'&type='+i+'&output=json';if(typeof t==='string'){jQuery.each(jQuery.parseJSON(t.replace(/'/g,'"')),function(e,a){n=n+'&'+e+'='+a})}
    122 else if(typeof t==='object'){jQuery.each(t,function(e,a){n=n+'&'+e+'='+a})}
    123 else{};$.getJSON(n,function(a){let ul=$('<ul class="or-navtree-list" />');$(e).append(ul);let output=a['output'];$.each(output['branch'],function(a,e){let new_li=$('<li class="or-navtree-node or-navtree-node--is-closed or-draggable or-draggable--type-'+e.type+'" data-name="'+e.text+'"  data-id="'+e.internalId+'" data-type="'+e.type+'" data-extra="'+JSON.stringify(e.extraId).replace(/"/g,'\'')+'"><div class="tree or-navtree-node-control"><i class="tree-icon image-icon image-icon--node-closed"></i></div><div class="clickable"><a href="'+Openrat.Navigator.createShortUrl(e.action,e.internalId)+'" class="entry" data-extra="'+JSON.stringify(e.extraId).replace(/"/g,'\'')+'" data-id="'+e.internalId+'" data-action="'+e.action+'" data-type="open" title="'+e.description+'"><i class="image-icon image-icon--action-'+e['icon']+'"></i> '+e.text+'</a></div></li>');$(ul).append(new_li);$(new_li).orTree();$(new_li).find('.clickable').orLinkify();$(new_li).find('.clickable a').click(function(e){e.preventDefault()});registerTreeBranchEvents(ul)});$(ul).slideDown('fast')}).fail(function(){Openrat.Workbench.notify('','','ERROR','failed to load subtree',[],!1)}).always(function(){$(e).closest('div.view').removeClass('loader')});$(a).addClass('or-navtree-node--is-open').removeClass('or-navtree-node--is-closed').find('.tree-icon').addClass('image-icon--node-open').removeClass('image-icon--node-closed')}})})};
    124 /* ./modules/cms/ui/themes/default/script/plugin/jquery-plugin-orAutoheight.min.js */;jQuery.fn.orAutoheight=function(){var t=function(t){var n=$(t).val().split('\n').length;$(t).attr('rows',n+3)};$(this).each(function(n){t(this)});return $(this).keypress(function(){t(this)})};
    125 /* ./modules/cms/ui/themes/default/script/jquery-qrcode.min.js *//*! jquery-qrcode v0.14.0 - https://larsjung.de/jquery-qrcode/ */
    126 !function(r){"use strict";function t(t,e,n,o){function a(r,t){return r-=o,t-=o,0>r||r>=c||0>t||t>=c?!1:f.isDark(r,t)}function i(r,t,e,n){var o=u.isDark,a=1/l;u.isDark=function(i,u){var f=u*a,c=i*a,l=f+a,g=c+a;return o(i,u)&&(r>l||f>e||t>g||c>n)}}var u={},f=r(n,e);f.addData(t),f.make(),o=o||0;var c=f.getModuleCount(),l=f.getModuleCount()+2*o;return u.text=t,u.level=e,u.version=n,u.moduleCount=l,u.isDark=a,u.addBlank=i,u}function e(r,e,n,o,a){n=Math.max(1,n||1),o=Math.min(40,o||40);for(var i=n;o>=i;i+=1)try{return t(r,e,i,a)}catch(u){}}function n(r,t,e){var n=e.size,o="bold "+e.mSize*n+"px "+e.fontname,a=w("<canvas/>")[0].getContext("2d");a.font=o;var i=a.measureText(e.label).width,u=e.mSize,f=i/n,c=(1-f)*e.mPosX,l=(1-u)*e.mPosY,g=c+f,s=l+u,v=.01;1===e.mode?r.addBlank(0,l-v,n,s+v):r.addBlank(c-v,l-v,g+v,s+v),t.fillStyle=e.fontcolor,t.font=o,t.fillText(e.label,c*n,l*n+.75*e.mSize*n)}function o(r,t,e){var n=e.size,o=e.image.naturalWidth||1,a=e.image.naturalHeight||1,i=e.mSize,u=i*o/a,f=(1-u)*e.mPosX,c=(1-i)*e.mPosY,l=f+u,g=c+i,s=.01;3===e.mode?r.addBlank(0,c-s,n,g+s):r.addBlank(f-s,c-s,l+s,g+s),t.drawImage(e.image,f*n,c*n,u*n,i*n)}function a(r,t,e){w(e.background).is("img")?t.drawImage(e.background,0,0,e.size,e.size):e.background&&(t.fillStyle=e.background,t.fillRect(e.left,e.top,e.size,e.size));var a=e.mode;1===a||2===a?n(r,t,e):(3===a||4===a)&&o(r,t,e)}function i(r,t,e,n,o,a,i,u){r.isDark(i,u)&&t.rect(n,o,a,a)}function u(r,t,e,n,o,a,i,u,f,c){i?r.moveTo(t+a,e):r.moveTo(t,e),u?(r.lineTo(n-a,e),r.arcTo(n,e,n,o,a)):r.lineTo(n,e),f?(r.lineTo(n,o-a),r.arcTo(n,o,t,o,a)):r.lineTo(n,o),c?(r.lineTo(t+a,o),r.arcTo(t,o,t,e,a)):r.lineTo(t,o),i?(r.lineTo(t,e+a),r.arcTo(t,e,n,e,a)):r.lineTo(t,e)}function f(r,t,e,n,o,a,i,u,f,c){i&&(r.moveTo(t+a,e),r.lineTo(t,e),r.lineTo(t,e+a),r.arcTo(t,e,t+a,e,a)),u&&(r.moveTo(n-a,e),r.lineTo(n,e),r.lineTo(n,e+a),r.arcTo(n,e,n-a,e,a)),f&&(r.moveTo(n-a,o),r.lineTo(n,o),r.lineTo(n,o-a),r.arcTo(n,o,n-a,o,a)),c&&(r.moveTo(t+a,o),r.lineTo(t,o),r.lineTo(t,o-a),r.arcTo(t,o,t+a,o,a))}function c(r,t,e,n,o,a,i,c){var l=r.isDark,g=n+a,s=o+a,v=e.radius*a,h=i-1,d=i+1,w=c-1,m=c+1,y=l(i,c),T=l(h,w),p=l(h,c),B=l(h,m),A=l(i,m),E=l(d,m),k=l(d,c),M=l(d,w),C=l(i,w);y?u(t,n,o,g,s,v,!p&&!C,!p&&!A,!k&&!A,!k&&!C):f(t,n,o,g,s,v,p&&C&&T,p&&A&&B,k&&A&&E,k&&C&&M)}function l(r,t,e){var n,o,a=r.moduleCount,u=e.size/a,f=i;for(e.radius>0&&e.radius<=.5&&(f=c),t.beginPath(),n=0;a>n;n+=1)for(o=0;a>o;o+=1){var l=e.left+o*u,g=e.top+n*u,s=u;f(r,t,e,l,g,s,n,o)}if(w(e.fill).is("img")){t.strokeStyle="rgba(0,0,0,0.5)",t.lineWidth=2,t.stroke();var v=t.globalCompositeOperation;t.globalCompositeOperation="destination-out",t.fill(),t.globalCompositeOperation=v,t.clip(),t.drawImage(e.fill,0,0,e.size,e.size),t.restore()}else t.fillStyle=e.fill,t.fill()}function g(r,t){var n=e(t.text,t.ecLevel,t.minVersion,t.maxVersion,t.quiet);if(!n)return null;var o=w(r).data("qrcode",n),i=o[0].getContext("2d");return a(n,i,t),l(n,i,t),o}function s(r){var t=w("<canvas/>").attr("width",r.size).attr("height",r.size);return g(t,r)}function v(r){return w("<img/>").attr("src",s(r)[0].toDataURL("image/png"))}function h(r){var t=e(r.text,r.ecLevel,r.minVersion,r.maxVersion,r.quiet);if(!t)return null;var n,o,a=r.size,i=r.background,u=Math.floor,f=t.moduleCount,c=u(a/f),l=u(.5*(a-c*f)),g={position:"relative",left:0,top:0,padding:0,margin:0,width:a,height:a},s={position:"absolute",padding:0,margin:0,width:c,height:c,"background-color":r.fill},v=w("<div/>").data("qrcode",t).css(g);for(i&&v.css("background-color",i),n=0;f>n;n+=1)for(o=0;f>o;o+=1)t.isDark(n,o)&&w("<div/>").css(s).css({left:l+o*c,top:l+n*c}).appendTo(v);return v}function d(r){return m&&"canvas"===r.render?s(r):m&&"image"===r.render?v(r):h(r)}var w=window.jQuery,m=function(){var r=document.createElement("canvas");return!(!r.getContext||!r.getContext("2d"))}(),y={render:"canvas",minVersion:1,maxVersion:40,ecLevel:"L",left:0,top:0,size:200,fill:"#000",background:null,text:"no text",radius:0,quiet:0,mode:0,mSize:.1,mPosX:.5,mPosY:.5,label:"no label",fontname:"sans",fontcolor:"#000",image:null};w.fn.qrcode=function(r){var t=w.extend({},y,r);return this.each(function(r,e){"canvas"===e.nodeName.toLowerCase()?g(e,t):w(e).append(d(t))})}}(function(){var r=function(){function r(t,e){if("undefined"==typeof t.length)throw new Error(t.length+"/"+e);var n=function(){for(var r=0;r<t.length&&0==t[r];)r+=1;for(var n=new Array(t.length-r+e),o=0;o<t.length-r;o+=1)n[o]=t[o+r];return n}(),o={};return o.getAt=function(r){return n[r]},o.getLength=function(){return n.length},o.multiply=function(t){for(var e=new Array(o.getLength()+t.getLength()-1),n=0;n<o.getLength();n+=1)for(var a=0;a<t.getLength();a+=1)e[n+a]^=i.gexp(i.glog(o.getAt(n))+i.glog(t.getAt(a)));return r(e,0)},o.mod=function(t){if(o.getLength()-t.getLength()<0)return o;for(var e=i.glog(o.getAt(0))-i.glog(t.getAt(0)),n=new Array(o.getLength()),a=0;a<o.getLength();a+=1)n[a]=o.getAt(a);for(var a=0;a<t.getLength();a+=1)n[a]^=i.gexp(i.glog(t.getAt(a))+e);return r(n,0).mod(t)},o}var t=function(t,e){var o=236,i=17,l=t,g=n[e],s=null,v=0,d=null,w=new Array,m={},y=function(r,t){v=4*l+17,s=function(r){for(var t=new Array(r),e=0;r>e;e+=1){t[e]=new Array(r);for(var n=0;r>n;n+=1)t[e][n]=null}return t}(v),T(0,0),T(v-7,0),T(0,v-7),A(),B(),k(r,t),l>=7&&E(r),null==d&&(d=D(l,g,w)),M(d,t)},T=function(r,t){for(var e=-1;7>=e;e+=1)if(!(-1>=r+e||r+e>=v))for(var n=-1;7>=n;n+=1)-1>=t+n||t+n>=v||(e>=0&&6>=e&&(0==n||6==n)||n>=0&&6>=n&&(0==e||6==e)||e>=2&&4>=e&&n>=2&&4>=n?s[r+e][t+n]=!0:s[r+e][t+n]=!1)},p=function(){for(var r=0,t=0,e=0;8>e;e+=1){y(!0,e);var n=a.getLostPoint(m);(0==e||r>n)&&(r=n,t=e)}return t},B=function(){for(var r=8;v-8>r;r+=1)null==s[r][6]&&(s[r][6]=r%2==0);for(var t=8;v-8>t;t+=1)null==s[6][t]&&(s[6][t]=t%2==0)},A=function(){for(var r=a.getPatternPosition(l),t=0;t<r.length;t+=1)for(var e=0;e<r.length;e+=1){var n=r[t],o=r[e];if(null==s[n][o])for(var i=-2;2>=i;i+=1)for(var u=-2;2>=u;u+=1)-2==i||2==i||-2==u||2==u||0==i&&0==u?s[n+i][o+u]=!0:s[n+i][o+u]=!1}},E=function(r){for(var t=a.getBCHTypeNumber(l),e=0;18>e;e+=1){var n=!r&&1==(t>>e&1);s[Math.floor(e/3)][e%3+v-8-3]=n}for(var e=0;18>e;e+=1){var n=!r&&1==(t>>e&1);s[e%3+v-8-3][Math.floor(e/3)]=n}},k=function(r,t){for(var e=g<<3|t,n=a.getBCHTypeInfo(e),o=0;15>o;o+=1){var i=!r&&1==(n>>o&1);6>o?s[o][8]=i:8>o?s[o+1][8]=i:s[v-15+o][8]=i}for(var o=0;15>o;o+=1){var i=!r&&1==(n>>o&1);8>o?s[8][v-o-1]=i:9>o?s[8][15-o-1+1]=i:s[8][15-o-1]=i}s[v-8][8]=!r},M=function(r,t){for(var e=-1,n=v-1,o=7,i=0,u=a.getMaskFunction(t),f=v-1;f>0;f-=2)for(6==f&&(f-=1);;){for(var c=0;2>c;c+=1)if(null==s[n][f-c]){var l=!1;i<r.length&&(l=1==(r[i]>>>o&1));var g=u(n,f-c);g&&(l=!l),s[n][f-c]=l,o-=1,-1==o&&(i+=1,o=7)}if(n+=e,0>n||n>=v){n-=e,e=-e;break}}},C=function(t,e){for(var n=0,o=0,i=0,u=new Array(e.length),f=new Array(e.length),c=0;c<e.length;c+=1){var l=e[c].dataCount,g=e[c].totalCount-l;o=Math.max(o,l),i=Math.max(i,g),u[c]=new Array(l);for(var s=0;s<u[c].length;s+=1)u[c][s]=255&t.getBuffer()[s+n];n+=l;var v=a.getErrorCorrectPolynomial(g),h=r(u[c],v.getLength()-1),d=h.mod(v);f[c]=new Array(v.getLength()-1);for(var s=0;s<f[c].length;s+=1){var w=s+d.getLength()-f[c].length;f[c][s]=w>=0?d.getAt(w):0}}for(var m=0,s=0;s<e.length;s+=1)m+=e[s].totalCount;for(var y=new Array(m),T=0,s=0;o>s;s+=1)for(var c=0;c<e.length;c+=1)s<u[c].length&&(y[T]=u[c][s],T+=1);for(var s=0;i>s;s+=1)for(var c=0;c<e.length;c+=1)s<f[c].length&&(y[T]=f[c][s],T+=1);return y},D=function(r,t,e){for(var n=u.getRSBlocks(r,t),c=f(),l=0;l<e.length;l+=1){var g=e[l];c.put(g.getMode(),4),c.put(g.getLength(),a.getLengthInBits(g.getMode(),r)),g.write(c)}for(var s=0,l=0;l<n.length;l+=1)s+=n[l].dataCount;if(c.getLengthInBits()>8*s)throw new Error("code length overflow. ("+c.getLengthInBits()+">"+8*s+")");for(c.getLengthInBits()+4<=8*s&&c.put(0,4);c.getLengthInBits()%8!=0;)c.putBit(!1);for(;;){if(c.getLengthInBits()>=8*s)break;if(c.put(o,8),c.getLengthInBits()>=8*s)break;c.put(i,8)}return C(c,n)};return m.addData=function(r){var t=c(r);w.push(t),d=null},m.isDark=function(r,t){if(0>r||r>=v||0>t||t>=v)throw new Error(r+","+t);return s[r][t]},m.getModuleCount=function(){return v},m.make=function(){y(!1,p())},m.createTableTag=function(r,t){r=r||2,t="undefined"==typeof t?4*r:t;var e="";e+='<table style="',e+=" border-width: 0px; border-style: none;",e+=" border-collapse: collapse;",e+=" padding: 0px; margin: "+t+"px;",e+='">',e+="<tbody>";for(var n=0;n<m.getModuleCount();n+=1){e+="<tr>";for(var o=0;o<m.getModuleCount();o+=1)e+='<td style="',e+=" border-width: 0px; border-style: none;",e+=" border-collapse: collapse;",e+=" padding: 0px; margin: 0px;",e+=" width: "+r+"px;",e+=" height: "+r+"px;",e+=" background-color: ",e+=m.isDark(n,o)?"#000000":"#ffffff",e+=";",e+='"/>';e+="</tr>"}return e+="</tbody>",e+="</table>"},m.createImgTag=function(r,t){r=r||2,t="undefined"==typeof t?4*r:t;var e=m.getModuleCount()*r+2*t,n=t,o=e-t;return h(e,e,function(t,e){if(t>=n&&o>t&&e>=n&&o>e){var a=Math.floor((t-n)/r),i=Math.floor((e-n)/r);return m.isDark(i,a)?0:1}return 1})},m};t.stringToBytes=function(r){for(var t=new Array,e=0;e<r.length;e+=1){var n=r.charCodeAt(e);t.push(255&n)}return t},t.createStringToBytes=function(r,t){var e=function(){for(var e=s(r),n=function(){var r=e.read();if(-1==r)throw new Error;return r},o=0,a={};;){var i=e.read();if(-1==i)break;var u=n(),f=n(),c=n(),l=String.fromCharCode(i<<8|u),g=f<<8|c;a[l]=g,o+=1}if(o!=t)throw new Error(o+" != "+t);return a}(),n="?".charCodeAt(0);return function(r){for(var t=new Array,o=0;o<r.length;o+=1){var a=r.charCodeAt(o);if(128>a)t.push(a);else{var i=e[r.charAt(o)];"number"==typeof i?(255&i)==i?t.push(i):(t.push(i>>>8),t.push(255&i)):t.push(n)}}return t}};var e={MODE_NUMBER:1,MODE_ALPHA_NUM:2,MODE_8BIT_BYTE:4,MODE_KANJI:8},n={L:1,M:0,Q:3,H:2},o={PATTERN000:0,PATTERN001:1,PATTERN010:2,PATTERN011:3,PATTERN100:4,PATTERN101:5,PATTERN110:6,PATTERN111:7},a=function(){var t=[[],[6,18],[6,22],[6,26],[6,30],[6,34],[6,22,38],[6,24,42],[6,26,46],[6,28,50],[6,30,54],[6,32,58],[6,34,62],[6,26,46,66],[6,26,48,70],[6,26,50,74],[6,30,54,78],[6,30,56,82],[6,30,58,86],[6,34,62,90],[6,28,50,72,94],[6,26,50,74,98],[6,30,54,78,102],[6,28,54,80,106],[6,32,58,84,110],[6,30,58,86,114],[6,34,62,90,118],[6,26,50,74,98,122],[6,30,54,78,102,126],[6,26,52,78,104,130],[6,30,56,82,108,134],[6,34,60,86,112,138],[6,30,58,86,114,142],[6,34,62,90,118,146],[6,30,54,78,102,126,150],[6,24,50,76,102,128,154],[6,28,54,80,106,132,158],[6,32,58,84,110,136,162],[6,26,54,82,110,138,166],[6,30,58,86,114,142,170]],n=1335,a=7973,u=21522,f={},c=function(r){for(var t=0;0!=r;)t+=1,r>>>=1;return t};return f.getBCHTypeInfo=function(r){for(var t=r<<10;c(t)-c(n)>=0;)t^=n<<c(t)-c(n);return(r<<10|t)^u},f.getBCHTypeNumber=function(r){for(var t=r<<12;c(t)-c(a)>=0;)t^=a<<c(t)-c(a);return r<<12|t},f.getPatternPosition=function(r){return t[r-1]},f.getMaskFunction=function(r){switch(r){case o.PATTERN000:return function(r,t){return(r+t)%2==0};case o.PATTERN001:return function(r,t){return r%2==0};case o.PATTERN010:return function(r,t){return t%3==0};case o.PATTERN011:return function(r,t){return(r+t)%3==0};case o.PATTERN100:return function(r,t){return(Math.floor(r/2)+Math.floor(t/3))%2==0};case o.PATTERN101:return function(r,t){return r*t%2+r*t%3==0};case o.PATTERN110:return function(r,t){return(r*t%2+r*t%3)%2==0};case o.PATTERN111:return function(r,t){return(r*t%3+(r+t)%2)%2==0};default:throw new Error("bad maskPattern:"+r)}},f.getErrorCorrectPolynomial=function(t){for(var e=r([1],0),n=0;t>n;n+=1)e=e.multiply(r([1,i.gexp(n)],0));return e},f.getLengthInBits=function(r,t){if(t>=1&&10>t)switch(r){case e.MODE_NUMBER:return 10;case e.MODE_ALPHA_NUM:return 9;case e.MODE_8BIT_BYTE:return 8;case e.MODE_KANJI:return 8;default:throw new Error("mode:"+r)}else if(27>t)switch(r){case e.MODE_NUMBER:return 12;case e.MODE_ALPHA_NUM:return 11;case e.MODE_8BIT_BYTE:return 16;case e.MODE_KANJI:return 10;default:throw new Error("mode:"+r)}else{if(!(41>t))throw new Error("type:"+t);switch(r){case e.MODE_NUMBER:return 14;case e.MODE_ALPHA_NUM:return 13;case e.MODE_8BIT_BYTE:return 16;case e.MODE_KANJI:return 12;default:throw new Error("mode:"+r)}}},f.getLostPoint=function(r){for(var t=r.getModuleCount(),e=0,n=0;t>n;n+=1)for(var o=0;t>o;o+=1){for(var a=0,i=r.isDark(n,o),u=-1;1>=u;u+=1)if(!(0>n+u||n+u>=t))for(var f=-1;1>=f;f+=1)0>o+f||o+f>=t||(0!=u||0!=f)&&i==r.isDark(n+u,o+f)&&(a+=1);a>5&&(e+=3+a-5)}for(var n=0;t-1>n;n+=1)for(var o=0;t-1>o;o+=1){var c=0;r.isDark(n,o)&&(c+=1),r.isDark(n+1,o)&&(c+=1),r.isDark(n,o+1)&&(c+=1),r.isDark(n+1,o+1)&&(c+=1),(0==c||4==c)&&(e+=3)}for(var n=0;t>n;n+=1)for(var o=0;t-6>o;o+=1)r.isDark(n,o)&&!r.isDark(n,o+1)&&r.isDark(n,o+2)&&r.isDark(n,o+3)&&r.isDark(n,o+4)&&!r.isDark(n,o+5)&&r.isDark(n,o+6)&&(e+=40);for(var o=0;t>o;o+=1)for(var n=0;t-6>n;n+=1)r.isDark(n,o)&&!r.isDark(n+1,o)&&r.isDark(n+2,o)&&r.isDark(n+3,o)&&r.isDark(n+4,o)&&!r.isDark(n+5,o)&&r.isDark(n+6,o)&&(e+=40);for(var l=0,o=0;t>o;o+=1)for(var n=0;t>n;n+=1)r.isDark(n,o)&&(l+=1);var g=Math.abs(100*l/t/t-50)/5;return e+=10*g},f}(),i=function(){for(var r=new Array(256),t=new Array(256),e=0;8>e;e+=1)r[e]=1<<e;for(var e=8;256>e;e+=1)r[e]=r[e-4]^r[e-5]^r[e-6]^r[e-8];for(var e=0;255>e;e+=1)t[r[e]]=e;var n={};return n.glog=function(r){if(1>r)throw new Error("glog("+r+")");return t[r]},n.gexp=function(t){for(;0>t;)t+=255;for(;t>=256;)t-=255;return r[t]},n}(),u=function(){var r=[[1,26,19],[1,26,16],[1,26,13],[1,26,9],[1,44,34],[1,44,28],[1,44,22],[1,44,16],[1,70,55],[1,70,44],[2,35,17],[2,35,13],[1,100,80],[2,50,32],[2,50,24],[4,25,9],[1,134,108],[2,67,43],[2,33,15,2,34,16],[2,33,11,2,34,12],[2,86,68],[4,43,27],[4,43,19],[4,43,15],[2,98,78],[4,49,31],[2,32,14,4,33,15],[4,39,13,1,40,14],[2,121,97],[2,60,38,2,61,39],[4,40,18,2,41,19],[4,40,14,2,41,15],[2,146,116],[3,58,36,2,59,37],[4,36,16,4,37,17],[4,36,12,4,37,13],[2,86,68,2,87,69],[4,69,43,1,70,44],[6,43,19,2,44,20],[6,43,15,2,44,16],[4,101,81],[1,80,50,4,81,51],[4,50,22,4,51,23],[3,36,12,8,37,13],[2,116,92,2,117,93],[6,58,36,2,59,37],[4,46,20,6,47,21],[7,42,14,4,43,15],[4,133,107],[8,59,37,1,60,38],[8,44,20,4,45,21],[12,33,11,4,34,12],[3,145,115,1,146,116],[4,64,40,5,65,41],[11,36,16,5,37,17],[11,36,12,5,37,13],[5,109,87,1,110,88],[5,65,41,5,66,42],[5,54,24,7,55,25],[11,36,12,7,37,13],[5,122,98,1,123,99],[7,73,45,3,74,46],[15,43,19,2,44,20],[3,45,15,13,46,16],[1,135,107,5,136,108],[10,74,46,1,75,47],[1,50,22,15,51,23],[2,42,14,17,43,15],[5,150,120,1,151,121],[9,69,43,4,70,44],[17,50,22,1,51,23],[2,42,14,19,43,15],[3,141,113,4,142,114],[3,70,44,11,71,45],[17,47,21,4,48,22],[9,39,13,16,40,14],[3,135,107,5,136,108],[3,67,41,13,68,42],[15,54,24,5,55,25],[15,43,15,10,44,16],[4,144,116,4,145,117],[17,68,42],[17,50,22,6,51,23],[19,46,16,6,47,17],[2,139,111,7,140,112],[17,74,46],[7,54,24,16,55,25],[34,37,13],[4,151,121,5,152,122],[4,75,47,14,76,48],[11,54,24,14,55,25],[16,45,15,14,46,16],[6,147,117,4,148,118],[6,73,45,14,74,46],[11,54,24,16,55,25],[30,46,16,2,47,17],[8,132,106,4,133,107],[8,75,47,13,76,48],[7,54,24,22,55,25],[22,45,15,13,46,16],[10,142,114,2,143,115],[19,74,46,4,75,47],[28,50,22,6,51,23],[33,46,16,4,47,17],[8,152,122,4,153,123],[22,73,45,3,74,46],[8,53,23,26,54,24],[12,45,15,28,46,16],[3,147,117,10,148,118],[3,73,45,23,74,46],[4,54,24,31,55,25],[11,45,15,31,46,16],[7,146,116,7,147,117],[21,73,45,7,74,46],[1,53,23,37,54,24],[19,45,15,26,46,16],[5,145,115,10,146,116],[19,75,47,10,76,48],[15,54,24,25,55,25],[23,45,15,25,46,16],[13,145,115,3,146,116],[2,74,46,29,75,47],[42,54,24,1,55,25],[23,45,15,28,46,16],[17,145,115],[10,74,46,23,75,47],[10,54,24,35,55,25],[19,45,15,35,46,16],[17,145,115,1,146,116],[14,74,46,21,75,47],[29,54,24,19,55,25],[11,45,15,46,46,16],[13,145,115,6,146,116],[14,74,46,23,75,47],[44,54,24,7,55,25],[59,46,16,1,47,17],[12,151,121,7,152,122],[12,75,47,26,76,48],[39,54,24,14,55,25],[22,45,15,41,46,16],[6,151,121,14,152,122],[6,75,47,34,76,48],[46,54,24,10,55,25],[2,45,15,64,46,16],[17,152,122,4,153,123],[29,74,46,14,75,47],[49,54,24,10,55,25],[24,45,15,46,46,16],[4,152,122,18,153,123],[13,74,46,32,75,47],[48,54,24,14,55,25],[42,45,15,32,46,16],[20,147,117,4,148,118],[40,75,47,7,76,48],[43,54,24,22,55,25],[10,45,15,67,46,16],[19,148,118,6,149,119],[18,75,47,31,76,48],[34,54,24,34,55,25],[20,45,15,61,46,16]],t=function(r,t){var e={};return e.totalCount=r,e.dataCount=t,e},e={},o=function(t,e){switch(e){case n.L:return r[4*(t-1)+0];case n.M:return r[4*(t-1)+1];case n.Q:return r[4*(t-1)+2];case n.H:return r[4*(t-1)+3];default:return}};return e.getRSBlocks=function(r,e){var n=o(r,e);if("undefined"==typeof n)throw new Error("bad rs block @ typeNumber:"+r+"/errorCorrectLevel:"+e);for(var a=n.length/3,i=new Array,u=0;a>u;u+=1)for(var f=n[3*u+0],c=n[3*u+1],l=n[3*u+2],g=0;f>g;g+=1)i.push(t(c,l));return i},e}(),f=function(){var r=new Array,t=0,e={};return e.getBuffer=function(){return r},e.getAt=function(t){var e=Math.floor(t/8);return 1==(r[e]>>>7-t%8&1)},e.put=function(r,t){for(var n=0;t>n;n+=1)e.putBit(1==(r>>>t-n-1&1))},e.getLengthInBits=function(){return t},e.putBit=function(e){var n=Math.floor(t/8);r.length<=n&&r.push(0),e&&(r[n]|=128>>>t%8),t+=1},e},c=function(r){var n=e.MODE_8BIT_BYTE,o=t.stringToBytes(r),a={};return a.getMode=function(){return n},a.getLength=function(r){return o.length},a.write=function(r){for(var t=0;t<o.length;t+=1)r.put(o[t],8)},a},l=function(){var r=new Array,t={};return t.writeByte=function(t){r.push(255&t)},t.writeShort=function(r){t.writeByte(r),t.writeByte(r>>>8)},t.writeBytes=function(r,e,n){e=e||0,n=n||r.length;for(var o=0;n>o;o+=1)t.writeByte(r[o+e])},t.writeString=function(r){for(var e=0;e<r.length;e+=1)t.writeByte(r.charCodeAt(e))},t.toByteArray=function(){return r},t.toString=function(){var t="";t+="[";for(var e=0;e<r.length;e+=1)e>0&&(t+=","),t+=r[e];return t+="]"},t},g=function(){var r=0,t=0,e=0,n="",o={},a=function(r){n+=String.fromCharCode(i(63&r))},i=function(r){if(0>r);else{if(26>r)return 65+r;if(52>r)return 97+(r-26);if(62>r)return 48+(r-52);if(62==r)return 43;if(63==r)return 47}throw new Error("n:"+r)};return o.writeByte=function(n){for(r=r<<8|255&n,t+=8,e+=1;t>=6;)a(r>>>t-6),t-=6},o.flush=function(){if(t>0&&(a(r<<6-t),r=0,t=0),e%3!=0)for(var o=3-e%3,i=0;o>i;i+=1)n+="="},o.toString=function(){return n},o},s=function(r){var t=r,e=0,n=0,o=0,a={};a.read=function(){for(;8>o;){if(e>=t.length){if(0==o)return-1;throw new Error("unexpected end of file./"+o)}var r=t.charAt(e);if(e+=1,"="==r)return o=0,-1;r.match(/^\s$/)||(n=n<<6|i(r.charCodeAt(0)),o+=6)}var a=n>>>o-8&255;return o-=8,a};var i=function(r){if(r>=65&&90>=r)return r-65;if(r>=97&&122>=r)return r-97+26;if(r>=48&&57>=r)return r-48+52;if(43==r)return 62;if(47==r)return 63;throw new Error("c:"+r)};return a},v=function(r,t){var e=r,n=t,o=new Array(r*t),a={};a.setPixel=function(r,t,n){o[t*e+r]=n},a.write=function(r){r.writeString("GIF87a"),r.writeShort(e),r.writeShort(n),r.writeByte(128),r.writeByte(0),r.writeByte(0),r.writeByte(0),r.writeByte(0),r.writeByte(0),r.writeByte(255),r.writeByte(255),r.writeByte(255),r.writeString(","),r.writeShort(0),r.writeShort(0),r.writeShort(e),r.writeShort(n),r.writeByte(0);var t=2,o=u(t);r.writeByte(t);for(var a=0;o.length-a>255;)r.writeByte(255),r.writeBytes(o,a,255),a+=255;r.writeByte(o.length-a),r.writeBytes(o,a,o.length-a),r.writeByte(0),r.writeString(";")};var i=function(r){var t=r,e=0,n=0,o={};return o.write=function(r,o){if(r>>>o!=0)throw new Error("length over");for(;e+o>=8;)t.writeByte(255&(r<<e|n)),o-=8-e,r>>>=8-e,n=0,e=0;n=r<<e|n,e+=o},o.flush=function(){e>0&&t.writeByte(n)},o},u=function(r){for(var t=1<<r,e=(1<<r)+1,n=r+1,a=f(),u=0;t>u;u+=1)a.add(String.fromCharCode(u));a.add(String.fromCharCode(t)),a.add(String.fromCharCode(e));var c=l(),g=i(c);g.write(t,n);var s=0,v=String.fromCharCode(o[s]);for(s+=1;s<o.length;){var h=String.fromCharCode(o[s]);s+=1,a.contains(v+h)?v+=h:(g.write(a.indexOf(v),n),a.size()<4095&&(a.size()==1<<n&&(n+=1),a.add(v+h)),v=h)}return g.write(a.indexOf(v),n),g.write(e,n),g.flush(),c.toByteArray()},f=function(){var r={},t=0,e={};return e.add=function(n){if(e.contains(n))throw new Error("dup key:"+n);r[n]=t,t+=1},e.size=function(){return t},e.indexOf=function(t){return r[t]},e.contains=function(t){return"undefined"!=typeof r[t]},e};return a},h=function(r,t,e,n){for(var o=v(r,t),a=0;t>a;a+=1)for(var i=0;r>i;i+=1)o.setPixel(i,a,e(i,a));var u=l();o.write(u);for(var f=g(),c=u.toByteArray(),s=0;s<c.length;s+=1)f.writeByte(c[s]);f.flush();var h="";return h+="<img",h+=' src="',h+="data:image/gif;base64,",h+=f,h+='"',h+=' width="',h+=r,h+='"',h+=' height="',h+=t,h+='"',n&&(h+=' alt="',h+=n,h+='"'),h+="/>"};return t}();return function(r){"function"==typeof define&&define.amd?define([],r):"object"==typeof exports&&(module.exports=r())}(function(){return r}),!function(r){r.stringToBytes=function(r){function t(r){for(var t=[],e=0;e<r.length;e++){var n=r.charCodeAt(e);128>n?t.push(n):2048>n?t.push(192|n>>6,128|63&n):55296>n||n>=57344?t.push(224|n>>12,128|n>>6&63,128|63&n):(e++,n=65536+((1023&n)<<10|1023&r.charCodeAt(e)),t.push(240|n>>18,128|n>>12&63,128|n>>6&63,128|63&n))}return t}return t(r)}}(r),r}());
    127 /* ./modules/cms/ui/themes/default/script/jquery.hotkeys.min.js */(function(t){t.hotkeys={version:"0.2.0",specialKeys:{8:"backspace",9:"tab",10:"return",13:"return",16:"shift",17:"ctrl",18:"alt",19:"pause",20:"capslock",27:"esc",32:"space",33:"pageup",34:"pagedown",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",45:"insert",46:"del",59:";",61:"=",96:"0",97:"1",98:"2",99:"3",100:"4",101:"5",102:"6",103:"7",104:"8",105:"9",106:"*",107:"+",109:"-",110:".",111:"/",112:"f1",113:"f2",114:"f3",115:"f4",116:"f5",117:"f6",118:"f7",119:"f8",120:"f9",121:"f10",122:"f11",123:"f12",144:"numlock",145:"scroll",173:"-",186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"`",219:"[",220:"\\",221:"]",222:"'"},shiftNums:{"`":"~","1":"!","2":"@","3":"#","4":"$","5":"%","6":"^","7":"&","8":"*","9":"(","0":")","-":"_","=":"+",";":": ","'":"\"",",":"<",".":">","/":"?","\\":"|"},textAcceptingInputTypes:["text","password","number","email","url","range","date","month","week","time","datetime","datetime-local","search","color","tel"],textInputTypes:/textarea|input|select/i,options:{filterInputAcceptingElements:!0,filterTextInputs:!0,filterContentEditable:!0}};function e(e){if(typeof e.data==="string"){e.data={keys:e.data}};if(!e.data||!e.data.keys||typeof e.data.keys!=="string"){return};var a=e.handler,s=e.data.keys.toLowerCase().split(" ");e.handler=function(e){if(this!==e.target&&(t.hotkeys.options.filterInputAcceptingElements&&t.hotkeys.textInputTypes.test(e.target.nodeName)||(t.hotkeys.options.filterContentEditable&&t(e.target).attr("contenteditable"))||(t.hotkeys.options.filterTextInputs&&t.inArray(e.target.type,t.hotkeys.textAcceptingInputTypes)>-1))){return};var n=e.type!=="keypress"&&t.hotkeys.specialKeys[e.which],f=String.fromCharCode(e.which).toLowerCase(),i="",r={};t.each(["alt","ctrl","shift"],function(t,s){if(e[s+"Key"]&&n!==s){i+=s+"+"}});if(e.metaKey&&!e.ctrlKey&&n!=="meta"){i+="meta+"};if(e.metaKey&&n!=="meta"&&i.indexOf("alt+ctrl+shift+")>-1){i=i.replace("alt+ctrl+shift+","hyper+")};if(n){r[i+n]=!0}
    128 else{r[i+f]=!0;r[i+t.hotkeys.shiftNums[f]]=!0;if(i==="shift+"){r[t.hotkeys.shiftNums[f]]=!0}};for(var o=0,p=s.length;o<p;o++){if(r[s[o]]){return a.apply(this,arguments)}}}};t.each(["keydown","keyup","keypress"],function(){t.event.special[this]={add:e}})})(jQuery||this.jQuery||window.jQuery);
    129 /* ./modules/editor/codemirror/lib/codemirror.min.js */// CodeMirror, copyright (c) by Marijn Haverbeke and others
    130 // Distributed under an MIT license: http://codemirror.net/LICENSE
    131 
    132 // This is CodeMirror (http://codemirror.net), a code editor
    133 // implemented in JavaScript on top of the browser's DOM.
    134 //
    135 // You can find some technical background for some of the code below
    136 // at http://marijnhaverbeke.nl/blog/#cm-internals .
    137 
    138 (function (global, factory) {
    139     typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
    140         typeof define === 'function' && define.amd ? define(factory) :
    141             (global.CodeMirror = factory());
    142 }(this, (function () { 'use strict';
    143 
    144 // Kludges for bugs and behavior differences that can't be feature
    145 // detected are enabled based on userAgent etc sniffing.
    146     var userAgent = navigator.userAgent
    147     var platform = navigator.platform
    148 
    149     var gecko = /gecko\/\d/i.test(userAgent)
    150     var ie_upto10 = /MSIE \d/.test(userAgent)
    151     var ie_11up = /Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(userAgent)
    152     var edge = /Edge\/(\d+)/.exec(userAgent)
    153     var ie = ie_upto10 || ie_11up || edge
    154     var ie_version = ie && (ie_upto10 ? document.documentMode || 6 : +(edge || ie_11up)[1])
    155     var webkit = !edge && /WebKit\//.test(userAgent)
    156     var qtwebkit = webkit && /Qt\/\d+\.\d+/.test(userAgent)
    157     var chrome = !edge && /Chrome\//.test(userAgent)
    158     var presto = /Opera\//.test(userAgent)
    159     var safari = /Apple Computer/.test(navigator.vendor)
    160     var mac_geMountainLion = /Mac OS X 1\d\D([8-9]|\d\d)\D/.test(userAgent)
    161     var phantom = /PhantomJS/.test(userAgent)
    162 
    163     var ios = !edge && /AppleWebKit/.test(userAgent) && /Mobile\/\w+/.test(userAgent)
    164     var android = /Android/.test(userAgent)
    165 // This is woefully incomplete. Suggestions for alternative methods welcome.
    166     var mobile = ios || android || /webOS|BlackBerry|Opera Mini|Opera Mobi|IEMobile/i.test(userAgent)
    167     var mac = ios || /Mac/.test(platform)
    168     var chromeOS = /\bCrOS\b/.test(userAgent)
    169     var windows = /win/i.test(platform)
    170 
    171     var presto_version = presto && userAgent.match(/Version\/(\d*\.\d*)/)
    172     if (presto_version) { presto_version = Number(presto_version[1]) }
    173     if (presto_version && presto_version >= 15) { presto = false; webkit = true }
    174 // Some browsers use the wrong event properties to signal cmd/ctrl on OS X
    175     var flipCtrlCmd = mac && (qtwebkit || presto && (presto_version == null || presto_version < 12.11))
    176     var captureRightClick = gecko || (ie && ie_version >= 9)
    177 
    178     function classTest(cls) { return new RegExp("(^|\\s)" + cls + "(?:$|\\s)\\s*") }
    179 
    180     var rmClass = function(node, cls) {
    181         var current = node.className
    182         var match = classTest(cls).exec(current)
    183         if (match) {
    184             var after = current.slice(match.index + match[0].length)
    185             node.className = current.slice(0, match.index) + (after ? match[1] + after : "")
    186         }
    187     }
    188 
    189     function removeChildren(e) {
    190         for (var count = e.childNodes.length; count > 0; --count)
    191         { e.removeChild(e.firstChild) }
    192         return e
    193     }
    194 
    195     function removeChildrenAndAdd(parent, e) {
    196         return removeChildren(parent).appendChild(e)
    197     }
    198 
    199     function elt(tag, content, className, style) {
    200         var e = document.createElement(tag)
    201         if (className) { e.className = className }
    202         if (style) { e.style.cssText = style }
    203         if (typeof content == "string") { e.appendChild(document.createTextNode(content)) }
    204         else if (content) { for (var i = 0; i < content.length; ++i) { e.appendChild(content[i]) } }
    205         return e
    206     }
    207 // wrapper for elt, which removes the elt from the accessibility tree
    208     function eltP(tag, content, className, style) {
    209         var e = elt(tag, content, className, style)
    210         e.setAttribute("role", "presentation")
    211         return e
    212     }
    213 
    214     var range
    215     if (document.createRange) { range = function(node, start, end, endNode) {
    216         var r = document.createRange()
    217         r.setEnd(endNode || node, end)
    218         r.setStart(node, start)
    219         return r
    220     } }
    221     else { range = function(node, start, end) {
    222         var r = document.body.createTextRange()
    223         try { r.moveToElementText(node.parentNode) }
    224         catch(e) { return r }
    225         r.collapse(true)
    226         r.moveEnd("character", end)
    227         r.moveStart("character", start)
    228         return r
    229     } }
    230 
    231     function contains(parent, child) {
    232         if (child.nodeType == 3) // Android browser always returns false when child is a textnode
    233         { child = child.parentNode }
    234         if (parent.contains)
    235         { return parent.contains(child) }
    236         do {
    237             if (child.nodeType == 11) { child = child.host }
    238             if (child == parent) { return true }
    239         } while (child = child.parentNode)
    240     }
    241 
    242     function activeElt() {
    243         // IE and Edge may throw an "Unspecified Error" when accessing document.activeElement.
    244         // IE < 10 will throw when accessed while the page is loading or in an iframe.
    245         // IE > 9 and Edge will throw when accessed in an iframe if document.body is unavailable.
    246         var activeElement
    247         try {
    248             activeElement = document.activeElement
    249         } catch(e) {
    250             activeElement = document.body || null
    251         }
    252         while (activeElement && activeElement.shadowRoot && activeElement.shadowRoot.activeElement)
    253         { activeElement = activeElement.shadowRoot.activeElement }
    254         return activeElement
    255     }
    256 
    257     function addClass(node, cls) {
    258         var current = node.className
    259         if (!classTest(cls).test(current)) { node.className += (current ? " " : "") + cls }
    260     }
    261     function joinClasses(a, b) {
    262         var as = a.split(" ")
    263         for (var i = 0; i < as.length; i++)
    264         { if (as[i] && !classTest(as[i]).test(b)) { b += " " + as[i] } }
    265         return b
    266     }
    267 
    268     var selectInput = function(node) { node.select() }
    269     if (ios) // Mobile Safari apparently has a bug where select() is broken.
    270     { selectInput = function(node) { node.selectionStart = 0; node.selectionEnd = node.value.length } }
    271     else if (ie) // Suppress mysterious IE10 errors
    272     { selectInput = function(node) { try { node.select() } catch(_e) {} } }
    273 
    274     function bind(f) {
    275         var args = Array.prototype.slice.call(arguments, 1)
    276         return function(){return f.apply(null, args)}
    277     }
    278 
    279     function copyObj(obj, target, overwrite) {
    280         if (!target) { target = {} }
    281         for (var prop in obj)
    282         { if (obj.hasOwnProperty(prop) && (overwrite !== false || !target.hasOwnProperty(prop)))
    283         { target[prop] = obj[prop] } }
    284         return target
    285     }
    286 
    287 // Counts the column offset in a string, taking tabs into account.
    288 // Used mostly to find indentation.
    289     function countColumn(string, end, tabSize, startIndex, startValue) {
    290         if (end == null) {
    291             end = string.search(/[^\s\u00a0]/)
    292             if (end == -1) { end = string.length }
    293         }
    294         for (var i = startIndex || 0, n = startValue || 0;;) {
    295             var nextTab = string.indexOf("\t", i)
    296             if (nextTab < 0 || nextTab >= end)
    297             { return n + (end - i) }
    298             n += nextTab - i
    299             n += tabSize - (n % tabSize)
    300             i = nextTab + 1
    301         }
    302     }
    303 
    304     var Delayed = function() {this.id = null};
    305     Delayed.prototype.set = function (ms, f) {
    306         clearTimeout(this.id)
    307         this.id = setTimeout(f, ms)
    308     };
    309 
    310     function indexOf(array, elt) {
    311         for (var i = 0; i < array.length; ++i)
    312         { if (array[i] == elt) { return i } }
    313         return -1
    314     }
    315 
    316 // Number of pixels added to scroller and sizer to hide scrollbar
    317     var scrollerGap = 30
    318 
    319 // Returned or thrown by various protocols to signal 'I'm not
    320 // handling this'.
    321     var Pass = {toString: function(){return "CodeMirror.Pass"}}
    322 
    323 // Reused option objects for setSelection & friends
    324     var sel_dontScroll = {scroll: false};
    325     var sel_mouse = {origin: "*mouse"};
    326     var sel_move = {origin: "+move"};
    327 // The inverse of countColumn -- find the offset that corresponds to
    328 // a particular column.
    329     function findColumn(string, goal, tabSize) {
    330         for (var pos = 0, col = 0;;) {
    331             var nextTab = string.indexOf("\t", pos)
    332             if (nextTab == -1) { nextTab = string.length }
    333             var skipped = nextTab - pos
    334             if (nextTab == string.length || col + skipped >= goal)
    335             { return pos + Math.min(skipped, goal - col) }
    336             col += nextTab - pos
    337             col += tabSize - (col % tabSize)
    338             pos = nextTab + 1
    339             if (col >= goal) { return pos }
    340         }
    341     }
    342 
    343     var spaceStrs = [""]
    344     function spaceStr(n) {
    345         while (spaceStrs.length <= n)
    346         { spaceStrs.push(lst(spaceStrs) + " ") }
    347         return spaceStrs[n]
    348     }
    349 
    350     function lst(arr) { return arr[arr.length-1] }
    351 
    352     function map(array, f) {
    353         var out = []
    354         for (var i = 0; i < array.length; i++) { out[i] = f(array[i], i) }
    355         return out
    356     }
    357 
    358     function insertSorted(array, value, score) {
    359         var pos = 0, priority = score(value)
    360         while (pos < array.length && score(array[pos]) <= priority) { pos++ }
    361         array.splice(pos, 0, value)
    362     }
    363 
    364     function nothing() {}
    365 
    366     function createObj(base, props) {
    367         var inst
    368         if (Object.create) {
    369             inst = Object.create(base)
    370         } else {
    371             nothing.prototype = base
    372             inst = new nothing()
    373         }
    374         if (props) { copyObj(props, inst) }
    375         return inst
    376     }
    377 
    378     var nonASCIISingleCaseWordChar = /[\u00df\u0587\u0590-\u05f4\u0600-\u06ff\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/
    379     function isWordCharBasic(ch) {
    380         return /\w/.test(ch) || ch > "\x80" &&
    381             (ch.toUpperCase() != ch.toLowerCase() || nonASCIISingleCaseWordChar.test(ch))
    382     }
    383     function isWordChar(ch, helper) {
    384         if (!helper) { return isWordCharBasic(ch) }
    385         if (helper.source.indexOf("\\w") > -1 && isWordCharBasic(ch)) { return true }
    386         return helper.test(ch)
    387     }
    388 
    389     function isEmpty(obj) {
    390         for (var n in obj) { if (obj.hasOwnProperty(n) && obj[n]) { return false } }
    391         return true
    392     }
    393 
    394 // Extending unicode characters. A series of a non-extending char +
    395 // any number of extending chars is treated as a single unit as far
    396 // as editing and measuring is concerned. This is not fully correct,
    397 // since some scripts/fonts/browsers also treat other configurations
    398 // of code points as a group.
    399     var extendingChars = /[\u0300-\u036f\u0483-\u0489\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u065e\u0670\u06d6-\u06dc\u06de-\u06e4\u06e7\u06e8\u06ea-\u06ed\u0711\u0730-\u074a\u07a6-\u07b0\u07eb-\u07f3\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0900-\u0902\u093c\u0941-\u0948\u094d\u0951-\u0955\u0962\u0963\u0981\u09bc\u09be\u09c1-\u09c4\u09cd\u09d7\u09e2\u09e3\u0a01\u0a02\u0a3c\u0a41\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a70\u0a71\u0a75\u0a81\u0a82\u0abc\u0ac1-\u0ac5\u0ac7\u0ac8\u0acd\u0ae2\u0ae3\u0b01\u0b3c\u0b3e\u0b3f\u0b41-\u0b44\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b82\u0bbe\u0bc0\u0bcd\u0bd7\u0c3e-\u0c40\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0cbc\u0cbf\u0cc2\u0cc6\u0ccc\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0d3e\u0d41-\u0d44\u0d4d\u0d57\u0d62\u0d63\u0dca\u0dcf\u0dd2-\u0dd4\u0dd6\u0ddf\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0eb1\u0eb4-\u0eb9\u0ebb\u0ebc\u0ec8-\u0ecd\u0f18\u0f19\u0f35\u0f37\u0f39\u0f71-\u0f7e\u0f80-\u0f84\u0f86\u0f87\u0f90-\u0f97\u0f99-\u0fbc\u0fc6\u102d-\u1030\u1032-\u1037\u1039\u103a\u103d\u103e\u1058\u1059\u105e-\u1060\u1071-\u1074\u1082\u1085\u1086\u108d\u109d\u135f\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b7-\u17bd\u17c6\u17c9-\u17d3\u17dd\u180b-\u180d\u18a9\u1920-\u1922\u1927\u1928\u1932\u1939-\u193b\u1a17\u1a18\u1a56\u1a58-\u1a5e\u1a60\u1a62\u1a65-\u1a6c\u1a73-\u1a7c\u1a7f\u1b00-\u1b03\u1b34\u1b36-\u1b3a\u1b3c\u1b42\u1b6b-\u1b73\u1b80\u1b81\u1ba2-\u1ba5\u1ba8\u1ba9\u1c2c-\u1c33\u1c36\u1c37\u1cd0-\u1cd2\u1cd4-\u1ce0\u1ce2-\u1ce8\u1ced\u1dc0-\u1de6\u1dfd-\u1dff\u200c\u200d\u20d0-\u20f0\u2cef-\u2cf1\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua66f-\ua672\ua67c\ua67d\ua6f0\ua6f1\ua802\ua806\ua80b\ua825\ua826\ua8c4\ua8e0-\ua8f1\ua926-\ua92d\ua947-\ua951\ua980-\ua982\ua9b3\ua9b6-\ua9b9\ua9bc\uaa29-\uaa2e\uaa31\uaa32\uaa35\uaa36\uaa43\uaa4c\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uabe5\uabe8\uabed\udc00-\udfff\ufb1e\ufe00-\ufe0f\ufe20-\ufe26\uff9e\uff9f]/
    400     function isExtendingChar(ch) { return ch.charCodeAt(0) >= 768 && extendingChars.test(ch) }
    401 
    402 // Returns a number from the range [`0`; `str.length`] unless `pos` is outside that range.
    403     function skipExtendingChars(str, pos, dir) {
    404         while ((dir < 0 ? pos > 0 : pos < str.length) && isExtendingChar(str.charAt(pos))) { pos += dir }
    405         return pos
    406     }
    407 
    408 // Returns the value from the range [`from`; `to`] that satisfies
    409 // `pred` and is closest to `from`. Assumes that at least `to`
    410 // satisfies `pred`. Supports `from` being greater than `to`.
    411     function findFirst(pred, from, to) {
    412         // At any point we are certain `to` satisfies `pred`, don't know
    413         // whether `from` does.
    414         var dir = from > to ? -1 : 1
    415         for (;;) {
    416             if (from == to) { return from }
    417             var midF = (from + to) / 2, mid = dir < 0 ? Math.ceil(midF) : Math.floor(midF)
    418             if (mid == from) { return pred(mid) ? from : to }
    419             if (pred(mid)) { to = mid }
    420             else { from = mid + dir }
    421         }
    422     }
    423 
    424 // The display handles the DOM integration, both for input reading
    425 // and content drawing. It holds references to DOM nodes and
    426 // display-related state.
    427 
    428     function Display(place, doc, input) {
    429         var d = this
    430         this.input = input
    431 
    432         // Covers bottom-right square when both scrollbars are present.
    433         d.scrollbarFiller = elt("div", null, "CodeMirror-scrollbar-filler")
    434         d.scrollbarFiller.setAttribute("cm-not-content", "true")
    435         // Covers bottom of gutter when coverGutterNextToScrollbar is on
    436         // and h scrollbar is present.
    437         d.gutterFiller = elt("div", null, "CodeMirror-gutter-filler")
    438         d.gutterFiller.setAttribute("cm-not-content", "true")
    439         // Will contain the actual code, positioned to cover the viewport.
    440         d.lineDiv = eltP("div", null, "CodeMirror-code")
    441         // Elements are added to these to represent selection and cursors.
    442         d.selectionDiv = elt("div", null, null, "position: relative; z-index: 1")
    443         d.cursorDiv = elt("div", null, "CodeMirror-cursors")
    444         // A visibility: hidden element used to find the size of things.
    445         d.measure = elt("div", null, "CodeMirror-measure")
    446         // When lines outside of the viewport are measured, they are drawn in this.
    447         d.lineMeasure = elt("div", null, "CodeMirror-measure")
    448         // Wraps everything that needs to exist inside the vertically-padded coordinate system
    449         d.lineSpace = eltP("div", [d.measure, d.lineMeasure, d.selectionDiv, d.cursorDiv, d.lineDiv],
    450             null, "position: relative; outline: none")
    451         var lines = eltP("div", [d.lineSpace], "CodeMirror-lines")
    452         // Moved around its parent to cover visible view.
    453         d.mover = elt("div", [lines], null, "position: relative")
    454         // Set to the height of the document, allowing scrolling.
    455         d.sizer = elt("div", [d.mover], "CodeMirror-sizer")
    456         d.sizerWidth = null
    457         // Behavior of elts with overflow: auto and padding is
    458         // inconsistent across browsers. This is used to ensure the
    459         // scrollable area is big enough.
    460         d.heightForcer = elt("div", null, null, "position: absolute; height: " + scrollerGap + "px; width: 1px;")
    461         // Will contain the gutters, if any.
    462         d.gutters = elt("div", null, "CodeMirror-gutters")
    463         d.lineGutter = null
    464         // Actual scrollable element.
    465         d.scroller = elt("div", [d.sizer, d.heightForcer, d.gutters], "CodeMirror-scroll")
    466         d.scroller.setAttribute("tabIndex", "-1")
    467         // The element in which the editor lives.
    468         d.wrapper = elt("div", [d.scrollbarFiller, d.gutterFiller, d.scroller], "CodeMirror")
    469 
    470         // Work around IE7 z-index bug (not perfect, hence IE7 not really being supported)
    471         if (ie && ie_version < 8) { d.gutters.style.zIndex = -1; d.scroller.style.paddingRight = 0 }
    472         if (!webkit && !(gecko && mobile)) { d.scroller.draggable = true }
    473 
    474         if (place) {
    475             if (place.appendChild) { place.appendChild(d.wrapper) }
    476             else { place(d.wrapper) }
    477         }
    478 
    479         // Current rendered range (may be bigger than the view window).
    480         d.viewFrom = d.viewTo = doc.first
    481         d.reportedViewFrom = d.reportedViewTo = doc.first
    482         // Information about the rendered lines.
    483         d.view = []
    484         d.renderedView = null
    485         // Holds info about a single rendered line when it was rendered
    486         // for measurement, while not in view.
    487         d.externalMeasured = null
    488         // Empty space (in pixels) above the view
    489         d.viewOffset = 0
    490         d.lastWrapHeight = d.lastWrapWidth = 0
    491         d.updateLineNumbers = null
    492 
    493         d.nativeBarWidth = d.barHeight = d.barWidth = 0
    494         d.scrollbarsClipped = false
    495 
    496         // Used to only resize the line number gutter when necessary (when
    497         // the amount of lines crosses a boundary that makes its width change)
    498         d.lineNumWidth = d.lineNumInnerWidth = d.lineNumChars = null
    499         // Set to true when a non-horizontal-scrolling line widget is
    500         // added. As an optimization, line widget aligning is skipped when
    501         // this is false.
    502         d.alignWidgets = false
    503 
    504         d.cachedCharWidth = d.cachedTextHeight = d.cachedPaddingH = null
    505 
    506         // Tracks the maximum line length so that the horizontal scrollbar
    507         // can be kept static when scrolling.
    508         d.maxLine = null
    509         d.maxLineLength = 0
    510         d.maxLineChanged = false
    511 
    512         // Used for measuring wheel scrolling granularity
    513         d.wheelDX = d.wheelDY = d.wheelStartX = d.wheelStartY = null
    514 
    515         // True when shift is held down.
    516         d.shift = false
    517 
    518         // Used to track whether anything happened since the context menu
    519         // was opened.
    520         d.selForContextMenu = null
    521 
    522         d.activeTouch = null
    523 
    524         input.init(d)
    525     }
    526 
    527 // Find the line object corresponding to the given line number.
    528     function getLine(doc, n) {
    529         n -= doc.first
    530         if (n < 0 || n >= doc.size) { throw new Error("There is no line " + (n + doc.first) + " in the document.") }
    531         var chunk = doc
    532         while (!chunk.lines) {
    533             for (var i = 0;; ++i) {
    534                 var child = chunk.children[i], sz = child.chunkSize()
    535                 if (n < sz) { chunk = child; break }
    536                 n -= sz
    537             }
    538         }
    539         return chunk.lines[n]
    540     }
    541 
    542 // Get the part of a document between two positions, as an array of
    543 // strings.
    544     function getBetween(doc, start, end) {
    545         var out = [], n = start.line
    546         doc.iter(start.line, end.line + 1, function (line) {
    547             var text = line.text
    548             if (n == end.line) { text = text.slice(0, end.ch) }
    549             if (n == start.line) { text = text.slice(start.ch) }
    550             out.push(text)
    551             ++n
    552         })
    553         return out
    554     }
    555 // Get the lines between from and to, as array of strings.
    556     function getLines(doc, from, to) {
    557         var out = []
    558         doc.iter(from, to, function (line) { out.push(line.text) }) // iter aborts when callback returns truthy value
    559         return out
    560     }
    561 
    562 // Update the height of a line, propagating the height change
    563 // upwards to parent nodes.
    564     function updateLineHeight(line, height) {
    565         var diff = height - line.height
    566         if (diff) { for (var n = line; n; n = n.parent) { n.height += diff } }
    567     }
    568 
    569 // Given a line object, find its line number by walking up through
    570 // its parent links.
    571     function lineNo(line) {
    572         if (line.parent == null) { return null }
    573         var cur = line.parent, no = indexOf(cur.lines, line)
    574         for (var chunk = cur.parent; chunk; cur = chunk, chunk = chunk.parent) {
    575             for (var i = 0;; ++i) {
    576                 if (chunk.children[i] == cur) { break }
    577                 no += chunk.children[i].chunkSize()
    578             }
    579         }
    580         return no + cur.first
    581     }
    582 
    583 // Find the line at the given vertical position, using the height
    584 // information in the document tree.
    585     function lineAtHeight(chunk, h) {
    586         var n = chunk.first
    587         outer: do {
    588             for (var i$1 = 0; i$1 < chunk.children.length; ++i$1) {
    589                 var child = chunk.children[i$1], ch = child.height
    590                 if (h < ch) { chunk = child; continue outer }
    591                 h -= ch
    592                 n += child.chunkSize()
    593             }
    594             return n
    595         } while (!chunk.lines)
    596         var i = 0
    597         for (; i < chunk.lines.length; ++i) {
    598             var line = chunk.lines[i], lh = line.height
    599             if (h < lh) { break }
    600             h -= lh
    601         }
    602         return n + i
    603     }
    604 
    605     function isLine(doc, l) {return l >= doc.first && l < doc.first + doc.size}
    606 
    607     function lineNumberFor(options, i) {
    608         return String(options.lineNumberFormatter(i + options.firstLineNumber))
    609     }
    610 
    611 // A Pos instance represents a position within the text.
    612     function Pos(line, ch, sticky) {
    613         if ( sticky === void 0 ) sticky = null;
    614 
    615         if (!(this instanceof Pos)) { return new Pos(line, ch, sticky) }
    616         this.line = line
    617         this.ch = ch
    618         this.sticky = sticky
    619     }
    620 
    621 // Compare two positions, return 0 if they are the same, a negative
    622 // number when a is less, and a positive number otherwise.
    623     function cmp(a, b) { return a.line - b.line || a.ch - b.ch }
    624 
    625     function equalCursorPos(a, b) { return a.sticky == b.sticky && cmp(a, b) == 0 }
    626 
    627     function copyPos(x) {return Pos(x.line, x.ch)}
    628     function maxPos(a, b) { return cmp(a, b) < 0 ? b : a }
    629     function minPos(a, b) { return cmp(a, b) < 0 ? a : b }
    630 
    631 // Most of the external API clips given positions to make sure they
    632 // actually exist within the document.
    633     function clipLine(doc, n) {return Math.max(doc.first, Math.min(n, doc.first + doc.size - 1))}
    634     function clipPos(doc, pos) {
    635         if (pos.line < doc.first) { return Pos(doc.first, 0) }
    636         var last = doc.first + doc.size - 1
    637         if (pos.line > last) { return Pos(last, getLine(doc, last).text.length) }
    638         return clipToLen(pos, getLine(doc, pos.line).text.length)
    639     }
    640     function clipToLen(pos, linelen) {
    641         var ch = pos.ch
    642         if (ch == null || ch > linelen) { return Pos(pos.line, linelen) }
    643         else if (ch < 0) { return Pos(pos.line, 0) }
    644         else { return pos }
    645     }
    646     function clipPosArray(doc, array) {
    647         var out = []
    648         for (var i = 0; i < array.length; i++) { out[i] = clipPos(doc, array[i]) }
    649         return out
    650     }
    651 
    652 // Optimize some code when these features are not used.
    653     var sawReadOnlySpans = false;
    654     var sawCollapsedSpans = false;
    655     function seeReadOnlySpans() {
    656         sawReadOnlySpans = true
    657     }
    658 
    659     function seeCollapsedSpans() {
    660         sawCollapsedSpans = true
    661     }
    662 
    663 // TEXTMARKER SPANS
    664 
    665     function MarkedSpan(marker, from, to) {
    666         this.marker = marker
    667         this.from = from; this.to = to
    668     }
    669 
    670 // Search an array of spans for a span matching the given marker.
    671     function getMarkedSpanFor(spans, marker) {
    672         if (spans) { for (var i = 0; i < spans.length; ++i) {
    673             var span = spans[i]
    674             if (span.marker == marker) { return span }
    675         } }
    676     }
    677 // Remove a span from an array, returning undefined if no spans are
    678 // left (we don't store arrays for lines without spans).
    679     function removeMarkedSpan(spans, span) {
    680         var r
    681         for (var i = 0; i < spans.length; ++i)
    682         { if (spans[i] != span) { (r || (r = [])).push(spans[i]) } }
    683         return r
    684     }
    685 // Add a span to a line.
    686     function addMarkedSpan(line, span) {
    687         line.markedSpans = line.markedSpans ? line.markedSpans.concat([span]) : [span]
    688         span.marker.attachLine(line)
    689     }
    690 
    691 // Used for the algorithm that adjusts markers for a change in the
    692 // document. These functions cut an array of spans at a given
    693 // character position, returning an array of remaining chunks (or
    694 // undefined if nothing remains).
    695     function markedSpansBefore(old, startCh, isInsert) {
    696         var nw
    697         if (old) { for (var i = 0; i < old.length; ++i) {
    698             var span = old[i], marker = span.marker
    699             var startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= startCh : span.from < startCh)
    700             if (startsBefore || span.from == startCh && marker.type == "bookmark" && (!isInsert || !span.marker.insertLeft)) {
    701                 var endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= startCh : span.to > startCh)
    702                 ;(nw || (nw = [])).push(new MarkedSpan(marker, span.from, endsAfter ? null : span.to))
    703             }
    704         } }
    705         return nw
    706     }
    707     function markedSpansAfter(old, endCh, isInsert) {
    708         var nw
    709         if (old) { for (var i = 0; i < old.length; ++i) {
    710             var span = old[i], marker = span.marker
    711             var endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= endCh : span.to > endCh)
    712             if (endsAfter || span.from == endCh && marker.type == "bookmark" && (!isInsert || span.marker.insertLeft)) {
    713                 var startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= endCh : span.from < endCh)
    714                 ;(nw || (nw = [])).push(new MarkedSpan(marker, startsBefore ? null : span.from - endCh,
    715                     span.to == null ? null : span.to - endCh))
    716             }
    717         } }
    718         return nw
    719     }
    720 
    721 // Given a change object, compute the new set of marker spans that
    722 // cover the line in which the change took place. Removes spans
    723 // entirely within the change, reconnects spans belonging to the
    724 // same marker that appear on both sides of the change, and cuts off
    725 // spans partially within the change. Returns an array of span
    726 // arrays with one element for each line in (after) the change.
    727     function stretchSpansOverChange(doc, change) {
    728         if (change.full) { return null }
    729         var oldFirst = isLine(doc, change.from.line) && getLine(doc, change.from.line).markedSpans
    730         var oldLast = isLine(doc, change.to.line) && getLine(doc, change.to.line).markedSpans
    731         if (!oldFirst && !oldLast) { return null }
    732 
    733         var startCh = change.from.ch, endCh = change.to.ch, isInsert = cmp(change.from, change.to) == 0
    734         // Get the spans that 'stick out' on both sides
    735         var first = markedSpansBefore(oldFirst, startCh, isInsert)
    736         var last = markedSpansAfter(oldLast, endCh, isInsert)
    737 
    738         // Next, merge those two ends
    739         var sameLine = change.text.length == 1, offset = lst(change.text).length + (sameLine ? startCh : 0)
    740         if (first) {
    741             // Fix up .to properties of first
    742             for (var i = 0; i < first.length; ++i) {
    743                 var span = first[i]
    744                 if (span.to == null) {
    745                     var found = getMarkedSpanFor(last, span.marker)
    746                     if (!found) { span.to = startCh }
    747                     else if (sameLine) { span.to = found.to == null ? null : found.to + offset }
    748                 }
    749             }
    750         }
    751         if (last) {
    752             // Fix up .from in last (or move them into first in case of sameLine)
    753             for (var i$1 = 0; i$1 < last.length; ++i$1) {
    754                 var span$1 = last[i$1]
    755                 if (span$1.to != null) { span$1.to += offset }
    756                 if (span$1.from == null) {
    757                     var found$1 = getMarkedSpanFor(first, span$1.marker)
    758                     if (!found$1) {
    759                         span$1.from = offset
    760                         if (sameLine) { (first || (first = [])).push(span$1) }
    761                     }
    762                 } else {
    763                     span$1.from += offset
    764                     if (sameLine) { (first || (first = [])).push(span$1) }
    765                 }
    766             }
    767         }
    768         // Make sure we didn't create any zero-length spans
    769         if (first) { first = clearEmptySpans(first) }
    770         if (last && last != first) { last = clearEmptySpans(last) }
    771 
    772         var newMarkers = [first]
    773         if (!sameLine) {
    774             // Fill gap with whole-line-spans
    775             var gap = change.text.length - 2, gapMarkers
    776             if (gap > 0 && first)
    777             { for (var i$2 = 0; i$2 < first.length; ++i$2)
    778             { if (first[i$2].to == null)
    779             { (gapMarkers || (gapMarkers = [])).push(new MarkedSpan(first[i$2].marker, null, null)) } } }
    780             for (var i$3 = 0; i$3 < gap; ++i$3)
    781             { newMarkers.push(gapMarkers) }
    782             newMarkers.push(last)
    783         }
    784         return newMarkers
    785     }
    786 
    787 // Remove spans that are empty and don't have a clearWhenEmpty
    788 // option of false.
    789     function clearEmptySpans(spans) {
    790         for (var i = 0; i < spans.length; ++i) {
    791             var span = spans[i]
    792             if (span.from != null && span.from == span.to && span.marker.clearWhenEmpty !== false)
    793             { spans.splice(i--, 1) }
    794         }
    795         if (!spans.length) { return null }
    796         return spans
    797     }
    798 
    799 // Used to 'clip' out readOnly ranges when making a change.
    800     function removeReadOnlyRanges(doc, from, to) {
    801         var markers = null
    802         doc.iter(from.line, to.line + 1, function (line) {
    803             if (line.markedSpans) { for (var i = 0; i < line.markedSpans.length; ++i) {
    804                 var mark = line.markedSpans[i].marker
    805                 if (mark.readOnly && (!markers || indexOf(markers, mark) == -1))
    806                 { (markers || (markers = [])).push(mark) }
    807             } }
    808         })
    809         if (!markers) { return null }
    810         var parts = [{from: from, to: to}]
    811         for (var i = 0; i < markers.length; ++i) {
    812             var mk = markers[i], m = mk.find(0)
    813             for (var j = 0; j < parts.length; ++j) {
    814                 var p = parts[j]
    815                 if (cmp(p.to, m.from) < 0 || cmp(p.from, m.to) > 0) { continue }
    816                 var newParts = [j, 1], dfrom = cmp(p.from, m.from), dto = cmp(p.to, m.to)
    817                 if (dfrom < 0 || !mk.inclusiveLeft && !dfrom)
    818                 { newParts.push({from: p.from, to: m.from}) }
    819                 if (dto > 0 || !mk.inclusiveRight && !dto)
    820                 { newParts.push({from: m.to, to: p.to}) }
    821                 parts.splice.apply(parts, newParts)
    822                 j += newParts.length - 3
    823             }
    824         }
    825         return parts
    826     }
    827 
    828 // Connect or disconnect spans from a line.
    829     function detachMarkedSpans(line) {
    830         var spans = line.markedSpans
    831         if (!spans) { return }
    832         for (var i = 0; i < spans.length; ++i)
    833         { spans[i].marker.detachLine(line) }
    834         line.markedSpans = null
    835     }
    836     function attachMarkedSpans(line, spans) {
    837         if (!spans) { return }
    838         for (var i = 0; i < spans.length; ++i)
    839         { spans[i].marker.attachLine(line) }
    840         line.markedSpans = spans
    841     }
    842 
    843 // Helpers used when computing which overlapping collapsed span
    844 // counts as the larger one.
    845     function extraLeft(marker) { return marker.inclusiveLeft ? -1 : 0 }
    846     function extraRight(marker) { return marker.inclusiveRight ? 1 : 0 }
    847 
    848 // Returns a number indicating which of two overlapping collapsed
    849 // spans is larger (and thus includes the other). Falls back to
    850 // comparing ids when the spans cover exactly the same range.
    851     function compareCollapsedMarkers(a, b) {
    852         var lenDiff = a.lines.length - b.lines.length
    853         if (lenDiff != 0) { return lenDiff }
    854         var aPos = a.find(), bPos = b.find()
    855         var fromCmp = cmp(aPos.from, bPos.from) || extraLeft(a) - extraLeft(b)
    856         if (fromCmp) { return -fromCmp }
    857         var toCmp = cmp(aPos.to, bPos.to) || extraRight(a) - extraRight(b)
    858         if (toCmp) { return toCmp }
    859         return b.id - a.id
    860     }
    861 
    862 // Find out whether a line ends or starts in a collapsed span. If
    863 // so, return the marker for that span.
    864     function collapsedSpanAtSide(line, start) {
    865         var sps = sawCollapsedSpans && line.markedSpans, found
    866         if (sps) { for (var sp = (void 0), i = 0; i < sps.length; ++i) {
    867             sp = sps[i]
    868             if (sp.marker.collapsed && (start ? sp.from : sp.to) == null &&
    869                 (!found || compareCollapsedMarkers(found, sp.marker) < 0))
    870             { found = sp.marker }
    871         } }
    872         return found
    873     }
    874     function collapsedSpanAtStart(line) { return collapsedSpanAtSide(line, true) }
    875     function collapsedSpanAtEnd(line) { return collapsedSpanAtSide(line, false) }
    876 
    877 // Test whether there exists a collapsed span that partially
    878 // overlaps (covers the start or end, but not both) of a new span.
    879 // Such overlap is not allowed.
    880     function conflictingCollapsedRange(doc, lineNo, from, to, marker) {
    881         var line = getLine(doc, lineNo)
    882         var sps = sawCollapsedSpans && line.markedSpans
    883         if (sps) { for (var i = 0; i < sps.length; ++i) {
    884             var sp = sps[i]
    885             if (!sp.marker.collapsed) { continue }
    886             var found = sp.marker.find(0)
    887             var fromCmp = cmp(found.from, from) || extraLeft(sp.marker) - extraLeft(marker)
    888             var toCmp = cmp(found.to, to) || extraRight(sp.marker) - extraRight(marker)
    889             if (fromCmp >= 0 && toCmp <= 0 || fromCmp <= 0 && toCmp >= 0) { continue }
    890             if (fromCmp <= 0 && (sp.marker.inclusiveRight && marker.inclusiveLeft ? cmp(found.to, from) >= 0 : cmp(found.to, from) > 0) ||
    891                 fromCmp >= 0 && (sp.marker.inclusiveRight && marker.inclusiveLeft ? cmp(found.from, to) <= 0 : cmp(found.from, to) < 0))
    892             { return true }
    893         } }
    894     }
    895 
    896 // A visual line is a line as drawn on the screen. Folding, for
    897 // example, can cause multiple logical lines to appear on the same
    898 // visual line. This finds the start of the visual line that the
    899 // given line is part of (usually that is the line itself).
    900     function visualLine(line) {
    901         var merged
    902         while (merged = collapsedSpanAtStart(line))
    903         { line = merged.find(-1, true).line }
    904         return line
    905     }
    906 
    907     function visualLineEnd(line) {
    908         var merged
    909         while (merged = collapsedSpanAtEnd(line))
    910         { line = merged.find(1, true).line }
    911         return line
    912     }
    913 
    914 // Returns an array of logical lines that continue the visual line
    915 // started by the argument, or undefined if there are no such lines.
    916     function visualLineContinued(line) {
    917         var merged, lines
    918         while (merged = collapsedSpanAtEnd(line)) {
    919             line = merged.find(1, true).line
    920             ;(lines || (lines = [])).push(line)
    921         }
    922         return lines
    923     }
    924 
    925 // Get the line number of the start of the visual line that the
    926 // given line number is part of.
    927     function visualLineNo(doc, lineN) {
    928         var line = getLine(doc, lineN), vis = visualLine(line)
    929         if (line == vis) { return lineN }
    930         return lineNo(vis)
    931     }
    932 
    933 // Get the line number of the start of the next visual line after
    934 // the given line.
    935     function visualLineEndNo(doc, lineN) {
    936         if (lineN > doc.lastLine()) { return lineN }
    937         var line = getLine(doc, lineN), merged
    938         if (!lineIsHidden(doc, line)) { return lineN }
    939         while (merged = collapsedSpanAtEnd(line))
    940         { line = merged.find(1, true).line }
    941         return lineNo(line) + 1
    942     }
    943 
    944 // Compute whether a line is hidden. Lines count as hidden when they
    945 // are part of a visual line that starts with another line, or when
    946 // they are entirely covered by collapsed, non-widget span.
    947     function lineIsHidden(doc, line) {
    948         var sps = sawCollapsedSpans && line.markedSpans
    949         if (sps) { for (var sp = (void 0), i = 0; i < sps.length; ++i) {
    950             sp = sps[i]
    951             if (!sp.marker.collapsed) { continue }
    952             if (sp.from == null) { return true }
    953             if (sp.marker.widgetNode) { continue }
    954             if (sp.from == 0 && sp.marker.inclusiveLeft && lineIsHiddenInner(doc, line, sp))
    955             { return true }
    956         } }
    957     }
    958     function lineIsHiddenInner(doc, line, span) {
    959         if (span.to == null) {
    960             var end = span.marker.find(1, true)
    961             return lineIsHiddenInner(doc, end.line, getMarkedSpanFor(end.line.markedSpans, span.marker))
    962         }
    963         if (span.marker.inclusiveRight && span.to == line.text.length)
    964         { return true }
    965         for (var sp = (void 0), i = 0; i < line.markedSpans.length; ++i) {
    966             sp = line.markedSpans[i]
    967             if (sp.marker.collapsed && !sp.marker.widgetNode && sp.from == span.to &&
    968                 (sp.to == null || sp.to != span.from) &&
    969                 (sp.marker.inclusiveLeft || span.marker.inclusiveRight) &&
    970                 lineIsHiddenInner(doc, line, sp)) { return true }
    971         }
    972     }
    973 
    974 // Find the height above the given line.
    975     function heightAtLine(lineObj) {
    976         lineObj = visualLine(lineObj)
    977 
    978         var h = 0, chunk = lineObj.parent
    979         for (var i = 0; i < chunk.lines.length; ++i) {
    980             var line = chunk.lines[i]
    981             if (line == lineObj) { break }
    982             else { h += line.height }
    983         }
    984         for (var p = chunk.parent; p; chunk = p, p = chunk.parent) {
    985             for (var i$1 = 0; i$1 < p.children.length; ++i$1) {
    986                 var cur = p.children[i$1]
    987                 if (cur == chunk) { break }
    988                 else { h += cur.height }
    989             }
    990         }
    991         return h
    992     }
    993 
    994 // Compute the character length of a line, taking into account
    995 // collapsed ranges (see markText) that might hide parts, and join
    996 // other lines onto it.
    997     function lineLength(line) {
    998         if (line.height == 0) { return 0 }
    999         var len = line.text.length, merged, cur = line
   1000         while (merged = collapsedSpanAtStart(cur)) {
   1001             var found = merged.find(0, true)
   1002             cur = found.from.line
   1003             len += found.from.ch - found.to.ch
   1004         }
   1005         cur = line
   1006         while (merged = collapsedSpanAtEnd(cur)) {
   1007             var found$1 = merged.find(0, true)
   1008             len -= cur.text.length - found$1.from.ch
   1009             cur = found$1.to.line
   1010             len += cur.text.length - found$1.to.ch
   1011         }
   1012         return len
   1013     }
   1014 
   1015 // Find the longest line in the document.
   1016     function findMaxLine(cm) {
   1017         var d = cm.display, doc = cm.doc
   1018         d.maxLine = getLine(doc, doc.first)
   1019         d.maxLineLength = lineLength(d.maxLine)
   1020         d.maxLineChanged = true
   1021         doc.iter(function (line) {
   1022             var len = lineLength(line)
   1023             if (len > d.maxLineLength) {
   1024                 d.maxLineLength = len
   1025                 d.maxLine = line
   1026             }
   1027         })
   1028     }
   1029 
   1030 // BIDI HELPERS
   1031 
   1032     function iterateBidiSections(order, from, to, f) {
   1033         if (!order) { return f(from, to, "ltr", 0) }
   1034         var found = false
   1035         for (var i = 0; i < order.length; ++i) {
   1036             var part = order[i]
   1037             if (part.from < to && part.to > from || from == to && part.to == from) {
   1038                 f(Math.max(part.from, from), Math.min(part.to, to), part.level == 1 ? "rtl" : "ltr", i)
   1039                 found = true
   1040             }
   1041         }
   1042         if (!found) { f(from, to, "ltr") }
   1043     }
   1044 
   1045     var bidiOther = null
   1046     function getBidiPartAt(order, ch, sticky) {
   1047         var found
   1048         bidiOther = null
   1049         for (var i = 0; i < order.length; ++i) {
   1050             var cur = order[i]
   1051             if (cur.from < ch && cur.to > ch) { return i }
   1052             if (cur.to == ch) {
   1053                 if (cur.from != cur.to && sticky == "before") { found = i }
   1054                 else { bidiOther = i }
   1055             }
   1056             if (cur.from == ch) {
   1057                 if (cur.from != cur.to && sticky != "before") { found = i }
   1058                 else { bidiOther = i }
   1059             }
   1060         }
   1061         return found != null ? found : bidiOther
   1062     }
   1063 
   1064 // Bidirectional ordering algorithm
   1065 // See http://unicode.org/reports/tr9/tr9-13.html for the algorithm
   1066 // that this (partially) implements.
   1067 
   1068 // One-char codes used for character types:
   1069 // L (L):   Left-to-Right
   1070 // R (R):   Right-to-Left
   1071 // r (AL):  Right-to-Left Arabic
   1072 // 1 (EN):  European Number
   1073 // + (ES):  European Number Separator
   1074 // % (ET):  European Number Terminator
   1075 // n (AN):  Arabic Number
   1076 // , (CS):  Common Number Separator
   1077 // m (NSM): Non-Spacing Mark
   1078 // b (BN):  Boundary Neutral
   1079 // s (B):   Paragraph Separator
   1080 // t (S):   Segment Separator
   1081 // w (WS):  Whitespace
   1082 // N (ON):  Other Neutrals
   1083 
   1084 // Returns null if characters are ordered as they appear
   1085 // (left-to-right), or an array of sections ({from, to, level}
   1086 // objects) in the order in which they occur visually.
   1087     var bidiOrdering = (function() {
   1088         // Character types for codepoints 0 to 0xff
   1089         var lowTypes = "bbbbbbbbbtstwsbbbbbbbbbbbbbbssstwNN%%%NNNNNN,N,N1111111111NNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNbbbbbbsbbbbbbbbbbbbbbbbbbbbbbbbbb,N%%%%NNNNLNNNNN%%11NLNNN1LNNNNNLLLLLLLLLLLLLLLLLLLLLLLNLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLN"
   1090         // Character types for codepoints 0x600 to 0x6f9
   1091         var arabicTypes = "nnnnnnNNr%%r,rNNmmmmmmmmmmmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmmmmmmmmmmmmmmmnnnnnnnnnn%nnrrrmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmnNmmmmmmrrmmNmmmmrr1111111111"
   1092         function charType(code) {
   1093             if (code <= 0xf7) { return lowTypes.charAt(code) }
   1094             else if (0x590 <= code && code <= 0x5f4) { return "R" }
   1095             else if (0x600 <= code && code <= 0x6f9) { return arabicTypes.charAt(code - 0x600) }
   1096             else if (0x6ee <= code && code <= 0x8ac) { return "r" }
   1097             else if (0x2000 <= code && code <= 0x200b) { return "w" }
   1098             else if (code == 0x200c) { return "b" }
   1099             else { return "L" }
   1100         }
   1101 
   1102         var bidiRE = /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac]/
   1103         var isNeutral = /[stwN]/, isStrong = /[LRr]/, countsAsLeft = /[Lb1n]/, countsAsNum = /[1n]/
   1104 
   1105         function BidiSpan(level, from, to) {
   1106             this.level = level
   1107             this.from = from; this.to = to
   1108         }
   1109 
   1110         return function(str, direction) {
   1111             var outerType = direction == "ltr" ? "L" : "R"
   1112 
   1113             if (str.length == 0 || direction == "ltr" && !bidiRE.test(str)) { return false }
   1114             var len = str.length, types = []
   1115             for (var i = 0; i < len; ++i)
   1116             { types.push(charType(str.charCodeAt(i))) }
   1117 
   1118             // W1. Examine each non-spacing mark (NSM) in the level run, and
   1119             // change the type of the NSM to the type of the previous
   1120             // character. If the NSM is at the start of the level run, it will
   1121             // get the type of sor.
   1122             for (var i$1 = 0, prev = outerType; i$1 < len; ++i$1) {
   1123                 var type = types[i$1]
   1124                 if (type == "m") { types[i$1] = prev }
   1125                 else { prev = type }
   1126             }
   1127 
   1128             // W2. Search backwards from each instance of a European number
   1129             // until the first strong type (R, L, AL, or sor) is found. If an
   1130             // AL is found, change the type of the European number to Arabic
   1131             // number.
   1132             // W3. Change all ALs to R.
   1133             for (var i$2 = 0, cur = outerType; i$2 < len; ++i$2) {
   1134                 var type$1 = types[i$2]
   1135                 if (type$1 == "1" && cur == "r") { types[i$2] = "n" }
   1136                 else if (isStrong.test(type$1)) { cur = type$1; if (type$1 == "r") { types[i$2] = "R" } }
   1137             }
   1138 
   1139             // W4. A single European separator between two European numbers
   1140             // changes to a European number. A single common separator between
   1141             // two numbers of the same type changes to that type.
   1142             for (var i$3 = 1, prev$1 = types[0]; i$3 < len - 1; ++i$3) {
   1143                 var type$2 = types[i$3]
   1144                 if (type$2 == "+" && prev$1 == "1" && types[i$3+1] == "1") { types[i$3] = "1" }
   1145                 else if (type$2 == "," && prev$1 == types[i$3+1] &&
   1146                     (prev$1 == "1" || prev$1 == "n")) { types[i$3] = prev$1 }
   1147                 prev$1 = type$2
   1148             }
   1149 
   1150             // W5. A sequence of European terminators adjacent to European
   1151             // numbers changes to all European numbers.
   1152             // W6. Otherwise, separators and terminators change to Other
   1153             // Neutral.
   1154             for (var i$4 = 0; i$4 < len; ++i$4) {
   1155                 var type$3 = types[i$4]
   1156                 if (type$3 == ",") { types[i$4] = "N" }
   1157                 else if (type$3 == "%") {
   1158                     var end = (void 0)
   1159                     for (end = i$4 + 1; end < len && types[end] == "%"; ++end) {}
   1160                     var replace = (i$4 && types[i$4-1] == "!") || (end < len && types[end] == "1") ? "1" : "N"
   1161                     for (var j = i$4; j < end; ++j) { types[j] = replace }
   1162                     i$4 = end - 1
   1163                 }
   1164             }
   1165 
   1166             // W7. Search backwards from each instance of a European number
   1167             // until the first strong type (R, L, or sor) is found. If an L is
   1168             // found, then change the type of the European number to L.
   1169             for (var i$5 = 0, cur$1 = outerType; i$5 < len; ++i$5) {
   1170                 var type$4 = types[i$5]
   1171                 if (cur$1 == "L" && type$4 == "1") { types[i$5] = "L" }
   1172                 else if (isStrong.test(type$4)) { cur$1 = type$4 }
   1173             }
   1174 
   1175             // N1. A sequence of neutrals takes the direction of the
   1176             // surrounding strong text if the text on both sides has the same
   1177             // direction. European and Arabic numbers act as if they were R in
   1178             // terms of their influence on neutrals. Start-of-level-run (sor)
   1179             // and end-of-level-run (eor) are used at level run boundaries.
   1180             // N2. Any remaining neutrals take the embedding direction.
   1181             for (var i$6 = 0; i$6 < len; ++i$6) {
   1182                 if (isNeutral.test(types[i$6])) {
   1183                     var end$1 = (void 0)
   1184                     for (end$1 = i$6 + 1; end$1 < len && isNeutral.test(types[end$1]); ++end$1) {}
   1185                     var before = (i$6 ? types[i$6-1] : outerType) == "L"
   1186                     var after = (end$1 < len ? types[end$1] : outerType) == "L"
   1187                     var replace$1 = before == after ? (before ? "L" : "R") : outerType
   1188                     for (var j$1 = i$6; j$1 < end$1; ++j$1) { types[j$1] = replace$1 }
   1189                     i$6 = end$1 - 1
   1190                 }
   1191             }
   1192 
   1193             // Here we depart from the documented algorithm, in order to avoid
   1194             // building up an actual levels array. Since there are only three
   1195             // levels (0, 1, 2) in an implementation that doesn't take
   1196             // explicit embedding into account, we can build up the order on
   1197             // the fly, without following the level-based algorithm.
   1198             var order = [], m
   1199             for (var i$7 = 0; i$7 < len;) {
   1200                 if (countsAsLeft.test(types[i$7])) {
   1201                     var start = i$7
   1202                     for (++i$7; i$7 < len && countsAsLeft.test(types[i$7]); ++i$7) {}
   1203                     order.push(new BidiSpan(0, start, i$7))
   1204                 } else {
   1205                     var pos = i$7, at = order.length
   1206                     for (++i$7; i$7 < len && types[i$7] != "L"; ++i$7) {}
   1207                     for (var j$2 = pos; j$2 < i$7;) {
   1208                         if (countsAsNum.test(types[j$2])) {
   1209                             if (pos < j$2) { order.splice(at, 0, new BidiSpan(1, pos, j$2)) }
   1210                             var nstart = j$2
   1211                             for (++j$2; j$2 < i$7 && countsAsNum.test(types[j$2]); ++j$2) {}
   1212                             order.splice(at, 0, new BidiSpan(2, nstart, j$2))
   1213                             pos = j$2
   1214                         } else { ++j$2 }
   1215                     }
   1216                     if (pos < i$7) { order.splice(at, 0, new BidiSpan(1, pos, i$7)) }
   1217                 }
   1218             }
   1219             if (direction == "ltr") {
   1220                 if (order[0].level == 1 && (m = str.match(/^\s+/))) {
   1221                     order[0].from = m[0].length
   1222                     order.unshift(new BidiSpan(0, 0, m[0].length))
   1223                 }
   1224                 if (lst(order).level == 1 && (m = str.match(/\s+$/))) {
   1225                     lst(order).to -= m[0].length
   1226                     order.push(new BidiSpan(0, len - m[0].length, len))
   1227                 }
   1228             }
   1229 
   1230             return direction == "rtl" ? order.reverse() : order
   1231         }
   1232     })()
   1233 
   1234 // Get the bidi ordering for the given line (and cache it). Returns
   1235 // false for lines that are fully left-to-right, and an array of
   1236 // BidiSpan objects otherwise.
   1237     function getOrder(line, direction) {
   1238         var order = line.order
   1239         if (order == null) { order = line.order = bidiOrdering(line.text, direction) }
   1240         return order
   1241     }
   1242 
   1243 // EVENT HANDLING
   1244 
   1245 // Lightweight event framework. on/off also work on DOM nodes,
   1246 // registering native DOM handlers.
   1247 
   1248     var noHandlers = []
   1249 
   1250     var on = function(emitter, type, f) {
   1251         if (emitter.addEventListener) {
   1252             emitter.addEventListener(type, f, false)
   1253         } else if (emitter.attachEvent) {
   1254             emitter.attachEvent("on" + type, f)
   1255         } else {
   1256             var map = emitter._handlers || (emitter._handlers = {})
   1257             map[type] = (map[type] || noHandlers).concat(f)
   1258         }
   1259     }
   1260 
   1261     function getHandlers(emitter, type) {
   1262         return emitter._handlers && emitter._handlers[type] || noHandlers
   1263     }
   1264 
   1265     function off(emitter, type, f) {
   1266         if (emitter.removeEventListener) {
   1267             emitter.removeEventListener(type, f, false)
   1268         } else if (emitter.detachEvent) {
   1269             emitter.detachEvent("on" + type, f)
   1270         } else {
   1271             var map = emitter._handlers, arr = map && map[type]
   1272             if (arr) {
   1273                 var index = indexOf(arr, f)
   1274                 if (index > -1)
   1275                 { map[type] = arr.slice(0, index).concat(arr.slice(index + 1)) }
   1276             }
   1277         }
   1278     }
   1279 
   1280     function signal(emitter, type /*, values...*/) {
   1281         var handlers = getHandlers(emitter, type)
   1282         if (!handlers.length) { return }
   1283         var args = Array.prototype.slice.call(arguments, 2)
   1284         for (var i = 0; i < handlers.length; ++i) { handlers[i].apply(null, args) }
   1285     }
   1286 
   1287 // The DOM events that CodeMirror handles can be overridden by
   1288 // registering a (non-DOM) handler on the editor for the event name,
   1289 // and preventDefault-ing the event in that handler.
   1290     function signalDOMEvent(cm, e, override) {
   1291         if (typeof e == "string")
   1292         { e = {type: e, preventDefault: function() { this.defaultPrevented = true }} }
   1293         signal(cm, override || e.type, cm, e)
   1294         return e_defaultPrevented(e) || e.codemirrorIgnore
   1295     }
   1296 
   1297     function signalCursorActivity(cm) {
   1298         var arr = cm._handlers && cm._handlers.cursorActivity
   1299         if (!arr) { return }
   1300         var set = cm.curOp.cursorActivityHandlers || (cm.curOp.cursorActivityHandlers = [])
   1301         for (var i = 0; i < arr.length; ++i) { if (indexOf(set, arr[i]) == -1)
   1302         { set.push(arr[i]) } }
   1303     }
   1304 
   1305     function hasHandler(emitter, type) {
   1306         return getHandlers(emitter, type).length > 0
   1307     }
   1308 
   1309 // Add on and off methods to a constructor's prototype, to make
   1310 // registering events on such objects more convenient.
   1311     function eventMixin(ctor) {
   1312         ctor.prototype.on = function(type, f) {on(this, type, f)}
   1313         ctor.prototype.off = function(type, f) {off(this, type, f)}
   1314     }
   1315 
   1316 // Due to the fact that we still support jurassic IE versions, some
   1317 // compatibility wrappers are needed.
   1318 
   1319     function e_preventDefault(e) {
   1320         if (e.preventDefault) { e.preventDefault() }
   1321         else { e.returnValue = false }
   1322     }
   1323     function e_stopPropagation(e) {
   1324         if (e.stopPropagation) { e.stopPropagation() }
   1325         else { e.cancelBubble = true }
   1326     }
   1327     function e_defaultPrevented(e) {
   1328         return e.defaultPrevented != null ? e.defaultPrevented : e.returnValue == false
   1329     }
   1330     function e_stop(e) {e_preventDefault(e); e_stopPropagation(e)}
   1331 
   1332     function e_target(e) {return e.target || e.srcElement}
   1333     function e_button(e) {
   1334         var b = e.which
   1335         if (b == null) {
   1336             if (e.button & 1) { b = 1 }
   1337             else if (e.button & 2) { b = 3 }
   1338             else if (e.button & 4) { b = 2 }
   1339         }
   1340         if (mac && e.ctrlKey && b == 1) { b = 3 }
   1341         return b
   1342     }
   1343 
   1344 // Detect drag-and-drop
   1345     var dragAndDrop = function() {
   1346         // There is *some* kind of drag-and-drop support in IE6-8, but I
   1347         // couldn't get it to work yet.
   1348         if (ie && ie_version < 9) { return false }
   1349         var div = elt('div')
   1350         return "draggable" in div || "dragDrop" in div
   1351     }()
   1352 
   1353     var zwspSupported
   1354     function zeroWidthElement(measure) {
   1355         if (zwspSupported == null) {
   1356             var test = elt("span", "\u200b")
   1357             removeChildrenAndAdd(measure, elt("span", [test, document.createTextNode("x")]))
   1358             if (measure.firstChild.offsetHeight != 0)
   1359             { zwspSupported = test.offsetWidth <= 1 && test.offsetHeight > 2 && !(ie && ie_version < 8) }
   1360         }
   1361         var node = zwspSupported ? elt("span", "\u200b") :
   1362             elt("span", "\u00a0", null, "display: inline-block; width: 1px; margin-right: -1px")
   1363         node.setAttribute("cm-text", "")
   1364         return node
   1365     }
   1366 
   1367 // Feature-detect IE's crummy client rect reporting for bidi text
   1368     var badBidiRects
   1369     function hasBadBidiRects(measure) {
   1370         if (badBidiRects != null) { return badBidiRects }
   1371         var txt = removeChildrenAndAdd(measure, document.createTextNode("A\u062eA"))
   1372         var r0 = range(txt, 0, 1).getBoundingClientRect()
   1373         var r1 = range(txt, 1, 2).getBoundingClientRect()
   1374         removeChildren(measure)
   1375         if (!r0 || r0.left == r0.right) { return false } // Safari returns null in some cases (#2780)
   1376         return badBidiRects = (r1.right - r0.right < 3)
   1377     }
   1378 
   1379 // See if "".split is the broken IE version, if so, provide an
   1380 // alternative way to split lines.
   1381     var splitLinesAuto = "\n\nb".split(/\n/).length != 3 ? function (string) {
   1382         var pos = 0, result = [], l = string.length
   1383         while (pos <= l) {
   1384             var nl = string.indexOf("\n", pos)
   1385             if (nl == -1) { nl = string.length }
   1386             var line = string.slice(pos, string.charAt(nl - 1) == "\r" ? nl - 1 : nl)
   1387             var rt = line.indexOf("\r")
   1388             if (rt != -1) {
   1389                 result.push(line.slice(0, rt))
   1390                 pos += rt + 1
   1391             } else {
   1392                 result.push(line)
   1393                 pos = nl + 1
   1394             }
   1395         }
   1396         return result
   1397     } : function (string) { return string.split(/\r\n?|\n/); }
   1398 
   1399     var hasSelection = window.getSelection ? function (te) {
   1400         try { return te.selectionStart != te.selectionEnd }
   1401         catch(e) { return false }
   1402     } : function (te) {
   1403         var range
   1404         try {range = te.ownerDocument.selection.createRange()}
   1405         catch(e) {}
   1406         if (!range || range.parentElement() != te) { return false }
   1407         return range.compareEndPoints("StartToEnd", range) != 0
   1408     }
   1409 
   1410     var hasCopyEvent = (function () {
   1411         var e = elt("div")
   1412         if ("oncopy" in e) { return true }
   1413         e.setAttribute("oncopy", "return;")
   1414         return typeof e.oncopy == "function"
   1415     })()
   1416 
   1417     var badZoomedRects = null
   1418     function hasBadZoomedRects(measure) {
   1419         if (badZoomedRects != null) { return badZoomedRects }
   1420         var node = removeChildrenAndAdd(measure, elt("span", "x"))
   1421         var normal = node.getBoundingClientRect()
   1422         var fromRange = range(node, 0, 1).getBoundingClientRect()
   1423         return badZoomedRects = Math.abs(normal.left - fromRange.left) > 1
   1424     }
   1425 
   1426     var modes = {};
   1427     var mimeModes = {};
   1428 // Extra arguments are stored as the mode's dependencies, which is
   1429 // used by (legacy) mechanisms like loadmode.js to automatically
   1430 // load a mode. (Preferred mechanism is the require/define calls.)
   1431     function defineMode(name, mode) {
   1432         if (arguments.length > 2)
   1433         { mode.dependencies = Array.prototype.slice.call(arguments, 2) }
   1434         modes[name] = mode
   1435     }
   1436 
   1437     function defineMIME(mime, spec) {
   1438         mimeModes[mime] = spec
   1439     }
   1440 
   1441 // Given a MIME type, a {name, ...options} config object, or a name
   1442 // string, return a mode config object.
   1443     function resolveMode(spec) {
   1444         if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
   1445             spec = mimeModes[spec]
   1446         } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
   1447             var found = mimeModes[spec.name]
   1448             if (typeof found == "string") { found = {name: found} }
   1449             spec = createObj(found, spec)
   1450             spec.name = found.name
   1451         } else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+xml$/.test(spec)) {
   1452             return resolveMode("application/xml")
   1453         } else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+json$/.test(spec)) {
   1454             return resolveMode("application/json")
   1455         }
   1456         if (typeof spec == "string") { return {name: spec} }
   1457         else { return spec || {name: "null"} }
   1458     }
   1459 
   1460 // Given a mode spec (anything that resolveMode accepts), find and
   1461 // initialize an actual mode object.
   1462     function getMode(options, spec) {
   1463         spec = resolveMode(spec)
   1464         var mfactory = modes[spec.name]
   1465         if (!mfactory) { return getMode(options, "text/plain") }
   1466         var modeObj = mfactory(options, spec)
   1467         if (modeExtensions.hasOwnProperty(spec.name)) {
   1468             var exts = modeExtensions[spec.name]
   1469             for (var prop in exts) {
   1470                 if (!exts.hasOwnProperty(prop)) { continue }
   1471                 if (modeObj.hasOwnProperty(prop)) { modeObj["_" + prop] = modeObj[prop] }
   1472                 modeObj[prop] = exts[prop]
   1473             }
   1474         }
   1475         modeObj.name = spec.name
   1476         if (spec.helperType) { modeObj.helperType = spec.helperType }
   1477         if (spec.modeProps) { for (var prop$1 in spec.modeProps)
   1478         { modeObj[prop$1] = spec.modeProps[prop$1] } }
   1479 
   1480         return modeObj
   1481     }
   1482 
   1483 // This can be used to attach properties to mode objects from
   1484 // outside the actual mode definition.
   1485     var modeExtensions = {}
   1486     function extendMode(mode, properties) {
   1487         var exts = modeExtensions.hasOwnProperty(mode) ? modeExtensions[mode] : (modeExtensions[mode] = {})
   1488         copyObj(properties, exts)
   1489     }
   1490 
   1491     function copyState(mode, state) {
   1492         if (state === true) { return state }
   1493         if (mode.copyState) { return mode.copyState(state) }
   1494         var nstate = {}
   1495         for (var n in state) {
   1496             var val = state[n]
   1497             if (val instanceof Array) { val = val.concat([]) }
   1498             nstate[n] = val
   1499         }
   1500         return nstate
   1501     }
   1502 
   1503 // Given a mode and a state (for that mode), find the inner mode and
   1504 // state at the position that the state refers to.
   1505     function innerMode(mode, state) {
   1506         var info
   1507         while (mode.innerMode) {
   1508             info = mode.innerMode(state)
   1509             if (!info || info.mode == mode) { break }
   1510             state = info.state
   1511             mode = info.mode
   1512         }
   1513         return info || {mode: mode, state: state}
   1514     }
   1515 
   1516     function startState(mode, a1, a2) {
   1517         return mode.startState ? mode.startState(a1, a2) : true
   1518     }
   1519 
   1520 // STRING STREAM
   1521 
   1522 // Fed to the mode parsers, provides helper functions to make
   1523 // parsers more succinct.
   1524 
   1525     var StringStream = function(string, tabSize, lineOracle) {
   1526         this.pos = this.start = 0
   1527         this.string = string
   1528         this.tabSize = tabSize || 8
   1529         this.lastColumnPos = this.lastColumnValue = 0
   1530         this.lineStart = 0
   1531         this.lineOracle = lineOracle
   1532     };
   1533 
   1534     StringStream.prototype.eol = function () {return this.pos >= this.string.length};
   1535     StringStream.prototype.sol = function () {return this.pos == this.lineStart};
   1536     StringStream.prototype.peek = function () {return this.string.charAt(this.pos) || undefined};
   1537     StringStream.prototype.next = function () {
   1538         if (this.pos < this.string.length)
   1539         { return this.string.charAt(this.pos++) }
   1540     };
   1541     StringStream.prototype.eat = function (match) {
   1542         var ch = this.string.charAt(this.pos)
   1543         var ok
   1544         if (typeof match == "string") { ok = ch == match }
   1545         else { ok = ch && (match.test ? match.test(ch) : match(ch)) }
   1546         if (ok) {++this.pos; return ch}
   1547     };
   1548     StringStream.prototype.eatWhile = function (match) {
   1549         var start = this.pos
   1550         while (this.eat(match)){}
   1551         return this.pos > start
   1552     };
   1553     StringStream.prototype.eatSpace = function () {
   1554         var this$1 = this;
   1555 
   1556         var start = this.pos
   1557         while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) { ++this$1.pos }
   1558         return this.pos > start
   1559     };
   1560     StringStream.prototype.skipToEnd = function () {this.pos = this.string.length};
   1561     StringStream.prototype.skipTo = function (ch) {
   1562         var found = this.string.indexOf(ch, this.pos)
   1563         if (found > -1) {this.pos = found; return true}
   1564     };
   1565     StringStream.prototype.backUp = function (n) {this.pos -= n};
   1566     StringStream.prototype.column = function () {
   1567         if (this.lastColumnPos < this.start) {
   1568             this.lastColumnValue = countColumn(this.string, this.start, this.tabSize, this.lastColumnPos, this.lastColumnValue)
   1569             this.lastColumnPos = this.start
   1570         }
   1571         return this.lastColumnValue - (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0)
   1572     };
   1573     StringStream.prototype.indentation = function () {
   1574         return countColumn(this.string, null, this.tabSize) -
   1575             (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0)
   1576     };
   1577     StringStream.prototype.match = function (pattern, consume, caseInsensitive) {
   1578         if (typeof pattern == "string") {
   1579             var cased = function (str) { return caseInsensitive ? str.toLowerCase() : str; }
   1580             var substr = this.string.substr(this.pos, pattern.length)
   1581             if (cased(substr) == cased(pattern)) {
   1582                 if (consume !== false) { this.pos += pattern.length }
   1583                 return true
   1584             }
   1585         } else {
   1586             var match = this.string.slice(this.pos).match(pattern)
   1587             if (match && match.index > 0) { return null }
   1588             if (match && consume !== false) { this.pos += match[0].length }
   1589             return match
   1590         }
   1591     };
   1592     StringStream.prototype.current = function (){return this.string.slice(this.start, this.pos)};
   1593     StringStream.prototype.hideFirstChars = function (n, inner) {
   1594         this.lineStart += n
   1595         try { return inner() }
   1596         finally { this.lineStart -= n }
   1597     };
   1598     StringStream.prototype.lookAhead = function (n) {
   1599         var oracle = this.lineOracle
   1600         return oracle && oracle.lookAhead(n)
   1601     };
   1602     StringStream.prototype.baseToken = function () {
   1603         var oracle = this.lineOracle
   1604         return oracle && oracle.baseToken(this.pos)
   1605     };
   1606 
   1607     var SavedContext = function(state, lookAhead) {
   1608         this.state = state
   1609         this.lookAhead = lookAhead
   1610     };
   1611 
   1612     var Context = function(doc, state, line, lookAhead) {
   1613         this.state = state
   1614         this.doc = doc
   1615         this.line = line
   1616         this.maxLookAhead = lookAhead || 0
   1617         this.baseTokens = null
   1618         this.baseTokenPos = 1
   1619     };
   1620 
   1621     Context.prototype.lookAhead = function (n) {
   1622         var line = this.doc.getLine(this.line + n)
   1623         if (line != null && n > this.maxLookAhead) { this.maxLookAhead = n }
   1624         return line
   1625     };
   1626 
   1627     Context.prototype.baseToken = function (n) {
   1628         var this$1 = this;
   1629 
   1630         if (!this.baseTokens) { return null }
   1631         while (this.baseTokens[this.baseTokenPos] <= n)
   1632         { this$1.baseTokenPos += 2 }
   1633         var type = this.baseTokens[this.baseTokenPos + 1]
   1634         return {type: type && type.replace(/( |^)overlay .*/, ""),
   1635             size: this.baseTokens[this.baseTokenPos] - n}
   1636     };
   1637 
   1638     Context.prototype.nextLine = function () {
   1639         this.line++
   1640         if (this.maxLookAhead > 0) { this.maxLookAhead-- }
   1641     };
   1642 
   1643     Context.fromSaved = function (doc, saved, line) {
   1644         if (saved instanceof SavedContext)
   1645         { return new Context(doc, copyState(doc.mode, saved.state), line, saved.lookAhead) }
   1646         else
   1647         { return new Context(doc, copyState(doc.mode, saved), line) }
   1648     };
   1649 
   1650     Context.prototype.save = function (copy) {
   1651         var state = copy !== false ? copyState(this.doc.mode, this.state) : this.state
   1652         return this.maxLookAhead > 0 ? new SavedContext(state, this.maxLookAhead) : state
   1653     };
   1654 
   1655 
   1656 // Compute a style array (an array starting with a mode generation
   1657 // -- for invalidation -- followed by pairs of end positions and
   1658 // style strings), which is used to highlight the tokens on the
   1659 // line.
   1660     function highlightLine(cm, line, context, forceToEnd) {
   1661         // A styles array always starts with a number identifying the
   1662         // mode/overlays that it is based on (for easy invalidation).
   1663         var st = [cm.state.modeGen], lineClasses = {}
   1664         // Compute the base array of styles
   1665         runMode(cm, line.text, cm.doc.mode, context, function (end, style) { return st.push(end, style); },
   1666             lineClasses, forceToEnd)
   1667         var state = context.state
   1668 
   1669         // Run overlays, adjust style array.
   1670         var loop = function ( o ) {
   1671             context.baseTokens = st
   1672             var overlay = cm.state.overlays[o], i = 1, at = 0
   1673             context.state = true
   1674             runMode(cm, line.text, overlay.mode, context, function (end, style) {
   1675                 var start = i
   1676                 // Ensure there's a token end at the current position, and that i points at it
   1677                 while (at < end) {
   1678                     var i_end = st[i]
   1679                     if (i_end > end)
   1680                     { st.splice(i, 1, end, st[i+1], i_end) }
   1681                     i += 2
   1682                     at = Math.min(end, i_end)
   1683                 }
   1684                 if (!style) { return }
   1685                 if (overlay.opaque) {
   1686                     st.splice(start, i - start, end, "overlay " + style)
   1687                     i = start + 2
   1688                 } else {
   1689                     for (; start < i; start += 2) {
   1690                         var cur = st[start+1]
   1691                         st[start+1] = (cur ? cur + " " : "") + "overlay " + style
   1692                     }
   1693                 }
   1694             }, lineClasses)
   1695             context.state = state
   1696             context.baseTokens = null
   1697             context.baseTokenPos = 1
   1698         };
   1699 
   1700         for (var o = 0; o < cm.state.overlays.length; ++o) loop( o );
   1701 
   1702         return {styles: st, classes: lineClasses.bgClass || lineClasses.textClass ? lineClasses : null}
   1703     }
   1704 
   1705     function getLineStyles(cm, line, updateFrontier) {
   1706         if (!line.styles || line.styles[0] != cm.state.modeGen) {
   1707             var context = getContextBefore(cm, lineNo(line))
   1708             var resetState = line.text.length > cm.options.maxHighlightLength && copyState(cm.doc.mode, context.state)
   1709             var result = highlightLine(cm, line, context)
   1710             if (resetState) { context.state = resetState }
   1711             line.stateAfter = context.save(!resetState)
   1712             line.styles = result.styles
   1713             if (result.classes) { line.styleClasses = result.classes }
   1714             else if (line.styleClasses) { line.styleClasses = null }
   1715             if (updateFrontier === cm.doc.highlightFrontier)
   1716             { cm.doc.modeFrontier = Math.max(cm.doc.modeFrontier, ++cm.doc.highlightFrontier) }
   1717         }
   1718         return line.styles
   1719     }
   1720 
   1721     function getContextBefore(cm, n, precise) {
   1722         var doc = cm.doc, display = cm.display
   1723         if (!doc.mode.startState) { return new Context(doc, true, n) }
   1724         var start = findStartLine(cm, n, precise)
   1725         var saved = start > doc.first && getLine(doc, start - 1).stateAfter
   1726         var context = saved ? Context.fromSaved(doc, saved, start) : new Context(doc, startState(doc.mode), start)
   1727 
   1728         doc.iter(start, n, function (line) {
   1729             processLine(cm, line.text, context)
   1730             var pos = context.line
   1731             line.stateAfter = pos == n - 1 || pos % 5 == 0 || pos >= display.viewFrom && pos < display.viewTo ? context.save() : null
   1732             context.nextLine()
   1733         })
   1734         if (precise) { doc.modeFrontier = context.line }
   1735         return context
   1736     }
   1737 
   1738 // Lightweight form of highlight -- proceed over this line and
   1739 // update state, but don't save a style array. Used for lines that
   1740 // aren't currently visible.
   1741     function processLine(cm, text, context, startAt) {
   1742         var mode = cm.doc.mode
   1743         var stream = new StringStream(text, cm.options.tabSize, context)
   1744         stream.start = stream.pos = startAt || 0
   1745         if (text == "") { callBlankLine(mode, context.state) }
   1746         while (!stream.eol()) {
   1747             readToken(mode, stream, context.state)
   1748             stream.start = stream.pos
   1749         }
   1750     }
   1751 
   1752     function callBlankLine(mode, state) {
   1753         if (mode.blankLine) { return mode.blankLine(state) }
   1754         if (!mode.innerMode) { return }
   1755         var inner = innerMode(mode, state)
   1756         if (inner.mode.blankLine) { return inner.mode.blankLine(inner.state) }
   1757     }
   1758 
   1759     function readToken(mode, stream, state, inner) {
   1760         for (var i = 0; i < 10; i++) {
   1761             if (inner) { inner[0] = innerMode(mode, state).mode }
   1762             var style = mode.token(stream, state)
   1763             if (stream.pos > stream.start) { return style }
   1764         }
   1765         throw new Error("Mode " + mode.name + " failed to advance stream.")
   1766     }
   1767 
   1768     var Token = function(stream, type, state) {
   1769         this.start = stream.start; this.end = stream.pos
   1770         this.string = stream.current()
   1771         this.type = type || null
   1772         this.state = state
   1773     };
   1774 
   1775 // Utility for getTokenAt and getLineTokens
   1776     function takeToken(cm, pos, precise, asArray) {
   1777         var doc = cm.doc, mode = doc.mode, style
   1778         pos = clipPos(doc, pos)
   1779         var line = getLine(doc, pos.line), context = getContextBefore(cm, pos.line, precise)
   1780         var stream = new StringStream(line.text, cm.options.tabSize, context), tokens
   1781         if (asArray) { tokens = [] }
   1782         while ((asArray || stream.pos < pos.ch) && !stream.eol()) {
   1783             stream.start = stream.pos
   1784             style = readToken(mode, stream, context.state)
   1785             if (asArray) { tokens.push(new Token(stream, style, copyState(doc.mode, context.state))) }
   1786         }
   1787         return asArray ? tokens : new Token(stream, style, context.state)
   1788     }
   1789 
   1790     function extractLineClasses(type, output) {
   1791         if (type) { for (;;) {
   1792             var lineClass = type.match(/(?:^|\s+)line-(background-)?(\S+)/)
   1793             if (!lineClass) { break }
   1794             type = type.slice(0, lineClass.index) + type.slice(lineClass.index + lineClass[0].length)
   1795             var prop = lineClass[1] ? "bgClass" : "textClass"
   1796             if (output[prop] == null)
   1797             { output[prop] = lineClass[2] }
   1798             else if (!(new RegExp("(?:^|\s)" + lineClass[2] + "(?:$|\s)")).test(output[prop]))
   1799             { output[prop] += " " + lineClass[2] }
   1800         } }
   1801         return type
   1802     }
   1803 
   1804 // Run the given mode's parser over a line, calling f for each token.
   1805     function runMode(cm, text, mode, context, f, lineClasses, forceToEnd) {
   1806         var flattenSpans = mode.flattenSpans
   1807         if (flattenSpans == null) { flattenSpans = cm.options.flattenSpans }
   1808         var curStart = 0, curStyle = null
   1809         var stream = new StringStream(text, cm.options.tabSize, context), style
   1810         var inner = cm.options.addModeClass && [null]
   1811         if (text == "") { extractLineClasses(callBlankLine(mode, context.state), lineClasses) }
   1812         while (!stream.eol()) {
   1813             if (stream.pos > cm.options.maxHighlightLength) {
   1814                 flattenSpans = false
   1815                 if (forceToEnd) { processLine(cm, text, context, stream.pos) }
   1816                 stream.pos = text.length
   1817                 style = null
   1818             } else {
   1819                 style = extractLineClasses(readToken(mode, stream, context.state, inner), lineClasses)
   1820             }
   1821             if (inner) {
   1822                 var mName = inner[0].name
   1823                 if (mName) { style = "m-" + (style ? mName + " " + style : mName) }
   1824             }
   1825             if (!flattenSpans || curStyle != style) {
   1826                 while (curStart < stream.start) {
   1827                     curStart = Math.min(stream.start, curStart + 5000)
   1828                     f(curStart, curStyle)
   1829                 }
   1830                 curStyle = style
   1831             }
   1832             stream.start = stream.pos
   1833         }
   1834         while (curStart < stream.pos) {
   1835             // Webkit seems to refuse to render text nodes longer than 57444
   1836             // characters, and returns inaccurate measurements in nodes
   1837             // starting around 5000 chars.
   1838             var pos = Math.min(stream.pos, curStart + 5000)
   1839             f(pos, curStyle)
   1840             curStart = pos
   1841         }
   1842     }
   1843 
   1844 // Finds the line to start with when starting a parse. Tries to
   1845 // find a line with a stateAfter, so that it can start with a
   1846 // valid state. If that fails, it returns the line with the
   1847 // smallest indentation, which tends to need the least context to
   1848 // parse correctly.
   1849     function findStartLine(cm, n, precise) {
   1850         var minindent, minline, doc = cm.doc
   1851         var lim = precise ? -1 : n - (cm.doc.mode.innerMode ? 1000 : 100)
   1852         for (var search = n; search > lim; --search) {
   1853             if (search <= doc.first) { return doc.first }
   1854             var line = getLine(doc, search - 1), after = line.stateAfter
   1855             if (after && (!precise || search + (after instanceof SavedContext ? after.lookAhead : 0) <= doc.modeFrontier))
   1856             { return search }
   1857             var indented = countColumn(line.text, null, cm.options.tabSize)
   1858             if (minline == null || minindent > indented) {
   1859                 minline = search - 1
   1860                 minindent = indented
   1861             }
   1862         }
   1863         return minline
   1864     }
   1865 
   1866     function retreatFrontier(doc, n) {
   1867         doc.modeFrontier = Math.min(doc.modeFrontier, n)
   1868         if (doc.highlightFrontier < n - 10) { return }
   1869         var start = doc.first
   1870         for (var line = n - 1; line > start; line--) {
   1871             var saved = getLine(doc, line).stateAfter
   1872             // change is on 3
   1873             // state on line 1 looked ahead 2 -- so saw 3
   1874             // test 1 + 2 < 3 should cover this
   1875             if (saved && (!(saved instanceof SavedContext) || line + saved.lookAhead < n)) {
   1876                 start = line + 1
   1877                 break
   1878             }
   1879         }
   1880         doc.highlightFrontier = Math.min(doc.highlightFrontier, start)
   1881     }
   1882 
   1883 // LINE DATA STRUCTURE
   1884 
   1885 // Line objects. These hold state related to a line, including
   1886 // highlighting info (the styles array).
   1887     var Line = function(text, markedSpans, estimateHeight) {
   1888         this.text = text
   1889         attachMarkedSpans(this, markedSpans)
   1890         this.height = estimateHeight ? estimateHeight(this) : 1
   1891     };
   1892 
   1893     Line.prototype.lineNo = function () { return lineNo(this) };
   1894     eventMixin(Line)
   1895 
   1896 // Change the content (text, markers) of a line. Automatically
   1897 // invalidates cached information and tries to re-estimate the
   1898 // line's height.
   1899     function updateLine(line, text, markedSpans, estimateHeight) {
   1900         line.text = text
   1901         if (line.stateAfter) { line.stateAfter = null }
   1902         if (line.styles) { line.styles = null }
   1903         if (line.order != null) { line.order = null }
   1904         detachMarkedSpans(line)
   1905         attachMarkedSpans(line, markedSpans)
   1906         var estHeight = estimateHeight ? estimateHeight(line) : 1
   1907         if (estHeight != line.height) { updateLineHeight(line, estHeight) }
   1908     }
   1909 
   1910 // Detach a line from the document tree and its markers.
   1911     function cleanUpLine(line) {
   1912         line.parent = null
   1913         detachMarkedSpans(line)
   1914     }
   1915 
   1916 // Convert a style as returned by a mode (either null, or a string
   1917 // containing one or more styles) to a CSS style. This is cached,
   1918 // and also looks for line-wide styles.
   1919     var styleToClassCache = {};
   1920     var styleToClassCacheWithMode = {};
   1921     function interpretTokenStyle(style, options) {
   1922         if (!style || /^\s*$/.test(style)) { return null }
   1923         var cache = options.addModeClass ? styleToClassCacheWithMode : styleToClassCache
   1924         return cache[style] ||
   1925             (cache[style] = style.replace(/\S+/g, "cm-$&"))
   1926     }
   1927 
   1928 // Render the DOM representation of the text of a line. Also builds
   1929 // up a 'line map', which points at the DOM nodes that represent
   1930 // specific stretches of text, and is used by the measuring code.
   1931 // The returned object contains the DOM node, this map, and
   1932 // information about line-wide styles that were set by the mode.
   1933     function buildLineContent(cm, lineView) {
   1934         // The padding-right forces the element to have a 'border', which
   1935         // is needed on Webkit to be able to get line-level bounding
   1936         // rectangles for it (in measureChar).
   1937         var content = eltP("span", null, null, webkit ? "padding-right: .1px" : null)
   1938         var builder = {pre: eltP("pre", [content], "CodeMirror-line"), content: content,
   1939             col: 0, pos: 0, cm: cm,
   1940             trailingSpace: false,
   1941             splitSpaces: (ie || webkit) && cm.getOption("lineWrapping")}
   1942         lineView.measure = {}
   1943 
   1944         // Iterate over the logical lines that make up this visual line.
   1945         for (var i = 0; i <= (lineView.rest ? lineView.rest.length : 0); i++) {
   1946             var line = i ? lineView.rest[i - 1] : lineView.line, order = (void 0)
   1947             builder.pos = 0
   1948             builder.addToken = buildToken
   1949             // Optionally wire in some hacks into the token-rendering
   1950             // algorithm, to deal with browser quirks.
   1951             if (hasBadBidiRects(cm.display.measure) && (order = getOrder(line, cm.doc.direction)))
   1952             { builder.addToken = buildTokenBadBidi(builder.addToken, order) }
   1953             builder.map = []
   1954             var allowFrontierUpdate = lineView != cm.display.externalMeasured && lineNo(line)
   1955             insertLineContent(line, builder, getLineStyles(cm, line, allowFrontierUpdate))
   1956             if (line.styleClasses) {
   1957                 if (line.styleClasses.bgClass)
   1958                 { builder.bgClass = joinClasses(line.styleClasses.bgClass, builder.bgClass || "") }
   1959                 if (line.styleClasses.textClass)
   1960                 { builder.textClass = joinClasses(line.styleClasses.textClass, builder.textClass || "") }
   1961             }
   1962 
   1963             // Ensure at least a single node is present, for measuring.
   1964             if (builder.map.length == 0)
   1965             { builder.map.push(0, 0, builder.content.appendChild(zeroWidthElement(cm.display.measure))) }
   1966 
   1967             // Store the map and a cache object for the current logical line
   1968             if (i == 0) {
   1969                 lineView.measure.map = builder.map
   1970                 lineView.measure.cache = {}
   1971             } else {
   1972                 ;(lineView.measure.maps || (lineView.measure.maps = [])).push(builder.map)
   1973                 ;(lineView.measure.caches || (lineView.measure.caches = [])).push({})
   1974             }
   1975         }
   1976 
   1977         // See issue #2901
   1978         if (webkit) {
   1979             var last = builder.content.lastChild
   1980             if (/\bcm-tab\b/.test(last.className) || (last.querySelector && last.querySelector(".cm-tab")))
   1981             { builder.content.className = "cm-tab-wrap-hack" }
   1982         }
   1983 
   1984         signal(cm, "renderLine", cm, lineView.line, builder.pre)
   1985         if (builder.pre.className)
   1986         { builder.textClass = joinClasses(builder.pre.className, builder.textClass || "") }
   1987 
   1988         return builder
   1989     }
   1990 
   1991     function defaultSpecialCharPlaceholder(ch) {
   1992         var token = elt("span", "\u2022", "cm-invalidchar")
   1993         token.title = "\\u" + ch.charCodeAt(0).toString(16)
   1994         token.setAttribute("aria-label", token.title)
   1995         return token
   1996     }
   1997 
   1998 // Build up the DOM representation for a single token, and add it to
   1999 // the line map. Takes care to render special characters separately.
   2000     function buildToken(builder, text, style, startStyle, endStyle, title, css) {
   2001         if (!text) { return }
   2002         var displayText = builder.splitSpaces ? splitSpaces(text, builder.trailingSpace) : text
   2003         var special = builder.cm.state.specialChars, mustWrap = false
   2004         var content
   2005         if (!special.test(text)) {
   2006             builder.col += text.length
   2007             content = document.createTextNode(displayText)
   2008             builder.map.push(builder.pos, builder.pos + text.length, content)
   2009             if (ie && ie_version < 9) { mustWrap = true }
   2010             builder.pos += text.length
   2011         } else {
   2012             content = document.createDocumentFragment()
   2013             var pos = 0
   2014             while (true) {
   2015                 special.lastIndex = pos
   2016                 var m = special.exec(text)
   2017                 var skipped = m ? m.index - pos : text.length - pos
   2018                 if (skipped) {
   2019                     var txt = document.createTextNode(displayText.slice(pos, pos + skipped))
   2020                     if (ie && ie_version < 9) { content.appendChild(elt("span", [txt])) }
   2021                     else { content.appendChild(txt) }
   2022                     builder.map.push(builder.pos, builder.pos + skipped, txt)
   2023                     builder.col += skipped
   2024                     builder.pos += skipped
   2025                 }
   2026                 if (!m) { break }
   2027                 pos += skipped + 1
   2028                 var txt$1 = (void 0)
   2029                 if (m[0] == "\t") {
   2030                     var tabSize = builder.cm.options.tabSize, tabWidth = tabSize - builder.col % tabSize
   2031                     txt$1 = content.appendChild(elt("span", spaceStr(tabWidth), "cm-tab"))
   2032                     txt$1.setAttribute("role", "presentation")
   2033                     txt$1.setAttribute("cm-text", "\t")
   2034                     builder.col += tabWidth
   2035                 } else if (m[0] == "\r" || m[0] == "\n") {
   2036                     txt$1 = content.appendChild(elt("span", m[0] == "\r" ? "\u240d" : "\u2424", "cm-invalidchar"))
   2037                     txt$1.setAttribute("cm-text", m[0])
   2038                     builder.col += 1
   2039                 } else {
   2040                     txt$1 = builder.cm.options.specialCharPlaceholder(m[0])
   2041                     txt$1.setAttribute("cm-text", m[0])
   2042                     if (ie && ie_version < 9) { content.appendChild(elt("span", [txt$1])) }
   2043                     else { content.appendChild(txt$1) }
   2044                     builder.col += 1
   2045                 }
   2046                 builder.map.push(builder.pos, builder.pos + 1, txt$1)
   2047                 builder.pos++
   2048             }
   2049         }
   2050         builder.trailingSpace = displayText.charCodeAt(text.length - 1) == 32
   2051         if (style || startStyle || endStyle || mustWrap || css) {
   2052             var fullStyle = style || ""
   2053             if (startStyle) { fullStyle += startStyle }
   2054             if (endStyle) { fullStyle += endStyle }
   2055             var token = elt("span", [content], fullStyle, css)
   2056             if (title) { token.title = title }
   2057             return builder.content.appendChild(token)
   2058         }
   2059         builder.content.appendChild(content)
   2060     }
   2061 
   2062     function splitSpaces(text, trailingBefore) {
   2063         if (text.length > 1 && !/  /.test(text)) { return text }
   2064         var spaceBefore = trailingBefore, result = ""
   2065         for (var i = 0; i < text.length; i++) {
   2066             var ch = text.charAt(i)
   2067             if (ch == " " && spaceBefore && (i == text.length - 1 || text.charCodeAt(i + 1) == 32))
   2068             { ch = "\u00a0" }
   2069             result += ch
   2070             spaceBefore = ch == " "
   2071         }
   2072         return result
   2073     }
   2074 
   2075 // Work around nonsense dimensions being reported for stretches of
   2076 // right-to-left text.
   2077     function buildTokenBadBidi(inner, order) {
   2078         return function (builder, text, style, startStyle, endStyle, title, css) {
   2079             style = style ? style + " cm-force-border" : "cm-force-border"
   2080             var start = builder.pos, end = start + text.length
   2081             for (;;) {
   2082                 // Find the part that overlaps with the start of this text
   2083                 var part = (void 0)
   2084                 for (var i = 0; i < order.length; i++) {
   2085                     part = order[i]
   2086                     if (part.to > start && part.from <= start) { break }
   2087                 }
   2088                 if (part.to >= end) { return inner(builder, text, style, startStyle, endStyle, title, css) }
   2089                 inner(builder, text.slice(0, part.to - start), style, startStyle, null, title, css)
   2090                 startStyle = null
   2091                 text = text.slice(part.to - start)
   2092                 start = part.to
   2093             }
   2094         }
   2095     }
   2096 
   2097     function buildCollapsedSpan(builder, size, marker, ignoreWidget) {
   2098         var widget = !ignoreWidget && marker.widgetNode
   2099         if (widget) { builder.map.push(builder.pos, builder.pos + size, widget) }
   2100         if (!ignoreWidget && builder.cm.display.input.needsContentAttribute) {
   2101             if (!widget)
   2102             { widget = builder.content.appendChild(document.createElement("span")) }
   2103             widget.setAttribute("cm-marker", marker.id)
   2104         }
   2105         if (widget) {
   2106             builder.cm.display.input.setUneditable(widget)
   2107             builder.content.appendChild(widget)
   2108         }
   2109         builder.pos += size
   2110         builder.trailingSpace = false
   2111     }
   2112 
   2113 // Outputs a number of spans to make up a line, taking highlighting
   2114 // and marked text into account.
   2115     function insertLineContent(line, builder, styles) {
   2116         var spans = line.markedSpans, allText = line.text, at = 0
   2117         if (!spans) {
   2118             for (var i$1 = 1; i$1 < styles.length; i$1+=2)
   2119             { builder.addToken(builder, allText.slice(at, at = styles[i$1]), interpretTokenStyle(styles[i$1+1], builder.cm.options)) }
   2120             return
   2121         }
   2122 
   2123         var len = allText.length, pos = 0, i = 1, text = "", style, css
   2124         var nextChange = 0, spanStyle, spanEndStyle, spanStartStyle, title, collapsed
   2125         for (;;) {
   2126             if (nextChange == pos) { // Update current marker set
   2127                 spanStyle = spanEndStyle = spanStartStyle = title = css = ""
   2128                 collapsed = null; nextChange = Infinity
   2129                 var foundBookmarks = [], endStyles = (void 0)
   2130                 for (var j = 0; j < spans.length; ++j) {
   2131                     var sp = spans[j], m = sp.marker
   2132                     if (m.type == "bookmark" && sp.from == pos && m.widgetNode) {
   2133                         foundBookmarks.push(m)
   2134                     } else if (sp.from <= pos && (sp.to == null || sp.to > pos || m.collapsed && sp.to == pos && sp.from == pos)) {
   2135                         if (sp.to != null && sp.to != pos && nextChange > sp.to) {
   2136                             nextChange = sp.to
   2137                             spanEndStyle = ""
   2138                         }
   2139                         if (m.className) { spanStyle += " " + m.className }
   2140                         if (m.css) { css = (css ? css + ";" : "") + m.css }
   2141                         if (m.startStyle && sp.from == pos) { spanStartStyle += " " + m.startStyle }
   2142                         if (m.endStyle && sp.to == nextChange) { (endStyles || (endStyles = [])).push(m.endStyle, sp.to) }
   2143                         if (m.title && !title) { title = m.title }
   2144                         if (m.collapsed && (!collapsed || compareCollapsedMarkers(collapsed.marker, m) < 0))
   2145                         { collapsed = sp }
   2146                     } else if (sp.from > pos && nextChange > sp.from) {
   2147                         nextChange = sp.from
   2148                     }
   2149                 }
   2150                 if (endStyles) { for (var j$1 = 0; j$1 < endStyles.length; j$1 += 2)
   2151                 { if (endStyles[j$1 + 1] == nextChange) { spanEndStyle += " " + endStyles[j$1] } } }
   2152 
   2153                 if (!collapsed || collapsed.from == pos) { for (var j$2 = 0; j$2 < foundBookmarks.length; ++j$2)
   2154                 { buildCollapsedSpan(builder, 0, foundBookmarks[j$2]) } }
   2155                 if (collapsed && (collapsed.from || 0) == pos) {
   2156                     buildCollapsedSpan(builder, (collapsed.to == null ? len + 1 : collapsed.to) - pos,
   2157                         collapsed.marker, collapsed.from == null)
   2158                     if (collapsed.to == null) { return }
   2159                     if (collapsed.to == pos) { collapsed = false }
   2160                 }
   2161             }
   2162             if (pos >= len) { break }
   2163 
   2164             var upto = Math.min(len, nextChange)
   2165             while (true) {
   2166                 if (text) {
   2167                     var end = pos + text.length
   2168                     if (!collapsed) {
   2169                         var tokenText = end > upto ? text.slice(0, upto - pos) : text
   2170                         builder.addToken(builder, tokenText, style ? style + spanStyle : spanStyle,
   2171                             spanStartStyle, pos + tokenText.length == nextChange ? spanEndStyle : "", title, css)
   2172                     }
   2173                     if (end >= upto) {text = text.slice(upto - pos); pos = upto; break}
   2174                     pos = end
   2175                     spanStartStyle = ""
   2176                 }
   2177                 text = allText.slice(at, at = styles[i++])
   2178                 style = interpretTokenStyle(styles[i++], builder.cm.options)
   2179             }
   2180         }
   2181     }
   2182 
   2183 
   2184 // These objects are used to represent the visible (currently drawn)
   2185 // part of the document. A LineView may correspond to multiple
   2186 // logical lines, if those are connected by collapsed ranges.
   2187     function LineView(doc, line, lineN) {
   2188         // The starting line
   2189         this.line = line
   2190         // Continuing lines, if any
   2191         this.rest = visualLineContinued(line)
   2192         // Number of logical lines in this visual line
   2193         this.size = this.rest ? lineNo(lst(this.rest)) - lineN + 1 : 1
   2194         this.node = this.text = null
   2195         this.hidden = lineIsHidden(doc, line)
   2196     }
   2197 
   2198 // Create a range of LineView objects for the given lines.
   2199     function buildViewArray(cm, from, to) {
   2200         var array = [], nextPos
   2201         for (var pos = from; pos < to; pos = nextPos) {
   2202             var view = new LineView(cm.doc, getLine(cm.doc, pos), pos)
   2203             nextPos = pos + view.size
   2204             array.push(view)
   2205         }
   2206         return array
   2207     }
   2208 
   2209     var operationGroup = null
   2210 
   2211     function pushOperation(op) {
   2212         if (operationGroup) {
   2213             operationGroup.ops.push(op)
   2214         } else {
   2215             op.ownsGroup = operationGroup = {
   2216                 ops: [op],
   2217                 delayedCallbacks: []
   2218             }
   2219         }
   2220     }
   2221 
   2222     function fireCallbacksForOps(group) {
   2223         // Calls delayed callbacks and cursorActivity handlers until no
   2224         // new ones appear
   2225         var callbacks = group.delayedCallbacks, i = 0
   2226         do {
   2227             for (; i < callbacks.length; i++)
   2228             { callbacks[i].call(null) }
   2229             for (var j = 0; j < group.ops.length; j++) {
   2230                 var op = group.ops[j]
   2231                 if (op.cursorActivityHandlers)
   2232                 { while (op.cursorActivityCalled < op.cursorActivityHandlers.length)
   2233                 { op.cursorActivityHandlers[op.cursorActivityCalled++].call(null, op.cm) } }
   2234             }
   2235         } while (i < callbacks.length)
   2236     }
   2237 
   2238     function finishOperation(op, endCb) {
   2239         var group = op.ownsGroup
   2240         if (!group) { return }
   2241 
   2242         try { fireCallbacksForOps(group) }
   2243         finally {
   2244             operationGroup = null
   2245             endCb(group)
   2246         }
   2247     }
   2248 
   2249     var orphanDelayedCallbacks = null
   2250 
   2251 // Often, we want to signal events at a point where we are in the
   2252 // middle of some work, but don't want the handler to start calling
   2253 // other methods on the editor, which might be in an inconsistent
   2254 // state or simply not expect any other events to happen.
   2255 // signalLater looks whether there are any handlers, and schedules
   2256 // them to be executed when the last operation ends, or, if no
   2257 // operation is active, when a timeout fires.
   2258     function signalLater(emitter, type /*, values...*/) {
   2259         var arr = getHandlers(emitter, type)
   2260         if (!arr.length) { return }
   2261         var args = Array.prototype.slice.call(arguments, 2), list
   2262         if (operationGroup) {
   2263             list = operationGroup.delayedCallbacks
   2264         } else if (orphanDelayedCallbacks) {
   2265             list = orphanDelayedCallbacks
   2266         } else {
   2267             list = orphanDelayedCallbacks = []
   2268             setTimeout(fireOrphanDelayed, 0)
   2269         }
   2270         var loop = function ( i ) {
   2271             list.push(function () { return arr[i].apply(null, args); })
   2272         };
   2273 
   2274         for (var i = 0; i < arr.length; ++i)
   2275             loop( i );
   2276     }
   2277 
   2278     function fireOrphanDelayed() {
   2279         var delayed = orphanDelayedCallbacks
   2280         orphanDelayedCallbacks = null
   2281         for (var i = 0; i < delayed.length; ++i) { delayed[i]() }
   2282     }
   2283 
   2284 // When an aspect of a line changes, a string is added to
   2285 // lineView.changes. This updates the relevant part of the line's
   2286 // DOM structure.
   2287     function updateLineForChanges(cm, lineView, lineN, dims) {
   2288         for (var j = 0; j < lineView.changes.length; j++) {
   2289             var type = lineView.changes[j]
   2290             if (type == "text") { updateLineText(cm, lineView) }
   2291             else if (type == "gutter") { updateLineGutter(cm, lineView, lineN, dims) }
   2292             else if (type == "class") { updateLineClasses(cm, lineView) }
   2293             else if (type == "widget") { updateLineWidgets(cm, lineView, dims) }
   2294         }
   2295         lineView.changes = null
   2296     }
   2297 
   2298 // Lines with gutter elements, widgets or a background class need to
   2299 // be wrapped, and have the extra elements added to the wrapper div
   2300     function ensureLineWrapped(lineView) {
   2301         if (lineView.node == lineView.text) {
   2302             lineView.node = elt("div", null, null, "position: relative")
   2303             if (lineView.text.parentNode)
   2304             { lineView.text.parentNode.replaceChild(lineView.node, lineView.text) }
   2305             lineView.node.appendChild(lineView.text)
   2306             if (ie && ie_version < 8) { lineView.node.style.zIndex = 2 }
   2307         }
   2308         return lineView.node
   2309     }
   2310 
   2311     function updateLineBackground(cm, lineView) {
   2312         var cls = lineView.bgClass ? lineView.bgClass + " " + (lineView.line.bgClass || "") : lineView.line.bgClass
   2313         if (cls) { cls += " CodeMirror-linebackground" }
   2314         if (lineView.background) {
   2315             if (cls) { lineView.background.className = cls }
   2316             else { lineView.background.parentNode.removeChild(lineView.background); lineView.background = null }
   2317         } else if (cls) {
   2318             var wrap = ensureLineWrapped(lineView)
   2319             lineView.background = wrap.insertBefore(elt("div", null, cls), wrap.firstChild)
   2320             cm.display.input.setUneditable(lineView.background)
   2321         }
   2322     }
   2323 
   2324 // Wrapper around buildLineContent which will reuse the structure
   2325 // in display.externalMeasured when possible.
   2326     function getLineContent(cm, lineView) {
   2327         var ext = cm.display.externalMeasured
   2328         if (ext && ext.line == lineView.line) {
   2329             cm.display.externalMeasured = null
   2330             lineView.measure = ext.measure
   2331             return ext.built
   2332         }
   2333         return buildLineContent(cm, lineView)
   2334     }
   2335 
   2336 // Redraw the line's text. Interacts with the background and text
   2337 // classes because the mode may output tokens that influence these
   2338 // classes.
   2339     function updateLineText(cm, lineView) {
   2340         var cls = lineView.text.className
   2341         var built = getLineContent(cm, lineView)
   2342         if (lineView.text == lineView.node) { lineView.node = built.pre }
   2343         lineView.text.parentNode.replaceChild(built.pre, lineView.text)
   2344         lineView.text = built.pre
   2345         if (built.bgClass != lineView.bgClass || built.textClass != lineView.textClass) {
   2346             lineView.bgClass = built.bgClass
   2347             lineView.textClass = built.textClass
   2348             updateLineClasses(cm, lineView)
   2349         } else if (cls) {
   2350             lineView.text.className = cls
   2351         }
   2352     }
   2353 
   2354     function updateLineClasses(cm, lineView) {
   2355         updateLineBackground(cm, lineView)
   2356         if (lineView.line.wrapClass)
   2357         { ensureLineWrapped(lineView).className = lineView.line.wrapClass }
   2358         else if (lineView.node != lineView.text)
   2359         { lineView.node.className = "" }
   2360         var textClass = lineView.textClass ? lineView.textClass + " " + (lineView.line.textClass || "") : lineView.line.textClass
   2361         lineView.text.className = textClass || ""
   2362     }
   2363 
   2364     function updateLineGutter(cm, lineView, lineN, dims) {
   2365         if (lineView.gutter) {
   2366             lineView.node.removeChild(lineView.gutter)
   2367             lineView.gutter = null
   2368         }
   2369         if (lineView.gutterBackground) {
   2370             lineView.node.removeChild(lineView.gutterBackground)
   2371             lineView.gutterBackground = null
   2372         }
   2373         if (lineView.line.gutterClass) {
   2374             var wrap = ensureLineWrapped(lineView)
   2375             lineView.gutterBackground = elt("div", null, "CodeMirror-gutter-background " + lineView.line.gutterClass,
   2376                 ("left: " + (cm.options.fixedGutter ? dims.fixedPos : -dims.gutterTotalWidth) + "px; width: " + (dims.gutterTotalWidth) + "px"))
   2377             cm.display.input.setUneditable(lineView.gutterBackground)
   2378             wrap.insertBefore(lineView.gutterBackground, lineView.text)
   2379         }
   2380         var markers = lineView.line.gutterMarkers
   2381         if (cm.options.lineNumbers || markers) {
   2382             var wrap$1 = ensureLineWrapped(lineView)
   2383             var gutterWrap = lineView.gutter = elt("div", null, "CodeMirror-gutter-wrapper", ("left: " + (cm.options.fixedGutter ? dims.fixedPos : -dims.gutterTotalWidth) + "px"))
   2384             cm.display.input.setUneditable(gutterWrap)
   2385             wrap$1.insertBefore(gutterWrap, lineView.text)
   2386             if (lineView.line.gutterClass)
   2387             { gutterWrap.className += " " + lineView.line.gutterClass }
   2388             if (cm.options.lineNumbers && (!markers || !markers["CodeMirror-linenumbers"]))
   2389             { lineView.lineNumber = gutterWrap.appendChild(
   2390                 elt("div", lineNumberFor(cm.options, lineN),
   2391                     "CodeMirror-linenumber CodeMirror-gutter-elt",
   2392                     ("left: " + (dims.gutterLeft["CodeMirror-linenumbers"]) + "px; width: " + (cm.display.lineNumInnerWidth) + "px"))) }
   2393             if (markers) { for (var k = 0; k < cm.options.gutters.length; ++k) {
   2394                 var id = cm.options.gutters[k], found = markers.hasOwnProperty(id) && markers[id]
   2395                 if (found)
   2396                 { gutterWrap.appendChild(elt("div", [found], "CodeMirror-gutter-elt",
   2397                     ("left: " + (dims.gutterLeft[id]) + "px; width: " + (dims.gutterWidth[id]) + "px"))) }
   2398             } }
   2399         }
   2400     }
   2401 
   2402     function updateLineWidgets(cm, lineView, dims) {
   2403         if (lineView.alignable) { lineView.alignable = null }
   2404         for (var node = lineView.node.firstChild, next = (void 0); node; node = next) {
   2405             next = node.nextSibling
   2406             if (node.className == "CodeMirror-linewidget")
   2407             { lineView.node.removeChild(node) }
   2408         }
   2409         insertLineWidgets(cm, lineView, dims)
   2410     }
   2411 
   2412 // Build a line's DOM representation from scratch
   2413     function buildLineElement(cm, lineView, lineN, dims) {
   2414         var built = getLineContent(cm, lineView)
   2415         lineView.text = lineView.node = built.pre
   2416         if (built.bgClass) { lineView.bgClass = built.bgClass }
   2417         if (built.textClass) { lineView.textClass = built.textClass }
   2418 
   2419         updateLineClasses(cm, lineView)
   2420         updateLineGutter(cm, lineView, lineN, dims)
   2421         insertLineWidgets(cm, lineView, dims)
   2422         return lineView.node
   2423     }
   2424 
   2425 // A lineView may contain multiple logical lines (when merged by
   2426 // collapsed spans). The widgets for all of them need to be drawn.
   2427     function insertLineWidgets(cm, lineView, dims) {
   2428         insertLineWidgetsFor(cm, lineView.line, lineView, dims, true)
   2429         if (lineView.rest) { for (var i = 0; i < lineView.rest.length; i++)
   2430         { insertLineWidgetsFor(cm, lineView.rest[i], lineView, dims, false) } }
   2431     }
   2432 
   2433     function insertLineWidgetsFor(cm, line, lineView, dims, allowAbove) {
   2434         if (!line.widgets) { return }
   2435         var wrap = ensureLineWrapped(lineView)
   2436         for (var i = 0, ws = line.widgets; i < ws.length; ++i) {
   2437             var widget = ws[i], node = elt("div", [widget.node], "CodeMirror-linewidget")
   2438             if (!widget.handleMouseEvents) { node.setAttribute("cm-ignore-events", "true") }
   2439             positionLineWidget(widget, node, lineView, dims)
   2440             cm.display.input.setUneditable(node)
   2441             if (allowAbove && widget.above)
   2442             { wrap.insertBefore(node, lineView.gutter || lineView.text) }
   2443             else
   2444             { wrap.appendChild(node) }
   2445             signalLater(widget, "redraw")
   2446         }
   2447     }
   2448 
   2449     function positionLineWidget(widget, node, lineView, dims) {
   2450         if (widget.noHScroll) {
   2451             ;(lineView.alignable || (lineView.alignable = [])).push(node)
   2452             var width = dims.wrapperWidth
   2453             node.style.left = dims.fixedPos + "px"
   2454             if (!widget.coverGutter) {
   2455                 width -= dims.gutterTotalWidth
   2456                 node.style.paddingLeft = dims.gutterTotalWidth + "px"
   2457             }
   2458             node.style.width = width + "px"
   2459         }
   2460         if (widget.coverGutter) {
   2461             node.style.zIndex = 5
   2462             node.style.position = "relative"
   2463             if (!widget.noHScroll) { node.style.marginLeft = -dims.gutterTotalWidth + "px" }
   2464         }
   2465     }
   2466 
   2467     function widgetHeight(widget) {
   2468         if (widget.height != null) { return widget.height }
   2469         var cm = widget.doc.cm
   2470         if (!cm) { return 0 }
   2471         if (!contains(document.body, widget.node)) {
   2472             var parentStyle = "position: relative;"
   2473             if (widget.coverGutter)
   2474             { parentStyle += "margin-left: -" + cm.display.gutters.offsetWidth + "px;" }
   2475             if (widget.noHScroll)
   2476             { parentStyle += "width: " + cm.display.wrapper.clientWidth + "px;" }
   2477             removeChildrenAndAdd(cm.display.measure, elt("div", [widget.node], null, parentStyle))
   2478         }
   2479         return widget.height = widget.node.parentNode.offsetHeight
   2480     }
   2481 
   2482 // Return true when the given mouse event happened in a widget
   2483     function eventInWidget(display, e) {
   2484         for (var n = e_target(e); n != display.wrapper; n = n.parentNode) {
   2485             if (!n || (n.nodeType == 1 && n.getAttribute("cm-ignore-events") == "true") ||
   2486                 (n.parentNode == display.sizer && n != display.mover))
   2487             { return true }
   2488         }
   2489     }
   2490 
   2491 // POSITION MEASUREMENT
   2492 
   2493     function paddingTop(display) {return display.lineSpace.offsetTop}
   2494     function paddingVert(display) {return display.mover.offsetHeight - display.lineSpace.offsetHeight}
   2495     function paddingH(display) {
   2496         if (display.cachedPaddingH) { return display.cachedPaddingH }
   2497         var e = removeChildrenAndAdd(display.measure, elt("pre", "x"))
   2498         var style = window.getComputedStyle ? window.getComputedStyle(e) : e.currentStyle
   2499         var data = {left: parseInt(style.paddingLeft), right: parseInt(style.paddingRight)}
   2500         if (!isNaN(data.left) && !isNaN(data.right)) { display.cachedPaddingH = data }
   2501         return data
   2502     }
   2503 
   2504     function scrollGap(cm) { return scrollerGap - cm.display.nativeBarWidth }
   2505     function displayWidth(cm) {
   2506         return cm.display.scroller.clientWidth - scrollGap(cm) - cm.display.barWidth
   2507     }
   2508     function displayHeight(cm) {
   2509         return cm.display.scroller.clientHeight - scrollGap(cm) - cm.display.barHeight
   2510     }
   2511 
   2512 // Ensure the lineView.wrapping.heights array is populated. This is
   2513 // an array of bottom offsets for the lines that make up a drawn
   2514 // line. When lineWrapping is on, there might be more than one
   2515 // height.
   2516     function ensureLineHeights(cm, lineView, rect) {
   2517         var wrapping = cm.options.lineWrapping
   2518         var curWidth = wrapping && displayWidth(cm)
   2519         if (!lineView.measure.heights || wrapping && lineView.measure.width != curWidth) {
   2520             var heights = lineView.measure.heights = []
   2521             if (wrapping) {
   2522                 lineView.measure.width = curWidth
   2523                 var rects = lineView.text.firstChild.getClientRects()
   2524                 for (var i = 0; i < rects.length - 1; i++) {
   2525                     var cur = rects[i], next = rects[i + 1]
   2526                     if (Math.abs(cur.bottom - next.bottom) > 2)
   2527                     { heights.push((cur.bottom + next.top) / 2 - rect.top) }
   2528                 }
   2529             }
   2530             heights.push(rect.bottom - rect.top)
   2531         }
   2532     }
   2533 
   2534 // Find a line map (mapping character offsets to text nodes) and a
   2535 // measurement cache for the given line number. (A line view might
   2536 // contain multiple lines when collapsed ranges are present.)
   2537     function mapFromLineView(lineView, line, lineN) {
   2538         if (lineView.line == line)
   2539         { return {map: lineView.measure.map, cache: lineView.measure.cache} }
   2540         for (var i = 0; i < lineView.rest.length; i++)
   2541         { if (lineView.rest[i] == line)
   2542         { return {map: lineView.measure.maps[i], cache: lineView.measure.caches[i]} } }
   2543         for (var i$1 = 0; i$1 < lineView.rest.length; i$1++)
   2544         { if (lineNo(lineView.rest[i$1]) > lineN)
   2545         { return {map: lineView.measure.maps[i$1], cache: lineView.measure.caches[i$1], before: true} } }
   2546     }
   2547 
   2548 // Render a line into the hidden node display.externalMeasured. Used
   2549 // when measurement is needed for a line that's not in the viewport.
   2550     function updateExternalMeasurement(cm, line) {
   2551         line = visualLine(line)
   2552         var lineN = lineNo(line)
   2553         var view = cm.display.externalMeasured = new LineView(cm.doc, line, lineN)
   2554         view.lineN = lineN
   2555         var built = view.built = buildLineContent(cm, view)
   2556         view.text = built.pre
   2557         removeChildrenAndAdd(cm.display.lineMeasure, built.pre)
   2558         return view
   2559     }
   2560 
   2561 // Get a {top, bottom, left, right} box (in line-local coordinates)
   2562 // for a given character.
   2563     function measureChar(cm, line, ch, bias) {
   2564         return measureCharPrepared(cm, prepareMeasureForLine(cm, line), ch, bias)
   2565     }
   2566 
   2567 // Find a line view that corresponds to the given line number.
   2568     function findViewForLine(cm, lineN) {
   2569         if (lineN >= cm.display.viewFrom && lineN < cm.display.viewTo)
   2570         { return cm.display.view[findViewIndex(cm, lineN)] }
   2571         var ext = cm.display.externalMeasured
   2572         if (ext && lineN >= ext.lineN && lineN < ext.lineN + ext.size)
   2573         { return ext }
   2574     }
   2575 
   2576 // Measurement can be split in two steps, the set-up work that
   2577 // applies to the whole line, and the measurement of the actual
   2578 // character. Functions like coordsChar, that need to do a lot of
   2579 // measurements in a row, can thus ensure that the set-up work is
   2580 // only done once.
   2581     function prepareMeasureForLine(cm, line) {
   2582         var lineN = lineNo(line)
   2583         var view = findViewForLine(cm, lineN)
   2584         if (view && !view.text) {
   2585             view = null
   2586         } else if (view && view.changes) {
   2587             updateLineForChanges(cm, view, lineN, getDimensions(cm))
   2588             cm.curOp.forceUpdate = true
   2589         }
   2590         if (!view)
   2591         { view = updateExternalMeasurement(cm, line) }
   2592 
   2593         var info = mapFromLineView(view, line, lineN)
   2594         return {
   2595             line: line, view: view, rect: null,
   2596             map: info.map, cache: info.cache, before: info.before,
   2597             hasHeights: false
   2598         }
   2599     }
   2600 
   2601 // Given a prepared measurement object, measures the position of an
   2602 // actual character (or fetches it from the cache).
   2603     function measureCharPrepared(cm, prepared, ch, bias, varHeight) {
   2604         if (prepared.before) { ch = -1 }
   2605         var key = ch + (bias || ""), found
   2606         if (prepared.cache.hasOwnProperty(key)) {
   2607             found = prepared.cache[key]
   2608         } else {
   2609             if (!prepared.rect)
   2610             { prepared.rect = prepared.view.text.getBoundingClientRect() }
   2611             if (!prepared.hasHeights) {
   2612                 ensureLineHeights(cm, prepared.view, prepared.rect)
   2613                 prepared.hasHeights = true
   2614             }
   2615             found = measureCharInner(cm, prepared, ch, bias)
   2616             if (!found.bogus) { prepared.cache[key] = found }
   2617         }
   2618         return {left: found.left, right: found.right,
   2619             top: varHeight ? found.rtop : found.top,
   2620             bottom: varHeight ? found.rbottom : found.bottom}
   2621     }
   2622 
   2623     var nullRect = {left: 0, right: 0, top: 0, bottom: 0}
   2624 
   2625     function nodeAndOffsetInLineMap(map, ch, bias) {
   2626         var node, start, end, collapse, mStart, mEnd
   2627         // First, search the line map for the text node corresponding to,
   2628         // or closest to, the target character.
   2629         for (var i = 0; i < map.length; i += 3) {
   2630             mStart = map[i]
   2631             mEnd = map[i + 1]
   2632             if (ch < mStart) {
   2633                 start = 0; end = 1
   2634                 collapse = "left"
   2635             } else if (ch < mEnd) {
   2636                 start = ch - mStart
   2637                 end = start + 1
   2638             } else if (i == map.length - 3 || ch == mEnd && map[i + 3] > ch) {
   2639                 end = mEnd - mStart
   2640                 start = end - 1
   2641                 if (ch >= mEnd) { collapse = "right" }
   2642             }
   2643             if (start != null) {
   2644                 node = map[i + 2]
   2645                 if (mStart == mEnd && bias == (node.insertLeft ? "left" : "right"))
   2646                 { collapse = bias }
   2647                 if (bias == "left" && start == 0)
   2648                 { while (i && map[i - 2] == map[i - 3] && map[i - 1].insertLeft) {
   2649                     node = map[(i -= 3) + 2]
   2650                     collapse = "left"
   2651                 } }
   2652                 if (bias == "right" && start == mEnd - mStart)
   2653                 { while (i < map.length - 3 && map[i + 3] == map[i + 4] && !map[i + 5].insertLeft) {
   2654                     node = map[(i += 3) + 2]
   2655                     collapse = "right"
   2656                 } }
   2657                 break
   2658             }
   2659         }
   2660         return {node: node, start: start, end: end, collapse: collapse, coverStart: mStart, coverEnd: mEnd}
   2661     }
   2662 
   2663     function getUsefulRect(rects, bias) {
   2664         var rect = nullRect
   2665         if (bias == "left") { for (var i = 0; i < rects.length; i++) {
   2666             if ((rect = rects[i]).left != rect.right) { break }
   2667         } } else { for (var i$1 = rects.length - 1; i$1 >= 0; i$1--) {
   2668             if ((rect = rects[i$1]).left != rect.right) { break }
   2669         } }
   2670         return rect
   2671     }
   2672 
   2673     function measureCharInner(cm, prepared, ch, bias) {
   2674         var place = nodeAndOffsetInLineMap(prepared.map, ch, bias)
   2675         var node = place.node, start = place.start, end = place.end, collapse = place.collapse
   2676 
   2677         var rect
   2678         if (node.nodeType == 3) { // If it is a text node, use a range to retrieve the coordinates.
   2679             for (var i$1 = 0; i$1 < 4; i$1++) { // Retry a maximum of 4 times when nonsense rectangles are returned
   2680                 while (start && isExtendingChar(prepared.line.text.charAt(place.coverStart + start))) { --start }
   2681                 while (place.coverStart + end < place.coverEnd && isExtendingChar(prepared.line.text.charAt(place.coverStart + end))) { ++end }
   2682                 if (ie && ie_version < 9 && start == 0 && end == place.coverEnd - place.coverStart)
   2683                 { rect = node.parentNode.getBoundingClientRect() }
   2684                 else
   2685                 { rect = getUsefulRect(range(node, start, end).getClientRects(), bias) }
   2686                 if (rect.left || rect.right || start == 0) { break }
   2687                 end = start
   2688                 start = start - 1
   2689                 collapse = "right"
   2690             }
   2691             if (ie && ie_version < 11) { rect = maybeUpdateRectForZooming(cm.display.measure, rect) }
   2692         } else { // If it is a widget, simply get the box for the whole widget.
   2693             if (start > 0) { collapse = bias = "right" }
   2694             var rects
   2695             if (cm.options.lineWrapping && (rects = node.getClientRects()).length > 1)
   2696             { rect = rects[bias == "right" ? rects.length - 1 : 0] }
   2697             else
   2698             { rect = node.getBoundingClientRect() }
   2699         }
   2700         if (ie && ie_version < 9 && !start && (!rect || !rect.left && !rect.right)) {
   2701             var rSpan = node.parentNode.getClientRects()[0]
   2702             if (rSpan)
   2703             { rect = {left: rSpan.left, right: rSpan.left + charWidth(cm.display), top: rSpan.top, bottom: rSpan.bottom} }
   2704             else
   2705             { rect = nullRect }
   2706         }
   2707 
   2708         var rtop = rect.top - prepared.rect.top, rbot = rect.bottom - prepared.rect.top
   2709         var mid = (rtop + rbot) / 2
   2710         var heights = prepared.view.measure.heights
   2711         var i = 0
   2712         for (; i < heights.length - 1; i++)
   2713         { if (mid < heights[i]) { break } }
   2714         var top = i ? heights[i - 1] : 0, bot = heights[i]
   2715         var result = {left: (collapse == "right" ? rect.right : rect.left) - prepared.rect.left,
   2716             right: (collapse == "left" ? rect.left : rect.right) - prepared.rect.left,
   2717             top: top, bottom: bot}
   2718         if (!rect.left && !rect.right) { result.bogus = true }
   2719         if (!cm.options.singleCursorHeightPerLine) { result.rtop = rtop; result.rbottom = rbot }
   2720 
   2721         return result
   2722     }
   2723 
   2724 // Work around problem with bounding client rects on ranges being
   2725 // returned incorrectly when zoomed on IE10 and below.
   2726     function maybeUpdateRectForZooming(measure, rect) {
   2727         if (!window.screen || screen.logicalXDPI == null ||
   2728             screen.logicalXDPI == screen.deviceXDPI || !hasBadZoomedRects(measure))
   2729         { return rect }
   2730         var scaleX = screen.logicalXDPI / screen.deviceXDPI
   2731         var scaleY = screen.logicalYDPI / screen.deviceYDPI
   2732         return {left: rect.left * scaleX, right: rect.right * scaleX,
   2733             top: rect.top * scaleY, bottom: rect.bottom * scaleY}
   2734     }
   2735 
   2736     function clearLineMeasurementCacheFor(lineView) {
   2737         if (lineView.measure) {
   2738             lineView.measure.cache = {}
   2739             lineView.measure.heights = null
   2740             if (lineView.rest) { for (var i = 0; i < lineView.rest.length; i++)
   2741             { lineView.measure.caches[i] = {} } }
   2742         }
   2743     }
   2744 
   2745     function clearLineMeasurementCache(cm) {
   2746         cm.display.externalMeasure = null
   2747         removeChildren(cm.display.lineMeasure)
   2748         for (var i = 0; i < cm.display.view.length; i++)
   2749         { clearLineMeasurementCacheFor(cm.display.view[i]) }
   2750     }
   2751 
   2752     function clearCaches(cm) {
   2753         clearLineMeasurementCache(cm)
   2754         cm.display.cachedCharWidth = cm.display.cachedTextHeight = cm.display.cachedPaddingH = null
   2755         if (!cm.options.lineWrapping) { cm.display.maxLineChanged = true }
   2756         cm.display.lineNumChars = null
   2757     }
   2758 
   2759     function pageScrollX() {
   2760         // Work around https://bugs.chromium.org/p/chromium/issues/detail?id=489206
   2761         // which causes page_Offset and bounding client rects to use
   2762         // different reference viewports and invalidate our calculations.
   2763         if (chrome && android) { return -(document.body.getBoundingClientRect().left - parseInt(getComputedStyle(document.body).marginLeft)) }
   2764         return window.pageXOffset || (document.documentElement || document.body).scrollLeft
   2765     }
   2766     function pageScrollY() {
   2767         if (chrome && android) { return -(document.body.getBoundingClientRect().top - parseInt(getComputedStyle(document.body).marginTop)) }
   2768         return window.pageYOffset || (document.documentElement || document.body).scrollTop
   2769     }
   2770 
   2771     function widgetTopHeight(lineObj) {
   2772         var height = 0
   2773         if (lineObj.widgets) { for (var i = 0; i < lineObj.widgets.length; ++i) { if (lineObj.widgets[i].above)
   2774         { height += widgetHeight(lineObj.widgets[i]) } } }
   2775         return height
   2776     }
   2777 
   2778 // Converts a {top, bottom, left, right} box from line-local
   2779 // coordinates into another coordinate system. Context may be one of
   2780 // "line", "div" (display.lineDiv), "local"./null (editor), "window",
   2781 // or "page".
   2782     function intoCoordSystem(cm, lineObj, rect, context, includeWidgets) {
   2783         if (!includeWidgets) {
   2784             var height = widgetTopHeight(lineObj)
   2785             rect.top += height; rect.bottom += height
   2786         }
   2787         if (context == "line") { return rect }
   2788         if (!context) { context = "local" }
   2789         var yOff = heightAtLine(lineObj)
   2790         if (context == "local") { yOff += paddingTop(cm.display) }
   2791         else { yOff -= cm.display.viewOffset }
   2792         if (context == "page" || context == "window") {
   2793             var lOff = cm.display.lineSpace.getBoundingClientRect()
   2794             yOff += lOff.top + (context == "window" ? 0 : pageScrollY())
   2795             var xOff = lOff.left + (context == "window" ? 0 : pageScrollX())
   2796             rect.left += xOff; rect.right += xOff
   2797         }
   2798         rect.top += yOff; rect.bottom += yOff
   2799         return rect
   2800     }
   2801 
   2802 // Coverts a box from "div" coords to another coordinate system.
   2803 // Context may be "window", "page", "div", or "local"./null.
   2804     function fromCoordSystem(cm, coords, context) {
   2805         if (context == "div") { return coords }
   2806         var left = coords.left, top = coords.top
   2807         // First move into "page" coordinate system
   2808         if (context == "page") {
   2809             left -= pageScrollX()
   2810             top -= pageScrollY()
   2811         } else if (context == "local" || !context) {
   2812             var localBox = cm.display.sizer.getBoundingClientRect()
   2813             left += localBox.left
   2814             top += localBox.top
   2815         }
   2816 
   2817         var lineSpaceBox = cm.display.lineSpace.getBoundingClientRect()
   2818         return {left: left - lineSpaceBox.left, top: top - lineSpaceBox.top}
   2819     }
   2820 
   2821     function charCoords(cm, pos, context, lineObj, bias) {
   2822         if (!lineObj) { lineObj = getLine(cm.doc, pos.line) }
   2823         return intoCoordSystem(cm, lineObj, measureChar(cm, lineObj, pos.ch, bias), context)
   2824     }
   2825 
   2826 // Returns a box for a given cursor position, which may have an
   2827 // 'other' property containing the position of the secondary cursor
   2828 // on a bidi boundary.
   2829 // A cursor Pos(line, char, "before") is on the same visual line as `char - 1`
   2830 // and after `char - 1` in writing order of `char - 1`
   2831 // A cursor Pos(line, char, "after") is on the same visual line as `char`
   2832 // and before `char` in writing order of `char`
   2833 // Examples (upper-case letters are RTL, lower-case are LTR):
   2834 //     Pos(0, 1, ...)
   2835 //     before   after
   2836 // ab     a|b     a|b
   2837 // aB     a|B     aB|
   2838 // Ab     |Ab     A|b
   2839 // AB     B|A     B|A
   2840 // Every position after the last character on a line is considered to stick
   2841 // to the last character on the line.
   2842     function cursorCoords(cm, pos, context, lineObj, preparedMeasure, varHeight) {
   2843         lineObj = lineObj || getLine(cm.doc, pos.line)
   2844         if (!preparedMeasure) { preparedMeasure = prepareMeasureForLine(cm, lineObj) }
   2845         function get(ch, right) {
   2846             var m = measureCharPrepared(cm, preparedMeasure, ch, right ? "right" : "left", varHeight)
   2847             if (right) { m.left = m.right; } else { m.right = m.left }
   2848             return intoCoordSystem(cm, lineObj, m, context)
   2849         }
   2850         var order = getOrder(lineObj, cm.doc.direction), ch = pos.ch, sticky = pos.sticky
   2851         if (ch >= lineObj.text.length) {
   2852             ch = lineObj.text.length
   2853             sticky = "before"
   2854         } else if (ch <= 0) {
   2855             ch = 0
   2856             sticky = "after"
   2857         }
   2858         if (!order) { return get(sticky == "before" ? ch - 1 : ch, sticky == "before") }
   2859 
   2860         function getBidi(ch, partPos, invert) {
   2861             var part = order[partPos], right = part.level == 1
   2862             return get(invert ? ch - 1 : ch, right != invert)
   2863         }
   2864         var partPos = getBidiPartAt(order, ch, sticky)
   2865         var other = bidiOther
   2866         var val = getBidi(ch, partPos, sticky == "before")
   2867         if (other != null) { val.other = getBidi(ch, other, sticky != "before") }
   2868         return val
   2869     }
   2870 
   2871 // Used to cheaply estimate the coordinates for a position. Used for
   2872 // intermediate scroll updates.
   2873     function estimateCoords(cm, pos) {
   2874         var left = 0
   2875         pos = clipPos(cm.doc, pos)
   2876         if (!cm.options.lineWrapping) { left = charWidth(cm.display) * pos.ch }
   2877         var lineObj = getLine(cm.doc, pos.line)
   2878         var top = heightAtLine(lineObj) + paddingTop(cm.display)
   2879         return {left: left, right: left, top: top, bottom: top + lineObj.height}
   2880     }
   2881 
   2882 // Positions returned by coordsChar contain some extra information.
   2883 // xRel is the relative x position of the input coordinates compared
   2884 // to the found position (so xRel > 0 means the coordinates are to
   2885 // the right of the character position, for example). When outside
   2886 // is true, that means the coordinates lie outside the line's
   2887 // vertical range.
   2888     function PosWithInfo(line, ch, sticky, outside, xRel) {
   2889         var pos = Pos(line, ch, sticky)
   2890         pos.xRel = xRel
   2891         if (outside) { pos.outside = true }
   2892         return pos
   2893     }
   2894 
   2895 // Compute the character position closest to the given coordinates.
   2896 // Input must be lineSpace-local ("div" coordinate system).
   2897     function coordsChar(cm, x, y) {
   2898         var doc = cm.doc
   2899         y += cm.display.viewOffset
   2900         if (y < 0) { return PosWithInfo(doc.first, 0, null, true, -1) }
   2901         var lineN = lineAtHeight(doc, y), last = doc.first + doc.size - 1
   2902         if (lineN > last)
   2903         { return PosWithInfo(doc.first + doc.size - 1, getLine(doc, last).text.length, null, true, 1) }
   2904         if (x < 0) { x = 0 }
   2905 
   2906         var lineObj = getLine(doc, lineN)
   2907         for (;;) {
   2908             var found = coordsCharInner(cm, lineObj, lineN, x, y)
   2909             var merged = collapsedSpanAtEnd(lineObj)
   2910             var mergedPos = merged && merged.find(0, true)
   2911             if (merged && (found.ch > mergedPos.from.ch || found.ch == mergedPos.from.ch && found.xRel > 0))
   2912             { lineN = lineNo(lineObj = mergedPos.to.line) }
   2913             else
   2914             { return found }
   2915         }
   2916     }
   2917 
   2918     function wrappedLineExtent(cm, lineObj, preparedMeasure, y) {
   2919         y -= widgetTopHeight(lineObj)
   2920         var end = lineObj.text.length
   2921         var begin = findFirst(function (ch) { return measureCharPrepared(cm, preparedMeasure, ch - 1).bottom <= y; }, end, 0)
   2922         end = findFirst(function (ch) { return measureCharPrepared(cm, preparedMeasure, ch).top > y; }, begin, end)
   2923         return {begin: begin, end: end}
   2924     }
   2925 
   2926     function wrappedLineExtentChar(cm, lineObj, preparedMeasure, target) {
   2927         if (!preparedMeasure) { preparedMeasure = prepareMeasureForLine(cm, lineObj) }
   2928         var targetTop = intoCoordSystem(cm, lineObj, measureCharPrepared(cm, preparedMeasure, target), "line").top
   2929         return wrappedLineExtent(cm, lineObj, preparedMeasure, targetTop)
   2930     }
   2931 
   2932 // Returns true if the given side of a box is after the given
   2933 // coordinates, in top-to-bottom, left-to-right order.
   2934     function boxIsAfter(box, x, y, left) {
   2935         return box.bottom <= y ? false : box.top > y ? true : (left ? box.left : box.right) > x
   2936     }
   2937 
   2938     function coordsCharInner(cm, lineObj, lineNo, x, y) {
   2939         // Move y into line-local coordinate space
   2940         y -= heightAtLine(lineObj)
   2941         var preparedMeasure = prepareMeasureForLine(cm, lineObj)
   2942         // When directly calling `measureCharPrepared`, we have to adjust
   2943         // for the widgets at this line.
   2944         var widgetHeight = widgetTopHeight(lineObj)
   2945         var begin = 0, end = lineObj.text.length, ltr = true
   2946 
   2947         var order = getOrder(lineObj, cm.doc.direction)
   2948         // If the line isn't plain left-to-right text, first figure out
   2949         // which bidi section the coordinates fall into.
   2950         if (order) {
   2951             var part = (cm.options.lineWrapping ? coordsBidiPartWrapped : coordsBidiPart)
   2952             (cm, lineObj, lineNo, preparedMeasure, order, x, y)
   2953             ltr = part.level != 1
   2954             // The awkward -1 offsets are needed because findFirst (called
   2955             // on these below) will treat its first bound as inclusive,
   2956             // second as exclusive, but we want to actually address the
   2957             // characters in the part's range
   2958             begin = ltr ? part.from : part.to - 1
   2959             end = ltr ? part.to : part.from - 1
   2960         }
   2961 
   2962         // A binary search to find the first character whose bounding box
   2963         // starts after the coordinates. If we run across any whose box wrap
   2964         // the coordinates, store that.
   2965         var chAround = null, boxAround = null
   2966         var ch = findFirst(function (ch) {
   2967             var box = measureCharPrepared(cm, preparedMeasure, ch)
   2968             box.top += widgetHeight; box.bottom += widgetHeight
   2969             if (!boxIsAfter(box, x, y, false)) { return false }
   2970             if (box.top <= y && box.left <= x) {
   2971                 chAround = ch
   2972                 boxAround = box
   2973             }
   2974             return true
   2975         }, begin, end)
   2976 
   2977         var baseX, sticky, outside = false
   2978         // If a box around the coordinates was found, use that
   2979         if (boxAround) {
   2980             // Distinguish coordinates nearer to the left or right side of the box
   2981             var atLeft = x - boxAround.left < boxAround.right - x, atStart = atLeft == ltr
   2982             ch = chAround + (atStart ? 0 : 1)
   2983             sticky = atStart ? "after" : "before"
   2984             baseX = atLeft ? boxAround.left : boxAround.right
   2985         } else {
   2986             // (Adjust for extended bound, if necessary.)
   2987             if (!ltr && (ch == end || ch == begin)) { ch++ }
   2988             // To determine which side to associate with, get the box to the
   2989             // left of the character and compare it's vertical position to the
   2990             // coordinates
   2991             sticky = ch == 0 ? "after" : ch == lineObj.text.length ? "before" :
   2992                 (measureCharPrepared(cm, preparedMeasure, ch - (ltr ? 1 : 0)).bottom + widgetHeight <= y) == ltr ?
   2993                     "after" : "before"
   2994             // Now get accurate coordinates for this place, in order to get a
   2995             // base X position
   2996             var coords = cursorCoords(cm, Pos(lineNo, ch, sticky), "line", lineObj, preparedMeasure)
   2997             baseX = coords.left
   2998             outside = y < coords.top || y >= coords.bottom
   2999         }
   3000 
   3001         ch = skipExtendingChars(lineObj.text, ch, 1)
   3002         return PosWithInfo(lineNo, ch, sticky, outside, x - baseX)
   3003     }
   3004 
   3005     function coordsBidiPart(cm, lineObj, lineNo, preparedMeasure, order, x, y) {
   3006         // Bidi parts are sorted left-to-right, and in a non-line-wrapping
   3007         // situation, we can take this ordering to correspond to the visual
   3008         // ordering. This finds the first part whose end is after the given
   3009         // coordinates.
   3010         var index = findFirst(function (i) {
   3011             var part = order[i], ltr = part.level != 1
   3012             return boxIsAfter(cursorCoords(cm, Pos(lineNo, ltr ? part.to : part.from, ltr ? "before" : "after"),
   3013                 "line", lineObj, preparedMeasure), x, y, true)
   3014         }, 0, order.length - 1)
   3015         var part = order[index]
   3016         // If this isn't the first part, the part's start is also after
   3017         // the coordinates, and the coordinates aren't on the same line as
   3018         // that start, move one part back.
   3019         if (index > 0) {
   3020             var ltr = part.level != 1
   3021             var start = cursorCoords(cm, Pos(lineNo, ltr ? part.from : part.to, ltr ? "after" : "before"),
   3022                 "line", lineObj, preparedMeasure)
   3023             if (boxIsAfter(start, x, y, true) && start.top > y)
   3024             { part = order[index - 1] }
   3025         }
   3026         return part
   3027     }
   3028 
   3029     function coordsBidiPartWrapped(cm, lineObj, _lineNo, preparedMeasure, order, x, y) {
   3030         // In a wrapped line, rtl text on wrapping boundaries can do things
   3031         // that don't correspond to the ordering in our `order` array at
   3032         // all, so a binary search doesn't work, and we want to return a
   3033         // part that only spans one line so that the binary search in
   3034         // coordsCharInner is safe. As such, we first find the extent of the
   3035         // wrapped line, and then do a flat search in which we discard any
   3036         // spans that aren't on the line.
   3037         var ref = wrappedLineExtent(cm, lineObj, preparedMeasure, y);
   3038         var begin = ref.begin;
   3039         var end = ref.end;
   3040         if (/\s/.test(lineObj.text.charAt(end - 1))) { end-- }
   3041         var part = null, closestDist = null
   3042         for (var i = 0; i < order.length; i++) {
   3043             var p = order[i]
   3044             if (p.from >= end || p.to <= begin) { continue }
   3045             var ltr = p.level != 1
   3046             var endX = measureCharPrepared(cm, preparedMeasure, ltr ? Math.min(end, p.to) - 1 : Math.max(begin, p.from)).right
   3047             // Weigh against spans ending before this, so that they are only
   3048             // picked if nothing ends after
   3049             var dist = endX < x ? x - endX + 1e9 : endX - x
   3050             if (!part || closestDist > dist) {
   3051                 part = p
   3052                 closestDist = dist
   3053             }
   3054         }
   3055         if (!part) { part = order[order.length - 1] }
   3056         // Clip the part to the wrapped line.
   3057         if (part.from < begin) { part = {from: begin, to: part.to, level: part.level} }
   3058         if (part.to > end) { part = {from: part.from, to: end, level: part.level} }
   3059         return part
   3060     }
   3061 
   3062     var measureText
   3063 // Compute the default text height.
   3064     function textHeight(display) {
   3065         if (display.cachedTextHeight != null) { return display.cachedTextHeight }
   3066         if (measureText == null) {
   3067             measureText = elt("pre")
   3068             // Measure a bunch of lines, for browsers that compute
   3069             // fractional heights.
   3070             for (var i = 0; i < 49; ++i) {
   3071                 measureText.appendChild(document.createTextNode("x"))
   3072                 measureText.appendChild(elt("br"))
   3073             }
   3074             measureText.appendChild(document.createTextNode("x"))
   3075         }
   3076         removeChildrenAndAdd(display.measure, measureText)
   3077         var height = measureText.offsetHeight / 50
   3078         if (height > 3) { display.cachedTextHeight = height }
   3079         removeChildren(display.measure)
   3080         return height || 1
   3081     }
   3082 
   3083 // Compute the default character width.
   3084     function charWidth(display) {
   3085         if (display.cachedCharWidth != null) { return display.cachedCharWidth }
   3086         var anchor = elt("span", "xxxxxxxxxx")
   3087         var pre = elt("pre", [anchor])
   3088         removeChildrenAndAdd(display.measure, pre)
   3089         var rect = anchor.getBoundingClientRect(), width = (rect.right - rect.left) / 10
   3090         if (width > 2) { display.cachedCharWidth = width }
   3091         return width || 10
   3092     }
   3093 
   3094 // Do a bulk-read of the DOM positions and sizes needed to draw the
   3095 // view, so that we don't interleave reading and writing to the DOM.
   3096     function getDimensions(cm) {
   3097         var d = cm.display, left = {}, width = {}
   3098         var gutterLeft = d.gutters.clientLeft
   3099         for (var n = d.gutters.firstChild, i = 0; n; n = n.nextSibling, ++i) {
   3100             left[cm.options.gutters[i]] = n.offsetLeft + n.clientLeft + gutterLeft
   3101             width[cm.options.gutters[i]] = n.clientWidth
   3102         }
   3103         return {fixedPos: compensateForHScroll(d),
   3104             gutterTotalWidth: d.gutters.offsetWidth,
   3105             gutterLeft: left,
   3106             gutterWidth: width,
   3107             wrapperWidth: d.wrapper.clientWidth}
   3108     }
   3109 
   3110 // Computes display.scroller.scrollLeft + display.gutters.offsetWidth,
   3111 // but using getBoundingClientRect to get a sub-pixel-accurate
   3112 // result.
   3113     function compensateForHScroll(display) {
   3114         return display.scroller.getBoundingClientRect().left - display.sizer.getBoundingClientRect().left
   3115     }
   3116 
   3117 // Returns a function that estimates the height of a line, to use as
   3118 // first approximation until the line becomes visible (and is thus
   3119 // properly measurable).
   3120     function estimateHeight(cm) {
   3121         var th = textHeight(cm.display), wrapping = cm.options.lineWrapping
   3122         var perLine = wrapping && Math.max(5, cm.display.scroller.clientWidth / charWidth(cm.display) - 3)
   3123         return function (line) {
   3124             if (lineIsHidden(cm.doc, line)) { return 0 }
   3125 
   3126             var widgetsHeight = 0
   3127             if (line.widgets) { for (var i = 0; i < line.widgets.length; i++) {
   3128                 if (line.widgets[i].height) { widgetsHeight += line.widgets[i].height }
   3129             } }
   3130 
   3131             if (wrapping)
   3132             { return widgetsHeight + (Math.ceil(line.text.length / perLine) || 1) * th }
   3133             else
   3134             { return widgetsHeight + th }
   3135         }
   3136     }
   3137 
   3138     function estimateLineHeights(cm) {
   3139         var doc = cm.doc, est = estimateHeight(cm)
   3140         doc.iter(function (line) {
   3141             var estHeight = est(line)
   3142             if (estHeight != line.height) { updateLineHeight(line, estHeight) }
   3143         })
   3144     }
   3145 
   3146 // Given a mouse event, find the corresponding position. If liberal
   3147 // is false, it checks whether a gutter or scrollbar was clicked,
   3148 // and returns null if it was. forRect is used by rectangular
   3149 // selections, and tries to estimate a character position even for
   3150 // coordinates beyond the right of the text.
   3151     function posFromMouse(cm, e, liberal, forRect) {
   3152         var display = cm.display
   3153         if (!liberal && e_target(e).getAttribute("cm-not-content") == "true") { return null }
   3154 
   3155         var x, y, space = display.lineSpace.getBoundingClientRect()
   3156         // Fails unpredictably on IE[67] when mouse is dragged around quickly.
   3157         try { x = e.clientX - space.left; y = e.clientY - space.top }
   3158         catch (e) { return null }
   3159         var coords = coordsChar(cm, x, y), line
   3160         if (forRect && coords.xRel == 1 && (line = getLine(cm.doc, coords.line).text).length == coords.ch) {
   3161             var colDiff = countColumn(line, line.length, cm.options.tabSize) - line.length
   3162             coords = Pos(coords.line, Math.max(0, Math.round((x - paddingH(cm.display).left) / charWidth(cm.display)) - colDiff))
   3163         }
   3164         return coords
   3165     }
   3166 
   3167 // Find the view element corresponding to a given line. Return null
   3168 // when the line isn't visible.
   3169     function findViewIndex(cm, n) {
   3170         if (n >= cm.display.viewTo) { return null }
   3171         n -= cm.display.viewFrom
   3172         if (n < 0) { return null }
   3173         var view = cm.display.view
   3174         for (var i = 0; i < view.length; i++) {
   3175             n -= view[i].size
   3176             if (n < 0) { return i }
   3177         }
   3178     }
   3179 
   3180     function updateSelection(cm) {
   3181         cm.display.input.showSelection(cm.display.input.prepareSelection())
   3182     }
   3183 
   3184     function prepareSelection(cm, primary) {
   3185         if ( primary === void 0 ) primary = true;
   3186 
   3187         var doc = cm.doc, result = {}
   3188         var curFragment = result.cursors = document.createDocumentFragment()
   3189         var selFragment = result.selection = document.createDocumentFragment()
   3190 
   3191         for (var i = 0; i < doc.sel.ranges.length; i++) {
   3192             if (!primary && i == doc.sel.primIndex) { continue }
   3193             var range = doc.sel.ranges[i]
   3194             if (range.from().line >= cm.display.viewTo || range.to().line < cm.display.viewFrom) { continue }
   3195             var collapsed = range.empty()
   3196             if (collapsed || cm.options.showCursorWhenSelecting)
   3197             { drawSelectionCursor(cm, range.head, curFragment) }
   3198             if (!collapsed)
   3199             { drawSelectionRange(cm, range, selFragment) }
   3200         }
   3201         return result
   3202     }
   3203 
   3204 // Draws a cursor for the given range
   3205     function drawSelectionCursor(cm, head, output) {
   3206         var pos = cursorCoords(cm, head, "div", null, null, !cm.options.singleCursorHeightPerLine)
   3207 
   3208         var cursor = output.appendChild(elt("div", "\u00a0", "CodeMirror-cursor"))
   3209         cursor.style.left = pos.left + "px"
   3210         cursor.style.top = pos.top + "px"
   3211         cursor.style.height = Math.max(0, pos.bottom - pos.top) * cm.options.cursorHeight + "px"
   3212 
   3213         if (pos.other) {
   3214             // Secondary cursor, shown when on a 'jump' in bi-directional text
   3215             var otherCursor = output.appendChild(elt("div", "\u00a0", "CodeMirror-cursor CodeMirror-secondarycursor"))
   3216             otherCursor.style.display = ""
   3217             otherCursor.style.left = pos.other.left + "px"
   3218             otherCursor.style.top = pos.other.top + "px"
   3219             otherCursor.style.height = (pos.other.bottom - pos.other.top) * .85 + "px"
   3220         }
   3221     }
   3222 
   3223     function cmpCoords(a, b) { return a.top - b.top || a.left - b.left }
   3224 
   3225 // Draws the given range as a highlighted selection
   3226     function drawSelectionRange(cm, range, output) {
   3227         var display = cm.display, doc = cm.doc
   3228         var fragment = document.createDocumentFragment()
   3229         var padding = paddingH(cm.display), leftSide = padding.left
   3230         var rightSide = Math.max(display.sizerWidth, displayWidth(cm) - display.sizer.offsetLeft) - padding.right
   3231         var docLTR = doc.direction == "ltr"
   3232 
   3233         function add(left, top, width, bottom) {
   3234             if (top < 0) { top = 0 }
   3235             top = Math.round(top)
   3236             bottom = Math.round(bottom)
   3237             fragment.appendChild(elt("div", null, "CodeMirror-selected", ("position: absolute; left: " + left + "px;\n                             top: " + top + "px; width: " + (width == null ? rightSide - left : width) + "px;\n                             height: " + (bottom - top) + "px")))
   3238         }
   3239 
   3240         function drawForLine(line, fromArg, toArg) {
   3241             var lineObj = getLine(doc, line)
   3242             var lineLen = lineObj.text.length
   3243             var start, end
   3244             function coords(ch, bias) {
   3245                 return charCoords(cm, Pos(line, ch), "div", lineObj, bias)
   3246             }
   3247 
   3248             function wrapX(pos, dir, side) {
   3249                 var extent = wrappedLineExtentChar(cm, lineObj, null, pos)
   3250                 var prop = (dir == "ltr") == (side == "after") ? "left" : "right"
   3251                 var ch = side == "after" ? extent.begin : extent.end - (/\s/.test(lineObj.text.charAt(extent.end - 1)) ? 2 : 1)
   3252                 return coords(ch, prop)[prop]
   3253             }
   3254 
   3255             var order = getOrder(lineObj, doc.direction)
   3256             iterateBidiSections(order, fromArg || 0, toArg == null ? lineLen : toArg, function (from, to, dir, i) {
   3257                 var ltr = dir == "ltr"
   3258                 var fromPos = coords(from, ltr ? "left" : "right")
   3259                 var toPos = coords(to - 1, ltr ? "right" : "left")
   3260 
   3261                 var openStart = fromArg == null && from == 0, openEnd = toArg == null && to == lineLen
   3262                 var first = i == 0, last = !order || i == order.length - 1
   3263                 if (toPos.top - fromPos.top <= 3) { // Single line
   3264                     var openLeft = (docLTR ? openStart : openEnd) && first
   3265                     var openRight = (docLTR ? openEnd : openStart) && last
   3266                     var left = openLeft ? leftSide : (ltr ? fromPos : toPos).left
   3267                     var right = openRight ? rightSide : (ltr ? toPos : fromPos).right
   3268                     add(left, fromPos.top, right - left, fromPos.bottom)
   3269                 } else { // Multiple lines
   3270                     var topLeft, topRight, botLeft, botRight
   3271                     if (ltr) {
   3272                         topLeft = docLTR && openStart && first ? leftSide : fromPos.left
   3273                         topRight = docLTR ? rightSide : wrapX(from, dir, "before")
   3274                         botLeft = docLTR ? leftSide : wrapX(to, dir, "after")
   3275                         botRight = docLTR && openEnd && last ? rightSide : toPos.right
   3276                     } else {
   3277                         topLeft = !docLTR ? leftSide : wrapX(from, dir, "before")
   3278                         topRight = !docLTR && openStart && first ? rightSide : fromPos.right
   3279                         botLeft = !docLTR && openEnd && last ? leftSide : toPos.left
   3280                         botRight = !docLTR ? rightSide : wrapX(to, dir, "after")
   3281                     }
   3282                     add(topLeft, fromPos.top, topRight - topLeft, fromPos.bottom)
   3283                     if (fromPos.bottom < toPos.top) { add(leftSide, fromPos.bottom, null, toPos.top) }
   3284                     add(botLeft, toPos.top, botRight - botLeft, toPos.bottom)
   3285                 }
   3286 
   3287                 if (!start || cmpCoords(fromPos, start) < 0) { start = fromPos }
   3288                 if (cmpCoords(toPos, start) < 0) { start = toPos }
   3289                 if (!end || cmpCoords(fromPos, end) < 0) { end = fromPos }
   3290                 if (cmpCoords(toPos, end) < 0) { end = toPos }
   3291             })
   3292             return {start: start, end: end}
   3293         }
   3294 
   3295         var sFrom = range.from(), sTo = range.to()
   3296         if (sFrom.line == sTo.line) {
   3297             drawForLine(sFrom.line, sFrom.ch, sTo.ch)
   3298         } else {
   3299             var fromLine = getLine(doc, sFrom.line), toLine = getLine(doc, sTo.line)
   3300             var singleVLine = visualLine(fromLine) == visualLine(toLine)
   3301             var leftEnd = drawForLine(sFrom.line, sFrom.ch, singleVLine ? fromLine.text.length + 1 : null).end
   3302             var rightStart = drawForLine(sTo.line, singleVLine ? 0 : null, sTo.ch).start
   3303             if (singleVLine) {
   3304                 if (leftEnd.top < rightStart.top - 2) {
   3305                     add(leftEnd.right, leftEnd.top, null, leftEnd.bottom)
   3306                     add(leftSide, rightStart.top, rightStart.left, rightStart.bottom)
   3307                 } else {
   3308                     add(leftEnd.right, leftEnd.top, rightStart.left - leftEnd.right, leftEnd.bottom)
   3309                 }
   3310             }
   3311             if (leftEnd.bottom < rightStart.top)
   3312             { add(leftSide, leftEnd.bottom, null, rightStart.top) }
   3313         }
   3314 
   3315         output.appendChild(fragment)
   3316     }
   3317 
   3318 // Cursor-blinking
   3319     function restartBlink(cm) {
   3320         if (!cm.state.focused) { return }
   3321         var display = cm.display
   3322         clearInterval(display.blinker)
   3323         var on = true
   3324         display.cursorDiv.style.visibility = ""
   3325         if (cm.options.cursorBlinkRate > 0)
   3326         { display.blinker = setInterval(function () { return display.cursorDiv.style.visibility = (on = !on) ? "" : "hidden"; },
   3327             cm.options.cursorBlinkRate) }
   3328         else if (cm.options.cursorBlinkRate < 0)
   3329         { display.cursorDiv.style.visibility = "hidden" }
   3330     }
   3331 
   3332     function ensureFocus(cm) {
   3333         if (!cm.state.focused) { cm.display.input.focus(); onFocus(cm) }
   3334     }
   3335 
   3336     function delayBlurEvent(cm) {
   3337         cm.state.delayingBlurEvent = true
   3338         setTimeout(function () { if (cm.state.delayingBlurEvent) {
   3339             cm.state.delayingBlurEvent = false
   3340             onBlur(cm)
   3341         } }, 100)
   3342     }
   3343 
   3344     function onFocus(cm, e) {
   3345         if (cm.state.delayingBlurEvent) { cm.state.delayingBlurEvent = false }
   3346 
   3347         if (cm.options.readOnly == "nocursor") { return }
   3348         if (!cm.state.focused) {
   3349             signal(cm, "focus", cm, e)
   3350             cm.state.focused = true
   3351             addClass(cm.display.wrapper, "CodeMirror-focused")
   3352             // This test prevents this from firing when a context
   3353             // menu is closed (since the input reset would kill the
   3354             // select-all detection hack)
   3355             if (!cm.curOp && cm.display.selForContextMenu != cm.doc.sel) {
   3356                 cm.display.input.reset()
   3357                 if (webkit) { setTimeout(function () { return cm.display.input.reset(true); }, 20) } // Issue #1730
   3358             }
   3359             cm.display.input.receivedFocus()
   3360         }
   3361         restartBlink(cm)
   3362     }
   3363     function onBlur(cm, e) {
   3364         if (cm.state.delayingBlurEvent) { return }
   3365 
   3366         if (cm.state.focused) {
   3367             signal(cm, "blur", cm, e)
   3368             cm.state.focused = false
   3369             rmClass(cm.display.wrapper, "CodeMirror-focused")
   3370         }
   3371         clearInterval(cm.display.blinker)
   3372         setTimeout(function () { if (!cm.state.focused) { cm.display.shift = false } }, 150)
   3373     }
   3374 
   3375 // Read the actual heights of the rendered lines, and update their
   3376 // stored heights to match.
   3377     function updateHeightsInViewport(cm) {
   3378         var display = cm.display
   3379         var prevBottom = display.lineDiv.offsetTop
   3380         for (var i = 0; i < display.view.length; i++) {
   3381             var cur = display.view[i], height = (void 0)
   3382             if (cur.hidden) { continue }
   3383             if (ie && ie_version < 8) {
   3384                 var bot = cur.node.offsetTop + cur.node.offsetHeight
   3385                 height = bot - prevBottom
   3386                 prevBottom = bot
   3387             } else {
   3388                 var box = cur.node.getBoundingClientRect()
   3389                 height = box.bottom - box.top
   3390             }
   3391             var diff = cur.line.height - height
   3392             if (height < 2) { height = textHeight(display) }
   3393             if (diff > .005 || diff < -.005) {
   3394                 updateLineHeight(cur.line, height)
   3395                 updateWidgetHeight(cur.line)
   3396                 if (cur.rest) { for (var j = 0; j < cur.rest.length; j++)
   3397                 { updateWidgetHeight(cur.rest[j]) } }
   3398             }
   3399         }
   3400     }
   3401 
   3402 // Read and store the height of line widgets associated with the
   3403 // given line.
   3404     function updateWidgetHeight(line) {
   3405         if (line.widgets) { for (var i = 0; i < line.widgets.length; ++i) {
   3406             var w = line.widgets[i], parent = w.node.parentNode
   3407             if (parent) { w.height = parent.offsetHeight }
   3408         } }
   3409     }
   3410 
   3411 // Compute the lines that are visible in a given viewport (defaults
   3412 // the the current scroll position). viewport may contain top,
   3413 // height, and ensure (see op.scrollToPos) properties.
   3414     function visibleLines(display, doc, viewport) {
   3415         var top = viewport && viewport.top != null ? Math.max(0, viewport.top) : display.scroller.scrollTop
   3416         top = Math.floor(top - paddingTop(display))
   3417         var bottom = viewport && viewport.bottom != null ? viewport.bottom : top + display.wrapper.clientHeight
   3418 
   3419         var from = lineAtHeight(doc, top), to = lineAtHeight(doc, bottom)
   3420         // Ensure is a {from: {line, ch}, to: {line, ch}} object, and
   3421         // forces those lines into the viewport (if possible).
   3422         if (viewport && viewport.ensure) {
   3423             var ensureFrom = viewport.ensure.from.line, ensureTo = viewport.ensure.to.line
   3424             if (ensureFrom < from) {
   3425                 from = ensureFrom
   3426                 to = lineAtHeight(doc, heightAtLine(getLine(doc, ensureFrom)) + display.wrapper.clientHeight)
   3427             } else if (Math.min(ensureTo, doc.lastLine()) >= to) {
   3428                 from = lineAtHeight(doc, heightAtLine(getLine(doc, ensureTo)) - display.wrapper.clientHeight)
   3429                 to = ensureTo
   3430             }
   3431         }
   3432         return {from: from, to: Math.max(to, from + 1)}
   3433     }
   3434 
   3435 // Re-align line numbers and gutter marks to compensate for
   3436 // horizontal scrolling.
   3437     function alignHorizontally(cm) {
   3438         var display = cm.display, view = display.view
   3439         if (!display.alignWidgets && (!display.gutters.firstChild || !cm.options.fixedGutter)) { return }
   3440         var comp = compensateForHScroll(display) - display.scroller.scrollLeft + cm.doc.scrollLeft
   3441         var gutterW = display.gutters.offsetWidth, left = comp + "px"
   3442         for (var i = 0; i < view.length; i++) { if (!view[i].hidden) {
   3443             if (cm.options.fixedGutter) {
   3444                 if (view[i].gutter)
   3445                 { view[i].gutter.style.left = left }
   3446                 if (view[i].gutterBackground)
   3447                 { view[i].gutterBackground.style.left = left }
   3448             }
   3449             var align = view[i].alignable
   3450             if (align) { for (var j = 0; j < align.length; j++)
   3451             { align[j].style.left = left } }
   3452         } }
   3453         if (cm.options.fixedGutter)
   3454         { display.gutters.style.left = (comp + gutterW) + "px" }
   3455     }
   3456 
   3457 // Used to ensure that the line number gutter is still the right
   3458 // size for the current document size. Returns true when an update
   3459 // is needed.
   3460     function maybeUpdateLineNumberWidth(cm) {
   3461         if (!cm.options.lineNumbers) { return false }
   3462         var doc = cm.doc, last = lineNumberFor(cm.options, doc.first + doc.size - 1), display = cm.display
   3463         if (last.length != display.lineNumChars) {
   3464             var test = display.measure.appendChild(elt("div", [elt("div", last)],
   3465                 "CodeMirror-linenumber CodeMirror-gutter-elt"))
   3466             var innerW = test.firstChild.offsetWidth, padding = test.offsetWidth - innerW
   3467             display.lineGutter.style.width = ""
   3468             display.lineNumInnerWidth = Math.max(innerW, display.lineGutter.offsetWidth - padding) + 1
   3469             display.lineNumWidth = display.lineNumInnerWidth + padding
   3470             display.lineNumChars = display.lineNumInnerWidth ? last.length : -1
   3471             display.lineGutter.style.width = display.lineNumWidth + "px"
   3472             updateGutterSpace(cm)
   3473             return true
   3474         }
   3475         return false
   3476     }
   3477 
   3478 // SCROLLING THINGS INTO VIEW
   3479 
   3480 // If an editor sits on the top or bottom of the window, partially
   3481 // scrolled out of view, this ensures that the cursor is visible.
   3482     function maybeScrollWindow(cm, rect) {
   3483         if (signalDOMEvent(cm, "scrollCursorIntoView")) { return }
   3484 
   3485         var display = cm.display, box = display.sizer.getBoundingClientRect(), doScroll = null
   3486         if (rect.top + box.top < 0) { doScroll = true }
   3487         else if (rect.bottom + box.top > (window.innerHeight || document.documentElement.clientHeight)) { doScroll = false }
   3488         if (doScroll != null && !phantom) {
   3489             var scrollNode = elt("div", "\u200b", null, ("position: absolute;\n                         top: " + (rect.top - display.viewOffset - paddingTop(cm.display)) + "px;\n                         height: " + (rect.bottom - rect.top + scrollGap(cm) + display.barHeight) + "px;\n                         left: " + (rect.left) + "px; width: " + (Math.max(2, rect.right - rect.left)) + "px;"))
   3490             cm.display.lineSpace.appendChild(scrollNode)
   3491             scrollNode.scrollIntoView(doScroll)
   3492             cm.display.lineSpace.removeChild(scrollNode)
   3493         }
   3494     }
   3495 
   3496 // Scroll a given position into view (immediately), verifying that
   3497 // it actually became visible (as line heights are accurately
   3498 // measured, the position of something may 'drift' during drawing).
   3499     function scrollPosIntoView(cm, pos, end, margin) {
   3500         if (margin == null) { margin = 0 }
   3501         var rect
   3502         if (!cm.options.lineWrapping && pos == end) {
   3503             // Set pos and end to the cursor positions around the character pos sticks to
   3504             // If pos.sticky == "before", that is around pos.ch - 1, otherwise around pos.ch
   3505             // If pos == Pos(_, 0, "before"), pos and end are unchanged
   3506             pos = pos.ch ? Pos(pos.line, pos.sticky == "before" ? pos.ch - 1 : pos.ch, "after") : pos
   3507             end = pos.sticky == "before" ? Pos(pos.line, pos.ch + 1, "before") : pos
   3508         }
   3509         for (var limit = 0; limit < 5; limit++) {
   3510             var changed = false
   3511             var coords = cursorCoords(cm, pos)
   3512             var endCoords = !end || end == pos ? coords : cursorCoords(cm, end)
   3513             rect = {left: Math.min(coords.left, endCoords.left),
   3514                 top: Math.min(coords.top, endCoords.top) - margin,
   3515                 right: Math.max(coords.left, endCoords.left),
   3516                 bottom: Math.max(coords.bottom, endCoords.bottom) + margin}
   3517             var scrollPos = calculateScrollPos(cm, rect)
   3518             var startTop = cm.doc.scrollTop, startLeft = cm.doc.scrollLeft
   3519             if (scrollPos.scrollTop != null) {
   3520                 updateScrollTop(cm, scrollPos.scrollTop)
   3521                 if (Math.abs(cm.doc.scrollTop - startTop) > 1) { changed = true }
   3522             }
   3523             if (scrollPos.scrollLeft != null) {
   3524                 setScrollLeft(cm, scrollPos.scrollLeft)
   3525                 if (Math.abs(cm.doc.scrollLeft - startLeft) > 1) { changed = true }
   3526             }
   3527             if (!changed) { break }
   3528         }
   3529         return rect
   3530     }
   3531 
   3532 // Scroll a given set of coordinates into view (immediately).
   3533     function scrollIntoView(cm, rect) {
   3534         var scrollPos = calculateScrollPos(cm, rect)
   3535         if (scrollPos.scrollTop != null) { updateScrollTop(cm, scrollPos.scrollTop) }
   3536         if (scrollPos.scrollLeft != null) { setScrollLeft(cm, scrollPos.scrollLeft) }
   3537     }
   3538 
   3539 // Calculate a new scroll position needed to scroll the given
   3540 // rectangle into view. Returns an object with scrollTop and
   3541 // scrollLeft properties. When these are undefined, the
   3542 // vertical/horizontal position does not need to be adjusted.
   3543     function calculateScrollPos(cm, rect) {
   3544         var display = cm.display, snapMargin = textHeight(cm.display)
   3545         if (rect.top < 0) { rect.top = 0 }
   3546         var screentop = cm.curOp && cm.curOp.scrollTop != null ? cm.curOp.scrollTop : display.scroller.scrollTop
   3547         var screen = displayHeight(cm), result = {}
   3548         if (rect.bottom - rect.top > screen) { rect.bottom = rect.top + screen }
   3549         var docBottom = cm.doc.height + paddingVert(display)
   3550         var atTop = rect.top < snapMargin, atBottom = rect.bottom > docBottom - snapMargin
   3551         if (rect.top < screentop) {
   3552             result.scrollTop = atTop ? 0 : rect.top
   3553         } else if (rect.bottom > screentop + screen) {
   3554             var newTop = Math.min(rect.top, (atBottom ? docBottom : rect.bottom) - screen)
   3555             if (newTop != screentop) { result.scrollTop = newTop }
   3556         }
   3557 
   3558         var screenleft = cm.curOp && cm.curOp.scrollLeft != null ? cm.curOp.scrollLeft : display.scroller.scrollLeft
   3559         var screenw = displayWidth(cm) - (cm.options.fixedGutter ? display.gutters.offsetWidth : 0)
   3560         var tooWide = rect.right - rect.left > screenw
   3561         if (tooWide) { rect.right = rect.left + screenw }
   3562         if (rect.left < 10)
   3563         { result.scrollLeft = 0 }
   3564         else if (rect.left < screenleft)
   3565         { result.scrollLeft = Math.max(0, rect.left - (tooWide ? 0 : 10)) }
   3566         else if (rect.right > screenw + screenleft - 3)
   3567         { result.scrollLeft = rect.right + (tooWide ? 0 : 10) - screenw }
   3568         return result
   3569     }
   3570 
   3571 // Store a relative adjustment to the scroll position in the current
   3572 // operation (to be applied when the operation finishes).
   3573     function addToScrollTop(cm, top) {
   3574         if (top == null) { return }
   3575         resolveScrollToPos(cm)
   3576         cm.curOp.scrollTop = (cm.curOp.scrollTop == null ? cm.doc.scrollTop : cm.curOp.scrollTop) + top
   3577     }
   3578 
   3579 // Make sure that at the end of the operation the current cursor is
   3580 // shown.
   3581     function ensureCursorVisible(cm) {
   3582         resolveScrollToPos(cm)
   3583         var cur = cm.getCursor()
   3584         cm.curOp.scrollToPos = {from: cur, to: cur, margin: cm.options.cursorScrollMargin}
   3585     }
   3586 
   3587     function scrollToCoords(cm, x, y) {
   3588         if (x != null || y != null) { resolveScrollToPos(cm) }
   3589         if (x != null) { cm.curOp.scrollLeft = x }
   3590         if (y != null) { cm.curOp.scrollTop = y }
   3591     }
   3592 
   3593     function scrollToRange(cm, range) {
   3594         resolveScrollToPos(cm)
   3595         cm.curOp.scrollToPos = range
   3596     }
   3597 
   3598 // When an operation has its scrollToPos property set, and another
   3599 // scroll action is applied before the end of the operation, this
   3600 // 'simulates' scrolling that position into view in a cheap way, so
   3601 // that the effect of intermediate scroll commands is not ignored.
   3602     function resolveScrollToPos(cm) {
   3603         var range = cm.curOp.scrollToPos
   3604         if (range) {
   3605             cm.curOp.scrollToPos = null
   3606             var from = estimateCoords(cm, range.from), to = estimateCoords(cm, range.to)
   3607             scrollToCoordsRange(cm, from, to, range.margin)
   3608         }
   3609     }
   3610 
   3611     function scrollToCoordsRange(cm, from, to, margin) {
   3612         var sPos = calculateScrollPos(cm, {
   3613             left: Math.min(from.left, to.left),
   3614             top: Math.min(from.top, to.top) - margin,
   3615             right: Math.max(from.right, to.right),
   3616             bottom: Math.max(from.bottom, to.bottom) + margin
   3617         })
   3618         scrollToCoords(cm, sPos.scrollLeft, sPos.scrollTop)
   3619     }
   3620 
   3621 // Sync the scrollable area and scrollbars, ensure the viewport
   3622 // covers the visible area.
   3623     function updateScrollTop(cm, val) {
   3624         if (Math.abs(cm.doc.scrollTop - val) < 2) { return }
   3625         if (!gecko) { updateDisplaySimple(cm, {top: val}) }
   3626         setScrollTop(cm, val, true)
   3627         if (gecko) { updateDisplaySimple(cm) }
   3628         startWorker(cm, 100)
   3629     }
   3630 
   3631     function setScrollTop(cm, val, forceScroll) {
   3632         val = Math.min(cm.display.scroller.scrollHeight - cm.display.scroller.clientHeight, val)
   3633         if (cm.display.scroller.scrollTop == val && !forceScroll) { return }
   3634         cm.doc.scrollTop = val
   3635         cm.display.scrollbars.setScrollTop(val)
   3636         if (cm.display.scroller.scrollTop != val) { cm.display.scroller.scrollTop = val }
   3637     }
   3638 
   3639 // Sync scroller and scrollbar, ensure the gutter elements are
   3640 // aligned.
   3641     function setScrollLeft(cm, val, isScroller, forceScroll) {
   3642         val = Math.min(val, cm.display.scroller.scrollWidth - cm.display.scroller.clientWidth)
   3643         if ((isScroller ? val == cm.doc.scrollLeft : Math.abs(cm.doc.scrollLeft - val) < 2) && !forceScroll) { return }
   3644         cm.doc.scrollLeft = val
   3645         alignHorizontally(cm)
   3646         if (cm.display.scroller.scrollLeft != val) { cm.display.scroller.scrollLeft = val }
   3647         cm.display.scrollbars.setScrollLeft(val)
   3648     }
   3649 
   3650 // SCROLLBARS
   3651 
   3652 // Prepare DOM reads needed to update the scrollbars. Done in one
   3653 // shot to minimize update/measure roundtrips.
   3654     function measureForScrollbars(cm) {
   3655         var d = cm.display, gutterW = d.gutters.offsetWidth
   3656         var docH = Math.round(cm.doc.height + paddingVert(cm.display))
   3657         return {
   3658             clientHeight: d.scroller.clientHeight,
   3659             viewHeight: d.wrapper.clientHeight,
   3660             scrollWidth: d.scroller.scrollWidth, clientWidth: d.scroller.clientWidth,
   3661             viewWidth: d.wrapper.clientWidth,
   3662             barLeft: cm.options.fixedGutter ? gutterW : 0,
   3663             docHeight: docH,
   3664             scrollHeight: docH + scrollGap(cm) + d.barHeight,
   3665             nativeBarWidth: d.nativeBarWidth,
   3666             gutterWidth: gutterW
   3667         }
   3668     }
   3669 
   3670     var NativeScrollbars = function(place, scroll, cm) {
   3671         this.cm = cm
   3672         var vert = this.vert = elt("div", [elt("div", null, null, "min-width: 1px")], "CodeMirror-vscrollbar")
   3673         var horiz = this.horiz = elt("div", [elt("div", null, null, "height: 100%; min-height: 1px")], "CodeMirror-hscrollbar")
   3674         place(vert); place(horiz)
   3675 
   3676         on(vert, "scroll", function () {
   3677             if (vert.clientHeight) { scroll(vert.scrollTop, "vertical") }
   3678         })
   3679         on(horiz, "scroll", function () {
   3680             if (horiz.clientWidth) { scroll(horiz.scrollLeft, "horizontal") }
   3681         })
   3682 
   3683         this.checkedZeroWidth = false
   3684         // Need to set a minimum width to see the scrollbar on IE7 (but must not set it on IE8).
   3685         if (ie && ie_version < 8) { this.horiz.style.minHeight = this.vert.style.minWidth = "18px" }
   3686     };
   3687 
   3688     NativeScrollbars.prototype.update = function (measure) {
   3689         var needsH = measure.scrollWidth > measure.clientWidth + 1
   3690         var needsV = measure.scrollHeight > measure.clientHeight + 1
   3691         var sWidth = measure.nativeBarWidth
   3692 
   3693         if (needsV) {
   3694             this.vert.style.display = "block"
   3695             this.vert.style.bottom = needsH ? sWidth + "px" : "0"
   3696             var totalHeight = measure.viewHeight - (needsH ? sWidth : 0)
   3697             // A bug in IE8 can cause this value to be negative, so guard it.
   3698             this.vert.firstChild.style.height =
   3699                 Math.max(0, measure.scrollHeight - measure.clientHeight + totalHeight) + "px"
   3700         } else {
   3701             this.vert.style.display = ""
   3702             this.vert.firstChild.style.height = "0"
   3703         }
   3704 
   3705         if (needsH) {
   3706             this.horiz.style.display = "block"
   3707             this.horiz.style.right = needsV ? sWidth + "px" : "0"
   3708             this.horiz.style.left = measure.barLeft + "px"
   3709             var totalWidth = measure.viewWidth - measure.barLeft - (needsV ? sWidth : 0)
   3710             this.horiz.firstChild.style.width =
   3711                 Math.max(0, measure.scrollWidth - measure.clientWidth + totalWidth) + "px"
   3712         } else {
   3713             this.horiz.style.display = ""
   3714             this.horiz.firstChild.style.width = "0"
   3715         }
   3716 
   3717         if (!this.checkedZeroWidth && measure.clientHeight > 0) {
   3718             if (sWidth == 0) { this.zeroWidthHack() }
   3719             this.checkedZeroWidth = true
   3720         }
   3721 
   3722         return {right: needsV ? sWidth : 0, bottom: needsH ? sWidth : 0}
   3723     };
   3724 
   3725     NativeScrollbars.prototype.setScrollLeft = function (pos) {
   3726         if (this.horiz.scrollLeft != pos) { this.horiz.scrollLeft = pos }
   3727         if (this.disableHoriz) { this.enableZeroWidthBar(this.horiz, this.disableHoriz, "horiz") }
   3728     };
   3729 
   3730     NativeScrollbars.prototype.setScrollTop = function (pos) {
   3731         if (this.vert.scrollTop != pos) { this.vert.scrollTop = pos }
   3732         if (this.disableVert) { this.enableZeroWidthBar(this.vert, this.disableVert, "vert") }
   3733     };
   3734 
   3735     NativeScrollbars.prototype.zeroWidthHack = function () {
   3736         var w = mac && !mac_geMountainLion ? "12px" : "18px"
   3737         this.horiz.style.height = this.vert.style.width = w
   3738         this.horiz.style.pointerEvents = this.vert.style.pointerEvents = "none"
   3739         this.disableHoriz = new Delayed
   3740         this.disableVert = new Delayed
   3741     };
   3742 
   3743     NativeScrollbars.prototype.enableZeroWidthBar = function (bar, delay, type) {
   3744         bar.style.pointerEvents = "auto"
   3745         function maybeDisable() {
   3746             // To find out whether the scrollbar is still visible, we
   3747             // check whether the element under the pixel in the bottom
   3748             // right corner of the scrollbar box is the scrollbar box
   3749             // itself (when the bar is still visible) or its filler child
   3750             // (when the bar is hidden). If it is still visible, we keep
   3751             // it enabled, if it's hidden, we disable pointer events.
   3752             var box = bar.getBoundingClientRect()
   3753             var elt = type == "vert" ? document.elementFromPoint(box.right - 1, (box.top + box.bottom) / 2)
   3754                 : document.elementFromPoint((box.right + box.left) / 2, box.bottom - 1)
   3755             if (elt != bar) { bar.style.pointerEvents = "none" }
   3756             else { delay.set(1000, maybeDisable) }
   3757         }
   3758         delay.set(1000, maybeDisable)
   3759     };
   3760 
   3761     NativeScrollbars.prototype.clear = function () {
   3762         var parent = this.horiz.parentNode
   3763         parent.removeChild(this.horiz)
   3764         parent.removeChild(this.vert)
   3765     };
   3766 
   3767     var NullScrollbars = function () {};
   3768 
   3769     NullScrollbars.prototype.update = function () { return {bottom: 0, right: 0} };
   3770     NullScrollbars.prototype.setScrollLeft = function () {};
   3771     NullScrollbars.prototype.setScrollTop = function () {};
   3772     NullScrollbars.prototype.clear = function () {};
   3773 
   3774     function updateScrollbars(cm, measure) {
   3775         if (!measure) { measure = measureForScrollbars(cm) }
   3776         var startWidth = cm.display.barWidth, startHeight = cm.display.barHeight
   3777         updateScrollbarsInner(cm, measure)
   3778         for (var i = 0; i < 4 && startWidth != cm.display.barWidth || startHeight != cm.display.barHeight; i++) {
   3779             if (startWidth != cm.display.barWidth && cm.options.lineWrapping)
   3780             { updateHeightsInViewport(cm) }
   3781             updateScrollbarsInner(cm, measureForScrollbars(cm))
   3782             startWidth = cm.display.barWidth; startHeight = cm.display.barHeight
   3783         }
   3784     }
   3785 
   3786 // Re-synchronize the fake scrollbars with the actual size of the
   3787 // content.
   3788     function updateScrollbarsInner(cm, measure) {
   3789         var d = cm.display
   3790         var sizes = d.scrollbars.update(measure)
   3791 
   3792         d.sizer.style.paddingRight = (d.barWidth = sizes.right) + "px"
   3793         d.sizer.style.paddingBottom = (d.barHeight = sizes.bottom) + "px"
   3794         d.heightForcer.style.borderBottom = sizes.bottom + "px solid transparent"
   3795 
   3796         if (sizes.right && sizes.bottom) {
   3797             d.scrollbarFiller.style.display = "block"
   3798             d.scrollbarFiller.style.height = sizes.bottom + "px"
   3799             d.scrollbarFiller.style.width = sizes.right + "px"
   3800         } else { d.scrollbarFiller.style.display = "" }
   3801         if (sizes.bottom && cm.options.coverGutterNextToScrollbar && cm.options.fixedGutter) {
   3802             d.gutterFiller.style.display = "block"
   3803             d.gutterFiller.style.height = sizes.bottom + "px"
   3804             d.gutterFiller.style.width = measure.gutterWidth + "px"
   3805         } else { d.gutterFiller.style.display = "" }
   3806     }
   3807 
   3808     var scrollbarModel = {"native": NativeScrollbars, "null": NullScrollbars}
   3809 
   3810     function initScrollbars(cm) {
   3811         if (cm.display.scrollbars) {
   3812             cm.display.scrollbars.clear()
   3813             if (cm.display.scrollbars.addClass)
   3814             { rmClass(cm.display.wrapper, cm.display.scrollbars.addClass) }
   3815         }
   3816 
   3817         cm.display.scrollbars = new scrollbarModel[cm.options.scrollbarStyle](function (node) {
   3818             cm.display.wrapper.insertBefore(node, cm.display.scrollbarFiller)
   3819             // Prevent clicks in the scrollbars from killing focus
   3820             on(node, "mousedown", function () {
   3821                 if (cm.state.focused) { setTimeout(function () { return cm.display.input.focus(); }, 0) }
   3822             })
   3823             node.setAttribute("cm-not-content", "true")
   3824         }, function (pos, axis) {
   3825             if (axis == "horizontal") { setScrollLeft(cm, pos) }
   3826             else { updateScrollTop(cm, pos) }
   3827         }, cm)
   3828         if (cm.display.scrollbars.addClass)
   3829         { addClass(cm.display.wrapper, cm.display.scrollbars.addClass) }
   3830     }
   3831 
   3832 // Operations are used to wrap a series of changes to the editor
   3833 // state in such a way that each change won't have to update the
   3834 // cursor and display (which would be awkward, slow, and
   3835 // error-prone). Instead, display updates are batched and then all
   3836 // combined and executed at once.
   3837 
   3838     var nextOpId = 0
   3839 // Start a new operation.
   3840     function startOperation(cm) {
   3841         cm.curOp = {
   3842             cm: cm,
   3843             viewChanged: false,      // Flag that indicates that lines might need to be redrawn
   3844             startHeight: cm.doc.height, // Used to detect need to update scrollbar
   3845             forceUpdate: false,      // Used to force a redraw
   3846             updateInput: null,       // Whether to reset the input textarea
   3847             typing: false,           // Whether this reset should be careful to leave existing text (for compositing)
   3848             changeObjs: null,        // Accumulated changes, for firing change events
   3849             cursorActivityHandlers: null, // Set of handlers to fire cursorActivity on
   3850             cursorActivityCalled: 0, // Tracks which cursorActivity handlers have been called already
   3851             selectionChanged: false, // Whether the selection needs to be redrawn
   3852             updateMaxLine: false,    // Set when the widest line needs to be determined anew
   3853             scrollLeft: null, scrollTop: null, // Intermediate scroll position, not pushed to DOM yet
   3854             scrollToPos: null,       // Used to scroll to a specific position
   3855             focus: false,
   3856             id: ++nextOpId           // Unique ID
   3857         }
   3858         pushOperation(cm.curOp)
   3859     }
   3860 
   3861 // Finish an operation, updating the display and signalling delayed events
   3862     function endOperation(cm) {
   3863         var op = cm.curOp
   3864         finishOperation(op, function (group) {
   3865             for (var i = 0; i < group.ops.length; i++)
   3866             { group.ops[i].cm.curOp = null }
   3867             endOperations(group)
   3868         })
   3869     }
   3870 
   3871 // The DOM updates done when an operation finishes are batched so
   3872 // that the minimum number of relayouts are required.
   3873     function endOperations(group) {
   3874         var ops = group.ops
   3875         for (var i = 0; i < ops.length; i++) // Read DOM
   3876         { endOperation_R1(ops[i]) }
   3877         for (var i$1 = 0; i$1 < ops.length; i$1++) // Write DOM (maybe)
   3878         { endOperation_W1(ops[i$1]) }
   3879         for (var i$2 = 0; i$2 < ops.length; i$2++) // Read DOM
   3880         { endOperation_R2(ops[i$2]) }
   3881         for (var i$3 = 0; i$3 < ops.length; i$3++) // Write DOM (maybe)
   3882         { endOperation_W2(ops[i$3]) }
   3883         for (var i$4 = 0; i$4 < ops.length; i$4++) // Read DOM
   3884         { endOperation_finish(ops[i$4]) }
   3885     }
   3886 
   3887     function endOperation_R1(op) {
   3888         var cm = op.cm, display = cm.display
   3889         maybeClipScrollbars(cm)
   3890         if (op.updateMaxLine) { findMaxLine(cm) }
   3891 
   3892         op.mustUpdate = op.viewChanged || op.forceUpdate || op.scrollTop != null ||
   3893             op.scrollToPos && (op.scrollToPos.from.line < display.viewFrom ||
   3894                 op.scrollToPos.to.line >= display.viewTo) ||
   3895             display.maxLineChanged && cm.options.lineWrapping
   3896         op.update = op.mustUpdate &&
   3897             new DisplayUpdate(cm, op.mustUpdate && {top: op.scrollTop, ensure: op.scrollToPos}, op.forceUpdate)
   3898     }
   3899 
   3900     function endOperation_W1(op) {
   3901         op.updatedDisplay = op.mustUpdate && updateDisplayIfNeeded(op.cm, op.update)
   3902     }
   3903 
   3904     function endOperation_R2(op) {
   3905         var cm = op.cm, display = cm.display
   3906         if (op.updatedDisplay) { updateHeightsInViewport(cm) }
   3907 
   3908         op.barMeasure = measureForScrollbars(cm)
   3909 
   3910         // If the max line changed since it was last measured, measure it,
   3911         // and ensure the document's width matches it.
   3912         // updateDisplay_W2 will use these properties to do the actual resizing
   3913         if (display.maxLineChanged && !cm.options.lineWrapping) {
   3914             op.adjustWidthTo = measureChar(cm, display.maxLine, display.maxLine.text.length).left + 3
   3915             cm.display.sizerWidth = op.adjustWidthTo
   3916             op.barMeasure.scrollWidth =
   3917                 Math.max(display.scroller.clientWidth, display.sizer.offsetLeft + op.adjustWidthTo + scrollGap(cm) + cm.display.barWidth)
   3918             op.maxScrollLeft = Math.max(0, display.sizer.offsetLeft + op.adjustWidthTo - displayWidth(cm))
   3919         }
   3920 
   3921         if (op.updatedDisplay || op.selectionChanged)
   3922         { op.preparedSelection = display.input.prepareSelection() }
   3923     }
   3924 
   3925     function endOperation_W2(op) {
   3926         var cm = op.cm
   3927 
   3928         if (op.adjustWidthTo != null) {
   3929             cm.display.sizer.style.minWidth = op.adjustWidthTo + "px"
   3930             if (op.maxScrollLeft < cm.doc.scrollLeft)
   3931             { setScrollLeft(cm, Math.min(cm.display.scroller.scrollLeft, op.maxScrollLeft), true) }
   3932             cm.display.maxLineChanged = false
   3933         }
   3934 
   3935         var takeFocus = op.focus && op.focus == activeElt()
   3936         if (op.preparedSelection)
   3937         { cm.display.input.showSelection(op.preparedSelection, takeFocus) }
   3938         if (op.updatedDisplay || op.startHeight != cm.doc.height)
   3939         { updateScrollbars(cm, op.barMeasure) }
   3940         if (op.updatedDisplay)
   3941         { setDocumentHeight(cm, op.barMeasure) }
   3942 
   3943         if (op.selectionChanged) { restartBlink(cm) }
   3944 
   3945         if (cm.state.focused && op.updateInput)
   3946         { cm.display.input.reset(op.typing) }
   3947         if (takeFocus) { ensureFocus(op.cm) }
   3948     }
   3949 
   3950     function endOperation_finish(op) {
   3951         var cm = op.cm, display = cm.display, doc = cm.doc
   3952 
   3953         if (op.updatedDisplay) { postUpdateDisplay(cm, op.update) }
   3954 
   3955         // Abort mouse wheel delta measurement, when scrolling explicitly
   3956         if (display.wheelStartX != null && (op.scrollTop != null || op.scrollLeft != null || op.scrollToPos))
   3957         { display.wheelStartX = display.wheelStartY = null }
   3958 
   3959         // Propagate the scroll position to the actual DOM scroller
   3960         if (op.scrollTop != null) { setScrollTop(cm, op.scrollTop, op.forceScroll) }
   3961 
   3962         if (op.scrollLeft != null) { setScrollLeft(cm, op.scrollLeft, true, true) }
   3963         // If we need to scroll a specific position into view, do so.
   3964         if (op.scrollToPos) {
   3965             var rect = scrollPosIntoView(cm, clipPos(doc, op.scrollToPos.from),
   3966                 clipPos(doc, op.scrollToPos.to), op.scrollToPos.margin)
   3967             maybeScrollWindow(cm, rect)
   3968         }
   3969 
   3970         // Fire events for markers that are hidden/unidden by editing or
   3971         // undoing
   3972         var hidden = op.maybeHiddenMarkers, unhidden = op.maybeUnhiddenMarkers
   3973         if (hidden) { for (var i = 0; i < hidden.length; ++i)
   3974         { if (!hidden[i].lines.length) { signal(hidden[i], "hide") } } }
   3975         if (unhidden) { for (var i$1 = 0; i$1 < unhidden.length; ++i$1)
   3976         { if (unhidden[i$1].lines.length) { signal(unhidden[i$1], "unhide") } } }
   3977 
   3978         if (display.wrapper.offsetHeight)
   3979         { doc.scrollTop = cm.display.scroller.scrollTop }
   3980 
   3981         // Fire change events, and delayed event handlers
   3982         if (op.changeObjs)
   3983         { signal(cm, "changes", cm, op.changeObjs) }
   3984         if (op.update)
   3985         { op.update.finish() }
   3986     }
   3987 
   3988 // Run the given function in an operation
   3989     function runInOp(cm, f) {
   3990         if (cm.curOp) { return f() }
   3991         startOperation(cm)
   3992         try { return f() }
   3993         finally { endOperation(cm) }
   3994     }
   3995 // Wraps a function in an operation. Returns the wrapped function.
   3996     function operation(cm, f) {
   3997         return function() {
   3998             if (cm.curOp) { return f.apply(cm, arguments) }
   3999             startOperation(cm)
   4000             try { return f.apply(cm, arguments) }
   4001             finally { endOperation(cm) }
   4002         }
   4003     }
   4004 // Used to add methods to editor and doc instances, wrapping them in
   4005 // operations.
   4006     function methodOp(f) {
   4007         return function() {
   4008             if (this.curOp) { return f.apply(this, arguments) }
   4009             startOperation(this)
   4010             try { return f.apply(this, arguments) }
   4011             finally { endOperation(this) }
   4012         }
   4013     }
   4014     function docMethodOp(f) {
   4015         return function() {
   4016             var cm = this.cm
   4017             if (!cm || cm.curOp) { return f.apply(this, arguments) }
   4018             startOperation(cm)
   4019             try { return f.apply(this, arguments) }
   4020             finally { endOperation(cm) }
   4021         }
   4022     }
   4023 
   4024 // Updates the display.view data structure for a given change to the
   4025 // document. From and to are in pre-change coordinates. Lendiff is
   4026 // the amount of lines added or subtracted by the change. This is
   4027 // used for changes that span multiple lines, or change the way
   4028 // lines are divided into visual lines. regLineChange (below)
   4029 // registers single-line changes.
   4030     function regChange(cm, from, to, lendiff) {
   4031         if (from == null) { from = cm.doc.first }
   4032         if (to == null) { to = cm.doc.first + cm.doc.size }
   4033         if (!lendiff) { lendiff = 0 }
   4034 
   4035         var display = cm.display
   4036         if (lendiff && to < display.viewTo &&
   4037             (display.updateLineNumbers == null || display.updateLineNumbers > from))
   4038         { display.updateLineNumbers = from }
   4039 
   4040         cm.curOp.viewChanged = true
   4041 
   4042         if (from >= display.viewTo) { // Change after
   4043             if (sawCollapsedSpans && visualLineNo(cm.doc, from) < display.viewTo)
   4044             { resetView(cm) }
   4045         } else if (to <= display.viewFrom) { // Change before
   4046             if (sawCollapsedSpans && visualLineEndNo(cm.doc, to + lendiff) > display.viewFrom) {
   4047                 resetView(cm)
   4048             } else {
   4049                 display.viewFrom += lendiff
   4050                 display.viewTo += lendiff
   4051             }
   4052         } else if (from <= display.viewFrom && to >= display.viewTo) { // Full overlap
   4053             resetView(cm)
   4054         } else if (from <= display.viewFrom) { // Top overlap
   4055             var cut = viewCuttingPoint(cm, to, to + lendiff, 1)
   4056             if (cut) {
   4057                 display.view = display.view.slice(cut.index)
   4058                 display.viewFrom = cut.lineN
   4059                 display.viewTo += lendiff
   4060             } else {
   4061                 resetView(cm)
   4062             }
   4063         } else if (to >= display.viewTo) { // Bottom overlap
   4064             var cut$1 = viewCuttingPoint(cm, from, from, -1)
   4065             if (cut$1) {
   4066                 display.view = display.view.slice(0, cut$1.index)
   4067                 display.viewTo = cut$1.lineN
   4068             } else {
   4069                 resetView(cm)
   4070             }
   4071         } else { // Gap in the middle
   4072             var cutTop = viewCuttingPoint(cm, from, from, -1)
   4073             var cutBot = viewCuttingPoint(cm, to, to + lendiff, 1)
   4074             if (cutTop && cutBot) {
   4075                 display.view = display.view.slice(0, cutTop.index)
   4076                     .concat(buildViewArray(cm, cutTop.lineN, cutBot.lineN))
   4077                     .concat(display.view.slice(cutBot.index))
   4078                 display.viewTo += lendiff
   4079             } else {
   4080                 resetView(cm)
   4081             }
   4082         }
   4083 
   4084         var ext = display.externalMeasured
   4085         if (ext) {
   4086             if (to < ext.lineN)
   4087             { ext.lineN += lendiff }
   4088             else if (from < ext.lineN + ext.size)
   4089             { display.externalMeasured = null }
   4090         }
   4091     }
   4092 
   4093 // Register a change to a single line. Type must be one of "text",
   4094 // "gutter", "class", "widget"
   4095     function regLineChange(cm, line, type) {
   4096         cm.curOp.viewChanged = true
   4097         var display = cm.display, ext = cm.display.externalMeasured
   4098         if (ext && line >= ext.lineN && line < ext.lineN + ext.size)
   4099         { display.externalMeasured = null }
   4100 
   4101         if (line < display.viewFrom || line >= display.viewTo) { return }
   4102         var lineView = display.view[findViewIndex(cm, line)]
   4103         if (lineView.node == null) { return }
   4104         var arr = lineView.changes || (lineView.changes = [])
   4105         if (indexOf(arr, type) == -1) { arr.push(type) }
   4106     }
   4107 
   4108 // Clear the view.
   4109     function resetView(cm) {
   4110         cm.display.viewFrom = cm.display.viewTo = cm.doc.first
   4111         cm.display.view = []
   4112         cm.display.viewOffset = 0
   4113     }
   4114 
   4115     function viewCuttingPoint(cm, oldN, newN, dir) {
   4116         var index = findViewIndex(cm, oldN), diff, view = cm.display.view
   4117         if (!sawCollapsedSpans || newN == cm.doc.first + cm.doc.size)
   4118         { return {index: index, lineN: newN} }
   4119         var n = cm.display.viewFrom
   4120         for (var i = 0; i < index; i++)
   4121         { n += view[i].size }
   4122         if (n != oldN) {
   4123             if (dir > 0) {
   4124                 if (index == view.length - 1) { return null }
   4125                 diff = (n + view[index].size) - oldN
   4126                 index++
   4127             } else {
   4128                 diff = n - oldN
   4129             }
   4130             oldN += diff; newN += diff
   4131         }
   4132         while (visualLineNo(cm.doc, newN) != newN) {
   4133             if (index == (dir < 0 ? 0 : view.length - 1)) { return null }
   4134             newN += dir * view[index - (dir < 0 ? 1 : 0)].size
   4135             index += dir
   4136         }
   4137         return {index: index, lineN: newN}
   4138     }
   4139 
   4140 // Force the view to cover a given range, adding empty view element
   4141 // or clipping off existing ones as needed.
   4142     function adjustView(cm, from, to) {
   4143         var display = cm.display, view = display.view
   4144         if (view.length == 0 || from >= display.viewTo || to <= display.viewFrom) {
   4145             display.view = buildViewArray(cm, from, to)
   4146             display.viewFrom = from
   4147         } else {
   4148             if (display.viewFrom > from)
   4149             { display.view = buildViewArray(cm, from, display.viewFrom).concat(display.view) }
   4150             else if (display.viewFrom < from)
   4151             { display.view = display.view.slice(findViewIndex(cm, from)) }
   4152             display.viewFrom = from
   4153             if (display.viewTo < to)
   4154             { display.view = display.view.concat(buildViewArray(cm, display.viewTo, to)) }
   4155             else if (display.viewTo > to)
   4156             { display.view = display.view.slice(0, findViewIndex(cm, to)) }
   4157         }
   4158         display.viewTo = to
   4159     }
   4160 
   4161 // Count the number of lines in the view whose DOM representation is
   4162 // out of date (or nonexistent).
   4163     function countDirtyView(cm) {
   4164         var view = cm.display.view, dirty = 0
   4165         for (var i = 0; i < view.length; i++) {
   4166             var lineView = view[i]
   4167             if (!lineView.hidden && (!lineView.node || lineView.changes)) { ++dirty }
   4168         }
   4169         return dirty
   4170     }
   4171 
   4172 // HIGHLIGHT WORKER
   4173 
   4174     function startWorker(cm, time) {
   4175         if (cm.doc.highlightFrontier < cm.display.viewTo)
   4176         { cm.state.highlight.set(time, bind(highlightWorker, cm)) }
   4177     }
   4178 
   4179     function highlightWorker(cm) {
   4180         var doc = cm.doc
   4181         if (doc.highlightFrontier >= cm.display.viewTo) { return }
   4182         var end = +new Date + cm.options.workTime
   4183         var context = getContextBefore(cm, doc.highlightFrontier)
   4184         var changedLines = []
   4185 
   4186         doc.iter(context.line, Math.min(doc.first + doc.size, cm.display.viewTo + 500), function (line) {
   4187             if (context.line >= cm.display.viewFrom) { // Visible
   4188                 var oldStyles = line.styles
   4189                 var resetState = line.text.length > cm.options.maxHighlightLength ? copyState(doc.mode, context.state) : null
   4190                 var highlighted = highlightLine(cm, line, context, true)
   4191                 if (resetState) { context.state = resetState }
   4192                 line.styles = highlighted.styles
   4193                 var oldCls = line.styleClasses, newCls = highlighted.classes
   4194                 if (newCls) { line.styleClasses = newCls }
   4195                 else if (oldCls) { line.styleClasses = null }
   4196                 var ischange = !oldStyles || oldStyles.length != line.styles.length ||
   4197                     oldCls != newCls && (!oldCls || !newCls || oldCls.bgClass != newCls.bgClass || oldCls.textClass != newCls.textClass)
   4198                 for (var i = 0; !ischange && i < oldStyles.length; ++i) { ischange = oldStyles[i] != line.styles[i] }
   4199                 if (ischange) { changedLines.push(context.line) }
   4200                 line.stateAfter = context.save()
   4201                 context.nextLine()
   4202             } else {
   4203                 if (line.text.length <= cm.options.maxHighlightLength)
   4204                 { processLine(cm, line.text, context) }
   4205                 line.stateAfter = context.line % 5 == 0 ? context.save() : null
   4206                 context.nextLine()
   4207             }
   4208             if (+new Date > end) {
   4209                 startWorker(cm, cm.options.workDelay)
   4210                 return true
   4211             }
   4212         })
   4213         doc.highlightFrontier = context.line
   4214         doc.modeFrontier = Math.max(doc.modeFrontier, context.line)
   4215         if (changedLines.length) { runInOp(cm, function () {
   4216             for (var i = 0; i < changedLines.length; i++)
   4217             { regLineChange(cm, changedLines[i], "text") }
   4218         }) }
   4219     }
   4220 
   4221 // DISPLAY DRAWING
   4222 
   4223     var DisplayUpdate = function(cm, viewport, force) {
   4224         var display = cm.display
   4225 
   4226         this.viewport = viewport
   4227         // Store some values that we'll need later (but don't want to force a relayout for)
   4228         this.visible = visibleLines(display, cm.doc, viewport)
   4229         this.editorIsHidden = !display.wrapper.offsetWidth
   4230         this.wrapperHeight = display.wrapper.clientHeight
   4231         this.wrapperWidth = display.wrapper.clientWidth
   4232         this.oldDisplayWidth = displayWidth(cm)
   4233         this.force = force
   4234         this.dims = getDimensions(cm)
   4235         this.events = []
   4236     };
   4237 
   4238     DisplayUpdate.prototype.signal = function (emitter, type) {
   4239         if (hasHandler(emitter, type))
   4240         { this.events.push(arguments) }
   4241     };
   4242     DisplayUpdate.prototype.finish = function () {
   4243         var this$1 = this;
   4244 
   4245         for (var i = 0; i < this.events.length; i++)
   4246         { signal.apply(null, this$1.events[i]) }
   4247     };
   4248 
   4249     function maybeClipScrollbars(cm) {
   4250         var display = cm.display
   4251         if (!display.scrollbarsClipped && display.scroller.offsetWidth) {
   4252             display.nativeBarWidth = display.scroller.offsetWidth - display.scroller.clientWidth
   4253             display.heightForcer.style.height = scrollGap(cm) + "px"
   4254             display.sizer.style.marginBottom = -display.nativeBarWidth + "px"
   4255             display.sizer.style.borderRightWidth = scrollGap(cm) + "px"
   4256             display.scrollbarsClipped = true
   4257         }
   4258     }
   4259 
   4260     function selectionSnapshot(cm) {
   4261         if (cm.hasFocus()) { return null }
   4262         var active = activeElt()
   4263         if (!active || !contains(cm.display.lineDiv, active)) { return null }
   4264         var result = {activeElt: active}
   4265         if (window.getSelection) {
   4266             var sel = window.getSelection()
   4267             if (sel.anchorNode && sel.extend && contains(cm.display.lineDiv, sel.anchorNode)) {
   4268                 result.anchorNode = sel.anchorNode
   4269                 result.anchorOffset = sel.anchorOffset
   4270                 result.focusNode = sel.focusNode
   4271                 result.focusOffset = sel.focusOffset
   4272             }
   4273         }
   4274         return result
   4275     }
   4276 
   4277     function restoreSelection(snapshot) {
   4278         if (!snapshot || !snapshot.activeElt || snapshot.activeElt == activeElt()) { return }
   4279         snapshot.activeElt.focus()
   4280         if (snapshot.anchorNode && contains(document.body, snapshot.anchorNode) && contains(document.body, snapshot.focusNode)) {
   4281             var sel = window.getSelection(), range = document.createRange()
   4282             range.setEnd(snapshot.anchorNode, snapshot.anchorOffset)
   4283             range.collapse(false)
   4284             sel.removeAllRanges()
   4285             sel.addRange(range)
   4286             sel.extend(snapshot.focusNode, snapshot.focusOffset)
   4287         }
   4288     }
   4289 
   4290 // Does the actual updating of the line display. Bails out
   4291 // (returning false) when there is nothing to be done and forced is
   4292 // false.
   4293     function updateDisplayIfNeeded(cm, update) {
   4294         var display = cm.display, doc = cm.doc
   4295 
   4296         if (update.editorIsHidden) {
   4297             resetView(cm)
   4298             return false
   4299         }
   4300 
   4301         // Bail out if the visible area is already rendered and nothing changed.
   4302         if (!update.force &&
   4303             update.visible.from >= display.viewFrom && update.visible.to <= display.viewTo &&
   4304             (display.updateLineNumbers == null || display.updateLineNumbers >= display.viewTo) &&
   4305             display.renderedView == display.view && countDirtyView(cm) == 0)
   4306         { return false }
   4307 
   4308         if (maybeUpdateLineNumberWidth(cm)) {
   4309             resetView(cm)
   4310             update.dims = getDimensions(cm)
   4311         }
   4312 
   4313         // Compute a suitable new viewport (from & to)
   4314         var end = doc.first + doc.size
   4315         var from = Math.max(update.visible.from - cm.options.viewportMargin, doc.first)
   4316         var to = Math.min(end, update.visible.to + cm.options.viewportMargin)
   4317         if (display.viewFrom < from && from - display.viewFrom < 20) { from = Math.max(doc.first, display.viewFrom) }
   4318         if (display.viewTo > to && display.viewTo - to < 20) { to = Math.min(end, display.viewTo) }
   4319         if (sawCollapsedSpans) {
   4320             from = visualLineNo(cm.doc, from)
   4321             to = visualLineEndNo(cm.doc, to)
   4322         }
   4323 
   4324         var different = from != display.viewFrom || to != display.viewTo ||
   4325             display.lastWrapHeight != update.wrapperHeight || display.lastWrapWidth != update.wrapperWidth
   4326         adjustView(cm, from, to)
   4327 
   4328         display.viewOffset = heightAtLine(getLine(cm.doc, display.viewFrom))
   4329         // Position the mover div to align with the current scroll position
   4330         cm.display.mover.style.top = display.viewOffset + "px"
   4331 
   4332         var toUpdate = countDirtyView(cm)
   4333         if (!different && toUpdate == 0 && !update.force && display.renderedView == display.view &&
   4334             (display.updateLineNumbers == null || display.updateLineNumbers >= display.viewTo))
   4335         { return false }
   4336 
   4337         // For big changes, we hide the enclosing element during the
   4338         // update, since that speeds up the operations on most browsers.
   4339         var selSnapshot = selectionSnapshot(cm)
   4340         if (toUpdate > 4) { display.lineDiv.style.display = "none" }
   4341         patchDisplay(cm, display.updateLineNumbers, update.dims)
   4342         if (toUpdate > 4) { display.lineDiv.style.display = "" }
   4343         display.renderedView = display.view
   4344         // There might have been a widget with a focused element that got
   4345         // hidden or updated, if so re-focus it.
   4346         restoreSelection(selSnapshot)
   4347 
   4348         // Prevent selection and cursors from interfering with the scroll
   4349         // width and height.
   4350         removeChildren(display.cursorDiv)
   4351         removeChildren(display.selectionDiv)
   4352         display.gutters.style.height = display.sizer.style.minHeight = 0
   4353 
   4354         if (different) {
   4355             display.lastWrapHeight = update.wrapperHeight
   4356             display.lastWrapWidth = update.wrapperWidth
   4357             startWorker(cm, 400)
   4358         }
   4359 
   4360         display.updateLineNumbers = null
   4361 
   4362         return true
   4363     }
   4364 
   4365     function postUpdateDisplay(cm, update) {
   4366         var viewport = update.viewport
   4367 
   4368         for (var first = true;; first = false) {
   4369             if (!first || !cm.options.lineWrapping || update.oldDisplayWidth == displayWidth(cm)) {
   4370                 // Clip forced viewport to actual scrollable area.
   4371                 if (viewport && viewport.top != null)
   4372                 { viewport = {top: Math.min(cm.doc.height + paddingVert(cm.display) - displayHeight(cm), viewport.top)} }
   4373                 // Updated line heights might result in the drawn area not
   4374                 // actually covering the viewport. Keep looping until it does.
   4375                 update.visible = visibleLines(cm.display, cm.doc, viewport)
   4376                 if (update.visible.from >= cm.display.viewFrom && update.visible.to <= cm.display.viewTo)
   4377                 { break }
   4378             }
   4379             if (!updateDisplayIfNeeded(cm, update)) { break }
   4380             updateHeightsInViewport(cm)
   4381             var barMeasure = measureForScrollbars(cm)
   4382             updateSelection(cm)
   4383             updateScrollbars(cm, barMeasure)
   4384             setDocumentHeight(cm, barMeasure)
   4385             update.force = false
   4386         }
   4387 
   4388         update.signal(cm, "update", cm)
   4389         if (cm.display.viewFrom != cm.display.reportedViewFrom || cm.display.viewTo != cm.display.reportedViewTo) {
   4390             update.signal(cm, "viewportChange", cm, cm.display.viewFrom, cm.display.viewTo)
   4391             cm.display.reportedViewFrom = cm.display.viewFrom; cm.display.reportedViewTo = cm.display.viewTo
   4392         }
   4393     }
   4394 
   4395     function updateDisplaySimple(cm, viewport) {
   4396         var update = new DisplayUpdate(cm, viewport)
   4397         if (updateDisplayIfNeeded(cm, update)) {
   4398             updateHeightsInViewport(cm)
   4399             postUpdateDisplay(cm, update)
   4400             var barMeasure = measureForScrollbars(cm)
   4401             updateSelection(cm)
   4402             updateScrollbars(cm, barMeasure)
   4403             setDocumentHeight(cm, barMeasure)
   4404             update.finish()
   4405         }
   4406     }
   4407 
   4408 // Sync the actual display DOM structure with display.view, removing
   4409 // nodes for lines that are no longer in view, and creating the ones
   4410 // that are not there yet, and updating the ones that are out of
   4411 // date.
   4412     function patchDisplay(cm, updateNumbersFrom, dims) {
   4413         var display = cm.display, lineNumbers = cm.options.lineNumbers
   4414         var container = display.lineDiv, cur = container.firstChild
   4415 
   4416         function rm(node) {
   4417             var next = node.nextSibling
   4418             // Works around a throw-scroll bug in OS X Webkit
   4419             if (webkit && mac && cm.display.currentWheelTarget == node)
   4420             { node.style.display = "none" }
   4421             else
   4422             { node.parentNode.removeChild(node) }
   4423             return next
   4424         }
   4425 
   4426         var view = display.view, lineN = display.viewFrom
   4427         // Loop over the elements in the view, syncing cur (the DOM nodes
   4428         // in display.lineDiv) with the view as we go.
   4429         for (var i = 0; i < view.length; i++) {
   4430             var lineView = view[i]
   4431             if (lineView.hidden) {
   4432             } else if (!lineView.node || lineView.node.parentNode != container) { // Not drawn yet
   4433                 var node = buildLineElement(cm, lineView, lineN, dims)
   4434                 container.insertBefore(node, cur)
   4435             } else { // Already drawn
   4436                 while (cur != lineView.node) { cur = rm(cur) }
   4437                 var updateNumber = lineNumbers && updateNumbersFrom != null &&
   4438                     updateNumbersFrom <= lineN && lineView.lineNumber
   4439                 if (lineView.changes) {
   4440                     if (indexOf(lineView.changes, "gutter") > -1) { updateNumber = false }
   4441                     updateLineForChanges(cm, lineView, lineN, dims)
   4442                 }
   4443                 if (updateNumber) {
   4444                     removeChildren(lineView.lineNumber)
   4445                     lineView.lineNumber.appendChild(document.createTextNode(lineNumberFor(cm.options, lineN)))
   4446                 }
   4447                 cur = lineView.node.nextSibling
   4448             }
   4449             lineN += lineView.size
   4450         }
   4451         while (cur) { cur = rm(cur) }
   4452     }
   4453 
   4454     function updateGutterSpace(cm) {
   4455         var width = cm.display.gutters.offsetWidth
   4456         cm.display.sizer.style.marginLeft = width + "px"
   4457     }
   4458 
   4459     function setDocumentHeight(cm, measure) {
   4460         cm.display.sizer.style.minHeight = measure.docHeight + "px"
   4461         cm.display.heightForcer.style.top = measure.docHeight + "px"
   4462         cm.display.gutters.style.height = (measure.docHeight + cm.display.barHeight + scrollGap(cm)) + "px"
   4463     }
   4464 
   4465 // Rebuild the gutter elements, ensure the margin to the left of the
   4466 // code matches their width.
   4467     function updateGutters(cm) {
   4468         var gutters = cm.display.gutters, specs = cm.options.gutters
   4469         removeChildren(gutters)
   4470         var i = 0
   4471         for (; i < specs.length; ++i) {
   4472             var gutterClass = specs[i]
   4473             var gElt = gutters.appendChild(elt("div", null, "CodeMirror-gutter " + gutterClass))
   4474             if (gutterClass == "CodeMirror-linenumbers") {
   4475                 cm.display.lineGutter = gElt
   4476                 gElt.style.width = (cm.display.lineNumWidth || 1) + "px"
   4477             }
   4478         }
   4479         gutters.style.display = i ? "" : "none"
   4480         updateGutterSpace(cm)
   4481     }
   4482 
   4483 // Make sure the gutters options contains the element
   4484 // "CodeMirror-linenumbers" when the lineNumbers option is true.
   4485     function setGuttersForLineNumbers(options) {
   4486         var found = indexOf(options.gutters, "CodeMirror-linenumbers")
   4487         if (found == -1 && options.lineNumbers) {
   4488             options.gutters = options.gutters.concat(["CodeMirror-linenumbers"])
   4489         } else if (found > -1 && !options.lineNumbers) {
   4490             options.gutters = options.gutters.slice(0)
   4491             options.gutters.splice(found, 1)
   4492         }
   4493     }
   4494 
   4495     var wheelSamples = 0;
   4496     var wheelPixelsPerUnit = null;
   4497 // Fill in a browser-detected starting value on browsers where we
   4498 // know one. These don't have to be accurate -- the result of them
   4499 // being wrong would just be a slight flicker on the first wheel
   4500 // scroll (if it is large enough).
   4501     if (ie) { wheelPixelsPerUnit = -.53 }
   4502     else if (gecko) { wheelPixelsPerUnit = 15 }
   4503     else if (chrome) { wheelPixelsPerUnit = -.7 }
   4504     else if (safari) { wheelPixelsPerUnit = -1/3 }
   4505 
   4506     function wheelEventDelta(e) {
   4507         var dx = e.wheelDeltaX, dy = e.wheelDeltaY
   4508         if (dx == null && e.detail && e.axis == e.HORIZONTAL_AXIS) { dx = e.detail }
   4509         if (dy == null && e.detail && e.axis == e.VERTICAL_AXIS) { dy = e.detail }
   4510         else if (dy == null) { dy = e.wheelDelta }
   4511         return {x: dx, y: dy}
   4512     }
   4513     function wheelEventPixels(e) {
   4514         var delta = wheelEventDelta(e)
   4515         delta.x *= wheelPixelsPerUnit
   4516         delta.y *= wheelPixelsPerUnit
   4517         return delta
   4518     }
   4519 
   4520     function onScrollWheel(cm, e) {
   4521         var delta = wheelEventDelta(e), dx = delta.x, dy = delta.y
   4522 
   4523         var display = cm.display, scroll = display.scroller
   4524         // Quit if there's nothing to scroll here
   4525         var canScrollX = scroll.scrollWidth > scroll.clientWidth
   4526         var canScrollY = scroll.scrollHeight > scroll.clientHeight
   4527         if (!(dx && canScrollX || dy && canScrollY)) { return }
   4528 
   4529         // Webkit browsers on OS X abort momentum scrolls when the target
   4530         // of the scroll event is removed from the scrollable element.
   4531         // This hack (see related code in patchDisplay) makes sure the
   4532         // element is kept around.
   4533         if (dy && mac && webkit) {
   4534             outer: for (var cur = e.target, view = display.view; cur != scroll; cur = cur.parentNode) {
   4535                 for (var i = 0; i < view.length; i++) {
   4536                     if (view[i].node == cur) {
   4537                         cm.display.currentWheelTarget = cur
   4538                         break outer
   4539                     }
   4540                 }
   4541             }
   4542         }
   4543 
   4544         // On some browsers, horizontal scrolling will cause redraws to
   4545         // happen before the gutter has been realigned, causing it to
   4546         // wriggle around in a most unseemly way. When we have an
   4547         // estimated pixels/delta value, we just handle horizontal
   4548         // scrolling entirely here. It'll be slightly off from native, but
   4549         // better than glitching out.
   4550         if (dx && !gecko && !presto && wheelPixelsPerUnit != null) {
   4551             if (dy && canScrollY)
   4552             { updateScrollTop(cm, Math.max(0, scroll.scrollTop + dy * wheelPixelsPerUnit)) }
   4553             setScrollLeft(cm, Math.max(0, scroll.scrollLeft + dx * wheelPixelsPerUnit))
   4554             // Only prevent default scrolling if vertical scrolling is
   4555             // actually possible. Otherwise, it causes vertical scroll
   4556             // jitter on OSX trackpads when deltaX is small and deltaY
   4557             // is large (issue #3579)
   4558             if (!dy || (dy && canScrollY))
   4559             { e_preventDefault(e) }
   4560             display.wheelStartX = null // Abort measurement, if in progress
   4561             return
   4562         }
   4563 
   4564         // 'Project' the visible viewport to cover the area that is being
   4565         // scrolled into view (if we know enough to estimate it).
   4566         if (dy && wheelPixelsPerUnit != null) {
   4567             var pixels = dy * wheelPixelsPerUnit
   4568             var top = cm.doc.scrollTop, bot = top + display.wrapper.clientHeight
   4569             if (pixels < 0) { top = Math.max(0, top + pixels - 50) }
   4570             else { bot = Math.min(cm.doc.height, bot + pixels + 50) }
   4571             updateDisplaySimple(cm, {top: top, bottom: bot})
   4572         }
   4573 
   4574         if (wheelSamples < 20) {
   4575             if (display.wheelStartX == null) {
   4576                 display.wheelStartX = scroll.scrollLeft; display.wheelStartY = scroll.scrollTop
   4577                 display.wheelDX = dx; display.wheelDY = dy
   4578                 setTimeout(function () {
   4579                     if (display.wheelStartX == null) { return }
   4580                     var movedX = scroll.scrollLeft - display.wheelStartX
   4581                     var movedY = scroll.scrollTop - display.wheelStartY
   4582                     var sample = (movedY && display.wheelDY && movedY / display.wheelDY) ||
   4583                         (movedX && display.wheelDX && movedX / display.wheelDX)
   4584                     display.wheelStartX = display.wheelStartY = null
   4585                     if (!sample) { return }
   4586                     wheelPixelsPerUnit = (wheelPixelsPerUnit * wheelSamples + sample) / (wheelSamples + 1)
   4587                     ++wheelSamples
   4588                 }, 200)
   4589             } else {
   4590                 display.wheelDX += dx; display.wheelDY += dy
   4591             }
   4592         }
   4593     }
   4594 
   4595 // Selection objects are immutable. A new one is created every time
   4596 // the selection changes. A selection is one or more non-overlapping
   4597 // (and non-touching) ranges, sorted, and an integer that indicates
   4598 // which one is the primary selection (the one that's scrolled into
   4599 // view, that getCursor returns, etc).
   4600     var Selection = function(ranges, primIndex) {
   4601         this.ranges = ranges
   4602         this.primIndex = primIndex
   4603     };
   4604 
   4605     Selection.prototype.primary = function () { return this.ranges[this.primIndex] };
   4606 
   4607     Selection.prototype.equals = function (other) {
   4608         var this$1 = this;
   4609 
   4610         if (other == this) { return true }
   4611         if (other.primIndex != this.primIndex || other.ranges.length != this.ranges.length) { return false }
   4612         for (var i = 0; i < this.ranges.length; i++) {
   4613             var here = this$1.ranges[i], there = other.ranges[i]
   4614             if (!equalCursorPos(here.anchor, there.anchor) || !equalCursorPos(here.head, there.head)) { return false }
   4615         }
   4616         return true
   4617     };
   4618 
   4619     Selection.prototype.deepCopy = function () {
   4620         var this$1 = this;
   4621 
   4622         var out = []
   4623         for (var i = 0; i < this.ranges.length; i++)
   4624         { out[i] = new Range(copyPos(this$1.ranges[i].anchor), copyPos(this$1.ranges[i].head)) }
   4625         return new Selection(out, this.primIndex)
   4626     };
   4627 
   4628     Selection.prototype.somethingSelected = function () {
   4629         var this$1 = this;
   4630 
   4631         for (var i = 0; i < this.ranges.length; i++)
   4632         { if (!this$1.ranges[i].empty()) { return true } }
   4633         return false
   4634     };
   4635 
   4636     Selection.prototype.contains = function (pos, end) {
   4637         var this$1 = this;
   4638 
   4639         if (!end) { end = pos }
   4640         for (var i = 0; i < this.ranges.length; i++) {
   4641             var range = this$1.ranges[i]
   4642             if (cmp(end, range.from()) >= 0 && cmp(pos, range.to()) <= 0)
   4643             { return i }
   4644         }
   4645         return -1
   4646     };
   4647 
   4648     var Range = function(anchor, head) {
   4649         this.anchor = anchor; this.head = head
   4650     };
   4651 
   4652     Range.prototype.from = function () { return minPos(this.anchor, this.head) };
   4653     Range.prototype.to = function () { return maxPos(this.anchor, this.head) };
   4654     Range.prototype.empty = function () { return this.head.line == this.anchor.line && this.head.ch == this.anchor.ch };
   4655 
   4656 // Take an unsorted, potentially overlapping set of ranges, and
   4657 // build a selection out of it. 'Consumes' ranges array (modifying
   4658 // it).
   4659     function normalizeSelection(ranges, primIndex) {
   4660         var prim = ranges[primIndex]
   4661         ranges.sort(function (a, b) { return cmp(a.from(), b.from()); })
   4662         primIndex = indexOf(ranges, prim)
   4663         for (var i = 1; i < ranges.length; i++) {
   4664             var cur = ranges[i], prev = ranges[i - 1]
   4665             if (cmp(prev.to(), cur.from()) >= 0) {
   4666                 var from = minPos(prev.from(), cur.from()), to = maxPos(prev.to(), cur.to())
   4667                 var inv = prev.empty() ? cur.from() == cur.head : prev.from() == prev.head
   4668                 if (i <= primIndex) { --primIndex }
   4669                 ranges.splice(--i, 2, new Range(inv ? to : from, inv ? from : to))
   4670             }
   4671         }
   4672         return new Selection(ranges, primIndex)
   4673     }
   4674 
   4675     function simpleSelection(anchor, head) {
   4676         return new Selection([new Range(anchor, head || anchor)], 0)
   4677     }
   4678 
   4679 // Compute the position of the end of a change (its 'to' property
   4680 // refers to the pre-change end).
   4681     function changeEnd(change) {
   4682         if (!change.text) { return change.to }
   4683         return Pos(change.from.line + change.text.length - 1,
   4684             lst(change.text).length + (change.text.length == 1 ? change.from.ch : 0))
   4685     }
   4686 
   4687 // Adjust a position to refer to the post-change position of the
   4688 // same text, or the end of the change if the change covers it.
   4689     function adjustForChange(pos, change) {
   4690         if (cmp(pos, change.from) < 0) { return pos }
   4691         if (cmp(pos, change.to) <= 0) { return changeEnd(change) }
   4692 
   4693         var line = pos.line + change.text.length - (change.to.line - change.from.line) - 1, ch = pos.ch
   4694         if (pos.line == change.to.line) { ch += changeEnd(change).ch - change.to.ch }
   4695         return Pos(line, ch)
   4696     }
   4697 
   4698     function computeSelAfterChange(doc, change) {
   4699         var out = []
   4700         for (var i = 0; i < doc.sel.ranges.length; i++) {
   4701             var range = doc.sel.ranges[i]
   4702             out.push(new Range(adjustForChange(range.anchor, change),
   4703                 adjustForChange(range.head, change)))
   4704         }
   4705         return normalizeSelection(out, doc.sel.primIndex)
   4706     }
   4707 
   4708     function offsetPos(pos, old, nw) {
   4709         if (pos.line == old.line)
   4710         { return Pos(nw.line, pos.ch - old.ch + nw.ch) }
   4711         else
   4712         { return Pos(nw.line + (pos.line - old.line), pos.ch) }
   4713     }
   4714 
   4715 // Used by replaceSelections to allow moving the selection to the
   4716 // start or around the replaced test. Hint may be "start" or "around".
   4717     function computeReplacedSel(doc, changes, hint) {
   4718         var out = []
   4719         var oldPrev = Pos(doc.first, 0), newPrev = oldPrev
   4720         for (var i = 0; i < changes.length; i++) {
   4721             var change = changes[i]
   4722             var from = offsetPos(change.from, oldPrev, newPrev)
   4723             var to = offsetPos(changeEnd(change), oldPrev, newPrev)
   4724             oldPrev = change.to
   4725             newPrev = to
   4726             if (hint == "around") {
   4727                 var range = doc.sel.ranges[i], inv = cmp(range.head, range.anchor) < 0
   4728                 out[i] = new Range(inv ? to : from, inv ? from : to)
   4729             } else {
   4730                 out[i] = new Range(from, from)
   4731             }
   4732         }
   4733         return new Selection(out, doc.sel.primIndex)
   4734     }
   4735 
   4736 // Used to get the editor into a consistent state again when options change.
   4737 
   4738     function loadMode(cm) {
   4739         cm.doc.mode = getMode(cm.options, cm.doc.modeOption)
   4740         resetModeState(cm)
   4741     }
   4742 
   4743     function resetModeState(cm) {
   4744         cm.doc.iter(function (line) {
   4745             if (line.stateAfter) { line.stateAfter = null }
   4746             if (line.styles) { line.styles = null }
   4747         })
   4748         cm.doc.modeFrontier = cm.doc.highlightFrontier = cm.doc.first
   4749         startWorker(cm, 100)
   4750         cm.state.modeGen++
   4751         if (cm.curOp) { regChange(cm) }
   4752     }
   4753 
   4754 // DOCUMENT DATA STRUCTURE
   4755 
   4756 // By default, updates that start and end at the beginning of a line
   4757 // are treated specially, in order to make the association of line
   4758 // widgets and marker elements with the text behave more intuitive.
   4759     function isWholeLineUpdate(doc, change) {
   4760         return change.from.ch == 0 && change.to.ch == 0 && lst(change.text) == "" &&
   4761             (!doc.cm || doc.cm.options.wholeLineUpdateBefore)
   4762     }
   4763 
   4764 // Perform a change on the document data structure.
   4765     function updateDoc(doc, change, markedSpans, estimateHeight) {
   4766         function spansFor(n) {return markedSpans ? markedSpans[n] : null}
   4767         function update(line, text, spans) {
   4768             updateLine(line, text, spans, estimateHeight)
   4769             signalLater(line, "change", line, change)
   4770         }
   4771         function linesFor(start, end) {
   4772             var result = []
   4773             for (var i = start; i < end; ++i)
   4774             { result.push(new Line(text[i], spansFor(i), estimateHeight)) }
   4775             return result
   4776         }
   4777 
   4778         var from = change.from, to = change.to, text = change.text
   4779         var firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line)
   4780         var lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line
   4781 
   4782         // Adjust the line structure
   4783         if (change.full) {
   4784             doc.insert(0, linesFor(0, text.length))
   4785             doc.remove(text.length, doc.size - text.length)
   4786         } else if (isWholeLineUpdate(doc, change)) {
   4787             // This is a whole-line replace. Treated specially to make
   4788             // sure line objects move the way they are supposed to.
   4789             var added = linesFor(0, text.length - 1)
   4790             update(lastLine, lastLine.text, lastSpans)
   4791             if (nlines) { doc.remove(from.line, nlines) }
   4792             if (added.length) { doc.insert(from.line, added) }
   4793         } else if (firstLine == lastLine) {
   4794             if (text.length == 1) {
   4795                 update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans)
   4796             } else {
   4797                 var added$1 = linesFor(1, text.length - 1)
   4798                 added$1.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight))
   4799                 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0))
   4800                 doc.insert(from.line + 1, added$1)
   4801             }
   4802         } else if (text.length == 1) {
   4803             update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0))
   4804             doc.remove(from.line + 1, nlines)
   4805         } else {
   4806             update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0))
   4807             update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans)
   4808             var added$2 = linesFor(1, text.length - 1)
   4809             if (nlines > 1) { doc.remove(from.line + 1, nlines - 1) }
   4810             doc.insert(from.line + 1, added$2)
   4811         }
   4812 
   4813         signalLater(doc, "change", doc, change)
   4814     }
   4815 
   4816 // Call f for all linked documents.
   4817     function linkedDocs(doc, f, sharedHistOnly) {
   4818         function propagate(doc, skip, sharedHist) {
   4819             if (doc.linked) { for (var i = 0; i < doc.linked.length; ++i) {
   4820                 var rel = doc.linked[i]
   4821                 if (rel.doc == skip) { continue }
   4822                 var shared = sharedHist && rel.sharedHist
   4823                 if (sharedHistOnly && !shared) { continue }
   4824                 f(rel.doc, shared)
   4825                 propagate(rel.doc, doc, shared)
   4826             } }
   4827         }
   4828         propagate(doc, null, true)
   4829     }
   4830 
   4831 // Attach a document to an editor.
   4832     function attachDoc(cm, doc) {
   4833         if (doc.cm) { throw new Error("This document is already in use.") }
   4834         cm.doc = doc
   4835         doc.cm = cm
   4836         estimateLineHeights(cm)
   4837         loadMode(cm)
   4838         setDirectionClass(cm)
   4839         if (!cm.options.lineWrapping) { findMaxLine(cm) }
   4840         cm.options.mode = doc.modeOption
   4841         regChange(cm)
   4842     }
   4843 
   4844     function setDirectionClass(cm) {
   4845         ;(cm.doc.direction == "rtl" ? addClass : rmClass)(cm.display.lineDiv, "CodeMirror-rtl")
   4846     }
   4847 
   4848     function directionChanged(cm) {
   4849         runInOp(cm, function () {
   4850             setDirectionClass(cm)
   4851             regChange(cm)
   4852         })
   4853     }
   4854 
   4855     function History(startGen) {
   4856         // Arrays of change events and selections. Doing something adds an
   4857         // event to done and clears undo. Undoing moves events from done
   4858         // to undone, redoing moves them in the other direction.
   4859         this.done = []; this.undone = []
   4860         this.undoDepth = Infinity
   4861         // Used to track when changes can be merged into a single undo
   4862         // event
   4863         this.lastModTime = this.lastSelTime = 0
   4864         this.lastOp = this.lastSelOp = null
   4865         this.lastOrigin = this.lastSelOrigin = null
   4866         // Used by the isClean() method
   4867         this.generation = this.maxGeneration = startGen || 1
   4868     }
   4869 
   4870 // Create a history change event from an updateDoc-style change
   4871 // object.
   4872     function historyChangeFromChange(doc, change) {
   4873         var histChange = {from: copyPos(change.from), to: changeEnd(change), text: getBetween(doc, change.from, change.to)}
   4874         attachLocalSpans(doc, histChange, change.from.line, change.to.line + 1)
   4875         linkedDocs(doc, function (doc) { return attachLocalSpans(doc, histChange, change.from.line, change.to.line + 1); }, true)
   4876         return histChange
   4877     }
   4878 
   4879 // Pop all selection events off the end of a history array. Stop at
   4880 // a change event.
   4881     function clearSelectionEvents(array) {
   4882         while (array.length) {
   4883             var last = lst(array)
   4884             if (last.ranges) { array.pop() }
   4885             else { break }
   4886         }
   4887     }
   4888 
   4889 // Find the top change event in the history. Pop off selection
   4890 // events that are in the way.
   4891     function lastChangeEvent(hist, force) {
   4892         if (force) {
   4893             clearSelectionEvents(hist.done)
   4894             return lst(hist.done)
   4895         } else if (hist.done.length && !lst(hist.done).ranges) {
   4896             return lst(hist.done)
   4897         } else if (hist.done.length > 1 && !hist.done[hist.done.length - 2].ranges) {
   4898             hist.done.pop()
   4899             return lst(hist.done)
   4900         }
   4901     }
   4902 
   4903 // Register a change in the history. Merges changes that are within
   4904 // a single operation, or are close together with an origin that
   4905 // allows merging (starting with "+") into a single event.
   4906     function addChangeToHistory(doc, change, selAfter, opId) {
   4907         var hist = doc.history
   4908         hist.undone.length = 0
   4909         var time = +new Date, cur
   4910         var last
   4911 
   4912         if ((hist.lastOp == opId ||
   4913             hist.lastOrigin == change.origin && change.origin &&
   4914             ((change.origin.charAt(0) == "+" && doc.cm && hist.lastModTime > time - doc.cm.options.historyEventDelay) ||
   4915                 change.origin.charAt(0) == "*")) &&
   4916             (cur = lastChangeEvent(hist, hist.lastOp == opId))) {
   4917             // Merge this change into the last event
   4918             last = lst(cur.changes)
   4919             if (cmp(change.from, change.to) == 0 && cmp(change.from, last.to) == 0) {
   4920                 // Optimized case for simple insertion -- don't want to add
   4921                 // new changesets for every character typed
   4922                 last.to = changeEnd(change)
   4923             } else {
   4924                 // Add new sub-event
   4925                 cur.changes.push(historyChangeFromChange(doc, change))
   4926             }
   4927         } else {
   4928             // Can not be merged, start a new event.
   4929             var before = lst(hist.done)
   4930             if (!before || !before.ranges)
   4931             { pushSelectionToHistory(doc.sel, hist.done) }
   4932             cur = {changes: [historyChangeFromChange(doc, change)],
   4933                 generation: hist.generation}
   4934             hist.done.push(cur)
   4935             while (hist.done.length > hist.undoDepth) {
   4936                 hist.done.shift()
   4937                 if (!hist.done[0].ranges) { hist.done.shift() }
   4938             }
   4939         }
   4940         hist.done.push(selAfter)
   4941         hist.generation = ++hist.maxGeneration
   4942         hist.lastModTime = hist.lastSelTime = time
   4943         hist.lastOp = hist.lastSelOp = opId
   4944         hist.lastOrigin = hist.lastSelOrigin = change.origin
   4945 
   4946         if (!last) { signal(doc, "historyAdded") }
   4947     }
   4948 
   4949     function selectionEventCanBeMerged(doc, origin, prev, sel) {
   4950         var ch = origin.charAt(0)
   4951         return ch == "*" ||
   4952             ch == "+" &&
   4953             prev.ranges.length == sel.ranges.length &&
   4954             prev.somethingSelected() == sel.somethingSelected() &&
   4955             new Date - doc.history.lastSelTime <= (doc.cm ? doc.cm.options.historyEventDelay : 500)
   4956     }
   4957 
   4958 // Called whenever the selection changes, sets the new selection as
   4959 // the pending selection in the history, and pushes the old pending
   4960 // selection into the 'done' array when it was significantly
   4961 // different (in number of selected ranges, emptiness, or time).
   4962     function addSelectionToHistory(doc, sel, opId, options) {
   4963         var hist = doc.history, origin = options && options.origin
   4964 
   4965         // A new event is started when the previous origin does not match
   4966         // the current, or the origins don't allow matching. Origins
   4967         // starting with * are always merged, those starting with + are
   4968         // merged when similar and close together in time.
   4969         if (opId == hist.lastSelOp ||
   4970             (origin && hist.lastSelOrigin == origin &&
   4971                 (hist.lastModTime == hist.lastSelTime && hist.lastOrigin == origin ||
   4972                     selectionEventCanBeMerged(doc, origin, lst(hist.done), sel))))
   4973         { hist.done[hist.done.length - 1] = sel }
   4974         else
   4975         { pushSelectionToHistory(sel, hist.done) }
   4976 
   4977         hist.lastSelTime = +new Date
   4978         hist.lastSelOrigin = origin
   4979         hist.lastSelOp = opId
   4980         if (options && options.clearRedo !== false)
   4981         { clearSelectionEvents(hist.undone) }
   4982     }
   4983 
   4984     function pushSelectionToHistory(sel, dest) {
   4985         var top = lst(dest)
   4986         if (!(top && top.ranges && top.equals(sel)))
   4987         { dest.push(sel) }
   4988     }
   4989 
   4990 // Used to store marked span information in the history.
   4991     function attachLocalSpans(doc, change, from, to) {
   4992         var existing = change["spans_" + doc.id], n = 0
   4993         doc.iter(Math.max(doc.first, from), Math.min(doc.first + doc.size, to), function (line) {
   4994             if (line.markedSpans)
   4995             { (existing || (existing = change["spans_" + doc.id] = {}))[n] = line.markedSpans }
   4996             ++n
   4997         })
   4998     }
   4999 
   5000 // When un/re-doing restores text containing marked spans, those
   5001 // that have been explicitly cleared should not be restored.
   5002     function removeClearedSpans(spans) {
   5003         if (!spans) { return null }
   5004         var out
   5005         for (var i = 0; i < spans.length; ++i) {
   5006             if (spans[i].marker.explicitlyCleared) { if (!out) { out = spans.slice(0, i) } }
   5007             else if (out) { out.push(spans[i]) }
   5008         }
   5009         return !out ? spans : out.length ? out : null
   5010     }
   5011 
   5012 // Retrieve and filter the old marked spans stored in a change event.
   5013     function getOldSpans(doc, change) {
   5014         var found = change["spans_" + doc.id]
   5015         if (!found) { return null }
   5016         var nw = []
   5017         for (var i = 0; i < change.text.length; ++i)
   5018         { nw.push(removeClearedSpans(found[i])) }
   5019         return nw
   5020     }
   5021 
   5022 // Used for un/re-doing changes from the history. Combines the
   5023 // result of computing the existing spans with the set of spans that
   5024 // existed in the history (so that deleting around a span and then
   5025 // undoing brings back the span).
   5026     function mergeOldSpans(doc, change) {
   5027         var old = getOldSpans(doc, change)
   5028         var stretched = stretchSpansOverChange(doc, change)
   5029         if (!old) { return stretched }
   5030         if (!stretched) { return old }
   5031 
   5032         for (var i = 0; i < old.length; ++i) {
   5033             var oldCur = old[i], stretchCur = stretched[i]
   5034             if (oldCur && stretchCur) {
   5035                 spans: for (var j = 0; j < stretchCur.length; ++j) {
   5036                     var span = stretchCur[j]
   5037                     for (var k = 0; k < oldCur.length; ++k)
   5038                     { if (oldCur[k].marker == span.marker) { continue spans } }
   5039                     oldCur.push(span)
   5040                 }
   5041             } else if (stretchCur) {
   5042                 old[i] = stretchCur
   5043             }
   5044         }
   5045         return old
   5046     }
   5047 
   5048 // Used both to provide a JSON-safe object in .getHistory, and, when
   5049 // detaching a document, to split the history in two
   5050     function copyHistoryArray(events, newGroup, instantiateSel) {
   5051         var copy = []
   5052         for (var i = 0; i < events.length; ++i) {
   5053             var event = events[i]
   5054             if (event.ranges) {
   5055                 copy.push(instantiateSel ? Selection.prototype.deepCopy.call(event) : event)
   5056                 continue
   5057             }
   5058             var changes = event.changes, newChanges = []
   5059             copy.push({changes: newChanges})
   5060             for (var j = 0; j < changes.length; ++j) {
   5061                 var change = changes[j], m = (void 0)
   5062                 newChanges.push({from: change.from, to: change.to, text: change.text})
   5063                 if (newGroup) { for (var prop in change) { if (m = prop.match(/^spans_(\d+)$/)) {
   5064                     if (indexOf(newGroup, Number(m[1])) > -1) {
   5065                         lst(newChanges)[prop] = change[prop]
   5066                         delete change[prop]
   5067                     }
   5068                 } } }
   5069             }
   5070         }
   5071         return copy
   5072     }
   5073 
   5074 // The 'scroll' parameter given to many of these indicated whether
   5075 // the new cursor position should be scrolled into view after
   5076 // modifying the selection.
   5077 
   5078 // If shift is held or the extend flag is set, extends a range to
   5079 // include a given position (and optionally a second position).
   5080 // Otherwise, simply returns the range between the given positions.
   5081 // Used for cursor motion and such.
   5082     function extendRange(range, head, other, extend) {
   5083         if (extend) {
   5084             var anchor = range.anchor
   5085             if (other) {
   5086                 var posBefore = cmp(head, anchor) < 0
   5087                 if (posBefore != (cmp(other, anchor) < 0)) {
   5088                     anchor = head
   5089                     head = other
   5090                 } else if (posBefore != (cmp(head, other) < 0)) {
   5091                     head = other
   5092                 }
   5093             }
   5094             return new Range(anchor, head)
   5095         } else {
   5096             return new Range(other || head, head)
   5097         }
   5098     }
   5099 
   5100 // Extend the primary selection range, discard the rest.
   5101     function extendSelection(doc, head, other, options, extend) {
   5102         if (extend == null) { extend = doc.cm && (doc.cm.display.shift || doc.extend) }
   5103         setSelection(doc, new Selection([extendRange(doc.sel.primary(), head, other, extend)], 0), options)
   5104     }
   5105 
   5106 // Extend all selections (pos is an array of selections with length
   5107 // equal the number of selections)
   5108     function extendSelections(doc, heads, options) {
   5109         var out = []
   5110         var extend = doc.cm && (doc.cm.display.shift || doc.extend)
   5111         for (var i = 0; i < doc.sel.ranges.length; i++)
   5112         { out[i] = extendRange(doc.sel.ranges[i], heads[i], null, extend) }
   5113         var newSel = normalizeSelection(out, doc.sel.primIndex)
   5114         setSelection(doc, newSel, options)
   5115     }
   5116 
   5117 // Updates a single range in the selection.
   5118     function replaceOneSelection(doc, i, range, options) {
   5119         var ranges = doc.sel.ranges.slice(0)
   5120         ranges[i] = range
   5121         setSelection(doc, normalizeSelection(ranges, doc.sel.primIndex), options)
   5122     }
   5123 
   5124 // Reset the selection to a single range.
   5125     function setSimpleSelection(doc, anchor, head, options) {
   5126         setSelection(doc, simpleSelection(anchor, head), options)
   5127     }
   5128 
   5129 // Give beforeSelectionChange handlers a change to influence a
   5130 // selection update.
   5131     function filterSelectionChange(doc, sel, options) {
   5132         var obj = {
   5133             ranges: sel.ranges,
   5134             update: function(ranges) {
   5135                 var this$1 = this;
   5136 
   5137                 this.ranges = []
   5138                 for (var i = 0; i < ranges.length; i++)
   5139                 { this$1.ranges[i] = new Range(clipPos(doc, ranges[i].anchor),
   5140                     clipPos(doc, ranges[i].head)) }
   5141             },
   5142             origin: options && options.origin
   5143         }
   5144         signal(doc, "beforeSelectionChange", doc, obj)
   5145         if (doc.cm) { signal(doc.cm, "beforeSelectionChange", doc.cm, obj) }
   5146         if (obj.ranges != sel.ranges) { return normalizeSelection(obj.ranges, obj.ranges.length - 1) }
   5147         else { return sel }
   5148     }
   5149 
   5150     function setSelectionReplaceHistory(doc, sel, options) {
   5151         var done = doc.history.done, last = lst(done)
   5152         if (last && last.ranges) {
   5153             done[done.length - 1] = sel
   5154             setSelectionNoUndo(doc, sel, options)
   5155         } else {
   5156             setSelection(doc, sel, options)
   5157         }
   5158     }
   5159 
   5160 // Set a new selection.
   5161     function setSelection(doc, sel, options) {
   5162         setSelectionNoUndo(doc, sel, options)
   5163         addSelectionToHistory(doc, doc.sel, doc.cm ? doc.cm.curOp.id : NaN, options)
   5164     }
   5165 
   5166     function setSelectionNoUndo(doc, sel, options) {
   5167         if (hasHandler(doc, "beforeSelectionChange") || doc.cm && hasHandler(doc.cm, "beforeSelectionChange"))
   5168         { sel = filterSelectionChange(doc, sel, options) }
   5169 
   5170         var bias = options && options.bias ||
   5171             (cmp(sel.primary().head, doc.sel.primary().head) < 0 ? -1 : 1)
   5172         setSelectionInner(doc, skipAtomicInSelection(doc, sel, bias, true))
   5173 
   5174         if (!(options && options.scroll === false) && doc.cm)
   5175         { ensureCursorVisible(doc.cm) }
   5176     }
   5177 
   5178     function setSelectionInner(doc, sel) {
   5179         if (sel.equals(doc.sel)) { return }
   5180 
   5181         doc.sel = sel
   5182 
   5183         if (doc.cm) {
   5184             doc.cm.curOp.updateInput = doc.cm.curOp.selectionChanged = true
   5185             signalCursorActivity(doc.cm)
   5186         }
   5187         signalLater(doc, "cursorActivity", doc)
   5188     }
   5189 
   5190 // Verify that the selection does not partially select any atomic
   5191 // marked ranges.
   5192     function reCheckSelection(doc) {
   5193         setSelectionInner(doc, skipAtomicInSelection(doc, doc.sel, null, false))
   5194     }
   5195 
   5196 // Return a selection that does not partially select any atomic
   5197 // ranges.
   5198     function skipAtomicInSelection(doc, sel, bias, mayClear) {
   5199         var out
   5200         for (var i = 0; i < sel.ranges.length; i++) {
   5201             var range = sel.ranges[i]
   5202             var old = sel.ranges.length == doc.sel.ranges.length && doc.sel.ranges[i]
   5203             var newAnchor = skipAtomic(doc, range.anchor, old && old.anchor, bias, mayClear)
   5204             var newHead = skipAtomic(doc, range.head, old && old.head, bias, mayClear)
   5205             if (out || newAnchor != range.anchor || newHead != range.head) {
   5206                 if (!out) { out = sel.ranges.slice(0, i) }
   5207                 out[i] = new Range(newAnchor, newHead)
   5208             }
   5209         }
   5210         return out ? normalizeSelection(out, sel.primIndex) : sel
   5211     }
   5212 
   5213     function skipAtomicInner(doc, pos, oldPos, dir, mayClear) {
   5214         var line = getLine(doc, pos.line)
   5215         if (line.markedSpans) { for (var i = 0; i < line.markedSpans.length; ++i) {
   5216             var sp = line.markedSpans[i], m = sp.marker
   5217             if ((sp.from == null || (m.inclusiveLeft ? sp.from <= pos.ch : sp.from < pos.ch)) &&
   5218                 (sp.to == null || (m.inclusiveRight ? sp.to >= pos.ch : sp.to > pos.ch))) {
   5219                 if (mayClear) {
   5220                     signal(m, "beforeCursorEnter")
   5221                     if (m.explicitlyCleared) {
   5222                         if (!line.markedSpans) { break }
   5223                         else {--i; continue}
   5224                     }
   5225                 }
   5226                 if (!m.atomic) { continue }
   5227 
   5228                 if (oldPos) {
   5229                     var near = m.find(dir < 0 ? 1 : -1), diff = (void 0)
   5230                     if (dir < 0 ? m.inclusiveRight : m.inclusiveLeft)
   5231                     { near = movePos(doc, near, -dir, near && near.line == pos.line ? line : null) }
   5232                     if (near && near.line == pos.line && (diff = cmp(near, oldPos)) && (dir < 0 ? diff < 0 : diff > 0))
   5233                     { return skipAtomicInner(doc, near, pos, dir, mayClear) }
   5234                 }
   5235 
   5236                 var far = m.find(dir < 0 ? -1 : 1)
   5237                 if (dir < 0 ? m.inclusiveLeft : m.inclusiveRight)
   5238                 { far = movePos(doc, far, dir, far.line == pos.line ? line : null) }
   5239                 return far ? skipAtomicInner(doc, far, pos, dir, mayClear) : null
   5240             }
   5241         } }
   5242         return pos
   5243     }
   5244 
   5245 // Ensure a given position is not inside an atomic range.
   5246     function skipAtomic(doc, pos, oldPos, bias, mayClear) {
   5247         var dir = bias || 1
   5248         var found = skipAtomicInner(doc, pos, oldPos, dir, mayClear) ||
   5249             (!mayClear && skipAtomicInner(doc, pos, oldPos, dir, true)) ||
   5250             skipAtomicInner(doc, pos, oldPos, -dir, mayClear) ||
   5251             (!mayClear && skipAtomicInner(doc, pos, oldPos, -dir, true))
   5252         if (!found) {
   5253             doc.cantEdit = true
   5254             return Pos(doc.first, 0)
   5255         }
   5256         return found
   5257     }
   5258 
   5259     function movePos(doc, pos, dir, line) {
   5260         if (dir < 0 && pos.ch == 0) {
   5261             if (pos.line > doc.first) { return clipPos(doc, Pos(pos.line - 1)) }
   5262             else { return null }
   5263         } else if (dir > 0 && pos.ch == (line || getLine(doc, pos.line)).text.length) {
   5264             if (pos.line < doc.first + doc.size - 1) { return Pos(pos.line + 1, 0) }
   5265             else { return null }
   5266         } else {
   5267             return new Pos(pos.line, pos.ch + dir)
   5268         }
   5269     }
   5270 
   5271     function selectAll(cm) {
   5272         cm.setSelection(Pos(cm.firstLine(), 0), Pos(cm.lastLine()), sel_dontScroll)
   5273     }
   5274 
   5275 // UPDATING
   5276 
   5277 // Allow "beforeChange" event handlers to influence a change
   5278     function filterChange(doc, change, update) {
   5279         var obj = {
   5280             canceled: false,
   5281             from: change.from,
   5282             to: change.to,
   5283             text: change.text,
   5284             origin: change.origin,
   5285             cancel: function () { return obj.canceled = true; }
   5286         }
   5287         if (update) { obj.update = function (from, to, text, origin) {
   5288             if (from) { obj.from = clipPos(doc, from) }
   5289             if (to) { obj.to = clipPos(doc, to) }
   5290             if (text) { obj.text = text }
   5291             if (origin !== undefined) { obj.origin = origin }
   5292         } }
   5293         signal(doc, "beforeChange", doc, obj)
   5294         if (doc.cm) { signal(doc.cm, "beforeChange", doc.cm, obj) }
   5295 
   5296         if (obj.canceled) { return null }
   5297         return {from: obj.from, to: obj.to, text: obj.text, origin: obj.origin}
   5298     }
   5299 
   5300 // Apply a change to a document, and add it to the document's
   5301 // history, and propagating it to all linked documents.
   5302     function makeChange(doc, change, ignoreReadOnly) {
   5303         if (doc.cm) {
   5304             if (!doc.cm.curOp) { return operation(doc.cm, makeChange)(doc, change, ignoreReadOnly) }
   5305             if (doc.cm.state.suppressEdits) { return }
   5306         }
   5307 
   5308         if (hasHandler(doc, "beforeChange") || doc.cm && hasHandler(doc.cm, "beforeChange")) {
   5309             change = filterChange(doc, change, true)
   5310             if (!change) { return }
   5311         }
   5312 
   5313         // Possibly split or suppress the update based on the presence
   5314         // of read-only spans in its range.
   5315         var split = sawReadOnlySpans && !ignoreReadOnly && removeReadOnlyRanges(doc, change.from, change.to)
   5316         if (split) {
   5317             for (var i = split.length - 1; i >= 0; --i)
   5318             { makeChangeInner(doc, {from: split[i].from, to: split[i].to, text: i ? [""] : change.text, origin: change.origin}) }
   5319         } else {
   5320             makeChangeInner(doc, change)
   5321         }
   5322     }
   5323 
   5324     function makeChangeInner(doc, change) {
   5325         if (change.text.length == 1 && change.text[0] == "" && cmp(change.from, change.to) == 0) { return }
   5326         var selAfter = computeSelAfterChange(doc, change)
   5327         addChangeToHistory(doc, change, selAfter, doc.cm ? doc.cm.curOp.id : NaN)
   5328 
   5329         makeChangeSingleDoc(doc, change, selAfter, stretchSpansOverChange(doc, change))
   5330         var rebased = []
   5331 
   5332         linkedDocs(doc, function (doc, sharedHist) {
   5333             if (!sharedHist && indexOf(rebased, doc.history) == -1) {
   5334                 rebaseHist(doc.history, change)
   5335                 rebased.push(doc.history)
   5336             }
   5337             makeChangeSingleDoc(doc, change, null, stretchSpansOverChange(doc, change))
   5338         })
   5339     }
   5340 
   5341 // Revert a change stored in a document's history.
   5342     function makeChangeFromHistory(doc, type, allowSelectionOnly) {
   5343         if (doc.cm && doc.cm.state.suppressEdits && !allowSelectionOnly) { return }
   5344 
   5345         var hist = doc.history, event, selAfter = doc.sel
   5346         var source = type == "undo" ? hist.done : hist.undone, dest = type == "undo" ? hist.undone : hist.done
   5347 
   5348         // Verify that there is a useable event (so that ctrl-z won't
   5349         // needlessly clear selection events)
   5350         var i = 0
   5351         for (; i < source.length; i++) {
   5352             event = source[i]
   5353             if (allowSelectionOnly ? event.ranges && !event.equals(doc.sel) : !event.ranges)
   5354             { break }
   5355         }
   5356         if (i == source.length) { return }
   5357         hist.lastOrigin = hist.lastSelOrigin = null
   5358 
   5359         for (;;) {
   5360             event = source.pop()
   5361             if (event.ranges) {
   5362                 pushSelectionToHistory(event, dest)
   5363                 if (allowSelectionOnly && !event.equals(doc.sel)) {
   5364                     setSelection(doc, event, {clearRedo: false})
   5365                     return
   5366                 }
   5367                 selAfter = event
   5368             }
   5369             else { break }
   5370         }
   5371 
   5372         // Build up a reverse change object to add to the opposite history
   5373         // stack (redo when undoing, and vice versa).
   5374         var antiChanges = []
   5375         pushSelectionToHistory(selAfter, dest)
   5376         dest.push({changes: antiChanges, generation: hist.generation})
   5377         hist.generation = event.generation || ++hist.maxGeneration
   5378 
   5379         var filter = hasHandler(doc, "beforeChange") || doc.cm && hasHandler(doc.cm, "beforeChange")
   5380 
   5381         var loop = function ( i ) {
   5382             var change = event.changes[i]
   5383             change.origin = type
   5384             if (filter && !filterChange(doc, change, false)) {
   5385                 source.length = 0
   5386                 return {}
   5387             }
   5388 
   5389             antiChanges.push(historyChangeFromChange(doc, change))
   5390 
   5391             var after = i ? computeSelAfterChange(doc, change) : lst(source)
   5392             makeChangeSingleDoc(doc, change, after, mergeOldSpans(doc, change))
   5393             if (!i && doc.cm) { doc.cm.scrollIntoView({from: change.from, to: changeEnd(change)}) }
   5394             var rebased = []
   5395 
   5396             // Propagate to the linked documents
   5397             linkedDocs(doc, function (doc, sharedHist) {
   5398                 if (!sharedHist && indexOf(rebased, doc.history) == -1) {
   5399                     rebaseHist(doc.history, change)
   5400                     rebased.push(doc.history)
   5401                 }
   5402                 makeChangeSingleDoc(doc, change, null, mergeOldSpans(doc, change))
   5403             })
   5404         };
   5405 
   5406         for (var i$1 = event.changes.length - 1; i$1 >= 0; --i$1) {
   5407             var returned = loop( i$1 );
   5408 
   5409             if ( returned ) return returned.v;
   5410         }
   5411     }
   5412 
   5413 // Sub-views need their line numbers shifted when text is added
   5414 // above or below them in the parent document.
   5415     function shiftDoc(doc, distance) {
   5416         if (distance == 0) { return }
   5417         doc.first += distance
   5418         doc.sel = new Selection(map(doc.sel.ranges, function (range) { return new Range(
   5419             Pos(range.anchor.line + distance, range.anchor.ch),
   5420             Pos(range.head.line + distance, range.head.ch)
   5421         ); }), doc.sel.primIndex)
   5422         if (doc.cm) {
   5423             regChange(doc.cm, doc.first, doc.first - distance, distance)
   5424             for (var d = doc.cm.display, l = d.viewFrom; l < d.viewTo; l++)
   5425             { regLineChange(doc.cm, l, "gutter") }
   5426         }
   5427     }
   5428 
   5429 // More lower-level change function, handling only a single document
   5430 // (not linked ones).
   5431     function makeChangeSingleDoc(doc, change, selAfter, spans) {
   5432         if (doc.cm && !doc.cm.curOp)
   5433         { return operation(doc.cm, makeChangeSingleDoc)(doc, change, selAfter, spans) }
   5434 
   5435         if (change.to.line < doc.first) {
   5436             shiftDoc(doc, change.text.length - 1 - (change.to.line - change.from.line))
   5437             return
   5438         }
   5439         if (change.from.line > doc.lastLine()) { return }
   5440 
   5441         // Clip the change to the size of this doc
   5442         if (change.from.line < doc.first) {
   5443             var shift = change.text.length - 1 - (doc.first - change.from.line)
   5444             shiftDoc(doc, shift)
   5445             change = {from: Pos(doc.first, 0), to: Pos(change.to.line + shift, change.to.ch),
   5446                 text: [lst(change.text)], origin: change.origin}
   5447         }
   5448         var last = doc.lastLine()
   5449         if (change.to.line > last) {
   5450             change = {from: change.from, to: Pos(last, getLine(doc, last).text.length),
   5451                 text: [change.text[0]], origin: change.origin}
   5452         }
   5453 
   5454         change.removed = getBetween(doc, change.from, change.to)
   5455 
   5456         if (!selAfter) { selAfter = computeSelAfterChange(doc, change) }
   5457         if (doc.cm) { makeChangeSingleDocInEditor(doc.cm, change, spans) }
   5458         else { updateDoc(doc, change, spans) }
   5459         setSelectionNoUndo(doc, selAfter, sel_dontScroll)
   5460     }
   5461 
   5462 // Handle the interaction of a change to a document with the editor
   5463 // that this document is part of.
   5464     function makeChangeSingleDocInEditor(cm, change, spans) {
   5465         var doc = cm.doc, display = cm.display, from = change.from, to = change.to
   5466 
   5467         var recomputeMaxLength = false, checkWidthStart = from.line
   5468         if (!cm.options.lineWrapping) {
   5469             checkWidthStart = lineNo(visualLine(getLine(doc, from.line)))
   5470             doc.iter(checkWidthStart, to.line + 1, function (line) {
   5471                 if (line == display.maxLine) {
   5472                     recomputeMaxLength = true
   5473                     return true
   5474                 }
   5475             })
   5476         }
   5477 
   5478         if (doc.sel.contains(change.from, change.to) > -1)
   5479         { signalCursorActivity(cm) }
   5480 
   5481         updateDoc(doc, change, spans, estimateHeight(cm))
   5482 
   5483         if (!cm.options.lineWrapping) {
   5484             doc.iter(checkWidthStart, from.line + change.text.length, function (line) {
   5485                 var len = lineLength(line)
   5486                 if (len > display.maxLineLength) {
   5487                     display.maxLine = line
   5488                     display.maxLineLength = len
   5489                     display.maxLineChanged = true
   5490                     recomputeMaxLength = false
   5491                 }
   5492             })
   5493             if (recomputeMaxLength) { cm.curOp.updateMaxLine = true }
   5494         }
   5495 
   5496         retreatFrontier(doc, from.line)
   5497         startWorker(cm, 400)
   5498 
   5499         var lendiff = change.text.length - (to.line - from.line) - 1
   5500         // Remember that these lines changed, for updating the display
   5501         if (change.full)
   5502         { regChange(cm) }
   5503         else if (from.line == to.line && change.text.length == 1 && !isWholeLineUpdate(cm.doc, change))
   5504         { regLineChange(cm, from.line, "text") }
   5505         else
   5506         { regChange(cm, from.line, to.line + 1, lendiff) }
   5507 
   5508         var changesHandler = hasHandler(cm, "changes"), changeHandler = hasHandler(cm, "change")
   5509         if (changeHandler || changesHandler) {
   5510             var obj = {
   5511                 from: from, to: to,
   5512                 text: change.text,
   5513                 removed: change.removed,
   5514                 origin: change.origin
   5515             }
   5516             if (changeHandler) { signalLater(cm, "change", cm, obj) }
   5517             if (changesHandler) { (cm.curOp.changeObjs || (cm.curOp.changeObjs = [])).push(obj) }
   5518         }
   5519         cm.display.selForContextMenu = null
   5520     }
   5521 
   5522     function replaceRange(doc, code, from, to, origin) {
   5523         if (!to) { to = from }
   5524         if (cmp(to, from) < 0) { var assign;
   5525             (assign = [to, from], from = assign[0], to = assign[1], assign) }
   5526         if (typeof code == "string") { code = doc.splitLines(code) }
   5527         makeChange(doc, {from: from, to: to, text: code, origin: origin})
   5528     }
   5529 
   5530 // Rebasing/resetting history to deal with externally-sourced changes
   5531 
   5532     function rebaseHistSelSingle(pos, from, to, diff) {
   5533         if (to < pos.line) {
   5534             pos.line += diff
   5535         } else if (from < pos.line) {
   5536             pos.line = from
   5537             pos.ch = 0
   5538         }
   5539     }
   5540 
   5541 // Tries to rebase an array of history events given a change in the
   5542 // document. If the change touches the same lines as the event, the
   5543 // event, and everything 'behind' it, is discarded. If the change is
   5544 // before the event, the event's positions are updated. Uses a
   5545 // copy-on-write scheme for the positions, to avoid having to
   5546 // reallocate them all on every rebase, but also avoid problems with
   5547 // shared position objects being unsafely updated.
   5548     function rebaseHistArray(array, from, to, diff) {
   5549         for (var i = 0; i < array.length; ++i) {
   5550             var sub = array[i], ok = true
   5551             if (sub.ranges) {
   5552                 if (!sub.copied) { sub = array[i] = sub.deepCopy(); sub.copied = true }
   5553                 for (var j = 0; j < sub.ranges.length; j++) {
   5554                     rebaseHistSelSingle(sub.ranges[j].anchor, from, to, diff)
   5555                     rebaseHistSelSingle(sub.ranges[j].head, from, to, diff)
   5556                 }
   5557                 continue
   5558             }
   5559             for (var j$1 = 0; j$1 < sub.changes.length; ++j$1) {
   5560                 var cur = sub.changes[j$1]
   5561                 if (to < cur.from.line) {
   5562                     cur.from = Pos(cur.from.line + diff, cur.from.ch)
   5563                     cur.to = Pos(cur.to.line + diff, cur.to.ch)
   5564                 } else if (from <= cur.to.line) {
   5565                     ok = false
   5566                     break
   5567                 }
   5568             }
   5569             if (!ok) {
   5570                 array.splice(0, i + 1)
   5571                 i = 0
   5572             }
   5573         }
   5574     }
   5575 
   5576     function rebaseHist(hist, change) {
   5577         var from = change.from.line, to = change.to.line, diff = change.text.length - (to - from) - 1
   5578         rebaseHistArray(hist.done, from, to, diff)
   5579         rebaseHistArray(hist.undone, from, to, diff)
   5580     }
   5581 
   5582 // Utility for applying a change to a line by handle or number,
   5583 // returning the number and optionally registering the line as
   5584 // changed.
   5585     function changeLine(doc, handle, changeType, op) {
   5586         var no = handle, line = handle
   5587         if (typeof handle == "number") { line = getLine(doc, clipLine(doc, handle)) }
   5588         else { no = lineNo(handle) }
   5589         if (no == null) { return null }
   5590         if (op(line, no) && doc.cm) { regLineChange(doc.cm, no, changeType) }
   5591         return line
   5592     }
   5593 
   5594 // The document is represented as a BTree consisting of leaves, with
   5595 // chunk of lines in them, and branches, with up to ten leaves or
   5596 // other branch nodes below them. The top node is always a branch
   5597 // node, and is the document object itself (meaning it has
   5598 // additional methods and properties).
   5599 //
   5600 // All nodes have parent links. The tree is used both to go from
   5601 // line numbers to line objects, and to go from objects to numbers.
   5602 // It also indexes by height, and is used to convert between height
   5603 // and line object, and to find the total height of the document.
   5604 //
   5605 // See also http://marijnhaverbeke.nl/blog/codemirror-line-tree.html
   5606 
   5607     function LeafChunk(lines) {
   5608         var this$1 = this;
   5609 
   5610         this.lines = lines
   5611         this.parent = null
   5612         var height = 0
   5613         for (var i = 0; i < lines.length; ++i) {
   5614             lines[i].parent = this$1
   5615             height += lines[i].height
   5616         }
   5617         this.height = height
   5618     }
   5619 
   5620     LeafChunk.prototype = {
   5621         chunkSize: function chunkSize() { return this.lines.length },
   5622 
   5623         // Remove the n lines at offset 'at'.
   5624         removeInner: function removeInner(at, n) {
   5625             var this$1 = this;
   5626 
   5627             for (var i = at, e = at + n; i < e; ++i) {
   5628                 var line = this$1.lines[i]
   5629                 this$1.height -= line.height
   5630                 cleanUpLine(line)
   5631                 signalLater(line, "delete")
   5632             }
   5633             this.lines.splice(at, n)
   5634         },
   5635 
   5636         // Helper used to collapse a small branch into a single leaf.
   5637         collapse: function collapse(lines) {
   5638             lines.push.apply(lines, this.lines)
   5639         },
   5640 
   5641         // Insert the given array of lines at offset 'at', count them as
   5642         // having the given height.
   5643         insertInner: function insertInner(at, lines, height) {
   5644             var this$1 = this;
   5645 
   5646             this.height += height
   5647             this.lines = this.lines.slice(0, at).concat(lines).concat(this.lines.slice(at))
   5648             for (var i = 0; i < lines.length; ++i) { lines[i].parent = this$1 }
   5649         },
   5650 
   5651         // Used to iterate over a part of the tree.
   5652         iterN: function iterN(at, n, op) {
   5653             var this$1 = this;
   5654 
   5655             for (var e = at + n; at < e; ++at)
   5656             { if (op(this$1.lines[at])) { return true } }
   5657         }
   5658     }
   5659 
   5660     function BranchChunk(children) {
   5661         var this$1 = this;
   5662 
   5663         this.children = children
   5664         var size = 0, height = 0
   5665         for (var i = 0; i < children.length; ++i) {
   5666             var ch = children[i]
   5667             size += ch.chunkSize(); height += ch.height
   5668             ch.parent = this$1
   5669         }
   5670         this.size = size
   5671         this.height = height
   5672         this.parent = null
   5673     }
   5674 
   5675     BranchChunk.prototype = {
   5676         chunkSize: function chunkSize() { return this.size },
   5677 
   5678         removeInner: function removeInner(at, n) {
   5679             var this$1 = this;
   5680 
   5681             this.size -= n
   5682             for (var i = 0; i < this.children.length; ++i) {
   5683                 var child = this$1.children[i], sz = child.chunkSize()
   5684                 if (at < sz) {
   5685                     var rm = Math.min(n, sz - at), oldHeight = child.height
   5686                     child.removeInner(at, rm)
   5687                     this$1.height -= oldHeight - child.height
   5688                     if (sz == rm) { this$1.children.splice(i--, 1); child.parent = null }
   5689                     if ((n -= rm) == 0) { break }
   5690                     at = 0
   5691                 } else { at -= sz }
   5692             }
   5693             // If the result is smaller than 25 lines, ensure that it is a
   5694             // single leaf node.
   5695             if (this.size - n < 25 &&
   5696                 (this.children.length > 1 || !(this.children[0] instanceof LeafChunk))) {
   5697                 var lines = []
   5698                 this.collapse(lines)
   5699                 this.children = [new LeafChunk(lines)]
   5700                 this.children[0].parent = this
   5701             }
   5702         },
   5703 
   5704         collapse: function collapse(lines) {
   5705             var this$1 = this;
   5706 
   5707             for (var i = 0; i < this.children.length; ++i) { this$1.children[i].collapse(lines) }
   5708         },
   5709 
   5710         insertInner: function insertInner(at, lines, height) {
   5711             var this$1 = this;
   5712 
   5713             this.size += lines.length
   5714             this.height += height
   5715             for (var i = 0; i < this.children.length; ++i) {
   5716                 var child = this$1.children[i], sz = child.chunkSize()
   5717                 if (at <= sz) {
   5718                     child.insertInner(at, lines, height)
   5719                     if (child.lines && child.lines.length > 50) {
   5720                         // To avoid memory thrashing when child.lines is huge (e.g. first view of a large file), it's never spliced.
   5721                         // Instead, small slices are taken. They're taken in order because sequential memory accesses are fastest.
   5722                         var remaining = child.lines.length % 25 + 25
   5723                         for (var pos = remaining; pos < child.lines.length;) {
   5724                             var leaf = new LeafChunk(child.lines.slice(pos, pos += 25))
   5725                             child.height -= leaf.height
   5726                             this$1.children.splice(++i, 0, leaf)
   5727                             leaf.parent = this$1
   5728                         }
   5729                         child.lines = child.lines.slice(0, remaining)
   5730                         this$1.maybeSpill()
   5731                     }
   5732                     break
   5733                 }
   5734                 at -= sz
   5735             }
   5736         },
   5737 
   5738         // When a node has grown, check whether it should be split.
   5739         maybeSpill: function maybeSpill() {
   5740             if (this.children.length <= 10) { return }
   5741             var me = this
   5742             do {
   5743                 var spilled = me.children.splice(me.children.length - 5, 5)
   5744                 var sibling = new BranchChunk(spilled)
   5745                 if (!me.parent) { // Become the parent node
   5746                     var copy = new BranchChunk(me.children)
   5747                     copy.parent = me
   5748                     me.children = [copy, sibling]
   5749                     me = copy
   5750                 } else {
   5751                     me.size -= sibling.size
   5752                     me.height -= sibling.height
   5753                     var myIndex = indexOf(me.parent.children, me)
   5754                     me.parent.children.splice(myIndex + 1, 0, sibling)
   5755                 }
   5756                 sibling.parent = me.parent
   5757             } while (me.children.length > 10)
   5758             me.parent.maybeSpill()
   5759         },
   5760 
   5761         iterN: function iterN(at, n, op) {
   5762             var this$1 = this;
   5763 
   5764             for (var i = 0; i < this.children.length; ++i) {
   5765                 var child = this$1.children[i], sz = child.chunkSize()
   5766                 if (at < sz) {
   5767                     var used = Math.min(n, sz - at)
   5768                     if (child.iterN(at, used, op)) { return true }
   5769                     if ((n -= used) == 0) { break }
   5770                     at = 0
   5771                 } else { at -= sz }
   5772             }
   5773         }
   5774     }
   5775 
   5776 // Line widgets are block elements displayed above or below a line.
   5777 
   5778     var LineWidget = function(doc, node, options) {
   5779         var this$1 = this;
   5780 
   5781         if (options) { for (var opt in options) { if (options.hasOwnProperty(opt))
   5782         { this$1[opt] = options[opt] } } }
   5783         this.doc = doc
   5784         this.node = node
   5785     };
   5786 
   5787     LineWidget.prototype.clear = function () {
   5788         var this$1 = this;
   5789 
   5790         var cm = this.doc.cm, ws = this.line.widgets, line = this.line, no = lineNo(line)
   5791         if (no == null || !ws) { return }
   5792         for (var i = 0; i < ws.length; ++i) { if (ws[i] == this$1) { ws.splice(i--, 1) } }
   5793         if (!ws.length) { line.widgets = null }
   5794         var height = widgetHeight(this)
   5795         updateLineHeight(line, Math.max(0, line.height - height))
   5796         if (cm) {
   5797             runInOp(cm, function () {
   5798                 adjustScrollWhenAboveVisible(cm, line, -height)
   5799                 regLineChange(cm, no, "widget")
   5800             })
   5801             signalLater(cm, "lineWidgetCleared", cm, this, no)
   5802         }
   5803     };
   5804 
   5805     LineWidget.prototype.changed = function () {
   5806         var this$1 = this;
   5807 
   5808         var oldH = this.height, cm = this.doc.cm, line = this.line
   5809         this.height = null
   5810         var diff = widgetHeight(this) - oldH
   5811         if (!diff) { return }
   5812         updateLineHeight(line, line.height + diff)
   5813         if (cm) {
   5814             runInOp(cm, function () {
   5815                 cm.curOp.forceUpdate = true
   5816                 adjustScrollWhenAboveVisible(cm, line, diff)
   5817                 signalLater(cm, "lineWidgetChanged", cm, this$1, lineNo(line))
   5818             })
   5819         }
   5820     };
   5821     eventMixin(LineWidget)
   5822 
   5823     function adjustScrollWhenAboveVisible(cm, line, diff) {
   5824         if (heightAtLine(line) < ((cm.curOp && cm.curOp.scrollTop) || cm.doc.scrollTop))
   5825         { addToScrollTop(cm, diff) }
   5826     }
   5827 
   5828     function addLineWidget(doc, handle, node, options) {
   5829         var widget = new LineWidget(doc, node, options)
   5830         var cm = doc.cm
   5831         if (cm && widget.noHScroll) { cm.display.alignWidgets = true }
   5832         changeLine(doc, handle, "widget", function (line) {
   5833             var widgets = line.widgets || (line.widgets = [])
   5834             if (widget.insertAt == null) { widgets.push(widget) }
   5835             else { widgets.splice(Math.min(widgets.length - 1, Math.max(0, widget.insertAt)), 0, widget) }
   5836             widget.line = line
   5837             if (cm && !lineIsHidden(doc, line)) {
   5838                 var aboveVisible = heightAtLine(line) < doc.scrollTop
   5839                 updateLineHeight(line, line.height + widgetHeight(widget))
   5840                 if (aboveVisible) { addToScrollTop(cm, widget.height) }
   5841                 cm.curOp.forceUpdate = true
   5842             }
   5843             return true
   5844         })
   5845         signalLater(cm, "lineWidgetAdded", cm, widget, typeof handle == "number" ? handle : lineNo(handle))
   5846         return widget
   5847     }
   5848 
   5849 // TEXTMARKERS
   5850 
   5851 // Created with markText and setBookmark methods. A TextMarker is a
   5852 // handle that can be used to clear or find a marked position in the
   5853 // document. Line objects hold arrays (markedSpans) containing
   5854 // {from, to, marker} object pointing to such marker objects, and
   5855 // indicating that such a marker is present on that line. Multiple
   5856 // lines may point to the same marker when it spans across lines.
   5857 // The spans will have null for their from/to properties when the
   5858 // marker continues beyond the start/end of the line. Markers have
   5859 // links back to the lines they currently touch.
   5860 
   5861 // Collapsed markers have unique ids, in order to be able to order
   5862 // them, which is needed for uniquely determining an outer marker
   5863 // when they overlap (they may nest, but not partially overlap).
   5864     var nextMarkerId = 0
   5865 
   5866     var TextMarker = function(doc, type) {
   5867         this.lines = []
   5868         this.type = type
   5869         this.doc = doc
   5870         this.id = ++nextMarkerId
   5871     };
   5872 
   5873 // Clear the marker.
   5874     TextMarker.prototype.clear = function () {
   5875         var this$1 = this;
   5876 
   5877         if (this.explicitlyCleared) { return }
   5878         var cm = this.doc.cm, withOp = cm && !cm.curOp
   5879         if (withOp) { startOperation(cm) }
   5880         if (hasHandler(this, "clear")) {
   5881             var found = this.find()
   5882             if (found) { signalLater(this, "clear", found.from, found.to) }
   5883         }
   5884         var min = null, max = null
   5885         for (var i = 0; i < this.lines.length; ++i) {
   5886             var line = this$1.lines[i]
   5887             var span = getMarkedSpanFor(line.markedSpans, this$1)
   5888             if (cm && !this$1.collapsed) { regLineChange(cm, lineNo(line), "text") }
   5889             else if (cm) {
   5890                 if (span.to != null) { max = lineNo(line) }
   5891                 if (span.from != null) { min = lineNo(line) }
   5892             }
   5893             line.markedSpans = removeMarkedSpan(line.markedSpans, span)
   5894             if (span.from == null && this$1.collapsed && !lineIsHidden(this$1.doc, line) && cm)
   5895             { updateLineHeight(line, textHeight(cm.display)) }
   5896         }
   5897         if (cm && this.collapsed && !cm.options.lineWrapping) { for (var i$1 = 0; i$1 < this.lines.length; ++i$1) {
   5898             var visual = visualLine(this$1.lines[i$1]), len = lineLength(visual)
   5899             if (len > cm.display.maxLineLength) {
   5900                 cm.display.maxLine = visual
   5901                 cm.display.maxLineLength = len
   5902                 cm.display.maxLineChanged = true
   5903             }
   5904         } }
   5905 
   5906         if (min != null && cm && this.collapsed) { regChange(cm, min, max + 1) }
   5907         this.lines.length = 0
   5908         this.explicitlyCleared = true
   5909         if (this.atomic && this.doc.cantEdit) {
   5910             this.doc.cantEdit = false
   5911             if (cm) { reCheckSelection(cm.doc) }
   5912         }
   5913         if (cm) { signalLater(cm, "markerCleared", cm, this, min, max) }
   5914         if (withOp) { endOperation(cm) }
   5915         if (this.parent) { this.parent.clear() }
   5916     };
   5917 
   5918 // Find the position of the marker in the document. Returns a {from,
   5919 // to} object by default. Side can be passed to get a specific side
   5920 // -- 0 (both), -1 (left), or 1 (right). When lineObj is true, the
   5921 // Pos objects returned contain a line object, rather than a line
   5922 // number (used to prevent looking up the same line twice).
   5923     TextMarker.prototype.find = function (side, lineObj) {
   5924         var this$1 = this;
   5925 
   5926         if (side == null && this.type == "bookmark") { side = 1 }
   5927         var from, to
   5928         for (var i = 0; i < this.lines.length; ++i) {
   5929             var line = this$1.lines[i]
   5930             var span = getMarkedSpanFor(line.markedSpans, this$1)
   5931             if (span.from != null) {
   5932                 from = Pos(lineObj ? line : lineNo(line), span.from)
   5933                 if (side == -1) { return from }
   5934             }
   5935             if (span.to != null) {
   5936                 to = Pos(lineObj ? line : lineNo(line), span.to)
   5937                 if (side == 1) { return to }
   5938             }
   5939         }
   5940         return from && {from: from, to: to}
   5941     };
   5942 
   5943 // Signals that the marker's widget changed, and surrounding layout
   5944 // should be recomputed.
   5945     TextMarker.prototype.changed = function () {
   5946         var this$1 = this;
   5947 
   5948         var pos = this.find(-1, true), widget = this, cm = this.doc.cm
   5949         if (!pos || !cm) { return }
   5950         runInOp(cm, function () {
   5951             var line = pos.line, lineN = lineNo(pos.line)
   5952             var view = findViewForLine(cm, lineN)
   5953             if (view) {
   5954                 clearLineMeasurementCacheFor(view)
   5955                 cm.curOp.selectionChanged = cm.curOp.forceUpdate = true
   5956             }
   5957             cm.curOp.updateMaxLine = true
   5958             if (!lineIsHidden(widget.doc, line) && widget.height != null) {
   5959                 var oldHeight = widget.height
   5960                 widget.height = null
   5961                 var dHeight = widgetHeight(widget) - oldHeight
   5962                 if (dHeight)
   5963                 { updateLineHeight(line, line.height + dHeight) }
   5964             }
   5965             signalLater(cm, "markerChanged", cm, this$1)
   5966         })
   5967     };
   5968 
   5969     TextMarker.prototype.attachLine = function (line) {
   5970         if (!this.lines.length && this.doc.cm) {
   5971             var op = this.doc.cm.curOp
   5972             if (!op.maybeHiddenMarkers || indexOf(op.maybeHiddenMarkers, this) == -1)
   5973             { (op.maybeUnhiddenMarkers || (op.maybeUnhiddenMarkers = [])).push(this) }
   5974         }
   5975         this.lines.push(line)
   5976     };
   5977 
   5978     TextMarker.prototype.detachLine = function (line) {
   5979         this.lines.splice(indexOf(this.lines, line), 1)
   5980         if (!this.lines.length && this.doc.cm) {
   5981             var op = this.doc.cm.curOp
   5982             ;(op.maybeHiddenMarkers || (op.maybeHiddenMarkers = [])).push(this)
   5983         }
   5984     };
   5985     eventMixin(TextMarker)
   5986 
   5987 // Create a marker, wire it up to the right lines, and
   5988     function markText(doc, from, to, options, type) {
   5989         // Shared markers (across linked documents) are handled separately
   5990         // (markTextShared will call out to this again, once per
   5991         // document).
   5992         if (options && options.shared) { return markTextShared(doc, from, to, options, type) }
   5993         // Ensure we are in an operation.
   5994         if (doc.cm && !doc.cm.curOp) { return operation(doc.cm, markText)(doc, from, to, options, type) }
   5995 
   5996         var marker = new TextMarker(doc, type), diff = cmp(from, to)
   5997         if (options) { copyObj(options, marker, false) }
   5998         // Don't connect empty markers unless clearWhenEmpty is false
   5999         if (diff > 0 || diff == 0 && marker.clearWhenEmpty !== false)
   6000         { return marker }
   6001         if (marker.replacedWith) {
   6002             // Showing up as a widget implies collapsed (widget replaces text)
   6003             marker.collapsed = true
   6004             marker.widgetNode = eltP("span", [marker.replacedWith], "CodeMirror-widget")
   6005             if (!options.handleMouseEvents) { marker.widgetNode.setAttribute("cm-ignore-events", "true") }
   6006             if (options.insertLeft) { marker.widgetNode.insertLeft = true }
   6007         }
   6008         if (marker.collapsed) {
   6009             if (conflictingCollapsedRange(doc, from.line, from, to, marker) ||
   6010                 from.line != to.line && conflictingCollapsedRange(doc, to.line, from, to, marker))
   6011             { throw new Error("Inserting collapsed marker partially overlapping an existing one") }
   6012             seeCollapsedSpans()
   6013         }
   6014 
   6015         if (marker.addToHistory)
   6016         { addChangeToHistory(doc, {from: from, to: to, origin: "markText"}, doc.sel, NaN) }
   6017 
   6018         var curLine = from.line, cm = doc.cm, updateMaxLine
   6019         doc.iter(curLine, to.line + 1, function (line) {
   6020             if (cm && marker.collapsed && !cm.options.lineWrapping && visualLine(line) == cm.display.maxLine)
   6021             { updateMaxLine = true }
   6022             if (marker.collapsed && curLine != from.line) { updateLineHeight(line, 0) }
   6023             addMarkedSpan(line, new MarkedSpan(marker,
   6024                 curLine == from.line ? from.ch : null,
   6025                 curLine == to.line ? to.ch : null))
   6026             ++curLine
   6027         })
   6028         // lineIsHidden depends on the presence of the spans, so needs a second pass
   6029         if (marker.collapsed) { doc.iter(from.line, to.line + 1, function (line) {
   6030             if (lineIsHidden(doc, line)) { updateLineHeight(line, 0) }
   6031         }) }
   6032 
   6033         if (marker.clearOnEnter) { on(marker, "beforeCursorEnter", function () { return marker.clear(); }) }
   6034 
   6035         if (marker.readOnly) {
   6036             seeReadOnlySpans()
   6037             if (doc.history.done.length || doc.history.undone.length)
   6038             { doc.clearHistory() }
   6039         }
   6040         if (marker.collapsed) {
   6041             marker.id = ++nextMarkerId
   6042             marker.atomic = true
   6043         }
   6044         if (cm) {
   6045             // Sync editor state
   6046             if (updateMaxLine) { cm.curOp.updateMaxLine = true }
   6047             if (marker.collapsed)
   6048             { regChange(cm, from.line, to.line + 1) }
   6049             else if (marker.className || marker.title || marker.startStyle || marker.endStyle || marker.css)
   6050             { for (var i = from.line; i <= to.line; i++) { regLineChange(cm, i, "text") } }
   6051             if (marker.atomic) { reCheckSelection(cm.doc) }
   6052             signalLater(cm, "markerAdded", cm, marker)
   6053         }
   6054         return marker
   6055     }
   6056 
   6057 // SHARED TEXTMARKERS
   6058 
   6059 // A shared marker spans multiple linked documents. It is
   6060 // implemented as a meta-marker-object controlling multiple normal
   6061 // markers.
   6062     var SharedTextMarker = function(markers, primary) {
   6063         var this$1 = this;
   6064 
   6065         this.markers = markers
   6066         this.primary = primary
   6067         for (var i = 0; i < markers.length; ++i)
   6068         { markers[i].parent = this$1 }
   6069     };
   6070 
   6071     SharedTextMarker.prototype.clear = function () {
   6072         var this$1 = this;
   6073 
   6074         if (this.explicitlyCleared) { return }
   6075         this.explicitlyCleared = true
   6076         for (var i = 0; i < this.markers.length; ++i)
   6077         { this$1.markers[i].clear() }
   6078         signalLater(this, "clear")
   6079     };
   6080 
   6081     SharedTextMarker.prototype.find = function (side, lineObj) {
   6082         return this.primary.find(side, lineObj)
   6083     };
   6084     eventMixin(SharedTextMarker)
   6085 
   6086     function markTextShared(doc, from, to, options, type) {
   6087         options = copyObj(options)
   6088         options.shared = false
   6089         var markers = [markText(doc, from, to, options, type)], primary = markers[0]
   6090         var widget = options.widgetNode
   6091         linkedDocs(doc, function (doc) {
   6092             if (widget) { options.widgetNode = widget.cloneNode(true) }
   6093             markers.push(markText(doc, clipPos(doc, from), clipPos(doc, to), options, type))
   6094             for (var i = 0; i < doc.linked.length; ++i)
   6095             { if (doc.linked[i].isParent) { return } }
   6096             primary = lst(markers)
   6097         })
   6098         return new SharedTextMarker(markers, primary)
   6099     }
   6100 
   6101     function findSharedMarkers(doc) {
   6102         return doc.findMarks(Pos(doc.first, 0), doc.clipPos(Pos(doc.lastLine())), function (m) { return m.parent; })
   6103     }
   6104 
   6105     function copySharedMarkers(doc, markers) {
   6106         for (var i = 0; i < markers.length; i++) {
   6107             var marker = markers[i], pos = marker.find()
   6108             var mFrom = doc.clipPos(pos.from), mTo = doc.clipPos(pos.to)
   6109             if (cmp(mFrom, mTo)) {
   6110                 var subMark = markText(doc, mFrom, mTo, marker.primary, marker.primary.type)
   6111                 marker.markers.push(subMark)
   6112                 subMark.parent = marker
   6113             }
   6114         }
   6115     }
   6116 
   6117     function detachSharedMarkers(markers) {
   6118         var loop = function ( i ) {
   6119             var marker = markers[i], linked = [marker.primary.doc]
   6120             linkedDocs(marker.primary.doc, function (d) { return linked.push(d); })
   6121             for (var j = 0; j < marker.markers.length; j++) {
   6122                 var subMarker = marker.markers[j]
   6123                 if (indexOf(linked, subMarker.doc) == -1) {
   6124                     subMarker.parent = null
   6125                     marker.markers.splice(j--, 1)
   6126                 }
   6127             }
   6128         };
   6129 
   6130         for (var i = 0; i < markers.length; i++) loop( i );
   6131     }
   6132 
   6133     var nextDocId = 0
   6134     var Doc = function(text, mode, firstLine, lineSep, direction) {
   6135         if (!(this instanceof Doc)) { return new Doc(text, mode, firstLine, lineSep, direction) }
   6136         if (firstLine == null) { firstLine = 0 }
   6137 
   6138         BranchChunk.call(this, [new LeafChunk([new Line("", null)])])
   6139         this.first = firstLine
   6140         this.scrollTop = this.scrollLeft = 0
   6141         this.cantEdit = false
   6142         this.cleanGeneration = 1
   6143         this.modeFrontier = this.highlightFrontier = firstLine
   6144         var start = Pos(firstLine, 0)
   6145         this.sel = simpleSelection(start)
   6146         this.history = new History(null)
   6147         this.id = ++nextDocId
   6148         this.modeOption = mode
   6149         this.lineSep = lineSep
   6150         this.direction = (direction == "rtl") ? "rtl" : "ltr"
   6151         this.extend = false
   6152 
   6153         if (typeof text == "string") { text = this.splitLines(text) }
   6154         updateDoc(this, {from: start, to: start, text: text})
   6155         setSelection(this, simpleSelection(start), sel_dontScroll)
   6156     }
   6157 
   6158     Doc.prototype = createObj(BranchChunk.prototype, {
   6159         constructor: Doc,
   6160         // Iterate over the document. Supports two forms -- with only one
   6161         // argument, it calls that for each line in the document. With
   6162         // three, it iterates over the range given by the first two (with
   6163         // the second being non-inclusive).
   6164         iter: function(from, to, op) {
   6165             if (op) { this.iterN(from - this.first, to - from, op) }
   6166             else { this.iterN(this.first, this.first + this.size, from) }
   6167         },
   6168 
   6169         // Non-public interface for adding and removing lines.
   6170         insert: function(at, lines) {
   6171             var height = 0
   6172             for (var i = 0; i < lines.length; ++i) { height += lines[i].height }
   6173             this.insertInner(at - this.first, lines, height)
   6174         },
   6175         remove: function(at, n) { this.removeInner(at - this.first, n) },
   6176 
   6177         // From here, the methods are part of the public interface. Most
   6178         // are also available from CodeMirror (editor) instances.
   6179 
   6180         getValue: function(lineSep) {
   6181             var lines = getLines(this, this.first, this.first + this.size)
   6182             if (lineSep === false) { return lines }
   6183             return lines.join(lineSep || this.lineSeparator())
   6184         },
   6185         setValue: docMethodOp(function(code) {
   6186             var top = Pos(this.first, 0), last = this.first + this.size - 1
   6187             makeChange(this, {from: top, to: Pos(last, getLine(this, last).text.length),
   6188                 text: this.splitLines(code), origin: "setValue", full: true}, true)
   6189             if (this.cm) { scrollToCoords(this.cm, 0, 0) }
   6190             setSelection(this, simpleSelection(top), sel_dontScroll)
   6191         }),
   6192         replaceRange: function(code, from, to, origin) {
   6193             from = clipPos(this, from)
   6194             to = to ? clipPos(this, to) : from
   6195             replaceRange(this, code, from, to, origin)
   6196         },
   6197         getRange: function(from, to, lineSep) {
   6198             var lines = getBetween(this, clipPos(this, from), clipPos(this, to))
   6199             if (lineSep === false) { return lines }
   6200             return lines.join(lineSep || this.lineSeparator())
   6201         },
   6202 
   6203         getLine: function(line) {var l = this.getLineHandle(line); return l && l.text},
   6204 
   6205         getLineHandle: function(line) {if (isLine(this, line)) { return getLine(this, line) }},
   6206         getLineNumber: function(line) {return lineNo(line)},
   6207 
   6208         getLineHandleVisualStart: function(line) {
   6209             if (typeof line == "number") { line = getLine(this, line) }
   6210             return visualLine(line)
   6211         },
   6212 
   6213         lineCount: function() {return this.size},
   6214         firstLine: function() {return this.first},
   6215         lastLine: function() {return this.first + this.size - 1},
   6216 
   6217         clipPos: function(pos) {return clipPos(this, pos)},
   6218 
   6219         getCursor: function(start) {
   6220             var range = this.sel.primary(), pos
   6221             if (start == null || start == "head") { pos = range.head }
   6222             else if (start == "anchor") { pos = range.anchor }
   6223             else if (start == "end" || start == "to" || start === false) { pos = range.to() }
   6224             else { pos = range.from() }
   6225             return pos
   6226         },
   6227         listSelections: function() { return this.sel.ranges },
   6228         somethingSelected: function() {return this.sel.somethingSelected()},
   6229 
   6230         setCursor: docMethodOp(function(line, ch, options) {
   6231             setSimpleSelection(this, clipPos(this, typeof line == "number" ? Pos(line, ch || 0) : line), null, options)
   6232         }),
   6233         setSelection: docMethodOp(function(anchor, head, options) {
   6234             setSimpleSelection(this, clipPos(this, anchor), clipPos(this, head || anchor), options)
   6235         }),
   6236         extendSelection: docMethodOp(function(head, other, options) {
   6237             extendSelection(this, clipPos(this, head), other && clipPos(this, other), options)
   6238         }),
   6239         extendSelections: docMethodOp(function(heads, options) {
   6240             extendSelections(this, clipPosArray(this, heads), options)
   6241         }),
   6242         extendSelectionsBy: docMethodOp(function(f, options) {
   6243             var heads = map(this.sel.ranges, f)
   6244             extendSelections(this, clipPosArray(this, heads), options)
   6245         }),
   6246         setSelections: docMethodOp(function(ranges, primary, options) {
   6247             var this$1 = this;
   6248 
   6249             if (!ranges.length) { return }
   6250             var out = []
   6251             for (var i = 0; i < ranges.length; i++)
   6252             { out[i] = new Range(clipPos(this$1, ranges[i].anchor),
   6253                 clipPos(this$1, ranges[i].head)) }
   6254             if (primary == null) { primary = Math.min(ranges.length - 1, this.sel.primIndex) }
   6255             setSelection(this, normalizeSelection(out, primary), options)
   6256         }),
   6257         addSelection: docMethodOp(function(anchor, head, options) {
   6258             var ranges = this.sel.ranges.slice(0)
   6259             ranges.push(new Range(clipPos(this, anchor), clipPos(this, head || anchor)))
   6260             setSelection(this, normalizeSelection(ranges, ranges.length - 1), options)
   6261         }),
   6262 
   6263         getSelection: function(lineSep) {
   6264             var this$1 = this;
   6265 
   6266             var ranges = this.sel.ranges, lines
   6267             for (var i = 0; i < ranges.length; i++) {
   6268                 var sel = getBetween(this$1, ranges[i].from(), ranges[i].to())
   6269                 lines = lines ? lines.concat(sel) : sel
   6270             }
   6271             if (lineSep === false) { return lines }
   6272             else { return lines.join(lineSep || this.lineSeparator()) }
   6273         },
   6274         getSelections: function(lineSep) {
   6275             var this$1 = this;
   6276 
   6277             var parts = [], ranges = this.sel.ranges
   6278             for (var i = 0; i < ranges.length; i++) {
   6279                 var sel = getBetween(this$1, ranges[i].from(), ranges[i].to())
   6280                 if (lineSep !== false) { sel = sel.join(lineSep || this$1.lineSeparator()) }
   6281                 parts[i] = sel
   6282             }
   6283             return parts
   6284         },
   6285         replaceSelection: function(code, collapse, origin) {
   6286             var dup = []
   6287             for (var i = 0; i < this.sel.ranges.length; i++)
   6288             { dup[i] = code }
   6289             this.replaceSelections(dup, collapse, origin || "+input")
   6290         },
   6291         replaceSelections: docMethodOp(function(code, collapse, origin) {
   6292             var this$1 = this;
   6293 
   6294             var changes = [], sel = this.sel
   6295             for (var i = 0; i < sel.ranges.length; i++) {
   6296                 var range = sel.ranges[i]
   6297                 changes[i] = {from: range.from(), to: range.to(), text: this$1.splitLines(code[i]), origin: origin}
   6298             }
   6299             var newSel = collapse && collapse != "end" && computeReplacedSel(this, changes, collapse)
   6300             for (var i$1 = changes.length - 1; i$1 >= 0; i$1--)
   6301             { makeChange(this$1, changes[i$1]) }
   6302             if (newSel) { setSelectionReplaceHistory(this, newSel) }
   6303             else if (this.cm) { ensureCursorVisible(this.cm) }
   6304         }),
   6305         undo: docMethodOp(function() {makeChangeFromHistory(this, "undo")}),
   6306         redo: docMethodOp(function() {makeChangeFromHistory(this, "redo")}),
   6307         undoSelection: docMethodOp(function() {makeChangeFromHistory(this, "undo", true)}),
   6308         redoSelection: docMethodOp(function() {makeChangeFromHistory(this, "redo", true)}),
   6309 
   6310         setExtending: function(val) {this.extend = val},
   6311         getExtending: function() {return this.extend},
   6312 
   6313         historySize: function() {
   6314             var hist = this.history, done = 0, undone = 0
   6315             for (var i = 0; i < hist.done.length; i++) { if (!hist.done[i].ranges) { ++done } }
   6316             for (var i$1 = 0; i$1 < hist.undone.length; i$1++) { if (!hist.undone[i$1].ranges) { ++undone } }
   6317             return {undo: done, redo: undone}
   6318         },
   6319         clearHistory: function() {this.history = new History(this.history.maxGeneration)},
   6320 
   6321         markClean: function() {
   6322             this.cleanGeneration = this.changeGeneration(true)
   6323         },
   6324         changeGeneration: function(forceSplit) {
   6325             if (forceSplit)
   6326             { this.history.lastOp = this.history.lastSelOp = this.history.lastOrigin = null }
   6327             return this.history.generation
   6328         },
   6329         isClean: function (gen) {
   6330             return this.history.generation == (gen || this.cleanGeneration)
   6331         },
   6332 
   6333         getHistory: function() {
   6334             return {done: copyHistoryArray(this.history.done),
   6335                 undone: copyHistoryArray(this.history.undone)}
   6336         },
   6337         setHistory: function(histData) {
   6338             var hist = this.history = new History(this.history.maxGeneration)
   6339             hist.done = copyHistoryArray(histData.done.slice(0), null, true)
   6340             hist.undone = copyHistoryArray(histData.undone.slice(0), null, true)
   6341         },
   6342 
   6343         setGutterMarker: docMethodOp(function(line, gutterID, value) {
   6344             return changeLine(this, line, "gutter", function (line) {
   6345                 var markers = line.gutterMarkers || (line.gutterMarkers = {})
   6346                 markers[gutterID] = value
   6347                 if (!value && isEmpty(markers)) { line.gutterMarkers = null }
   6348                 return true
   6349             })
   6350         }),
   6351 
   6352         clearGutter: docMethodOp(function(gutterID) {
   6353             var this$1 = this;
   6354 
   6355             this.iter(function (line) {
   6356                 if (line.gutterMarkers && line.gutterMarkers[gutterID]) {
   6357                     changeLine(this$1, line, "gutter", function () {
   6358                         line.gutterMarkers[gutterID] = null
   6359                         if (isEmpty(line.gutterMarkers)) { line.gutterMarkers = null }
   6360                         return true
   6361                     })
   6362                 }
   6363             })
   6364         }),
   6365 
   6366         lineInfo: function(line) {
   6367             var n
   6368             if (typeof line == "number") {
   6369                 if (!isLine(this, line)) { return null }
   6370                 n = line
   6371                 line = getLine(this, line)
   6372                 if (!line) { return null }
   6373             } else {
   6374                 n = lineNo(line)
   6375                 if (n == null) { return null }
   6376             }
   6377             return {line: n, handle: line, text: line.text, gutterMarkers: line.gutterMarkers,
   6378                 textClass: line.textClass, bgClass: line.bgClass, wrapClass: line.wrapClass,
   6379                 widgets: line.widgets}
   6380         },
   6381 
   6382         addLineClass: docMethodOp(function(handle, where, cls) {
   6383             return changeLine(this, handle, where == "gutter" ? "gutter" : "class", function (line) {
   6384                 var prop = where == "text" ? "textClass"
   6385                     : where == "background" ? "bgClass"
   6386                         : where == "gutter" ? "gutterClass" : "wrapClass"
   6387                 if (!line[prop]) { line[prop] = cls }
   6388                 else if (classTest(cls).test(line[prop])) { return false }
   6389                 else { line[prop] += " " + cls }
   6390                 return true
   6391             })
   6392         }),
   6393         removeLineClass: docMethodOp(function(handle, where, cls) {
   6394             return changeLine(this, handle, where == "gutter" ? "gutter" : "class", function (line) {
   6395                 var prop = where == "text" ? "textClass"
   6396                     : where == "background" ? "bgClass"
   6397                         : where == "gutter" ? "gutterClass" : "wrapClass"
   6398                 var cur = line[prop]
   6399                 if (!cur) { return false }
   6400                 else if (cls == null) { line[prop] = null }
   6401                 else {
   6402                     var found = cur.match(classTest(cls))
   6403                     if (!found) { return false }
   6404                     var end = found.index + found[0].length
   6405                     line[prop] = cur.slice(0, found.index) + (!found.index || end == cur.length ? "" : " ") + cur.slice(end) || null
   6406                 }
   6407                 return true
   6408             })
   6409         }),
   6410 
   6411         addLineWidget: docMethodOp(function(handle, node, options) {
   6412             return addLineWidget(this, handle, node, options)
   6413         }),
   6414         removeLineWidget: function(widget) { widget.clear() },
   6415 
   6416         markText: function(from, to, options) {
   6417             return markText(this, clipPos(this, from), clipPos(this, to), options, options && options.type || "range")
   6418         },
   6419         setBookmark: function(pos, options) {
   6420             var realOpts = {replacedWith: options && (options.nodeType == null ? options.widget : options),
   6421                 insertLeft: options && options.insertLeft,
   6422                 clearWhenEmpty: false, shared: options && options.shared,
   6423                 handleMouseEvents: options && options.handleMouseEvents}
   6424             pos = clipPos(this, pos)
   6425             return markText(this, pos, pos, realOpts, "bookmark")
   6426         },
   6427         findMarksAt: function(pos) {
   6428             pos = clipPos(this, pos)
   6429             var markers = [], spans = getLine(this, pos.line).markedSpans
   6430             if (spans) { for (var i = 0; i < spans.length; ++i) {
   6431                 var span = spans[i]
   6432                 if ((span.from == null || span.from <= pos.ch) &&
   6433                     (span.to == null || span.to >= pos.ch))
   6434                 { markers.push(span.marker.parent || span.marker) }
   6435             } }
   6436             return markers
   6437         },
   6438         findMarks: function(from, to, filter) {
   6439             from = clipPos(this, from); to = clipPos(this, to)
   6440             var found = [], lineNo = from.line
   6441             this.iter(from.line, to.line + 1, function (line) {
   6442                 var spans = line.markedSpans
   6443                 if (spans) { for (var i = 0; i < spans.length; i++) {
   6444                     var span = spans[i]
   6445                     if (!(span.to != null && lineNo == from.line && from.ch >= span.to ||
   6446                         span.from == null && lineNo != from.line ||
   6447                         span.from != null && lineNo == to.line && span.from >= to.ch) &&
   6448                         (!filter || filter(span.marker)))
   6449                     { found.push(span.marker.parent || span.marker) }
   6450                 } }
   6451                 ++lineNo
   6452             })
   6453             return found
   6454         },
   6455         getAllMarks: function() {
   6456             var markers = []
   6457             this.iter(function (line) {
   6458                 var sps = line.markedSpans
   6459                 if (sps) { for (var i = 0; i < sps.length; ++i)
   6460                 { if (sps[i].from != null) { markers.push(sps[i].marker) } } }
   6461             })
   6462             return markers
   6463         },
   6464 
   6465         posFromIndex: function(off) {
   6466             var ch, lineNo = this.first, sepSize = this.lineSeparator().length
   6467             this.iter(function (line) {
   6468                 var sz = line.text.length + sepSize
   6469                 if (sz > off) { ch = off; return true }
   6470                 off -= sz
   6471                 ++lineNo
   6472             })
   6473             return clipPos(this, Pos(lineNo, ch))
   6474         },
   6475         indexFromPos: function (coords) {
   6476             coords = clipPos(this, coords)
   6477             var index = coords.ch
   6478             if (coords.line < this.first || coords.ch < 0) { return 0 }
   6479             var sepSize = this.lineSeparator().length
   6480             this.iter(this.first, coords.line, function (line) { // iter aborts when callback returns a truthy value
   6481                 index += line.text.length + sepSize
   6482             })
   6483             return index
   6484         },
   6485 
   6486         copy: function(copyHistory) {
   6487             var doc = new Doc(getLines(this, this.first, this.first + this.size),
   6488                 this.modeOption, this.first, this.lineSep, this.direction)
   6489             doc.scrollTop = this.scrollTop; doc.scrollLeft = this.scrollLeft
   6490             doc.sel = this.sel
   6491             doc.extend = false
   6492             if (copyHistory) {
   6493                 doc.history.undoDepth = this.history.undoDepth
   6494                 doc.setHistory(this.getHistory())
   6495             }
   6496             return doc
   6497         },
   6498 
   6499         linkedDoc: function(options) {
   6500             if (!options) { options = {} }
   6501             var from = this.first, to = this.first + this.size
   6502             if (options.from != null && options.from > from) { from = options.from }
   6503             if (options.to != null && options.to < to) { to = options.to }
   6504             var copy = new Doc(getLines(this, from, to), options.mode || this.modeOption, from, this.lineSep, this.direction)
   6505             if (options.sharedHist) { copy.history = this.history
   6506             ; }(this.linked || (this.linked = [])).push({doc: copy, sharedHist: options.sharedHist})
   6507             copy.linked = [{doc: this, isParent: true, sharedHist: options.sharedHist}]
   6508             copySharedMarkers(copy, findSharedMarkers(this))
   6509             return copy
   6510         },
   6511         unlinkDoc: function(other) {
   6512             var this$1 = this;
   6513 
   6514             if (other instanceof CodeMirror) { other = other.doc }
   6515             if (this.linked) { for (var i = 0; i < this.linked.length; ++i) {
   6516                 var link = this$1.linked[i]
   6517                 if (link.doc != other) { continue }
   6518                 this$1.linked.splice(i, 1)
   6519                 other.unlinkDoc(this$1)
   6520                 detachSharedMarkers(findSharedMarkers(this$1))
   6521                 break
   6522             } }
   6523             // If the histories were shared, split them again
   6524             if (other.history == this.history) {
   6525                 var splitIds = [other.id]
   6526                 linkedDocs(other, function (doc) { return splitIds.push(doc.id); }, true)
   6527                 other.history = new History(null)
   6528                 other.history.done = copyHistoryArray(this.history.done, splitIds)
   6529                 other.history.undone = copyHistoryArray(this.history.undone, splitIds)
   6530             }
   6531         },
   6532         iterLinkedDocs: function(f) {linkedDocs(this, f)},
   6533 
   6534         getMode: function() {return this.mode},
   6535         getEditor: function() {return this.cm},
   6536 
   6537         splitLines: function(str) {
   6538             if (this.lineSep) { return str.split(this.lineSep) }
   6539             return splitLinesAuto(str)
   6540         },
   6541         lineSeparator: function() { return this.lineSep || "\n" },
   6542 
   6543         setDirection: docMethodOp(function (dir) {
   6544             if (dir != "rtl") { dir = "ltr" }
   6545             if (dir == this.direction) { return }
   6546             this.direction = dir
   6547             this.iter(function (line) { return line.order = null; })
   6548             if (this.cm) { directionChanged(this.cm) }
   6549         })
   6550     })
   6551 
   6552 // Public alias.
   6553     Doc.prototype.eachLine = Doc.prototype.iter
   6554 
   6555 // Kludge to work around strange IE behavior where it'll sometimes
   6556 // re-fire a series of drag-related events right after the drop (#1551)
   6557     var lastDrop = 0
   6558 
   6559     function onDrop(e) {
   6560         var cm = this
   6561         clearDragCursor(cm)
   6562         if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e))
   6563         { return }
   6564         e_preventDefault(e)
   6565         if (ie) { lastDrop = +new Date }
   6566         var pos = posFromMouse(cm, e, true), files = e.dataTransfer.files
   6567         if (!pos || cm.isReadOnly()) { return }
   6568         // Might be a file drop, in which case we simply extract the text
   6569         // and insert it.
   6570         if (files && files.length && window.FileReader && window.File) {
   6571             var n = files.length, text = Array(n), read = 0
   6572             var loadFile = function (file, i) {
   6573                 if (cm.options.allowDropFileTypes &&
   6574                     indexOf(cm.options.allowDropFileTypes, file.type) == -1)
   6575                 { return }
   6576 
   6577                 var reader = new FileReader
   6578                 reader.onload = operation(cm, function () {
   6579                     var content = reader.result
   6580                     if (/[\x00-\x08\x0e-\x1f]{2}/.test(content)) { content = "" }
   6581                     text[i] = content
   6582                     if (++read == n) {
   6583                         pos = clipPos(cm.doc, pos)
   6584                         var change = {from: pos, to: pos,
   6585                             text: cm.doc.splitLines(text.join(cm.doc.lineSeparator())),
   6586                             origin: "paste"}
   6587                         makeChange(cm.doc, change)
   6588                         setSelectionReplaceHistory(cm.doc, simpleSelection(pos, changeEnd(change)))
   6589                     }
   6590                 })
   6591                 reader.readAsText(file)
   6592             }
   6593             for (var i = 0; i < n; ++i) { loadFile(files[i], i) }
   6594         } else { // Normal drop
   6595             // Don't do a replace if the drop happened inside of the selected text.
   6596             if (cm.state.draggingText && cm.doc.sel.contains(pos) > -1) {
   6597                 cm.state.draggingText(e)
   6598                 // Ensure the editor is re-focused
   6599                 setTimeout(function () { return cm.display.input.focus(); }, 20)
   6600                 return
   6601             }
   6602             try {
   6603                 var text$1 = e.dataTransfer.getData("Text")
   6604                 if (text$1) {
   6605                     var selected
   6606                     if (cm.state.draggingText && !cm.state.draggingText.copy)
   6607                     { selected = cm.listSelections() }
   6608                     setSelectionNoUndo(cm.doc, simpleSelection(pos, pos))
   6609                     if (selected) { for (var i$1 = 0; i$1 < selected.length; ++i$1)
   6610                     { replaceRange(cm.doc, "", selected[i$1].anchor, selected[i$1].head, "drag") } }
   6611                     cm.replaceSelection(text$1, "around", "paste")
   6612                     cm.display.input.focus()
   6613                 }
   6614             }
   6615             catch(e){}
   6616         }
   6617     }
   6618 
   6619     function onDragStart(cm, e) {
   6620         if (ie && (!cm.state.draggingText || +new Date - lastDrop < 100)) { e_stop(e); return }
   6621         if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e)) { return }
   6622 
   6623         e.dataTransfer.setData("Text", cm.getSelection())
   6624         e.dataTransfer.effectAllowed = "copyMove"
   6625 
   6626         // Use dummy image instead of default browsers image.
   6627         // Recent Safari (~6.0.2) have a tendency to segfault when this happens, so we don't do it there.
   6628         if (e.dataTransfer.setDragImage && !safari) {
   6629             var img = elt("img", null, null, "position: fixed; left: 0; top: 0;")
   6630             img.src = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
   6631             if (presto) {
   6632                 img.width = img.height = 1
   6633                 cm.display.wrapper.appendChild(img)
   6634                 // Force a relayout, or Opera won't use our image for some obscure reason
   6635                 img._top = img.offsetTop
   6636             }
   6637             e.dataTransfer.setDragImage(img, 0, 0)
   6638             if (presto) { img.parentNode.removeChild(img) }
   6639         }
   6640     }
   6641 
   6642     function onDragOver(cm, e) {
   6643         var pos = posFromMouse(cm, e)
   6644         if (!pos) { return }
   6645         var frag = document.createDocumentFragment()
   6646         drawSelectionCursor(cm, pos, frag)
   6647         if (!cm.display.dragCursor) {
   6648             cm.display.dragCursor = elt("div", null, "CodeMirror-cursors CodeMirror-dragcursors")
   6649             cm.display.lineSpace.insertBefore(cm.display.dragCursor, cm.display.cursorDiv)
   6650         }
   6651         removeChildrenAndAdd(cm.display.dragCursor, frag)
   6652     }
   6653 
   6654     function clearDragCursor(cm) {
   6655         if (cm.display.dragCursor) {
   6656             cm.display.lineSpace.removeChild(cm.display.dragCursor)
   6657             cm.display.dragCursor = null
   6658         }
   6659     }
   6660 
   6661 // These must be handled carefully, because naively registering a
   6662 // handler for each editor will cause the editors to never be
   6663 // garbage collected.
   6664 
   6665     function forEachCodeMirror(f) {
   6666         if (!document.getElementsByClassName) { return }
   6667         var byClass = document.getElementsByClassName("CodeMirror")
   6668         for (var i = 0; i < byClass.length; i++) {
   6669             var cm = byClass[i].CodeMirror
   6670             if (cm) { f(cm) }
   6671         }
   6672     }
   6673 
   6674     var globalsRegistered = false
   6675     function ensureGlobalHandlers() {
   6676         if (globalsRegistered) { return }
   6677         registerGlobalHandlers()
   6678         globalsRegistered = true
   6679     }
   6680     function registerGlobalHandlers() {
   6681         // When the window resizes, we need to refresh active editors.
   6682         var resizeTimer
   6683         on(window, "resize", function () {
   6684             if (resizeTimer == null) { resizeTimer = setTimeout(function () {
   6685                 resizeTimer = null
   6686                 forEachCodeMirror(onResize)
   6687             }, 100) }
   6688         })
   6689         // When the window loses focus, we want to show the editor as blurred
   6690         on(window, "blur", function () { return forEachCodeMirror(onBlur); })
   6691     }
   6692 // Called when the window resizes
   6693     function onResize(cm) {
   6694         var d = cm.display
   6695         if (d.lastWrapHeight == d.wrapper.clientHeight && d.lastWrapWidth == d.wrapper.clientWidth)
   6696         { return }
   6697         // Might be a text scaling operation, clear size caches.
   6698         d.cachedCharWidth = d.cachedTextHeight = d.cachedPaddingH = null
   6699         d.scrollbarsClipped = false
   6700         cm.setSize()
   6701     }
   6702 
   6703     var keyNames = {
   6704         3: "Enter", 8: "Backspace", 9: "Tab", 13: "Enter", 16: "Shift", 17: "Ctrl", 18: "Alt",
   6705         19: "Pause", 20: "CapsLock", 27: "Esc", 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End",
   6706         36: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "PrintScrn", 45: "Insert",
   6707         46: "Delete", 59: ";", 61: "=", 91: "Mod", 92: "Mod", 93: "Mod",
   6708         106: "*", 107: "=", 109: "-", 110: ".", 111: "/", 127: "Delete",
   6709         173: "-", 186: ";", 187: "=", 188: ",", 189: "-", 190: ".", 191: "/", 192: "`", 219: "[", 220: "\\",
   6710         221: "]", 222: "'", 63232: "Up", 63233: "Down", 63234: "Left", 63235: "Right", 63272: "Delete",
   6711         63273: "Home", 63275: "End", 63276: "PageUp", 63277: "PageDown", 63302: "Insert"
   6712     }
   6713 
   6714 // Number keys
   6715     for (var i = 0; i < 10; i++) { keyNames[i + 48] = keyNames[i + 96] = String(i) }
   6716 // Alphabetic keys
   6717     for (var i$1 = 65; i$1 <= 90; i$1++) { keyNames[i$1] = String.fromCharCode(i$1) }
   6718 // Function keys
   6719     for (var i$2 = 1; i$2 <= 12; i$2++) { keyNames[i$2 + 111] = keyNames[i$2 + 63235] = "F" + i$2 }
   6720 
   6721     var keyMap = {}
   6722 
   6723     keyMap.basic = {
   6724         "Left": "goCharLeft", "Right": "goCharRight", "Up": "goLineUp", "Down": "goLineDown",
   6725         "End": "goLineEnd", "Home": "goLineStartSmart", "PageUp": "goPageUp", "PageDown": "goPageDown",
   6726         "Delete": "delCharAfter", "Backspace": "delCharBefore", "Shift-Backspace": "delCharBefore",
   6727         "Tab": "defaultTab", "Shift-Tab": "indentAuto",
   6728         "Enter": "newlineAndIndent", "Insert": "toggleOverwrite",
   6729         "Esc": "singleSelection"
   6730     }
   6731 // Note that the save and find-related commands aren't defined by
   6732 // default. User code or addons can define them. Unknown commands
   6733 // are simply ignored.
   6734     keyMap.pcDefault = {
   6735         "Ctrl-A": "selectAll", "Ctrl-D": "deleteLine", "Ctrl-Z": "undo", "Shift-Ctrl-Z": "redo", "Ctrl-Y": "redo",
   6736         "Ctrl-Home": "goDocStart", "Ctrl-End": "goDocEnd", "Ctrl-Up": "goLineUp", "Ctrl-Down": "goLineDown",
   6737         "Ctrl-Left": "goGroupLeft", "Ctrl-Right": "goGroupRight", "Alt-Left": "goLineStart", "Alt-Right": "goLineEnd",
   6738         "Ctrl-Backspace": "delGroupBefore", "Ctrl-Delete": "delGroupAfter", "Ctrl-S": "save", "Ctrl-F": "find",
   6739         "Ctrl-G": "findNext", "Shift-Ctrl-G": "findPrev", "Shift-Ctrl-F": "replace", "Shift-Ctrl-R": "replaceAll",
   6740         "Ctrl-[": "indentLess", "Ctrl-]": "indentMore",
   6741         "Ctrl-U": "undoSelection", "Shift-Ctrl-U": "redoSelection", "Alt-U": "redoSelection",
   6742         fallthrough: "basic"
   6743     }
   6744 // Very basic readline/emacs-style bindings, which are standard on Mac.
   6745     keyMap.emacsy = {
   6746         "Ctrl-F": "goCharRight", "Ctrl-B": "goCharLeft", "Ctrl-P": "goLineUp", "Ctrl-N": "goLineDown",
   6747         "Alt-F": "goWordRight", "Alt-B": "goWordLeft", "Ctrl-A": "goLineStart", "Ctrl-E": "goLineEnd",
   6748         "Ctrl-V": "goPageDown", "Shift-Ctrl-V": "goPageUp", "Ctrl-D": "delCharAfter", "Ctrl-H": "delCharBefore",
   6749         "Alt-D": "delWordAfter", "Alt-Backspace": "delWordBefore", "Ctrl-K": "killLine", "Ctrl-T": "transposeChars",
   6750         "Ctrl-O": "openLine"
   6751     }
   6752     keyMap.macDefault = {
   6753         "Cmd-A": "selectAll", "Cmd-D": "deleteLine", "Cmd-Z": "undo", "Shift-Cmd-Z": "redo", "Cmd-Y": "redo",
   6754         "Cmd-Home": "goDocStart", "Cmd-Up": "goDocStart", "Cmd-End": "goDocEnd", "Cmd-Down": "goDocEnd", "Alt-Left": "goGroupLeft",
   6755         "Alt-Right": "goGroupRight", "Cmd-Left": "goLineLeft", "Cmd-Right": "goLineRight", "Alt-Backspace": "delGroupBefore",
   6756         "Ctrl-Alt-Backspace": "delGroupAfter", "Alt-Delete": "delGroupAfter", "Cmd-S": "save", "Cmd-F": "find",
   6757         "Cmd-G": "findNext", "Shift-Cmd-G": "findPrev", "Cmd-Alt-F": "replace", "Shift-Cmd-Alt-F": "replaceAll",
   6758         "Cmd-[": "indentLess", "Cmd-]": "indentMore", "Cmd-Backspace": "delWrappedLineLeft", "Cmd-Delete": "delWrappedLineRight",
   6759         "Cmd-U": "undoSelection", "Shift-Cmd-U": "redoSelection", "Ctrl-Up": "goDocStart", "Ctrl-Down": "goDocEnd",
   6760         fallthrough: ["basic", "emacsy"]
   6761     }
   6762     keyMap["default"] = mac ? keyMap.macDefault : keyMap.pcDefault
   6763 
   6764 // KEYMAP DISPATCH
   6765 
   6766     function normalizeKeyName(name) {
   6767         var parts = name.split(/-(?!$)/)
   6768         name = parts[parts.length - 1]
   6769         var alt, ctrl, shift, cmd
   6770         for (var i = 0; i < parts.length - 1; i++) {
   6771             var mod = parts[i]
   6772             if (/^(cmd|meta|m)$/i.test(mod)) { cmd = true }
   6773             else if (/^a(lt)?$/i.test(mod)) { alt = true }
   6774             else if (/^(c|ctrl|control)$/i.test(mod)) { ctrl = true }
   6775             else if (/^s(hift)?$/i.test(mod)) { shift = true }
   6776             else { throw new Error("Unrecognized modifier name: " + mod) }
   6777         }
   6778         if (alt) { name = "Alt-" + name }
   6779         if (ctrl) { name = "Ctrl-" + name }
   6780         if (cmd) { name = "Cmd-" + name }
   6781         if (shift) { name = "Shift-" + name }
   6782         return name
   6783     }
   6784 
   6785 // This is a kludge to keep keymaps mostly working as raw objects
   6786 // (backwards compatibility) while at the same time support features
   6787 // like normalization and multi-stroke key bindings. It compiles a
   6788 // new normalized keymap, and then updates the old object to reflect
   6789 // this.
   6790     function normalizeKeyMap(keymap) {
   6791         var copy = {}
   6792         for (var keyname in keymap) { if (keymap.hasOwnProperty(keyname)) {
   6793             var value = keymap[keyname]
   6794             if (/^(name|fallthrough|(de|at)tach)$/.test(keyname)) { continue }
   6795             if (value == "...") { delete keymap[keyname]; continue }
   6796 
   6797             var keys = map(keyname.split(" "), normalizeKeyName)
   6798             for (var i = 0; i < keys.length; i++) {
   6799                 var val = (void 0), name = (void 0)
   6800                 if (i == keys.length - 1) {
   6801                     name = keys.join(" ")
   6802                     val = value
   6803                 } else {
   6804                     name = keys.slice(0, i + 1).join(" ")
   6805                     val = "..."
   6806                 }
   6807                 var prev = copy[name]
   6808                 if (!prev) { copy[name] = val }
   6809                 else if (prev != val) { throw new Error("Inconsistent bindings for " + name) }
   6810             }
   6811             delete keymap[keyname]
   6812         } }
   6813         for (var prop in copy) { keymap[prop] = copy[prop] }
   6814         return keymap
   6815     }
   6816 
   6817     function lookupKey(key, map, handle, context) {
   6818         map = getKeyMap(map)
   6819         var found = map.call ? map.call(key, context) : map[key]
   6820         if (found === false) { return "nothing" }
   6821         if (found === "...") { return "multi" }
   6822         if (found != null && handle(found)) { return "handled" }
   6823 
   6824         if (map.fallthrough) {
   6825             if (Object.prototype.toString.call(map.fallthrough) != "[object Array]")
   6826             { return lookupKey(key, map.fallthrough, handle, context) }
   6827             for (var i = 0; i < map.fallthrough.length; i++) {
   6828                 var result = lookupKey(key, map.fallthrough[i], handle, context)
   6829                 if (result) { return result }
   6830             }
   6831         }
   6832     }
   6833 
   6834 // Modifier key presses don't count as 'real' key presses for the
   6835 // purpose of keymap fallthrough.
   6836     function isModifierKey(value) {
   6837         var name = typeof value == "string" ? value : keyNames[value.keyCode]
   6838         return name == "Ctrl" || name == "Alt" || name == "Shift" || name == "Mod"
   6839     }
   6840 
   6841     function addModifierNames(name, event, noShift) {
   6842         var base = name
   6843         if (event.altKey && base != "Alt") { name = "Alt-" + name }
   6844         if ((flipCtrlCmd ? event.metaKey : event.ctrlKey) && base != "Ctrl") { name = "Ctrl-" + name }
   6845         if ((flipCtrlCmd ? event.ctrlKey : event.metaKey) && base != "Cmd") { name = "Cmd-" + name }
   6846         if (!noShift && event.shiftKey && base != "Shift") { name = "Shift-" + name }
   6847         return name
   6848     }
   6849 
   6850 // Look up the name of a key as indicated by an event object.
   6851     function keyName(event, noShift) {
   6852         if (presto && event.keyCode == 34 && event["char"]) { return false }
   6853         var name = keyNames[event.keyCode]
   6854         if (name == null || event.altGraphKey) { return false }
   6855         return addModifierNames(name, event, noShift)
   6856     }
   6857 
   6858     function getKeyMap(val) {
   6859         return typeof val == "string" ? keyMap[val] : val
   6860     }
   6861 
   6862 // Helper for deleting text near the selection(s), used to implement
   6863 // backspace, delete, and similar functionality.
   6864     function deleteNearSelection(cm, compute) {
   6865         var ranges = cm.doc.sel.ranges, kill = []
   6866         // Build up a set of ranges to kill first, merging overlapping
   6867         // ranges.
   6868         for (var i = 0; i < ranges.length; i++) {
   6869             var toKill = compute(ranges[i])
   6870             while (kill.length && cmp(toKill.from, lst(kill).to) <= 0) {
   6871                 var replaced = kill.pop()
   6872                 if (cmp(replaced.from, toKill.from) < 0) {
   6873                     toKill.from = replaced.from
   6874                     break
   6875                 }
   6876             }
   6877             kill.push(toKill)
   6878         }
   6879         // Next, remove those actual ranges.
   6880         runInOp(cm, function () {
   6881             for (var i = kill.length - 1; i >= 0; i--)
   6882             { replaceRange(cm.doc, "", kill[i].from, kill[i].to, "+delete") }
   6883             ensureCursorVisible(cm)
   6884         })
   6885     }
   6886 
   6887     function moveCharLogically(line, ch, dir) {
   6888         var target = skipExtendingChars(line.text, ch + dir, dir)
   6889         return target < 0 || target > line.text.length ? null : target
   6890     }
   6891 
   6892     function moveLogically(line, start, dir) {
   6893         var ch = moveCharLogically(line, start.ch, dir)
   6894         return ch == null ? null : new Pos(start.line, ch, dir < 0 ? "after" : "before")
   6895     }
   6896 
   6897     function endOfLine(visually, cm, lineObj, lineNo, dir) {
   6898         if (visually) {
   6899             var order = getOrder(lineObj, cm.doc.direction)
   6900             if (order) {
   6901                 var part = dir < 0 ? lst(order) : order[0]
   6902                 var moveInStorageOrder = (dir < 0) == (part.level == 1)
   6903                 var sticky = moveInStorageOrder ? "after" : "before"
   6904                 var ch
   6905                 // With a wrapped rtl chunk (possibly spanning multiple bidi parts),
   6906                 // it could be that the last bidi part is not on the last visual line,
   6907                 // since visual lines contain content order-consecutive chunks.
   6908                 // Thus, in rtl, we are looking for the first (content-order) character
   6909                 // in the rtl chunk that is on the last line (that is, the same line
   6910                 // as the last (content-order) character).
   6911                 if (part.level > 0 || cm.doc.direction == "rtl") {
   6912                     var prep = prepareMeasureForLine(cm, lineObj)
   6913                     ch = dir < 0 ? lineObj.text.length - 1 : 0
   6914                     var targetTop = measureCharPrepared(cm, prep, ch).top
   6915                     ch = findFirst(function (ch) { return measureCharPrepared(cm, prep, ch).top == targetTop; }, (dir < 0) == (part.level == 1) ? part.from : part.to - 1, ch)
   6916                     if (sticky == "before") { ch = moveCharLogically(lineObj, ch, 1) }
   6917                 } else { ch = dir < 0 ? part.to : part.from }
   6918                 return new Pos(lineNo, ch, sticky)
   6919             }
   6920         }
   6921         return new Pos(lineNo, dir < 0 ? lineObj.text.length : 0, dir < 0 ? "before" : "after")
   6922     }
   6923 
   6924     function moveVisually(cm, line, start, dir) {
   6925         var bidi = getOrder(line, cm.doc.direction)
   6926         if (!bidi) { return moveLogically(line, start, dir) }
   6927         if (start.ch >= line.text.length) {
   6928             start.ch = line.text.length
   6929             start.sticky = "before"
   6930         } else if (start.ch <= 0) {
   6931             start.ch = 0
   6932             start.sticky = "after"
   6933         }
   6934         var partPos = getBidiPartAt(bidi, start.ch, start.sticky), part = bidi[partPos]
   6935         if (cm.doc.direction == "ltr" && part.level % 2 == 0 && (dir > 0 ? part.to > start.ch : part.from < start.ch)) {
   6936             // Case 1: We move within an ltr part in an ltr editor. Even with wrapped lines,
   6937             // nothing interesting happens.
   6938             return moveLogically(line, start, dir)
   6939         }
   6940 
   6941         var mv = function (pos, dir) { return moveCharLogically(line, pos instanceof Pos ? pos.ch : pos, dir); }
   6942         var prep
   6943         var getWrappedLineExtent = function (ch) {
   6944             if (!cm.options.lineWrapping) { return {begin: 0, end: line.text.length} }
   6945             prep = prep || prepareMeasureForLine(cm, line)
   6946             return wrappedLineExtentChar(cm, line, prep, ch)
   6947         }
   6948         var wrappedLineExtent = getWrappedLineExtent(start.sticky == "before" ? mv(start, -1) : start.ch)
   6949 
   6950         if (cm.doc.direction == "rtl" || part.level == 1) {
   6951             var moveInStorageOrder = (part.level == 1) == (dir < 0)
   6952             var ch = mv(start, moveInStorageOrder ? 1 : -1)
   6953             if (ch != null && (!moveInStorageOrder ? ch >= part.from && ch >= wrappedLineExtent.begin : ch <= part.to && ch <= wrappedLineExtent.end)) {
   6954                 // Case 2: We move within an rtl part or in an rtl editor on the same visual line
   6955                 var sticky = moveInStorageOrder ? "before" : "after"
   6956                 return new Pos(start.line, ch, sticky)
   6957             }
   6958         }
   6959 
   6960         // Case 3: Could not move within this bidi part in this visual line, so leave
   6961         // the current bidi part
   6962 
   6963         var searchInVisualLine = function (partPos, dir, wrappedLineExtent) {
   6964             var getRes = function (ch, moveInStorageOrder) { return moveInStorageOrder
   6965                 ? new Pos(start.line, mv(ch, 1), "before")
   6966                 : new Pos(start.line, ch, "after"); }
   6967 
   6968             for (; partPos >= 0 && partPos < bidi.length; partPos += dir) {
   6969                 var part = bidi[partPos]
   6970                 var moveInStorageOrder = (dir > 0) == (part.level != 1)
   6971                 var ch = moveInStorageOrder ? wrappedLineExtent.begin : mv(wrappedLineExtent.end, -1)
   6972                 if (part.from <= ch && ch < part.to) { return getRes(ch, moveInStorageOrder) }
   6973                 ch = moveInStorageOrder ? part.from : mv(part.to, -1)
   6974                 if (wrappedLineExtent.begin <= ch && ch < wrappedLineExtent.end) { return getRes(ch, moveInStorageOrder) }
   6975             }
   6976         }
   6977 
   6978         // Case 3a: Look for other bidi parts on the same visual line
   6979         var res = searchInVisualLine(partPos + dir, dir, wrappedLineExtent)
   6980         if (res) { return res }
   6981 
   6982         // Case 3b: Look for other bidi parts on the next visual line
   6983         var nextCh = dir > 0 ? wrappedLineExtent.end : mv(wrappedLineExtent.begin, -1)
   6984         if (nextCh != null && !(dir > 0 && nextCh == line.text.length)) {
   6985             res = searchInVisualLine(dir > 0 ? 0 : bidi.length - 1, dir, getWrappedLineExtent(nextCh))
   6986             if (res) { return res }
   6987         }
   6988 
   6989         // Case 4: Nowhere to move
   6990         return null
   6991     }
   6992 
   6993 // Commands are parameter-less actions that can be performed on an
   6994 // editor, mostly used for keybindings.
   6995     var commands = {
   6996         selectAll: selectAll,
   6997         singleSelection: function (cm) { return cm.setSelection(cm.getCursor("anchor"), cm.getCursor("head"), sel_dontScroll); },
   6998         killLine: function (cm) { return deleteNearSelection(cm, function (range) {
   6999             if (range.empty()) {
   7000                 var len = getLine(cm.doc, range.head.line).text.length
   7001                 if (range.head.ch == len && range.head.line < cm.lastLine())
   7002                 { return {from: range.head, to: Pos(range.head.line + 1, 0)} }
   7003                 else
   7004                 { return {from: range.head, to: Pos(range.head.line, len)} }
   7005             } else {
   7006                 return {from: range.from(), to: range.to()}
   7007             }
   7008         }); },
   7009         deleteLine: function (cm) { return deleteNearSelection(cm, function (range) { return ({
   7010             from: Pos(range.from().line, 0),
   7011             to: clipPos(cm.doc, Pos(range.to().line + 1, 0))
   7012         }); }); },
   7013         delLineLeft: function (cm) { return deleteNearSelection(cm, function (range) { return ({
   7014             from: Pos(range.from().line, 0), to: range.from()
   7015         }); }); },
   7016         delWrappedLineLeft: function (cm) { return deleteNearSelection(cm, function (range) {
   7017             var top = cm.charCoords(range.head, "div").top + 5
   7018             var leftPos = cm.coordsChar({left: 0, top: top}, "div")
   7019             return {from: leftPos, to: range.from()}
   7020         }); },
   7021         delWrappedLineRight: function (cm) { return deleteNearSelection(cm, function (range) {
   7022             var top = cm.charCoords(range.head, "div").top + 5
   7023             var rightPos = cm.coordsChar({left: cm.display.lineDiv.offsetWidth + 100, top: top}, "div")
   7024             return {from: range.from(), to: rightPos }
   7025         }); },
   7026         undo: function (cm) { return cm.undo(); },
   7027         redo: function (cm) { return cm.redo(); },
   7028         undoSelection: function (cm) { return cm.undoSelection(); },
   7029         redoSelection: function (cm) { return cm.redoSelection(); },
   7030         goDocStart: function (cm) { return cm.extendSelection(Pos(cm.firstLine(), 0)); },
   7031         goDocEnd: function (cm) { return cm.extendSelection(Pos(cm.lastLine())); },
   7032         goLineStart: function (cm) { return cm.extendSelectionsBy(function (range) { return lineStart(cm, range.head.line); },
   7033             {origin: "+move", bias: 1}
   7034         ); },
   7035         goLineStartSmart: function (cm) { return cm.extendSelectionsBy(function (range) { return lineStartSmart(cm, range.head); },
   7036             {origin: "+move", bias: 1}
   7037         ); },
   7038         goLineEnd: function (cm) { return cm.extendSelectionsBy(function (range) { return lineEnd(cm, range.head.line); },
   7039             {origin: "+move", bias: -1}
   7040         ); },
   7041         goLineRight: function (cm) { return cm.extendSelectionsBy(function (range) {
   7042             var top = cm.cursorCoords(range.head, "div").top + 5
   7043             return cm.coordsChar({left: cm.display.lineDiv.offsetWidth + 100, top: top}, "div")
   7044         }, sel_move); },
   7045         goLineLeft: function (cm) { return cm.extendSelectionsBy(function (range) {
   7046             var top = cm.cursorCoords(range.head, "div").top + 5
   7047             return cm.coordsChar({left: 0, top: top}, "div")
   7048         }, sel_move); },
   7049         goLineLeftSmart: function (cm) { return cm.extendSelectionsBy(function (range) {
   7050             var top = cm.cursorCoords(range.head, "div").top + 5
   7051             var pos = cm.coordsChar({left: 0, top: top}, "div")
   7052             if (pos.ch < cm.getLine(pos.line).search(/\S/)) { return lineStartSmart(cm, range.head) }
   7053             return pos
   7054         }, sel_move); },
   7055         goLineUp: function (cm) { return cm.moveV(-1, "line"); },
   7056         goLineDown: function (cm) { return cm.moveV(1, "line"); },
   7057         goPageUp: function (cm) { return cm.moveV(-1, "page"); },
   7058         goPageDown: function (cm) { return cm.moveV(1, "page"); },
   7059         goCharLeft: function (cm) { return cm.moveH(-1, "char"); },
   7060         goCharRight: function (cm) { return cm.moveH(1, "char"); },
   7061         goColumnLeft: function (cm) { return cm.moveH(-1, "column"); },
   7062         goColumnRight: function (cm) { return cm.moveH(1, "column"); },
   7063         goWordLeft: function (cm) { return cm.moveH(-1, "word"); },
   7064         goGroupRight: function (cm) { return cm.moveH(1, "group"); },
   7065         goGroupLeft: function (cm) { return cm.moveH(-1, "group"); },
   7066         goWordRight: function (cm) { return cm.moveH(1, "word"); },
   7067         delCharBefore: function (cm) { return cm.deleteH(-1, "char"); },
   7068         delCharAfter: function (cm) { return cm.deleteH(1, "char"); },
   7069         delWordBefore: function (cm) { return cm.deleteH(-1, "word"); },
   7070         delWordAfter: function (cm) { return cm.deleteH(1, "word"); },
   7071         delGroupBefore: function (cm) { return cm.deleteH(-1, "group"); },
   7072         delGroupAfter: function (cm) { return cm.deleteH(1, "group"); },
   7073         indentAuto: function (cm) { return cm.indentSelection("smart"); },
   7074         indentMore: function (cm) { return cm.indentSelection("add"); },
   7075         indentLess: function (cm) { return cm.indentSelection("subtract"); },
   7076         insertTab: function (cm) { return cm.replaceSelection("\t"); },
   7077         insertSoftTab: function (cm) {
   7078             var spaces = [], ranges = cm.listSelections(), tabSize = cm.options.tabSize
   7079             for (var i = 0; i < ranges.length; i++) {
   7080                 var pos = ranges[i].from()
   7081                 var col = countColumn(cm.getLine(pos.line), pos.ch, tabSize)
   7082                 spaces.push(spaceStr(tabSize - col % tabSize))
   7083             }
   7084             cm.replaceSelections(spaces)
   7085         },
   7086         defaultTab: function (cm) {
   7087             if (cm.somethingSelected()) { cm.indentSelection("add") }
   7088             else { cm.execCommand("insertTab") }
   7089         },
   7090         // Swap the two chars left and right of each selection's head.
   7091         // Move cursor behind the two swapped characters afterwards.
   7092         //
   7093         // Doesn't consider line feeds a character.
   7094         // Doesn't scan more than one line above to find a character.
   7095         // Doesn't do anything on an empty line.
   7096         // Doesn't do anything with non-empty selections.
   7097         transposeChars: function (cm) { return runInOp(cm, function () {
   7098             var ranges = cm.listSelections(), newSel = []
   7099             for (var i = 0; i < ranges.length; i++) {
   7100                 if (!ranges[i].empty()) { continue }
   7101                 var cur = ranges[i].head, line = getLine(cm.doc, cur.line).text
   7102                 if (line) {
   7103                     if (cur.ch == line.length) { cur = new Pos(cur.line, cur.ch - 1) }
   7104                     if (cur.ch > 0) {
   7105                         cur = new Pos(cur.line, cur.ch + 1)
   7106                         cm.replaceRange(line.charAt(cur.ch - 1) + line.charAt(cur.ch - 2),
   7107                             Pos(cur.line, cur.ch - 2), cur, "+transpose")
   7108                     } else if (cur.line > cm.doc.first) {
   7109                         var prev = getLine(cm.doc, cur.line - 1).text
   7110                         if (prev) {
   7111                             cur = new Pos(cur.line, 1)
   7112                             cm.replaceRange(line.charAt(0) + cm.doc.lineSeparator() +
   7113                                 prev.charAt(prev.length - 1),
   7114                                 Pos(cur.line - 1, prev.length - 1), cur, "+transpose")
   7115                         }
   7116                     }
   7117                 }
   7118                 newSel.push(new Range(cur, cur))
   7119             }
   7120             cm.setSelections(newSel)
   7121         }); },
   7122         newlineAndIndent: function (cm) { return runInOp(cm, function () {
   7123             var sels = cm.listSelections()
   7124             for (var i = sels.length - 1; i >= 0; i--)
   7125             { cm.replaceRange(cm.doc.lineSeparator(), sels[i].anchor, sels[i].head, "+input") }
   7126             sels = cm.listSelections()
   7127             for (var i$1 = 0; i$1 < sels.length; i$1++)
   7128             { cm.indentLine(sels[i$1].from().line, null, true) }
   7129             ensureCursorVisible(cm)
   7130         }); },
   7131         openLine: function (cm) { return cm.replaceSelection("\n", "start"); },
   7132         toggleOverwrite: function (cm) { return cm.toggleOverwrite(); }
   7133     }
   7134 
   7135 
   7136     function lineStart(cm, lineN) {
   7137         var line = getLine(cm.doc, lineN)
   7138         var visual = visualLine(line)
   7139         if (visual != line) { lineN = lineNo(visual) }
   7140         return endOfLine(true, cm, visual, lineN, 1)
   7141     }
   7142     function lineEnd(cm, lineN) {
   7143         var line = getLine(cm.doc, lineN)
   7144         var visual = visualLineEnd(line)
   7145         if (visual != line) { lineN = lineNo(visual) }
   7146         return endOfLine(true, cm, line, lineN, -1)
   7147     }
   7148     function lineStartSmart(cm, pos) {
   7149         var start = lineStart(cm, pos.line)
   7150         var line = getLine(cm.doc, start.line)
   7151         var order = getOrder(line, cm.doc.direction)
   7152         if (!order || order[0].level == 0) {
   7153             var firstNonWS = Math.max(0, line.text.search(/\S/))
   7154             var inWS = pos.line == start.line && pos.ch <= firstNonWS && pos.ch
   7155             return Pos(start.line, inWS ? 0 : firstNonWS, start.sticky)
   7156         }
   7157         return start
   7158     }
   7159 
   7160 // Run a handler that was bound to a key.
   7161     function doHandleBinding(cm, bound, dropShift) {
   7162         if (typeof bound == "string") {
   7163             bound = commands[bound]
   7164             if (!bound) { return false }
   7165         }
   7166         // Ensure previous input has been read, so that the handler sees a
   7167         // consistent view of the document
   7168         cm.display.input.ensurePolled()
   7169         var prevShift = cm.display.shift, done = false
   7170         try {
   7171             if (cm.isReadOnly()) { cm.state.suppressEdits = true }
   7172             if (dropShift) { cm.display.shift = false }
   7173             done = bound(cm) != Pass
   7174         } finally {
   7175             cm.display.shift = prevShift
   7176             cm.state.suppressEdits = false
   7177         }
   7178         return done
   7179     }
   7180 
   7181     function lookupKeyForEditor(cm, name, handle) {
   7182         for (var i = 0; i < cm.state.keyMaps.length; i++) {
   7183             var result = lookupKey(name, cm.state.keyMaps[i], handle, cm)
   7184             if (result) { return result }
   7185         }
   7186         return (cm.options.extraKeys && lookupKey(name, cm.options.extraKeys, handle, cm))
   7187             || lookupKey(name, cm.options.keyMap, handle, cm)
   7188     }
   7189 
   7190 // Note that, despite the name, this function is also used to check
   7191 // for bound mouse clicks.
   7192 
   7193     var stopSeq = new Delayed
   7194 
   7195     function dispatchKey(cm, name, e, handle) {
   7196         var seq = cm.state.keySeq
   7197         if (seq) {
   7198             if (isModifierKey(name)) { return "handled" }
   7199             if (/\'$/.test(name))
   7200             { cm.state.keySeq = null }
   7201             else
   7202             { stopSeq.set(50, function () {
   7203                 if (cm.state.keySeq == seq) {
   7204                     cm.state.keySeq = null
   7205                     cm.display.input.reset()
   7206                 }
   7207             }) }
   7208             if (dispatchKeyInner(cm, seq + " " + name, e, handle)) { return true }
   7209         }
   7210         return dispatchKeyInner(cm, name, e, handle)
   7211     }
   7212 
   7213     function dispatchKeyInner(cm, name, e, handle) {
   7214         var result = lookupKeyForEditor(cm, name, handle)
   7215 
   7216         if (result == "multi")
   7217         { cm.state.keySeq = name }
   7218         if (result == "handled")
   7219         { signalLater(cm, "keyHandled", cm, name, e) }
   7220 
   7221         if (result == "handled" || result == "multi") {
   7222             e_preventDefault(e)
   7223             restartBlink(cm)
   7224         }
   7225 
   7226         return !!result
   7227     }
   7228 
   7229 // Handle a key from the keydown event.
   7230     function handleKeyBinding(cm, e) {
   7231         var name = keyName(e, true)
   7232         if (!name) { return false }
   7233 
   7234         if (e.shiftKey && !cm.state.keySeq) {
   7235             // First try to resolve full name (including 'Shift-'). Failing
   7236             // that, see if there is a cursor-motion command (starting with
   7237             // 'go') bound to the keyname without 'Shift-'.
   7238             return dispatchKey(cm, "Shift-" + name, e, function (b) { return doHandleBinding(cm, b, true); })
   7239                 || dispatchKey(cm, name, e, function (b) {
   7240                     if (typeof b == "string" ? /^go[A-Z]/.test(b) : b.motion)
   7241                     { return doHandleBinding(cm, b) }
   7242                 })
   7243         } else {
   7244             return dispatchKey(cm, name, e, function (b) { return doHandleBinding(cm, b); })
   7245         }
   7246     }
   7247 
   7248 // Handle a key from the keypress event
   7249     function handleCharBinding(cm, e, ch) {
   7250         return dispatchKey(cm, "'" + ch + "'", e, function (b) { return doHandleBinding(cm, b, true); })
   7251     }
   7252 
   7253     var lastStoppedKey = null
   7254     function onKeyDown(e) {
   7255         var cm = this
   7256         cm.curOp.focus = activeElt()
   7257         if (signalDOMEvent(cm, e)) { return }
   7258         // IE does strange things with escape.
   7259         if (ie && ie_version < 11 && e.keyCode == 27) { e.returnValue = false }
   7260         var code = e.keyCode
   7261         cm.display.shift = code == 16 || e.shiftKey
   7262         var handled = handleKeyBinding(cm, e)
   7263         if (presto) {
   7264             lastStoppedKey = handled ? code : null
   7265             // Opera has no cut event... we try to at least catch the key combo
   7266             if (!handled && code == 88 && !hasCopyEvent && (mac ? e.metaKey : e.ctrlKey))
   7267             { cm.replaceSelection("", null, "cut") }
   7268         }
   7269 
   7270         // Turn mouse into crosshair when Alt is held on Mac.
   7271         if (code == 18 && !/\bCodeMirror-crosshair\b/.test(cm.display.lineDiv.className))
   7272         { showCrossHair(cm) }
   7273     }
   7274 
   7275     function showCrossHair(cm) {
   7276         var lineDiv = cm.display.lineDiv
   7277         addClass(lineDiv, "CodeMirror-crosshair")
   7278 
   7279         function up(e) {
   7280             if (e.keyCode == 18 || !e.altKey) {
   7281                 rmClass(lineDiv, "CodeMirror-crosshair")
   7282                 off(document, "keyup", up)
   7283                 off(document, "mouseover", up)
   7284             }
   7285         }
   7286         on(document, "keyup", up)
   7287         on(document, "mouseover", up)
   7288     }
   7289 
   7290     function onKeyUp(e) {
   7291         if (e.keyCode == 16) { this.doc.sel.shift = false }
   7292         signalDOMEvent(this, e)
   7293     }
   7294 
   7295     function onKeyPress(e) {
   7296         var cm = this
   7297         if (eventInWidget(cm.display, e) || signalDOMEvent(cm, e) || e.ctrlKey && !e.altKey || mac && e.metaKey) { return }
   7298         var keyCode = e.keyCode, charCode = e.charCode
   7299         if (presto && keyCode == lastStoppedKey) {lastStoppedKey = null; e_preventDefault(e); return}
   7300         if ((presto && (!e.which || e.which < 10)) && handleKeyBinding(cm, e)) { return }
   7301         var ch = String.fromCharCode(charCode == null ? keyCode : charCode)
   7302         // Some browsers fire keypress events for backspace
   7303         if (ch == "\x08") { return }
   7304         if (handleCharBinding(cm, e, ch)) { return }
   7305         cm.display.input.onKeyPress(e)
   7306     }
   7307 
   7308     var DOUBLECLICK_DELAY = 400
   7309 
   7310     var PastClick = function(time, pos, button) {
   7311         this.time = time
   7312         this.pos = pos
   7313         this.button = button
   7314     };
   7315 
   7316     PastClick.prototype.compare = function (time, pos, button) {
   7317         return this.time + DOUBLECLICK_DELAY > time &&
   7318             cmp(pos, this.pos) == 0 && button == this.button
   7319     };
   7320 
   7321     var lastClick;
   7322     var lastDoubleClick;
   7323     function clickRepeat(pos, button) {
   7324         var now = +new Date
   7325         if (lastDoubleClick && lastDoubleClick.compare(now, pos, button)) {
   7326             lastClick = lastDoubleClick = null
   7327             return "triple"
   7328         } else if (lastClick && lastClick.compare(now, pos, button)) {
   7329             lastDoubleClick = new PastClick(now, pos, button)
   7330             lastClick = null
   7331             return "double"
   7332         } else {
   7333             lastClick = new PastClick(now, pos, button)
   7334             lastDoubleClick = null
   7335             return "single"
   7336         }
   7337     }
   7338 
   7339 // A mouse down can be a single click, double click, triple click,
   7340 // start of selection drag, start of text drag, new cursor
   7341 // (ctrl-click), rectangle drag (alt-drag), or xwin
   7342 // middle-click-paste. Or it might be a click on something we should
   7343 // not interfere with, such as a scrollbar or widget.
   7344     function onMouseDown(e) {
   7345         var cm = this, display = cm.display
   7346         if (signalDOMEvent(cm, e) || display.activeTouch && display.input.supportsTouch()) { return }
   7347         display.input.ensurePolled()
   7348         display.shift = e.shiftKey
   7349 
   7350         if (eventInWidget(display, e)) {
   7351             if (!webkit) {
   7352                 // Briefly turn off draggability, to allow widgets to do
   7353                 // normal dragging things.
   7354                 display.scroller.draggable = false
   7355                 setTimeout(function () { return display.scroller.draggable = true; }, 100)
   7356             }
   7357             return
   7358         }
   7359         if (clickInGutter(cm, e)) { return }
   7360         var pos = posFromMouse(cm, e), button = e_button(e), repeat = pos ? clickRepeat(pos, button) : "single"
   7361         window.focus()
   7362 
   7363         // #3261: make sure, that we're not starting a second selection
   7364         if (button == 1 && cm.state.selectingText)
   7365         { cm.state.selectingText(e) }
   7366 
   7367         if (pos && handleMappedButton(cm, button, pos, repeat, e)) { return }
   7368 
   7369         if (button == 1) {
   7370             if (pos) { leftButtonDown(cm, pos, repeat, e) }
   7371             else if (e_target(e) == display.scroller) { e_preventDefault(e) }
   7372         } else if (button == 2) {
   7373             if (pos) { extendSelection(cm.doc, pos) }
   7374             setTimeout(function () { return display.input.focus(); }, 20)
   7375         } else if (button == 3) {
   7376             if (captureRightClick) { onContextMenu(cm, e) }
   7377             else { delayBlurEvent(cm) }
   7378         }
   7379     }
   7380 
   7381     function handleMappedButton(cm, button, pos, repeat, event) {
   7382         var name = "Click"
   7383         if (repeat == "double") { name = "Double" + name }
   7384         else if (repeat == "triple") { name = "Triple" + name }
   7385         name = (button == 1 ? "Left" : button == 2 ? "Middle" : "Right") + name
   7386 
   7387         return dispatchKey(cm,  addModifierNames(name, event), event, function (bound) {
   7388             if (typeof bound == "string") { bound = commands[bound] }
   7389             if (!bound) { return false }
   7390             var done = false
   7391             try {
   7392                 if (cm.isReadOnly()) { cm.state.suppressEdits = true }
   7393                 done = bound(cm, pos) != Pass
   7394             } finally {
   7395                 cm.state.suppressEdits = false
   7396             }
   7397             return done
   7398         })
   7399     }
   7400 
   7401     function configureMouse(cm, repeat, event) {
   7402         var option = cm.getOption("configureMouse")
   7403         var value = option ? option(cm, repeat, event) : {}
   7404         if (value.unit == null) {
   7405             var rect = chromeOS ? event.shiftKey && event.metaKey : event.altKey
   7406             value.unit = rect ? "rectangle" : repeat == "single" ? "char" : repeat == "double" ? "word" : "line"
   7407         }
   7408         if (value.extend == null || cm.doc.extend) { value.extend = cm.doc.extend || event.shiftKey }
   7409         if (value.addNew == null) { value.addNew = mac ? event.metaKey : event.ctrlKey }
   7410         if (value.moveOnDrag == null) { value.moveOnDrag = !(mac ? event.altKey : event.ctrlKey) }
   7411         return value
   7412     }
   7413 
   7414     function leftButtonDown(cm, pos, repeat, event) {
   7415         if (ie) { setTimeout(bind(ensureFocus, cm), 0) }
   7416         else { cm.curOp.focus = activeElt() }
   7417 
   7418         var behavior = configureMouse(cm, repeat, event)
   7419 
   7420         var sel = cm.doc.sel, contained
   7421         if (cm.options.dragDrop && dragAndDrop && !cm.isReadOnly() &&
   7422             repeat == "single" && (contained = sel.contains(pos)) > -1 &&
   7423             (cmp((contained = sel.ranges[contained]).from(), pos) < 0 || pos.xRel > 0) &&
   7424             (cmp(contained.to(), pos) > 0 || pos.xRel < 0))
   7425         { leftButtonStartDrag(cm, event, pos, behavior) }
   7426         else
   7427         { leftButtonSelect(cm, event, pos, behavior) }
   7428     }
   7429 
   7430 // Start a text drag. When it ends, see if any dragging actually
   7431 // happen, and treat as a click if it didn't.
   7432     function leftButtonStartDrag(cm, event, pos, behavior) {
   7433         var display = cm.display, moved = false
   7434         var dragEnd = operation(cm, function (e) {
   7435             if (webkit) { display.scroller.draggable = false }
   7436             cm.state.draggingText = false
   7437             off(document, "mouseup", dragEnd)
   7438             off(document, "mousemove", mouseMove)
   7439             off(display.scroller, "dragstart", dragStart)
   7440             off(display.scroller, "drop", dragEnd)
   7441             if (!moved) {
   7442                 e_preventDefault(e)
   7443                 if (!behavior.addNew)
   7444                 { extendSelection(cm.doc, pos, null, null, behavior.extend) }
   7445                 // Work around unexplainable focus problem in IE9 (#2127) and Chrome (#3081)
   7446                 if (webkit || ie && ie_version == 9)
   7447                 { setTimeout(function () {document.body.focus(); display.input.focus()}, 20) }
   7448                 else
   7449                 { display.input.focus() }
   7450             }
   7451         })
   7452         var mouseMove = function(e2) {
   7453             moved = moved || Math.abs(event.clientX - e2.clientX) + Math.abs(event.clientY - e2.clientY) >= 10
   7454         }
   7455         var dragStart = function () { return moved = true; }
   7456         // Let the drag handler handle this.
   7457         if (webkit) { display.scroller.draggable = true }
   7458         cm.state.draggingText = dragEnd
   7459         dragEnd.copy = !behavior.moveOnDrag
   7460         // IE's approach to draggable
   7461         if (display.scroller.dragDrop) { display.scroller.dragDrop() }
   7462         on(document, "mouseup", dragEnd)
   7463         on(document, "mousemove", mouseMove)
   7464         on(display.scroller, "dragstart", dragStart)
   7465         on(display.scroller, "drop", dragEnd)
   7466 
   7467         delayBlurEvent(cm)
   7468         setTimeout(function () { return display.input.focus(); }, 20)
   7469     }
   7470 
   7471     function rangeForUnit(cm, pos, unit) {
   7472         if (unit == "char") { return new Range(pos, pos) }
   7473         if (unit == "word") { return cm.findWordAt(pos) }
   7474         if (unit == "line") { return new Range(Pos(pos.line, 0), clipPos(cm.doc, Pos(pos.line + 1, 0))) }
   7475         var result = unit(cm, pos)
   7476         return new Range(result.from, result.to)
   7477     }
   7478 
   7479 // Normal selection, as opposed to text dragging.
   7480     function leftButtonSelect(cm, event, start, behavior) {
   7481         var display = cm.display, doc = cm.doc
   7482         e_preventDefault(event)
   7483 
   7484         var ourRange, ourIndex, startSel = doc.sel, ranges = startSel.ranges
   7485         if (behavior.addNew && !behavior.extend) {
   7486             ourIndex = doc.sel.contains(start)
   7487             if (ourIndex > -1)
   7488             { ourRange = ranges[ourIndex] }
   7489             else
   7490             { ourRange = new Range(start, start) }
   7491         } else {
   7492             ourRange = doc.sel.primary()
   7493             ourIndex = doc.sel.primIndex
   7494         }
   7495 
   7496         if (behavior.unit == "rectangle") {
   7497             if (!behavior.addNew) { ourRange = new Range(start, start) }
   7498             start = posFromMouse(cm, event, true, true)
   7499             ourIndex = -1
   7500         } else {
   7501             var range = rangeForUnit(cm, start, behavior.unit)
   7502             if (behavior.extend)
   7503             { ourRange = extendRange(ourRange, range.anchor, range.head, behavior.extend) }
   7504             else
   7505             { ourRange = range }
   7506         }
   7507 
   7508         if (!behavior.addNew) {
   7509             ourIndex = 0
   7510             setSelection(doc, new Selection([ourRange], 0), sel_mouse)
   7511             startSel = doc.sel
   7512         } else if (ourIndex == -1) {
   7513             ourIndex = ranges.length
   7514             setSelection(doc, normalizeSelection(ranges.concat([ourRange]), ourIndex),
   7515                 {scroll: false, origin: "*mouse"})
   7516         } else if (ranges.length > 1 && ranges[ourIndex].empty() && behavior.unit == "char" && !behavior.extend) {
   7517             setSelection(doc, normalizeSelection(ranges.slice(0, ourIndex).concat(ranges.slice(ourIndex + 1)), 0),
   7518                 {scroll: false, origin: "*mouse"})
   7519             startSel = doc.sel
   7520         } else {
   7521             replaceOneSelection(doc, ourIndex, ourRange, sel_mouse)
   7522         }
   7523 
   7524         var lastPos = start
   7525         function extendTo(pos) {
   7526             if (cmp(lastPos, pos) == 0) { return }
   7527             lastPos = pos
   7528 
   7529             if (behavior.unit == "rectangle") {
   7530                 var ranges = [], tabSize = cm.options.tabSize
   7531                 var startCol = countColumn(getLine(doc, start.line).text, start.ch, tabSize)
   7532                 var posCol = countColumn(getLine(doc, pos.line).text, pos.ch, tabSize)
   7533                 var left = Math.min(startCol, posCol), right = Math.max(startCol, posCol)
   7534                 for (var line = Math.min(start.line, pos.line), end = Math.min(cm.lastLine(), Math.max(start.line, pos.line));
   7535                      line <= end; line++) {
   7536                     var text = getLine(doc, line).text, leftPos = findColumn(text, left, tabSize)
   7537                     if (left == right)
   7538                     { ranges.push(new Range(Pos(line, leftPos), Pos(line, leftPos))) }
   7539                     else if (text.length > leftPos)
   7540                     { ranges.push(new Range(Pos(line, leftPos), Pos(line, findColumn(text, right, tabSize)))) }
   7541                 }
   7542                 if (!ranges.length) { ranges.push(new Range(start, start)) }
   7543                 setSelection(doc, normalizeSelection(startSel.ranges.slice(0, ourIndex).concat(ranges), ourIndex),
   7544                     {origin: "*mouse", scroll: false})
   7545                 cm.scrollIntoView(pos)
   7546             } else {
   7547                 var oldRange = ourRange
   7548                 var range = rangeForUnit(cm, pos, behavior.unit)
   7549                 var anchor = oldRange.anchor, head
   7550                 if (cmp(range.anchor, anchor) > 0) {
   7551                     head = range.head
   7552                     anchor = minPos(oldRange.from(), range.anchor)
   7553                 } else {
   7554                     head = range.anchor
   7555                     anchor = maxPos(oldRange.to(), range.head)
   7556                 }
   7557                 var ranges$1 = startSel.ranges.slice(0)
   7558                 ranges$1[ourIndex] = bidiSimplify(cm, new Range(clipPos(doc, anchor), head))
   7559                 setSelection(doc, normalizeSelection(ranges$1, ourIndex), sel_mouse)
   7560             }
   7561         }
   7562 
   7563         var editorSize = display.wrapper.getBoundingClientRect()
   7564         // Used to ensure timeout re-tries don't fire when another extend
   7565         // happened in the meantime (clearTimeout isn't reliable -- at
   7566         // least on Chrome, the timeouts still happen even when cleared,
   7567         // if the clear happens after their scheduled firing time).
   7568         var counter = 0
   7569 
   7570         function extend(e) {
   7571             var curCount = ++counter
   7572             var cur = posFromMouse(cm, e, true, behavior.unit == "rectangle")
   7573             if (!cur) { return }
   7574             if (cmp(cur, lastPos) != 0) {
   7575                 cm.curOp.focus = activeElt()
   7576                 extendTo(cur)
   7577                 var visible = visibleLines(display, doc)
   7578                 if (cur.line >= visible.to || cur.line < visible.from)
   7579                 { setTimeout(operation(cm, function () {if (counter == curCount) { extend(e) }}), 150) }
   7580             } else {
   7581                 var outside = e.clientY < editorSize.top ? -20 : e.clientY > editorSize.bottom ? 20 : 0
   7582                 if (outside) { setTimeout(operation(cm, function () {
   7583                     if (counter != curCount) { return }
   7584                     display.scroller.scrollTop += outside
   7585                     extend(e)
   7586                 }), 50) }
   7587             }
   7588         }
   7589 
   7590         function done(e) {
   7591             cm.state.selectingText = false
   7592             counter = Infinity
   7593             e_preventDefault(e)
   7594             display.input.focus()
   7595             off(document, "mousemove", move)
   7596             off(document, "mouseup", up)
   7597             doc.history.lastSelOrigin = null
   7598         }
   7599 
   7600         var move = operation(cm, function (e) {
   7601             if (!e_button(e)) { done(e) }
   7602             else { extend(e) }
   7603         })
   7604         var up = operation(cm, done)
   7605         cm.state.selectingText = up
   7606         on(document, "mousemove", move)
   7607         on(document, "mouseup", up)
   7608     }
   7609 
   7610 // Used when mouse-selecting to adjust the anchor to the proper side
   7611 // of a bidi jump depending on the visual position of the head.
   7612     function bidiSimplify(cm, range) {
   7613         var anchor = range.anchor;
   7614         var head = range.head;
   7615         var anchorLine = getLine(cm.doc, anchor.line)
   7616         if (cmp(anchor, head) == 0 && anchor.sticky == head.sticky) { return range }
   7617         var order = getOrder(anchorLine)
   7618         if (!order) { return range }
   7619         var index = getBidiPartAt(order, anchor.ch, anchor.sticky), part = order[index]
   7620         if (part.from != anchor.ch && part.to != anchor.ch) { return range }
   7621         var boundary = index + ((part.from == anchor.ch) == (part.level != 1) ? 0 : 1)
   7622         if (boundary == 0 || boundary == order.length) { return range }
   7623 
   7624         // Compute the relative visual position of the head compared to the
   7625         // anchor (<0 is to the left, >0 to the right)
   7626         var leftSide
   7627         if (head.line != anchor.line) {
   7628             leftSide = (head.line - anchor.line) * (cm.doc.direction == "ltr" ? 1 : -1) > 0
   7629         } else {
   7630             var headIndex = getBidiPartAt(order, head.ch, head.sticky)
   7631             var dir = headIndex - index || (head.ch - anchor.ch) * (part.level == 1 ? -1 : 1)
   7632             if (headIndex == boundary - 1 || headIndex == boundary)
   7633             { leftSide = dir < 0 }
   7634             else
   7635             { leftSide = dir > 0 }
   7636         }
   7637 
   7638         var usePart = order[boundary + (leftSide ? -1 : 0)]
   7639         var from = leftSide == (usePart.level == 1)
   7640         var ch = from ? usePart.from : usePart.to, sticky = from ? "after" : "before"
   7641         return anchor.ch == ch && anchor.sticky == sticky ? range : new Range(new Pos(anchor.line, ch, sticky), head)
   7642     }
   7643 
   7644 
   7645 // Determines whether an event happened in the gutter, and fires the
   7646 // handlers for the corresponding event.
   7647     function gutterEvent(cm, e, type, prevent) {
   7648         var mX, mY
   7649         if (e.touches) {
   7650             mX = e.touches[0].clientX
   7651             mY = e.touches[0].clientY
   7652         } else {
   7653             try { mX = e.clientX; mY = e.clientY }
   7654             catch(e) { return false }
   7655         }
   7656         if (mX >= Math.floor(cm.display.gutters.getBoundingClientRect().right)) { return false }
   7657         if (prevent) { e_preventDefault(e) }
   7658 
   7659         var display = cm.display
   7660         var lineBox = display.lineDiv.getBoundingClientRect()
   7661 
   7662         if (mY > lineBox.bottom || !hasHandler(cm, type)) { return e_defaultPrevented(e) }
   7663         mY -= lineBox.top - display.viewOffset
   7664 
   7665         for (var i = 0; i < cm.options.gutters.length; ++i) {
   7666             var g = display.gutters.childNodes[i]
   7667             if (g && g.getBoundingClientRect().right >= mX) {
   7668                 var line = lineAtHeight(cm.doc, mY)
   7669                 var gutter = cm.options.gutters[i]
   7670                 signal(cm, type, cm, line, gutter, e)
   7671                 return e_defaultPrevented(e)
   7672             }
   7673         }
   7674     }
   7675 
   7676     function clickInGutter(cm, e) {
   7677         return gutterEvent(cm, e, "gutterClick", true)
   7678     }
   7679 
   7680 // CONTEXT MENU HANDLING
   7681 
   7682 // To make the context menu work, we need to briefly unhide the
   7683 // textarea (making it as unobtrusive as possible) to let the
   7684 // right-click take effect on it.
   7685     function onContextMenu(cm, e) {
   7686         if (eventInWidget(cm.display, e) || contextMenuInGutter(cm, e)) { return }
   7687         if (signalDOMEvent(cm, e, "contextmenu")) { return }
   7688         cm.display.input.onContextMenu(e)
   7689     }
   7690 
   7691     function contextMenuInGutter(cm, e) {
   7692         if (!hasHandler(cm, "gutterContextMenu")) { return false }
   7693         return gutterEvent(cm, e, "gutterContextMenu", false)
   7694     }
   7695 
   7696     function themeChanged(cm) {
   7697         cm.display.wrapper.className = cm.display.wrapper.className.replace(/\s*cm-s-\S+/g, "") +
   7698             cm.options.theme.replace(/(^|\s)\s*/g, " cm-s-")
   7699         clearCaches(cm)
   7700     }
   7701 
   7702     var Init = {toString: function(){return "CodeMirror.Init"}}
   7703 
   7704     var defaults = {}
   7705     var optionHandlers = {}
   7706 
   7707     function defineOptions(CodeMirror) {
   7708         var optionHandlers = CodeMirror.optionHandlers
   7709 
   7710         function option(name, deflt, handle, notOnInit) {
   7711             CodeMirror.defaults[name] = deflt
   7712             if (handle) { optionHandlers[name] =
   7713                 notOnInit ? function (cm, val, old) {if (old != Init) { handle(cm, val, old) }} : handle }
   7714         }
   7715 
   7716         CodeMirror.defineOption = option
   7717 
   7718         // Passed to option handlers when there is no old value.
   7719         CodeMirror.Init = Init
   7720 
   7721         // These two are, on init, called from the constructor because they
   7722         // have to be initialized before the editor can start at all.
   7723         option("value", "", function (cm, val) { return cm.setValue(val); }, true)
   7724         option("mode", null, function (cm, val) {
   7725             cm.doc.modeOption = val
   7726             loadMode(cm)
   7727         }, true)
   7728 
   7729         option("indentUnit", 2, loadMode, true)
   7730         option("indentWithTabs", false)
   7731         option("smartIndent", true)
   7732         option("tabSize", 4, function (cm) {
   7733             resetModeState(cm)
   7734             clearCaches(cm)
   7735             regChange(cm)
   7736         }, true)
   7737         option("lineSeparator", null, function (cm, val) {
   7738             cm.doc.lineSep = val
   7739             if (!val) { return }
   7740             var newBreaks = [], lineNo = cm.doc.first
   7741             cm.doc.iter(function (line) {
   7742                 for (var pos = 0;;) {
   7743                     var found = line.text.indexOf(val, pos)
   7744                     if (found == -1) { break }
   7745                     pos = found + val.length
   7746                     newBreaks.push(Pos(lineNo, found))
   7747                 }
   7748                 lineNo++
   7749             })
   7750             for (var i = newBreaks.length - 1; i >= 0; i--)
   7751             { replaceRange(cm.doc, val, newBreaks[i], Pos(newBreaks[i].line, newBreaks[i].ch + val.length)) }
   7752         })
   7753         option("specialChars", /[\u0000-\u001f\u007f-\u009f\u00ad\u061c\u200b-\u200f\u2028\u2029\ufeff]/g, function (cm, val, old) {
   7754             cm.state.specialChars = new RegExp(val.source + (val.test("\t") ? "" : "|\t"), "g")
   7755             if (old != Init) { cm.refresh() }
   7756         })
   7757         option("specialCharPlaceholder", defaultSpecialCharPlaceholder, function (cm) { return cm.refresh(); }, true)
   7758         option("electricChars", true)
   7759         option("inputStyle", mobile ? "contenteditable" : "textarea", function () {
   7760             throw new Error("inputStyle can not (yet) be changed in a running editor") // FIXME
   7761         }, true)
   7762         option("spellcheck", false, function (cm, val) { return cm.getInputField().spellcheck = val; }, true)
   7763         option("rtlMoveVisually", !windows)
   7764         option("wholeLineUpdateBefore", true)
   7765 
   7766         option("theme", "default", function (cm) {
   7767             themeChanged(cm)
   7768             guttersChanged(cm)
   7769         }, true)
   7770         option("keyMap", "default", function (cm, val, old) {
   7771             var next = getKeyMap(val)
   7772             var prev = old != Init && getKeyMap(old)
   7773             if (prev && prev.detach) { prev.detach(cm, next) }
   7774             if (next.attach) { next.attach(cm, prev || null) }
   7775         })
   7776         option("extraKeys", null)
   7777         option("configureMouse", null)
   7778 
   7779         option("lineWrapping", false, wrappingChanged, true)
   7780         option("gutters", [], function (cm) {
   7781             setGuttersForLineNumbers(cm.options)
   7782             guttersChanged(cm)
   7783         }, true)
   7784         option("fixedGutter", true, function (cm, val) {
   7785             cm.display.gutters.style.left = val ? compensateForHScroll(cm.display) + "px" : "0"
   7786             cm.refresh()
   7787         }, true)
   7788         option("coverGutterNextToScrollbar", false, function (cm) { return updateScrollbars(cm); }, true)
   7789         option("scrollbarStyle", "native", function (cm) {
   7790             initScrollbars(cm)
   7791             updateScrollbars(cm)
   7792             cm.display.scrollbars.setScrollTop(cm.doc.scrollTop)
   7793             cm.display.scrollbars.setScrollLeft(cm.doc.scrollLeft)
   7794         }, true)
   7795         option("lineNumbers", false, function (cm) {
   7796             setGuttersForLineNumbers(cm.options)
   7797             guttersChanged(cm)
   7798         }, true)
   7799         option("firstLineNumber", 1, guttersChanged, true)
   7800         option("lineNumberFormatter", function (integer) { return integer; }, guttersChanged, true)
   7801         option("showCursorWhenSelecting", false, updateSelection, true)
   7802 
   7803         option("resetSelectionOnContextMenu", true)
   7804         option("lineWiseCopyCut", true)
   7805         option("pasteLinesPerSelection", true)
   7806 
   7807         option("readOnly", false, function (cm, val) {
   7808             if (val == "nocursor") {
   7809                 onBlur(cm)
   7810                 cm.display.input.blur()
   7811             }
   7812             cm.display.input.readOnlyChanged(val)
   7813         })
   7814         option("disableInput", false, function (cm, val) {if (!val) { cm.display.input.reset() }}, true)
   7815         option("dragDrop", true, dragDropChanged)
   7816         option("allowDropFileTypes", null)
   7817 
   7818         option("cursorBlinkRate", 530)
   7819         option("cursorScrollMargin", 0)
   7820         option("cursorHeight", 1, updateSelection, true)
   7821         option("singleCursorHeightPerLine", true, updateSelection, true)
   7822         option("workTime", 100)
   7823         option("workDelay", 100)
   7824         option("flattenSpans", true, resetModeState, true)
   7825         option("addModeClass", false, resetModeState, true)
   7826         option("pollInterval", 100)
   7827         option("undoDepth", 200, function (cm, val) { return cm.doc.history.undoDepth = val; })
   7828         option("historyEventDelay", 1250)
   7829         option("viewportMargin", 10, function (cm) { return cm.refresh(); }, true)
   7830         option("maxHighlightLength", 10000, resetModeState, true)
   7831         option("moveInputWithCursor", true, function (cm, val) {
   7832             if (!val) { cm.display.input.resetPosition() }
   7833         })
   7834 
   7835         option("tabindex", null, function (cm, val) { return cm.display.input.getField().tabIndex = val || ""; })
   7836         option("autofocus", null)
   7837         option("direction", "ltr", function (cm, val) { return cm.doc.setDirection(val); }, true)
   7838     }
   7839 
   7840     function guttersChanged(cm) {
   7841         updateGutters(cm)
   7842         regChange(cm)
   7843         alignHorizontally(cm)
   7844     }
   7845 
   7846     function dragDropChanged(cm, value, old) {
   7847         var wasOn = old && old != Init
   7848         if (!value != !wasOn) {
   7849             var funcs = cm.display.dragFunctions
   7850             var toggle = value ? on : off
   7851             toggle(cm.display.scroller, "dragstart", funcs.start)
   7852             toggle(cm.display.scroller, "dragenter", funcs.enter)
   7853             toggle(cm.display.scroller, "dragover", funcs.over)
   7854             toggle(cm.display.scroller, "dragleave", funcs.leave)
   7855             toggle(cm.display.scroller, "drop", funcs.drop)
   7856         }
   7857     }
   7858 
   7859     function wrappingChanged(cm) {
   7860         if (cm.options.lineWrapping) {
   7861             addClass(cm.display.wrapper, "CodeMirror-wrap")
   7862             cm.display.sizer.style.minWidth = ""
   7863             cm.display.sizerWidth = null
   7864         } else {
   7865             rmClass(cm.display.wrapper, "CodeMirror-wrap")
   7866             findMaxLine(cm)
   7867         }
   7868         estimateLineHeights(cm)
   7869         regChange(cm)
   7870         clearCaches(cm)
   7871         setTimeout(function () { return updateScrollbars(cm); }, 100)
   7872     }
   7873 
   7874 // A CodeMirror instance represents an editor. This is the object
   7875 // that user code is usually dealing with.
   7876 
   7877     function CodeMirror(place, options) {
   7878         var this$1 = this;
   7879 
   7880         if (!(this instanceof CodeMirror)) { return new CodeMirror(place, options) }
   7881 
   7882         this.options = options = options ? copyObj(options) : {}
   7883         // Determine effective options based on given values and defaults.
   7884         copyObj(defaults, options, false)
   7885         setGuttersForLineNumbers(options)
   7886 
   7887         var doc = options.value
   7888         if (typeof doc == "string") { doc = new Doc(doc, options.mode, null, options.lineSeparator, options.direction) }
   7889         this.doc = doc
   7890 
   7891         var input = new CodeMirror.inputStyles[options.inputStyle](this)
   7892         var display = this.display = new Display(place, doc, input)
   7893         display.wrapper.CodeMirror = this
   7894         updateGutters(this)
   7895         themeChanged(this)
   7896         if (options.lineWrapping)
   7897         { this.display.wrapper.className += " CodeMirror-wrap" }
   7898         initScrollbars(this)
   7899 
   7900         this.state = {
   7901             keyMaps: [],  // stores maps added by addKeyMap
   7902             overlays: [], // highlighting overlays, as added by addOverlay
   7903             modeGen: 0,   // bumped when mode/overlay changes, used to invalidate highlighting info
   7904             overwrite: false,
   7905             delayingBlurEvent: false,
   7906             focused: false,
   7907             suppressEdits: false, // used to disable editing during key handlers when in readOnly mode
   7908             pasteIncoming: false, cutIncoming: false, // help recognize paste/cut edits in input.poll
   7909             selectingText: false,
   7910             draggingText: false,
   7911             highlight: new Delayed(), // stores highlight worker timeout
   7912             keySeq: null,  // Unfinished key sequence
   7913             specialChars: null
   7914         }
   7915 
   7916         if (options.autofocus && !mobile) { display.input.focus() }
   7917 
   7918         // Override magic textarea content restore that IE sometimes does
   7919         // on our hidden textarea on reload
   7920         if (ie && ie_version < 11) { setTimeout(function () { return this$1.display.input.reset(true); }, 20) }
   7921 
   7922         registerEventHandlers(this)
   7923         ensureGlobalHandlers()
   7924 
   7925         startOperation(this)
   7926         this.curOp.forceUpdate = true
   7927         attachDoc(this, doc)
   7928 
   7929         if ((options.autofocus && !mobile) || this.hasFocus())
   7930         { setTimeout(bind(onFocus, this), 20) }
   7931         else
   7932         { onBlur(this) }
   7933 
   7934         for (var opt in optionHandlers) { if (optionHandlers.hasOwnProperty(opt))
   7935         { optionHandlers[opt](this$1, options[opt], Init) } }
   7936         maybeUpdateLineNumberWidth(this)
   7937         if (options.finishInit) { options.finishInit(this) }
   7938         for (var i = 0; i < initHooks.length; ++i) { initHooks[i](this$1) }
   7939         endOperation(this)
   7940         // Suppress optimizelegibility in Webkit, since it breaks text
   7941         // measuring on line wrapping boundaries.
   7942         if (webkit && options.lineWrapping &&
   7943             getComputedStyle(display.lineDiv).textRendering == "optimizelegibility")
   7944         { display.lineDiv.style.textRendering = "auto" }
   7945     }
   7946 
   7947 // The default configuration options.
   7948     CodeMirror.defaults = defaults
   7949 // Functions to run when options are changed.
   7950     CodeMirror.optionHandlers = optionHandlers
   7951 
   7952 // Attach the necessary event handlers when initializing the editor
   7953     function registerEventHandlers(cm) {
   7954         var d = cm.display
   7955         on(d.scroller, "mousedown", operation(cm, onMouseDown))
   7956         // Older IE's will not fire a second mousedown for a double click
   7957         if (ie && ie_version < 11)
   7958         { on(d.scroller, "dblclick", operation(cm, function (e) {
   7959             if (signalDOMEvent(cm, e)) { return }
   7960             var pos = posFromMouse(cm, e)
   7961             if (!pos || clickInGutter(cm, e) || eventInWidget(cm.display, e)) { return }
   7962             e_preventDefault(e)
   7963             var word = cm.findWordAt(pos)
   7964             extendSelection(cm.doc, word.anchor, word.head)
   7965         })) }
   7966         else
   7967         { on(d.scroller, "dblclick", function (e) { return signalDOMEvent(cm, e) || e_preventDefault(e); }) }
   7968         // Some browsers fire contextmenu *after* opening the menu, at
   7969         // which point we can't mess with it anymore. Context menu is
   7970         // handled in onMouseDown for these browsers.
   7971         if (!captureRightClick) { on(d.scroller, "contextmenu", function (e) { return onContextMenu(cm, e); }) }
   7972 
   7973         // Used to suppress mouse event handling when a touch happens
   7974         var touchFinished, prevTouch = {end: 0}
   7975         function finishTouch() {
   7976             if (d.activeTouch) {
   7977                 touchFinished = setTimeout(function () { return d.activeTouch = null; }, 1000)
   7978                 prevTouch = d.activeTouch
   7979                 prevTouch.end = +new Date
   7980             }
   7981         }
   7982         function isMouseLikeTouchEvent(e) {
   7983             if (e.touches.length != 1) { return false }
   7984             var touch = e.touches[0]
   7985             return touch.radiusX <= 1 && touch.radiusY <= 1
   7986         }
   7987         function farAway(touch, other) {
   7988             if (other.left == null) { return true }
   7989             var dx = other.left - touch.left, dy = other.top - touch.top
   7990             return dx * dx + dy * dy > 20 * 20
   7991         }
   7992         on(d.scroller, "touchstart", function (e) {
   7993             if (!signalDOMEvent(cm, e) && !isMouseLikeTouchEvent(e) && !clickInGutter(cm, e)) {
   7994                 d.input.ensurePolled()
   7995                 clearTimeout(touchFinished)
   7996                 var now = +new Date
   7997                 d.activeTouch = {start: now, moved: false,
   7998                     prev: now - prevTouch.end <= 300 ? prevTouch : null}
   7999                 if (e.touches.length == 1) {
   8000                     d.activeTouch.left = e.touches[0].pageX
   8001                     d.activeTouch.top = e.touches[0].pageY
   8002                 }
   8003             }
   8004         })
   8005         on(d.scroller, "touchmove", function () {
   8006             if (d.activeTouch) { d.activeTouch.moved = true }
   8007         })
   8008         on(d.scroller, "touchend", function (e) {
   8009             var touch = d.activeTouch
   8010             if (touch && !eventInWidget(d, e) && touch.left != null &&
   8011                 !touch.moved && new Date - touch.start < 300) {
   8012                 var pos = cm.coordsChar(d.activeTouch, "page"), range
   8013                 if (!touch.prev || farAway(touch, touch.prev)) // Single tap
   8014                 { range = new Range(pos, pos) }
   8015                 else if (!touch.prev.prev || farAway(touch, touch.prev.prev)) // Double tap
   8016                 { range = cm.findWordAt(pos) }
   8017                 else // Triple tap
   8018                 { range = new Range(Pos(pos.line, 0), clipPos(cm.doc, Pos(pos.line + 1, 0))) }
   8019                 cm.setSelection(range.anchor, range.head)
   8020                 cm.focus()
   8021                 e_preventDefault(e)
   8022             }
   8023             finishTouch()
   8024         })
   8025         on(d.scroller, "touchcancel", finishTouch)
   8026 
   8027         // Sync scrolling between fake scrollbars and real scrollable
   8028         // area, ensure viewport is updated when scrolling.
   8029         on(d.scroller, "scroll", function () {
   8030             if (d.scroller.clientHeight) {
   8031                 updateScrollTop(cm, d.scroller.scrollTop)
   8032                 setScrollLeft(cm, d.scroller.scrollLeft, true)
   8033                 signal(cm, "scroll", cm)
   8034             }
   8035         })
   8036 
   8037         // Listen to wheel events in order to try and update the viewport on time.
   8038         on(d.scroller, "mousewheel", function (e) { return onScrollWheel(cm, e); })
   8039         on(d.scroller, "DOMMouseScroll", function (e) { return onScrollWheel(cm, e); })
   8040 
   8041         // Prevent wrapper from ever scrolling
   8042         on(d.wrapper, "scroll", function () { return d.wrapper.scrollTop = d.wrapper.scrollLeft = 0; })
   8043 
   8044         d.dragFunctions = {
   8045             enter: function (e) {if (!signalDOMEvent(cm, e)) { e_stop(e) }},
   8046             over: function (e) {if (!signalDOMEvent(cm, e)) { onDragOver(cm, e); e_stop(e) }},
   8047             start: function (e) { return onDragStart(cm, e); },
   8048             drop: operation(cm, onDrop),
   8049             leave: function (e) {if (!signalDOMEvent(cm, e)) { clearDragCursor(cm) }}
   8050         }
   8051 
   8052         var inp = d.input.getField()
   8053         on(inp, "keyup", function (e) { return onKeyUp.call(cm, e); })
   8054         on(inp, "keydown", operation(cm, onKeyDown))
   8055         on(inp, "keypress", operation(cm, onKeyPress))
   8056         on(inp, "focus", function (e) { return onFocus(cm, e); })
   8057         on(inp, "blur", function (e) { return onBlur(cm, e); })
   8058     }
   8059 
   8060     var initHooks = []
   8061     CodeMirror.defineInitHook = function (f) { return initHooks.push(f); }
   8062 
   8063 // Indent the given line. The how parameter can be "smart",
   8064 // "add"/null, "subtract", or "prev". When aggressive is false
   8065 // (typically set to true for forced single-line indents), empty
   8066 // lines are not indented, and places where the mode returns Pass
   8067 // are left alone.
   8068     function indentLine(cm, n, how, aggressive) {
   8069         var doc = cm.doc, state
   8070         if (how == null) { how = "add" }
   8071         if (how == "smart") {
   8072             // Fall back to "prev" when the mode doesn't have an indentation
   8073             // method.
   8074             if (!doc.mode.indent) { how = "prev" }
   8075             else { state = getContextBefore(cm, n).state }
   8076         }
   8077 
   8078         var tabSize = cm.options.tabSize
   8079         var line = getLine(doc, n), curSpace = countColumn(line.text, null, tabSize)
   8080         if (line.stateAfter) { line.stateAfter = null }
   8081         var curSpaceString = line.text.match(/^\s*/)[0], indentation
   8082         if (!aggressive && !/\S/.test(line.text)) {
   8083             indentation = 0
   8084             how = "not"
   8085         } else if (how == "smart") {
   8086             indentation = doc.mode.indent(state, line.text.slice(curSpaceString.length), line.text)
   8087             if (indentation == Pass || indentation > 150) {
   8088                 if (!aggressive) { return }
   8089                 how = "prev"
   8090             }
   8091         }
   8092         if (how == "prev") {
   8093             if (n > doc.first) { indentation = countColumn(getLine(doc, n-1).text, null, tabSize) }
   8094             else { indentation = 0 }
   8095         } else if (how == "add") {
   8096             indentation = curSpace + cm.options.indentUnit
   8097         } else if (how == "subtract") {
   8098             indentation = curSpace - cm.options.indentUnit
   8099         } else if (typeof how == "number") {
   8100             indentation = curSpace + how
   8101         }
   8102         indentation = Math.max(0, indentation)
   8103 
   8104         var indentString = "", pos = 0
   8105         if (cm.options.indentWithTabs)
   8106         { for (var i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t"} }
   8107         if (pos < indentation) { indentString += spaceStr(indentation - pos) }
   8108 
   8109         if (indentString != curSpaceString) {
   8110             replaceRange(doc, indentString, Pos(n, 0), Pos(n, curSpaceString.length), "+input")
   8111             line.stateAfter = null
   8112             return true
   8113         } else {
   8114             // Ensure that, if the cursor was in the whitespace at the start
   8115             // of the line, it is moved to the end of that space.
   8116             for (var i$1 = 0; i$1 < doc.sel.ranges.length; i$1++) {
   8117                 var range = doc.sel.ranges[i$1]
   8118                 if (range.head.line == n && range.head.ch < curSpaceString.length) {
   8119                     var pos$1 = Pos(n, curSpaceString.length)
   8120                     replaceOneSelection(doc, i$1, new Range(pos$1, pos$1))
   8121                     break
   8122                 }
   8123             }
   8124         }
   8125     }
   8126 
   8127 // This will be set to a {lineWise: bool, text: [string]} object, so
   8128 // that, when pasting, we know what kind of selections the copied
   8129 // text was made out of.
   8130     var lastCopied = null
   8131 
   8132     function setLastCopied(newLastCopied) {
   8133         lastCopied = newLastCopied
   8134     }
   8135 
   8136     function applyTextInput(cm, inserted, deleted, sel, origin) {
   8137         var doc = cm.doc
   8138         cm.display.shift = false
   8139         if (!sel) { sel = doc.sel }
   8140 
   8141         var paste = cm.state.pasteIncoming || origin == "paste"
   8142         var textLines = splitLinesAuto(inserted), multiPaste = null
   8143         // When pasing N lines into N selections, insert one line per selection
   8144         if (paste && sel.ranges.length > 1) {
   8145             if (lastCopied && lastCopied.text.join("\n") == inserted) {
   8146                 if (sel.ranges.length % lastCopied.text.length == 0) {
   8147                     multiPaste = []
   8148                     for (var i = 0; i < lastCopied.text.length; i++)
   8149                     { multiPaste.push(doc.splitLines(lastCopied.text[i])) }
   8150                 }
   8151             } else if (textLines.length == sel.ranges.length && cm.options.pasteLinesPerSelection) {
   8152                 multiPaste = map(textLines, function (l) { return [l]; })
   8153             }
   8154         }
   8155 
   8156         var updateInput
   8157         // Normal behavior is to insert the new text into every selection
   8158         for (var i$1 = sel.ranges.length - 1; i$1 >= 0; i$1--) {
   8159             var range = sel.ranges[i$1]
   8160             var from = range.from(), to = range.to()
   8161             if (range.empty()) {
   8162                 if (deleted && deleted > 0) // Handle deletion
   8163                 { from = Pos(from.line, from.ch - deleted) }
   8164                 else if (cm.state.overwrite && !paste) // Handle overwrite
   8165                 { to = Pos(to.line, Math.min(getLine(doc, to.line).text.length, to.ch + lst(textLines).length)) }
   8166                 else if (lastCopied && lastCopied.lineWise && lastCopied.text.join("\n") == inserted)
   8167                 { from = to = Pos(from.line, 0) }
   8168             }
   8169             updateInput = cm.curOp.updateInput
   8170             var changeEvent = {from: from, to: to, text: multiPaste ? multiPaste[i$1 % multiPaste.length] : textLines,
   8171                 origin: origin || (paste ? "paste" : cm.state.cutIncoming ? "cut" : "+input")}
   8172             makeChange(cm.doc, changeEvent)
   8173             signalLater(cm, "inputRead", cm, changeEvent)
   8174         }
   8175         if (inserted && !paste)
   8176         { triggerElectric(cm, inserted) }
   8177 
   8178         ensureCursorVisible(cm)
   8179         cm.curOp.updateInput = updateInput
   8180         cm.curOp.typing = true
   8181         cm.state.pasteIncoming = cm.state.cutIncoming = false
   8182     }
   8183 
   8184     function handlePaste(e, cm) {
   8185         var pasted = e.clipboardData && e.clipboardData.getData("Text")
   8186         if (pasted) {
   8187             e.preventDefault()
   8188             if (!cm.isReadOnly() && !cm.options.disableInput)
   8189             { runInOp(cm, function () { return applyTextInput(cm, pasted, 0, null, "paste"); }) }
   8190             return true
   8191         }
   8192     }
   8193 
   8194     function triggerElectric(cm, inserted) {
   8195         // When an 'electric' character is inserted, immediately trigger a reindent
   8196         if (!cm.options.electricChars || !cm.options.smartIndent) { return }
   8197         var sel = cm.doc.sel
   8198 
   8199         for (var i = sel.ranges.length - 1; i >= 0; i--) {
   8200             var range = sel.ranges[i]
   8201             if (range.head.ch > 100 || (i && sel.ranges[i - 1].head.line == range.head.line)) { continue }
   8202             var mode = cm.getModeAt(range.head)
   8203             var indented = false
   8204             if (mode.electricChars) {
   8205                 for (var j = 0; j < mode.electricChars.length; j++)
   8206                 { if (inserted.indexOf(mode.electricChars.charAt(j)) > -1) {
   8207                     indented = indentLine(cm, range.head.line, "smart")
   8208                     break
   8209                 } }
   8210             } else if (mode.electricInput) {
   8211                 if (mode.electricInput.test(getLine(cm.doc, range.head.line).text.slice(0, range.head.ch)))
   8212                 { indented = indentLine(cm, range.head.line, "smart") }
   8213             }
   8214             if (indented) { signalLater(cm, "electricInput", cm, range.head.line) }
   8215         }
   8216     }
   8217 
   8218     function copyableRanges(cm) {
   8219         var text = [], ranges = []
   8220         for (var i = 0; i < cm.doc.sel.ranges.length; i++) {
   8221             var line = cm.doc.sel.ranges[i].head.line
   8222             var lineRange = {anchor: Pos(line, 0), head: Pos(line + 1, 0)}
   8223             ranges.push(lineRange)
   8224             text.push(cm.getRange(lineRange.anchor, lineRange.head))
   8225         }
   8226         return {text: text, ranges: ranges}
   8227     }
   8228 
   8229     function disableBrowserMagic(field, spellcheck) {
   8230         field.setAttribute("autocorrect", "off")
   8231         field.setAttribute("autocapitalize", "off")
   8232         field.setAttribute("spellcheck", !!spellcheck)
   8233     }
   8234 
   8235     function hiddenTextarea() {
   8236         var te = elt("textarea", null, null, "position: absolute; bottom: -1em; padding: 0; width: 1px; height: 1em; outline: none")
   8237         var div = elt("div", [te], null, "overflow: hidden; position: relative; width: 3px; height: 0px;")
   8238         // The textarea is kept positioned near the cursor to prevent the
   8239         // fact that it'll be scrolled into view on input from scrolling
   8240         // our fake cursor out of view. On webkit, when wrap=off, paste is
   8241         // very slow. So make the area wide instead.
   8242         if (webkit) { te.style.width = "1000px" }
   8243         else { te.setAttribute("wrap", "off") }
   8244         // If border: 0; -- iOS fails to open keyboard (issue #1287)
   8245         if (ios) { te.style.border = "1px solid black" }
   8246         disableBrowserMagic(te)
   8247         return div
   8248     }
   8249 
   8250 // The publicly visible API. Note that methodOp(f) means
   8251 // 'wrap f in an operation, performed on its `this` parameter'.
   8252 
   8253 // This is not the complete set of editor methods. Most of the
   8254 // methods defined on the Doc type are also injected into
   8255 // CodeMirror.prototype, for backwards compatibility and
   8256 // convenience.
   8257 
   8258     function addEditorMethods(CodeMirror) {
   8259         var optionHandlers = CodeMirror.optionHandlers
   8260 
   8261         var helpers = CodeMirror.helpers = {}
   8262 
   8263         CodeMirror.prototype = {
   8264             constructor: CodeMirror,
   8265             focus: function(){window.focus(); this.display.input.focus()},
   8266 
   8267             setOption: function(option, value) {
   8268                 var options = this.options, old = options[option]
   8269                 if (options[option] == value && option != "mode") { return }
   8270                 options[option] = value
   8271                 if (optionHandlers.hasOwnProperty(option))
   8272                 { operation(this, optionHandlers[option])(this, value, old) }
   8273                 signal(this, "optionChange", this, option)
   8274             },
   8275 
   8276             getOption: function(option) {return this.options[option]},
   8277             getDoc: function() {return this.doc},
   8278 
   8279             addKeyMap: function(map, bottom) {
   8280                 this.state.keyMaps[bottom ? "push" : "unshift"](getKeyMap(map))
   8281             },
   8282             removeKeyMap: function(map) {
   8283                 var maps = this.state.keyMaps
   8284                 for (var i = 0; i < maps.length; ++i)
   8285                 { if (maps[i] == map || maps[i].name == map) {
   8286                     maps.splice(i, 1)
   8287                     return true
   8288                 } }
   8289             },
   8290 
   8291             addOverlay: methodOp(function(spec, options) {
   8292                 var mode = spec.token ? spec : CodeMirror.getMode(this.options, spec)
   8293                 if (mode.startState) { throw new Error("Overlays may not be stateful.") }
   8294                 insertSorted(this.state.overlays,
   8295                     {mode: mode, modeSpec: spec, opaque: options && options.opaque,
   8296                         priority: (options && options.priority) || 0},
   8297                     function (overlay) { return overlay.priority; })
   8298                 this.state.modeGen++
   8299                 regChange(this)
   8300             }),
   8301             removeOverlay: methodOp(function(spec) {
   8302                 var this$1 = this;
   8303 
   8304                 var overlays = this.state.overlays
   8305                 for (var i = 0; i < overlays.length; ++i) {
   8306                     var cur = overlays[i].modeSpec
   8307                     if (cur == spec || typeof spec == "string" && cur.name == spec) {
   8308                         overlays.splice(i, 1)
   8309                         this$1.state.modeGen++
   8310                         regChange(this$1)
   8311                         return
   8312                     }
   8313                 }
   8314             }),
   8315 
   8316             indentLine: methodOp(function(n, dir, aggressive) {
   8317                 if (typeof dir != "string" && typeof dir != "number") {
   8318                     if (dir == null) { dir = this.options.smartIndent ? "smart" : "prev" }
   8319                     else { dir = dir ? "add" : "subtract" }
   8320                 }
   8321                 if (isLine(this.doc, n)) { indentLine(this, n, dir, aggressive) }
   8322             }),
   8323             indentSelection: methodOp(function(how) {
   8324                 var this$1 = this;
   8325 
   8326                 var ranges = this.doc.sel.ranges, end = -1
   8327                 for (var i = 0; i < ranges.length; i++) {
   8328                     var range = ranges[i]
   8329                     if (!range.empty()) {
   8330                         var from = range.from(), to = range.to()
   8331                         var start = Math.max(end, from.line)
   8332                         end = Math.min(this$1.lastLine(), to.line - (to.ch ? 0 : 1)) + 1
   8333                         for (var j = start; j < end; ++j)
   8334                         { indentLine(this$1, j, how) }
   8335                         var newRanges = this$1.doc.sel.ranges
   8336                         if (from.ch == 0 && ranges.length == newRanges.length && newRanges[i].from().ch > 0)
   8337                         { replaceOneSelection(this$1.doc, i, new Range(from, newRanges[i].to()), sel_dontScroll) }
   8338                     } else if (range.head.line > end) {
   8339                         indentLine(this$1, range.head.line, how, true)
   8340                         end = range.head.line
   8341                         if (i == this$1.doc.sel.primIndex) { ensureCursorVisible(this$1) }
   8342                     }
   8343                 }
   8344             }),
   8345 
   8346             // Fetch the parser token for a given character. Useful for hacks
   8347             // that want to inspect the mode state (say, for completion).
   8348             getTokenAt: function(pos, precise) {
   8349                 return takeToken(this, pos, precise)
   8350             },
   8351 
   8352             getLineTokens: function(line, precise) {
   8353                 return takeToken(this, Pos(line), precise, true)
   8354             },
   8355 
   8356             getTokenTypeAt: function(pos) {
   8357                 pos = clipPos(this.doc, pos)
   8358                 var styles = getLineStyles(this, getLine(this.doc, pos.line))
   8359                 var before = 0, after = (styles.length - 1) / 2, ch = pos.ch
   8360                 var type
   8361                 if (ch == 0) { type = styles[2] }
   8362                 else { for (;;) {
   8363                     var mid = (before + after) >> 1
   8364                     if ((mid ? styles[mid * 2 - 1] : 0) >= ch) { after = mid }
   8365                     else if (styles[mid * 2 + 1] < ch) { before = mid + 1 }
   8366                     else { type = styles[mid * 2 + 2]; break }
   8367                 } }
   8368                 var cut = type ? type.indexOf("overlay ") : -1
   8369                 return cut < 0 ? type : cut == 0 ? null : type.slice(0, cut - 1)
   8370             },
   8371 
   8372             getModeAt: function(pos) {
   8373                 var mode = this.doc.mode
   8374                 if (!mode.innerMode) { return mode }
   8375                 return CodeMirror.innerMode(mode, this.getTokenAt(pos).state).mode
   8376             },
   8377 
   8378             getHelper: function(pos, type) {
   8379                 return this.getHelpers(pos, type)[0]
   8380             },
   8381 
   8382             getHelpers: function(pos, type) {
   8383                 var this$1 = this;
   8384 
   8385                 var found = []
   8386                 if (!helpers.hasOwnProperty(type)) { return found }
   8387                 var help = helpers[type], mode = this.getModeAt(pos)
   8388                 if (typeof mode[type] == "string") {
   8389                     if (help[mode[type]]) { found.push(help[mode[type]]) }
   8390                 } else if (mode[type]) {
   8391                     for (var i = 0; i < mode[type].length; i++) {
   8392                         var val = help[mode[type][i]]
   8393                         if (val) { found.push(val) }
   8394                     }
   8395                 } else if (mode.helperType && help[mode.helperType]) {
   8396                     found.push(help[mode.helperType])
   8397                 } else if (help[mode.name]) {
   8398                     found.push(help[mode.name])
   8399                 }
   8400                 for (var i$1 = 0; i$1 < help._global.length; i$1++) {
   8401                     var cur = help._global[i$1]
   8402                     if (cur.pred(mode, this$1) && indexOf(found, cur.val) == -1)
   8403                     { found.push(cur.val) }
   8404                 }
   8405                 return found
   8406             },
   8407 
   8408             getStateAfter: function(line, precise) {
   8409                 var doc = this.doc
   8410                 line = clipLine(doc, line == null ? doc.first + doc.size - 1: line)
   8411                 return getContextBefore(this, line + 1, precise).state
   8412             },
   8413 
   8414             cursorCoords: function(start, mode) {
   8415                 var pos, range = this.doc.sel.primary()
   8416                 if (start == null) { pos = range.head }
   8417                 else if (typeof start == "object") { pos = clipPos(this.doc, start) }
   8418                 else { pos = start ? range.from() : range.to() }
   8419                 return cursorCoords(this, pos, mode || "page")
   8420             },
   8421 
   8422             charCoords: function(pos, mode) {
   8423                 return charCoords(this, clipPos(this.doc, pos), mode || "page")
   8424             },
   8425 
   8426             coordsChar: function(coords, mode) {
   8427                 coords = fromCoordSystem(this, coords, mode || "page")
   8428                 return coordsChar(this, coords.left, coords.top)
   8429             },
   8430 
   8431             lineAtHeight: function(height, mode) {
   8432                 height = fromCoordSystem(this, {top: height, left: 0}, mode || "page").top
   8433                 return lineAtHeight(this.doc, height + this.display.viewOffset)
   8434             },
   8435             heightAtLine: function(line, mode, includeWidgets) {
   8436                 var end = false, lineObj
   8437                 if (typeof line == "number") {
   8438                     var last = this.doc.first + this.doc.size - 1
   8439                     if (line < this.doc.first) { line = this.doc.first }
   8440                     else if (line > last) { line = last; end = true }
   8441                     lineObj = getLine(this.doc, line)
   8442                 } else {
   8443                     lineObj = line
   8444                 }
   8445                 return intoCoordSystem(this, lineObj, {top: 0, left: 0}, mode || "page", includeWidgets || end).top +
   8446                     (end ? this.doc.height - heightAtLine(lineObj) : 0)
   8447             },
   8448 
   8449             defaultTextHeight: function() { return textHeight(this.display) },
   8450             defaultCharWidth: function() { return charWidth(this.display) },
   8451 
   8452             getViewport: function() { return {from: this.display.viewFrom, to: this.display.viewTo}},
   8453 
   8454             addWidget: function(pos, node, scroll, vert, horiz) {
   8455                 var display = this.display
   8456                 pos = cursorCoords(this, clipPos(this.doc, pos))
   8457                 var top = pos.bottom, left = pos.left
   8458                 node.style.position = "absolute"
   8459                 node.setAttribute("cm-ignore-events", "true")
   8460                 this.display.input.setUneditable(node)
   8461                 display.sizer.appendChild(node)
   8462                 if (vert == "over") {
   8463                     top = pos.top
   8464                 } else if (vert == "above" || vert == "near") {
   8465                     var vspace = Math.max(display.wrapper.clientHeight, this.doc.height),
   8466                         hspace = Math.max(display.sizer.clientWidth, display.lineSpace.clientWidth)
   8467                     // Default to positioning above (if specified and possible); otherwise default to positioning below
   8468                     if ((vert == 'above' || pos.bottom + node.offsetHeight > vspace) && pos.top > node.offsetHeight)
   8469                     { top = pos.top - node.offsetHeight }
   8470                     else if (pos.bottom + node.offsetHeight <= vspace)
   8471                     { top = pos.bottom }
   8472                     if (left + node.offsetWidth > hspace)
   8473                     { left = hspace - node.offsetWidth }
   8474                 }
   8475                 node.style.top = top + "px"
   8476                 node.style.left = node.style.right = ""
   8477                 if (horiz == "right") {
   8478                     left = display.sizer.clientWidth - node.offsetWidth
   8479                     node.style.right = "0px"
   8480                 } else {
   8481                     if (horiz == "left") { left = 0 }
   8482                     else if (horiz == "middle") { left = (display.sizer.clientWidth - node.offsetWidth) / 2 }
   8483                     node.style.left = left + "px"
   8484                 }
   8485                 if (scroll)
   8486                 { scrollIntoView(this, {left: left, top: top, right: left + node.offsetWidth, bottom: top + node.offsetHeight}) }
   8487             },
   8488 
   8489             triggerOnKeyDown: methodOp(onKeyDown),
   8490             triggerOnKeyPress: methodOp(onKeyPress),
   8491             triggerOnKeyUp: onKeyUp,
   8492             triggerOnMouseDown: methodOp(onMouseDown),
   8493 
   8494             execCommand: function(cmd) {
   8495                 if (commands.hasOwnProperty(cmd))
   8496                 { return commands[cmd].call(null, this) }
   8497             },
   8498 
   8499             triggerElectric: methodOp(function(text) { triggerElectric(this, text) }),
   8500 
   8501             findPosH: function(from, amount, unit, visually) {
   8502                 var this$1 = this;
   8503 
   8504                 var dir = 1
   8505                 if (amount < 0) { dir = -1; amount = -amount }
   8506                 var cur = clipPos(this.doc, from)
   8507