MathMLinHTML.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. March 19, 2004 MathHTML (c) Peter Jipsen http://www.chapman.edu/~jipsen
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or (at
  6. your option) any later version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  9. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  10. (at http://www.gnu.org/copyleft/gpl.html) for more details.
  11. */
  12. function convertMath(node) {// for Gecko
  13. if (node.nodeType==1) {
  14. var newnode =
  15. document.createElementNS("http://www.w3.org/1998/Math/MathML",
  16. node.nodeName.toLowerCase());
  17. for(var i=0; i < node.attributes.length; i++)
  18. newnode.setAttribute(node.attributes[i].nodeName,
  19. node.attributes[i].nodeValue);
  20. for (var i=0; i<node.childNodes.length; i++) {
  21. var st = node.childNodes[i].nodeValue;
  22. if (st==null || st.slice(0,1)!=" " && st.slice(0,1)!="\n")
  23. newnode.appendChild(convertMath(node.childNodes[i]));
  24. }
  25. return newnode;
  26. }
  27. else return node;
  28. }
  29. function convert() {
  30. var mmlnode = document.getElementsByTagName("math");
  31. var st,str,node,newnode;
  32. for (var i=0; i<mmlnode.length; i++)
  33. if (document.createElementNS!=null)
  34. mmlnode[i].parentNode.replaceChild(convertMath(mmlnode[i]),mmlnode[i]);
  35. else { // convert for IE
  36. str = "";
  37. node = mmlnode[i];
  38. while (node.nodeName!="/MATH") {
  39. st = node.nodeName.toLowerCase();
  40. if (st=="#text") str += node.nodeValue;
  41. else {
  42. str += (st.slice(0,1)=="/" ? "</m:"+st.slice(1) : "<m:"+st);
  43. if (st.slice(0,1)!="/")
  44. for(var j=0; j < node.attributes.length; j++)
  45. if (node.attributes[j].nodeValue!="italic" &&
  46. node.attributes[j].nodeValue!="" &&
  47. node.attributes[j].nodeValue!="inherit" &&
  48. node.attributes[j].nodeValue!=undefined)
  49. str += " "+node.attributes[j].nodeName+"="+
  50. "\""+node.attributes[j].nodeValue+"\"";
  51. str += ">";
  52. }
  53. node = node.nextSibling;
  54. node.parentNode.removeChild(node.previousSibling);
  55. }
  56. str += "</m:math>";
  57. newnode = document.createElement("span");
  58. node.parentNode.replaceChild(newnode,node);
  59. newnode.innerHTML = str;
  60. }
  61. }
  62. if (document.createElementNS==null) {
  63. document.write("<object id=\"mathplayer\"\
  64. classid=\"clsid:32F66A20-7614-11D4-BD11-00104BD3F987\"></object>");
  65. document.write("<?import namespace=\"m\" implementation=\"#mathplayer\"?>");
  66. }
  67. if(typeof window.addEventListener != 'undefined'){
  68. window.addEventListener('load', convert, false);
  69. }
  70. if(typeof window.attachEvent != 'undefined') {
  71. window.attachEvent('onload', convert);
  72. }