input.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  11. import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
  12. import Input from '../src/input';
  13. import Writer from '@ckeditor/ckeditor5-engine/src/model/writer';
  14. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  15. import ModelSelection from '@ckeditor/ckeditor5-engine/src/model/selection';
  16. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  17. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  18. import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
  19. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  20. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  21. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  22. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  23. /* global document */
  24. describe( 'Input feature', () => {
  25. let editor, model, modelRoot, view, viewDocument, viewRoot, listenter;
  26. testUtils.createSinonSandbox();
  27. beforeEach( () => {
  28. listenter = Object.create( EmitterMixin );
  29. const domElement = document.createElement( 'div' );
  30. document.body.appendChild( domElement );
  31. return ClassicTestEditor.create( domElement, { plugins: [ Input, Paragraph, Bold, Italic, List, ShiftEnter ] } )
  32. .then( newEditor => {
  33. // Mock image feature.
  34. newEditor.model.schema.register( 'image', { allowWhere: '$text' } );
  35. newEditor.conversion.elementToElement( {
  36. model: 'image',
  37. view: 'img'
  38. } );
  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. editor.setData( '<p>foobar</p>' );
  46. model.change( writer => {
  47. writer.setSelection(
  48. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 0 ), 3 )
  49. );
  50. } );
  51. } );
  52. } );
  53. afterEach( () => {
  54. listenter.stopListening();
  55. return editor.destroy();
  56. } );
  57. describe( 'mutations handling', () => {
  58. it( 'should handle text mutation', () => {
  59. viewDocument.fire( 'mutations', [
  60. {
  61. type: 'text',
  62. oldText: 'foobar',
  63. newText: 'fooxbar',
  64. node: viewRoot.getChild( 0 ).getChild( 0 )
  65. }
  66. ] );
  67. expect( getModelData( model ) ).to.equal( '<paragraph>foox[]bar</paragraph>' );
  68. expect( getViewData( view ) ).to.equal( '<p>foox{}bar</p>' );
  69. } );
  70. it( 'should handle text mutation change', () => {
  71. viewDocument.fire( 'mutations', [
  72. {
  73. type: 'text',
  74. oldText: 'foobar',
  75. newText: 'foodar',
  76. node: viewRoot.getChild( 0 ).getChild( 0 )
  77. }
  78. ] );
  79. expect( getModelData( model ) ).to.equal( '<paragraph>food[]ar</paragraph>' );
  80. expect( getViewData( view ) ).to.equal( '<p>food{}ar</p>' );
  81. } );
  82. it( 'should handle text node insertion', () => {
  83. editor.setData( '<p></p>' );
  84. viewDocument.fire( 'mutations', [
  85. {
  86. type: 'children',
  87. oldChildren: [],
  88. newChildren: [ new ViewText( 'x' ) ],
  89. node: viewRoot.getChild( 0 )
  90. }
  91. ] );
  92. expect( getModelData( model ) ).to.equal( '<paragraph>x[]</paragraph>' );
  93. expect( getViewData( view ) ).to.equal( '<p>x{}</p>' );
  94. } );
  95. it( 'should apply selection attributes to the inserted text', () => {
  96. setModelData( model, '<paragraph>[]</paragraph>', {
  97. selectionAttributes: {
  98. bold: true,
  99. italic: true
  100. }
  101. } );
  102. viewDocument.fire( 'mutations', [
  103. {
  104. type: 'children',
  105. oldChildren: [],
  106. newChildren: [ new ViewText( 'x' ) ],
  107. node: viewRoot.getChild( 0 )
  108. }
  109. ] );
  110. expect( getModelData( model ) ).to.equal(
  111. '<paragraph><$text bold="true" italic="true">x[]</$text></paragraph>'
  112. );
  113. } );
  114. it( 'should handle multiple text mutations', () => {
  115. editor.setData( '<p>foo<strong>bar</strong></p>' );
  116. viewDocument.fire( 'mutations', [
  117. {
  118. type: 'text',
  119. oldText: 'foo',
  120. newText: 'foob',
  121. node: viewRoot.getChild( 0 ).getChild( 0 )
  122. },
  123. {
  124. type: 'text',
  125. oldText: 'bar',
  126. newText: 'ar',
  127. node: viewRoot.getChild( 0 ).getChild( 1 ).getChild( 0 )
  128. }
  129. ] );
  130. expect( getModelData( model ) ).to.equal( '<paragraph>foob[]<$text bold="true">ar</$text></paragraph>' );
  131. expect( getViewData( view ) ).to.equal( '<p>foob{}<strong>ar</strong></p>' );
  132. } );
  133. it( 'should handle multiple text node insertion', () => {
  134. editor.setData( '<p></p><p></p>' );
  135. viewDocument.fire( 'mutations', [
  136. {
  137. type: 'children',
  138. oldChildren: [],
  139. newChildren: [ new ViewText( 'x' ) ],
  140. node: viewRoot.getChild( 0 )
  141. },
  142. {
  143. type: 'children',
  144. oldChildren: [],
  145. newChildren: [ new ViewText( 'y' ) ],
  146. node: viewRoot.getChild( 1 )
  147. }
  148. ] );
  149. expect( getModelData( model ) ).to.equal( '<paragraph>x</paragraph><paragraph>y[]</paragraph>' );
  150. expect( getViewData( view ) ).to.equal( '<p>x</p><p>y{}</p>' );
  151. } );
  152. it( 'should do nothing when two nodes were inserted', () => {
  153. editor.setData( '<p></p>' );
  154. viewDocument.fire( 'mutations', [
  155. {
  156. type: 'children',
  157. oldChildren: [],
  158. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  159. node: viewRoot.getChild( 0 )
  160. }
  161. ] );
  162. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  163. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  164. } );
  165. it( 'should do nothing when two nodes were inserted and one removed', () => {
  166. viewDocument.fire( 'mutations', [
  167. {
  168. type: 'children',
  169. oldChildren: [ new ViewText( 'foobar' ) ],
  170. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  171. node: viewRoot.getChild( 0 )
  172. }
  173. ] );
  174. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  175. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  176. } );
  177. it( 'should handle multiple children in the node', () => {
  178. editor.setData( '<p>foo<img></img></p>' );
  179. viewDocument.fire( 'mutations', [
  180. {
  181. type: 'children',
  182. oldChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ) ],
  183. newChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ), new ViewText( 'x' ) ],
  184. node: viewRoot.getChild( 0 )
  185. }
  186. ] );
  187. expect( getModelData( model ) ).to.equal( '<paragraph>foo<image></image>x[]</paragraph>' );
  188. expect( getViewData( view ) ).to.equal( '<p>foo<img></img>x{}</p>' );
  189. } );
  190. it( 'should do nothing when node was removed', () => {
  191. viewDocument.fire( 'mutations', [
  192. {
  193. type: 'children',
  194. oldChildren: [ new ViewText( 'foobar' ) ],
  195. newChildren: [],
  196. node: viewRoot.getChild( 0 )
  197. }
  198. ] );
  199. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  200. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  201. } );
  202. it( 'should do nothing when element was inserted', () => {
  203. editor.setData( '<p></p>' );
  204. viewDocument.fire( 'mutations', [
  205. {
  206. type: 'children',
  207. oldChildren: [],
  208. newChildren: [ new ViewElement( 'img' ) ],
  209. node: viewRoot.getChild( 0 )
  210. }
  211. ] );
  212. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  213. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  214. } );
  215. it( 'should set model selection appropriately to view selection passed in mutations event', () => {
  216. // This test case emulates spellchecker correction.
  217. const viewSelection = new ViewSelection();
  218. viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  219. viewDocument.fire( 'mutations',
  220. [ {
  221. type: 'text',
  222. oldText: 'foobar',
  223. newText: 'foodar',
  224. node: viewRoot.getChild( 0 ).getChild( 0 )
  225. } ],
  226. viewSelection
  227. );
  228. expect( getModelData( model ) ).to.equal( '<paragraph>foodar[]</paragraph>' );
  229. expect( getViewData( view ) ).to.equal( '<p>foodar{}</p>' );
  230. } );
  231. it( 'should use up to one insert and remove operations (spellchecker)', () => {
  232. // This test case emulates spellchecker correction.
  233. const viewSelection = new ViewSelection();
  234. viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  235. testUtils.sinon.spy( Writer.prototype, 'insert' );
  236. testUtils.sinon.spy( Writer.prototype, 'remove' );
  237. viewDocument.fire( 'mutations',
  238. [ {
  239. type: 'text',
  240. oldText: 'foobar',
  241. newText: 'fxobxr',
  242. node: viewRoot.getChild( 0 ).getChild( 0 )
  243. } ],
  244. viewSelection
  245. );
  246. expect( Writer.prototype.insert.calledOnce ).to.be.true;
  247. expect( Writer.prototype.remove.calledOnce ).to.be.true;
  248. } );
  249. it( 'should place selection after when correcting to longer word (spellchecker)', () => {
  250. // This test case emulates spellchecker correction.
  251. editor.setData( '<p>Foo hous a</p>' );
  252. const viewSelection = new ViewSelection();
  253. viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  254. viewDocument.fire( 'mutations',
  255. [ {
  256. type: 'text',
  257. oldText: 'Foo hous a',
  258. newText: 'Foo house a',
  259. node: viewRoot.getChild( 0 ).getChild( 0 )
  260. } ],
  261. viewSelection
  262. );
  263. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[] a</paragraph>' );
  264. expect( getViewData( view ) ).to.equal( '<p>Foo house{} a</p>' );
  265. } );
  266. it( 'should place selection after when correcting to shorter word (spellchecker)', () => {
  267. // This test case emulates spellchecker correction.
  268. editor.setData( '<p>Bar athat foo</p>' );
  269. const viewSelection = new ViewSelection();
  270. viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  271. viewDocument.fire( 'mutations',
  272. [ {
  273. type: 'text',
  274. oldText: 'Bar athat foo',
  275. newText: 'Bar that foo',
  276. node: viewRoot.getChild( 0 ).getChild( 0 )
  277. } ],
  278. viewSelection
  279. );
  280. expect( getModelData( model ) ).to.equal( '<paragraph>Bar that[] foo</paragraph>' );
  281. expect( getViewData( view ) ).to.equal( '<p>Bar that{} foo</p>' );
  282. } );
  283. it( 'should place selection after when merging two words (spellchecker)', () => {
  284. // This test case emulates spellchecker correction.
  285. editor.setData( '<p>Foo hous e</p>' );
  286. const viewSelection = new ViewSelection();
  287. viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  288. viewDocument.fire( 'mutations',
  289. [ {
  290. type: 'text',
  291. oldText: 'Foo hous e',
  292. newText: 'Foo house',
  293. node: viewRoot.getChild( 0 ).getChild( 0 )
  294. } ],
  295. viewSelection
  296. );
  297. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[]</paragraph>' );
  298. expect( getViewData( view ) ).to.equal( '<p>Foo house{}</p>' );
  299. } );
  300. it( 'should place non-collapsed selection after changing single character (composition)', () => {
  301. editor.setData( '<p>Foo house</p>' );
  302. const viewSelection = new ViewSelection();
  303. viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  304. viewSelection.setFocus( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  305. viewDocument.fire( 'mutations',
  306. [ {
  307. type: 'text',
  308. oldText: 'Foo house',
  309. newText: 'Foo housa',
  310. node: viewRoot.getChild( 0 ).getChild( 0 )
  311. } ],
  312. viewSelection
  313. );
  314. expect( getModelData( model ) ).to.equal( '<paragraph>Foo hous[a]</paragraph>' );
  315. expect( getViewData( view ) ).to.equal( '<p>Foo hous{a}</p>' );
  316. } );
  317. it( 'should replace last &nbsp; with space', () => {
  318. model.change( writer => {
  319. writer.setSelection(
  320. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  321. );
  322. } );
  323. viewDocument.fire( 'mutations', [
  324. {
  325. type: 'text',
  326. oldText: 'foobar',
  327. newText: 'foobar\u00A0',
  328. node: viewRoot.getChild( 0 ).getChild( 0 )
  329. }
  330. ] );
  331. expect( getModelData( model ) ).to.equal( '<paragraph>foobar []</paragraph>' );
  332. expect( getViewData( view ) ).to.equal( '<p>foobar {}</p>' );
  333. } );
  334. it( 'should replace first &nbsp; with space', () => {
  335. model.change( writer => {
  336. writer.setSelection(
  337. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 0 ), 0 )
  338. );
  339. } );
  340. viewDocument.fire( 'mutations', [
  341. {
  342. type: 'text',
  343. oldText: 'foobar',
  344. newText: '\u00A0foobar',
  345. node: viewRoot.getChild( 0 ).getChild( 0 )
  346. }
  347. ] );
  348. expect( getModelData( model ) ).to.equal( '<paragraph> []foobar</paragraph>' );
  349. expect( getViewData( view ) ).to.equal( '<p> {}foobar</p>' );
  350. } );
  351. it( 'should replace all &nbsp; with spaces', () => {
  352. model.change( writer => {
  353. writer.setSelection(
  354. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  355. );
  356. } );
  357. viewDocument.fire( 'mutations', [
  358. {
  359. type: 'text',
  360. oldText: 'foobar',
  361. newText: 'foobar\u00A0\u00A0\u00A0baz',
  362. node: viewRoot.getChild( 0 ).getChild( 0 )
  363. }
  364. ] );
  365. expect( getModelData( model ) ).to.equal( '<paragraph>foobar baz[]</paragraph>' );
  366. expect( getViewData( view ) ).to.equal( '<p>foobar baz{}</p>' );
  367. } );
  368. // ckeditor5#718.
  369. it( 'should not crash and prevent all changes if view common ancestor of mutations cannot be mapped to model', () => {
  370. editor.setData( '<p>Foo</p><ul><li>Bar</li><li>Baz</li></ul>' );
  371. const ul = viewRoot.getChild( 1 );
  372. viewDocument.fire( 'mutations', [
  373. {
  374. type: 'text',
  375. oldText: 'Bar',
  376. newText: 'Bx',
  377. node: ul.getChild( 0 )
  378. },
  379. {
  380. type: 'children',
  381. oldChildren: [ ul.getChild( 0 ), ul.getChild( 1 ) ],
  382. newChildren: [ ul.getChild( 0 ) ],
  383. node: ul
  384. }
  385. ] );
  386. expect( getViewData( view ) ).to.equal( '<p>{}Foo</p><ul><li>Bar</li><li>Baz</li></ul>' );
  387. } );
  388. it( 'should handle bogus br correctly', () => {
  389. editor.setData( '<p><strong>Foo</strong></p>' );
  390. editor.model.change( writer => {
  391. writer.setSelection( editor.model.document.getRoot().getChild( 0 ), 'end' );
  392. writer.removeSelectionAttribute( 'bold' );
  393. } );
  394. // We need to change the DOM content manually because typing algorithm actually does not check
  395. // `newChildren` and `oldChildren` list but takes them from DOM and model.
  396. const p = viewRoot.getChild( 0 );
  397. const domP = editor.editing.view.domConverter.mapViewToDom( p );
  398. domP.appendChild( document.createTextNode( ' ' ) );
  399. domP.appendChild( document.createElement( 'br' ) );
  400. viewDocument.fire( 'mutations', [
  401. {
  402. type: 'children',
  403. oldChildren: [ viewRoot.getChild( 0 ).getChild( 0 ) ],
  404. newChildren: [
  405. new ViewElement( 'strong', null, new ViewText( 'Foo' ) ),
  406. new ViewText( ' ' ),
  407. new ViewElement( 'br' )
  408. ],
  409. node: viewRoot.getChild( 0 )
  410. }
  411. ] );
  412. expect( getViewData( view ) ).to.equal( '<p><strong>Foo</strong> {}</p>' );
  413. } );
  414. it( 'should handle children mutation correctly if there are soft breaks in the mutated container', () => {
  415. editor.setData( '<p><strong>Foo</strong><br /><strong>Bar</strong></p>' );
  416. editor.model.change( writer => {
  417. writer.setSelection( editor.model.document.getRoot().getChild( 0 ), 'end' );
  418. writer.removeSelectionAttribute( 'bold' );
  419. } );
  420. // We need to change the DOM content manually because typing algorithm actually does not check
  421. // `newChildren` and `oldChildren` list but takes them from DOM and model.
  422. const p = viewRoot.getChild( 0 );
  423. const domP = editor.editing.view.domConverter.mapViewToDom( p );
  424. domP.appendChild( document.createTextNode( ' ' ) );
  425. domP.appendChild( document.createElement( 'br' ) );
  426. viewDocument.fire( 'mutations', [
  427. {
  428. type: 'children',
  429. oldChildren: [ ...viewRoot.getChild( 0 ).getChildren() ],
  430. newChildren: [
  431. new ViewElement( 'strong', null, new ViewText( 'Foo' ) ),
  432. new ViewElement( 'br' ),
  433. new ViewElement( 'strong', null, new ViewText( 'Bar' ) ),
  434. new ViewText( ' ' ),
  435. new ViewElement( 'br' )
  436. ],
  437. node: viewRoot.getChild( 0 )
  438. }
  439. ] );
  440. expect( getViewData( view ) ).to.equal( '<p><strong>Foo</strong><br></br><strong>Bar</strong> {}</p>' );
  441. } );
  442. } );
  443. describe( 'keystroke handling', () => {
  444. it( 'should remove contents', () => {
  445. model.change( writer => {
  446. writer.setSelection(
  447. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  448. } );
  449. listenter.listenTo( viewDocument, 'keydown', () => {
  450. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  451. }, { priority: 'lowest' } );
  452. } );
  453. // #97
  454. it( 'should remove contents and merge blocks', () => {
  455. setModelData( model, '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  456. listenter.listenTo( viewDocument, 'keydown', () => {
  457. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  458. viewDocument.fire( 'mutations', [
  459. {
  460. type: 'text',
  461. oldText: 'foar',
  462. newText: 'foyar',
  463. node: viewRoot.getChild( 0 ).getChild( 0 )
  464. }
  465. ] );
  466. }, { priority: 'lowest' } );
  467. viewDocument.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  468. expect( getModelData( model ) ).to.equal( '<paragraph>foy[]ar</paragraph>' );
  469. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  470. } );
  471. it( 'should do nothing on arrow key', () => {
  472. model.change( writer => {
  473. writer.setSelection(
  474. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  475. } );
  476. viewDocument.fire( 'keydown', { keyCode: getCode( 'arrowdown' ) } );
  477. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  478. } );
  479. it( 'should do nothing on ctrl combinations', () => {
  480. model.change( writer => {
  481. writer.setSelection(
  482. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  483. } );
  484. viewDocument.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  485. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  486. } );
  487. it( 'should do nothing on non printable keys', () => {
  488. model.change( writer => {
  489. writer.setSelection(
  490. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  491. } );
  492. viewDocument.fire( 'keydown', { keyCode: 16 } ); // Shift
  493. viewDocument.fire( 'keydown', { keyCode: 35 } ); // Home
  494. viewDocument.fire( 'keydown', { keyCode: 112 } ); // F1
  495. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  496. } );
  497. // #69
  498. it( 'should do nothing on tab key', () => {
  499. model.change( writer => {
  500. writer.setSelection(
  501. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  502. } );
  503. viewDocument.fire( 'keydown', { keyCode: 9 } ); // Tab
  504. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  505. } );
  506. it( 'should do nothing if selection is collapsed', () => {
  507. viewDocument.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  508. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  509. } );
  510. it( 'should lock buffer if selection is not collapsed', () => {
  511. const buffer = editor.commands.get( 'input' )._buffer;
  512. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  513. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  514. model.change( writer => {
  515. writer.setSelection(
  516. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  517. } );
  518. viewDocument.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  519. expect( lockSpy.calledOnce ).to.be.true;
  520. expect( unlockSpy.calledOnce ).to.be.true;
  521. } );
  522. it( 'should not lock buffer on non printable keys', () => {
  523. const buffer = editor.commands.get( 'input' )._buffer;
  524. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  525. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  526. viewDocument.fire( 'keydown', { keyCode: 16 } ); // Shift
  527. viewDocument.fire( 'keydown', { keyCode: 35 } ); // Home
  528. viewDocument.fire( 'keydown', { keyCode: 112 } ); // F1
  529. expect( lockSpy.callCount ).to.be.equal( 0 );
  530. expect( unlockSpy.callCount ).to.be.equal( 0 );
  531. } );
  532. it( 'should not lock buffer on collapsed selection', () => {
  533. const buffer = editor.commands.get( 'input' )._buffer;
  534. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  535. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  536. viewDocument.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  537. viewDocument.fire( 'keydown', { keyCode: getCode( 'a' ) } );
  538. viewDocument.fire( 'keydown', { keyCode: getCode( 'z' ) } );
  539. expect( lockSpy.callCount ).to.be.equal( 0 );
  540. expect( unlockSpy.callCount ).to.be.equal( 0 );
  541. } );
  542. it( 'should not modify document when input command is disabled and selection is collapsed', () => {
  543. setModelData( model, '<paragraph>foo[]bar</paragraph>' );
  544. editor.commands.get( 'input' ).isEnabled = false;
  545. viewDocument.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  546. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  547. } );
  548. it( 'should not modify document when input command is disabled and selection is non-collapsed', () => {
  549. setModelData( model, '<paragraph>fo[ob]ar</paragraph>' );
  550. editor.commands.get( 'input' ).isEnabled = false;
  551. viewDocument.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  552. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  553. } );
  554. describe( '#83', () => {
  555. it( 'should remove contents on composition start key if not during composition', () => {
  556. model.change( writer => {
  557. writer.setSelection(
  558. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  559. } );
  560. viewDocument.fire( 'keydown', { keyCode: 229 } );
  561. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  562. } );
  563. it( 'should not remove contents on composition start key if during composition', () => {
  564. model.change( writer => {
  565. writer.setSelection(
  566. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  567. } );
  568. viewDocument.fire( 'compositionstart' );
  569. viewDocument.fire( 'keydown', { keyCode: 229 } );
  570. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  571. } );
  572. it( 'should not remove contents on compositionstart event if selection is flat', () => {
  573. editor.setData( '<p><strong>foo</strong> <i>bar</i></p>' );
  574. model.change( writer => {
  575. writer.setSelection(
  576. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 5 ) );
  577. } );
  578. viewDocument.fire( 'compositionstart' );
  579. expect( getModelData( model ) ).to.equal(
  580. '<paragraph><$text bold="true">fo[o</$text> <$text italic="true">b]ar</$text></paragraph>' );
  581. } );
  582. it( 'should not remove contents on compositionstart event if no selection', () => {
  583. editor.setData( '<p><strong>foo</strong> <i>bar</i></p>' );
  584. const documentSelection = model.document.selection;
  585. // Create empty selection.
  586. model.document.selection = new ModelSelection();
  587. viewDocument.fire( 'compositionstart' );
  588. expect( getModelData( model ) ).to.equal(
  589. '<paragraph><$text bold="true">foo</$text> <$text italic="true">bar</$text></paragraph>' );
  590. // Restore document selection.
  591. model.document.selection = documentSelection;
  592. } );
  593. it( 'should remove contents on compositionstart event if selection is not flat', () => {
  594. editor.setData( '<p><strong>foo</strong></p><p><i>bar</i></p>' );
  595. model.change( writer => {
  596. writer.setSelection(
  597. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 1 ), 2 ) );
  598. } );
  599. viewDocument.fire( 'compositionstart' );
  600. expect( getModelData( model ) ).to.equal(
  601. '<paragraph><$text bold="true">fo[]</$text><$text italic="true">r</$text></paragraph>' );
  602. } );
  603. it( 'should not remove contents on keydown event after compositionend event if selection did not change', () => {
  604. editor.setData( '<p><strong>foo</strong></p><p><i>bar</i></p>' );
  605. model.change( writer => {
  606. writer.setSelection(
  607. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 1 ), 2 ) );
  608. } );
  609. viewDocument.fire( 'compositionend' );
  610. viewDocument.fire( 'keydown', { keyCode: 229 } );
  611. expect( getModelData( model ) ).to.equal(
  612. '<paragraph><$text bold="true">fo[o</$text></paragraph><paragraph><$text italic="true">ba]r</$text></paragraph>' );
  613. } );
  614. it( 'should remove contents on keydown event after compositionend event if selection have changed', () => {
  615. editor.setData( '<p><strong>foo</strong></p><p><i>bar</i></p>' );
  616. model.change( writer => {
  617. writer.setSelection(
  618. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 1 ), 2 ) );
  619. } );
  620. viewDocument.fire( 'compositionend' );
  621. model.change( writer => {
  622. writer.setSelection(
  623. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 1 ), 1 ) );
  624. } );
  625. viewDocument.fire( 'keydown', { keyCode: 229 } );
  626. expect( getModelData( model ) ).to.equal(
  627. '<paragraph><$text bold="true">fo[]</$text><$text italic="true">ar</$text></paragraph>' );
  628. } );
  629. } );
  630. } );
  631. } );