insert.js 22 KB

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