toMathML-testsuite 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
  2. /* vim: set ts=2 et sw=2 tw=80: */
  3. /*************************************************************
  4. *
  5. * MathJax/extensions/toMathML.js
  6. *
  7. * Implements a toMathML() method for the mml Element Jax that returns
  8. * a MathML string from a given math expression.
  9. *
  10. * ---------------------------------------------------------------------
  11. *
  12. * Copyright (c) 2010-2018 The MathJax Consortium
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. MathJax.Hub.Register.LoadHook("[MathJax]/jax/element/mml/jax.js",function () {
  27. var VERSION = "2.7.5";
  28. var MML = MathJax.ElementJax.mml,
  29. SETTINGS = MathJax.Hub.config.menuSettings;
  30. MML.mbase.Augment({
  31. toMathML: function (space) {
  32. var inferred = (this.inferred && this.parent.inferRow);
  33. if (space == null) {space = ""}
  34. var tag = this.type, attr = this.toMathMLattributes();
  35. // if (tag === "mspace") {return space + "<"+tag+attr+" />"}
  36. if (tag === "mspace") {return space + "<"+tag+attr+"></"+tag+">"}
  37. var data = [], SPACE = (this.isToken ? "" : space+(inferred ? "" : " "));
  38. for (var i = 0, m = this.data.length; i < m; i++) {
  39. if (this.data[i]) {data.push(this.data[i].toMathML(SPACE))}
  40. // else if (!this.isToken && !this.isChars) {data.push(SPACE+"<mrow />")}
  41. else if (!this.isToken && !this.isChars) {data.push(SPACE+"<mrow></mrow>")}
  42. }
  43. if (this.isToken || this.isChars) {return space + "<"+tag+attr+">"+data.join("")+"</"+tag+">"}
  44. if (inferred) {return data.join("\n")}
  45. if (data.length === 0 || (data.length === 1 && data[0] === ""))
  46. // {return space + "<"+tag+attr+" />"}
  47. {return space + "<"+tag+attr+"></"+tag+">"}
  48. return space + "<"+tag+attr+">\n"+data.join("\n")+"\n"+ space +"</"+tag+">";
  49. },
  50. toMathMLattributes: function () {
  51. var defaults = (this.type === "mstyle" ? MML.math.prototype.defaults : this.defaults);
  52. var names = (this.attrNames||MML.copyAttributeNames),
  53. skip = MML.skipAttributes, copy = MML.copyAttributes;
  54. var attr = [];
  55. if (this.type === "math" && (!this.attr || !this.attr.xmlns))
  56. {attr.push('xmlns="http://www.w3.org/1998/Math/MathML"')}
  57. if (!this.attrNames) {
  58. for (var id in defaults) {if (!skip[id] && !copy[id] && defaults.hasOwnProperty(id)) {
  59. if (this[id] != null && this[id] !== defaults[id]) {
  60. if (this.Get(id,null,1) !== this[id])
  61. attr.push(id+'="'+this.toMathMLattribute(this[id])+'"');
  62. }
  63. }}
  64. }
  65. for (var i = 0, m = names.length; i < m; i++) {
  66. if (copy[names[i]] === 1 && !defaults.hasOwnProperty(names[i])) continue;
  67. value = (this.attr||{})[names[i]]; if (value == null) {value = this[names[i]]}
  68. if (value != null) {attr.push(names[i]+'="'+this.toMathMLquote(value)+'"')}
  69. }
  70. this.toMathMLclass(attr);
  71. if (attr.length) {return " "+attr.join(" ")} else {return ""}
  72. },
  73. toMathMLclass: function (attr) {
  74. var CLASS = []; if (this["class"]) {CLASS.push(this["class"])}
  75. if (this.isa(MML.TeXAtom) && SETTINGS.texHints) {
  76. var TEXCLASS = ["ORD","OP","BIN","REL","OPEN","CLOSE","PUNCT","INNER","VCENTER"][this.texClass];
  77. if (TEXCLASS) {
  78. CLASS.push("MJX-TeXAtom-"+TEXCLASS)
  79. if (TEXCLASS === "OP" && !this.movablelimits) CLASS.push("MJX-fixedlimits");
  80. }
  81. }
  82. if (this.mathvariant && this.toMathMLvariants[this.mathvariant])
  83. {CLASS.push("MJX"+this.mathvariant)}
  84. if (this.variantForm) {CLASS.push("MJX-variant")}
  85. if (CLASS.length) {attr.unshift('class="'+CLASS.join(" ")+'"')}
  86. },
  87. toMathMLattribute: function (value) {
  88. if (typeof(value) === "string" &&
  89. value.replace(/ /g,"").match(/^(([-+])?(\d+(\.\d*)?|\.\d+))mu$/)) {
  90. // FIXME: should take scriptlevel into account
  91. return (RegExp.$2||"")+((1/18)*RegExp.$3).toFixed(3).replace(/\.?0+$/,"")+"em";
  92. }
  93. else if (this.toMathMLvariants[value]) {return this.toMathMLvariants[value]}
  94. return this.toMathMLquote(value);
  95. },
  96. toMathMLvariants: {
  97. "-tex-caligraphic": MML.VARIANT.SCRIPT,
  98. "-tex-caligraphic-bold": MML.VARIANT.BOLDSCRIPT,
  99. "-tex-oldstyle": MML.VARIANT.NORMAL,
  100. "-tex-oldstyle-bold": MML.VARIANT.BOLD,
  101. "-tex-mathit": MML.VARIANT.ITALIC
  102. },
  103. toMathMLquote: function (string) {
  104. string = String(string).split("");
  105. for (var i = 0, m = string.length; i < m; i++) {
  106. var n = string[i].charCodeAt(0);
  107. if (n <= 0xD7FF || 0xE000 <= n) {
  108. // Code points U+0000 to U+D7FF and U+E000 to U+FFFF.
  109. // They are directly represented by n.
  110. if (n > 0x7E || (n < 0x20 && n !== 0x0A && n !== 0x0D && n !== 0x09)) {
  111. string[i] = "&#x"+n.toString(16).toUpperCase()+";";
  112. } else {
  113. var c =
  114. {'&':'&amp;', '<':'&lt;', '>':'&gt;', '"':'&quot;'}[string[i]];
  115. if (c) {string[i] = c}
  116. }
  117. } else if (i+1 < m) {
  118. // Code points U+10000 to U+10FFFF.
  119. // n is the lead surrogate, let's read the trail surrogate.
  120. var trailSurrogate = string[i+1].charCodeAt(0);
  121. var codePoint = (((n-0xD800)<<10)+(trailSurrogate-0xDC00)+0x10000);
  122. string[i] = "&#x"+codePoint.toString(16).toUpperCase()+";";
  123. string[i+1] = "";
  124. i++;
  125. } else {
  126. // n is a lead surrogate without corresponding trail surrogate:
  127. // remove that character.
  128. string[i] = "";
  129. }
  130. }
  131. return string.join("");
  132. }
  133. });
  134. //
  135. // Override math.toMathML in order to add semantics tag
  136. // for the input format, if the user requests that in the
  137. // Show As menu.
  138. //
  139. MML.math.Augment({
  140. toMathML: function (space,jax) {
  141. var annotation;
  142. // if (space == null) {space = ""}
  143. space = " ";
  144. if (jax && jax.originalText && SETTINGS.semantics)
  145. {annotation = MathJax.InputJax[jax.inputJax].annotationEncoding}
  146. var nested = (this.data[0] && this.data[0].data.length > 1);
  147. var tag = this.type, attr = this.toMathMLattributes();
  148. var data = [], SPACE = space + (annotation ? " " + (nested ? " " : "") : "") + " ";
  149. for (var i = 0, m = this.data.length; i < m; i++) {
  150. if (this.data[i]) {data.push(this.data[i].toMathML(SPACE))}
  151. // else {data.push(SPACE+"<mrow />")}
  152. else {data.push(SPACE+"<mrow></mrow>")}
  153. }
  154. if (data.length === 0 || (data.length === 1 && data[0] === "")) {
  155. // if (!annotation) {return "<"+tag+attr+" />"}
  156. // data.push(SPACE+"<mrow />");
  157. if (!annotation) {return "<"+tag+attr+"></"+tag+">"}
  158. data.push(SPACE+"<mrow></mrow>");
  159. }
  160. if (annotation) {
  161. if (nested) {data.unshift(space+" <mrow>"); data.push(space+" </mrow>")}
  162. data.unshift(space+" <semantics>");
  163. var xmlEscapedTex = jax.originalText.replace(/[&<>]/g, function(item) {
  164. return { '>': '&gt;', '<': '&lt;','&': '&amp;' }[item]
  165. });
  166. data.push(space+' <annotation encoding="'+annotation+'">'+xmlEscapedTex+"</annotation>");
  167. data.push(space+" </semantics>");
  168. }
  169. return "//\n"+space+"<"+tag+attr+">\n"+data.join("\n")+"\n"+space+"</"+tag+">";
  170. }
  171. });
  172. MML.msubsup.Augment({
  173. toMathML: function (space) {
  174. var tag = this.type;
  175. if (this.data[this.sup] == null) {tag = "msub"}
  176. if (this.data[this.sub] == null) {tag = "msup"}
  177. var attr = this.toMathMLattributes();
  178. delete this.data[0].inferred;
  179. var data = [];
  180. for (var i = 0, m = this.data.length; i < m; i++)
  181. {if (this.data[i]) {data.push(this.data[i].toMathML(space+" "))}}
  182. return space + "<"+tag+attr+">\n" + data.join("\n") + "\n" + space + "</"+tag+">";
  183. }
  184. });
  185. MML.munderover.Augment({
  186. toMathML: function (space) {
  187. var tag = this.type;
  188. var base = this.data[this.base];
  189. if (base && base.isa(MML.TeXAtom) && base.movablelimits && !base.Get("displaystyle")) {
  190. type = "msubsup";
  191. if (this.data[this.under] == null) {tag = "msup"}
  192. if (this.data[this.over] == null) {tag = "msub"}
  193. } else {
  194. if (this.data[this.under] == null) {tag = "mover"}
  195. if (this.data[this.over] == null) {tag = "munder"}
  196. }
  197. var attr = this.toMathMLattributes();
  198. delete this.data[0].inferred;
  199. var data = [];
  200. for (var i = 0, m = this.data.length; i < m; i++)
  201. {if (this.data[i]) {data.push(this.data[i].toMathML(space+" "))}}
  202. return space + "<"+tag+attr+">\n" + data.join("\n") + "\n" + space + "</"+tag+">";
  203. }
  204. });
  205. MML.TeXAtom.Augment({
  206. toMathML: function (space) {
  207. // FIXME: Handle spacing using mpadded?
  208. var attr = this.toMathMLattributes();
  209. if (!attr && this.data[0].data.length === 1) {return space.substr(2) + this.data[0].toMathML(space)}
  210. var content = this.data[0].toMathML(space+" ");
  211. if (content.replace(/ /g,"") === "") content = ""; else content = "\n"+content+"\n"+space;
  212. return space+"<mrow"+attr+">"+content+"</mrow>";
  213. // return space+"<mrow"+attr+">\n" + this.data[0].toMathML(space+" ")+"\n"+space+"</mrow>";
  214. }
  215. });
  216. MML.chars.Augment({
  217. toMathML: function (space) {return (space||"") + this.toMathMLquote(this.toString())}
  218. });
  219. MML.entity.Augment({
  220. // toMathML: function (space) {return (space||"") + "&"+this.data[0]+";<!-- "+this.toString()+" -->"}
  221. toMathML: function (space) {return (space||"") + "&"+this.data[0]+";"}
  222. });
  223. MML.xml.Augment({
  224. toMathML: function (space) {return (space||"") + this.toString()}
  225. });
  226. MathJax.Hub.Register.StartupHook("TeX mathchoice Ready",function () {
  227. MML.TeXmathchoice.Augment({
  228. toMathML: function (space) {return this.Core().toMathML(space)}
  229. });
  230. });
  231. MathJax.Hub.Startup.signal.Post("toMathML Ready");
  232. });
  233. MathJax.Ajax.loadComplete("[MathJax]/extensions/toMathML.js");