1
1
mirror of https://github.com/go-gitea/gitea synced 2025-01-05 15:34:25 +00:00
gitea/web_src/js/features/comp/EditorMarkdown.test.ts
metiftikci 1a7591d7f9
fix textarea newline handle (#32966)
- Fix cursor position if input newline on middle of lines
- ~Increment number if numbered list~


![image](https://github.com/user-attachments/assets/bcfe2625-11a8-4ea4-9a71-b7ecfe81b2e0)

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2024-12-25 13:03:43 +08:00

42 lines
1.5 KiB
TypeScript

import {initTextareaMarkdown} from './EditorMarkdown.ts';
test('EditorMarkdown', () => {
const textarea = document.createElement('textarea');
initTextareaMarkdown(textarea);
type ValueWithCursor = string | {
value: string;
pos: number;
}
const testInput = (input: ValueWithCursor, result: ValueWithCursor) => {
const intputValue = typeof input === 'string' ? input : input.value;
const inputPos = typeof input === 'string' ? intputValue.length : input.pos;
textarea.value = intputValue;
textarea.setSelectionRange(inputPos, inputPos);
const e = new KeyboardEvent('keydown', {key: 'Enter', cancelable: true});
textarea.dispatchEvent(e);
if (!e.defaultPrevented) textarea.value += '\n'; // simulate default behavior
const expectedValue = typeof result === 'string' ? result : result.value;
const expectedPos = typeof result === 'string' ? expectedValue.length : result.pos;
expect(textarea.value).toEqual(expectedValue);
expect(textarea.selectionStart).toEqual(expectedPos);
};
testInput('-', '-\n');
testInput('1.', '1.\n');
testInput('- ', '');
testInput('1. ', '');
testInput({value: '1. \n2. ', pos: 3}, {value: '\n2. ', pos: 0});
testInput('- x', '- x\n- ');
testInput('1. foo', '1. foo\n1. ');
testInput({value: '1. a\n2. b\n3. c', pos: 4}, {value: '1. a\n1. \n2. b\n3. c', pos: 8});
testInput('- [ ]', '- [ ]\n- ');
testInput('- [ ] foo', '- [ ] foo\n- [ ] ');
testInput('* [x] foo', '* [x] foo\n* [ ] ');
testInput('1. [x] foo', '1. [x] foo\n1. [ ] ');
});