|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { getKatexEquationRows } from './katex' |
| 3 | + |
| 4 | +describe('getKatexEquationRows', () => { |
| 5 | + it('ignores nested KaTeX tables when collecting equation rows', () => { |
| 6 | + const leftRows = createRows('left', 3) |
| 7 | + const rightRows = createRows('right', 3) |
| 8 | + |
| 9 | + const root = { |
| 10 | + querySelectorAll(selector: string) { |
| 11 | + expect(selector).toBe('.mtable > [class*=col-align]') |
| 12 | + return [ |
| 13 | + createColumn(leftRows), |
| 14 | + createColumn(rightRows), |
| 15 | + createColumn(createRows('substack', 2), true), |
| 16 | + createColumn(createRows('matrix', 4), true), |
| 17 | + ] |
| 18 | + }, |
| 19 | + } as unknown as Element |
| 20 | + |
| 21 | + expect(getKatexEquationRows(root)).toEqual([ |
| 22 | + [leftRows[0], rightRows[0]], |
| 23 | + [leftRows[1], rightRows[1]], |
| 24 | + [leftRows[2], rightRows[2]], |
| 25 | + ]) |
| 26 | + }) |
| 27 | +}) |
| 28 | + |
| 29 | +function createColumn(rows: Element[], nested = false) { |
| 30 | + return { |
| 31 | + parentElement: { |
| 32 | + parentElement: { |
| 33 | + closest(selector: string) { |
| 34 | + expect(selector).toBe('.mtable') |
| 35 | + return nested ? {} : null |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + querySelectorAll(selector: string) { |
| 40 | + expect(selector).toBe(':scope > .vlist-t > .vlist-r > .vlist > span > .mord') |
| 41 | + return rows |
| 42 | + }, |
| 43 | + } as unknown as Element |
| 44 | +} |
| 45 | + |
| 46 | +function createRows(name: string, count: number) { |
| 47 | + return Array.from({ length: count }, (_, index) => ({ name, index } as unknown as Element)) |
| 48 | +} |
0 commit comments