mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-06 03:38:39 +00:00
Add MathML rendering to improve accessibility
Summary: This adds support for rendering KaTeX to both HTML and MathML with the intent of improving accessibility. To accomplish this, both MathML and HTML are rendered, but with the MathML visually hidden and the HTML spans aria-hidden. Hopefully, this should produce much better accessibility for KaTeX. Should fix/improve #38 Closes #189 Test Plan: - Ensure all the tests, and the new tests, still pass. - Ensure that for each of the group types in `buildHTML.js`, there is a corresponding one in `buildMathML.js`. - Ensure that the huxley screenshots didn't change (except for BinomTest, which changed because I fixed a bug in `buildHTML` where `genfrac` didn't have a `groupToType` mapping). - Run ChromeVox on the test page, render some math. (for example, `\sqrt{x^2}`) - Ensure that a mathy-sounding expression is read. (I hear "group square root of x squared math"). - Ensure that nothing else is read (like no "x" or "2"). - Ensure that MathML markup is generated correctly and is interpreted by the browser correctly by running `document.getElementById("math").innerHTML = katex.renderToString("\\sqrt{x^2}");` and seeing that the same speech is read. Reviewers: john, alpert Reviewed By: john, alpert Subscribers: alpert, john Differential Revision: https://phabricator.khanacademy.org/D16373
This commit is contained in:
Binary file not shown.
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB |
@@ -1,4 +1,5 @@
|
||||
var buildTree = require("../src/buildTree");
|
||||
var buildHTML = require("../src/buildHTML");
|
||||
var buildMathML = require("../src/buildMathML");
|
||||
var katex = require("../katex");
|
||||
var ParseError = require("../src/ParseError");
|
||||
var parseTree = require("../src/parseTree");
|
||||
@@ -9,10 +10,10 @@ var defaultSettings = new Settings({});
|
||||
var getBuilt = function(expr) {
|
||||
expect(expr).toBuild();
|
||||
|
||||
var built = buildTree(parseTree(expr), defaultSettings);
|
||||
var built = buildHTML(parseTree(expr), defaultSettings);
|
||||
|
||||
// Remove the outer .katex and .katex-inner layers
|
||||
return built.children[0].children[2].children;
|
||||
return built.children[2].children;
|
||||
};
|
||||
|
||||
var getParsed = function(expr) {
|
||||
@@ -87,7 +88,7 @@ beforeEach(function() {
|
||||
expect(actual).toParse();
|
||||
|
||||
try {
|
||||
buildTree(parseTree(actual), defaultSettings);
|
||||
buildHTML(parseTree(actual), defaultSettings);
|
||||
} catch (e) {
|
||||
result.pass = false;
|
||||
if (e instanceof ParseError) {
|
||||
@@ -1094,6 +1095,13 @@ describe("A markup generator", function() {
|
||||
expect(markup).toContain("margin-right");
|
||||
expect(markup).not.toContain("marginRight");
|
||||
});
|
||||
|
||||
it("generates both MathML and HTML", function() {
|
||||
var markup = katex.renderToString("a");
|
||||
|
||||
expect(markup).toContain("<span");
|
||||
expect(markup).toContain("<math");
|
||||
});
|
||||
});
|
||||
|
||||
describe("An accent parser", function() {
|
||||
@@ -1174,3 +1182,37 @@ describe("An optional argument parser", function() {
|
||||
expect("\\sqrt[").toNotParse();
|
||||
});
|
||||
});
|
||||
|
||||
var getMathML = function(expr) {
|
||||
expect(expr).toParse();
|
||||
|
||||
var built = buildMathML(parseTree(expr));
|
||||
|
||||
// Strip off the surrounding <span>
|
||||
return built.children[0];
|
||||
};
|
||||
|
||||
describe("A MathML builder", function() {
|
||||
it("should generate math nodes", function() {
|
||||
var node = getMathML("x^2");
|
||||
|
||||
expect(node.type).toEqual("math");
|
||||
});
|
||||
|
||||
it("should generate appropriate MathML types", function() {
|
||||
var identifier = getMathML("x").children[0].children[0];
|
||||
expect(identifier.children[0].type).toEqual("mi");
|
||||
|
||||
var number = getMathML("1").children[0].children[0];
|
||||
expect(number.children[0].type).toEqual("mn");
|
||||
|
||||
var operator = getMathML("+").children[0].children[0];
|
||||
expect(operator.children[0].type).toEqual("mo");
|
||||
|
||||
var space = getMathML("\\;").children[0].children[0];
|
||||
expect(space.children[0].type).toEqual("mspace");
|
||||
|
||||
var text = getMathML("\\text{a}").children[0].children[0];
|
||||
expect(text.children[0].type).toEqual("mtext");
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user