instiki/public/MathJax/jax/input/MathML/jax.js

17 lines
12 KiB
JavaScript
Raw Normal View History

/*
2011-01-28 02:12:00 +01:00
* /MathJax/jax/input/MathML/jax.js
*
* Copyright (c) 2010 Design Science, Inc.
*
* Part of the MathJax library.
* See http://www.mathjax.org for details.
*
* Licensed under the Apache License, Version 2.0;
* you may not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
2011-06-01 21:35:18 +02:00
(function(b,c){var a;b.Parse=MathJax.Object.Subclass({Init:function(d){this.Parse(d)},Parse:function(f){var g;if(typeof f!=="string"){g=f.parentNode}else{if(f.match(/^<[a-z]+:/i)&&!f.match(/^<[^<>]* xmlns:/)){f=f.replace(/^<([a-z]+)(:math)/i,'<$1$2 xmlns:$1="http://www.w3.org/1998/Math/MathML"')}f=f.replace(/^\s*(?:\/\/)?<!(--)?\[CDATA\[((.|\n)*)(\/\/)?\]\]\1>\s*$/,"$2");f=f.replace(/&([a-z]+);/ig,this.replaceEntity);g=b.ParseXML(f);if(g==null){b.Error("Error parsing MathML")}}var e=g.getElementsByTagName("parsererror")[0];if(e){b.Error("Error parsing MathML: "+e.textContent.replace(/This page.*?errors:|XML Parsing Error: |Below is a rendering of the page.*/g,""))}if(g.childNodes.length!==1){b.Error("MathML must be formed by a single element")}if(g.firstChild.nodeName.toLowerCase()==="html"){var d=g.getElementsByTagName("h1")[0];if(d&&d.textContent==="XML parsing error"&&d.nextSibling){b.Error("Error parsing MathML: "+String(d.nextSibling.nodeValue).replace(/fatal parsing error: /,""))}}if(g.firstChild.nodeName.toLowerCase().replace(/^[a-z]+:/,"")!=="math"){b.Error("MathML must be formed by a <math> element, not <"+g.firstChild.nodeName+">")}this.mml=this.MakeMML(g.firstChild)},MakeMML:function(g){var k=g.nodeName.toLowerCase().replace(/^[a-z]+:/,"");if(!(a[k]&&a[k].isa&&a[k].isa(a.mbase))){return a.merror("Unknown node type: "+k)}var d=a[k](),j,h,l;for(j=0,h=g.attributes.length;j<h;j++){l=g.attributes[j].value;if(l.toLowerCase()==="true"){l=true}else{if(l.toLowerCase()==="false"){l=false}}d[g.attributes[j].name]=l}for(j=0,h=g.childNodes.length;j<h;j++){var e=g.childNodes[j];if(e.nodeName==="#comment"){continue}if(e.nodeName==="#text"){if(d.isToken&&!d.mmlSelfClosing){var n=this.trimSpace(e.nodeValue);if(d.isa(a.mo)&&n.length===1&&this.Remap[n.charAt(0)]){n=this.Remap[n.charAt(0)]}n=n.replace(/&([a-z]+);/ig,this.replaceEntity);d.Append(a.chars(n))}else{if(e.nodeValue.match(/\S/)){b.Error("Unexpected text node: '"+e.nodeValue+"'")}}}else{var f=this.MakeMML(e);d.Append(f);if(f.mmlSelfClosing&&f.data.length){d.Append.apply(d,f.data);f.data=[]}}}if(b.config.useMathMLspacing){d.useMMLspacing=8}return d},trimSpace:function(d){return d.replace(/^[ \t\n\r]+/,"").replace(/[ \t\n\r]+$/,"").replace(/[ \t\n\r][ \t\n\r]+/g," ")},replaceEntity:function(f,e){if(e==="lt"||e==="amp"){return f}if(b.Parse.Entity[e]){return b.Parse.Entity[e]}var g=e.charAt(0).toLowerCase();var d=e.match(/^[a-zA-Z](fr|scr|opf)$/);if(d){g=d[1]}if(!b.Parse.loaded[g]){b.Parse.loaded[g]=true;MathJax.Hub.RestartAfter(MathJax.Ajax.Require(b.entityDir+"/"+g+".js"))}return f},Remap:{"\u0027":"\u2032","\u002A":"\u2217","\u002D":"\u2212"}},{loaded:[]});b.Augment({Translate:function(d){var e,g;if(d.firstChild&&d.firstChild.nodeName.toLowerCase().replace(/^[a-z]+:/,"")==="math"){g=this.prefilterMathML(d.firstChild)}else{g=d.innerHTML.replace(/^\s+/,"").replace(/\s+$/,"");if(c.isMSIE){g=g.replace(/(&nbsp;)+$/,"")}else{if(c.isKonqueror){g=g.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&")}}g=this.prefilterMath(g,d)}try{e=b.Parse(g).mml}catch(f){if(!f.mathmlError){throw f}e=this.formatError(f,g,d)}return a(e)},prefilterMath:function(e,d){return e},prefilterMathML:function(d){return d},formatError:function(f,e,d){return a.merror(f.message.replace(/\n.*/,""))},Error:function(d){throw MathJax.Hub.Insert(Error(d),{mathmlError:true})},parseDOM:function(d){return this.parser.parseFromString(d,"text/xml")},parseMS:function(d){return(this.parser.loadXML(d)?this.parser:null)},parseDIV:function(d){this.div.innerHTML=d.replace(/<([a-z]+)([^>]*)\/>/g,"<$1$2></$1>");return this.div},Startup:function(){a=MathJax.ElementJax.mml;a.mspace.Augment({mmlSelfClosing:true});a.none.Augment({mmlSelfClosing:true});a.mprescripts.Augment({mmlSelfClossing:true});if(window.DOMParser){this.parser=new DOMParser();this.ParseXML=this.parseDOM}else{if(window.ActiveXObject){var e=["MSXML2.DOMDocument.6.0","MSXML2.DOMDocument.5.0","MSXML2.DOMDocument.4.0","MSXML2.DOMDocument.3.0","MSXML2.DOMDocument.2.0","Microsoft.XMLDOM"];for(var f=0,d=e.length;f<d&&!this.parser;f++){try{this.parser=new Act