Repository navigation
Expand file tree
/
Copy pathmathquill.min.js
More file actions
7 lines (7 loc) · 42.1 KB
/
Copy pathmathquill.min.js
File metadata and controls
7 lines (7 loc) · 42.1 KB
1
2
3
4
5
6
7
/**
* Copyleft 2010-2011 Jay and Han (laughinghan@gmail.com)
* under the GNU Lesser General Public License
* http://www.gnu.org/licenses/lgpl.html
* Project Website: http://mathquill.com
*/(function(){function R(a,b,c){i.apply(this,arguments)}function Q(b){this.parent=this.root=b;var c=this.jQ=this._jQ=a('<span class="cursor"></span>');this.blink=function(){c.toggleClass("blink")}}function P(a,b){g.call(this,"\\"+b+" ","<span>"+b+"</span>")}function O(a,b){g.call(this,a,"<big>"+b+"</big>")}function N(a,b){K.apply(this,arguments)}function M(a,b,c){g.call(this,a,'<span class="binary-operator">'+b+"</span>",c)}function L(a,b){g.call(this,a,'<span class="nonSymbola">'+(b||a)+"</span>")}function K(a,b){g.call(this,a,"<span>"+(b||a)+"</span>")}function J(a,b){g.call(this,a,"<var>"+(b||a)+"</var>")}function I(a){this.init("\\vector",b,b,a)}function H(){G.apply(this,arguments)}function G(a){this.init("\\binom",b,b,a),this.jQ.wrapInner('<span class="array"></span>').prepend('<span class="paren">(</span>').append('<span class="paren">)</span>')}function F(a){this.init("\\"),a&&(this.replacedFragment=a.detach(),this.isEmpty=function(){return!1})}function E(a,b){function d(){C.apply(this,arguments)}c=d.prototype=new C,c.cmd=a,c.html_template=[b];return d}function D(){}function C(a){a instanceof i?this.replacedText=a.remove().jQ.text():typeof a==="string"&&(this.replacedText=a),this.init()}function B(a){z.call(this,"|","|",a)}function A(a,b,c){y.call(this,a,b,a,b,c)}function z(a,b,c){x.call(this,a,b,a,b,c)}function y(a,b,c,d,e){x.apply(this,arguments)}function x(a,b,c,d,e){this.init("\\left"+c,['<span class="cmd"><span class="paren">'+a+'</span><span class="cmd"></span><span class="paren">'+b+"</span></span>"],[a,b],e),this.end="\\right"+d}function w(a){v.call(this,a),this.jQ=this.firstChild.jQ.detach().add(this.jQ)}function v(a){this.init("\\sqrt",b,b,a)}function u(){t.apply(this,arguments)}function t(a){this.init("\\frac",b,b,a),this.jQ.append('<span style="display:inline-block;width:0"> </span>')}function s(a,b,c,d){this.init(a,[b],[c],d)}function r(a,c,d){this.init(a,[c],b,d)}function q(a){var b=Array.prototype.slice.call(arguments,1);return p(a,function(){a.apply(this,Array.prototype.concat.apply(b,arguments))})}function p(a,b){b.prototype=a.prototype;return b}function m(){}function l(a){this.init("$"),this.firstChild.cursor=a,this.firstChild.textInput=function(b){this.skipTextInput||(b!=="$"||a.parent!==this?a.write(b):this.isEmpty()?a.insertAfter(this.parent).backspace().insertNew(new K("\\$","$")).show():a.next?a.prev?a.write(b):a.insertBefore(this.parent):a.insertAfter(this.parent))}}function k(){}function j(c,e,f,g){function y(){if(!x){var a=l.val();if(a){l.val("");for(var c=0;c<a.length;c++)j.parent.textInput(a.charAt(c))}else(j.selection||m!==b)&&n()}}function v(){var a=l.val();a.slice(0,1)==="$"&&a.slice(-1)==="$"?a=a.slice(1,-1):a="\\text{"+a+"}",j.writeLatex(a).show(),l.val("")}function u(){l.focus()}function s(d){o=b,j.blink=p,j.selection||(g?j.show():k.detach()),c.unbind("mousemove",q),a(document).unbind("mousemove",r).unbind("mouseup",s)}function r(a){delete a.target;return q(a)}function q(b){j.seek(a(b.target),b.pageX,b.pageY),(j.prev!==o.prev||j.parent!==o.parent)&&j.selectFrom(o);return!1}function n(){m=b;var a=j.selection?"$"+j.selection.latex()+"$":"";l.val(a);if(a)if(l[0].select)l[0].select();else if(document.selection){var c=l[0].createTextRange();c.expand("textedit"),c.select()}}var h=c.contents().detach();f||c.addClass("mathquill-rendered-math"),e.jQ=c.data(d,{block:e,revert:function(){c.empty().unbind(".mathquill").removeClass("mathquill-rendered-math mathquill-editable mathquill-textbox").append(h)}});var j=e.cursor=new Q(e);e.renderLatex(h.text());var k=e.textarea=a('<span class="textarea"><textarea></textarea></span>'),l=k.children(),m;e.selectionChanged=function(){m===b&&(m=setTimeout(n))},c.bind("selectstart.mathquill",function(a){a.target!==l[0]&&a.preventDefault(),a.stopPropagation()});var o,p=j.blink;c.bind("mousedown.mathquill",function(b){j.blink=a.noop,j.seek(a(b.target),b.pageX,b.pageY),o=new i(j.parent,j.prev,j.next),g||c.prepend(k),c.mousemove(q),a(document).mousemove(r).mouseup(s),b.stopPropagation()});if(g){c.prepend(k),c.addClass("mathquill-editable"),f&&c.addClass("mathquill-textbox"),l.focus(function(a){j.parent||j.appendTo(e),j.parent.jQ.addClass("hasCursor"),j.selection?(j.selection.jQ.removeClass("blur"),setTimeout(e.selectionChanged)):j.show(),a.stopPropagation()}).blur(function(a){j.hide().parent.blur(),j.selection&&j.selection.jQ.addClass("blur"),a.stopPropagation()}),c.bind("focus.mathquill blur.mathquill",function(a){l.trigger(a)}).bind("mousedown.mathquill",function(){setTimeout(u)}).bind("click.mathquill",u).blur(),c.bind("cut",function(a){n(),j.selection&&setTimeout(function(){j.deleteSelection(),j.redraw()}),a.stopPropagation()}).bind("copy",function(a){n(),x=!0,a.stopPropagation()}).bind("paste",function(a){x=!0,setTimeout(v),a.stopPropagation()});var w={},x=!1;c.bind("keydown.mathquill",function(a){w.evt=a,w.happened=!0,j.parent.keydown(a)===!1&&a.preventDefault()}).bind("keypress.mathquill",function(a){w.happened?w.happened=!1:j.parent.keydown(w.evt),m!==b&&clearTimeout(m),(j.selection||m!==b)&&l.val(""),x=!1,setTimeout(y)})}else{c.bind("cut paste",!1).bind("copy",n).prepend('<span class="selectable">$'+e.latex()+"$</span>"),l.blur(function(){j.clearSelection(),setTimeout(t)});function t(){k.detach()}}}function i(b,c,d){if(arguments.length){var e=this;e.parent=b,e.prev=c||0,e.next=d||0,e.jQinit(e.fold(a(),function(a,b){return b.jQ.add(a)}))}}function h(){}function g(a,b,c){this.init(a,[b],[c||(a&&a.length>1?a.slice(1):a)])}function f(){}function e(){}var a=jQuery,b,c,d="[[mathquill internal data]]";c=e.prototype,c.prev=0,c.next=0,c.parent=0,c.firstChild=0,c.lastChild=0,c.eachChild=function(a){for(var b=this.firstChild;b;b=b.next)if(a.call(this,b)===!1)break;return this},c.foldChildren=function(a,b){this.eachChild(function(c){a=b.call(this,a,c)});return a},c.keydown=function(a){return this.parent.keydown(a)},c.textInput=function(a){return this.parent.textInput(a)},c=f.prototype=new e,c.init=function(b,c,e,f){var g=this;b&&(g.cmd=b),c&&(g.html_template=c),e&&(g.text_template=e),g.jQ=a(g.html_template[0]).data(d,{cmd:g}),g.initBlocks(f)},c.initBlocks=function(b){var c=this;if(c.html_template.length===1)c.firstChild=c.lastChild=c.jQ.data(d).block=b&&b.blockify()||new h,c.firstChild.parent=c,c.firstChild.jQ=c.jQ.append(c.firstChild.jQ);else{var e,f,g=c.html_template.length;this.firstChild=e=f=b&&b.blockify()||new h,e.parent=c,e.jQ=a(c.html_template[1]).data(d,{block:e}).append(e.jQ).appendTo(c.jQ),e.blur();for(var i=2;i<g;i+=1)e=new h,e.parent=c,e.prev=f,f.next=e,f=e,e.jQ=a(c.html_template[i]).data(d,{block:e}).appendTo(c.jQ),e.blur();c.lastChild=e}},c.latex=function(){return this.foldChildren(this.cmd,function(a,b){return a+"{"+(b.latex()||" ")+"}"})},c.text_template=[""],c.text=function(){var a=0;return this.foldChildren(this.text_template[a],function(b,c){a+=1;var d=c.text();if(b&&this.text_template[a]==="("&&d[0]==="("&&d.slice(-1)===")")return b+d.slice(1,-1)+this.text_template[a];return b+c.text()+(this.text_template[a]||"")})},c.insertAt=function(a){var b=this;b.parent=a.parent,b.next=a.next,b.prev=a.prev,a.prev?a.prev.next=b:a.parent.firstChild=b,a.next?a.next.prev=b:a.parent.lastChild=b,a.prev=b,b.jQ.insertBefore(a.jQ),b.respace(),b.next&&b.next.respace(),b.prev&&b.prev.respace(),b.placeCursor(a),a.redraw()},c.respace=a.noop,c.placeCursor=function(a){a.appendTo(this.foldChildren(this.firstChild,function(a,b){return a.isEmpty()?a:b}))},c.isEmpty=function(){return this.foldChildren(!0,function(a,b){return a&&b.isEmpty()})},c.remove=function(){var a=this,b=a.prev,c=a.next,d=a.parent;b?b.next=c:d.firstChild=c,c?c.prev=b:d.lastChild=b,a.jQ.remove();return a},c=g.prototype=new f,c.initBlocks=a.noop,c.latex=function(){return this.cmd},c.text=function(){return this.text_template},c.placeCursor=a.noop,c.isEmpty=function(){return!0},c=h.prototype=new e,c.latex=function(){return this.foldChildren("",function(a,b){return a+b.latex()})},c.text=function(){return this.firstChild===this.lastChild?this.firstChild.text():this.foldChildren("(",function(a,b){return a+b.text()})+")"},c.isEmpty=function(){return this.firstChild===0&&this.lastChild===0},c.focus=function(){this.jQ.addClass("hasCursor"),this.isEmpty()&&this.jQ.removeClass("empty");return this},c.blur=function(){this.jQ.removeClass("hasCursor"),this.isEmpty()&&this.jQ.addClass("empty");return this},c=i.prototype,c.remove=f.prototype.remove,c.jQinit=function(a){this.jQ=a},c.each=function(a){for(var b=this.prev.next||this.parent.firstChild;b!==this.next;b=b.next)if(a.call(this,b)===!1)break;return this},c.fold=function(a,b){this.each(function(c){a=b.call(this,a,c)});return a},c.latex=function(){return this.fold("",function(a,b){return a+b.latex()})},c.blockify=function(){var a=this,b=a.prev,c=a.next,d=a.parent,e=new h,f=e.firstChild=b.next||d.firstChild,g=e.lastChild=c.prev||d.lastChild;b?b.next=c:d.firstChild=c,c?c.prev=b:d.lastChild=b,f.prev=a.prev=0,g.next=a.next=0,a.parent=e,a.each(function(a){a.parent=e}),e.jQ=a.jQ;return e},c=k.prototype=new h,c.latex=function(){return h.prototype.latex.call(this).replace(/(\\[a-z]+) (?![a-z])/ig,"$1")},c.text=function(){return this.foldChildren("",function(a,b){return a+b.text()})},c.renderLatex=function(a){this.jQ.children().slice(1).remove(),this.firstChild=this.lastChild=0,this.cursor.appendTo(this).writeLatex(a),this.blur()},c.keydown=function(a){a.ctrlKey=a.ctrlKey||a.metaKey;switch(a.originalEvent&&a.originalEvent.keyIdentifier||a.which){case 8:case"Backspace":case"U+0008":if(a.ctrlKey)while(this.cursor.prev||this.cursor.selection)this.cursor.backspace();else this.cursor.backspace();break;case 27:case"Esc":case"U+001B":case 9:case"Tab":case"U+0009":if(a.ctrlKey)break;var b=this.cursor.parent;if(a.shiftKey){if(b===this)return this.skipTextInput=!0;b.prev?this.cursor.appendTo(b.prev):this.cursor.insertBefore(b.parent)}else{if(b===this)return this.skipTextInput=!0;b.next?this.cursor.prependTo(b.next):this.cursor.insertAfter(b.parent)}this.cursor.clearSelection();break;case 13:case"Enter":break;case 35:case"End":if(a.shiftKey)while(this.cursor.next||a.ctrlKey&&this.cursor.parent!==this)this.cursor.selectRight();else this.cursor.clearSelection().appendTo(a.ctrlKey?this:this.cursor.parent);break;case 36:case"Home":if(a.shiftKey)while(this.cursor.prev||a.ctrlKey&&this.cursor.parent!==this)this.cursor.selectLeft();else this.cursor.clearSelection().prependTo(a.ctrlKey?this:this.cursor.parent);break;case 37:case"Left":if(a.ctrlKey)break;a.shiftKey?this.cursor.selectLeft():this.cursor.moveLeft();break;case 38:case"Up":if(a.ctrlKey)break;if(a.shiftKey)if(this.cursor.prev)while(this.cursor.prev)this.cursor.selectLeft();else this.cursor.selectLeft();else this.cursor.parent.prev?this.cursor.clearSelection().appendTo(this.cursor.parent.prev):this.cursor.prev?this.cursor.clearSelection().prependTo(this.cursor.parent):this.cursor.parent!==this&&this.cursor.clearSelection().insertBefore(this.cursor.parent.parent);break;case 39:case"Right":if(a.ctrlKey)break;a.shiftKey?this.cursor.selectRight():this.cursor.moveRight();break;case 40:case"Down":if(a.ctrlKey)break;if(a.shiftKey)if(this.cursor.next)while(this.cursor.next)this.cursor.selectRight();else this.cursor.selectRight();else this.cursor.parent.next?this.cursor.clearSelection().prependTo(this.cursor.parent.next):this.cursor.next?this.cursor.clearSelection().appendTo(this.cursor.parent):this.cursor.parent!==this&&this.cursor.clearSelection().insertAfter(this.cursor.parent.parent);break;case 46:case"Del":case"U+007F":if(a.ctrlKey)while(this.cursor.next||this.cursor.selection)this.cursor.deleteForward();else this.cursor.deleteForward();break;case 65:case"A":case"U+0041":if(a.ctrlKey&&!a.shiftKey&&!a.altKey){if(this!==this.cursor.root)return this.parent.keydown(a);this.cursor.clearSelection().appendTo(this);while(this.cursor.prev)this.cursor.selectLeft();break};default:this.skipTextInput=!1;return!0}this.skipTextInput=!0;return!1},c.textInput=function(a){this.skipTextInput||this.cursor.write(a)},c=l.prototype=new f,c.html_template=['<span class="mathquill-rendered-math"></span>'],c.initBlocks=function(){this.firstChild=this.lastChild=this.jQ.data(d).block=new k,this.firstChild.parent=this,this.firstChild.jQ=this.jQ},c.latex=function(){return"$"+this.firstChild.latex()+"$"},c=m.prototype=new h,c.renderLatex=function(a){var b=this,c=b.cursor;b.jQ.children().slice(1).remove(),b.firstChild=b.lastChild=0,c.show().appendTo(b),a=a.match(/(?:\\\$|[^$])+|\$(?:\\\$|[^$])*\$|\$(?:\\\$|[^$])*$/g)||"";for(var d=0;d<a.length;d+=1){var e=a[d];if(e[0]==="$"){e[-1+e.length]==="$"&&e[-2+e.length]!=="\\"?e=e.slice(1,-1):e=e.slice(1);var f=new l(c);c.insertNew(f),f.firstChild.renderLatex(e),c.show().insertAfter(f)}else for(var g=0;g<e.length;g+=1)this.cursor.insertNew(new K(e[g]))}},c.keydown=k.prototype.keydown,c.textInput=function(a){this.skipTextInput||(this.cursor.deleteSelection(),a==="$"?this.cursor.insertNew(new l(this.cursor)):this.cursor.insertNew(new K(a)))};var n={},o={};p(f,r),o.mathrm=q(r,"\\mathrm",'<span class="roman font"></span>'),o.mathit=q(r,"\\mathit",'<i class="font"></i>'),o.mathbf=q(r,"\\mathbf",'<b class="font"></b>'),o.mathsf=q(r,"\\mathsf",'<span class="sans-serif font"></span>'),o.mathtt=q(r,"\\mathtt",'<span class="monospace font"></span>'),o.underline=q(r,"\\underline",'<span class="underline"></span>'),o.overline=o.bar=q(r,"\\overline",'<span class="overline"></span>'),c=s.prototype=new f,c.latex=function(){var a=this.firstChild.latex();return a.length===1?this.cmd+a:this.cmd+"{"+(a||" ")+"}"},c.redraw=function(){this.respace(),this.next&&this.next.respace(),this.prev&&this.prev.respace()},c.respace=function(){this.prev.cmd==="\\int "||this.prev instanceof s&&this.prev.cmd!=this.cmd&&this.prev.prev&&this.prev.prev.cmd==="\\int "?this.limit||(this.limit=!0,this.jQ.addClass("limit")):this.limit&&(this.limit=!1,this.jQ.removeClass("limit")),(this.respaced=this.prev instanceof s&&this.prev.cmd!=this.cmd&&!this.prev.respaced)?this.limit&&this.cmd==="_"?this.jQ.css({left:-.25-this.prev.jQ.outerWidth()/+this.jQ.css("fontSize").slice(0,-2)+"em",marginRight:.1-Math.min(this.jQ.outerWidth(),this.prev.jQ.outerWidth())/+this.jQ.css("fontSize").slice(0,-2)+"em"}):this.jQ.css({left:-this.prev.jQ.outerWidth()/+this.jQ.css("fontSize").slice(0,-2)+"em",marginRight:.1-Math.min(this.jQ.outerWidth(),this.prev.jQ.outerWidth())/+this.jQ.css("fontSize").slice(0,-2)+"em"}):this.limit&&this.cmd==="_"?this.jQ.css({left:"-.25em",marginRight:""}):this.jQ.css({left:"",marginRight:""});return this},o.subscript=o._=p(s,function(a){s.call(this,"_","<sub></sub>","_",a)}),o.superscript=o.supscript=o["^"]=p(s,function(a){s.call(this,"^","<sup></sup>","**",a)}),c=t.prototype=new f,c.html_template=['<span class="fraction"></span>','<span class="numerator"></span>','<span class="denominator"></span>'],c.text_template=["(","/",")"],o.frac=o.dfrac=o.cfrac=o.fraction=t,c=u.prototype=new t,c.placeCursor=function(a){if(this.firstChild.isEmpty()){var b=this.prev;while(b&&!(b instanceof M||b instanceof C||b instanceof O))b=b.prev;b instanceof O&&b.next instanceof s&&(b=b.next,b.next instanceof s&&b.next.cmd!=b.cmd&&(b=b.next));if(b!==this.prev){var c=(new i(this.parent,b,this)).blockify();c.jQ=this.firstChild.jQ.empty().removeClass("empty").append(c.jQ).data(d,{block:c}),c.next=this.lastChild,c.parent=this,this.firstChild=this.lastChild.prev=c}}a.appendTo(this.lastChild)},n["/"]=u,c=v.prototype=new f,c.html_template=['<span class="cmd"><span class="sqrt-prefix">√</span></span>','<span class="sqrt-stem"></span>'],c.text_template=["sqrt(",")"],c.redraw=function(){var a=this.lastChild.jQ,b=a.outerHeight(!0);a.css({borderTopWidth:b/28+1}).prev().css({fontSize:.9*b/+a.css("fontSize").slice(0,-2)+"em"})},o.sqrt=o["√"]=v,c=w.prototype=new v,c.html_template=['<span class="cmd"><span class="sqrt-prefix">√</span></span>','<sup class="nthroot"></sup>','<span class="sqrt-stem"></span>'],c.text_template=["sqrt[","](",")"],c.latex=function(){return"\\sqrt["+this.firstChild.latex()+"]{"+this.lastChild.latex()+"}"},o.nthroot=w,c=x.prototype=new f,c.initBlocks=function(a){this.firstChild=this.lastChild=a&&a.blockify()||new h,this.firstChild.parent=this,this.firstChild.jQ=this.jQ.children(":eq(1)").data(d,{block:this.firstChild}).append(this.firstChild.jQ)},c.latex=function(){return this.cmd+this.firstChild.latex()+this.end},c.redraw=function(){var a=this.firstChild.jQ;a.prev().add(a.next()).css("fontSize",a.outerHeight()/(+a.css("fontSize").slice(0,-2)*1.02)+"em")},o.lbrace=n["{"]=p(x,function(a){x.call(this,"{","}","\\{","\\}",a)}),o.langle=o.lang=p(x,function(a){x.call(this,"⟨","⟩","\\langle ","\\rangle ",a)}),c=y.prototype=new x,c.placeCursor=function(a){!this.next&&this.parent.parent&&this.parent.parent.end===this.end&&this.firstChild.isEmpty()?a.backspace().insertAfter(this.parent.parent):this.firstChild.blur()},o.rbrace=n["}"]=p(y,function(a){y.call(this,"{","}","\\{","\\}",a)}),o.rangle=o.rang=p(y,function(a){y.call(this,"⟨","⟩","\\langle ","\\rangle ",a)}),z.prototype=x.prototype,o.lparen=n["("]=p(z,function(a){z.call(this,"(",")",a)}),o.lbrack=o.lbracket=n["["]=p(z,function(a){z.call(this,"[","]",a)}),A.prototype=y.prototype,o.rparen=n[")"]=p(A,function(a){A.call(this,"(",")",a)}),o.rbrack=o.rbracket=n["]"]=p(A,function(a){A.call(this,"[","]",a)}),c=B.prototype=new z,c.placeCursor=function(a){!this.next&&this.parent.parent&&this.parent.parent.end===this.end&&this.firstChild.isEmpty()?a.backspace().insertAfter(this.parent.parent):a.appendTo(this.firstChild)},o.lpipe=o.rpipe=n["|"]=B,c=C.prototype=new f,c.cmd="\\text",c.html_template=['<span class="text"></span>'],c.text_template=['"','"'],c.initBlocks=function(){this.firstChild=this.lastChild=this.jQ.data(d).block=new D,this.firstChild.parent=this,this.firstChild.jQ=this.jQ.append(this.firstChild.jQ)},c.placeCursor=function(a){(this.cursor=a).appendTo(this.firstChild);if(this.replacedText)for(var b=0;b<this.replacedText.length;b+=1)this.write(this.replacedText.charAt(b))},c.write=function(a){this.cursor.insertNew(new K(a))},c.keydown=function(a){if(!this.cursor.selection&&(a.which===8&&!this.cursor.prev||a.which===46&&!this.cursor.next)){this.isEmpty()&&this.cursor.insertAfter(this);return!1}return this.parent.keydown(a)},c.textInput=function(a){this.cursor.deleteSelection();if(a!=="$")this.write(a);else if(this.isEmpty())this.cursor.insertAfter(this).backspace().insertNew(new K("\\$","$"));else if(this.cursor.next)if(this.cursor.prev){var b=new C(new i(this.firstChild,this.cursor.prev));b.placeCursor=function(a){this.prev=0,delete this.placeCursor,this.placeCursor(a)},b.firstChild.focus=function(){return this},this.cursor.insertAfter(this).insertNew(b),b.prev=this,this.cursor.insertBefore(b),delete b.firstChild.focus}else this.cursor.insertBefore(this);else this.cursor.insertAfter(this)},c=D.prototype=new h,c.blur=function(){this.jQ.removeClass("hasCursor");if(this.isEmpty()){var a=this.parent,b=a.cursor;b.parent===this?this.jQ.addClass("empty"):(b.hide(),a.remove(),b.next===a?b.next=a.next:b.prev===a&&(b.prev=a.prev),b.show().redraw())}return this},c.focus=function(){h.prototype.focus.call(this);var a=this.parent;if(a.next.cmd===a.cmd){var b=this,c=a.cursor,d=a.next.firstChild;d.eachChild(function(a){a.parent=b,a.jQ.appendTo(b.jQ)}),this.lastChild?this.lastChild.next=d.firstChild:this.firstChild=d.firstChild,d.firstChild.prev=this.lastChild,this.lastChild=d.lastChild,d.parent.remove(),c.prev?c.insertAfter(c.prev):c.prependTo(this),c.redraw()}else if(a.prev.cmd===a.cmd){var c=a.cursor;c.prev?a.prev.firstChild.focus():c.appendTo(a.prev.firstChild)}return this},n.$=o.text=o.textnormal=o.textrm=o.textup=o.textmd=C,o.em=o.italic=o.italics=o.emph=o.textit=o.textsl=E("\\textit",'<i class="text"></i>'),o.strong=o.bold=o.textbf=E("\\textbf",'<b class="text"></b>'),o.sf=o.textsf=E("\\textsf",'<span class="sans-serif text"></span>'),o.tt=o.texttt=E("\\texttt",'<span class="monospace text"></span>'),o.textsc=E("\\textsc",'<span style="font-variant:small-caps" class="text"></span>'),o.uppercase=E("\\uppercase",'<span style="text-transform:uppercase" class="text"></span>'),o.lowercase=E("\\lowercase",'<span style="text-transform:lowercase" class="text"></span>'),c=F.prototype=new f,c.html_template=['<span class="latex-command-input">\\</span>'],c.text_template=["\\"],c.placeCursor=function(b){this.cursor=b.appendTo(this.firstChild),this.replacedFragment&&(this.jQ=this.jQ.add(this.replacedFragment.jQ.addClass("blur").bind("mousedown mousemove",function(b){a(b.target=this.nextSibling).trigger(b);return!1}).insertBefore(this.jQ)))},c.latex=function(){return"\\"+this.firstChild.latex()+" "},c.keydown=function(a){if(a.which===9||a.which===13){this.renderCommand();return!1}return this.parent.keydown(a)},c.textInput=function(a){if(a.match(/[a-z]/i))this.cursor.deleteSelection(),this.cursor.insertNew(new K(a));else{this.renderCommand();if(a===" "||a==="\\"&&this.firstChild.isEmpty())return;this.cursor.parent.textInput(a)}},c.renderCommand=function(){this.jQ=this.jQ.last(),this.remove(),this.next?this.cursor.insertBefore(this.next):this.cursor.appendTo(this.parent);var a=this.firstChild.latex(),b;if(a)if(b=o[a])b=new b(this.replacedFragment,a);else{b=new C(a),b.firstChild.focus=function(){delete this.focus;return this},this.cursor.insertNew(b).insertAfter(b),this.replacedFragment&&this.replacedFragment.remove();return}else b=new K("\\backslash ","\\");this.cursor.insertNew(b),b instanceof g&&this.replacedFragment&&this.replacedFragment.remove()},n["\\"]=F,c=G.prototype=new f,c.html_template=['<span class="cmd"></span>',"<span></span>","<span></span>"],c.text_template=["choose(",",",")"],c.redraw=function(){this.jQ.children(":first").add(this.jQ.children(":last")).css("fontSize",this.jQ.outerHeight()/(+this.jQ.css("fontSize").slice(0,-2)*.9+2)+"em")},o.binom=o.binomial=G,c=H.prototype=new G,c.placeCursor=u.prototype.placeCursor,o.choose=H,c=I.prototype=new f,c.html_template=['<span class="array"></span>',"<span></span>"],c.latex=function(){return"\\begin{matrix}"+this.foldChildren([],function(a,b){a.push(b.latex());return a}).join("\\\\")+"\\end{matrix}"},c.text=function(){return"["+this.foldChildren([],function(a,b){a.push(b.text());return a}).join()+"]"},c.placeCursor=function(a){this.cursor=a.appendTo(this.firstChild)},c.keydown=function(b){var c=this.cursor.parent;if(c.parent===this){if(b.which===13){var e=new h;e.parent=this,e.jQ=a("<span></span>").data(d,{block:e}).insertAfter(c.jQ),c.next?c.next.prev=e:this.lastChild=e,e.next=c.next,c.next=e,e.prev=c,this.cursor.appendTo(e).redraw();return!1}if(b.which===9&&!b.shiftKey&&!c.next){if(c.isEmpty()){if(c.prev){this.cursor.insertAfter(this),delete c.prev.next,this.lastChild=c.prev,c.jQ.remove(),this.cursor.redraw();return!1}return this.parent.keydown(b)}var e=new h;e.parent=this,e.jQ=a("<span></span>").data(d,{block:e}).appendTo(this.jQ),this.lastChild=e,c.next=e,e.prev=c,this.cursor.appendTo(e).redraw();return!1}if(b.which===8){if(c.isEmpty()){c.prev?(this.cursor.appendTo(c.prev),c.prev.next=c.next):(this.cursor.insertBefore(this),this.firstChild=c.next),c.next?c.next.prev=c.prev:this.lastChild=c.prev,c.jQ.remove(),this.isEmpty()?this.cursor.deleteForward():this.cursor.redraw();return!1}if(!this.cursor.prev)return!1}}return this.parent.keydown(b)},o.vector=I,o.editable=p(l,function(){this.init("\\editable"),j(this.jQ,this.firstChild,!1,!0);var a;this.placeCursor=function(b){a=b.appendTo(this.firstChild)},this.firstChild.blur=function(){a.prev===this.parent&&(delete this.blur,this.cursor.appendTo(this),h.prototype.blur.call(this))},this.latex=function(){return this.firstChild.latex()},this.text=function(){return this.firstChild.text()}}),o.f=q(g,"f",'<var class="florin">ƒ</var>'),c=J.prototype=new g,c.text=function(){var a=this.cmd;this.prev&&!(this.prev instanceof J)&&!(this.prev instanceof M)&&(a="*"+a),this.next&&!(this.next instanceof M)&&this.next.cmd!=="^"&&(a+="*");return a},K.prototype=g.prototype,n[" "]=q(K,"\\:"," "),o.prime=n["'"]=q(K,"'","′"),L.prototype=g.prototype,o["@"]=L,o["&"]=q(L,"\\&","&"),o["%"]=q(L,"\\%","%"),o.alpha=o.beta=o.gamma=o.delta=o.zeta=o.eta=o.theta=o.iota=o.kappa=o.mu=o.nu=o.xi=o.rho=o.sigma=o.tau=o.chi=o.psi=o.omega=p(g,function(a,b){J.call(this,"\\"+b+" ","&"+b+";")}),o.phi=q(J,"\\phi ","ϕ"),o.phiv=o.varphi=q(J,"\\varphi ","φ"),o.epsilon=q(J,"\\epsilon ","ϵ"),o.epsiv=o.varepsilon=q(J,"\\varepsilon ","ε"),o.piv=o.varpi=q(J,"\\varpi ","ϖ"),o.sigmaf=o.sigmav=o.varsigma=q(J,"\\varsigma ","ς"),o.thetav=o.vartheta=o.thetasym=q(J,"\\vartheta ","ϑ"),o.upsilon=o.upsi=q(J,"\\upsilon ","υ"),o.gammad=o.Gammad=o.digamma=q(J,"\\digamma ","ϝ"),o.kappav=o.varkappa=q(J,"\\varkappa ","ϰ"),o.rhov=o.varrho=q(J,"\\varrho ","ϱ"),o.pi=o["π"]=q(L,"\\pi ","π"),o.lambda=q(L,"\\lambda ","λ"),o.Upsilon=o.Upsi=o.upsih=o.Upsih=q(g,"\\Upsilon ",'<var style="font-family: serif">ϒ</var>'),o.Gamma=o.Delta=o.Theta=o.Lambda=o.Xi=o.Pi=o.Sigma=o.Phi=o.Psi=o.Omega=o.forall=p(g,function(a,b){K.call(this,"\\"+b+" ","&"+b+";")}),M.prototype=new g,c=N.prototype=new M,c.respace=function(){this.prev?this.prev instanceof M&&this.next&&!(this.next instanceof M)?this.jQ[0].className="unary-operator":this.jQ[0].className="binary-operator":this.jQ[0].className="";return this},o["+"]=q(N,"+"),o["-"]=q(N,"-","−"),o.pm=o.plusmn=o.plusminus=q(N,"\\pm ","±"),o.mp=o.mnplus=o.minusplus=q(N,"\\mp ","∓"),n["*"]=o.sdot=o.cdot=q(M,"\\cdot ","·"),o["="]=q(M,"=","="),o["<"]=q(M,"<","<"),o[">"]=q(M,">",">"),o.notin=o.sim=o.cong=o.equiv=o.oplus=o.otimes=p(M,function(a,b){M.call(this,"\\"+b+" ","&"+b+";")}),o.times=p(M,function(a,b){M.call(this,"\\times ","×","[x]")}),o.div=o.divide=o.divides=q(M,"\\div ","÷","[/]"),o.ne=o.neq=q(M,"\\ne ","≠"),o.ast=o.star=o.loast=o.lowast=q(M,"\\ast ","∗"),o.therefor=o.therefore=q(M,"\\therefore ","∴"),o.cuz=o.because=q(M,"\\because ","∵"),o.prop=o.propto=q(M,"\\propto ","∝"),o.asymp=o.approx=q(M,"\\approx ","≈"),o.lt=q(M,"<","<"),o.gt=q(M,">",">"),o.le=o.leq=q(M,"\\le ","≤"),o.ge=o.geq=q(M,"\\ge ","≥"),o.isin=o["in"]=q(M,"\\in ","∈"),o.ni=o.contains=q(M,"\\ni ","∋"),o.notni=o.niton=o.notcontains=o.doesnotcontain=q(M,"\\not\\ni ","∌"),o.sub=o.subset=q(M,"\\subset ","⊂"),o.sup=o.supset=o.superset=q(M,"\\supset ","⊃"),o.nsub=o.notsub=o.nsubset=o.notsubset=q(M,"\\not\\subset ","⊄"),o.nsup=o.notsup=o.nsupset=o.notsupset=o.nsuperset=o.notsuperset=q(M,"\\not\\supset ","⊅"),o.sube=o.subeq=o.subsete=o.subseteq=q(M,"\\subseteq ","⊆"),o.supe=o.supeq=o.supsete=o.supseteq=o.supersete=o.superseteq=q(M,"\\supseteq ","⊇"),o.nsube=o.nsubeq=o.notsube=o.notsubeq=o.nsubsete=o.nsubseteq=o.notsubsete=o.notsubseteq=q(M,"\\not\\subseteq ","⊈"),o.nsupe=o.nsupeq=o.notsupe=o.notsupeq=o.nsupsete=o.nsupseteq=o.notsupsete=o.notsupseteq=o.nsupersete=o.nsuperseteq=o.notsupersete=o.notsuperseteq=q(M,"\\not\\supseteq ","⊉"),O.prototype=new g,o.sum=o.summation=q(O,"\\sum ","∑"),o.prod=o.product=q(O,"\\prod ","∏"),o.coprod=o.coproduct=q(O,"\\coprod ","∐"),o.int=o.integral=o["∫"]=q(O,"\\int ","∫"),o.N=o.naturals=o.Naturals=q(K,"\\mathbb{N}","ℕ"),o.P=o.primes=o.Primes=o.projective=o.Projective=o.probability=o.Probability=q(K,"\\mathbb{P}","ℙ"),o.Z=o.integers=o.Integers=q(K,"\\mathbb{Z}","ℤ"),o.Q=o.rationals=o.Rationals=q(K,"\\mathbb{Q}","ℚ"),o.R=o.reals=o.Reals=q(K,"\\mathbb{R}","ℝ"),o.C=o.complex=o.Complex=o.complexes=o.Complexes=o.complexplane=o.Complexplane=o.ComplexPlane=q(K,"\\mathbb{C}","ℂ"),o.H=o.Hamiltonian=o.quaternions=o.Quaternions=q(K,"\\mathbb{H}","ℍ"),o.quad=o.emsp=q(K,"\\quad "," "),o.qquad=q(K,"\\qquad "," "),o.diamond=q(K,"\\diamond ","◇"),o.bigtriangleup=q(K,"\\bigtriangleup ","△"),o.ominus=q(K,"\\ominus ","⊖"),o.uplus=q(K,"\\uplus ","⊎"),o.bigtriangledown=q(K,"\\bigtriangledown ","▽"),o.sqcap=q(K,"\\sqcap ","⊓"),o.triangleleft=q(K,"\\triangleleft ","⊲"),o.sqcup=q(K,"\\sqcup ","⊔"),o.triangleright=q(K,"\\triangleright ","⊳"),o.odot=q(K,"\\odot ","⊙"),o.bigcirc=q(K,"\\bigcirc ","◯"),o.dagger=q(K,"\\dagger ","†"),o.ddagger=q(K,"\\ddagger ","‡"),o.wr=q(K,"\\wr ","≀"),o.amalg=q(K,"\\amalg ","∐"),o.models=q(K,"\\models ","⊨"),o.prec=q(K,"\\prec ","≺"),o.succ=q(K,"\\succ ","≻"),o.preceq=q(K,"\\preceq ","≼"),o.succeq=q(K,"\\succeq ","≽"),o.simeq=q(K,"\\simeq ","≃"),o.mid=q(K,"\\mid ","∣"),o.ll=q(K,"\\ll ","≪"),o.gg=q(K,"\\gg ","≫"),o.parallel=q(K,"\\parallel ","∥"),o.bowtie=q(K,"\\bowtie ","⋈"),o.sqsubset=q(K,"\\sqsubset ","⊏"),o.sqsupset=q(K,"\\sqsupset ","⊐"),o.smile=q(K,"\\smile ","⌣"),o.sqsubseteq=q(K,"\\sqsubseteq ","⊑"),o.sqsupseteq=q(K,"\\sqsupseteq ","⊒"),o.doteq=q(K,"\\doteq ","≐"),o.frown=q(K,"\\frown ","⌢"),o.vdash=q(K,"\\vdash ","⊦"),o.dashv=q(K,"\\dashv ","⊣"),o.longleftarrow=q(K,"\\longleftarrow ","←"),o.longrightarrow=q(K,"\\longrightarrow ","→"),o.Longleftarrow=q(K,"\\Longleftarrow ","⇐"),o.Longrightarrow=q(K,"\\Longrightarrow ","⇒"),o.longleftrightarrow=q(K,"\\longleftrightarrow ","↔"),o.updownarrow=q(K,"\\updownarrow ","↕"),o.Longleftrightarrow=q(K,"\\Longleftrightarrow ","⇔"),o.Updownarrow=q(K,"\\Updownarrow ","⇕"),o.mapsto=q(K,"\\mapsto ","↦"),o.nearrow=q(K,"\\nearrow ","↗"),o.hookleftarrow=q(K,"\\hookleftarrow ","↩"),o.hookrightarrow=q(K,"\\hookrightarrow ","↪"),o.searrow=q(K,"\\searrow ","↘"),o.leftharpoonup=q(K,"\\leftharpoonup ","↼"),o.rightharpoonup=q(K,"\\rightharpoonup ","⇀"),o.swarrow=q(K,"\\swarrow ","↙"),o.leftharpoondown=q(K,"\\leftharpoondown ","↽"),o.rightharpoondown=q(K,"\\rightharpoondown ","⇁"),o.nwarrow=q(K,"\\nwarrow ","↖"),o.ldots=q(K,"\\ldots ","…"),o.cdots=q(K,"\\cdots ","⋯"),o.vdots=q(K,"\\vdots ","⋮"),o.ddots=q(K,"\\ddots ","⋰"),o.surd=q(K,"\\surd ","√"),o.triangle=q(K,"\\triangle ","▵"),o.ell=q(K,"\\ell ","ℓ"),o.top=q(K,"\\top ","⊤"),o.flat=q(K,"\\flat ","♭"),o.natural=q(K,"\\natural ","♮"),o.sharp=q(K,"\\sharp ","♯"),o.wp=q(K,"\\wp ","℘"),o.bot=q(K,"\\bot ","⊥"),o.clubsuit=q(K,"\\clubsuit ","♣"),o.diamondsuit=q(K,"\\diamondsuit ","♢"),o.heartsuit=q(K,"\\heartsuit ","♡"),o.spadesuit=q(K,"\\spadesuit ","♠"),o.oint=q(K,"\\oint ","∮"),o.bigcap=q(K,"\\bigcap ","∩"),o.bigcup=q(K,"\\bigcup ","∪"),o.bigsqcup=q(K,"\\bigsqcup ","⊔"),o.bigvee=q(K,"\\bigvee ","∨"),o.bigwedge=q(K,"\\bigwedge ","∧"),o.bigodot=q(K,"\\bigodot ","⊙"),o.bigotimes=q(K,"\\bigotimes ","⊗"),o.bigoplus=q(K,"\\bigoplus ","⊕"),o.biguplus=q(K,"\\biguplus ","⊎"),o.lfloor=q(K,"\\lfloor ","⌊"),o.rfloor=q(K,"\\rfloor ","⌋"),o.lceil=q(K,"\\lceil ","⌈"),o.rceil=q(K,"\\rceil ","⌉"),o.slash=q(K,"\\slash ","/"),o.opencurlybrace=q(K,"\\opencurlybrace ","{"),o.closecurlybrace=q(K,"\\closecurlybrace ","}"),o.caret=q(K,"\\caret ","^"),o.underscore=q(K,"\\underscore ","_"),o.backslash=q(K,"\\backslash ","\\"),o.vert=q(K,"|"),o.perp=o.perpendicular=q(K,"\\perp ","⊥"),o.nabla=o.del=q(K,"\\nabla ","∇"),o.hbar=q(K,"\\hbar ","ℏ"),o.AA=o.Angstrom=o.angstrom=q(K,"\\text\\AA ","Å"),o.ring=o.circ=o.circle=q(K,"\\circ ","∘"),o.bull=o.bullet=q(K,"\\bullet ","•"),o.setminus=o.smallsetminus=q(K,"\\setminus ","∖"),o.not=o.neg=q(K,"\\neg ","¬"),o.dots=o.ellip=o.hellip=o.ellipsis=o.hellipsis=q(K,"\\dots ","…"),o.converges=o.darr=o.dnarr=o.dnarrow=o.downarrow=q(K,"\\downarrow ","↓"),o.dArr=o.dnArr=o.dnArrow=o.Downarrow=q(K,"\\Downarrow ","⇓"),o.diverges=o.uarr=o.uparrow=q(K,"\\uparrow ","↑"),o.uArr=o.Uparrow=q(K,"\\Uparrow ","⇑"),o.to=q(M,"\\to ","→"),o.rarr=o.rightarrow=q(K,"\\rightarrow ","→"),o.implies=q(M,"\\Rightarrow ","⇒"),o.rArr=o.Rightarrow=q(K,"\\Rightarrow ","⇒"),o.gets=q(M,"\\gets ","←"),o.larr=o.leftarrow=q(K,"\\leftarrow ","←"),o.impliedby=q(M,"\\Leftarrow ","⇐"),o.lArr=o.Leftarrow=q(K,"\\Leftarrow ","⇐"),o.harr=o.lrarr=o.leftrightarrow=q(K,"\\leftrightarrow ","↔"),o.iff=q(M,"\\Leftrightarrow "
,"⇔"),o.hArr=o.lrArr=o.Leftrightarrow=q(K,"\\Leftrightarrow ","⇔"),o.Re=o.Real=o.real=q(K,"\\Re ","ℜ"),o.Im=o.imag=o.image=o.imagin=o.imaginary=o.Imaginary=q(K,"\\Im ","ℑ"),o.part=o.partial=q(K,"\\partial ","∂"),o.inf=o.infin=o.infty=o.infinity=q(K,"\\infty ","∞"),o.alef=o.alefsym=o.aleph=o.alephsym=q(K,"\\aleph ","ℵ"),o.xist=o.xists=o.exist=o.exists=q(K,"\\exists ","∃"),o.and=o.land=o.wedge=q(K,"\\wedge ","∧"),o.or=o.lor=o.vee=q(K,"\\vee ","∨"),o.o=o.O=o.empty=o.emptyset=o.oslash=o.Oslash=o.nothing=o.varnothing=q(M,"\\varnothing ","∅"),o.cup=o.union=q(K,"\\cup ","∪"),o.cap=o.intersect=o.intersection=q(K,"\\cap ","∩"),o.deg=o.degree=q(K,"^\\circ ","°"),o.ang=o.angle=q(K,"\\angle ","∠"),c=P.prototype=new g,c.respace=function(){this.jQ[0].className=this.next instanceof s||this.next instanceof x?"":"non-italicized-function"},o.ln=o.lg=o.log=o.span=o.proj=o.det=o.dim=o.min=o.max=o.mod=o.lcm=o.gcd=o.gcf=o.hcf=o.lim=P,function(){var a=["sin","cos","tan","sec","cosec","csc","cotan","cot"];for(var b in a)o[a[b]]=o[a[b]+"h"]=o["a"+a[b]]=o["arc"+a[b]]=o["a"+a[b]+"h"]=o["arc"+a[b]+"h"]=P}(),c=Q.prototype,c.prev=0,c.next=0,c.parent=0,c.show=function(){this.jQ=this._jQ.removeClass("blink"),"intervalId"in this?clearInterval(this.intervalId):(this.next?this.selection&&this.selection.prev===this.prev?this.jQ.insertBefore(this.selection.jQ):this.jQ.insertBefore(this.next.jQ.first()):this.jQ.appendTo(this.parent.jQ),this.parent.focus()),this.intervalId=setInterval(this.blink,500);return this},c.hide=function(){"intervalId"in this&&clearInterval(this.intervalId),delete this.intervalId,this.jQ.detach(),this.jQ=a();return this},c.redraw=function(){for(var a=this.parent;a;a=a.parent)a.redraw&&a.redraw()},c.insertAt=function(a,b,c){var d=this.parent;this.parent=a,this.prev=b,this.next=c,d.blur()},c.insertBefore=function(a){this.insertAt(a.parent,a.prev,a),this.parent.jQ.addClass("hasCursor"),this.jQ.insertBefore(a.jQ.first());return this},c.insertAfter=function(a){this.insertAt(a.parent,a,a.next),this.parent.jQ.addClass("hasCursor"),this.jQ.insertAfter(a.jQ.last());return this},c.prependTo=function(a){this.insertAt(a,0,a.firstChild),a.textarea?this.jQ.insertAfter(a.textarea):this.jQ.prependTo(a.jQ),a.focus();return this},c.appendTo=function(a){this.insertAt(a,a.lastChild,0),this.jQ.appendTo(a.jQ),a.focus();return this},c.hopLeft=function(){this.jQ.insertBefore(this.prev.jQ.first()),this.next=this.prev,this.prev=this.prev.prev;return this},c.hopRight=function(){this.jQ.insertAfter(this.next.jQ.last()),this.prev=this.next,this.next=this.next.next;return this},c.moveLeft=function(){this.selection?this.insertBefore(this.selection.prev.next||this.parent.firstChild).clearSelection():this.prev?this.prev.lastChild?this.appendTo(this.prev.lastChild):this.hopLeft():this.parent.prev?this.appendTo(this.parent.prev):this.parent!==this.root&&this.insertBefore(this.parent.parent);return this.show()},c.moveRight=function(){this.selection?this.insertAfter(this.selection.next.prev||this.parent.lastChild).clearSelection():this.next?this.next.firstChild?this.prependTo(this.next.firstChild):this.hopRight():this.parent.next?this.prependTo(this.parent.next):this.parent!==this.root&&this.insertAfter(this.parent.parent);return this.show()},c.seek=function(a,b,c){var e=this.clearSelection();if(a.hasClass("empty")){e.prependTo(a.data(d).block);return e}var f=a.data(d);if(f){if(f.cmd&&!f.block){a.outerWidth()>2*(b-a.offset().left)?e.insertBefore(f.cmd):e.insertAfter(f.cmd);return e}}else a=a.parent(),f=a.data(d),f||(f={block:e.root});f.cmd?e.insertAfter(f.cmd):e.appendTo(f.block);var g=e.jQ.offset().left-b,h;do e.moveLeft(),h=g,g=e.jQ.offset().left-b;while(g>0&&(e.prev||e.parent!==e.root));-g>h&&e.moveRight();return e},c.writeLatex=function(a){this.deleteSelection(),a=a&&a.match(/\\text\{([^}]|\\\})*\}|\\[a-z]*|[^\s]/ig)||0,function c(d){while(a.length){var e=a.shift();if(!e||e==="}")return;var f;if(e.slice(0,6)==="\\text{"){f=new C(e.slice(6,-1)),d.insertNew(f).insertAfter(f);continue}if(e==="\\left"||e==="\\right"){e=a.shift(),e==="\\"&&(e=a.shift()),d.insertCh(e),f=d.prev||d.parent.parent;if(d.prev)return;a.unshift("{")}else if(/^\\[a-z]+$/i.test(e)){e=e.slice(1);var f=o[e];if(f)d.insertNew(f=new f(b,e));else{f=new C(e),d.insertNew(f).insertAfter(f);continue}}else e.match(/[a-eg-zA-Z]/)?f=new J(e):(f=o[e])?f=new f:f=new K(e),d.insertNew(f);f.eachChild(function(b){d.appendTo(b);var e=a.shift();if(!e)return!1;e==="{"?c(d):d.insertCh(e)}),d.insertAfter(f)}}(this);return this.hide()},c.write=function(a){return this.show().insertCh(a)},c.insertCh=function(a){this.selection&&(this.prev=this.selection.prev,this.next=this.selection.next);var b;a.match(/^[a-eg-zA-Z]$/)?b=new J(a):(b=n[a]||o[a])?b=new b(this.selection,a):b=new K(a),this.selection&&(b instanceof g&&this.selection.remove(),delete this.selection);return this.insertNew(b)},c.insertNew=function(a){a.insertAt(this);return this},c.unwrapGramp=function(){var a=this.parent.parent,b=a.parent,c=a.prev,d=this;a.eachChild(function(d){d.isEmpty()||(d.eachChild(function(c){c.parent=b,c.jQ.insertBefore(a.jQ.first())}),d.firstChild.prev=c,c?c.next=d.firstChild:b.firstChild=d.firstChild,c=d.lastChild)}),c.next=a.next,a.next?a.next.prev=c:b.lastChild=c;if(!this.next)if(this.prev)this.next=this.prev.next;else while(!this.next){this.parent=this.parent.next;if(this.parent)this.next=this.parent.firstChild;else{this.next=a.next,this.parent=b;break}}this.next?this.insertBefore(this.next):this.appendTo(b),a.jQ.remove(),a.prev&&a.prev.respace(),a.next&&a.next.respace()},c.backspace=function(){if(!this.deleteSelection())if(this.prev)this.prev.isEmpty()?this.prev=this.prev.remove().prev:this.selectLeft();else if(this.parent!==this.root){if(this.parent.parent.isEmpty())return this.insertAfter(this.parent.parent).backspace();this.unwrapGramp()}this.prev&&this.prev.respace(),this.next&&this.next.respace(),this.redraw();return this},c.deleteForward=function(){if(!this.deleteSelection())if(this.next)this.next.isEmpty()?this.next=this.next.remove().next:this.selectRight();else if(this.parent!==this.root){if(this.parent.parent.isEmpty())return this.insertBefore(this.parent.parent).deleteForward();this.unwrapGramp()}this.prev&&this.prev.respace(),this.next&&this.next.respace(),this.redraw();return this},c.selectFrom=function(a){var b=this,c=a;loopThroughAncestors:while(!0){for(var d=this;d!==b.parent.parent;d=d.parent.parent)if(d.parent===c.parent){f=d,g=c;break loopThroughAncestors}for(var e=a;e!==c.parent.parent;e=e.parent.parent)if(b.parent===e.parent){f=b,g=e;break loopThroughAncestors}b.parent.parent&&(b=b.parent.parent),c.parent.parent&&(c=c.parent.parent)}var f,g,h;if(f.next!==g){for(var i=f;i;i=i.next)if(i===g.prev){h=!0;break}h||(h=g,g=f,f=h)}this.hide().selection=new R(f.parent,f.prev,g.next),this.insertAfter(g.next.prev||g.parent.lastChild),this.root.selectionChanged()},c.selectLeft=function(){if(this.selection)if(this.selection.prev===this.prev)this.prev?(this.hopLeft().next.jQ.prependTo(this.selection.jQ),this.selection.prev=this.prev):this.parent!==this.root&&this.insertBefore(this.parent.parent).selection.levelUp();else{this.prev.jQ.insertAfter(this.selection.jQ),this.hopLeft().selection.next=this.next;if(this.selection.prev===this.prev){this.deleteSelection();return}}else this.prev?this.hopLeft():this.parent!==this.root&&this.insertBefore(this.parent.parent),this.hide().selection=new R(this.parent,this.prev,this.next.next);this.root.selectionChanged()},c.selectRight=function(){if(this.selection)if(this.selection.next===this.next)this.next?(this.hopRight().prev.jQ.appendTo(this.selection.jQ),this.selection.next=this.next):this.parent!==this.root&&this.insertAfter(this.parent.parent).selection.levelUp();else{this.next.jQ.insertBefore(this.selection.jQ),this.hopRight().selection.prev=this.prev;if(this.selection.next===this.next){this.deleteSelection();return}}else this.next?this.hopRight():this.parent!==this.root&&this.insertAfter(this.parent.parent),this.hide().selection=new R(this.parent,this.prev.prev,this.next);this.root.selectionChanged()},c.clearSelection=function(){this.show().selection&&(this.selection.clear(),delete this.selection,this.root.selectionChanged());return this},c.deleteSelection=function(){if(!this.show().selection)return!1;this.prev=this.selection.prev,this.next=this.selection.next,this.selection.remove(),delete this.selection,this.root.selectionChanged();return!0},c=R.prototype=new i,c.jQinit=function(a){this.jQ=a.wrapAll('<span class="selection"></span>').parent()},c.levelUp=function(){this.clear().jQinit(this.parent.parent.jQ),this.prev=this.parent.parent.prev,this.next=this.parent.parent.next,this.parent=this.parent.parent.parent;return this},c.clear=function(){this.jQ.replaceWith(this.jQ.children());return this},c.blockify=function(){this.jQ.replaceWith(this.jQ=this.jQ.children());return i.prototype.blockify.call(this)},c.detach=function(){var a=i.prototype.blockify.call(this);this.blockify=function(){this.jQ.replaceWith(a.jQ=this.jQ=this.jQ.children());return a};return this},a.fn.mathquill=function(b,c){switch(b){case"redraw":this.find(":not(:has(:first))").each(function(){var b=a(this).data(d);b&&(b.cmd||b.block)&&Q.prototype.redraw.call(b.cmd||b.block)});return this;case"revert":return this.each(function(){var b=a(this).data(d);b&&b.revert&&b.revert()});case"latex":if(arguments.length>1)return this.each(function(){var b=a(this).data(d);b&&b.block&&b.block.renderLatex&&b.block.renderLatex(c)});var e=this.data(d);return e&&e.block&&e.block.latex();case"text":var e=this.data(d);return e&&e.block&&e.block.text();case"html":return this.html().replace(/ ?hasCursor|hasCursor /,"").replace(/ class=(""|(?= |>))/g,"").replace(/<span class="?cursor( blink)?"?><\/span>/i,"").replace(/<span class="?textarea"?><textarea><\/textarea><\/span>/i,"");case"write":if(arguments.length>1)return this.each(function(){var b=a(this).data(d),e=b&&b.block,f=e&&e.cursor;f&&(f.writeLatex(c),e.blur())});default:var f=b==="textbox",g=f||b==="editable",h=f?m:k;return this.each(function(){j(a(this),new h,f,g)})}},a(function(){a(".mathquill-editable:not(.mathquill-rendered-math)").mathquill("editable"),a(".mathquill-textbox:not(.mathquill-rendered-math)").mathquill("textbox"),a(".mathquill-embedded-latex").mathquill()})})()