insertcontent.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. it( 'should not merge a paragraph wrapped in blockQuote with list item (checking left merge)', () => {
  335. model.schema.register( 'blockQuote', {
  336. allowWhere: '$block',
  337. allowContentOf: '$root',
  338. } );
  339. setData( model, '<listItem>fo[]o</listItem>' );
  340. insertHelper( '<blockQuote><paragraph>xxx</paragraph></blockQuote><heading1>yyy</heading1>' );
  341. expect( getData( model ) ).to.equal(
  342. '<listItem>fo</listItem>' +
  343. '<blockQuote>' +
  344. '<paragraph>xxx</paragraph>' +
  345. '</blockQuote>' +
  346. '<heading1>yyy[]o</heading1>'
  347. );
  348. } );
  349. it( 'should not merge a paragraph wrapped in blockQuote with list item (checking right merge)', () => {
  350. model.schema.register( 'blockQuote', {
  351. allowWhere: '$block',
  352. allowContentOf: '$root',
  353. } );
  354. setData( model, '<listItem>fo[]o</listItem>' );
  355. insertHelper( '<heading1>yyy</heading1><blockQuote><paragraph>xxx</paragraph></blockQuote>' );
  356. expect( getData( model ) ).to.equal(
  357. '<listItem>foyyy</listItem>' +
  358. '<blockQuote>' +
  359. '<paragraph>xxx</paragraph>' +
  360. '</blockQuote>' +
  361. '<listItem>[]o</listItem>'
  362. );
  363. } );
  364. it( 'should not merge a paragraph wrapped in blockQuote with list item (checking both merges)', () => {
  365. model.schema.register( 'blockQuote', {
  366. allowWhere: '$block',
  367. allowContentOf: '$root',
  368. } );
  369. setData( model, '<listItem>fo[]o</listItem>' );
  370. insertHelper( '<blockQuote><paragraph>xxx</paragraph></blockQuote>' );
  371. expect( getData( model ) ).to.equal(
  372. '<listItem>fo</listItem>' +
  373. '<blockQuote>' +
  374. '<paragraph>xxx</paragraph>' +
  375. '</blockQuote>' +
  376. '<listItem>[]o</listItem>'
  377. );
  378. } );
  379. } );
  380. describe( 'mixed content to block', () => {
  381. it( 'inserts text + paragraph', () => {
  382. setData( model, '<paragraph>f[]oo</paragraph>' );
  383. insertHelper( 'xxx<paragraph>yyy</paragraph>' );
  384. expect( getData( model ) ).to.equal( '<paragraph>fxxx</paragraph><paragraph>yyy[]oo</paragraph>' );
  385. } );
  386. it( 'inserts text + inlineWidget + text + paragraph', () => {
  387. setData( model, '<paragraph>f[]oo</paragraph>' );
  388. insertHelper( 'xxx<inlineWidget></inlineWidget>yyy<paragraph>zzz</paragraph>' );
  389. expect( getData( model ) ).to.equal(
  390. '<paragraph>fxxx<inlineWidget></inlineWidget>yyy</paragraph><paragraph>zzz[]oo</paragraph>'
  391. );
  392. } );
  393. it( 'inserts text + paragraph (at the beginning)', () => {
  394. setData( model, '<paragraph>[]foo</paragraph>' );
  395. insertHelper( 'xxx<paragraph>yyy</paragraph>' );
  396. expect( getData( model ) ).to.equal( '<paragraph>xxx</paragraph><paragraph>yyy[]foo</paragraph>' );
  397. } );
  398. it( 'inserts text + paragraph (at the end)', () => {
  399. setData( model, '<paragraph>foo[]</paragraph>' );
  400. insertHelper( 'xxx<paragraph>yyy</paragraph>' );
  401. expect( getData( model ) ).to.equal( '<paragraph>fooxxx</paragraph><paragraph>yyy[]</paragraph>' );
  402. } );
  403. it( 'inserts paragraph + text', () => {
  404. setData( model, '<paragraph>f[]oo</paragraph>' );
  405. insertHelper( '<paragraph>yyy</paragraph>xxx' );
  406. expect( getData( model ) ).to.equal( '<paragraph>fyyy</paragraph><paragraph>xxx[]oo</paragraph>' );
  407. } );
  408. // This is the expected result, but it was so hard to achieve at this stage that I
  409. // decided to go with the what the next test represents.
  410. // it( 'inserts paragraph + text + inlineWidget + text', () => {
  411. // setData( model, '<paragraph>f[]oo</paragraph>' );
  412. // insertHelper( '<paragraph>yyy</paragraph>xxx<inlineWidget></inlineWidget>zzz' );
  413. // expect( getData( model ) )
  414. // .to.equal( '<paragraph>fyyy</paragraph><paragraph>xxx<inlineWidget></inlineWidget>zzz[]oo</paragraph>' );
  415. // } );
  416. // See the comment above.
  417. it( 'inserts paragraph + text + inlineWidget + text', () => {
  418. setData( model, '<paragraph>f[]oo</paragraph>' );
  419. insertHelper( '<paragraph>yyy</paragraph>xxx<inlineWidget></inlineWidget>zzz' );
  420. expect( getData( model ) ).to.equal(
  421. '<paragraph>fyyy</paragraph><paragraph>xxx</paragraph>' +
  422. '<paragraph><inlineWidget></inlineWidget></paragraph>' +
  423. '<paragraph>zzz[]oo</paragraph>'
  424. );
  425. } );
  426. it( 'inserts paragraph + text + paragraph', () => {
  427. setData( model, '<paragraph>f[]oo</paragraph>' );
  428. insertHelper( '<paragraph>yyy</paragraph>xxx<paragraph>zzz</paragraph>' );
  429. expect( getData( model ) ).to.equal(
  430. '<paragraph>fyyy</paragraph><paragraph>xxx</paragraph><paragraph>zzz[]oo</paragraph>'
  431. );
  432. } );
  433. it( 'inserts paragraph + text (at the beginning)', () => {
  434. setData( model, '<paragraph>[]foo</paragraph>' );
  435. insertHelper( '<paragraph>yyy</paragraph>xxx' );
  436. expect( getData( model ) ).to.equal( '<paragraph>yyy</paragraph><paragraph>xxx[]foo</paragraph>' );
  437. } );
  438. it( 'inserts paragraph + text (at the end)', () => {
  439. setData( model, '<paragraph>foo[]</paragraph>' );
  440. insertHelper( '<paragraph>yyy</paragraph>xxx' );
  441. expect( getData( model ) ).to.equal( '<paragraph>fooyyy</paragraph><paragraph>xxx[]</paragraph>' );
  442. } );
  443. it( 'inserts text + heading', () => {
  444. setData( model, '<paragraph>f[]oo</paragraph>' );
  445. insertHelper( 'xxx<heading1>yyy</heading1>' );
  446. expect( getData( model ) ).to.equal( '<paragraph>fxxx</paragraph><heading1>yyy[]oo</heading1>' );
  447. } );
  448. it( 'inserts paragraph + object', () => {
  449. setData( model, '<paragraph>f[]oo</paragraph>' );
  450. insertHelper( '<paragraph>xxx</paragraph><blockWidget></blockWidget>' );
  451. expect( getData( model ) ).to.equal(
  452. '<paragraph>fxxx</paragraph>[<blockWidget></blockWidget>]<paragraph>oo</paragraph>'
  453. );
  454. } );
  455. it( 'inserts object + paragraph', () => {
  456. setData( model, '<paragraph>f[]oo</paragraph>' );
  457. insertHelper( '<blockWidget></blockWidget><paragraph>xxx</paragraph>' );
  458. expect( getData( model ) ).to.equal(
  459. '<paragraph>f</paragraph><blockWidget></blockWidget><paragraph>xxx[]oo</paragraph>'
  460. );
  461. } );
  462. } );
  463. describe( 'content over a block object', () => {
  464. it( 'inserts text', () => {
  465. setData( model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  466. insertHelper( 'xxx' );
  467. expect( getData( model ) ).to.equal(
  468. '<paragraph>foo</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  469. );
  470. } );
  471. it( 'inserts paragraph', () => {
  472. setData( model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  473. insertHelper( '<paragraph>xxx</paragraph>' );
  474. expect( getData( model ) )
  475. .to.equal( '<paragraph>foo</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>' );
  476. } );
  477. it( 'inserts text + paragraph', () => {
  478. setData( model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  479. insertHelper( 'yyy<paragraph>xxx</paragraph>' );
  480. expect( getData( model ) )
  481. .to.equal(
  482. '<paragraph>foo</paragraph><paragraph>yyy</paragraph><paragraph>xxx[]</paragraph><paragraph>bar</paragraph>'
  483. );
  484. } );
  485. it( 'inserts two blocks', () => {
  486. setData( model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  487. insertHelper( '<heading1>xxx</heading1><paragraph>yyy</paragraph>' );
  488. expect( getData( model ) )
  489. .to.equal(
  490. '<paragraph>foo</paragraph><heading1>xxx</heading1><paragraph>yyy[]</paragraph><paragraph>bar</paragraph>'
  491. );
  492. } );
  493. it( 'inserts block object', () => {
  494. setData( model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  495. insertHelper( '<blockWidget></blockWidget>' );
  496. // It's enough, don't worry.
  497. expect( getData( model ) ).to.equal(
  498. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>'
  499. );
  500. } );
  501. it( 'inserts inline object', () => {
  502. setData( model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );
  503. insertHelper( '<inlineWidget></inlineWidget>' );
  504. expect( getData( model ) )
  505. .to.equal(
  506. '<paragraph>foo</paragraph><paragraph><inlineWidget></inlineWidget>[]</paragraph><paragraph>bar</paragraph>'
  507. );
  508. } );
  509. } );
  510. describe( 'content over an inline object', () => {
  511. it( 'inserts text', () => {
  512. setData( model, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  513. insertHelper( 'xxx' );
  514. expect( getData( model ) ).to.equal( '<paragraph>fooxxx[]bar</paragraph>' );
  515. } );
  516. it( 'inserts paragraph', () => {
  517. setData( model, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  518. insertHelper( '<paragraph>xxx</paragraph>' );
  519. expect( getData( model ) ).to.equal( '<paragraph>fooxxx[]bar</paragraph>' );
  520. } );
  521. it( 'inserts text + paragraph', () => {
  522. setData( model, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  523. insertHelper( 'yyy<paragraph>xxx</paragraph>' );
  524. expect( getData( model ) ).to.equal( '<paragraph>fooyyy</paragraph><paragraph>xxx[]bar</paragraph>' );
  525. } );
  526. it( 'inserts two blocks', () => {
  527. setData( model, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  528. insertHelper( '<heading1>xxx</heading1><paragraph>yyy</paragraph>' );
  529. expect( getData( model ) ).to.equal( '<paragraph>fooxxx</paragraph><paragraph>yyy[]bar</paragraph>' );
  530. } );
  531. it( 'inserts inline object', () => {
  532. setData( model, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  533. insertHelper( '<inlineWidget></inlineWidget>' );
  534. expect( getData( model ) ).to.equal( '<paragraph>foo<inlineWidget></inlineWidget>[]bar</paragraph>' );
  535. } );
  536. it( 'inserts block object', () => {
  537. setData( model, '<paragraph>foo[<inlineWidget></inlineWidget>]bar</paragraph>' );
  538. insertHelper( '<blockWidget></blockWidget>' );
  539. expect( getData( model ) ).to.equal(
  540. '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>'
  541. );
  542. } );
  543. } );
  544. } );
  545. describe( 'filtering out', () => {
  546. beforeEach( () => {
  547. model = new Model();
  548. doc = model.document;
  549. doc.createRoot();
  550. const schema = model.schema;
  551. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  552. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  553. schema.register( 'element', { inheritAllFrom: '$block' } );
  554. schema.register( 'table' );
  555. schema.register( 'td' );
  556. schema.register( 'disallowedWidget', {
  557. isObject: true
  558. } );
  559. schema.extend( 'table', { allowIn: '$clipboardHolder' } );
  560. schema.extend( 'td', { allowIn: '$clipboardHolder' } );
  561. schema.extend( 'td', { allowIn: 'table' } );
  562. schema.extend( 'element', { allowIn: 'td' } );
  563. schema.extend( '$block', { allowIn: 'td' } );
  564. schema.extend( '$text', { allowIn: 'td' } );
  565. schema.extend( 'table', { allowIn: 'element' } );
  566. schema.extend( 'disallowedWidget', { allowIn: '$clipboardHolder' } );
  567. schema.extend( '$text', { allowIn: 'disallowedWidget' } );
  568. schema.extend( 'element', { allowIn: 'paragraph' } );
  569. schema.extend( 'element', { allowIn: 'heading1' } );
  570. schema.addAttributeCheck( ( ctx, attributeName ) => {
  571. // Allow 'b' on paragraph>$text.
  572. if ( ctx.endsWith( 'paragraph $text' ) && attributeName == 'b' ) {
  573. return true;
  574. }
  575. // Allow 'b' on paragraph>element>$text.
  576. if ( ctx.endsWith( 'paragraph element $text' ) && attributeName == 'b' ) {
  577. return true;
  578. }
  579. // Allow 'a' and 'b' on heading1>element>$text.
  580. if ( ctx.endsWith( 'heading1 element $text' ) && [ 'a', 'b' ].includes( attributeName ) ) {
  581. return true;
  582. }
  583. // Allow 'b' on element>table>td>$text.
  584. if ( ctx.endsWith( 'element table td $text' ) && attributeName == 'b' ) {
  585. return true;
  586. }
  587. } );
  588. } );
  589. it( 'filters out disallowed elements and leaves out the text', () => {
  590. setData( model, '<paragraph>f[]oo</paragraph>' );
  591. insertHelper( '<table><td>xxx</td><td>yyy</td></table>' );
  592. expect( getData( model ) ).to.equal( '<paragraph>fxxxyyy[]oo</paragraph>' );
  593. } );
  594. it( 'filters out disallowed elements and leaves out the paragraphs', () => {
  595. setData( model, '<paragraph>f[]oo</paragraph>' );
  596. insertHelper( '<table><td><paragraph>xxx</paragraph><paragraph>yyy</paragraph><paragraph>zzz</paragraph></td></table>' );
  597. expect( getData( model ) )
  598. .to.equal( '<paragraph>fxxx</paragraph><paragraph>yyy</paragraph><paragraph>zzz[]oo</paragraph>' );
  599. } );
  600. it( 'filters out disallowed objects', () => {
  601. setData( model, '<paragraph>f[]oo</paragraph>' );
  602. insertHelper( '<disallowedWidget>xxx</disallowedWidget>' );
  603. expect( getData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  604. } );
  605. it( 'filters out disallowed attributes when inserting text', () => {
  606. setData( model, '<paragraph>f[]oo</paragraph>' );
  607. insertHelper( 'x<$text a="1" b="1">x</$text>xy<$text a="1">y</$text>y' );
  608. expect( getData( model ) ).to.equal( '<paragraph>fx<$text b="1">x</$text>xyyy[]oo</paragraph>' );
  609. } );
  610. it( 'filters out disallowed attributes when inserting nested elements', () => {
  611. setData( model, '<element>[]</element>' );
  612. insertHelper( '<table><td>f<$text a="1" b="1" c="1">o</$text>o</td></table>' );
  613. expect( getData( model ) ).to.equal( '<element><table><td>f<$text b="1">o</$text>o</td></table>[]</element>' );
  614. } );
  615. it( 'filters out disallowed attributes when inserting text in disallowed elements', () => {
  616. setData( model, '<paragraph>f[]oo</paragraph>' );
  617. insertHelper( '<table><td>x<$text a="1" b="1">x</$text>x</td><td>y<$text a="1">y</$text>y</td></table>' );
  618. expect( getData( model ) ).to.equal( '<paragraph>fx<$text b="1">x</$text>xyyy[]oo</paragraph>' );
  619. } );
  620. it( 'filters out disallowed attributes when merging #1', () => {
  621. setData( model, '<paragraph>[]foo</paragraph>' );
  622. insertHelper( '<paragraph>x<$text a="1" b="1">x</$text>x</paragraph>' );
  623. expect( getData( model ) ).to.equal( '<paragraph>x<$text b="1">x</$text>x[]foo</paragraph>' );
  624. } );
  625. it( 'filters out disallowed attributes when merging #2', () => {
  626. setData( model, '<paragraph>f[]oo</paragraph>' );
  627. insertHelper( '<paragraph>x<$text a="1" b="1">x</$text>x</paragraph>' );
  628. expect( getData( model ) ).to.equal( '<paragraph>fx<$text b="1">x</$text>x[]oo</paragraph>' );
  629. } );
  630. it( 'filters out disallowed attributes when merging #3', () => {
  631. setData( model, '<paragraph>foo[]</paragraph>' );
  632. insertHelper( '<paragraph>x<$text a="1" b="1">x</$text>x</paragraph>' );
  633. expect( getData( model ) ).to.equal( '<paragraph>foox<$text b="1">x</$text>x[]</paragraph>' );
  634. } );
  635. it( 'filters out disallowed attributes from nested nodes when merging', () => {
  636. setData( model, '<paragraph>f[]oo</paragraph>' );
  637. insertHelper( '<heading1>x<element>b<$text a="1" b="1">a</$text>r</element>x</heading1>' );
  638. expect( getData( model ) ).to.equal( '<paragraph>fx<element>b<$text b="1">a</$text>r</element>x[]oo</paragraph>' );
  639. } );
  640. it( 'filters out disallowed attributes when autoparagraphing', () => {
  641. setData( model, '<paragraph>f[]oo</paragraph>' );
  642. insertHelper( '<paragraph>xxx</paragraph><$text a="1" b="1">yyy</$text>' );
  643. expect( getData( model ) ).to.equal( '<paragraph>fxxx</paragraph><paragraph><$text b="1">yyy[]</$text>oo</paragraph>' );
  644. } );
  645. } );
  646. } );
  647. describe( 'integration with limit elements', () => {
  648. beforeEach( () => {
  649. model = new Model();
  650. doc = model.document;
  651. doc.createRoot();
  652. const schema = model.schema;
  653. schema.register( 'limit', {
  654. isLimit: true
  655. } );
  656. schema.extend( 'limit', { allowIn: '$root' } );
  657. schema.extend( '$text', { allowIn: 'limit' } );
  658. schema.register( 'disallowedElement' );
  659. schema.extend( 'disallowedElement', { allowIn: '$clipboardHolder' } );
  660. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  661. } );
  662. it( 'should insert limit element', () => {
  663. insertHelper( '<limit></limit>' );
  664. expect( getData( model ) ).to.equal( '<limit>[]</limit>' );
  665. } );
  666. it( 'should insert text into limit element', () => {
  667. setData( model, '<limit>[]</limit>' );
  668. insertHelper( 'foo bar' );
  669. expect( getData( model ) ).to.equal( '<limit>foo bar[]</limit>' );
  670. } );
  671. it( 'should insert text into limit element', () => {
  672. setData( model, '<limit>foo[</limit><limit>]bar</limit>' );
  673. insertHelper( 'baz' );
  674. expect( getData( model ) ).to.equal( '<limit>foobaz[]</limit><limit>bar</limit>' );
  675. } );
  676. it( 'should not insert disallowed elements inside limit elements', () => {
  677. setData( model, '<limit>[]</limit>' );
  678. insertHelper( '<disallowedElement></disallowedElement>' );
  679. expect( getData( model ) ).to.equal( '<limit>[]</limit>' );
  680. } );
  681. it( 'should not leave the limit element when inserting at the end', () => {
  682. setData( model, '<limit>foo[]</limit>' );
  683. insertHelper( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  684. expect( getData( model ) ).to.equal( '<limit>fooab[]</limit>' );
  685. } );
  686. it( 'should not leave the limit element when inserting at the beginning', () => {
  687. setData( model, '<limit>[]foo</limit>' );
  688. insertHelper( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  689. expect( getData( model ) ).to.equal( '<limit>ab[]foo</limit>' );
  690. } );
  691. } );
  692. // Helper function that parses given content and inserts it at the cursor position.
  693. //
  694. // @param {module:engine/model/item~Item|String} content
  695. function insertHelper( content ) {
  696. if ( typeof content == 'string' ) {
  697. content = parse( content, model.schema, {
  698. context: [ '$clipboardHolder' ]
  699. } );
  700. }
  701. insertContent( model, content, doc.selection );
  702. }
  703. } );