8
0

insertcontent.js 34 KB

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