PHP and Javascript implementations of a simple markdown parser
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

testjs.html 60KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Markdown Unit Tests</title>
  6. <link rel="icon" href="data:;base64,iVBORw0KGgo=">
  7. <style type="text/css">
  8. :root {
  9. font-family: sans-serif;
  10. --color-passed: #090;
  11. --color-failed: #a00;
  12. --color-errored: #a80;
  13. --color-untested: #888;
  14. --color-background: #fff;
  15. --color-text: #000;
  16. --color-secondary: #888;
  17. background-color: var(--color-background);
  18. color: var(--color-text);
  19. }
  20. .testclass {
  21. border: 1px solid var(--color-secondary);
  22. padding: 0.5em 1em;
  23. margin-bottom: 1em;
  24. max-width: 50em;
  25. }
  26. .testclassname {
  27. font-weight: bold;
  28. font-size: 1.25rem;
  29. padding-bottom: 0.25em;
  30. }
  31. .testclassstatus.passed { color: var(--color-passed); }
  32. .testclassstatus.failed { color: var(--color-failed); }
  33. .testclassstatus.errored { color: var(--color-errored); }
  34. .testclassstatus.untested { color: var(--color-untested); }
  35. .testcase {
  36. clear: both;
  37. padding: 0.2em 0;
  38. margin-left: 2em;
  39. }
  40. .testcase {
  41. border-top: 1px solid var(--color-secondary);
  42. }
  43. .testcasename {
  44. font-family: monospace;
  45. }
  46. .testcasestatus {
  47. font-weight: bold;
  48. font-size: 80%;
  49. float: left;
  50. }
  51. .testcasetiming {
  52. float: right;
  53. color: var(--color-secondary);
  54. font-size: 80%;
  55. }
  56. .testcaseresult {
  57. height: 1em;
  58. }
  59. .testcasemessage {
  60. clear: both;
  61. }
  62. .result-untested {
  63. color: var(--color-untested);
  64. }
  65. .result-testing {
  66. color: var(--color-text);
  67. }
  68. .result-passed {
  69. color: var(--color-passed);
  70. }
  71. .result-failed {
  72. color: var(--color-failed);
  73. }
  74. .result-errored {
  75. color: var(--color-errored);
  76. }
  77. @media (prefers-color-scheme: dark) {
  78. :root {
  79. --color-passed: #090;
  80. --color-failed: #a00;
  81. --color-errored: #a80;
  82. --color-untested: #888;
  83. --color-background: #000;
  84. --color-text: #fff;
  85. --color-secondary: #888;
  86. }
  87. }
  88. </style>
  89. <script src="js/markdown.js"></script>
  90. <script src="js/spreadsheet.js"></script>
  91. <!-- Testing infrastructure -->
  92. <script>
  93. /**
  94. * @param {String} text
  95. * @returns {String}
  96. */
  97. function escapeHTML(text) {
  98. return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br/>\n');
  99. }
  100. class ResultType {
  101. static untested = new ResultType('untested');
  102. static testing = new ResultType('testing');
  103. static passed = new ResultType('passed');
  104. static failed = new ResultType('failed');
  105. static errored = new ResultType('errored');
  106. #name;
  107. constructor(name) {
  108. this.#name = name;
  109. }
  110. toString() {
  111. return `${this.constructor.name}.${this.#name}`;
  112. }
  113. }
  114. class FailureError extends Error {
  115. constructor(message=null) {
  116. super(message);
  117. }
  118. }
  119. class BaseTest {
  120. setUp() {}
  121. tearDown() {}
  122. /** @var {TestCaseRunner|null} */
  123. currentRunner = null;
  124. fail(failMessage=null) {
  125. throw new FailureError(failMessage || 'failed');
  126. }
  127. assertTrue(test, failMessage=null) {
  128. if (!test) this.fail(failMessage || `expected true, got ${test}`);
  129. }
  130. assertFalse(test, failMessage=null) {
  131. if (test) this.fail(failMessage || `expected false, got ${test}`);
  132. }
  133. assertEqual(a, b, floatDifferenceRatio=0.000001, failMessage=null) {
  134. if (MDUtils.equal(a, b, floatDifferenceRatio)) return;
  135. const aVal = (typeof a == 'string') ? `"${a}"` : `${a}`;
  136. const bVal = (typeof b == 'string') ? `"${b}"` : `${b}`;
  137. if (aVal.length > 20 || bVal.length > 20) {
  138. this.fail(failMessage || `equality failed:\n${aVal}\n!=\n${bVal}`);
  139. } else {
  140. this.fail(failMessage || `equality failed: ${aVal} != ${bVal}`);
  141. }
  142. }
  143. expectError(e=true) {
  144. if (this.currentRunner) this.currentRunner.expectedError = e;
  145. }
  146. /**
  147. * @param {number} maxTime maximum time in seconds
  148. * @param {function} timedCode code to run and time
  149. */
  150. profile(maxTime, timedCode) {
  151. const startTime = performance.now();
  152. const result = timedCode();
  153. const endTime = performance.now();
  154. const seconds = (endTime - startTime) / 1000.0;
  155. if (seconds > maxTime) {
  156. this.fail(`Expected <= ${maxTime}s execution time, actual ${seconds}s.`);
  157. }
  158. return result;
  159. }
  160. }
  161. /**
  162. * Manages the running and results of a single test method on a test
  163. * class.
  164. */
  165. class TestCaseRunner {
  166. /** @var {number} */
  167. static #nextUniqueId = 1;
  168. /** @var {number} */
  169. uniqueId = 0;
  170. /** @var {BaseTest} */
  171. #objectUnderTest;
  172. /** @var {method} */
  173. #method;
  174. /** @var {ResultType} */
  175. result = ResultType.untested;
  176. /** @var {String|null} */
  177. message = null;
  178. expectedError = null;
  179. duration = null;
  180. /** @var {String} */
  181. get className() { return this.#objectUnderTest.constructor.name; }
  182. /** @var {String} */
  183. get methodName() { return this.#method.name; }
  184. /**
  185. * @param {BaseTest} objectUnderTest
  186. * @param {method} method
  187. */
  188. constructor(objectUnderTest, method) {
  189. this.#objectUnderTest = objectUnderTest;
  190. this.#method = method;
  191. this.uniqueId = TestCaseRunner.#nextUniqueId++;
  192. }
  193. run() {
  194. var start;
  195. this.expectedError = null;
  196. this.#objectUnderTest.currentRunner = this;
  197. try {
  198. this.#objectUnderTest.setUp();
  199. } catch (e) {
  200. console.error(`Failed to run ${this.className}.setUp() - ${e.message}`);
  201. this.result = ResultType.errored;
  202. this.message = e.message;
  203. return;
  204. }
  205. try {
  206. start = performance.now();
  207. this.#method.bind(this.#objectUnderTest)();
  208. this.duration = performance.now() - start;
  209. this.result = ResultType.passed;
  210. this.message = null;
  211. } catch (e) {
  212. this.duration = performance.now() - start;
  213. if (e instanceof FailureError) {
  214. this.result = ResultType.failed;
  215. this.message = e.message;
  216. } else if (this.#isExpectedError(e)) {
  217. this.result = ResultType.passed;
  218. this.message = null;
  219. } else {
  220. this.result = ResultType.errored;
  221. this.message = e.message;
  222. if (e.stack !== undefined) {
  223. this.message += "\n" + e.stack;
  224. }
  225. }
  226. } finally {
  227. this.expectedError = null;
  228. try {
  229. this.#objectUnderTest.tearDown();
  230. this.#objectUnderTest.currentRunner = null;
  231. } catch (e0) {
  232. console.error(`Failed to run ${this.className}.tearDown() - ${e0.message}`);
  233. this.result = ResultType.errored;
  234. this.message = e0.message;
  235. }
  236. }
  237. }
  238. get #cssId() { return `testcase${this.uniqueId}`; }
  239. #isExpectedError(e) {
  240. if (this.expectedError === null) return false;
  241. if (this.expectedError === true) return true;
  242. // TODO: Have a way to specify details about what kind of error is expected. Maybe a prototype instance and/or a testing lambda.
  243. return false;
  244. }
  245. toHTML() {
  246. var html = `<div class="testcase" id="${this.#cssId}">`;
  247. html += `<div class="testcasename"><span class="testcasemethod">${this.methodName}</span></div>`;
  248. html += '<div class="testcaseresult">';
  249. switch (this.result) {
  250. case ResultType.untested:
  251. html += '<div class="testcasestatus result-untested">Waiting to test</div>';
  252. break;
  253. case ResultType.testing:
  254. html += '<div class="testcasestatus result-testing">Testing...</div>';
  255. break;
  256. case ResultType.passed:
  257. html += '<div class="testcasestatus result-passed">Passed</div>';
  258. break;
  259. case ResultType.failed:
  260. html += '<div class="testcasestatus result-failed">Failed</div>';
  261. break;
  262. case ResultType.errored:
  263. html += '<div class="testcasestatus result-errored">Errored</div>';
  264. break;
  265. }
  266. if (this.duration !== null) {
  267. html += `<div class="testcasetiming">${Number(this.duration / 1000.0).toFixed(3)}s</div>`;
  268. }
  269. html += '</div>';
  270. if (this.message !== null) {
  271. html += `<div class="testcasemessage">${escapeHTML(this.message)}</div>`;
  272. }
  273. html += `</div>`;
  274. return html;
  275. }
  276. /**
  277. * Updates the HTML node in-place with the current status, or
  278. * adds it if it does not exist yet.
  279. */
  280. updateHTML() {
  281. let existing = document.getElementById(this.#cssId);
  282. if (existing) {
  283. existing.outerHTML = this.toHTML();
  284. } else {
  285. document.getElementById('results').innerHTML += this.toHTML();
  286. }
  287. }
  288. /**
  289. * @param {object} objectUnderTest
  290. * @returns {TestCaseRunner[]}
  291. */
  292. static findTestCases(objectUnderTest) {
  293. if (!(objectUnderTest instanceof BaseTest)) return [];
  294. var members = [];
  295. var obj = objectUnderTest;
  296. do {
  297. members.push(...Object.getOwnPropertyNames(obj));
  298. } while (obj = Object.getPrototypeOf(obj));
  299. return members.sort().filter((e, i, arr) => {
  300. if (e != arr[i + 1] && typeof objectUnderTest[e] == 'function' && e.startsWith('test')) return true;
  301. }).map((name) => {
  302. return new TestCaseRunner(objectUnderTest, objectUnderTest[name]);
  303. });
  304. }
  305. }
  306. class TestClassRunner {
  307. static #nextUniqueId = 1;
  308. #uniqueId = 0;
  309. #theClass;
  310. #instance;
  311. #testCases;
  312. get testCases() { return this.#testCases; }
  313. constructor(theClass) {
  314. this.#theClass = theClass;
  315. this.#instance = new theClass();
  316. this.#testCases = TestCaseRunner.findTestCases(this.#instance);
  317. this.#uniqueId = TestClassRunner.#nextUniqueId++;
  318. }
  319. get #cssId() { return `testclass${this.#uniqueId}`; }
  320. #summaryHTML() {
  321. var anyTesting = false;
  322. var anyFailed = false;
  323. var anyErrored = false;
  324. var anyUntested = false;
  325. var anyPassed = false;
  326. var allPassed = true;
  327. for (const test of this.testCases) {
  328. switch (test.result) {
  329. case ResultType.untested:
  330. anyUntested = true;
  331. allPassed = false;
  332. break;
  333. case ResultType.testing:
  334. anyTesting = true;
  335. allPassed = false;
  336. break;
  337. case ResultType.passed:
  338. anyPassed = true;
  339. break;
  340. case ResultType.failed:
  341. anyFailed = true;
  342. allPassed = false;
  343. break;
  344. case ResultType.errored:
  345. anyErrored = true;
  346. allPassed = false;
  347. break;
  348. }
  349. }
  350. var html = '';
  351. html += `<summary class="testclasssummary" id="${this.#cssId}summary">`;
  352. html += `<span class="testclassname">${this.#theClass.name}</span> `;
  353. if (anyTesting || (anyUntested && (anyPassed || anyFailed || anyErrored))) {
  354. html += '<span class="testclassstatus testing">Testing...</span>';
  355. } else if (anyErrored) {
  356. html += '<span class="testclassstatus errored">Errored</span>';
  357. } else if (anyFailed) {
  358. html += '<span class="testclassstatus failed">Failed</span>';
  359. } else if (allPassed) {
  360. html += '<span class="testclassstatus passed">Passed</span>';
  361. }
  362. html += '</summary>';
  363. return html;
  364. }
  365. toHTML() {
  366. var html = '';
  367. html += `<div class="testclass" id="${this.#cssId}">`;
  368. html += `<details id="${this.#cssId}details">`;
  369. html += this.#summaryHTML();
  370. for (const testCase of this.#testCases) {
  371. html += testCase.toHTML();
  372. }
  373. html += '</details>';
  374. html += '</div>';
  375. return html;
  376. }
  377. updateHTML() {
  378. var existing = document.getElementById(`${this.#cssId}summary`);
  379. if (!existing) {
  380. document.getElementById('results').innerHTML += this.toHTML();
  381. } else {
  382. existing.outerHTML = this.#summaryHTML();
  383. var allPassed = true;
  384. for (const test of this.testCases) {
  385. if (test.result != ResultType.passed) {
  386. allPassed = false;
  387. break;
  388. }
  389. }
  390. document.getElementById(`${this.#cssId}details`).open = !allPassed;
  391. }
  392. }
  393. static runAll(testClasses) {
  394. var tests = []; // tuples of TestClassRunner and TestCaseRunner
  395. for (const testClass of testClasses) {
  396. const classRunner = new TestClassRunner(testClass);
  397. classRunner.updateHTML();
  398. tests.push(...classRunner.testCases.map(function(test) { return [ classRunner, test ] }));
  399. }
  400. var testInterval = setInterval(function() {
  401. if (tests.length == 0) {
  402. clearInterval(testInterval);
  403. testInterval = null;
  404. return;
  405. }
  406. var classRunner;
  407. var testCase;
  408. [ classRunner, testCase ] = tests[0];
  409. if (testCase.result == ResultType.untested) {
  410. testCase.result = ResultType.testing;
  411. testCase.updateHTML();
  412. classRunner.updateHTML();
  413. } else if (testCase.result == ResultType.testing) {
  414. tests.splice(0, 1);
  415. testCase.run();
  416. testCase.updateHTML();
  417. classRunner.updateHTML();
  418. }
  419. }, 1);
  420. }
  421. }
  422. </script>
  423. <!-- Tests -->
  424. <script>
  425. function onLoad() {
  426. let testClasses = [
  427. TokenTests,
  428. UtilsTests,
  429. InlineTests,
  430. BlockTests,
  431. CellValueTests,
  432. CellAddressRangeTests,
  433. ExpressionSetTests,
  434. SpreadsheetMarkdownIntegrationTests,
  435. ];
  436. TestClassRunner.runAll(testClasses);
  437. }
  438. document.addEventListener('DOMContentLoaded', onLoad);
  439. function normalizeWhitespace(str) {
  440. return str.replace(/\s+/g, ' ').replace(/(?:^\s+|\s+$)/g, '');
  441. }
  442. class TokenTests extends BaseTest {
  443. test_findFirstTokens() {
  444. const tokens = [
  445. new MDToken('Lorem', MDTokenType.Text),
  446. new MDToken(' ', MDTokenType.Whitespace),
  447. new MDToken('_', MDTokenType.Underscore),
  448. new MDToken('ipsum', MDTokenType.Text),
  449. new MDToken('_', MDTokenType.Underscore),
  450. new MDToken(' ', MDTokenType.Whitespace),
  451. new MDToken('dolor', MDTokenType.Text),
  452. new MDToken('_', MDTokenType.Underscore),
  453. new MDToken('sit', MDTokenType.Text),
  454. new MDToken('_', MDTokenType.Underscore),
  455. ];
  456. const pattern = [
  457. MDTokenType.Underscore,
  458. ];
  459. const result = MDToken.findFirstTokens(tokens, pattern);
  460. const expected = {
  461. tokens: [ tokens[2] ],
  462. index: 2,
  463. };
  464. this.assertEqual(result, expected);
  465. }
  466. test_findFirstTokens_optionalWhitespace1() {
  467. const tokens = [
  468. new MDToken('Lorem', MDTokenType.Text),
  469. new MDToken(' ', MDTokenType.Whitespace),
  470. new MDToken('[ipsum]', MDTokenType.Label, 'ipsum'),
  471. new MDToken('(link.html)', MDTokenType.URL, 'link.html'),
  472. new MDToken(' ', MDTokenType.Whitespace),
  473. new MDToken('dolor', MDTokenType.Text),
  474. ];
  475. const pattern = [
  476. MDTokenType.Label,
  477. MDTokenType.META_OptionalWhitespace,
  478. MDTokenType.URL,
  479. ];
  480. const result = MDToken.findFirstTokens(tokens, pattern);
  481. const expected = {
  482. tokens: [ tokens[2], tokens[3] ],
  483. index: 2,
  484. };
  485. this.assertEqual(result, expected);
  486. }
  487. test_findFirstTokens_optionalWhitespace2() {
  488. const tokens = [
  489. new MDToken('Lorem', MDTokenType.Text),
  490. new MDToken(' ', MDTokenType.Whitespace),
  491. new MDToken('[ipsum]', MDTokenType.Label, 'ipsum'),
  492. new MDToken(' ', MDTokenType.Whitespace),
  493. new MDToken('(link.html)', MDTokenType.URL, 'link.html'),
  494. new MDToken(' ', MDTokenType.Whitespace),
  495. new MDToken('dolor', MDTokenType.Text),
  496. ];
  497. const pattern = [
  498. MDTokenType.Label,
  499. MDTokenType.META_OptionalWhitespace,
  500. MDTokenType.URL,
  501. ];
  502. const result = MDToken.findFirstTokens(tokens, pattern);
  503. const expected = {
  504. tokens: [ tokens[2], tokens[3], tokens[4] ],
  505. index: 2,
  506. };
  507. this.assertEqual(result, expected);
  508. }
  509. test_findPairedTokens() {
  510. const tokens = [
  511. new MDToken('Lorem', MDTokenType.Text),
  512. new MDToken(' ', MDTokenType.Whitespace),
  513. new MDToken('_', MDTokenType.Underscore),
  514. new MDToken('ipsum', MDTokenType.Text),
  515. new MDToken('_', MDTokenType.Underscore),
  516. new MDToken(' ', MDTokenType.Whitespace),
  517. new MDToken('dolor', MDTokenType.Text),
  518. new MDToken('_', MDTokenType.Underscore),
  519. new MDToken('sit', MDTokenType.Text),
  520. new MDToken('_', MDTokenType.Underscore),
  521. ];
  522. const pattern = [
  523. MDTokenType.Underscore,
  524. ];
  525. const result = MDToken.findPairedTokens(tokens, pattern, pattern);
  526. const expected = {
  527. startTokens: [ tokens[2] ],
  528. contentTokens: [ tokens[3] ],
  529. endTokens: [ tokens[4] ],
  530. startIndex: 2,
  531. contentIndex: 3,
  532. endIndex: 4,
  533. totalLength: 3,
  534. }
  535. this.assertEqual(result, expected);
  536. }
  537. }
  538. class UtilsTests extends BaseTest {
  539. test_stripIndent() {
  540. this.assertEqual(MDUtils.stripIndent(''), '');
  541. this.assertEqual(MDUtils.stripIndent(' '), '');
  542. this.assertEqual(MDUtils.stripIndent('foo'), 'foo');
  543. this.assertEqual(MDUtils.stripIndent(' foo'), 'foo');
  544. this.assertEqual(MDUtils.stripIndent(' foo'), 'foo');
  545. this.assertEqual(MDUtils.stripIndent(' foo'), 'foo');
  546. this.assertEqual(MDUtils.stripIndent(' foo'), 'foo');
  547. this.assertEqual(MDUtils.stripIndent(' foo'), ' foo');
  548. this.assertEqual(MDUtils.stripIndent('\tfoo'), 'foo');
  549. this.assertEqual(MDUtils.stripIndent('\t\tfoo'), '\tfoo');
  550. this.assertEqual(MDUtils.stripIndent('\t\tfoo', 2), 'foo');
  551. this.assertEqual(MDUtils.stripIndent(' foo', 2), 'foo');
  552. }
  553. test_countIndents() {
  554. this.assertEqual(MDUtils.countIndents(''), 0);
  555. this.assertEqual(MDUtils.countIndents(' '), 1);
  556. this.assertEqual(MDUtils.countIndents(' '), 1);
  557. this.assertEqual(MDUtils.countIndents('foo'), 0);
  558. this.assertEqual(MDUtils.countIndents('foo'), 0);
  559. this.assertEqual(MDUtils.countIndents(' foo'), 1);
  560. this.assertEqual(MDUtils.countIndents(' foo'), 1);
  561. this.assertEqual(MDUtils.countIndents(' foo'), 1);
  562. this.assertEqual(MDUtils.countIndents(' foo'), 1);
  563. this.assertEqual(MDUtils.countIndents(' foo'), 2);
  564. this.assertEqual(MDUtils.countIndents('\tfoo'), 1);
  565. this.assertEqual(MDUtils.countIndents('\t\tfoo'), 2);
  566. this.assertEqual(MDUtils.countIndents('', true), 0);
  567. this.assertEqual(MDUtils.countIndents(' ', true), 0);
  568. this.assertEqual(MDUtils.countIndents(' ', true), 1);
  569. this.assertEqual(MDUtils.countIndents('foo', true), 0);
  570. this.assertEqual(MDUtils.countIndents(' foo', true), 0);
  571. this.assertEqual(MDUtils.countIndents(' foo', true), 0);
  572. this.assertEqual(MDUtils.countIndents(' foo', true), 0);
  573. this.assertEqual(MDUtils.countIndents(' foo', true), 1);
  574. this.assertEqual(MDUtils.countIndents(' foo', true), 1);
  575. this.assertEqual(MDUtils.countIndents('\tfoo', true), 1);
  576. this.assertEqual(MDUtils.countIndents('\t\tfoo', true), 2);
  577. }
  578. test_tokenizeLabel() {
  579. // Escapes are preserved
  580. this.assertEqual(MDUtils.tokenizeLabel('[foo] bar'), [ '[foo]', 'foo' ]);
  581. this.assertEqual(MDUtils.tokenizeLabel('[foo\\[] bar'), [ '[foo\\[]', 'foo\\[' ]);
  582. this.assertEqual(MDUtils.tokenizeLabel('[foo\\]] bar'), [ '[foo\\]]', 'foo\\]' ]);
  583. this.assertEqual(MDUtils.tokenizeLabel('[foo[]] bar'), [ '[foo[]]', 'foo[]' ]);
  584. this.assertEqual(MDUtils.tokenizeLabel('[foo\\(] bar'), [ '[foo\\(]', 'foo\\(' ]);
  585. this.assertEqual(MDUtils.tokenizeLabel('[foo\\)] bar'), [ '[foo\\)]', 'foo\\)' ]);
  586. this.assertEqual(MDUtils.tokenizeLabel('[foo()] bar'), [ '[foo()]', 'foo()' ]);
  587. this.assertEqual(MDUtils.tokenizeLabel('foo bar'), null);
  588. this.assertEqual(MDUtils.tokenizeLabel('[foo\\] bar'), null);
  589. this.assertEqual(MDUtils.tokenizeLabel('[foo bar'), null);
  590. this.assertEqual(MDUtils.tokenizeLabel('[foo[] bar'), null);
  591. }
  592. test_tokenizeURL() {
  593. this.assertEqual(MDUtils.tokenizeURL('(page.html) foo'), [ '(page.html)', 'page.html', null ]);
  594. this.assertEqual(MDUtils.tokenizeURL('(page.html "link title") foo'), [ '(page.html "link title")', 'page.html', 'link title' ]);
  595. this.assertEqual(MDUtils.tokenizeURL('(https://example.com/path/page.html?query=foo&bar=baz#fragment) foo'), [ '(https://example.com/path/page.html?query=foo&bar=baz#fragment)', 'https://example.com/path/page.html?query=foo&bar=baz#fragment', null ]);
  596. this.assertEqual(MDUtils.tokenizeURL('page.html foo'), null);
  597. this.assertEqual(MDUtils.tokenizeURL('(page.html foo'), null);
  598. this.assertEqual(MDUtils.tokenizeURL('page.html) foo'), null);
  599. this.assertEqual(MDUtils.tokenizeURL('(page.html "title) foo'), null);
  600. this.assertEqual(MDUtils.tokenizeURL('(page .html) foo'), null);
  601. this.assertEqual(MDUtils.tokenizeURL('(user@example.com) foo'), null);
  602. this.assertEqual(MDUtils.tokenizeURL('(user@example.com "title") foo'), null);
  603. }
  604. test_tokenizeEmail() {
  605. this.assertEqual(MDUtils.tokenizeEmail('(user@example.com)'), [ '(user@example.com)', 'user@example.com', null ]);
  606. this.assertEqual(MDUtils.tokenizeEmail('(user@example.com "link title")'), [ '(user@example.com "link title")', 'user@example.com', 'link title' ]);
  607. this.assertEqual(MDUtils.tokenizeEmail('(https://example.com) foo'), null);
  608. this.assertEqual(MDUtils.tokenizeEmail('(https://example.com "link title") foo'), null);
  609. this.assertEqual(MDUtils.tokenizeEmail('(user@example.com "link title) foo'), null);
  610. this.assertEqual(MDUtils.tokenizeEmail('(user@example.com foo'), null);
  611. this.assertEqual(MDUtils.tokenizeEmail('user@example.com) foo'), null);
  612. }
  613. }
  614. class InlineTests extends BaseTest {
  615. /** @type {Markdown} */
  616. parser;
  617. md(markdown) {
  618. return normalizeWhitespace(this.parser.toHTML(markdown));
  619. }
  620. setUp() {
  621. this.parser = Markdown.completeParser;
  622. }
  623. test_simpleText() {
  624. let markdown = 'Lorem ipsum';
  625. let expected = 'Lorem ipsum';
  626. let actual = this.md(markdown);
  627. this.assertEqual(actual, expected);
  628. }
  629. test_strong() {
  630. let markdown = 'Lorem **ipsum** dolor **sit**';
  631. let expected = 'Lorem <strong>ipsum</strong> dolor <strong>sit</strong>';
  632. let actual = this.md(markdown);
  633. this.assertEqual(actual, expected);
  634. }
  635. test_emphasis() {
  636. let markdown = 'Lorem _ipsum_ dolor _sit_';
  637. let expected = 'Lorem <em>ipsum</em> dolor <em>sit</em>';
  638. let actual = this.md(markdown);
  639. this.assertEqual(actual, expected);
  640. }
  641. test_strongEmphasis_cleanNesting1() {
  642. let markdown = 'Lorem **ipsum *dolor* sit** amet';
  643. let expected = 'Lorem <strong>ipsum <em>dolor</em> sit</strong> amet';
  644. let actual = this.md(markdown);
  645. this.assertEqual(actual, expected);
  646. }
  647. test_strongEmphasis_cleanNesting2() {
  648. let markdown = 'Lorem *ipsum **dolor** sit* amet';
  649. let expected = 'Lorem <em>ipsum <strong>dolor</strong> sit</em> amet';
  650. let actual = this.md(markdown);
  651. this.assertEqual(actual, expected);
  652. }
  653. test_strongEmphasis_tightNesting() {
  654. let markdown = 'Lorem ***ipsum*** dolor';
  655. let expected1 = 'Lorem <strong><em>ipsum</em></strong> dolor';
  656. let expected2 = 'Lorem <em><strong>ipsum</strong></em> dolor';
  657. let actual = this.md(markdown);
  658. this.assertTrue(actual == expected1 || actual == expected2);
  659. }
  660. test_strongEmphasis_lopsidedNesting1() {
  661. let markdown = 'Lorem ***ipsum* dolor** sit';
  662. let expected = 'Lorem <strong><em>ipsum</em> dolor</strong> sit';
  663. let actual = this.md(markdown);
  664. this.assertEqual(actual, expected);
  665. }
  666. test_strongEmphasis_lopsidedNesting2() {
  667. let markdown = 'Lorem ***ipsum** dolor* sit';
  668. let expected = 'Lorem <em><strong>ipsum</strong> dolor</em> sit';
  669. let actual = this.md(markdown);
  670. this.assertEqual(actual, expected);
  671. }
  672. test_strongEmphasis_lopsidedNesting3() {
  673. let markdown = 'Lorem **ipsum *dolor*** sit';
  674. let expected = 'Lorem <strong>ipsum <em>dolor</em></strong> sit';
  675. let actual = this.md(markdown);
  676. this.assertEqual(actual, expected);
  677. }
  678. test_strongEmphasis_lopsidedNesting4() {
  679. let markdown = 'Lorem *ipsum **dolor*** sit';
  680. let expected = 'Lorem <em>ipsum <strong>dolor</strong></em> sit';
  681. let actual = this.md(markdown);
  682. this.assertEqual(actual, expected);
  683. }
  684. test_inlineCode() {
  685. let markdown = 'Lorem `ipsum` dolor';
  686. let expected = 'Lorem <code>ipsum</code> dolor';
  687. let actual = this.md(markdown);
  688. this.assertEqual(actual, expected);
  689. }
  690. test_inlineCode_withInnerBacktick() {
  691. let markdown = 'Lorem ``ip`su`m`` dolor';
  692. let expected = 'Lorem <code>ip`su`m</code> dolor';
  693. let actual = this.md(markdown);
  694. this.assertEqual(actual, expected);
  695. }
  696. test_strikethrough_single() {
  697. let markdown = 'Lorem ~ipsum~ dolor';
  698. let expected = 'Lorem <strike>ipsum</strike> dolor';
  699. let actual = this.md(markdown);
  700. this.assertEqual(actual, expected);
  701. }
  702. test_strikethrough_double() {
  703. let markdown = 'Lorem ~~ipsum~~ dolor';
  704. let expected = 'Lorem <strike>ipsum</strike> dolor';
  705. let actual = this.md(markdown);
  706. this.assertEqual(actual, expected);
  707. }
  708. test_link_fullyQualified() {
  709. let markdown = 'Lorem [ipsum](https://example.com/path/page.html) dolor';
  710. let expected = 'Lorem <a href="https://example.com/path/page.html">ipsum</a> dolor';
  711. let actual = this.md(markdown);
  712. this.assertEqual(actual, expected);
  713. }
  714. test_link_relative() {
  715. let markdown = 'Lorem [ipsum](page.html) dolor';
  716. let expected = 'Lorem <a href="page.html">ipsum</a> dolor';
  717. let actual = this.md(markdown);
  718. this.assertEqual(actual, expected);
  719. }
  720. test_link_title() {
  721. let markdown = 'Lorem [ipsum](page.html "link title") dolor';
  722. let expected = 'Lorem <a href="page.html" title="link title">ipsum</a> dolor';
  723. let actual = this.md(markdown);
  724. this.assertEqual(actual, expected);
  725. }
  726. test_link_literal() {
  727. let markdown = 'Lorem <https://example.com> dolor';
  728. let expected = 'Lorem <a href="https://example.com">https://example.com</a> dolor';
  729. let actual = this.md(markdown);
  730. this.assertEqual(actual, expected);
  731. }
  732. test_link_ref() {
  733. let markdown = "Lorem [ipsum][ref] dolor\n\n[ref]: https://example.com";
  734. let expected = '<p>Lorem <a href="https://example.com">ipsum</a> dolor</p>';
  735. let actual = this.md(markdown);
  736. this.assertEqual(actual, expected);
  737. }
  738. test_link_email() {
  739. let markdown = 'Lorem [ipsum](user@example.com) dolor';
  740. let expected = 'Lorem <a href="mailto:&#117;&#115;&#101;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;">ipsum</a> dolor';
  741. let actual = this.md(markdown);
  742. this.assertEqual(actual, expected);
  743. }
  744. test_link_email_withTitle() {
  745. let markdown = 'Lorem [ipsum](user@example.com "title") dolor';
  746. let expected = 'Lorem <a href="mailto:&#117;&#115;&#101;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;" title="title">ipsum</a> dolor';
  747. let actual = this.md(markdown);
  748. this.assertEqual(actual, expected);
  749. }
  750. test_link_literalEmail() {
  751. let markdown = 'Lorem <user@example.com> dolor';
  752. let expected = 'Lorem <a href="mailto:&#117;&#115;&#101;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;">&#117;&#115;&#101;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;</a> dolor';
  753. let actual = this.md(markdown);
  754. this.assertEqual(actual, expected);
  755. }
  756. test_link_image() {
  757. let markdown = 'Lorem [![alt](image.jpg)](page.html) ipsum';
  758. let expected = 'Lorem <a href="page.html"><img src="image.jpg" alt="alt"></a> ipsum';
  759. let actual = this.md(markdown);
  760. this.assertEqual(actual, expected);
  761. }
  762. test_link_image_complex() {
  763. let markdown = 'Lorem [![alt] (image.jpg "image title")] (page.html "link title") ipsum';
  764. let expected = 'Lorem <a href="page.html" title="link title"><img src="image.jpg" alt="alt" title="image title"></a> ipsum';
  765. let actual = this.md(markdown);
  766. this.assertEqual(actual, expected);
  767. }
  768. test_image() {
  769. let markdown = 'Lorem ![alt text](image.jpg) dolor';
  770. let expected = 'Lorem <img src="image.jpg" alt="alt text"> dolor';
  771. let actual = this.md(markdown);
  772. this.assertEqual(actual, expected);
  773. }
  774. test_image_noAlt() {
  775. let markdown = 'Lorem ![](image.jpg) dolor';
  776. let expected = 'Lorem <img src="image.jpg"> dolor';
  777. let actual = this.md(markdown);
  778. this.assertEqual(actual, expected);
  779. }
  780. test_image_withTitle() {
  781. let markdown = 'Lorem ![alt text](image.jpg "image title") dolor';
  782. let expected = 'Lorem <img src="image.jpg" alt="alt text" title="image title"> dolor';
  783. let actual = this.md(markdown);
  784. this.assertEqual(actual, expected);
  785. }
  786. test_image_ref() {
  787. let markdown = 'Lorem ![alt text][ref] dolor\n\n' +
  788. '[ref]: image.jpg "image title"';
  789. let expected = '<p>Lorem <img src="image.jpg" alt="alt text" title="image title"> dolor</p>';
  790. let actual = this.md(markdown);
  791. this.assertEqual(actual, expected);
  792. }
  793. test_htmlTags() {
  794. let markdown = 'Lorem <strong title="value" foo=\'with " quote\' bar="with \' apostrophe" attr=unquoted checked>ipsum</strong> dolor';
  795. let expected = markdown;
  796. let actual = this.md(markdown);
  797. this.assertEqual(actual, expected);
  798. }
  799. }
  800. class BlockTests extends BaseTest {
  801. /** @type {Markdown} */
  802. parser;
  803. md(markdown) {
  804. return normalizeWhitespace(this.parser.toHTML(markdown));
  805. }
  806. setUp() {
  807. this.parser = Markdown.completeParser;
  808. }
  809. test_paragraphs() {
  810. let markdown = "Lorem ipsum\n\nDolor sit amet";
  811. let expected = "<p>Lorem ipsum</p> <p>Dolor sit amet</p>";
  812. let actual = this.md(markdown);
  813. this.assertEqual(actual, expected);
  814. }
  815. test_paragraph_lineGrouping() {
  816. let markdown = "Lorem ipsum\ndolor sit amet";
  817. let expected = "Lorem ipsum dolor sit amet";
  818. let actual = this.md(markdown);
  819. this.assertEqual(actual, expected);
  820. }
  821. test_header_underlineH1() {
  822. let markdown = "Header 1\n===\n\nLorem ipsum";
  823. let expected = "<h1>Header 1</h1> <p>Lorem ipsum</p>";
  824. let actual = this.md(markdown);
  825. this.assertEqual(actual, expected);
  826. }
  827. test_header_underlineH2() {
  828. let markdown = "Header 2\n---\n\nLorem ipsum";
  829. let expected = "<h2>Header 2</h2> <p>Lorem ipsum</p>";
  830. let actual = this.md(markdown);
  831. this.assertEqual(actual, expected);
  832. }
  833. test_header_hash() {
  834. let markdown = "# Header 1\n## Header 2\n### Header 3\n#### Header 4\n##### Header 5\n###### Header 6\n";
  835. let expected = '<h1>Header 1</h1> <h2>Header 2</h2> <h3>Header 3</h3> <h4>Header 4</h4> <h5>Header 5</h5> <h6>Header 6</h6>';
  836. let actual = this.md(markdown);
  837. this.assertEqual(actual, expected);
  838. }
  839. test_header_hash_trailing() {
  840. let markdown = "# Header 1 #\n## Header 2 ##\n### Header 3 ######";
  841. let expected = '<h1>Header 1</h1> <h2>Header 2</h2> <h3>Header 3</h3>';
  842. let actual = this.md(markdown);
  843. this.assertEqual(actual, expected);
  844. }
  845. test_unorderedList() {
  846. let markdown = "* Lorem\n* Ipsum\n* Dolor";
  847. let expected = '<ul> <li>Lorem</li> <li>Ipsum</li> <li>Dolor</li> </ul>';
  848. let actual = this.md(markdown);
  849. this.assertEqual(actual, expected);
  850. }
  851. test_unorderedList_nested() {
  852. let markdown = "* Lorem\n + Ipsum\n* Dolor";
  853. let expected = '<ul> <li>Lorem <ul> <li>Ipsum</li> </ul></li> <li>Dolor</li> </ul>';
  854. let actual = this.md(markdown);
  855. this.assertEqual(actual, expected);
  856. }
  857. test_unorderedList_hitch() {
  858. // This incomplete bulleted list locked up the browser at one
  859. // point, not forever but a REALLY long time
  860. this.profile(1.0, () => {
  861. let markdown = "Testing\n\n* ";
  862. let expected = '<p>Testing</p> <ul> <li></li> </ul>';
  863. let actual = this.md(markdown);
  864. this.assertEqual(actual, expected);
  865. });
  866. }
  867. test_orderedList() {
  868. let markdown = "1. Lorem\n1. Ipsum\n5. Dolor";
  869. let expected = '<ol> <li>Lorem</li> <li>Ipsum</li> <li>Dolor</li> </ol>';
  870. let actual = this.md(markdown);
  871. this.assertEqual(actual, expected);
  872. }
  873. test_orderedList_numbering() {
  874. let markdown = "4. Lorem\n1. Ipsum\n9. Dolor";
  875. let expected = '<ol start="4"> <li>Lorem</li> <li>Ipsum</li> <li>Dolor</li> </ol>';
  876. let actual = this.md(markdown);
  877. this.assertEqual(actual, expected);
  878. }
  879. test_orderedList_nested1() {
  880. let markdown = "1. Lorem\n 1. Ipsum\n1. Dolor";
  881. let expected = '<ol> <li>Lorem <ol> <li>Ipsum</li> </ol></li> <li>Dolor</li> </ol>';
  882. let actual = this.md(markdown);
  883. this.assertEqual(actual, expected);
  884. }
  885. test_orderedList_nested2() {
  886. let markdown = "1. Lorem\n 1. Ipsum\n 1. Dolor\n 1. Sit\n1. Amet";
  887. let expected = '<ol> <li>Lorem <ol> <li>Ipsum <ol> <li>Dolor</li> </ol></li> <li>Sit</li> </ol></li> <li>Amet</li> </ol>';
  888. let actual = this.md(markdown);
  889. this.assertEqual(actual, expected);
  890. }
  891. test_blockquote() {
  892. let markdown = '> Lorem ipsum dolor';
  893. let expected = '<blockquote> Lorem ipsum dolor </blockquote>';
  894. let actual = this.md(markdown);
  895. this.assertEqual(actual, expected);
  896. }
  897. test_blockquote_paragraphs() {
  898. let markdown = '> Lorem ipsum dolor\n>\n>Sit amet';
  899. let expected = '<blockquote> <p>Lorem ipsum dolor</p> <p>Sit amet</p> </blockquote>';
  900. let actual = this.md(markdown);
  901. this.assertEqual(actual, expected);
  902. }
  903. test_blockquote_list() {
  904. let markdown = '> 1. Lorem\n> 2. Ipsum';
  905. let expected = '<blockquote> <ol> <li>Lorem</li> <li>Ipsum</li> </ol> </blockquote>';
  906. let actual = this.md(markdown);
  907. this.assertEqual(actual, expected);
  908. }
  909. test_codeBlock_indented() {
  910. let markdown = "Code\n\n function foo() {\n return 'bar';\n }\n\nend";
  911. let expected = "<p>Code</p>\n\n<pre><code>function foo() {\n return 'bar';\n}</code></pre>\n<p>end</p>\n";
  912. let actual = this.parser.toHTML(markdown); // don't normalize whitespace
  913. this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
  914. }
  915. test_codeBlock_fenced() {
  916. let markdown = "Code\n\n```\nfunction foo() {\n return 'bar';\n}\n```\n\nend";
  917. let expected = "<p>Code</p>\n\n<pre><code>function foo() {\n return 'bar';\n}</code></pre>\n<p>end</p>\n";
  918. let actual = this.parser.toHTML(markdown); // don't normalize whitespace
  919. this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
  920. }
  921. test_horizontalRule() {
  922. let markdown = "Before\n\n---\n\n- - -\n\n***\n\n* * * * * * *\n\nafter";
  923. let expected = "<p>Before</p> <hr> <hr> <hr> <hr> <p>after</p>";
  924. let actual = this.md(markdown);
  925. this.assertEqual(actual, expected);
  926. }
  927. test_table_unfenced() {
  928. let markdown = "Column A | Column B | Column C\n--- | --- | ---\n1 | 2 | 3\n4 | 5 | 6";
  929. let expected = "<table> <thead> <tr> <th>Column A</th> <th>Column B</th> <th>Column C</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>4</td> <td>5</td> <td>6</td> </tr> </tbody> </table>";
  930. let actual = this.md(markdown);
  931. this.assertEqual(actual, expected);
  932. }
  933. test_table_fenced() {
  934. let markdown = "| Column A | Column B | Column C |\n| --- | --- | --- |\n| 1 | 2 | 3\n4 | 5 | 6 |";
  935. let expected = "<table> <thead> <tr> <th>Column A</th> <th>Column B</th> <th>Column C</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>4</td> <td>5</td> <td>6</td> </tr> </tbody> </table>";
  936. let actual = this.md(markdown);
  937. this.assertEqual(actual, expected);
  938. }
  939. test_table_alignment() {
  940. let markdown = 'Column A | Column B | Column C\n' +
  941. ':--- | :---: | ---:\n' +
  942. '1 | 2 | 3\n' +
  943. '4 | 5 | 6';
  944. let expected = '<table> ' +
  945. '<thead> ' +
  946. '<tr> ' +
  947. '<th align="left">Column A</th> ' +
  948. '<th align="center">Column B</th> ' +
  949. '<th align="right">Column C</th> ' +
  950. '</tr> ' +
  951. '</thead> ' +
  952. '<tbody> ' +
  953. '<tr> ' +
  954. '<td align="left">1</td> ' +
  955. '<td align="center">2</td> ' +
  956. '<td align="right">3</td> ' +
  957. '</tr> ' +
  958. '<tr> ' +
  959. '<td align="left">4</td> ' +
  960. '<td align="center">5</td> ' +
  961. '<td align="right">6</td> ' +
  962. '</tr> ' +
  963. '</tbody> ' +
  964. '</table>';
  965. let actual = this.md(markdown);
  966. this.assertEqual(actual, expected);
  967. }
  968. test_table_holes() {
  969. let markdown = 'Column A||Column C\n' +
  970. '---|---|---\n' +
  971. '|1|2||\n' +
  972. '|4||6|\n' +
  973. '||8|9|';
  974. let expected = '<table> ' +
  975. '<thead> ' +
  976. '<tr> ' +
  977. '<th>Column A</th> ' +
  978. '<th></th> ' +
  979. '<th>Column C</th> ' +
  980. '</tr> ' +
  981. '</thead> ' +
  982. '<tbody> ' +
  983. '<tr> ' +
  984. '<td>1</td> ' +
  985. '<td>2</td> ' +
  986. '<td></td> ' +
  987. '</tr> ' +
  988. '<tr> ' +
  989. '<td>4</td> ' +
  990. '<td></td> ' +
  991. '<td>6</td> ' +
  992. '</tr> ' +
  993. '<tr> ' +
  994. '<td></td> ' +
  995. '<td>8</td> ' +
  996. '<td>9</td> ' +
  997. '</tr> ' +
  998. '</tbody> ' +
  999. '</table>';
  1000. let actual = this.md(markdown);
  1001. this.assertEqual(actual, expected);
  1002. }
  1003. test_definitionList() {
  1004. let markdown = 'term\n' +
  1005. ': definition\n' +
  1006. 'another' +
  1007. ' term\n' +
  1008. ': def 1\n' +
  1009. ' broken on next line\n' +
  1010. ': def 2';
  1011. let expected = '<dl> ' +
  1012. '<dt>term</dt> ' +
  1013. '<dd>definition</dd> ' +
  1014. '<dt>another term</dt> ' +
  1015. '<dd>def 1 broken on next line</dd> ' +
  1016. '<dd>def 2</dd> ' +
  1017. '</dl>';
  1018. let actual = this.md(markdown);
  1019. this.assertEqual(actual, expected);
  1020. }
  1021. test_footnotes() {
  1022. let markdown = 'Lorem ipsum[^1] dolor[^2] sit[^1] amet\n\n[^1]: A footnote\n[^2]: Another footnote';
  1023. let expected = '<p>Lorem ipsum<sup id="footnoteref_1"><a href="#footnote_1">1</a></sup> ' +
  1024. 'dolor<sup id="footnoteref_2"><a href="#footnote_2">2</a></sup> ' +
  1025. 'sit<sup id="footnoteref_3"><a href="#footnote_1">1</a></sup> amet</p> ' +
  1026. '<div class="footnotes">' +
  1027. '<hr/>' +
  1028. '<ol>' +
  1029. '<li value="1" id="footnote_1">A footnote <a href="#footnoteref_1" class="footnote-backref">↩︎</a> <a href="#footnoteref_3" class="footnote-backref">↩︎</a></li> ' +
  1030. '<li value="2" id="footnote_2">Another footnote <a href="#footnoteref_2" class="footnote-backref">↩︎</a></li> ' +
  1031. '</ol>' +
  1032. '</div>';
  1033. let actual = this.md(markdown);
  1034. this.assertEqual(actual, expected);
  1035. }
  1036. test_abbreviations() {
  1037. let markdown = 'Lorem ipsum HTML dolor HTML sit\n' +
  1038. '\n' +
  1039. '*[HTML]: Hypertext Markup Language';
  1040. let expected = '<p>Lorem ipsum <abbr title="Hypertext Markup Language">HTML</abbr> dolor <abbr title="Hypertext Markup Language">HTML</abbr> sit</p>';
  1041. let actual = this.md(markdown);
  1042. this.assertEqual(actual, expected);
  1043. }
  1044. }
  1045. class CellValueTests extends BaseTest {
  1046. test_fromCellString_blank() {
  1047. var value;
  1048. value = CellValue.fromCellString('');
  1049. this.assertEqual(value.type, CellValue.TYPE_BLANK);
  1050. this.assertEqual(value.formattedValue, '');
  1051. this.assertEqual(value.value, null);
  1052. value = CellValue.fromCellString(' ');
  1053. this.assertEqual(value.type, CellValue.TYPE_BLANK);
  1054. this.assertEqual(value.formattedValue, '');
  1055. this.assertEqual(value.value, null);
  1056. }
  1057. test_fromCellString_number() {
  1058. var value;
  1059. value = CellValue.fromCellString('123');
  1060. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1061. this.assertEqual(value.formattedValue, '123');
  1062. this.assertEqual(value.value, 123);
  1063. this.assertEqual(value.decimals, 0);
  1064. value = CellValue.fromCellString('-0');
  1065. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1066. this.assertEqual(value.formattedValue, '-0');
  1067. this.assertEqual(value.value, 0);
  1068. this.assertEqual(value.decimals, 0);
  1069. value = CellValue.fromCellString('1,234');
  1070. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1071. this.assertEqual(value.formattedValue, '1,234');
  1072. this.assertEqual(value.value, 1234);
  1073. this.assertEqual(value.decimals, 0);
  1074. value = CellValue.fromCellString('-1,234,567.89');
  1075. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1076. this.assertEqual(value.formattedValue, '-1,234,567.89');
  1077. this.assertEqual(value.value, -1234567.89);
  1078. this.assertEqual(value.decimals, 2);
  1079. }
  1080. test_fromCellString_percent() {
  1081. var value;
  1082. value = CellValue.fromCellString('123%');
  1083. this.assertEqual(value.type, CellValue.TYPE_PERCENT);
  1084. this.assertEqual(value.formattedValue, '123%');
  1085. this.assertEqual(value.value, 1.23, 0.0001);
  1086. this.assertEqual(value.decimals, 0);
  1087. value = CellValue.fromCellString('-12.3%');
  1088. this.assertEqual(value.type, CellValue.TYPE_PERCENT);
  1089. this.assertEqual(value.formattedValue, '-12.3%');
  1090. this.assertEqual(value.value, -0.123, 0.0001);
  1091. this.assertEqual(value.decimals, 1);
  1092. }
  1093. test_fromCellString_currency() {
  1094. var value;
  1095. value = CellValue.fromCellString('$123');
  1096. this.assertEqual(value.type, CellValue.TYPE_CURRENCY);
  1097. this.assertEqual(value.formattedValue, '$123');
  1098. this.assertEqual(value.value, 123);
  1099. this.assertEqual(value.decimals, 0);
  1100. value = CellValue.fromCellString('-$12.34');
  1101. this.assertEqual(value.type, CellValue.TYPE_CURRENCY);
  1102. this.assertEqual(value.formattedValue, '-$12.34');
  1103. this.assertEqual(value.value, -12.34, 0.0001);
  1104. this.assertEqual(value.decimals, 2);
  1105. }
  1106. test_fromCellString_boolean() {
  1107. var value;
  1108. value = CellValue.fromCellString('true');
  1109. this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
  1110. this.assertEqual(value.formattedValue, 'TRUE');
  1111. this.assertEqual(value.value, true);
  1112. value = CellValue.fromCellString('false');
  1113. this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
  1114. this.assertEqual(value.formattedValue, 'FALSE');
  1115. this.assertEqual(value.value, false);
  1116. }
  1117. test_fromCellString_string() {
  1118. var value;
  1119. value = CellValue.fromCellString('some text');
  1120. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1121. this.assertEqual(value.formattedValue, 'some text');
  1122. this.assertEqual(value.value, 'some text');
  1123. value = CellValue.fromCellString("'123");
  1124. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1125. this.assertEqual(value.formattedValue, '123');
  1126. this.assertEqual(value.value, '123');
  1127. }
  1128. test_fromCellString_formula() {
  1129. var value;
  1130. value = CellValue.fromCellString('=A*B');
  1131. this.assertEqual(value.type, CellValue.TYPE_FORMULA);
  1132. this.assertEqual(value.formattedValue, '=A*B');
  1133. this.assertEqual(value.value, '=A*B');
  1134. value = CellValue.fromCellString('=MAX(A, 3)');
  1135. this.assertEqual(value.type, CellValue.TYPE_FORMULA);
  1136. this.assertEqual(value.formattedValue, '=MAX(A, 3)');
  1137. this.assertEqual(value.value, '=MAX(A, 3)');
  1138. }
  1139. test_fromValue_null() {
  1140. var value;
  1141. value = CellValue.fromValue(null);
  1142. this.assertEqual(value.type, CellValue.TYPE_BLANK);
  1143. }
  1144. test_fromValue_number() {
  1145. var value;
  1146. value = CellValue.fromValue(123);
  1147. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1148. this.assertEqual(value.formattedValue, '123');
  1149. value = CellValue.fromValue(3.141592);
  1150. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1151. this.assertEqual(value.formattedValue, '3.141592');
  1152. value = CellValue.fromValue(123456789);
  1153. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1154. this.assertEqual(value.formattedValue, '123,456,789');
  1155. }
  1156. test_fromValue_boolean() {
  1157. var value;
  1158. value = CellValue.fromValue(true);
  1159. this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
  1160. this.assertEqual(value.formattedValue, 'TRUE');
  1161. value = CellValue.fromValue(false);
  1162. this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
  1163. this.assertEqual(value.formattedValue, 'FALSE');
  1164. }
  1165. test_fromValue_string() {
  1166. var value;
  1167. value = CellValue.fromValue('foo');
  1168. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1169. this.assertEqual(value.formattedValue, 'foo');
  1170. value = CellValue.fromValue('123');
  1171. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1172. this.assertEqual(value.formattedValue, '123');
  1173. }
  1174. test_fromValue_formula() {
  1175. var value;
  1176. value = CellValue.fromValue('=A*B');
  1177. this.assertEqual(value.type, CellValue.TYPE_FORMULA);
  1178. this.assertEqual(value.formattedValue, '=A*B');
  1179. }
  1180. test_operation_add() {
  1181. var a, b, result, expected;
  1182. a = CellValue.fromValue(3);
  1183. b = CellValue.fromValue(4);
  1184. result = a.add(b)
  1185. expected = new CellValue('7', 7, CellValue.TYPE_NUMBER, 0);
  1186. this.assertEqual(result, expected);
  1187. a = CellValue.fromCellString('100%');
  1188. b = CellValue.fromCellString('50%');
  1189. result = a.add(b);
  1190. expected = new CellValue('150%', 1.5, CellValue.TYPE_PERCENT, 0);
  1191. this.assertEqual(result, expected);
  1192. a = CellValue.fromCellString('$123');
  1193. b = CellValue.fromCellString('$321');
  1194. result = a.add(b);
  1195. expected = new CellValue('$444.00', 444, CellValue.TYPE_CURRENCY, 2);
  1196. this.assertEqual(result, expected);
  1197. }
  1198. test_operation_subtract() {
  1199. var a, b, result, expected;
  1200. a = CellValue.fromValue(9);
  1201. b = CellValue.fromValue(4);
  1202. result = a.subtract(b)
  1203. expected = new CellValue('5', 5, CellValue.TYPE_NUMBER, 0);
  1204. this.assertEqual(result, expected);
  1205. a = CellValue.fromCellString('100%');
  1206. b = CellValue.fromCellString('50%');
  1207. result = a.subtract(b);
  1208. expected = new CellValue('50%', 0.5, CellValue.TYPE_PERCENT, 0);
  1209. this.assertEqual(result, expected);
  1210. a = CellValue.fromCellString('$321');
  1211. b = CellValue.fromCellString('$123');
  1212. result = a.subtract(b);
  1213. expected = new CellValue('$198.00', 198, CellValue.TYPE_CURRENCY, 2);
  1214. this.assertEqual(result, expected);
  1215. }
  1216. test_operation_multiply() {
  1217. var a, b, result, expected;
  1218. a = CellValue.fromValue(3);
  1219. b = CellValue.fromValue(4);
  1220. result = a.multiply(b)
  1221. expected = new CellValue('12', 12, CellValue.TYPE_NUMBER, 0);
  1222. this.assertEqual(result, expected);
  1223. a = CellValue.fromCellString('150%');
  1224. b = CellValue.fromCellString('50%');
  1225. result = a.multiply(b);
  1226. expected = new CellValue('75%', 0.75, CellValue.TYPE_PERCENT, 0);
  1227. this.assertEqual(result, expected);
  1228. a = CellValue.fromCellString('$321');
  1229. b = CellValue.fromCellString('50%');
  1230. result = a.multiply(b);
  1231. expected = new CellValue('$160.50', 160.50, CellValue.TYPE_CURRENCY, 2);
  1232. this.assertEqual(result, expected);
  1233. }
  1234. test_operation_divide() {
  1235. var a, b, result, expected;
  1236. a = CellValue.fromValue(12);
  1237. b = CellValue.fromValue(4);
  1238. result = a.divide(b)
  1239. expected = new CellValue('3', 3, CellValue.TYPE_NUMBER, 0);
  1240. this.assertEqual(result, expected);
  1241. a = CellValue.fromCellString('150%');
  1242. b = CellValue.fromCellString('50%');
  1243. result = a.divide(b);
  1244. expected = new CellValue('300%', 3.0, CellValue.TYPE_PERCENT, 0);
  1245. this.assertEqual(result, expected);
  1246. a = CellValue.fromCellString('$321');
  1247. b = CellValue.fromCellString('200%');
  1248. result = a.divide(b);
  1249. expected = new CellValue('$160.50', 160.50, CellValue.TYPE_CURRENCY, 2);
  1250. this.assertEqual(result, expected);
  1251. }
  1252. test_operation_modulo() {
  1253. var a, b, result, expected;
  1254. a = CellValue.fromValue(7);
  1255. b = CellValue.fromValue(4);
  1256. result = a.modulo(b)
  1257. expected = new CellValue('3', 3, CellValue.TYPE_NUMBER, 0);
  1258. this.assertEqual(result, expected);
  1259. a = CellValue.fromCellString('175%');
  1260. b = CellValue.fromCellString('50%');
  1261. result = a.modulo(b);
  1262. expected = new CellValue('25%', 0.25, CellValue.TYPE_PERCENT, 0);
  1263. this.assertEqual(result, expected);
  1264. a = CellValue.fromCellString('$327');
  1265. b = CellValue.fromCellString('$20');
  1266. result = a.modulo(b);
  1267. expected = new CellValue('$7.00', 7.00, CellValue.TYPE_CURRENCY, 2);
  1268. this.assertEqual(result, expected);
  1269. }
  1270. test_operation_unaryNot() {
  1271. this.assertEqual(CellValue.fromValue(true).not(), CellValue.fromValue(false));
  1272. this.assertEqual(CellValue.fromValue(false).not(), CellValue.fromValue(true));
  1273. }
  1274. test_operation_comparators() {
  1275. const a = CellValue.fromValue(3);
  1276. const b = CellValue.fromValue(4);
  1277. const t = CellValue.fromValue(true);
  1278. const f = CellValue.fromValue(false);
  1279. this.assertEqual(a.lt(b), t);
  1280. this.assertEqual(a.lte(b), t);
  1281. this.assertEqual(a.gt(b), f);
  1282. this.assertEqual(a.gte(b), f);
  1283. this.assertEqual(a.eq(b), f);
  1284. this.assertEqual(a.neq(b), t);
  1285. this.assertEqual(b.lt(a), f);
  1286. this.assertEqual(b.lte(a), f);
  1287. this.assertEqual(b.gt(a), t);
  1288. this.assertEqual(b.gte(a), t);
  1289. this.assertEqual(b.eq(a), f);
  1290. this.assertEqual(b.neq(a), t);
  1291. this.assertEqual(a.lt(a), f);
  1292. this.assertEqual(a.lte(a), t);
  1293. this.assertEqual(a.gt(a), f);
  1294. this.assertEqual(a.gte(a), t);
  1295. this.assertEqual(a.eq(a), t);
  1296. this.assertEqual(a.neq(a), f);
  1297. }
  1298. test_operation_concatenate() {
  1299. const a = CellValue.fromValue('abc');
  1300. const b = CellValue.fromValue('xyz');
  1301. const result = a.concatenate(b);
  1302. const expected = CellValue.fromValue('abcxyz');
  1303. this.assertEqual(result, expected);
  1304. }
  1305. }
  1306. class CellAddressRangeTests extends BaseTest {
  1307. test_iterator() {
  1308. const grid = new SpreadsheetGrid(3, 4);
  1309. let range = new CellAddressRange(new CellAddress(0, 0), new CellAddress(2, 3));
  1310. var visited = [];
  1311. var sanity = 100;
  1312. for (const address of range.cellsIn(grid)) {
  1313. visited.push(address.name);
  1314. if (sanity-- < 0) break;
  1315. }
  1316. const result = visited.join(',');
  1317. const expected = 'A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4';
  1318. this.assertEqual(result, expected);
  1319. }
  1320. test_iterator_column() {
  1321. const grid = new SpreadsheetGrid(3, 4);
  1322. let range = new CellAddressRange(new CellAddress(1, -1), new CellAddress(2, -1));
  1323. var visited = [];
  1324. var sanity = 100;
  1325. for (const address of range.cellsIn(grid)) {
  1326. visited.push(address.name);
  1327. if (sanity-- < 0) break;
  1328. }
  1329. const result = visited.join(',');
  1330. const expected = 'B1,B2,B3,B4,C1,C2,C3,C4';
  1331. this.assertEqual(result, expected);
  1332. }
  1333. test_iterator_beyondBounds() {
  1334. const grid = new SpreadsheetGrid(3, 4);
  1335. let range = new CellAddressRange(new CellAddress(0, 1), new CellAddress(9, 9));
  1336. var visited = [];
  1337. var sanity = 100;
  1338. for (const address of range.cellsIn(grid)) {
  1339. visited.push(address.name);
  1340. if (sanity-- < 0) break;
  1341. }
  1342. const result = visited.join(',');
  1343. const expected = 'A2,A3,A4,B2,B3,B4,C2,C3,C4';
  1344. this.assertEqual(result, expected);
  1345. }
  1346. test_iterator_outOfBounds() {
  1347. const grid = new SpreadsheetGrid(3, 4);
  1348. let range = new CellAddressRange(new CellAddress(5, 0), new CellAddress(5, 9));
  1349. var visited = [];
  1350. var sanity = 100;
  1351. for (const address of range.cellsIn(grid)) {
  1352. visited.push(address.name);
  1353. if (sanity-- < 0) break;
  1354. }
  1355. const result = visited.join(',');
  1356. const expected = '';
  1357. this.assertEqual(result, expected);
  1358. }
  1359. }
  1360. class ExpressionSetTests extends BaseTest {
  1361. test_simpleMath() {
  1362. const grid = new SpreadsheetGrid(1, 1);
  1363. grid.cells[0][0].originalValue = CellValue.fromCellString('=7*3');
  1364. const expressionSet = new CellExpressionSet(grid);
  1365. expressionSet.calculateCells();
  1366. const expected = CellValue.fromValue(21);
  1367. this.assertEqual(grid.cells[0][0].outputValue, expected);
  1368. }
  1369. test_reference() {
  1370. const grid = new SpreadsheetGrid(3, 1);
  1371. grid.cells[0][0].originalValue = CellValue.fromValue(123);
  1372. grid.cells[1][0].originalValue = CellValue.fromValue(3);
  1373. grid.cells[2][0].originalValue = CellValue.fromCellString('=A1*B1');
  1374. const expressionSet = new CellExpressionSet(grid);
  1375. expressionSet.calculateCells();
  1376. const expected = CellValue.fromValue(369);
  1377. this.assertEqual(grid.cells[2][0].outputValue, expected);
  1378. }
  1379. test_infixPriority() {
  1380. const grid = new SpreadsheetGrid(1, 1);
  1381. grid.cells[0][0].originalValue = CellValue.fromCellString('=4*9/3+7-4');
  1382. const expressionSet = new CellExpressionSet(grid);
  1383. expressionSet.calculateCells();
  1384. const expected = CellValue.fromValue(15);
  1385. this.assertEqual(grid.cells[0][0].outputValue, expected);
  1386. }
  1387. test_filledFormula() {
  1388. const grid = new SpreadsheetGrid(3, 3);
  1389. grid.cells[0][0].originalValue = CellValue.fromValue(4);
  1390. grid.cells[1][0].originalValue = CellValue.fromValue(5);
  1391. grid.cells[2][0].originalValue = CellValue.fromCellString('=A1*B1 FILL');
  1392. grid.cells[0][1].originalValue = CellValue.fromValue(6);
  1393. grid.cells[1][1].originalValue = CellValue.fromValue(7);
  1394. grid.cells[0][2].originalValue = CellValue.fromValue(8);
  1395. grid.cells[1][2].originalValue = CellValue.fromValue(9);
  1396. const expressionSet = new CellExpressionSet(grid);
  1397. expressionSet.calculateCells();
  1398. this.assertEqual(grid.cells[2][0].outputValue, CellValue.fromValue(20));
  1399. this.assertEqual(grid.cells[2][1].outputValue, CellValue.fromValue(42));
  1400. this.assertEqual(grid.cells[2][2].outputValue, CellValue.fromValue(72));
  1401. }
  1402. test_dependencies() {
  1403. const grid = new SpreadsheetGrid(2, 4);
  1404. grid.cells[0][0].originalValue = CellValue.fromValue(1);
  1405. grid.cells[1][0].originalValue = CellValue.fromCellString('=A1+B2');
  1406. grid.cells[0][1].originalValue = CellValue.fromValue(2);
  1407. grid.cells[1][1].originalValue = CellValue.fromCellString('=A2+B3');
  1408. grid.cells[0][2].originalValue = CellValue.fromValue(3);
  1409. grid.cells[1][2].originalValue = CellValue.fromCellString('=A3+B4');
  1410. grid.cells[0][3].originalValue = CellValue.fromValue(4);
  1411. grid.cells[1][3].originalValue = CellValue.fromCellString('=A4');
  1412. const expressionSet = new CellExpressionSet(grid);
  1413. expressionSet.calculateCells();
  1414. this.assertEqual(grid.cells[1][0].outputValue, CellValue.fromValue(10));
  1415. this.assertEqual(grid.cells[1][1].outputValue, CellValue.fromValue(9));
  1416. this.assertEqual(grid.cells[1][2].outputValue, CellValue.fromValue(7));
  1417. this.assertEqual(grid.cells[1][3].outputValue, CellValue.fromValue(4));
  1418. }
  1419. _test_simple_formula(formula, expected) {
  1420. const grid = new SpreadsheetGrid(1, 1);
  1421. grid.cells[0][0].originalValue = CellValue.fromCellString(formula);
  1422. const expressionSet = new CellExpressionSet(grid);
  1423. expressionSet.calculateCells();
  1424. const result = grid.cells[0][0].outputValue;
  1425. const exp = (expected instanceof CellValue) ? expected : CellValue.fromValue(expected);
  1426. this.assertEqual(result.type, exp.type);
  1427. this.assertEqual(result.decimals, exp.decimals);
  1428. this.assertEqual(result.formattedValue, exp.formattedValue);
  1429. this.assertEqual(result.value, exp.value);
  1430. }
  1431. test_func_abs() {
  1432. this._test_simple_formula('=ABS(-3)', 3);
  1433. this._test_simple_formula('=ABS(4)', 4);
  1434. }
  1435. test_func_and() {
  1436. this._test_simple_formula('=AND(FALSE, FALSE)', false);
  1437. this._test_simple_formula('=AND(FALSE, TRUE)', false);
  1438. this._test_simple_formula('=AND(TRUE, FALSE)', false);
  1439. this._test_simple_formula('=AND(TRUE, TRUE)', true);
  1440. this._test_simple_formula('=AND(TRUE, TRUE, TRUE, TRUE)', true);
  1441. this._test_simple_formula('=AND(TRUE, TRUE, TRUE, FALSE)', false);
  1442. }
  1443. test_func_average() {
  1444. this._test_simple_formula('=AVERAGE(4, 6, 2, 4)', 4);
  1445. }
  1446. test_func_ceiling() {
  1447. this._test_simple_formula('=CEILING(3.1)', 4);
  1448. this._test_simple_formula('=CEILING(3)', 3);
  1449. this._test_simple_formula('=CEILING(-3.1)', -3);
  1450. }
  1451. test_func_exp() {
  1452. this._test_simple_formula('=EXP(1)', 2.718281828459045);
  1453. }
  1454. test_func_floor() {
  1455. this._test_simple_formula('=FLOOR(3.1)', 3);
  1456. this._test_simple_formula('=FLOOR(3)', 3);
  1457. this._test_simple_formula('=FLOOR(-3.1)', -4);
  1458. }
  1459. test_func_if() {
  1460. this._test_simple_formula('=IF(FALSE, 4, 6)', 6);
  1461. this._test_simple_formula('=IF(TRUE, 4, 6)', 4);
  1462. }
  1463. test_func_ifs() {
  1464. this._test_simple_formula('=IFS(TRUE, 1, FALSE, 2, FALSE, 3, FALSE, 4, 5)', 1);
  1465. this._test_simple_formula('=IFS(FALSE, 1, TRUE, 2, FALSE, 3, FALSE, 4, 5)', 2);
  1466. this._test_simple_formula('=IFS(FALSE, 1, FALSE, 2, TRUE, 3, FALSE, 4, 5)', 3);
  1467. this._test_simple_formula('=IFS(FALSE, 1, FALSE, 2, FALSE, 3, TRUE, 4, 5)', 4);
  1468. this._test_simple_formula('=IFS(FALSE, 1, FALSE, 2, FALSE, 3, FALSE, 4, 5)', 5);
  1469. }
  1470. test_func_ln() {
  1471. this._test_simple_formula('=LN(2.718281828459045)', 1);
  1472. }
  1473. test_func_log() {
  1474. this._test_simple_formula('=LOG(1000, 10)', 3);
  1475. }
  1476. test_func_lower() {
  1477. this._test_simple_formula('=LOWER("MiXeD")', 'mixed');
  1478. }
  1479. test_func_max() {
  1480. this._test_simple_formula('=MAX(4, 8, 5, 2)', 8);
  1481. }
  1482. test_func_min() {
  1483. this._test_simple_formula('=MIN(4, 8, 5, 2)', 2);
  1484. }
  1485. test_func_mod() {
  1486. this._test_simple_formula('=MOD(37, 4)', 1);
  1487. }
  1488. test_func_not() {
  1489. this._test_simple_formula('=NOT(TRUE)', false);
  1490. this._test_simple_formula('=NOT(FALSE)', true);
  1491. }
  1492. test_func_or() {
  1493. this._test_simple_formula('=OR(FALSE, FALSE)', false);
  1494. this._test_simple_formula('=OR(FALSE, TRUE)', true);
  1495. this._test_simple_formula('=OR(TRUE, FALSE)', true);
  1496. this._test_simple_formula('=OR(TRUE, TRUE)', true);
  1497. this._test_simple_formula('=OR(FALSE, FALSE, FALSE, TRUE)', true);
  1498. }
  1499. test_func_power() {
  1500. this._test_simple_formula('=POWER(2, 3)', 8);
  1501. }
  1502. test_func_round() {
  1503. this._test_simple_formula('=ROUND(3.1)', 3);
  1504. this._test_simple_formula('=ROUND(3.5)', 4);
  1505. this._test_simple_formula('=ROUND(4)', 4);
  1506. this._test_simple_formula('=ROUND(-3.1)', -3);
  1507. this._test_simple_formula('=ROUND(-3.5)', -3);
  1508. this._test_simple_formula('=ROUND(-3.9)', -4);
  1509. this._test_simple_formula('=ROUND(3.1415926535, 1)', 3.1);
  1510. this._test_simple_formula('=ROUND(3.1415926535, 2)', 3.14);
  1511. this._test_simple_formula('=ROUND(31.415926535, -1)', 30);
  1512. }
  1513. test_func_sqrt() {
  1514. this._test_simple_formula('=SQRT(16)', 4);
  1515. }
  1516. test_func_substitute() {
  1517. this._test_simple_formula('=SUBSTITUTE("cat sat on the mat", "at", "ot")', 'cot sot on the mot');
  1518. }
  1519. test_func_sum() {
  1520. this._test_simple_formula('=SUM(1, 2, 3, 4, 5)', 15);
  1521. }
  1522. test_func_upper() {
  1523. this._test_simple_formula('=UPPER("mIxEd")', 'MIXED');
  1524. }
  1525. test_func_xor() {
  1526. this._test_simple_formula('=XOR(FALSE, FALSE)', false);
  1527. this._test_simple_formula('=XOR(FALSE, TRUE)', true);
  1528. this._test_simple_formula('=XOR(TRUE, FALSE)', true);
  1529. this._test_simple_formula('=XOR(TRUE, TRUE)', false);
  1530. this._test_simple_formula('=XOR(FALSE, FALSE, TRUE)', true);
  1531. this._test_simple_formula('=XOR(TRUE, FALSE, TRUE)', false);
  1532. }
  1533. test_format() {
  1534. this._test_simple_formula('=2.718281828459045 ; number 3', new CellValue('2.718', 2.718281828459045, 'number', 3));
  1535. this._test_simple_formula('=2.718281828459045 ; percent 2', new CellValue('271.83%', 2.718281828459045, 'percent', 2));
  1536. this._test_simple_formula('=2.718281828459045 ; currency 2', new CellValue('$2.72', 2.718281828459045, 'currency', 2));
  1537. }
  1538. }
  1539. class SpreadsheetMarkdownIntegrationTests extends BaseTest {
  1540. parser;
  1541. setUp() {
  1542. this.parser = new Markdown([ ...Markdown.allBlockReaders, new SpreadsheetBlockReader() ], Markdown.allInlineReaders);
  1543. }
  1544. md(markdown) {
  1545. return normalizeWhitespace(this.parser.toHTML(markdown));
  1546. }
  1547. test_integration() {
  1548. const markdown = '| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | =A*B |';
  1549. const expected = '<table> <thead> ' +
  1550. '<tr> <th>A</th> <th>B</th> <th>C</th> </tr> ' +
  1551. '</thead> <tbody> <tr> ' +
  1552. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td> ' +
  1553. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td> ' +
  1554. '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12">12</td> ' +
  1555. '</tr> </tbody> </table>';
  1556. const result = this.md(markdown);
  1557. this.assertEqual(result, expected);
  1558. }
  1559. test_bailOut() {
  1560. // If no formulas found table isn't modified
  1561. const markdown = '| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | A*B |';
  1562. const expected = '<table> <thead> ' +
  1563. '<tr> <th>A</th> <th>B</th> <th>C</th> </tr> ' +
  1564. '</thead> <tbody> <tr> ' +
  1565. '<td>3</td> ' +
  1566. '<td>4</td> ' +
  1567. '<td>A*B</td> ' +
  1568. '</tr> </tbody> </table>';
  1569. const result = this.md(markdown);
  1570. this.assertEqual(result, expected);
  1571. }
  1572. }
  1573. </script>
  1574. </head>
  1575. <body>
  1576. <div id="results"></div>
  1577. </body>
  1578. </html>