insertcontent.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../../src/model/model';
  6. import insertContent from '../../../src/model/utils/insertcontent';
  7. import DocumentFragment from '../../../src/model/documentfragment';
  8. import Text from '../../../src/model/text';
  9. import Element from '../../../src/model/element';
  10. import Selection from '../../../src/model/selection';
  11. import Position from '../../../src/model/position';
  12. import { setData, getData, parse } from '../../../src/dev-utils/model';
  13. describe( 'DataController utils', () => {
  14. let model, doc;
  15. describe( 'insertContent', () => {
  16. it( 'should use parent batch', () => {
  17. model = new Model();
  18. doc = model.document;
  19. doc.createRoot();
  20. model.schema.extend( '$text', { allowIn: '$root' } );
  21. setData( model, 'x[]x' );
  22. model.change( writer => {
  23. insertContent( model, new Text( 'a' ), doc.selection );
  24. expect( writer.batch.deltas ).to.length( 1 );
  25. } );
  26. } );
  27. it( 'should be able to insert content at custom selection', () => {
  28. model = new Model();
  29. doc = model.document;
  30. doc.createRoot();
  31. model.schema.extend( '$text', { allowIn: '$root' } );
  32. setData( model, 'a[]bc' );
  33. const selection = new Selection( new Position( doc.getRoot(), [ 2 ] ) );
  34. model.change( () => {
  35. insertContent( model, new Text( 'x' ), selection );
  36. expect( getData( model ) ).to.equal( 'a[]bxc' );
  37. } );
  38. } );
  39. it( 'accepts DocumentFragment', () => {
  40. model = new Model();
  41. doc = model.document;
  42. doc.createRoot();
  43. model.schema.extend( '$text', { allowIn: '$root' } );
  44. setData( model, 'x[]x' );
  45. insertContent( model, new DocumentFragment( [ new Text( 'a' ) ] ), doc.selection );
  46. expect( getData( model ) ).to.equal( 'xa[]x' );
  47. } );
  48. it( 'accepts Text', () => {
  49. model = new Model();
  50. doc = model.document;
  51. doc.createRoot();
  52. model.schema.extend( '$text', { allowIn: '$root' } );
  53. setData( model, 'x[]x' );
  54. insertContent( model, new Text( 'a' ), doc.selection );
  55. expect( getData( model ) ).to.equal( 'xa[]x' );
  56. } );
  57. it( 'should save the reference to the original object', () => {
  58. model = new Model();
  59. doc = model.document;
  60. doc.createRoot();
  61. const content = new Element( 'image' );
  62. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  63. model.schema.register( 'image', {
  64. allowWhere: '$text',
  65. isObject: true
  66. } );
  67. setData( model, '<paragraph>foo[]</paragraph>' );
  68. insertContent( model, content, doc.selection );
  69. expect( doc.getRoot().getChild( 0 ).getChild( 1 ) ).to.equal( content );
  70. } );
  71. describe( 'in simple scenarios', () => {
  72. beforeEach( () => {
  73. model = new Model();
  74. doc = model.document;
  75. doc.createRoot();
  76. const schema = model.schema;
  77. schema.register( 'image', {
  78. allowWhere: '$text',
  79. isObject: true
  80. } );
  81. schema.register( 'disallowedElement' );
  82. schema.extend( '$text', { allowIn: '$root' } );
  83. schema.extend( 'image', { allowIn: '$root' } );
  84. // Otherwise it won't be passed to the temporary model fragment used inside insert().
  85. schema.extend( 'disallowedElement', { allowIn: '$clipboardHolder' } );
  86. schema.extend( '$text', {
  87. allowIn: 'disallowedElement',
  88. allowAttributes: [ 'bold', 'italic' ]
  89. } );
  90. } );
  91. it( 'inserts one text node', () => {
  92. setData( model, 'f[]oo' );
  93. insertHelper( 'xyz' );
  94. expect( getData( model ) ).to.equal( 'fxyz[]oo' );
  95. } );
  96. it( 'inserts one text node (at the end)', () => {
  97. setData( model, 'foo[]' );
  98. insertHelper( 'xyz' );
  99. expect( getData( model ) ).to.equal( 'fooxyz[]' );
  100. } );
  101. it( 'inserts one text node with attribute', () => {
  102. setData( model, 'f[]oo' );
  103. insertHelper( '<$text bold="true">xyz</$text>' );
  104. expect( getData( model ) ).to.equal( 'f<$text bold="true">xyz[]</$text>oo' );
  105. expect( doc.selection.getAttribute( 'bold' ) ).to.be.true;
  106. } );
  107. it( 'inserts one text node with attribute into text with a different attribute', () => {
  108. setData( model, '<$text bold="true">f[]oo</$text>' );
  109. insertHelper( '<$text italic="true">xyz</$text>' );
  110. expect( getData( model ) )
  111. .to.equal( '<$text bold="true">f</$text><$text italic="true">xyz[]</$text><$text bold="true">oo</$text>' );
  112. expect( doc.selection.getAttribute( 'italic' ) ).to.be.true;
  113. expect( doc.selection.hasAttribute( 'bold' ) ).to.be.false;
  114. } );
  115. it( 'inserts one text node with attribute into text with the same attribute', () => {
  116. setData( model, '<$text bold="true">f[]oo</$text>' );
  117. insertHelper( '<$text bold="true">xyz</$text>' );
  118. expect( getData( model ) )
  119. .to.equal( '<$text bold="true">fxyz[]oo</$text>' );
  120. expect( doc.selection.getAttribute( 'bold' ) ).to.be.true;
  121. } );
  122. it( 'inserts a text without attributes into a text with an attribute', () => {
  123. setData( model, '<$text bold="true">f[]oo</$text>' );
  124. insertHelper( 'xyz' );
  125. expect( getData( model ) ).to.equal( '<$text bold="true">f</$text>xyz[]<$text bold="true">oo</$text>' );
  126. expect( doc.selection.hasAttribute( 'bold' ) ).to.be.false;
  127. } );
  128. it( 'inserts an element', () => {
  129. setData( model, 'f[]oo' );
  130. insertHelper( '<image></image>' );
  131. expect( getData( model ) ).to.equal( 'f<image></image>[]oo' );
  132. } );
  133. it( 'inserts a text and an element', () => {
  134. setData( model, 'f[]oo' );
  135. insertHelper( 'xyz<image></image>' );
  136. expect( getData( model ) ).to.equal( 'fxyz<image></image>[]oo' );
  137. } );
  138. it( 'strips a disallowed element', () => {
  139. setData( model, 'f[]oo' );
  140. insertHelper( '<disallowedElement>xyz</disallowedElement>' );
  141. expect( getData( model ) ).to.equal( 'fxyz[]oo' );
  142. } );
  143. it( 'deletes selection before inserting the content', () => {
  144. setData( model, 'f[abc]oo' );
  145. insertHelper( 'x' );
  146. expect( getData( model ) ).to.equal( 'fx[]oo' );
  147. } );
  148. describe( 'spaces handling', () => {
  149. // Note: spaces in the view are not encoded like in the DOM, so subsequent spaces must be
  150. // inserted into the model as is. The conversion to nbsps happen on view<=>DOM conversion.
  151. it( 'inserts one space', () => {
  152. setData( model, 'f[]oo' );
  153. insertHelper( new Text( ' ' ) );
  154. expect( getData( model ) ).to.equal( 'f []oo' );
  155. } );
  156. it( 'inserts three spaces', () => {
  157. setData( model, 'f[]oo' );
  158. insertHelper( new Text( ' ' ) );
  159. expect( getData( model ) ).to.equal( 'f []oo' );
  160. } );
  161. it( 'inserts spaces at the end', () => {
  162. setData( model, 'foo[]' );
  163. insertHelper( new Text( ' ' ) );
  164. expect( getData( model ) ).to.equal( 'foo []' );
  165. } );
  166. it( 'inserts one nbsp', () => {
  167. setData( model, 'f[]oo' );
  168. insertHelper( new Text( '\u200a' ) );
  169. expect( getData( model ) ).to.equal( 'f\u200a[]oo' );
  170. } );
  171. it( 'inserts word surrounded by spaces', () => {
  172. setData( model, 'f[]oo' );
  173. insertHelper( new Text( ' xyz ' ) );
  174. expect( getData( model ) ).to.equal( 'f xyz []oo' );
  175. } );
  176. } );
  177. } );
  178. describe( 'in blocks', () => {
  179. beforeEach( () => {
  180. model = new Model();
  181. doc = model.document;
  182. doc.createRoot();
  183. const schema = model.schema;
  184. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  185. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  186. schema.register( 'heading2', { inheritAllFrom: '$block' } );
  187. schema.register( 'blockWidget', {
  188. isObject: true,
  189. allowIn: '$root'
  190. } );
  191. schema.register( 'inlineWidget', {
  192. isObject: true,
  193. allowIn: [ '$block', '$clipboardHolder' ],
  194. } );
  195. schema.register( 'listItem', {
  196. inheritAllFrom: '$block',
  197. allowAttributes: [ 'type', 'indent' ]
  198. } );
  199. } );
  200. it( 'inserts one text node', () => {
  201. setData( model, '<paragraph>f[]oo</paragraph>' );
  202. insertHelper( 'xyz' );
  203. expect( getData( model ) ).to.equal( '<paragraph>fxyz[]oo</paragraph>' );
  204. } );
  205. it( 'inserts one text node to fully selected paragraph', () => {
  206. setData( model, '<paragraph>[foo]</paragraph>' );
  207. insertHelper( 'xyz' );
  208. expect( getData( model ) ).to.equal( '<paragraph>xyz[]</paragraph>' );
  209. } );
  210. it( 'inserts one text node to fully selected paragraphs (from outside)', () => {
  211. setData( model, '[<paragraph>foo</paragraph><paragraph>bar</paragraph>]' );
  212. insertHelper( 'xyz' );
  213. expect( getData( model ) ).to.equal( '<paragraph>xyz[]</paragraph>' );
  214. } );
  215. it( 'merges two blocks before inserting content (p+p)', () => {
  216. setData( model, '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  217. insertHelper( 'xyz' );
  218. expect( getData( model ) ).to.equal( '<paragraph>foxyz[]ar</paragraph>' );
  219. } );
  220. it( 'inserts inline widget and text', () => {
  221. setData( model, '<paragraph>f[]oo</paragraph>' );
  222. insertHelper( 'xyz<inlineWidget></inlineWidget>' );
  223. expect( getData( model ) ).to.equal( '<paragraph>fxyz<inlineWidget></inlineWidget>[]oo</paragraph>' );
  224. } );
  225. // Note: In CKEditor 4 the blocks are not merged, but to KISS we're merging here
  226. // because that's what deleteContent() does.
  227. it( 'merges two blocks before inserting content (h+p)', () => {
  228. setData( model, '<heading1>fo[o</heading1><paragraph>b]ar</paragraph>' );
  229. insertHelper( 'xyz' );
  230. expect( getData( model ) ).to.equal( '<heading1>foxyz[]ar</heading1>' );
  231. } );
  232. it( 'not insert autoparagraph when paragraph is disallowed at the current position', () => {
  233. // Disallow paragraph in $root.
  234. model.schema.addChildCheck( ( ctx, childDef ) => {
  235. if ( childDef.name == 'paragraph' && ctx.endsWith( '$root' ) ) {
  236. return false;
  237. }
  238. } );
  239. const content = new DocumentFragment( [
  240. new Element( 'heading1', [], [ new Text( 'bar' ) ] ),
  241. new Text( 'biz' )
  242. ] );
  243. setData( model, '[<heading2>foo</heading2>]' );
  244. insertContent( model, content, doc.selection );
  245. expect( getData( model ) ).to.equal( '<heading1>bar[]</heading1>' );
  246. } );
  247. describe( 'block to block handling', () => {
  248. it( 'inserts one paragraph', () => {
  249. setData( model, '<paragraph>f[]oo</paragraph>' );
  250. insertHelper( '<paragraph>xyz</paragraph>' );
  251. expect( getData( model ) ).to.equal( '<paragraph>fxyz[]oo</paragraph>' );
  252. } );
  253. it( 'inserts one paragraph (at the end)', () => {
  254. setData( model, '<paragraph>foo[]</paragraph>' );
  255. insertHelper( '<paragraph>xyz</paragraph>' );
  256. expect( getData( model ) ).to.equal( '<paragraph>fooxyz[]</paragraph>' );
  257. } );
  258. it( 'inserts one paragraph into an empty paragraph', () => {
  259. setData( model, '<paragraph>[]</paragraph>' );
  260. insertHelper( '<paragraph>xyz</paragraph>' );
  261. expect( getData( model ) ).to.equal( '<paragraph>xyz[]</paragraph>' );
  262. } );
  263. it( 'inserts one empty paragraph', () => {
  264. setData( model, '<paragraph>f[]oo</paragraph>' );
  265. insertHelper( '<paragraph></paragraph>' );
  266. expect( getData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  267. } );
  268. it( 'inserts one block into a fully selected content', () => {
  269. setData( model, '<heading1>[foo</heading1><paragraph>bar]</paragraph>' );
  270. insertHelper( '<heading2>xyz</heading2>' );
  271. expect( getData( model ) ).to.equal( '<heading2>xyz[]</heading2>' );
  272. } );
  273. it( 'inserts one heading', () => {
  274. setData( model, '<paragraph>f[]oo</paragraph>' );
  275. insertHelper( '<heading1>xyz</heading1>' );
  276. expect( getData( model ) ).to.equal( '<paragraph>fxyz[]oo</paragraph>' );
  277. } );
  278. it( 'inserts two headings', () => {
  279. setData( model, '<paragraph>f[]oo</paragraph>' );
  280. insertHelper( '<heading1>xxx</heading1><heading1>yyy</heading1>' );
  281. expect( getData( model ) ).to.equal( '<paragraph>fxxx</paragraph><heading1>yyy[]oo</heading1>' );
  282. } );
  283. it( 'inserts one object', () => {
  284. setData( model, '<paragraph>f[]oo</paragraph>' );
  285. insertHelper( '<blockWidget></blockWidget>' );
  286. expect( getData( model ) ).to.equal( '<paragraph>f</paragraph>[<blockWidget></blockWidget>]<paragraph>oo</paragraph>' );
  287. } );
  288. it( 'inserts one object (at the end)', () => {
  289. setData( model, '<paragraph>foo[]</paragraph>' );
  290. insertHelper( '<blockWidget></blockWidget>' );
  291. expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );
  292. } );
  293. it( 'inserts one object (at the beginning)', () => {
  294. setData( model, '<paragraph>[]bar</paragraph>' );
  295. insertHelper( '<blockWidget></blockWidget>' );
  296. expect( getData( model ) ).to.equal( '[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  297. } );
  298. it( 'inserts one list item', () => {
  299. setData( model, '<paragraph>f[]oo</paragraph>' );
  300. insertHelper( '<listItem indent="0" type="bulleted">xyz</listItem>' );
  301. expect( getData( model ) ).to.equal( '<paragraph>fxyz[]oo</paragraph>' );
  302. } );
  303. it( 'inserts list item to empty element', () => {
  304. setData( model, '<paragraph>[]</paragraph>' );
  305. insertHelper( '<listItem indent="0" type="bulleted">xyz</listItem>' );
  306. expect( getData( model ) ).to.equal( '<listItem indent="0" type="bulleted">xyz[]</listItem>' );
  307. } );
  308. it( 'inserts three list items at the end of paragraph', () => {
  309. setData( model, '<paragraph>foo[]</paragraph>' );
  310. insertHelper(
  311. '<listItem indent="0" type="bulleted">xxx</listItem>' +
  312. '<listItem indent="0" type="bulleted">yyy</listItem>' +
  313. '<listItem indent="0" type="bulleted">zzz</listItem>'
  314. );
  315. expect( getData( model ) ).to.equal(
  316. '<paragraph>fooxxx</paragraph>' +
  317. '<listItem indent="0" type="bulleted">yyy</listItem>' +
  318. '<listItem indent="0" type="bulleted">zzz[]</listItem>'
  319. );
  320. } );
  321. it( 'inserts two list items to an empty paragraph', () => {
  322. setData( model, '<paragraph>a</paragraph><paragraph>[]</paragraph><paragraph>b</paragraph>' );
  323. insertHelper(
  324. '<listItem indent="0" type="bulleted">xxx</listItem>' +
  325. '<listItem indent="0" type="bulleted">yyy</listItem>'
  326. );
  327. expect( getData( model ) ).to.equal(
  328. '<paragraph>a</paragraph>' +
  329. '<listItem indent="0" type="bulleted">xxx</listItem>' +
  330. '<listItem indent="0" type="bulleted">yyy[]</listItem>' +
  331. '<paragraph>b</paragraph>'
  332. );
  333. } );
  334. } );
  335. describe( 'mixed content to block', () => {
  336. it( 'inserts text + paragraph', () => {
  337. setData( model, '<paragraph>f[]oo</paragraph>' );
  338. insertHelper( 'xxx<paragraph>yyy</paragraph>' );
  339. expect( getData( model ) ).to.equal( '<paragraph>fxxx</paragraph><paragraph>yyy[]oo</paragraph>' );
  340. } );
  341. it( 'inserts text + inlineWidget + text + paragraph', () => {
  342. setData( model, '<paragraph>f[]oo</paragraph>' );
  343. insertHelper( 'xxx<inlineWidget></inlineWidget>yyy<paragraph>zzz</paragraph>' );
  344. expect( getData( model ) ).to.equal(
  345. '<paragraph>fxxx<inlineWidget></inlineWidget>yyy</paragraph><paragraph>zzz[]oo</paragraph>'
  346. );
  347. } );
  348. it( 'inserts text + paragraph (at the beginning)', () => {
  349. setData( model, '<paragraph>[]foo</paragraph>' );
  350. insertHelper( 'xxx<paragraph>yyy</paragraph>' );
  351. expect( getData( model ) ).to.equal( '<paragraph>xxx</paragraph><paragraph>yyy[]foo</paragraph>' );
  352. } );
  353. it( 'inserts text + paragraph (at the end)', () => {
  354. setData( model, '<paragraph>foo[]</paragraph>' );
  355. insertHelper( 'xxx<paragraph>yyy</paragraph>' );
  356. expect( getData( model ) ).to.equal( '<paragraph>fooxxx</paragraph><paragraph>yyy[]</paragraph>' );
  357. } );
  358. it( 'inserts paragraph + text', () => {
  359. setData( model, '<paragraph>f[]oo</paragraph>' );
  360. insertHelper( '<paragraph>yyy</paragraph>xxx' );
  361. expect( getData( model ) ).to.equal( '<paragraph>fyyy</paragraph><paragraph>xxx[]oo</paragraph>' );
  362. } );
  363. // This is the expected result, but it was so hard to achieve at this stage that I
  364. // decided to go with the what the next test represents.
  365. // it( 'inserts paragraph + text + inlineWidget + text', () => {
  366. // setData( model, '<paragraph>f[]oo</paragraph>' );
  367. // insertHelper( '<paragraph>yyy</paragraph>xxx<inlineWidget></inlineWidget>zzz' );
  368. // expect( getData( model ) )
  369. // .to.equal( '<paragraph>fyyy</paragraph><paragraph>xxx<inlineWidget></inlineWidget>zzz[]oo</paragraph>' );
  370. // } );
  371. // See the comment above.
  372. it( 'inserts paragraph + text + inlineWidget + text', () => {
  373. setData( model, '<paragraph>f[]oo</paragraph>' );
  374. insertHelper( '<paragraph>yyy</paragraph>xxx<inlineWidget></inlineWidget>zzz' );
  375. expect( getData( model ) ).to.equal(
  376. '<paragraph>fyyy</paragraph><paragraph>xxx</paragraph>' +
  377. '<paragraph><inlineWidget></inlineWidget></paragraph>' +
  378. '<paragraph>zzz[]oo</paragraph>'
  379. );
  380. } );
  381. it( 'inserts paragraph + text + paragraph', () => {
  382. setData( model, '<paragraph>f[]oo</paragraph>' );
  383. insertHelper( '<paragraph>yyy</paragraph>xxx<paragraph>zzz</paragraph>' );
  384. expect( getData( model ) ).to.equal(
  385. '<paragraph>fyyy</paragraph><paragraph>xxx</paragraph><paragraph>zzz[]oo</paragraph>'
  386. );
  387. } );
  388. it( 'inserts paragraph + text (at the beginning)', () => {
  389. setData( model, '<paragraph>[]foo</paragraph>' );
  390. insertHelper( '<paragraph>yyy</paragraph>xxx' );
  391. expect( getData( model ) ).to.equal( '<paragraph>yyy</paragraph><paragraph>xxx[]foo</paragraph>' );
  392. } );
  393. it( 'inserts paragraph + text (at the end)', () => {
  394. setData( model, '<paragraph>foo[]</paragraph>' );
  395. insertHelper( '<paragraph>yyy</paragraph>xxx' );
  396. expect( getData( model ) ).to.equal( '<paragraph>fooyyy</paragraph><paragraph>xxx[]</paragraph>' );
  397. } );
  398. it( 'inserts text + heading', () => {
  399. setData( model, '<paragraph>f[]oo</paragraph>' );
  400. insertHelper( 'xxx<heading1>yyy</heading1>' );
  401. expect( getData( model ) ).to.equal( '<paragraph>fxxx</paragraph><heading1>yyy[]oo</heading1>' );
  402. } );
  403. it( 'inserts paragraph + object', () => {
  404. setData( model, '<paragraph>f[]oo</paragraph>' );
  405. insertHelper( '<paragraph>xxx</paragraph><blockWidget></blockWidget>' );
  406. expect( getData( model ) ).to.equal(
  407. '<paragraph>fxxx</paragraph>[<blockWidget></blockWidget>]<paragraph>oo</paragraph>'
  408. );
  409. } );
  410. it( 'inserts object + paragraph', () => {
  411. setData( model, '<paragraph>f[]oo</paragraph>' );
  412. insertHelper( '<blockWidget></blockWidget><paragraph>xxx</paragraph>' );
  413. expect( getData( model ) ).to.equal(
  414. '<paragraph>f</paragraph><blockWidget></blockWidget><paragraph>xxx[]oo</paragraph>'
  415. );
  416. } );
  417. } );
  418. describe( 'content over a block object', () => {
  419. it( 'inserts text', () => {
  420. setData( model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  421. insertHelper( 'xxx' );
  422. expect( getData( model ) ).to.equal(
  423. '<paragraph>foo</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  424. );
  425. } );
  426. it( 'inserts paragraph', () => {
  427. setData( model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  428. insertHelper( '<paragraph>xxx</paragraph>' );
  429. expect( getData( model ) )
  430. .to.equal( '<paragraph>foo</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>' );
  431. } );
  432. it( 'inserts text + paragraph', () => {
  433. setData( model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  434. insertHelper( 'yyy<paragraph>xxx</paragraph>' );
  435. expect( getData( model ) )
  436. .to.equal(
  437. '<paragraph>foo</paragraph><paragraph>yyy</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  438. );
  439. } );
  440. it( 'inserts two blocks', () => {
  441. setData( model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  442. insertHelper( '<heading1>xxx</heading1><paragraph>yyy</paragraph>' );
  443. expect( getData( model ) )
  444. .to.equal(
  445. '<paragraph>foo</paragraph><heading1>xxx</heading1><paragraph>yyy[]</paragraph><paragraph>bar</paragraph>'
  446. );
  447. } );
  448. it( 'inserts block object', () => {
  449. setData( model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  450. insertHelper( '<blockWidget></blockWidget>' );
  451. // It's enough, don't worry.
  452. expect( getData( model ) ).to.equal(
  453. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>'
  454. );
  455. } );
  456. it( 'inserts inline object', () => {
  457. setData( model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  458. insertHelper( '<inlineWidget></inlineWidget>' );
  459. expect( getData( model ) )
  460. .to.equal(
  461. '<paragraph>foo</paragraph><paragraph><inlineWidget></inlineWidget>[]</paragraph><paragraph>bar</paragraph>'
  462. );
  463. } );
  464. } );
  465. describe( 'content over an inline object', () => {
  466. it( 'inserts text', () => {
  467. setData( model, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  468. insertHelper( 'xxx' );
  469. expect( getData( model ) ).to.equal( '<paragraph>fooxxx[]bar</paragraph>' );
  470. } );
  471. it( 'inserts paragraph', () => {
  472. setData( model, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  473. insertHelper( '<paragraph>xxx</paragraph>' );
  474. expect( getData( model ) ).to.equal( '<paragraph>fooxxx[]bar</paragraph>' );
  475. } );
  476. it( 'inserts text + paragraph', () => {
  477. setData( model, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  478. insertHelper( 'yyy<paragraph>xxx</paragraph>' );
  479. expect( getData( model ) ).to.equal( '<paragraph>fooyyy</paragraph><paragraph>xxx[]bar</paragraph>' );
  480. } );
  481. it( 'inserts two blocks', () => {
  482. setData( model, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  483. insertHelper( '<heading1>xxx</heading1><paragraph>yyy</paragraph>' );
  484. expect( getData( model ) ).to.equal( '<paragraph>fooxxx</paragraph><paragraph>yyy[]bar</paragraph>' );
  485. } );
  486. it( 'inserts inline object', () => {
  487. setData( model, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  488. insertHelper( '<inlineWidget></inlineWidget>' );
  489. expect( getData( model ) ).to.equal( '<paragraph>foo<inlineWidget></inlineWidget>[]bar</paragraph>' );
  490. } );
  491. it( 'inserts block object', () => {
  492. setData( model, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  493. insertHelper( '<blockWidget></blockWidget>' );
  494. expect( getData( model ) ).to.equal(
  495. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>'
  496. );
  497. } );
  498. } );
  499. } );
  500. describe( 'filtering out', () => {
  501. beforeEach( () => {
  502. model = new Model();
  503. doc = model.document;
  504. doc.createRoot();
  505. const schema = model.schema;
  506. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  507. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  508. schema.register( 'element', { inheritAllFrom: '$block' } );
  509. schema.register( 'table' );
  510. schema.register( 'td' );
  511. schema.register( 'disallowedWidget', {
  512. isObject: true
  513. } );
  514. schema.extend( 'table', { allowIn: '$clipboardHolder' } );
  515. schema.extend( 'td', { allowIn: '$clipboardHolder' } );
  516. schema.extend( 'td', { allowIn: 'table' } );
  517. schema.extend( 'element', { allowIn: 'td' } );
  518. schema.extend( '$block', { allowIn: 'td' } );
  519. schema.extend( '$text', { allowIn: 'td' } );
  520. schema.extend( 'table', { allowIn: 'element' } );
  521. schema.extend( 'disallowedWidget', { allowIn: '$clipboardHolder' } );
  522. schema.extend( '$text', { allowIn: 'disallowedWidget' } );
  523. schema.extend( 'element', { allowIn: 'paragraph' } );
  524. schema.extend( 'element', { allowIn: 'heading1' } );
  525. schema.addAttributeCheck( ( ctx, attributeName ) => {
  526. // Allow 'b' on paragraph>$text.
  527. if ( ctx.endsWith( 'paragraph $text' ) && attributeName == 'b' ) {
  528. return true;
  529. }
  530. // Allow 'b' on paragraph>element>$text.
  531. if ( ctx.endsWith( 'paragraph element $text' ) && attributeName == 'b' ) {
  532. return true;
  533. }
  534. // Allow 'a' and 'b' on heading1>element>$text.
  535. if ( ctx.endsWith( 'heading1 element $text' ) && [ 'a', 'b' ].includes( attributeName ) ) {
  536. return true;
  537. }
  538. // Allow 'b' on element>table>td>$text.
  539. if ( ctx.endsWith( 'element table td $text' ) && attributeName == 'b' ) {
  540. return true;
  541. }
  542. } );
  543. } );
  544. it( 'filters out disallowed elements and leaves out the text', () => {
  545. setData( model, '<paragraph>f[]oo</paragraph>' );
  546. insertHelper( '<table><td>xxx</td><td>yyy</td></table>' );
  547. expect( getData( model ) ).to.equal( '<paragraph>fxxxyyy[]oo</paragraph>' );
  548. } );
  549. it( 'filters out disallowed elements and leaves out the paragraphs', () => {
  550. setData( model, '<paragraph>f[]oo</paragraph>' );
  551. insertHelper( '<table><td><paragraph>xxx</paragraph><paragraph>yyy</paragraph><paragraph>zzz</paragraph></td></table>' );
  552. expect( getData( model ) )
  553. .to.equal( '<paragraph>fxxx</paragraph><paragraph>yyy</paragraph><paragraph>zzz[]oo</paragraph>' );
  554. } );
  555. it( 'filters out disallowed objects', () => {
  556. setData( model, '<paragraph>f[]oo</paragraph>' );
  557. insertHelper( '<disallowedWidget>xxx</disallowedWidget>' );
  558. expect( getData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  559. } );
  560. it( 'filters out disallowed attributes when inserting text', () => {
  561. setData( model, '<paragraph>f[]oo</paragraph>' );
  562. insertHelper( 'x<$text a="1" b="1">x</$text>xy<$text a="1">y</$text>y' );
  563. expect( getData( model ) ).to.equal( '<paragraph>fx<$text b="1">x</$text>xyyy[]oo</paragraph>' );
  564. } );
  565. it( 'filters out disallowed attributes when inserting nested elements', () => {
  566. setData( model, '<element>[]</element>' );
  567. insertHelper( '<table><td>f<$text a="1" b="1" c="1">o</$text>o</td></table>' );
  568. expect( getData( model ) ).to.equal( '<element><table><td>f<$text b="1">o</$text>o</td></table>[]</element>' );
  569. } );
  570. it( 'filters out disallowed attributes when inserting text in disallowed elements', () => {
  571. setData( model, '<paragraph>f[]oo</paragraph>' );
  572. insertHelper( '<table><td>x<$text a="1" b="1">x</$text>x</td><td>y<$text a="1">y</$text>y</td></table>' );
  573. expect( getData( model ) ).to.equal( '<paragraph>fx<$text b="1">x</$text>xyyy[]oo</paragraph>' );
  574. } );
  575. it( 'filters out disallowed attributes when merging #1', () => {
  576. setData( model, '<paragraph>[]foo</paragraph>' );
  577. insertHelper( '<paragraph>x<$text a="1" b="1">x</$text>x</paragraph>' );
  578. expect( getData( model ) ).to.equal( '<paragraph>x<$text b="1">x</$text>x[]foo</paragraph>' );
  579. } );
  580. it( 'filters out disallowed attributes when merging #2', () => {
  581. setData( model, '<paragraph>f[]oo</paragraph>' );
  582. insertHelper( '<paragraph>x<$text a="1" b="1">x</$text>x</paragraph>' );
  583. expect( getData( model ) ).to.equal( '<paragraph>fx<$text b="1">x</$text>x[]oo</paragraph>' );
  584. } );
  585. it( 'filters out disallowed attributes when merging #3', () => {
  586. setData( model, '<paragraph>foo[]</paragraph>' );
  587. insertHelper( '<paragraph>x<$text a="1" b="1">x</$text>x</paragraph>' );
  588. expect( getData( model ) ).to.equal( '<paragraph>foox<$text b="1">x</$text>x[]</paragraph>' );
  589. } );
  590. it( 'filters out disallowed attributes from nested nodes when merging', () => {
  591. setData( model, '<paragraph>f[]oo</paragraph>' );
  592. insertHelper( '<heading1>x<element>b<$text a="1" b="1">a</$text>r</element>x</heading1>' );
  593. expect( getData( model ) ).to.equal( '<paragraph>fx<element>b<$text b="1">a</$text>r</element>x[]oo</paragraph>' );
  594. } );
  595. it( 'filters out disallowed attributes when autoparagraphing', () => {
  596. setData( model, '<paragraph>f[]oo</paragraph>' );
  597. insertHelper( '<paragraph>xxx</paragraph><$text a="1" b="1">yyy</$text>' );
  598. expect( getData( model ) ).to.equal( '<paragraph>fxxx</paragraph><paragraph><$text b="1">yyy[]</$text>oo</paragraph>' );
  599. } );
  600. } );
  601. } );
  602. describe( 'integration with limit elements', () => {
  603. beforeEach( () => {
  604. model = new Model();
  605. doc = model.document;
  606. doc.createRoot();
  607. const schema = model.schema;
  608. schema.register( 'limit', {
  609. isLimit: true
  610. } );
  611. schema.extend( 'limit', { allowIn: '$root' } );
  612. schema.extend( '$text', { allowIn: 'limit' } );
  613. schema.register( 'disallowedElement' );
  614. schema.extend( 'disallowedElement', { allowIn: '$clipboardHolder' } );
  615. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  616. } );
  617. it( 'should insert limit element', () => {
  618. insertHelper( '<limit></limit>' );
  619. expect( getData( model ) ).to.equal( '<limit>[]</limit>' );
  620. } );
  621. it( 'should insert text into limit element', () => {
  622. setData( model, '<limit>[]</limit>' );
  623. insertHelper( 'foo bar' );
  624. expect( getData( model ) ).to.equal( '<limit>foo bar[]</limit>' );
  625. } );
  626. it( 'should insert text into limit element', () => {
  627. setData( model, '<limit>foo[</limit><limit>]bar</limit>' );
  628. insertHelper( 'baz' );
  629. expect( getData( model ) ).to.equal( '<limit>foobaz[]</limit><limit>bar</limit>' );
  630. } );
  631. it( 'should not insert disallowed elements inside limit elements', () => {
  632. setData( model, '<limit>[]</limit>' );
  633. insertHelper( '<disallowedElement></disallowedElement>' );
  634. expect( getData( model ) ).to.equal( '<limit>[]</limit>' );
  635. } );
  636. it( 'should not leave the limit element when inserting at the end', () => {
  637. setData( model, '<limit>foo[]</limit>' );
  638. insertHelper( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  639. expect( getData( model ) ).to.equal( '<limit>fooab[]</limit>' );
  640. } );
  641. it( 'should not leave the limit element when inserting at the beginning', () => {
  642. setData( model, '<limit>[]foo</limit>' );
  643. insertHelper( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  644. expect( getData( model ) ).to.equal( '<limit>ab[]foo</limit>' );
  645. } );
  646. } );
  647. // Helper function that parses given content and inserts it at the cursor position.
  648. //
  649. // @param {module:engine/model/item~Item|String} content
  650. function insertHelper( content ) {
  651. if ( typeof content == 'string' ) {
  652. content = parse( content, model.schema, {
  653. context: [ '$clipboardHolder' ]
  654. } );
  655. }
  656. insertContent( model, content, doc.selection );
  657. }
  658. } );