input.js 29 KB

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