8
0

input.js 15 KB

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