insertcontent.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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.registerItem( 'listItem', '$block' );
  113. schema.allow( { name: 'blockWidget', inside: '$root' } );
  114. schema.allow( { name: 'inlineWidget', inside: '$block' } );
  115. schema.allow( { name: 'inlineWidget', inside: '$clipboardHolder' } );
  116. schema.allow( {
  117. name: 'listItem',
  118. inside: '$root',
  119. attributes: [ 'type', 'indent' ]
  120. } );
  121. schema.requireAttributes( 'listItem', [ 'type', 'indent' ] );
  122. schema.objects.add( 'blockWidget' );
  123. schema.objects.add( 'inlineWidget' );
  124. } );
  125. test(
  126. 'inserts one text node',
  127. 'xyz',
  128. '<paragraph>f[]oo</paragraph>',
  129. '<paragraph>fxyz[]oo</paragraph>'
  130. );
  131. test(
  132. 'inserts one text node to fully selected paragraph',
  133. 'xyz',
  134. '<paragraph>[foo]</paragraph>',
  135. '<paragraph>xyz[]</paragraph>'
  136. );
  137. test(
  138. 'inserts one text node to fully selected paragraphs (from outside)',
  139. 'xyz',
  140. '[<paragraph>foo</paragraph><paragraph>bar</paragraph>]',
  141. '<paragraph>xyz[]</paragraph>'
  142. );
  143. test(
  144. 'merges two blocks before inserting content (p+p)',
  145. 'xyz',
  146. '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>',
  147. '<paragraph>foxyz[]ar</paragraph>'
  148. );
  149. test(
  150. 'inserts inline widget and text',
  151. 'xyz<inlineWidget></inlineWidget>',
  152. '<paragraph>f[]oo</paragraph>',
  153. '<paragraph>fxyz<inlineWidget></inlineWidget>[]oo</paragraph>'
  154. );
  155. // Note: In CKEditor 4 the blocks are not merged, but to KISS we're merging here
  156. // because that's what deleteContent() does.
  157. test(
  158. 'merges two blocks before inserting content (h+p)',
  159. 'xyz',
  160. '<heading1>fo[o</heading1><paragraph>b]ar</paragraph>',
  161. '<heading1>foxyz[]ar</heading1>'
  162. );
  163. describe( 'block to block handling', () => {
  164. test(
  165. 'inserts one paragraph',
  166. '<paragraph>xyz</paragraph>',
  167. '<paragraph>f[]oo</paragraph>',
  168. '<paragraph>fxyz[]oo</paragraph>'
  169. );
  170. test(
  171. 'inserts one paragraph (at the end)',
  172. '<paragraph>xyz</paragraph>',
  173. '<paragraph>foo[]</paragraph>',
  174. '<paragraph>fooxyz[]</paragraph>'
  175. );
  176. test(
  177. 'inserts one paragraph into an empty paragraph',
  178. '<paragraph>xyz</paragraph>',
  179. '<paragraph>[]</paragraph>',
  180. '<paragraph>xyz[]</paragraph>'
  181. );
  182. test(
  183. 'inserts one block into a fully selected content',
  184. '<heading2>xyz</heading2>',
  185. '<heading1>[foo</heading1><paragraph>bar]</paragraph>',
  186. '<heading2>xyz[]</heading2>'
  187. );
  188. test(
  189. 'inserts one heading',
  190. '<heading1>xyz</heading1>',
  191. '<paragraph>f[]oo</paragraph>',
  192. '<paragraph>fxyz[]oo</paragraph>'
  193. );
  194. test(
  195. 'inserts two headings',
  196. '<heading1>xxx</heading1><heading1>yyy</heading1>',
  197. '<paragraph>f[]oo</paragraph>',
  198. '<paragraph>fxxx</paragraph><heading1>yyy[]oo</heading1>'
  199. );
  200. test(
  201. 'inserts one object',
  202. '<blockWidget></blockWidget>',
  203. '<paragraph>f[]oo</paragraph>',
  204. '<paragraph>f</paragraph>[<blockWidget></blockWidget>]<paragraph>oo</paragraph>'
  205. );
  206. test(
  207. 'inserts one object (at the end)',
  208. '<blockWidget></blockWidget>',
  209. '<paragraph>foo[]</paragraph>',
  210. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]'
  211. );
  212. test(
  213. 'inserts one object (at the beginning)',
  214. '<blockWidget></blockWidget>',
  215. '<paragraph>[]bar</paragraph>',
  216. '[<blockWidget></blockWidget>]<paragraph>bar</paragraph>'
  217. );
  218. test(
  219. 'inserts one list item',
  220. '<listItem indent="0" type="bulleted">xyz</listItem>',
  221. '<paragraph>f[]oo</paragraph>',
  222. '<paragraph>fxyz[]oo</paragraph>'
  223. );
  224. test(
  225. 'inserts list item to empty element',
  226. '<listItem indent="0" type="bulleted">xyz</listItem>',
  227. '<paragraph>[]</paragraph>',
  228. '<listItem indent="0" type="bulleted">xyz[]</listItem>'
  229. );
  230. test(
  231. 'inserts three list items at the end of paragraph',
  232. (
  233. '<listItem indent="0" type="bulleted">xxx</listItem>' +
  234. '<listItem indent="0" type="bulleted">yyy</listItem>' +
  235. '<listItem indent="0" type="bulleted">zzz</listItem>'
  236. ),
  237. '<paragraph>foo[]</paragraph>',
  238. (
  239. '<paragraph>fooxxx</paragraph>' +
  240. '<listItem indent="0" type="bulleted">yyy</listItem>' +
  241. '<listItem indent="0" type="bulleted">zzz[]</listItem>'
  242. )
  243. );
  244. test(
  245. 'inserts two list items to an empty paragraph',
  246. (
  247. '<listItem indent="0" type="bulleted">xxx</listItem>' +
  248. '<listItem indent="0" type="bulleted">yyy</listItem>'
  249. ),
  250. '<paragraph>a</paragraph><paragraph>[]</paragraph><paragraph>b</paragraph>',
  251. (
  252. '<paragraph>a</paragraph>' +
  253. '<listItem indent="0" type="bulleted">xxx</listItem>' +
  254. '<listItem indent="0" type="bulleted">yyy[]</listItem>' +
  255. '<paragraph>b</paragraph>'
  256. )
  257. );
  258. } );
  259. describe( 'mixed content to block', () => {
  260. test(
  261. 'inserts text + paragraph',
  262. 'xxx<paragraph>yyy</paragraph>',
  263. '<paragraph>f[]oo</paragraph>',
  264. '<paragraph>fxxx</paragraph><paragraph>yyy[]oo</paragraph>'
  265. );
  266. test(
  267. 'inserts text + inlineWidget + text + paragraph',
  268. 'xxx<inlineWidget></inlineWidget>yyy<paragraph>zzz</paragraph>',
  269. '<paragraph>f[]oo</paragraph>',
  270. '<paragraph>fxxx<inlineWidget></inlineWidget>yyy</paragraph><paragraph>zzz[]oo</paragraph>'
  271. );
  272. test(
  273. 'inserts text + paragraph (at the beginning)',
  274. 'xxx<paragraph>yyy</paragraph>',
  275. '<paragraph>[]foo</paragraph>',
  276. '<paragraph>xxx</paragraph><paragraph>yyy[]foo</paragraph>'
  277. );
  278. test(
  279. 'inserts text + paragraph (at the end)',
  280. 'xxx<paragraph>yyy</paragraph>',
  281. '<paragraph>foo[]</paragraph>',
  282. '<paragraph>fooxxx</paragraph><paragraph>yyy[]</paragraph>'
  283. );
  284. test(
  285. 'inserts paragraph + text',
  286. '<paragraph>yyy</paragraph>xxx',
  287. '<paragraph>f[]oo</paragraph>',
  288. '<paragraph>fyyy</paragraph><paragraph>xxx[]oo</paragraph>'
  289. );
  290. // This is the expected result, but it was so hard to achieve at this stage that I
  291. // decided to go with the what the next test represents.
  292. // test(
  293. // 'inserts paragraph + text + inlineWidget + text',
  294. // '<paragraph>yyy</paragraph>xxx<inlineWidget></inlineWidget>zzz',
  295. // '<paragraph>f[]oo</paragraph>',
  296. // '<paragraph>fyyy</paragraph><paragraph>xxx<inlineWidget></inlineWidget>zzz[]oo</paragraph>'
  297. // );
  298. // See the comment above.
  299. test(
  300. 'inserts paragraph + text + inlineWidget + text',
  301. '<paragraph>yyy</paragraph>xxx<inlineWidget></inlineWidget>zzz',
  302. '<paragraph>f[]oo</paragraph>',
  303. (
  304. '<paragraph>fyyy</paragraph><paragraph>xxx</paragraph>' +
  305. '<paragraph><inlineWidget></inlineWidget></paragraph>' +
  306. '<paragraph>zzz[]oo</paragraph>'
  307. )
  308. );
  309. test(
  310. 'inserts paragraph + text + paragraph',
  311. '<paragraph>yyy</paragraph>xxx<paragraph>zzz</paragraph>',
  312. '<paragraph>f[]oo</paragraph>',
  313. '<paragraph>fyyy</paragraph><paragraph>xxx</paragraph><paragraph>zzz[]oo</paragraph>'
  314. );
  315. test(
  316. 'inserts paragraph + text (at the beginning)',
  317. '<paragraph>yyy</paragraph>xxx',
  318. '<paragraph>[]foo</paragraph>',
  319. '<paragraph>yyy</paragraph><paragraph>xxx[]foo</paragraph>'
  320. );
  321. test(
  322. 'inserts paragraph + text (at the end)',
  323. '<paragraph>yyy</paragraph>xxx',
  324. '<paragraph>foo[]</paragraph>',
  325. '<paragraph>fooyyy</paragraph><paragraph>xxx[]</paragraph>'
  326. );
  327. test(
  328. 'inserts text + heading',
  329. 'xxx<heading1>yyy</heading1>',
  330. '<paragraph>f[]oo</paragraph>',
  331. '<paragraph>fxxx</paragraph><heading1>yyy[]oo</heading1>'
  332. );
  333. test(
  334. 'inserts paragraph + object',
  335. '<paragraph>xxx</paragraph><blockWidget></blockWidget>',
  336. '<paragraph>f[]oo</paragraph>',
  337. '<paragraph>fxxx</paragraph>[<blockWidget></blockWidget>]<paragraph>oo</paragraph>'
  338. );
  339. test(
  340. 'inserts object + paragraph',
  341. '<blockWidget></blockWidget><paragraph>xxx</paragraph>',
  342. '<paragraph>f[]oo</paragraph>',
  343. '<paragraph>f</paragraph><blockWidget></blockWidget><paragraph>xxx[]oo</paragraph>'
  344. );
  345. } );
  346. describe( 'content over a block object', () => {
  347. test(
  348. 'inserts text',
  349. 'xxx',
  350. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  351. '<paragraph>foo</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  352. );
  353. test(
  354. 'inserts paragraph',
  355. '<paragraph>xxx</paragraph>',
  356. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  357. '<paragraph>foo</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  358. );
  359. test(
  360. 'inserts text + paragraph',
  361. 'yyy<paragraph>xxx</paragraph>',
  362. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  363. '<paragraph>foo</paragraph><paragraph>yyy</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  364. );
  365. test(
  366. 'inserts two blocks',
  367. '<heading1>xxx</heading1><paragraph>yyy</paragraph>',
  368. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  369. '<paragraph>foo</paragraph><heading1>xxx</heading1><paragraph>yyy[]</paragraph><paragraph>bar</paragraph>'
  370. );
  371. test(
  372. 'inserts block object',
  373. '<blockWidget></blockWidget>',
  374. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  375. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' // It's enough, don't worry.
  376. );
  377. test(
  378. 'inserts inline object',
  379. '<inlineWidget></inlineWidget>',
  380. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>',
  381. '<paragraph>foo</paragraph><paragraph><inlineWidget></inlineWidget>[]</paragraph><paragraph>bar</paragraph>'
  382. );
  383. } );
  384. describe( 'content over an inline object', () => {
  385. test(
  386. 'inserts text',
  387. 'xxx',
  388. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  389. '<paragraph>fooxxx[]bar</paragraph>'
  390. );
  391. test(
  392. 'inserts paragraph',
  393. '<paragraph>xxx</paragraph>',
  394. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  395. '<paragraph>fooxxx[]bar</paragraph>'
  396. );
  397. test(
  398. 'inserts text + paragraph',
  399. 'yyy<paragraph>xxx</paragraph>',
  400. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  401. '<paragraph>fooyyy</paragraph><paragraph>xxx[]bar</paragraph>'
  402. );
  403. test(
  404. 'inserts two blocks',
  405. '<heading1>xxx</heading1><paragraph>yyy</paragraph>',
  406. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  407. '<paragraph>fooxxx</paragraph><paragraph>yyy[]bar</paragraph>'
  408. );
  409. test(
  410. 'inserts inline object',
  411. '<inlineWidget></inlineWidget>',
  412. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  413. '<paragraph>foo<inlineWidget></inlineWidget>[]bar</paragraph>'
  414. );
  415. test(
  416. 'inserts block object',
  417. '<blockWidget></blockWidget>',
  418. '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>',
  419. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>'
  420. );
  421. } );
  422. } );
  423. describe( 'filtering out', () => {
  424. beforeEach( () => {
  425. doc = new Document();
  426. doc.createRoot();
  427. dataController = new DataController( doc );
  428. const schema = doc.schema;
  429. schema.registerItem( 'paragraph', '$block' );
  430. // Let's use table as an example of content which needs to be filtered out.
  431. schema.registerItem( 'table' );
  432. schema.registerItem( 'td' );
  433. schema.registerItem( 'disallowedWidget' );
  434. schema.allow( { name: 'table', inside: '$clipboardHolder' } );
  435. schema.allow( { name: 'td', inside: '$clipboardHolder' } );
  436. schema.allow( { name: '$block', inside: 'td' } );
  437. schema.allow( { name: '$text', inside: 'td' } );
  438. schema.allow( { name: 'disallowedWidget', inside: '$clipboardHolder' } );
  439. schema.allow( { name: '$text', inside: 'disallowedWidget' } );
  440. schema.objects.add( 'disallowedWidget' );
  441. } );
  442. test(
  443. 'filters out disallowed elements and leaves out the text',
  444. '<table><td>xxx</td><td>yyy</td></table>',
  445. '<paragraph>f[]oo</paragraph>',
  446. '<paragraph>fxxxyyy[]oo</paragraph>'
  447. );
  448. test(
  449. 'filters out disallowed elements and leaves out the paragraphs',
  450. '<table><td><paragraph>xxx</paragraph><paragraph>yyy</paragraph><paragraph>zzz</paragraph></td></table>',
  451. '<paragraph>f[]oo</paragraph>',
  452. '<paragraph>fxxx</paragraph><paragraph>yyy</paragraph><paragraph>zzz[]oo</paragraph>'
  453. );
  454. test(
  455. 'filters out disallowed objects',
  456. '<disallowedWidget>xxx</disallowedWidget>',
  457. '<paragraph>f[]oo</paragraph>',
  458. '<paragraph>f[]oo</paragraph>'
  459. );
  460. } );
  461. } );
  462. // @param {String} title
  463. // @param {engine.model.Item|String} content
  464. function test( title, content, initialData, expectedData ) {
  465. it( title, () => {
  466. setData( doc, initialData );
  467. if ( typeof content == 'string' ) {
  468. content = parse( content, doc.schema, {
  469. context: [ '$clipboardHolder' ]
  470. } );
  471. }
  472. if ( !( content instanceof ModelDocumentFragment ) ) {
  473. content = new ModelDocumentFragment( [ content ] );
  474. }
  475. // Override the convertion so we get exactly the model that we defined in the content param.
  476. // This way we avoid the need to write converters for everything we want to test.
  477. dataController.viewToModel.convert = () => {
  478. return content;
  479. };
  480. insertContent( dataController, doc.batch(), doc.selection, new ViewDocumentFragment() );
  481. expect( getData( doc ) ).to.equal( expectedData );
  482. } );
  483. }
  484. } );