code.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import Delta from 'quill-delta';
  2. import Editor from '../../../core/editor';
  3. describe('Code', function() {
  4. it('format newline', function() {
  5. const editor = this.initialize(Editor, '<p><br></p>');
  6. editor.formatLine(0, 1, { 'code-block': true });
  7. expect(editor.scroll.domNode).toEqualHTML(
  8. `<div class="ql-code-block-container" spellcheck="false">
  9. <div class="ql-code-block"><br></div>
  10. </div>`,
  11. );
  12. });
  13. it('format lines', function() {
  14. const editor = this.initialize(Editor, '<p><em>0123</em></p><p>5678</p>');
  15. editor.formatLine(2, 5, { 'code-block': true });
  16. expect(editor.getDelta()).toEqual(
  17. new Delta()
  18. .insert('0123')
  19. .insert('\n', { 'code-block': true })
  20. .insert('5678')
  21. .insert('\n', { 'code-block': true }),
  22. );
  23. expect(editor.scroll.domNode).toEqualHTML(
  24. `<div class="ql-code-block-container" spellcheck="false">
  25. <div class="ql-code-block">0123</div>
  26. <div class="ql-code-block">5678</div>
  27. </div>`,
  28. );
  29. });
  30. it('remove format', function() {
  31. const editor = this.initialize(
  32. Editor,
  33. '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">0123</div></div>',
  34. );
  35. editor.formatText(4, 1, { 'code-block': false });
  36. expect(editor.getDelta()).toEqual(new Delta().insert('0123\n'));
  37. expect(editor.scroll.domNode).toEqualHTML('<p>0123</p>');
  38. });
  39. it('delete last', function() {
  40. const editor = this.initialize(
  41. Editor,
  42. '<p>0123</p><div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block"><br></div></div><p>5678</p>',
  43. );
  44. editor.deleteText(4, 1);
  45. expect(editor.getDelta()).toEqual(
  46. new Delta()
  47. .insert('0123')
  48. .insert('\n', { 'code-block': true })
  49. .insert('5678\n'),
  50. );
  51. expect(editor.scroll.domNode).toEqualHTML(
  52. '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">0123</div></div><p>5678</p>',
  53. );
  54. });
  55. it('delete merge before', function() {
  56. const editor = this.initialize(
  57. Editor,
  58. '<h1>0123</h1><div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">4567</div></div>',
  59. );
  60. editor.deleteText(4, 1);
  61. expect(editor.getDelta()).toEqual(
  62. new Delta().insert('01234567').insert('\n', { 'code-block': true }),
  63. );
  64. expect(editor.scroll.domNode).toEqualHTML(
  65. '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">01234567</div></div>',
  66. );
  67. });
  68. it('delete merge after', function() {
  69. const editor = this.initialize(
  70. Editor,
  71. '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">0123</div></div><h1>4567</h1>',
  72. );
  73. editor.deleteText(4, 1);
  74. expect(editor.getDelta()).toEqual(
  75. new Delta().insert('01234567').insert('\n', { header: 1 }),
  76. );
  77. expect(editor.scroll.domNode).toEqualHTML('<h1>01234567</h1>');
  78. });
  79. it('delete across before partial merge', function() {
  80. const editor = this.initialize(
  81. Editor,
  82. `<div class="ql-code-block-container" spellcheck="false">
  83. <div class="ql-code-block">01</div>
  84. <div class="ql-code-block">34</div>
  85. <div class="ql-code-block">67</div>
  86. </div>
  87. <h1>90</h1>`,
  88. );
  89. editor.deleteText(7, 3);
  90. expect(editor.getDelta()).toEqual(
  91. new Delta()
  92. .insert('01')
  93. .insert('\n', { 'code-block': true })
  94. .insert('34')
  95. .insert('\n', { 'code-block': true })
  96. .insert('60')
  97. .insert('\n', { header: 1 }),
  98. );
  99. expect(editor.scroll.domNode.innerHTML).toEqualHTML(
  100. `<div class="ql-code-block-container" spellcheck="false">
  101. <div class="ql-code-block">01</div>
  102. <div class="ql-code-block">34</div>
  103. </div>
  104. <h1>60</h1>`,
  105. );
  106. });
  107. it('delete across before no merge', function() {
  108. const editor = this.initialize(
  109. Editor,
  110. `<div class="ql-code-block-container" spellcheck="false">
  111. <div class="ql-code-block">01</div>
  112. <div class="ql-code-block">34</div>
  113. </div>
  114. <h1>6789</h1>`,
  115. );
  116. editor.deleteText(3, 5);
  117. expect(editor.getDelta()).toEqual(
  118. new Delta()
  119. .insert('01')
  120. .insert('\n', { 'code-block': true })
  121. .insert('89')
  122. .insert('\n', { header: 1 }),
  123. );
  124. expect(editor.scroll.domNode.innerHTML).toEqualHTML(
  125. '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">01</div></div><h1>89</h1>',
  126. );
  127. });
  128. it('delete across after', function() {
  129. const editor = this.initialize(
  130. Editor,
  131. `<h1>0123</h1>
  132. <div class="ql-code-block-container" spellcheck="false">
  133. <div class="ql-code-block">56</div>
  134. <div class="ql-code-block">89</div>
  135. </div>`,
  136. );
  137. editor.deleteText(2, 4);
  138. expect(editor.getDelta()).toEqual(
  139. new Delta()
  140. .insert('016')
  141. .insert('\n', { 'code-block': true })
  142. .insert('89')
  143. .insert('\n', { 'code-block': true }),
  144. );
  145. expect(editor.scroll.domNode.innerHTML).toEqualHTML(`
  146. <div class="ql-code-block-container" spellcheck="false">
  147. <div class="ql-code-block">016</div>
  148. <div class="ql-code-block">89</div>
  149. </div>
  150. `);
  151. });
  152. it('replace', function() {
  153. const editor = this.initialize(
  154. Editor,
  155. '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">0123</div></div>',
  156. );
  157. editor.formatText(4, 1, { header: 1 });
  158. expect(editor.getDelta()).toEqual(
  159. new Delta().insert('0123').insert('\n', { header: 1 }),
  160. );
  161. expect(editor.scroll.domNode).toEqualHTML('<h1>0123</h1>');
  162. });
  163. it('replace multiple', function() {
  164. const editor = this.initialize(
  165. Editor,
  166. `
  167. <div class="ql-code-block-container" spellcheck="false">
  168. <div class="ql-code-block">01</div>
  169. <div class="ql-code-block">23</div>
  170. </div>
  171. `,
  172. );
  173. editor.formatText(0, 6, { header: 1 });
  174. expect(editor.getDelta()).toEqual(
  175. new Delta()
  176. .insert('01')
  177. .insert('\n', { header: 1 })
  178. .insert('23')
  179. .insert('\n', { header: 1 }),
  180. );
  181. expect(editor.scroll.domNode).toEqualHTML('<h1>01</h1><h1>23</h1>');
  182. });
  183. it('format imprecise bounds', function() {
  184. const editor = this.initialize(
  185. Editor,
  186. `
  187. <div class="ql-code-block-container" spellcheck="false">
  188. <div class="ql-code-block">01</div>
  189. <div class="ql-code-block">23</div>
  190. <div class="ql-code-block">45</div>
  191. </div>
  192. `,
  193. );
  194. editor.formatText(1, 6, { header: 1 });
  195. expect(editor.getDelta()).toEqual(
  196. new Delta()
  197. .insert('01')
  198. .insert('\n', { header: 1 })
  199. .insert('23')
  200. .insert('\n', { header: 1 })
  201. .insert('45')
  202. .insert('\n', { 'code-block': true }),
  203. );
  204. expect(editor.scroll.domNode.innerHTML).toEqualHTML(
  205. '<h1>01</h1><h1>23</h1><div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">45</div></div>',
  206. );
  207. });
  208. it('format without newline', function() {
  209. const editor = this.initialize(
  210. Editor,
  211. `
  212. <div class="ql-code-block-container" spellcheck="false">
  213. <div class="ql-code-block">01</div>
  214. <div class="ql-code-block">23</div>
  215. <div class="ql-code-block">45</div>
  216. </div>
  217. `,
  218. );
  219. editor.formatText(3, 1, { header: 1 });
  220. expect(editor.getDelta()).toEqual(
  221. new Delta()
  222. .insert('01')
  223. .insert('\n', { 'code-block': true })
  224. .insert('23')
  225. .insert('\n', { 'code-block': true })
  226. .insert('45')
  227. .insert('\n', { 'code-block': true }),
  228. );
  229. expect(editor.scroll.domNode.innerHTML).toEqualHTML(`
  230. <div class="ql-code-block-container" spellcheck="false">
  231. <div class="ql-code-block">01</div>
  232. <div class="ql-code-block">23</div>
  233. <div class="ql-code-block">45</div>
  234. </div>
  235. `);
  236. });
  237. it('format line', function() {
  238. const editor = this.initialize(
  239. Editor,
  240. `
  241. <div class="ql-code-block-container" spellcheck="false">
  242. <div class="ql-code-block">01</div>
  243. <div class="ql-code-block">23</div>
  244. <div class="ql-code-block">45</div>
  245. </div>
  246. `,
  247. );
  248. editor.formatLine(3, 1, { header: 1 });
  249. expect(editor.getDelta()).toEqual(
  250. new Delta()
  251. .insert('01')
  252. .insert('\n', { 'code-block': true })
  253. .insert('23')
  254. .insert('\n', { header: 1 })
  255. .insert('45')
  256. .insert('\n', { 'code-block': true }),
  257. );
  258. expect(editor.scroll.domNode.innerHTML).toEqualHTML(`
  259. <div class="ql-code-block-container" spellcheck="false">
  260. <div class="ql-code-block">01</div>
  261. </div>
  262. <h1>23</h1>
  263. <div class="ql-code-block-container" spellcheck="false">
  264. <div class="ql-code-block">45</div>
  265. </div>
  266. `);
  267. });
  268. it('ignore formatAt', function() {
  269. const editor = this.initialize(
  270. Editor,
  271. '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">0123</div></div>',
  272. );
  273. editor.formatText(1, 1, { bold: true });
  274. expect(editor.getDelta()).toEqual(
  275. new Delta().insert('0123').insert('\n', { 'code-block': true }),
  276. );
  277. expect(editor.scroll.domNode).toEqualHTML(
  278. '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">0123</div></div>',
  279. );
  280. });
  281. it('partial block modification applyDelta', function() {
  282. const editor = this.initialize(
  283. Editor,
  284. `<div class="ql-code-block-container" spellcheck="false">
  285. <div class="ql-code-block">a</div>
  286. <div class="ql-code-block">b</div>
  287. <div class="ql-code-block"><br></div>
  288. </div>`,
  289. );
  290. const delta = new Delta()
  291. .retain(3)
  292. .insert('\n', { 'code-block': true })
  293. .delete(1)
  294. .retain(1, { 'code-block': null });
  295. editor.applyDelta(delta);
  296. expect(editor.scroll.domNode.innerHTML).toEqualHTML(
  297. `<div class="ql-code-block-container" spellcheck="false">
  298. <div class="ql-code-block">a</div>
  299. <div class="ql-code-block">b</div>
  300. </div>
  301. <p><br></p>`,
  302. );
  303. });
  304. });