input.js 29 KB

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