8
0

input.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. /*
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import Bold from '@ckeditor/ckeditor5-basic-styles/src/boldengine';
  11. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italicengine';
  12. import LinkEngine from '@ckeditor/ckeditor5-link/src/linkengine';
  13. import Input from '../src/input';
  14. import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  15. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  16. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  17. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  18. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  19. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  20. import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
  21. import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
  22. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  23. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  24. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  25. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  26. describe( 'Input feature', () => {
  27. let editor, model, modelRoot, view, viewRoot, listenter;
  28. testUtils.createSinonSandbox();
  29. before( () => {
  30. listenter = Object.create( EmitterMixin );
  31. return VirtualTestEditor.create( { plugins: [ Input, Paragraph ] } )
  32. .then( newEditor => {
  33. // Mock image feature.
  34. newEditor.document.schema.registerItem( 'image', '$inline' );
  35. buildModelConverter().for( newEditor.data.modelToView, newEditor.editing.modelToView )
  36. .fromElement( 'image' )
  37. .toElement( 'img' );
  38. buildViewConverter().for( newEditor.data.viewToModel )
  39. .fromElement( 'img' )
  40. .toElement( 'image' );
  41. editor = newEditor;
  42. model = editor.editing.model;
  43. modelRoot = model.getRoot();
  44. view = editor.editing.view;
  45. viewRoot = view.getRoot();
  46. } );
  47. } );
  48. after( () => {
  49. return editor.destroy();
  50. } );
  51. beforeEach( () => {
  52. editor.setData( '<p>foobar</p>' );
  53. model.enqueueChanges( () => {
  54. model.selection.setRanges( [
  55. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 0 ), 3 )
  56. ] );
  57. } );
  58. } );
  59. afterEach( () => {
  60. listenter.stopListening();
  61. } );
  62. describe( 'mutations handling', () => {
  63. it( 'should handle text mutation', () => {
  64. view.fire( 'mutations', [
  65. {
  66. type: 'text',
  67. oldText: 'foobar',
  68. newText: 'fooxbar',
  69. node: viewRoot.getChild( 0 ).getChild( 0 )
  70. }
  71. ] );
  72. expect( getModelData( model ) ).to.equal( '<paragraph>foox[]bar</paragraph>' );
  73. expect( getViewData( view ) ).to.equal( '<p>foox{}bar</p>' );
  74. } );
  75. it( 'should handle text mutation change', () => {
  76. view.fire( 'mutations', [
  77. {
  78. type: 'text',
  79. oldText: 'foobar',
  80. newText: 'foodar',
  81. node: viewRoot.getChild( 0 ).getChild( 0 )
  82. }
  83. ] );
  84. expect( getModelData( model ) ).to.equal( '<paragraph>food[]ar</paragraph>' );
  85. expect( getViewData( view ) ).to.equal( '<p>food{}ar</p>' );
  86. } );
  87. it( 'should handle text node insertion', () => {
  88. editor.setData( '<p></p>' );
  89. view.fire( 'mutations', [
  90. {
  91. type: 'children',
  92. oldChildren: [],
  93. newChildren: [ new ViewText( 'x' ) ],
  94. node: viewRoot.getChild( 0 )
  95. }
  96. ] );
  97. expect( getModelData( model ) ).to.equal( '<paragraph>x[]</paragraph>' );
  98. expect( getViewData( view ) ).to.equal( '<p>x{}</p>' );
  99. } );
  100. it( 'should do nothing when two nodes were inserted', () => {
  101. editor.setData( '<p></p>' );
  102. view.fire( 'mutations', [
  103. {
  104. type: 'children',
  105. oldChildren: [],
  106. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  107. node: viewRoot.getChild( 0 )
  108. }
  109. ] );
  110. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  111. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  112. } );
  113. it( 'should do nothing when two nodes were inserted and one removed', () => {
  114. view.fire( 'mutations', [
  115. {
  116. type: 'children',
  117. oldChildren: [ new ViewText( 'foobar' ) ],
  118. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  119. node: viewRoot.getChild( 0 )
  120. }
  121. ] );
  122. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  123. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  124. } );
  125. it( 'should handle multiple children in the node', () => {
  126. editor.setData( '<p>foo<img></img></p>' );
  127. view.fire( 'mutations', [
  128. {
  129. type: 'children',
  130. oldChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ) ],
  131. newChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ), new ViewText( 'x' ) ],
  132. node: viewRoot.getChild( 0 )
  133. }
  134. ] );
  135. expect( getModelData( model ) ).to.equal( '<paragraph>foo<image></image>x[]</paragraph>' );
  136. expect( getViewData( view ) ).to.equal( '<p>foo<img></img>x{}</p>' );
  137. } );
  138. it( 'should do nothing when node was removed', () => {
  139. view.fire( 'mutations', [
  140. {
  141. type: 'children',
  142. oldChildren: [ new ViewText( 'foobar' ) ],
  143. newChildren: [],
  144. node: viewRoot.getChild( 0 )
  145. }
  146. ] );
  147. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  148. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  149. } );
  150. it( 'should do nothing when element was inserted', () => {
  151. editor.setData( '<p></p>' );
  152. view.fire( 'mutations', [
  153. {
  154. type: 'children',
  155. oldChildren: [],
  156. newChildren: [ new ViewElement( 'img' ) ],
  157. node: viewRoot.getChild( 0 )
  158. }
  159. ] );
  160. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  161. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  162. } );
  163. it( 'should set model selection appropriately to view selection passed in mutations event', () => {
  164. // This test case emulates spellchecker correction.
  165. const viewSelection = new ViewSelection();
  166. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  167. view.fire( 'mutations',
  168. [ {
  169. type: 'text',
  170. oldText: 'foobar',
  171. newText: 'foodar',
  172. node: viewRoot.getChild( 0 ).getChild( 0 )
  173. } ],
  174. viewSelection
  175. );
  176. expect( getModelData( model ) ).to.equal( '<paragraph>foodar[]</paragraph>' );
  177. expect( getViewData( view ) ).to.equal( '<p>foodar{}</p>' );
  178. } );
  179. it( 'should use up to one insert and remove operations (spellchecker)', () => {
  180. // This test case emulates spellchecker correction.
  181. const viewSelection = new ViewSelection();
  182. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  183. testUtils.sinon.spy( Batch.prototype, 'weakInsert' );
  184. testUtils.sinon.spy( Batch.prototype, 'remove' );
  185. view.fire( 'mutations',
  186. [ {
  187. type: 'text',
  188. oldText: 'foobar',
  189. newText: 'fxobxr',
  190. node: viewRoot.getChild( 0 ).getChild( 0 )
  191. } ],
  192. viewSelection
  193. );
  194. expect( Batch.prototype.weakInsert.calledOnce ).to.be.true;
  195. expect( Batch.prototype.remove.calledOnce ).to.be.true;
  196. } );
  197. it( 'should place selection after when correcting to longer word (spellchecker)', () => {
  198. // This test case emulates spellchecker correction.
  199. editor.setData( '<p>Foo hous a</p>' );
  200. const viewSelection = new ViewSelection();
  201. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  202. view.fire( 'mutations',
  203. [ {
  204. type: 'text',
  205. oldText: 'Foo hous a',
  206. newText: 'Foo house a',
  207. node: viewRoot.getChild( 0 ).getChild( 0 )
  208. } ],
  209. viewSelection
  210. );
  211. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[] a</paragraph>' );
  212. expect( getViewData( view ) ).to.equal( '<p>Foo house{} a</p>' );
  213. } );
  214. it( 'should place selection after when correcting to shorter word (spellchecker)', () => {
  215. // This test case emulates spellchecker correction.
  216. editor.setData( '<p>Bar athat foo</p>' );
  217. const viewSelection = new ViewSelection();
  218. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  219. view.fire( 'mutations',
  220. [ {
  221. type: 'text',
  222. oldText: 'Bar athat foo',
  223. newText: 'Bar that foo',
  224. node: viewRoot.getChild( 0 ).getChild( 0 )
  225. } ],
  226. viewSelection
  227. );
  228. expect( getModelData( model ) ).to.equal( '<paragraph>Bar that[] foo</paragraph>' );
  229. expect( getViewData( view ) ).to.equal( '<p>Bar that{} foo</p>' );
  230. } );
  231. it( 'should place selection after when merging two words (spellchecker)', () => {
  232. // This test case emulates spellchecker correction.
  233. editor.setData( '<p>Foo hous e</p>' );
  234. const viewSelection = new ViewSelection();
  235. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  236. view.fire( 'mutations',
  237. [ {
  238. type: 'text',
  239. oldText: 'Foo hous e',
  240. newText: 'Foo house',
  241. node: viewRoot.getChild( 0 ).getChild( 0 )
  242. } ],
  243. viewSelection
  244. );
  245. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[]</paragraph>' );
  246. expect( getViewData( view ) ).to.equal( '<p>Foo house{}</p>' );
  247. } );
  248. it( 'should place non-collapsed selection after changing single character (composition)', () => {
  249. editor.setData( '<p>Foo house</p>' );
  250. const viewSelection = new ViewSelection();
  251. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  252. viewSelection.setFocus( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  253. view.fire( 'mutations',
  254. [ {
  255. type: 'text',
  256. oldText: 'Foo house',
  257. newText: 'Foo housa',
  258. node: viewRoot.getChild( 0 ).getChild( 0 )
  259. } ],
  260. viewSelection
  261. );
  262. expect( getModelData( model ) ).to.equal( '<paragraph>Foo hous[a]</paragraph>' );
  263. expect( getViewData( view ) ).to.equal( '<p>Foo hous{a}</p>' );
  264. } );
  265. it( 'should replace last &nbsp; with space', () => {
  266. model.enqueueChanges( () => {
  267. model.selection.setRanges( [
  268. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  269. ] );
  270. } );
  271. view.fire( 'mutations', [
  272. {
  273. type: 'text',
  274. oldText: 'foobar',
  275. newText: 'foobar\u00A0',
  276. node: viewRoot.getChild( 0 ).getChild( 0 )
  277. }
  278. ] );
  279. expect( getModelData( model ) ).to.equal( '<paragraph>foobar []</paragraph>' );
  280. expect( getViewData( view ) ).to.equal( '<p>foobar {}</p>' );
  281. } );
  282. it( 'should replace first &nbsp; with space', () => {
  283. model.enqueueChanges( () => {
  284. model.selection.setRanges( [
  285. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 0 ), 0 )
  286. ] );
  287. } );
  288. view.fire( 'mutations', [
  289. {
  290. type: 'text',
  291. oldText: 'foobar',
  292. newText: '\u00A0foobar',
  293. node: viewRoot.getChild( 0 ).getChild( 0 )
  294. }
  295. ] );
  296. expect( getModelData( model ) ).to.equal( '<paragraph> []foobar</paragraph>' );
  297. expect( getViewData( view ) ).to.equal( '<p> {}foobar</p>' );
  298. } );
  299. it( 'should replace all &nbsp; with spaces', () => {
  300. model.enqueueChanges( () => {
  301. model.selection.setRanges( [
  302. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  303. ] );
  304. } );
  305. view.fire( 'mutations', [
  306. {
  307. type: 'text',
  308. oldText: 'foobar',
  309. newText: 'foobar\u00A0\u00A0\u00A0baz',
  310. node: viewRoot.getChild( 0 ).getChild( 0 )
  311. }
  312. ] );
  313. expect( getModelData( model ) ).to.equal( '<paragraph>foobar baz[]</paragraph>' );
  314. expect( getViewData( view ) ).to.equal( '<p>foobar baz{}</p>' );
  315. } );
  316. } );
  317. describe( 'keystroke handling', () => {
  318. it( 'should remove contents', () => {
  319. model.enqueueChanges( () => {
  320. model.selection.setRanges( [
  321. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  322. } );
  323. listenter.listenTo( view, 'keydown', () => {
  324. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  325. }, { priority: 'lowest' } );
  326. } );
  327. // #97
  328. it( 'should remove contents and merge blocks', () => {
  329. setModelData( model, '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  330. listenter.listenTo( view, 'keydown', () => {
  331. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  332. view.fire( 'mutations', [
  333. {
  334. type: 'text',
  335. oldText: 'foar',
  336. newText: 'foyar',
  337. node: viewRoot.getChild( 0 ).getChild( 0 )
  338. }
  339. ] );
  340. }, { priority: 'lowest' } );
  341. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  342. expect( getModelData( model ) ).to.equal( '<paragraph>foy[]ar</paragraph>' );
  343. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  344. } );
  345. it( 'should do nothing on arrow key', () => {
  346. model.enqueueChanges( () => {
  347. model.selection.setRanges( [
  348. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  349. } );
  350. view.fire( 'keydown', { keyCode: getCode( 'arrowright' ) } );
  351. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  352. } );
  353. it( 'should do nothing on ctrl combinations', () => {
  354. model.enqueueChanges( () => {
  355. model.selection.setRanges( [
  356. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  357. } );
  358. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  359. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  360. } );
  361. it( 'should do nothing on non printable keys', () => {
  362. model.enqueueChanges( () => {
  363. model.selection.setRanges( [
  364. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  365. } );
  366. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  367. view.fire( 'keydown', { keyCode: 35 } ); // Home
  368. view.fire( 'keydown', { keyCode: 112 } ); // F1
  369. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  370. } );
  371. // #69
  372. it( 'should do nothing on tab key', () => {
  373. model.enqueueChanges( () => {
  374. model.selection.setRanges( [
  375. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  376. } );
  377. view.fire( 'keydown', { keyCode: 9 } ); // Tab
  378. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  379. } );
  380. // #82
  381. it( 'should do nothing on composition start key', () => {
  382. model.enqueueChanges( () => {
  383. model.selection.setRanges( [
  384. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  385. } );
  386. view.fire( 'keydown', { keyCode: 229 } );
  387. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  388. } );
  389. it( 'should do nothing if selection is collapsed', () => {
  390. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  391. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  392. } );
  393. it( 'should lock buffer if selection is not collapsed', () => {
  394. const buffer = editor.commands.get( 'input' )._buffer;
  395. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  396. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  397. model.enqueueChanges( () => {
  398. model.selection.setRanges( [
  399. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  400. } );
  401. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  402. expect( lockSpy.calledOnce ).to.be.true;
  403. expect( unlockSpy.calledOnce ).to.be.true;
  404. } );
  405. it( 'should not lock buffer on non printable keys', () => {
  406. const buffer = editor.commands.get( 'input' )._buffer;
  407. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  408. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  409. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  410. view.fire( 'keydown', { keyCode: 35 } ); // Home
  411. view.fire( 'keydown', { keyCode: 112 } ); // F1
  412. expect( lockSpy.callCount ).to.be.equal( 0 );
  413. expect( unlockSpy.callCount ).to.be.equal( 0 );
  414. } );
  415. it( 'should not lock buffer on collapsed selection', () => {
  416. const buffer = editor.commands.get( 'input' )._buffer;
  417. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  418. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  419. view.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  420. view.fire( 'keydown', { keyCode: getCode( 'a' ) } );
  421. view.fire( 'keydown', { keyCode: getCode( 'z' ) } );
  422. expect( lockSpy.callCount ).to.be.equal( 0 );
  423. expect( unlockSpy.callCount ).to.be.equal( 0 );
  424. } );
  425. it( 'should not modify document when input command is disabled and selection is collapsed', () => {
  426. setModelData( model, '<paragraph>foo[]bar</paragraph>' );
  427. editor.commands.get( 'input' ).isEnabled = false;
  428. view.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  429. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  430. } );
  431. it( 'should not modify document when input command is disabled and selection is non-collapsed', () => {
  432. setModelData( model, '<paragraph>fo[ob]ar</paragraph>' );
  433. editor.commands.get( 'input' ).isEnabled = false;
  434. view.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  435. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  436. } );
  437. } );
  438. describe( '#100', () => {
  439. let domElement, domRoot;
  440. beforeEach( () => {
  441. domElement = document.createElement( 'div' );
  442. document.body.appendChild( domElement );
  443. return ClassicTestEditor.create( domElement, { plugins: [ Input, Paragraph, Bold, Italic, LinkEngine ] } )
  444. .then( newEditor => {
  445. editor = newEditor;
  446. model = editor.document;
  447. modelRoot = model.getRoot();
  448. view = editor.editing.view;
  449. viewRoot = view.getRoot();
  450. domRoot = view.getDomRoot();
  451. // Mock image feature.
  452. newEditor.document.schema.registerItem( 'image', '$inline' );
  453. buildModelConverter().for( newEditor.data.modelToView, newEditor.editing.modelToView )
  454. .fromElement( 'image' )
  455. .toElement( 'img' );
  456. buildViewConverter().for( newEditor.data.viewToModel )
  457. .fromElement( 'img' )
  458. .toElement( 'image' );
  459. } );
  460. } );
  461. afterEach( () => {
  462. return editor.destroy();
  463. } );
  464. // This happens when browser automatically switches parent and child nodes.
  465. it( 'should handle mutations switching inner and outer node when adding new text node after', () => {
  466. setModelData( model,
  467. '<paragraph>' +
  468. '<$text italic="true" linkHref="foo">' +
  469. 'text[]' +
  470. '</$text>' +
  471. '</paragraph>'
  472. );
  473. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>text{}</i></a></p>' );
  474. const paragraph = viewRoot.getChild( 0 );
  475. const link = paragraph.getChild( 0 );
  476. const italic = link.getChild( 0 );
  477. const text = italic.getChild( 0 );
  478. // Simulate mutations and DOM change.
  479. domRoot.childNodes[ 0 ].innerHTML = '<i><a href="foo">text</a>x</i>';
  480. view.fire( 'mutations', [
  481. // First mutation - remove all children from link element.
  482. {
  483. type: 'children',
  484. node: link,
  485. oldChildren: [ italic ],
  486. newChildren: []
  487. },
  488. // Second mutation - remove link from paragraph and put italic there.
  489. {
  490. type: 'children',
  491. node: paragraph,
  492. oldChildren: [ link ],
  493. newChildren: [ italic ]
  494. },
  495. // Third mutation - italic's new children.
  496. {
  497. type: 'children',
  498. node: italic,
  499. oldChildren: [ text ],
  500. newChildren: [ new ViewElement( 'a', null, text ), new ViewText( 'x' ) ]
  501. }
  502. ] );
  503. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>textx{}</i></a></p>' );
  504. } );
  505. it( 'should handle mutations switching inner and outer node when adding new text node before', () => {
  506. setModelData( model,
  507. '<paragraph>' +
  508. '<$text italic="true" linkHref="foo">' +
  509. '[]text' +
  510. '</$text>' +
  511. '</paragraph>'
  512. );
  513. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>{}text</i></a></p>' );
  514. const paragraph = viewRoot.getChild( 0 );
  515. const link = paragraph.getChild( 0 );
  516. const italic = link.getChild( 0 );
  517. const text = italic.getChild( 0 );
  518. // Simulate mutations and DOM change.
  519. domRoot.childNodes[ 0 ].innerHTML = '<i>x<a href="foo">text</a></i>';
  520. view.fire( 'mutations', [
  521. // First mutation - remove all children from link element.
  522. {
  523. type: 'children',
  524. node: link,
  525. oldChildren: [ italic ],
  526. newChildren: []
  527. },
  528. // Second mutation - remove link from paragraph and put italic there.
  529. {
  530. type: 'children',
  531. node: paragraph,
  532. oldChildren: [ link ],
  533. newChildren: [ italic ]
  534. },
  535. // Third mutation - italic's new children.
  536. {
  537. type: 'children',
  538. node: italic,
  539. oldChildren: [ text ],
  540. newChildren: [ new ViewText( 'x' ), new ViewElement( 'a', null, text ) ]
  541. }
  542. ] );
  543. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>x{}text</i></a></p>' );
  544. } );
  545. it( 'should handle mutations switching inner and outer node - with text before', () => {
  546. setModelData( model,
  547. '<paragraph>' +
  548. 'xxx<$text italic="true" linkHref="foo">' +
  549. 'text[]' +
  550. '</$text>' +
  551. '</paragraph>'
  552. );
  553. expect( getViewData( view ) ).to.equal( '<p>xxx<a href="foo"><i>text{}</i></a></p>' );
  554. const paragraph = viewRoot.getChild( 0 );
  555. const textBefore = paragraph.getChild( 0 );
  556. const link = paragraph.getChild( 1 );
  557. const italic = link.getChild( 0 );
  558. const text = italic.getChild( 0 );
  559. // Simulate mutations and DOM change.
  560. domRoot.childNodes[ 0 ].innerHTML = 'xxx<i><a href="foo">text</a>x</i>';
  561. view.fire( 'mutations', [
  562. // First mutation - remove all children from link element.
  563. {
  564. type: 'children',
  565. node: link,
  566. oldChildren: [ italic ],
  567. newChildren: []
  568. },
  569. // Second mutation - remove link from paragraph and put italic there.
  570. {
  571. type: 'children',
  572. node: paragraph,
  573. oldChildren: [ textBefore, link ],
  574. newChildren: [ textBefore, italic ]
  575. },
  576. // Third mutation - italic's new children.
  577. {
  578. type: 'children',
  579. node: italic,
  580. oldChildren: [ text ],
  581. newChildren: [ new ViewElement( 'a', null, text ), new ViewText( 'x' ) ]
  582. }
  583. ] );
  584. expect( getViewData( view ) ).to.equal( '<p>xxx<a href="foo"><i>textx{}</i></a></p>' );
  585. } );
  586. // This happens when spell checker is applied on <strong> element and changes it to <b>.
  587. it( 'should handle mutations replacing node', () => {
  588. setModelData( model,
  589. '<paragraph>' +
  590. '<$text bold="true">' +
  591. 'text[]' +
  592. '</$text>' +
  593. '</paragraph>'
  594. );
  595. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  596. const paragraph = viewRoot.getChild( 0 );
  597. const strong = paragraph.getChild( 0 );
  598. // Simulate mutations and DOM change.
  599. domRoot.childNodes[ 0 ].innerHTML = '<b>fixed text</b>';
  600. view.fire( 'mutations', [
  601. // Replace `<strong>` with `<b>`.
  602. {
  603. type: 'children',
  604. node: paragraph,
  605. oldChildren: [ strong ],
  606. newChildren: [ new ViewElement( 'b', null, new ViewText( 'fixed text' ) ) ]
  607. }
  608. ] );
  609. expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '<p><strong>fixed text</strong></p>' );
  610. } );
  611. // Spell checker splits text inside attributes to two text nodes.
  612. it( 'should handle mutations inside attribute element', () => {
  613. setModelData( model,
  614. '<paragraph>' +
  615. '<$text bold="true">' +
  616. 'this is foo text[]' +
  617. '</$text>' +
  618. '</paragraph>'
  619. );
  620. expect( getViewData( view ) ).to.equal( '<p><strong>this is foo text{}</strong></p>' );
  621. const paragraph = viewRoot.getChild( 0 );
  622. const strong = paragraph.getChild( 0 );
  623. const text = strong.getChild( 0 );
  624. // Simulate mutations and DOM change.
  625. domRoot.childNodes[ 0 ].innerHTML = '<strong>this is bar text</strong>';
  626. view.fire( 'mutations', [
  627. {
  628. type: 'children',
  629. node: strong,
  630. oldChildren: [ text ],
  631. newChildren: [ new ViewText( 'this is bar' ), new ViewText( ' text' ) ]
  632. }
  633. ] );
  634. expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '<p><strong>this is bar text</strong></p>' );
  635. } );
  636. it( 'should do nothing if elements mutated', () => {
  637. setModelData( model,
  638. '<paragraph>' +
  639. '<$text bold="true">' +
  640. 'text[]' +
  641. '</$text>' +
  642. '</paragraph>'
  643. );
  644. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  645. const paragraph = viewRoot.getChild( 0 );
  646. const strong = paragraph.getChild( 0 );
  647. // Simulate mutations and DOM change.
  648. domRoot.childNodes[ 0 ].innerHTML = '<strong>text</strong><img />';
  649. view.fire( 'mutations', [
  650. {
  651. type: 'children',
  652. node: paragraph,
  653. oldChildren: [ strong ],
  654. newChildren: [
  655. new ViewElement( 'strong', null, new ViewText( 'text' ) ),
  656. new ViewElement( 'img' )
  657. ]
  658. }
  659. ] );
  660. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  661. } );
  662. it( 'should do nothing if text is not changed', () => {
  663. setModelData( model,
  664. '<paragraph>' +
  665. '<$text bold="true">' +
  666. 'text[]' +
  667. '</$text>' +
  668. '</paragraph>'
  669. );
  670. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  671. const paragraph = viewRoot.getChild( 0 );
  672. const strong = paragraph.getChild( 0 );
  673. // Simulate mutations and DOM change.
  674. domRoot.childNodes[ 0 ].innerHTML = '<strong>text</strong>';
  675. view.fire( 'mutations', [
  676. {
  677. type: 'children',
  678. node: paragraph,
  679. oldChildren: [ strong ],
  680. newChildren: [ new ViewElement( 'strong', null, new ViewText( 'text' ) ) ]
  681. }
  682. ] );
  683. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  684. } );
  685. it( 'should do nothing on empty mutations', () => {
  686. setModelData( model,
  687. '<paragraph>' +
  688. '<$text bold="true">' +
  689. 'text[]' +
  690. '</$text>' +
  691. '</paragraph>'
  692. );
  693. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  694. // Simulate mutations and DOM change.
  695. domRoot.childNodes[ 0 ].innerHTML = '<strong>text</strong>';
  696. view.fire( 'mutations', [] );
  697. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  698. } );
  699. it( 'should do nothing if mutations does not have common ancestor', () => {
  700. setModelData( model,
  701. '<paragraph>' +
  702. '<$text bold="true">' +
  703. 'text[]' +
  704. '</$text>' +
  705. '</paragraph>'
  706. );
  707. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  708. const paragraph = viewRoot.getChild( 0 );
  709. const strong = paragraph.getChild( 0 );
  710. // Simulate mutations and DOM change.
  711. domRoot.childNodes[ 0 ].innerHTML = '<strong>text</strong>';
  712. view.fire( 'mutations', [
  713. {
  714. type: 'children',
  715. node: paragraph,
  716. oldChildren: [ strong ],
  717. newChildren: [ strong ]
  718. },
  719. {
  720. type: 'children',
  721. node: new ViewContainerElement( 'div' ),
  722. oldChildren: [],
  723. newChildren: [ new ViewText( 'foo' ), new ViewText( 'bar' ) ]
  724. }
  725. ] );
  726. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  727. } );
  728. it( 'should handle view selection if one is returned from mutations', () => {
  729. setModelData( model,
  730. '<paragraph>' +
  731. '<$text bold="true">' +
  732. 'text[]' +
  733. '</$text>' +
  734. '</paragraph>'
  735. );
  736. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  737. const paragraph = viewRoot.getChild( 0 );
  738. const strong = paragraph.getChild( 0 );
  739. const viewSelection = new ViewSelection();
  740. viewSelection.collapse( paragraph, 0 );
  741. // Simulate mutations and DOM change.
  742. domRoot.childNodes[ 0 ].innerHTML = '<b>textx</b>';
  743. view.fire( 'mutations', [
  744. // Replace `<strong>` with `<b>`.
  745. {
  746. type: 'children',
  747. node: paragraph,
  748. oldChildren: [ strong ],
  749. newChildren: [ new ViewElement( 'b', null, new ViewText( 'textx' ) ) ]
  750. }
  751. ], viewSelection );
  752. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">[]textx</$text></paragraph>' );
  753. expect( getViewData( view ) ).to.equal( '<p><strong>{}textx</strong></p>' );
  754. } );
  755. } );
  756. } );