input.js 19 KB

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