8
0

insertcontent.js 25 KB

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