noErrors.js (13483B)
1 /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 4 /************************************************************* 5 * 6 * MathJax/extensions/TeX/noErrors.js 7 * 8 * Prevents the TeX error messages from being displayed and shows the 9 * original TeX code instead. You can configure whether the dollar signs 10 * are shown or not for in-line math, and whether to put all the TeX on 11 * one line or use multiple-lines. 12 * 13 * To configure this extension, use 14 * 15 * MathJax.Hub.Config({ 16 * TeX: { 17 * noErrors: { 18 * inlineDelimiters: ["",""], // or ["$","$"] or ["\\(","\\)"] 19 * multiLine: true, // false for TeX on all one line 20 * style: { 21 * "font-size": "90%", 22 * "text-align": "left", 23 * "color": "black", 24 * "padding": "1px 3px", 25 * "border": "1px solid" 26 * // add any additional CSS styles that you want 27 * // (be sure there is no extra comma at the end of the last item) 28 * } 29 * } 30 * } 31 * }); 32 * 33 * Display-style math is always shown in multi-line format, and without 34 * delimiters, as it will already be set off in its own centered 35 * paragraph, like standard display mathematics. 36 * 37 * The default settings place the invalid TeX in a multi-line box with a 38 * black border. If you want it to look as though the TeX is just part of 39 * the paragraph, use 40 * 41 * MathJax.Hub.Config({ 42 * TeX: { 43 * noErrors: { 44 * inlineDelimiters: ["$","$"], // or ["",""] or ["\\(","\\)"] 45 * multiLine: false, 46 * style: { 47 * "font-size": "normal", 48 * "border": "" 49 * } 50 * } 51 * } 52 * }); 53 * 54 * You may also wish to set the font family, as the default is "serif" 55 * 56 * --------------------------------------------------------------------- 57 * 58 * Copyright (c) 2009-2015 The MathJax Consortium 59 * 60 * Licensed under the Apache License, Version 2.0 (the "License"); 61 * you may not use this file except in compliance with the License. 62 * You may obtain a copy of the License at 63 * 64 * http://www.apache.org/licenses/LICENSE-2.0 65 * 66 * Unless required by applicable law or agreed to in writing, software 67 * distributed under the License is distributed on an "AS IS" BASIS, 68 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 69 * See the License for the specific language governing permissions and 70 * limitations under the License. 71 */ 72 73 (function (HUB,HTML) { 74 var VERSION = "2.6.0"; 75 76 var CONFIG = HUB.CombineConfig("TeX.noErrors",{ 77 disabled: false, // set to true to return to original error messages 78 multiLine: true, 79 inlineDelimiters: ["",""], // or use ["$","$"] or ["\\(","\\)"] 80 style: { 81 "font-size": "90%", 82 "text-align": "left", 83 "color": "black", 84 "padding": "1px 3px", 85 "border": "1px solid" 86 } 87 }); 88 89 var NBSP = "\u00A0"; 90 91 // 92 // The configuration defaults, augmented by the user settings 93 // 94 MathJax.Extension["TeX/noErrors"] = { 95 version: VERSION, 96 config: CONFIG 97 }; 98 99 HUB.Register.StartupHook("TeX Jax Ready",function () { 100 var FORMAT = MathJax.InputJax.TeX.formatError; 101 102 MathJax.InputJax.TeX.Augment({ 103 // 104 // Make error messages be the original TeX code 105 // Mark them as errors and multi-line or not, and for 106 // multi-line TeX, make spaces non-breakable (to get formatting right) 107 // 108 formatError: function (err,math,displaystyle,script) { 109 if (CONFIG.disabled) {return FORMAT.apply(this,arguments)} 110 var message = err.message.replace(/\n.*/,""); 111 HUB.signal.Post(["TeX Jax - parse error",message,math,displaystyle,script]); 112 var delim = CONFIG.inlineDelimiters; 113 var multiLine = (displaystyle || CONFIG.multiLine); 114 if (!displaystyle) {math = delim[0] + math + delim[1]} 115 if (multiLine) {math = math.replace(/ /g,NBSP)} else {math = math.replace(/\n/g," ")} 116 return MathJax.ElementJax.mml.merror(math).With({isError:true, multiLine: multiLine}); 117 } 118 }); 119 }); 120 121 /******************************************************************* 122 * 123 * Fix HTML-CSS output 124 */ 125 126 HUB.Register.StartupHook("HTML-CSS Jax Config",function () { 127 HUB.Config({ 128 "HTML-CSS": { 129 styles: { 130 ".MathJax .noError": HUB.Insert({ 131 "vertical-align": (HUB.Browser.isMSIE && CONFIG.multiLine ? "-2px" : "") 132 },CONFIG.style) 133 } 134 } 135 }); 136 }); 137 138 HUB.Register.StartupHook("HTML-CSS Jax Ready",function () { 139 var MML = MathJax.ElementJax.mml; 140 var HTMLCSS = MathJax.OutputJax["HTML-CSS"]; 141 142 var MATH = MML.math.prototype.toHTML, 143 MERROR = MML.merror.prototype.toHTML; 144 145 // 146 // Override math toHTML routine so that error messages 147 // don't have the clipping and other unneeded overhead 148 // 149 MML.math.Augment({ 150 toHTML: function (span,node) { 151 var data = this.data[0]; 152 if (data && data.data[0] && data.data[0].isError) { 153 span.style.fontSize = ""; 154 span = this.HTMLcreateSpan(span); 155 span.bbox = data.data[0].toHTML(span).bbox; 156 } else { 157 span = MATH.apply(this,arguments); 158 } 159 return span; 160 } 161 }); 162 163 // 164 // Override merror toHTML routine so that it puts out the 165 // TeX code in an inline-block with line breaks as in the original 166 // 167 MML.merror.Augment({ 168 toHTML: function (span) { 169 if (!this.isError) {return MERROR.apply(this,arguments)} 170 span = this.HTMLcreateSpan(span); span.className = "noError" 171 if (this.multiLine) {span.style.display = "inline-block"} 172 var text = this.data[0].data[0].data.join("").split(/\n/); 173 for (var i = 0, m = text.length; i < m; i++) { 174 HTMLCSS.addText(span,text[i]); 175 if (i !== m-1) {HTMLCSS.addElement(span,"br",{isMathJax:true})} 176 } 177 var HD = HTMLCSS.getHD(span.parentNode), W = HTMLCSS.getW(span.parentNode); 178 if (m > 1) { 179 var H = (HD.h + HD.d)/2, x = HTMLCSS.TeX.x_height/2; 180 span.parentNode.style.verticalAlign = HTMLCSS.Em(HD.d+(x-H)); 181 HD.h = x + H; HD.d = H - x; 182 } 183 span.bbox = {h: HD.h, d: HD.d, w: W, lw: 0, rw: W}; 184 return span; 185 } 186 }); 187 188 }); 189 190 /******************************************************************* 191 * 192 * Fix SVG output 193 */ 194 195 HUB.Register.StartupHook("SVG Jax Config",function () { 196 HUB.Config({ 197 "SVG": { 198 styles: { 199 ".MathJax_SVG .noError": HUB.Insert({ 200 "vertical-align": (HUB.Browser.isMSIE && CONFIG.multiLine ? "-2px" : "") 201 },CONFIG.style) 202 } 203 } 204 }); 205 }); 206 207 HUB.Register.StartupHook("SVG Jax Ready",function () { 208 var MML = MathJax.ElementJax.mml; 209 210 var MATH = MML.math.prototype.toSVG, 211 MERROR = MML.merror.prototype.toSVG; 212 213 // 214 // Override math toSVG routine so that error messages 215 // don't have the clipping and other unneeded overhead 216 // 217 MML.math.Augment({ 218 toSVG: function (span,node) { 219 var data = this.data[0]; 220 if (data && data.data[0] && data.data[0].isError) 221 {span = data.data[0].toSVG(span)} else {span = MATH.apply(this,arguments)} 222 return span; 223 } 224 }); 225 226 // 227 // Override merror toSVG routine so that it puts out the 228 // TeX code in an inline-block with line breaks as in the original 229 // 230 MML.merror.Augment({ 231 toSVG: function (span) { 232 if (!this.isError || this.Parent().type !== "math") {return MERROR.apply(this,arguments)} 233 span = HTML.addElement(span,"span",{className: "noError", isMathJax:true}); 234 if (this.multiLine) {span.style.display = "inline-block"} 235 var text = this.data[0].data[0].data.join("").split(/\n/); 236 for (var i = 0, m = text.length; i < m; i++) { 237 HTML.addText(span,text[i]); 238 if (i !== m-1) {HTML.addElement(span,"br",{isMathJax:true})} 239 } 240 if (m > 1) { 241 var H = span.offsetHeight/2; 242 span.style.verticalAlign = (-H+(H/m))+"px"; 243 } 244 return span; 245 } 246 }); 247 248 }); 249 250 /******************************************************************* 251 * 252 * Fix NativeMML output 253 */ 254 255 HUB.Register.StartupHook("NativeMML Jax Ready",function () { 256 var MML = MathJax.ElementJax.mml; 257 var CONFIG = MathJax.Extension["TeX/noErrors"].config; 258 259 var MATH = MML.math.prototype.toNativeMML, 260 MERROR = MML.merror.prototype.toNativeMML; 261 262 // 263 // Override math toNativeMML routine so that error messages 264 // don't get placed inside math tags. 265 // 266 MML.math.Augment({ 267 toNativeMML: function (span) { 268 var data = this.data[0]; 269 if (data && data.data[0] && data.data[0].isError) 270 {span = data.data[0].toNativeMML(span)} else {span = MATH.apply(this,arguments)} 271 return span; 272 } 273 }); 274 275 // 276 // Override merror toNativeMML routine so that it puts out the 277 // TeX code in an inline-block with line breaks as in the original 278 // 279 MML.merror.Augment({ 280 toNativeMML: function (span) { 281 if (!this.isError) {return MERROR.apply(this,arguments)} 282 span = span.appendChild(document.createElement("span")); 283 var text = this.data[0].data[0].data.join("").split(/\n/); 284 for (var i = 0, m = text.length; i < m; i++) { 285 span.appendChild(document.createTextNode(text[i])); 286 if (i !== m-1) {span.appendChild(document.createElement("br"))} 287 } 288 if (this.multiLine) { 289 span.style.display = "inline-block"; 290 if (m > 1) {span.style.verticalAlign = "middle"} 291 } 292 for (var id in CONFIG.style) {if (CONFIG.style.hasOwnProperty(id)) { 293 var ID = id.replace(/-./g,function (c) {return c.charAt(1).toUpperCase()}); 294 span.style[ID] = CONFIG.style[id]; 295 }} 296 return span; 297 } 298 }); 299 300 }); 301 302 /******************************************************************* 303 * 304 * Fix PreviewHTML output 305 */ 306 307 HUB.Register.StartupHook("PreviewHTML Jax Config",function () { 308 HUB.Config({ 309 PreviewHTML: { 310 styles: { 311 ".MathJax_PHTML .noError": HUB.Insert({ 312 "vertical-align": (HUB.Browser.isMSIE && CONFIG.multiLine ? "-2px" : "") 313 },CONFIG.style) 314 } 315 } 316 }); 317 }); 318 319 HUB.Register.StartupHook("PreviewHTML Jax Ready",function () { 320 var MML = MathJax.ElementJax.mml; 321 var HTML = MathJax.HTML; 322 323 var MERROR = MML.merror.prototype.toPreviewHTML; 324 325 // 326 // Override merror toPreviewHTML routine so that it puts out the 327 // TeX code in an inline-block with line breaks as in the original 328 // 329 MML.merror.Augment({ 330 toPreviewHTML: function (span) { 331 if (!this.isError) return MERROR.apply(this,arguments); 332 span = this.PHTMLcreateSpan(span); span.className = "noError" 333 if (this.multiLine) span.style.display = "inline-block"; 334 var text = this.data[0].data[0].data.join("").split(/\n/); 335 for (var i = 0, m = text.length; i < m; i++) { 336 HTML.addText(span,text[i]); 337 if (i !== m-1) {HTML.addElement(span,"br",{isMathJax:true})} 338 } 339 return span; 340 } 341 }); 342 343 }); 344 345 /******************************************************************* 346 * 347 * Fix CommonHTML output 348 */ 349 350 HUB.Register.StartupHook("CommonHTML Jax Config",function () { 351 HUB.Config({ 352 CommonHTML: { 353 styles: { 354 ".mjx-chtml .mjx-noError": HUB.Insert({ 355 "line-height": 1.2, 356 "vertical-align": (HUB.Browser.isMSIE && CONFIG.multiLine ? "-2px" : "") 357 },CONFIG.style) 358 } 359 } 360 }); 361 }); 362 363 HUB.Register.StartupHook("CommonHTML Jax Ready",function () { 364 var MML = MathJax.ElementJax.mml; 365 var CHTML = MathJax.OutputJax.CommonHTML; 366 var HTML = MathJax.HTML; 367 368 var MERROR = MML.merror.prototype.toCommonHTML; 369 370 // 371 // Override merror toCommonHTML routine so that it puts out the 372 // TeX code in an inline-block with line breaks as in the original 373 // 374 MML.merror.Augment({ 375 toCommonHTML: function (node) { 376 if (!this.isError) return MERROR.apply(this,arguments); 377 node = CHTML.addElement(node,"mjx-noError"); 378 var text = this.data[0].data[0].data.join("").split(/\n/); 379 for (var i = 0, m = text.length; i < m; i++) { 380 HTML.addText(node,text[i]); 381 if (i !== m-1) {CHTML.addElement(node,"br",{isMathJax:true})} 382 } 383 var bbox = this.CHTML = CHTML.BBOX.zero(); 384 bbox.w = (node.offsetWidth)/CHTML.em; 385 if (m > 1) { 386 var H2 = 1.2*m/2; 387 bbox.h = H2+.25; bbox.d = H2-.25; 388 node.style.verticalAlign = CHTML.Em(.45-H2); 389 } else { 390 bbox.h = 1; bbox.d = .2 + 2/CHTML.em; 391 } 392 return node; 393 } 394 }); 395 396 }); 397 398 /*******************************************************************/ 399 400 HUB.Startup.signal.Post("TeX noErrors Ready"); 401 402 })(MathJax.Hub,MathJax.HTML); 403 404 405 MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/noErrors.js");