8
0

insertcontent.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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 empty paragraph', () => {
  235. setData( doc, '<paragraph>f[]oo</paragraph>' );
  236. insertHelper( '<paragraph></paragraph>' );
  237. expect( getData( doc ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  238. } );
  239. it( 'inserts one block into a fully selected content', () => {
  240. setData( doc, '<heading1>[foo</heading1><paragraph>bar]</paragraph>' );
  241. insertHelper( '<heading2>xyz</heading2>' );
  242. expect( getData( doc ) ).to.equal( '<heading2>xyz[]</heading2>' );
  243. } );
  244. it( 'inserts one heading', () => {
  245. setData( doc, '<paragraph>f[]oo</paragraph>' );
  246. insertHelper( '<heading1>xyz</heading1>' );
  247. expect( getData( doc ) ).to.equal( '<paragraph>fxyz[]oo</paragraph>' );
  248. } );
  249. it( 'inserts two headings', () => {
  250. setData( doc, '<paragraph>f[]oo</paragraph>' );
  251. insertHelper( '<heading1>xxx</heading1><heading1>yyy</heading1>' );
  252. expect( getData( doc ) ).to.equal( '<paragraph>fxxx</paragraph><heading1>yyy[]oo</heading1>' );
  253. } );
  254. it( 'inserts one object', () => {
  255. setData( doc, '<paragraph>f[]oo</paragraph>' );
  256. insertHelper( '<blockWidget></blockWidget>' );
  257. expect( getData( doc ) ).to.equal( '<paragraph>f</paragraph>[<blockWidget></blockWidget>]<paragraph>oo</paragraph>' );
  258. } );
  259. it( 'inserts one object (at the end)', () => {
  260. setData( doc, '<paragraph>foo[]</paragraph>' );
  261. insertHelper( '<blockWidget></blockWidget>' );
  262. expect( getData( doc ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );
  263. } );
  264. it( 'inserts one object (at the beginning)', () => {
  265. setData( doc, '<paragraph>[]bar</paragraph>' );
  266. insertHelper( '<blockWidget></blockWidget>' );
  267. expect( getData( doc ) ).to.equal( '[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  268. } );
  269. it( 'inserts one list item', () => {
  270. setData( doc, '<paragraph>f[]oo</paragraph>' );
  271. insertHelper( '<listItem indent="0" type="bulleted">xyz</listItem>' );
  272. expect( getData( doc ) ).to.equal( '<paragraph>fxyz[]oo</paragraph>' );
  273. } );
  274. it( 'inserts list item to empty element', () => {
  275. setData( doc, '<paragraph>[]</paragraph>' );
  276. insertHelper( '<listItem indent="0" type="bulleted">xyz</listItem>' );
  277. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">xyz[]</listItem>' );
  278. } );
  279. it( 'inserts three list items at the end of paragraph', () => {
  280. setData( doc, '<paragraph>foo[]</paragraph>' );
  281. insertHelper(
  282. '<listItem indent="0" type="bulleted">xxx</listItem>' +
  283. '<listItem indent="0" type="bulleted">yyy</listItem>' +
  284. '<listItem indent="0" type="bulleted">zzz</listItem>'
  285. );
  286. expect( getData( doc ) ).to.equal(
  287. '<paragraph>fooxxx</paragraph>' +
  288. '<listItem indent="0" type="bulleted">yyy</listItem>' +
  289. '<listItem indent="0" type="bulleted">zzz[]</listItem>'
  290. );
  291. } );
  292. it( 'inserts two list items to an empty paragraph', () => {
  293. setData( doc, '<paragraph>a</paragraph><paragraph>[]</paragraph><paragraph>b</paragraph>' );
  294. insertHelper(
  295. '<listItem indent="0" type="bulleted">xxx</listItem>' +
  296. '<listItem indent="0" type="bulleted">yyy</listItem>'
  297. );
  298. expect( getData( doc ) ).to.equal(
  299. '<paragraph>a</paragraph>' +
  300. '<listItem indent="0" type="bulleted">xxx</listItem>' +
  301. '<listItem indent="0" type="bulleted">yyy[]</listItem>' +
  302. '<paragraph>b</paragraph>'
  303. );
  304. } );
  305. } );
  306. describe( 'mixed content to block', () => {
  307. it( 'inserts text + paragraph', () => {
  308. setData( doc, '<paragraph>f[]oo</paragraph>' );
  309. insertHelper( 'xxx<paragraph>yyy</paragraph>' );
  310. expect( getData( doc ) ).to.equal( '<paragraph>fxxx</paragraph><paragraph>yyy[]oo</paragraph>' );
  311. } );
  312. it( 'inserts text + inlineWidget + text + paragraph', () => {
  313. setData( doc, '<paragraph>f[]oo</paragraph>' );
  314. insertHelper( 'xxx<inlineWidget></inlineWidget>yyy<paragraph>zzz</paragraph>' );
  315. expect( getData( doc ) ).to.equal(
  316. '<paragraph>fxxx<inlineWidget></inlineWidget>yyy</paragraph><paragraph>zzz[]oo</paragraph>'
  317. );
  318. } );
  319. it( 'inserts text + paragraph (at the beginning)', () => {
  320. setData( doc, '<paragraph>[]foo</paragraph>' );
  321. insertHelper( 'xxx<paragraph>yyy</paragraph>' );
  322. expect( getData( doc ) ).to.equal( '<paragraph>xxx</paragraph><paragraph>yyy[]foo</paragraph>' );
  323. } );
  324. it( 'inserts text + paragraph (at the end)', () => {
  325. setData( doc, '<paragraph>foo[]</paragraph>' );
  326. insertHelper( 'xxx<paragraph>yyy</paragraph>' );
  327. expect( getData( doc ) ).to.equal( '<paragraph>fooxxx</paragraph><paragraph>yyy[]</paragraph>' );
  328. } );
  329. it( 'inserts paragraph + text', () => {
  330. setData( doc, '<paragraph>f[]oo</paragraph>' );
  331. insertHelper( '<paragraph>yyy</paragraph>xxx' );
  332. expect( getData( doc ) ).to.equal( '<paragraph>fyyy</paragraph><paragraph>xxx[]oo</paragraph>' );
  333. } );
  334. // This is the expected result, but it was so hard to achieve at this stage that I
  335. // decided to go with the what the next test represents.
  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 ) )
  340. // .to.equal( '<paragraph>fyyy</paragraph><paragraph>xxx<inlineWidget></inlineWidget>zzz[]oo</paragraph>' );
  341. // } );
  342. // See the comment above.
  343. it( 'inserts paragraph + text + inlineWidget + text', () => {
  344. setData( doc, '<paragraph>f[]oo</paragraph>' );
  345. insertHelper( '<paragraph>yyy</paragraph>xxx<inlineWidget></inlineWidget>zzz' );
  346. expect( getData( doc ) ).to.equal(
  347. '<paragraph>fyyy</paragraph><paragraph>xxx</paragraph>' +
  348. '<paragraph><inlineWidget></inlineWidget></paragraph>' +
  349. '<paragraph>zzz[]oo</paragraph>'
  350. );
  351. } );
  352. it( 'inserts paragraph + text + paragraph', () => {
  353. setData( doc, '<paragraph>f[]oo</paragraph>' );
  354. insertHelper( '<paragraph>yyy</paragraph>xxx<paragraph>zzz</paragraph>' );
  355. expect( getData( doc ) ).to.equal(
  356. '<paragraph>fyyy</paragraph><paragraph>xxx</paragraph><paragraph>zzz[]oo</paragraph>'
  357. );
  358. } );
  359. it( 'inserts paragraph + text (at the beginning)', () => {
  360. setData( doc, '<paragraph>[]foo</paragraph>' );
  361. insertHelper( '<paragraph>yyy</paragraph>xxx' );
  362. expect( getData( doc ) ).to.equal( '<paragraph>yyy</paragraph><paragraph>xxx[]foo</paragraph>' );
  363. } );
  364. it( 'inserts paragraph + text (at the end)', () => {
  365. setData( doc, '<paragraph>foo[]</paragraph>' );
  366. insertHelper( '<paragraph>yyy</paragraph>xxx' );
  367. expect( getData( doc ) ).to.equal( '<paragraph>fooyyy</paragraph><paragraph>xxx[]</paragraph>' );
  368. } );
  369. it( 'inserts text + heading', () => {
  370. setData( doc, '<paragraph>f[]oo</paragraph>' );
  371. insertHelper( 'xxx<heading1>yyy</heading1>' );
  372. expect( getData( doc ) ).to.equal( '<paragraph>fxxx</paragraph><heading1>yyy[]oo</heading1>' );
  373. } );
  374. it( 'inserts paragraph + object', () => {
  375. setData( doc, '<paragraph>f[]oo</paragraph>' );
  376. insertHelper( '<paragraph>xxx</paragraph><blockWidget></blockWidget>' );
  377. expect( getData( doc ) ).to.equal(
  378. '<paragraph>fxxx</paragraph>[<blockWidget></blockWidget>]<paragraph>oo</paragraph>'
  379. );
  380. } );
  381. it( 'inserts object + paragraph', () => {
  382. setData( doc, '<paragraph>f[]oo</paragraph>' );
  383. insertHelper( '<blockWidget></blockWidget><paragraph>xxx</paragraph>' );
  384. expect( getData( doc ) ).to.equal(
  385. '<paragraph>f</paragraph><blockWidget></blockWidget><paragraph>xxx[]oo</paragraph>'
  386. );
  387. } );
  388. } );
  389. describe( 'content over a block object', () => {
  390. it( 'inserts text', () => {
  391. setData( doc, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  392. insertHelper( 'xxx' );
  393. expect( getData( doc ) ).to.equal(
  394. '<paragraph>foo</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  395. );
  396. } );
  397. it( 'inserts paragraph', () => {
  398. setData( doc, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  399. insertHelper( '<paragraph>xxx</paragraph>' );
  400. expect( getData( doc ) ).to.equal( '<paragraph>foo</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>' );
  401. } );
  402. it( 'inserts text + paragraph', () => {
  403. setData( doc, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  404. insertHelper( 'yyy<paragraph>xxx</paragraph>' );
  405. expect( getData( doc ) )
  406. .to.equal(
  407. '<paragraph>foo</paragraph><paragraph>yyy</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  408. );
  409. } );
  410. it( 'inserts two blocks', () => {
  411. setData( doc, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  412. insertHelper( '<heading1>xxx</heading1><paragraph>yyy</paragraph>' );
  413. expect( getData( doc ) )
  414. .to.equal(
  415. '<paragraph>foo</paragraph><heading1>xxx</heading1><paragraph>yyy[]</paragraph><paragraph>bar</paragraph>'
  416. );
  417. } );
  418. it( 'inserts block object', () => {
  419. setData( doc, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  420. insertHelper( '<blockWidget></blockWidget>' );
  421. // It's enough, don't worry.
  422. expect( getData( doc ) ).to.equal(
  423. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>'
  424. );
  425. } );
  426. it( 'inserts inline object', () => {
  427. setData( doc, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  428. insertHelper( '<inlineWidget></inlineWidget>' );
  429. expect( getData( doc ) )
  430. .to.equal(
  431. '<paragraph>foo</paragraph><paragraph><inlineWidget></inlineWidget>[]</paragraph><paragraph>bar</paragraph>'
  432. );
  433. } );
  434. } );
  435. describe( 'content over an inline object', () => {
  436. it( 'inserts text', () => {
  437. setData( doc, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  438. insertHelper( 'xxx' );
  439. expect( getData( doc ) ).to.equal( '<paragraph>fooxxx[]bar</paragraph>' );
  440. } );
  441. it( 'inserts paragraph', () => {
  442. setData( doc, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  443. insertHelper( '<paragraph>xxx</paragraph>' );
  444. expect( getData( doc ) ).to.equal( '<paragraph>fooxxx[]bar</paragraph>' );
  445. } );
  446. it( 'inserts text + paragraph', () => {
  447. setData( doc, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  448. insertHelper( 'yyy<paragraph>xxx</paragraph>' );
  449. expect( getData( doc ) ).to.equal( '<paragraph>fooyyy</paragraph><paragraph>xxx[]bar</paragraph>' );
  450. } );
  451. it( 'inserts two blocks', () => {
  452. setData( doc, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  453. insertHelper( '<heading1>xxx</heading1><paragraph>yyy</paragraph>' );
  454. expect( getData( doc ) ).to.equal( '<paragraph>fooxxx</paragraph><paragraph>yyy[]bar</paragraph>' );
  455. } );
  456. it( 'inserts inline object', () => {
  457. setData( doc, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  458. insertHelper( '<inlineWidget></inlineWidget>' );
  459. expect( getData( doc ) ).to.equal( '<paragraph>foo<inlineWidget></inlineWidget>[]bar</paragraph>' );
  460. } );
  461. it( 'inserts block object', () => {
  462. setData( doc, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  463. insertHelper( '<blockWidget></blockWidget>' );
  464. expect( getData( doc ) ).to.equal(
  465. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>'
  466. );
  467. } );
  468. } );
  469. } );
  470. describe( 'filtering out', () => {
  471. beforeEach( () => {
  472. doc = new Document();
  473. doc.createRoot();
  474. dataController = new DataController( doc );
  475. const schema = doc.schema;
  476. schema.registerItem( 'paragraph', '$block' );
  477. // Let's use table as an example of content which needs to be filtered out.
  478. schema.registerItem( 'table' );
  479. schema.registerItem( 'td' );
  480. schema.registerItem( 'disallowedWidget' );
  481. schema.allow( { name: 'table', inside: '$clipboardHolder' } );
  482. schema.allow( { name: 'td', inside: '$clipboardHolder' } );
  483. schema.allow( { name: 'td', inside: 'table' } );
  484. schema.allow( { name: '$block', inside: 'td' } );
  485. schema.allow( { name: '$text', inside: 'td' } );
  486. schema.allow( { name: 'disallowedWidget', inside: '$clipboardHolder' } );
  487. schema.allow( { name: '$text', inside: 'disallowedWidget' } );
  488. schema.objects.add( 'disallowedWidget' );
  489. schema.allow( { name: '$text', attributes: 'b', inside: 'paragraph' } );
  490. } );
  491. it( 'filters out disallowed elements and leaves out the text', () => {
  492. setData( doc, '<paragraph>f[]oo</paragraph>' );
  493. insertHelper( '<table><td>xxx</td><td>yyy</td></table>' );
  494. expect( getData( doc ) ).to.equal( '<paragraph>fxxxyyy[]oo</paragraph>' );
  495. } );
  496. it( 'filters out disallowed elements and leaves out the paragraphs', () => {
  497. setData( doc, '<paragraph>f[]oo</paragraph>' );
  498. insertHelper( '<table><td><paragraph>xxx</paragraph><paragraph>yyy</paragraph><paragraph>zzz</paragraph></td></table>' );
  499. expect( getData( doc ) ).to.equal( '<paragraph>fxxx</paragraph><paragraph>yyy</paragraph><paragraph>zzz[]oo</paragraph>' );
  500. } );
  501. it( 'filters out disallowed objects', () => {
  502. setData( doc, '<paragraph>f[]oo</paragraph>' );
  503. insertHelper( '<disallowedWidget>xxx</disallowedWidget>' );
  504. expect( getData( doc ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  505. } );
  506. it( 'filters out disallowed attributes when inserting text', () => {
  507. setData( doc, '<paragraph>f[]oo</paragraph>' );
  508. insertHelper( 'x<$text a="1" b="1">x</$text>xy<$text a="1">y</$text>y' );
  509. expect( getData( doc ) ).to.equal( '<paragraph>fx<$text b="1">x</$text>xyyy[]oo</paragraph>' );
  510. } );
  511. it( 'filters out disallowed attributes when inserting text in disallowed elements', () => {
  512. setData( doc, '<paragraph>f[]oo</paragraph>' );
  513. insertHelper( '<table><td>x<$text a="1" b="1">x</$text>x</td><td>y<$text a="1">y</$text>y</td></table>' );
  514. expect( getData( doc ) ).to.equal( '<paragraph>fx<$text b="1">x</$text>xyyy[]oo</paragraph>' );
  515. } );
  516. it( 'filters out disallowed attributes when merging #1', () => {
  517. setData( doc, '<paragraph>[]foo</paragraph>' );
  518. insertHelper( '<paragraph>x<$text a="1" b="1">x</$text>x</paragraph>' );
  519. expect( getData( doc ) ).to.equal( '<paragraph>x<$text b="1">x</$text>x[]foo</paragraph>' );
  520. } );
  521. it( 'filters out disallowed attributes when merging #2', () => {
  522. setData( doc, '<paragraph>f[]oo</paragraph>' );
  523. insertHelper( '<paragraph>x<$text a="1" b="1">x</$text>x</paragraph>' );
  524. expect( getData( doc ) ).to.equal( '<paragraph>fx<$text b="1">x</$text>x[]oo</paragraph>' );
  525. } );
  526. it( 'filters out disallowed attributes when merging #3', () => {
  527. setData( doc, '<paragraph>foo[]</paragraph>' );
  528. insertHelper( '<paragraph>x<$text a="1" b="1">x</$text>x</paragraph>' );
  529. expect( getData( doc ) ).to.equal( '<paragraph>foox<$text b="1">x</$text>x[]</paragraph>' );
  530. } );
  531. it( 'filters out disallowed attributes when autoparagraphing', () => {
  532. setData( doc, '<paragraph>f[]oo</paragraph>' );
  533. insertHelper( '<paragraph>xxx</paragraph><$text a="1" b="1">yyy</$text>' );
  534. expect( getData( doc ) ).to.equal( '<paragraph>fxxx</paragraph><paragraph><$text b="1">yyy[]</$text>oo</paragraph>' );
  535. } );
  536. } );
  537. } );
  538. describe( 'integration with limit elements', () => {
  539. beforeEach( () => {
  540. doc = new Document();
  541. doc.createRoot();
  542. dataController = new DataController( doc );
  543. const schema = doc.schema;
  544. schema.registerItem( 'limit' );
  545. schema.allow( { name: 'limit', inside: '$root' } );
  546. schema.allow( { name: '$text', inside: 'limit' } );
  547. schema.limits.add( 'limit' );
  548. schema.registerItem( 'disallowedElement' );
  549. schema.allow( { name: 'disallowedElement', inside: '$clipboardHolder' } );
  550. schema.registerItem( 'paragraph', '$block' );
  551. } );
  552. it( 'should insert limit element', () => {
  553. insertHelper( '<limit></limit>' );
  554. expect( getData( doc ) ).to.equal( '<limit>[]</limit>' );
  555. } );
  556. it( 'should insert text into limit element', () => {
  557. setData( doc, '<limit>[]</limit>' );
  558. insertHelper( 'foo bar' );
  559. expect( getData( doc ) ).to.equal( '<limit>foo bar[]</limit>' );
  560. } );
  561. it( 'should insert text into limit element', () => {
  562. setData( doc, '<limit>foo[</limit><limit>]bar</limit>' );
  563. insertHelper( 'baz' );
  564. expect( getData( doc ) ).to.equal( '<limit>foobaz[]</limit><limit>bar</limit>' );
  565. } );
  566. it( 'should not insert disallowed elements inside limit elements', () => {
  567. setData( doc, '<limit>[]</limit>' );
  568. insertHelper( '<disallowedElement></disallowedElement>' );
  569. expect( getData( doc ) ).to.equal( '<limit>[]</limit>' );
  570. } );
  571. it( 'should not leave the limit element when inserting at the end', () => {
  572. setData( doc, '<limit>foo[]</limit>' );
  573. insertHelper( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  574. expect( getData( doc ) ).to.equal( '<limit>fooab[]</limit>' );
  575. } );
  576. it( 'should not leave the limit element when inserting at the beginning', () => {
  577. setData( doc, '<limit>[]foo</limit>' );
  578. insertHelper( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  579. expect( getData( doc ) ).to.equal( '<limit>ab[]foo</limit>' );
  580. } );
  581. } );
  582. // Helper function that parses given content and inserts it at the cursor position.
  583. //
  584. // @param {module:engine/model/item~Item|String} content
  585. function insertHelper( content ) {
  586. if ( typeof content == 'string' ) {
  587. content = parse( content, doc.schema, {
  588. context: [ '$clipboardHolder' ]
  589. } );
  590. }
  591. insertContent( dataController, content, doc.selection );
  592. }
  593. } );