|
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import { chromium, type Browser } from 'playwright'; |
| 3 | +import { afterAll, beforeAll, expect, it } from 'vite-plus/test'; |
| 4 | +import { compile } from '../../src/compile.ts'; |
| 5 | + |
| 6 | +const css = ['reset.css', 'prose.css', 'code.css'] |
| 7 | + .map((name) => readFileSync(new URL(`../../src/theme/${name}`, import.meta.url), 'utf8')) |
| 8 | + .join('\n'); |
| 9 | + |
| 10 | +let browser: Browser; |
| 11 | +let html: string; |
| 12 | + |
| 13 | +beforeAll(async () => { |
| 14 | + const nested = await compile('- [ ] parent\n - [x] child\n - [ ] another child'); |
| 15 | + const loose = await compile('- [ ] first paragraph\n\n second paragraph\n\n- ordinary item'); |
| 16 | + const code = await compile('## Heading\n\n```js\nconst x = 1;\n```'); |
| 17 | + html = `<style>${css}</style><main class="void-md" style="margin: 40px"> |
| 18 | + <section id="nested">${nested.html}</section> |
| 19 | + <section id="loose">${loose.html}</section> |
| 20 | + <section id="code">${code.html}</section> |
| 21 | + </main>`; |
| 22 | + browser = await chromium.launch(); |
| 23 | +}); |
| 24 | + |
| 25 | +afterAll(async () => { |
| 26 | + await browser?.close(); |
| 27 | +}); |
| 28 | + |
| 29 | +it.each(['ltr', 'rtl'])('keeps task-list content in block flow in %s', async (direction) => { |
| 30 | + const page = await browser.newPage(); |
| 31 | + try { |
| 32 | + await page.setContent(`<html dir="${direction}"><body>${html}</body></html>`); |
| 33 | + const layout = await page.evaluate(() => { |
| 34 | + const parent = document.querySelector('#nested li')!; |
| 35 | + const parentText = [...parent.childNodes].find((node) => node.nodeType === Node.TEXT_NODE)!; |
| 36 | + const range = document.createRange(); |
| 37 | + range.selectNode(parentText); |
| 38 | + const paragraphs = document.querySelectorAll('#loose li:first-child > p'); |
| 39 | + const ordinary = document.querySelector('#loose li:last-child')!; |
| 40 | + return { |
| 41 | + parentBottom: range.getBoundingClientRect().bottom, |
| 42 | + childTop: parent.querySelector('ul')!.getBoundingClientRect().top, |
| 43 | + firstParagraphBottom: paragraphs[0].getBoundingClientRect().bottom, |
| 44 | + secondParagraphTop: paragraphs[1].getBoundingClientRect().top, |
| 45 | + ordinaryMarker: getComputedStyle(ordinary).listStyleType, |
| 46 | + taskMarker: getComputedStyle(parent).listStyleType, |
| 47 | + checkboxes: [...document.querySelectorAll<HTMLInputElement>('input[type="checkbox"]')].map( |
| 48 | + (input) => ({ disabled: input.disabled, checked: input.checked }), |
| 49 | + ), |
| 50 | + }; |
| 51 | + }); |
| 52 | + expect(layout.childTop).toBeGreaterThan(layout.parentBottom); |
| 53 | + expect(layout.secondParagraphTop).toBeGreaterThanOrEqual(layout.firstParagraphBottom); |
| 54 | + expect(layout.ordinaryMarker).not.toBe('none'); |
| 55 | + expect(layout.taskMarker).toBe('none'); |
| 56 | + expect(layout.checkboxes).toEqual([ |
| 57 | + { disabled: true, checked: false }, |
| 58 | + { disabled: true, checked: true }, |
| 59 | + { disabled: true, checked: false }, |
| 60 | + { disabled: true, checked: false }, |
| 61 | + ]); |
| 62 | + } finally { |
| 63 | + await page.close(); |
| 64 | + } |
| 65 | +}); |
| 66 | + |
| 67 | +it.each(['ltr', 'rtl'])('positions heading anchors and copy buttons in %s', async (direction) => { |
| 68 | + const page = await browser.newPage(); |
| 69 | + try { |
| 70 | + await page.setContent(`<html dir="${direction}"><body>${html}</body></html>`); |
| 71 | + const layout = await page.evaluate(() => { |
| 72 | + const rect = (selector: string) => |
| 73 | + document.querySelector(selector)!.getBoundingClientRect().toJSON(); |
| 74 | + return { |
| 75 | + heading: rect('#code h2'), |
| 76 | + anchor: rect('#code .header-anchor'), |
| 77 | + code: rect('#code [class*="language-"]'), |
| 78 | + copy: rect('#code .copy'), |
| 79 | + }; |
| 80 | + }); |
| 81 | + if (direction === 'ltr') { |
| 82 | + expect(layout.anchor.right).toBeLessThanOrEqual(layout.heading.left); |
| 83 | + expect(layout.code.right - layout.copy.right).toBeLessThan( |
| 84 | + layout.copy.left - layout.code.left, |
| 85 | + ); |
| 86 | + } else { |
| 87 | + expect(layout.anchor.left).toBeGreaterThanOrEqual(layout.heading.right); |
| 88 | + expect(layout.copy.left - layout.code.left).toBeLessThan( |
| 89 | + layout.code.right - layout.copy.right, |
| 90 | + ); |
| 91 | + } |
| 92 | + expect(layout.copy.left).toBeGreaterThan(layout.code.left); |
| 93 | + expect(layout.copy.right).toBeLessThan(layout.code.right); |
| 94 | + } finally { |
| 95 | + await page.close(); |
| 96 | + } |
| 97 | +}); |
0 commit comments