8
0

deletecontent.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../src/model/document';
  6. import deleteContent from '../../src/controller/deletecontent';
  7. import { setData, getData } from '../../src/dev-utils/model';
  8. describe( 'DataController', () => {
  9. let doc;
  10. describe( 'deleteContent', () => {
  11. describe( 'in simple scenarios', () => {
  12. beforeEach( () => {
  13. doc = new Document();
  14. doc.createRoot();
  15. const schema = doc.schema;
  16. schema.registerItem( 'image', '$inline' );
  17. schema.allow( { name: '$text', inside: '$root' } );
  18. schema.allow( { name: 'image', inside: '$root' } );
  19. } );
  20. test(
  21. 'does nothing on collapsed selection',
  22. 'f[]oo',
  23. 'f[]oo'
  24. );
  25. test(
  26. 'deletes single character',
  27. 'f[o]o',
  28. 'f[]o'
  29. );
  30. it( 'deletes single character (backward selection)' , () => {
  31. setData( doc, 'f[o]o', { lastRangeBackward: true } );
  32. deleteContent( doc.selection, doc.batch() );
  33. expect( getData( doc ) ).to.equal( 'f[]o' );
  34. } );
  35. test(
  36. 'deletes whole text',
  37. '[foo]',
  38. '[]'
  39. );
  40. test(
  41. 'deletes whole text between nodes',
  42. '<image></image>[foo]<image></image>',
  43. '<image></image>[]<image></image>'
  44. );
  45. test(
  46. 'deletes an element',
  47. 'x[<image></image>]y',
  48. 'x[]y'
  49. );
  50. test(
  51. 'deletes a bunch of nodes',
  52. 'w[x<image></image>y]z',
  53. 'w[]z'
  54. );
  55. test(
  56. 'does not break things when option.merge passed',
  57. 'w[x<image></image>y]z',
  58. 'w[]z',
  59. { merge: true }
  60. );
  61. } );
  62. describe( 'with text attributes', () => {
  63. beforeEach( () => {
  64. doc = new Document();
  65. doc.createRoot();
  66. const schema = doc.schema;
  67. schema.registerItem( 'image', '$inline' );
  68. schema.registerItem( 'paragraph', '$block' );
  69. schema.allow( { name: '$text', inside: '$root' } );
  70. schema.allow( { name: '$text', attributes: [ 'bold', 'italic' ] } );
  71. } );
  72. it( 'deletes characters (first half has attrs)', () => {
  73. setData( doc, '<$text bold="true">fo[o</$text>b]ar' );
  74. deleteContent( doc.selection, doc.batch() );
  75. expect( getData( doc ) ).to.equal( '<$text bold="true">fo[]</$text>ar' );
  76. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  77. } );
  78. it( 'deletes characters (2nd half has attrs)', () => {
  79. setData( doc, 'fo[o<$text bold="true">b]ar</$text>' );
  80. deleteContent( doc.selection, doc.batch() );
  81. expect( getData( doc ) ).to.equal( 'fo[]<$text bold="true">ar</$text>' );
  82. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  83. } );
  84. it( 'clears selection attrs when emptied content', () => {
  85. setData( doc, '<paragraph>x</paragraph><paragraph>[<$text bold="true">foo</$text>]</paragraph><paragraph>y</paragraph>' );
  86. deleteContent( doc.selection, doc.batch() );
  87. expect( getData( doc ) ).to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  88. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  89. } );
  90. it( 'leaves selection attributes when text contains them', () => {
  91. setData(
  92. doc,
  93. '<paragraph>x<$text bold="true">a[foo]b</$text>y</paragraph>',
  94. {
  95. selectionAttributes: {
  96. bold: true
  97. }
  98. }
  99. );
  100. deleteContent( doc.selection, doc.batch() );
  101. expect( getData( doc ) ).to.equal( '<paragraph>x<$text bold="true">a[]b</$text>y</paragraph>' );
  102. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  103. } );
  104. } );
  105. // Note: The algorithm does not care what kind of it's merging as it knows nothing useful about these elements.
  106. // In most cases it handles all elements like you'd expect to handle block elements in HTML. However,
  107. // in some scenarios where the tree depth is bigger results may be hard to justify. In fact, such cases
  108. // should not happen unless we're talking about lists or tables, but these features will need to cover
  109. // their scenarios themselves. In all generic scenarios elements are never nested.
  110. //
  111. // You may also be thinking – but I don't want my elements to be merged. It means that there are some special rules,
  112. // like – multiple editing hosts (cE=true/false in use) or block limit elements like <td>.
  113. // Those case should, again, be handled by their specific implementations.
  114. describe( 'in multi-element scenarios', () => {
  115. beforeEach( () => {
  116. doc = new Document();
  117. doc.createRoot();
  118. const schema = doc.schema;
  119. schema.registerItem( 'paragraph', '$block' );
  120. schema.registerItem( 'heading1', '$block' );
  121. schema.registerItem( 'pchild' );
  122. schema.registerItem( 'image', '$inline' );
  123. schema.allow( { name: 'pchild', inside: 'paragraph' } );
  124. schema.allow( { name: '$text', inside: 'pchild' } );
  125. schema.allow( { name: 'paragraph', attributes: [ 'align' ] } );
  126. } );
  127. test(
  128. 'do not merge when no need to',
  129. '<paragraph>x</paragraph><paragraph>[foo]</paragraph><paragraph>y</paragraph>',
  130. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>',
  131. { merge: true }
  132. );
  133. test(
  134. 'merges second element into the first one (same name)',
  135. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  136. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>',
  137. { merge: true }
  138. );
  139. test(
  140. 'does not merge second element into the first one (same name, !option.merge)',
  141. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  142. '<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>'
  143. );
  144. test(
  145. 'merges second element into the first one (same name)',
  146. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  147. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>',
  148. { merge: true }
  149. );
  150. test(
  151. 'merges second element into the first one (different name)',
  152. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  153. '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>',
  154. { merge: true }
  155. );
  156. it( 'merges second element into the first one (different name, backward selection)', () => {
  157. setData(
  158. doc,
  159. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  160. { lastRangeBackward: true }
  161. );
  162. deleteContent( doc.selection, doc.batch(), { merge: true } );
  163. expect( getData( doc ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
  164. } );
  165. test(
  166. 'merges second element into the first one (different attrs)',
  167. '<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  168. '<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>',
  169. { merge: true }
  170. );
  171. test(
  172. 'merges second element to an empty first element',
  173. '<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
  174. '<paragraph>x</paragraph><heading1>[]o</heading1><paragraph>y</paragraph>',
  175. { merge: true }
  176. );
  177. test(
  178. 'merges elements when deep nested',
  179. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
  180. '<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>',
  181. { merge: true }
  182. );
  183. // For code coverage reasons.
  184. test(
  185. 'merges element when selection is in two consecutive nodes even when it is empty',
  186. '<paragraph>foo[</paragraph><paragraph>]bar</paragraph>',
  187. '<paragraph>foo[]bar</paragraph>',
  188. { merge: true }
  189. );
  190. // If you disagree with this case please read the notes before this section.
  191. test(
  192. 'merges elements when left end deep nested',
  193. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph>',
  194. '<paragraph>x<pchild>fo[]</pchild>ary</paragraph>',
  195. { merge: true }
  196. );
  197. // If you disagree with this case please read the notes before this section.
  198. test(
  199. 'merges elements when right end deep nested',
  200. '<paragraph>xfo[o</paragraph><paragraph><pchild>b]ar</pchild>y<image></image></paragraph>',
  201. '<paragraph>xfo[]<pchild>ar</pchild>y<image></image></paragraph>',
  202. { merge: true }
  203. );
  204. test(
  205. 'merges elements when more content in the right branch',
  206. '<paragraph>xfo[o</paragraph><paragraph>b]a<pchild>r</pchild>y</paragraph>',
  207. '<paragraph>xfo[]a<pchild>r</pchild>y</paragraph>',
  208. { merge: true }
  209. );
  210. test(
  211. 'leaves just one element when all selected',
  212. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  213. '<heading1>[]</heading1>',
  214. { merge: true }
  215. );
  216. it( 'uses remove delta instead of merge delta if merged element is empty', () => {
  217. setData( doc, '<paragraph>ab[cd</paragraph><paragraph>efgh]</paragraph>' );
  218. const batch = doc.batch();
  219. const spyMerge = sinon.spy( batch, 'merge' );
  220. const spyRemove = sinon.spy( batch, 'remove' );
  221. deleteContent( doc.selection, batch, { merge: true } );
  222. expect( getData( doc ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  223. expect( spyMerge.called ).to.be.false;
  224. expect( spyRemove.called ).to.be.true;
  225. } );
  226. } );
  227. describe( 'in element selections scenarios', () => {
  228. beforeEach( () => {
  229. doc = new Document();
  230. // <p> like root.
  231. doc.createRoot( 'paragraph', 'paragraphRoot' );
  232. // <body> like root.
  233. doc.createRoot( '$root', 'bodyRoot' );
  234. // Special root which allows only blockWidgets inside itself.
  235. doc.createRoot( 'restrictedRoot', 'restrictedRoot' );
  236. const schema = doc.schema;
  237. schema.registerItem( 'image', '$inline' );
  238. schema.registerItem( 'paragraph', '$block' );
  239. schema.registerItem( 'heading1', '$block' );
  240. schema.registerItem( 'blockWidget' );
  241. schema.registerItem( 'restrictedRoot' );
  242. schema.allow( { name: '$block', inside: '$root' } );
  243. schema.allow( { name: 'blockWidget', inside: '$root' } );
  244. schema.allow( { name: 'blockWidget', inside: 'restrictedRoot' } );
  245. } );
  246. // See also "in simple scenarios => deletes an element".
  247. it( 'deletes two inline elements', () => {
  248. setData(
  249. doc,
  250. 'x[<image></image><image></image>]z',
  251. { rootName: 'paragraphRoot' }
  252. );
  253. deleteContent( doc.selection, doc.batch() );
  254. expect( getData( doc, { rootName: 'paragraphRoot' } ) )
  255. .to.equal( 'x[]z' );
  256. } );
  257. it( 'creates a paragraph when text is not allowed (paragraph selected)', () => {
  258. setData(
  259. doc,
  260. '<paragraph>x</paragraph>[<paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  261. { rootName: 'bodyRoot' }
  262. );
  263. deleteContent( doc.selection, doc.batch() );
  264. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  265. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  266. } );
  267. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  268. setData(
  269. doc,
  270. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  271. { rootName: 'bodyRoot' }
  272. );
  273. deleteContent( doc.selection, doc.batch() );
  274. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  275. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  276. } );
  277. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  278. setData(
  279. doc,
  280. '<paragraph>x</paragraph>[<heading1>yyy</heading1>]<paragraph>z</paragraph>',
  281. { rootName: 'bodyRoot' }
  282. );
  283. deleteContent( doc.selection, doc.batch() );
  284. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  285. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  286. } );
  287. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  288. setData(
  289. doc,
  290. '<paragraph>x</paragraph>[<heading1>yyy</heading1><paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  291. { rootName: 'bodyRoot' }
  292. );
  293. deleteContent( doc.selection, doc.batch() );
  294. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  295. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  296. } );
  297. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  298. setData(
  299. doc,
  300. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  301. { rootName: 'bodyRoot' }
  302. );
  303. deleteContent( doc.selection, doc.batch() );
  304. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  305. .to.equal( '<paragraph>[]</paragraph>' );
  306. } );
  307. it( 'does not create a paragraph when it is not allowed', () => {
  308. setData(
  309. doc,
  310. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  311. { rootName: 'restrictedRoot' }
  312. );
  313. deleteContent( doc.selection, doc.batch() );
  314. expect( getData( doc, { rootName: 'restrictedRoot' } ) )
  315. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  316. } );
  317. } );
  318. function test( title, input, output, options ) {
  319. it( title, () => {
  320. setData( doc, input );
  321. deleteContent( doc.selection, doc.batch(), options );
  322. expect( getData( doc ) ).to.equal( output );
  323. } );
  324. }
  325. } );
  326. } );