From ca718359ccdf12e510473f969b8ecd2894b621a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Wang?= Date: Tue, 5 Mar 2013 12:30:48 +0100 Subject: [PATCH] Handle parsing of the source better. --- unpacked/extensions/MathML/content-mathml.js | 26 ++++++++++---- unpacked/jax/input/MathML/jax.js | 38 +++++++++++++------- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/unpacked/extensions/MathML/content-mathml.js b/unpacked/extensions/MathML/content-mathml.js index d0dbe9d7e..11560a86f 100644 --- a/unpacked/extensions/MathML/content-mathml.js +++ b/unpacked/extensions/MathML/content-mathml.js @@ -41,17 +41,31 @@ MathJax.Extension["MathML/content-mathml"] = { MathJax.Hub.Register.StartupHook("mml Jax Ready",function () { - MathJax.InputJax.MathML.prefilterHooks.Add(function (data) { - var doc = MathJax.InputJax.MathML.ParseXML(data.math); + var MATHML = MathJax.InputJax.MathML; + + MATHML.prefilterHooks.Add(function (data) { + // Parse the MathML source but add a dummy root to handle the HTML5 case + // where the MathML namespace may not be specified on the element. + var doc = MathJax.InputJax.MathML.ParseXML( + "" + + MATHML.Parse(data.math, true) + + ""); + + // Now transform the using the ctop stylesheet. var newMath = MathJax.InputJax.MathML.ctopXSLT. - transformToFragment(doc.documentElement, doc); + transformToFragment(doc.documentElement.firstChild, doc); + + // Serialize the again. var serializer = new XMLSerializer(); data.math = serializer.serializeToString(newMath, doc); }); - var ctopStylesheet = new DOMParser().parseFromString(' +i +i / / ei ei E 0x . (-1) λ. id domain codomain image unexpected domainofapplication restriction {   if     otherwise / ! / max min (+) mod (×) gcd xor ¬ . . , || ¯ arg lcm = > < | d dddd D, + + , div div() grad grad() curl 2 () || × = lim lim tendsto e loglog , σ σ2 median mode () [|] [|] () () [m , | m , =;] () det || T , . Z R Q N C P e i NaN true false π γ () () ||, share ) ( ] [ } { ) ( ] [ } { \ )(}{>< top right ;color:;background-color:; 0 decimalpoint decimalpoint. decimalpoint*0.1em0.15em0.2em0.15em 0 /\)(:=) ', "text/xml"); - MathJax.InputJax.MathML.ctopXSLT = new XSLTProcessor(); - MathJax.InputJax.MathML.ctopXSLT.importStylesheet(ctopStylesheet); + if (!MATHML.ParseXML) {MATHML.ParseXML = MATHML.createParser()} + var ctopStylesheet = MathJax.InputJax.MathML.ParseXML(' +i +i / / ei ei E 0x . (-1) λ. id domain codomain image unexpected domainofapplication restriction {   if     otherwise / ! / max min (+) mod (×) gcd xor ¬ . . , || ¯ arg lcm = > < | d dddd D, + + , div div() grad grad() curl 2 () || × = lim lim tendsto e loglog , σ σ2 median mode () [|] [|] () () [m , | m , =;] () det || T , . Z R Q N C P e i NaN true false π γ () () ||, share ) ( ] [ } { ) ( ] [ } { \ )(}{>< top right ;color:;background-color:; 0 decimalpoint decimalpoint. decimalpoint*0.1em0.15em0.2em0.15em 0 /\)(:=) ', "text/xml"); + if (window.XSLTProcessor) { + MATHML.ctopXSLT = new XSLTProcessor(); + MATHML.ctopXSLT.importStylesheet(ctopStylesheet); + } MathJax.Hub.Startup.signal.Post("MathML content-mathml Ready"); }); diff --git a/unpacked/jax/input/MathML/jax.js b/unpacked/jax/input/MathML/jax.js index 1270eefb7..b19180da8 100644 --- a/unpacked/jax/input/MathML/jax.js +++ b/unpacked/jax/input/MathML/jax.js @@ -1,3 +1,5 @@ +/* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ /************************************************************* * * MathJax/jax/input/MathML/jax.js @@ -28,7 +30,12 @@ MATHML.Parse = MathJax.Object.Subclass({ - Init: function (string) {this.Parse(string)}, + Init: function (string, cleanUpOnly) { + if (cleanUpOnly) { + return this.cleanUpMath.call(this,string); + } + this.Parse(string); + }, // // Parse the MathML and check for errors @@ -36,17 +43,8 @@ Parse: function (math) { var doc; if (typeof math !== "string") {doc = math.parentNode} else { - if (math.match(/^<[a-z]+:/i) && !math.match(/^<[^<>]* xmlns:/)) - {math = math.replace(/^<([a-z]+)(:math)/i,'<$1$2 xmlns:$1="http://www.w3.org/1998/Math/MathML"')} - // HTML5 removes xmlns: namespaces, so put them back for XML - var match = math.match(/^(])+)>)/i); - if (match && match[2].match(/ (?!xmlns=)[a-z]+=\"http:/i)) { - math = match[1].replace(/ (?!xmlns=)([a-z]+=(['"])http:.*?\2)/ig," xmlns:$1 $1") + - math.substr(match[0].length); - } - math = math.replace(/^\s*(?:\/\/)?\s*$/,"$2"); - math = math.replace(/&([a-z][a-z0-9]*);/ig,this.replaceEntity); - doc = MATHML.ParseXML(math); if (doc == null) {MATHML.Error("Error parsing MathML")} + doc = MATHML.ParseXML(this.cleanUpMath.call(this,math)); + if (doc == null) {MATHML.Error("Error parsing MathML")} } var err = doc.getElementsByTagName("parsererror")[0]; if (err) MATHML.Error("Error parsing MathML: "+err.textContent.replace(/This page.*?errors:|XML Parsing Error: |Below is a rendering of the page.*/g,"")); @@ -160,6 +158,22 @@ } }, + // + // Clean Up the source to prepare for XML parsing + // + cleanUpMath: function (math) { + if (math.match(/^<[a-z]+:/i) && !math.match(/^<[^<>]* xmlns:/)) { + math = math.replace(/^<([a-z]+)(:math)/i,'<$1$2 xmlns:$1="http://www.w3.org/1998/Math/MathML"') + } + // HTML5 removes xmlns: namespaces, so put them back for XML + var match = math.match(/^(])+)>)/i); + if (match && match[2].match(/ (?!xmlns=)[a-z]+=\"http:/i)) { + math = match[1].replace(/ (?!xmlns=)([a-z]+=(['"])http:.*?\2)/ig," xmlns:$1 $1") + math.substr(match[0].length); + } + math = math.replace(/^\s*(?:\/\/)?\s*$/,"$2"); + return math.replace(/&([a-z][a-z0-9]*);/ig,this.replaceEntity); + }, + // // Remove attribute whitespace //