input.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 Input from '../src/input';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  9. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  10. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  11. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  12. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  13. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  14. import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
  15. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  16. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  17. import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  18. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  19. describe( 'Input feature', () => {
  20. let editor, model, modelRoot, view, viewRoot, listenter;
  21. before( () => {
  22. listenter = Object.create( EmitterMixin );
  23. return VirtualTestEditor.create( {
  24. plugins: [ Input, Paragraph ]
  25. } )
  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. beforeEach( () => {
  43. editor.setData( '<p>foobar</p>' );
  44. model.enqueueChanges( () => {
  45. model.selection.setRanges( [
  46. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 0 ), 3 )
  47. ] );
  48. } );
  49. } );
  50. afterEach( () => {
  51. listenter.stopListening();
  52. } );
  53. it( 'has a buffer configured to default value of config.typing.undoStep', () => {
  54. expect( editor.plugins.get( Input )._buffer ).to.have.property( 'limit', 20 );
  55. } );
  56. it( 'has a buffer configured to config.typing.undoStep', () => {
  57. return VirtualTestEditor.create( {
  58. plugins: [ Input ],
  59. typing: {
  60. undoStep: 5
  61. }
  62. } )
  63. .then( editor => {
  64. expect( editor.plugins.get( Input )._buffer ).to.have.property( 'limit', 5 );
  65. } );
  66. } );
  67. describe( 'mutations handling', () => {
  68. it( 'should handle text mutation', () => {
  69. view.fire( 'mutations', [
  70. {
  71. type: 'text',
  72. oldText: 'foobar',
  73. newText: 'fooxbar',
  74. node: viewRoot.getChild( 0 ).getChild( 0 )
  75. }
  76. ] );
  77. expect( getModelData( model ) ).to.equal( '<paragraph>foox[]bar</paragraph>' );
  78. expect( getViewData( view ) ).to.equal( '<p>foox{}bar</p>' );
  79. } );
  80. it( 'should handle text mutation change', () => {
  81. view.fire( 'mutations', [
  82. {
  83. type: 'text',
  84. oldText: 'foobar',
  85. newText: 'foodar',
  86. node: viewRoot.getChild( 0 ).getChild( 0 )
  87. }
  88. ] );
  89. expect( getModelData( model ) ).to.equal( '<paragraph>food[]ar</paragraph>' );
  90. expect( getViewData( view ) ).to.equal( '<p>food{}ar</p>' );
  91. } );
  92. it( 'should handle text node insertion', () => {
  93. editor.setData( '<p></p>' );
  94. view.fire( 'mutations', [
  95. {
  96. type: 'children',
  97. oldChildren: [],
  98. newChildren: [ new ViewText( 'x' ) ],
  99. node: viewRoot.getChild( 0 )
  100. }
  101. ] );
  102. expect( getModelData( model ) ).to.equal( '<paragraph>x[]</paragraph>' );
  103. expect( getViewData( view ) ).to.equal( '<p>x{}</p>' );
  104. } );
  105. it( 'should do nothing when two nodes were inserted', () => {
  106. editor.setData( '<p></p>' );
  107. view.fire( 'mutations', [
  108. {
  109. type: 'children',
  110. oldChildren: [],
  111. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  112. node: viewRoot.getChild( 0 )
  113. }
  114. ] );
  115. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  116. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  117. } );
  118. it( 'should do nothing when two nodes were inserted and one removed', () => {
  119. view.fire( 'mutations', [
  120. {
  121. type: 'children',
  122. oldChildren: [ new ViewText( 'foobar' ) ],
  123. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  124. node: viewRoot.getChild( 0 )
  125. }
  126. ] );
  127. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  128. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  129. } );
  130. it( 'should handle multiple children in the node', () => {
  131. editor.setData( '<p>foo<img></img></p>' );
  132. view.fire( 'mutations', [
  133. {
  134. type: 'children',
  135. oldChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ) ],
  136. newChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ), new ViewText( 'x' ) ],
  137. node: viewRoot.getChild( 0 )
  138. }
  139. ] );
  140. expect( getModelData( model ) ).to.equal( '<paragraph>foo<image></image>x[]</paragraph>' );
  141. expect( getViewData( view ) ).to.equal( '<p>foo<img></img>x{}</p>' );
  142. } );
  143. it( 'should do nothing when node was removed', () => {
  144. view.fire( 'mutations', [
  145. {
  146. type: 'children',
  147. oldChildren: [ new ViewText( 'foobar' ) ],
  148. newChildren: [],
  149. node: viewRoot.getChild( 0 )
  150. }
  151. ] );
  152. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  153. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  154. } );
  155. it( 'should do nothing when element was inserted', () => {
  156. editor.setData( '<p></p>' );
  157. view.fire( 'mutations', [
  158. {
  159. type: 'children',
  160. oldChildren: [],
  161. newChildren: [ new ViewElement( 'img' ) ],
  162. node: viewRoot.getChild( 0 )
  163. }
  164. ] );
  165. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  166. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  167. } );
  168. it( 'should set model selection appropriately to view selection passed in mutations event', () => {
  169. // This test case emulates spellchecker correction.
  170. const viewSelection = new ViewSelection();
  171. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  172. view.fire( 'mutations',
  173. [ {
  174. type: 'text',
  175. oldText: 'foobar',
  176. newText: 'foodar',
  177. node: viewRoot.getChild( 0 ).getChild( 0 )
  178. } ],
  179. viewSelection
  180. );
  181. expect( getModelData( model ) ).to.equal( '<paragraph>foodar[]</paragraph>' );
  182. expect( getViewData( view ) ).to.equal( '<p>foodar{}</p>' );
  183. } );
  184. it( 'should use up to one insert and remove operations', () => {
  185. // This test case emulates spellchecker correction.
  186. const viewSelection = new ViewSelection();
  187. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  188. sinon.spy( Batch.prototype, 'weakInsert' );
  189. sinon.spy( Batch.prototype, 'remove' );
  190. view.fire( 'mutations',
  191. [ {
  192. type: 'text',
  193. oldText: 'foobar',
  194. newText: 'fxobxr',
  195. node: viewRoot.getChild( 0 ).getChild( 0 )
  196. } ],
  197. viewSelection
  198. );
  199. expect( Batch.prototype.weakInsert.calledOnce ).to.be.true;
  200. expect( Batch.prototype.remove.calledOnce ).to.be.true;
  201. } );
  202. it( 'should replace last &nbsp; with space', () => {
  203. model.enqueueChanges( () => {
  204. model.selection.setRanges( [
  205. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  206. ] );
  207. } );
  208. view.fire( 'mutations', [
  209. {
  210. type: 'text',
  211. oldText: 'foobar',
  212. newText: 'foobar\u00A0',
  213. node: viewRoot.getChild( 0 ).getChild( 0 )
  214. }
  215. ] );
  216. expect( getModelData( model ) ).to.equal( '<paragraph>foobar []</paragraph>' );
  217. expect( getViewData( view ) ).to.equal( '<p>foobar {}</p>' );
  218. } );
  219. it( 'should replace first &nbsp; with space', () => {
  220. model.enqueueChanges( () => {
  221. model.selection.setRanges( [
  222. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  223. ] );
  224. } );
  225. view.fire( 'mutations', [
  226. {
  227. type: 'text',
  228. oldText: 'foobar',
  229. newText: '\u00A0foobar',
  230. node: viewRoot.getChild( 0 ).getChild( 0 )
  231. }
  232. ] );
  233. expect( getModelData( model ) ).to.equal( '<paragraph> foobar[]</paragraph>' );
  234. expect( getViewData( view ) ).to.equal( '<p> foobar{}</p>' );
  235. } );
  236. it( 'should replace all &nbsp; with spaces', () => {
  237. view.fire( 'mutations', [
  238. {
  239. type: 'text',
  240. oldText: 'foobar',
  241. newText: 'foobar\u00A0\u00A0\u00A0baz',
  242. node: viewRoot.getChild( 0 ).getChild( 0 )
  243. }
  244. ] );
  245. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar baz</paragraph>' );
  246. expect( getViewData( view ) ).to.equal( '<p>foo{}bar' +
  247. ' baz</p>' );
  248. } );
  249. } );
  250. describe( 'keystroke handling', () => {
  251. it( 'should remove contents', () => {
  252. model.enqueueChanges( () => {
  253. model.selection.setRanges( [
  254. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  255. } );
  256. listenter.listenTo( view, 'keydown', () => {
  257. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  258. view.fire( 'mutations', [
  259. {
  260. type: 'text',
  261. oldText: 'foar',
  262. newText: 'foyar',
  263. node: viewRoot.getChild( 0 ).getChild( 0 )
  264. }
  265. ] );
  266. }, { priority: 'lowest' } );
  267. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  268. expect( getModelData( model ) ).to.equal( '<paragraph>foy[]ar</paragraph>' );
  269. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  270. } );
  271. it( 'should do nothing on arrow key', () => {
  272. model.enqueueChanges( () => {
  273. model.selection.setRanges( [
  274. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  275. } );
  276. view.fire( 'keydown', { keyCode: getCode( 'arrowright' ) } );
  277. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  278. } );
  279. it( 'should do nothing on ctrl combinations', () => {
  280. model.enqueueChanges( () => {
  281. model.selection.setRanges( [
  282. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  283. } );
  284. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  285. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  286. } );
  287. it( 'should do nothing on non printable keys', () => {
  288. model.enqueueChanges( () => {
  289. model.selection.setRanges( [
  290. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  291. } );
  292. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  293. view.fire( 'keydown', { keyCode: 35 } ); // Home
  294. view.fire( 'keydown', { keyCode: 112 } ); // F1
  295. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  296. } );
  297. // #69
  298. it( 'should do nothing on tab key', () => {
  299. model.enqueueChanges( () => {
  300. model.selection.setRanges( [
  301. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  302. } );
  303. view.fire( 'keydown', { keyCode: 9 } ); // Tab
  304. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  305. } );
  306. it( 'should do nothing if selection is collapsed', () => {
  307. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  308. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  309. } );
  310. } );
  311. describe( 'destroy', () => {
  312. it( 'should destroy change buffer', () => {
  313. const typing = new Input( new VirtualTestEditor() );
  314. typing.init();
  315. const destroy = typing._buffer.destroy = sinon.spy();
  316. typing.destroy();
  317. expect( destroy.calledOnce ).to.be.true;
  318. expect( typing._buffer ).to.be.null;
  319. } );
  320. } );
  321. } );