Skip to content
This repository was archived by the owner on May 15, 2019. It is now read-only.

Commit faa1920

Browse files
author
David Flanagan
committed
Update the <TeX> component to the latest upstream KaTeX
Summary: This diff updates docs/js/katex.js to c71ee4bdd5439c39e78ef2e91c9a2ddb8053844f which is approximately v0.90-beta and modifies the <TeX> component to use the new colorIsTextColor option for backward-compatible handling of \color{}. It adds a test to ensure that the option has the desired effect. It also makes a number of updates to js/katex-a11y.js to bring it back in sync with the latest KaTeX. Test plan: - make test Reviewers: emily, kevinb Subscribers: tom Differential Revision: https://phabricator.khanacademy.org/D41809
1 parent 24e4194 commit faa1920

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

docs/js/katex.js

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/katex-a11y.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ var typeHandlers = {
174174
buildRegion(a11yStrings, function(a11yStrings) {
175175
buildA11yStrings(tree.value.base, a11yStrings);
176176
a11yStrings.push("with");
177-
buildA11yStrings(tree.value.accent, a11yStrings);
177+
buildA11yStrings(tree.value.label, a11yStrings);
178178
a11yStrings.push("on top");
179179
});
180180
},
@@ -227,12 +227,19 @@ var typeHandlers = {
227227
});
228228
},
229229

230-
// inner
230+
inner: function(tree, a11yStrings) {
231+
buildA11yStrings(tree.value, a11yStrings);
232+
},
231233

232234
katex: function(tree, a11yStrings) {
233235
a11yStrings.push("KaTeX");
234236
},
235237

238+
kern: function(tree, a11yStrings) {
239+
// No op: we don't attempt to present kerning information
240+
// to the screen reader.
241+
},
242+
236243
leftright: function(tree, a11yStrings) {
237244
buildRegion(a11yStrings, function(a11yStrings) {
238245
buildString(tree.value.left, "open", a11yStrings);
@@ -241,7 +248,7 @@ var typeHandlers = {
241248
});
242249
},
243250

244-
llap: function(tree, a11yStrings) {
251+
lap: function(tree, a11yStrings) {
245252
buildA11yStrings(tree.value.body, a11yStrings);
246253
},
247254

@@ -277,6 +284,10 @@ var typeHandlers = {
277284
buildString(tree.value, "punct", a11yStrings);
278285
},
279286

287+
raisebox: function(tree, a11yStrings) {
288+
buildA11yStrings(tree.value, a11yStrings);
289+
},
290+
280291
rel: function(tree, a11yStrings) {
281292
buildString(tree.value, "rel", a11yStrings);
282293
},
@@ -452,8 +463,13 @@ var flattenStrings = function(a11yStrings, results) {
452463
};
453464

454465
var parseMath = function(text) {
455-
// NOTE: `katex` is a global, should be included using require
456-
return katex.__parse(text);
466+
// NOTE: `katex` is a global. We assume it has been imported somehow.
467+
//
468+
// colorIsTextColor is an option added in KaTeX 0.9.0 for backward
469+
// compatibility. It makes \color parse like \textcolor. We use it
470+
// in the KA webapp, and need it here because the tests are written
471+
// assuming it is set.
472+
return katex.__parse(text, { colorIsTextColor: true });
457473
};
458474

459475
var render = function(text, a11yNode) {

js/tex.jsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const srOnly = {
7979
const TeX = React.createClass({
8080
propTypes: {
8181
children: React.PropTypes.node,
82+
katexOptions: React.PropTypes.any,
8283
onClick: React.PropTypes.func,
8384
onRender: React.PropTypes.func,
8485
style: React.PropTypes.any,
@@ -88,6 +89,15 @@ const TeX = React.createClass({
8889

8990
getDefaultProps: function() {
9091
return {
92+
katexOptions: {
93+
// There was a breaking change in the behavior of \color{}
94+
// in KaTeX 0.8.0. KA content relies on the old behavior
95+
// so we set this option to retain that old behavior even
96+
// though it is not purely compatible with LaTeX.
97+
// See https://github.com/Khan/KaTeX/blob/master/README.md
98+
// for details on this option.
99+
colorIsTextColor: true
100+
},
91101
// Called after math is rendered or re-rendered
92102
onRender: function() {},
93103
onClick: null,
@@ -178,7 +188,8 @@ const TeX = React.createClass({
178188
let katexHtml = null;
179189
try {
180190
katexHtml = {
181-
__html: katex.renderToString(this.props.children),
191+
__html: katex.renderToString(this.props.children,
192+
this.props.katexOptions),
182193
};
183194
} catch (e) {
184195
/* jshint -W103 */

test/katex-a11y-tex.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
},
5454
{
5555
"input": "\\KaTeX",
56-
"output": "KaTeX"
56+
"output": "K, A, T, E, X"
5757
},
5858
{
5959
"input": "ab\\llap{f}cd\\rlap{g}h",
@@ -151,4 +151,4 @@
151151
"input": "4 \\geq 3",
152152
"output": "4, is greater than or equal to, 3"
153153
}
154-
]
154+
]

test/tex-test.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ describe("TeX", () => {
3232
assert(html.indexOf('q') === -1);
3333
});
3434

35+
it("handles \\color{} in the old mathjax-compatible way", () => {
36+
const html = ReactDOMServer.renderToString(
37+
<TeX>{"\\text{\\color{#f00} xyz}"}</TeX>
38+
);
39+
assert(html.indexOf(
40+
'<span class="mord" style="color:#f00;">x</span>') !== -1);
41+
});
42+
3543
describe("MathJax", () => {
3644
beforeEach(function() {
3745
global.document = jsdom.jsdom();

0 commit comments

Comments
 (0)