noErrors.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/TeX/noErrors.js
  6. *
  7. * Prevents the TeX error messages from being displayed and shows the
  8. * original TeX code instead. You can configure whether the dollar signs
  9. * are shown or not for in-line math, and whether to put all the TeX on
  10. * one line or use multiple-lines.
  11. *
  12. * To configure this extension, use
  13. *
  14. * MathJax.Hub.Config({
  15. * TeX: {
  16. * noErrors: {
  17. * inlineDelimiters: ["",""], // or ["$","$"] or ["\\(","\\)"]
  18. * multiLine: true, // false for TeX on all one line
  19. * style: {
  20. * "font-size": "90%",
  21. * "text-align": "left",
  22. * "color": "black",
  23. * "padding": "1px 3px",
  24. * "border": "1px solid"
  25. * // add any additional CSS styles that you want
  26. * // (be sure there is no extra comma at the end of the last item)
  27. * }
  28. * }
  29. * }
  30. * });
  31. *
  32. * Display-style math is always shown in multi-line format, and without
  33. * delimiters, as it will already be set off in its own centered
  34. * paragraph, like standard display mathematics.
  35. *
  36. * The default settings place the invalid TeX in a multi-line box with a
  37. * black border. If you want it to look as though the TeX is just part of
  38. * the paragraph, use
  39. *
  40. * MathJax.Hub.Config({
  41. * TeX: {
  42. * noErrors: {
  43. * inlineDelimiters: ["$","$"], // or ["",""] or ["\\(","\\)"]
  44. * multiLine: false,
  45. * style: {
  46. * "font-size": "normal",
  47. * "border": ""
  48. * }
  49. * }
  50. * }
  51. * });
  52. *
  53. * You may also wish to set the font family, as the default is "serif"
  54. *
  55. * ---------------------------------------------------------------------
  56. *
  57. * Copyright (c) 2009-2013 The MathJax Consortium
  58. *
  59. * Licensed under the Apache License, Version 2.0 (the "License");
  60. * you may not use this file except in compliance with the License.
  61. * You may obtain a copy of the License at
  62. *
  63. * http://www.apache.org/licenses/LICENSE-2.0
  64. *
  65. * Unless required by applicable law or agreed to in writing, software
  66. * distributed under the License is distributed on an "AS IS" BASIS,
  67. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  68. * See the License for the specific language governing permissions and
  69. * limitations under the License.
  70. */
  71. (function (HUB,HTML) {
  72. var VERSION = "2.2";
  73. var CONFIG = HUB.CombineConfig("TeX.noErrors",{
  74. disabled: false, // set to true to return to original error messages
  75. multiLine: true,
  76. inlineDelimiters: ["",""], // or use ["$","$"] or ["\\(","\\)"]
  77. style: {
  78. "font-size": "90%",
  79. "text-align": "left",
  80. "color": "black",
  81. "padding": "1px 3px",
  82. "border": "1px solid"
  83. }
  84. });
  85. var NBSP = "\u00A0";
  86. //
  87. // The configuration defaults, augmented by the user settings
  88. //
  89. MathJax.Extension["TeX/noErrors"] = {
  90. version: VERSION,
  91. config: CONFIG
  92. };
  93. HUB.Register.StartupHook("TeX Jax Ready",function () {
  94. var FORMAT = MathJax.InputJax.TeX.formatError;
  95. MathJax.InputJax.TeX.Augment({
  96. //
  97. // Make error messages be the original TeX code
  98. // Mark them as errors and multi-line or not, and for
  99. // multi-line TeX, make spaces non-breakable (to get formatting right)
  100. //
  101. formatError: function (err,math,displaystyle,script) {
  102. if (CONFIG.disabled) {return FORMAT.apply(this,arguments)}
  103. var message = err.message.replace(/\n.*/,"");
  104. HUB.signal.Post(["TeX Jax - parse error",message,math,displaystyle,script]);
  105. var delim = CONFIG.inlineDelimiters;
  106. var multiLine = (displaystyle || CONFIG.multiLine);
  107. if (!displaystyle) {math = delim[0] + math + delim[1]}
  108. if (multiLine) {math = math.replace(/ /g,NBSP)} else {math = math.replace(/\n/g," ")}
  109. return MathJax.ElementJax.mml.merror(math).With({isError:true, multiLine: multiLine});
  110. }
  111. });
  112. });
  113. /*******************************************************************
  114. *
  115. * Fix HTML-CSS output
  116. */
  117. HUB.Register.StartupHook("HTML-CSS Jax Config",function () {
  118. HUB.Config({
  119. "HTML-CSS": {
  120. styles: {
  121. ".MathJax .noError": HUB.Insert({
  122. "vertical-align": (HUB.Browser.isMSIE && CONFIG.multiLine ? "-2px" : "")
  123. },CONFIG.style)
  124. }
  125. }
  126. });
  127. });
  128. HUB.Register.StartupHook("HTML-CSS Jax Ready",function () {
  129. var MML = MathJax.ElementJax.mml;
  130. var HTMLCSS = MathJax.OutputJax["HTML-CSS"];
  131. var MATH = MML.math.prototype.toHTML,
  132. MERROR = MML.merror.prototype.toHTML;
  133. //
  134. // Override math toHTML routine so that error messages
  135. // don't have the clipping and other unneeded overhead
  136. //
  137. MML.math.Augment({
  138. toHTML: function (span,node) {
  139. var data = this.data[0];
  140. if (data && data.data[0] && data.data[0].isError) {
  141. span.style.fontSize = "";
  142. span = this.HTMLcreateSpan(span);
  143. span.bbox = data.data[0].toHTML(span).bbox;
  144. } else {
  145. span = MATH.call(this,span,node);
  146. }
  147. return span;
  148. }
  149. });
  150. //
  151. // Override merror toHTML routine so that it puts out the
  152. // TeX code in an inline-block with line breaks as in the original
  153. //
  154. MML.merror.Augment({
  155. toHTML: function (span) {
  156. if (!this.isError) {return MERROR.call(this,span)}
  157. span = this.HTMLcreateSpan(span); span.className = "noError"
  158. if (this.multiLine) {span.style.display = "inline-block"}
  159. var text = this.data[0].data[0].data.join("").split(/\n/);
  160. for (var i = 0, m = text.length; i < m; i++) {
  161. HTMLCSS.addText(span,text[i]);
  162. if (i !== m-1) {HTMLCSS.addElement(span,"br",{isMathJax:true})}
  163. }
  164. var HD = HTMLCSS.getHD(span.parentNode), W = HTMLCSS.getW(span.parentNode);
  165. if (m > 1) {
  166. var H = (HD.h + HD.d)/2, x = HTMLCSS.TeX.x_height/2;
  167. span.parentNode.style.verticalAlign = HTMLCSS.Em(HD.d+(x-H));
  168. HD.h = x + H; HD.d = H - x;
  169. }
  170. span.bbox = {h: HD.h, d: HD.d, w: W, lw: 0, rw: W};
  171. return span;
  172. }
  173. });
  174. });
  175. /*******************************************************************
  176. *
  177. * Fix SVG output
  178. */
  179. HUB.Register.StartupHook("SVG Jax Config",function () {
  180. HUB.Config({
  181. "SVG": {
  182. styles: {
  183. ".MathJax_SVG .noError": HUB.Insert({
  184. "vertical-align": (HUB.Browser.isMSIE && CONFIG.multiLine ? "-2px" : "")
  185. },CONFIG.style)
  186. }
  187. }
  188. });
  189. });
  190. HUB.Register.StartupHook("SVG Jax Ready",function () {
  191. var MML = MathJax.ElementJax.mml;
  192. var MATH = MML.math.prototype.toSVG,
  193. MERROR = MML.merror.prototype.toSVG;
  194. //
  195. // Override math toSVG routine so that error messages
  196. // don't have the clipping and other unneeded overhead
  197. //
  198. MML.math.Augment({
  199. toSVG: function (span,node) {
  200. var data = this.data[0];
  201. if (data && data.data[0] && data.data[0].isError)
  202. {span = data.data[0].toSVG(span)} else {span = MATH.call(this,span,node)}
  203. return span;
  204. }
  205. });
  206. //
  207. // Override merror toSVG routine so that it puts out the
  208. // TeX code in an inline-block with line breaks as in the original
  209. //
  210. MML.merror.Augment({
  211. toSVG: function (span) {
  212. if (!this.isError || this.Parent().type !== "math") {return MERROR.call(this,span)}
  213. span = HTML.addElement(span,"span",{className: "noError", isMathJax:true});
  214. if (this.multiLine) {span.style.display = "inline-block"}
  215. var text = this.data[0].data[0].data.join("").split(/\n/);
  216. for (var i = 0, m = text.length; i < m; i++) {
  217. HTML.addText(span,text[i]);
  218. if (i !== m-1) {HTML.addElement(span,"br",{isMathJax:true})}
  219. }
  220. if (m > 1) {
  221. var H = span.offsetHeight/2;
  222. span.style.verticalAlign = (-H+(H/m))+"px";
  223. }
  224. return span;
  225. }
  226. });
  227. });
  228. /*******************************************************************
  229. *
  230. * Fix NativeMML output
  231. */
  232. HUB.Register.StartupHook("NativeMML Jax Ready",function () {
  233. var MML = MathJax.ElementJax.mml;
  234. var CONFIG = MathJax.Extension["TeX/noErrors"].config;
  235. var MATH = MML.math.prototype.toNativeMML,
  236. MERROR = MML.merror.prototype.toNativeMML;
  237. //
  238. // Override math toNativeMML routine so that error messages
  239. // don't get placed inside math tags.
  240. //
  241. MML.math.Augment({
  242. toNativeMML: function (span) {
  243. var data = this.data[0];
  244. if (data && data.data[0] && data.data[0].isError)
  245. {span = data.data[0].toNativeMML(span)} else {span = MATH.call(this,span)}
  246. return span;
  247. }
  248. });
  249. //
  250. // Override merror toNativeMML routine so that it puts out the
  251. // TeX code in an inline-block with line breaks as in the original
  252. //
  253. MML.merror.Augment({
  254. toNativeMML: function (span) {
  255. if (!this.isError) {return MERROR.call(this,span)}
  256. span = span.appendChild(document.createElement("span"));
  257. var text = this.data[0].data[0].data.join("").split(/\n/);
  258. for (var i = 0, m = text.length; i < m; i++) {
  259. span.appendChild(document.createTextNode(text[i]));
  260. if (i !== m-1) {span.appendChild(document.createElement("br"))}
  261. }
  262. if (this.multiLine) {
  263. span.style.display = "inline-block";
  264. if (m > 1) {span.style.verticalAlign = "middle"}
  265. }
  266. for (var id in CONFIG.style) {if (CONFIG.style.hasOwnProperty(id)) {
  267. var ID = id.replace(/-./g,function (c) {return c.charAt(1).toUpperCase()});
  268. span.style[ID] = CONFIG.style[id];
  269. }}
  270. return span;
  271. }
  272. });
  273. });
  274. /*******************************************************************/
  275. HUB.Startup.signal.Post("TeX noErrors Ready");
  276. })(MathJax.Hub,MathJax.HTML);
  277. MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/noErrors.js");