8
0

deletecontent.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. describe( 'object elements', () => {
  227. beforeEach( () => {
  228. const schema = doc.schema;
  229. schema.registerItem( 'blockWidget' );
  230. schema.registerItem( 'nestedEditable' );
  231. schema.allow( { name: 'blockWidget', inside: '$root' } );
  232. schema.allow( { name: 'nestedEditable', inside: 'blockWidget' } );
  233. schema.allow( { name: '$text', inside: 'nestedEditable' } );
  234. schema.objects.add( 'blockWidget' );
  235. schema.limits.add( 'nestedEditable' );
  236. } );
  237. test(
  238. 'does not merge an object element (if it is first)',
  239. '<blockWidget><nestedEditable>fo[o</nestedEditable></blockWidget><paragraph>b]ar</paragraph>',
  240. '<blockWidget><nestedEditable>fo[]</nestedEditable></blockWidget><paragraph>ar</paragraph>',
  241. { merge: true }
  242. );
  243. test(
  244. 'does not merge an object element (if it is second)',
  245. '<paragraph>ba[r</paragraph><blockWidget><nestedEditable>f]oo</nestedEditable></blockWidget>',
  246. '<paragraph>ba[]</paragraph><blockWidget><nestedEditable>oo</nestedEditable></blockWidget>',
  247. { merge: true }
  248. );
  249. } );
  250. } );
  251. describe( 'in element selections scenarios', () => {
  252. beforeEach( () => {
  253. doc = new Document();
  254. // <p> like root.
  255. doc.createRoot( 'paragraph', 'paragraphRoot' );
  256. // <body> like root.
  257. doc.createRoot( '$root', 'bodyRoot' );
  258. // Special root which allows only blockWidgets inside itself.
  259. doc.createRoot( 'restrictedRoot', 'restrictedRoot' );
  260. const schema = doc.schema;
  261. schema.registerItem( 'image', '$inline' );
  262. schema.registerItem( 'paragraph', '$block' );
  263. schema.registerItem( 'heading1', '$block' );
  264. schema.registerItem( 'blockWidget' );
  265. schema.registerItem( 'restrictedRoot' );
  266. schema.allow( { name: '$block', inside: '$root' } );
  267. schema.allow( { name: 'blockWidget', inside: '$root' } );
  268. schema.allow( { name: 'blockWidget', inside: 'restrictedRoot' } );
  269. } );
  270. // See also "in simple scenarios => deletes an element".
  271. it( 'deletes two inline elements', () => {
  272. setData(
  273. doc,
  274. 'x[<image></image><image></image>]z',
  275. { rootName: 'paragraphRoot' }
  276. );
  277. deleteContent( doc.selection, doc.batch() );
  278. expect( getData( doc, { rootName: 'paragraphRoot' } ) )
  279. .to.equal( 'x[]z' );
  280. } );
  281. it( 'creates a paragraph when text is not allowed (paragraph selected)', () => {
  282. setData(
  283. doc,
  284. '<paragraph>x</paragraph>[<paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  285. { rootName: 'bodyRoot' }
  286. );
  287. deleteContent( doc.selection, doc.batch() );
  288. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  289. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  290. } );
  291. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  292. setData(
  293. doc,
  294. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  295. { rootName: 'bodyRoot' }
  296. );
  297. deleteContent( doc.selection, doc.batch() );
  298. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  299. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  300. } );
  301. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  302. setData(
  303. doc,
  304. '<paragraph>x</paragraph>[<heading1>yyy</heading1>]<paragraph>z</paragraph>',
  305. { rootName: 'bodyRoot' }
  306. );
  307. deleteContent( doc.selection, doc.batch() );
  308. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  309. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  310. } );
  311. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  312. setData(
  313. doc,
  314. '<paragraph>x</paragraph>[<heading1>yyy</heading1><paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  315. { rootName: 'bodyRoot' }
  316. );
  317. deleteContent( doc.selection, doc.batch() );
  318. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  319. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  320. } );
  321. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  322. setData(
  323. doc,
  324. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  325. { rootName: 'bodyRoot' }
  326. );
  327. deleteContent( doc.selection, doc.batch() );
  328. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  329. .to.equal( '<paragraph>[]</paragraph>' );
  330. } );
  331. it( 'does not create a paragraph when it is not allowed', () => {
  332. setData(
  333. doc,
  334. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  335. { rootName: 'restrictedRoot' }
  336. );
  337. deleteContent( doc.selection, doc.batch() );
  338. expect( getData( doc, { rootName: 'restrictedRoot' } ) )
  339. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  340. } );
  341. } );
  342. describe( 'integration with inline limit elements', () => {
  343. beforeEach( () => {
  344. doc = new Document();
  345. doc.createRoot();
  346. const schema = doc.schema;
  347. schema.registerItem( 'inlineLimit' );
  348. schema.allow( { name: 'inlineLimit', inside: '$root' } );
  349. schema.allow( { name: '$text', inside: 'inlineLimit' } );
  350. schema.limits.add( 'inlineLimit' );
  351. schema.allow( { name: '$inline', inside: '$root' } );
  352. schema.registerItem( 'x' );
  353. schema.allow( { name: '$text', inside: 'x' } );
  354. schema.allow( { name: 'x', inside: '$root' } );
  355. } );
  356. test(
  357. 'should delete inside inline limit element',
  358. '<inlineLimit>foo [bar] baz</inlineLimit>',
  359. '<inlineLimit>foo [] baz</inlineLimit>'
  360. );
  361. test(
  362. 'should delete whole inline limit element',
  363. 'x[<inlineLimit>foo bar</inlineLimit>]x',
  364. 'x[]x'
  365. );
  366. test(
  367. 'should delete from two inline limit elements',
  368. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  369. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  370. );
  371. test(
  372. 'merge option should be ignored if both elements are limits',
  373. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  374. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>',
  375. { merge: true }
  376. );
  377. test(
  378. 'merge option should be ignored if the first element is a limit',
  379. '<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
  380. '<inlineLimit>foo []</inlineLimit><x> qux</x>',
  381. { merge: true }
  382. );
  383. test(
  384. 'merge option should be ignored if the second element is a limit',
  385. '<x>baz [qux</x><inlineLimit>foo] bar</inlineLimit>',
  386. '<x>baz []</x><inlineLimit> bar</inlineLimit>',
  387. { merge: true }
  388. );
  389. } );
  390. describe( 'integration with block limit elements', () => {
  391. beforeEach( () => {
  392. doc = new Document();
  393. doc.createRoot();
  394. const schema = doc.schema;
  395. schema.registerItem( 'blockLimit' );
  396. schema.allow( { name: 'blockLimit', inside: '$root' } );
  397. schema.allow( { name: '$block', inside: 'blockLimit' } );
  398. schema.limits.add( 'blockLimit' );
  399. schema.registerItem( 'paragraph', '$block' );
  400. } );
  401. test(
  402. 'should delete inside block limit element',
  403. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  404. '<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>'
  405. );
  406. test(
  407. 'should delete inside block limit element',
  408. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  409. '<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>',
  410. { merge: true }
  411. );
  412. test(
  413. 'should delete whole block limit element',
  414. '<paragraph>x</paragraph>[<blockLimit><paragraph>foo</paragraph></blockLimit>]<paragraph>x</paragraph>',
  415. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>'
  416. );
  417. test(
  418. 'should delete from two block limit elements',
  419. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  420. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  421. );
  422. test(
  423. 'merge option should be ignored if any of the elements is a limit',
  424. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  425. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>',
  426. { merge: true }
  427. );
  428. } );
  429. function test( title, input, output, options ) {
  430. it( title, () => {
  431. setData( doc, input );
  432. deleteContent( doc.selection, doc.batch(), options );
  433. expect( getData( doc ) ).to.equal( output );
  434. } );
  435. }
  436. } );
  437. } );