Last active
February 11, 2019 21:45
-
-
Save TamerRizk/f65ace8ff89a5c136f09ee18290350c0 to your computer and use it in GitHub Desktop.
mathKey
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
mathKey.js: Copyright (c) 2018-2028 TR (tr[at]mathkey.org) | |
By exercising any rights granted by this license (the "License"), You agree and accept to be bound by the terms and conditions herein. To the extent that the License may be interpreted as a contract, You are granted consideration of Your agreement with these terms and conditions by consideration for your utility of (the "Software", "mathKey.js"), and the Copyright (the "Licensor", "TR") specified in the notice above is granted consideration for utilitizing the software from acceptance of the Software through its utilization. | |
Subject to Your agreement to be bound by the terms and conditions herein the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable License that shall permit You to deal in the Software and permit those upon whom You furnish the Software to use the Software, without limitation to Your rights to use, link to, copy, and/or sell copies of the Software, subject to the following terms and conditions: | |
Any alteration, arrangement, transformation, or other modification of the Software is an Adapted Work. Any process of publication to alter, arrange, transform, or otherwise modify the Software to an Adapted Work is disclosure of Adapted Work. | |
You may exercise rights to alter, arrange, transform, or otherwise modify the Software subject to the former and following terms and conditions: | |
Except in disclosure and distribution as part of the Software through express consent of the Licensor, Adapted Work shall not be disclosed or distributed to any recipient without such recipient of express prior agreement to not publish such Adapted Work. The Text of the License must be included in such Adapted Work. | |
Rights are hereby granted, free of charge, for You to publish and distribute the Software subject to the former and following terms and conditions: | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
The full text of the License, from the above Copyright notice to this license notice, (the "Text") must be included in all copies or substantial portions of the Software. | |
This is MathKey version 0.33 | |
With special thanks to ASCIIMathML.js: | |
Peter Jipsen http://www.chapman.edu/~jipsen | |
Douglas Woodall | |
Paulo Soares | |
*/ | |
var __hashSum = function(){ | |
/******************************************************************************************/ | |
/* hashSum */ | |
/******************************************************************************************/ | |
/* | |
The object of return by this function, and following this notice, is made available under: | |
The MIT License (MIT) | |
Copyright (c) 2014 Nicolas Bevacqua | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE | |
*/ | |
var hs = {}; | |
hs.pad = function(hash, len) { | |
while (hash.length < len) { | |
hash = '0' + hash; | |
} | |
return hash; | |
}; | |
hs.fold = function(hash, text) { | |
var i, chr, len; | |
if ((len=text.length) === 0) { | |
return hash; | |
} | |
for(i=-1; len>++i;) { | |
chr = text.charCodeAt(i); | |
hash = ((hash << 5) - hash) + chr; | |
hash |= 0; | |
} | |
return hash < 0 ? hash * -2 : hash; | |
}; | |
hs.foldObject = function(hash, o, seen) { | |
var foldKey = function(hash, key) { | |
return hs.foldValue(hash, o[key], key, seen); | |
}; | |
return Object.keys(o).sort().reduce(foldKey, hash); | |
}; | |
hs.foldValue = function (input, value, key, seen) { | |
var hash = hs.fold(hs.fold(hs.fold(input, key), Object.prototype.toString.call(hs.normalize(value))), typeof value); | |
if (value === null) { | |
return hs.fold(hash, 'null'); | |
} | |
if (value === undefined) { | |
return hs.fold(hash, 'undefined'); | |
} | |
if (typeof value === 'object') { | |
if (seen.indexOf(value) !== -1) { | |
return hs.fold(hash, '[Circular]' + key); | |
} | |
seen.push(value); | |
return hs.foldObject(hash, value, seen); | |
} | |
return hs.fold(hash, hs.normalize(value).toString()); | |
}; | |
hs.normalize = !!String.prototype.normalize ? function(s){ | |
return 'string'==typeof s? s.normalize() : s; | |
} : function(s){ return s; }; //see unorm polyfill | |
hs.hash = function(o) { | |
return hs.pad(hs.normalize(hs.foldValue(0, o, '', [])).toString(16), 8); | |
}; | |
return hs; | |
}; | |
/******************************************************************************************/ | |
var __levenshtein = function(value='', other='', insensitive=false){ | |
/******************************************************************************************/ | |
/* levenshtein-edit-distance */ | |
/******************************************************************************************/ | |
/* | |
This function is made available under: | |
The MIT License (MIT) | |
Copyright (c) 2018 Titus Wormer | |
(License text incorporated by reference) | |
*/ | |
var cache = [], codes = [], length, lengthOther, code, result, distance, distanceOther, index, indexOther; | |
if (value === other) { | |
return 0 | |
} | |
length = value.length; | |
lengthOther = other.length; | |
if (length === 0) { | |
return lengthOther; | |
} | |
if (lengthOther === 0) { | |
return length; | |
} | |
if (!!insensitive) { | |
value = value.toLowerCase(); | |
other = other.toLowerCase(); | |
} | |
index = 0; | |
while (index < length) { | |
codes[index] = value.charCodeAt(index); | |
cache[index] = ++index; | |
} | |
indexOther = 0; | |
while (indexOther < lengthOther) { | |
code = other.charCodeAt(indexOther); | |
result = distance = indexOther++; | |
index = -1; | |
while (++index < length) { | |
distanceOther = code === codes[index] ? distance : distance + 1; | |
distance = cache[index]; | |
cache[index] = result = | |
distance > result ? | |
distanceOther > result ? result + 1 : distanceOther | |
: | |
distanceOther > distance ? distance + 1 : distanceOther; | |
} | |
} | |
return result; | |
}; | |
/******************************************************************************************/ | |
(function (root, factory) { | |
if (typeof define === 'function' && define.amd) { | |
define([], factory); | |
} else if ((typeof module === 'object' || typeof exports === 'object') && module.exports) { | |
module.exports = factory(); | |
} else { | |
root.mathKey = factory(); | |
} | |
}(typeof self !== 'undefined' ? self : this, function () { | |
var conf = { | |
checkformathml: true, | |
notifyifnomathml: false, | |
notifywarnings: true, | |
notifywarningscallback: null, | |
translateonstart: true, | |
document: null, | |
autostart: true, | |
translateclass: '_MK_', | |
fallbackcssurl: '/mathml-mozilla.css', // or mathml-w3c.css ... see: https://developer.mozilla.org/en-US/docs/Mozilla/MathML_Project/test-mathml-css and https://www.w3.org/TR/MathML2/appendixg.html | |
fallbackliburl: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js', //if available, ex: https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js or http://www.wiris.com/quizzes/demo/generic/quizzes/resources/quizzes.js | |
mstyle:{displaystyle:'true', scriptsizemultiplier:'0.8', scriptminsize:'6px', mathcolor:'', fontfamily:'serif'}, | |
manage:'`', | |
managementinterface: null | |
}, mk = null; | |
// token types | |
var mkDef = {VAR:0, UNARY:1, BINARY:2, INFIX:3, LEFTBRACKET:4, RIGHTBRACKET:5, LEFTRIGHT:6, SHAPE:7, SPACE:8, UNDEROVER:9, DIFFERENTIAL:10, TEXT:11, PHANTOM: 12, NONE: 13, MULTI:14, CONST:15, STYLE:16}, mkRdef ={}; | |
for(mk in mkDef){ | |
eval('var '+mk+'='+mkDef[mk]+';'); | |
mkRdef[mkDef[mk]]=mk; | |
} | |
mk = {init:null}; | |
var mkDebug = false, errorBuf = [], userAgent = '', mkMathns = 'http://www.w3.org/1998/Math/MathML', mkDoc, mkHasMathML = false, mkConfigured = false, mkReady = false, mkTranslated = 0, mkTsp={mo:0.7}, mkCk = false, mkFallback = '', mkFallbackCSS = '', mkLineHeight = 5, mkFields = {}, | |
mkMng = {html:['<div style="background-color: #ffffff;display:inline-block;width:700px;box-shadow: 3px 3px 3px #000; padding:5px 0 15px 0; position:relative;"><div style="text-align:right;height:10px;width:100%;"><div style="height:10px;color:#aaaaaa;font-weight: 600;user-select: none;cursor: pointer;width:20px;font-size:14px;display:inline-block;margin-right:7px;line-height:14px" onclick="this.parentNode.parentNode.parentNode.style.display=\'none\'" aria-label="close this panel" tabIndex="0">X</div></div><table border="0" cellspacing="0" cellpadding="0" width="100%" style="width: 620px; max-width: 620px; display:inline-block;"><tr><td style="text-align:right;height:10px;"><span class="mathKeyFex" style="font-size: 9px;font-family: times;float: left;white-space: nowrap;text-align: left;max-width:500px;overflow:hidden;"></span><span style="position:relative;top:38px;cursor:pointer;font-size:9px;color:#666666;text-decoration:underline;" onclick="mathKey.manageReset();">reset definitions</span></td><td></td><td rowspan="3" style="vertical-align:middle;text-align:center;width:225px" class="mathKeyOutput"></td></tr><tr><td style="text-align:left;vertical-align:bottom;height:25px;width:410px;"><input type="text" class="mathKeyInput" onkeyup="mathKey.manageInput()" placeholder="New symbol entry" style="width:400px;height:20px;border:1px solid #ccc;font-size:10px;"></td><td style="text-align:left;vertical-align:bottom;text-align:left;"><button style="cursor:pointer;width:40px;height:24px;font-size:11px;" onclick="mathKey.manageAdd()">Add</button></td></tr><tr><td style="text-align:left;height:40px;"><select style="width:280px;height:20px;border:1px solid #ccc;font-size:10px;margin-right:5px;" class="mathKeyFields" onclick="mathKey.manageFieldData();"><option value="" style="color:#cccccc">--fields--</option></select><span style="cursor:pointer;font-size:12px;color:#444;background:#ccc;border:1px solid #999999;line-height:12px;padding:0 3px 0 3px;" onclick="mathKey.manageField();">▲</span></td><td></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%" style="border: 1px solid #ddd; border-radius: 5px; width: 620px; max-width: 620px; display:inline-block;font-family: Times, serif;font-size:10px;">', '<tr>', | |
'<td style="cursor: pointer; text-align:center;vertical-align:middle;background:%MKROWCOLOR%;padding: 2px;height: 25px !important;overflow: hidden;border-bottom: 1px solid #ccc;outline:none;width:50px;max-width:50px;" onclick="mathKey.manageCopy(this.parentNode)"><span class="mathKeyTranslateDict">%MKDICT%</span></td><td style="cursor: pointer;font-family: monospace; text-align:left;vertical-align:middle;width:540px;max-width:540px;background:%MKROWCOLOR%;padding: 2px;height: 25px !important;overflow: hidden;border-bottom: 1px solid #ccc;outline:none;" onclick="mathKey.manageCopy(this.parentNode)">{%MKENTRY%}</td><td style="border-bottom: 1px solid #ccc;outline:none;font-size:10px;text-align:center;vertical-align:middle;;user-select: none;width:25px;background:%MKROWCOLOR%;cursor: pointer;color:#ff0000" onclick="mathKey.manageDelete(this.parentNode);">X</td>', '</tr>', '</table></div>'], rowcolors:['#ffffff','#fcfcfc'], style: {display:'none', position:'absolute', zIndex:'2147483636', width:'100%', height:'1000%', top:'0', left:'0', textAlign:'center', background:'rgba(0,0,0,0.8)'}}, | |
mkLoading = new Image(); | |
mkLoading.height=16; | |
mkLoading.width=16; | |
mkLoading.className = '__mkLoading'; | |
mkLoading.src='data:image/gif;base64,R0lGODlhIAAgANUAAP////f///f39/f3/+/39+/v9+/v7+bv7+bm7+bm5t7e3t7e5tbW3s7O1sXOzsXO1sXF1sXFzr3Fxb3Fzr29xbW9vbW9xbW1xbW1va21ta21va2tva2ttaWttZylrZycrZScrZScpZSUpYyUpYyUnISMnP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFCAAmACwAAAAAIAAgAAAG/0CTcEgkgkolUHHJLBaGBBKSRBgCmk2Bo9MQFpDIp6mAKFyxQ0Omw35+wVUAYo4IoIWCNVtiIoBLVWR0Z3cKbGwHAn8CcnQCaAKPQhaHGgYlIyMlBYIIBlaEeBERCEIIhxMGBpGrnZICc6EKoxENnxMYn0ywTwKdYiYCtLQMBZJNAACdg0IAC8O1och0dGZEAgy0x3cD1dJpDQx3TolLyeeR40Tn7CYJC/Dx32iN1Ajx+PNY9dT48upW7M1550/fNIEm2AFgBLCZwm+8Go5BsG1dpwEAu82xZmVZrHH8Np4JWSYZPWUCCXVaNbHikIgCDFRbN+eVN4Wu8HxcR8gevymQoCBRY0Rt4VB1/J78nMjsToCiCaHyswOSjJilEzkCJIQ1oUQm1AAGAQAh+QQFCAAmACwAAAAAGgATAAAGmkCTcEgkYjodTHG5FBCRSKKBSVREFkIBtOM0WUqWblEQKUecWqjQUGqPEExzhCHc1tulEYGJkBdMWwALeCUNRQBDDWYNBB0ZGR0mIXgfRIILfyYFZgwCXQIGHXhYQwYLpwhODIZUCSCRRKeyC6mIVCYCmaWzCwm3v0IIssDEAglwxMAAAclFCM/QyM1D0dDTRNXP19TZ21S220EAIfkEBQgAJgAsAAAAAB8ADgAABpZAk3BIJDYikUZxyWyakEin1AQwLAxDaGTY6Dimw4V4kYUOO+gMVgoYLxBCrVCC7mTApoJbAJAj6h0KTgBECWMJT1ACGnUWQwIHRQgIAkICY3BUhCYTdZkNIySVQ5MIBUIICaNLBhhbCx4lshdCAKWlp3gbsrwlaya3k3gCIb0lFKsABaV4qLwdmUsHuc0dIILNeAXJJkEAIfkEBQgAJgAsBgAAABoAFAAABp9Ak3AoTCwWCaJyKQQMAcejUyhgKhHP6GK6iCisAYQY4YRGnYKIOlJdjhEFk1lqYqwjVsFbqDUZ7lhWBnt9DWsNRAZuYgUAAU0CdmpxJgoaHUsFgVYNDCYIFh2iDkpTVnImEaKrmKenl6yIroKrE4qzpxIWm7inBgIEBQUEvZkjJcjIIMVEAiTJycxDztDI0kIGz9Ah1yYCw8HD3UKNSkEAIfkEBQgAJgAsDQAAABMAGgAABoxAE2KIMBmPyCSxmGwel0fDwgBwConHhXZhhQq3i2oTKgAXnIAlAJxACqxf7VsYicBNgkTR0KhHFHd4DH5+c1YAfYRhgQJ+DIaBDA0GgUkCc5eVeB2cnRiVGZ2doKKcjKGiGoGUVZmVHSBcmkIltRtMdwIhtbwUkE0FG7y8lIELHrwXsyYNIyS/jAdHQQAh+QQFCAAmACwSAAEADgAeAAAGi0CT0AQYGo8mBPIIQDgRgaWp8FRKq4Lr0zAsdqnOYWGx8A4RBZMAQV5wkew2eZmQu6VtqzYr7UsjgIENSACBhoSGgUuJEYNHAnx+EhUJfgYdmBNvRwEamJ+OQ1kRn59GCSAbSRafDkKXJbELQgqeoiOxJR9GmyYLuSWhSB25IwRSBsV6SBYlFpHIfUEAIfkEBQgAJgAsDAAGABQAGgAABp1Ak3BILJoAxiRRgEAolYWm8zkERKVNJNWEbW6J0oL2ayoYyEUkYL0mAxbweIIcr9Pr8O8bP0ezx1QMDWdoJgIRiAwCaA2IjguASgyOjotKBhgTJgaNiApDAgYCoSYTHadTCBARQwUFJSMjJAYapx0VS1UCJbwlAge2HZ9JBL0lBKW2GUrFvQVCyoRFr85CDR0OTwQkvCTIaCG8IWhBACH5BAUIACYALAYADAAaABQAAAabQJNwSCwSD8akkghYOo2IqBTxVE6l1eQ1moVuu8RAE2xNCMjJhZqKHibUcEOxcF4CBAg4vNgBJZYNDCZ4cEhDCyWJHQZ1AgIMEZEFQgVqY0IfiSUhJh0ZGR0EDZERDUOXQg2aJQsAHa8dJgWkEWxFBCOasZ2wQpCkWrmJcryvZwK0dUUCFiUWQgKwHXULEQpPxELSRMpdGq8aXUEAIfkEBQgAJgAsAAASAB8ADgAABphAk3AoKAyPyKRyqAB1lssCYonolK5TKBLBLQCIlKs4JNAOueisySAWb8xDaRrxNV3EnoUpgjEsBQlZBmhGQgIkIw1CBx2NE0IAdQgLlGUmAlxIB5YmFY0dGgIRoxEmCZQLCUd1SgqfHVOkEQACqAuFZhmfj3ukQpOorEsGuo1DskO2cEIOHYpCyEIGCwbCy8e+13ANo89mQQAh+QQFCAAmACwAAAwAGgAUAAAGnECTUAgoDI/IpJBQKBAEBYFyOgSVrleSgUrFYklSrtKbDYuRIfLIeE4ynYJtm4uoSOZKw6TDl+OFDXyCGn9DgoIRJgBiAUgOghYIJgwNdGxHHRoKQgURngwCiyYBRQimSX4mDZ4RlQuvCyams1QIrBFbsLGys2ZItwyKuosFs5JJArdSAMOKxo1KChG7zLCismdm1a/X13MJrwlTQQAh+QQFCAAmACwAAAYAEwAaAAAGkkCT8CAQGo9IoYA0aiSfwktp6llAj4aptrQpXE0CyrYUKn5NiI0WcT4uQJ02uAgwGc6Yjl6fMV/3e31ngHxtGoSCbQJmi3JGBg0MjgIMEZZ+UAALlpwNdVcCnJwMmE8KnXcICaVIERBsYAuysE+YCbKyAAi7CJ9IBbgLRby0R5q4sMRQwUbKTwAGC3dCznLVbcpBACH5BAkIACYALAAAAAAgACAAAAaiQJNwSCwaj8iiIclkCiwlS3NKRIxK2CV1SrhiO1tqA4tdhI9a4Yc8EpyLHY1CuCB30m9HZ29BmDYgCW9we4URJm6DRA2FexoBikYGE4V4kUQJFRKXSQKJnA0RoqOcQ6OnAKUmp6OppaGsqrJUAgl+syYIC7uzBru/gqq/v7elB8RuBcWRALsFQwjRBa7M0NHRswLXy6Xbz7IB29Sz3LPjuKVBADs='; | |
var mkCref = [{'>':'>','<':'<','&':'&','"':'"','>':'>','<':'<','&':'&','"':'"','(:':'\u2774',':)':'\u2775', '||':'\u2228', '^|^':'uparrow','.|.':'downarrow','___':'\uff3f\uff3f','__':'\uff3f','\uff3f_':'\uff3f\uff3f'},{':\\:':'\uff3c','\\_':'\uff0f','|--':'\u27cc ','+/-':'\u00B1','___':'\uff3f\uff3f','__':'\uff3f','\uff3f_':'\uff3f\uff3f'}], | |
mkParen = [{'\u2774':'\u2775','(':')','{':'}','[':']'}]; | |
mkParen[1] = {}; | |
mkParen[2] = {}; | |
Object.keys(mkParen[0]).filter(function(k){ mkParen[1][mkParen[0][k]]=k; 255>k.charCodeAt(0)&&(mkParen[2][k]=mkParen[0][k])&&(mkParen[2][mkParen[0][k]]=k);}); | |
var mkSync = {}, mkToParseSo={}, mkCopying=null, mkCCopy = 0, mkExt={sn:{},ref:{},id:{},img:{},cache:['','','','']}, mkOrigin={}, mkRorigin={}, mkFrames = {}, mkFrameUrl = {}, mkFromMathKey, mkUrl, mkTo = {pmInteractive:[100,50], fDoc:[500,5], fStart:[2000,1], wfpmInteractive:[5000,1], checkForId:[250,30]}, | |
mkRand = [], mkHashSum = __hashSum(), mkSig = {'06e42274':true,'5e218b5f':true}; | |
var mkNames = {}, mkRnames = {}, mkBak = [], mkBakNames={}, mkSym = [], mkRLength = 0, | |
mkElementPhantom = {symbol:{input:'', ptag:'mrow', output:'', type:PHANTOM}, pos:0, text:''}, | |
mkSymbolNone = {input:'', ptag:'mi', output:'', type:NONE}, | |
mkSymbolMulti = {input:'', ptag:'mrow', output:'', type:MULTI}; | |
var mkSymbols = [ | |
{input:'alpha', ptag:'mi', output:'\u03B1', type:VAR, sym:true}, | |
{input:'beta', ptag:'mi', output:'\u03B2', type:VAR, sym:true}, | |
{input:'chi', ptag:'mi', output:'\u03C7', type:VAR, sym:true}, | |
{input:'delta', ptag:'mi', output:'\u03B4', type:VAR, sym:true}, | |
{input:'Delta', ctag:'minus', csymbol: 'displacement', ptag:'mi', output:'\u0394', type:VAR, sym:true, functional: BINARY, isprefixop:true, paren:true}, | |
{input:'epsilon', ptag:'mi', output:'\u03B5', type:VAR, sym:true}, | |
{input:'varepsilon', ptag:'mi', output:'\u025B', type:VAR, sym:true}, | |
{input:'eta', ptag:'mi', output:'\u03B7', type:VAR, sym:true}, | |
{input:'gamma', ptag:'mi', output:'\u03B3', type:VAR, sym:true}, | |
{input:'Gamma', ptag:'mi', output:'\u0393', type:VAR, functional: UNARY, paren:true}, | |
{input:'iota', ptag:'mi', output:'\u03B9', type:VAR, sym:true}, | |
{input:'kappa', ptag:'mi', output:'\u03BA', type:VAR, sym:true}, | |
{input:'lambda', ctag:'lambda', ptag:'mi', output:'\u03BB', type:VAR, sym:true, functional: UNARY, paren:true}, | |
{input:'Lambda', ptag:'mi', output:'\u039B', type:VAR, functional: UNARY, paren:true}, | |
{input:'mu', ptag:'mi', output:'\u03BC', type:VAR, sym:true}, | |
{input:'nu', ptag:'mi', output:'\u03BD', type:VAR, sym:true}, | |
{input:'omega', ptag:'mi', output:'\u03C9', type:VAR, sym:true}, | |
{input:'Omega', ptag:'mi', output:'\u03A9', type:VAR, sym:true, functional: UNARY, paren:true}, | |
{input:'phi', ptag:'mi', output:'\u03C6', type:VAR, sym:true}, | |
{input:'varphi', ptag:'mi', output:'\u03D5', type:VAR, sym:true}, | |
{input:'Phi', ptag:'mi', output:'\u03A6', type:VAR, sym:true, functional: UNARY, paren:true}, | |
{input:'pi', ctag:'pi', ptag:'mi', output:'\u03C0', type:CONST, sym:true}, | |
{input:'Pi', ptag:'mi', ctag:'product', output:'\u220F', type:UNDEROVER, sym:true, functional: UNARY, sov:true}, | |
{input:'prod', ptag:'mo', ctag:'product', output:'\u220F', type:UNDEROVER, sov:true}, | |
{input:'Sigma', ptag:'mi', ctag:'sum', output:'\u2211', type:UNDEROVER, sov:true, functional:UNARY, separators:'+'}, | |
{input:'sum', ptag:'mo', ctag:'sum', output:'\u2211', type:UNDEROVER, sov:true, separators:'+'}, | |
{input:'stdev', ptag:'mo', ctag:'sdev', output:'\u03C3', type:UNARY, paren:true, sov:true}, | |
{input:'sigma', ptag:'mi', ctag:'sdev', output:'\u03C3', type:VAR, sym:true, functional: UNARY, sov:true, separators:'',paren:true}, | |
{input:'sigma2', ptag:'mi', ctag:'variance', output:'\u03C3\u00B2', type:VAR, sym:true, functional: UNARY, sov:true, paren:true}, | |
{input:'vari', ptag:'mo', ctag:'variance', output:'\u03C3\u00B2', type:UNARY, paren:true, sov:true}, | |
{input:'div', ptag:'mo', ctag:'divergence', output:'\u2207\u22C5', type:UNARY}, | |
{input:'grad', ptag:'mo', ctag:'grad', output:'\u2207', type:UNARY, paren:true}, | |
{input:'curl', ptag:'mo', ctag:'curl', output:'\u2207\u00D7', type:VAR, functional: UNARY}, | |
{input:'nab2', ptag:'mi', ctag:'laplacian', output:'\u2207\u00B2', type:VAR, sym:true, functional: UNARY,paren:true}, | |
{input:'Lap', ptag:'mi', ctag:'laplacian', output:'\u2112', type:VAR, paren:true, functional:UNARY, separators:'',sov:true}, | |
{input:'psi', ptag:'mi', output:'\u03C8', type:VAR, sym:true}, | |
{input:'Psi', ptag:'mi', output:'\u03A8', type:VAR, sym:true, functional: UNARY, paren:true}, | |
{input:'rho', ptag:'mi', output:'\u03C1', type:VAR, sym:true}, | |
{input:'tau', ptag:'mi', output:'\u03C4', type:VAR, sym:true}, | |
{input:'theta', ptag:'mi', output:'\u03B8', type:VAR, sym:true}, | |
{input:'vartheta', ptag:'mi', output:'\u03D1', type:VAR, sym:true}, | |
{input:'Theta', ptag:'mi', output:'\u0398', type:VAR, sym:true, functional: UNARY, paren:true}, | |
{input:'upsilon', ptag:'mi', output:'\u03C5', type:VAR, sym:true}, | |
{input:'xi', ptag:'mi', output:'\u03BE', type:VAR, sym:true}, | |
{input:'Xi', ptag:'mi', output:'\u039E', type:VAR, sym:true, functional: UNARY, paren:true}, | |
{input:'zeta', ptag:'mi', output:'\u03B6', type:VAR, sym:true}, | |
{input:'euler', ctag:'eulergamma', ptag:'mi', output:'\u03B3', type:CONST, sym:true}, | |
{input:'+', ptag:'mo', ctag:'plus', output:'+', type:CONST, arith:true, pri:true}, | |
{input:'-', ptag:'mo', ctag:'minus', output:'-', type:CONST, arith:true, pri:true}, | |
{input:'**', ptag:'mo', ctag:'times', output:'\u00D7', type:CONST, arith: true}, | |
{input:'*', ptag:'mo', ctag:'times', output:'*', type:CONST, arith: true}, | |
{input:'/', ptag:'mfrac', ctag:'divide', output:'/', type:INFIX, arith:true}, | |
{input:'-:', ptag:'mo', ctag:'divide', output:'\u00F7', type:CONST, arith:true}, | |
{input:'\u27cc', vinput:'|--', ptag:'mlongdiv', ctag:'divide', output:'\u27cc', type:INFIX, infixop:true, arith:true, infixswap:1}, | |
{input:'.*', ptag:'mo', ctag:'scalarproduct', output:'\u22C5', type:CONST, arith:true}, | |
{input:'~*', ptag:'mo', ctag:'vectorproduct', output:'\u00D7', type:CONST, arith:true}, | |
{input:'@*', ptag:'mo', ctag:'outerproduct', output:'\u2297', type:CONST, arith:true}, | |
{input:'@', ptag:'mi', ctag:'compose' , output:'\u26AC', type:CONST, arith:true}, | |
{input:'_', ptag:'msub', output:'_', type:INFIX}, | |
{input:'^', ptag:'msup', output:'^', type:INFIX}, | |
{input:'!', ptag:'mo', ctag:'factorial', output:'!', type:CONST, issuffixop:true}, | |
{input:'\u00B1', vinput:'+/-', ctag:'plusminus', ptag:'mo', output:'\u00B1', type:CONST, arith:true, pri:true}, | |
{input:'%', ctag:'rem', ptag:'mo', output:'mod', type:INFIX, infixop:true}, | |
{input:'\uff0f', vinput:'\\_', ctag:'ci', ptag:'mo', output:'\u2220', type:INFIX, infixop:true, ctype:'complex-polar'}, | |
{input:'imgi', ctag:'imaginaryi', ptag:'mi', output:'\u2170', type:CONST, sym:true}, | |
{input:'Real', ctag:'real', ptag:'mo', output:'\u211b', type:UNARY, paren:true}, | |
{input:'Imag', ctag:'imaginary', ptag:'mo', output:'\u2111', type:UNARY, paren:true}, | |
{input:'Argu', ctag:'arg', ptag:'mo', output:'Arg', type:UNARY, paren:true}, | |
{input:'Conj', ctag:'conjugate', ptag:'mover', output:'\u00AF', type:UNARY, acc:true, functional:true}, | |
{input:'bar', ptag:'mover', ctag:'conjugate', output:'\u00AF', type:CONST, acc:true, functional:true}, | |
{input:'=', ptag:'mo', ctag:'eq', output:'=', type:CONST, eq:true}, | |
{input:'==', ptag:'mo', ctag:'equivalent', output:'\u2261', type:CONST, eq:true}, | |
{input:'~=', ptag:'mo', ctag:'equivalent', output:'\u2245', type:CONST, eq:true}, | |
{input:'~~', ptag:'mo', ctag:'approx', output:'\u2248', type:CONST, eq:true}, | |
{input:'=~', ptag:'mo', ctag:'factorof', output:'\u221D', type:CONST, eq:true}, //not entirely accurate | |
{input:'!=', ptag:'mo', ctag:'neq', output:'\u2260', type:CONST, eq:true}, | |
{input:':=', ptag:'mo', ctag:'eq', output:':=', type:CONST, eq:true}, | |
{input:'<', ptag:'mo', ctag:'lt', output:'<', type:CONST, eq:true}, | |
{input:'>', ptag:'mo', ctag:'gt', output:'>', type:CONST, eq:true}, | |
{input:'<=', ptag:'mo', ctag:'leq', output:'\u2264', type:CONST, eq:true}, | |
{input:'>=', ptag:'mo', ctag:'geq', output:'\u2265', type:CONST, eq:true}, | |
{input:'->', ptag:'mo', ctag:'tendsto', output:'\u2192', type:CONST, rel:true}, | |
{input:'=>', ptag:'mo', ctag:'implies', output:'\u21D2', type:CONST, rel:true}, | |
{input:'if:', ptag:'mo', output:'if', ctag:'if', type:CONST, fi:true }, /////////// | |
{input:'\u01c0', vinput:'|', ptag:'mo', ctag:'condition' , output:'|', type:CONST, cond:true,arith:true,ipop:true}, | |
{input:'...', ptag:'mo', output:'...', type:SHAPE}, | |
{input:':.', ptag:'mo', output:'\u2234', type:SHAPE}, | |
{input:'&&', ptag:'mo', ctag:'and', output:'\u2227', type:CONST, logic:true, pri:true}, | |
{input:'\u2228', vinput:'||', ptag:'mo', ctag:'or', output:'\u2228', type:CONST, logic:true, pri:true}, | |
{input:'`', ptag:'mo', ctag:'not', output:'\'', type:CONST, issuffixop:true}, | |
{input:'true', ctag:'true', ptag:'mi', output:'true', type:CONST, sym:true}, | |
{input:'false', ctag:'false', ptag:'mi', output:'false', type:CONST, sym:true}, | |
{input:':n:', ptag:'mo', ctag:'intersect', output:'\u2229', type:CONST, setop:true, pri:true}, | |
{input:':u:', ptag:'mo', ctag:'union', output:'\u222A', type:CONST, setop:true, pri:true}, | |
{input:':in:', ptag:'mo', ctag:'in', output:'\u2208', type:CONST, setop:true}, | |
{input:'!in:', ptag:'mo', ctag:'notin', output:'\u2209', type:CONST, setop:true}, | |
{input:':c:', ptag:'mo', ctag:'prsubset', output:'\u2282', type:CONST, setop:true}, | |
{input:'!c:', ptag:'mo', ctag:'notprsubset', output:'\u2282', type:CONST, setop:true}, | |
{input:':c::', ptag:'mo', ctag:'subset', output:'\u2286', type:CONST, setop:true}, | |
{input:'!c::', ptag:'mo', ctag:'notsubset', output:'\u2288', type:CONST, setop:true}, | |
{input:'\uff3c', vinput:':\\:', ptag:'mo', ctag:'setdiff', output:'\\', type:CONST, setop:true, ipop:true}, | |
{input:':*:', ptag:'mo', ctag:'cartesianproduct', output:'\u00D7', type:CONST, setop:true, ipop:true}, | |
{input:'O/', ctag:'emptyset', ptag:'mi', output:'\u2205', type:CONST, sym:true}, | |
{input:'#_', ptag:'mo', ctag:'floor', output:'\u230A', append:'\u230B', type:CONST, isprefixop:true}, | |
{input:'#^', ptag:'mo', ctag:'ceiling', output:'\u2308', append:'\u2309', type:CONST, isprefixop:true}, | |
{input:'gcd', ptag:'mo', ctag:'gcd', output:'gcd', type:UNARY, sov:true, paren:true}, | |
{input:'lcm', ptag:'mo', ctag:'lcm', output:'lcm', type:UNARY, sov:true, paren:true}, | |
{input:'mean', ptag:'mo', ctag:'mean', output:'mean', type:UNARY, sov:true, paren:true}, | |
{input:'mode', ptag:'mo', ctag:'mode', output:'glb', type:UNARY, sov:true, paren:true}, | |
{input:'min', ptag:'mo', ctag:'min', output:'min', type:UNARY,sov:true, paren:true}, | |
{input:'max', ptag:'mo', ctag:'max', output:'max', type:UNARY, sov:true, paren:true}, | |
//grouping brackets | |
{input:'​', ptag:'mo', output:'\u200B', type:SPACE, invisible:true}, | |
{input:',', ptag:'mo', output:',', type:CONST, sep:true}, | |
{input:'(', ptag:'mo', output:'(', type:LEFTBRACKET}, | |
{input:')', ptag:'mo', output:')', type:RIGHTBRACKET}, | |
{input:'[', ptag:'mo', output:'[', type:LEFTBRACKET}, | |
{input:']', ptag:'mo', output:']', type:RIGHTBRACKET}, | |
{input:'{', ptag:'mo', output:'{', type:LEFTBRACKET}, | |
{input:'}', ptag:'mo', output:'}', type:RIGHTBRACKET}, | |
{input:'|', ptag:'mo', output:'|', type:LEFTRIGHT}, | |
{input:'\\', ptag:'mo', output:'(', type:LEFTBRACKET}, | |
{input:'\u2774', vinput:'(:', ptag:'mo', output:'\u2774', type:LEFTBRACKET}, | |
{input:'\u2775', vinput:':)', ptag:'mo', output:'\u2775', type:RIGHTBRACKET}, | |
{input:'\'', ptag:'mo', ctag:'diff', output:'\'', type:CONST, issuffixop:true}, | |
{input:'int', ctag:'int', ptag:'mo', output:'\u222B', type:UNARY, integral:true}, | |
{input:'d', ctag:'diff', ptag:'mo', output:'d', iscalcop:true, type:DIFFERENTIAL, carry:true}, | |
{input:'dd', ctag:'partialdiff', ptag:'mo', output:'\u2202', iscalcop:true, type:DIFFERENTIAL, carry:true}, | |
{input:'oint', ctag:'int', ptag:'mo', output:'\u222E', type:UNARY, integral:true}, | |
{input:'oo', ctag:'infinity', ptag:'mi', output:'\u221E', type:CONST, sym:true}, | |
{input:'NaN', ctag:'notanumber', ptag:'mi', output:'NaN', type:CONST, sym:true}, | |
{input:'CC', ctag:'complexes', ptag:'mi', output:'\u2102', type:CONST, sym:true}, | |
{input:'NN', ctag:'naturalnumbers', ptag:'mi', output:'\u2115', type:CONST, sym:true}, | |
{input:'QQ', ctag:'rationals', ptag:'mi', output:'\u211A', type:CONST, sym:true}, | |
{input:'RR', ctag:'reals', ptag:'mi', output:'\u211D', type:CONST, sym:true}, | |
{input:'ZZ', ctag:'integers', ptag:'mi', output:'\u2124', type:CONST, sym:true}, | |
{input:'F', ptag:'mi', output:'F', type:VAR, functional:true, ctype:'function',paren:true}, | |
{input:'f', ptag:'mi', output:'f', type:VAR, functional:true, ctype:'function',paren:true}, | |
{input:'g', ptag:'mi', output:'g', type:VAR, functional:true, ctype:'function',paren:true}, | |
{input:'\uff45', ctag:'exp', ptag:'mi', output:'\uff45', type:CONST, eop:true}, | |
{input:'exp', ctag:'exp', ptag:'mi', output:'\uff45', type:VAR, functional:UNARY, paren:true}, | |
{input:'lim', ctag:'limit', ptag:'mo', output:'lim', type:UNDEROVER}, | |
{input:'Lim', ctag:'limit', ptag:'mo', output:'Lim', type:UNDEROVER}, | |
{input:'sin', ctag:'sin', ptag:'mo', output:'sin', type:UNARY, paren:true}, | |
{input:'cos', ctag:'cos', ptag:'mo', output:'cos', type:UNARY, paren:true}, | |
{input:'tan', ctag:'tan', ptag:'mo', output:'tan', type:UNARY, paren:true}, | |
{input:'sinh', ctag:'sinh', ptag:'mo', output:'sinh', type:UNARY, paren:true}, | |
{input:'cosh', ctag:'cosh', ptag:'mo', output:'cosh', type:UNARY, paren:true}, | |
{input:'tanh', ctag:'tanh', ptag:'mo', output:'tanh', type:UNARY, paren:true}, | |
{input:'cot', ctag:'cot', ptag:'mo', output:'cot', type:UNARY, paren:true}, | |
{input:'sec', ctag:'sec', ptag:'mo', output:'sec', type:UNARY, paren:true}, | |
{input:'csc', ctag:'csc', ptag:'mo', output:'csc', type:UNARY, paren:true}, | |
{input:'arcsin', ctag:'arcsin', ptag:'mo', output:'arcsin', type:UNARY, paren:true}, | |
{input:'arccos', ctag:'arccos', ptag:'mo', output:'arccos', type:UNARY, paren:true}, | |
{input:'arctan', ctag:'arctan', ptag:'mo', output:'arctan', type:UNARY, paren:true}, | |
{input:'arccot', ctag:'arccot', ptag:'mo', output:'arccot', type:UNARY, paren:true}, | |
{input:'arcsec', ctag:'arcsec', ptag:'mo', output:'arcsec', type:UNARY, paren:true}, | |
{input:'arccsc', ctag:'arccsc', ptag:'mo', output:'arccsc', type:UNARY, paren:true}, | |
{input:'arcsinh', ctag:'arcsinh', ptag:'mo', output:'arcsinh', type:UNARY, paren:true}, | |
{input:'arccosh', ctag:'arccosh', ptag:'mo', output:'arccosh', type:UNARY, paren:true}, | |
{input:'arctanh', ctag:'arctanh', ptag:'mo', output:'arctanh', type:UNARY, paren:true}, | |
{input:'arccoth', ctag:'arccoth', ptag:'mo', output:'arccoth', type:UNARY, paren:true}, | |
{input:'arcsech', ctag:'arcsech', ptag:'mo', output:'arcsech', type:UNARY, paren:true}, | |
{input:'arccsch', ctag:'arccsch', ptag:'mo', output:'arccsch', type:UNARY, paren:true}, | |
{input:'log', ctag:'log', barge:['_logbase',10], ptag:'mo', output:'log', type:UNARY, paren:true}, | |
{input:'ln', ctag:'ln', ptag:'mo', output:'ln', type:UNARY, paren:true}, | |
{input:'det', ctag:'determinant', ptag:'mo', output:'det', type:UNARY}, | |
//shapes | |
{input:'---', ptag:'mi', output: '\u203e\u203e\u203e', type:SHAPE, shape:'line'}, | |
{input:'\uff3f', vinput:'__', ptag:'mi', output: '\uff3f', type:SHAPE}, | |
{input:'uparrow', vinput:'^|^', ptag:'mi', output:'\u2191', type:SHAPE}, | |
{input:'downarrow', vinput:'.|.', ptag:'mi', output:'\u2193', type:SHAPE}, | |
{input:'-->', ptag:'mi', output:'\u2192', type:SHAPE}, | |
{input:'<--', ptag:'mi', output:'\u2190', type:SHAPE}, | |
{input:'<->', ptag:'mi', output:'\u2194', type:SHAPE}, | |
{input:'==>', ptag:'mi', output:'\u21D2', type:SHAPE}, | |
{input:'<==', ptag:'mi', output:'\u21D0', type:SHAPE}, | |
{input:'<=>', ptag:'mi', output:'\u21D4', type:SHAPE}, | |
{input:'***', ptag:'mi', output:'\u22C6', type:VAR, sym:true}, | |
{input:'diamond', ptag:'mi', output:'\u22C4', type:VAR, sym:true}, | |
{input:'square', ptag:'mi', output:'\u25A1', type:VAR, sym:true}, | |
{input:'sqrt', ctag:'root', barge:['degree',2], ptag:'msqrt', output:'msqrt', type:UNARY, paren:true}, | |
{input:'root', ptag:'mroot', ctag:'root', barge:['degree',1], output:'mroot', type:BINARY, paren:true}, | |
{input:'stackrel', ptag:'mrow', output:'mrow', type:BINARY}, | |
{input:'hat', ptag:'mover', output:'\u005E', type:VAR, acc:true, ctype:'vector'}, | |
//note: https://www.openmath.org/cd/linalg2.html#vector This symbol represents an n-ary function used to construct (or describe) vectors. Vectors in this CD are considered to be row vectors and must therefore be transposed to be considered as column vectors. vs https://www.w3.org/TR/MathML3/chapter4.html#contm.vector For purposes of interaction with matrices and matrix multiplication, vectors are regarded as equivalent to a matrix consisting of a single column, and the transpose of a vector as a matrix consisting of a single row.... Since the W3C spec references the linalg2 cd, treating them as row vectors herein | |
{input:'vec', ptag:'mover', output:'\u2192', type:VAR, acc:true, ctype:'vector'}, | |
{input:'dot', ptag:'mover', output:'.', type:VAR, acc:true}, | |
{input:'ddot', ptag:'mover', output:'..', type:VAR, acc:true}, | |
{input:'ul', ptag:'munder', output:'\u0332', type:VAR, acc:true}, | |
{input:'"', ptag:'mtext', output:'mbox', type:TEXT}, | |
{input:'strike:', ptag:'mstyle', attr:{style:'text-decoration:line-through;'}, output:'strike:', type:STYLE}, | |
{input:'large:', ptag:'mstyle', attr:{mathsize:'large', style:'font-size:150%'}, output:'large:', type:STYLE}, | |
{input:'small:', ptag:'mstyle', attr:{mathsize:'small',style:'font-size:75%'}, output:'small:', type:STYLE}, | |
{input:'light:', ptag:'mstyle', attr:{style:'opacity:0.35'}, output:'light:', type:STYLE}, | |
{input:'hide:', ptag:'mstyle', attr:{style:'opacity:0'}, output:'hide:', type:STYLE}, | |
{input:'bold:', ptag:'mstyle', attr:{fontweight:'bold',style:'font-weight:bold'}, output:'bold:', type:STYLE}, | |
{input:'serif:', ptag:'mstyle', attr:{fontfamily:'sans-serif'}, output:'serif:', type:STYLE}, | |
{input:'struck:', ptag:'mstyle', attr:{mathvariant:'double-struck'}, output:'struck:', type:STYLE}, | |
{input:'script:', ptag:'mstyle', attr:{mathvariant:'script'}, output:'script:', type:STYLE}, | |
{input:'mono:', ptag:'mstyle', attr:{fontfamily:'monospace'}, output:'mono:', type:STYLE} | |
]; | |
mk.getUserAgent = function(){ | |
var isedge, isie; | |
if(!!userAgent){ | |
return userAgent; | |
} | |
isie = /*@cc_on!@*/false || !!mkDoc.documentMode; | |
if(!(isedge = !isie && !!window.StyleMedia) && !!(userAgent = -1!==navigator.userAgent.indexOf('Chrome') ? 'chrome' : (-1!==navigator.userAgent.indexOf('Safari') ? 'safari' : ''))){ | |
return userAgent; | |
} | |
return userAgent = -1!==navigator.userAgent.indexOf('Firefox') ? 'firefox' : ( (-1!==navigator.userAgent.indexOf('MSIE')||!!mkDoc.documentMode) ? 'ie' : (isedge?'edge': ( (-1!==navigator.userAgent.indexOf('Opera') || -1!==navigator.userAgent.indexOf('OPR')) ? 'opera':'other' ) ) ); | |
}; | |
var mk_rand = (window.crypto && window.crypto.getRandomValues) ? function(){ | |
if(0==mkRand.length){ | |
var i=32, a = new Uint32Array(32); | |
window.crypto.getRandomValues(a); | |
while(-1<--i){mkRand.push(a[i]);} | |
} | |
return mkRand.pop(); | |
} : function(){ return Math.floor(Math.random() * Math.floor(Number.MAX_SAFE_INTEGER>>1)); }; | |
var mk_regSig = function(fn){ | |
mkSig[mkHashSum.hash(fn)] = true; | |
if(2==mkDebug){ | |
console.log(mkHashSum.hash(fn)); | |
} | |
return fn; | |
}; | |
var mk_idFromUrl = function(s=''){ | |
var i = 0; | |
return -1!=(i = s.indexOf('#')) ? s.substring(i).replace(/^[\s#]+/ug,'').trim() : ''; | |
}; | |
var mk_isFromMathKey = function(url=null){ | |
var v = 0; | |
return -1==(v=(url = url||location.href).lastIndexOf('?mathKeyCom'+Date.now().toString().slice(0,6))) ? false : url.substring(0, v); | |
}; | |
var mk_mathKeyUrl = function(url,id=''){ | |
var i = -1; | |
if(!!id){ | |
id='#'+id | |
} | |
if(-1!=url.indexOf('?mathKeyCom')){ | |
url=url.replace(/\?mathKeyCom=/ug,'?&mathKeyCom=').replace(/\?mathKeyCom/ug, '%3FmathKeyCom'); | |
} | |
return (-1!=(i=url.indexOf('#')) ? | |
(url.substring(0,i)+'?mathKeyCom'+Date.now()+'mathKey='+url.substring(i).replace(/#+/g,'')):(url+'?mathKeyCom'+Date.now()))+id; | |
}; | |
if(2==mkDebug){ | |
console.log(document.location.href); | |
} | |
(function(){ | |
if(false!==(mkUrl = mk_isFromMathKey())){ | |
if(2==mkDebug){ | |
console.log('c:'+mkUrl); | |
} | |
mkFromMathKey = mk_idFromUrl(document.location.href)||'#'; | |
var fn=null, id = mkFromMathKey, to=mkTo.pmInteractive[0], tm = mkTo.pmInteractive[1], src = null; | |
(fn = function(){ | |
if(0>--to || '|complete|interactive|'.indexOf(document.readyState)){ | |
window.addEventListener('message', function(e){ | |
if(!!src && src!==e.source){ | |
console.log('source mismatch'); | |
return; | |
} | |
var data = null, er = false, msg = null; | |
try{ | |
data = JSON.parse(e.data); | |
msg = !!data.msg ? JSON.stringify(data.msg) : ''; | |
}catch(err){ | |
console.log(err); | |
er = true; | |
} | |
if(!er && !!data.msg.fn){ | |
try{ | |
data.msg.fn = (new Function('return '+data.msg.fn+';'))(); | |
}catch(err){ | |
console.log(err); | |
er = true; | |
} | |
} | |
if(!!er || !id || id!==data.to || !data.sig || !data.msg || !data.msg.sig || !data.msg.fn || !msg || !mkSig[mkHashSum.hash(data.msg.fn)] || data.sig!==mkHashSum.hash(e.origin+"\n"+id+"\n"+msg)){ | |
console.log('pm '+id+' could not decode msg'); | |
window.parent.postMessage({from:id, status:'error'},'*'); | |
return false; | |
} | |
if(!src){ | |
src = e.source; | |
} | |
data.msg.fn({args: (!!data.msg.args?data.msg.args:{}), doc:document, id:id, cb: function(res){if(!!res && !!res.status){res.from=id; src.postMessage(res,'*');}else{console.log('pm '+id+' invalid return');console.log(res);} window.stop();}, path:'pm', res: ''}); | |
}, false); | |
window.parent.postMessage({from:id, status:'loaded'},'*'); | |
return; | |
} | |
window.setTimeout(fn, tm); | |
})(); | |
}else{ | |
mkUrl = document.location.href; | |
if(2==mkDebug){ | |
console.log('p:'+mkUrl); | |
} | |
window.addEventListener('message', function(e){ | |
var f = null; | |
if(!e.data || !e.source || !e.data.from || !e.data.status || !(f=mkFrames[e.data.from]) || !!f.error.pm || (!!f.src && f.src !== e.source)){ | |
return f.onerror('pm', 'child returned invalid data'); | |
} | |
if(!f.src){ | |
f.src = e.source; | |
} | |
f.active.pm=0; | |
if('error'===e.data.status){ | |
return f.onerror('pm'); | |
} | |
f.oncancel('pm'); | |
if('loaded'===e.data.status){ | |
f.onloaded('pm'); | |
}else if('completed'===e.data.status){ | |
f.oncompleted('pm', e.data); | |
} | |
}); | |
} | |
})(); | |
var mk_trimUrl = function(url=''){ | |
return url.replace(/(?:\s+[0-9]+\.?[0-9]*\s*[whx])+\s*$/uig,' ').trim(); | |
}; | |
var mk_isValidUrl = function(url=''){ | |
var er = false, pre = '', i=0; | |
url = mk_trimUrl(url); | |
if('about:blank'===url){ | |
return {}; | |
} | |
//todo: | |
if(!url || (-1==url.indexOf('/') && -1==url.indexOf('.') && -1==url.indexOf('#'))){ | |
return false; | |
} | |
if(!url.match(/^[a-zA-Z0-9]+\:/)){ | |
++i; | |
pre+=location.protocol; | |
} | |
if(!((pre+url).match(/^[a-zA-Z0-9]+\:\/\//))){ | |
++i; | |
pre+='//'; | |
} | |
if(!((pre+url).match(/^[a-zA-Z0-9]+\:\/\/(?:[a-zA-Z0-9][a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]+(?:\:[0-9]+)?(?:\?|\/|$)/))){ | |
++i; | |
pre+=document.location.host; | |
if(0!=url.indexOf('/')){ | |
pre+='/'; | |
} | |
} | |
try { | |
er = new URL(pre+url); | |
} catch(err) { | |
console.log(err); | |
} | |
//todo: multilingual, only perform this check if the input url is substantially ascii | |
if(!!er){ | |
if(0.5>(1-(0<i ? __levenshtein(url=pre+url,er.href) : __levenshtein(url,er.href))/url.length)){ | |
return false; | |
} | |
} | |
return er; | |
}; | |
var mk_frame = function(url, fn=null, ld=null, cb=null, ka=false){ | |
var args = {}, k =''; | |
if('object' == typeof fn){ | |
if(!!fn.args){ | |
args = fn.args; | |
} | |
fn = !!fn.fn ? fn.fn : null; | |
} | |
if(!!fn && ('function' != typeof fn || !mkSig[k=mkHashSum.hash(fn)])){ | |
console.log('could not frame '+url); | |
return false; | |
} | |
var id = mkHashSum.hash(Date.now()+mk_rand()), data=null; | |
if(!(data=mk_isValidUrl(url)) || !!mkFrameUrl[url]){ | |
return false; | |
} | |
mkFrameUrl[url] = id; | |
var f, doc = (!!mkDoc&&!!mkDoc.body) ? mkDoc : document; | |
data = data.pathname; | |
mkFrames[id] = { | |
active: {}, | |
cancelfrom: 'xhr', | |
cp:false, | |
cb: cb, | |
completed:false, | |
doc: doc, | |
data: null, | |
error:{}, | |
file:data, | |
fn: fn, | |
id: id, | |
if:doc.createElement('iframe'), | |
ka:ka, | |
ld:ld, | |
loaded:false, | |
started:false, | |
fto:mkTo.fDoc[1], | |
res:'', | |
src:null, | |
url:[url] | |
}; | |
f = mkFrames[id]; | |
f.data = { to: id, msg: {sig: k, args: args, fn: !!fn ? fn.toString():null} } | |
f.data.sig = mkHashSum.hash(window.location.origin+"\n"+id+"\n"+JSON.stringify(f.data.msg)); | |
if(2==mkDebug){ | |
f.log = function(msg=''){ | |
console.log((!!mkFromMathKey?'*':'')+'['+f.id+'] '+msg+' @'+Math.round(parseFloat((Date.now()-f.started)/1000),1)+'s'); | |
}; | |
} | |
f.if.style = 'display:none;'; | |
f.oncompleted = function(path='',d=null){ | |
if(2==mkDebug){ | |
f.log('oncompleted '+path+': '+f.url[0]); | |
} | |
if('xhr'!=path){ | |
var w = ''; | |
if(!!(w=(f.if.contentDocument || f.if.contentWindow.document)) && !!(w.contentWindow)){ | |
w.contentWindow.stop(); | |
} | |
} | |
if(!f.oncancel(path)){ | |
return; | |
} | |
if(!!f.completed){ | |
console.log('already completed'); | |
return; | |
} | |
if(!!d && !!d.msg && undefined!==d.msg.keepalive){ | |
f.ka = !!d.msg.keepalive; | |
} | |
if(!!f.cb){ | |
f.cb(d); | |
} | |
f.completed = Date.now(); | |
if(!f.ka){ | |
f.doc.body.removeChild(f.if); | |
delete mkFrameUrl[f.url.pop()]; | |
delete mkFrames[f.id]; | |
} | |
}; | |
f.onloaded = function(path=''){ | |
if(2==mkDebug){ | |
f.log('onloaded '+path+': '+f.url[0]); | |
} | |
if(!f.oncancel(path)){ | |
return; | |
} | |
for(var i in f.active){ | |
window.clearTimeout(f.active[path]); | |
delete f.active[path]; | |
} | |
if(!!f.ld && !f.loaded){ | |
f.ld(); | |
} | |
f.loaded = Date.now(); | |
if('pm'==path){ | |
if(!!f.src){ | |
f.src.postMessage(JSON.stringify(f.data),'*'); | |
}else{ | |
console.log('no src for '+id); | |
} | |
}else if(!!f.fn){ | |
fn({args:f.data.msg.args, cb:function(d){ f.oncompleted(path, d); }, doc: (f.if.contentDocument || !!f.if.contentWindow?f.if.contentWindow.document:null), id:id, path:path, res:f.res}); | |
} | |
}; | |
f.onerror = function(path='',msg=''){ | |
f.error[path] = true; | |
if(undefined!==f.active[path]){ | |
window.clearTimeout(f.active[path]); | |
delete f.active[path]; | |
} | |
if(2==mkDebug){ | |
f.log('error loading '+path); | |
} | |
if(!!msg){ | |
console.log(msg); | |
} | |
if(!f.oncancel(path)){ | |
return; | |
} | |
f.cancelfrom = 'xhr'; | |
f.retry(path); | |
}; | |
f.oncancel = function(path=''){ | |
if(2==mkDebug){ | |
f.log('cancel from '+path+' [current:'+f.cancelfrom+']'); | |
} | |
if(undefined!==f.active[path]){ | |
window.clearTimeout(f.active[path]); | |
delete f.active[path]; | |
} | |
if('pm'==path){ | |
return !f.error[path] ? f.cancelfrom = path : false; | |
} | |
if('pm'==f.cancelfrom){ | |
return !f.error[path]&&path==f.cancelfrom; | |
} | |
if('frame'==path){ | |
return !f.error[path] ? f.cancelfrom = path : false; | |
} | |
if('frame'==f.cancelfrom){ | |
return !f.error[path]&&path==f.cancelfrom; | |
} | |
return !f.error[path]&&path==f.cancelfrom; | |
}; | |
f.retry = function(path=''){ | |
if(2==mkDebug){ | |
f.log('retry from: '+path); | |
} | |
if(undefined!==f.active.xhr || !!f.res){ | |
window.clearTimeout(f.active.xhr); | |
delete f.active.xhr; | |
if(!f.error.xhr){ | |
if(2==mkDebug){ | |
f.log('setup long xhr read'); | |
} | |
f.active.xhr = window.setTimeout(function(){f.onloaded('xhr');},mkTo.wfpmInteractive[0]); | |
} | |
} | |
if(!f.error.frame){ | |
if(2==mkDebug){ | |
f.log('retrying frame'); | |
} | |
if(undefined!==f.active.frame){ | |
window.clearTimeout(f.active.frame); | |
delete f.active.frame; | |
} | |
var m = null; | |
if(!!(m=f.file.match(/\.((?:png)|(?:jpe?g)|(?:gif)|(?:svg)|(?:bmp)|(?:tiff))$/i))){ | |
var img = new Image(); | |
img.style='display:none;'; | |
img.contentType='image/'+m[1].toLowerCase(); | |
//TODO: | |
//img.crossOrigin = 'anonymous'; | |
img.onload = function(){ | |
if(undefined!==f.active.frame){ | |
window.clearTimeout(f.active.frame); | |
delete f.active.frame; | |
} | |
f.res = this; | |
f.onloaded(f.cancelfrom='img'); | |
}; | |
img.src = f.url[f.url.length-1]; | |
return; | |
} | |
f.fto = mkTo.fDoc[1]*4; | |
f.load(); | |
}else if(!f.error.xhr&&!f.res){ | |
if(2==mkDebug){ | |
f.log('retrying xhr'); | |
} | |
f.fto = -1; | |
f.load(); | |
} | |
}; | |
f.load = function(){ | |
if(!f.started){ | |
f.started = Date.now(); | |
f.if.src = 'about:blank'; | |
f.doc.body.appendChild(f.if); | |
(f.if.src = mk_mathKeyUrl(f.url[0], f.id)), (f.url.unshift(f.if.src)); | |
if(undefined!==f.active.frame){ | |
window.clearTimeout(f.active.frame); | |
} | |
return f.active.iframe=window.setTimeout(function(){f.load();},mkTo.fStart[0]); | |
} | |
//TODO: dns prefetch, parallel xhr/iframe responsive to errors in former, recursive requests via `grep -n mkFromMathKey | tail -n 1` | |
var d = null; | |
if(!!f && !!f.if && !!(d=(f.if.contentDocument || !!f.if.contentWindow?f.if.contentWindow.document:null)) && 'about:blank'!=d.location.href){ | |
if(2==mkDebug){ | |
f.log('frame contentDocument'); | |
} | |
if(undefined!==f.active.frame){ | |
window.clearTimeout(f.active.frame); | |
} | |
f.active.iframe = 0; | |
if(!d.location || !d.documentElement){ | |
return f.onerror('frame'); | |
}else if(!f.oncancel('frame')){ | |
return; | |
} | |
if(2==mkDebug){ | |
f.log('frame contentElement'); | |
} | |
if(d.location.href.replace(/[#?].*$/g,'')!==f.url[0].replace(/[#?].*$/g,'')){ | |
f.url.unshift(d.location.href); | |
} | |
f.onloaded('frame'); | |
}else if(!!f && -1<--f.fto){ | |
if(mkTo.fDoc[1]==f.fto){ | |
return f.onerror('frame'); | |
} | |
if(2==mkDebug){ | |
if((mkTo.fDoc[1]-1)==f.fto){ | |
f.log('frame sequence'); | |
} | |
} | |
if(undefined!==f.active.frame){ | |
window.clearTimeout(f.active.frame); | |
} | |
f.active.iframe=window.setTimeout(f.load(), mkTo.fDoc[0]); | |
}else{ | |
if(2==mkDebug){ | |
f.log('xhr request'); | |
} | |
if(undefined!==f.active.xhr){ | |
f.active.xhr = 0; | |
} | |
if(!f || !f.oncancel('xhr')){ | |
return; | |
} | |
d = new XMLHttpRequest(); | |
d.open('GET', f.url[0], true); | |
d.responseType = 'text'; | |
d.onerror = function(e) { | |
f.onerror('xhr'); | |
f.retry('xhr'); | |
}; | |
d.onload = function(e) { | |
if(2==mkDebug){ | |
f.log('xhr loaded'); | |
} | |
if(!f||!f.oncancel('xhr')||!!f.res){ | |
return; | |
} | |
if(!e || !e.target){ | |
if(!!f){ | |
f.onerror('xhr','target undefined'); | |
f.retry('xhr'); | |
} | |
return; | |
} | |
var res = e.target.response || ''; | |
if(!res || !!res.replace(/[\r\n]/ugm,' ').match(/src\s*=\s*['"][^'"]*mathKey.js/u) || (!!(d=(f.if.contentDocument || f.if.contentWindow?f.if.contentWindow.document:null)) && !!d.documentElement && !(f.error['frame']=false))){ | |
if(2==mkDebug){ | |
f.log('xhr response may be for pm'); | |
} | |
f.res = res; | |
f.active.xhr=0; | |
f.retry('xhr'); | |
return; | |
} | |
if(2==mkDebug){ | |
f.log('xhr response'); | |
} | |
f.res = res; | |
f.onloaded('xhr'); | |
}; | |
d.send(); | |
} | |
}; | |
f.load(); | |
return true; | |
}; | |
mk.reInit = function(){ | |
mkRnames = {}; | |
mkNames = {}; | |
mkRLength = 0; | |
mkSymbols = mkSymbols.filter(function(v){ return v !== null; }); | |
mkSymbols.filter(function(v,i){ | |
var j = -1, k = -1; | |
k = v.input.length; | |
if(mkRLength<k){ | |
mkRLength = k; | |
} | |
while(k>++j){ | |
mkRnames[v.input.slice(-j)] = true; | |
} | |
mkNames[v.input] = i; | |
return false; | |
}); | |
}; | |
var mk_fallbackCss = function(){ | |
if(!!conf.fallbackcssurl && !mkFromMathKey){ | |
mk_frame(conf.fallbackcssurl, {args:{fallbackurl: conf.fallbackcssurl}, fn: mk_regSig(function(com){ | |
var css = (!!com.doc && !!com.doc.body)? com.doc.body.innerText : (!!com.res? com.res : ''); | |
com.cb({status:'completed',msg:{cssText:css}}); | |
})}, function(){mkSync.mathKeyTranslate=true;}, function(data){if(!!data && !!data.msg &&!!data.msg.cssText && (!mkHasMathML || 1===mkHasMathML)){mkFallbackCSS=data.msg.cssText;} mkSync.mathKeyTranslate=false;}); | |
} | |
}; | |
mk_fallbackCss(); | |
var mk_checkMathML = function(mj=false, mp=false){ | |
mkHasMathML = false; | |
if ('firefox'==userAgent || 'safari'==userAgent){ | |
mkHasMathML = true; | |
mkFallbackCSS=''; | |
}else if ('opera'==userAgent && 9.5<=parseFloat(navigator.appVersion.slice(0,1))){ | |
mkHasMathML = true; | |
mkFallbackCSS=''; | |
}else if ('other'==userAgent && 'Netscape'==navigator.appName.slice(0,8) && 5<=parseFloat(navigator.appVersion.slice(0,1))){ | |
mkHasMathML = true; | |
mkFallbackCSS=''; | |
} | |
if (!mkHasMathML && !!mkDoc.createElementNS) { | |
var d = null, div = mkDoc.createElement('div'), mfrac = null; | |
div.style.position = 'absolute'; | |
div.style.top = 0; | |
div.style.left = 0; | |
div.style.opacity = '0'; | |
div.style.width = 'auto'; | |
div.style.height = 'auto'; | |
div.style.fontFamily = 'serif'; | |
div.style.lineheight = 'normal'; | |
div.appendChild( | |
mfrac = mkDoc.createElementNS(mkMathns,'math') | |
); | |
mfrac.appendChild(mkDoc.createElementNS(mkMathns,'mfrac')); | |
mfrac = mfrac.children[0]; | |
mfrac.appendChild( | |
d = mkDoc.createElementNS(mkMathns,'mi') | |
); | |
d.appendChild(mkDoc.createTextNode('xx')); | |
mfrac.appendChild( | |
d = mkDoc.createElementNS(mkMathns,'mi') | |
); | |
d.appendChild(mkDoc.createTextNode('yy')); | |
mkDoc.body.appendChild(div); | |
mkHasMathML = div.offsetHeight > div.offsetWidth; | |
mkDoc.body.removeChild(div); | |
} | |
if (!mkHasMathML && !mp && 'ie'==userAgent){ | |
if(!mj && !mkDoc.getElementById('mathplayer')){ | |
var head = mkDoc.getElementsByTagName('head')[0], object = mkDoc.createElement('object'); //script = mkDoc.createElement('script'); | |
object.id = 'mkmathplayer'; | |
object.onload = function(){mk_checkMathML(true);}; | |
object.classid = 'clsid:32F66A20-7614-11D4-BD11-00104BD3F987'; | |
head.innerHTML+='<?import namespace="m" implementation="#mathplayer"?>'; | |
//mkDoc.namespaces.add("m","http://www.w3.org/1998/Math/MathML"); | |
// mkDoc.namespaces.m.doImport("#mathplayer"); | |
head.appendChild(object); | |
return; | |
}else if (!!mkDoc.getElementById('mkmathplayer')){ | |
var ActiveX = null; | |
try { | |
ActiveX = new ActiveXObject('MathPlayer.Factory.1'); | |
mkHasMathML = true; | |
mkFallbackCSS=''; | |
} catch (e) { | |
mkHasMathML = false; | |
mj = false; | |
} | |
} | |
} | |
if(!mkHasMathML && !mp && !conf.fallbackliburl && !!conf.fallbackcssurl){ | |
var head = mkDoc.getElementsByTagName('head')[0], | |
link = mkDoc.createElement('link'); | |
link.setAttribute('rel','stylesheet'); | |
link.setAttribute('type','text/css'); | |
head.appendChild(link); | |
link.href = conf.fallbackcssurl; | |
mkHasMathML = 1; | |
mkFallback = conf.fallbackcssurl.replace(/^.*\//g,''); | |
} | |
if (!!mj && !mkHasMathML && !!conf.fallbackliburl){ | |
if(conf.fallbackliburl.match(/MathJax[^\/]*(?:\/|(?:\.js))/i) && undefined!=window['MathJax'] && !!window['MathJax'].Hub) { | |
mkHasMathML = 2; | |
mkFallbackCSS=''; | |
}else if(conf.fallbackliburl.match(/(?:(?:wiris)|(?:quizzes))[^\/]*(?:\/|(?:\.js))/i) && !!window['com'] && !!window['com'].wiris){ | |
mkHasMathML = 3; | |
mkFallbackCSS=''; | |
} | |
} | |
if(!mj && !mkHasMathML){ | |
var head = mkDoc.getElementsByTagName('head')[0]; | |
if(conf.fallbackliburl.match(/MathJax[^\/]*(?:\/|(?:\.js))/i)){ | |
var scripts = [ mkDoc.createElement('script'), mkDoc.createElement('script')], | |
xconfig = 'MathJax.Hub.Config({' + | |
'mml2jax: {preview: "mathml"},' + | |
'extensions: ["mml2jax.js"],' + | |
'jax: ["input/MathML","output/HTML-CSS"]' + | |
'});'; | |
scripts[0].type = 'text/x-mathjax-config'; | |
(userAgent !== 'opera') ? scripts[0].text = xconfig : scripts[0].innerHTML = xconfig; | |
head.appendChild(scripts[0]); | |
mkFallback = 'MathJax'; | |
scripts[1].async = true; | |
scripts[1].type = 'text/javascript'; | |
scripts[1].onload = function(){ | |
var fn = function(){ | |
(!window['MathJax']||!window['MathJax'].isReady)? window.setTimeout(fn,500):mk_checkMathML(true, userAgent==='ie'); | |
}; | |
fn(); | |
}; | |
scripts[1].src = conf.fallbackliburl+'?config=MML_HTMLorMML'; | |
head.appendChild(scripts[1]); | |
return; | |
}else if(conf.fallbackliburl.match(/(?:(?:wiris)|(?:quizzes))[^\/]*(?:\/|(?:\.js))/i)){ | |
var script = mkDoc.createElement('script'); | |
script.type = 'text/javascript'; | |
script.onload = function(){ | |
var fn = function(){ | |
(!window['com']||!window['com'].wiris)? window.setTimeout(fn,500):mk_checkMathML(true, userAgent==='ie'); | |
}; | |
fn(); | |
}; | |
mkFallback = 'WIRIS'; | |
script.src = conf.fallbackliburl; | |
head.appendChild(script); | |
return; | |
} | |
} | |
if (!mkHasMathML) { | |
mkFallback = 'none'; | |
if(!!conf.notifyifnomathml){ | |
mk_warn('ASCIIMathML is unavailable. Please upgrade your browser.'); | |
} | |
return false; | |
} | |
if(!!mj && !!conf.translateonstart){ | |
mk_translateOnStart(); | |
} | |
return (mkReady = true); | |
}; | |
var mk_fallback = function(o=null,r=false){ | |
var fn = (!!o&&!!r) ? function(){ var c,i,v=o.getElementsByTagName('mo');for(i in v){ if('*'==v[i].innerHTML && (!v[i].previousElementSibling || 'mspace'!=mk_nodeName(v[i].previousElementSibling)) && (!v[i].nextElementSibling || ('mspace'!=mk_nodeName(v[i].nextElementSibling) | |
&& (!(c=v[i].nextElementSibling.textContent.charAt(0).toLowerCase()) || '0'>c || c>'9' ) | |
)) ){v[i].innerHTML='⁢<!-- ⁢ -->';} } } : function(){}; | |
if(2===mkHasMathML && !!window['MathJax']){ | |
fn(); | |
window['MathJax'].Hub.Queue(['Typeset',window['MathJax'].Hub,o]); | |
}else if(3===mkHasMathML){ | |
fn(); | |
var builder, uibuilder, viewer, toFilter, tag, i, m; | |
builder = window['com'].wiris.quizzes.api.QuizzesBuilder.getInstance(); | |
uibuilder = builder.getQuizzesUIBuilder(); | |
viewer = uibuilder.getMathViewer(); | |
viewer.setZoom(2.0); | |
toFilter = 'math'==mk_nodeName(o)? [o] : o.getElementsByTagName('math'); | |
m=toFilter.length; | |
for(i=-1; m>++i;){ | |
//todo: filter on class mathMLPresentation added to presentation nodes | |
if(!toFilter[i] || !toFilter[i].getAttribute('alttext')){ | |
continue; | |
} | |
tag = document.createElement('span'); | |
toFilter[i].insertAdjacentElement('beforebegin',tag); | |
tag.appendChild(toFilter[i]); | |
viewer.filter(tag); | |
} | |
}else{ | |
window.setTimeout(fn,1000); | |
} | |
}; | |
mk.usingFallback = function(){ | |
return mkFallback; | |
}; | |
mk.translate = function(translateclass=''){ | |
if (mkTranslated<(Date.now()-1000)) { | |
mkTranslated = Date.now(); | |
var i=-1, j = -1, m = null, obj = null; | |
if(!translateclass){ | |
translateclass=conf.translateclass; | |
} | |
translateclass = translateclass.replace(/^\./g,''); | |
if(!mkDoc.querySelector('.'+translateclass)){ | |
console.log('Could not find an element of class '+translateclass); | |
return; | |
} | |
mkSync.mathKeyTranslate=true; | |
obj = mkDoc.body.getElementsByClassName(translateclass); | |
for(i=-1;obj.length>++i;){ | |
if(!obj[i].firstChild || 'math'==mk_nodeName(obj[i].firstChild)){ | |
continue; | |
} | |
if(!!(m = mk.parseR(obj[i].innerHTML))){ | |
if(!obj[i]){ | |
console.log('no object:'); | |
console.log(m); | |
continue; | |
} | |
if(-1<(j = obj[i].childNodes.length-1)){ | |
while(-1<--j){ | |
obj[i].removeChild(obj[i].childNodes[0]); | |
} | |
obj[i].replaceChild(m,obj[i].childNodes[0]); | |
}else{ | |
obj[i].appendChild(m); | |
} | |
} | |
mkExt.cache[0]=''; | |
mkExt.cache[1]=''; | |
} | |
mkSync.mathKeyTranslate=false; | |
mkDoc.dispatchEvent(new CustomEvent('mktranslated')); | |
mk_fallback(mkDoc.body); | |
} | |
}; | |
var mk_warn = function(warnings='') { | |
if(!warnings && 0==errorBuf.length){ | |
return; | |
} | |
if(!Array.isArray(warnings)){ | |
warnings = [warnings]; | |
} | |
if(0==warnings.length){ | |
warnings = errorBuf.map(mk_acopy); | |
}else if(errorBuf.length>0){ | |
warnings = warnings.concat(errorBuf); | |
} | |
errorBuf.splice(0); | |
if(!!conf.notifywarnings){ | |
(!!conf.notifywarningscallback && !!window[conf.notifywarningscallback])? | |
window[conf.notifywarningscallback](warnings) : alert(warnings.join('; ')); | |
} | |
}; | |
var mk_setCurrentSymbol = function(t,s,passive=false){ | |
if(!t.par){ | |
t.cs = s; | |
} | |
if(!passive){ | |
t.par = false; | |
} | |
}; | |
var mk_spaces = function(str,i,p,cs,ls){ | |
var c = '', n=!!mkTsp[p]?mkTsp[p]:0.5, sp = '', sn=''; | |
while(' '==(c=str.charAt(i)) || '\n'==c){ | |
++i; | |
} | |
while(-1<--i && ((' '==(c=str.charAt(i))&&(sp+=c)) || ('\n'==c&&(sn+=c)))); | |
if(!sp&&!!sn){ | |
sp=' '; | |
} | |
return [Math.round(sp.length*n),(1>cs?Math.abs(cs):0),Math.round(sn.length*n),1>ls?Math.abs(ls):0]; | |
}; | |
var mk_isNeg = function(str,k){ | |
var i = k, s = '', c=''; | |
if('-'!=str.charAt(i)){ | |
'-'==str.charAt(i-1) ? --i : ++i; | |
} | |
if(1>i){ | |
return true; | |
} | |
k = i; | |
while((-1<--i || (k=0)) && (!!s || ((33>(c=str.charCodeAt(i)))&&10!=c&&0<--k) || (s=str.charAt(i))) && (!s || !!mkRnames[str.substring(i, k)] || -1==++i)); | |
return (('A'<=s && s<='Z') || ('a'<=s && s<='z') || ('0'<=s && s<='9') || (i!=k && -1==',{(^_'.indexOf(s) && undefined!=(i=mkNames[str.substring(i, k)]) && !!(s=mkSymbols[i]) && !(INFIX==s.type || LEFTBRACKET==s.type || !!s.arith || !!s.setop || !!s.rel || !!s.eq || !!s.isprefixop) )) ? false : true; | |
}; | |
var mk_isComma = function(s,str,i,n=0){ | |
if(1>i||','!=s || 3!=n|| '0'>(s=str.charAt(--i)) || s>'9'){ | |
return false; | |
} | |
var p = {}; | |
while(-1<--i){ | |
n = s; | |
s=str.charAt(i); | |
if(','==s && ('0'>n || n>'9' || (0<i && ('0'>(n=str.charAt(i-1)) || n>'9'))) ){ | |
return false; | |
}else if(!!mkParen[0][s]){ | |
if(!p[mkParen[0][s]]){ | |
return false; | |
} | |
}else if(!!mkParen[1][s]){ | |
!p[s]?p[s]=1:p[s]++; | |
} | |
} | |
return true; | |
}; | |
var mk_continueSymbol = function(str='', hint=null, r=0){ | |
var a = '', i = 0, sym = '', strl, def=false, mode = true; | |
if(null!=hint){ | |
hint = hint.toLowerCase(); | |
}else{ | |
str=str.trimRight(); | |
mode = false; | |
} | |
strl = str.length; | |
i = strl-1; | |
if(4096<r){ | |
console.log('recursion'); | |
return; | |
} | |
while((!!mkRnames[str.slice(i,strl)] || -1==++i) && (-1<--i || (i=0))); | |
sym = str.slice(i,strl); | |
a = str.charAt(--strl).toLowerCase(); | |
if(!mode){ | |
return '\u200c'==a ? null : (undefined!=mkNames[sym] ? -i : strl); | |
} | |
if(undefined==mkNames[sym]){ | |
if(('a'>a||a>'z') && ('0'>a||a>'9')){ | |
return null; | |
}else if((0<strl && -1!='\/_^'.indexOf(def=str.charAt(strl-1)))||(1<strl && '-'==def && -1!='\/_^'.indexOf(str.charAt(strl-2)))){ | |
return 0; | |
}else{ | |
def = true; | |
} | |
} | |
if(undefined==(sym=mkNames[sym]) || 1>i){ | |
return (!!(sym = mkSymbols[sym]) && DIFFERENTIAL!=sym.type && !!sym.input&& 2>sym.input.length && (('a'<=a&&a<='z')|| ('0'<=a&&a<='9'))) ? true : (undefined==sym ? true : ((!!sym.sym&&VAR==sym.type) ? 0 : (!!sym.attr? '' : false))); | |
} | |
sym = mkSymbols[sym]; | |
a = str.charAt(i-1).toLowerCase(); | |
hint = hint.charAt(0); | |
if(DIFFERENTIAL==sym.type){ | |
if((-1=='^({[\\/'.indexOf(hint) && ('a'>hint||hint>'z')) || -1!='^_'.indexOf(a) ){ | |
return true; | |
} | |
if('a'<=a&&a<='z' || '0'<=a&&a<='9'){ | |
while(-1<--i && !!(hint=(a=str.charAt(i))+hint) && (undefined==mkNames[hint] && | |
('d'!=a || ( | |
(-1=='^({[\\/'.indexOf(a=str.charAt(i+1).toLowerCase())&&('a'>a||a>'z')) | |
|| | |
(0!=i && ('d'!=str.charAt(i-1)||(0!=--i&&(hint=a+hint))) && -1!='^_'.indexOf(str.charAt(i-1))) | |
)))); | |
if(undefined==mkNames[hint] && 'd'!=a && -1==str.indexOf('int') ){ | |
return true; | |
} | |
} | |
return false; | |
} | |
if(2>sym.input.length && 'a'<=sym.input.toLowerCase()<='z' && (('a'<=hint&&hint<='z')|| ('0'<=hint&&hint<='9')|| ('0'<=a&&a<='9') )){ | |
if(!r){ | |
return true; | |
} | |
def = true; | |
} | |
if(!!sym.attr){ | |
return ''; | |
}else if(!!sym.sym&&VAR==sym.type){ | |
return 0; | |
} | |
if(!!r && (!!sym.arith || !!sym.sep || !!sym.eq || INFIX<= sym.type && sym.type<=SHAPE || !!sym.rel || !!sym.logic || !!sym.setop || !!sym.issuffixop || !!sym.isprefixop || -1!=sym.input.indexOf('\\') | |
)){ | |
return 1===r ? false : undefined; | |
} | |
if(undefined==(a = mk_continueSymbol(str.slice(0,i),sym.input,++r)) && !sym.acc){ | |
return def; | |
} | |
return true===a || (!!sym.acc?null:false); | |
}; | |
var mk_cloneSymbol = function(sym={},s={}) { | |
if(undefined!=sym.charAt||undefined!=sym.toPrecision||true===sym||false===sym){ | |
return sym; | |
} | |
for(var i in sym){ | |
s[i] = (!sym[i] || undefined!=sym[i].charAt||undefined!=sym[i].toPrecision||true===sym[i]||false===sym[i])?sym[i]:mk_cloneSymbol(sym[i],!!sym[i].length?[]:{}); | |
} | |
return s; | |
}; | |
mkBak = mkSymbols.map(function(v,i){mkBakNames[v.input]=i; return mk_cloneSymbol(v);}); | |
var mk_findStackOrigin = function(str=''){ | |
var i, k=str.length, c =0, s='', r=-1; | |
i = k; | |
while(-1<--i && (!!s || ((33>(c=str.charCodeAt(i))||10==c)&&-1<--k) || !!(s=1)) && (!s || !!mkRorigin[str.slice(i,k)])); | |
r = (!!mkOrigin[str.slice(++i,k)]) ? i : -1; | |
if(-1<r){ | |
if(!!mk_continueSymbol(str.slice(0,r), str.charAt(r),1)){ | |
return -1; | |
} | |
i = k = r-1; | |
s = ''; | |
while(-1<--i && (!!s || ((33>(c=str.charCodeAt(i))||10==c||'"'==c||'('==c)&&-1<--k) || !!(s=1)) && (!s || -1!='stackrel'.indexOf(str.slice(i,k)) )); | |
if('stackrel'==str.slice(++i,k)){ | |
r = -1; | |
} | |
} | |
return r; | |
}; | |
//return maximal final substring of str that appears in names | |
var mk_rgetSymbol = function(str,spa=[0,0,0,0],literal=false,t=null) { | |
var f=false,i=0, j=true, k=0, ptag='',b=null,c='', s='', strl = str.length, sl=spa[2], sp = spa[0], bb=[]; | |
if(!t){ | |
t = mk_t(); | |
} | |
i = strl-1; | |
while(( ((' '==(s=str.charAt(i))&&(undefined!=--sp)) || ('\n'==s&&(undefined!=--sl))) || 33>s.charCodeAt(0) || '\u200b'==s) && -1<--i); | |
if(0>(strl=i) || ''===literal || '\u200c'==str || '\u200b'==str){ | |
t.par = false; | |
return {symbol: mk_cloneSymbol(mkSymbolNone), pos:i, text:''}; | |
} | |
++strl; | |
if(!t.par){ | |
t.ps = t.cs; | |
} | |
if(!literal && -1!=(j=mk_findStackOrigin(str))){ | |
s = str.slice(j,strl); | |
if(!!(b = mk_rgetSymbol(str.slice(0, j), sp)) && !!b.symbol.acc){ | |
mk_setCurrentSymbol(t,b.symbol.type, true); | |
return {symbol: mk_cloneSymbol(b.symbol,{multi:true}), pos:b.pos, text:[{input:s, ptag:'mi', ctag:'ci', text:s}], sp:b.sp, origin:s}; | |
} | |
return {symbol: {input:s, ptag:'mi', ctag:'ci', output:s, type:VAR, mys:true}, pos:j, text:s, sp:mk_spaces(str,j,ptag,sp,sl), origin:s}; | |
} | |
j = true; | |
if( (null===(i = mk_continueSymbol(str)) || !!literal) ? (f=true) : (0<i || undefined==(k=mkNames[str.slice(i=-i,strl)]) || !(k = mkSymbols[k])|| (VAR==k.type && !!k.sym && 0!==literal) || (0!==literal && 0<(j=t.o.length-1) && RIGHTBRACKET==t.o[j].symbol.type && 2>k.input.length && 'a'<=k.input && 'z'>=k.input && (literal=true) ) ) ){ | |
if(0===literal){ | |
t.par = false; | |
return {symbol: mk_cloneSymbol(mkSymbolNone), pos:i, text:''}; | |
} | |
if(null==i){--strl}; | |
s = str.charAt(--strl); | |
mk_setCurrentSymbol(t,VAR, true); | |
t.par = t.par? false : true; | |
b = {}; | |
i=strl; | |
if(!!k&&!literal){ | |
++i; | |
ptag=k.ptag; | |
s = k.output; | |
bb[0] = mk_cloneSymbol(k); | |
bb[0].text=''; | |
}else { | |
if('0'<=s && s<='9'){ | |
ptag = 'mn'; | |
j=true; | |
while(-1<--i && (s = str.charAt(i)) && ((('0'<=s && s<='9' && ++c)||(mk_isComma(s,str,i,c)?!(c=j=0):(c=0))) ? j||true : ((j && '.'==s && 0<i && '0'<=(s=str.charAt(i-1))&& s<='9') ? !(j=c) : false))); | |
j = false; | |
if('-'==s){ | |
if(!!(j = !mk_isNeg(str,k=i))){ | |
s=''; | |
} | |
} | |
s = str.slice(('-'==s&&!j)?i:++i); | |
}else if((ptag = ((('A'>s || s>'Z') && ('a'>s || s>'z') && (!!mkRnames[s] || s.charCodeAt(0)<300)) ?'mo':'mi'))=='mi' && 0<(strl-1) && '-'==str.charAt(strl-1) && !!mk_isNeg(str,i-1)){ | |
s='-'+s; | |
--i; | |
} | |
s = s.trim(); | |
b.text = s; | |
} | |
j = false; | |
b.pos=i; | |
b.sp=mk_spaces(str,i,ptag,sp,sl); | |
c = ptag; | |
if(t.par && ('mo'!=ptag||!!literal) && 1>b.sp[0] && '-'!=s){ | |
if(!k){ | |
bb.unshift({ptag:c,text:s, sp: b.sp, output:s, pos:i}); | |
} | |
k = false; | |
while(!!(b = mk_rgetSymbol(str.slice(0, b.pos), b.sp, (!!literal || mk_continueSymbol(str.slice(0,b.pos), str.charAt(b.pos),1)), t)) && -1<(b.pos=Math.abs(b.pos)) && !b.origin && NONE!=b.symbol.type && ( ((VAR==b.symbol.type && (!!b.symbol.sym || !!b.symbol.mys))||(CONST==b.symbol.type && !!b.symbol.sym)||!!b.symbol.acc)) && (t.par||!!b.symbol.par)&&(!bb[0].par||!!b.symbol.acc)){ | |
if(!!b.symbol.par){ | |
bb[0].par=true; | |
} | |
if(b.pos==bb[0].pos){ | |
b.pos=bb[0].pos-=bb[0].input.length; | |
continue; | |
} | |
k = false; | |
j = true; | |
(b.symbol.ptag==c)? (bb[0].text=b.symbol.output.trim()+bb[0].text) : bb.unshift({ptag:b.symbol.ptag,text:b.symbol.output.trim()}); | |
c=b.symbol.ptag; | |
bb[0].pos =b.pos; | |
bb[0].sp =b.sp; | |
if(0<b.sp[0]||0<b.sp[1]){ | |
break; | |
} | |
} | |
if(0<bb.length){ | |
if(!b.symbol){ | |
b.symbol = bb[0]; | |
b.pos=0; | |
} | |
bb[0].origin = !!mkOrigin[bb[0].text] ? bb[0].text : ''; | |
if('-'==b.text){ | |
++bb[0].pos; | |
bb[0].text='-'+bb[0].text; | |
} | |
}else if(!b.symbol){ | |
b.symbol = mk_cloneSymbol(mkSymbolNone); | |
b.pos=0; | |
} | |
j = j || !!b.symbol.acc; | |
} | |
if(j){ | |
t.par = false; | |
if(!!b.symbol.acc){ | |
mk_setCurrentSymbol(t,b.symbol.type, true); | |
return {symbol: mk_cloneSymbol(b.symbol,{multi:true}), pos:b.pos, text:bb, sp:b.sp, origin:bb[0].origin}; | |
}else if(!!bb[0].par){ | |
return {symbol: mk_cloneSymbol(bb[0]), pos:bb[0].pos, text:'', sp:bb[0].sp, origin:bb[0].origin} | |
} | |
return {symbol: mk_cloneSymbol(mkSymbolMulti), pos:bb[0].pos, text:bb, sp:bb[0].sp, origin: bb[0].origin}; | |
}else{ | |
t.par = t.par? false : ptag!='mo'; | |
} | |
return {symbol: {input:s, ptag:ptag, output:s, type:VAR, mys:true}, pos:i, text:'', sp:mk_spaces(str,i,ptag,sp,sl)}; | |
} | |
if(0===literal){ | |
k = mk_cloneSymbol(k); | |
k.par = true; | |
} | |
if(STYLE==k.type){ | |
mk_setCurrentSymbol(t,STYLE); | |
f=strl; | |
j = null; | |
bb = []; | |
while((!!mkRnames[str.slice(i,f)] && ((undefined!=(j=mkNames[str.slice(i, f)])&&!!(k=mkSymbols[j]) && STYLE==k.type && (bb.push(k),1) && -1!=(f=i) ) || 1)) && (-1<--i || (i=0))); | |
if(0==bb.length){ | |
t.par = false; | |
return {symbol: mk_cloneSymbol(mkSymbolNone), pos:i, text:''}; | |
} | |
k = mk_cloneSymbol(bb.pop()); | |
i = strl - k.input.length; | |
for(s in bb){ | |
for(j in bb[s].attr){ | |
if('style'==j){ | |
if(!k.attr.style){ | |
k.attr.style=''; | |
}else if(';'!=k.attr.style.charAt(k.attr.style.length-1)){ | |
k.attr.style+=';'; | |
} | |
k.attr.style+=bb[s].attr.style; | |
}else{ | |
k.attr[j]=bb[s].attr[j]; | |
} | |
} | |
i-=bb[s].input.length; | |
} | |
}else if(TEXT==k.type){ | |
--strl; | |
if((i=str.slice(0,strl).lastIndexOf(k.input))==-1){ | |
t.par = false; | |
return {symbol: mk_cloneSymbol(mkSymbolNone), pos:0, text:''}; | |
} | |
mk_setCurrentSymbol(t,TEXT); | |
return {symbol: mk_cloneSymbol(k), pos:i, text:str.slice(i+1,strl), sp:mk_spaces(str,i,'mtext',sp,sl)}; | |
} | |
s = ''; | |
if(i>0 && (!!k.paren||!!k.carry||!!k.sov||!!k.sym||!!k.acc) && str.charAt(i-1)=='-' && !!mk_isNeg(str,i-1)){ | |
s='-'+s; | |
--i; | |
} | |
mk_setCurrentSymbol(t,k.type); | |
k = mk_cloneSymbol(k); | |
if(!!k.shape && 'line'==k.shape){ | |
while('-'==str.charAt(i)){ | |
k.output+=k.output.charAt(0); | |
--i; | |
} | |
++i; | |
} | |
return {symbol: k, pos:i, text:s, sp:mk_spaces(str,i,k.ptag,sp,sl), found:true}; | |
}; | |
mk.rgetSymbol = mk_rgetSymbol; | |
var mk_nodeName = function(n){ | |
return (!!n&&!!n.nodeName) ? n.nodeName.toLowerCase(): ''; | |
}; | |
var mk_acopy = function(v){ | |
return v; | |
}; | |
var mk_tcopy = function(v){ | |
return v.text; | |
}; | |
var mk_anotneg1 = function(v){ | |
return v!=-1; | |
}; | |
var mk_dunwrap = function(node=null){ | |
if(!node){ | |
return; | |
} | |
if('declare' == mk_nodeName(node)){ | |
while(!!node.children[0]){ | |
node.parentNode.insertBefore(node.children[0],node); | |
} | |
node.parentNode.removeChild(node); | |
return; | |
} | |
if(0==node.children.length){ | |
return; | |
} | |
var i = -1; | |
for(i=-1;node.children.length>++i;){ | |
mk_dunwrap(node.children[i]); | |
} | |
}; | |
var mk_funwrap = function(node=null){ | |
if(!node){ | |
return; | |
} | |
if(!!node.hasAttribute('class') && !node.className){ | |
node.removeAttribute('class'); | |
} | |
var c = mk_nodeName(node); | |
if(0==node.children.length){ | |
if('mrow'==c){ | |
node.parentNode.removeChild(node); | |
return 1; | |
} | |
return 0; | |
} | |
if('mrow' == c){ | |
if(1==node.children.length && 'mrow'==mk_nodeName(node.children[0])){ | |
c = node.children[0]; | |
node.parentNode.insertBefore(c,node); | |
node.parentNode.removeChild(node); | |
node = c; | |
} | |
} | |
var i = -1; | |
for(i=-1;node.children.length>++i;){ | |
mk_funwrap(node.children[i]); | |
} | |
return 0; | |
//todo: empty mrows @ end | |
}; | |
var mk_tunwrap = function(t,node=null){ | |
if(!node){ | |
return t.pML('math'); | |
} | |
mk_dunwrap(node); | |
var p=null,n=null,b=null,c=null,d=null,tbl=null,i,j,k; | |
n = node.getElementsByClassName('__mkFraction'); | |
i = n.length; | |
while(-1<--i){ | |
if(!!n[i].children[0] && 'mspace'==mk_nodeName(n[i].children[0]) && !!n[i].parentNode){ | |
n[i].parentNode.insertAdjacentElement('beforebegin',n[i].children[0]); | |
} | |
mk_removeClass(n[i], '__mkFraction'); | |
} | |
n = node.getElementsByClassName('__mkDeferOriginal'); | |
i = n.length; | |
while(-1<--i){ | |
if(!!(b = n[i].className.match(/__mkDeferID([0-9]+)(?:[^0-9]|$)/u))&&!!(d=t.o[b[1]])&&!!d.origin&&!!mkOrigin[d.origin]&&!!(c=mk_getExt(mkOrigin[d.origin]))&&!!c.original){ | |
p = n[i].parentNode; | |
p.replaceChild(c.original.cloneNode(true),n[i]); | |
}else{ | |
if(!!b[1]){ | |
mk_removeClass(n[i],'__mkDeferID'+b[1]); | |
} | |
mk_removeClass(n[i],'__mkDeferOriginal'); | |
} | |
} | |
n = node.getElementsByClassName('__mkmlongdiv'); | |
for(i=-1;n.length>++i;){ | |
b = !!n[i].previousElementSibling ? n[i].parentNode : n[i].parentNode.previousElementSibling; | |
if(!!b && !!(b=b.querySelector('.__mklinebreak'))){ | |
p = b; | |
while(!p.previousElementSibling && !!(p=p.parentNode)); | |
d = b.parentNode; | |
if(1<d.children.length && b==d.children[0]){ | |
while(!d.previousElementSibling && !!(d=d.parentNode)); | |
//d = !!d.previousElementSibling ? d.previousElementSibling : null; | |
} | |
b.parentNode.removeChild(b); | |
c = null; | |
if(!!d && !!(d=d.previousElementSibling)){ | |
c = t.pML('mtd'); | |
do{ | |
b = d.previousElementSibling; | |
c.insertAdjacentElement('afterbegin', d); | |
}while(!!(d=b)); | |
} | |
if(!!p && 'mtable'!=mk_nodeName(p) && !p.querySelector('mtable')){ | |
// n[i].children[0].children[0].insertAdjacentElement('afterbegin',p); | |
} | |
b = mk_tcunwrap(t,null,n[i].children[0].children[1]); | |
b.replaceChild(n[i].children[0].children[0], b.children[0]); | |
n[i].replaceChild(b, n[i].children[0]); | |
if(!!n[i].children[0].children[2].textContent.trim()){ | |
mk_setStyle(n[i].children[0].children[2], 'border-top:1px solid'); | |
} | |
p = n[i].parentNode; | |
if(!!(p=p.nextElementSibling) && ((!p.className||-1==p.className.indexOf('__mklinebreak'))&&!p.querySelector('.__mklinebreak')) ){ | |
do{ | |
b = p.nextElementSibling; | |
mk_appendChild(n[i].children[0].children[2], p); | |
}while(!!(p=b) && ((!p.className||-1==p.className.indexOf('__mklinebreak'))&&!p.querySelector('.__mklinebreak'))); | |
} | |
if(!!c){ | |
n[i].insertAdjacentElement('afterbegin',mk_tcunwrap(t,null,c)); | |
} | |
p = n[i].parentNode; | |
mk_trunwrap(t,tbl=n[i],c=p.parentNode,p); | |
tbl.setAttribute('displaystyle','true'); | |
} | |
n[i].removeAttribute('class'); | |
} | |
n = node.getElementsByClassName('__mkline'); | |
for(i=-1;n.length>++i;){ | |
if(!!(p=n[i].parentNode) && !!(b=p.querySelector('.__mklinebreak'))){ | |
b.parentNode.removeChild(b); | |
while(!p.previousElementSibling && !!(p=p.parentNode)); | |
if(!!p && !!(p = p.parentNode)){ | |
p.appendChild(tbl=t.pML('mtable')); | |
mk_trunwrap(t,tbl,p,tbl,true); | |
tbl.setAttribute('displaystyle','true'); | |
} | |
} | |
} | |
n = node.getElementsByClassName('__mkline'); | |
i = n.length; | |
while(-1<--i){ | |
n[i].removeAttribute('class'); | |
} | |
n = node.getElementsByTagName('mstyle'); | |
i = n.length; | |
while(-1<--i){ | |
n[i].removeAttribute('class'); | |
} | |
do{ | |
c = node.getElementsByClassName('__mklinebreak'); | |
if(!!(n = c[0])){ | |
if(!n.hasAttribute('linebreak')){ | |
n.removeAttribute('class'); | |
c = node.getElementsByClassName('__mklinebreak'); | |
n = c[0]; | |
continue; | |
} | |
p = n.parentNode; | |
j = !!n.nextElementSibling?1:0; | |
i = (!(i = n.getAttribute('height'))) ? mkLineHeight : parseInt(i.replace(/[^0-9\-]/g,''),10)*mkLineHeight; | |
n.removeAttribute('linebreak'); | |
n.removeAttribute('height'); | |
while(!!(n=p) &&!(p=null)&& !!(p = n.parentNode) && -1=='|math|mstyle|mrow|mtd|mfenced|'.indexOf(b=mk_nodeName(p)) && !!b); | |
b = t.pML('mtable'); | |
b.appendChild(t.pML('mtr')); | |
b.appendChild(t.pML('mtr')); | |
b.children[0].appendChild(t.pML('mtd')); | |
b.children[1].appendChild(t.pML('mtd')); | |
n.insertAdjacentElement(0==j?'afterend':'beforebegin', b); | |
d = null; | |
d = b.children[0].children[0]; | |
while(!!p.children[1] && b!=p.children[0]){ | |
d.appendChild(p.children[0]); | |
} | |
d = b.children[1].children[0]; | |
while(!!p.children[1] && b==p.children[0]){ | |
d.appendChild(p.children[1]); | |
} | |
d = null; | |
d = 1==j ? b.children[1].children[0] : b.children[0].children[0]; | |
if(!d.children || !d.children[0] || 'mpadded'!=mk_nodeName(d.children[0])){ | |
d.insertAdjacentElement('afterbegin', t.pML('mpadded')); | |
} | |
d = d.children[0]; | |
d.setAttribute('height', i+'px'); | |
} | |
}while(0<c.length); | |
n = node.getElementsByTagName('mspace'); | |
i = n.length; | |
while(-1<--i){ | |
if(!!n[i].className && -1!=n[i].className.indexOf('__mkSpaceMrow')){ | |
continue; | |
} | |
b = n[i].parentNode; | |
for(j=-1;b.children.length>++j;){ | |
if('mspace'==mk_nodeName(b.children[j])){ | |
mk_addClass(b.children[j],'__mkSpaceMrow'); | |
if(!!(d=b.children[j].previousElementSibling)){ | |
b.children[j].insertAdjacentElement('beforebegin',mk_appendChild( t.pML('mrow'), d)); | |
if(!!(d=b.children[j].previousElementSibling)){ | |
mk_appendChild(d, b.children[j]); | |
--j; | |
} | |
}else if(!!(d=b.children[j].nextElementSibling)){ | |
b.children[j].insertAdjacentElement('afterend',mk_appendChild( t.pML('mrow'), d)); | |
if(!!(d=b.children[j].nextElementSibling)){ | |
d.insertAdjacentElement('afterbegin', b.children[j]); | |
--j; | |
} | |
} | |
} | |
} | |
} | |
n = node.getElementsByClassName('__mkSpaceMrow'); | |
i = n.length; | |
while(-1<--i){ | |
mk_removeClass(n[i],'__mkSpaceMrow'); | |
} | |
mk_funwrap(node); | |
return node; | |
}; | |
var mk_trunwrap = function(t,tbl,c,p,nodelay=false){ | |
var j = -1, b = null, n = null, h=false, k=1024, d; | |
if(!c.children){ | |
return; | |
} | |
c = c.children; | |
for(j=-1;c.length>++j;){ | |
if(0>--k){ | |
break; | |
} | |
if(c[j]!==p&&!!nodelay){ | |
if(!!nodelay && 42!==nodelay && !n){ | |
n = t.pML('mtd'); | |
//n.setAttribute('columnalign','right'); | |
} | |
d = false; | |
if(!!(b=c[j].querySelector('.__mkline')) || (b=c[j]).className.match(/\b\_\_mkline\b/)){ | |
if(!!(d=b.className.match(/\b\_\_mkID([0-9]+)\b/))){ | |
t.i[d[1]]='thus'; | |
} | |
b.parentNode.removeChild(b); | |
mk_tcunwrap(t,null,n,true); | |
d=true; | |
} | |
if(!!c[j]&&!!(b=c[j].querySelector('.__mklinebreak'))){ | |
if(!!d && c.length>(j+1) && p!=c[j+1]){ | |
++j; | |
} | |
b.parentNode.removeChild(b); | |
if(!!n){ | |
mk_tcunwrap(t,tbl,n); | |
} | |
n = t.pML('mtd'); | |
// n.setAttribute('columnalign','right'); | |
} | |
if(!!n){ | |
n = mk_appendChild(n, c[j]); | |
} | |
--j; | |
}else if(c[j]===p){ | |
nodelay = 42;//nag lé hitchikers condition | |
} | |
if(!!tbl.lastChild&&!!tbl.lastChild.lastChild&&!!tbl.lastChild.lastChild.textContent){ | |
h=true; | |
} | |
} | |
if(!!n){ | |
if(!!(b=n.querySelector('.__mklinebreak'))){ | |
b.parentNode.removeChild(b); | |
} | |
mk_tcunwrap(t,tbl,n); | |
} | |
for(k=-1;tbl.children.length>++k;){ | |
if(!!h){ | |
d = mk_setStyle(tbl.children[k].children[1]); | |
if(!!d['border-bottom']){ | |
mk_setStyle(tbl.children[k].children[2], 'border-bottom:'+d['border-bottom']+';padding-right:5px;'); | |
} | |
} | |
tbl.children[k].children[1].setAttribute('columnalign','right'); | |
tbl.children[k].children[2].setAttribute('columnalign','left'); | |
} | |
}; | |
var mk_tsunwrap = function(t,n){ | |
var b,i,j,k,m,q={}; | |
if(!n || !(b=n.querySelector('mstyle')) || !(m=b.className.match(/\b\_\_mkID([0-9]+)\b/))){ | |
return 0; | |
} | |
if(-1!=n.innerHTML.indexOf('mathsize="small"')){ | |
c=n.getElementsByTagName('mstyle'); | |
for(i=-1;c.length>++i;){ | |
if(!!c[i].className && !!(m=c[i].className.match(/\b\_\_mkID([0-9]+)\b/))){ | |
t.o[m[1]].symbol.supressmath = true; | |
} | |
} | |
}else { | |
c=n.getElementsByTagName('mstyle'); | |
for(i=-1;c.length>++i;){ | |
if(!!c[i].className && !!(m=c[i].className.match(/\b\_\_mkID([0-9]+)\b/)) && 'mn'==t.o[j=parseInt(m[1],10)].symbol.ptag){ | |
m.splice(0); | |
--j; | |
while(t.o.length>++j && (('mn'==t.o[j].symbol.ptag||('.'==t.o[j].symbol.output && t.o.length>(j+1) && 'mn'==t.o[j+1].symbol.ptag)) ? ((undefined==q[j]&&(m.unshift(q[j]=j))),!t.o[j].sp[2]) : 'mstyle'==t.o[j].symbol.ptag) && !t.o[j].sp[3]); | |
while(-1<--j && !t.o[j].sp[2] && (('mn'==t.o[j].symbol.ptag||('.'==t.o[j].symbol.output && -1<(j-1) && ('mn'==t.o[j-1].symbol.ptag || (-1<(j-2) && 'mstyle'==t.o[j-1].symbol.ptag && 'mn'==t.o[j-2].symbol.ptag)))) ? ((undefined==q[j]&&(m.push(q[j]=j))),!t.o[j].sp[2]) : 'mstyle'==t.o[j].symbol.ptag)); | |
if(0<m.length){ | |
t.o[m[0]].text = t.o[m[0]].symbol.output; | |
for(j=0;m.length>++j;){ | |
t.o[m[0]].text+=t.o[m[j]].symbol.output; | |
t.o[m[j]].symbol.supressmath = true; | |
} | |
t.o[m[0]].__text = t.o[m[0]].text; | |
} | |
k=c[i].textContent; | |
m.splice(0); | |
while(c[i].children[0]){ | |
m.push(c[i].children[0].getAttribute('style')||''); | |
c[i].removeChild(c[i].children[0]); | |
} | |
if('-'==k.charAt(0)){ | |
j=k.charAt(1); | |
mk_appendChild(c[i],t.pML((('0'<=j&&j<='9')?'mn':'mi'),k.substring(0,1))); | |
k=k.substring(2); | |
}else{ | |
j=k.charAt(0); | |
mk_appendChild(c[i],t.pML((('0'<=j&&j<='9')?'mn':'mi'),j)); | |
k=k.substring(1); | |
} | |
if('.'==(j=k.charAt(0))){ | |
j=k.charAt(1); | |
} | |
j = ('0'<=j&&j<='9')?'mn':'mi'; | |
c[i].insertAdjacentElement('afterend',t.pML(j,k)); | |
for(j=-1;c[i].children.length<++j;){ | |
if(!!m[j]){ | |
c[i].children[j].setAttribute('style',m[j]); | |
} | |
} | |
} | |
} | |
} | |
return 1; | |
}; | |
var mk_tpunwrap = function(h=''){ | |
return h.replace(/<(m[roi][ow]*)><\/\1>/g,'').replace(/(<mstyle[^>]*:line-through[^>]*>)(.*?)(<\/mstyle>)/g,'$1<menclose notation="downdiagonalstrike">$2</menclose>$3'); | |
}; | |
var mk_tcunwrap = function(t,tbl=null,n=null,b=false){ | |
var h = '', i = 0, k = '', m = null, o = null, r = null, s = null; | |
if(!!n && !b){ | |
mk_tsunwrap(t,n); | |
s = n.getAttribute('style'); | |
o = t.pML('mtd'); | |
h = n.innerHTML.replace(/(<\/?mspace[^>]*>)/g, ' $1 '); | |
if(!!(m = !!(k=-1!=h.indexOf('mathsize="small"')) ? h.match(/^(.*<[^>]*>[^<>]*)\.([^<>]*<[^>]*>.*)$/): | |
(h.match(/^(.*<[^>]*>[^<>]*[0-9])([^0-9,<>][^<>]*<[^>]*>.*)$/) | |
||h.match(/^(.*<[^>]*>[^<>]*[0-9][^<>]*<[^>]*>)(.*?<[^>]*>[^<>]*[^0-9,<>].*)$/ | |
)))){ | |
m[1] = m[1].replace(/\s*(<\/?mspace[^>]*>)\s*/g, '$1').trim(); | |
m[2] = m[2].replace(/\s*(<\/?mspace[^>]*>)\s*/g, '$1').trim(); | |
r=m[1].match(/<(\/?\s*[^\s<>]+)[^>]*>/g); | |
i=r.length; | |
m[3]={}; | |
while(-1<--i){ | |
r[i] = r[i].slice(1, r[i].indexOf(' ')); | |
if(1<r[i].length && r[i].length!=(r[i].indexOf('/')+1)){ | |
if(0==r[i].indexOf('/')){ | |
m[4]=r[i].substring(1); | |
undefined==m[3][m[4]]?m[3][m[4]]=1:m[3][m[4]]++; | |
}else if(!m[3][r[i]]){ | |
m[1]+='</'+r[i]+'>'; | |
}else{ | |
--m[3][r[i]]; | |
} | |
} | |
} | |
r=m[2].match(/<(\/?\s*[^\s<>]+)[^>]*>/g); | |
i=-1; | |
m[3]={}; | |
while(r.length>++i){ | |
r[i] = r[i].slice(1, r[i].indexOf(' ')); | |
if(1<r[i].length && r[i].length!=(r[i].indexOf('/')+1)){ | |
if(0!=r[i].indexOf('/')){ | |
m[4]='/'+r[i]; | |
undefined==m[3][m[4]]?m[3][m[4]]=1:m[3][m[4]]++; | |
}else if(!m[3][r[i]]){ | |
m[2]='<'+r[i].substring(1)+'>'+m[2]; | |
}else{ | |
--m[3][r[i]]; | |
} | |
} | |
} | |
n.innerHTML= mk_tpunwrap(m[1]); | |
o.innerHTML= mk_tpunwrap(m[2]).replace('<mo>.</mo>','<mi>.</mi>'); | |
//n.setAttribute('columnalign','right'); | |
//o.setAttribute('columnalign','left'); | |
if(0<o.children.length){ | |
if(0==o.textContent.indexOf('.')){ | |
mk_setStyle(o.children[0], 'margin-left:-8px;margin-right:1px;'); | |
}else if(0==o.textContent.indexOf('\u2193')){ | |
mk_setStyle(o.children[0], 'margin-left:-5px'); | |
} | |
} | |
if(!!s&&s.indexOf('border-top')){ | |
mk_setStyle(o, s); | |
} | |
}else{ | |
n.innerHTML= mk_tpunwrap(n.innerHTML); | |
} | |
} | |
if(!!n){ | |
r = mk_appendChild(mk_appendChild(mk_appendChild(t.pML('mtr'),t.pML('mtd')),n),o); | |
if(r.innerHTML.match(/font-size:[0-9][0-9]?[^0-9]/i)){ | |
m = r.getElementsByTagName('mtd'); | |
for(i=-1;m.length>++i;){ | |
mk_setStyle(m[i], 'margin:0;padding:0;height:5px;line-height:5px;'); | |
} | |
} | |
m = r.getElementsByClassName('__mklinebreak'); | |
for(i=-1;m.length>++i;){ | |
m[i].removeAttribute('class'); | |
} | |
if(!!tbl){ | |
tbl.insertAdjacentElement('beforeend',r); | |
} | |
if(!b){ | |
return r; | |
} | |
} | |
if(!!b && !!(b = (!!tbl && !!tbl.lastChild) ? tbl.lastChild.children[1] : n)){ | |
mk_setStyle(b, 'border-bottom:1px solid'); | |
} | |
}; | |
//todo: getstyle | |
var mk_setStyle = function(o,s=null){ | |
var ss = {}, a=null, b=''; | |
(o.getAttribute('style')||'').split(';').filter(function(v){ | |
a = v.split(':'); | |
if(1<a.length && !!(b=(a.shift()).trim().toLowerCase()) && !!(a[0]=(a.join(':')).trim())){ | |
ss[b] = a[0]; | |
} | |
}); | |
if(null==s){ | |
return ss; | |
} | |
s.split(';').filter(function(v){ | |
a = v.split(':'); | |
if(0<a.length && !!(b=(a.shift()).trim().toLowerCase())){ | |
if(!!(a[0]=(a.join(':')).trim())){ | |
ss[b] = a[0]; | |
}else{ | |
delete ss[b]; | |
} | |
} | |
}); | |
s = ''; | |
Object.keys(ss).map(function(k){ | |
s+=k+':'+ss[k]+';'; | |
}); | |
o.setAttribute('style', s); | |
}; | |
var mk_unwrap = function(node, t='declare'){ | |
if(!node || !node.children || 1!=node.children.length){ | |
return node; | |
} | |
if('declare' == mk_nodeName(node.children[0])){ | |
while(!!node.children[0].children[0]){ | |
node.appendChild(node.children[0].children[0]); | |
} | |
node.removeChild(node.children[0]); | |
} | |
return ((('apply'!=t || -1!='|ci|cn|set|vector|cs|apply|'.indexOf(mk_nodeName(node.children[0])))&& t == mk_nodeName(node) && 1==node.children.length )) ? node.children[0] : node; | |
}; | |
var mk_appendChild = function(p, c=null, r=true){ | |
var cc=null, i=-1, j=-1; | |
if(!c||!c.nodeName){ | |
return p; | |
} | |
p.appendChild(c); | |
if(r){ | |
cc = p.getElementsByTagName('declare'); | |
i = cc.length; | |
while(-1<--i){ | |
j = cc[i].children.length; | |
while(-1<--j){ | |
cc[i].insertAdjacentElement('beforebegin', cc[i].children[0]); | |
} | |
cc[i].parentNode.removeChild(cc[i]); | |
} | |
} | |
return p; | |
}; | |
var mk_cFirstClosedTag = function(content){ | |
var c = -1; | |
return (!!content && !!content.outerHTML && (c=content.outerHTML.indexOf('/>'))!=-1 && c<content.outerHTML.indexOf('>')); | |
}; | |
var mk_cMinus = function(t, p){ | |
if(!!t.o[p].neg){ | |
var content = mk_content(t,p,true); | |
if('cminus'!=mk_nodeName(content) && (1!=content.children.length||'cminus'!=mk_nodeName(content.children[0]))){ | |
var node = t.cML('apply', t.cML('minus')); | |
mk_appendChild(node, content); | |
content = node; | |
} | |
t.o[p].content = content; | |
delete t.o[p].neg; | |
} | |
return t.o[p].content; | |
}; | |
var mk_cAffix = (function(){ | |
var ops = ['suffixop','prefixop']; | |
return function(t,p,n=null){ | |
var o = t.o[p]; | |
if(!n && !!o._cAffix){ | |
return; | |
} | |
var i = -1, j=-1, node=null, op=null; | |
i = ops.length; | |
while(-1<--i){ | |
if(!!(op = o[ops[i]]) && 0<op.length){ | |
j = -1, node = null; | |
while(op.length>++j){ | |
node = t.cML('apply', t.cML(op[j].ctag || op[j].csymbol)); | |
if(null==n){ | |
mk_appendChild(node, o.content); | |
o.content = node; | |
}else{ | |
mk_appendChild(node, n); | |
n = node; | |
} | |
} | |
} | |
} | |
if(undefined != o.suffixof){ | |
node = null; | |
node = t.cML('apply', {symbol: o.ofprefixop}); | |
mk_appendChild(node,mk_content(t, o.suffixof));//t.o[o.suffixof].content); | |
if(null==n){ | |
mk_appendChild(node,o.content); | |
o.content = node; | |
}else{ | |
mk_appendChild(node,n); | |
n=node; | |
} | |
} | |
t.o[p]._cAffix=true; | |
}; | |
})(); | |
var mk_cBvar = function(t,p,content=null,node=null){ | |
var c = null, i = -1, j = -1, k = null==content ? 1 : t.o[p].c.length; | |
while(k>++i){ | |
if(!!content && !!t.o[t.o[p].c[i]].c){ | |
node = mk_cBvar(t, t.o[p].c[i], content, node); | |
}else { | |
c = null; | |
if(!content) { | |
c = p; | |
}else if(0<i && (!!t.o[t.o[p].c[i]].symbol.eq || !!t.o[t.o[p].c[i]].symbol.rel)){ | |
j=i; | |
while(k>++j && 'mi'!=t.o[t.o[p].c[j]].symbol.ptag); | |
if(j<k){ | |
c = t.o[p].c[j]; | |
} | |
} | |
if(null!=c){ | |
if(undefined!=t.o[c].subscript){ | |
node = t.cML('apply',t.cML('selector')); | |
mk_appendChild(node, t.cML(c)); | |
mk_appendChild(node,!!t.o[t.o[c].subscript].content?t.o[t.o[c].subscript].content:t.cML(t.o[c].subscript)); | |
node = t.cML('bvar',node); | |
}else{ | |
node = t.cML('bvar',t.o[c]); | |
} | |
if(undefined!=t.o[c].calcop && (undefined!=(j=t.o[c].order) || undefined!=(j=t.o[t.o[c].calcop].order))){ | |
mk_appendChild(node,t.cML('degree', mk_content(t, j))); | |
}else if(undefined!=t.o[c].power){ | |
mk_appendChild(node,t.cML('degree',mk_content(t, t.o[c].power))); | |
} | |
if(!!content){ | |
mk_appendChild(content,node); | |
} | |
} | |
} | |
} | |
return node; | |
}; | |
var mk_cSubPow = function(t,p,r=false,pre=''){ | |
var o = t.o[p], u = false, pow = pre+'power', sub = pre+'subscript', b = null; | |
if(!!o._cSubPow && (!pre || !!o._cemSubPow)){ | |
if(!!pre || (undefined==o._upower && undefined==o._usubscript)){ | |
return; | |
} | |
u = true; | |
o._opower = o.power; | |
o._osubscript = o.subscript; | |
o.power = o._upower; | |
o.subscript = o._usubscript; | |
delete o._upower; | |
delete o._usubscript; | |
} | |
var content=o.content, i=(undefined==o['_'+sub]?o[sub]:null), j=-1, k=(undefined==o['_'+pow]?o[pow]:null), node=null, v=-1; | |
if(!content){ | |
return mk_content(t,p); | |
} | |
if(mk_cFirstClosedTag(content) && 0!=content.children.length){ | |
content = t.cML('apply', content); | |
} | |
if(!!(node = v = 'apply'==mk_nodeName(content)? content : content.querySelector('apply')) ){ | |
for(j=-1;v.children.length>++j;){ | |
if('bvar'==mk_nodeName(v.children[j])){ | |
node = v.children[j]; | |
} | |
} | |
} | |
if('apply'==mk_nodeName(node)){ | |
node = node.firstChild; | |
} | |
if( !pre && UNDEROVER == o.symbol.type && undefined!=(v = (undefined!=i && !!t.o[i].c) ? i : ((undefined!=k && !!t.o[k].c)? k : undefined))){ | |
if(!u){ | |
node = mk_cBvar(t,v,content,node); | |
} | |
} | |
v = ((undefined!=i && undefined!=t.o[i][pow] && undefined==t.o[i]['_'+pow]) ? t.o[i][pow] : (undefined!=k ? k : -1)); | |
if(undefined!=i){ | |
if(!u && (UNDEROVER==o.symbol.type||!!o.symbol.integral)){ | |
!!node ? | |
(node.insertAdjacentElement( | |
('apply'==mk_nodeName(node)? 'afterbegin' : 'afterend'), | |
t.cML('lowlimit',mk_content(t,i)) | |
),(node = node.nextElementSibling)) : (mk_appendChild( | |
content, | |
node=t.cML('lowlimit',mk_content(t,i)) | |
), (node=content.lastChild)); | |
}else{ | |
node = t.cML('apply', t.cML('selector')); | |
mk_appendChild(node,content); | |
mk_appendChild(node, mk_content(t,i)); | |
content = node; | |
} | |
t.o[p].content = content; | |
if(undefined != t.o[i][pow]){ | |
t.x[t.o[i][pow]] = true; | |
} | |
} | |
if(v>-1 && (!o['is'+sub] || r)){ | |
t.x[v] = true; | |
if(!u && !pre && (UNDEROVER==o.symbol.type||!!o.symbol.integral)){ | |
!!node ? | |
node.insertAdjacentElement( | |
('apply'==mk_nodeName(node)? 'afterbegin' : 'afterend'), | |
t.cML('uplimit',mk_content(t,v)) | |
) : mk_appendChild( | |
content, | |
t.cML('uplimit',mk_content(t,v)) | |
); | |
}else{ | |
//TODO: better check on contents of PHANTOM and share | |
b = (!!t.o[v].c&&1==t.o[v].c.length)?t.o[t.o[v].c[0]]:t.o[v]; | |
if('T'==b.symbol.input && (!!o.matrix ||PHANTOM==o.symbol.type||!!o.origin ||!o.stackname)){ | |
node = t.cML('apply', t.cML('transpose')); | |
mk_appendChild(node,content); | |
}else if('-1'==b.symbol.input || '-1'==b.text){ | |
node = t.cML('apply', t.cML('inverse')); | |
mk_appendChild(node,content); | |
}else{ | |
node = t.cML('apply', t.cML('power')); | |
mk_appendChild(node,content); | |
mk_appendChild(node, mk_content(t,v)); | |
} | |
content = node; | |
} | |
t.o[p].content = content; | |
if(undefined != t.o[v][sub] || undefined != t.o[v][pow]){ | |
mk_cSubPow(t,v,true,pre); | |
mk_appendChild(t.o[p].content,t.o[v].content); | |
} | |
} | |
if(!u && !pre && !!t.o[p].symbol.integral && 'oint'==t.o[p].symbol.input){ | |
b = []; | |
if(!!(content = t.o[p].content.querySelector('lowlimit'))){ | |
b.push((!!content.firstChild?content.firstChild:content).cloneNode(true)); | |
t.o[p].content.removeChild(content); | |
} | |
if(!!(content = t.o[p].content.querySelector('uplimit'))){ | |
b.push((!!content.firstChild?content.firstChild:content).cloneNode(true)); | |
t.o[p].content.removeChild(content); | |
} | |
if(0<b.length){ | |
if(1==b.length){ | |
b[1]=b[0].cloneNode(true); | |
} | |
node = t.cML('csymbol'); | |
node.innerHTML = 'oriented_interval'; | |
node = t.cML('apply', node); | |
mk_appendChild(node,b[0]); | |
mk_appendChild(node,b[1]); | |
content = t.o[p].content.querySelector('int'); | |
content.insertAdjacentElement('afterend', node); | |
} | |
} | |
if(!r){ | |
t.o[p]._cSubPow=true; | |
} | |
if(!!pre){ | |
t.o[p]._cemSubPow=true; | |
} | |
if(!pre && (undefined!=o._opower || undefined!=o._osubscript)){ | |
o.power = o._opower; | |
o.subscript = o._osubscript; | |
delete o._opower; | |
delete o._osubscript; | |
} | |
return t.o[p].content; | |
}; | |
var mk_cLambda = function(t, a, d='r', c=0, b={}){ | |
var i = a.length, j = -1; | |
if(0<i){ | |
if(!c){ | |
j = i; | |
'r'==d? RIGHTBRACKET==t.o[a[0]].symbol.type && (a.shift() ^ --i) : LEFTBRACKET==t.o[a[i-1]].symbol.type && (a.pop() ^ --i); | |
if(1==a.length && i!=j && !!t.o[a[0]].c){ | |
if(!!t.o[a[0]].matrix||!!t.o[a[0]].set||!!t.o[a[0]].vector){ | |
return a; | |
} | |
return mk_cLambda(t,t.o[a[0]].c.slice(0),d,++c); | |
} | |
}else if(1<i && RIGHTBRACKET==t.o[a[0]].symbol.type && LEFTBRACKET==t.o[a[i-1]].symbol.type){ | |
a.shift(); --i; | |
a.pop(); --i; | |
} | |
while(0<--i){ | |
if(!!t.o[a[i]].args){ | |
j = t.o[a[i]].args.length; | |
while(-1<--j){b[t.o[a[i]].args[j]]=true;} | |
} | |
if(!!t.o[a[i]].c){ | |
j = t.o[a[i]].c.length; | |
while(-1<--j){b[t.o[a[i]].c[j]]=true;} | |
} | |
if(!!t.o[a[i]].symbol.sep){ | |
b[a[i]] = true; | |
} | |
} | |
if(0==c){ | |
a.unshift(null); | |
} | |
return a.filter(function(v){return !b[v];}); | |
} | |
return a; | |
}; | |
var mk_popOp = function(t, p){ | |
var o = t.o[p], i = -1, b = null; | |
if(!!o.ipopop && !!o.ipoparg){ | |
b = null; | |
i = o.ipopop.length; | |
while(-1<--i){ | |
b = t.cML('apply', t.cML(o.ipopop.shift().ctag)); | |
mk_appendChild(b, o.content); | |
if(0<o.ipoparg.length){ | |
mk_content(t, o.ipoparg[0], true); | |
mk_appendChild(b, t.o[o.ipoparg.shift()].content.cloneNode(true)); | |
} | |
o.content = b; | |
} | |
delete t.o[p].ipopop; | |
} | |
}; | |
var mk_content = function(t, p, force=false){ | |
var o = t.o[p]; | |
return (!!o.content && (!!o._forced || !force || !(!!o.vector || !!o.origin || !!o.matrix || !!o.set || !!o.matrixrow || !!o.crow || !!o.piecewise || !!o.suffixop || !!o.prefixop || !!o.ofprefixop || !!o.symbol.acc ||(PHANTOM==o.symbol.type&&!o._cSubPow&&undefined!=o.power)))) ? ((o._forced=false), o.content) : ((o._forced = force), mk_cTerm(t,p)); | |
}; | |
var mk_isValidId = function(id=''){ | |
return !id ? false : 0!==id.toLowerCase().indexOf('xml') && id.match(/[a-zA-Z]/u) && id.match(/^[_a-zA-Z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u{10000}-\u{EFFFF}][\-.0-9\u00B7\u0300-\u036F_a-zA-Z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u{10000}-\u{EFFFF}]*$/u); | |
}; | |
var mk_getValidId = function(id=''){ | |
id = !id ? '' : id.replace(/^.*[#]+/u,'').replace(/^[^_a-zA-Z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u{10000}-\u{EFFFF}]*(?:[xX][mM][lL])?/ug,'').replace(/[^\-.0-9\u00B7\u0300-\u036F_a-zA-Z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u{10000}-\u{EFFFF}]+/ug,''); | |
return !id ? '':(id.match(/[a-zA-Z]/u)? id : 'a'+id); | |
}; | |
var mk_validStackRefName = function(sr='',s=false){ | |
var k=sr.replace(/(?:^[",\s])|(?:[",\s]*$)/ug,''); | |
if(!k){ | |
k = 'mk__'+mkHashSum.hash(sr); | |
} | |
return (!s&&!!mkExt.img[k]) ? mkExt.img[k] : k; | |
}; | |
var mk_newStackOrigin = function(r='',s=''){ | |
r=r.replace(/(?:^[",\s])|(?:[",\s]*$)/ug,''); | |
s=s.replace(/(?:^[",\s])|(?:[",\s]*$)/ug,''); | |
if(!r || !s){ | |
return; | |
} | |
var k = r.length, j = -1; | |
while(k>++j){ | |
mkRorigin[r.slice(-j)] = true; | |
} | |
mkOrigin[r]=s; | |
mkOrigin[' '+s]=r; | |
}; | |
var mk_newStackName = function(sn='',s=''){ | |
sn = mk_validStackRefName(sn,true===s); | |
var k = sn; | |
if(!!mkExt.sn[k] || (!!mkExt.sn[k=mk_trimUrl(sn)] && !!k)){ | |
if(sn!=k){ | |
mkExt.img[sn]=k; | |
} | |
return true===s? sn:k; | |
} | |
if(!k){ | |
k=sn; | |
} | |
if(false===s){ | |
return k; | |
}else if(true===s){ | |
s=''; | |
} | |
if(!!mkExt.cache[1] && 2>Math.abs(__levenshtein(k,mkExt.cache[1]))){ | |
if(3<k.length || 1000>(Date.now()-mkExt.sn[mkExt.cache[1]].created) ){ | |
mk_deleteExt(mkExt.cache[1]); | |
} | |
} | |
mkExt.cache[1]=k; | |
mkExt.sn[k] = { | |
data: '', | |
status: s, | |
original: '', | |
originalid: '', | |
hit: Date.now(), | |
created:Date.now(), | |
type: '', | |
hash: mk_idFromUrl(k), | |
id: '', | |
ref: '', | |
atype:'' | |
}; | |
return sn; | |
}; | |
var mk_newStackRefId = function(id='',k=''){ | |
var i = mk_getValidId(id), n=''; | |
if(!i){ | |
i = 'mk__'+mkHashSum.hash(id); | |
} | |
while(!!mkDoc.querySelector('#'+i+n) || (!!mkExt.id[id+n]&&k!=mkExt.id[i+n])){ | |
if(0===mkExt.id[id+n].indexOf('mk__')){ | |
mk_deleteExt(mkExt.id[id+n]); | |
break; | |
} | |
n=!n?1:++n; | |
} | |
return i+''+n; | |
}; | |
var mk_newStackRef = function(r='',k=''){ | |
var id = ''; | |
r = mk_validStackRefName(r); | |
if(!k || !r){ | |
return r; | |
} | |
if(!mkExt.sn[k]){ | |
k=mk_newStackName(k); | |
} | |
if(!!mkExt.cache[0] && 2>Math.abs(__levenshtein(r,mkExt.cache[0])) && !!mkExt.ref[mkExt.cache[0]]){ | |
if(3<r.length || 1000>(Date.now()-mkExt.sn[mkExt.ref[mkExt.cache[0]]].created) ){ | |
mk_deleteExt(mkExt.ref[mkExt.cache[0]]); | |
} | |
} | |
if(!mkExt.sn[k]){ | |
k=mk_newStackName(k); | |
} | |
mkExt.cache[0]=r; | |
id = mk_newStackRefId(r,k); | |
mkExt.ref[r]=k; | |
mkExt.id[id]=k; | |
if(!mkExt.sn[k].ref){ | |
mkExt.sn[k].ref=r; | |
} | |
if(!mkExt.sn[k].id){ | |
mkExt.sn[k].id=id; | |
} | |
return r; | |
}; | |
var mk_getStackId = function(sn=''){ | |
if(!mkExt.sn[sn] && !mkExt.sn[sn=mk_validStackRefName(sn)]){ | |
return ''; | |
} | |
if(!mkExt.sn[sn].id){ | |
var id = mk_newStackRefId(mkExt.sn[sn].hash||sn, sn); | |
mkExt.id[id]=sn; | |
mkExt.sn[sn].id=id; | |
} | |
mkExt.sn[sn].hit = Date.now(); | |
return mkExt.sn[sn].id; | |
}; | |
var mk_getStackName = function(r=''){ | |
var sn = '',b=(0==r.indexOf('#')?((r=r.substring(1)),'id'):'ref'); | |
if(!(sn=mkExt[b][r]) && !(sn='ref'==b?mkExt.ref[mk_validStackRefName(r)]: mkExt.id[mk_validStackRefName(mk_getValidId(r))])){ | |
return ''; | |
} | |
mkExt.sn[sn].hit = Date.now(); | |
return sn; | |
}; | |
var mk_getStackRef = function(sn=''){ | |
if(!mkExt.sn[sn] && !mkExt.sn[sn=mk_validStackRefName(sn)]){ | |
return ''; | |
} | |
mkExt.sn[sn].hit = Date.now(); | |
return mkExt.sn[sn].ref; | |
}; | |
var mk_getStackHref = function(sn=''){ | |
if(!mkExt.sn[sn] && !mkExt.sn[sn=mk_validStackRefName(sn)]){ | |
return ''; | |
} | |
mk_getStackId(sn); | |
return 'math'==mkExt.sn[sn].type?sn.replace(/(\/\/[^a-z0-9]*\.?mathkey\.org)\/mk\//ui, '$1/ml/'):'#'+mkExt.sn[sn].id; | |
}; | |
var mk_getExt = function(sn=''){ | |
return mkExt.sn[sn] || mkExt.sn[mk_validStackRefName(sn)]; | |
}; | |
var mk_setExt = function(sn='', d=null, m={}){ | |
if(!sn || !mkExt.sn[sn] || !mkExt.sn[sn=mk_validStackRefName(sn)]){ | |
return; | |
} | |
var i; | |
if(!!d){ | |
if('string'==typeof d){ | |
if(0===d.indexOf('blob:')){ | |
mkExt.sn[sn].status='completed'; | |
mkExt.sn[sn].original = mkExt.sn[sn].data; | |
mkExt.sn[sn].data = d; | |
} else if(0===d.indexOf('data:image/')){ | |
mkExt.sn[sn].status='imagetoblob'; | |
mkExt.sn[sn].data = d; | |
mkExt.sn[sn].type = mk_typeImageUrl(sn); | |
}else { | |
mkExt.sn[sn].data = d; | |
} | |
}else if('object'==typeof d){ | |
mkExt.sn[sn].status='completed'; | |
if(!!d.querySelector && ('math'==mk_nodeName(d) || !!d.querySelector('math') )){ | |
mkExt.sn[sn].data = d; | |
mkExt.sn[sn].type ='math'; | |
} | |
}else{ | |
mkExt.sn[sn].data = d; | |
} | |
}else if('string'==typeof d){ | |
mk_deleteExt(sn); | |
return; | |
} | |
for(i in m){ | |
mkExt.sn[sn][i] = m[i]; | |
} | |
}; | |
var mk_deleteExt = function(sn=''){ | |
var i; | |
if(!sn){return;} | |
for(i in mkExt.id){ | |
if(sn==mkExt.id[i]){ | |
delete mkExt.id[i]; | |
} | |
} | |
for(i in mkExt.img){ | |
if(sn==mkExt.img[i]){ | |
delete mkExt.img[i]; | |
} | |
} | |
for(i in mkExt.ref){ | |
if(sn==mkExt.ref[i]){ | |
delete mkExt.ref[i]; | |
} | |
} | |
var k ='',j=-1; | |
mkRorigin={}; | |
delete mkOrigin[' '+sn]; | |
for(i in mkOrigin){ | |
if(sn==mkOrigin[i]){ | |
delete mkOrigin[i]; | |
continue; | |
} | |
if(' '==i.charAt(0)){ | |
continue; | |
} | |
k=i.length; | |
j = -1; | |
while(k>++j){ | |
mkRorigin[i.slice(-j)]=true; | |
} | |
} | |
delete mkExt.sn[sn]; | |
}; | |
var mk_findStackName = function(t,eq,i,a=false){ | |
var f = i, k = i; | |
a = 1===a ? ',' : ')'; | |
while(-1<--k && !t.o[k].pos); | |
while(-1<--f && !(t.o[i].l==t.o[f].l && a==t.o[f].symbol.input)); | |
return eq.slice(t.o[k].pos,!!t.o[f]?t.o[f].pos:(eq.length-1)); | |
}; | |
var mk_cTerm = function(t,p){ | |
var b = null, ext=null, i = -1, j = -1, k = '', o = t.o[p]; | |
if(undefined==o.__thaw){ | |
mk_thaw(t,p); | |
} | |
if(!o._cminus){ | |
o._cminus=0; | |
} | |
if( !!o.origin && !!(k=mk_getStackHref(mk_getStackName(o.origin) ))){ | |
o.content = t.cML('share'); | |
o.content.setAttribute('href',k); | |
}else if(!!o.stackname && 'stackrel'==o.symbol.input){ | |
ext = mk_getExt(o.stackname); | |
k = 'MathML-Content'; | |
b = [null,null]; | |
if(undefined===o.stackref && undefined===o.arg){ | |
b[0]=t.cML('cs',undefined!==ext?ext.data:''); | |
b[1]=t.cML(); | |
}else if(undefined===ext || 'error'===ext.data){ | |
if(!!ext.data){ | |
b[0]=mk_content(t,undefined===o.stackref?o.arg:o.stackref); | |
b[1]=t.cML('cs',ext.data); | |
}else if(undefined===o.stackref){ | |
b[0]=mk_content(t,o.arg); | |
b[1]=t.cML(); | |
}else { | |
i = mk_getStackId(o.stackname); | |
if(!!i){ | |
b[0] = mk_appendChild(t.cML('lambda'), mk_content(t,o.stackrel)); | |
b[0] = mk_appendChild(t.cML('apply'), b[0]); | |
b[0].setAttribute('id',i); | |
b[1] = mk_content(t, o.stackref); | |
}else{ | |
b[0] = mk_content(t,o.stackref); | |
b[1] = mk_content(t, o.stackrel); | |
} | |
} | |
}else if('image'==o.stype.substring(0,5)){ | |
k='text/html'; | |
if(undefined===o.stackref){ | |
b[0]=t.cML(); | |
}else{ | |
b[0]=mk_content(t,o.stackref); | |
} | |
b[1] = t.cML('img'); | |
b[1].setAttribute('src', ext.data.replace(/"/ug,'"e;')); | |
b[1].setAttribute('alt', ext.ref); | |
if(undefined!==o.stackref){ | |
b[1].setAttribute('id',mk_getStackId(o.stackname)); | |
} | |
}else if('math'==o.stype){ | |
j = mk_getStackHref(o.stackname); | |
if(undefined===o.stackref){ | |
if(-1!=o.stackname.indexOf('#')){ | |
delete t.o[o.arg].mk; | |
o.content = t.cML('share'); | |
o.content.setAttribute('href',j); | |
return o.content; | |
} | |
if(!ext.data.mathKey){ | |
k = 'MathML-Presentation'; | |
b[0] = t.cML(); | |
b[1] = mk_replaceML(ext.data); | |
}else{ | |
b[0] = mk_replaceML(t,ext.data.mathKey); | |
b[1] = t.cML('cs', o.stackname); | |
} | |
}else{ | |
if(!ext.data.mathKey){ | |
if(!h.hash){ | |
k = 'MathML-Presentation'; | |
b[0] = mk_content(t,o.stackref); | |
b[1] = mk_replaceML(t,ext.data); | |
}else if(-1!=o.stackname.indexOf('#')){ | |
b[0] = mk_appendChild(t.cML('lambda'), mk_content(t,o.stackref)); | |
b[0] = mk_appendChild(t.cML('apply'), b[0]); | |
b[0].setAttribute('id',ext.id); | |
b[1] = t.cML('share'); | |
b[1].setAttribute('href',j); | |
}else{ | |
k = 'MathML-Presentation'; | |
b[0] = mk_appendChild(t.cML('lambda'), mk_content(t,o.stackref)); | |
b[0] = mk_appendChild(t.cML('apply'), b[0]); | |
b[0].setAttribute('id',ext.id); | |
b[1] = mk_replaceML(t,ext.data); | |
} | |
}else{ | |
b[0] = mk_replaceML(t, ext.data.mathKey); | |
b[1] = mk_content(t,o.stackref); | |
if(!!ext.id){ | |
b[0] = mk_appendChild(t.cML('lambda'), b[0]); | |
b[0] = mk_appendChild(t.cML('apply'), b[0]); | |
b[0].setAttribute('id',ext.id); | |
} | |
} | |
} | |
}else if('function'==o.stype){ | |
if(undefined===o.stackref){ | |
b[0] = t.cML(); | |
b[1] = mk_content(t,o.arg); | |
mk_setExt(o.stackname,b[1].cloneNode(true)); | |
}else{ | |
if(!!ext.id){ | |
b[0] = mk_appendChild(t.cML('lambda'), mk_content(t,o.stackrel)); | |
b[0] = mk_appendChild(t.cML('apply'), b[0]); | |
b[0].setAttribute('id',ext.id); | |
b[1] = mk_content(t, o.stackref); | |
mk_setExt(o.stackname,b[0].cloneNode(true)); | |
}else{ | |
b[0] = mk_content(t,o.stackref); | |
b[1] = mk_content(t, o.stackrel); | |
mk_setExt(o.stackname,b[1].cloneNode(true)); | |
} | |
} | |
}else{ | |
if(undefined===o.stackref){ | |
b[0] = t.cML(); | |
b[1] = mk_content(t,o.arg); | |
}else{ | |
b[0] = mk_content(t,o.stackref); | |
b[1] = mk_content(t, o.stackrel); | |
} | |
mk_setExt(o.stackname,b[1].cloneNode(true)); | |
} | |
b[0] = mk_appendChild(t.pML('semantics'),b[0]); | |
b[1] = mk_appendChild(t.pML('annotation-xml'), b[1]); | |
b[1].setAttribute('encoding',k); | |
o.content = mk_appendChild(b[0], b[1]); | |
if(undefined!==o.stackref){ | |
delete t.o[o.stackref].mk; | |
delete t.o[o.stackref].mk; | |
} | |
return o.content; | |
} else if(!!o.symbol.output&&'e'==o.symbol.output&&undefined!=o.power){ | |
if(!o.args){ | |
o.args=[]; | |
} | |
o.args.push(o.power); | |
delete t.o[o.power].ispower; | |
t.o[o.power].isarg = true; | |
t.o[o.power].symbol.type = PHANTOM; | |
delete t.o[p].power; | |
o.symbol.ctag = 'exp'; | |
o.symbol.type = UNARY; | |
}else if(TEXT==o.symbol.type){ | |
o.content = !!o.text ? t.cML('cs', o.text) : t.cML(); | |
}else if('dot'==o.symbol.input || 'ddot'==o.symbol.input){ | |
o.content = mk_appendChild(t.cML('apply', t.cML('diff')), t.cML('ci', o.text.replace(/^\.+/ug,''))); | |
if('ddot'==o.symbol.input){ | |
o.content = mk_appendChild(t.cML('apply', t.cML('diff')), o.content); | |
} | |
}else if(undefined != o.p && t.o[o.p].piecewise){ | |
if(!o.content){ | |
o.content = t.cML(p); | |
} | |
if(!t.o[o.p].content){ | |
t.o[o.p].content = t.cML(t.o[o.p].ctag); | |
t.o[p]._p = true; | |
} | |
while(!!(b = o.content.querySelector('if'))){ | |
if(!b.parentNode){ | |
b = t.cML('declare',b).children[0]; | |
} | |
k = b.parentNode; | |
o.content = t.cML('apply', !!k.children[0] ? k.children[0].cloneNode(true):'approx'); | |
while(!!b.nextElementSibling){ | |
mk_appendChild(o.content, b.nextElementSibling.cloneNode(true)); | |
k.removeChild(b.nextElementSibling); | |
} | |
k.removeChild(k.children[0]); | |
k.removeChild(b); | |
if('apply'==mk_nodeName(k) && 'apply'!=mk_nodeName(k=mk_unwrap(k,'apply')) ){ | |
k = t.cML('declare',k); | |
} | |
k.insertAdjacentElement('afterend',o.content); | |
o.content = mk_unwrap(!!k.parentNode ? k.parentNode : k); | |
mk_appendChild(t.o[o.p].content, t.cML('piece', o.content)); | |
} | |
return t.o[o.p].content; | |
} | |
if((!o.content || ('declare'==mk_nodeName(o.content) &&0==o.content.children.length) )&& !!(o.content = t.cML(p))){ | |
t.o[p].content = mk_unwrap(o.content); | |
if(undefined!=o.calcopof){ | |
mk_cMinus(t, p); | |
return o.content; | |
} | |
} | |
if( | |
(o[k='matrix'] || o[k='vector'] || o[k='set'] || o[k='matrixrow'] || o[k='piecewise']) || ((o['vectorcol'] || o['setcol'] || | |
-1 != '|vector|set|matrixrow|matrix|'.indexOf('|'+mk_nodeName(o.content)+'|'))? (k='istrue') : | |
((k='apply'),((-1=='|apply|bvar|cn|ci|degree|lowlimit|uplimit|'.indexOf('|'+mk_nodeName(o.content)+'|') || ('ci'==mk_nodeName(o.content) && !!o.ctype && 'function'==o.ctype)) && (CONST!=o.symbol.type || !o.symbol.sym))))){ | |
if('istrue'==k){ | |
t.o[p]._p = true; | |
if((!!o.vectorcol||!!o.setcol)&&'declare'==mk_nodeName(o.content) && 1<o.content.children.length&&!mk_cFirstClosedTag(o.content)){ | |
o.content = t.cML('list', o.content); | |
} | |
return o.content; | |
}else if('apply'!=k){ | |
if(('declare'==(j=mk_nodeName(o.content)) && (0<o.content.children.length || k!=j)) ||k!=j){ | |
o.content = t.cML(k, o.content); | |
t.o[p]._p = true; | |
} | |
if('matrixrow'!=k){ | |
mk_cAffix(t,p); | |
mk_cSubPow(t,p); | |
mk_popOp(t,p); | |
} | |
return t.o[p].content; | |
}else if(!!o.args){ | |
o.content = t.cML(k, o.content); | |
} | |
} | |
if(!!o.symbol.integral){ | |
if(!o.int){ | |
if(undefined!=o.dop){ | |
mk_appendChild(o.content, mk_cBvar(t,o.dop)); | |
mk_cSubPow(t, p); | |
mk_cAffix(t, p); | |
mk_cSubPow(t,p,false,'em'); | |
if(undefined!=o.intop){ | |
t.o[o.intop]._content = mk_appendChild(o.content, t.o[o.intop]._content); | |
if(p==o.intop){ | |
} | |
delete o.intop; | |
} | |
delete o.dop; | |
return o.content; | |
} | |
}else{ | |
o._int = []; | |
if(!o.args){ | |
o.args = []; | |
} | |
k=-1; | |
b = null; | |
if(!(0==o.args.length || !t.o[i=o.args[0]].c || 0==t.o[i].c.length || undefined==(i=t.o[k=t.o[i].c[0]].calcop))){ | |
++i; | |
b=[]; | |
while(-1<--i){ | |
if((undefined!=t.o[i].op && (p==t.o[i].op||(1<b.length && undefined!=t.o[i].calcopof && undefined!=t.o[b[1]].intop && t.o[b[1]].intop==t.o[i].op))) || (undefined!=t.o[i].calcop&&b[0]==t.o[i].calcop)){ | |
b.unshift(i); | |
} | |
} | |
} | |
if(!!t.o[k]){ | |
if(undefined!=t.o[k].power){ | |
o._upower = t.o[k].power; | |
delete t.o[k].power; | |
} | |
if(undefined!=t.o[k].subscript){ | |
o._usubscript = t.o[k].subscript; | |
delete t.o[k].subscript; | |
} | |
} | |
if(!!b){ | |
k = -1; | |
i = b.length; | |
if(2<i && 1==o.int.length && !!t.o[o.int[0]].args && undefined!=(k=t.o[o.int[0]].args[0]) && !!t.o[k].calcop && !!t.o[k].suffixop){ | |
delete o.suffixop; | |
o.suffixop = t.o[k].suffixop; | |
delete t.o[k].suffixop; | |
} | |
k=-1; | |
while(-1<--i){ | |
if(undefined!=t.o[b[i]].intop||(undefined!=t.o[b[i]].calcop&&undefined!=t.o[t.o[b[i]].calcop].op&& t.o[t.o[t.o[b[i]].calcop].op].symbol.integral )){ | |
if(undefined!=t.o[b[i]].calcop){ | |
if(-1==k && !!t.o[b[i]].suffixop){ | |
delete o.suffixop; | |
o.suffixop = t.o[b[i]].suffixop; | |
delete t.o[b[i]].suffixop; | |
} | |
k=t.o[b[i]].calcop; | |
} | |
if(undefined!=t.o[b[i]].intop){ | |
t.o[t.o[b[i]].intop].dop = t.o[b[i]].calcopof; | |
o._int.unshift(t.o[b[i]].intop); | |
} | |
b.splice(i,1); | |
} | |
} | |
if(-1!=k && undefined!=(k=t.o[k].intop)){ | |
if(undefined!=o._upower){ | |
t.o[k]._upower = o._upower; | |
delete o._upower; | |
} | |
if(undefined!=o._usubscript){ | |
t.o[k]._usubscript = o._usubscript; | |
delete o._usubscript; | |
} | |
if(undefined!=o.suffixop){ | |
t.o[k].suffixop = o.suffixop; | |
delete o.suffixop; | |
} | |
} | |
} | |
if(!!o.int && undefined!=o.int[0]){ | |
mk_appendChild(o.content, mk_cBvar(t,t.o[o.int[0]].calcopof)); | |
} | |
} | |
} | |
if(!!(b=o.symbol.barge) && !!b.length){ | |
i=-1; | |
k = b.length-1; | |
if(!o.args){ | |
o.args = []; | |
} | |
while(k>(j=++i)){ | |
mk_appendChild(o.content, t.cML( | |
('^'==b[i++].charAt(0) ? (o.args.push(undefined!=(o._power=o.power) ? mk_content(t,o.power) : t.cML(!b[i].toPrecision?'mi':'mn',b[i])), b[j].slice(1)) : | |
('_'==b[j].charAt(0) ? (o.args.push(undefined!=(o._subscript=o.subscript) ? mk_content(t,o.subscript) : t.cML(!b[i].toPrecision?'mi':'mn', b[i])), b[j].slice(1)) : | |
(o.args.push(1<o.args.length ? mk_content(t, o.args.pop()) : t.cML((!b[i].toPrecision?'mi':'mn'),b[i])), b[j]))), o.args.pop())); | |
} | |
if(0==o.args.length){ | |
delete o.args; | |
} | |
} | |
if( UNDEROVER == o.symbol.type ||!!t.o[p].symbol.integral){ | |
(!!o.suffixop && undefined!=o.power)?(mk_cAffix(t, p),mk_cSubPow(t, p)):(mk_cSubPow(t, p),mk_cAffix(t, p)); | |
} | |
if(undefined == o.calcopof && !!o.args && 0<o.args.length){ | |
j = -1; | |
if(undefined!=(j=o.apply) && 0<j && (b=t.o[o.args[0]]).c.length>++j){ | |
o.farg = mk_cLambda(t, b.c.slice(j, b.c.length), 'l'); | |
o.func = mk_cLambda(t, b.c.slice(0, --j)); | |
i=o.farg.length; | |
if(0<i && null==o.farg[0]){ | |
o.farg.shift(); | |
--i; | |
} | |
b = [[],[]]; | |
while(-1<--i){ | |
b[0].push(mk_content(t, o.farg[i]).outerHTML.replace(/ xmlns="[^"]+"/ig,'')); | |
mk_appendChild(o.content, t.cML('bvar', t.o[o.farg[i]].content)); | |
} | |
i=o.func.length; | |
if(0<i){ | |
if(null==o.func[0]){ | |
o.func.shift(); | |
--i; | |
while(-1<--i){ | |
if(!!t.o[o.func[i]].mk){ | |
t.o[o.func[i]].content = t.o[o.func[i]].mk; | |
b[1].unshift(o.func[i]); | |
} | |
} | |
if(0<b[1].length){ | |
o.func = b[1].map(mk_acopy); | |
if(!!(b[1] = t.o[o.func[0]].content.querySelector('tendsto'))){ | |
if(!!b[1].parentNode){ | |
b[1] = b[1].parentNode; | |
} | |
if(!!b[1].parentNode){ | |
b[1] = b[1].parentNode; | |
} | |
b[2] = b[1].innerHTML.replace(/<apply[^>]*><tendsto[^>]*>(.*)<\/apply>/ui, '<tendsto><\/tendsto>$1'); | |
b[3] = b[2].substr(0,b[2].indexOf('<tendsto><\/tendsto>')); | |
b[4] = b[2].substr(b[2].indexOf('<tendsto><\/tendsto>')+19); | |
for(i in b[0]){ | |
b[4] = b[4].replace(b[0][i], ''); | |
} | |
try{ | |
t.o[o.func[0]].content.innerHTML = b[3].replace(/\s*xmlns="[^"]+"/ug,'')+b[4].replace(/\s*xmlns="[^"]+"/ug,''); | |
}catch(e){ | |
console.log(e); | |
} | |
t.o[o.func[0]].content = mk_unwrap(t.o[o.func[0]].content,'apply'); | |
} | |
} | |
}else{ | |
k = o.func[0]; | |
if(false!==k && !!t.o[k].mk){ | |
o.func = [k]; | |
if(!!(k = t.o[k].mk.querySelector('tendsto'))){ | |
t.o[o.func[0]].mk.removeChild(k); | |
} | |
t.o[o.func[0]].content = t.o[o.func[0]].mk; | |
t.o[o.func[0]]._p=true; | |
} | |
} | |
} | |
i=o.func.length; | |
while(-1<--i){ | |
b = mk_content(t, o.func[i]); | |
if(!!b.outerHTML.match(/^<apply[^>]*><apply[^>]*>/i)){ | |
b = mk_unwrap(b, 'apply'); | |
} | |
mk_appendChild(o.content, b); | |
} | |
}else{ | |
i = -1; | |
j=o.args.length; | |
o._arg = null; | |
while(j>++i){ | |
b = o.args[i]; | |
if((INFIX==t.o[b].symbol.type&&('_'==t.o[b].symbol.input||'^'==t.o[b].symbol.input))){ | |
continue; | |
} | |
if(!!t.o[b][k='matrix'] || (!!t.o[b][k='vector'] || !!t.o[b][k='set'])){ | |
if( ((undefined!=o.power && undefined!=t.o[b].power && o.power==t.o[b].power)&&((delete t.o[p].power),true))||!t.o[b].content || k!=mk_nodeName(t.o[b].content)){ | |
t.o[b].content = t.cML(k, t.o[b].content); | |
delete t.o[b]._cAffix; | |
delete t.o[b]._cSubPow; | |
delete t.o[b]._cemSubPow; | |
mk_cAffix(t, b); | |
mk_cSubPow(t, b); | |
mk_cSubPow(t,b,false,'em'); | |
} | |
}else{ | |
!!t.o[b]._content ? ((t.o[b].content=t.o[b]._content),delete t.o[b]._content) : mk_content(t, b); | |
if(((undefined==t.o[p]._apower && undefined==t.o[p]._asubscript) && ((undefined!=o.arg?((k=o.arg),(o._arg = o.arg),(delete o.arg),1):0) || (1==j&&-1!=(k=b)))) && (UNDEROVER != o.symbol.type && !o.symbol.integral && !!t.o[k].c && undefined!=t.o[k].c[0] && RIGHTBRACKET==t.o[t.o[k].c[0]].symbol.type && (((undefined!=t.o[k].power && (undefined!=o.power || o.power!=t.o[k].power)) && ((o._apower=t.o[k].power),1) ) ^ ((undefined!=t.o[k].subscript && (undefined==o.subscript || o.subscript!=t.o[k].subscript)) && ((o._asubscript=t.o[k].subscript),0))) && (undefined!=o._apower||undefined!=o._asubscript))){ | |
delete t.o[k]._cSubPow; | |
}else{ | |
k = -1; | |
} | |
if(b!=k){ | |
if(undefined!=o.power && undefined!=t.o[b].power && o.power==t.o[b].power){ | |
delete t.o[b].power; | |
} | |
if(undefined!=o.subscript && undefined!=t.o[b].subscript && o.subscript==t.o[b].subscript){ | |
delete t.o[b].power; | |
} | |
mk_cSubPow(t, b); | |
mk_cSubPow(t,b,false,'em'); | |
} | |
} | |
'bvar'==mk_nodeName(t.o[b].content) ? (('apply'==mk_nodeName(o.content)&&!!o.content.firstChild)?o.content.firstChild.insertAdjacentElement('afterend', t.o[b].content.cloneNode(true)):o.content.insertAdjacentElement('afterbegin', t.o[b].content.cloneNode(true))): mk_appendChild(o.content, t.o[b]._content || t.o[b].content); | |
} | |
if(undefined!=o._arg){ | |
o.arg = o._arg; | |
delete o._arg; | |
} | |
if(undefined!=o._apower||undefined!=o._asubscript){ | |
o.power = o._apower; | |
o.subscript = o._subscript; | |
delete t.o[p]._cSubPow; | |
} | |
} | |
} | |
(!!o.suffixop && undefined!=o.power&&!o.args)?(mk_cAffix(t, p),mk_cSubPow(t, p)):(mk_cSubPow(t, p),mk_cAffix(t, p)); | |
mk_cSubPow(t,p,false,'em'); | |
if(!t.x[p]){ | |
mk_cMinus(t, p); | |
o._cminus=true; | |
mk_popOp(t, p); | |
t.o[p].content = mk_unwrap(o.content); | |
} | |
if(!!o.symbol.integral && !!o._int && 0<(i=o._int.length)){ | |
t.o[o._int[0]]._content = t.o[p].content; | |
while(-1<--i){ | |
t.o[o._int[i]].intop = o._int[0]; | |
} | |
} | |
return o.content; | |
}; | |
var mk_cResolveDiff = function(t,a,i=null){ | |
var content = null, j = -1, node = null, q = null, r = null; | |
while(a.length>++j){ | |
r = a[j]; | |
if(!Array.isArray(r)){ | |
if(undefined!=t.o[r].calcopof && DIFFERENTIAL==t.o[r].symbol.type){ | |
t.o[r]._cSubPow=true; | |
if(!!t.o[r].isnumerator || !!t.o[r].isdenominator){ | |
if(undefined!=t.o[r].op && !!t.o[q = t.o[r].op].diff && !!t.o[q].diff[0] && !!t.o[q].diff[1] && null!==t.o[q].diff[0][0] && t.o[q].diff[0][0] == t.o[q].diff[1][0]){ | |
if(!!t.o[r].neg){ | |
!!t.o[q].neg? delete t.o[q].neg : t.o[q].neg=true; | |
} | |
if(null!=i){ | |
t.o[t.o[t.c.op[i][j]].op].symbol.ctag = 1==t.o[q].diff[0][0].length ? 'diff' : 'partialdiff'; | |
t.c.op[i].splice(j--,1); | |
} | |
delete t.o[q].symbol.arith; | |
}else{ | |
//todo: algebra | |
content = t.o[r].content.cloneNode(true); | |
t.o[r].content = t.o[r].content.firstChild; | |
if(undefined!=t.o[t.o[r].calcopof].power){ | |
t.o[r].power=t.o[t.o[r].calcopof].power; | |
delete t.o[r]._cSubPow; | |
mk_cSubPow(t,r); | |
} | |
node = t.cML('apply',t.cML('d'==t.o[r].symbol.input? 'diff' : 'partialdiff')); | |
mk_appendChild(node,content); | |
t.o[r].content = mk_appendChild(node,t.o[r].content); | |
} | |
}else if(undefined==t.o[r].op || !t.o[t.o[r].op].symbol.integral){ | |
//todo: algebra | |
content = t.o[r].content.cloneNode(true); | |
t.o[r].content = t.o[r].content.firstChild; | |
if(undefined!=t.o[t.o[r].calcopof].power){ | |
t.o[r].power=t.o[t.o[r].calcopof].power; | |
delete t.o[r]._cSubPow; | |
mk_cSubPow(t,r); | |
} | |
node = t.cML('apply',t.cML('d'==t.o[r].symbol.input? 'diff' : 'partialdiff')); | |
mk_appendChild(node,content); | |
t.o[r].content = mk_appendChild(node,t.o[r].content); | |
} | |
} | |
} | |
} | |
}; | |
var mk_cChildren = function(t,p){ | |
return !!t.o[p].c ? t.o[p].c :((1==t.o[p].args.length && !!t.o[t.o[p].args[0]].c) ? t.o[t.o[p].args[0]].c : ((undefined!=t.o[p].arg && !!t.o[t.o[p].arg].c) ? t.o[t.o[p].arg].c : [] )); | |
}; | |
var mk_cText = function(t, p){ | |
var content, i, j, k, o = t.o[p]; | |
if(!o._text && !!o.text && Array.isArray(o.text)){ | |
if(0<(j = o.text.length)){ | |
content = t.cML(); | |
k = ''; | |
if('mi'==o.text[0].ptag){ | |
if(o.symbol.acc){ | |
k = '-'==o.text[0].text.charAt(0) ? ((o.text[0].text=o.text[0].text.replace('-','')),'-') : ''; | |
o.text[0].text = k + o.symbol.output + o.text[0].text; | |
k = ''; | |
} | |
k = o.text[0].text = o.text.map(mk_tcopy).join(''); | |
o.text.splice(1); | |
j = o.text.length; | |
if(MULTI==o.symbol.type){ | |
o.symbol.type = VAR; | |
} | |
} | |
mk_appendChild(content, t.cML(o.text[0].ptag, o.text[0].text)); | |
if(1<j){ | |
i = 0; | |
while(j>++i){ | |
k+=o.text[i].text; | |
mk_appendChild(content,t.cML(o.text[i].ptag, o.text[i].text)); | |
} | |
} | |
} | |
if(!!o.symbol.ctype && !!content.firstChild){ | |
content.firstChild.setAttribute('type',o.symbol.ctype); | |
} | |
o._text = true; | |
!!o.symbol.output?o.text = k:o.symbol.output=k; | |
o.content = content; | |
} | |
}; | |
var mk_cNeg = function(t,p,n=false){ | |
var o = t.o[p],i,j,r; | |
i = undefined!=o.text && !o._text; | |
j = -1!='|mi|mn|munder|mover|'.indexOf(o.symbol.ptag)||DIFFERENTIAL==o.symbol.type; | |
r = !!o._text && !!o.text && !!o.text.firstChild; | |
if(!!n){ | |
if(j && (!o.text || !o.text.charAt || '-'!=o.text.charAt(0) )){ | |
o.symbol.output = '-'==o.symbol.output.charAt(0) ? o.symbol.output.slice(1):'-'+o.symbol.output; | |
}else if(i){ | |
o.text = '-'==o.text.charAt(0) ? o.text.slice(1) : '-' + o.text; | |
}else if(r){ | |
o.text.firstChild.textContent = '-'==o.text.firstChild.textContent.charAt(0) ? o.text.firstChild.textContent.slice(1) : '-' + o.text.firstChild.textContent; | |
}else if(!!o.content || !!o.mk){ | |
if(!o.text){ | |
o.text = ''; | |
} | |
n = Array.isArray(o.text) ? o.text[0] : o; | |
o.text = '-'==n.text.charAt(0) ? n.text.slice(1) : '-' + n.text; | |
} | |
t.c.mem['-'] = false; | |
} | |
if(j&&'-'==o.symbol.output.charAt(0)){ | |
o.symbol.output = o.symbol.output.slice(1); | |
o.neg = true; | |
}else if(i && '-'==o.text.charAt(0)){ | |
o.text = o.text.slice(1); | |
o.neg = true; | |
}else if(r && '-'==o.text.firstChild.textContent.charAt(0)){ | |
o.text.firstChild.textContent = o.text.firstChild.textContent.slice(1); | |
o.neg = true; | |
} | |
}; | |
var mk_contents = (function(){ | |
var dcompare = function(a,b){ | |
return a[1]===b[1]? (a[2]===b[2]?(a[3]===b[3]?0:(a[3]>b[3]?1:-1)):(a[2]>b[2]?-1:1)) :(a[1]>b[1]?-1:1); | |
}, acompare = function(a,b){ return (a=parseInt(a,10))==(b=parseInt(b,10))?0:(a>b?1:-1); }; | |
return function(t, p, c){ | |
var b=null, content=null, i=-1, j=-1, k='', l=t.o[p].l, node=null, o=t.o[p], r=null; | |
mk_cText(t,p); | |
if(1==mkDebug){ | |
console.log(t.o[p].symbol.output+': '+p+'@'+t.o[p].l); | |
console.log(JSON.stringify(c)); | |
} | |
i = j = c.indexOf(p); | |
if(undefined == (k=c[++i]) || -1 == k || !!t.o[k].xbracket || (LEFTBRACKET == t.o[k].symbol.type && (undefined == (k=c[++i]) || -1 == k || !!t.o[k].xbracket ) )){ | |
b = false; | |
} | |
if(-1==--j || undefined == (k=c[j]) || -1 == k || !!t.i[k] || !!t.o[k].xbracket || (RIGHTBRACKET == t.o[k].symbol.type && (undefined == (k=c[j-1]) || -1 == k || !!t.o[k].xbracket ) ) ){ | |
b = false == b ? 1 : true; | |
if(undefined != t.o[k=c[j]] && RIGHTBRACKET == t.o[k].symbol.type){ | |
t.x[k]=true; | |
} | |
} | |
if(!b && t.x[c[j]]){ | |
while(-1<--j && t.x[k=c[j]] && !t.o[k].xbracket); | |
if( -1 == j || undefined == c[j] || -1 == c[j]){ | |
b = false === b ? 1 : true; | |
} | |
} | |
if(o.isarg){ | |
t.x[p] = true; | |
} | |
if(!!o.outfixop){ | |
o.content = mk_appendChild(t.cML('apply', t.cML(o.outfixop)), mk_content(t, p)); | |
} | |
if(!!t.i[p]){ | |
if(undefined==t.annotation){ | |
t.annotation=p+1; | |
} | |
b = 1; | |
o.symbol.ctag = 'cysmbol'; | |
o.symbol.ctype = 'thus'; | |
o.symbol.type = CONST; | |
o.symbol.output = t.i[p]; | |
} | |
if(!t.annotate&&undefined!=t.annotation){ | |
b=1; | |
t.annotate = mk_appendChild(t.cML('semantics'), t.cML('annotation-xml')); | |
t.annotate.lastChild.setAttribute('encoding', 'MathML-Content'); | |
} | |
k = o.symbol.type; | |
if(!!o.ofsuffixop && o.ofsuffixop.infixswap){ | |
r = mk_cloneSymbol(t.o[o.prefixof].symbol); | |
t.o[o.prefixof].symbol = mk_cloneSymbol(o.symbol); | |
delete o.ofsuffixop.infixswap; | |
o.symbol = r; | |
if('mlongdiv'==o.ofsuffixop.ptag && !!o.sp[2] && !!t.c.arg && t.c.arg[0] && 'mn'==t.o[t.c.arg[0]].symbol.ptag){ | |
if(undefined==t.annotation){ | |
t.annotation=o.prefixof; | |
} | |
} | |
r = null; | |
} | |
if((j=(RIGHTBRACKET==k || LEFTBRACKET==k || LEFTRIGHT==k || !!o.issubscript || !!o.ispower || !!o.symbol.sep || !!o.symbol.supressmath) ) || undefined!=o.prefixof || !(VAR==k || CONST==k || UNARY==k || 'mfrac'==o.symbol.ptag || UNDEROVER==k || DIFFERENTIAL==k || PHANTOM==k || MULTI==k || BINARY==k || TEXT==k)){ | |
if(!j){ | |
mk_content(t, p); | |
mk_cNeg(t,p,t.c.mem['-']); | |
if((!!o.content || !!o.mk) && !!o.neg){ | |
if(!!o.mk){ | |
o.content = o.mk; | |
} | |
mk_cMinus(t, p); | |
if(!!o.mk){ | |
o.mk = o.content; | |
} | |
} | |
} | |
}else{ | |
if(DIFFERENTIAL == k && undefined != o.calcopof){ | |
j = p; | |
while(!t.o[j].isdenominator && !t.o[j].isnumerator && undefined!=t.o[j].p && undefined!=t.o[j=t.o[j].p]); | |
if(undefined==o.intop){ | |
o.content = mk_cBvar(t,o.calcopof); | |
}else{ | |
t.o[p].symbol.supressmath = true; | |
t.o[o.calcopof].symbol.supressmath = true; | |
} | |
if(undefined!=o.op && !!t.o[o.op].symbol.integral){ | |
if(undefined==o.intop && undefined!=o.op){ | |
if(!t.o[o.op].int){ | |
t.o[o.op].int = []; | |
} | |
t.o[o.op].int.push(p); | |
r = mk_cChildren(t,o.op); | |
j = r.length; | |
} | |
o.isarg = true; | |
}else if(!!t.o[j].isdenominator){ | |
r = t.o[t.o[j].op]; | |
if(!r.diff){ | |
r.diff = [null,null]; | |
} | |
if(!r.diff[1]){ | |
r.diff[1] = [o.symbol.input]; | |
} | |
r.diff[1].push(p); | |
t.o[o.calcopof].isdenominator=true; | |
t.o[o.calcopof].op=t.o[j].op; | |
//mark operator as partialdiff or diff | |
}else if(!!o.isnumerator){ | |
r = t.o[t.o[j].op]; | |
if(!r.diff){ | |
r.diff = [null,null]; | |
} | |
if(!r.diff[0]){ | |
r.diff[0] = [o.symbol.input]; | |
} | |
t.o[o.calcopof].isnumerator=true; | |
t.o[o.calcopof].op=t.o[j].op; | |
r.diff[0].push(p); | |
t.o[p]._content = t.cML(); | |
content = t.cML(); | |
if(!!t.o[o.calcopof].args){ | |
i=-1; | |
j = t.o[o.calcopof].args.length; | |
while(j>++i){ | |
if(!!t.o[t.o[o.calcopof].args[i]].content){ | |
mk_appendChild(content, | |
t.o[t.o[o.calcopof].args[i]].content.cloneNode(true)); | |
} | |
} | |
}else if(!!t.o[o.calcopof].c){ | |
mk_appendChild(content,mk_content(t,o.calcopof)); | |
} | |
if(undefined!=t.o[o.calcopof].power){ | |
content = mk_cSubPow(t,o.calcopof); | |
} | |
content = mk_unwrap(content); | |
if('apply'!=mk_nodeName(content)){ | |
node = t.cML('apply', !!(t.o[o.calcopof].symbol.output||t.o[o.calcopof].text)?t.o[o.calcopof]:undefined); | |
mk_appendChild(node,content); | |
}else{ | |
node = content; | |
} | |
mk_appendChild(t.o[p]._content,node); | |
} | |
} | |
if(!!o.symbol.eq&&undefined!=t._logic&&o.l<t._logic){ | |
delete t._logic; | |
} | |
if(0<t.c._eq.length){ | |
if('condition'==t.c._eq[0]){ | |
if(o.symbol.eq){ | |
delete o.symbol.eq; | |
o.symbol.setop = true; | |
} | |
}else if(!!o.symbol.logic){ | |
if(undefined==t._logic){ | |
t._logic=o.l; | |
i = -1; | |
content = mk_content(t, t.c.arg[0]).cloneNode(true); | |
while(t.c._eq.length>++i){ | |
t.o[t.c.arg[0]].content = t.cML('apply', t.cML(!!t.c._eq[i].toPrecision? t.c.req[i] : t.c._eq[i])); | |
j = -1; | |
while(t.c.eq[t.c._eq[i]].length>++j){ | |
mk_appendChild(t.o[t.c.arg[0]].content, t.c.eq[t.c._eq[i]][j]); | |
} | |
} | |
t.c._eq.splice(0); | |
mk_appendChild(t.o[t.c.arg[0]].content, content); | |
} | |
} | |
}else if(!!o.symbol.logic && undefined==t._logic){ | |
t._logic=-o.l; | |
} | |
if(undefined!=t._logic && -1<t._logic && !!o.symbol.eq){ | |
delete o.symbol.eq; | |
o.symbol.setop = true; | |
} | |
if((!!o.symbol.eq || !!o.symbol.cond) && (undefined==t._logic || 0>=t._logic)){ | |
b = !!b? 2 : 1; | |
}else if(!!o.symbol.ipop){ | |
if(-1<(j=t.c.arg.length-1)){ | |
!!t.o[t.c.arg[j]].ipopop ? t.o[t.c.arg[j]].ipopop.push(o.symbol) : | |
t.o[t.c.arg[j]].ipopop = [o.symbol]; | |
t.o[t.c.arg[j]].opipop = true; | |
} | |
}else if((!!o.symbol.arith || !!o.symbol.setop || !!o.symbol.logic || !!o.symbol.rel) && !o.symbol.iscalcop){ | |
//current operation/previous operation | |
if('-'==o.symbol.output){ | |
t.c.mem['-'] = !t.c.mem['-']; | |
o.symbol.ctag = 'plus'; | |
o.symbol.output = '+'; | |
o.symbol._wasneg = true; | |
}else if(-1<(j=t.c.arg.length-1) && t.o[t.c.arg[j]].opipop){ | |
delete t.o[t.c.arg[j]].opipop; | |
} | |
if(!!t.c.crop){ | |
t.c.prop = t.c.crop; | |
} | |
if(!t.c.crop || !t.c.rop || !t.c.rop[t.c.crop] || o.symbol.output!=t.c.rop[t.c.crop].symbol.output){ | |
t.c.crop = !!o.symbol.pri ? o.symbol.output : p; | |
if(!o.symbol.arith && !!o.symbol.pri && t.c.prop!=t.c.crop){ | |
for(j in t.c.mem){ | |
if(t.c.mem[j]==t.c.rop[t.c.mem[j]].symbol.output){ | |
t.c.adj = [t.c.mem[j],j]; | |
t.fk.filter(t.cremap); | |
if(t.c.prop == t.c.adj[0]){ | |
t.c.prop = t.c.adj[1]; | |
} | |
delete t.c.adj; | |
} | |
} | |
} | |
if(!t.c.op[t.c.crop]){ | |
t.c.rel[t.c.crop] = {}; | |
t.c.op[t.c.crop] = []; | |
t.c.d[t.c.crop] = [t.c.crop,1]; | |
if(!t.c.rop){ | |
t.c.rop = {}; | |
} | |
t.c.rop[t.c.crop] = t.o[p]; | |
t.c.rop[t.c.crop].mem = p; | |
t.c.mem[p] = t.c.crop; | |
} | |
} | |
}else if(undefined==o.calcop && !o.isarg && !o.symbol.supressmath){ | |
mk_cNeg(t,p,t.c.mem['-']); | |
if((!!o.content || !!o.mk) && !!o.neg){ | |
if(!!o.mk){ | |
o.content = o.mk; | |
} | |
mk_cMinus(t, p); | |
if(!!o.mk){ | |
o.mk = o.content; | |
} | |
} | |
if(-1<(j=t.c.arg.length-1)&&!!t.o[t.c.arg[j]].opipop){ | |
!!t.o[t.c.arg[j]].ipoparg ? t.o[t.c.arg[j]].ipoparg.push(p):t.o[t.c.arg[j]].ipoparg=[p]; | |
}else if(!t.c.crop){ | |
t.c.arg.push(p); | |
}else if(!t.c.prop || t.c.crop==t.c.prop){ | |
Array.prototype.push.apply(t.c.op[t.c.crop], t.c.arg); | |
t.c.arg.splice(0); | |
t.c.arg.push(p); | |
}else if(!!t.c.prop){ | |
if(t.c.rop[t.c.prop].symbol.pri || ('*'==t.c.rop[t.c.prop].symbol.output && '/'==t.c.rop[t.c.crop].symbol.output && -1!=(t.c.rop[t.c.crop].prop=t.c.prop) )){ | |
r = !!t.c.rel[t.c.crop][t.c.prop] ? [t.c.crop,t.c.prop] : [t.c.prop,t.c.crop]; | |
if(!t.c.rel[t.c.crop][t.c.prop]){ | |
t.c.op[t.c.prop].push(t.c.op[t.c.crop]); | |
} | |
t.c.d[t.c.prop][1]+=t.c.d[t.c.crop][1]; | |
Object.keys(t.c.rel[t.c.prop]).filter(t.c.f1); | |
t.c.rel[t.c.crop][t.c.prop] = !!t.c.rel[t.c.crop][t.c.prop] ? t.c.rel[t.c.crop][t.c.prop]+1 : 1; | |
Array.prototype.push.apply(t.c.op[t.c.crop], t.c.arg); | |
}else { | |
j=t.c.op[t.c.crop].length-1; | |
if(undefined!=t.c.rop[t.c.prop].prop){ | |
if(-1<j && (t.c.op[t.c.crop][j]===t.c.op[t.c.rop[t.c.prop].prop][0] || t.c.op[t.c.crop][j]===t.c.op[t.c.rop[t.c.prop].prop])){ | |
t.c.op[t.c.crop].pop(); | |
if(1>--t.c.rel[t.c.rop[t.c.prop].prop][t.c.crop]){ | |
delete t.c.rel[t.c.rop[t.c.prop].prop][t.c.crop]; | |
} | |
} | |
for(k in t.c.rel[t.c.rop[t.c.prop].prop]){ | |
if(t.c.crop!=k){ | |
t.c.op[k][t.c.op[k].indexOf(t.c.op[t.c.rop[t.c.prop].prop])]=t.c.op[t.c.crop]; | |
delete t.c.rel[t.c.rop[t.c.prop].prop][k]; | |
} | |
} | |
Array.prototype.push.apply(t.c.op[t.c.prop], t.c.arg); | |
t.c.op[t.c.crop].push(t.c.op[t.c.rop[t.c.prop].prop]); | |
t.c.d[t.c.crop][1]+=t.c.d[t.c.rop[t.c.prop].prop][1]; | |
Object.keys(t.c.rel[t.c.crop]).filter(t.c.f1); | |
t.c.rel[t.c.rop[t.c.prop].prop][t.c.crop] = !!t.c.rel[t.c.rop[t.c.prop].prop][t.c.crop] ? t.c.rel[t.c.rop[t.c.prop].prop][t.c.crop]+1 : 1; | |
delete t.c.rop[t.c.prop].prop; | |
}else{ | |
if(-1<j && (t.c.op[t.c.crop][j]===t.c.op[t.c.prop][0] || t.c.op[t.c.crop][j]===t.c.op[t.c.prop])){ | |
t.c.op[t.c.crop].pop(); | |
if(1>--t.c.rel[t.c.prop][t.c.crop]){ | |
delete t.c.rel[t.c.prop][t.c.crop]; | |
} | |
} | |
for(k in t.c.rel[t.c.prop]){ | |
if(t.c.crop!=k){ | |
t.c.op[k][t.c.op[k].indexOf(t.c.op[t.c.prop])]=t.c.op[t.c.crop]; | |
delete t.c.rel[t.c.prop][k]; | |
} | |
} | |
Array.prototype.push.apply(t.c.op[t.c.prop], t.c.arg); | |
t.c.op[t.c.crop].push(t.c.op[t.c.prop]); | |
t.c.d[t.c.crop][1]+=t.c.d[t.c.prop][1]; | |
Object.keys(t.c.rel[t.c.crop]).filter(t.c.f1); | |
t.c.rel[t.c.prop][t.c.crop] = !!t.c.rel[t.c.prop][t.c.crop] ? t.c.rel[t.c.prop][t.c.crop]+1 : 1; | |
} | |
} | |
t.c.arg.splice(0); | |
t.c.arg.push(p); | |
}else { | |
Array.prototype.push.apply(t.c.op[t.c.crop], t.c.arg); | |
t.c.arg.splice(0); | |
t.c.arg.push(p); | |
} | |
} | |
} | |
if(!!b && !!t.c.d){ | |
if(!!t.c.crop && !!t.c.arg && 0<t.c.arg.length){ | |
Array.prototype.push.apply(t.c.op[t.c.crop], t.c.arg); | |
t.c.arg.splice(0); | |
} | |
if(0<t.c.arg.length){ | |
mk_cResolveDiff(t,t.c.arg); | |
}else{ | |
for(i in t.c.op){ | |
mk_cResolveDiff(t,t.c.op[i], i); | |
} | |
} | |
if(1==mkDebug){ | |
console.log(JSON.stringify(t.c)); | |
} | |
t.c.d = Object.keys(t.c.d).map(t.c.m1); | |
t.c.d.sort(dcompare); | |
i = t.c.d.length; | |
r = null; | |
while(-1<--i){ //in order of depth | |
k = t.c.d[i][0]; | |
r = t.c.rop[k]; | |
r.content = t.cML('apply',r); | |
if(!!r.diff && !r.symbol.arith){ | |
j = 0; | |
while(r.diff[1].length>++j){ | |
mk_appendChild(r.content, t.o[r.diff[1][j]].content.cloneNode(true)); | |
} | |
j = 0; | |
while(r.diff[0].length>++j){ | |
t.o[r.diff[0][j]].content = t.o[r.diff[0][j]]._content.cloneNode(true); | |
if('ci'==mk_nodeName(t.o[r.diff[0][j]].content.firstChild) && !t.o[r.diff[0][j]].content.firstChild.getAttribute('type')){ | |
t.o[r.diff[0][j]].content.firstChild.setAttribute('type', 'function'); | |
} | |
mk_appendChild(r.content, t.o[r.diff[0][j]].content.cloneNode(true)); | |
} | |
mk_cMinus(t, k); | |
} | |
j = -1; | |
while(t.c.op[k].length>++j){ | |
if(!!t.c.op[k][j].toPrecision){ | |
mk_appendChild(r.content, mk_content(t,t.c.op[k][j], true)); | |
delete t.o[t.c.op[k][j]].content; | |
}else{ | |
if(!!t.c.op[k][j][0].cloneNode){ | |
mk_appendChild(r.content, t.c.op[k][j][0].cloneNode(true)); | |
} | |
delete t.c.op[t.c.op[k][j][1]]; | |
} | |
} | |
t.c.op[k].splice(0); | |
t.c.op[k].push(r.content.cloneNode(true), k); | |
} | |
node = t.cML(); | |
if(0<(j=t.c.arg.length)){ | |
t.c.arg.sort(acompare); | |
while(-1<--j){ | |
mk_appendChild(node, mk_content(t, t.c.arg[j], true)); | |
} | |
}else{ | |
for(i in t.c.op){ | |
mk_appendChild(node, t.c.op[i][0]); | |
} | |
} | |
if((0==t.c._eq.length || 'condition'!=t.c._eq[0]) && (k = (!!o.symbol.eq||!!o.symbol.cond) ? o.symbol.ctag : ( 0<t.c._eq.length ? t.c._eq[t.c._eq.length-1] : false) )){ | |
content = mk_unwrap(node); | |
if(!!o.symbol.cond){ | |
t.c.eq[k] = []; | |
r = t.cML(); | |
j=-1; | |
while(t.c.arg.length>++j){ | |
mk_appendChild(r, t.cML('bvar', t.cML(t.c.arg[j]))); | |
} | |
r = mk_appendChild(t.cML('apply', r), t.cML('condition')); | |
r.insertAdjacentElement('beforeend',content.cloneNode(true)); | |
t.cc(); | |
t.c._eq = [k]; | |
t.c.eq[k] = [r]; | |
}else{ | |
if(!!t.c.eq[k]){ | |
if(k != t.c._eq[t.c._eq.length-1]){ | |
t.c._eq[t.c._eq.indexOf(k)] = p; | |
t.c.req[p]=k; | |
t.c._eq.push(k); | |
t.c.eq[p] = t.c.eq[k].map(mk_acopy); | |
t.c.eq[k] = [t.c.eq[p][t.c.eq[p].length-1]]; | |
} | |
}else { | |
t.c.eq[k] = []; | |
if(t.c._eq.length>0){ | |
t.c.eq[ t.c._eq[t.c._eq.length-1] ].push(content.cloneNode(true)); | |
} | |
t.c._eq.push(k); | |
} | |
t.c.eq[k].push(content); | |
} | |
node = t.cML(); | |
if((!o.symbol.eq && !o.symbol.cond) || 2==b){ | |
i = -1; | |
b = null; | |
while(t.c._eq.length>++i){ | |
content = t.cML('apply', t.cML(!!t.c._eq[i].toPrecision? t.c.req[i] : t.c._eq[i])); | |
if(!t.c.eq[t.c._eq[i]]){ | |
k=i+1; | |
t.c.eq[t.c._eq[i]]=[]; | |
while(t.c._eq.length>k){ | |
if(!t.c.eq[t.c._eq[k]]){ | |
break; | |
} | |
if(0<(j=t.c.eq[t.c._eq[k]].length)){ | |
r = t.c.eq[t.c._eq[k]][!!(b = !b) ? j-1 : 0]; | |
if(r.children.length){ | |
r.removeChild(!b?r.children[0]:r.children[r.children.length-1]); | |
} | |
} | |
r = t.cML('apply', t.cML(!!t.c._eq[k].toPrecision? t.c.req[k] : t.c._eq[k])); | |
j = -1; | |
while(t.c.eq[t.c._eq[k]].length>++j){ | |
mk_appendChild(r, t.c.eq[t.c._eq[k]][j]); | |
} | |
t.c.eq[t.c._eq[i]].push(r); | |
t.c._eq.splice(k,1); | |
} | |
} | |
j = -1; | |
while(t.c.eq[t.c._eq[i]].length>++j){ | |
mk_appendChild(content, t.c.eq[t.c._eq[i]][j]); | |
} | |
mk_appendChild(node, content); | |
} | |
} | |
t.cc(false); | |
} | |
if((!o.symbol.eq&& !o.symbol.cond) || 2==b){ | |
if(!!node){ | |
if(0<t.c._eq.length && 'condition'==t.c._eq[0] && !!t.c.eq['condition'] && !!t.c.eq['condition'][0] && !!(content = t.c.eq['condition'][0].querySelector('condition'))){ | |
mk_appendChild(content, node); | |
node = t.c.eq['condition'][0]; | |
} | |
b = false; | |
if((!node || ('declare'==mk_nodeName(node) && 1>node.children.length)) && !!o.c && 0<o.c.length){ | |
i=o.c.length; | |
j = o.l+1; | |
while(-1<--i){ | |
if(!!(k=t.o[o.c[i]].mk) && j==t.o[o.c[i]].l && ('declare'!=mk_nodeName(k) || 0<k.children.length)){ | |
b = true; | |
t.o[p]._p = true; | |
node =o.content= k.cloneNode(true); | |
break; | |
} | |
} | |
k = null; | |
}else{ | |
node = mk_unwrap(node); | |
} | |
k = (!b && o.isarg && undefined!=o.p && !!t.o[o.p].args && 1==t.o[o.p].args.length) ? o.p : p; | |
t.o[k].mk = node; | |
// t.o[k]._p = true; | |
if(!b && undefined!=t.o[k].p && (!t.o[t.o[k].p]._p || !t.o[t.o[k].p].content || ('declare'==mk_nodeName(t.o[t.o[k].p].content) && 1>t.o[t.o[k].p].content.children.length))){ | |
t.o[t.o[k].p].content = node; | |
t.o[t.o[k].p]._p = true; | |
} | |
} | |
t.cc(); | |
} | |
} | |
}; | |
})(); | |
var mk_addClass = function(o,c=''){ | |
if(!!o){ | |
mk_removeClass(o,c); | |
o.className = (o.className+' '+c).trim(); | |
} | |
}; | |
var mk_removeClass = function(o,c=''){ | |
if(!!o){ | |
o.className = o.className.split(/\s/u).filter(function(k){return !!k&&k!==c; }).join(' '); | |
} | |
}; | |
var mk_pSpace = function(t,i,j=0){ | |
var pres = t.pML(); | |
if(0<i){ | |
mk_appendChild(pres, t.pML('mspace')); | |
pres.lastChild.setAttribute('width',i+'ex'); | |
if(j){ | |
pres.lastChild.setAttribute('class','__mklinebreak'); | |
pres.lastChild.setAttribute('linebreak','newline'); | |
pres.lastChild.setAttribute('height', j+'px'); | |
} | |
} | |
return pres; | |
}; | |
var mk_pRemoveBrackets = function(t,k){ | |
var i = -1, j = -1; | |
if(PHANTOM==t.o[k].symbol.type && !!t.o[k].c && undefined!=(i=t.o[k].c[0]) && RIGHTBRACKET==t.o[i].symbol.type && undefined!=(j=t.o[k].c[t.o[k].c.length-1]) && LEFTBRACKET==t.o[j].symbol.type){ | |
if(!!t.o[i].sp && (!!t.o[i].sp[0] || !!t.o[i].sp[1])){ | |
t.o[i].symbol = mk_cloneSymbol(mkSymbolNone); | |
t.o[i].text = ''; | |
t.o[i].xbracket = true; | |
t.o[i].presentation = mk_pSpace(t,t.o[i].sp[0]+t.o[i].sp[1],t.o[i].sp[2]+t.o[i].sp[3]); | |
}else{ | |
t.x[i]=true; | |
} | |
if(!!t.o[j].sp && (!!t.o[j].sp[0]||!!t.o[j].sp[1])){ | |
t.o[j].symbol = mk_cloneSymbol(mkSymbolNone); | |
t.o[j].text = ''; | |
t.o[i].xbracket = true; | |
t.o[j].presentation = mk_pSpace(t,t.o[j].sp[0]+t.o[j].sp[1],t.o[j].sp[2]+t.o[j].sp[3]); | |
}else{ | |
t.x[j]=true; | |
} | |
//initially spacing was the same, test if this is backwards. | |
if(!!t.o[k].presentation){ | |
var node = null; | |
if(!t.o[k].presentation.lastChild){ | |
!!t.o[j].presentation && t.o[k].presentation.appendChild(t.o[j].presentation); | |
!!t.o[i].presentation && t.o[k].presentation.appendChild(t.o[i].presentation); | |
}else{ | |
node=t.o[k].presentation.firstChild; | |
while(!!node && node.textContent!='(' && !!(node=node.nextElementSibling)); | |
if(!!node){(t.o[j].symbol.type==SPACE && !!t.o[j].presentation) ? t.o[k].presentation.replaceChild(node, t.o[j].presentation) : t.o[k].presentation.removeChild(node);} | |
node=t.o[k].presentation.lastChild; | |
while(!!node && node.textContent!=')' && !!(node=node.previousElementSibling)); | |
if(!!node){(t.o[i].symbol.type==SPACE && !!t.o[i].presentation) ? t.o[k].presentation.replaceChild(node, t.o[i].presentation) : t.o[k].presentation.removeChild(node);} | |
} | |
} | |
} | |
}; | |
var mk_pMrow = function(n,force=false){ | |
return (!!n && (force||1<n.children.length) && 'mrow'!=mk_nodeName(n))?n=mk_createElementMML('mrow', n):n; | |
}; | |
var mk_pSubPow = function(t,p,node,pre=''){ | |
var i, k, o=t.o[p], v = -1, pow=pre+'power', sub=pre+'subscript'; | |
i = t.o[p][sub]; | |
k = t.o[p][pow]; | |
v = ((undefined!=i && undefined!=t.o[i][pow]) ? t.o[i][pow] : (undefined!=k ? k : -1)); | |
if(undefined!=i){ | |
node = t.pML((!pre&&(UNDEROVER==o.symbol.type||!!o.symbol.integral) ? (-1!=v ?'munderover':'munder') : (-1!=v ? 'msubsup' : 'msub')), mk_pMrow(node)); | |
node = mk_appendChild(node, mk_present(t,i)); | |
delete t.o[p][sub]; | |
undefined!=t.o[i][pow] ? (t.x[t.o[i][pow]] = true) : (v = k); | |
if(undefined!=k){ | |
t.x[k] = true; | |
} | |
} | |
if(-1<v && !o['is'+sub]){ //.issubscript sloppy | |
if(i==undefined){ | |
node = t.pML(!pre&&(UNDEROVER==o.symbol.type||!!o.symbol.integral) ? 'munder':'msup', mk_pMrow(node)); | |
} | |
node = mk_appendChild(node, mk_present(t,v)); | |
if(!!o.order && !!o.text){ | |
node = mk_appendChild(t.pML('mrow', node), t.pML('mi', o.text)); | |
} | |
delete t.o[v][pow]; | |
delete t.o[p][pow]; | |
} | |
return node; | |
}; | |
var mk_present = function(t,p){ | |
return !!t.o[p].presentation ? t.o[p].presentation : mk_presentation(t,p); | |
}; | |
var mk_pChildren = function(t,p){ | |
var c=null, i = 0, j=-1, k='', o=null, pres=t.pML(), ptag='mrow', vs=false; | |
o = t.o[p]; | |
if(!!o._c){ | |
return o.presentation; | |
} | |
c = !!o[k='args']?o.args:(!!o[k='arg']?[o.args]:(!!o[k='c']?o.c:[])); | |
j = c.length; | |
vs = !!o.vector || !!o.set; | |
if(!!o.matrixrow || !!o.crow){ | |
ptag = 'mtr'; | |
}else if(!!o.matrixcol){ | |
ptag = 'mtd'; | |
}else if(!!o.matrix || !!o.piecewise || !!vs){ | |
pres = t.pML(!!vs ?'mfenced':'mtable'); | |
if(!!o.set){ | |
pres.setAttribute('open','{'); | |
pres.setAttribute('close','}'); | |
}else if(!!o.matrix){ | |
ptag = 'mfenced'; | |
} | |
if((!o.set||1===o.set)&&(!o.vector || 1===o.vector)){ | |
pres.setAttribute('separators',''); | |
}else if(undefined!=o.symbol.separators && (!vs || undefined==o.symbol.sov)){ | |
pres.setAttribute('separators',o.symbol.separators); | |
} | |
} | |
while(-1<--j){ | |
i = c[j]; | |
if(!o._c && !t.x[i] && !!t.o[i] && !!t.o[i].presentation){ | |
t.o[i]._c = true; | |
mk_appendChild(pres, t.o[i].presentation); | |
} | |
} | |
if(!!o.crow){ | |
pres = t.pML('mtd', pres); | |
} | |
if(!o.suppresstag && undefined==o.arg){ | |
pres = 'mrow'==ptag ? mk_pMrow(pres) : t.pML(ptag, pres); | |
} | |
if(!!o.matrix){ | |
pres.setAttribute('open','['); | |
pres.setAttribute('close',']'); | |
} | |
return pres; | |
}; | |
var mk_presentation = function(t, p){ | |
var h='', i=-1, j=-1, k=-1, node=null, o=t.o[p], pres=null, txt=null; | |
if(!!o.text && Array.isArray(o.text)){ | |
if(0<(k = o.text.length)){ | |
h=o.text[0].text; | |
txt = t.pML(o.text[0].ptag,'-'==h.charAt(0)?h.replace('-',''): h); | |
if(1<k){ | |
j = 0; | |
txt = t.pML('mrow', txt); | |
while(k>++j){ | |
h+=o.text[j].text; | |
mk_appendChild(txt, t.pML(o.text[j].ptag,o.text[j].text)); | |
} | |
} | |
} | |
delete t.o[p].text; | |
} | |
k = o.symbol.type; | |
if(!!o.append && !!o.prepend){ | |
mk_pRemoveBrackets(t, p) | |
} | |
if(RIGHTBRACKET==k || LEFTBRACKET==k || LEFTRIGHT==k){ | |
if (!o.symbol.invisible){ | |
node = t.pML('mo', o.symbol.output); | |
} | |
}else if(PHANTOM==k){ | |
} else if(!!o.symbol.acc){ | |
node = t.pML(o.symbol.ptag); | |
if(!!o.args){ | |
pres = t.pML(); | |
i=-1; | |
j = o.args.length; | |
while(j>++i){ | |
mk_appendChild(pres, mk_present(t,o.args[i])); | |
t.x[o.args[i]]=true; | |
} | |
mk_appendChild(node, pres); | |
}else{ | |
mk_appendChild(node, !!txt?txt:t.pML('mi',o.text)); | |
} | |
if(!!o.symbol.acc){ | |
mk_appendChild(node, t.pML('mo',o.symbol.output)); | |
} | |
txt = null; | |
delete t.o[p].text; | |
} else if(!!o.args && DIFFERENTIAL!=k){ | |
node = o.symbol.ptag==o.symbol.output?t.pML(o.symbol.ptag) : (!!o.symbol.output ? t.pML(o.symbol.ptag,o.symbol.output) : t.pML()); | |
} else if(SPACE==k){ | |
node = mk_pSpace(t,1); | |
txt = null; | |
delete t.o[p].text; | |
} else if(TEXT==k){ | |
if(!txt){ | |
node = t.pML(o.symbol.ptag,o.text.trim()); | |
delete t.o[p].text; | |
} | |
} else if(DIFFERENTIAL==k){ | |
i = -1; j = -1; | |
node = t.pML('mo', o.symbol.output); | |
if(undefined!=o.order){ | |
if(undefined!=o.subscript){ | |
i = o.subscript; | |
delete t.o[p].subscript; | |
} | |
if(undefined!=o.power){ | |
j = o.power; | |
} | |
o.power = o.order; | |
node = t.pML('mrow', mk_pSubPow(t, p, node)); | |
delete t.o[p].power; | |
if(i>-1){ | |
o.subscript = i; | |
} | |
if(j>-1){ | |
o.power = j; | |
} | |
} | |
node = t.pML('mrow',node); | |
if(undefined!=o.calcopof){ | |
t.x[o.calcopof]=true; | |
mk_appendChild(node, mk_present(t, o.calcopof)); | |
if(undefined!=t.o[o.calcopof].order && undefined!=t.o[o.calcopof].power){ | |
delete t.o[o.calcopof].power; | |
} | |
} | |
} else if(VAR==k || CONST==k || SHAPE==k || !!o.symbol.output){ | |
node = t.pML(o.symbol.ptag, o.symbol.output); | |
if(!!o.symbol.shape){ | |
node.setAttribute('class','__mk'+o.symbol.shape); | |
mk_addClass(node,'__mkID'+p); | |
} | |
} | |
if(!!txt){ | |
node = !node ? txt : mk_appendChild(node, txt); | |
txt = null; | |
} | |
if(UNDEROVER==o.symbol.type||!!o.symbol.integral||!!o.args){ | |
node = mk_pMrow(mk_pSubPow(t,p,node)); | |
} | |
if(!!h && '-'==h.charAt(0)){ | |
node = mk_pMrow(node); | |
node.insertAdjacentElement('afterbegin',t.pML('mi','-')); | |
} | |
if(!!o.c && 0<o.c.length && !o.args && (!o.isarg || (undefined!=o.p && 2>t.o[o.p].args.length))){ | |
pres = mk_pChildren(t,p); | |
(!!node && (0<node.children.length||mk_nodeName(node)!=mk_nodeName(pres))) ? mk_appendChild(node,pres) : node=pres; | |
if(!!o.piecewise && !!node.firstChild){ | |
node.firstChild.setAttribute('columnalign','left'); | |
node.insertAdjacentElement('afterbegin',t.pML('mo','{')); | |
} | |
} | |
t.o[p].presentation = node; | |
if(!!o.origin && !!(j=mkOrigin[o.origin])){ | |
h = mk_getExt(j); | |
k = 'MathML-Presentation'; | |
pres = !!h.originalid ? h.originalid.cloneNode(true) : t.o[p].presentation; | |
pres.setAttribute('mathvariant','italic'); | |
mk_setStyle(pres, 'font-style:italic'); | |
txt = null; | |
if(!h){ | |
txt = t.pML('mtext', j); | |
}else if(undefined==h.data ||'error'==h.data){ | |
txt = t.pML('mtext', j+': '+h.data.toString()); | |
}else if('image'==h.type.substring(0,5)){ | |
k = 'text/html'; | |
txt = t.pML('img'); | |
txt.setAttribute('src', h.data); | |
txt.setAttribute('alt', h.ref); | |
txt.setAttribute('title', j.replace(mk_trimUrl(j),'').trim()); | |
txt.insertAdjacentElement('afterend',t.pML('div', t.o[p].presentation.textContent)); | |
pres = mk_appendChild(t.pML('mphantom'),t.o[p].presentation.cloneNode(true)); | |
}else if('math'==h.type){ | |
txt = mk_replaceML(t,h.data); | |
}else if('function'==h.type){ | |
if(!!h.ref && h.ref!=j){ | |
!!h.original? txt = h.original.cloneNode(true) : ((txt=t.pML('mrow')),(txt.className='__mkDeferOriginal __mkDeferID'+p)); | |
}else{ | |
txt = t.pML('mtext',j); | |
} | |
}else{ | |
txt = t.pML('mtext', j); | |
} | |
if(!!txt.textContent.trim()){ | |
pres.setAttribute('title',txt.textContent); | |
} | |
pres = mk_appendChild(t.pML('semantics'), pres); | |
txt = mk_appendChild(t.pML('annotation-xml'), txt); | |
txt.setAttribute('encoding', k); | |
t.o[p].presentation = mk_appendChild(pres, txt); | |
} | |
// t.o[p].presentation = o.presentation = (UNDEROVER==o.symbol.type||!!o.symbol.integral) ? node.cloneNode(true) : mk_pSubPow(t,p,node); | |
if(!!o.args && !o.symbol.acc){ | |
if(o.symbol.ptag!=o.symbol.output){ | |
o.presentation = mk_pMrow(t.o[p].presentation, true); | |
} | |
node = null; | |
if(!!o.symbol.separators){ | |
node = t.pML('mfenced'); | |
node.setAttribute('separators',o.symbol.separators); | |
node.setAttribute('open',''); | |
node.setAttribute('close',''); | |
}else{ | |
node = t.pML(); | |
} | |
if(!o._c){ | |
if(undefined!=(k=o.arg) || (1==o.args.length && PHANTOM==t.o[k=o.args[0]].symbol.type)){ | |
if(undefined==o.power && undefined!=t.o[k].power){ | |
o.power = t.o[k].power; | |
delete t.o[k].power; | |
} | |
if(undefined!=t.o[k].suffixop){ | |
undefined==o.suffixop ? o.suffixop=t.o[k].suffixop : Array.prototype.push.apply(o.suffixop,t.o[k].suffixop); | |
delete t.o[k].suffixop; | |
} | |
if(undefined!=t.o[k].append){ | |
undefined==o.append ? o.suffixop=t.o[k].append : Array.prototype.push.apply(o.append,t.o[k].append); | |
delete t.o[k].append; | |
} | |
if(o.symbol.ptag==o.symbol.output){ | |
// mk_pRemoveBrackets(t,k); | |
} | |
t.x[k]=true; | |
if(undefined!=o.arg && !!o.symbol.functional){ | |
j=-1; | |
node = mk_appendChild(node, mk_presentation(t, o.arg)); | |
} | |
} | |
i = -1; | |
j = o.args.length; | |
while(j>++i){ | |
// if(!!t.o[ o.args[i]]._c) | |
// continue; | |
pres = mk_pMrow(mk_present(t, o.args[i])); | |
//node = mk_pMrow(o.symbol.ptag!=o.symbol.output ? mk_present(t, o.args[i]) : mk_pChildren(t, o.args[i]), true); | |
node = mk_appendChild(node, pres.cloneNode(true)); | |
t.x[o.args[i]]=true; | |
} | |
t.o[p].presentation = mk_appendChild(t.o[p].presentation, node); | |
if(o.symbol.ptag==o.symbol.output && (undefined!=o.subscript || undefined!=o.power)){ | |
// t.o[p].presentation = o.presentation = mk_pSubPow(t,p,t.o[p].presentation.cloneNode(true)); | |
} | |
} | |
} | |
if(!o.presentation){ | |
t.o[p].presentation = o.presentation = t.pML('mrow'); | |
} | |
if(!!o.text && '-'==o.text && (!!o.c || !!o.args)){ | |
t.o[p].presentation = mk_appendChild(mk_appendChild(t.pML('mrow'), t.pML('mi','-')),t.o[p].presentation); | |
} | |
if(!!o.append && 0<(j=o.append.length)){ | |
// node = mk_pResuffix(t,p); | |
o.presentation = mk_pMrow(t.o[p].presentation, true); | |
i=-1; | |
while(j>++i){ | |
// !!node ? node.insertAdjacentElement('afterend', t.pML(o.append[j].ptag, o.append[j].output)): | |
mk_appendChild(t.o[p].presentation, t.pML(o.append[i].ptag, o.append[i].output)); | |
} | |
} | |
if(!!o.prepend && 0<(j=o.prepend.length)){ | |
o.presentation = mk_pMrow(t.o[p].presentation, true); | |
i = -1; | |
while(j>++i){ | |
t.o[p].presentation.insertAdjacentElement('afterbegin',t.pML(o.prepend[i].ptag, o.prepend[i].output)); | |
} | |
} | |
if(!!o.ofprefixop && !t.x[p]){ | |
o.presentation = mk_pMrow(t.o[p].presentation, true); | |
t.o[p].presentation.insertAdjacentElement('afterbegin',t.pML(o.ofprefixop.ptag, o.ofprefixop.output)); | |
t.x[o.suffixof] = true; | |
if(undefined!=o.suffixof){ | |
t.o[p].presentation.insertAdjacentElement('afterbegin',mk_presentation(t, o.suffixof)); | |
} | |
} | |
if(!!o.ofsuffixop && !t.x[p]){ | |
t.x[o.prefixof] = true; | |
if('mlongdiv'==o.ofsuffixop.ptag){ | |
node = mk_appendChild(t.pML('mtable'), t.pML('mtr')); | |
node.setAttribute('class','__mk'+o.ofsuffixop.ptag); | |
mk_appendChild(node.children[0], t.pML('mtd')); | |
mk_appendChild(node.children[0], t.pML('mtd')); | |
mk_appendChild(node.children[0], t.pML('mtd')); | |
mk_appendChild(node.children[0].children[0], t.o[p].presentation); | |
mk_appendChild(node.children[0].children[1], mk_presentation(t, o.prefixof)); | |
node.children[0].children[0].setAttribute('columnalign','right'); | |
node.children[0].children[1].setAttribute('columnalign','left'); | |
t.o[p].presentation = mk_pMrow(node, true); | |
node = node.children[0].children[1]; | |
node.setAttribute('style','border-top:1px solid #000;border-left:1px solid #000;'); | |
//t.o[p].presentation.setAttribute('style', 'margin-top:-'+mkLineHeight+'px'); | |
}else{ | |
o.presentation = mk_pMrow(t.o[p].presentation, true); | |
mk_appendChild(t.o[p].presentation, t.pML(o.ofsuffixop.ptag, o.ofsuffixop.output)); | |
mk_appendChild(t.o[p].presentation, mk_presentation(t, o.prefixof)); | |
} | |
} | |
t.o[p].presentation = mk_pSubPow(t,p,mk_pSubPow(t,p,t.o[p].presentation),'em'); | |
if(1==mkDebug){ | |
console.log((t.o[p].p!=undefined?t.o[p].p:-1)+':'+p+'('+t.o[p].l+') '+t.o[p].presentation.outerHTML); | |
} | |
if('stackrel'==o.symbol.input && !!o.stackname){ | |
txt = null; | |
node = null; | |
pres = null; | |
h = mk_getExt(o.stackname); | |
if(undefined!=o.stackref){ | |
delete t.o[o.stackref].presentation; | |
delete t.o[o.stackref]._c; | |
}else if(undefined!=o.arg){ | |
delete t.o[o.arg].presentation; | |
delete t.o[o.arg]._c; | |
} | |
if(undefined!=o.stackrel && (!t.o[o.stackrel].presentation || ('declare'==mk_nodeName(t.o[o.stackrel].presentation) && 1>t.o[o.stackrel].presentation.children.length)) && !!t.o[o.stackrel].c && 1==t.o[o.stackrel].c.length && undefined!=(j=t.o[o.stackrel].c[0]) && !!t.o[j].presentation){ | |
t.o[o.stackrel].presentation = t.o[j].presentation.cloneNode(true); | |
t.x[o.stackrel] = true; | |
} | |
k = 'MathML-Presentation'; | |
if(undefined===o.stackref && undefined===o.arg){ | |
pres = t.pML(); | |
txt = t.pML('mtext', o.stackname); | |
}else if(!h || undefined==h.data || 'error'==h.data){ | |
pres = undefined!==o.stackref ? mk_present(t, o.stackref).cloneNode(true) : mk_present(t, o.arg).cloneNode(true); | |
txt = t.pML('mtext', o.stackname+': '+h.ref.toString()); | |
}else if('image'==o.stype.substring(0,5)){ | |
k = 'text/html'; | |
node = t.pML('img'); | |
node.setAttribute('src', h.data); | |
node.setAttribute('alt', h.ref); | |
node.setAttribute('title', o.stackname.replace(mk_trimUrl(o.stackname),'').trim()); | |
if(undefined!==o.stackref){ | |
pres = mk_appendChild(t.pML('mphantom'),mk_present(t, o.stackref)); | |
txt = node; | |
}else{ | |
pres = t.pML('mphantom'); | |
txt = node; | |
} | |
pres = mk_pMrow(pres,1); | |
}else if('math'==o.stype){ | |
node = mk_replaceML(t,h.data); | |
if(undefined!==o.stackref){ | |
//should look different and have a title | |
pres = mk_present(t, o.stackref).cloneNode(true); | |
txt = node.cloneNode(true); | |
}else{ | |
pres = node; | |
txt = t.pML('mtext',h.ref); | |
} | |
}else if('function'==o.stype){ | |
//mk_setExt(o.stackname,null,{original:mk_present(t, o.stackrel || o.arg).cloneNode(true)}); | |
if(undefined!==o.stackref){ | |
pres = mk_present(t, o.stackref).cloneNode(true); | |
txt = mk_present(t, o.stackrel).cloneNode(true); | |
mk_setExt(o.stackname,null,{original:txt.cloneNode(true),originalid:pres.cloneNode(true)}); | |
}else{ | |
txt = t.pML(); | |
pres = mk_present(t, o.arg).cloneNode(true); | |
mk_setExt(o.stackname,null,{original:pres.cloneNode(true),originalid:txt.cloneNode(true)}); | |
} | |
}else{ | |
if(undefined!==o.stackref){ | |
pres = mk_present(t, o.stackref).cloneNode(true); | |
txt = t.pML('mtext', o.stackname); | |
mk_setExt(o.stackname,null,{originalid:pres.cloneNode(true),original:txt.cloneNode(true)}); | |
}else{ | |
txt = t.pML(); | |
pres = t.pML('mtext', o.stackname); | |
mk_setExt(o.stackname,null,{original:pres.cloneNode(true)}); | |
} | |
} | |
pres.setAttribute('mathvariant','italic'); | |
mk_setStyle(pres, 'font-style:italic'); | |
if(!!txt.textContent.trim()){ | |
pres.setAttribute('title',txt.textContent); | |
} | |
pres = mk_appendChild(t.pML('semantics'), pres); | |
txt = mk_appendChild(t.pML('annotation-xml'), txt); | |
txt.setAttribute('encoding', k); | |
t.o[p].presentation = mk_appendChild(pres, txt); | |
if(undefined!==o.stackref){ | |
t.x[o.stackref] = true; | |
t.x[o.stackrel] = true; | |
} | |
} | |
if(undefined!=t.o[p].attr){ | |
o = t.o[t.o[p].attr]; | |
t.o[p].presentation = mk_appendChild(t.pML('mstyle'), t.o[p].presentation); | |
for(k in o.symbol.attr){ | |
t.o[p].presentation.setAttribute(k, o.symbol.attr[k]); | |
} | |
mk_addClass(t.o[p].presentation,'__mkID'+p); | |
} | |
o = t.o[p]; | |
if(!!o.sp && (!!o.sp[0] || !!o.sp[1])){ | |
t.o[p].presentation = mk_pMrow(o.presentation, true); | |
if(!!o.sp[0]){ | |
t.o[p].presentation.insertAdjacentElement('afterbegin',mk_pSpace(t,o.sp[0],o.sp[2])); | |
} | |
if(!!o.sp[1]){ | |
t.o[p].presentation.insertAdjacentElement('beforeend',mk_pSpace(t,o.sp[0],o.sp[2])); | |
} | |
} | |
return mk_unwrap(t.o[p].presentation).cloneNode(true); | |
}; | |
mk.checkR = function(str, pos=-1){ | |
var cref={}, i=-1, k, o=null; | |
if(-1<pos){ | |
cref = Object.assign(mkCref[0], mkCref[1]); | |
for(k in cref){ | |
while(-1<(i=str.indexOf(k)) && i<(pos-k.length)){ | |
str = str.substr(0,i)+cref[k]+str.substr(i+k.length); | |
pos = pos - k.length + cref[k].length; | |
} | |
} | |
} | |
o = mk.parseR(str.slice(0,pos), 0); | |
if(!!o.found && (o.symbol.input!=o.symbol.output || 1<o.symbol.output.length || ('a'<=o.symbol.output && o.symbol.output<='z'))){ | |
return o.pos; | |
} | |
return false; | |
}; | |
var mk_reSpace = function(t,p){ | |
var k=p, o=t.o[p], m=t.o.length; | |
if(!o.sp){return;} | |
if(0<o.sp[0]||0<o.sp[2]){ | |
while(m>++k && !t.o[k].sp); | |
if(m>k){ | |
t.o[k].sp[1] += o.sp[0]; | |
t.o[k].sp[3] += o.sp[2]; | |
o.sp[0] = 0; | |
o.sp[2] = 0; | |
} | |
} | |
if(0<o.sp[1]||0<o.sp[3]){ | |
k = p; | |
while(-1<--k && !t.o[k].sp); | |
if(-1<k){ | |
t.o[k].sp[0] += o.sp[1]; | |
t.o[k].sp[2] += o.sp[3]; | |
o.sp[1] = 0; | |
o.sp[3] = 0; | |
} | |
} | |
}; | |
mk.reParen = function(str){ //naive reparenthesization | |
var c='', len=0, rparen={}; | |
for(c in mkParen[0]){ | |
rparen[c] = []; | |
rparen[mkParen[0][c]] = []; | |
} | |
str=mk_escRef(str); | |
len = str.length; | |
while(-1<--len){ | |
c = str.charAt(len); | |
if(!!mkParen[1][c]){ | |
rparen[c].unshift(len); | |
}else if(!!mkParen[0][c]){ | |
rparen[mkParen[0][c]].shift(); | |
} | |
} | |
for(c in rparen){ | |
len = -1; | |
while(rparen[c].length>0){ | |
len = rparen[c].pop(); | |
str = str.substr(0,len)+mkParen[1][c]+str.substr(len); | |
} | |
if(len>-1){ | |
rparen[c] = -1; | |
} | |
} | |
len = -1; | |
while(str.length>++len){ | |
c = str.charAt(len); | |
if(!!mkParen[0][c]){ | |
rparen[c].push(len); | |
}else if(!!mkParen[1][c]){ | |
rparen[mkParen[1][c]].pop(); | |
rparen[c] = len; | |
} | |
} | |
for(c in rparen){ | |
if(Array.isArray(rparen[c])){ | |
while(rparen[c].length>0){ | |
len = rparen[c].pop(); | |
str = (-1!=rparen[mkParen[0][c]] && len>=rparen[mkParen[0][c]]) ? str.replace(/([\]\}\)]+)$/ug,mkParen[0][c]+'$1') : str.substr(0,len+1)+mkParen[0][c]+str.substr(len+1); | |
} | |
} | |
} | |
return str;//.replace(/^\(\)/ugm,''); | |
}; | |
var mk_aCloseParen = function(str, c, pos){ | |
var j = 1, k = pos, s = ''; | |
if('|'==c){ | |
while((k = str.lastIndexOf(c,--k))!=-1 && (j=j?0:1) && k>0 ); | |
return !j; | |
} | |
if('/'==c){ | |
j=1; | |
while((k = str.lastIndexOf(c,--k))!=-1 && (((s=str.charAt(k+1)) && '('!=s && '['!=s && '{'!=s && '|'!=s && ('-'!=s || (s=str.charAt(pos+2)) || (s=str.charAt(k+1))) && (('a'>s||s>'z') && ('A'>s||s>'Z') && ('0'>s||s>'9'))) ? ++j : true) && 0<k ); | |
k = pos; | |
while((k = str.lastIndexOf('\\',--k))!=-1 && (--j) && 0<k ); | |
return !j; | |
} | |
return false; | |
}; | |
var mk_adjLevel = function(t,c,a=[],d=[],r={}){ | |
var i = -1, j = -1, k, v; | |
var fadjLevel = function(l){ | |
j = ++l; | |
--l; | |
v = r[k][l]; | |
if(-1!=c.indexOf(v)){ | |
r[k][l]=-1; | |
if(undefined!==r[k][j] && v>r[k][j]){ | |
r[k][j] = v; | |
} | |
} | |
}; | |
if(1>(i = c.length)){ | |
return; | |
} | |
for(k in r){ | |
Object.keys(r[k]).filter(fadjLevel); | |
} | |
k = (0<i && undefined!==t.o[c[0]].p && undefined!==t.o[t.o[c[0]].p].l) ? t.o[t.o[c[0]].p].l : t.o[c[0]].l; | |
++k | |
while(-1<--i){ | |
if(-1!=(j = d.indexOf(c[i]))){ | |
d[j++]=-1; | |
if(undefined==d[j] || d[j]<c[i]){ | |
d[j] = c[i]; | |
} | |
} | |
if(-1!=(j = a.indexOf(c[i]))){ | |
a[j++]=-1; | |
if(undefined==a[j] || a[j]<c[i]){ | |
a[j] = c[i]; | |
} | |
} | |
t.o[c[i]].l = k; | |
if(!!t.o[c[i]].c){ | |
mk_adjLevel(t, t.o[c[i]].c, a, d, r); | |
} | |
} | |
}; | |
//this should rarely happen (...creating phantom for fn->subscript->power->args) | |
var mk_adjBreadth = (function(){ | |
var ops = ['subscript', 'power','op','intop','calcop','calcopof','prefixof', | |
'suffixof','stackop','stackrel','stackref','powerof','subscriptof','empowerof','emsubscriptof','empower','emsubscript','attr']; | |
return function(t,p,c,e,r,a){ | |
var i = c.length, j = 0, k = 0, o=null; | |
while(-1<--i){ | |
j = c[i].length; | |
while(-1<--j){ | |
if(p<=c[i][j]){ | |
++c[i][j]; | |
} | |
} | |
} | |
i = a.length; | |
while(-1<--i){ | |
j = a[i].length; | |
while(-1<--j){ | |
if(p<=a[i][j]){ | |
++a[i][j]; | |
} | |
} | |
} | |
for(i in r){ | |
j = r[i].length; | |
while(-1<--j){ | |
if(p<=r[i][j]){ | |
++r[i][j]; | |
} | |
} | |
} | |
i = t.o.length; | |
while(-1<--i){ | |
if(!!t.x[i] && i>=p){ | |
delete t.x[i]; | |
t.x[i+1]=true; | |
} | |
j = ops.length; | |
while(-1<--j){ | |
k=ops[j]; | |
if(undefined!=t.o[i][k] && p<=t.o[i][k]){ | |
++t.o[i][k]; | |
} | |
} | |
if(!!t.o[i].c){ | |
j = t.o[i].c.length; | |
while(-1<--j){ | |
if(p<=t.o[i].c[j]){ | |
++t.o[i].c[j]; | |
} | |
} | |
} | |
if(undefined!=t.o[i].arg && p<=t.o[i].arg){ | |
++t.o[i].arg; | |
} | |
if(!!t.o[i].args){ | |
j = t.o[i].args.length; | |
while(-1<--j){ | |
if(p<=t.o[i].args[j]){ | |
++t.o[i].args[j]; | |
} | |
} | |
} | |
} | |
e._br={}; | |
for(i in e.br){ | |
i = parseInt(i,10); | |
if(p<=e.br[i]){ | |
++e.br[i]; | |
} | |
if(p<=i){ | |
undefined!=e.br[i+1] ? e._br[i+1]=e.br[i] : ((e.br[i+1] = e.br[i]),delete e.br[i]); | |
} | |
} | |
for(i in e._br){ | |
i = parseInt(i,10); | |
e.br[i]=e._br[i]; | |
} | |
e._sp={}; | |
for(i in e.sp){ | |
i = parseInt(i,10); | |
if(p<=e.sp[i]){ | |
++e.sp[i]; | |
} | |
if(p<=i){ | |
undefined!=e.sp[i+1] ? e._sp[i+1]=e.sp[i] : ((e.sp[i+1] = e.sp[i]),delete e.sp[i]); | |
} | |
} | |
for(i in e._sp){ | |
i = parseInt(i,10); | |
e.sp[i]=e._sp[i]; | |
} | |
for(i in e.l){ | |
i = parseInt(i,10); | |
if(p<=i){ | |
++e.l[i]; | |
} | |
} | |
for(j in e.i){ | |
j = parseInt(j,10); | |
i = p<=e.i[j] ? e.i[j]+1 : e.i[j]; | |
k = p<=j ? j+1 : j; | |
if(i!=e.i[j]){ | |
e.f[i] = k; | |
e.i[k] = i; | |
delete e.f[e.i[j]]; | |
} | |
if(k!=j){ | |
e.i[k] = i; | |
e.f[i] = k; | |
delete e.i[j]; | |
} | |
} | |
}; | |
})(); | |
var mk_b64d = (function(){ | |
var b64 = { A:0, Q:16, g:32, w:48, B:1, R:17, h:33, x:49, C:2, S:18, i:34, y:50, D:3, T:19, j:35, z:51, E:4, U:20, k:36, 0:52, F:5, V:21, l:37, 1:53, G:6, W:22, m:38, 2:54, H:7, X:23, n:39, 3:55, I:8, Y:24, o:40, 4:56, J:9, Z:25, p:41, 5:57, K:10, a:26, q:42, 6:58, L:11, b:27, r:43, 7:59, M:12, c:28, s:44, 8:60, N:13, d:29, t:45, 9:61, O:14, e:30, u:46, '+':62, P:15, f:31, v:47, '/':63 }, b0=[0, 48, 60, 63], b1 = [63, 15, 3, 0], b2=[2, 4, 6, 0], b3=[0, 4, 2, 0]; | |
return function(b=''){ | |
var i = b.indexOf(','), j = -1, f = b.length, r0, r1, a =[]; | |
while(f>++i){ | |
undefined!=(r1 = b64[b.charAt(i)]) ? (((0 == ++j) && ((r0 = (r1 & b1[j])<<b2[j]),1)) | |
|| (3==j && (a.push(parseInt(r0 | (r1 & b0[j]),10)),(j=-1)) ) || (a.push(parseInt(r0 | ((r1 & b0[j])>>b3[j]),10)),(r0 = (r1 & b1[j]) << b2[j])) ) : ('='==b.charAt(i)&&(i=f)); | |
} | |
return a; | |
}; | |
})(); | |
var mk_b64e = function(b=''){ | |
return window.btoa(encodeURIComponent(b).replace(/%([0-9A-F]{2})/g, function(m, p){return String.fromCharCode('0x' + p);})); | |
}; | |
var mk_typeImageUrl = function(k=''){ | |
var h = mk_getExt(k), i=-1, m=''; | |
if(!h || !h.data || 0!=h.data.indexOf('data:image/')){ | |
return 'text/html'; | |
} | |
m = h.data.substring(5,h.data.indexOf(';')); | |
if(-1!=(i=h.data.indexOf(';text/plain,'))){ | |
mk_setExt(k, h.data.slice(i+12), {type:m}); | |
h.original = h.data; | |
}else{ | |
window.setTimeout(function(){ | |
try{ | |
u = URL.createObjectURL(new Blob([new Uint8Array(mk_b64d(h.data) )], {type: m})); | |
}catch(err){ | |
console.log(err); | |
} | |
if(!!u){ | |
mk_setExt(k, u, {type:m}); | |
}else{ | |
mk_setExt(k, null, {type:m}); | |
} | |
},10); | |
} | |
return m; | |
}; | |
var mk_replaceML = function(t,o){ | |
var node = t.cML(); | |
node.innerHTML = (!!o.innerHTML ? o.innerHTML : o.toString()).replace(/(?:^<math[^>]*>)|(?:<\/math>)/uig,'').replace(/(?:^<mstyle[^>]*>)|(?:<\/mstyle>)/uimg,''); | |
for(i=-1;node.children.length>++i;){ | |
node.children[i].setAttribute('xmlns',mkMathns); | |
} | |
node.innerHTML = node.innerHTML.replace(/\b(xmlns(:[^=]*)?\s*=\s*")[^"]*"/uig,'').replace(/(\/?>)/u,' xmlns="'+mkMathns+'"$1'); | |
node = node.cloneNode(true); | |
if(!!o.mathKey){ | |
node.mathKey = o.mathKey; | |
} | |
node.firstChild.removeAttribute('xmlns'); | |
return node; | |
}; | |
var mk_escRef = function(str=''){ | |
if(!mkCref[2]){ | |
var fn = function(v){return '(?:'+v.replace(/[\^\\\[\]\{\}()*+?.,$|#]/ug, '\\$&')+')';}; | |
mkCref[2] = new RegExp('('+Object.keys(mkCref[0]).map(fn).join('|')+')', 'uig'); | |
mkCref[3] = new RegExp('('+Object.keys(mkCref[1]).map(fn).join('|')+')', 'uig'); | |
} | |
return str.replace(/([0-9]),([0-9])/ug,'$1,$2').replace(mkCref[2], function(m0,m1){ | |
return mkCref[0][m1];}).replace(/([a-z0-9])\|([a-z0-9])/iug,'$1\u01C0$2').replace(mkCref[3], function(m0,m1){return mkCref[1][m1];}); | |
//.trimEnd(); | |
}; | |
var mk_linkParent = function(t,p){ | |
var i = -1, j = -1; | |
if(!!t.o[p].c){ | |
i = t.o[p].c.length; | |
while(-1<--i){ | |
t.o[j = t.o[p].c[i]].p = p; | |
if(!!t.o[j].c){ | |
mk_linkParent(t,j); | |
} | |
} | |
} | |
if(t.o[p].args){ | |
i = t.o[p].args.length; | |
while(-1<--i){ | |
t.o[j=t.o[p].args[i]].p = p; | |
if(!!t.o[j].c){ | |
mk_linkParent(t,j); | |
} | |
} | |
} | |
}; | |
var mk_freeze = function(t,p){ | |
if(-1<p){ | |
var o = t.o[p]; | |
undefined!=o.emsubscript && (o.__emsubscript = o.emsubscript); | |
undefined!=o.empower && (o.__empower = o.empower); | |
undefined!=o.power && (o.__power = o.power); | |
undefined!=o.subscript && (o.__subscript = o.subscript); | |
undefined!=o.text && (o.__text = o.text); | |
} | |
}; | |
var mk_thaw = function(t,p){ | |
var o = null; | |
if(0>p||!(o=t.o[p])){ | |
return false; | |
} | |
if(undefined!=o.__thaw){ | |
return o.__thaw; | |
} | |
delete o.empower; | |
delete o.emsubscript; | |
delete o.power; | |
delete o.subscript; | |
delete o.text; | |
undefined!=o.__empower && (o.empower = o.__empower); | |
undefined!=o.__power && (o.power = o.__power); | |
undefined!=o.__emsubscript && (o.emsubscript = o.__emsubscript); | |
undefined!=o.__subscript && (o.subscript = o.__subscript); | |
undefined!=o.__text && (o.text = o.__text); | |
return o.__thaw = (!t.x[p] || PHANTOM==t.o[p].symbol.type); | |
}; | |
var mk_nextInfix = function(o, c, p, l, r=false){ | |
(-1<p&&!!o[c[l][p]].symbol.isprefixop&&(-1<--p)),(-1<p&&undefined!=o[c[l][p]].prefixof&&(-1<--p)),(-1<p&&undefined!=o[c[l][p]].suffixof),(-1<p&&!!o[c[l][p]].symbol.suffixop&&(-1<--p)); | |
//return (!r && '/'==t.o[c[level][p]].symbol.input&&INFIX==t.o[c[level][p]].symbol.type&&-1<--p)? nextInfix(o, c, p, l, true):p; //todo: create phantom | |
return p; | |
}; | |
var mk_t = function(){ | |
var q='', t = {cs:0, ps:0, o: [], x:{}, s:{}, i:{}, par:false, pML:null, cML:null, cc:null, fk:['rel','mem','op','d','rop']}, f1 = null, m1=null, xml=document.implementation.createDocument(null,'math'); | |
f1 = function(v){ ++t.c.d[v][1]; }; | |
m1 = function(v){ | |
t.c.d[v][2] = !!t.c.rop[t.c.d[v][0]].symbol.pri?1:0; | |
t.c.d[v][3] = parseInt(t.c.rop[t.c.d[v][0]].mem,10); | |
return t.c.d[v]; | |
}; | |
t.cc = function(clr=true){ | |
t.c = clr ? {d:{},op:{},arg:[],_eq:[],eq:{},req:{},rel:{},mem:{}, f1:f1, m1:m1} : {d:{},op:{},arg:[],_eq:t.c._eq,eq:t.c.eq,req:t.c.req,rel:{},mem:{}, f1:f1, m1:m1}; | |
}; | |
t.cc(); | |
t.cremap = function(v){ | |
if(!t.c.adj){return false;} | |
if('mem'==v && t.c.mem[t.c.adj[1]]){ | |
t.c.mem[t.c.adj[1]] = t.c.adj[1]; | |
}else if('rel'==v){ | |
for(q in t.c.rel){ | |
if(!!t.c.rel[q][t.c.adj[0]]){ | |
Object.defineProperty(t.c.rel[q], t.c.adj[1], Object.getOwnPropertyDescriptor(t.c.rel[q], t.c.adj[0])); | |
delete t.c.rel[q][t.c.adj[0]]; | |
} | |
} | |
} | |
if(undefined!=t.c[v][t.c.adj[0]]){ | |
if('d'==v){ | |
t.c.d[t.c.adj[0]][0] = t.c.adj[1]; | |
} | |
Object.defineProperty(t.c[v], t.c.adj[1], Object.getOwnPropertyDescriptor(t.c[v], t.c.adj[0])); | |
delete t.c[v][t.c.adj[0]]; | |
} | |
return false; | |
}; | |
t.pML = function(i=-1,s=null){ | |
var node = null, o=null; | |
if(-1==i || (undefined==i.charAt && undefined==(o=t.o[i]))){ | |
return mkDoc.createElement('declare'); | |
} | |
if(!!((!o && !!s && !!s.symbol) ? o=s : o)){ | |
if(!!o.symbol.ctag){ | |
node = mk_createElementMML(o.symbol.ctag, null); | |
}else{ | |
node = (!!o.text && undefined==o.text.charAt && undefined==o.text.toPrecision) ? o.text :((!!o.text || !!o.symbol.output) ? mk_createElementMML(o.symbol.ptag, mkDoc.createTextNode(!!o.text? o.text : o.symbol.output)): mkDoc.createElement('declare')); | |
} | |
if(!s || !s.symbol){ | |
return mk_unwrap(node); | |
} | |
} | |
if(undefined!=(o=mkNames[i]) && !!mkSymbols[o].ptag){ | |
return mk_createElementMML(mkSymbols[o].ptag, null); | |
} | |
return mk_unwrap(mk_createElementMML(i, null == s ? node : ((undefined!=s.charAt||!!s.toPrecision)?mkDoc.createTextNode(s):s))); | |
}; | |
t.cML = function(i=-1,s=null){ | |
var node = null, o=null; | |
if(-1==i || (undefined==i.charAt && undefined==(o=t.o[i]))){ | |
return xml.createElement('declare'); | |
} | |
if(!!((!o && !!s && !!s.symbol) ? o=s : o)){ | |
if(!!o.symbol.ctag){ | |
node = mk_createElementMML(o.symbol.ctag, null, xml); | |
}else{ | |
node = (!!o.text && undefined==o.text.charAt && undefined==o.text.toPrecision) ? o.text :((!!o.text || !!o.symbol.output) ? mk_createElementMML('mn'==o.symbol.ptag?'cn':'ci', xml.createTextNode(!!o.text? o.text : o.symbol.output),xml): xml.createElement('declare')); | |
} | |
if(!s || !s.symbol){ | |
if(!!o && !!o.symbol && !!o.symbol.ctype && !!node.setAttribute){ | |
node.setAttribute('type',o.symbol.ctype); | |
if('function' == o.symbol.ctype){ | |
node = mk_createElementMML('apply', node); | |
} | |
} | |
return mk_unwrap(node); | |
} | |
} | |
if(undefined!=(o=mkNames[i]) && !!mkSymbols[o].ctag){ | |
return mk_createElementMML(mkSymbols[o].ctag, null, xml); | |
} | |
return mk_unwrap(mk_createElementMML('mn'==i?'cn':('mi'==i?'ci':i), | |
null != s ? ( | |
((!!node && !!s.symbol) && (!s.symbol.ctype ? true : | |
'c'==mk_nodeName(node).charAt(0) ? (node.setAttribute('type',s.symbol.ctype),true) : ( | |
(-1!='|cn|ci|csymbol|'.indexOf('|'+mk_nodeName(node.firstChild)+'|'))? | |
(node.firstChild.setAttribute('type',s.symbol.ctype),true) : true | |
) | |
) | |
) ? node : (undefined!=s.charAt||!!s.toPrecision)?xml.createTextNode(s):s):null, xml)); | |
}; | |
return t; | |
}; | |
mk.parseR = function(str, until=null){ | |
var a=[], b=[], c=[], d=[], e={l:{},i:{},f:{},sp:{},br:{}}, eq='', h=0, i=0, j=0, k=0, l=0, level=0, lr=0, node=null, o=null, p=0, q=0, r={so:[],ps:[],'_':[],'^':[]}, rc={m:{},s:{},v:{},mr:{},c:{}}, t, to=0, rparen=mkParen[2],br=[],sr=0; | |
t = mk_t(); | |
if(null!==until){ | |
to = true; | |
} | |
eq = str = to ? mk_escRef(str) : mk.reParen(str); | |
q = str.length; | |
mkExt.cache[0]=mkExt.cache[3]; | |
mkExt.cache[1]=mkExt.cache[4]; | |
while(!!str){ | |
if(q==((o = mk_rgetSymbol(str, ((!!o&&!!o.sp)?o.sp:[0,0,0,0]),false,t))).pos){ | |
--o.pos; | |
} | |
q=o.pos; | |
if(NONE==o.symbol.type){ | |
str = str.slice(0, o.pos>0?o.pos:0); | |
continue; | |
} | |
i = j = t.o.length; | |
k = i-1; | |
t.o.push(o); | |
if('/'==t.o[i].symbol.input && (0==i || (LEFTBRACKET!=t.o[k].symbol.type && LEFTRIGHT!=t.o[k].symbol.type && (!!t.o[k].text.charAt?'-'!=t.o[k].text.charAt(0):(t.o[k].text[0]?'-'!=t.o[k].text[0].text.charAt(0):false)) && (!(h=eq.charAt(o.pos+1)) || (('a'>h||h>'z') && ('A'>h||h>'Z') && ('0'>h||h>'9'))))) && !!mk_aCloseParen(str, '/', o.pos)){ | |
t.o[i].symbol.output = ')'; | |
t.o[i].symbol.type = RIGHTBRACKET; | |
} | |
l = 1==t.o[i].symbol.input.length ? ((LEFTBRACKET==t.o[i].symbol.type || (LEFTRIGHT==t.o[i].symbol.type && 0<i && ((LEFTRIGHT!=t.o[k].symbol.type && 1==lr)||(LEFTRIGHT==t.o[k].symbol.type && (2==lr || (1==lr && undefined==(level=--t.o[k].l) )) )) && !(lr=0))) ? level-- : ((RIGHTBRACKET==t.o[i].symbol.type || (LEFTRIGHT==t.o[i].symbol.type && mk_aCloseParen(str, '|', o.pos) && 0==lr++ && (0==i||LEFTRIGHT!=t.o[k].symbol.type))) ? level++ : level)):level; | |
if(undefined==a[level]){ | |
a[level] = -1; | |
d[level] = -1; | |
c[level] = []; | |
} | |
if(undefined==a[l]){ | |
a[l] = -1; | |
d[l] = -1; | |
c[l] = []; | |
} | |
if(!!t.o[i].symbol.iscalcop){ | |
k = i; | |
if(undefined!==r['^'][level]&& -1!=r['^'][level]){ | |
k = r['^'][level]; | |
} | |
while(-1<--k && level!=t.o[k].l); | |
if(-1<k && (VAR==t.o[k].symbol.type || (CONST==t.o[k].symbol.type && !!t.o[k].symbol.sym) || PHANTOM==t.o[k].symbol.type || !!t.o[k].args)){ | |
t.o[k].calcop = i; | |
t.o[i].calcopof = k; | |
}else if(-1<k && INFIX==t.o[k].symbol.type && '/'==t.o[k].symbol.input){ | |
while(-1<--k && (level!=t.o[k].l || '^'==t.o[k].symbol.input || undefined!=t.o[k].calcop || !!t.o[k].symbol.iscalcop) && !t.o[k].symbol.arith); | |
if(-1<k && (VAR==t.o[k].symbol.type || PHANTOM==t.o[k].symbol.type|| (CONST==t.o[k].symbol.type && !!t.o[k].symbol.sym) || !!t.o[k].args)){ | |
t.o[k].calcop = i; | |
t.o[i].calcopof = k; | |
} | |
} | |
if(undefined==t.o[i].calcopof){ | |
t.o[i].symbol.type = VAR; | |
t.o[i].symbol.ptag='mi'; | |
delete t.o[i].symbol.calcop; | |
delete t.o[i].symbol.ctag; | |
} | |
}else if(0<i&&!!t.o[i].symbol.attr){ | |
t.x[i]=true; | |
t.o[k=i-1].attr=i; | |
t.o[k].sp[0]+=t.o[i].sp[0]; | |
t.o[k].sp[1]+=t.o[i].sp[1]; | |
t.o[k].sp[2]+=t.o[i].sp[2]; | |
t.o[k].sp[3]+=t.o[i].sp[3]; | |
t.o[i].sp[0]=t.o[k].sp[0]; | |
t.o[i].sp[1]=t.o[k].sp[1]; | |
t.o[i].sp[2]=t.o[k].sp[2]; | |
t.o[i].sp[3]=t.o[k].sp[3]; | |
} | |
j = i; | |
if(0<i && !!t.o[i].symbol.infixop){ | |
str = str.slice(0, t.o[i].pos); | |
t.o[k=i-1].ofprefixop = t.o.pop().symbol; | |
r.ps[level] = k; | |
continue; | |
} | |
if(!!t.o[i].symbol.isprefixop){ | |
k = i; | |
while(-1<--k && (level!=t.o[k].l || ('^'==t.o[k].symbol.input|| '_'==t.o[k].symbol.input) && -1!=--k)); | |
if(k>-1 && (VAR==t.o[k].symbol.type || PHANTOM==t.o[k].symbol.type || (CONST==t.o[k].symbol.type && !!t.o[k].symbol.sym) || !!t.o[k].args)){ | |
str = str.slice(0, t.o[i].pos); | |
p = t.o.pop().symbol; | |
if(p.functional){ | |
delete p.functional; | |
delete p.ctag; | |
} | |
if(!!p.supressmath){ | |
t.o[k].symbol.supressmath=true; | |
} | |
!!t.o[k].prefixop ? t.o[k].prefixop.push(p) : t.o[k].prefixop = [p]; | |
!!t.o[k].prepend ? t.o[k].prepend.push(p) : t.o[k].prepend = [p]; | |
if(!!p.append){ | |
p = mk_cloneSymbol(p); | |
p.output=p.append; | |
!!t.o[k].append ? t.o[k].append.unshift(p) : t.o[k].append = [p]; | |
} | |
continue; | |
} | |
} | |
if(!!t.o[i].symbol.issuffixop){ | |
str = str.slice(0, t.o[i].pos); | |
!!r.so[level] ? r.so[level].unshift(t.o.pop().symbol) : r.so[level]=[t.o.pop().symbol]; | |
continue; | |
} | |
if(0<i && INFIX==t.o[i].symbol.type && ('_'==(p=t.o[i].symbol.input) || '^'==p)){ | |
h = i-1; | |
k = '^'==p ? 'power':'subscript'; | |
str = str.slice(0, t.o[i].pos); | |
if(PHANTOM!=t.o[h].symbol.type){ | |
j = h-1; | |
t.o[i] = mk_cloneSymbol(mkElementPhantom); | |
t.o[i].l = t.o[h].l; | |
t.o[h].l = t.o[h].l+1; | |
t.o[i].c = [c[level].pop()]; | |
c[level].push(i); | |
e.i[h] = h; | |
e.f[h] = h; | |
}else{ | |
j = t.o[h].c[0]-1; | |
t.o.pop(); | |
i = h; | |
} | |
if(-1<j && INFIX==t.o[j].symbol.type && p==t.o[j].symbol.input){ | |
t.o[j][k+'of'] = i; | |
t.o[i][k] = j; | |
delete e.sp[j]; | |
if(!!(b = c[t.o[j].l].splice(c[t.o[j].l].indexOf(j),1)) && b.length){ | |
t.o[i].c.unshift(b[0]); | |
} | |
++t.o[j].l; | |
mk_adjLevel(t,t.o[j].c,a,d,r); | |
} | |
e.sp[i] = -1; | |
r[p][level] = i; | |
t.o[i].symbol.type = INFIX; | |
t.o[i].symbol.input = p; | |
k = 0; | |
p = 0; | |
continue; | |
} | |
if(l!=level){ | |
if(level>l){ | |
if(undefined==e.l[level]){ | |
e.l[level]=i+0; | |
} | |
l=level; | |
}else if(level<l && undefined!=(k=e.l[l])){ | |
e.i[k] = i+0; | |
e.f[i] = k+0; | |
delete e.l[l]; | |
} | |
} | |
if(RIGHTBRACKET==t.o[i].symbol.type || ':'==t.o[i].symbol.output){ | |
if(']'==t.o[i].symbol.output){ | |
rc.m[level] = 1; | |
t.x[i] = true; | |
}else if('/'==t.o[i].symbol.input && ')'==t.o[i].symbol.output){ | |
rc.v[level] = 1; | |
t.x[i] = true; | |
}else if('}'==t.o[i].symbol.output){ | |
(0<t.o[i].pos && ':'==str.charAt(t.o[i].pos-1))? ((rc.c[level] = 1), (t.x[i+1] = true)) : (!!rc.m[level-1] ? rc.mr[level] = 1 : rc.s[level] = 1); | |
t.x[i] = true; | |
}else if(':'==t.o[i].symbol.output && !!rc.c[level] && '{'==str.charAt(t.o[i].pos-1)){ | |
t.x[i] = true; | |
} | |
} | |
j = i; | |
if((l==level && !!t.o[i].symbol.sep) || (l>level && -1!=d[l] && -1!=c[l].indexOf(d[l]))){ | |
//mk_adjBreadth(t,i,c,e,r,[a,d,b]); | |
t.o.splice(i++, 0, mk_cloneSymbol(mkElementPhantom)); | |
if(!!r.so[l]){ | |
!!t.o[j].suffixop ? Array.prototype.unshift.apply(t.o[j].suffixop, r.so[l]) : t.o[j].suffixop = r.so[l].map(mk_acopy); | |
!!t.o[j].append ? Array.prototype.unshift.apply(t.o[j].append, r.so[l]) : t.o[j].append = r.so[l].map(mk_acopy); | |
delete r.so[l]; | |
} | |
t.o[j].l = l; | |
t.o[j].c = []; | |
k=-1; | |
h = c[l].length; | |
while(-1==d[l] && h>++k && RIGHTBRACKET!=t.o[c[l][k]].symbol.type); | |
while(k<--h && d[l]!=c[l][h]){ | |
t.o[j].c.unshift(c[l].pop()); | |
} | |
k = t.o[j].c.length-1; | |
if(-1<k){ | |
e.i[t.o[j].c[0]] = t.o[j].c[k]; | |
e.f[t.o[j].c[k]] = t.o[j].c[0]; | |
} | |
mk_adjLevel(t, t.o[j].c, a, d, r); | |
c[l].push(j); | |
d[l] = l>level ? -1 : i;//last decent at this level | |
if(rc.mr[l]){ | |
++rc.mr[l]; | |
t.x[i] = true; | |
t.o[j].matrixcol = true; | |
}else if(rc.v[l]){ | |
++rc.v[l]; | |
t.x[i] = true; | |
t.o[j].vectorcol = true; | |
}else if(rc.s[l]){ | |
++rc.s[l]; | |
t.x[i] = true; | |
t.o[j].setcol = true; | |
}else if(rc.m[l]){ | |
++rc.m[l]; | |
t.x[i] = true; | |
t.o[j].suppresstag = true; | |
}else if(rc.c[l]){ | |
++rc.c[l]; | |
t.x[i] = true; | |
t.o[j].crow = true; | |
} | |
j = i-1; | |
} | |
if(RIGHTBRACKET==t.o[i].symbol.type){ | |
br.unshift( [t.o[i].symbol.output,i] ); | |
}else if(LEFTBRACKET==t.o[j].symbol.type && 0<br.length && br[0][0]==rparen[t.o[j].symbol.output]){ | |
b = br.shift(); | |
e.br[b[1]] = j; | |
e.br[j] = b[1]; | |
} | |
j = i; | |
if(0==o.pos && 0!=level){ | |
--level; | |
str = '\u200a;'; | |
o.pos=1; | |
if(undefined==c[level]){//should not happen | |
break; | |
} | |
} | |
if(l>level && -1<++j){ | |
//mk_adjBreadth(t,i,c,e,r,[a,d,b]); | |
t.o.push(mk_cloneSymbol(mkElementPhantom,{lp:true})); | |
if(!!r.so[level]){ | |
!!t.o[j].suffixop ? Array.prototype.unshift.apply(t.o[j].suffixop, r.so[level]) : t.o[j].suffixop = r.so[level].map(mk_acopy); | |
!!t.o[j].append ? Array.prototype.unshift.apply(t.o[j].append, r.so[level]) : t.o[j].append = r.so[level].map(mk_acopy); | |
delete r.so[level]; | |
} | |
k=-1; | |
h = mkParen[0][t.o[i].symbol.output] || t.o[i].symbol.output; | |
while(undefined!=(p=c[l][++k]) ? ((h!=t.o[p].symbol.output || !!t.o[p].brk) && !t.o[p].c):((k=-1),false)); | |
if(-1<k){ | |
k=c[l][k]; | |
p=-1; | |
while((!t.o[k].brk && h==t.o[k].symbol.output) ? ((t.o[k].brk=true),(t.o[k].l=l),(c[l].unshift((-1==p?c[l]:t.o[p].c).shift())),false) : (!!t.o[k].c && undefined!=(k=t.o[p=k].c[0]))); | |
} | |
t.o[j].l = level; | |
c[l].push(i); | |
t.o[j].c = c[l].map(mk_acopy); | |
c[l] = []; | |
c[level].push(j); | |
if('^'!=(h = str.slice(t.o[i].pos-1, t.o[i].pos)) && '_'!=h){ | |
a[level] = j; //last ascent at this level | |
} | |
h = 0; | |
if(LEFTRIGHT==t.o[i].symbol.type){ | |
t.o[j].outfixop = 'abs'; | |
} | |
if('['==t.o[i].symbol.output && !!rc.m[l]){ | |
t.o[j].matrix = true; | |
rc.m[l] = false; | |
t.x[i] = true; | |
} | |
if(('{'==t.o[i].symbol.output || '\\'==t.o[i].symbol.input) && (!!rc.v[l] || !!rc.mr[l] || !!rc.c[l] || !!rc.s[l])){ | |
!rc.v[l] || (t.o[j].vector = rc.v[l]); | |
!rc.s[l] || (t.o[j].set = rc.s[l]); | |
!rc.mr[l] || (t.o[j].matrixrow = rc.mr[l]); | |
!rc.c[l] || (t.o[j].piecewise = rc.c[l]); | |
rc.s[l] = false; | |
rc.v[l] = false; | |
rc.mr[l] = false; | |
rc.c[l] = false; | |
t.x[i] = true; | |
} | |
} | |
if(i==j){ | |
k = false; | |
if(undefined!=t.o[i].symbol.functional){ | |
p = c[level].length; | |
while(-1<--p && a[level]!=(j=c[level][p]) && !t.o[j].sep && INFIX==t.o[j].symbol.type && ('^'==t.o[j].symbol.input || '_'==t.o[j].symbol.input)); | |
if(-1<p && PHANTOM==t.o[j].symbol.type){ | |
k = true; | |
} | |
} | |
p = null; | |
if(k || ('mo'==t.o[i].symbol.ptag && UNDEROVER==t.o[i].symbol.type) || UNARY==t.o[i].symbol.type || BINARY==t.o[i].symbol.type){ | |
p = null; | |
if(undefined!=t.o[i].symbol.functional){ | |
if(VAR==t.o[i].symbol.type){ | |
t.o[i].symbol.ptag = 'mo'; | |
if(!!t.o[i].symbol.sym){ | |
delete t.o[i].symbol.sym; | |
} | |
if(!!t.o[i].symbol.iscalcop){ | |
delete t.o[i].symbol.iscalcop; | |
} | |
if(!t.o[i].symbol.ctag){ | |
t.o[i].symbol.ctype='function'; | |
} | |
t.o[i].symbol.type = t.o[i].symbol.functional; | |
} | |
} | |
h = -1; | |
k = -1; | |
p = c[level].length; | |
b = null; | |
while(-1<--p && c[level][p]!=a[level] && !t.o[c[level][p]].sep && (-1===h && -1===k && 0<(h=(('_'==t.o[c[level][p]].symbol.input&&!!(k='^')) ? p-- : (('^'==t.o[c[level][p]].symbol.input&&(k='_')) ? p-- : -2))) && -1<(p=mk_nextInfix(t.o,c,p,level)) && (k==t.o[c[level][p]].symbol.input ? (p=mk_nextInfix(t.o,c,(k=p)-1,level)) : (k=-2))&&0) || (-1<p && (!!b || !!(b=[p,false,false,false])) && ( | |
(3<b.length && PHANTOM!=t.o[c[level][p]].symbol.type && DIFFERENTIAL!=t.o[c[level][p]].symbol.type && ((!!t.o[c[level][p]].symbol.pri || !!t.o[c[level][p]].symbol.sep)? ((b[1]=++p),b.pop()) : true )) || | |
(2<b.length && !!t.o[i].symbol.integral && 0<p && ((DIFFERENTIAL==t.o[c[level][p]].symbol.type && 'd'==t.o[c[level][p]].symbol.output && 'mi'==t.o[c[level][p-1]].symbol.ptag)? ((t.o[c[level][p]].intop=i),(t.o[i].op=c[level][p]), -1<(b[1]=--p), b.pop()):true))))); | |
; | |
if(!b){ | |
b = [p,false,false,false]; | |
} | |
if(false===b[1]){ | |
b[1]=p; | |
} | |
if(-1<h){ | |
t.o[i][j= '^'==(p=t.o[c[level][h]].symbol.input) ? 'power':'subscript'] = c[level][h]; | |
t.o[c[level][h]][j+'of']=i; | |
delete e.sp[c[level][h]]; | |
if(c[level][h]==r[p][level]){ | |
r[p][level] = -1; | |
} | |
} | |
if(-1<k){ | |
t.o[i][j= '^'==(p=t.o[c[level][k]].symbol.input) ? 'power':'subscript'] = c[level][k]; | |
t.o[c[level][k]][j+'of']=i; | |
delete e.sp[c[level][k]]; | |
if(c[level][k]==r[p][level]){ | |
r[p][level] = -1; | |
} | |
} | |
p = b[0]; | |
j = (-1==b[1])?0:b[1]; | |
if(1==mkDebug){ | |
console.log('--------------'+t.o[i].symbol.output); | |
console.log(c[level]); | |
console.log(h+','+k+','+p+','+j); | |
console.log((h>-1?(c[level][h]+':'+t.o[c[level][h]].symbol.output):-1)+','+(k>-1?(c[level][k]+':'+t.o[c[level][k]].symbol.output):-1)+','+(p>-1?(c[level][p]+':'+t.o[c[level][p]].symbol.input):-1)); | |
} | |
b.splice(0); | |
t.o[i].args=[]; | |
if(0<c[level].length && -1<p){ | |
b = c[level].slice(j, (h>-1?h:p)+1); | |
p = c[level][k=p]; | |
if((1==b.length || (-1<h && c[level][h]==b[b.length-1])) && PHANTOM==t.o[b[0]].symbol.type){ | |
p=b[0]; | |
mk_adjLevel(t,b,a,d,r); | |
t.o[p].l = level+1; | |
a[level] = p; | |
t.o[i].args=[p]; | |
c[level] = c[level].filter(function(v){return -1==b.indexOf(v);}); | |
b = b.filter(function(v){return -1==t.o[p].c.indexOf(v);}); | |
t.o[i].c = b.map(mk_acopy); | |
k=t.o[p].c.length; | |
if(!t.o[p].matrix && 4<k && RIGHTBRACKET==t.o[t.o[p].c[0]].symbol.type && LEFTBRACKET==t.o[t.o[p].c[k-1]].symbol.type && PHANTOM==t.o[t.o[p].c[1]].symbol.type && PHANTOM==t.o[t.o[p].c[k-2]].symbol.type && !!t.o[t.o[p].c[2]].symbol.sep && (BINARY==t.o[i].symbol.type||t.o[i].symbol.sov)){ | |
b=t.o[p].c.map(mk_acopy).reverse(); | |
b.pop(); | |
b.shift(); | |
k=b.length; | |
while(-1<--k){ | |
if(!!t.o[b[k]].symbol.sep||','==t.o[b[k]].symbol.output){ | |
b.splice(k,1); | |
} | |
} | |
} | |
}else { | |
mk_adjBreadth(t,++p,c,e,r,[a,d,b]); | |
e.i[b[0]]=p-1; | |
e.f[p-1]=b[0]; | |
++i; | |
t.o.splice(p, 0, mk_cloneSymbol(mkElementPhantom)); | |
mk_adjLevel(t,b,a,d,r); | |
t.o[p].l = level+1; | |
t.o[p].c = c[level].slice(j, k+1); | |
mk_adjLevel(t,t.o[p].c,a,d,r); | |
a[level] = p; | |
t.o[i].args=[p]; | |
c[level] = c[level].filter(function(v){return -1==b.indexOf(v);}); | |
b = b.filter(function(v){return -1==t.o[p].c.indexOf(v);}); | |
b.unshift(p); | |
t.o[i].c = b.map(mk_acopy); | |
} | |
t.o[p].isarg = true; | |
while(0<(k=b.length-1) && ('^'==t.o[b[k]].symbol.input||'_'==t.o[b[k]].symbol.input)){ | |
b.pop();b.pop(); | |
} | |
if(1<b.length && !t.o[i].symbol.integral){ | |
t.o[i].arg = t.o[i].args.pop(); | |
t.o[i].args = b.map(mk_acopy);//.reverse(); | |
} | |
} | |
if(BINARY==t.o[i].symbol.type){ | |
if(undefined==t.o[i].arg){ | |
t.o[i].arg = t.o[i].args.pop(); | |
} | |
if(1>t.o[i].args.length && -1<(p = c[level].indexOf(t.o[i].arg)) && !!t.o[p=c[level][p]].c && 1<(j=t.o[p].c.length)){ | |
while(-1<--j && (2>t.o[i].args.length||!!t.o[t.o[p].c[j]].ofsuffixop) && (PHANTOM!=t.o[t.o[p].c[j]].symbol.type || (t.o[i].args.push(t.o[p].c[j]),(t.o[t.o[p].c[j]].isarg=true)))); | |
} | |
} | |
p = undefined!=t.o[i].arg ? t.o[i].arg : t.o[i].args[0]; | |
if(t.o[i].symbol.integral){ | |
if(-1<(h=t.o[i].args.length-1) && !!t.o[t.o[i].args[h]].c && -1<(k=t.o[t.o[i].args[h]].c.length-1)){ | |
if(t.o[t.o[t.o[i].args[h]].c[k]].symbol.integral){ | |
t.o[i].op = t.o[t.o[t.o[i].args[h]].c[1]].op = t.o[t.o[i].args[h]].c[k]; | |
}else if(undefined!=t.o[t.o[i].args[h]].c[1]){ | |
t.o[t.o[t.o[i].args[h]].c[1]].op=t.o[t.o[t.o[i].args[h]].c[1]].intop; | |
delete t.o[t.o[t.o[i].args[h]].c[1]].intop; | |
delete t.o[i].op; | |
} | |
} | |
} | |
if(undefined!=(p=t.o[i].arg)||undefined!=(p=t.o[i].args[0])){ | |
if( undefined!=t.o[p].suffixop){ | |
!!t.o[i].suffixop ? Array.prototype.unshift.apply(t.o[i].suffixop, t.o[p].suffixop) : t.o[i].suffixop = t.o[p].suffixop.map(mk_acopy); | |
!!t.o[i].append ? Array.prototype.unshift.apply(t.o[i].append, t.o[p].append) : t.o[i].append = t.o[p].append.map(mk_acopy); | |
delete t.o[p].append; | |
delete t.o[p].suffixop; | |
} | |
if( undefined!=t.o[p].ofsuffixop){ | |
t.o[i].ofsuffixop = t.o[p].ofsuffixop; | |
t.o[i].prefixof = t.o[p].prefixof; | |
t.o[t.o[i].prefixof].suffixof = i; | |
delete t.o[p].ofsuffixop; | |
delete t.o[p].prefixof; | |
} | |
if(undefined!=(k=t.o[p].c[0]) && ((1<k && ')'==t.o[k].symbol.output) && ((p=i),(j=level+1),(h='em'))) && (('^'==t.o[k-1].symbol.input && (h+='power')) || ('_'==t.o[k-1].symbol.input) && (h+='subscript')) ){ | |
delete e.sp[k-1]; | |
t.o[p][h]=k-1; | |
t.o[k-1][h+'of']=p; | |
t.o[k-1].l=j; | |
t.o[k-2].l=t.o[k-1].l+1; | |
if(!!t.o[k-2].c){ | |
mk_adjLevel(t,t.o[k-2].c,a,d,r); | |
} | |
} | |
t.o[p].c = t.o[p].c.filter(function(v){return INFIX!==t.o[v].symbol.type && '^'!==t.o[v].symbol.input && '_'!==t.o[v].symbol.input; }); | |
} | |
h=0; | |
if(null!=p){ | |
if(!(!!t.o[p].vector || !!t.o[p].set || !!t.o[p].matrix)){ | |
if(!!t.o[i].symbol.separators && !!t.o[p].c){ | |
j = t.o[p].c.length; | |
while(-1<--j && (!t.o[t.o[p].c[j]].symbol.sep || (t.o[t.o[p].c[j]].symbol.output=t.o[i].symbol.separators))); | |
} | |
} | |
} | |
if('stackrel'==t.o[i].symbol.input && undefined!=(p=t.o[i].args[1])){ | |
h = t.o[i].args[0]; | |
t.o[h].stackrel = p; | |
t.o[p].stackref = h; | |
if(2>=++sr){ | |
mkExt.cache[3]=mkExt.cache[0]; | |
mkExt.cache[4]=mkExt.cache[1]; | |
mkExt.cache[0]=''; | |
mkExt.cache[1]=''; | |
} | |
t.o[i].stackname = t.o[p].stackname = t.o[h].stackname = mk_newStackName((!!t.o[p].c && 1==t.o[p].c.length && TEXT==t.o[t.o[p].c[0]].symbol.type) ? t.o[t.o[p].c[0]].text.trim() : mk_findStackName(t,eq,p,2), true); | |
k = mk_newStackRef(((!!t.o[h].c && 1==t.o[h].c.length && TEXT==t.o[t.o[h].c[0]].symbol.type && !!t.o[t.o[h].c[0]].text.trim()) ? t.o[t.o[h].c[0]].text.trim() : mk_findStackName(t,eq,h,1)), t.o[i].stackname); | |
if(3===mkDebug){ | |
console.log(eq); | |
console.log(t.o[i].stackname); | |
console.log(t.s); | |
} | |
t.o[h].stype = t.o[i].stype = t.o[p].stype = ''; | |
if((!!t.o[h].symbol.output || !!t.o[h].symbol.text || PHANTOM==t.o[h].symbol.type) && !!t.o[p].c && 0<t.o[p].c.length && (((VAR==t.o[t.o[p].c[0]].symbol.type&&(k='function'))||(TEXT==t.o[t.o[p].c[0]].symbol.type)&&(k='text')) || !!t.o[t.o[p].c[0]][k='vector'] || !!t.o[t.o[p].c[0]][k='set'] || !!t.o[t.o[p].c[0]][k='matrix'] || !!t.o[t.o[p].c[0]][k='function'] || (PHANTOM==t.o[t.o[p].c[0]].symbol.type&&(k='function')) || (MULTI==t.o[t.o[p].c[0]].symbol.type&&(k='function')))){ | |
//experimental | |
if(!!(j=mk_getExt(t.o[i].stackname)) && 'text'==k && 'error'!==j.data && !!j.type){ | |
k = j.atype = j.type; | |
}else{ | |
j.atype = k; | |
} | |
t.o[p].stackop = t.o[h].stackop = i; | |
if(-1=='|image|text|'.indexOf(k.replace(/\/.*$/ug,''))){ | |
t.o[h].symbol.ctype = k.replace('math','function'); | |
if('math'!=k){ | |
k='function'; | |
} | |
} | |
j.type = t.o[h].stype = t.o[i].stype = t.o[p].stype = k; | |
if('loading'==j.status && !j.data && !!j.type){ | |
j.status='error'; | |
} | |
j = i; | |
t.o[i].stackref = t.o[p].stackref; | |
t.o[i].stackrel = t.o[h].stackrel; | |
t.o[i].args.pop(); | |
t.o[i].symbol.type = UNARY; | |
t.o[i].symbol.ptag = 'mi'; | |
t.o[i].symbol.output = ''; | |
} | |
}else if('stackrel'==t.o[i].symbol.input && undefined!=(p=t.o[i].arg)){ | |
t.o[i].stackname = mk_newStackName(( | |
((TEXT==t.o[p].symbol.type && !!t.o[h=p].text)|| | |
(!!t.o[p].c && 1==t.o[p].c.length && TEXT==t.o[h=t.o[p].c[0]].symbol.type && !!t.o[h=t.o[p].c[0]].text)) ? t.o[h].text.trim() : j=mk_findStackName(t,eq,p-1,0)),true); | |
if(2>=++sr){ | |
mkExt.cache[3]=mkExt.cache[0]; | |
mkExt.cache[4]=mkExt.cache[1]; | |
mkExt.cache[0]=''; | |
mkExt.cache[1]=''; | |
} | |
mk_newStackRef(t.o[i].stackname,t.o[i].stackname); | |
if(3===mkDebug){ | |
console.log(eq); | |
console.log(t.o[i].stackname); | |
console.log(t.s); | |
} | |
if(!!(j=mk_getExt(t.o[i].stackname)) && 'error'!==j.data && !!j.type){ | |
k = j.atype = j.type; | |
}else{ | |
k = (t.o[p][j.atype='vector'] || !!t.o[p][j.atype='set'] || !!t.o[p][j.atype='matrix'] || PHANTOM==t.o[p].symbol.type || VAR==t.o[p].symbol.type) ? 'function' : 'text'; | |
if(!j.atype){ | |
j.atype = 'code'; | |
} | |
} | |
if(-1=='|image|text|'.indexOf(k.replace(/\/.*$/ug,''))){ | |
t.o[p].symbol.ctype = k.replace('math','function'); | |
} | |
j.type = t.o[p].stype = t.o[i].stype = k; | |
if('loading'==j.status && !j.data && !!j.type){ | |
j.status='error'; | |
} | |
j = i; | |
t.o[i].symbol.type = UNARY; | |
t.o[i].symbol.ptag = 'mi'; | |
t.o[i].symbol.output = ''; | |
h = null; | |
}else if(!!(b = t.o[t.o[i].args[0]]) && !!b.c && 1<(k=b.c.length)){ | |
while(-1<--k && 'tendsto'!=t.o[b.c[k]].symbol.ctag); | |
if(-1<k){ | |
t.o[i].apply = k; | |
} | |
} | |
}else { | |
if(!!t.o[i].symbol.functional){ | |
delete t.o[i].symbol.functional; | |
if(VAR==t.o[i].symbol.type || UNDEROVER==t.o[i].symbol.type){ | |
t.o[i].symbol.type = VAR; | |
delete t.o[i].symbol.ctag; | |
} | |
} | |
} | |
j=i; | |
!!c[level] ? c[level].push(i) : (c[level]=[i]); | |
} | |
t.o[i].l = l; | |
j = t.o.length -1; | |
if(!!r.so[level]){ | |
!!t.o[j].suffixop ? Array.prototype.unshift.apply(t.o[j].suffixop, r.so[level]) : t.o[j].suffixop = r.so[level].map(mk_acopy); | |
!!t.o[j].append ? Array.prototype.unshift.apply(t.o[j].append, r.so[level]) : t.o[j].append = r.so[level].map(mk_acopy); | |
delete r.so[level]; | |
} | |
if(undefined!=(k=r.ps[level])){ | |
t.o[j].ofsuffixop = t.o[k].ofprefixop; | |
t.o[j].prefixof = k; | |
t.o[k].suffixof = j; | |
delete r.ps[level]; | |
} | |
b = ['^','_',false]; | |
while(!!(p=b.shift())){ | |
j=i-1; | |
while(t.o.length>++j){; | |
h = null!=t.o[j].l ? t.o[j].l : l; | |
if(undefined!=(k=r[p][h])&&-1!=k){ | |
if(LEFTBRACKET==t.o[j].symbol.type){ | |
if(1<h){ | |
r[p][h-1] = k; | |
} | |
e.sp[k] = -2; | |
}else{ | |
if(undefined!==e.sp[k]){ | |
e.sp[k] = j; | |
} | |
r[p][h] = -1; | |
} | |
}else if(undefined!=(k=r[p][h+1]) && undefined!==e.sp[k] && -2==e.sp[k]){ | |
e.sp[k] = j; | |
r[p][h+1] = -1; | |
} | |
} | |
} | |
p = 0; | |
str = str.slice(0, t.o[i].pos); | |
if(!!to && level==until){ | |
return t.o[i]; | |
} | |
} | |
c = []; | |
i = k = t.o.length; | |
h = j = p = -1; | |
rc = {'power':'^','subscript':'_'}; | |
r = {'em':false,'':true}; | |
while(-1<--i){ | |
o = t.o[i]; | |
if(!!o.c||!!o.args){ | |
mk_linkParent(t,i); | |
} | |
if(undefined!=e.br[i]){ | |
o.br = e.br[i]; | |
t.o[e.br[i]].br = i; | |
delete e.br[e.br[i]]; | |
delete e.br[i]; | |
} | |
if('mstyle'==o.symbol.ptag){ | |
o.sp[0]=0; | |
o.sp[1]=0; | |
o.sp[2]=0; | |
o.sp[3]=0; | |
} | |
for(p in rc){ | |
if(INFIX == o.symbol.type && rc[p]==o.symbol.input){ | |
lr = false; | |
for(h in r){ | |
if(!!r[h] && !!lr){ | |
continue; | |
}else if(undefined==(j=o[h+p+'of'])){ | |
if(undefined!=e.sp[i] && -1<e.sp[i] && (undefined!=(j=o[p+'of'])||!!r[h])){ | |
j = e.sp[i]; | |
}else if(!r[h]){ | |
continue; | |
}else { | |
j = i; | |
while(k>++j && o.l!=t.o[j].l){ | |
console.log(i+' discontinuity@'+j); | |
} | |
} | |
if(-1>j || !t.o[j]){ | |
continue; | |
} | |
o[h+p+'of'] = j; | |
t.o[j][h+p] = i; | |
} | |
lr = true; | |
e.i[i]=i; | |
e.f[i]=i; | |
o['is'+h+p] = true; | |
if(!!t.o[j].c && -1!=(l=t.o[j].c.indexOf(i))){ | |
t.o[j].c.splice(l,1); | |
} | |
while(o.l<=t.o[j].l){ | |
++t.o[i].l; | |
} | |
o.symbol.output = ''; | |
} | |
delete e.sp[i]; | |
} | |
} | |
if(!!t.o[i].c && 0<t.o[i].c.length && undefined!==t.o[t.o[i].c[0]].p){ | |
mk_adjLevel(t,t.o[i].c); | |
} | |
} | |
if(1==mkDebug){ | |
console.log(c); | |
t.o.map(function(v,i){console.log(i+': '+JSON.stringify(v));}); | |
} | |
rc = {}; | |
i = t.o.length; | |
while(-1<--i){ | |
o = t.o[i]; | |
if(!!o.c && 0<o.c.length && !!t.o[k=o.c[0]] && undefined==o.calcop && (RIGHTBRACKET==t.o[k].symbol.type || (o.symbol.ptag==o.symbol.output &&!!o.args&&1==o.args.length&&!!t.o[k=o.args[0]] && !!t.o[k].c && 0<t.o[k].c.length && RIGHTBRACKET==t.o[k=t.o[k].c[0]].symbol.type)) && undefined!=t.o[k].br && ((4>o.c.length&&!o.isarg) || 3>o.c.length || (!!o.args && o.symbol.ptag==o.symbol.output) || !!o.issubscript || (undefined!=o.powerof && (UNDEROVER==t.o[o.powerof].symbol.type || !!t.o[o.powerof].symbol.integral) ) || (undefined!=o.p && (!!t.o[o.p].symbol.sov || UNDEROVER==t.o[o.p].symbol.type)) ) ){ | |
t.o[t.o[k].br].symbol.invisible=true; | |
t.o[k].symbol.invisible=true; | |
}else if(!!o.symbol.invisible && 0<i && o.symbol.output!=o.symbol.input && RIGHTBRACKET==t.o[k=i-1].symbol.type && t.o[k].symbol.output==t.o[k].symbol.input){ | |
t.o[t.o[k].br].symbol.invisible=true; | |
t.o[k].symbol.invisible=true; | |
}else if('stackrel'==o.symbol.input && undefined!=o.arg && !!t.o[o.arg].c && 1<t.o[o.arg].c.length && RIGHTBRACKET==t.o[k=t.o[o.arg].c[0]].symbol.type && LEFTBRACKET==t.o[j=t.o[o.arg].c[t.o[o.arg].c.length-1]].symbol.type){ | |
t.o[k].symbol.invisible=true; | |
t.o[j].symbol.invisible=true; | |
} | |
if(RIGHTBRACKET==o.symbol.type || LEFTBRACKET==o.symbol.type){ | |
//todo: better respacing, including sep within mfence etc.... | |
mk_reSpace(t,i); | |
} | |
if(undefined!=e.f[i]){ | |
!!c[o.l]?c[o.l].unshift(-1):c[o.l]=[-1]; | |
} | |
!!c[o.l]?c[o.l].unshift(i):c[o.l]=[i]; | |
if(undefined!=e.i[i]){ | |
c[o.l].unshift(-1); | |
} | |
if('stackrel'==o.symbol.input){ | |
t.s[o.stackname] = i; | |
} | |
mk_freeze(t, i); | |
rc[i]=t.x[i]; | |
} | |
for(j=-1;c.length>++j;){ | |
if(!c[j]){ | |
for(k=-1;t.o.length>++k;){ | |
if(j<=t.o[k].l){ | |
--t.o[k].l; | |
} | |
} | |
c.splice(j--,1); | |
} | |
} | |
if(2==mkDebug){ | |
t.o.map(function(v,i){console.log(i+': '+ JSON.stringify(v));}); | |
console.log(mkExt); | |
console.log(mkOrigin); | |
} | |
a=[]; | |
h = lr = -1; | |
i = c.length; | |
while(-1<--i){ | |
if(undefined==c[i]){ | |
console.log(c); | |
break; | |
} | |
j = c[i].length; | |
while(-1<--j){ | |
if(-1==(p=c[i][j]) || !(o = t.o[p]) || (!!t.x[p] && PHANTOM!=o.symbol.type)){ | |
continue; | |
} | |
if(-1<(k=undefined!=o.calcop?((h=p),o.calcop):(undefined!=o.calcopof?((h=o.calcopof),p):-1))){ | |
t.x[h] = true; | |
if(!t.o[k]._calcop){ | |
t.o[k]._calcop = true; | |
if(0<lr && undefined!=t.o[h].power){ | |
t.o[h].order = t.o[h].power; | |
}else if(undefined!=t.o[k].power){ | |
t.o[k].order = t.o[k].power; | |
delete t.o[k].power; | |
} | |
if(undefined!=t.o[k].subscript && undefined==t.o[h].subscript){ | |
t.o[h].subscript=t.o[k].subscript; | |
delete t.o[k].subscript; | |
} | |
}else if(0<lr){ | |
t.o[h].order = t.o[k].order; | |
delete t.o[k].order; | |
} | |
} | |
mk_presentation(t, p); | |
if(INFIX==o.symbol.type && '/'==o.symbol.output){ | |
k = j; | |
while(c[i].length>++k && -1!=(h=c[i][k]) && !!t.x[h]); | |
if(-1<h && !!t.o[h]){ | |
lr = h; | |
mk_pMrow(t.o[lr].presentation); | |
mk_addClass(t.o[lr].presentation,'__mkFraction'); | |
mk_pRemoveBrackets(t, lr); | |
t.o[lr].op = p; | |
t.o[lr].isnumerator = true; | |
o.numerator = lr; | |
t.x[lr] = true; | |
t.x[p] = true; | |
} | |
}else if(lr>-1){ | |
o.op = t.o[lr].op; | |
t.o[t.o[lr].op].denominator = p; | |
o.isdenominator = true; | |
mk_pMrow(o.presentation); | |
mk_pRemoveBrackets(t, p); | |
node = t.pML('mfrac'); | |
mk_appendChild(node, t.o[lr].presentation); | |
mk_appendChild(node, o.presentation); | |
o.presentation = node; | |
lr = -1; | |
} | |
} | |
} | |
o = mk_createElementMML('math'); | |
j = !!c[0] ? c[0].length : 0; | |
while(-1<--j){ | |
if(0>(p = c[0][j]) || !!t.x[p] || !t.o[p].presentation){ | |
continue; | |
} | |
o.appendChild(t.o[p].presentation); | |
} | |
o.setAttribute('alttext', eq.replace(/</ug,'<').replace(/>/ug,'>')); | |
o.setAttribute('xmlns', mkMathns); | |
if(!mkDoc.createElementNS){ | |
o.setAttribute('xmlns:m', mkMathns); | |
} | |
for(k in conf.mstyle){ | |
o.setAttribute(k,conf.mstyle[k]); | |
} | |
mk_tunwrap(t,o); | |
(function(){ | |
var fo = '<foreignObject width="100%" height="100%" style="background-color:#ffffff;"><style>/* <![CDATA[ */'+mkFallbackCSS+'/* ]]> */</style>'+o.outerHTML+'</foreignObject>', div = mkDoc.createElement('div'), img = new Image(), dh = 0, dw = 0; | |
o.setAttribute('altimg', 'data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg">'+fo+'</svg>'); | |
div.appendChild(o.cloneNode(true)); | |
div.style='opacity:0;position:absolute;width:auto;height:auto;'; | |
mkDoc.body.appendChild(div); | |
dh = div.clientHeight*2, dw = div.clientWidth*2; | |
mkDoc.body.removeChild(div); | |
if(1>dw || 1>dh){ | |
return; | |
} | |
fo = 'data:image/svg+xml;utf8,'+encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" width="'+dw+'" height="'+dh+'">'+fo+'</svg>'); | |
img.onload = function(){ | |
var c = mkDoc.createElement('canvas'), im=''; | |
c.width=dw; | |
c.height=dh; | |
c.getContext('2d').drawImage(img, 0, 0); | |
try{ | |
im = c.toDataURL(); | |
o.setAttribute('altimg', im); | |
}catch(err){ | |
console.log(err); | |
} | |
}; | |
img.src = fo; | |
})(); | |
t.x = rc; | |
t.cc(); | |
i = c.length; | |
while(-1<--i){ | |
if(undefined==c[i]){ | |
console.log(c); | |
break; | |
} | |
j = c[i].length; | |
while(-1<--j){ | |
if(!mk_thaw(t, p = c[i][j]) || !!t.o[p].xbracket){ | |
continue; | |
} | |
mk_contents(t, p, c[i]); | |
} | |
} | |
o.mathKey = t.cML('math'); | |
o.mathKey.setAttribute('xmlns', mkMathns); | |
if(!mkDoc.createElementNS){ | |
o.mathKey.setAttribute('xmlns:m', mkMathns); | |
} | |
j = !!c[0] ? c[0].length : 0; | |
while(-1<--j){ | |
if(0>(p = c[0][j]) || !t.o[p] || !t.o[p].mk){ | |
continue; | |
} | |
if(undefined!=t.annotation&&t.annotation>p){ | |
mk_appendChild(t.annotate.lastChild, t.o[p].mk); | |
continue; | |
} | |
mk_appendChild(o.mathKey, t.o[p].mk); | |
} | |
if(!!t.annotate){ | |
mk_appendChild(o.mathKey, t.annotate); | |
} | |
c = o.getElementsByTagName('img'); | |
for(p=-1;c.length>++p;){ | |
c[p].mathKey_img = mkDoc.createElement('img'); | |
(function(){ | |
var mimg=c[p],himg=c[p].mathKey_img,src='',r,x='',s=[]; | |
himg.style.display = 'none'; | |
s[0] = mimg.getAttribute('title'); | |
if(!!(s[1]=s[0].match(/(?:^|[^0-9])([0-9]+\.?[0-9]*)\s*x/i))){ | |
s[0] = parseFloat(s[1][1]); | |
s.splice(1,1); | |
}else{ | |
if(!!(s[1]=s[0].match(/(?:^|[^0-9])([0-9]+\.?[0-9]*)\s*w/i))){ | |
s[1] = parseFloat(s[1][1]); | |
} | |
if(!!(s[2]=s[0].match(/(?:^|[^0-9])([0-9]+\.?[0-9]*)\s*h/i))){ | |
s[2] = parseFloat(s[2][1]); | |
} | |
s[0]=0; | |
} | |
mimg.removeAttribute('title'); | |
himg.setAttribute('alt',mimg.getAttribute('alt')); | |
himg.onload = function(){ | |
if('mi'==mk_nodeName(mimg.parentNode.previousElementSibling) && !mimg.parentNode.previousElementSibling.innerHTML){ | |
mimg.parentNode.parentNode.removeChild(mimg.parentNode.previousElementSibling); | |
} | |
x = mimg; | |
while(!!(x=x.parentNode)&&'math'!=mk_nodeName(x)&&(!x.scrollWidth || !x.scrollHeight)); | |
if(!x || (!x.scrollWidth&&!x.scrollHeight)){ | |
x = {scrollHeight:6, scrollWidth:0}; | |
} | |
if(!this.height){ | |
this.height = 1; | |
} | |
if(!this.width){ | |
this.width = 1; | |
} | |
r = !!s[0]?s[0]:1; | |
if(x.scrollHeight>x.scrollWidth){ | |
this.setAttribute('width', Math.round(r*x.scrollHeight*this.width/this.height)); | |
this.setAttribute('height', Math.round(r*x.scrollHeight)); | |
}else{ | |
this.setAttribute('height', Math.round(r*x.scrollWidth*this.height/this.width)); | |
this.setAttribute('width', Math.round(r*x.scrollWidth)); | |
} | |
if(!!s[1]){ | |
if(!s[2]){ | |
this.setAttribute('height', Math.round(s[1]*this.height/this.width)); | |
} | |
this.setAttribute('width', Math.round(s[1])); | |
} | |
if(!!s[2]){ | |
if(!s[1]){ | |
this.setAttribute('width', Math.round(s[2]*this.width/this.height)); | |
} | |
this.setAttribute('height', Math.round(s[2])); | |
} | |
if(!!(x=mimg.parentNode.parentNode.firstChild.firstChild) && 'mphantom'==mk_nodeName(x)){ | |
x.setAttribute('height','0px'); | |
x.setAttribute('width','0px'); | |
x.setAttribute('style','display:none;'); | |
} | |
x = t.pML('mspace'); | |
x.setAttribute('height',this.getAttribute('height')+'px'); | |
x.setAttribute('width',this.getAttribute('width')+'px'); | |
mimg.parentNode.parentNode.firstChild.appendChild(x); | |
r = [mkDoc.createElement('div')]; | |
x = x.parentNode; | |
r[0].style = 'float:left;height:0px;width:0px;line-height:0px;margin:0;padding:0;overflow:visible;z-index:99999;'; | |
this.insertAdjacentElement('beforebegin', r[0]); | |
r[0].appendChild(this); | |
r[1]=x; | |
r[1].removeChild(r[1].children[0]); | |
while(!!(r[2]=r[1].parentNode) && (r[1]=r[2]) && 'math'!=mk_nodeName(r[1])); | |
r[2]=mkDoc.createElement('div'); | |
r[2].style = 'display:inline-block;'; | |
r[1].insertAdjacentElement('beforebegin', r[2]); | |
r[2].appendChild(r[0]); | |
r[2].appendChild(r[1]); | |
r[3] = r[2].getBoundingClientRect(); | |
r[4] = x.getBoundingClientRect(); | |
if(!!x){ | |
this.setAttribute('style','position:relative;display:inline-block;top:'+Math.round(Math.abs(r[4].y-r[3].y)+0.2*this.height)+'px;left:'+(Math.abs(r[4].x-r[3].x))+'px'); | |
} | |
delete mimg.mathKey_img; | |
}; | |
src = mimg.getAttribute('src'); | |
mimg.removeAttribute('src'); | |
mkDoc.body.appendChild(himg); | |
himg.src = src; | |
})(); | |
} | |
o.addEventListener('click', function(e){ | |
var that=this, ta = document.createElement('textarea'); | |
ta.value = that.outerHTML; | |
that.setAttribute('style','background-color:#999;cursor:pointer;-webkit-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;'); | |
ta.style = 'opacity:0;height:1px;width:1px;position:absolute;top:'+e.clientY+'px;left:'+e.clientX+'px;'; | |
document.body.appendChild(ta); | |
ta.select(); | |
document.execCommand('copy'); | |
document.body.removeChild(ta); | |
window.setTimeout(function(){that.removeAttribute('style');},350); | |
},true); | |
o.addEventListener('dblclick', function(){ | |
var that=this,ta = document.createElement('textarea'); | |
ta.value = this.mathKey.outerHTML; | |
that.setAttribute('style','background-color:#999;cursor:pointer;-webkit-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;'); | |
ta.style = 'opacity:0;height:1px;width:1px;position:absolute;top:'+e.clientY+'px;left:'+e.clientX+'px;'; | |
document.body.appendChild(ta); | |
ta.select(); | |
document.execCommand('copy'); | |
document.body.removeChild(ta); | |
window.setTimeout(function(){that.removeAttribute('style');},350); | |
},true); | |
if(1==mkDebug){ | |
console.log(o.outerHTML); | |
console.log(o.mathKey.outerHTML); | |
} | |
return o; | |
}; | |
window.addEventListener('keydown', function(e){ | |
if(!!e.ctrlKey && 'c'==e.key){ | |
var node = null; | |
if('math'==mk_nodeName(node=document.activeElement) || 1==node.children.length && 'math'==mk_nodeName(node=node.children[0])){ | |
mkCopying = node; | |
} | |
} | |
},true); | |
window.addEventListener('keyup', function(e){ | |
if(!!mkCopying){ | |
if('Control'==e.key){ | |
mkCopying = null; | |
mkCCopy = 0; | |
}else{ | |
var ta = document.createElement('textarea'); | |
ta.value = (0==(++mkCCopy%2) && !!mkCopying.mathKey)? mkCopying.mathKey.outerHTML : mkCopying.outerHTML; | |
document.body.appendChild(ta); | |
ta.select(); | |
document.execCommand('copy'); | |
document.body.removeChild(ta); | |
} | |
} | |
},true); | |
var mk_parseSo = function(input,output,cb=null,rc=false){ | |
var ext = {}, v = input.value, name, fn=null, x; | |
if(!!mkFromMathKey){ | |
rc = true; | |
}else if(window.self!==window.top){ | |
window.stop(); | |
return false; | |
} | |
if(!rc){ | |
v = v.replace(/stackrel\s*\(\s*"?([^",]+?)"?\s*,\s*("?)(\s*[^"]+?\s*)"?\s*\)/gum, function(m0,m1,m2,m3){ if(')'==m3.charAt(0)){return ' '} mk_newStackOrigin(m1,m3); ext[mk_newStackName(m3,false)]=m3; return ' ';}); | |
v = v.replace(/stackrel\s*\(\s*"?([^",]+?)"?\s*,\s*""\s*\)/gum, function(m0,m1){ if(')'==m1.charAt(0)){return ' '} if(!!mkOrigin[m1]){mk_setExt(mkOrigin[m1],'');} return ' ';}); | |
v = v.replace(/stackrel\s*\(\s*""\s*,\s*"?([^",]+?)"?\s*\)/gum, function(m0,m1){ if(')'==m1.charAt(0)){return ' '}mk_setExt(m1,''); return ' ';}); | |
v.replace(/stackrel\s*\(\s*"?\s*([^"]+?)\s*"?\s*\)/gum, function(m0,m1){ if(')'==m1.charAt(0)){return ' '} ext[mk_newStackName(m1,false)]=m1; return ' ';}); | |
(fn = (function(){var to=mkTo.checkForId[1]; return function(init=false){ | |
for(name in ext){ | |
if(undefined!==mkExt.sn[name]){ | |
if('loading'!==mkExt.sn[name].status){ | |
delete ext[name]; | |
} | |
continue; | |
} | |
} | |
if(!!init){ | |
return; | |
} | |
return (1>--to || 1>Object.keys(ext).length)?( | |
(!!output&&!!output.appendChild&&!!output.querySelector('.__mkLoading')&&(output.removeChild(mkLoading))), mk_parseSo(input, output, cb, true)) : | |
window.setTimeout(fn, mkTo.checkForId[0]); | |
};})())(true); | |
} | |
if(0<Object.keys(ext).length){ | |
output.innerHTML=''; | |
(!!output&&!!output.appendChild&&(output.appendChild(mkLoading))); | |
for(name in ext){ | |
if(!mkExt.sn[name]){ | |
mk_newStackName(name, 'loading'); | |
} | |
x = mk_frame(name, {args:{name: name, ext:ext[name]}, fn: mk_regSig(function(com){ | |
var m = '',o=null, k=false, | |
_mk_idFromUrl = function(s=''){ | |
var i = 0; | |
return -1!=(i = s.indexOf('#')) ? s.substring(i).replace(/^[\s#]+/ug,'').trim() : ''; | |
}; | |
if(!!com.res && 'object'==typeof com.res && com.res instanceof HTMLImageElement){ | |
k = true; | |
com.doc = document; | |
} | |
if(!!com.doc){ | |
if((!!k&&(m=com.res.contentType)) || (!!(m=(com.doc.contentType || com.doc.mimeType)) && 0===m.indexOf('image/'))){ | |
var c = com.doc.createElement('canvas'), img = !!k ? com.res : com.doc.body.children[0]; | |
c.width = img.naturalWidth; | |
c.height = img.naturalHeight; | |
c.getContext('2d').drawImage(img, 0, 0); | |
try{ | |
o = c.toDataURL(m); | |
}catch(err){ | |
c = null; | |
console.log(err); | |
} | |
m = (!c || !o) ? 'data:'+m+';text/plain,'+img.src : o; | |
c = null; | |
if(!!k){ | |
img.src=''; | |
} | |
k = false; | |
}else if(!!(m=_mk_idFromUrl(com.args.name))){ | |
if(!!(m=com.doc.getElementById(m))){ | |
m = 'pm'==com.path ? (m.outerHTML + (!!m.mathKey? (!!m.mathKey.outerHTML?m.mathKey.outerHTML:m.mathKey.toString()):'')) : ((k=true),m); | |
}else{ | |
m = 'not found'; | |
} | |
}else{ | |
m = ''; | |
} | |
}else if(!!com.res){ | |
if(!!(m=_mk_idFromUrl(com.args.name))){ | |
m=new RegExp('(<math[^>]+\sid\s*=\s*[\'"]?\s*\Q'+m+'\E[\s\'">].*?</math>)','ui'); | |
m = !!(m=com.res.replace(/[\r\n]/ug, ' ').match(m))?m[1]:'not found'; | |
}else{ | |
m = 'error'; | |
} | |
}else{ | |
m = 'error'; | |
} | |
com.cb({status:'completed',msg:{name:com.args.name, ext:com.args.ext, data:m, keepalive:k}}); | |
})}, null, function(data){ | |
var b='', a = null, n = ''; | |
if(!!data.msg.name){ | |
if('string' == typeof data.msg.data && 0==data.msg.data.indexOf('<math')){ | |
var a = data.msg.data.replace(/<\/math>/uig,'</math>').split('</math>'), b = mkFrames[data.from].doc.implementation.createDocument(null,'math'); c = mkFrames[data.from].doc.createElement('declare'); | |
c.innerHTML = a[0]+'</math>'; | |
if(!!a[1]){c.mathKey=a[1]+'</math>'} | |
mk_setExt(data.msg.name, c.firstChild); | |
}else{ | |
mk_setExt(data.msg.name, data.msg.data); | |
} | |
if(!!data.msg.data && 'error'!=data.msg.data && !!data.msg.ext){ | |
b = input.value; | |
n = data.msg.ext.replace(/[-[\](){}*+?.,\\^$|]/ug, '\\$&'); | |
a=new RegExp('"\s*'+n+'\s*"','ug'); | |
if(!b.match(a)){ | |
a=new RegExp('\\s*'+n+'\\s*','ug'); | |
input.value = b.replace(a,'"'+data.msg.ext+'"'); | |
} | |
} | |
} | |
}); | |
if(!x){ | |
delete ext[name]; | |
mk_setExt(name, null, {status:'local'}); | |
} | |
} | |
fn(); | |
return 0; | |
} | |
if(2==mkDebug){ | |
console.log(window.location.href); | |
console.log('render: '+mkUrl); | |
console.log(mkExt); | |
console.log(mkOrigin); | |
} | |
if('object'!=typeof output){ | |
return mk.parseR('object'==typeof input?input.value:input.toString()); | |
} | |
if(!output.firstChild){ | |
output.appendChild(mk_createElementMML('span')); | |
} | |
var id = !!output.dataset.mkid ? output.dataset.mkid : output.dataset.mkid='amid'+Date.now(); | |
fn = null; | |
fn = function(){ | |
if(!!mkSync.mathKeyInput||!!mkSync.mathKeyTranslate){ | |
mkSync[id]=2; | |
}else if(mkSync[id]==2){ | |
mkSync[id]=0; | |
} | |
!!mkSync[id] ? window.setTimeout(fn,500) : ((mkSync[id]=1),(window.clearTimeout(mkToParseSo[id]||0),output.replaceChild(mk.parseR('object'==typeof input?input.value:input.toString()),output.firstChild)),(output.mathKey=output.firstChild.mathKey),mk_fallback(output),(mkToParseSo[id]=window.setTimeout(function(){ while(1<output.children.length){output.removeChild(output.lastChild);} (output.replaceChild(mk.parseR('object'==typeof input?input.value:input.toString()),output.firstChild)),(output.mathKey=output.firstChild.mathKey),(!!cb&&cb(output)),mk_fallback(output,true);},500)),(mkSync[id]=0)); | |
return 0; | |
}; | |
return !!(mkSync[id] ^ fn()) ? false : true; | |
}; | |
mk.insertText = function(input, text, s0=null, s1=null, c=0){ | |
// Most of the used APIs only work with the field selected | |
input.focus(); | |
var start = null!==s0? input.selectionStart+s0:input.selectionStart; | |
var end = null!==s1?input.selectionStart+s1:input.selectionEnd; | |
input.value = input.value.substr(0,start)+text+input.value.substr(end); | |
input.setSelectionRange(start + text.length+c, start + text.length+c); | |
}; | |
mk.processIO = (function(){ | |
var wsd = {'\u200b':true,'\u200c':true}, isd = {'_':'\uff3f'}; | |
return function(input, output, cb=null) { | |
if('object'!=typeof input){ | |
return mk_parseSo(input,output,cb); | |
} | |
if(-1==input.className.indexOf('hasMathKey')){ | |
input.className = !!input.className? input.className+' hasMathKey':'hasMathKey'; | |
var mkd = mkDoc.createElement('div'), r = null, btop=0, click=false, cs = input.getBoundingClientRect(); | |
input.style.backgroundPosition = (!!input.style.backgroundPosition?input.style.backgroundPosition+',':'')+'right bottom'; | |
input.style.backgroundRepeat = (!!input.style.backgroundRepeat?input.style.backgroundRepeat+',':'')+'no-repeat'; | |
input.style.backgroundImage = (!!input.style.backgroundImage?input.style.backgroundImage+',':'')+ 'url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAK6wAACusBgosNWgAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNXG14zYAAAAWdEVYdENyZWF0aW9uIFRpbWUAMTAvMjYvMTiaHFIxAAAByklEQVRIie2WUW/TMBSFv3sTO0lLyyp1PBQhAdoj//+PgNS3iWdUVaAONfFsXx6aVmWtxLKiCaT50c7xl2MfX1vMjOdo+iyUF9D/DfLeL6qqWvwtUHmus67reUrprZlRVVXoum51KejEUdM01zHGGzMrgCLGeDMaja4vBcnxga3r+k2M8WPOuQUQEcwMVa3Lsrxt2/bbU0EHR9PptIgxLnLOAVgCrZm1wDLnHGKMi/F4XFzkSFUxMymKojGzlFLqVPUTQM75S1EUlYgUZrYFLKU0GFRWVTUBrgCXc0ZE2qZpVoAB1HXtgXnOuQIQkXvv/fcQwmYQKMb4HhiLSOwnjymlH4D1++eAObuESq+5Aj4PAvWTJeArcKeqWUQCoCLCZDL5uV6vl+z285WZfRgCOIBExPpf7/ZpK8ty74TVaoWZBQBVdYCJyOC7pTyK9yGBqgq71MmD759csvaOfusMIdA0za2Z0bbtiehE8BgQ4HtxPh7YbrfdGUAWEVTVDwb1p1+cc81sNrsPIZxdHu99ds41McaHy/k4ENCKyOuU0rvNZpPYRfikdV1n+/oH3A0GOedWXdeNzcyJiPuTQFVjWZaDq7m8vIL+edAv4ZHow8JhvWcAAAAASUVORK5CYII=)'; | |
input.addEventListener('mousemove',function(e){ | |
var p = cs.right-cs.left; | |
if((cs.bottom-cs.top-28)<e.layerY && (p-28)<e.layerX && (p-14)>e.layerX ){ | |
click=true; | |
this.style.cursor = 'pointer'; | |
}else if(click){ | |
this.style.cursor = 'text'; | |
click = false; | |
} | |
}); | |
input.addEventListener('click',function(e){ | |
if(click){ | |
window.location.href = 'https://mathkey.org/sh.htm?e='+mk_b64e(input.value); | |
console.log('click'); | |
console.log(e); | |
} | |
}); | |
} | |
input.addEventListener('keydown', function(e){ | |
var del=false, pos = this.selectionStart, selection = null; | |
if(e.key==='Backspace'){ | |
if(this.selectionStart===this.selectionEnd){ | |
var c=''; | |
selection = this.selectionStart; | |
while(input.value.length>++selection && '\u200c'==input.value.charAt(selection)); | |
--selection; | |
while(-1<--selection && '\u200c'==input.value.charAt(selection)){ | |
mk.insertText(input,'',-1); | |
del=true; | |
} | |
pos = this.selectionStart; | |
if(!!(c=isd[this.value.charAt(selection)])){ | |
mk.insertText(input,'',-1); | |
mk.insertText(input,c); | |
input.setSelectionRange(pos, pos); | |
mk_parseSo(input, output, cb); | |
e.preventDefault(); | |
e.stopPropagation(); | |
return false; | |
} | |
c = pos = this.selectionStart; | |
while(-1<--selection && '\u200c'!=input.value.charAt(selection)){ | |
if('\u200b'==input.value.charAt(selection)){ | |
input.setSelectionRange(selection+1, selection+1); | |
mk.insertText(input,'',-1); | |
--pos; | |
} | |
} | |
if(c!=pos){ | |
input.setSelectionRange(pos, pos); | |
} | |
pos=false; | |
(' '==this.value.charAt(this.selectionStart-1) || '^'==this.value.charAt(this.selectionStart-1))? del=true :pos = mk.checkR(this.value, this.selectionStart); | |
if(!del && false!==pos){ | |
mk.insertText(input, '\u200c'); | |
selection = this.selectionStart; | |
mk.insertText(input, '\u200b', pos-this.selectionStart, pos-this.selectionStart ); | |
input.setSelectionRange(selection+1, selection+1); | |
mk_parseSo(input, output, cb); | |
e.preventDefault(); | |
e.stopPropagation(); | |
return false; | |
} | |
} | |
}else if('ArrowLeft'==e.key){ | |
pos = selection = this.selectionStart; | |
while(0<selection && !!wsd[this.value.charAt(--selection)]); | |
if(selection==--pos){ | |
selection=null; | |
} | |
}else if('ArrowRight'==e.key){ | |
var len = this.value.length-1; | |
selection = this.selectionStart-1; | |
while(selection<len && !!wsd[this.value.charAt(++selection)]); | |
if(selection==pos){ | |
selection=null; | |
}else if(selection<=len){ | |
++selection; | |
} | |
} | |
if(null!=selection && 'Backspace'!=e.key){ | |
input.setSelectionRange(selection, selection); | |
e.preventDefault(); | |
e.stopPropagation(); | |
} | |
}); | |
input.addEventListener('keyup', function(e){ | |
if(!!e.ctrlKey && !!conf.manage && conf.manage==e.key){ | |
var r = this.getBoundingClientRect(); | |
mk.management(mkSymbols,r.top); | |
}else if(!!e.ctrlKey && '\''==e.key){ | |
window.location.href = 'https://mathkey.org/sh.htm?e='+mk_b64e(this.value); | |
}else if(((e.key.length==1 && e.key.charCodeAt(0)>31) || 'Backspace'==e.key || 'Enter'==e.key)&&(!e.ctrlKey || 'c'==e.key || 'v'==e.key || 'z'==e.key || 'y'==e.key)){ | |
mk_parseSo(input, output, cb); | |
} | |
}); | |
}; | |
})(); | |
var mk_compareSymbolInput = function(a,b){ | |
return a.input===b.input?0:(a.input.toLowerCase()<b.input.toLowerCase()?-1:1); | |
}; | |
var mk_extract = function(v, a, k=''){ | |
if(!!k){k+='.';} | |
if('object' === typeof v && !Array.isArray(v)){ | |
for(var i in v){ | |
if('vinput'==i){ | |
continue; | |
} | |
if(undefined===a[k+i]){ | |
a[k+i] = ['object'===typeof v[i] ? (Array.isArray(v[i])?[[],'[]']:[{},null]):(!!v[i].charAt?['',null]:(!!v[i].toPrecision?('type'==i?[VAR,'"VAR"']:[0,null]):[false,'true, false'])),{}]; | |
} | |
if('object' === typeof v[i] && !Array.isArray(v[i])){ | |
mk_extract(v[i], a, k+i); | |
}else if('type'==i){ | |
a[k+i][1][mkRdef[v[i]]] = true; | |
}else if((!!v[i].charAt && -1==i.indexOf('input') && -1==i.indexOf('output'))||v[i].toPrecision){ | |
a[k+i][1][v[i]]=true; | |
}else if(!!a[k+i][0][1]){ | |
a[k+i][1][a[k+i][0][1]]= true; | |
}else if(Array.isArray(v[i])){ | |
try{ | |
a[k+i][1][JSON.stringify(v[i])]=true; | |
}catch(e){ | |
console.log(v[i]); | |
} | |
} | |
} | |
} | |
}; | |
var mk_ckcopy = function(v){var k = mk_cloneSymbol(v); if(undefined!==k.type && undefined!==mkRdef[k.type]){k.type=mkRdef[k.type];} return k; }; | |
mk.management = function(symbols, topx=-1, display=true){ | |
var c='',ck='',j='',cksym=null, lb = null,lba=false, mkmng = conf.managementinterface, examples = {'!':'5!', '/':'1/2', "'":"f'", '(':'(x,y)', '{':'{x,y}', '[':'[{x},{y}]', '\\':'\\x,y/', '^':'x^2', '_':'x_i', '`':'x`y', 'bar':'barx', 'styleb':'styleby', 'stylebb':'stylebby', 'fontsf':'fontsfx', 'fontcal':'fontcaly', 'fontmono':'fontmonox', 'Conj':'Conj(x+y)', 'root':'root(x,y)', 'stackrel':'stackrel(x,y)'}; | |
if(!conf.manage || !mkmng.html || !Array.isArray(mkmng.html) || 5>mkmng.html.length){ | |
return; | |
} | |
if(!mkmng.style || 'object' !== typeof mkmng.style){ | |
return; | |
} | |
if(!mkmng.rowcolor || !Array.isArray(mkmng.rowcolor) || undefined===mkmng.rowcolor[0] || undefined===mkmng.rowcolor[1]){ | |
mkmng.rowcolor = ['#ffffff','#fcfcfc']; | |
} | |
if(!(lb=mkDoc.querySelector('#mathKeyLightBox'))){ | |
lba = true; | |
lb = mkDoc.createElement('div'); | |
lb.id = 'mathKeyLightBox'; | |
for (c in mkmng.style){ | |
lb.style[c.toString()] = mkmng.style[c]; | |
} | |
window.addEventListener('keydown',function(e){ | |
if('Escape'==e.key){ | |
lb.style.display='none'; | |
} | |
}); | |
if(!mkCk){ | |
mkCk = true; | |
ck = mkDoc.cookie.split(';'); | |
for(c in ck){ | |
if(0==ck[c].indexOf('mathKey=')){ | |
try{ | |
cksym=!!(c = decodeURIComponent(ck[c].slice(8, (j=ck[c].indexOf(';'))==-1?undefined:j)).trim())?JSON.parse(c):null; | |
}catch(err){ | |
console.log('Could not decode '+ck[c]); | |
} | |
break; | |
} | |
} | |
if(!!cksym){ | |
mkSym = cksym.map(mk_acopy); | |
for(c in cksym){ | |
if(!!cksym[c].input){ | |
if(!!cksym[c].type && undefined!==mkDef[cksym[c].type]){ | |
cksym[c].type = mkDef[cksym[c].type]; | |
} | |
(undefined!==(j=mkNames[cksym[c].input]))? ((undefined===cksym[c].type)? (mkSymbols[j]=null) : (mkSymbols[j] = cksym[c])) : mkSymbols.push(cksym[c]); | |
} | |
} | |
} | |
} | |
} | |
if(!!cksym){ | |
mk.reInit(); | |
} | |
ck = mkSymbols.map(mk_ckcopy); | |
if(!display && -1<topx){ | |
if(0<topx){ | |
cksym = ck.splice(topx,1); | |
} | |
ck.sort(mk_compareSymbolInput); | |
if(0<topx){ | |
ck.unshift(cksym[0]); | |
} | |
if(!!lb.firstChild){ | |
topx = lb.firstChild.style.top; | |
} | |
}else{ | |
ck.sort(mk_compareSymbolInput); | |
} | |
cksym = JSON.stringify(ck).replace(/(?:^[\[\{ \t\n]+)|(?:[\]\} \t\n]+$)/ug,'').split(/\},[ ]*\{/u); | |
c = mkmng.rowcolor[0]; | |
lb.innerHTML=mkmng.html[0]+mkmng.html[1]+ck.map(function(v,i){c=c==mkmng.rowcolor[0]?mkmng.rowcolor[1]:mkmng.rowcolor[0]; return mkmng.html[2].replace(/%MKROWCOLOR%/ug, c).replace(/%MKENTRY%/ug, cksym[i]).replace(/%MKDICT%/ug, examples[ck[i].vinput || ck[i].input] || ck[i].vinput || ck[i].input); }).join(mkmng.html[3]+mkmng.html[1])+mkmng.html[3]+mkmng.html[4]; | |
mkFields = {}; | |
for(c=-1;mkSymbols.length>++c;){ | |
mk_extract(mkSymbols[c], mkFields); | |
} | |
mkFields._ = Object.keys(mkFields); | |
mkFields._.sort(); | |
if(!!(c = mkDoc.querySelector('.mathKeyFields'))){ | |
c.innerHTML = '<option value="" style="color:#ccc;">--fields--</option>'+mkFields._.map(function(v){return '<option value="'+v.replace(/"/ug,'')+'">'+v.replace(/[<>]/ug,'')+'</option>';}).join(''); | |
} | |
delete mkFields._; | |
if(-1<topx){ | |
lb.firstChild.style.top = topx+'px'; | |
} | |
if(!!lba){ | |
mkDoc.body.appendChild(lb); | |
} | |
if(!!display){ | |
mk.translate('mathKeyTranslateDict'); | |
lb.style.display = 'block'; | |
if(-1<topx){ | |
c = lb.firstChild.getBoundingClientRect(); | |
lb.style.height=c.height+topx+20+'px'; | |
} | |
} | |
}; | |
mk.manageFieldData = function(){ | |
var c = '', f = mkDoc.querySelector('.mathKeyFields'), x = mkDoc.querySelector('.mathKeyFex'); | |
x.innerHTML=''; | |
if(!!mkFields[f.value]){ | |
if(!!(c = Object.keys(mkFields[f.value][1]).join(', '))){ | |
x.innerHTML='<i>ex:</i>'+c; | |
} | |
} | |
}; | |
mk.manageField = function(){ | |
var c=null, f = mkDoc.querySelector('.mathKeyFields'), j=null, n='', p=null, q=mkDoc.querySelector('.mathKeyInput'), r = ''; | |
if(!!f && !!f.value && !! mkFields[f.value] && !!q){ | |
if(!!(r=q.value.trim())){ | |
try{ | |
j = JSON.parse(r); | |
}catch(e){ | |
j=null; | |
} | |
} | |
if(!!j){ | |
p = j; | |
f.value.split('.').filter(function(v){ | |
n+=!!n?'.'+v:v; | |
if(undefined === p[v]){ | |
p[v] = mkFields[n][0][0]; | |
} | |
p=p[v]; | |
}); | |
q.value = JSON.stringify(j); | |
}else{ | |
r = r.replace(/(?:^\{)|(?:[ ,}]+$)/,'').trim(); | |
if(!!r){ | |
r+=', '; | |
} | |
j = p = {}; | |
f.value.split('.').filter(function(v){ | |
n+=!!n?'.'+v:v; | |
if(undefined === p[v]){ | |
p[v] = mkFields[n][0][0]; | |
} | |
p=p[v]; | |
}); | |
q.value = '{'+r+JSON.stringify(j).replace(/^\{/u,'').replace(/\}$/u,'').trim()+'}'; | |
} | |
} | |
}; | |
mk.manageCopy = function(pn){ | |
if(!!pn.firstChild){ | |
mkDoc.querySelector('.mathKeyInput').value = pn.children[1].innerHTML; | |
mk.manageInput(); | |
} | |
}; | |
mk.manageDelete = function(pn){ | |
if(!!pn.firstChild){ | |
var c = null, j = -1; | |
try{ | |
c = JSON.parse(pn.firstChild.innerHTML); | |
}catch(err){ | |
console.log(err); | |
} | |
if(!!c){ | |
if(undefined!==c.type && undefined!==mkDef[c.type]){ | |
c.type = mkDef[c.type]; | |
} | |
if(-1!=(j=mkSym.indexOf(c))){ | |
mkSym.splice(j,1); | |
} | |
for(var i in mkSym){ | |
if(c.input===mkSym[i].input){ | |
mkSym.splice(i,1); | |
break; | |
} | |
} | |
delete c.type; | |
mkSym.push(c); | |
mk_setCk(); | |
pn.parentNode.removeChild(pn); | |
mk.reInit(); | |
mk.management(mkSymbols,j,false); | |
} | |
} | |
}; | |
var mk_setCk = function(){ | |
var d = new Date(); | |
d.setTime(d.getTime() + (365*24*60*60*1000)); | |
//document.cookie = 'mathKey=; expires=-1; path=/'; | |
document.cookie = 'mathKey='+encodeURIComponent(JSON.stringify(mkSym))+'; expires=' + d.toUTCString()+ '; path=/'; | |
}; | |
mk.manageReset = function(){ | |
document.cookie = 'mathKey=; expires=-1; path=/'; | |
mkSym.splice(0) | |
mkSymbols=mkBak.map(function(v){return mk_cloneSymbol(v);}); | |
mk.reInit(); | |
mk.management(mkSymbols,0,false); | |
mk.translate('mathKeyTranslateDict'); | |
}; | |
mk.manageAdd = function(){ | |
var c = mk.manageInput(), i, j = -1; | |
if(!!c && undefined!==c.type){ | |
undefined!==(j=mkNames[c.input])? (mkSymbols[j] = c) : ((j=mkSymbols.length),mkSymbols.push(c)); | |
for(i in mkSym){ | |
if(mkSym[i].input==c.input){ | |
mkSym.splice(i,1); | |
break; | |
} | |
} | |
mkSym.push(c); | |
mk_setCk(); | |
mk.reInit(); | |
mk.management(mkSymbols,j,false); | |
} | |
}; | |
mk.manageInput = function(){ | |
if(!!mkSync['mathKeyInput']){ | |
if(true!==mkSync['mathKeyInput']){ | |
window.clearTimeout(mkSync['mathKeyInput']); | |
} | |
false===mkSync['mathKeyInput']? mk.manageInput(): (mkSync['mathKeyInput']=window.setTimeout(mk.manageInput,1000)); | |
return null; | |
} | |
mkSync['mathKeyInput'] = true; | |
var c = null, input = mkDoc.querySelector('.mathKeyInput'), output = mkDoc.querySelector('.mathKeyOutput'); | |
if(input.value.trim().match(/^\{.*?\}$/u)){ | |
try { | |
c = JSON.parse(input.value.trim()); | |
}catch(err){ | |
console.log(err); | |
} | |
if(!!c && !!c.input && undefined!==c.type && undefined!==c.output && undefined!==c.ptag && undefined!==mkDef[c.type]){ | |
var i=-1,j=-1,k=[],o=null; | |
c.type=mkDef[c.type]; | |
if(undefined==mkNames[c.input]){ | |
mkNames[c.input] = mkSymbols.length; | |
mkSymbols.push(c); | |
j = c.input.length; | |
while(j>++i){ | |
k.unshift(c.input.slice(-i)); | |
if(!!mkRnames[k[0]]){ | |
k.shift(); | |
continue; | |
} | |
mkRnames[k[0]] = true; | |
} | |
} | |
o = mk.parseR(c.input); | |
if(j>-1){ | |
delete mkNames[c.input]; | |
mkSymbols.pop(); | |
k.map(function(v){delete mkRnames[v];}); | |
} | |
if(!!o){ | |
if(!output.firstChild){ | |
output.appendChild(mk_createElementMML('span')); | |
} | |
output.replaceChild(o,output.firstChild); | |
}else{ | |
c = null; | |
} | |
} | |
} | |
mkSync['mathKeyInput'] = false; | |
return c; | |
}; | |
var mkUpdateIO = null; | |
mk.updateIO = function(input=null, output=null, val=null, fn=null) { | |
if(!input && !!mkUpdateIO[0]){ | |
input = mkUpdateIO[0]; | |
output = mkUpdateIO[1]; | |
fn = mkUpdateIO[2]; | |
} | |
if('object'==typeof input){ | |
input.focus(); | |
if(null!==val){ | |
mk.insertText(input, val); | |
} | |
mkUpdateIO = [input, output, fn]; | |
} | |
mk_parseSo(input,output,fn); | |
}; | |
var mk_translateOnStart = function(){ | |
if(!!mkSync['mathKeyTranslate']){ | |
return window.setTimeout(mk_translateOnStart,250); | |
} | |
mkDoc.readystate != 'loading'? mkDoc.dispatchEvent(new CustomEvent('mktranslate')) : (mkDoc.onreadystatechange = function(){if('interactive'==mkDoc.readyState){mkDoc.dispatchEvent(new CustomEvent('mktranslate'));}}); | |
}; | |
var mk_createElementMML = null; | |
mk.configure = function(cf={}, ini=false){ | |
for(var i in cf){ | |
if(undefined!==conf[i]){ | |
conf[i] = 'document'==i ? cf[i] : mk_cloneSymbol(cf[i]); | |
} | |
} | |
return !!ini? (mk_fallbackCss(),true) : mk_init(cf,false); | |
}; | |
var mk_init = function(cf=null,ini=true){ | |
if(!!cf && !!ini){ | |
mk.configure(cf, ini); | |
} | |
mkDoc = (!!conf.document && !!conf.document.body) ? conf.document : document; | |
if(!conf.management || 'object' !== typeof conf.managementinterface){ | |
conf.managementinterface = mk_cloneSymbol(mkMng); | |
} | |
if(!mkConfigured){ | |
if(!!mkDoc.createElementNS){ | |
mk_createElementMML = function(t,df=null,xml=null) { | |
var node = (xml || mkDoc).createElementNS(mkMathns,t); | |
if (!!df){ | |
node.appendChild(df); | |
} | |
node.removeAttribute('xmlns'); | |
return node; | |
}; | |
}else{ | |
mk_createElementMML = function(t,df=null,xml=null) { | |
var node = (xml || mkDoc).createElement('m:'+t); | |
if (!!df) node.appendChild(df); | |
return node; | |
}; | |
} | |
mk.getUserAgent(); | |
} | |
mk.reInit(); | |
(!!conf.checkformathml && !mkConfigured) ? mk_checkMathML() : (mkReady=true); | |
if(!mkConfigured){ | |
mkConfigured = true; | |
mk.management(mkSymbols,-1,false); | |
mkDoc.addEventListener('mktranslate', function(){mk.translate(); }, false); | |
if (conf.translateonstart && !!mkReady) { | |
mk_translateOnStart(); | |
} | |
} | |
return mk; | |
}; | |
(function(){ | |
var a = false, b = '', d = 'false', i, s = document.getElementsByTagName('script'); | |
for(i in s){ | |
if(!!s[i].src && s[i].src.match(/(?:^|\/)mathKey[^\/]*\.js[^\/]*$/) && undefined!=(a=s[i].dataset.mkAutostart)){ | |
d = true; | |
break; | |
}else{ | |
a=''; | |
} | |
} | |
if(d){ | |
b = a.toLowerCase().trim(); | |
if(!!(d=a.trim()) && 'false'!==b && '0'!==b && 'true'!==b && typeof window[d] === 'function'){ | |
window[d](mk); | |
}else if(!!d && 'false'!==b && '0'!==b){ | |
mk_init(); | |
}else{ | |
window['mathKey'] = mk; | |
} | |
} | |
})(); | |
return mk; | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment