8
0

input.js 28 KB

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