unwrap.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import { Client, syncClients, expectClients, clearBuffer } from './utils.js';
  6. describe( 'transform', () => {
  7. let john, kate;
  8. beforeEach( () => {
  9. return Promise.all( [
  10. Client.get( 'john' ).then( client => ( john = client ) ),
  11. Client.get( 'kate' ).then( client => ( kate = client ) )
  12. ] );
  13. } );
  14. afterEach( () => {
  15. clearBuffer();
  16. return Promise.all( [ john.destroy(), kate.destroy() ] );
  17. } );
  18. describe( 'unwrap', () => {
  19. describe( 'by unwrap', () => {
  20. it( 'unwrap two siblings', () => {
  21. john.setData(
  22. '<blockQuote>[]<paragraph>Foo</paragraph></blockQuote>' +
  23. '<blockQuote><paragraph>Bar</paragraph></blockQuote>'
  24. );
  25. kate.setData(
  26. '<blockQuote><paragraph>Foo</paragraph></blockQuote>' +
  27. '<blockQuote>[]<paragraph>Bar</paragraph></blockQuote>'
  28. );
  29. john.unwrap();
  30. kate.unwrap();
  31. syncClients();
  32. expectClients(
  33. '<paragraph>Foo</paragraph>' +
  34. '<paragraph>Bar</paragraph>'
  35. );
  36. } );
  37. it( 'unwrap two siblings then undo', () => {
  38. john.setData(
  39. '<blockQuote>[]<paragraph>Foo</paragraph></blockQuote>' +
  40. '<blockQuote><paragraph>Bar</paragraph></blockQuote>'
  41. );
  42. kate.setData(
  43. '<blockQuote><paragraph>Foo</paragraph></blockQuote>' +
  44. '<blockQuote>[]<paragraph>Bar</paragraph></blockQuote>'
  45. );
  46. john.unwrap();
  47. kate.unwrap();
  48. syncClients();
  49. john.undo();
  50. kate.undo();
  51. syncClients();
  52. expectClients(
  53. '<blockQuote><paragraph>Foo</paragraph></blockQuote>' +
  54. '<blockQuote><paragraph>Bar</paragraph></blockQuote>'
  55. );
  56. } );
  57. it( 'text in different path', () => {
  58. john.setData(
  59. '<blockQuote><paragraph>[]Foo</paragraph></blockQuote>' +
  60. '<blockQuote><paragraph>Bar</paragraph></blockQuote>'
  61. );
  62. kate.setData(
  63. '<blockQuote><paragraph>Foo</paragraph></blockQuote>' +
  64. '<blockQuote><paragraph>[]Bar</paragraph></blockQuote>'
  65. );
  66. john.unwrap();
  67. kate.unwrap();
  68. syncClients();
  69. expectClients(
  70. '<blockQuote>Foo</blockQuote>' +
  71. '<blockQuote>Bar</blockQuote>'
  72. );
  73. } );
  74. it( 'the same element', () => {
  75. john.setData( '<blockQuote>[<paragraph>Foo</paragraph>]</blockQuote>' );
  76. kate.setData( '<blockQuote>[<paragraph>Foo</paragraph>]</blockQuote>' );
  77. john.unwrap();
  78. kate.unwrap();
  79. syncClients();
  80. expectClients( '<paragraph>Foo</paragraph>' );
  81. } );
  82. it( 'the same element, then undo', () => {
  83. john.setData( '<blockQuote>[]<paragraph>Foo</paragraph></blockQuote>' );
  84. kate.setData( '<blockQuote>[]<paragraph>Foo</paragraph></blockQuote>' );
  85. john.unwrap();
  86. kate.unwrap();
  87. syncClients();
  88. expectClients( '<paragraph>Foo</paragraph>' );
  89. john.undo();
  90. syncClients();
  91. // Below would be the expected effect with correct wrap transformation.
  92. // expectClients( '<paragraph>Foo</paragraph>' );
  93. expectClients( '<blockQuote><paragraph>Foo</paragraph></blockQuote>' );
  94. kate.undo();
  95. syncClients();
  96. // Below would be the expected effect with correct wrap transformation.
  97. // expectClients( '<paragraph>Foo</paragraph>' );
  98. expectClients( '<blockQuote><paragraph>Foo</paragraph></blockQuote>' );
  99. } );
  100. } );
  101. describe( 'by delete', () => {
  102. it( 'text from two elements #1', () => {
  103. john.setData( '<blockQuote>[<paragraph>Foo</paragraph>]</blockQuote><paragraph>Bar</paragraph>' );
  104. kate.setData( '<blockQuote><paragraph>Fo[o</paragraph></blockQuote><paragraph>Ba]r</paragraph>' );
  105. john.unwrap();
  106. kate.delete();
  107. syncClients();
  108. expectClients( '<paragraph>For</paragraph>' );
  109. } );
  110. it( 'text in same path', () => {
  111. john.setData( '<blockQuote>[<paragraph>Foo</paragraph>]</blockQuote>' );
  112. kate.setData( '<blockQuote><paragraph>F[oo]</paragraph></blockQuote>' );
  113. john.unwrap();
  114. kate.delete();
  115. syncClients();
  116. expectClients( '<paragraph>F</paragraph>' );
  117. } );
  118. } );
  119. describe( 'by merge', () => {
  120. it( 'element into paragraph #1', () => {
  121. john.setData( '<blockQuote>[<paragraph>Foo</paragraph>]<paragraph>Bar</paragraph></blockQuote>' );
  122. kate.setData( '<blockQuote><paragraph>Foo</paragraph>[]<paragraph>Bar</paragraph></blockQuote>' );
  123. john.unwrap();
  124. kate.merge();
  125. syncClients();
  126. expectClients( '<paragraph>FooBar</paragraph>' );
  127. } );
  128. it( 'element into paragraph #2', () => {
  129. john.setData( '<blockQuote>[<paragraph>Foo</paragraph><paragraph>Bar</paragraph>]</blockQuote>' );
  130. kate.setData( '<blockQuote><paragraph>Foo</paragraph>[]<paragraph>Bar</paragraph></blockQuote>' );
  131. john.unwrap();
  132. kate.merge();
  133. syncClients();
  134. expectClients( '<paragraph>FooBar</paragraph>' );
  135. } );
  136. it( 'unwrapped element', () => {
  137. john.setData( '<paragraph>Foo</paragraph><blockQuote>[<paragraph>Bar</paragraph>]</blockQuote>' );
  138. kate.setData( '<paragraph>Foo</paragraph>[]<blockQuote><paragraph>Bar</paragraph></blockQuote>' );
  139. john.unwrap();
  140. kate.merge();
  141. syncClients();
  142. expectClients(
  143. '<paragraph>Foo</paragraph>' +
  144. '<paragraph>Bar</paragraph>'
  145. );
  146. } );
  147. it( 'unwrap merge target element', () => {
  148. john.setData( '<blockQuote>[]<paragraph>Foo</paragraph></blockQuote><blockQuote><paragraph>Bar</paragraph></blockQuote>' );
  149. kate.setData( '<blockQuote><paragraph>Foo</paragraph></blockQuote>[]<blockQuote><paragraph>Bar</paragraph></blockQuote>' );
  150. john.unwrap();
  151. kate.merge();
  152. syncClients();
  153. // Below would be the expected effect with correct wrap transformation.
  154. // expectClients(
  155. // '<paragraph>Foo</paragraph>' +
  156. // '<paragraph>Bar</paragraph>'
  157. // );
  158. expectClients( '<paragraph>Foo</paragraph>' );
  159. } );
  160. } );
  161. describe( 'by split', () => {
  162. it( 'unwrap, then split and undo', () => {
  163. // This is pretty weird case. Right now it cannot be reproduced with the features that we have.
  164. john.editor.model.schema.extend( 'paragraph', { allowIn: 'listItem' } );
  165. john.editor.model.schema.extend( 'blockQuote', { allowIn: 'listItem' } );
  166. kate.editor.model.schema.extend( 'paragraph', { allowIn: 'listItem' } );
  167. kate.editor.model.schema.extend( 'blockQuote', { allowIn: 'listItem' } );
  168. john.setData(
  169. '<listItem>' +
  170. '<blockQuote>' +
  171. '[]' +
  172. '<paragraph>A</paragraph>' +
  173. '<paragraph>B</paragraph>' +
  174. '</blockQuote>' +
  175. '</listItem>'
  176. );
  177. kate.setData(
  178. '<listItem>' +
  179. '<blockQuote>' +
  180. '[]' +
  181. '<paragraph>A</paragraph>' +
  182. '<paragraph>B</paragraph>' +
  183. '</blockQuote>' +
  184. '</listItem>'
  185. );
  186. john.unwrap();
  187. syncClients();
  188. expectClients(
  189. '<listItem>' +
  190. '<paragraph>A</paragraph>' +
  191. '<paragraph>B</paragraph>' +
  192. '</listItem>'
  193. );
  194. john.undo();
  195. kate.setSelection( [ 0, 1 ] );
  196. kate.split();
  197. syncClients();
  198. // Below would be the expected effect with correct wrap transformation.
  199. // expectClients(
  200. // '<listItem>' +
  201. // '<paragraph>A</paragraph>' +
  202. // '</listItem>' +
  203. // '<listItem>' +
  204. // '<paragraph>B</paragraph>' +
  205. // '</listItem>'
  206. // );
  207. expectClients(
  208. '<listItem><blockQuote><paragraph>A</paragraph><paragraph>B</paragraph></blockQuote></listItem><listItem></listItem>'
  209. );
  210. kate.undo();
  211. syncClients();
  212. // Below would be the expected effect with correct wrap transformation.
  213. // expectClients(
  214. // '<listItem>' +
  215. // '<paragraph>A</paragraph>' +
  216. // '<paragraph>B</paragraph>' +
  217. // '</listItem>'
  218. // );
  219. expectClients(
  220. '<listItem><blockQuote><paragraph>A</paragraph><paragraph>B</paragraph></blockQuote></listItem>'
  221. );
  222. } );
  223. } );
  224. describe( 'by wrap', () => {
  225. it( 'unwrap, then undo and wrap #1', () => {
  226. john.setData(
  227. '<blockQuote>' +
  228. '[]' +
  229. '<paragraph>A</paragraph>' +
  230. '<paragraph>B</paragraph>' +
  231. '<paragraph>C</paragraph>' +
  232. '</blockQuote>'
  233. );
  234. kate.setData(
  235. '<blockQuote>' +
  236. '[]' +
  237. '<paragraph>A</paragraph>' +
  238. '<paragraph>B</paragraph>' +
  239. '<paragraph>C</paragraph>' +
  240. '</blockQuote>'
  241. );
  242. john.unwrap();
  243. syncClients();
  244. expectClients(
  245. '<paragraph>A</paragraph>' +
  246. '<paragraph>B</paragraph>' +
  247. '<paragraph>C</paragraph>'
  248. );
  249. john.undo();
  250. kate.setSelection( [ 1 ], [ 2 ] );
  251. kate.wrap( 'blockQuote' );
  252. syncClients();
  253. expectClients(
  254. '<blockQuote>' +
  255. '<paragraph>A</paragraph>' +
  256. '<paragraph>B</paragraph>' +
  257. '<paragraph>C</paragraph>' +
  258. '</blockQuote>'
  259. );
  260. } );
  261. it( 'unwrap, then undo and wrap #2', () => {
  262. john.setData(
  263. '<paragraph>A</paragraph>' +
  264. '<blockQuote>' +
  265. '[]' +
  266. '<paragraph>B</paragraph>' +
  267. '</blockQuote>' +
  268. '<paragraph>C</paragraph>'
  269. );
  270. kate.setData(
  271. '<paragraph>A</paragraph>' +
  272. '<blockQuote>' +
  273. '[]' +
  274. '<paragraph>B</paragraph>' +
  275. '</blockQuote>' +
  276. '<paragraph>C</paragraph>'
  277. );
  278. john.unwrap();
  279. syncClients();
  280. expectClients(
  281. '<paragraph>A</paragraph>' +
  282. '<paragraph>B</paragraph>' +
  283. '<paragraph>C</paragraph>'
  284. );
  285. john.undo();
  286. kate.setSelection( [ 1 ], [ 2 ] );
  287. kate.wrap( 'blockQuote' );
  288. syncClients();
  289. expectClients(
  290. '<paragraph>A</paragraph>' +
  291. '<blockQuote>' +
  292. '<paragraph>B</paragraph>' +
  293. '</blockQuote>' +
  294. '<paragraph>C</paragraph>'
  295. );
  296. } );
  297. } );
  298. } );
  299. } );