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-03 09:03:06 +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][a-z0-9]*);/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(h){var l=h.nodeName.toLowerCase().replace(/^[a-z]+:/,"");if(!(a[l]&&a[l].isa&&a[l].isa(a.mbase))){return a.merror("Unknown node type: "+l)}var d=a[l](),k,j,e,n;for(k=0,j=h.attributes.length;k<j;k++){e=h.attributes[k].name;if(e=="xlink:href"){e="href"}if(e.match(/:/)){continue}n=h.attributes[k].value;if(n.toLowerCase()==="true"){n=true}else{if(n.toLowerCase()==="false"){n=false}}d[e]=n}for(k=0,j=h.childNodes.length;k<j;k++){var f=h.childNodes[k];if(f.nodeName==="#comment"){continue}if(f.nodeName==="#text"){if(d.isToken&&!d.mmlSelfClosing){var o=this.trimSpace(f.nodeValue);if(d.isa(a.mo)&&o.length===1&&this.Remap[o.charAt(0)]){o=this.Remap[o.charAt(0)]}o=o.replace(/&([a-z][a-z0-9]*);/ig,this.replaceEntity);d.Append(a.chars(o))}else{if(f.nodeValue.match(/\S/)){b.Error("Unexpected text node: '"+f.nodeValue+"'")}}}else{if(d.type==="annotation-xml"){d.Append(a.xml(f))}else{var g=this.MakeMML(f);d.Append(g);if(g.mmlSelfClosing&&g.data.length){d.Append.apply(d,g.data);g.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.match(/^(lt|amp|quot)$/)){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){if(!this.ParseXML){this.ParseXML=this.createParser()}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},parseError:function(d){return null},createParser:function(){if(window.DOMParser){this.parser=new DOMParser();return(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.