input.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Bold from '@ckeditor/ckeditor5-basic-styles/src/boldengine';
  9. import Input from '../src/input';
  10. import Writer from '@ckeditor/ckeditor5-engine/src/model/writer';
  11. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  12. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  13. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  14. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  15. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  16. import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
  17. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  18. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  19. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  20. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  21. describe( 'Input feature', () => {
  22. let editor, model, modelRoot, view, viewDocument, viewRoot, listenter;
  23. testUtils.createSinonSandbox();
  24. before( () => {
  25. listenter = Object.create( EmitterMixin );
  26. return VirtualTestEditor
  27. .create( {
  28. plugins: [ Input, Paragraph, Bold ]
  29. } )
  30. .then( newEditor => {
  31. // Mock image feature.
  32. newEditor.model.schema.register( 'image', { allowWhere: '$text' } );
  33. buildModelConverter().for( newEditor.data.modelToView, newEditor.editing.modelToView )
  34. .fromElement( 'image' )
  35. .toElement( 'img' );
  36. buildViewConverter().for( newEditor.data.viewToModel )
  37. .fromElement( 'img' )
  38. .toElement( 'image' );
  39. editor = newEditor;
  40. model = editor.model;
  41. modelRoot = model.document.getRoot();
  42. view = editor.editing.view;
  43. viewDocument = view.document;
  44. viewRoot = viewDocument.getRoot();
  45. } );
  46. } );
  47. after( () => {
  48. return editor.destroy();
  49. } );
  50. beforeEach( () => {
  51. editor.setData( '<p>foobar</p>' );
  52. model.change( writer => {
  53. writer.setSelection(
  54. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 0 ), 3 )
  55. );
  56. } );
  57. } );
  58. afterEach( () => {
  59. listenter.stopListening();
  60. } );
  61. describe( 'mutations handling', () => {
  62. it( 'should handle text mutation', () => {
  63. viewDocument.fire( 'mutations', [
  64. {
  65. type: 'text',
  66. oldText: 'foobar',
  67. newText: 'fooxbar',
  68. node: viewRoot.getChild( 0 ).getChild( 0 )
  69. }
  70. ] );
  71. expect( getModelData( model ) ).to.equal( '<paragraph>foox[]bar</paragraph>' );
  72. expect( getViewData( view ) ).to.equal( '<p>foox{}bar</p>' );
  73. } );
  74. it( 'should handle text mutation change', () => {
  75. viewDocument.fire( 'mutations', [
  76. {
  77. type: 'text',
  78. oldText: 'foobar',
  79. newText: 'foodar',
  80. node: viewRoot.getChild( 0 ).getChild( 0 )
  81. }
  82. ] );
  83. expect( getModelData( model ) ).to.equal( '<paragraph>food[]ar</paragraph>' );
  84. expect( getViewData( view ) ).to.equal( '<p>food{}ar</p>' );
  85. } );
  86. it( 'should handle text node insertion', () => {
  87. editor.setData( '<p></p>' );
  88. viewDocument.fire( 'mutations', [
  89. {
  90. type: 'children',
  91. oldChildren: [],
  92. newChildren: [ new ViewText( 'x' ) ],
  93. node: viewRoot.getChild( 0 )
  94. }
  95. ] );
  96. expect( getModelData( model ) ).to.equal( '<paragraph>x[]</paragraph>' );
  97. expect( getViewData( view ) ).to.equal( '<p>x{}</p>' );
  98. } );
  99. it( 'should apply selection attributes to the inserted text', () => {
  100. setModelData( model, '<paragraph>[]</paragraph>', {
  101. selectionAttributes: {
  102. bold: true,
  103. italic: true
  104. }
  105. } );
  106. viewDocument.fire( 'mutations', [
  107. {
  108. type: 'children',
  109. oldChildren: [],
  110. newChildren: [ new ViewText( 'x' ) ],
  111. node: viewRoot.getChild( 0 )
  112. }
  113. ] );
  114. expect( getModelData( model ) ).to.equal(
  115. '<paragraph><$text bold="true" italic="true">x[]</$text></paragraph>'
  116. );
  117. } );
  118. it( 'should handle multiple text mutations', () => {
  119. editor.setData( '<p>foo<strong>bar</strong></p>' );
  120. viewDocument.fire( 'mutations', [
  121. {
  122. type: 'text',
  123. oldText: 'foo',
  124. newText: 'foob',
  125. node: viewRoot.getChild( 0 ).getChild( 0 )
  126. },
  127. {
  128. type: 'text',
  129. oldText: 'bar',
  130. newText: 'ar',
  131. node: viewRoot.getChild( 0 ).getChild( 1 ).getChild( 0 )
  132. }
  133. ] );
  134. expect( getModelData( model ) ).to.equal( '<paragraph>foob[]<$text bold="true">ar</$text></paragraph>' );
  135. expect( getViewData( view ) ).to.equal( '<p>foob{}<strong>ar</strong></p>' );
  136. } );
  137. it( 'should handle multiple text node insertion', () => {
  138. editor.setData( '<p></p><p></p>' );
  139. viewDocument.fire( 'mutations', [
  140. {
  141. type: 'children',
  142. oldChildren: [],
  143. newChildren: [ new ViewText( 'x' ) ],
  144. node: viewRoot.getChild( 0 )
  145. },
  146. {
  147. type: 'children',
  148. oldChildren: [],
  149. newChildren: [ new ViewText( 'y' ) ],
  150. node: viewRoot.getChild( 1 )
  151. }
  152. ] );
  153. expect( getModelData( model ) ).to.equal( '<paragraph>x</paragraph><paragraph>y[]</paragraph>' );
  154. expect( getViewData( view ) ).to.equal( '<p>x</p><p>y{}</p>' );
  155. } );
  156. it( 'should do nothing when two nodes were inserted', () => {
  157. editor.setData( '<p></p>' );
  158. viewDocument.fire( 'mutations', [
  159. {
  160. type: 'children',
  161. oldChildren: [],
  162. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  163. node: viewRoot.getChild( 0 )
  164. }
  165. ] );
  166. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  167. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  168. } );
  169. it( 'should do nothing when two nodes were inserted and one removed', () => {
  170. viewDocument.fire( 'mutations', [
  171. {
  172. type: 'children',
  173. oldChildren: [ new ViewText( 'foobar' ) ],
  174. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  175. node: viewRoot.getChild( 0 )
  176. }
  177. ] );
  178. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  179. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  180. } );
  181. it( 'should handle multiple children in the node', () => {
  182. editor.setData( '<p>foo<img></img></p>' );
  183. viewDocument.fire( 'mutations', [
  184. {
  185. type: 'children',
  186. oldChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ) ],
  187. newChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ), new ViewText( 'x' ) ],
  188. node: viewRoot.getChild( 0 )
  189. }
  190. ] );
  191. expect( getModelData( model ) ).to.equal( '<paragraph>foo<image></image>x[]</paragraph>' );
  192. expect( getViewData( view ) ).to.equal( '<p>foo<img></img>x{}</p>' );
  193. } );
  194. it( 'should do nothing when node was removed', () => {
  195. viewDocument.fire( 'mutations', [
  196. {
  197. type: 'children',
  198. oldChildren: [ new ViewText( 'foobar' ) ],
  199. newChildren: [],
  200. node: viewRoot.getChild( 0 )
  201. }
  202. ] );
  203. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  204. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  205. } );
  206. it( 'should do nothing when element was inserted', () => {
  207. editor.setData( '<p></p>' );
  208. viewDocument.fire( 'mutations', [
  209. {
  210. type: 'children',
  211. oldChildren: [],
  212. newChildren: [ new ViewElement( 'img' ) ],
  213. node: viewRoot.getChild( 0 )
  214. }
  215. ] );
  216. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  217. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  218. } );
  219. it( 'should set model selection appropriately to view selection passed in mutations event', () => {
  220. // This test case emulates spellchecker correction.
  221. const viewSelection = new ViewSelection();
  222. viewSelection._setTo( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  223. viewDocument.fire( 'mutations',
  224. [ {
  225. type: 'text',
  226. oldText: 'foobar',
  227. newText: 'foodar',
  228. node: viewRoot.getChild( 0 ).getChild( 0 )
  229. } ],
  230. viewSelection
  231. );
  232. expect( getModelData( model ) ).to.equal( '<paragraph>foodar[]</paragraph>' );
  233. expect( getViewData( view ) ).to.equal( '<p>foodar{}</p>' );
  234. } );
  235. it( 'should use up to one insert and remove operations (spellchecker)', () => {
  236. // This test case emulates spellchecker correction.
  237. const viewSelection = new ViewSelection();
  238. viewSelection._setTo( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  239. testUtils.sinon.spy( Writer.prototype, 'insert' );
  240. testUtils.sinon.spy( Writer.prototype, 'remove' );
  241. viewDocument.fire( 'mutations',
  242. [ {
  243. type: 'text',
  244. oldText: 'foobar',
  245. newText: 'fxobxr',
  246. node: viewRoot.getChild( 0 ).getChild( 0 )
  247. } ],
  248. viewSelection
  249. );
  250. expect( Writer.prototype.insert.calledOnce ).to.be.true;
  251. expect( Writer.prototype.remove.calledOnce ).to.be.true;
  252. } );
  253. it( 'should place selection after when correcting to longer word (spellchecker)', () => {
  254. // This test case emulates spellchecker correction.
  255. editor.setData( '<p>Foo hous a</p>' );
  256. const viewSelection = new ViewSelection();
  257. viewSelection._setTo( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  258. viewDocument.fire( 'mutations',
  259. [ {
  260. type: 'text',
  261. oldText: 'Foo hous a',
  262. newText: 'Foo house a',
  263. node: viewRoot.getChild( 0 ).getChild( 0 )
  264. } ],
  265. viewSelection
  266. );
  267. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[] a</paragraph>' );
  268. expect( getViewData( view ) ).to.equal( '<p>Foo house{} a</p>' );
  269. } );
  270. it( 'should place selection after when correcting to shorter word (spellchecker)', () => {
  271. // This test case emulates spellchecker correction.
  272. editor.setData( '<p>Bar athat foo</p>' );
  273. const viewSelection = new ViewSelection();
  274. viewSelection._setTo( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  275. viewDocument.fire( 'mutations',
  276. [ {
  277. type: 'text',
  278. oldText: 'Bar athat foo',
  279. newText: 'Bar that foo',
  280. node: viewRoot.getChild( 0 ).getChild( 0 )
  281. } ],
  282. viewSelection
  283. );
  284. expect( getModelData( model ) ).to.equal( '<paragraph>Bar that[] foo</paragraph>' );
  285. expect( getViewData( view ) ).to.equal( '<p>Bar that{} foo</p>' );
  286. } );
  287. it( 'should place selection after when merging two words (spellchecker)', () => {
  288. // This test case emulates spellchecker correction.
  289. editor.setData( '<p>Foo hous e</p>' );
  290. const viewSelection = new ViewSelection();
  291. viewSelection._setTo( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  292. viewDocument.fire( 'mutations',
  293. [ {
  294. type: 'text',
  295. oldText: 'Foo hous e',
  296. newText: 'Foo house',
  297. node: viewRoot.getChild( 0 ).getChild( 0 )
  298. } ],
  299. viewSelection
  300. );
  301. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[]</paragraph>' );
  302. expect( getViewData( view ) ).to.equal( '<p>Foo house{}</p>' );
  303. } );
  304. it( 'should place non-collapsed selection after changing single character (composition)', () => {
  305. editor.setData( '<p>Foo house</p>' );
  306. const viewSelection = new ViewSelection();
  307. viewSelection._setTo( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  308. viewSelection._setFocus( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  309. viewDocument.fire( 'mutations',
  310. [ {
  311. type: 'text',
  312. oldText: 'Foo house',
  313. newText: 'Foo housa',
  314. node: viewRoot.getChild( 0 ).getChild( 0 )
  315. } ],
  316. viewSelection
  317. );
  318. expect( getModelData( model ) ).to.equal( '<paragraph>Foo hous[a]</paragraph>' );
  319. expect( getViewData( view ) ).to.equal( '<p>Foo hous{a}</p>' );
  320. } );
  321. it( 'should replace last &nbsp; with space', () => {
  322. model.change( writer => {
  323. writer.setSelection(
  324. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  325. );
  326. } );
  327. viewDocument.fire( 'mutations', [
  328. {
  329. type: 'text',
  330. oldText: 'foobar',
  331. newText: 'foobar\u00A0',
  332. node: viewRoot.getChild( 0 ).getChild( 0 )
  333. }
  334. ] );
  335. expect( getModelData( model ) ).to.equal( '<paragraph>foobar []</paragraph>' );
  336. expect( getViewData( view ) ).to.equal( '<p>foobar {}</p>' );
  337. } );
  338. it( 'should replace first &nbsp; with space', () => {
  339. model.change( writer => {
  340. writer.setSelection(
  341. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 0 ), 0 )
  342. );
  343. } );
  344. viewDocument.fire( 'mutations', [
  345. {
  346. type: 'text',
  347. oldText: 'foobar',
  348. newText: '\u00A0foobar',
  349. node: viewRoot.getChild( 0 ).getChild( 0 )
  350. }
  351. ] );
  352. expect( getModelData( model ) ).to.equal( '<paragraph> []foobar</paragraph>' );
  353. expect( getViewData( view ) ).to.equal( '<p> {}foobar</p>' );
  354. } );
  355. it( 'should replace all &nbsp; with spaces', () => {
  356. model.change( writer => {
  357. writer.setSelection(
  358. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  359. );
  360. } );
  361. viewDocument.fire( 'mutations', [
  362. {
  363. type: 'text',
  364. oldText: 'foobar',
  365. newText: 'foobar\u00A0\u00A0\u00A0baz',
  366. node: viewRoot.getChild( 0 ).getChild( 0 )
  367. }
  368. ] );
  369. expect( getModelData( model ) ).to.equal( '<paragraph>foobar baz[]</paragraph>' );
  370. expect( getViewData( view ) ).to.equal( '<p>foobar baz{}</p>' );
  371. } );
  372. } );
  373. describe( 'keystroke handling', () => {
  374. it( 'should remove contents', () => {
  375. model.change( writer => {
  376. writer.setSelection(
  377. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  378. } );
  379. listenter.listenTo( viewDocument, 'keydown', () => {
  380. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  381. }, { priority: 'lowest' } );
  382. } );
  383. // #97
  384. it( 'should remove contents and merge blocks', () => {
  385. setModelData( model, '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  386. listenter.listenTo( viewDocument, 'keydown', () => {
  387. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  388. viewDocument.fire( 'mutations', [
  389. {
  390. type: 'text',
  391. oldText: 'foar',
  392. newText: 'foyar',
  393. node: viewRoot.getChild( 0 ).getChild( 0 )
  394. }
  395. ] );
  396. }, { priority: 'lowest' } );
  397. viewDocument.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  398. expect( getModelData( model ) ).to.equal( '<paragraph>foy[]ar</paragraph>' );
  399. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  400. } );
  401. it( 'should do nothing on arrow key', () => {
  402. model.change( writer => {
  403. writer.setSelection(
  404. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  405. } );
  406. viewDocument.fire( 'keydown', { keyCode: getCode( 'arrowdown' ) } );
  407. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  408. } );
  409. it( 'should do nothing on ctrl combinations', () => {
  410. model.change( writer => {
  411. writer.setSelection(
  412. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  413. } );
  414. viewDocument.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  415. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  416. } );
  417. it( 'should do nothing on non printable keys', () => {
  418. model.change( writer => {
  419. writer.setSelection(
  420. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  421. } );
  422. viewDocument.fire( 'keydown', { keyCode: 16 } ); // Shift
  423. viewDocument.fire( 'keydown', { keyCode: 35 } ); // Home
  424. viewDocument.fire( 'keydown', { keyCode: 112 } ); // F1
  425. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  426. } );
  427. // #69
  428. it( 'should do nothing on tab key', () => {
  429. model.change( writer => {
  430. writer.setSelection(
  431. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  432. } );
  433. viewDocument.fire( 'keydown', { keyCode: 9 } ); // Tab
  434. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  435. } );
  436. // #82
  437. it( 'should do nothing on composition start key', () => {
  438. model.change( writer => {
  439. writer.setSelection(
  440. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  441. } );
  442. viewDocument.fire( 'keydown', { keyCode: 229 } );
  443. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  444. } );
  445. it( 'should do nothing if selection is collapsed', () => {
  446. viewDocument.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  447. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  448. } );
  449. it( 'should lock buffer if selection is not collapsed', () => {
  450. const buffer = editor.commands.get( 'input' )._buffer;
  451. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  452. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  453. model.change( writer => {
  454. writer.setSelection(
  455. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  456. } );
  457. viewDocument.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  458. expect( lockSpy.calledOnce ).to.be.true;
  459. expect( unlockSpy.calledOnce ).to.be.true;
  460. } );
  461. it( 'should not lock buffer on non printable keys', () => {
  462. const buffer = editor.commands.get( 'input' )._buffer;
  463. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  464. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  465. viewDocument.fire( 'keydown', { keyCode: 16 } ); // Shift
  466. viewDocument.fire( 'keydown', { keyCode: 35 } ); // Home
  467. viewDocument.fire( 'keydown', { keyCode: 112 } ); // F1
  468. expect( lockSpy.callCount ).to.be.equal( 0 );
  469. expect( unlockSpy.callCount ).to.be.equal( 0 );
  470. } );
  471. it( 'should not lock buffer on collapsed selection', () => {
  472. const buffer = editor.commands.get( 'input' )._buffer;
  473. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  474. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  475. viewDocument.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  476. viewDocument.fire( 'keydown', { keyCode: getCode( 'a' ) } );
  477. viewDocument.fire( 'keydown', { keyCode: getCode( 'z' ) } );
  478. expect( lockSpy.callCount ).to.be.equal( 0 );
  479. expect( unlockSpy.callCount ).to.be.equal( 0 );
  480. } );
  481. it( 'should not modify document when input command is disabled and selection is collapsed', () => {
  482. setModelData( model, '<paragraph>foo[]bar</paragraph>' );
  483. editor.commands.get( 'input' ).isEnabled = false;
  484. viewDocument.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  485. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  486. } );
  487. it( 'should not modify document when input command is disabled and selection is non-collapsed', () => {
  488. setModelData( model, '<paragraph>fo[ob]ar</paragraph>' );
  489. editor.commands.get( 'input' ).isEnabled = false;
  490. viewDocument.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  491. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  492. } );
  493. } );
  494. } );