mml-preview.js-abandoned 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/mml-preview.js
  6. *
  7. * Implements a fast preview using the NativeMML output jax
  8. * and then a slower update using whatever output jax the user
  9. * has selected. The MathML is styled for those browsers that
  10. * don't support MathML natively.
  11. *
  12. * ---------------------------------------------------------------------
  13. *
  14. * Copyright (c) 2014-2018 The MathJax Consortium
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License");
  17. * you may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. (function (HUB,HTML) {
  29. var MMLpreview = MathJax.Extension["mml-preview"] = {
  30. version: "2.7.5",
  31. //
  32. // Configuration for the chunking of the main output
  33. // after the previews have been created
  34. //
  35. config: HUB.CombineConfig("mml-preview",{
  36. EqnChunk: 5, EqnChunkFactor: 1, EqnChunkDelay: 15000
  37. }),
  38. //
  39. // Styles used for browsers that don't have native MathML support
  40. //
  41. styles: {
  42. "math": {
  43. "font-family": "Times, 'Times New Roman', serif",
  44. "display": "inline",
  45. "text-indent": "0",
  46. "white-space": "nowrap"
  47. },
  48. "math[display='block']": {
  49. "display": "block",
  50. "text-indent": "0",
  51. "text-align": "center",
  52. "margin": "1em 0"
  53. },
  54. // "math *[mathbackground]": {
  55. // "display": "inline-block!important",
  56. // "background-color": "attr(mathbackground)"
  57. // },
  58. // "math *[mathcolor]": {
  59. // "color": "attr(mathcolor)"
  60. // },
  61. "mi": {"font-style": "italic"},
  62. "mo": {"margin": "0 .15em"},
  63. "mphantom": {"visibility": "hidden", "display":"inline-block"},
  64. // "mspace": {
  65. // "display": "inline-block!important",
  66. // "width": "attr(width)",
  67. // "height": "attr(height)"
  68. // },
  69. "mfrac": {
  70. "display": "inline-table!important",
  71. "vertical-align": "middle",
  72. "border-collapse": "collapse",
  73. "margin": "0 .125em"
  74. },
  75. "mfrac > *": {
  76. "display": "table-row!important"
  77. },
  78. "mfrac:not([linethickness='0']) > *:first-child": {
  79. "border-bottom": "thin solid"
  80. },
  81. "msqrt, mroot": {
  82. "display":"-webkit-inline-flex!important",
  83. "display ":"inline-flex!important",
  84. "margin-left": ".6em",
  85. "vertical-align": "middle",
  86. "border-top": "solid thin",
  87. "margin-top": "1px"
  88. },
  89. "msqrt:before, mroot:before": {
  90. "margin-left": "-.6em",
  91. "content": '"\u221A"'
  92. },
  93. "mroot > *:nth-child(2)": {
  94. "margin-top": "-.5em",
  95. "margin-right": ".25em",
  96. "margin-left": "-.75em",
  97. "font-size": "80%", "line-height": 1,
  98. "-webkit-order": -1, "order": -1
  99. },
  100. "msubsup > *:nth-child(2), msub > *:nth-child(2)": {
  101. "font-size": "80%", "line-height": 1,
  102. "vertical-align": "sub"
  103. },
  104. "msubsup > *:nth-child(3), msup > *:nth-child(2)": {
  105. "font-size": "80%", "line-height": 1,
  106. "vertical-align": "super"
  107. },
  108. "munderover, munder, mover": {
  109. "display":"-webkit-inline-flex!important",
  110. "-webkit-flex-direction": "column",
  111. "display ":"inline-flex!important",
  112. "flex-direction": "column"
  113. },
  114. "munderover > *:nth-child(2), munder > *:nth-child(2)": {
  115. "font-size": "80%", "line-height": 1
  116. },
  117. "munderover > *:nth-child(3), mover > *:nth-child(2)": {
  118. "-webkit-order": -1, "order": -1,
  119. "font-size": "80%", "line-height": 1
  120. },
  121. "mover": {"vertical-align": "text-bottom"},
  122. "munder": {"vertical-align": "text-top"},
  123. "munderover": {"vertical-align": "middle"},
  124. "mtable": {
  125. "display": "inline-table!important",
  126. "vertical-align": "middle",
  127. "border-collapse": "collapse"
  128. },
  129. "mtr, mlabeledtr": {
  130. "display": "table-row!important"
  131. },
  132. "mtd": {
  133. "display": "table-cell!important",
  134. "padding-left": "1em",
  135. "padding-top": ".5em"
  136. },
  137. "mtr > *:first-child": {"padding-left": 0},
  138. "mtable > mtr:first-child > *": {"padding-top": 0},
  139. "mlabeledtr > *:first-child": {"display":"none"},
  140. "semantics > *": {"display":"none!important"},
  141. "semantics > *:first-child": {"display":"inline"}
  142. },
  143. //
  144. // Ajust the chunking of the output jax
  145. //
  146. Config: function () {
  147. HUB.Config({
  148. "HTML-CSS": this.config,
  149. "SVG": this.config
  150. });
  151. },
  152. //
  153. // Insert a preview span, if there isn't one already,
  154. // and call the NativeMML output jax to create the preview
  155. //
  156. Preview: function (data) {
  157. var preview = data.script.previousSibling;
  158. if (!preview || preview.className !== HUB.config.preRemoveClass) {
  159. preview = HTML.Element("span",{className:HUB.config.preRemoveClass});
  160. data.script.parentNode.insertBefore(preview,data.script);
  161. }
  162. preview.innerHTML = "";
  163. try {data.math.root.toNativeMML(preview)} catch (e) {}
  164. return data;
  165. },
  166. //
  167. // Hook into the inut jax postFilter to create the previews as
  168. // the input jax are processed.
  169. //
  170. Register: function (name) {
  171. HUB.Register.StartupHook(name+" Jax Require",function () {
  172. var jax = MathJax.InputJax[name];
  173. var delay = HUB.config.delayJaxRegistration;
  174. HUB.config.delayJaxRegistration = true;
  175. HUB.Register.StartupHook(name+" Jax Ready",function () {HUB.config.delayJaxRegistration = delay});
  176. jax.require.push(
  177. "[MathJax]/jax/output/NativeMML/config.js",
  178. "[MathJax]/jax/output/NativeMML/jax.js"
  179. );
  180. jax.postfilterHooks.Add(["Preview",MathJax.Extension["mml-preview"]]);
  181. });
  182. }
  183. }
  184. //
  185. // Hook into each input jax
  186. //
  187. MMLpreview.Register("TeX");
  188. MMLpreview.Register("MathML");
  189. MMLpreview.Register("AsciiMath");
  190. HUB.Register.StartupHook("End Config",["Config",MMLpreview]);
  191. HUB.Register.StartupHook("onLoad",function () {
  192. var test = HTML.addElement(document.body,"div",{
  193. style: {
  194. visibility:"hidden", position:"absolute", left:0, top:0,
  195. height:"auto", width:"auto", "font-family":"serif", "line-height":"normal",
  196. display:"block!important"
  197. }
  198. });
  199. test.innerHTML = "<math><mfrac><mi>xx</mi><mi>yy</mi></mfrac></math>";
  200. if (test.offsetHeight < test.offsetWidth) {MathJax.Ajax.Styles(MMLpreview.styles)}
  201. });
  202. })(MathJax.Hub,MathJax.HTML);
  203. MathJax.Ajax.loadComplete("[MathJax]/extensions/mml-preview.js");