8
0

insertcontent.js 22 KB

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