8
0

insertcontent.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 + paragraph (at the beginning)',
  223. 'xxx<paragraph>yyy</paragraph>',
  224. '<paragraph>[]foo</paragraph>',
  225. '<paragraph>xxx</paragraph><paragraph>yyy[]foo</paragraph>'
  226. );
  227. test(
  228. 'inserts text + paragraph (at the end)',
  229. 'xxx<paragraph>yyy</paragraph>',
  230. '<paragraph>foo[]</paragraph>',
  231. '<paragraph>fooxxx</paragraph><paragraph>yyy[]</paragraph>'
  232. );
  233. test(
  234. 'inserts paragraph + text',
  235. '<paragraph>yyy</paragraph>xxx',
  236. '<paragraph>f[]oo</paragraph>',
  237. '<paragraph>fyyy</paragraph><paragraph>xxx[]oo</paragraph>'
  238. );
  239. test(
  240. 'inserts paragraph + text + inlineWidget + text',
  241. '<paragraph>yyy</paragraph>xxx<inlineWidget></inlineWidget>zzz',
  242. '<paragraph>f[]oo</paragraph>',
  243. '<paragraph>fyyy</paragraph><paragraph>xxx<inlineWidget></inlineWidget>zzz[]oo</paragraph>'
  244. );
  245. test(
  246. 'inserts paragraph + text (at the beginning)',
  247. '<paragraph>yyy</paragraph>xxx',
  248. '<paragraph>[]foo</paragraph>',
  249. '<paragraph>yyy</paragraph><paragraph>xxx[]foo</paragraph>'
  250. );
  251. test(
  252. 'inserts paragraph + text (at the end)',
  253. '<paragraph>yyy</paragraph>xxx',
  254. '<paragraph>foo[]</paragraph>',
  255. '<paragraph>fooyyy</paragraph><paragraph>xxx[]</paragraph>'
  256. );
  257. test(
  258. 'inserts text + heading',
  259. 'xxx<heading1>yyy</heading1>',
  260. '<paragraph>f[]oo</paragraph>',
  261. '<paragraph>fxxx</paragraph><heading1>yyy[]oo</heading1>'
  262. );
  263. test(
  264. 'inserts paragraph + object',
  265. '<paragraph>xxx</paragraph><blockWidget></blockWidget>',
  266. '<paragraph>f[]oo</paragraph>',
  267. '<paragraph>fxxx</paragraph>[<blockWidget></blockWidget>]<paragraph>oo</paragraph>'
  268. );
  269. test(
  270. 'inserts object + paragraph',
  271. '<blockWidget></blockWidget><paragraph>xxx</paragraph>',
  272. '<paragraph>f[]oo</paragraph>',
  273. '<paragraph>f</paragraph><blockWidget></blockWidget><paragraph>xxx[]oo</paragraph>'
  274. );
  275. } );
  276. describe( 'content over a block object', () => {
  277. test(
  278. 'inserts text',
  279. 'xxx',
  280. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  281. '<paragraph>foo</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  282. );
  283. test(
  284. 'inserts paragraph',
  285. '<paragraph>xxx</paragraph>',
  286. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  287. '<paragraph>foo</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  288. );
  289. test(
  290. 'inserts text + paragraph',
  291. 'yyy<paragraph>xxx</paragraph>',
  292. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  293. '<paragraph>foo</paragraph><paragraph>yyy</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  294. );
  295. test(
  296. 'inserts two blocks',
  297. '<heading1>xxx</heading1><paragraph>yyy</paragraph>',
  298. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  299. '<paragraph>foo</paragraph><heading1>xxx</heading1><paragraph>yyy[]</paragraph><paragraph>bar</paragraph>'
  300. );
  301. test(
  302. 'inserts block object',
  303. '<blockWidget></blockWidget>',
  304. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  305. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' // It's enough, don't worry.
  306. );
  307. test(
  308. 'inserts inline object',
  309. '<inlineWidget></inlineWidget>',
  310. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  311. '<paragraph>foo</paragraph><paragraph><inlineWidget></inlineWidget>[]</paragraph><paragraph>bar</paragraph>'
  312. );
  313. } );
  314. describe( 'content over an inline object', () => {
  315. test(
  316. 'inserts text',
  317. 'xxx',
  318. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  319. '<paragraph>fooxxx[]bar</paragraph>'
  320. );
  321. test(
  322. 'inserts paragraph',
  323. '<paragraph>xxx</paragraph>',
  324. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  325. '<paragraph>fooxxx[]bar</paragraph>'
  326. );
  327. test(
  328. 'inserts text + paragraph',
  329. 'yyy<paragraph>xxx</paragraph>',
  330. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  331. '<paragraph>fooyyy</paragraph><paragraph>xxx[]bar</paragraph>'
  332. );
  333. test(
  334. 'inserts two blocks',
  335. '<heading1>xxx</heading1><paragraph>yyy</paragraph>',
  336. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  337. '<paragraph>fooxxx</paragraph><paragraph>yyy[]bar</paragraph>'
  338. );
  339. test(
  340. 'inserts inline object',
  341. '<inlineWidget></inlineWidget>',
  342. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  343. '<paragraph>foo<inlineWidget></inlineWidget>[]bar</paragraph>'
  344. );
  345. test(
  346. 'inserts block object',
  347. '<blockWidget></blockWidget>',
  348. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  349. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>'
  350. );
  351. } );
  352. } );
  353. describe( 'filtering out', () => {
  354. beforeEach( () => {
  355. doc = new Document();
  356. doc.createRoot();
  357. dataController = new DataController( doc );
  358. const schema = doc.schema;
  359. schema.registerItem( 'paragraph', '$block' );
  360. // Let's use table as an example of content which needs to be filtered out.
  361. schema.registerItem( 'table' );
  362. schema.registerItem( 'td' );
  363. schema.registerItem( 'disallowedWidget' );
  364. schema.allow( { name: 'table', inside: '$clipboardHolder' } );
  365. schema.allow( { name: 'td', inside: '$clipboardHolder' } );
  366. schema.allow( { name: '$block', inside: 'td' } );
  367. schema.allow( { name: '$text', inside: 'td' } );
  368. schema.allow( { name: 'disallowedWidget', inside: '$clipboardHolder' } );
  369. schema.allow( { name: '$text', inside: 'disallowedWidget' } );
  370. schema.objects.add( 'disallowedWidget' );
  371. } );
  372. test(
  373. 'filters out disallowed elements and leaves out the text',
  374. '<table><td>xxx</td><td>yyy</td></table>',
  375. '<paragraph>f[]oo</paragraph>',
  376. '<paragraph>fxxxyyy[]oo</paragraph>'
  377. );
  378. test(
  379. 'filters out disallowed elements and leaves out the paragraphs',
  380. '<table><td><paragraph>xxx</paragraph><paragraph>yyy</paragraph><paragraph>zzz</paragraph></td></table>',
  381. '<paragraph>f[]oo</paragraph>',
  382. '<paragraph>fxxx</paragraph><paragraph>yyy</paragraph><paragraph>zzz[]oo</paragraph>'
  383. );
  384. test(
  385. 'filters out disallowed objects',
  386. '<disallowedWidget>xxx</disallowedWidget>',
  387. '<paragraph>f[]oo</paragraph>',
  388. '<paragraph>f[]oo</paragraph>'
  389. );
  390. } );
  391. } );
  392. // @param {String} title
  393. // @param {engine.model.Item|String} content
  394. function test( title, content, initialData, expectedData ) {
  395. it( title, () => {
  396. setData( doc, initialData );
  397. if ( typeof content == 'string' ) {
  398. content = parse( content, doc.schema, {
  399. context: [ '$clipboardHolder' ]
  400. } );
  401. }
  402. if ( !( content instanceof ModelDocumentFragment ) ) {
  403. content = new ModelDocumentFragment( [ content ] );
  404. }
  405. // Override the convertion so we get exactly the model that we defined in the content param.
  406. // This way we avoid the need to write converters for everything we want to test.
  407. dataController.viewToModel.convert = () => {
  408. return content;
  409. };
  410. insertContent( dataController, doc.batch(), doc.selection, new ViewDocumentFragment() );
  411. expect( getData( doc ) ).to.equal( expectedData );
  412. } );
  413. }
  414. } );