8
0

insertcontent.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. import Document from '/ckeditor5/engine/model/document.js';
  7. import DataController from '/ckeditor5/engine/datacontroller.js';
  8. import insertContent from '/ckeditor5/engine/datacontroller/insertcontent.js';
  9. import ViewDocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  10. import ModelDocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
  11. import Text from '/ckeditor5/engine/model/text.js';
  12. import { setData, getData, parse } from '/ckeditor5/engine/dev-utils/model.js';
  13. describe( 'DataController', () => {
  14. let doc, dataController;
  15. describe( 'insertContent', () => {
  16. describe( 'in simple scenarios', () => {
  17. beforeEach( () => {
  18. doc = new Document();
  19. doc.createRoot();
  20. dataController = new DataController( doc );
  21. const schema = doc.schema;
  22. schema.registerItem( 'image', '$inline' );
  23. schema.registerItem( 'disallowedElement' );
  24. schema.allow( { name: '$text', inside: '$root' } );
  25. schema.allow( { name: 'image', inside: '$root' } );
  26. // Otherwise it won't be passed to the temporary model fragment used inside insertContent().
  27. schema.allow( { name: 'disallowedElement', inside: '$clipboardHolder' } );
  28. schema.objects.add( 'image' );
  29. } );
  30. test(
  31. 'inserts one text node',
  32. 'xyz',
  33. 'f[]oo',
  34. 'fxyz[]oo'
  35. );
  36. test(
  37. 'inserts one text node (at the end)',
  38. 'xyz',
  39. 'foo[]',
  40. 'fooxyz[]'
  41. );
  42. test(
  43. 'inserts an element',
  44. '<image></image>',
  45. 'f[]oo',
  46. 'f<image></image>[]oo'
  47. );
  48. test(
  49. 'inserts a text and an element',
  50. 'xyz<image></image>',
  51. 'f[]oo',
  52. 'fxyz<image></image>[]oo'
  53. );
  54. test(
  55. 'strips a disallowed element',
  56. '<disallowedElement>xyz</disallowedElement>',
  57. 'f[]oo',
  58. 'fxyz[]oo'
  59. );
  60. test(
  61. 'deletes selection before inserting the content',
  62. 'x',
  63. 'f[abc]oo',
  64. 'fx[]oo'
  65. );
  66. describe( 'spaces handling', () => {
  67. // Note: spaces in the view are not encoded like in the DOM, so subsequent spaces must be
  68. // inserted into the model as is. The conversion to nbsps happen on view<=>DOM conversion.
  69. test(
  70. 'inserts one space',
  71. new Text( ' ' ),
  72. 'f[]oo',
  73. 'f []oo'
  74. );
  75. test(
  76. 'inserts three spaces',
  77. new Text( ' ' ),
  78. 'f[]oo',
  79. 'f []oo'
  80. );
  81. test(
  82. 'inserts spaces at the end',
  83. new Text( ' ' ),
  84. 'foo[]',
  85. 'foo []'
  86. );
  87. test(
  88. 'inserts one nbsp',
  89. new Text( '\u200a' ),
  90. 'f[]oo',
  91. 'f\u200a[]oo'
  92. );
  93. test(
  94. 'inserts word surrounded by spaces',
  95. new Text( ' xyz ' ),
  96. 'f[]oo',
  97. 'f xyz []oo'
  98. );
  99. } );
  100. } );
  101. describe( 'in blocks', () => {
  102. beforeEach( () => {
  103. doc = new Document();
  104. doc.createRoot();
  105. dataController = new DataController( doc );
  106. const schema = doc.schema;
  107. schema.registerItem( 'paragraph', '$block' );
  108. schema.registerItem( 'heading1', '$block' );
  109. schema.registerItem( 'heading2', '$block' );
  110. schema.registerItem( 'blockWidget' );
  111. schema.registerItem( 'inlineWidget' );
  112. schema.allow( { name: 'blockWidget', inside: '$root' } );
  113. schema.allow( { name: 'inlineWidget', inside: '$block' } );
  114. schema.allow( { name: 'inlineWidget', inside: '$clipboardHolder' } );
  115. schema.objects.add( 'blockWidget' );
  116. schema.objects.add( 'inlineWidget' );
  117. } );
  118. test(
  119. 'inserts one text node',
  120. 'xyz',
  121. '<paragraph>f[]oo</paragraph>',
  122. '<paragraph>fxyz[]oo</paragraph>'
  123. );
  124. test(
  125. 'inserts one text node to fully selected paragraph',
  126. 'xyz',
  127. '<paragraph>[foo]</paragraph>',
  128. '<paragraph>xyz[]</paragraph>'
  129. );
  130. test(
  131. 'inserts one text node to fully selected paragraphs (from outside)',
  132. 'xyz',
  133. '[<paragraph>foo</paragraph><paragraph>bar</paragraph>]',
  134. '<paragraph>xyz[]</paragraph>'
  135. );
  136. test(
  137. 'merges two blocks before inserting content (p+p)',
  138. 'xyz',
  139. '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>',
  140. '<paragraph>foxyz[]ar</paragraph>'
  141. );
  142. test(
  143. 'inserts inline widget and text',
  144. 'xyz<inlineWidget></inlineWidget>',
  145. '<paragraph>f[]oo</paragraph>',
  146. '<paragraph>fxyz<inlineWidget></inlineWidget>[]oo</paragraph>'
  147. );
  148. // Note: In CKEditor 4 the blocks are not merged, but to KISS we're merging here
  149. // because that's what deleteContent() does.
  150. test(
  151. 'merges two blocks before inserting content (h+p)',
  152. 'xyz',
  153. '<heading1>fo[o</heading1><paragraph>b]ar</paragraph>',
  154. '<heading1>foxyz[]ar</heading1>'
  155. );
  156. describe( 'block to block handling', () => {
  157. // Note: This is temporary implementation which gives a quite poor UX.
  158. // See https://github.com/ckeditor/ckeditor5-engine/issues/652
  159. test(
  160. 'inserts one paragraph',
  161. '<paragraph>xyz</paragraph>',
  162. '<paragraph>f[]oo</paragraph>',
  163. '<paragraph>fxyz[]oo</paragraph>'
  164. );
  165. test(
  166. 'inserts one paragraph (at the end)',
  167. '<paragraph>xyz</paragraph>',
  168. '<paragraph>foo[]</paragraph>',
  169. '<paragraph>fooxyz[]</paragraph>'
  170. );
  171. test(
  172. 'inserts one paragraph into an empty paragraph',
  173. '<paragraph>xyz</paragraph>',
  174. '<paragraph>[]</paragraph>',
  175. '<paragraph>xyz[]</paragraph>'
  176. );
  177. test(
  178. 'inserts one block into a fully selected content',
  179. '<heading2>xyz</heading2>',
  180. '<heading1>[foo</heading1><paragraph>bar]</paragraph>',
  181. '<heading2>xyz[]</heading2>'
  182. );
  183. test(
  184. 'inserts one heading',
  185. '<heading1>xyz</heading1>',
  186. '<paragraph>f[]oo</paragraph>',
  187. '<paragraph>fxyz[]oo</paragraph>'
  188. );
  189. test(
  190. 'inserts two headings',
  191. '<heading1>xxx</heading1><heading1>yyy</heading1>',
  192. '<paragraph>f[]oo</paragraph>',
  193. '<paragraph>fxxx</paragraph><heading1>yyy[]oo</heading1>'
  194. );
  195. test(
  196. 'inserts one object',
  197. '<blockWidget></blockWidget>',
  198. '<paragraph>f[]oo</paragraph>',
  199. '<paragraph>f</paragraph>[<blockWidget></blockWidget>]<paragraph>oo</paragraph>'
  200. );
  201. test(
  202. 'inserts one object (at the end)',
  203. '<blockWidget></blockWidget>',
  204. '<paragraph>foo[]</paragraph>',
  205. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]'
  206. );
  207. test(
  208. 'inserts one object (at the beginning)',
  209. '<blockWidget></blockWidget>',
  210. '<paragraph>[]bar</paragraph>',
  211. '[<blockWidget></blockWidget>]<paragraph>bar</paragraph>'
  212. );
  213. } );
  214. describe( 'mixed content to block', () => {
  215. test(
  216. 'inserts text + paragraph',
  217. 'xxx<paragraph>yyy</paragraph>',
  218. '<paragraph>f[]oo</paragraph>',
  219. '<paragraph>fxxx</paragraph><paragraph>yyy[]oo</paragraph>'
  220. );
  221. test(
  222. 'inserts text + inlineWidget + text + paragraph',
  223. 'xxx<inlineWidget></inlineWidget>yyy<paragraph>zzz</paragraph>',
  224. '<paragraph>f[]oo</paragraph>',
  225. '<paragraph>fxxx<inlineWidget></inlineWidget>yyy</paragraph><paragraph>zzz[]oo</paragraph>'
  226. );
  227. test(
  228. 'inserts text + paragraph (at the beginning)',
  229. 'xxx<paragraph>yyy</paragraph>',
  230. '<paragraph>[]foo</paragraph>',
  231. '<paragraph>xxx</paragraph><paragraph>yyy[]foo</paragraph>'
  232. );
  233. test(
  234. 'inserts text + paragraph (at the end)',
  235. 'xxx<paragraph>yyy</paragraph>',
  236. '<paragraph>foo[]</paragraph>',
  237. '<paragraph>fooxxx</paragraph><paragraph>yyy[]</paragraph>'
  238. );
  239. test(
  240. 'inserts paragraph + text',
  241. '<paragraph>yyy</paragraph>xxx',
  242. '<paragraph>f[]oo</paragraph>',
  243. '<paragraph>fyyy</paragraph><paragraph>xxx[]oo</paragraph>'
  244. );
  245. // This is the expected result, but it was so hard to achieve at this stage that I
  246. // decided to go with the what the next test represents.
  247. // test(
  248. // 'inserts paragraph + text + inlineWidget + text',
  249. // '<paragraph>yyy</paragraph>xxx<inlineWidget></inlineWidget>zzz',
  250. // '<paragraph>f[]oo</paragraph>',
  251. // '<paragraph>fyyy</paragraph><paragraph>xxx<inlineWidget></inlineWidget>zzz[]oo</paragraph>'
  252. // );
  253. // See the comment above.
  254. test(
  255. 'inserts paragraph + text + inlineWidget + text',
  256. '<paragraph>yyy</paragraph>xxx<inlineWidget></inlineWidget>zzz',
  257. '<paragraph>f[]oo</paragraph>',
  258. (
  259. '<paragraph>fyyy</paragraph><paragraph>xxx</paragraph>' +
  260. '<paragraph><inlineWidget></inlineWidget></paragraph>' +
  261. '<paragraph>zzz[]oo</paragraph>'
  262. )
  263. );
  264. test(
  265. 'inserts paragraph + text + paragraph',
  266. '<paragraph>yyy</paragraph>xxx<paragraph>zzz</paragraph>',
  267. '<paragraph>f[]oo</paragraph>',
  268. '<paragraph>fyyy</paragraph><paragraph>xxx</paragraph><paragraph>zzz[]oo</paragraph>'
  269. );
  270. test(
  271. 'inserts paragraph + text (at the beginning)',
  272. '<paragraph>yyy</paragraph>xxx',
  273. '<paragraph>[]foo</paragraph>',
  274. '<paragraph>yyy</paragraph><paragraph>xxx[]foo</paragraph>'
  275. );
  276. test(
  277. 'inserts paragraph + text (at the end)',
  278. '<paragraph>yyy</paragraph>xxx',
  279. '<paragraph>foo[]</paragraph>',
  280. '<paragraph>fooyyy</paragraph><paragraph>xxx[]</paragraph>'
  281. );
  282. test(
  283. 'inserts text + heading',
  284. 'xxx<heading1>yyy</heading1>',
  285. '<paragraph>f[]oo</paragraph>',
  286. '<paragraph>fxxx</paragraph><heading1>yyy[]oo</heading1>'
  287. );
  288. test(
  289. 'inserts paragraph + object',
  290. '<paragraph>xxx</paragraph><blockWidget></blockWidget>',
  291. '<paragraph>f[]oo</paragraph>',
  292. '<paragraph>fxxx</paragraph>[<blockWidget></blockWidget>]<paragraph>oo</paragraph>'
  293. );
  294. test(
  295. 'inserts object + paragraph',
  296. '<blockWidget></blockWidget><paragraph>xxx</paragraph>',
  297. '<paragraph>f[]oo</paragraph>',
  298. '<paragraph>f</paragraph><blockWidget></blockWidget><paragraph>xxx[]oo</paragraph>'
  299. );
  300. } );
  301. describe( 'content over a block object', () => {
  302. test(
  303. 'inserts text',
  304. 'xxx',
  305. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  306. '<paragraph>foo</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  307. );
  308. test(
  309. 'inserts paragraph',
  310. '<paragraph>xxx</paragraph>',
  311. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  312. '<paragraph>foo</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  313. );
  314. test(
  315. 'inserts text + paragraph',
  316. 'yyy<paragraph>xxx</paragraph>',
  317. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  318. '<paragraph>foo</paragraph><paragraph>yyy</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  319. );
  320. test(
  321. 'inserts two blocks',
  322. '<heading1>xxx</heading1><paragraph>yyy</paragraph>',
  323. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  324. '<paragraph>foo</paragraph><heading1>xxx</heading1><paragraph>yyy[]</paragraph><paragraph>bar</paragraph>'
  325. );
  326. test(
  327. 'inserts block object',
  328. '<blockWidget></blockWidget>',
  329. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  330. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' // It's enough, don't worry.
  331. );
  332. test(
  333. 'inserts inline object',
  334. '<inlineWidget></inlineWidget>',
  335. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  336. '<paragraph>foo</paragraph><paragraph><inlineWidget></inlineWidget>[]</paragraph><paragraph>bar</paragraph>'
  337. );
  338. } );
  339. describe( 'content over an inline object', () => {
  340. test(
  341. 'inserts text',
  342. 'xxx',
  343. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  344. '<paragraph>fooxxx[]bar</paragraph>'
  345. );
  346. test(
  347. 'inserts paragraph',
  348. '<paragraph>xxx</paragraph>',
  349. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  350. '<paragraph>fooxxx[]bar</paragraph>'
  351. );
  352. test(
  353. 'inserts text + paragraph',
  354. 'yyy<paragraph>xxx</paragraph>',
  355. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  356. '<paragraph>fooyyy</paragraph><paragraph>xxx[]bar</paragraph>'
  357. );
  358. test(
  359. 'inserts two blocks',
  360. '<heading1>xxx</heading1><paragraph>yyy</paragraph>',
  361. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  362. '<paragraph>fooxxx</paragraph><paragraph>yyy[]bar</paragraph>'
  363. );
  364. test(
  365. 'inserts inline object',
  366. '<inlineWidget></inlineWidget>',
  367. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  368. '<paragraph>foo<inlineWidget></inlineWidget>[]bar</paragraph>'
  369. );
  370. test(
  371. 'inserts block object',
  372. '<blockWidget></blockWidget>',
  373. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  374. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>'
  375. );
  376. } );
  377. } );
  378. describe( 'filtering out', () => {
  379. beforeEach( () => {
  380. doc = new Document();
  381. doc.createRoot();
  382. dataController = new DataController( doc );
  383. const schema = doc.schema;
  384. schema.registerItem( 'paragraph', '$block' );
  385. // Let's use table as an example of content which needs to be filtered out.
  386. schema.registerItem( 'table' );
  387. schema.registerItem( 'td' );
  388. schema.registerItem( 'disallowedWidget' );
  389. schema.allow( { name: 'table', inside: '$clipboardHolder' } );
  390. schema.allow( { name: 'td', inside: '$clipboardHolder' } );
  391. schema.allow( { name: '$block', inside: 'td' } );
  392. schema.allow( { name: '$text', inside: 'td' } );
  393. schema.allow( { name: 'disallowedWidget', inside: '$clipboardHolder' } );
  394. schema.allow( { name: '$text', inside: 'disallowedWidget' } );
  395. schema.objects.add( 'disallowedWidget' );
  396. } );
  397. test(
  398. 'filters out disallowed elements and leaves out the text',
  399. '<table><td>xxx</td><td>yyy</td></table>',
  400. '<paragraph>f[]oo</paragraph>',
  401. '<paragraph>fxxxyyy[]oo</paragraph>'
  402. );
  403. test(
  404. 'filters out disallowed elements and leaves out the paragraphs',
  405. '<table><td><paragraph>xxx</paragraph><paragraph>yyy</paragraph><paragraph>zzz</paragraph></td></table>',
  406. '<paragraph>f[]oo</paragraph>',
  407. '<paragraph>fxxx</paragraph><paragraph>yyy</paragraph><paragraph>zzz[]oo</paragraph>'
  408. );
  409. test(
  410. 'filters out disallowed objects',
  411. '<disallowedWidget>xxx</disallowedWidget>',
  412. '<paragraph>f[]oo</paragraph>',
  413. '<paragraph>f[]oo</paragraph>'
  414. );
  415. } );
  416. } );
  417. // @param {String} title
  418. // @param {engine.model.Item|String} content
  419. function test( title, content, initialData, expectedData ) {
  420. it( title, () => {
  421. setData( doc, initialData );
  422. if ( typeof content == 'string' ) {
  423. content = parse( content, doc.schema, {
  424. context: [ '$clipboardHolder' ]
  425. } );
  426. }
  427. if ( !( content instanceof ModelDocumentFragment ) ) {
  428. content = new ModelDocumentFragment( [ content ] );
  429. }
  430. // Override the convertion so we get exactly the model that we defined in the content param.
  431. // This way we avoid the need to write converters for everything we want to test.
  432. dataController.viewToModel.convert = () => {
  433. return content;
  434. };
  435. insertContent( dataController, doc.batch(), doc.selection, new ViewDocumentFragment() );
  436. expect( getData( doc ) ).to.equal( expectedData );
  437. } );
  438. }
  439. } );