8
0

insertcontent.js 30 KB

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