input.js 19 KB

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