8
0

typing.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import VirtualTestEditor from '/tests/ckeditor5/_utils/virtualtesteditor.js';
  7. import Typing from '/ckeditor5/typing/typing.js';
  8. import Paragraph from '/ckeditor5/paragraph/paragraph.js';
  9. import ModelRange from '/ckeditor5/engine/model/range.js';
  10. import ViewText from '/ckeditor5/engine/view/text.js';
  11. import ViewElement from '/ckeditor5/engine/view/element.js';
  12. import EmitterMixin from '/ckeditor5/utils/emittermixin.js';
  13. import { getCode } from '/ckeditor5/utils/keyboard.js';
  14. import { getData as getModelData } from '/tests/engine/_utils/model.js';
  15. import { getData as getViewData } from '/tests/engine/_utils/view.js';
  16. describe( 'Typing feature', () => {
  17. let editor, model, modelRoot, view, viewRoot, listenter;
  18. before( () => {
  19. listenter = Object.create( EmitterMixin );
  20. return VirtualTestEditor.create( {
  21. features: [ Typing, Paragraph ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. model = editor.editing.model;
  26. modelRoot = model.getRoot();
  27. view = editor.editing.view;
  28. viewRoot = view.getRoot();
  29. } );
  30. } );
  31. beforeEach( () => {
  32. editor.setData( '<p>foobar</p>' );
  33. model.enqueueChanges( () => {
  34. model.selection.setRanges( [
  35. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 0 ), 3 ) ] );
  36. } );
  37. } );
  38. afterEach( () => {
  39. listenter.stopListening();
  40. } );
  41. it( 'has a buffer configured to default value of config.typing.undoStep', () => {
  42. expect( editor.plugins.get( Typing )._buffer ).to.have.property( 'limit', 20 );
  43. } );
  44. it( 'has a buffer configured to config.typing.undoStep', () => {
  45. return VirtualTestEditor.create( {
  46. features: [ Typing ],
  47. typing: {
  48. undoStep: 5
  49. }
  50. } )
  51. .then( editor => {
  52. expect( editor.plugins.get( Typing )._buffer ).to.have.property( 'limit', 5 );
  53. } );
  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<selection />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<selection />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<selection /></paragraph>' );
  91. expect( getViewData( view ) ).to.equal( '<p>x{}</p>' );
  92. } );
  93. it( 'should do nothing when two nodes where 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 node was removed', () => {
  107. view.fire( 'mutations', [
  108. {
  109. type: 'children',
  110. oldChildren: [ viewRoot.getChild( 0 ).getChild( 0 ) ],
  111. newChildren: [],
  112. node: viewRoot.getChild( 0 )
  113. }
  114. ] );
  115. expect( getModelData( model ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
  116. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  117. } );
  118. it( 'should do nothing when element was inserted', () => {
  119. editor.setData( '<p></p>' );
  120. view.fire( 'mutations', [
  121. {
  122. type: 'children',
  123. oldChildren: [],
  124. newChildren: [ new ViewElement( 'img' ) ],
  125. node: viewRoot.getChild( 0 )
  126. }
  127. ] );
  128. expect( getModelData( model ) ).to.equal( '<paragraph></paragraph>' );
  129. expect( getViewData( view ) ).to.equal( '<p></p>' );
  130. } );
  131. } );
  132. describe( 'keystroke handling', () => {
  133. it( 'should remove contents', () => {
  134. model.enqueueChanges( () => {
  135. model.selection.setRanges( [
  136. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  137. } );
  138. listenter.listenTo( view, 'keydown', () => {
  139. expect( getModelData( model ) ).to.equal( '<paragraph>fo<selection />ar</paragraph>' );
  140. view.fire( 'mutations', [
  141. {
  142. type: 'text',
  143. oldText: 'foar',
  144. newText: 'foyar',
  145. node: viewRoot.getChild( 0 ).getChild( 0 )
  146. }
  147. ] );
  148. }, null, 1000000 );
  149. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  150. expect( getModelData( model ) ).to.equal( '<paragraph>foy<selection />ar</paragraph>' );
  151. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  152. } );
  153. it( 'should do nothing on arrow key', () => {
  154. model.enqueueChanges( () => {
  155. model.selection.setRanges( [
  156. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  157. } );
  158. view.fire( 'keydown', { keyCode: getCode( 'arrowright' ) } );
  159. expect( getModelData( model ) ).to.equal( '<paragraph>fo<selection>ob</selection>ar</paragraph>' );
  160. } );
  161. it( 'should do nothing on ctrl combinations', () => {
  162. model.enqueueChanges( () => {
  163. model.selection.setRanges( [
  164. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  165. } );
  166. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  167. expect( getModelData( model ) ).to.equal( '<paragraph>fo<selection>ob</selection>ar</paragraph>' );
  168. } );
  169. it( 'should do nothing on non printable keys', () => {
  170. model.enqueueChanges( () => {
  171. model.selection.setRanges( [
  172. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  173. } );
  174. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  175. view.fire( 'keydown', { keyCode: 35 } ); // Home
  176. view.fire( 'keydown', { keyCode: 112 } ); // F1
  177. expect( getModelData( model ) ).to.equal( '<paragraph>fo<selection>ob</selection>ar</paragraph>' );
  178. } );
  179. it( 'should do nothing if selection is collapsed', () => {
  180. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  181. expect( getModelData( model ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
  182. } );
  183. } );
  184. describe( 'destroy', () => {
  185. it( 'should destroy change buffer', () => {
  186. const typing = new Typing( new VirtualTestEditor() );
  187. typing.init();
  188. const destroy = typing._buffer.destroy = sinon.spy();
  189. typing.destroy();
  190. expect( destroy.calledOnce ).to.be.true;
  191. expect( typing._buffer ).to.be.null;
  192. } );
  193. } );
  194. } );