insertcontent.js 22 KB

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