insertcontent.js 36 KB

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