Skip to content

Commit

Permalink
feat: support configuring mathjax context (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlockhart authored Aug 1, 2023
1 parent 7631066 commit 0c43fa1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/browser/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { sendMouse, setViewport } from '@web/test-runner-commands';
import { nextFrame } from '@open-wc/testing';

const DEFAULT_LANG = 'en',
DEFAULT_MATHJAX_RENDER_LATEX = false,
DEFAULT_VIEWPORT_HEIGHT = 800,
DEFAULT_VIEWPORT_WIDTH = 800;

let currentLang = undefined,
currentMathjaxRenderLatex = DEFAULT_MATHJAX_RENDER_LATEX,
currentRtl = false,
currentViewportHeight = 0,
currentViewportWidth = 0,
Expand All @@ -19,6 +21,8 @@ export async function reset(opts) {

opts = opts || {};
opts.lang = opts.lang || DEFAULT_LANG;
opts.mathjax = opts.mathjax || {};
opts.mathjax.renderLatex = (typeof opts.mathjax.renderLatex === 'boolean') ? opts.mathjax.renderLatex : DEFAULT_MATHJAX_RENDER_LATEX;
opts.rtl = opts.lang.startsWith('ar') || !!opts.rtl;
opts.viewport = opts.viewport || {};
opts.viewport.height = opts.viewport.height || DEFAULT_VIEWPORT_HEIGHT;
Expand Down Expand Up @@ -61,6 +65,17 @@ export async function reset(opts) {
currentViewportWidth = opts.viewport.width;
}

if (opts.mathjax.renderLatex !== currentMathjaxRenderLatex) {
currentMathjaxRenderLatex = opts.mathjax.renderLatex;
if (opts.mathjax.renderLatex === DEFAULT_MATHJAX_RENDER_LATEX) {
document.documentElement.removeAttribute('data-mathjax-context');
} else {
document.documentElement.dataset.mathjaxContext = JSON.stringify({
renderLatex: opts.mathjax.renderLatex
});
}
}

if (awaitNextFrame) {
await nextFrame();
}
Expand Down
12 changes: 12 additions & 0 deletions test/browser/fixture.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ describe('fixture', () => {
expect(document.documentElement.getAttribute('lang')).to.equal('en');
});

it('should use specified mathjax latex config', async() => {
await fixture(html`<p>hello</p>`, { mathjax: { renderLatex: true } });
const config = JSON.parse(document.documentElement.dataset.mathjaxContext);
expect(config.renderLatex).to.be.true;
});

it('should reset mathjax latex config', async() => {
await fixture(html`<p>hello</p>`, { mathjax: { renderLatex: true } });
await fixture(html`<p>hello</p>`);
expect(document.documentElement.hasAttribute('data-mathjax-context')).to.be.false;
});

it('should default viewport size to 800x800', async() => {
await fixture(html`<p>hello</p>`);
expect(window.innerHeight).to.equal(800);
Expand Down

0 comments on commit 0c43fa1

Please sign in to comment.