auto-render-spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**
  2. * @jest-environment jsdom
  3. */
  4. import splitAtDelimiters from "../splitAtDelimiters";
  5. import renderMathInElement from "../auto-render";
  6. import type {
  7. DelimiterSpec,
  8. SplitAtDelimiterData,
  9. } from "../splitAtDelimiters";
  10. beforeEach(function() {
  11. expect.extend({
  12. toSplitInto: function(
  13. actual: string,
  14. result: SplitAtDelimiterData[],
  15. delimiters: DelimiterSpec[],
  16. ) {
  17. const message = {
  18. pass: true,
  19. message: () => "'" + actual + "' split correctly",
  20. };
  21. const split =
  22. splitAtDelimiters(actual, delimiters);
  23. if (split.length !== result.length) {
  24. message.pass = false;
  25. message.message = () => "Different number of splits: " +
  26. split.length + " vs. " + result.length + " (" +
  27. JSON.stringify(split) + " vs. " +
  28. JSON.stringify(result) + ")";
  29. return message;
  30. }
  31. for (let i = 0; i < split.length; i++) {
  32. const real = split[i];
  33. const correct = result[i];
  34. let good = true;
  35. let diff = "";
  36. if (real.type !== correct.type) {
  37. good = false;
  38. diff = "type";
  39. } else if (real.data !== correct.data) {
  40. good = false;
  41. diff = "data";
  42. } else if (real.display !== correct.display) {
  43. good = false;
  44. diff = "display";
  45. }
  46. if (!good) {
  47. message.pass = false;
  48. message.message = () => "Difference at split " +
  49. (i + 1) + ": " + JSON.stringify(real) +
  50. " vs. " + JSON.stringify(correct) +
  51. " (" + diff + " differs)";
  52. break;
  53. }
  54. }
  55. return message;
  56. },
  57. });
  58. });
  59. describe("A delimiter splitter", function() {
  60. it("doesn't split when there are no delimiters", function() {
  61. expect("hello").toSplitInto(
  62. [
  63. {type: "text", data: "hello"},
  64. ],
  65. [
  66. {left: "(", right: ")", display: false},
  67. ]);
  68. });
  69. it("doesn't create a math node with only one left delimiter", function() {
  70. expect("hello ( world").toSplitInto(
  71. [
  72. {type: "text", data: "hello "},
  73. {type: "text", data: "( world"},
  74. ],
  75. [
  76. {left: "(", right: ")", display: false},
  77. ]);
  78. });
  79. it("doesn't split when there's only a right delimiter", function() {
  80. expect("hello ) world").toSplitInto(
  81. [
  82. {type: "text", data: "hello ) world"},
  83. ],
  84. [
  85. {left: "(", right: ")", display: false},
  86. ]);
  87. });
  88. it("splits when there are both delimiters", function() {
  89. expect("hello ( world ) boo").toSplitInto(
  90. [
  91. {type: "text", data: "hello "},
  92. {type: "math", data: " world ",
  93. rawData: "( world )", display: false},
  94. {type: "text", data: " boo"},
  95. ],
  96. [
  97. {left: "(", right: ")", display: false},
  98. ]);
  99. });
  100. it("splits on multi-character delimiters", function() {
  101. expect("hello [[ world ]] boo").toSplitInto(
  102. [
  103. {type: "text", data: "hello "},
  104. {type: "math", data: " world ",
  105. rawData: "[[ world ]]", display: false},
  106. {type: "text", data: " boo"},
  107. ],
  108. [
  109. {left: "[[", right: "]]", display: false},
  110. ]);
  111. expect("hello \\begin{equation} world \\end{equation} boo").toSplitInto(
  112. [
  113. {type: "text", data: "hello "},
  114. {type: "math", data: "\\begin{equation} world \\end{equation}",
  115. rawData: "\\begin{equation} world \\end{equation}",
  116. display: false},
  117. {type: "text", data: " boo"},
  118. ],
  119. [
  120. {left: "\\begin{equation}", right: "\\end{equation}",
  121. display: false},
  122. ]);
  123. });
  124. it("splits multiple times", function() {
  125. expect("hello ( world ) boo ( more ) stuff").toSplitInto(
  126. [
  127. {type: "text", data: "hello "},
  128. {type: "math", data: " world ",
  129. rawData: "( world )", display: false},
  130. {type: "text", data: " boo "},
  131. {type: "math", data: " more ",
  132. rawData: "( more )", display: false},
  133. {type: "text", data: " stuff"},
  134. ],
  135. [
  136. {left: "(", right: ")", display: false},
  137. ]);
  138. });
  139. it("leaves the ending when there's only a left delimiter", function() {
  140. expect("hello ( world ) boo ( left").toSplitInto(
  141. [
  142. {type: "text", data: "hello "},
  143. {type: "math", data: " world ",
  144. rawData: "( world )", display: false},
  145. {type: "text", data: " boo "},
  146. {type: "text", data: "( left"},
  147. ],
  148. [
  149. {left: "(", right: ")", display: false},
  150. ]);
  151. });
  152. it("doesn't split when close delimiters are in {}s", function() {
  153. expect("hello ( world { ) } ) boo").toSplitInto(
  154. [
  155. {type: "text", data: "hello "},
  156. {type: "math", data: " world { ) } ",
  157. rawData: "( world { ) } )", display: false},
  158. {type: "text", data: " boo"},
  159. ],
  160. [
  161. {left: "(", right: ")", display: false},
  162. ]);
  163. expect("hello ( world { { } ) } ) boo").toSplitInto(
  164. [
  165. {type: "text", data: "hello "},
  166. {type: "math", data: " world { { } ) } ",
  167. rawData: "( world { { } ) } )", display: false},
  168. {type: "text", data: " boo"},
  169. ],
  170. [
  171. {left: "(", right: ")", display: false},
  172. ]);
  173. });
  174. it("correctly processes sequences of $..$", function() {
  175. expect("$hello$$world$$boo$").toSplitInto(
  176. [
  177. {type: "math", data: "hello",
  178. rawData: "$hello$", display: false},
  179. {type: "math", data: "world",
  180. rawData: "$world$", display: false},
  181. {type: "math", data: "boo",
  182. rawData: "$boo$", display: false},
  183. ],
  184. [
  185. {left: "$", right: "$", display: false},
  186. ]);
  187. });
  188. it("doesn't split at escaped delimiters", function() {
  189. expect("hello ( world \\) ) boo").toSplitInto(
  190. [
  191. {type: "text", data: "hello "},
  192. {type: "math", data: " world \\) ",
  193. rawData: "( world \\) )", display: false},
  194. {type: "text", data: " boo"},
  195. ],
  196. [
  197. {left: "(", right: ")", display: false},
  198. ]);
  199. /* TODO(emily): make this work maybe?
  200. expect("hello \\( ( world ) boo").toSplitInto(
  201. "(", ")",
  202. [
  203. {type: "text", data: "hello \\( "},
  204. {type: "math", data: " world ",
  205. rawData: "( world )", display: false},
  206. {type: "text", data: " boo"},
  207. ]);
  208. */
  209. });
  210. it("splits when the right and left delimiters are the same", function() {
  211. expect("hello $ world $ boo").toSplitInto(
  212. [
  213. {type: "text", data: "hello "},
  214. {type: "math", data: " world ",
  215. rawData: "$ world $", display: false},
  216. {type: "text", data: " boo"},
  217. ],
  218. [
  219. {left: "$", right: "$", display: false},
  220. ]);
  221. });
  222. it("ignores \\$", function() {
  223. expect("$x = \\$5$").toSplitInto(
  224. [
  225. {type: "math", data: "x = \\$5",
  226. rawData: "$x = \\$5$", display: false},
  227. ],
  228. [
  229. {left: "$", right: "$", display: false},
  230. ]);
  231. });
  232. it("remembers which delimiters are display-mode", function() {
  233. const startData = "hello ( world ) boo";
  234. expect(splitAtDelimiters(startData,
  235. [{left:"(", right:")", display:true}])).toEqual(
  236. [
  237. {type: "text", data: "hello "},
  238. {type: "math", data: " world ",
  239. rawData: "( world )", display: true},
  240. {type: "text", data: " boo"},
  241. ]);
  242. });
  243. it("handles nested delimiters irrespective of order", function() {
  244. expect(splitAtDelimiters("$\\fbox{\\(hi\\)}$",
  245. [
  246. {left:"\\(", right:"\\)", display:false},
  247. {left:"$", right:"$", display:false},
  248. ])).toEqual(
  249. [
  250. {type: "math", data: "\\fbox{\\(hi\\)}",
  251. rawData: "$\\fbox{\\(hi\\)}$", display: false},
  252. ]);
  253. expect(splitAtDelimiters("\\(\\fbox{$hi$}\\)",
  254. [
  255. {left:"\\(", right:"\\)", display:false},
  256. {left:"$", right:"$", display:false},
  257. ])).toEqual(
  258. [
  259. {type: "math", data: "\\fbox{$hi$}",
  260. rawData: "\\(\\fbox{$hi$}\\)", display: false},
  261. ]);
  262. });
  263. it("handles a mix of $ and $$", function() {
  264. expect(splitAtDelimiters("$hello$world$$boo$$",
  265. [
  266. {left:"$$", right:"$$", display:true},
  267. {left:"$", right:"$", display:false},
  268. ])).toEqual(
  269. [
  270. {type: "math", data: "hello",
  271. rawData: "$hello$", display: false},
  272. {type: "text", data: "world"},
  273. {type: "math", data: "boo",
  274. rawData: "$$boo$$", display: true},
  275. ]);
  276. expect(splitAtDelimiters("$hello$$world$$$boo$$",
  277. [
  278. {left:"$$", right:"$$", display:true},
  279. {left:"$", right:"$", display:false},
  280. ])).toEqual(
  281. [
  282. {type: "math", data: "hello",
  283. rawData: "$hello$", display: false},
  284. {type: "math", data: "world",
  285. rawData: "$world$", display: false},
  286. {type: "math", data: "boo",
  287. rawData: "$$boo$$", display: true},
  288. ]);
  289. });
  290. });
  291. describe("Pre-process callback", function() {
  292. it("replace `-squared` with `^2 `", function() {
  293. const el1 = document.createElement('div');
  294. el1.textContent = 'Circle equation: $x-squared + y-squared = r-squared$.';
  295. const el2 = document.createElement('div');
  296. el2.textContent = 'Circle equation: $x^2 + y^2 = r^2$.';
  297. const delimiters = [{left: "$", right: "$", display: false}];
  298. renderMathInElement(el1, {
  299. delimiters,
  300. preProcess: math => math.replace(/-squared/g, '^2'),
  301. });
  302. renderMathInElement(el2, {delimiters});
  303. expect(el1.innerHTML).toEqual(el2.innerHTML);
  304. });
  305. });
  306. describe("Parse adjacent text nodes", function() {
  307. it("parse adjacent text nodes with math", function() {
  308. const textNodes = ['\\[',
  309. 'x^2 + y^2 = r^2',
  310. '\\]'];
  311. const el = document.createElement('div');
  312. for (let i = 0; i < textNodes.length; i++) {
  313. const txt = document.createTextNode(textNodes[i]);
  314. el.appendChild(txt);
  315. }
  316. const el2 = document.createElement('div');
  317. const txt = document.createTextNode(textNodes.join(''));
  318. el2.appendChild(txt);
  319. const delimiters = [{left: "\\[", right: "\\]", display: true}];
  320. renderMathInElement(el, {delimiters});
  321. renderMathInElement(el2, {delimiters});
  322. expect(el).toStrictEqual(el2);
  323. });
  324. it("parse adjacent text nodes without math", function() {
  325. const textNodes = ['Lorem ipsum dolor',
  326. 'sit amet',
  327. 'consectetur adipiscing elit'];
  328. const el = document.createElement('div');
  329. for (let i = 0; i < textNodes.length; i++) {
  330. const txt = document.createTextNode(textNodes[i]);
  331. el.appendChild(txt);
  332. }
  333. const el2 = document.createElement('div');
  334. for (let i = 0; i < textNodes.length; i++) {
  335. const txt = document.createTextNode(textNodes[i]);
  336. el2.appendChild(txt);
  337. }
  338. const delimiters = [{left: "\\[", right: "\\]", display: true}];
  339. renderMathInElement(el, {delimiters});
  340. expect(el).toStrictEqual(el2);
  341. });
  342. });