insertcontent.js 33 KB

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