insertcontent.js 25 KB

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