Commit Graph

290 Commits

Author SHA1 Message Date
Erik Demaine
7b22eeb64a Move test helpers into common modules (#1318)
* Move test helpers into common modules

* helpers.js gets all the helper functions
* setup.js gets the common Jest setup (serializer, expect extensions)

* Exclude test from coverage testing

* @ylemkimon's comments: parsing -> building, settings || defaultSettings

* Default argument for settings

* Fix lint errors

* @ylemklemon's comment: use buildAndSetResult

* Use template literals
2018-05-18 09:45:19 -04:00
Erik Demaine
369b5a8276 Allow all Unicode symbols in nonstrict mode (#1217)
Change symbol parsing to allow all Unicode symbols when the appropriate strict setting allows it.  By default, this allows all symbols, but the user will get (probably two) warnings about them.
2018-05-17 11:33:01 -04:00
Erik Demaine
1ed99d9ff3 Strict setting controls \newline display-mode behavior; fix MacroExpander space handling (#1314)
* Strict setting controls \newline display-mode behavior

* Bug-fix space handling in macros

Whitespace after a \controlWord is now handled within the lexer, not by the
MacroExpander.  This way, \\ expanding to \newline doesn't accidentally
cause spaces to get consumed.

* Rename nonstrict -> reportNonstrict; strictBehavior -> useStrictBehavior

* Second category of errorCodes
2018-05-16 09:37:41 -04:00
Erik Demaine
7ab4f76e16 Implement strict mode (replacing unicodeTextInMathMode) (#1278)
* Implement strict mode (replacing unicodeTextInMathMode)

Add new "strict" setting (default value false) that can take a boolean
(whether to throw an error or silently ignore), string ("ignore",
"warn", or "error"), or a function possibly returning such a value.
This enables a variety of ways of handling or ignoring transgressions
from "true" LaTeX behavior, making KaTeX easier to use while still
providing the ability for strict LaTeX adherance.

Resolve #1226, implementing that spec, for two existing
transgressions from regular LaTeX:

* src/functions/kern.js had some errors and warnings about use of
  (units in) math vs. text mode commands.
* The former setting unicodeTextInMathMode (not in any released version)
  needed to be set to true to enable Unicode text symbols in math mode.

Now these are controlled by the strict setting.  By default, KaTeX is now
very permissive, but if desired, the user can request warnings or errors.

* Rewrite strict description

* Add tests for strict functions

* Stricter type for strict

* Switch default strict setting to "warn"

* Fix new flow error

* Fix another flow bug
2018-05-13 14:27:30 -04:00
Erik Demaine
4801ab875a Support for top-level \newline and \\ in inline math (#1298)
* Support for top-level \newline and \\ in inline math

This was a little tricky because `\\` was defined as an endOfExpression.
Instead made `\\` a termination specific to an array environment.
Outside an array environment, buildHTML handles the `cr` object,
resulting in a `.newline` class.  Currently this turns into a
`display: block` (with appropriate vertical spacing) only in inline math,
matching LaTeX.

* Simplify code

* Fix Jest errors

* NewLine screenshot test

* Bug fix: \\ only works at top level of inline

* Add \newline and \cr to test

* Switch test to pmatrix

* Add vertical space test

* Add \\ vs. \newline tests

* Fix flow errors

* Add \cr test

* Add documentation for \\ at top level

* Comment out newRow

* Fix commenting out
2018-05-13 09:58:24 -04:00
Ron Kok
74422e1462 Support \hline (#1306)
* Support \hline

Support `\hline`.

* Fix CSS

* Fix lint errors

* Add double \hline. Code Cleanup

* Reduce width of screenshot test

* Add screenshots

* Use consumeSpaces, read multiple \hlines

* Code cleanup

* Update screenshots
2018-05-12 13:51:14 -04:00
Erik Demaine
929b9bf5e7 One strut instead of two (#1307)
* One strut instead of two

As suggested in https://github.com/Khan/KaTeX/pull/1287#discussion_r187485780
the two struts were redundant; the formerly "bottom" strut suffices to
implement both height and depth constraints.

* Update screenshots
2018-05-11 16:21:52 -04:00
Erik Demaine
523df299e5 Line breaks for inline formulas (#1287)
* Line breaks for inline formulas

* Basic support for \allowbreak and \nobreak

* Fix spacing around \nobreak, and add documentation

* Backwards-compatibility _getBuilt to fix tests

* Put operator spacing on same line as operator

* One approach to ~

* Simplify \allowbreak/\nobreak, make ~/\nobreakspace prevent line breaks

* Adapt to #1295

* Prevent wrapping within a .base

* Implement \hspace* properly

* Fix flow error

* Update comment for regularSpace

* Update screenshots

* Move `width: min-content` from .katex into .base

* Fix screenshot

* Add min-width rule to .vlist-s

* Factor out hasClass method

* Cleanup nobreak test

* Pull out buildHTMLUnbreakable

* Fix \hspace* test (no longer the same as \hspace)

* Fix \nobreak handling

* Add screenshot test
2018-05-11 01:44:26 +02:00
Ron Kok
34e6458245 Fix underbrace (#1304)
* Fix \underbrace

Fix the vertical alignment so that it correctly takes into account the descender of the lower element.

Fixes issue 1303.

* Update screenshots
2018-05-10 12:50:59 -04:00
Ron Kok
c2ae9e0da6 Revert frac-lines to borders (#1249)
* Revert frac-lines to borders

This PR reverts the rendering of frac-lines from SVG paths back to span borders. This solves the thick grey bar reported by @mbourne in issue #1173.

The result is a frac-line similar to KaTeX v0.9.0-alpha1. The frac-lines are span borders and the CSS contains a `min-width: 1px;` rule to keep those borders visible.

There is one difference between this PR and v0.9.0-alpha1. The earlier work contained a second CSS rule:
```
@media screen and (-webkit-min-device-pixel-ratio: 2), screen and (min-resolution: 192dpi) {
      .katex.mfra.fracline {
            min-height:0.5px;
        }
    }

That second CSS rule did not help much. Instead, it caused some lines to disappear. In the thread to issue 1173, people tested the rendering from the proposed approach and reported that frac-lines disappeared only at sized that were already too small to read.

* Fix lint errors

* Remove superfluous SVG paths.

* Remove superfluous CSS

* Removed `rulespan` from `stretchy.js`

* Support Unicode \ll and \lll

Fixes issue #1271

* Fix indent

* update screenshots for fracLineBorder

* Update Arrays-chrome screenshot for fracLineBorder
2018-05-10 16:05:24 +09:00
Ron Kok
7b7b532fb6 Adjust \underset alignment and spacing (#1290)
* Support Unicode \ll and \lll

Fixes issue #1271

* Adjust \underset alignment and spacing

Fixes issue #1288.

1.  Fixes vertical alignment by keeping the base element on the baseline.
2. Assigns horizontal spacing to \underset in a manner that aligns with LaTeX `\binrel`

* Fix flow error

* Add comment

* Allow for shift of mathops

* Update screenshots

* Test for nested ordgroups

* Set \stackrel spacing class to mrel

* Fix lint error

* Better determination of atom type

* Add comment.

* Add `suppressBaseShift` to ParseNodeTypes
2018-05-10 13:13:37 +09:00
Ashish Myles
5a4aedd882 Make ParseNode value payload and defineFunction handler functions type-safe (#1276)
* Make ParseNode `value` payload type-safe.

* Make defineFunction handlers aware of ParseNode data types.

* Add `type` to all function definitions to help determine handler return type.

* Added unit test for case caught only in screenshot test and fixed issue.

* Rename some symbol `Group`s to avoid conflicts with `ParseNode` groups.

Symbol `Group`s are also used as `ParseNode` types. However, `ParseNode`s of
these types always contain a raw text token as opposed to any structured
content. These `ParseNode`s are passed as arguments into function handlers to
create more semantical `ParseNode`s with more structure.

Before this change, "accent" and "op" were both symbol `Group`s and `ParseNode`
types. With this change, these two types (the raw accent token `ParseNode`, and
the structured semantical `ParseNode` are separated for better type-safety on
the `ParseNode` payload).

* stretchy: Remove FlowFixMe for a forced typecast that's no longer needed.
2018-05-09 20:13:31 -04:00
Erik Demaine
49e673a59c Stop throwing ParseError when throwOnError is false (#1169)
* Stop throwing ParseError when throwOnError is false

`render`, `renderToString`, etc. now catch ParseError and render it to the
raw LaTeX (with proper escaping) and a hover title with the error.

Along the way:
* Use new `katex.__renderToDomTree` in katex-spec's `_getBuilt`.
  (This was necessary to get the new error handling in `_getBuilt`.)
* Fix jest results which must always be functions, not strings.

* fix lint

* Fix flow error

Leave error type unspecified, as we check it with instanceof.

* Update katex-spec.js
2018-05-09 22:18:50 +09:00
Erik Demaine
3e529dd5a0 Tilde \textasciitilde and fix ~ in \verb (#1286)
* \textasciitilde and fix ~ in \verb

* Add support for `\textasciitilde` which reproduces tilde character
  in text mode.
* Make `~` render as tilde instead of space within `\verb`,
  by manually mapping that character to `\textasciitilde`.
  This seems to be the only character that needs such manual mapping
  to reproduce LaTeX's `\verb` behavior.

* Add `, -, ', ~ to Verb screenshot test
2018-05-09 08:48:47 -04:00
Erik Demaine
5c159abfbb Switch makeGlue from .mord .rule to .mspace (#1295)
* Switch makeGlue from .mord .rule to .mspace

* Update screenshots
2018-05-08 15:28:53 -04:00
Erik Demaine
ba24d2073b Remove special \verb space handling now that space is in Typewriter-Regular (#1258)
* Remove special \verb space handling now that space is in Typewriter-Regular

* Fix screenshots
2018-05-07 21:07:53 -04:00
Ron Kok
f01f504cfe Support some Unicode Mathematical Alphanumeric Symbols (#1232)
* Support some  Unicode Mathematical Alphanumeric Symbols

This PR adds support for some of the characters in Unicode range U+1D400 to U+1D7FF, [Mathematical Alphanumeric Symbols](https://www.unicode.org/charts/PDF/U1D400.pdf).  Specifically, it adds support for:
* A-Z a-z bold
* A-Z a-z bold italic
* A-Z a-z Fractur
* A-Z a-z sans-serif
* A-Z double struck
•	A-Z script

Addresses issue #1215 and parts of #260.

* Fix lint error

* Fix MathML, and pick up review comments

* Fix lint error

* Add text mode. Remove sans-serif.

* Fix lint error

* Fixed \mathrm, added screenshotter test

* Change screenshotter test to an array

* Add screenshots

* Picked up review comments. Add characters
2018-05-07 20:06:16 -04:00
Ron Kok
f25e08d618 Support Unicode ≘≙≚≛≝≞≟ (#1274)
* Support Unicode stackrel

Supports Unicode characters ≘ ≙ ≚ ≛ ≝ ≞ ≟

* Fix lint error

* Delete unnecessary braces
2018-05-03 16:58:04 -04:00
Ron Kok
7fec5ae5dc Fix \underset (#1277)
* Fix \underset

This PR modifies `\underset` and `\overset` to align with LaTeX behavior when a superscript is applied after the `\underset`.  Fixes issue #1275.

I also changed the spacing from KaTeX `mop` to KaTeX `rmel`. The result seems to be a closer visual match to LaTeX.

* Update screenshots
2018-05-03 16:50:18 -04:00
Ron Kok
5535d720dc Greek caps (#1283)
* Support Greek capital letters

Supports Greek letters ΑΒΕΖΗΙΚΜΝΟΡΤΧ.

Resolves issue #1282.
2018-05-02 14:29:57 -04:00
Ron Kok
b4b57cbc2b Support Unicode \ll and \lll (#1273)
* Support Unicode \ll and \lll

Fixes #1272. Consistent with `unicode-math`

* Rearranged test strings
2018-04-24 21:01:05 -04:00
Erik Demaine
812b350a5d Fix space width in \texttt (#1261)
* Fix space width in \texttt

Fix #1255 via https://github.com/KaTeX/katex-fonts/pull/41.
Also fix width metrics via https://github.com/KaTeX/katex-fonts/pull/42.

* Improve test

* Update screenshots
2018-04-21 19:52:18 -04:00
Erik Demaine
933a0ee5b5 Include Bold-Italic fonts for \boldsymbol (#1257)
* Include Bold-Italic fonts for \boldsymbol

Fix #1228

* Update screenshots
2018-04-12 22:24:06 -04:00
ylemkimon
1c5a7ed232 Fix anchor.toMarkup (#1246)
* Fix anchor.toMarkup

* Add space between `a`(tag name) and `href`(attribute name)
* Remove `this.href` appended to `markup` in the template literal

* Add regression test on #1245
2018-03-31 08:44:27 -04:00
Erik Demaine
4f29c5a942 Support \( and fix instant mode switching for $, \(, \text (#1213)
* Fix #1212 by supporting `\(...\)` in addition to `$...$` inline math nested inside `\text`. This turned out to be harder than one would think because of the way `$` was being handled specially, while `\(` needed to be handled as a function, which immediately consumed the `\(` token, which meant that the first following token got consumed in text mode instead of math mode (unlike `$` where we could switch the mode before consumption).
* Added new `consumeMode` setting for functions which tells the parser to switch modes just for the consumption of the command token.
* Now that we're working with functions, move all the `$` handling code into `functions/math.js`.  Somewhat bizarrely, we can define `$` as a function (nearly identical to `\(`) and it all works (just like we can have single-character macros).  This move removed a lot of messy stuff from `Parser.js`: `ParsedDollar`, `ParsedFuncOrArgOrDollar`, `newDollar`, `assertFuncOrArg`, and at least three explicit checks for `if (... === "$")`.
* Moved the `consume()` for the command token from `parseSymbol` to `parseGivenFunction`.  This ended up not being strictly necessary, but seems conceptually cleaner.
* Partially address #1027 by setting `consumeMode: "text"` for `\text` commands.  As a result, `\text` now instantly switches the mode to `"text"`, so even an unbraced macro argument will properly detect text mode.  I added a test for this, which is a slightly weaker form of #1027.
2018-03-14 10:54:36 -04:00
Ron Kok
65b47430b5 Support Unicode delimiters (#1207)
* Support Unicode delimiters

This PR adds support for the Unicode versions of \lfloor, \rfloor, \lceil, \rceil, \lgroup, \rgroup, \lmoustache, \rmoustache, \ulcorner, \urcorner, \llcorner, and \lrcorner.

All the mappings agree with `unicode math`

* Change tests from Parse to Build
2018-03-13 09:21:19 -03:00
Erik Demaine
0d3cf000d2 Add \yen support to text mode (#1208)
Fix #1204 by adding `\yen` support to text mode.

More generally, I checked all the text+mode symbols supported by
[amsfonts](http://mirrors.ctan.org/fonts/amsfonts/doc/amsfonts.pdf)
(see Section 6), and found that `\yen` is the only one lacking text-mode
support.
2018-03-13 02:22:04 +01:00
Ron Kok
b148d5da71 Support Unicode ° (#1203)
Oddly, `unicode-math-table.txt` does not contain an entry for U+00B0. But `\degree` is surely the correct mapping.
2018-03-10 13:19:13 -05:00
Erik Demaine
abfb641d7a Fix \nobreakspace, refactor "regular space" checking (#1200)
* Fix \nobreakspace, refactor "regular space" checking

Fix #1199 by adding \nobreakspace to the list of commands/symbols that
behave like a regular space character.  Refactor to put that list in one
place, and use an object so it checks instantly instead of sequentially.

* Copy test from #1201
2018-03-10 00:14:42 +01:00
Erik Demaine
20b5a58451 Work around negative space bug in Chrome (#1194)
* Work around negative space bug in Chrome

Fix #984

The screenshot test is still somewhat defective, showing a space before
the "post" text (a period).  This seems limited to the screenshotter, however,
as it works fine in the tester via
http://localhost:7936/?text=%5Cfbox%7B%5C%241%2C%5C!000%2C%5C!000%7D%5CKaTeX&display=0&after=.

* Update screenshots

* Switch to inherited `width: min-content`
2018-03-09 16:38:25 +01:00
ylemkimon
96ad6e0f4f Add unicode-fonts as submodule (#1182)
* Add unicode-fonts as submodule

Removed checkout process from screenshotter.sh.

* Rename test-fonts submodule and update repo URL
2018-03-05 23:29:19 -05:00
Erik Demaine
aed1c1e564 unicodeTextInMathMode setting (#1117)
* unicodeTextInMathMode setting

* When `unicodeTextInMathMode` is `true`, accented letters from
  `unicodeSymbols.js`, and CJK and other supported languages,
  get added support in math mode (as requested in #895).
* When `unicodeTextInMathMode` is `false, all of these stop working in
  math mode, and are only supported in text mode (matching XeTeX behavior).
  Note that this is a backwards incompatibility with some 0.9.0 alpha/betas.

* Fix handling of Unicode characters ð, Å, å

* Fix double handling of ð (math maps to \eth, not special Unicode character)
* Remove Åå special math handling, thanks to #1125

* Forbid extraLatin when unicodeTextInMathMode is false
2018-02-19 21:25:20 -05:00
Ryan Randall
7de91f73eb Nested Math in Non-Default Text Fix (#1111)
* Fixing nested math in non-default text. Added appropriate screenshot tests.

* Adding appropriate logic for buildMathML and resolving tests.

* Addressing Kevin's comment. Adding tests.

* Adding appropriate screenshots.

* Removing unnecessary data, adding consistent naming convention.

* Cleanup

* Handling possible edge cases.

* Updating per PR comments
2018-02-19 17:37:08 -05:00
Erik Demaine
b341034d2b Add tests for __* internal interface (#1170)
Inspired by codecov
2018-02-19 10:39:16 -05:00
Ron Kok
95ffb4fad4 Padding over \sqrt and Paths for frac-line (#1143)
* Padding over \sqrt and Paths for frac-line

This replaces two earlier PRs.

* Restore reaction arrows

* regenerate screenshots

* Set line padding in a constant

* Update with latest master

* Fix lint error

* update screenshots
2018-02-17 13:56:13 -05:00
Erik Demaine
627fc11f1b Support \underline in text mode (#1159)
* Support \underline in text mode

Fix #1158

* Add to LowerAccent test
2018-02-13 20:30:30 -05:00
Ron Kok
d80be86015 Support Unicode colon equals (#1151)
* Support Unicode colon equals

This PR adds support for three Unicode characters: `≔` `≕` `⩴`

* Fix error in \Coloneqq
2018-02-09 07:58:23 -05:00
Ron Kok
73d80f595c Add \nobreakspace (#1145) 2018-02-05 21:17:36 -05:00
Ron Kok
2aee354ca2 Support Reaction Arrows (#1078)
* Support Reaction Arrows

This PR is written to supply reaction arrows for a future `mhchem` extension. `mhchem` uses seven reaction arrows. Four of them correspond to extensible arrows already available in KaTeX. This PR creates the other three.

These arrows will also be useful to chemists writing about reactions when `mhchem` is unavailable.

Three new extensible arrows are introduced: `\xrightleftarrows`, `\xrightequilibrium`, and `\xleftequilibrium`.

These names are not `mhchem`’s user-facing function names. In `mhchem`, users would call these arrows with syntax such as: `\ce{A<-->B}`, or `\ce{A<=>>B}`, or `\ce{A<<=>B}`. I’ve provided names that look more like the other extensible arrow names. That’s probably worth some discussion.

* generate screenshots for ReactionArrows

* Increase overlap in arrow shaft

To prevent a small gap when rendering in very large font sizes.

* Adjust upper text vert alignment. Add warning.

* Limit alignment adjustment to \xleftequilibrium

* generate screenshots for reaction arrows
2018-02-04 15:52:22 -05:00
Ron Kok
ea3b6abe32 Enable spacing functions in text mode (#1139)
* Enable spacing functions in text mode

Spacing functions such as `\;` or `\quad` should be accepted in either math mode or text mode

* Add tests

* Fix tests

* Fix lint error
2018-02-04 13:58:04 -05:00
Kevin Barabash
603f12df8d add support for unicode angle brackets, single vertical bar, double vertical bar (#1123)
* add support for unicode angle brackets

* add tests, remove vertical bar changes

* fix 'message not a function' errors
2018-01-31 20:03:16 -05:00
Kevin Barabash
4bec90be0c extract font functions and implicit color function (#1119)
* extract font functions and implicit color function

* remove oldFontFuncs from Parser.js

* allow old font commands in text mode
2018-01-31 10:03:30 -05:00
Kevin Barabash
f80d0afee8 extract sizing and styling implicit functions (#1116)
* extract sizing and styling implicit functions

* add BreakToken type
2018-01-30 07:13:01 -05:00
Kevin Barabash
03c5ce7f87 add perf tests, fixes #1053 (#1054) 2018-01-29 20:38:14 -05:00
Ryan Randall
097d592b47 Merge branch 'master' into bolditalic 2018-01-28 14:54:35 -05:00
Kevin Barabash
c627de04d9 use correct spacing with tight styles (#1106) 2018-01-27 22:12:25 -05:00
Kevin Barabash
ea7a560191 add Main-BoldItalic font to allow nesting of \textit and \textbf 2018-01-27 11:32:14 -05:00
Kevin Barabash
f03849c831 update fonts and metrics so that accents are positioned correctly (#1094)
* update fonts and metrics so that accents are positioned correctly

* fix degree symbol, move more accents into the 0x2c0-0x2df range

* fix accents in Unicode screenshots

* update jest snapshots, update AccentsText to include requested characters

* update fonts submodule, add unicode chars to AccentsText test

* update submodules after merging its PR
2018-01-26 19:46:57 -05:00
David Flanagan
53203ed92c Add a screenshot test to ensure that we don't inherit svg-only CSS properties (#1092)
We want to inherit CSS properties that apply to both text and SVG
but we don't want to inherit styles that affect SVG without affecting text.

This patch adds a screenshot test to verify this.
2018-01-25 14:02:06 -05:00
Kevin Barabash
63f541b6e6 Use JS for spacing between atoms instead of CSS (#1070)
* Use JS for spacing between atoms instead of CSS

Summary:
This is the first step towards creating an intermediate representation
that can be used to generate HTML, SVG, and Canvas commands for rendering.
By generating spans that contain the width of the spaces instead of
relying on CSS sibling rules we'll be able to one day replaces the spans
with intermeidate 'Glue' nodes (in a later PR).

An added benefit of this approach is that is enables us to programmatically
change the values for thinspace, mediumspace, and thickspace which will
allow us to implement the \setlength command.

Test Plan:
- npm test
- dockers/Screenshotter/screenshotter.sh --verify

* fixed failures in BinCancellation, BoldSymbol, and OperatorName

* update screenshots

* don't use current size when determining size of spaces, update more screenshots

* fix spacing in SizingBaseline and StyleSwitching

* actually do the right thing for sizing groups

* fix \not for Chrome and Firefox

* do TODOs

* address feedback from the code review

* fix issue in delimsizing.js

* add TODO to think about a better solution in href.js

* fix typos, simplify href, be honest about paddingLeft for \not
2018-01-24 23:03:36 -05:00