Website/node_modules/MathJax/unpacked/extensions/MathML/mml3.js

129 lines
23 KiB
JavaScript
Raw Normal View History

/*************************************************************
*
* MathJax/extensions/MathML/mml3.js
*
* This file implements an XSLT transform to convert some MathML 3
* constructs to constructs MathJax can render. 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 correctly,
* but the transformed MathML can be obtained from the regular MathML menu.
*
* To load it, include
*
* MathML: {
* extensions: ["mml3.js"]
* }
*
* in your configuration.
*
* A portion of this file is taken from mml3mj.xsl which is
* Copyright (c) David Carlisle 2008-2015
* and is used by permission of David Carlisle, who has agreed to allow us
* to release it under the Apache2 license (see below). That portion is
* indicated via comments.
*
* The remainder falls under the copyright that follows.
* ---------------------------------------------------------------------
*
* Copyright (c) 2013-2015 The MathJax Consortium
*
* 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/mml3"] = {
version: "2.5.1"
};
MathJax.Hub.Register.StartupHook("MathML Jax Ready",function () {
var MATHML = MathJax.InputJax.MathML,
PARSE = MATHML.Parse.prototype;
MATHML.prefilterHooks.Add(function (data) {
if (!MATHML.mml3XSLT) return;
// Parse the <math> but use MATHML.Parse's preProcessMath to apply the normal preprocessing.
if (!MATHML.ParseXML) {MATHML.ParseXML = MATHML.createParser()}
var doc = MATHML.ParseXML(PARSE.preProcessMath(data.math));
// Now transform the <math> using the mml3 stylesheet.
var newdoc = MATHML.mml3XSLT.transformToDocument(doc);
if ((typeof newdoc) === "string") {
// Internet Explorer returns a string, so just use it.
data.math = newdoc;
} else if (window.XMLSerializer) {
// Serialize the <math> 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);
}
});
/*
* The following is taken from mml3mj.xsl
* (https://web-xslt.googlecode.com/svn/trunk/ctop/mml3mj.xsl)
* which is Copyright (c) David Carlisle 2008-2015.
* It is used by permission of David Carlisle, who has agreed to allow it to
* be released under the Apache License, Version 2.0.
*/
var mml3Stylesheet = '<x:stylesheet version="1.0" xmlns:x="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns="http://www.w3.org/1998/Math/MathML" xmlns:c="http://exslt.org/common" exclude-result-prefixes="m c"><ms:script language="JScript" implements-prefix="c" xmlns:ms="urn:schemas-microsoft-com:xslt"> this[\'node-set\'] = function (x) { return x; }</ms:script><x:output indent="yes" omit-xml-declaration="yes"/><x:template match="*"><x:copy><x:copy-of select="@*"/><x:apply-templates/></x:copy></x:template><x:template match="*[@dir=\'rtl\']" priority="10"><x:apply-templates mode="rtl" select="."/></x:template><x:template match="@*" mode="rtl"><x:copy-of select="."/></x:template><x:template match="*" mode="rtl"><x:copy><x:apply-templates select="@*" mode="rtl"/><x:for-each select="node()"><x:sort data-type="number" order="descending" select="position()"/><x:text></x:text><x:apply-templates mode="rtl" select="."/></x:for-each></x:copy></x:template><x:template match="@open" mode="rtl"><x:attribute name="close"><x:value-of select="."/></x:attribute></x:template><x:template match="@open[.=\'(\']" mode="rtl"><x:attribute name="close">)</x:attribute></x:template><x:template match="@open[.=\')\']" mode="rtl"><x:attribute name="close">(</x:attribute></x:template><x:template match="@open[.=\'[\']" mode="rtl"><x:attribute name="close">]</x:attribute></x:template><x:template match="@open[.=\']\']" mode="rtl"><x:attribute name="close">[</x:attribute></x:template><x:template match="@open[.=\'{\']" mode="rtl"><x:attribute name="close">}</x:attribute></x:template><x:template match="@open[.=\'}\']" mode="rtl"><x:attribute name="close">{</x:attribute></x:template><x:template match="@close" mode="rtl"><x:attribute name="open"><x:value-of select="."/></x:attribute></x:template><x:template match="@close[.=\'(\']" mode="rtl"><x:attribute name="open">)</x:attribute></x:template><x:template match="@close[.=\')\']" mode="rtl"><x:attribute name="open">(</x:attribute></x:template><x:template match="@close[.=\'[\']" mode="rtl"><x:attribute name="open">]</x:attribute></x:template><x:template match="@close[.=\']\']" mode="rtl"><x:attribute name="open">[</x:attribute></x:template><x:template match="@close[.=\'{\']" mode="rtl"><x:attribute name="open">}</x:attribute></x:template><x:template match="@close[.=\'}\']" mode="rtl"><x:attribute name="open">{</x:attribute></x:template><x:template match="m:mfrac[@bevelled=\'true\']" mode="rtl"><mrow><msub><mi></mi><x:apply-templates select="*[2]" mode="rtl"/></msub><mo>&#x5c;</mo><msup><mi></mi><x:apply-templates select="*[1]" mode="rtl"/></msup></mrow></x:template><x:template match="m:mfrac" mode="rtl"><x:copy><x:apply-templates mode="rtl" select="@*|*"/></x:copy></x:template><x:template match="m:mroot" mode="rtl"><msup><menclose notation="top right"><x:apply-templates mode="rtl" select="@*|*[1]"/></menclose><x:apply-templates mode="rtl" select="*[2]"/></msup></x:template><x:template match="m:msqrt" mode="rtl"><menclose notation="top right"><x:apply-templates mode="rtl" select="@*|*[1]"/></menclose></x:template><x:template match="m:mtable|m:munder|m:mover|m:munderover" mode="rtl" priority="2"><x:copy><x:apply-templates select="@*" mode="rtl"/><x:apply-templates mode="rtl"></x:apply-templates></x:copy></x:template><x:template match="m:msup" mode="rtl" priority="2"><mmultiscripts><x:apply-templates select="*[1]" mode="rtl"/><mprescripts/><none/><x:apply-templates select="*[2]" mode="rtl"/></mmultiscripts></x:template><x:template match="m:msub" mode="rtl" priority="2"><mmultiscripts><x:apply-templates select="*[1]" mode="rtl"/><mprescripts/><x:apply-templates select="*[2]" mode="rtl"/><none/></mmultiscripts></x:template><x:template match="m:msubsup" mode="rtl" priority="2"><mmultiscripts><x:apply-templates select="*[1]" mode="rtl"/><mprescripts/><x:apply-templates select="*[2]" mode="rtl"/><x:apply-templates select="*[3]" mode="rtl"/></mmultiscripts></x:template><x:template match="m:mmultiscripts" mode="rtl" priority="2"><mmultiscripts><x:apply-templates select="*[1]"
/*
* End of mml3mj.xsl material.
*/
var mml3;
if (window.XSLTProcessor) {
// standard method: just use an XSLTProcessor and parse the stylesheet
if (!MATHML.ParseXML) {MATHML.ParseXML = MATHML.createParser()}
MATHML.mml3XSLT = new XSLTProcessor();
MATHML.mml3XSLT.importStylesheet(MATHML.ParseXML(mml3Stylesheet));
} else if (MathJax.Hub.Browser.isMSIE) {
// nonstandard methods for Internet Explorer
if (MathJax.Hub.Browser.versionAtLeast("9.0") || (document.documentMode||0) >= 9) {
// For Internet Explorer >= 9, use createProcessor
mml3 = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
mml3.loadXML(mml3Stylesheet);
var xslt = new ActiveXObject("Msxml2.XSLTemplate");
xslt.stylesheet = mml3;
MATHML.mml3XSLT = {
mml3: xslt.createProcessor(),
transformToDocument: function(doc) {
this.mml3.input = doc;
this.mml3.transform();
return this.mml3.output;
}
}
} else {
// For Internet Explorer <= 8, use transformNode
mml3 = MATHML.createMSParser();
mml3.async = false;
mml3.loadXML(mml3Stylesheet);
MATHML.mml3XSLT = {
mml3: mml3,
transformToDocument: function(doc) {
return doc.documentElement.transformNode(this.mml3);
}
}
}
} else {
// No XSLT support. Do not change the <math> content.
MATHML.mml3XSLT = null;
}
MathJax.Hub.Startup.signal.Post("MathML mml3.js Ready");
});
MathJax.Ajax.loadComplete("[MathJax]/extensions/MathML/mml3.js");