wrap.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. import { Client, syncClients, expectClients, clearBuffer } from './utils.js';
  2. describe( 'transform', () => {
  3. let john, kate;
  4. beforeEach( () => {
  5. return Promise.all( [
  6. Client.get( 'john' ).then( client => ( john = client ) ),
  7. Client.get( 'kate' ).then( client => ( kate = client ) )
  8. ] ).then( () => {
  9. john.editor.model.schema.register( 'div', { inheritAllFrom: 'blockQuote' } );
  10. kate.editor.model.schema.register( 'div', { inheritAllFrom: 'blockQuote' } );
  11. } );
  12. } );
  13. afterEach( () => {
  14. clearBuffer();
  15. return Promise.all( [ john.destroy(), kate.destroy() ] );
  16. } );
  17. describe( 'wrap', () => {
  18. describe( 'by wrap', () => {
  19. it( 'element in different path', () => {
  20. john.setData( '[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph>' );
  21. kate.setData( '<paragraph>Foo</paragraph>[<paragraph>Bar</paragraph>]' );
  22. john.wrap( 'blockQuote' );
  23. kate.wrap( 'div' );
  24. syncClients();
  25. expectClients(
  26. '<blockQuote><paragraph>Foo</paragraph></blockQuote>' +
  27. '<div><paragraph>Bar</paragraph></div>'
  28. );
  29. } );
  30. it( 'the same element', () => {
  31. john.setData( '[<paragraph>Foo</paragraph>]' );
  32. kate.setData( '[<paragraph>Foo</paragraph>]' );
  33. john.wrap( 'blockQuote' );
  34. kate.wrap( 'div' );
  35. syncClients();
  36. // Below would be the expected effect with correct wrap transformation.
  37. // expectClients( '<blockQuote><paragraph>Foo</paragraph></blockQuote>' );
  38. expectClients( '<blockQuote><paragraph>Foo</paragraph></blockQuote><div></div>' );
  39. } );
  40. it( 'intersecting wrap #1', () => {
  41. john.setData( '[<paragraph>Foo</paragraph><paragraph>Bar</paragraph>]<paragraph>Xyz</paragraph>' );
  42. kate.setData( '<paragraph>Foo</paragraph>[<paragraph>Bar</paragraph><paragraph>Xyz</paragraph>]' );
  43. john.wrap( 'blockQuote' );
  44. kate.wrap( 'div' );
  45. syncClients();
  46. // Below would be the expected effect with correct wrap transformation.
  47. // expectClients(
  48. // '<blockQuote>' +
  49. // '<paragraph>Foo</paragraph>' +
  50. // '<paragraph>Bar</paragraph>' +
  51. // '</blockQuote>' +
  52. // '<div>' +
  53. // '<paragraph>Xyz</paragraph>' +
  54. // '</div>'
  55. // );
  56. expectClients(
  57. '<blockQuote><paragraph>Foo</paragraph><div><paragraph>Xyz</paragraph></div><paragraph>Bar</paragraph></blockQuote>'
  58. );
  59. } );
  60. it( 'intersecting wrap #2', () => {
  61. john.setData( '[<paragraph>Foo</paragraph><paragraph>Bar</paragraph><paragraph>Abc</paragraph>]' );
  62. kate.setData( '<paragraph>Foo</paragraph>[<paragraph>Bar</paragraph>]<paragraph>Abc</paragraph>' );
  63. john.wrap( 'blockQuote' );
  64. kate.wrap( 'div' );
  65. syncClients();
  66. // Below would be the expected effect with correct wrap transformation.
  67. // expectClients(
  68. // '<blockQuote>' +
  69. // '<paragraph>Foo</paragraph>' +
  70. // '<paragraph>Bar</paragraph>' +
  71. // '<paragraph>Abc</paragraph>' +
  72. // '</blockQuote>'
  73. // );
  74. expectClients(
  75. '<blockQuote><paragraph>Foo</paragraph><div><paragraph>Bar</paragraph></div><paragraph>Abc</paragraph></blockQuote>'
  76. );
  77. } );
  78. it( 'intersecting wrap #3', () => {
  79. john.setData( '[<paragraph>Foo</paragraph><paragraph>Bar</paragraph>]<paragraph>Abc</paragraph>' );
  80. kate.setData( '[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph><paragraph>Abc</paragraph>' );
  81. john.wrap( 'blockQuote' );
  82. kate.wrap( 'div' );
  83. syncClients();
  84. // Below would be the expected effect with correct wrap transformation.
  85. // expectClients(
  86. // '<blockQuote>' +
  87. // '<paragraph>Foo</paragraph>' +
  88. // '<paragraph>Bar</paragraph>' +
  89. // '</blockQuote>' +
  90. // '<paragraph>Abc</paragraph>'
  91. // );
  92. expectClients(
  93. '<blockQuote><paragraph>Foo</paragraph><paragraph>Bar</paragraph></blockQuote><div></div><paragraph>Abc</paragraph>'
  94. );
  95. } );
  96. it( 'intersecting wrap, then undo #1', () => {
  97. john.setData( '[<paragraph>Foo</paragraph><paragraph>Bar</paragraph>]<paragraph>Abc</paragraph>' );
  98. kate.setData( '<paragraph>Foo</paragraph>[<paragraph>Bar</paragraph><paragraph>Abc</paragraph>]' );
  99. john.wrap( 'blockQuote' );
  100. kate.wrap( 'div' );
  101. syncClients();
  102. // Below would be the expected effect with correct wrap transformation.
  103. // expectClients(
  104. // '<blockQuote>' +
  105. // '<paragraph>Foo</paragraph>' +
  106. // '<paragraph>Bar</paragraph>' +
  107. // '</blockQuote>' +
  108. // '<div>' +
  109. // '<paragraph>Abc</paragraph>' +
  110. // '</div>'
  111. // );
  112. expectClients(
  113. '<blockQuote><paragraph>Foo</paragraph><div><paragraph>Abc</paragraph></div><paragraph>Bar</paragraph></blockQuote>'
  114. );
  115. john.undo();
  116. kate.undo();
  117. syncClients();
  118. // Below would be the expected effect with correct wrap transformation.
  119. // expectClients(
  120. // '<paragraph>Foo</paragraph>' +
  121. // '<paragraph>Bar</paragraph>' +
  122. // '<paragraph>Abc</paragraph>'
  123. // );
  124. expectClients( '<paragraph>Abc</paragraph><paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  125. } );
  126. it( 'intersecting wrap, then undo #2', () => {
  127. john.setData( '[<paragraph>Foo</paragraph><paragraph>Bar</paragraph>]<paragraph>Abc</paragraph>' );
  128. kate.setData( '<paragraph>Foo</paragraph>[<paragraph>Bar</paragraph><paragraph>Abc</paragraph>]' );
  129. john.wrap( 'blockQuote' );
  130. kate.wrap( 'div' );
  131. syncClients();
  132. // Below would be the expected effect with correct wrap transformation.
  133. // expectClients(
  134. // '<blockQuote>' +
  135. // '<paragraph>Foo</paragraph>' +
  136. // '<paragraph>Bar</paragraph>' +
  137. // '</blockQuote>' +
  138. // '<div>' +
  139. // '<paragraph>Abc</paragraph>' +
  140. // '</div>'
  141. // );
  142. expectClients(
  143. '<blockQuote><paragraph>Foo</paragraph><div><paragraph>Abc</paragraph></div><paragraph>Bar</paragraph></blockQuote>'
  144. );
  145. john.undo();
  146. kate.undo();
  147. syncClients();
  148. // Below would be the expected effect with correct wrap transformation.
  149. // expectClients( '<paragraph>Foo</paragraph><paragraph>Bar</paragraph><paragraph>Abc</paragraph>' );
  150. expectClients( '<paragraph>Abc</paragraph><paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  151. } );
  152. it( 'intersecting wrap, then undo #3', () => {
  153. john.setData( '[<paragraph>Foo</paragraph><paragraph>Bar</paragraph>]<paragraph>Abc</paragraph>' );
  154. kate.setData( '[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph><paragraph>Abc</paragraph>' );
  155. john.wrap( 'blockQuote' );
  156. kate.wrap( 'div' );
  157. syncClients();
  158. john.undo();
  159. kate.undo();
  160. syncClients();
  161. expectClients(
  162. '<paragraph>Foo</paragraph>' +
  163. '<paragraph>Bar</paragraph>' +
  164. '<paragraph>Abc</paragraph>'
  165. );
  166. } );
  167. it( 'element and text', () => {
  168. john.setData( '[<paragraph>Foo</paragraph>]' );
  169. kate.setData( '<paragraph>[Foo]</paragraph>' );
  170. john.wrap( 'blockQuote' );
  171. kate.wrap( 'div' );
  172. syncClients();
  173. expectClients( '<blockQuote><paragraph><div>Foo</div></paragraph></blockQuote>' );
  174. } );
  175. } );
  176. describe( 'by unwrap', () => {
  177. it( 'element in different path', () => {
  178. john.setData( '[<paragraph>Foo</paragraph>]<blockQuote><paragraph>Bar</paragraph></blockQuote>' );
  179. kate.setData( '<paragraph>Foo</paragraph><blockQuote>[<paragraph>Bar</paragraph>]</blockQuote>' );
  180. john.wrap( 'blockQuote' );
  181. kate.unwrap();
  182. syncClients();
  183. expectClients(
  184. '<blockQuote><paragraph>Foo</paragraph></blockQuote>' +
  185. '<paragraph>Bar</paragraph>'
  186. );
  187. } );
  188. it( 'text in different path', () => {
  189. john.setData( '<paragraph>[Foo]</paragraph><blockQuote><paragraph>Bar</paragraph></blockQuote>' );
  190. kate.setData( '<paragraph>Foo</paragraph><blockQuote><paragraph>[]Bar</paragraph></blockQuote>' );
  191. john.wrap( 'div' );
  192. kate.unwrap();
  193. syncClients();
  194. expectClients(
  195. '<paragraph><div>Foo</div></paragraph>' +
  196. '<blockQuote>Bar</blockQuote>'
  197. );
  198. } );
  199. it( 'the same element through undo', () => {
  200. john.setData( '[<paragraph>Foo</paragraph>]' );
  201. kate.setData( '<paragraph>[]Foo</paragraph>' );
  202. john.wrap( 'blockQuote' );
  203. syncClients();
  204. expectClients( '<blockQuote><paragraph>Foo</paragraph></blockQuote>' );
  205. kate.setSelection( [ 0, 0 ] );
  206. kate.unwrap();
  207. syncClients();
  208. expectClients( '<paragraph>Foo</paragraph>' );
  209. john.undo();
  210. kate.undo();
  211. syncClients();
  212. expectClients( '<paragraph>Foo</paragraph>' );
  213. } );
  214. it( 'wrap in unwrapped element', () => {
  215. john.setData( '<blockQuote>[<paragraph>Foo</paragraph>]</blockQuote>' );
  216. kate.setData( '<blockQuote>[]<paragraph>Foo</paragraph></blockQuote>' );
  217. john.wrap( 'div' );
  218. kate.unwrap();
  219. syncClients();
  220. // Below would be the expected effect with correct wrap transformation.
  221. // expectClients( '<div><paragraph>Foo</paragraph></div>' );
  222. expectClients( '<paragraph></paragraph>' );
  223. } );
  224. it.skip( 'wrap in unwrapped element, then undo', () => {
  225. // This is interesting scenario actually. Normally in wrap x wrap situation the stronger wrap just wins
  226. // so we won't get incorrect model. But John was actually to make a wrap like this then he had
  227. // <bq><div><p> structure for a while. So it should be possible to revert to it.
  228. john.setData( '<blockQuote>[<paragraph>Foo</paragraph>]</blockQuote>' );
  229. kate.setData( '<blockQuote>[]<paragraph>Foo</paragraph></blockQuote>' );
  230. john.wrap( 'div' );
  231. kate.unwrap();
  232. syncClients();
  233. expectClients( '<div><paragraph>Foo</paragraph></div>' );
  234. kate.undo();
  235. syncClients();
  236. expectClients( '<blockQuote><div><paragraph>Foo</paragraph></div></blockQuote>' );
  237. } );
  238. it.skip( 'the same text, then undo', () => {
  239. // This is interesting scenario actually. Normally in wrap x wrap situation the stronger wrap just wins
  240. // so we won't get incorrect model. But John was actually to make a wrap like this then he had
  241. // <bq><p><div> structure for a while. So it should be possible to revert to it.
  242. john.setData( '<blockQuote><paragraph>[Foo]</paragraph></blockQuote>' );
  243. kate.setData( '<blockQuote><paragraph>[]Foo</paragraph></blockQuote>' );
  244. john.wrap( 'div' );
  245. kate.unwrap();
  246. syncClients();
  247. expectClients( '<blockQuote><div>Foo</div></blockQuote>' );
  248. kate.undo();
  249. syncClients();
  250. expectClients( '<blockQuote><paragraph><div>Foo</div></paragraph></blockQuote>' );
  251. } );
  252. } );
  253. describe( 'by delete', () => {
  254. it( 'text in two elements #1', () => {
  255. john.setData( '[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph>' );
  256. kate.setData( '<paragraph>Fo[o</paragraph><paragraph>Ba]r</paragraph>' );
  257. john.wrap( 'blockQuote' );
  258. kate.delete();
  259. syncClients();
  260. expectClients(
  261. '<blockQuote>' +
  262. '<paragraph>For</paragraph>' +
  263. '</blockQuote>'
  264. );
  265. } );
  266. it( 'text in two elements #2', () => {
  267. john.setData( '<paragraph>[Foo]</paragraph><paragraph>Bar</paragraph>' );
  268. kate.setData( '<paragraph>Fo[o</paragraph><paragraph>Ba]r</paragraph>' );
  269. john.wrap( 'div' );
  270. kate.delete();
  271. syncClients();
  272. expectClients( '<paragraph><div>Fo</div>r</paragraph>' );
  273. } );
  274. it( 'delete all wrapped content', () => {
  275. john.setData( '[<paragraph>Foo</paragraph><paragraph>Bar</paragraph><paragraph>Abc</paragraph>]' );
  276. kate.setData( '<paragraph>[Foo</paragraph><paragraph>Bar</paragraph><paragraph>Ab]c</paragraph>' );
  277. john.wrap( 'blockQuote' );
  278. kate.delete();
  279. syncClients();
  280. expectClients( '<blockQuote><paragraph>c</paragraph></blockQuote>' );
  281. } );
  282. it.skip( 'delete all wrapped content and undo', () => {
  283. john.setData( '[<paragraph>Foo</paragraph><paragraph>Bar</paragraph><paragraph>Abc</paragraph>]' );
  284. kate.setData( '<paragraph>[Foo</paragraph><paragraph>Bar</paragraph><paragraph>Ab]c</paragraph>' );
  285. john.wrap( 'blockQuote' );
  286. kate.delete();
  287. syncClients();
  288. expectClients( '<blockQuote><paragraph>c</paragraph></blockQuote>' );
  289. john.undo();
  290. // There is a bug in undo for Kate.
  291. // Kate's content is: '<blockQuote><paragraph>c</paragraph></blockQuote>'.
  292. // Then goes undo and it returns "Foo" paragraph into block quote, but "Bar" goes after it.
  293. // There is a move (reinsert) x wrap transformation and the move is not included inside the wrap.
  294. kate.undo();
  295. syncClients();
  296. expectClients( '<paragraph>Foo</paragraph><paragraph>Bar</paragraph><paragraph>Abc</paragraph>' );
  297. } );
  298. } );
  299. describe( 'by remove', () => {
  300. it( 'remove the only wrapped element', () => {
  301. john.setData( '[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph>' );
  302. kate.setData( '[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph>' );
  303. john.wrap( 'blockQuote' );
  304. kate.remove();
  305. syncClients();
  306. expectClients( '<paragraph>Bar</paragraph>' );
  307. } );
  308. it( 'remove one of two wrapped elements', () => {
  309. john.setData( '[<paragraph>Foo</paragraph><paragraph>Bar</paragraph>]' );
  310. kate.setData( '[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph>' );
  311. john.wrap( 'blockQuote' );
  312. kate.remove();
  313. syncClients();
  314. expectClients( '<blockQuote><paragraph>Bar</paragraph></blockQuote>' );
  315. } );
  316. it( 'remove all wrapped elements', () => {
  317. john.setData( '[<paragraph>Foo</paragraph><paragraph>Bar</paragraph>]<paragraph>Xyz</paragraph>' );
  318. kate.setData( '[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph><paragraph>Xyz</paragraph>' );
  319. john.wrap( 'blockQuote' );
  320. kate.remove();
  321. kate.setSelection( [ 0 ], [ 1 ] );
  322. kate.remove();
  323. syncClients();
  324. expectClients( '<paragraph>Xyz</paragraph>' );
  325. } );
  326. it( 'remove the only wrapped element with undo', () => {
  327. john.setData( '[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph>' );
  328. kate.setData( '[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph>' );
  329. john.wrap( 'blockQuote' );
  330. kate.remove();
  331. syncClients();
  332. expectClients( '<paragraph>Bar</paragraph>' );
  333. john.undo();
  334. kate.undo();
  335. syncClients();
  336. expectClients( '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  337. } );
  338. it( 'remove one of two wrapped elements with undo', () => {
  339. john.setData( '[<paragraph>Foo</paragraph><paragraph>Bar</paragraph>]' );
  340. kate.setData( '[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph>' );
  341. john.wrap( 'blockQuote' );
  342. kate.remove();
  343. syncClients();
  344. expectClients( '<blockQuote><paragraph>Bar</paragraph></blockQuote>' );
  345. john.undo();
  346. kate.undo();
  347. syncClients();
  348. expectClients( '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  349. } );
  350. it( 'remove all wrapped elements with undo', () => {
  351. john.setData( '[<paragraph>Foo</paragraph><paragraph>Bar</paragraph>]<paragraph>Xyz</paragraph>' );
  352. kate.setData( '[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph><paragraph>Xyz</paragraph>' );
  353. john.wrap( 'blockQuote' );
  354. kate.remove();
  355. kate.setSelection( [ 0 ], [ 1 ] );
  356. kate.remove();
  357. syncClients();
  358. expectClients( '<paragraph>Xyz</paragraph>' );
  359. john.undo();
  360. kate.undo();
  361. kate.undo();
  362. syncClients();
  363. expectClients( '<paragraph>Foo</paragraph><paragraph>Bar</paragraph><paragraph>Xyz</paragraph>' );
  364. } );
  365. } );
  366. describe( 'by merge', () => {
  367. it( 'element into paragraph #1', () => {
  368. john.setData( '[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph>' );
  369. kate.setData( '<paragraph>Foo</paragraph>[]<paragraph>Bar</paragraph>' );
  370. john.wrap( 'blockQuote' );
  371. kate.merge();
  372. syncClients();
  373. expectClients( '<blockQuote><paragraph>FooBar</paragraph></blockQuote>' );
  374. } );
  375. it( 'element into paragraph #2', () => {
  376. john.setData( '<paragraph>[Foo]</paragraph><paragraph>Bar</paragraph>' );
  377. kate.setData( '<paragraph>Foo</paragraph>[]<paragraph>Bar</paragraph>' );
  378. john.wrap( 'div' );
  379. kate.merge();
  380. syncClients();
  381. john.undo();
  382. kate.undo();
  383. syncClients();
  384. expectClients( '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  385. } );
  386. it( 'element into paragraph, then undo', () => {
  387. john.setData( '<paragraph>Foo</paragraph>[<paragraph>Bar</paragraph>]' );
  388. kate.setData( '<paragraph>Foo</paragraph>[]<paragraph>Bar</paragraph>' );
  389. john.wrap( 'blockQuote' );
  390. kate.merge();
  391. syncClients();
  392. expectClients( '<paragraph>FooBar</paragraph>' );
  393. john.undo();
  394. kate.undo();
  395. syncClients();
  396. expectClients( '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  397. } );
  398. } );
  399. } );
  400. } );