@@ -30,10 +30,11 @@ var http = require('http');
3030var fs = require ( 'fs' ) ;
3131var path = require ( 'path' ) ;
3232var url = require ( 'url' ) ;
33- var jsdom = require ( 'jsdom' ) . jsdom ;
33+ var jsdom = require ( 'jsdom' ) ;
34+ var JSDOM = jsdom . JSDOM ;
3435var isFullwidthCodePoint = require ( 'is-fullwidth-code-point' ) ;
3536
36- require ( './patch/jsdom.js' ) . patch ( jsdom ) ; // Fix some bugs in jsdom
37+ require ( './patch/jsdom.js' ) . patch ( JSDOM ) ; // Fix some bugs in jsdom
3738
3839var displayMessages = false ; // don't log Message.Set() calls
3940var displayErrors = true ; // show error messages on the console
@@ -114,22 +115,22 @@ var CHTMLSTYLES; // filled in when CommonHTML is loaded
114115// the jax to be loaded completely)
115116//
116117function GetWindow ( ) {
117- document = jsdom ( '' , { userAgent : "Node.js" } ) ;
118+ var virtualConsole = new jsdom . VirtualConsole ( ) ;
119+ virtualConsole . sendTo ( console ) ;
120+ window = new JSDOM ( '' , {
121+ virtualConsole : virtualConsole ,
122+ userAgent : "Node.js" ,
123+ runScripts : "dangerously" ,
124+ resources : "usable"
125+ } ) . window ;
126+ document = window . document ;
118127 html = document . firstChild ;
119- window = document . defaultView ;
120- window . console = console ;
121128 window . addEventListener ( "error" , function ( event ) { AddError ( "Error: " + event . error . stack ) } ) ;
122129 content = document . body . appendChild ( document . createElement ( "div" ) ) ;
123130 content . id = "MathJax_Content" ;
124131 content . innerHTML = '<script type="math/tex">x</script>' +
125132 '<script type="math/asciimath">x</script>' +
126133 '<script type="math/mml"><math><mi>x</mi></math></script>' ;
127- //
128- // Node's url.resolve() has a problem with resolving a file:// URL when
129- // the base URL is "about:blank", so force it to be something else (HACK)
130- // since jsdom 3.x sets the base to "about:blank".
131- //
132- if ( document . _URL === "about:blank" ) document . _URL = "file:///blank.html" ;
133134}
134135
135136//
@@ -140,7 +141,6 @@ function ConfigureMathJax() {
140141 //
141142 // Load all input jax and preprocessors
142143 // Load AMS extensions and the autoload extension for TeX
143- // Allow $...$ delimiters and don't create previews for any preprocessor,
144144 // Create stand-alone SVG elements with font caches by default
145145 // (users can override that)
146146 //
@@ -324,6 +324,30 @@ function ConfigureMathJax() {
324324 this . d = this . D = ( bbox . height + bbox . y ) * scale ;
325325 }
326326 } ) ;
327+
328+ //
329+ // Don't have mglyph load images
330+ //
331+ MathJax . Hub . Register . StartupHook ( "SVG mglyph Ready" , function ( ) {
332+ var MML = MathJax . ElementJax . mml ;
333+ var MGLYPH = MML . mglyph ;
334+ var TOSVG = MGLYPH . prototype . toSVG ;
335+ MGLYPH . Augment ( {
336+ toSVG : function ( variant , scale ) {
337+ var values = this . getValues ( "src" , "width" , "height" ) ;
338+ if ( values . src !== "" && ! MGLYPH . GLYPH [ values . src ] ) {
339+ if ( ! values . width || ! values . height ) {
340+ AddError ( "mglyphs must have explicit width and height in mathjax-node" ) ;
341+ }
342+ MGLYPH . GLYPH [ values . src ] = {
343+ img : { SRC : values . src , width : 0 , height : 0 } ,
344+ status : "OK"
345+ } ;
346+ }
347+ return TOSVG . apply ( this , arguments ) ;
348+ }
349+ } ) ;
350+ } ) ;
327351
328352 } ) ;
329353
@@ -427,7 +451,43 @@ function ConfigureMathJax() {
427451
428452 } ) ;
429453
454+ //
455+ // Don't have mglyph load images
456+ //
457+ MathJax . Hub . Register . StartupHook ( "CommonHTML mglyph Ready" , function ( ) {
458+ var MML = MathJax . ElementJax . mml ;
459+ var MGLYPH = MML . mglyph ;
460+ var TOCHTML = MGLYPH . prototype . toCommonHTML ;
461+ MGLYPH . Augment ( {
462+ toCommonHTML : function ( node , options ) {
463+ var values = this . getValues ( "src" , "width" , "height" ) ;
464+ if ( values . src !== "" && ! MGLYPH . GLYPH [ values . src ] ) {
465+ if ( ! values . width || ! values . height ) {
466+ AddError ( "mglyphs must have explicit width and height in mathjax-node" ) ;
467+ }
468+ MGLYPH . GLYPH [ values . src ] = {
469+ img : { SRC : values . src , width : 0 , height : 0 } ,
470+ status : "OK"
471+ } ;
472+ }
473+ return TOCHTML . apply ( this , arguments ) ;
474+ }
475+ } ) ;
476+ } ) ;
477+
478+ } ) ;
479+
480+ //
481+ // Set up None output jax (for when only MathML output is needed)
482+ //
483+ MathJax . OutputJax . None = MathJax . OutputJax ( {
484+ id : "None" ,
485+ preTranslate : function ( ) { } ,
486+ Translate : function ( ) { } ,
487+ postTranslate : function ( ) { }
430488 } ) ;
489+ MathJax . OutputJax . None . loadComplete ( "jax.js" ) ;
490+ MathJax . OutputJax . None . Register ( "jax/mml" ) ;
431491
432492 //
433493 // Reset the color extension after `autoload-all`
@@ -448,14 +508,14 @@ function ConfigureMathJax() {
448508 // (reseting the counters so that the initial math doesn't affect them)
449509 //
450510 MathJax . Hub . Register . StartupHook ( "End" , function ( ) {
451- MathJax . OutputJax . SVG . resetGlyphs ( true ) ;
511+ if ( MathJax . OutputJax . SVG . resetGlyphs ) MathJax . OutputJax . SVG . resetGlyphs ( true ) ;
452512 MathJax . ElementJax . mml . ID = 0 ;
453513 serverState = STATE . READY ;
454514 MathJax . Hub . Queue ( StartQueue ) ;
455515 } ) ;
456516 }
457517 } ;
458-
518+
459519 if ( extensions ) {
460520 //
461521 // Parse added extensions list and add to standard ones
@@ -511,7 +571,7 @@ function StartMathJax() {
511571 serverState = STATE . STARTED ;
512572 var script = document . createElement ( "script" ) ;
513573 script . src = MathJaxPath ;
514- script . onerror = function ( ) { AddError ( "Can't load MathJax.js from " + MathJaxPath ) }
574+ script . onerror = function ( ) { AddError ( "Can't load MathJax.js from " + MathJaxPath ) } ;
515575 document . head . appendChild ( script ) ;
516576}
517577
@@ -543,7 +603,7 @@ function AddError(message,nopush) {
543603function GetMML ( result ) {
544604 if ( ! data . mml && ! data . mmlNode ) return ;
545605 var jax = MathJax . Hub . getAllJax ( ) [ 0 ] ;
546- if ( data . speakText && ! jax . root . alttext ) {
606+ if ( data . speakText && ! jax . root . alttext ) {
547607 jax . root . alttext = result . speakText ;
548608 var attrNames = jax . root . attrNames ;
549609 if ( attrNames && attrNames . indexOf ( "alttext" ) === - 1 ) {
@@ -557,7 +617,7 @@ function GetMML(result) {
557617 return MathJax . Callback . After ( window . Array ( GetMML , result ) , err . restart ) ;
558618 }
559619 if ( data . mml ) result . mml = mml ;
560- if ( data . mmlNode ) result . mmlNode = jsdom ( mml ) . body . firstChild ;
620+ if ( data . mmlNode ) result . mmlNode = JSDOM . fragment ( mml ) . firstChild ;
561621}
562622
563623//
@@ -700,7 +760,13 @@ function StartQueue() {
700760 //
701761 GetState ( data . state ) ;
702762
703- var renderer = ( ( data . html || data . htmlNode || data . css ) ? "CommonHTML" : "SVG" ) ;
763+ //
764+ // Get the renderer to use
765+ //
766+ var renderer = (
767+ ( data . html || data . htmlNode || data . css ) ? "CommonHTML" :
768+ ( data . svg || data . svgNode ) ? "SVG" : "None"
769+ ) ;
704770
705771 //
706772 // Set up a timeout timer to restart MathJax if it runs too long,
@@ -731,7 +797,7 @@ function GetState(state) {
731797 MML = MathJax . ElementJax . mml ,
732798 AMS = MathJax . Extension [ "TeX/AMSmath" ] ,
733799 HUB = MathJax . Hub , HTML = MathJax . HTML ,
734- GLYPH = SVG . BBOX . GLYPH ;
800+ GLYPH = ( SVG . BBOX || { } ) . GLYPH ;
735801
736802 if ( state && state . AMS ) {
737803 AMS . startNumber = state . AMS . startNumber ;
@@ -744,7 +810,7 @@ function GetState(state) {
744810 ID = state . ID ;
745811 } else {
746812 if ( state ) { state . AMS = { } }
747- SVG . resetGlyphs ( true ) ;
813+ if ( SVG . resetGlyphs ) SVG . resetGlyphs ( true ) ;
748814 if ( data . useGlobalCache ) {
749815 state . glyphs = { } ;
750816 state . defs = HTML . Element ( "defs" ) ;
@@ -777,7 +843,7 @@ function ReturnResult(result) {
777843 var state = data . state ;
778844 if ( state ) {
779845 var AMS = MathJax . Extension [ "TeX/AMSmath" ] ;
780- var GLYPH = MathJax . OutputJax . SVG . BBOX . GLYPH ;
846+ var GLYPH = ( MathJax . OutputJax . SVG || { } ) . BBOX . GLYPH ;
781847 state . AMS . startNumber = AMS . startNumber ;
782848 state . AMS . labels = AMS . labels ;
783849 state . AMS . IDs = AMS . IDs ;
0 commit comments