diff --git a/unpacked/extensions/MathML/content-mathml.js b/unpacked/extensions/MathML/content-mathml.js new file mode 100644 index 000000000..76a973921 --- /dev/null +++ b/unpacked/extensions/MathML/content-mathml.js @@ -0,0 +1,112 @@ +/* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ + +/************************************************************* + * + * MathJax/extensions/MathML/content-mathml.js + * + * This file implements an XSLT transform to convert Content-MathML to + * Presentation MathML for processing by MathJax. The transform is + * performed in a pre-filter for the MathML input jax, so that the + * Show Math As menu will still show the Original MathML as Content MathML, + * but the Presentation MathML can be obtained from the main MathML menu. + * + * To load it, include + * + * MathML: { + * extensions: ["content-mathml.js"] + * } + * + * in your configuration. + * + * --------------------------------------------------------------------- + * + * Copyright (c) 2013 Design Science, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +MathJax.Extension["MathML/content-mathml"] = { + version: "2.2" +}; + +MathJax.Hub.Register.StartupHook("MathML Jax Ready",function () { + + var MATHML = MathJax.InputJax.MathML, + PARSE = MATHML.Parse.prototype; + + MATHML.prefilterHooks.Add(function (data) { + if (!MATHML.ctopXSLT) return; + + // Parse the but use MATHML.Parse to apply the normal preprocessing. + if (!MATHML.ParseXML) {MATHML.ParseXML = MATHML.createParser()} + var doc = MATHML.ParseXML(PARSE.preProcessMath(data.math)); + + // Now transform the using the ctop stylesheet. + var newdoc = MATHML.ctopXSLT.transformToDocument(doc); + + if ((typeof newdoc) === "string") { + // Internet Explorer returns a string, so just use it. + data.math = newdoc; + } else if (window.XMLSerializer) { + // Serialize the again. We could directly provide the DOM content + // but other prefilterHooks may assume data.math is still a string. + var serializer = new XMLSerializer(); + data.math = serializer.serializeToString(newdoc.documentElement, doc); + } + }); + + var ctopStylesheet = ' +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 /\)(:=) '; + if (window.XSLTProcessor) { + // standard method: just use an XSLTProcessor and parse the stylesheet + if (!MATHML.ParseXML) {MATHML.ParseXML = MATHML.createParser()} + MATHML.ctopXSLT = new XSLTProcessor(); + MATHML.ctopXSLT.importStylesheet(MATHML.ParseXML(ctopStylesheet)); + } else if (MathJax.Hub.Browser.isMSIE) { + // nonstandard methods for Internet Explorer + if (MathJax.Hub.Browser.versionAtLeast("9.0")) { + // For Internet Explorer >= 9, use createProcessor + var ctop = new ActiveXObject("Msxml2.FreeThreadedDOMDocument"); + ctop.loadXML(ctopStylesheet); + var xslt = new ActiveXObject("Msxml2.XSLTemplate"); + xslt.stylesheet = ctop; + MATHML.ctopXSLT = { + ctop: xslt.createProcessor(), + transformToDocument: function(doc) { + this.ctop.input = doc; + this.ctop.transform(); + return this.ctop.output; + } + } + } else { + // For Internet Explorer <= 8, use transformNode + var ctop = MATHML.createMSParser(); + ctop.async = false; + ctop.loadXML(ctopStylesheet); + MATHML.ctopXSLT = { + ctop: ctop, + transformToDocument: function(doc) { + return doc.documentElement.transformNode(this.ctop); + } + } + } + } else { + // No XSLT support. Do not change the content. + MATHML.ctopXSLT = null; + } + + MathJax.Hub.Startup.signal.Post("MathML content-mathml Ready"); +}); + +MathJax.Ajax.loadComplete("[MathJax]/extensions/MathML/content-mathml.js"); diff --git a/unpacked/jax/input/MathML/jax.js b/unpacked/jax/input/MathML/jax.js index 1270eefb7..b0a0c908a 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 @@ -36,17 +38,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.preProcessMath.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 +153,26 @@ } }, + // + // Clean Up the source to prepare for XML parsing + // + preProcessMath: 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); + } + if (math.match(/^]* xmlns=/)) { + // append the MathML namespace + math = math.replace(/^<(math)/i,'\s*$/,"$2"); + return math.replace(/&([a-z][a-z0-9]*);/ig,this.replaceEntity); + }, + // // Remove attribute whitespace // @@ -239,6 +252,18 @@ return this.div; }, parseError: function (string) {return null}, + createMSParser: function() { + var parser = null; + var xml = ["MSXML2.DOMDocument.6.0","MSXML2.DOMDocument.5.0", + "MSXML2.DOMDocument.4.0","MSXML2.DOMDocument.3.0", + "MSXML2.DOMDocument.2.0","Microsoft.XMLDOM"]; + for (var i = 0, m = xml.length; i < m && !parser; i++) { + try { + parser = new ActiveXObject(xml[i]) + } catch (err) {} + } + return parser; + }, // // Create the parser using a DOMParser, or other fallback method // @@ -247,10 +272,7 @@ this.parser = new DOMParser(); return(this.parseDOM); } else if (window.ActiveXObject) { - var xml = ["MSXML2.DOMDocument.6.0","MSXML2.DOMDocument.5.0","MSXML2.DOMDocument.4.0", - "MSXML2.DOMDocument.3.0","MSXML2.DOMDocument.2.0","Microsoft.XMLDOM"]; - for (var i = 0, m = xml.length; i < m && !this.parser; i++) - {try {this.parser = new ActiveXObject(xml[i])} catch (err) {}} + this.parser = this.createMSParser(); if (!this.parser) { alert("MathJax can't create an XML parser for MathML. Check that\n"+ "the 'Script ActiveX controls marked safe for scripting' security\n"+