input.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
  21. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  22. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  23. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  24. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  25. describe( 'Input feature', () => {
  26. let editor, model, modelRoot, view, viewRoot, listenter;
  27. testUtils.createSinonSandbox();
  28. before( () => {
  29. listenter = Object.create( EmitterMixin );
  30. return VirtualTestEditor.create( { plugins: [ Input, Paragraph ] } )
  31. .then( newEditor => {
  32. // Mock image feature.
  33. newEditor.document.schema.registerItem( 'image', '$inline' );
  34. buildModelConverter().for( newEditor.data.modelToView, newEditor.editing.modelToView )
  35. .fromElement( 'image' )
  36. .toElement( 'img' );
  37. buildViewConverter().for( newEditor.data.viewToModel )
  38. .fromElement( 'img' )
  39. .toElement( 'image' );
  40. editor = newEditor;
  41. model = editor.editing.model;
  42. modelRoot = model.getRoot();
  43. view = editor.editing.view;
  44. viewRoot = view.getRoot();
  45. } );
  46. } );
  47. after( () => {
  48. return editor.destroy();
  49. } );
  50. beforeEach( () => {
  51. editor.setData( '<p>foobar</p>' );
  52. model.enqueueChanges( () => {
  53. model.selection.setRanges( [
  54. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 0 ), 3 )
  55. ] );
  56. } );
  57. } );
  58. afterEach( () => {
  59. listenter.stopListening();
  60. } );
  61. describe( 'mutations handling', () => {
  62. it( 'should handle text mutation', () => {
  63. view.fire( 'mutations', [
  64. {
  65. type: 'text',
  66. oldText: 'foobar',
  67. newText: 'fooxbar',
  68. node: viewRoot.getChild( 0 ).getChild( 0 )
  69. }
  70. ] );
  71. expect( getModelData( model ) ).to.equal( '<paragraph>foox[]bar</paragraph>' );
  72. expect( getViewData( view ) ).to.equal( '<p>foox{}bar</p>' );
  73. } );
  74. it( 'should handle text mutation change', () => {
  75. view.fire( 'mutations', [
  76. {
  77. type: 'text',
  78. oldText: 'foobar',
  79. newText: 'foodar',
  80. node: viewRoot.getChild( 0 ).getChild( 0 )
  81. }
  82. ] );
  83. expect( getModelData( model ) ).to.equal( '<paragraph>food[]ar</paragraph>' );
  84. expect( getViewData( view ) ).to.equal( '<p>food{}ar</p>' );
  85. } );
  86. it( 'should handle text node insertion', () => {
  87. editor.setData( '<p></p>' );
  88. view.fire( 'mutations', [
  89. {
  90. type: 'children',
  91. oldChildren: [],
  92. newChildren: [ new ViewText( 'x' ) ],
  93. node: viewRoot.getChild( 0 )
  94. }
  95. ] );
  96. expect( getModelData( model ) ).to.equal( '<paragraph>x[]</paragraph>' );
  97. expect( getViewData( view ) ).to.equal( '<p>x{}</p>' );
  98. } );
  99. it( 'should do nothing when two nodes were inserted', () => {
  100. editor.setData( '<p></p>' );
  101. view.fire( 'mutations', [
  102. {
  103. type: 'children',
  104. oldChildren: [],
  105. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  106. node: viewRoot.getChild( 0 )
  107. }
  108. ] );
  109. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  110. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  111. } );
  112. it( 'should do nothing when two nodes were inserted and one removed', () => {
  113. view.fire( 'mutations', [
  114. {
  115. type: 'children',
  116. oldChildren: [ new ViewText( 'foobar' ) ],
  117. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  118. node: viewRoot.getChild( 0 )
  119. }
  120. ] );
  121. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  122. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  123. } );
  124. it( 'should handle multiple children in the node', () => {
  125. editor.setData( '<p>foo<img></img></p>' );
  126. view.fire( 'mutations', [
  127. {
  128. type: 'children',
  129. oldChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ) ],
  130. newChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ), new ViewText( 'x' ) ],
  131. node: viewRoot.getChild( 0 )
  132. }
  133. ] );
  134. expect( getModelData( model ) ).to.equal( '<paragraph>foo<image></image>x[]</paragraph>' );
  135. expect( getViewData( view ) ).to.equal( '<p>foo<img></img>x{}</p>' );
  136. } );
  137. it( 'should do nothing when node was removed', () => {
  138. view.fire( 'mutations', [
  139. {
  140. type: 'children',
  141. oldChildren: [ new ViewText( 'foobar' ) ],
  142. newChildren: [],
  143. node: viewRoot.getChild( 0 )
  144. }
  145. ] );
  146. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  147. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  148. } );
  149. it( 'should do nothing when element was inserted', () => {
  150. editor.setData( '<p></p>' );
  151. view.fire( 'mutations', [
  152. {
  153. type: 'children',
  154. oldChildren: [],
  155. newChildren: [ new ViewElement( 'img' ) ],
  156. node: viewRoot.getChild( 0 )
  157. }
  158. ] );
  159. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  160. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  161. } );
  162. it( 'should set model selection appropriately to view selection passed in mutations event', () => {
  163. // This test case emulates spellchecker correction.
  164. const viewSelection = new ViewSelection();
  165. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  166. view.fire( 'mutations',
  167. [ {
  168. type: 'text',
  169. oldText: 'foobar',
  170. newText: 'foodar',
  171. node: viewRoot.getChild( 0 ).getChild( 0 )
  172. } ],
  173. viewSelection
  174. );
  175. expect( getModelData( model ) ).to.equal( '<paragraph>foodar[]</paragraph>' );
  176. expect( getViewData( view ) ).to.equal( '<p>foodar{}</p>' );
  177. } );
  178. it( 'should use up to one insert and remove operations (spellchecker)', () => {
  179. // This test case emulates spellchecker correction.
  180. const viewSelection = new ViewSelection();
  181. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  182. testUtils.sinon.spy( Batch.prototype, 'weakInsert' );
  183. testUtils.sinon.spy( Batch.prototype, 'remove' );
  184. view.fire( 'mutations',
  185. [ {
  186. type: 'text',
  187. oldText: 'foobar',
  188. newText: 'fxobxr',
  189. node: viewRoot.getChild( 0 ).getChild( 0 )
  190. } ],
  191. viewSelection
  192. );
  193. expect( Batch.prototype.weakInsert.calledOnce ).to.be.true;
  194. expect( Batch.prototype.remove.calledOnce ).to.be.true;
  195. } );
  196. it( 'should place selection after when correcting to longer word (spellchecker)', () => {
  197. // This test case emulates spellchecker correction.
  198. editor.setData( '<p>Foo hous a</p>' );
  199. const viewSelection = new ViewSelection();
  200. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  201. view.fire( 'mutations',
  202. [ {
  203. type: 'text',
  204. oldText: 'Foo hous a',
  205. newText: 'Foo house a',
  206. node: viewRoot.getChild( 0 ).getChild( 0 )
  207. } ],
  208. viewSelection
  209. );
  210. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[] a</paragraph>' );
  211. expect( getViewData( view ) ).to.equal( '<p>Foo house{} a</p>' );
  212. } );
  213. it( 'should place selection after when correcting to shorter word (spellchecker)', () => {
  214. // This test case emulates spellchecker correction.
  215. editor.setData( '<p>Bar athat foo</p>' );
  216. const viewSelection = new ViewSelection();
  217. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  218. view.fire( 'mutations',
  219. [ {
  220. type: 'text',
  221. oldText: 'Bar athat foo',
  222. newText: 'Bar that foo',
  223. node: viewRoot.getChild( 0 ).getChild( 0 )
  224. } ],
  225. viewSelection
  226. );
  227. expect( getModelData( model ) ).to.equal( '<paragraph>Bar that[] foo</paragraph>' );
  228. expect( getViewData( view ) ).to.equal( '<p>Bar that{} foo</p>' );
  229. } );
  230. it( 'should place selection after when merging two words (spellchecker)', () => {
  231. // This test case emulates spellchecker correction.
  232. editor.setData( '<p>Foo hous e</p>' );
  233. const viewSelection = new ViewSelection();
  234. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  235. view.fire( 'mutations',
  236. [ {
  237. type: 'text',
  238. oldText: 'Foo hous e',
  239. newText: 'Foo house',
  240. node: viewRoot.getChild( 0 ).getChild( 0 )
  241. } ],
  242. viewSelection
  243. );
  244. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[]</paragraph>' );
  245. expect( getViewData( view ) ).to.equal( '<p>Foo house{}</p>' );
  246. } );
  247. it( 'should place non-collapsed selection after changing single character (composition)', () => {
  248. editor.setData( '<p>Foo house</p>' );
  249. const viewSelection = new ViewSelection();
  250. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  251. viewSelection.setFocus( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  252. view.fire( 'mutations',
  253. [ {
  254. type: 'text',
  255. oldText: 'Foo house',
  256. newText: 'Foo housa',
  257. node: viewRoot.getChild( 0 ).getChild( 0 )
  258. } ],
  259. viewSelection
  260. );
  261. expect( getModelData( model ) ).to.equal( '<paragraph>Foo hous[a]</paragraph>' );
  262. expect( getViewData( view ) ).to.equal( '<p>Foo hous{a}</p>' );
  263. } );
  264. it( 'should replace last &nbsp; with space', () => {
  265. model.enqueueChanges( () => {
  266. model.selection.setRanges( [
  267. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  268. ] );
  269. } );
  270. view.fire( 'mutations', [
  271. {
  272. type: 'text',
  273. oldText: 'foobar',
  274. newText: 'foobar\u00A0',
  275. node: viewRoot.getChild( 0 ).getChild( 0 )
  276. }
  277. ] );
  278. expect( getModelData( model ) ).to.equal( '<paragraph>foobar []</paragraph>' );
  279. expect( getViewData( view ) ).to.equal( '<p>foobar {}</p>' );
  280. } );
  281. it( 'should replace first &nbsp; with space', () => {
  282. model.enqueueChanges( () => {
  283. model.selection.setRanges( [
  284. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 0 ), 0 )
  285. ] );
  286. } );
  287. view.fire( 'mutations', [
  288. {
  289. type: 'text',
  290. oldText: 'foobar',
  291. newText: '\u00A0foobar',
  292. node: viewRoot.getChild( 0 ).getChild( 0 )
  293. }
  294. ] );
  295. expect( getModelData( model ) ).to.equal( '<paragraph> []foobar</paragraph>' );
  296. expect( getViewData( view ) ).to.equal( '<p> {}foobar</p>' );
  297. } );
  298. it( 'should replace all &nbsp; with spaces', () => {
  299. model.enqueueChanges( () => {
  300. model.selection.setRanges( [
  301. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  302. ] );
  303. } );
  304. view.fire( 'mutations', [
  305. {
  306. type: 'text',
  307. oldText: 'foobar',
  308. newText: 'foobar\u00A0\u00A0\u00A0baz',
  309. node: viewRoot.getChild( 0 ).getChild( 0 )
  310. }
  311. ] );
  312. expect( getModelData( model ) ).to.equal( '<paragraph>foobar baz[]</paragraph>' );
  313. expect( getViewData( view ) ).to.equal( '<p>foobar baz{}</p>' );
  314. } );
  315. } );
  316. describe( 'keystroke handling', () => {
  317. it( 'should remove contents', () => {
  318. model.enqueueChanges( () => {
  319. model.selection.setRanges( [
  320. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  321. } );
  322. listenter.listenTo( view, 'keydown', () => {
  323. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  324. }, { priority: 'lowest' } );
  325. } );
  326. // #97
  327. it( 'should remove contents and merge blocks', () => {
  328. setModelData( model, '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  329. listenter.listenTo( view, 'keydown', () => {
  330. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  331. view.fire( 'mutations', [
  332. {
  333. type: 'text',
  334. oldText: 'foar',
  335. newText: 'foyar',
  336. node: viewRoot.getChild( 0 ).getChild( 0 )
  337. }
  338. ] );
  339. }, { priority: 'lowest' } );
  340. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  341. expect( getModelData( model ) ).to.equal( '<paragraph>foy[]ar</paragraph>' );
  342. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  343. } );
  344. it( 'should do nothing on arrow key', () => {
  345. model.enqueueChanges( () => {
  346. model.selection.setRanges( [
  347. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  348. } );
  349. view.fire( 'keydown', { keyCode: getCode( 'arrowright' ) } );
  350. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  351. } );
  352. it( 'should do nothing on ctrl combinations', () => {
  353. model.enqueueChanges( () => {
  354. model.selection.setRanges( [
  355. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  356. } );
  357. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  358. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  359. } );
  360. it( 'should do nothing on non printable keys', () => {
  361. model.enqueueChanges( () => {
  362. model.selection.setRanges( [
  363. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  364. } );
  365. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  366. view.fire( 'keydown', { keyCode: 35 } ); // Home
  367. view.fire( 'keydown', { keyCode: 112 } ); // F1
  368. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  369. } );
  370. // #69
  371. it( 'should do nothing on tab key', () => {
  372. model.enqueueChanges( () => {
  373. model.selection.setRanges( [
  374. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  375. } );
  376. view.fire( 'keydown', { keyCode: 9 } ); // Tab
  377. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  378. } );
  379. // #82
  380. it( 'should do nothing on composition start key', () => {
  381. model.enqueueChanges( () => {
  382. model.selection.setRanges( [
  383. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  384. } );
  385. view.fire( 'keydown', { keyCode: 229 } );
  386. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  387. } );
  388. it( 'should do nothing if selection is collapsed', () => {
  389. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  390. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  391. } );
  392. it( 'should lock buffer if selection is not collapsed', () => {
  393. const buffer = editor.commands.get( 'input' )._buffer;
  394. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  395. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  396. model.enqueueChanges( () => {
  397. model.selection.setRanges( [
  398. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  399. } );
  400. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  401. expect( lockSpy.calledOnce ).to.be.true;
  402. expect( unlockSpy.calledOnce ).to.be.true;
  403. } );
  404. it( 'should not lock buffer on non printable keys', () => {
  405. const buffer = editor.commands.get( 'input' )._buffer;
  406. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  407. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  408. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  409. view.fire( 'keydown', { keyCode: 35 } ); // Home
  410. view.fire( 'keydown', { keyCode: 112 } ); // F1
  411. expect( lockSpy.callCount ).to.be.equal( 0 );
  412. expect( unlockSpy.callCount ).to.be.equal( 0 );
  413. } );
  414. it( 'should not lock buffer on collapsed selection', () => {
  415. const buffer = editor.commands.get( 'input' )._buffer;
  416. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  417. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  418. view.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  419. view.fire( 'keydown', { keyCode: getCode( 'a' ) } );
  420. view.fire( 'keydown', { keyCode: getCode( 'z' ) } );
  421. expect( lockSpy.callCount ).to.be.equal( 0 );
  422. expect( unlockSpy.callCount ).to.be.equal( 0 );
  423. } );
  424. it( 'should not modify document when input command is disabled and selection is collapsed', () => {
  425. setModelData( model, '<paragraph>foo[]bar</paragraph>' );
  426. editor.commands.get( 'input' ).isEnabled = false;
  427. view.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  428. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  429. } );
  430. it( 'should not modify document when input command is disabled and selection is non-collapsed', () => {
  431. setModelData( model, '<paragraph>fo[ob]ar</paragraph>' );
  432. editor.commands.get( 'input' ).isEnabled = false;
  433. view.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  434. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  435. } );
  436. } );
  437. describe( '#100', () => {
  438. let domElement, domRoot;
  439. beforeEach( () => {
  440. domElement = document.createElement( 'div' );
  441. document.body.appendChild( domElement );
  442. return ClassicTestEditor.create( domElement, { plugins: [ Input, Paragraph, Bold, Italic, LinkEngine ] } )
  443. .then( newEditor => {
  444. editor = newEditor;
  445. model = editor.editing.model;
  446. modelRoot = model.getRoot();
  447. view = editor.editing.view;
  448. viewRoot = view.getRoot();
  449. domRoot = view.getDomRoot();
  450. } );
  451. } );
  452. afterEach( () => {
  453. domElement.remove();
  454. } );
  455. it( 'should handle typing when wrapping is switched', () => {
  456. setModelData( model,
  457. '<paragraph>' +
  458. '<$text italic="true" linkHref="foo">' +
  459. 'text[]' +
  460. '</$text>' +
  461. '</paragraph>'
  462. );
  463. const paragraph = viewRoot.getChild( 0 );
  464. const link = paragraph.getChild( 0 );
  465. const italic = link.getChild( 0 );
  466. const text = italic.getChild( 0 );
  467. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>text{}</i></a></p>' );
  468. // Simulate mutations and DOM change.
  469. domRoot.childNodes[ 0 ].innerHTML = '<i><a href="foo">text</a>x</i>';
  470. view.fire( 'mutations', [
  471. // First mutation - remove all children from link element.
  472. {
  473. type: 'children',
  474. node: link,
  475. oldChildren: [ italic ],
  476. newChildren: []
  477. },
  478. // Second mutation - remove link from paragraph and put italic there.
  479. {
  480. type: 'children',
  481. node: paragraph,
  482. oldChildren: [ link ],
  483. newChildren: [ italic ]
  484. },
  485. // Third mutation - italic's new children.
  486. {
  487. type: 'children',
  488. node: italic,
  489. oldChildren: [ text ],
  490. newChildren: [ new ViewElement( 'a', null, text ), new ViewText( 'x' ) ]
  491. }
  492. ] );
  493. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>textx{}</i></a></p>' );
  494. } );
  495. } );
  496. } );