typing.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 BuildModelConverterFor from '/ckeditor5/engine/conversion/model-converter-builder.js';
  11. import BuildViewConverterFor from '/ckeditor5/engine/conversion/view-converter-builder.js';
  12. import ViewText from '/ckeditor5/engine/view/text.js';
  13. import ViewElement from '/ckeditor5/engine/view/element.js';
  14. import EmitterMixin from '/ckeditor5/utils/emittermixin.js';
  15. import { getCode } from '/ckeditor5/utils/keyboard.js';
  16. import { getData as getModelData } from '/tests/engine/_utils/model.js';
  17. import { getData as getViewData } from '/tests/engine/_utils/view.js';
  18. describe( 'Typing feature', () => {
  19. let editor, model, modelRoot, view, viewRoot, listenter;
  20. before( () => {
  21. listenter = Object.create( EmitterMixin );
  22. return VirtualTestEditor.create( {
  23. features: [ Typing, Paragraph ]
  24. } )
  25. .then( newEditor => {
  26. // Mock image feature.
  27. newEditor.document.schema.registerItem( 'image', '$inline' );
  28. BuildModelConverterFor( newEditor.data.modelToView, newEditor.editing.modelToView )
  29. .fromElement( 'image' )
  30. .toElement( 'img' );
  31. BuildViewConverterFor( newEditor.data.viewToModel )
  32. .fromElement( 'img' )
  33. .toElement( 'image' );
  34. editor = newEditor;
  35. model = editor.editing.model;
  36. modelRoot = model.getRoot();
  37. view = editor.editing.view;
  38. viewRoot = view.getRoot();
  39. } );
  40. } );
  41. beforeEach( () => {
  42. editor.setData( '<p>foobar</p>' );
  43. model.enqueueChanges( () => {
  44. model.selection.setRanges( [
  45. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 0 ), 3 ) ] );
  46. } );
  47. } );
  48. afterEach( () => {
  49. listenter.stopListening();
  50. } );
  51. it( 'has a buffer configured to default value of config.typing.undoStep', () => {
  52. expect( editor.plugins.get( Typing )._buffer ).to.have.property( 'limit', 20 );
  53. } );
  54. it( 'has a buffer configured to config.typing.undoStep', () => {
  55. return VirtualTestEditor.create( {
  56. features: [ Typing ],
  57. typing: {
  58. undoStep: 5
  59. }
  60. } )
  61. .then( editor => {
  62. expect( editor.plugins.get( Typing )._buffer ).to.have.property( 'limit', 5 );
  63. } );
  64. } );
  65. describe( 'mutations handling', () => {
  66. it( 'should handle text mutation', () => {
  67. view.fire( 'mutations', [
  68. {
  69. type: 'text',
  70. oldText: 'foobar',
  71. newText: 'fooxbar',
  72. node: viewRoot.getChild( 0 ).getChild( 0 )
  73. }
  74. ] );
  75. expect( getModelData( model ) ).to.equal( '<paragraph>foox<selection />bar</paragraph>' );
  76. expect( getViewData( view ) ).to.equal( '<p>foox{}bar</p>' );
  77. } );
  78. it( 'should handle text mutation change', () => {
  79. view.fire( 'mutations', [
  80. {
  81. type: 'text',
  82. oldText: 'foobar',
  83. newText: 'foodar',
  84. node: viewRoot.getChild( 0 ).getChild( 0 )
  85. }
  86. ] );
  87. expect( getModelData( model ) ).to.equal( '<paragraph>food<selection />ar</paragraph>' );
  88. expect( getViewData( view ) ).to.equal( '<p>food{}ar</p>' );
  89. } );
  90. it( 'should handle text node insertion', () => {
  91. editor.setData( '<p></p>' );
  92. view.fire( 'mutations', [
  93. {
  94. type: 'children',
  95. oldChildren: [],
  96. newChildren: [ new ViewText( 'x' ) ],
  97. node: viewRoot.getChild( 0 )
  98. }
  99. ] );
  100. expect( getModelData( model ) ).to.equal( '<paragraph>x<selection /></paragraph>' );
  101. expect( getViewData( view ) ).to.equal( '<p>x{}</p>' );
  102. } );
  103. it( 'should do nothing when two nodes were inserted', () => {
  104. editor.setData( '<p></p>' );
  105. view.fire( 'mutations', [
  106. {
  107. type: 'children',
  108. oldChildren: [],
  109. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  110. node: viewRoot.getChild( 0 )
  111. }
  112. ] );
  113. expect( getModelData( model ) ).to.equal( '<paragraph></paragraph>' );
  114. expect( getViewData( view ) ).to.equal( '<p></p>' );
  115. } );
  116. it( 'should do nothing when two nodes were inserted and one removed', () => {
  117. view.fire( 'mutations', [
  118. {
  119. type: 'children',
  120. oldChildren: [ new ViewText( 'foobar' ) ],
  121. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  122. node: viewRoot.getChild( 0 )
  123. }
  124. ] );
  125. expect( getModelData( model ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
  126. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  127. } );
  128. it( 'should handle multiple children in the node', () => {
  129. editor.setData( '<p>foo<img></img></p>' );
  130. view.fire( 'mutations', [
  131. {
  132. type: 'children',
  133. oldChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ) ],
  134. newChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ), new ViewText( 'x' ) ],
  135. node: viewRoot.getChild( 0 )
  136. }
  137. ] );
  138. expect( getModelData( model ) ).to.equal( '<paragraph>foo<image></image>x<selection /></paragraph>' );
  139. expect( getViewData( view ) ).to.equal( '<p>foo<img></img>x{}</p>' );
  140. } );
  141. it( 'should do nothing when node was removed', () => {
  142. view.fire( 'mutations', [
  143. {
  144. type: 'children',
  145. oldChildren: [ new ViewText( 'foobar' ) ],
  146. newChildren: [],
  147. node: viewRoot.getChild( 0 )
  148. }
  149. ] );
  150. expect( getModelData( model ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
  151. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  152. } );
  153. it( 'should do nothing when element was inserted', () => {
  154. editor.setData( '<p></p>' );
  155. view.fire( 'mutations', [
  156. {
  157. type: 'children',
  158. oldChildren: [],
  159. newChildren: [ new ViewElement( 'img' ) ],
  160. node: viewRoot.getChild( 0 )
  161. }
  162. ] );
  163. expect( getModelData( model ) ).to.equal( '<paragraph></paragraph>' );
  164. expect( getViewData( view ) ).to.equal( '<p></p>' );
  165. } );
  166. } );
  167. describe( 'keystroke handling', () => {
  168. it( 'should remove contents', () => {
  169. model.enqueueChanges( () => {
  170. model.selection.setRanges( [
  171. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  172. } );
  173. listenter.listenTo( view, 'keydown', () => {
  174. expect( getModelData( model ) ).to.equal( '<paragraph>fo<selection />ar</paragraph>' );
  175. view.fire( 'mutations', [
  176. {
  177. type: 'text',
  178. oldText: 'foar',
  179. newText: 'foyar',
  180. node: viewRoot.getChild( 0 ).getChild( 0 )
  181. }
  182. ] );
  183. }, null, 1000000 );
  184. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  185. expect( getModelData( model ) ).to.equal( '<paragraph>foy<selection />ar</paragraph>' );
  186. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  187. } );
  188. it( 'should do nothing on arrow key', () => {
  189. model.enqueueChanges( () => {
  190. model.selection.setRanges( [
  191. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  192. } );
  193. view.fire( 'keydown', { keyCode: getCode( 'arrowright' ) } );
  194. expect( getModelData( model ) ).to.equal( '<paragraph>fo<selection>ob</selection>ar</paragraph>' );
  195. } );
  196. it( 'should do nothing on ctrl combinations', () => {
  197. model.enqueueChanges( () => {
  198. model.selection.setRanges( [
  199. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  200. } );
  201. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  202. expect( getModelData( model ) ).to.equal( '<paragraph>fo<selection>ob</selection>ar</paragraph>' );
  203. } );
  204. it( 'should do nothing on non printable keys', () => {
  205. model.enqueueChanges( () => {
  206. model.selection.setRanges( [
  207. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  208. } );
  209. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  210. view.fire( 'keydown', { keyCode: 35 } ); // Home
  211. view.fire( 'keydown', { keyCode: 112 } ); // F1
  212. expect( getModelData( model ) ).to.equal( '<paragraph>fo<selection>ob</selection>ar</paragraph>' );
  213. } );
  214. it( 'should do nothing if selection is collapsed', () => {
  215. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  216. expect( getModelData( model ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
  217. } );
  218. } );
  219. describe( 'destroy', () => {
  220. it( 'should destroy change buffer', () => {
  221. const typing = new Typing( new VirtualTestEditor() );
  222. typing.init();
  223. const destroy = typing._buffer.destroy = sinon.spy();
  224. typing.destroy();
  225. expect( destroy.calledOnce ).to.be.true;
  226. expect( typing._buffer ).to.be.null;
  227. } );
  228. } );
  229. } );