mml2jax.js 4.2 KB

12345678910111213
  1. /*
  2. * /MathJax/extensions/mml2jax.js
  3. *
  4. * Copyright (c) 2009-2013 The MathJax Consortium
  5. *
  6. * Part of the MathJax library.
  7. * See http://www.mathjax.org for details.
  8. *
  9. * Licensed under the Apache License, Version 2.0;
  10. * you may not use this file except in compliance with the License.
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. */MathJax.Extension.mml2jax={version:"2.2",config:{preview:"alttext"},MMLnamespace:"http://www.w3.org/1998/Math/MathML",PreProcess:function(a){this.configured||(this.config=MathJax.Hub.CombineConfig("mml2jax",this.config),this.config.Augment&&MathJax.Hub.Insert(this,this.config.Augment),this.InitBrowser(),this.configured=!0);"string"===typeof a&&(a=document.getElementById(a));a||(a=document.body);this.ProcessMathArray(a.getElementsByTagName("math"));a.getElementsByTagNameNS&&this.ProcessMathArray(a.getElementsByTagNameNS(this.MMLnamespace,"math"));var b,c;if("undefined"!==typeof document.namespaces)try{for(b=0,c=document.namespaces.length;b<c;b++){var d=document.namespaces[b];d.urn===this.MMLnamespace&&this.ProcessMathArray(a.getElementsByTagName(d.name+":math"))}}catch(f){}else if(d=document.getElementsByTagName("html")[0])for(b=0,c=d.attributes.length;b<c;b++){var e=d.attributes[b];"xmlns:"===e.nodeName.substr(0,6)&&e.nodeValue===this.MMLnamespace&&this.ProcessMathArray(a.getElementsByTagName(e.nodeName.substr(6)+":math"))}},ProcessMathArray:function(a){var b;if(a.length)if(this.MathTagBug)for(b=a.length-1;0<=b;b--)"MATH"===a[b].nodeName?this.ProcessMathFlattened(a[b]):this.ProcessMath(a[b]);else for(b=a.length-1;0<=b;b--)this.ProcessMath(a[b])},ProcessMath:function(a){var b=a.parentNode,c=document.createElement("script");c.type="math/mml";b.insertBefore(c,a);if(this.AttributeBug){var d=this.OuterHTML(a);this.CleanupHTML&&(d=d.replace(/<\?import .*?>/i,"").replace(/<\?xml:namespace .*?\/>/i,""),d=d.replace(/&nbsp;/g,"\x26#xA0;"));MathJax.HTML.setScript(c,d);b.removeChild(a)}else b=MathJax.HTML.Element("span"),b.appendChild(a),MathJax.HTML.setScript(c,b.innerHTML);"none"!==this.config.preview&&this.createPreview(a,c)},ProcessMathFlattened:function(a){var b=a.parentNode,c=document.createElement("script");c.type="math/mml";b.insertBefore(c,a);for(var b="",d,e=a;a&&"/MATH"!==a.nodeName;)d=a,a=a.nextSibling,b+=this.NodeHTML(d),d.parentNode.removeChild(d);a&&"/MATH"===a.nodeName&&a.parentNode.removeChild(a);c.text=b+"\x3c/math\x3e";"none"!==this.config.preview&&this.createPreview(e,c)},NodeHTML:function(a){var b,c,d;if("#text"===a.nodeName)b=this.quoteHTML(a.nodeValue);else if("#comment"===a.nodeName)b="\x3c!--"+a.nodeValue+"--\x3e";else{b="\x3c"+a.nodeName.toLowerCase();c=0;for(d=a.attributes.length;c<d;c++){var e=a.attributes[c];if(e.specified){b+=" "+e.nodeName.toLowerCase().replace(/xmlns:xmlns/,"xmlns")+"\x3d";var f=e.nodeValue;null==f&&"style"===e.nodeName&&a.style&&(f=a.style.cssText);b+='"'+this.quoteHTML(f)+'"'}}b+="\x3e";if(null!=a.outerHTML&&a.outerHTML.match(/(.<\/[A-Z]+>|\/>)$/)){c=0;for(d=a.childNodes.length;c<d;c++)b+=this.OuterHTML(a.childNodes[c]);b+="\x3c/"+a.nodeName.toLowerCase()+"\x3e"}}return b},OuterHTML:function(a){if("#"===a.nodeName.charAt(0))return this.NodeHTML(a);if(!this.AttributeBug)return a.outerHTML;for(var b=this.NodeHTML(a),c=0,d=a.childNodes.length;c<d;c++)b+=this.OuterHTML(a.childNodes[c]);return b+="\x3c/"+a.nodeName.toLowerCase()+"\x3e"},quoteHTML:function(a){null==a&&(a="");return a.replace(/&/g,"\x26#x26;").replace(/</g,"\x26lt;").replace(/>/g,"\x26gt;").replace(/\"/g,"\x26quot;")},createPreview:function(a,b){var c=this.config.preview;"none"!==c&&("alttext"===c&&(c=a.getAttribute("alttext"),c=null!=c?[this.filterPreview(c)]:null),c&&(c=MathJax.HTML.Element("span",{className:MathJax.Hub.config.preRemoveClass},c),b.parentNode.insertBefore(c,b)))},filterPreview:function(a){return a},InitBrowser:function(){var a=MathJax.HTML.Element("span",{id:"\x3c",className:"mathjax",innerHTML:"\x3cmath\x3e\x3cmi\x3ex\x3c/mi\x3e\x3cmspace /\x3e\x3c/math\x3e"}),b=a.outerHTML||"";this.AttributeBug=""!==b&&!(b.match(/id="&lt;"/)&&b.match(/class="mathjax"/)&&b.match(/<\/math>/));this.MathTagBug=1<a.childNodes.length;this.CleanupHTML=MathJax.Hub.Browser.isMSIE}};MathJax.Hub.Register.PreProcessor(["PreProcess",MathJax.Extension.mml2jax]);MathJax.Ajax.loadComplete("[MathJax]/extensions/mml2jax.js");