8
0

input.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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 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 ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  16. import ModelSelection from '@ckeditor/ckeditor5-engine/src/model/selection';
  17. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  18. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  19. import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
  20. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  21. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  22. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  23. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  24. /* global document */
  25. describe( 'Input feature', () => {
  26. let editor, model, modelRoot, view, viewDocument, viewRoot, listenter;
  27. testUtils.createSinonSandbox();
  28. beforeEach( () => {
  29. listenter = Object.create( EmitterMixin );
  30. const domElement = document.createElement( 'div' );
  31. document.body.appendChild( domElement );
  32. return ClassicTestEditor.create( domElement, { plugins: [ Input, Paragraph, Bold, Italic, List, ShiftEnter, Link ] } )
  33. .then( newEditor => {
  34. // Mock image feature.
  35. newEditor.model.schema.register( 'image', { allowWhere: '$text' } );
  36. newEditor.conversion.elementToElement( {
  37. model: 'image',
  38. view: 'img'
  39. } );
  40. editor = newEditor;
  41. model = editor.model;
  42. modelRoot = model.document.getRoot();
  43. view = editor.editing.view;
  44. viewDocument = view.document;
  45. viewRoot = viewDocument.getRoot();
  46. editor.setData( '<p>foobar</p>' );
  47. model.change( writer => {
  48. writer.setSelection(
  49. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 0 ), 3 )
  50. );
  51. } );
  52. } );
  53. } );
  54. afterEach( () => {
  55. listenter.stopListening();
  56. return editor.destroy();
  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 ViewSelection();
  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 ViewSelection();
  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 ViewSelection();
  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 ViewSelection();
  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 ViewSelection();
  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 ViewSelection();
  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. it( 'should handle bogus br correctly', () => {
  390. editor.setData( '<p><strong>Foo</strong></p>' );
  391. editor.model.change( writer => {
  392. writer.setSelection( editor.model.document.getRoot().getChild( 0 ), 'end' );
  393. writer.removeSelectionAttribute( 'bold' );
  394. } );
  395. // We need to change the DOM content manually because typing algorithm actually does not check
  396. // `newChildren` and `oldChildren` list but takes them from DOM and model.
  397. const p = viewRoot.getChild( 0 );
  398. const domP = editor.editing.view.domConverter.mapViewToDom( p );
  399. domP.appendChild( document.createTextNode( ' ' ) );
  400. domP.appendChild( document.createElement( 'br' ) );
  401. viewDocument.fire( 'mutations', [
  402. {
  403. type: 'children',
  404. oldChildren: [ viewRoot.getChild( 0 ).getChild( 0 ) ],
  405. newChildren: [
  406. new ViewElement( 'strong', null, new ViewText( 'Foo' ) ),
  407. new ViewText( ' ' ),
  408. new ViewElement( 'br' )
  409. ],
  410. node: viewRoot.getChild( 0 )
  411. }
  412. ] );
  413. expect( getViewData( view ) ).to.equal( '<p><strong>Foo</strong> {}</p>' );
  414. } );
  415. it( 'should handle children mutation correctly if there are soft breaks in the mutated container', () => {
  416. editor.setData( '<p><strong>Foo</strong><br /><strong>Bar</strong></p>' );
  417. editor.model.change( writer => {
  418. writer.setSelection( editor.model.document.getRoot().getChild( 0 ), 'end' );
  419. writer.removeSelectionAttribute( 'bold' );
  420. } );
  421. // We need to change the DOM content manually because typing algorithm actually does not check
  422. // `newChildren` and `oldChildren` list but takes them from DOM and model.
  423. const p = viewRoot.getChild( 0 );
  424. const domP = editor.editing.view.domConverter.mapViewToDom( p );
  425. domP.appendChild( document.createTextNode( ' ' ) );
  426. domP.appendChild( document.createElement( 'br' ) );
  427. viewDocument.fire( 'mutations', [
  428. {
  429. type: 'children',
  430. oldChildren: [ ...viewRoot.getChild( 0 ).getChildren() ],
  431. newChildren: [
  432. new ViewElement( 'strong', null, new ViewText( 'Foo' ) ),
  433. new ViewElement( 'br' ),
  434. new ViewElement( 'strong', null, new ViewText( 'Bar' ) ),
  435. new ViewText( ' ' ),
  436. new ViewElement( 'br' )
  437. ],
  438. node: viewRoot.getChild( 0 )
  439. }
  440. ] );
  441. expect( getViewData( view ) ).to.equal( '<p><strong>Foo</strong><br></br><strong>Bar</strong> {}</p>' );
  442. } );
  443. // ckeditor5-typing#170.
  444. it( 'should handle mutations correctly if there was an &nbsp; in model already', () => {
  445. editor.setData( '<p><a href="#"><strong>F</strong></a><strong>oo&nbsp;bar</strong></p>' );
  446. model.change( writer => {
  447. writer.setSelection(
  448. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 1, modelRoot.getChild( 0 ), 1 )
  449. );
  450. } );
  451. // We need to change the DOM content manually because typing algorithm actually does not check
  452. // `newChildren` and `oldChildren` list but takes them from DOM and model.
  453. const strong = viewRoot.getChild( 0 ).getChild( 0 ).getChild( 0 );
  454. const domStrong = editor.editing.view.domConverter.mapViewToDom( strong );
  455. domStrong.appendChild( document.createTextNode( 'x' ) );
  456. // The mutation provided here is a bit different than what browser outputs, but browser outputs three mutations
  457. // which changes the order of elements in the DOM so to keep it simple, only one, key mutation is used in the test.
  458. // Also, the only thing that the typing algorithm takes from the mutations is `node`...
  459. viewDocument.fire( 'mutations', [
  460. {
  461. type: 'children',
  462. oldChildren: Array.from( viewRoot.getChild( 0 ).getChild( 0 ).getChildren() ),
  463. newChildren: [
  464. new ViewElement( 'strong', null, new ViewText( 'Fx' ) ),
  465. ],
  466. node: viewRoot.getChild( 0 ).getChild( 0 )
  467. }
  468. ] );
  469. expect( getModelData( model ) ).to.equal(
  470. '<paragraph>' +
  471. '<$text bold="true" linkHref="#">Fx[]</$text>' +
  472. '<$text bold="true">oo\u00A0bar</$text>' +
  473. '</paragraph>'
  474. );
  475. expect( getViewData( view ) ).to.equal(
  476. '<p>' +
  477. '<a class="ck-link_selected" href="#"><strong>Fx{}</strong></a>' +
  478. '<strong>oo\u00A0bar</strong>' +
  479. '</p>'
  480. );
  481. } );
  482. } );
  483. describe( 'keystroke handling', () => {
  484. it( 'should remove contents', () => {
  485. model.change( writer => {
  486. writer.setSelection(
  487. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  488. } );
  489. listenter.listenTo( viewDocument, 'keydown', () => {
  490. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  491. }, { priority: 'lowest' } );
  492. } );
  493. // #97
  494. it( 'should remove contents and merge blocks', () => {
  495. setModelData( model, '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  496. listenter.listenTo( viewDocument, 'keydown', () => {
  497. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  498. viewDocument.fire( 'mutations', [
  499. {
  500. type: 'text',
  501. oldText: 'foar',
  502. newText: 'foyar',
  503. node: viewRoot.getChild( 0 ).getChild( 0 )
  504. }
  505. ] );
  506. }, { priority: 'lowest' } );
  507. viewDocument.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  508. expect( getModelData( model ) ).to.equal( '<paragraph>foy[]ar</paragraph>' );
  509. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  510. } );
  511. it( 'should do nothing on arrow key', () => {
  512. model.change( writer => {
  513. writer.setSelection(
  514. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  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. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  523. } );
  524. viewDocument.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  525. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  526. } );
  527. it( 'should do nothing on non printable keys', () => {
  528. model.change( writer => {
  529. writer.setSelection(
  530. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  531. } );
  532. viewDocument.fire( 'keydown', { keyCode: 16 } ); // Shift
  533. viewDocument.fire( 'keydown', { keyCode: 19 } ); // Pause
  534. viewDocument.fire( 'keydown', { keyCode: 35 } ); // Home
  535. viewDocument.fire( 'keydown', { keyCode: 112 } ); // F1
  536. viewDocument.fire( 'keydown', { keyCode: 255 } ); // Display brightness
  537. // Media control keys
  538. viewDocument.fire( 'keydown', { keyCode: 173 } ); // Mute
  539. viewDocument.fire( 'keydown', { keyCode: 174 } ); // Volume up
  540. viewDocument.fire( 'keydown', { keyCode: 175 } ); // Volume down
  541. viewDocument.fire( 'keydown', { keyCode: 176 } ); // Next song
  542. viewDocument.fire( 'keydown', { keyCode: 177 } ); // Previous song
  543. viewDocument.fire( 'keydown', { keyCode: 179 } ); // Stop
  544. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  545. } );
  546. // #69
  547. it( 'should do nothing on tab key', () => {
  548. model.change( writer => {
  549. writer.setSelection(
  550. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  551. } );
  552. viewDocument.fire( 'keydown', { keyCode: 9 } ); // Tab
  553. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  554. } );
  555. it( 'should do nothing if selection is collapsed', () => {
  556. viewDocument.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  557. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  558. } );
  559. it( 'should lock buffer if selection is not collapsed', () => {
  560. const buffer = editor.commands.get( 'input' )._buffer;
  561. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  562. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  563. model.change( writer => {
  564. writer.setSelection(
  565. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  566. } );
  567. viewDocument.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  568. expect( lockSpy.calledOnce ).to.be.true;
  569. expect( unlockSpy.calledOnce ).to.be.true;
  570. } );
  571. it( 'should not lock buffer on non printable keys', () => {
  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. viewDocument.fire( 'keydown', { keyCode: 16 } ); // Shift
  576. viewDocument.fire( 'keydown', { keyCode: 35 } ); // Home
  577. viewDocument.fire( 'keydown', { keyCode: 112 } ); // F1
  578. expect( lockSpy.callCount ).to.be.equal( 0 );
  579. expect( unlockSpy.callCount ).to.be.equal( 0 );
  580. } );
  581. it( 'should not lock buffer on collapsed selection', () => {
  582. const buffer = editor.commands.get( 'input' )._buffer;
  583. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  584. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  585. viewDocument.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  586. viewDocument.fire( 'keydown', { keyCode: getCode( 'a' ) } );
  587. viewDocument.fire( 'keydown', { keyCode: getCode( 'z' ) } );
  588. expect( lockSpy.callCount ).to.be.equal( 0 );
  589. expect( unlockSpy.callCount ).to.be.equal( 0 );
  590. } );
  591. it( 'should not modify document when input command is disabled and selection is collapsed', () => {
  592. setModelData( model, '<paragraph>foo[]bar</paragraph>' );
  593. editor.commands.get( 'input' ).isEnabled = false;
  594. viewDocument.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  595. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  596. } );
  597. it( 'should not modify document when input command is disabled and selection is non-collapsed', () => {
  598. setModelData( model, '<paragraph>fo[ob]ar</paragraph>' );
  599. editor.commands.get( 'input' ).isEnabled = false;
  600. viewDocument.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  601. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  602. } );
  603. describe( '#83', () => {
  604. it( 'should remove contents on composition start key if not during composition', () => {
  605. model.change( writer => {
  606. writer.setSelection(
  607. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  608. } );
  609. viewDocument.fire( 'keydown', { keyCode: 229 } );
  610. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  611. } );
  612. it( 'should not remove contents on composition start key if during composition', () => {
  613. model.change( writer => {
  614. writer.setSelection(
  615. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
  616. } );
  617. viewDocument.fire( 'compositionstart' );
  618. viewDocument.fire( 'keydown', { keyCode: 229 } );
  619. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  620. } );
  621. it( 'should not remove contents on compositionstart event if selection is flat', () => {
  622. editor.setData( '<p><strong>foo</strong> <i>bar</i></p>' );
  623. model.change( writer => {
  624. writer.setSelection(
  625. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 5 ) );
  626. } );
  627. viewDocument.fire( 'compositionstart' );
  628. expect( getModelData( model ) ).to.equal(
  629. '<paragraph><$text bold="true">fo[o</$text> <$text italic="true">b]ar</$text></paragraph>' );
  630. } );
  631. it( 'should not remove contents on compositionstart event if no selection', () => {
  632. editor.setData( '<p><strong>foo</strong> <i>bar</i></p>' );
  633. const documentSelection = model.document.selection;
  634. // Create empty selection.
  635. model.document.selection = new ModelSelection();
  636. viewDocument.fire( 'compositionstart' );
  637. expect( getModelData( model ) ).to.equal(
  638. '<paragraph><$text bold="true">foo</$text> <$text italic="true">bar</$text></paragraph>' );
  639. // Restore document selection.
  640. model.document.selection = documentSelection;
  641. } );
  642. it( 'should remove contents on compositionstart event if selection is not flat', () => {
  643. editor.setData( '<p><strong>foo</strong></p><p><i>bar</i></p>' );
  644. model.change( writer => {
  645. writer.setSelection(
  646. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 1 ), 2 ) );
  647. } );
  648. viewDocument.fire( 'compositionstart' );
  649. expect( getModelData( model ) ).to.equal(
  650. '<paragraph><$text bold="true">fo[]</$text><$text italic="true">r</$text></paragraph>' );
  651. } );
  652. it( 'should not remove contents on keydown event after compositionend event if selection did not change', () => {
  653. editor.setData( '<p><strong>foo</strong></p><p><i>bar</i></p>' );
  654. model.change( writer => {
  655. writer.setSelection(
  656. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 1 ), 2 ) );
  657. } );
  658. viewDocument.fire( 'compositionend' );
  659. viewDocument.fire( 'keydown', { keyCode: 229 } );
  660. expect( getModelData( model ) ).to.equal(
  661. '<paragraph><$text bold="true">fo[o</$text></paragraph><paragraph><$text italic="true">ba]r</$text></paragraph>' );
  662. } );
  663. it( 'should remove contents on keydown event after compositionend event if selection have changed', () => {
  664. editor.setData( '<p><strong>foo</strong></p><p><i>bar</i></p>' );
  665. model.change( writer => {
  666. writer.setSelection(
  667. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 1 ), 2 ) );
  668. } );
  669. viewDocument.fire( 'compositionend' );
  670. model.change( writer => {
  671. writer.setSelection(
  672. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 1 ), 1 ) );
  673. } );
  674. viewDocument.fire( 'keydown', { keyCode: 229 } );
  675. expect( getModelData( model ) ).to.equal(
  676. '<paragraph><$text bold="true">fo[]</$text><$text italic="true">ar</$text></paragraph>' );
  677. } );
  678. } );
  679. } );
  680. } );