input.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from 'tests/core/_utils/virtualtesteditor.js';
  6. import Input from 'ckeditor5/typing/input.js';
  7. import Paragraph from 'ckeditor5/paragraph/paragraph.js';
  8. import Batch from 'ckeditor5/engine/model/batch.js';
  9. import ModelRange from 'ckeditor5/engine/model/range.js';
  10. import buildModelConverter from 'ckeditor5/engine/conversion/buildmodelconverter.js';
  11. import buildViewConverter from 'ckeditor5/engine/conversion/buildviewconverter.js';
  12. import ViewText from 'ckeditor5/engine/view/text.js';
  13. import ViewElement from 'ckeditor5/engine/view/element.js';
  14. import ViewSelection from 'ckeditor5/engine/view/selection.js';
  15. import EmitterMixin from 'ckeditor5/utils/emittermixin.js';
  16. import { getCode } from 'ckeditor5/utils/keyboard.js';
  17. import { getData as getModelData } from 'ckeditor5/engine/dev-utils/model.js';
  18. import { getData as getViewData } from 'ckeditor5/engine/dev-utils/view.js';
  19. describe( 'Input feature', () => {
  20. let editor, model, modelRoot, view, viewRoot, listenter;
  21. before( () => {
  22. listenter = Object.create( EmitterMixin );
  23. return VirtualTestEditor.create( {
  24. features: [ 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. features: [ 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. } );
  203. describe( 'keystroke handling', () => {
  204. it( 'should remove contents', () => {
  205. model.enqueueChanges( () => {
  206. model.selection.setRanges( [
  207. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  208. } );
  209. listenter.listenTo( view, 'keydown', () => {
  210. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  211. view.fire( 'mutations', [
  212. {
  213. type: 'text',
  214. oldText: 'foar',
  215. newText: 'foyar',
  216. node: viewRoot.getChild( 0 ).getChild( 0 )
  217. }
  218. ] );
  219. }, { priority: 'lowest' } );
  220. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  221. expect( getModelData( model ) ).to.equal( '<paragraph>foy[]ar</paragraph>' );
  222. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  223. } );
  224. it( 'should do nothing on arrow key', () => {
  225. model.enqueueChanges( () => {
  226. model.selection.setRanges( [
  227. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  228. } );
  229. view.fire( 'keydown', { keyCode: getCode( 'arrowright' ) } );
  230. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  231. } );
  232. it( 'should do nothing on ctrl combinations', () => {
  233. model.enqueueChanges( () => {
  234. model.selection.setRanges( [
  235. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  236. } );
  237. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  238. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  239. } );
  240. it( 'should do nothing on non printable keys', () => {
  241. model.enqueueChanges( () => {
  242. model.selection.setRanges( [
  243. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  244. } );
  245. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  246. view.fire( 'keydown', { keyCode: 35 } ); // Home
  247. view.fire( 'keydown', { keyCode: 112 } ); // F1
  248. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  249. } );
  250. it( 'should do nothing if selection is collapsed', () => {
  251. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  252. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  253. } );
  254. } );
  255. describe( 'destroy', () => {
  256. it( 'should destroy change buffer', () => {
  257. const typing = new Input( new VirtualTestEditor() );
  258. typing.init();
  259. const destroy = typing._buffer.destroy = sinon.spy();
  260. typing.destroy();
  261. expect( destroy.calledOnce ).to.be.true;
  262. expect( typing._buffer ).to.be.null;
  263. } );
  264. } );
  265. } );