list.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import Delta from 'quill-delta';
  2. import Editor from '../../../core/editor';
  3. describe('List', function() {
  4. it('add', function() {
  5. const editor = this.initialize(
  6. Editor,
  7. `
  8. <p>0123</p>
  9. <p>5678</p>
  10. <p>0123</p>`,
  11. );
  12. editor.formatText(9, 1, { list: 'ordered' });
  13. expect(editor.getDelta()).toEqual(
  14. new Delta()
  15. .insert('0123\n5678')
  16. .insert('\n', { list: 'ordered' })
  17. .insert('0123\n'),
  18. );
  19. expect(this.container).toEqualHTML(`
  20. <p>0123</p>
  21. <ol><li data-list="ordered">5678</li></ol>
  22. <p>0123</p>
  23. `);
  24. });
  25. it('checklist', function() {
  26. const editor = this.initialize(
  27. Editor,
  28. `
  29. <p>0123</p>
  30. <p>5678</p>
  31. <p>0123</p>
  32. `,
  33. );
  34. editor.scroll.domNode.classList.add('ql-editor');
  35. editor.formatText(4, 1, { list: 'checked' });
  36. editor.formatText(9, 1, { list: 'unchecked' });
  37. expect(editor.getDelta()).toEqual(
  38. new Delta()
  39. .insert('0123')
  40. .insert('\n', { list: 'checked' })
  41. .insert('5678')
  42. .insert('\n', { list: 'unchecked' })
  43. .insert('0123\n'),
  44. );
  45. expect(this.container).toEqualHTML(`
  46. <ol>
  47. <li data-list="checked">0123</li>
  48. <li data-list="unchecked">5678</li>
  49. </ol>
  50. <p>0123</p>
  51. `);
  52. });
  53. it('remove', function() {
  54. const editor = this.initialize(
  55. Editor,
  56. `
  57. <p>0123</p>
  58. <ol><li data-list="ordered">5678</li></ol>
  59. <p>0123</p>
  60. `,
  61. );
  62. editor.formatText(9, 1, { list: null });
  63. expect(editor.getDelta()).toEqual(new Delta().insert('0123\n5678\n0123\n'));
  64. expect(this.container).toEqualHTML(`
  65. <p>0123</p>
  66. <p>5678</p>
  67. <p>0123</p>
  68. `);
  69. });
  70. it('replace', function() {
  71. const editor = this.initialize(
  72. Editor,
  73. `
  74. <p>0123</p>
  75. <ol><li data-list="ordered">5678</li></ol>
  76. <p>0123</p>
  77. `,
  78. );
  79. editor.formatText(9, 1, { list: 'bullet' });
  80. expect(editor.getDelta()).toEqual(
  81. new Delta()
  82. .insert('0123\n5678')
  83. .insert('\n', { list: 'bullet' })
  84. .insert('0123\n'),
  85. );
  86. expect(this.container).toEqualHTML(`
  87. <p>0123</p>
  88. <ol><li data-list="bullet">5678</li></ol>
  89. <p>0123</p>
  90. `);
  91. });
  92. it('replace checklist with bullet', function() {
  93. const editor = this.initialize(
  94. Editor,
  95. `
  96. <ol>
  97. <li data-list="checked">0123</li>
  98. </ol>
  99. `,
  100. );
  101. editor.formatText(4, 1, { list: 'bullet' });
  102. expect(editor.getDelta()).toEqual(
  103. new Delta().insert('0123').insert('\n', { list: 'bullet' }),
  104. );
  105. expect(this.container).toEqualHTML(`
  106. <ol><li data-list="bullet">0123</li></ol>
  107. `);
  108. });
  109. it('replace with attributes', function() {
  110. const editor = this.initialize(
  111. Editor,
  112. '<ol><li data-list="ordered" class="ql-align-center">0123</li></ol>',
  113. );
  114. editor.formatText(4, 1, { list: 'bullet' });
  115. expect(editor.getDelta()).toEqual(
  116. new Delta()
  117. .insert('0123')
  118. .insert('\n', { align: 'center', list: 'bullet' }),
  119. );
  120. expect(this.container).toEqualHTML(
  121. '<ol><li data-list="bullet" class="ql-align-center">0123</li></ol>',
  122. );
  123. });
  124. it('format merge', function() {
  125. const editor = this.initialize(
  126. Editor,
  127. `
  128. <ol><li data-list="ordered">0123</li></ol>
  129. <p>5678</p>
  130. <ol><li data-list="ordered">0123</li></ol>
  131. `,
  132. );
  133. editor.formatText(9, 1, { list: 'ordered' });
  134. expect(editor.getDelta()).toEqual(
  135. new Delta()
  136. .insert('0123')
  137. .insert('\n', { list: 'ordered' })
  138. .insert('5678')
  139. .insert('\n', { list: 'ordered' })
  140. .insert('0123')
  141. .insert('\n', { list: 'ordered' }),
  142. );
  143. expect(this.container).toEqualHTML(`
  144. <ol>
  145. <li data-list="ordered">0123</li>
  146. <li data-list="ordered">5678</li>
  147. <li data-list="ordered">0123</li>
  148. </ol>`);
  149. });
  150. it('delete merge', function() {
  151. const editor = this.initialize(
  152. Editor,
  153. `
  154. <ol><li data-list="ordered">0123</li></ol>
  155. <p>5678</p>
  156. <ol><li data-list="ordered">0123</li></ol>`,
  157. );
  158. editor.deleteText(5, 5);
  159. expect(editor.getDelta()).toEqual(
  160. new Delta()
  161. .insert('0123')
  162. .insert('\n', { list: 'ordered' })
  163. .insert('0123')
  164. .insert('\n', { list: 'ordered' }),
  165. );
  166. expect(this.container).toEqualHTML(`
  167. <ol>
  168. <li data-list="ordered">0123</li>
  169. <li data-list="ordered">0123</li>
  170. </ol>`);
  171. });
  172. it('merge checklist', function() {
  173. const editor = this.initialize(
  174. Editor,
  175. `
  176. <ol><li data-list="checked">0123</li></ol>
  177. <p>5678</p>
  178. <ol><li data-list="checked">0123</li></ol>
  179. `,
  180. );
  181. editor.formatText(9, 1, { list: 'checked' });
  182. expect(editor.getDelta()).toEqual(
  183. new Delta()
  184. .insert('0123')
  185. .insert('\n', { list: 'checked' })
  186. .insert('5678')
  187. .insert('\n', { list: 'checked' })
  188. .insert('0123')
  189. .insert('\n', { list: 'checked' }),
  190. );
  191. expect(this.container).toEqualHTML(`
  192. <ol>
  193. <li data-list="checked">0123</li>
  194. <li data-list="checked">5678</li>
  195. <li data-list="checked">0123</li>
  196. </ol>`);
  197. });
  198. it('empty line interop', function() {
  199. const editor = this.initialize(
  200. Editor,
  201. '<ol><li data-list="ordered"><br></li></ol>',
  202. );
  203. editor.insertText(0, 'Test');
  204. expect(this.container).toEqualHTML(
  205. '<ol><li data-list="ordered">Test</li></ol>',
  206. );
  207. editor.deleteText(0, 4);
  208. expect(this.container).toEqualHTML(
  209. '<ol><li data-list="ordered"><br></li></ol>',
  210. );
  211. });
  212. it('delete multiple items', function() {
  213. const editor = this.initialize(
  214. Editor,
  215. `
  216. <ol>
  217. <li data-list="ordered">0123</li>
  218. <li data-list="ordered">5678</li>
  219. <li data-list="ordered">0123</li>
  220. </ol>`,
  221. );
  222. editor.deleteText(2, 5);
  223. expect(this.container).toEqualHTML(`
  224. <ol>
  225. <li data-list="ordered">0178</li>
  226. <li data-list="ordered">0123</li>
  227. </ol>`);
  228. });
  229. it('delete across last item', function() {
  230. const editor = this.initialize(
  231. Editor,
  232. `
  233. <ol><li data-list="ordered">0123</li></ol>
  234. <p>5678</p>`,
  235. );
  236. editor.deleteText(2, 5);
  237. expect(this.container).toEqualHTML('<p>0178</p>');
  238. });
  239. it('delete partial', function() {
  240. const editor = this.initialize(
  241. Editor,
  242. '<p>0123</p><ol><li data-list="ordered">5678</li></ol>',
  243. );
  244. editor.deleteText(2, 5);
  245. expect(this.container).toEqualHTML(
  246. '<ol><li data-list="ordered">0178</li></ol>',
  247. );
  248. });
  249. it('nested list replacement', function() {
  250. const editor = this.initialize(
  251. Editor,
  252. `
  253. <ol>
  254. <li data-list="bullet">One</li>
  255. <li data-list="bullet" class='ql-indent-1'>Alpha</li>
  256. <li data-list="bullet">Two</li>
  257. </ol>
  258. `,
  259. );
  260. editor.formatLine(1, 10, { list: 'bullet' });
  261. expect(this.container).toEqualHTML(`
  262. <ol>
  263. <li data-list="bullet">One</li>
  264. <li data-list="bullet" class='ql-indent-1'>Alpha</li>
  265. <li data-list="bullet">Two</li>
  266. </ol>
  267. `);
  268. });
  269. it('copy atttributes', function() {
  270. const editor = this.initialize(
  271. Editor,
  272. '<p class="ql-align-center">Test</p>',
  273. );
  274. editor.formatLine(4, 1, { list: 'bullet' });
  275. expect(this.container).toEqualHTML(
  276. '<ol><li data-list="bullet" class="ql-align-center">Test</li></ol>',
  277. );
  278. });
  279. it('insert block embed', function() {
  280. const editor = this.initialize(
  281. Editor,
  282. '<ol><li data-list="ordered">Test</li></ol>',
  283. );
  284. editor.insertEmbed(
  285. 2,
  286. 'video',
  287. 'https://www.youtube.com/embed/QHH3iSeDBLo?showinfo=0',
  288. );
  289. expect(this.container).toEqualHTML(`
  290. <ol><li data-list="ordered">Te</li></ol>
  291. <iframe class="ql-video" frameborder="0" allowfullscreen="true" src="https://www.youtube.com/embed/QHH3iSeDBLo?showinfo=0"></iframe>
  292. <ol><li data-list="ordered">st</li></ol>
  293. `);
  294. });
  295. it('insert block embed at beginning', function() {
  296. const editor = this.initialize(
  297. Editor,
  298. '<ol><li data-list="ordered">Test</li></ol>',
  299. );
  300. editor.insertEmbed(
  301. 0,
  302. 'video',
  303. 'https://www.youtube.com/embed/QHH3iSeDBLo?showinfo=0',
  304. );
  305. expect(this.container).toEqualHTML(`
  306. <iframe class="ql-video" frameborder="0" allowfullscreen="true" src="https://www.youtube.com/embed/QHH3iSeDBLo?showinfo=0"></iframe>
  307. <ol><li data-list="ordered">Test</li></ol>
  308. `);
  309. });
  310. it('insert block embed at end', function() {
  311. const editor = this.initialize(
  312. Editor,
  313. '<ol><li data-list="ordered">Test</li></ol>',
  314. );
  315. editor.insertEmbed(
  316. 4,
  317. 'video',
  318. 'https://www.youtube.com/embed/QHH3iSeDBLo?showinfo=0',
  319. );
  320. expect(this.container).toEqualHTML(`
  321. <ol><li data-list="ordered">Test</li></ol>
  322. <iframe class="ql-video" frameborder="0" allowfullscreen="true" src="https://www.youtube.com/embed/QHH3iSeDBLo?showinfo=0"></iframe>
  323. <ol><li data-list="ordered"><br></li></ol>
  324. `);
  325. });
  326. });