insertcontent.js 20 KB

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