input.js 16 KB

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