gitea源码

EditorMarkdown.test.ts 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import {initTextareaMarkdown, markdownHandleIndention, textareaSplitLines} from './EditorMarkdown.ts';
  2. test('textareaSplitLines', () => {
  3. let ret = textareaSplitLines('a\nbc\nd', 0);
  4. expect(ret).toEqual({lines: ['a', 'bc', 'd'], lengthBeforePosLine: 0, posLineIndex: 0, inlinePos: 0});
  5. ret = textareaSplitLines('a\nbc\nd', 1);
  6. expect(ret).toEqual({lines: ['a', 'bc', 'd'], lengthBeforePosLine: 0, posLineIndex: 0, inlinePos: 1});
  7. ret = textareaSplitLines('a\nbc\nd', 2);
  8. expect(ret).toEqual({lines: ['a', 'bc', 'd'], lengthBeforePosLine: 2, posLineIndex: 1, inlinePos: 0});
  9. ret = textareaSplitLines('a\nbc\nd', 3);
  10. expect(ret).toEqual({lines: ['a', 'bc', 'd'], lengthBeforePosLine: 2, posLineIndex: 1, inlinePos: 1});
  11. ret = textareaSplitLines('a\nbc\nd', 4);
  12. expect(ret).toEqual({lines: ['a', 'bc', 'd'], lengthBeforePosLine: 2, posLineIndex: 1, inlinePos: 2});
  13. ret = textareaSplitLines('a\nbc\nd', 5);
  14. expect(ret).toEqual({lines: ['a', 'bc', 'd'], lengthBeforePosLine: 5, posLineIndex: 2, inlinePos: 0});
  15. ret = textareaSplitLines('a\nbc\nd', 6);
  16. expect(ret).toEqual({lines: ['a', 'bc', 'd'], lengthBeforePosLine: 5, posLineIndex: 2, inlinePos: 1});
  17. });
  18. test('markdownHandleIndention', () => {
  19. const testInput = (input: string, expected?: string) => {
  20. const inputPos = input.indexOf('|');
  21. input = input.replace('|', '');
  22. const ret = markdownHandleIndention({value: input, selStart: inputPos, selEnd: inputPos});
  23. if (expected === null) {
  24. expect(ret).toEqual({handled: false});
  25. } else {
  26. const expectedPos = expected.indexOf('|');
  27. expected = expected.replace('|', '');
  28. expect(ret).toEqual({
  29. handled: true,
  30. valueSelection: {value: expected, selStart: expectedPos, selEnd: expectedPos},
  31. });
  32. }
  33. };
  34. testInput(`
  35. a|b
  36. `, `
  37. a
  38. |b
  39. `);
  40. testInput(`
  41. 1. a
  42. 2. |
  43. `, `
  44. 1. a
  45. |
  46. `);
  47. testInput(`
  48. |1. a
  49. `, null); // let browser handle it
  50. testInput(`
  51. 1. a
  52. 1. b|c
  53. `, `
  54. 1. a
  55. 2. b
  56. 3. |c
  57. `);
  58. testInput(`
  59. 2. a
  60. 2. b|
  61. 1. x
  62. 1. y
  63. `, `
  64. 1. a
  65. 2. b
  66. 3. |
  67. 1. x
  68. 1. y
  69. `);
  70. testInput(`
  71. 2. a
  72. 2. b
  73. 1. x|
  74. 1. y
  75. `, `
  76. 2. a
  77. 2. b
  78. 1. x
  79. 2. |
  80. 3. y
  81. `);
  82. testInput(`
  83. 1. a
  84. 2. b|
  85. 3. c
  86. `, `
  87. 1. a
  88. 2. b
  89. 3. |
  90. 4. c
  91. `);
  92. testInput(`
  93. 1. a
  94. 1. b
  95. 2. b
  96. 3. b
  97. 4. b
  98. 1. c|
  99. `, `
  100. 1. a
  101. 1. b
  102. 2. b
  103. 3. b
  104. 4. b
  105. 2. c
  106. 3. |
  107. `);
  108. testInput(`
  109. 1. a
  110. 2. a
  111. 3. a
  112. 4. a
  113. 5. a
  114. 6. a
  115. 7. a
  116. 8. a
  117. 9. b|c
  118. `, `
  119. 1. a
  120. 2. a
  121. 3. a
  122. 4. a
  123. 5. a
  124. 6. a
  125. 7. a
  126. 8. a
  127. 9. b
  128. 10. |c
  129. `);
  130. // this is a special case, it's difficult to re-format the parent level at the moment, so leave it to the future
  131. testInput(`
  132. 1. a
  133. 2. b|
  134. 3. c
  135. `, `
  136. 1. a
  137. 1. b
  138. 2. |
  139. 3. c
  140. `);
  141. });
  142. test('EditorMarkdown', () => {
  143. const textarea = document.createElement('textarea');
  144. initTextareaMarkdown(textarea);
  145. type ValueWithCursor = string | {
  146. value: string;
  147. pos: number;
  148. };
  149. const testInput = (input: ValueWithCursor, result: ValueWithCursor) => {
  150. const intputValue = typeof input === 'string' ? input : input.value;
  151. const inputPos = typeof input === 'string' ? intputValue.length : input.pos;
  152. textarea.value = intputValue;
  153. textarea.setSelectionRange(inputPos, inputPos);
  154. const e = new KeyboardEvent('keydown', {key: 'Enter', cancelable: true});
  155. textarea.dispatchEvent(e);
  156. if (!e.defaultPrevented) textarea.value += '\n'; // simulate default behavior
  157. const expectedValue = typeof result === 'string' ? result : result.value;
  158. const expectedPos = typeof result === 'string' ? expectedValue.length : result.pos;
  159. expect(textarea.value).toEqual(expectedValue);
  160. expect(textarea.selectionStart).toEqual(expectedPos);
  161. };
  162. testInput('-', '-\n');
  163. testInput('1.', '1.\n');
  164. testInput('- ', '');
  165. testInput('1. ', '');
  166. testInput({value: '1. \n2. ', pos: 3}, {value: '\n2. ', pos: 0});
  167. testInput('- x', '- x\n- ');
  168. testInput('1. foo', '1. foo\n2. ');
  169. testInput({value: '1. a\n2. b\n3. c', pos: 4}, {value: '1. a\n2. \n3. b\n4. c', pos: 8});
  170. testInput('- [ ]', '- [ ]\n- ');
  171. testInput('- [ ] foo', '- [ ] foo\n- [ ] ');
  172. testInput('* [x] foo', '* [x] foo\n* [ ] ');
  173. testInput('1. [x] foo', '1. [x] foo\n2. [ ] ');
  174. });