insertcontent.js 36 KB

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