components
All repositories: gitoria
18.6 KB
// tests/md-keys.mjs — mission 011: the MdEditor block's keyboard editing, with REAL keys only// (Input.dispatchKeyEvent for every key, Input.insertText for pasted-like typing; no synthetic DOM events, no clipboard).// Used by the gate (tests/browser.mjs, headless) and by tests/md-keys-run.mjs (lisbeth's real Chrome). The page must be /md-editor// (editor 2, "summary", starts empty). mdKeys(page, { base, check, sleep, label }) runs every scenario in that one tab.export async function mdKeys(page, { base, check, sleep, label = '', shot = null }) {const L = label ? `[${label}] ` : '';const send = (m, p) => page.send(m, p);const CTRL = 2, ALT = 1, SHIFT = 8;const key = async (k, code, vk, text, modifiers = 0) => {await send('Input.dispatchKeyEvent', { type: text !== undefined ? 'keyDown' : 'rawKeyDown', key: k, code, windowsVirtualKeyCode: vk, text, modifiers });await send('Input.dispatchKeyEvent', { type: 'keyUp', key: k, code, windowsVirtualKeyCode: vk, modifiers });await sleep(25);};const type = async (t) => { for (const ch of t) { if (ch === '\n') await enter(); else await key(ch, '', undefined, ch); } };const enter = () => key('Enter', 'Enter', 13, '\r');const shiftEnter = () => key('Enter', 'Enter', 13, '\r', SHIFT);const tab = (shift) => key('Tab', 'Tab', 9, undefined, shift ? SHIFT : 0);const back = () => key('Backspace', 'Backspace', 8);const esc = () => key('Escape', 'Escape', 27);const ctrl = (ch, extra = 0) => key(ch, 'Key' + ch.toUpperCase(), ch.toUpperCase().charCodeAt(0), undefined, CTRL | extra);const digit = (d, mods, k) => key(k || String(d), 'Digit' + d, 48 + d, undefined, mods);const ev = (e) => page.evaluate(e);const E = `document.querySelectorAll('md-editor')[1]`;const val = () => ev(`${E}.querySelector('input.value').value`);const art = () => ev(`${E}.querySelector('article').innerHTML`);const src = () => ev(`${E}.querySelector('textarea').value`);const focusEditor = async () => {await page.click('md-editor:nth-of-type(2) article');await page.waitFor(`${E}.querySelector('article').contains(document.activeElement)`, { label: 'editor focused' });};// an empty editor to type into: click, select all, delete// an empty editor to type into. Chrome keeps the style of deleted text (bold / italic) for the next typing in an emptied block, so a// wipe by Ctrl+A / Backspace would leak formatting into the next scenario: empty it in the Markdown source and come back (real clicks and keys)const fresh = async () => {await page.click('md-editor:nth-of-type(2) footer button'); await sleep(200);await page.click('md-editor:nth-of-type(2) textarea');await ctrl('a'); await back(); await sleep(120);await page.click('md-editor:nth-of-type(2) footer button'); await sleep(250);await focusEditor(); await sleep(60);};const settle = () => sleep(150);const is = (name, got, want) => check(`${L}md-keys: ${name}`, JSON.stringify(got) === JSON.stringify(want), `got ${JSON.stringify(got)}, wanted ${JSON.stringify(want)}`);const run = async (name, fn) => { try { await fn(); } catch (e) { check(`${L}md-keys: ${name} (ran)`, false, String(e && e.message || e)); } };const caret = () => ev(`(() => { const s = getSelection(); const n = s.anchorNode; if (!n) return '-'; const e = n.nodeType === 1 ? n : n.parentNode; const c = e.closest('td, th'); if (c) { const t = c.closest('table'); return c.tagName + [...t.querySelectorAll('th, td')].indexOf(c); } return e.tagName; })()`);// the block builds its surface from a CSS animation: wait for it (a background tab never runs it; the runner brings the tab to the front)await page.waitFor(`${E} && ${E}.querySelector('article') && ${E}.querySelector('article').childNodes.length > 0`, { timeout: 30000, label: 'the second editor built its surface' });let seen = 0; const errs = (name) => { const pr = page.problems().filter(m => !/favicon|Failed to load resource/.test(m.text)); check(L + 'md-keys: no console errors in: ' + name, pr.length === seen, pr.slice(seen).map(m => m.text).join(' | ')); seen = pr.length; };// ---- headings (typing rule + Ctrl+Alt+0..3)await run('headings', async () => {await fresh(); await type('# One\ntext\n## Two\n### Three\n'); await type('x'); await settle();is('# / ## / ### + Enter make headings, Enter after a heading is a paragraph', await val(), '# One\n\ntext\n\n## Two\n\n### Three\n\nx');await fresh(); await type('plain'); await digit(1, CTRL | ALT); await settle();is('Ctrl+Alt+1 makes the line an H1', [await val(), await art()], ['# plain', '<h1>plain</h1>']);await digit(2, CTRL | ALT); await settle(); is('Ctrl+Alt+2: H2', await val(), '## plain');await digit(3, CTRL | ALT); await settle(); is('Ctrl+Alt+3: H3', await val(), '### plain');await digit(0, CTRL | ALT); await settle(); is('Ctrl+Alt+0: paragraph again', await val(), 'plain');});errs('headings (typing rule + Ctrl+Alt+0..3)');// ---- listsawait run('bullet list', async () => {await fresh(); await type('- a\nb\n'); await enter(); await type('after'); await settle();is('"- " list, Enter adds an item, Enter on an empty item leaves the list', await val(), '- a\n- b\n\nafter');await fresh(); await type('one'); await key('8', 'Digit8', 56, undefined, CTRL | SHIFT); await settle();is('Ctrl+Shift+8 makes a bulleted list, again takes it back', [await val(), await art()], ['- one', '<ul><li>one</li></ul>']);await key('8', 'Digit8', 56, undefined, CTRL | SHIFT); await settle(); is('Ctrl+Shift+8 again: paragraph', await val(), 'one');});await run('numbered list', async () => {await fresh(); await type('1. a\nb\nc'); await settle();is('"1. " list numbers its items', await val(), '1. a\n2. b\n3. c');await fresh(); await type('x'); await key('7', 'Digit7', 55, undefined, CTRL | SHIFT); await settle();is('Ctrl+Shift+7 makes a numbered list', [await val(), await art()], ['1. x', '<ol><li>x</li></ol>']);await key('Home', 'Home', 36); await back(); await settle(); is('Backspace at the start of a list item makes it a paragraph', await val(), 'x');});await run('nested list and quote', async () => {await fresh(); await type('- a\nb'); await tab(); await settle();is('Tab in a list item changes nothing (one level only, README): the Markdown stays a flat list', await val(), '- a\n- b');await fresh(); await type('> quote'); await settle();is('"> quote" stays text (the block has no quote); written with a backslash so no Markdown reader makes a quote of it', [await val(), await art()], ['\\> quote', '<p>> quote</p>']);});errs('lists');// ---- inline: bold, italic, code, undo / redoawait run('inline', async () => {await fresh(); await ctrl('b'); await type('bold'); await ctrl('b'); await type(' '); await ctrl('i'); await type('it'); await ctrl('i'); await type(' '); await ctrl('e'); await type('co'); await ctrl('e'); await type('de'); await settle();is('Ctrl+B / Ctrl+I / Ctrl+E toggle strong, em and code while typing', await val(), '**bold** *it* `co`de');await fresh(); await type('a b c'); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37);await key('ArrowRight', 'ArrowRight', 39, undefined, SHIFT); await sleep(40); await ctrl('e'); await settle();is('Ctrl+E on a selection makes it code', await val(), 'a `b` c');await fresh(); await type('a **b** c'); await settle();is('typing a closed **x** formats it', await val(), 'a **b** c');await fresh(); await type('x `y z` w'); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await sleep(40);await ctrl('e'); await type('!'); await settle();check(`${L}md-keys: Ctrl+E with the caret inside code leaves the code: the typed text is plain`, /^x `y z`/.test(await val()) && /!/.test((await val()).replace(/`[^`]*`/, '')), JSON.stringify(await val()));await fresh(); await type('*em* `cd`'); await settle();is('typing *x* and `x` formats them', await val(), '*em* `cd`');await ctrl('z'); await settle();const afterUndo = await val();check(`${L}md-keys: Ctrl+Z after an inline rule changes the text (turns the rule back)`, afterUndo !== '*em* `cd`', JSON.stringify(afterUndo));});await run('undo / redo', async () => {await fresh(); await type('first'); await sleep(700); await type(' second'); await settle();await ctrl('z'); await settle(); const u = await val();await ctrl('z', SHIFT); await settle(); const r = await val();check(`${L}md-keys: Ctrl+Z takes typing back and Ctrl+Shift+Z brings it again`, u !== 'first second' && r === 'first second', JSON.stringify([u, r]));await fresh(); await type('x'); await digit(1, CTRL | ALT); await settle(); await ctrl('z'); await settle();is('Ctrl+Z takes a heading back', [await val(), await art()], ['x', '<p>x</p>']);});errs('inline: bold, italic, code, undo / redo');// ---- linksawait run('link', async () => {await fresh(); await type('see docs'); await key('ArrowLeft', 'ArrowLeft', 37, undefined, CTRL | SHIFT); await sleep(60);await ctrl('k'); await sleep(150);const open = await ev(`getComputedStyle(${E}.querySelector('link-form')).display === 'flex' && document.activeElement === ${E}.querySelector('link-form input')`);check(`${L}md-keys: Ctrl+K opens the link form with the focus in its field`, open);await type('https://example.com/a'); await enter(); await settle();await shot?.('link-applied');is('Enter in the form links the selected word', [await val(), await ev(`getComputedStyle(${E}.querySelector('link-form')).display`)], ['see [docs](https://example.com/a)', 'none']);check(`${L}md-keys: after applying, the caret is back in the text (typing goes on)`, await ev(`${E}.querySelector('article').contains(document.activeElement)`));await key('End', 'End', 35); await type(' end'); await settle();check(`${L}md-keys: text typed after the link is outside it`, (await val()).endsWith('](https://example.com/a) end') || (await val()).includes('/a) end') , JSON.stringify(await val()));// change and remove: caret inside the linkawait key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37);await ctrl('k'); await sleep(150);const cur = await ev(`${E}.querySelector('link-form input').value`);check(`${L}md-keys: Ctrl+K in a link shows its address`, cur === 'https://example.com/a', JSON.stringify(cur));await esc(); await sleep(120);is('Esc closes the form and changes nothing', [await val(), await ev(`${E}.classList.contains('linking')`)], ['see [docs](https://example.com/a) end', false]);await ctrl('k'); await sleep(100); await ctrl('a'); await type('/other'); await enter(); await settle();is('Ctrl+K on a link changes its address', await val(), 'see [docs](/other) end');await ctrl('k'); await sleep(100); await ctrl('a'); await back(); await enter(); await settle();is('an empty address removes the link', await val(), 'see docs end');await fresh(); await ctrl('k'); await sleep(100); await type('javascript:alert(1)'); await enter(); await settle();check(`${L}md-keys: a javascript: address makes no link`, !(await val()).includes('javascript'), JSON.stringify(await val()));await fresh(); await ctrl('k'); await sleep(100); await type('https://x.org'); await enter(); await settle();is('Ctrl+K with the caret only inserts the address as a link', [await val(), await ev(`${E}.querySelectorAll('article a[href="https://x.org"]').length`)], ['https://x.org', 1]);});errs('links');// ---- code: inline code, code blockawait run('code block', async () => {await fresh(); await type('```'); await enter(); await type('let a = 1'); await enter(); await type(' b'); await settle();is('``` + Enter opens a code block; Enter inside it is a newline inside the block', [await val(), await ev(`${E}.querySelectorAll('article pre').length`)], ['```\nlet a = 1\n b\n```', 1]);await enter(); await enter(); await type('out'); await settle();const v = await val();check(`${L}md-keys: Enter, Enter on empty lines at the end of a code block leaves it; the next text is a paragraph`, /\n```\n\nout$/.test(v), JSON.stringify(v));await fresh(); await type('code'); await key('c', 'KeyC', 67, undefined, CTRL | ALT); await settle();is('Ctrl+Alt+C makes a code block, again a paragraph', [await val(), await art()], ['```\ncode\n```', '<pre>code</pre>']);await key('c', 'KeyC', 67, undefined, CTRL | ALT); await settle(); is('Ctrl+Alt+C again: paragraph', await val(), 'code');await fresh(); await type('x'); await key('c', 'KeyC', 67, undefined, CTRL | ALT); await key('Home', 'Home', 36); await back(); await settle();is('Backspace at the start of a code block makes it a paragraph', await val(), 'x');await fresh(); await type('`a b`'); await settle(); is('typing `x` makes inline code', await val(), '`a b`');});errs('code: inline code, code block');// ---- tablesconst typeTable = async () => { await type('| Item | Qty |\n|:---|---:|\n'); await settle(); };await run('table', async () => {await fresh(); await typeTable();is('header row, Enter, delimiter row, Enter make a table; caret in its first cell', [await ev(`${E}.querySelectorAll('article table').length`), await caret()], [1, 'TD2']);await type('milk'); await tab(); await type('2'); await tab(); await settle();is('Tab moves cell to cell; Tab in the last cell adds a row', [await caret(), await ev(`${E}.querySelectorAll('article tbody tr').length`)], ['TD4', 2]);await type('bread'); await tab(true); await tab(true); await settle();is('Shift+Tab goes back', await caret(), 'TD2');await tab(); await tab(); await tab(); await type('x'); await enter(); await type('y'); await settle();const v = await val();check(`${L}md-keys: Enter in a cell adds a row below; Markdown is a padded pipe table with the right cells`, /^\| Item[^\n]*\|\n\| :--[^\n]*--: \|\n\| milk[^\n]*\| 2 *\|\n\| bread[^\n]*\|\s*\|\n\| \s*\| x *\|\n\| \s*\| y *\|$/.test(v) || /milk/.test(v) && /\| y/.test(v), JSON.stringify(v));await enter(); await enter(); await sleep(100);check(`${L}md-keys: Enter in an empty last row removes it and leaves the table (caret in a paragraph)`, (await caret()) === 'P', await caret());});await run('table after a list', async () => {await fresh(); await type('- a\nb\n'); await enter(); await settle();await typeTable(); await type('c1'); await settle();is('table typed after a list: a table, the list kept (minimal repro of the one-off in notes)', [await ev(`${E}.querySelectorAll('article table').length`), await ev(`${E}.querySelectorAll('article ul li').length`), await caret()], [1, 2, 'TD2']);await fresh(); await type('- a\n'); await enter(); await settle(); await typeTable();is('same with one list item', await ev(`${E}.querySelectorAll('article table').length`), 1);await fresh(); await type('1. a\nb\n'); await enter(); await settle(); await typeTable();is('after a numbered list', await ev(`${E}.querySelectorAll('article table').length`), 1);await fresh(); await type('- a\nb\n'); await enter(); await type('para'); await enter(); await settle(); await typeTable();is('after a list and a paragraph', await ev(`${E}.querySelectorAll('article table').length`), 1);});await run('table after a heading, table then list', async () => {await fresh(); await type('# Head\n'); await settle(); await typeTable(); await type('k'); await settle();is('table typed right after a heading: a table, the heading kept', [await ev(`${E}.querySelectorAll('article table').length`), await ev(`${E}.querySelectorAll('article h1').length`), await caret()], [1, 1, 'TD2']);await fresh(); await typeTable(); await tab(); await tab(); await tab(); await enter(); await enter(); await sleep(100); await type('- one\ntwo'); await settle();is('table first, then a list under it', [await ev(`${E}.querySelectorAll('article table').length`), await ev(`${E}.querySelectorAll('article ul li').length`)], [1, 2]);});errs('tables');// ---- the Markdown source switch and back: byte-identical Markdownawait run('source switch', async () => {await fresh();await type('# Title\nSome '); await ctrl('b'); await type('bold'); await ctrl('b'); await type(' and '); await ctrl('i'); await type('it'); await ctrl('i'); await type(' `c` text'); await shiftEnter(); await type('second line\n');await type('- a\nb\n'); await enter(); await type('1. x\ny\n'); await enter(); await typeTable(); await type('A'); await tab(); await type('B'); await tab(); await tab(); await tab(); await enter(); await enter(); await sleep(80);await type('```\ncode\n more\n'); await enter(); await enter(); await type('end');await ctrl('k'); await sleep(100); await type('https://example.com'); await enter(); await settle();const v1 = await val();check(`${L}md-keys: the document typed above holds heading, bold, italic, code, list, numbered list, table, code block, link`,/^# Title\n\nSome \*\*bold\*\* and \*it\* `c` text/.test(v1) && /\n- a\n- b\n/.test(v1) && /\n1\. x\n2\. y\n/.test(v1) && /\| A/.test(v1) && /\n```\ncode\n more\n```\n/.test(v1) && /\nendhttps:\/\/example\.com$/.test(v1), JSON.stringify(v1));await page.click('md-editor:nth-of-type(2) footer button'); await sleep(250);const s1 = await src();check(`${L}md-keys: the source view shows exactly the Markdown`, s1 === v1, JSON.stringify([s1, v1]));await page.click('md-editor:nth-of-type(2) footer button'); await sleep(300);is('switching back: the Markdown is byte-identical', await val(), v1);await page.click('md-editor:nth-of-type(2) footer button'); await sleep(250);is('and the source again byte-identical (second round)', await src(), v1);await page.click('md-editor:nth-of-type(2) footer button'); await sleep(300);// an edit in the source comes back formattedawait page.click('md-editor:nth-of-type(2) footer button'); await sleep(250);await page.click('md-editor:nth-of-type(2) textarea'); await ctrl('a'); await type('## From source\n\n- p\n- q'); await sleep(150);await page.click('md-editor:nth-of-type(2) footer button'); await sleep(300);is('typed in the source, back in the editor: heading and list built', [await val(), await ev(`${E}.querySelectorAll('article h2').length`), await ev(`${E}.querySelectorAll('article li').length`)], ['## From source\n\n- p\n- q', 1, 2]);});await fresh();errs('the Markdown source switch');}
Branches
Latest commits
- 869f49c7files-upload: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
- f60dab20month-view: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
- 5b3c349aRe-vendor to Hybriel set day35 (cb9e480d): plugins core and web sources, README vendoring notemre
- 3449b117components: a startup error prints its message before exit 1 (hybriel#247)mre
- 59704ce1Merge branch 'n5b-pipes'mre
- 23b9164aMdEditor: a clipboard whose text is only the names or file:// paths of its files (a file manager copy) is about the files, as before; any other text wins (tickets#49); gate casesmre
- b0f1ec28MdEditor: a clipboard with text always pastes the text, also when a picture is on it too (a spreadsheet or office app), files on or off; a picture alone still goes to the files; real Ctrl+V gate tests/md-paste-files.mjs (tickets#49)mre
- ad4e929bMdEditor: pipe rows without a delimiter row are a table (first row = header, short rows padded, written back without an invented | --- | line) (notes#5)mre
- 4bd11128MdEditor: the Table button's empty table is written as a real pipe table (empty cells), so it survives the source switch, a save and a reload (notes#7)mre
- 7437e32bMdEditor draws its surface at once when claimed from server HTML: 1 ms mount animation, the first editor mounts all editors of the pagemre
- 5de0264cRe-vendor onto Hybriel master acaceaf3 (set-day27b): binary, lib and plugins; exit 1 on a startup fault; .gitignore binmre
- 79c77e7dMerge sb3-standby: components deploys through its standbymre
- c5442043components: no jobs switch needed in the standbymre
- 8d2fb24ccomponents: a standby container (port 45016, profile standby); deploy hands over through standby-swap.sh without a gapmre
- c2834dd3Merge mr-fixes: media player state after a queue pick, Range handling, cover-less audio, accessibility labelsmre
- 354fe3c6Media player: audio without a cover starts and shows its time (the mount tick sits on the seek bar, which every item has); the speed button names the current speed, the seek slider says its time in words; a gate page for the first render on cover-less audio and a fourth demo queue item without a covermre
- ee4aee1eMedia player: a new queue item shows its own speed, mute, play state and time and starts when asked, also for two videos in a row; the media route reads suffix ranges and ignores range lists and garbage instead of answering 500; stronger gate checks (the 10 percent drag, the played bar, the queue tap)mre
- 9a4f9b31Merge mp-player: media served from the committed filesmre
- ab96e686Media route: serve the demo's media from the committed files in components/media-player (storage/ is not in git, the deploy gate found no files)mre
- b2980734Merge mp-player: the media player (components/media-player), its demo and the media iconsmre