input.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  21. describe( 'Input feature', () => {
  22. let editor, model, modelRoot, view, viewRoot, listenter;
  23. testUtils.createSinonSandbox();
  24. before( () => {
  25. listenter = Object.create( EmitterMixin );
  26. return VirtualTestEditor.create( {
  27. plugins: [ Input, Paragraph ]
  28. } )
  29. .then( newEditor => {
  30. // Mock image feature.
  31. newEditor.document.schema.registerItem( 'image', '$inline' );
  32. buildModelConverter().for( newEditor.data.modelToView, newEditor.editing.modelToView )
  33. .fromElement( 'image' )
  34. .toElement( 'img' );
  35. buildViewConverter().for( newEditor.data.viewToModel )
  36. .fromElement( 'img' )
  37. .toElement( 'image' );
  38. editor = newEditor;
  39. model = editor.editing.model;
  40. modelRoot = model.getRoot();
  41. view = editor.editing.view;
  42. viewRoot = view.getRoot();
  43. } );
  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 replace last &nbsp; with space', () => {
  243. model.enqueueChanges( () => {
  244. model.selection.setRanges( [
  245. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  246. ] );
  247. } );
  248. view.fire( 'mutations', [
  249. {
  250. type: 'text',
  251. oldText: 'foobar',
  252. newText: 'foobar\u00A0',
  253. node: viewRoot.getChild( 0 ).getChild( 0 )
  254. }
  255. ] );
  256. expect( getModelData( model ) ).to.equal( '<paragraph>foobar []</paragraph>' );
  257. expect( getViewData( view ) ).to.equal( '<p>foobar {}</p>' );
  258. } );
  259. it( 'should replace first &nbsp; with space', () => {
  260. model.enqueueChanges( () => {
  261. model.selection.setRanges( [
  262. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 0 ), 0 )
  263. ] );
  264. } );
  265. view.fire( 'mutations', [
  266. {
  267. type: 'text',
  268. oldText: 'foobar',
  269. newText: '\u00A0foobar',
  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 all &nbsp; with spaces', () => {
  277. model.enqueueChanges( () => {
  278. model.selection.setRanges( [
  279. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  280. ] );
  281. } );
  282. view.fire( 'mutations', [
  283. {
  284. type: 'text',
  285. oldText: 'foobar',
  286. newText: 'foobar\u00A0\u00A0\u00A0baz',
  287. node: viewRoot.getChild( 0 ).getChild( 0 )
  288. }
  289. ] );
  290. expect( getModelData( model ) ).to.equal( '<paragraph>foobar baz[]</paragraph>' );
  291. expect( getViewData( view ) ).to.equal( '<p>foobar baz{}</p>' );
  292. } );
  293. } );
  294. describe( 'keystroke handling', () => {
  295. it( 'should remove contents', () => {
  296. model.enqueueChanges( () => {
  297. model.selection.setRanges( [
  298. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  299. } );
  300. listenter.listenTo( view, 'keydown', () => {
  301. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  302. view.fire( 'mutations', [
  303. {
  304. type: 'text',
  305. oldText: 'foar',
  306. newText: 'foyar',
  307. node: viewRoot.getChild( 0 ).getChild( 0 )
  308. }
  309. ] );
  310. }, { priority: 'lowest' } );
  311. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  312. expect( getModelData( model ) ).to.equal( '<paragraph>foy[]ar</paragraph>' );
  313. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  314. } );
  315. it( 'should do nothing on arrow key', () => {
  316. model.enqueueChanges( () => {
  317. model.selection.setRanges( [
  318. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  319. } );
  320. view.fire( 'keydown', { keyCode: getCode( 'arrowright' ) } );
  321. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  322. } );
  323. it( 'should do nothing on ctrl combinations', () => {
  324. model.enqueueChanges( () => {
  325. model.selection.setRanges( [
  326. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  327. } );
  328. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  329. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  330. } );
  331. it( 'should do nothing on non printable keys', () => {
  332. model.enqueueChanges( () => {
  333. model.selection.setRanges( [
  334. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  335. } );
  336. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  337. view.fire( 'keydown', { keyCode: 35 } ); // Home
  338. view.fire( 'keydown', { keyCode: 112 } ); // F1
  339. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  340. } );
  341. // #69
  342. it( 'should do nothing on tab key', () => {
  343. model.enqueueChanges( () => {
  344. model.selection.setRanges( [
  345. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  346. } );
  347. view.fire( 'keydown', { keyCode: 9 } ); // Tab
  348. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  349. } );
  350. it( 'should do nothing if selection is collapsed', () => {
  351. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  352. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  353. } );
  354. it( 'should lock buffer if selection is not collapsed', () => {
  355. const buffer = editor.plugins.get( Input )._buffer;
  356. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  357. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  358. model.enqueueChanges( () => {
  359. model.selection.setRanges( [
  360. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  361. } );
  362. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  363. expect( lockSpy.calledOnce ).to.be.true;
  364. expect( unlockSpy.calledOnce ).to.be.true;
  365. } );
  366. it( 'should not lock buffer on non printable keys', () => {
  367. const buffer = editor.plugins.get( Input )._buffer;
  368. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  369. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  370. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  371. view.fire( 'keydown', { keyCode: 35 } ); // Home
  372. view.fire( 'keydown', { keyCode: 112 } ); // F1
  373. expect( lockSpy.callCount ).to.be.equal( 0 );
  374. expect( unlockSpy.callCount ).to.be.equal( 0 );
  375. } );
  376. it( 'should not lock buffer on collapsed selection', () => {
  377. const buffer = editor.plugins.get( Input )._buffer;
  378. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  379. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  380. view.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  381. view.fire( 'keydown', { keyCode: getCode( 'a' ) } );
  382. view.fire( 'keydown', { keyCode: getCode( 'z' ) } );
  383. expect( lockSpy.callCount ).to.be.equal( 0 );
  384. expect( unlockSpy.callCount ).to.be.equal( 0 );
  385. } );
  386. } );
  387. } );