input.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Input from '../src/input';
  9. import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  10. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  11. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  12. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  13. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  14. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  15. import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
  16. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  17. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  18. import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  19. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  20. describe( 'Input feature', () => {
  21. let editor, model, modelRoot, view, viewRoot, listenter;
  22. testUtils.createSinonSandbox();
  23. before( () => {
  24. listenter = Object.create( EmitterMixin );
  25. return VirtualTestEditor.create( { plugins: [ Input, Paragraph ] } )
  26. .then( newEditor => {
  27. // Mock image feature.
  28. newEditor.document.schema.registerItem( 'image', '$inline' );
  29. buildModelConverter().for( newEditor.data.modelToView, newEditor.editing.modelToView )
  30. .fromElement( 'image' )
  31. .toElement( 'img' );
  32. buildViewConverter().for( newEditor.data.viewToModel )
  33. .fromElement( 'img' )
  34. .toElement( 'image' );
  35. editor = newEditor;
  36. model = editor.editing.model;
  37. modelRoot = model.getRoot();
  38. view = editor.editing.view;
  39. viewRoot = view.getRoot();
  40. } );
  41. } );
  42. after( () => {
  43. return editor.destroy();
  44. } );
  45. beforeEach( () => {
  46. editor.setData( '<p>foobar</p>' );
  47. model.enqueueChanges( () => {
  48. model.selection.setRanges( [
  49. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 0 ), 3 )
  50. ] );
  51. } );
  52. } );
  53. afterEach( () => {
  54. listenter.stopListening();
  55. } );
  56. describe( 'mutations handling', () => {
  57. it( 'should handle text mutation', () => {
  58. view.fire( 'mutations', [
  59. {
  60. type: 'text',
  61. oldText: 'foobar',
  62. newText: 'fooxbar',
  63. node: viewRoot.getChild( 0 ).getChild( 0 )
  64. }
  65. ] );
  66. expect( getModelData( model ) ).to.equal( '<paragraph>foox[]bar</paragraph>' );
  67. expect( getViewData( view ) ).to.equal( '<p>foox{}bar</p>' );
  68. } );
  69. it( 'should handle text mutation change', () => {
  70. view.fire( 'mutations', [
  71. {
  72. type: 'text',
  73. oldText: 'foobar',
  74. newText: 'foodar',
  75. node: viewRoot.getChild( 0 ).getChild( 0 )
  76. }
  77. ] );
  78. expect( getModelData( model ) ).to.equal( '<paragraph>food[]ar</paragraph>' );
  79. expect( getViewData( view ) ).to.equal( '<p>food{}ar</p>' );
  80. } );
  81. it( 'should handle text node insertion', () => {
  82. editor.setData( '<p></p>' );
  83. view.fire( 'mutations', [
  84. {
  85. type: 'children',
  86. oldChildren: [],
  87. newChildren: [ new ViewText( 'x' ) ],
  88. node: viewRoot.getChild( 0 )
  89. }
  90. ] );
  91. expect( getModelData( model ) ).to.equal( '<paragraph>x[]</paragraph>' );
  92. expect( getViewData( view ) ).to.equal( '<p>x{}</p>' );
  93. } );
  94. it( 'should do nothing when two nodes were inserted', () => {
  95. editor.setData( '<p></p>' );
  96. view.fire( 'mutations', [
  97. {
  98. type: 'children',
  99. oldChildren: [],
  100. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  101. node: viewRoot.getChild( 0 )
  102. }
  103. ] );
  104. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  105. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  106. } );
  107. it( 'should do nothing when two nodes were inserted and one removed', () => {
  108. view.fire( 'mutations', [
  109. {
  110. type: 'children',
  111. oldChildren: [ new ViewText( 'foobar' ) ],
  112. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  113. node: viewRoot.getChild( 0 )
  114. }
  115. ] );
  116. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  117. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  118. } );
  119. it( 'should handle multiple children in the node', () => {
  120. editor.setData( '<p>foo<img></img></p>' );
  121. view.fire( 'mutations', [
  122. {
  123. type: 'children',
  124. oldChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ) ],
  125. newChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ), new ViewText( 'x' ) ],
  126. node: viewRoot.getChild( 0 )
  127. }
  128. ] );
  129. expect( getModelData( model ) ).to.equal( '<paragraph>foo<image></image>x[]</paragraph>' );
  130. expect( getViewData( view ) ).to.equal( '<p>foo<img></img>x{}</p>' );
  131. } );
  132. it( 'should do nothing when node was removed', () => {
  133. view.fire( 'mutations', [
  134. {
  135. type: 'children',
  136. oldChildren: [ new ViewText( 'foobar' ) ],
  137. newChildren: [],
  138. node: viewRoot.getChild( 0 )
  139. }
  140. ] );
  141. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  142. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  143. } );
  144. it( 'should do nothing when element was inserted', () => {
  145. editor.setData( '<p></p>' );
  146. view.fire( 'mutations', [
  147. {
  148. type: 'children',
  149. oldChildren: [],
  150. newChildren: [ new ViewElement( 'img' ) ],
  151. node: viewRoot.getChild( 0 )
  152. }
  153. ] );
  154. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  155. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  156. } );
  157. it( 'should set model selection appropriately to view selection passed in mutations event', () => {
  158. // This test case emulates spellchecker correction.
  159. const viewSelection = new ViewSelection();
  160. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  161. view.fire( 'mutations',
  162. [ {
  163. type: 'text',
  164. oldText: 'foobar',
  165. newText: 'foodar',
  166. node: viewRoot.getChild( 0 ).getChild( 0 )
  167. } ],
  168. viewSelection
  169. );
  170. expect( getModelData( model ) ).to.equal( '<paragraph>foodar[]</paragraph>' );
  171. expect( getViewData( view ) ).to.equal( '<p>foodar{}</p>' );
  172. } );
  173. it( 'should use up to one insert and remove operations (spellchecker)', () => {
  174. // This test case emulates spellchecker correction.
  175. const viewSelection = new ViewSelection();
  176. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  177. testUtils.sinon.spy( Batch.prototype, 'weakInsert' );
  178. testUtils.sinon.spy( Batch.prototype, 'remove' );
  179. view.fire( 'mutations',
  180. [ {
  181. type: 'text',
  182. oldText: 'foobar',
  183. newText: 'fxobxr',
  184. node: viewRoot.getChild( 0 ).getChild( 0 )
  185. } ],
  186. viewSelection
  187. );
  188. expect( Batch.prototype.weakInsert.calledOnce ).to.be.true;
  189. expect( Batch.prototype.remove.calledOnce ).to.be.true;
  190. } );
  191. it( 'should place selection after when correcting to longer word (spellchecker)', () => {
  192. // This test case emulates spellchecker correction.
  193. editor.setData( '<p>Foo hous a</p>' );
  194. const viewSelection = new ViewSelection();
  195. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  196. view.fire( 'mutations',
  197. [ {
  198. type: 'text',
  199. oldText: 'Foo hous a',
  200. newText: 'Foo house a',
  201. node: viewRoot.getChild( 0 ).getChild( 0 )
  202. } ],
  203. viewSelection
  204. );
  205. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[] a</paragraph>' );
  206. expect( getViewData( view ) ).to.equal( '<p>Foo house{} a</p>' );
  207. } );
  208. it( 'should place selection after when correcting to shorter word (spellchecker)', () => {
  209. // This test case emulates spellchecker correction.
  210. editor.setData( '<p>Bar athat foo</p>' );
  211. const viewSelection = new ViewSelection();
  212. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  213. view.fire( 'mutations',
  214. [ {
  215. type: 'text',
  216. oldText: 'Bar athat foo',
  217. newText: 'Bar that foo',
  218. node: viewRoot.getChild( 0 ).getChild( 0 )
  219. } ],
  220. viewSelection
  221. );
  222. expect( getModelData( model ) ).to.equal( '<paragraph>Bar that[] foo</paragraph>' );
  223. expect( getViewData( view ) ).to.equal( '<p>Bar that{} foo</p>' );
  224. } );
  225. it( 'should place selection after when merging two words (spellchecker)', () => {
  226. // This test case emulates spellchecker correction.
  227. editor.setData( '<p>Foo hous e</p>' );
  228. const viewSelection = new ViewSelection();
  229. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  230. view.fire( 'mutations',
  231. [ {
  232. type: 'text',
  233. oldText: 'Foo hous e',
  234. newText: 'Foo house',
  235. node: viewRoot.getChild( 0 ).getChild( 0 )
  236. } ],
  237. viewSelection
  238. );
  239. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[]</paragraph>' );
  240. expect( getViewData( view ) ).to.equal( '<p>Foo house{}</p>' );
  241. } );
  242. it( 'should place non-collapsed selection after changing single character (composition)', () => {
  243. editor.setData( '<p>Foo house</p>' );
  244. const viewSelection = new ViewSelection();
  245. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  246. viewSelection.setFocus( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  247. view.fire( 'mutations',
  248. [ {
  249. type: 'text',
  250. oldText: 'Foo house',
  251. newText: 'Foo housa',
  252. node: viewRoot.getChild( 0 ).getChild( 0 )
  253. } ],
  254. viewSelection
  255. );
  256. expect( getModelData( model ) ).to.equal( '<paragraph>Foo hous[a]</paragraph>' );
  257. expect( getViewData( view ) ).to.equal( '<p>Foo hous{a}</p>' );
  258. } );
  259. it( 'should replace last &nbsp; with space', () => {
  260. model.enqueueChanges( () => {
  261. model.selection.setRanges( [
  262. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  263. ] );
  264. } );
  265. view.fire( 'mutations', [
  266. {
  267. type: 'text',
  268. oldText: 'foobar',
  269. newText: 'foobar\u00A0',
  270. node: viewRoot.getChild( 0 ).getChild( 0 )
  271. }
  272. ] );
  273. expect( getModelData( model ) ).to.equal( '<paragraph>foobar []</paragraph>' );
  274. expect( getViewData( view ) ).to.equal( '<p>foobar {}</p>' );
  275. } );
  276. it( 'should replace first &nbsp; with space', () => {
  277. model.enqueueChanges( () => {
  278. model.selection.setRanges( [
  279. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 0 ), 0 )
  280. ] );
  281. } );
  282. view.fire( 'mutations', [
  283. {
  284. type: 'text',
  285. oldText: 'foobar',
  286. newText: '\u00A0foobar',
  287. node: viewRoot.getChild( 0 ).getChild( 0 )
  288. }
  289. ] );
  290. expect( getModelData( model ) ).to.equal( '<paragraph> []foobar</paragraph>' );
  291. expect( getViewData( view ) ).to.equal( '<p> {}foobar</p>' );
  292. } );
  293. it( 'should replace all &nbsp; with spaces', () => {
  294. model.enqueueChanges( () => {
  295. model.selection.setRanges( [
  296. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  297. ] );
  298. } );
  299. view.fire( 'mutations', [
  300. {
  301. type: 'text',
  302. oldText: 'foobar',
  303. newText: 'foobar\u00A0\u00A0\u00A0baz',
  304. node: viewRoot.getChild( 0 ).getChild( 0 )
  305. }
  306. ] );
  307. expect( getModelData( model ) ).to.equal( '<paragraph>foobar baz[]</paragraph>' );
  308. expect( getViewData( view ) ).to.equal( '<p>foobar baz{}</p>' );
  309. } );
  310. } );
  311. describe( 'keystroke handling', () => {
  312. it( 'should remove contents', () => {
  313. model.enqueueChanges( () => {
  314. model.selection.setRanges( [
  315. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  316. } );
  317. listenter.listenTo( view, 'keydown', () => {
  318. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  319. view.fire( 'mutations', [
  320. {
  321. type: 'text',
  322. oldText: 'foar',
  323. newText: 'foyar',
  324. node: viewRoot.getChild( 0 ).getChild( 0 )
  325. }
  326. ] );
  327. }, { priority: 'lowest' } );
  328. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  329. expect( getModelData( model ) ).to.equal( '<paragraph>foy[]ar</paragraph>' );
  330. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  331. } );
  332. it( 'should do nothing on arrow key', () => {
  333. model.enqueueChanges( () => {
  334. model.selection.setRanges( [
  335. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  336. } );
  337. view.fire( 'keydown', { keyCode: getCode( 'arrowright' ) } );
  338. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  339. } );
  340. it( 'should do nothing on ctrl combinations', () => {
  341. model.enqueueChanges( () => {
  342. model.selection.setRanges( [
  343. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  344. } );
  345. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  346. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  347. } );
  348. it( 'should do nothing on non printable keys', () => {
  349. model.enqueueChanges( () => {
  350. model.selection.setRanges( [
  351. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  352. } );
  353. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  354. view.fire( 'keydown', { keyCode: 35 } ); // Home
  355. view.fire( 'keydown', { keyCode: 112 } ); // F1
  356. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  357. } );
  358. // #69
  359. it( 'should do nothing on tab key', () => {
  360. model.enqueueChanges( () => {
  361. model.selection.setRanges( [
  362. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  363. } );
  364. view.fire( 'keydown', { keyCode: 9 } ); // Tab
  365. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  366. } );
  367. // #82
  368. it( 'should do nothing on composition start key', () => {
  369. model.enqueueChanges( () => {
  370. model.selection.setRanges( [
  371. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  372. } );
  373. view.fire( 'keydown', { keyCode: 229 } );
  374. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  375. } );
  376. it( 'should do nothing if selection is collapsed', () => {
  377. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  378. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  379. } );
  380. it( 'should lock buffer if selection is not collapsed', () => {
  381. const buffer = editor.commands.get( 'input' )._buffer;
  382. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  383. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  384. model.enqueueChanges( () => {
  385. model.selection.setRanges( [
  386. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  387. } );
  388. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  389. expect( lockSpy.calledOnce ).to.be.true;
  390. expect( unlockSpy.calledOnce ).to.be.true;
  391. } );
  392. it( 'should not lock buffer on non printable keys', () => {
  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. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  397. view.fire( 'keydown', { keyCode: 35 } ); // Home
  398. view.fire( 'keydown', { keyCode: 112 } ); // F1
  399. expect( lockSpy.callCount ).to.be.equal( 0 );
  400. expect( unlockSpy.callCount ).to.be.equal( 0 );
  401. } );
  402. it( 'should not lock buffer on collapsed selection', () => {
  403. const buffer = editor.commands.get( 'input' )._buffer;
  404. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  405. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  406. view.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  407. view.fire( 'keydown', { keyCode: getCode( 'a' ) } );
  408. view.fire( 'keydown', { keyCode: getCode( 'z' ) } );
  409. expect( lockSpy.callCount ).to.be.equal( 0 );
  410. expect( unlockSpy.callCount ).to.be.equal( 0 );
  411. } );
  412. } );
  413. } );