input.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. describe( 'mutations handling', () => {
  54. it( 'should handle text mutation', () => {
  55. view.fire( 'mutations', [
  56. {
  57. type: 'text',
  58. oldText: 'foobar',
  59. newText: 'fooxbar',
  60. node: viewRoot.getChild( 0 ).getChild( 0 )
  61. }
  62. ] );
  63. expect( getModelData( model ) ).to.equal( '<paragraph>foox[]bar</paragraph>' );
  64. expect( getViewData( view ) ).to.equal( '<p>foox{}bar</p>' );
  65. } );
  66. it( 'should handle text mutation change', () => {
  67. view.fire( 'mutations', [
  68. {
  69. type: 'text',
  70. oldText: 'foobar',
  71. newText: 'foodar',
  72. node: viewRoot.getChild( 0 ).getChild( 0 )
  73. }
  74. ] );
  75. expect( getModelData( model ) ).to.equal( '<paragraph>food[]ar</paragraph>' );
  76. expect( getViewData( view ) ).to.equal( '<p>food{}ar</p>' );
  77. } );
  78. it( 'should handle text node insertion', () => {
  79. editor.setData( '<p></p>' );
  80. view.fire( 'mutations', [
  81. {
  82. type: 'children',
  83. oldChildren: [],
  84. newChildren: [ new ViewText( 'x' ) ],
  85. node: viewRoot.getChild( 0 )
  86. }
  87. ] );
  88. expect( getModelData( model ) ).to.equal( '<paragraph>x[]</paragraph>' );
  89. expect( getViewData( view ) ).to.equal( '<p>x{}</p>' );
  90. } );
  91. it( 'should do nothing when two nodes were inserted', () => {
  92. editor.setData( '<p></p>' );
  93. view.fire( 'mutations', [
  94. {
  95. type: 'children',
  96. oldChildren: [],
  97. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  98. node: viewRoot.getChild( 0 )
  99. }
  100. ] );
  101. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  102. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  103. } );
  104. it( 'should do nothing when two nodes were inserted and one removed', () => {
  105. view.fire( 'mutations', [
  106. {
  107. type: 'children',
  108. oldChildren: [ new ViewText( 'foobar' ) ],
  109. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  110. node: viewRoot.getChild( 0 )
  111. }
  112. ] );
  113. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  114. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  115. } );
  116. it( 'should handle multiple children in the node', () => {
  117. editor.setData( '<p>foo<img></img></p>' );
  118. view.fire( 'mutations', [
  119. {
  120. type: 'children',
  121. oldChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ) ],
  122. newChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ), new ViewText( 'x' ) ],
  123. node: viewRoot.getChild( 0 )
  124. }
  125. ] );
  126. expect( getModelData( model ) ).to.equal( '<paragraph>foo<image></image>x[]</paragraph>' );
  127. expect( getViewData( view ) ).to.equal( '<p>foo<img></img>x{}</p>' );
  128. } );
  129. it( 'should do nothing when node was removed', () => {
  130. view.fire( 'mutations', [
  131. {
  132. type: 'children',
  133. oldChildren: [ new ViewText( 'foobar' ) ],
  134. newChildren: [],
  135. node: viewRoot.getChild( 0 )
  136. }
  137. ] );
  138. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  139. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  140. } );
  141. it( 'should do nothing when element was inserted', () => {
  142. editor.setData( '<p></p>' );
  143. view.fire( 'mutations', [
  144. {
  145. type: 'children',
  146. oldChildren: [],
  147. newChildren: [ new ViewElement( 'img' ) ],
  148. node: viewRoot.getChild( 0 )
  149. }
  150. ] );
  151. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  152. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  153. } );
  154. it( 'should set model selection appropriately to view selection passed in mutations event', () => {
  155. // This test case emulates spellchecker correction.
  156. const viewSelection = new ViewSelection();
  157. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  158. view.fire( 'mutations',
  159. [ {
  160. type: 'text',
  161. oldText: 'foobar',
  162. newText: 'foodar',
  163. node: viewRoot.getChild( 0 ).getChild( 0 )
  164. } ],
  165. viewSelection
  166. );
  167. expect( getModelData( model ) ).to.equal( '<paragraph>foodar[]</paragraph>' );
  168. expect( getViewData( view ) ).to.equal( '<p>foodar{}</p>' );
  169. } );
  170. it( 'should use up to one insert and remove operations', () => {
  171. // This test case emulates spellchecker correction.
  172. const viewSelection = new ViewSelection();
  173. viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  174. sinon.spy( Batch.prototype, 'weakInsert' );
  175. sinon.spy( Batch.prototype, 'remove' );
  176. view.fire( 'mutations',
  177. [ {
  178. type: 'text',
  179. oldText: 'foobar',
  180. newText: 'fxobxr',
  181. node: viewRoot.getChild( 0 ).getChild( 0 )
  182. } ],
  183. viewSelection
  184. );
  185. expect( Batch.prototype.weakInsert.calledOnce ).to.be.true;
  186. expect( Batch.prototype.remove.calledOnce ).to.be.true;
  187. } );
  188. } );
  189. describe( 'keystroke handling', () => {
  190. it( 'should remove contents', () => {
  191. model.enqueueChanges( () => {
  192. model.selection.setRanges( [
  193. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  194. } );
  195. listenter.listenTo( view, 'keydown', () => {
  196. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  197. view.fire( 'mutations', [
  198. {
  199. type: 'text',
  200. oldText: 'foar',
  201. newText: 'foyar',
  202. node: viewRoot.getChild( 0 ).getChild( 0 )
  203. }
  204. ] );
  205. }, { priority: 'lowest' } );
  206. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  207. expect( getModelData( model ) ).to.equal( '<paragraph>foy[]ar</paragraph>' );
  208. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  209. } );
  210. it( 'should do nothing on arrow key', () => {
  211. model.enqueueChanges( () => {
  212. model.selection.setRanges( [
  213. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  214. } );
  215. view.fire( 'keydown', { keyCode: getCode( 'arrowright' ) } );
  216. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  217. } );
  218. it( 'should do nothing on ctrl combinations', () => {
  219. model.enqueueChanges( () => {
  220. model.selection.setRanges( [
  221. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  222. } );
  223. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  224. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  225. } );
  226. it( 'should do nothing on non printable keys', () => {
  227. model.enqueueChanges( () => {
  228. model.selection.setRanges( [
  229. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  230. } );
  231. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  232. view.fire( 'keydown', { keyCode: 35 } ); // Home
  233. view.fire( 'keydown', { keyCode: 112 } ); // F1
  234. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  235. } );
  236. // #69
  237. it( 'should do nothing on tab key', () => {
  238. model.enqueueChanges( () => {
  239. model.selection.setRanges( [
  240. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  241. } );
  242. view.fire( 'keydown', { keyCode: 9 } ); // Tab
  243. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  244. } );
  245. it( 'should do nothing if selection is collapsed', () => {
  246. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  247. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  248. } );
  249. } );
  250. } );