mathjax-parser
Version:
Find & Replace the mathjax delimiters in a HTML string
76 lines (57 loc) • 2.1 kB
JavaScript
(function() {
var config = {
inlineMath: [['$','$'],['\\(','\\)']],
displayMath: [['$$','$$'],['\\[','\\]']],
inlineMathReplacement: ['XXX', 'XXX'],
displayMathReplacement: ['YYY','ZZZ']
};
var parser = new MathjaxParser();
var html;
var out;
QUnit.test( "Tests", function( assert ) {
//simple inline
html ="First $test$";
out = parser.parse(html, config).outputHtml;
assert.equal( out, 'First XXXtestXXX');
//shouldnt replace anything
html ="I thought $<span>it's great$</span>";
out = parser.parse(html, config).outputHtml;
assert.equal( out, html);
//mixed delims
html ="I $thought$ that \\(it's great\\)";
out = parser.parse(html, config).outputHtml;
assert.equal( out, "I XXXthoughtXXX that XXXit's greatXXX");
//with br
html ="Hello $\\frac a b = c <br> =d$";
out = parser.parse(html, config).outputHtml;
assert.equal( out, "Hello XXX\\frac a b = c <br> =dXXX");
//$ on edges
html = "$How you$";
out = parser.parse(html, config).outputHtml;
assert.equal( out, "XXXHow youXXX");
//children
html = "I $thought$ <span>\\(it's great\\) however <b>$bla$</b> </span>";
out = parser.parse(html, config).outputHtml;
assert.equal( out, "I XXXthoughtXXX <span>XXXit's greatXXX however <b>XXXblaXXX</b> </span>");
//simple inline
html ="First $<x$";
out = parser.parse(html, config).outputHtml;
assert.equal( out, 'First XXX<xXXX');
//block test
html ="First $$test$$";
out = parser.parse(html, config).outputHtml;
assert.equal( out, 'First YYYtestZZZ');
//with less than sign
html ="<p>$<x$</p>";
out = parser.parse(html, config).outputHtml;
assert.equal( out, '<p>XXX<xXXX</p>');
//nested math, shouldnt be parsed
html ="$$ Hello $World$ $$";
out = parser.parse(html, config).outputHtml;
assert.equal( out, 'YYY Hello $World$ ZZZ');
//same line $$ then $
html ="$$a$$ $b$";
out = parser.parse(html, config).outputHtml;
assert.equal( out, 'YYYaZZZ XXXbXXX');
});
})();