Skip to content

Commit 0ecf8ee

Browse files
authored
Merge pull request #252 from mathjax/issue219
[main] add option for returning dom objects
2 parents c41110c + a6634cf commit 0ecf8ee

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

lib/main.js

+26-17
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ var defaults = {
5454
xmlns: "mml", // the namespace to use for MathML
5555

5656
html: false, // return HTML output?
57+
htmlNode: false, // DOM element for HTML output?
5758
css: false, // return CSS for HTML output?
5859
mml: false, // return mml output?
60+
mmlNode: false, // DOM element for MML output?
5961
svg: false, // return svg output?
62+
svgNode: false, // DOM element for SVG output?
6063

6164
speakText: false, // add spoken annotations to svg output?
6265
speakRuleset: "mathspeak", // set speech ruleset (default (chromevox rules), mathspeak)
@@ -517,9 +520,12 @@ function AddError(message,nopush) {
517520
// into account)
518521
//
519522
function GetMML(result) {
520-
if (!data.mml && !data.speakText && !data.semantic) return;
523+
if (!data.mml && !data.mmlNode && !data.speakText && !data.semantic) return;
521524
var jax = MathJax.Hub.getAllJax()[0];
522-
try {result.mml = jax.root.toMathML('',jax)} catch(err) {
525+
try {
526+
if (data.mml) result.mml = jax.root.toMathML('',jax);
527+
if (data.mmlNode) result.mmlNode = jsdom(result.mml).body.firstChild;
528+
} catch(err) {
523529
if (!err.restart) {throw err;} // an actual error
524530
return MathJax.Callback.After(window.Array(GetMML,result),err.restart);
525531
}
@@ -557,7 +563,7 @@ function GetSemantic(result) {
557563
// Create HTML and CSS output, if requested
558564
//
559565
function GetHTML(result) {
560-
if (!data.html) return;
566+
if (!data.html && !data.htmlNode) return;
561567
var jax = MathJax.Hub.getAllJax()[0]; if (!jax) return;
562568
var script = jax.SourceElement(), html = script.previousSibling;
563569

@@ -584,15 +590,16 @@ function GetHTML(result) {
584590
// otherwise (inline-mode) the frame is the root element
585591
html.removeAttribute("id");
586592
}
587-
result.html = html.outerHTML;
593+
if (data.html) result.html = html.outerHTML;
594+
if (data.htmlNode) result.htmlNode = html;
588595
if (data.css) result.css = CHTMLSTYLES;
589596
}
590597

591598
//
592599
// Create SVG output, if requested
593600
//
594601
function GetSVG(result) {
595-
if (!data.svg) return;
602+
if (!data.svg && !data.svgNode) return;
596603
var jax = MathJax.Hub.getAllJax()[0]; if (!jax) return;
597604
var script = jax.SourceElement(),
598605
svg = script.previousSibling.getElementsByTagName("svg")[0];
@@ -613,22 +620,24 @@ function GetSVG(result) {
613620
svg.insertBefore(node,svg.firstChild);
614621
}
615622

616-
//
617-
// SVG data is modified to add linebreaks for readability,
618-
// and to put back the xlink namespace that is removed in HTML5
619-
//
620-
var svgdata = svg.outerHTML.replace(/><([^/])/g,">\n<$1")
621-
.replace(/(<\/[a-z]*>)(?=<\/)/g,"$1\n")
622-
.replace(/(<(?:use|image) [^>]*)(href=)/g,' $1xlink:$2');
623+
if (data.svg){
624+
//
625+
// SVG data is modified to add linebreaks for readability,
626+
// and to put back the xlink namespace that is removed in HTML5
627+
//
628+
var svgdata = svg.outerHTML.replace(/><([^/])/g,">\n<$1")
629+
.replace(/(<\/[a-z]*>)(?=<\/)/g,"$1\n")
630+
.replace(/(<(?:use|image) [^>]*)(href=)/g,' $1xlink:$2');
623631

624-
//
625-
// Add the requested data to the results
626-
//
627-
result.svg = svgdata;
632+
//
633+
// Add the requested data to the results
634+
//
635+
result.svg = svgdata;
636+
}
637+
if (data.svgNode) result.svgNode = svg;
628638
result.width = svg.getAttribute("width");
629639
result.height = svg.getAttribute("height");
630640
result.style = svg.style.cssText;
631-
632641
}
633642

634643
/********************************************************************/

test/issue219.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var tape = require('tape');
2+
var mjAPI = require("../lib/main.js");
3+
var jsdom = require('jsdom').jsdom;
4+
5+
tape('Basic Check: pass jsdom object to output', function(t) {
6+
t.plan(3);
7+
8+
mjAPI.start();
9+
var tex = 'x';
10+
11+
mjAPI.typeset({
12+
math: tex,
13+
format: "TeX",
14+
html: true,
15+
htmlNode: true,
16+
svg: true,
17+
svgNode: true,
18+
mml: true,
19+
mmlNode: true
20+
}, function(data) {
21+
var window = jsdom().defaultView;
22+
t.ok(data.htmlNode instanceof window.HTMLElement, 'htmlNode is an HTMLElement');
23+
t.ok(data.svgNode instanceof window.HTMLElement, 'svgNode is an HTMLElement');
24+
t.ok(data.mmlNode instanceof window.HTMLElement, 'mmlNode is an HTMLElement');
25+
});
26+
});

0 commit comments

Comments
 (0)