8
0

input.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 TextTransformation from '../src/texttransformation';
  15. import Writer from '@ckeditor/ckeditor5-engine/src/model/writer';
  16. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  17. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  18. import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
  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. import env from '@ckeditor/ckeditor5-utils/src/env';
  24. /* global document */
  25. describe( 'Input feature', () => {
  26. let editor, model, modelRoot, view, viewDocument, viewRoot, listenter, domElement;
  27. testUtils.createSinonSandbox();
  28. beforeEach( () => {
  29. listenter = Object.create( EmitterMixin );
  30. domElement = document.createElement( 'div' );
  31. document.body.appendChild( domElement );
  32. const options = {
  33. plugins: [ Input, Paragraph, Bold, Italic, List, ShiftEnter, Link, TextTransformation ],
  34. typing: {
  35. transformations: {
  36. extra: [
  37. { from: ':)', to: '🙂' },
  38. { from: ':family:', to: '👨‍👩‍👧' }
  39. ]
  40. }
  41. }
  42. };
  43. return ClassicTestEditor.create( domElement, options )
  44. .then( newEditor => {
  45. // Mock image feature.
  46. newEditor.model.schema.register( 'image', { allowWhere: '$text' } );
  47. newEditor.conversion.elementToElement( {
  48. model: 'image',
  49. view: 'img'
  50. } );
  51. editor = newEditor;
  52. model = editor.model;
  53. modelRoot = model.document.getRoot();
  54. view = editor.editing.view;
  55. viewDocument = view.document;
  56. viewRoot = viewDocument.getRoot();
  57. editor.setData( '<p>foobar</p>' );
  58. model.change( writer => {
  59. writer.setSelection( modelRoot.getChild( 0 ), 3 );
  60. } );
  61. } );
  62. } );
  63. afterEach( () => {
  64. listenter.stopListening();
  65. domElement.remove();
  66. return editor.destroy();
  67. } );
  68. describe( 'isInput()', () => {
  69. let input;
  70. beforeEach( () => {
  71. input = editor.plugins.get( 'Input' );
  72. } );
  73. it( 'returns true for batch created using "input" command', done => {
  74. model.document.once( 'change:data', ( evt, batch ) => {
  75. expect( input.isInput( batch ) ).to.be.true;
  76. done();
  77. } );
  78. editor.execute( 'input', { text: 'foo' } );
  79. } );
  80. it( 'returns false for batch not created using "input" command', () => {
  81. const batch = model.createBatch();
  82. expect( input.isInput( batch ) ).to.be.false;
  83. } );
  84. } );
  85. describe( 'mutations handling', () => {
  86. it( 'should handle text mutation', () => {
  87. viewDocument.fire( 'mutations', [
  88. {
  89. type: 'text',
  90. oldText: 'foobar',
  91. newText: 'fooxbar',
  92. node: viewRoot.getChild( 0 ).getChild( 0 )
  93. }
  94. ] );
  95. expect( getModelData( model ) ).to.equal( '<paragraph>foox[]bar</paragraph>' );
  96. expect( getViewData( view ) ).to.equal( '<p>foox{}bar</p>' );
  97. } );
  98. it( 'should handle text mutation change', () => {
  99. viewDocument.fire( 'mutations', [
  100. {
  101. type: 'text',
  102. oldText: 'foobar',
  103. newText: 'foodar',
  104. node: viewRoot.getChild( 0 ).getChild( 0 )
  105. }
  106. ] );
  107. expect( getModelData( model ) ).to.equal( '<paragraph>food[]ar</paragraph>' );
  108. expect( getViewData( view ) ).to.equal( '<p>food{}ar</p>' );
  109. } );
  110. it( 'should handle text node insertion', () => {
  111. editor.setData( '<p></p>' );
  112. viewDocument.fire( 'mutations', [
  113. {
  114. type: 'children',
  115. oldChildren: [],
  116. newChildren: [ new ViewText( viewDocument, 'x' ) ],
  117. node: viewRoot.getChild( 0 )
  118. }
  119. ] );
  120. expect( getModelData( model ) ).to.equal( '<paragraph>x[]</paragraph>' );
  121. expect( getViewData( view ) ).to.equal( '<p>x{}</p>' );
  122. } );
  123. it( 'should apply selection attributes to the inserted text', () => {
  124. setModelData( model, '<paragraph>[]</paragraph>', {
  125. selectionAttributes: {
  126. bold: true,
  127. italic: true
  128. }
  129. } );
  130. viewDocument.fire( 'mutations', [
  131. {
  132. type: 'children',
  133. oldChildren: [],
  134. newChildren: [ new ViewText( viewDocument, 'x' ) ],
  135. node: viewRoot.getChild( 0 )
  136. }
  137. ] );
  138. expect( getModelData( model ) ).to.equal(
  139. '<paragraph><$text bold="true" italic="true">x[]</$text></paragraph>'
  140. );
  141. } );
  142. it( 'should handle multiple text mutations', () => {
  143. editor.setData( '<p>foo<strong>bar</strong></p>' );
  144. viewDocument.fire( 'mutations', [
  145. {
  146. type: 'text',
  147. oldText: 'foo',
  148. newText: 'foob',
  149. node: viewRoot.getChild( 0 ).getChild( 0 )
  150. },
  151. {
  152. type: 'text',
  153. oldText: 'bar',
  154. newText: 'ar',
  155. node: viewRoot.getChild( 0 ).getChild( 1 ).getChild( 0 )
  156. }
  157. ] );
  158. expect( getModelData( model ) ).to.equal( '<paragraph>foob[]<$text bold="true">ar</$text></paragraph>' );
  159. expect( getViewData( view ) ).to.equal( '<p>foob{}<strong>ar</strong></p>' );
  160. } );
  161. it( 'should handle multiple text node insertion', () => {
  162. editor.setData( '<p></p><p></p>' );
  163. viewDocument.fire( 'mutations', [
  164. {
  165. type: 'children',
  166. oldChildren: [],
  167. newChildren: [ new ViewText( viewDocument, 'x' ) ],
  168. node: viewRoot.getChild( 0 )
  169. },
  170. {
  171. type: 'children',
  172. oldChildren: [],
  173. newChildren: [ new ViewText( viewDocument, 'y' ) ],
  174. node: viewRoot.getChild( 1 )
  175. }
  176. ] );
  177. expect( getModelData( model ) ).to.equal( '<paragraph>x</paragraph><paragraph>y[]</paragraph>' );
  178. expect( getViewData( view ) ).to.equal( '<p>x</p><p>y{}</p>' );
  179. } );
  180. it( 'should do nothing when two nodes were inserted', () => {
  181. editor.setData( '<p></p>' );
  182. viewDocument.fire( 'mutations', [
  183. {
  184. type: 'children',
  185. oldChildren: [],
  186. newChildren: [ new ViewText( viewDocument, 'x' ), new ViewElement( viewDocument, 'img' ) ],
  187. node: viewRoot.getChild( 0 )
  188. }
  189. ] );
  190. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  191. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  192. } );
  193. it( 'should do nothing when two nodes were inserted and one removed', () => {
  194. viewDocument.fire( 'mutations', [
  195. {
  196. type: 'children',
  197. oldChildren: [ new ViewText( viewDocument, 'foobar' ) ],
  198. newChildren: [ new ViewText( viewDocument, 'x' ), new ViewElement( viewDocument, 'img' ) ],
  199. node: viewRoot.getChild( 0 )
  200. }
  201. ] );
  202. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  203. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  204. } );
  205. it( 'should handle multiple children in the node', () => {
  206. editor.setData( '<p>foo<img></img></p>' );
  207. viewDocument.fire( 'mutations', [
  208. {
  209. type: 'children',
  210. oldChildren: [ new ViewText( viewDocument, 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ) ],
  211. newChildren: [
  212. new ViewText( viewDocument, 'foo' ),
  213. viewRoot.getChild( 0 ).getChild( 1 ),
  214. new ViewText( viewDocument, 'x' )
  215. ],
  216. node: viewRoot.getChild( 0 )
  217. }
  218. ] );
  219. expect( getModelData( model ) ).to.equal( '<paragraph>foo<image></image>x[]</paragraph>' );
  220. expect( getViewData( view ) ).to.equal( '<p>foo<img></img>x{}</p>' );
  221. } );
  222. it( 'should do nothing when node was removed', () => {
  223. viewDocument.fire( 'mutations', [
  224. {
  225. type: 'children',
  226. oldChildren: [ new ViewText( viewDocument, 'foobar' ) ],
  227. newChildren: [],
  228. node: viewRoot.getChild( 0 )
  229. }
  230. ] );
  231. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  232. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  233. } );
  234. it( 'should do nothing when element was inserted', () => {
  235. editor.setData( '<p></p>' );
  236. viewDocument.fire( 'mutations', [
  237. {
  238. type: 'children',
  239. oldChildren: [],
  240. newChildren: [ new ViewElement( viewDocument, 'img' ) ],
  241. node: viewRoot.getChild( 0 )
  242. }
  243. ] );
  244. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  245. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  246. } );
  247. it( 'should set model selection appropriately to view selection passed in mutations event', () => {
  248. // This test case emulates spellchecker correction.
  249. const viewSelection = view.createSelection();
  250. viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  251. viewDocument.fire( 'mutations',
  252. [ {
  253. type: 'text',
  254. oldText: 'foobar',
  255. newText: 'foodar',
  256. node: viewRoot.getChild( 0 ).getChild( 0 )
  257. } ],
  258. viewSelection
  259. );
  260. expect( getModelData( model ) ).to.equal( '<paragraph>foodar[]</paragraph>' );
  261. expect( getViewData( view ) ).to.equal( '<p>foodar{}</p>' );
  262. } );
  263. it( 'should use up to one insert and remove operations (spellchecker)', () => {
  264. // This test case emulates spellchecker correction.
  265. const viewSelection = view.createSelection();
  266. viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  267. testUtils.sinon.spy( Writer.prototype, 'insert' );
  268. testUtils.sinon.spy( Writer.prototype, 'remove' );
  269. viewDocument.fire( 'mutations',
  270. [ {
  271. type: 'text',
  272. oldText: 'foobar',
  273. newText: 'fxobxr',
  274. node: viewRoot.getChild( 0 ).getChild( 0 )
  275. } ],
  276. viewSelection
  277. );
  278. expect( Writer.prototype.insert.calledOnce ).to.be.true;
  279. expect( Writer.prototype.remove.calledOnce ).to.be.true;
  280. } );
  281. it( 'should place selection after when correcting to longer word (spellchecker)', () => {
  282. // This test case emulates spellchecker correction.
  283. editor.setData( '<p>Foo hous a</p>' );
  284. const viewSelection = view.createSelection();
  285. viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  286. viewDocument.fire( 'mutations',
  287. [ {
  288. type: 'text',
  289. oldText: 'Foo hous a',
  290. newText: 'Foo house a',
  291. node: viewRoot.getChild( 0 ).getChild( 0 )
  292. } ],
  293. viewSelection
  294. );
  295. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[] a</paragraph>' );
  296. expect( getViewData( view ) ).to.equal( '<p>Foo house{} a</p>' );
  297. } );
  298. it( 'should place selection after when correcting to shorter word (spellchecker)', () => {
  299. // This test case emulates spellchecker correction.
  300. editor.setData( '<p>Bar athat foo</p>' );
  301. const viewSelection = view.createSelection();
  302. viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  303. viewDocument.fire( 'mutations',
  304. [ {
  305. type: 'text',
  306. oldText: 'Bar athat foo',
  307. newText: 'Bar that foo',
  308. node: viewRoot.getChild( 0 ).getChild( 0 )
  309. } ],
  310. viewSelection
  311. );
  312. expect( getModelData( model ) ).to.equal( '<paragraph>Bar that[] foo</paragraph>' );
  313. expect( getViewData( view ) ).to.equal( '<p>Bar that{} foo</p>' );
  314. } );
  315. it( 'should place selection after when merging two words (spellchecker)', () => {
  316. // This test case emulates spellchecker correction.
  317. editor.setData( '<p>Foo hous e</p>' );
  318. const viewSelection = view.createSelection();
  319. viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  320. viewDocument.fire( 'mutations',
  321. [ {
  322. type: 'text',
  323. oldText: 'Foo hous e',
  324. newText: 'Foo house',
  325. node: viewRoot.getChild( 0 ).getChild( 0 )
  326. } ],
  327. viewSelection
  328. );
  329. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[]</paragraph>' );
  330. expect( getViewData( view ) ).to.equal( '<p>Foo house{}</p>' );
  331. } );
  332. it( 'should place non-collapsed selection after changing single character (composition)', () => {
  333. editor.setData( '<p>Foo house</p>' );
  334. const viewSelection = view.createSelection();
  335. viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  336. viewSelection.setFocus( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  337. viewDocument.fire( 'mutations',
  338. [ {
  339. type: 'text',
  340. oldText: 'Foo house',
  341. newText: 'Foo housa',
  342. node: viewRoot.getChild( 0 ).getChild( 0 )
  343. } ],
  344. viewSelection
  345. );
  346. expect( getModelData( model ) ).to.equal( '<paragraph>Foo hous[a]</paragraph>' );
  347. expect( getViewData( view ) ).to.equal( '<p>Foo hous{a}</p>' );
  348. } );
  349. it( 'should replace last &nbsp; with space', () => {
  350. model.change( writer => {
  351. writer.setSelection( modelRoot.getChild( 0 ), 6 );
  352. } );
  353. viewDocument.fire( 'mutations', [
  354. {
  355. type: 'text',
  356. oldText: 'foobar',
  357. newText: 'foobar\u00A0',
  358. node: viewRoot.getChild( 0 ).getChild( 0 )
  359. }
  360. ] );
  361. expect( getModelData( model ) ).to.equal( '<paragraph>foobar []</paragraph>' );
  362. expect( getViewData( view ) ).to.equal( '<p>foobar {}</p>' );
  363. } );
  364. it( 'should replace first &nbsp; with space', () => {
  365. model.change( writer => {
  366. writer.setSelection(
  367. writer.createRange(
  368. writer.createPositionAt( modelRoot.getChild( 0 ), 0 ),
  369. writer.createPositionAt( modelRoot.getChild( 0 ), 0 )
  370. )
  371. );
  372. } );
  373. viewDocument.fire( 'mutations', [
  374. {
  375. type: 'text',
  376. oldText: 'foobar',
  377. newText: '\u00A0foobar',
  378. node: viewRoot.getChild( 0 ).getChild( 0 )
  379. }
  380. ] );
  381. expect( getModelData( model ) ).to.equal( '<paragraph> []foobar</paragraph>' );
  382. expect( getViewData( view ) ).to.equal( '<p> {}foobar</p>' );
  383. } );
  384. it( 'should replace all &nbsp; with spaces', () => {
  385. model.change( writer => {
  386. writer.setSelection( modelRoot.getChild( 0 ), 6 );
  387. } );
  388. viewDocument.fire( 'mutations', [
  389. {
  390. type: 'text',
  391. oldText: 'foobar',
  392. newText: 'foobar\u00A0\u00A0\u00A0baz',
  393. node: viewRoot.getChild( 0 ).getChild( 0 )
  394. }
  395. ] );
  396. expect( getModelData( model ) ).to.equal( '<paragraph>foobar baz[]</paragraph>' );
  397. expect( getViewData( view ) ).to.equal( '<p>foobar baz{}</p>' );
  398. } );
  399. // ckeditor5#718.
  400. it( 'should not crash and prevent all changes if view common ancestor of mutations cannot be mapped to model', () => {
  401. editor.setData( '<p>Foo</p><ul><li>Bar</li><li>Baz</li></ul>' );
  402. const ul = viewRoot.getChild( 1 );
  403. viewDocument.fire( 'mutations', [
  404. {
  405. type: 'text',
  406. oldText: 'Bar',
  407. newText: 'Bx',
  408. node: ul.getChild( 0 )
  409. },
  410. {
  411. type: 'children',
  412. oldChildren: [ ul.getChild( 0 ), ul.getChild( 1 ) ],
  413. newChildren: [ ul.getChild( 0 ) ],
  414. node: ul
  415. }
  416. ] );
  417. expect( getViewData( view ) ).to.equal( '<p>{}Foo</p><ul><li>Bar</li><li>Baz</li></ul>' );
  418. } );
  419. it( 'should handle bogus br correctly', () => {
  420. editor.setData( '<p><strong>Foo</strong></p>' );
  421. editor.model.change( writer => {
  422. writer.setSelection( editor.model.document.getRoot().getChild( 0 ), 'end' );
  423. writer.removeSelectionAttribute( 'bold' );
  424. } );
  425. // We need to change the DOM content manually because typing algorithm actually does not check
  426. // `newChildren` and `oldChildren` list but takes them from DOM and model.
  427. const p = viewRoot.getChild( 0 );
  428. const domP = editor.editing.view.domConverter.mapViewToDom( p );
  429. domP.appendChild( document.createTextNode( ' ' ) );
  430. domP.appendChild( document.createElement( 'br' ) );
  431. viewDocument.fire( 'mutations', [
  432. {
  433. type: 'children',
  434. oldChildren: [ viewRoot.getChild( 0 ).getChild( 0 ) ],
  435. newChildren: [
  436. new ViewElement( viewDocument, 'strong', null, new ViewText( viewDocument, 'Foo' ) ),
  437. new ViewText( viewDocument, ' ' ),
  438. new ViewElement( viewDocument, 'br' )
  439. ],
  440. node: viewRoot.getChild( 0 )
  441. }
  442. ] );
  443. expect( getViewData( view ) ).to.equal( '<p><strong>Foo</strong> {}</p>' );
  444. } );
  445. it( 'should handle children mutation correctly if there are soft breaks in the mutated container', () => {
  446. editor.setData( '<p><strong>Foo</strong><br /><strong>Bar</strong></p>' );
  447. editor.model.change( writer => {
  448. writer.setSelection( editor.model.document.getRoot().getChild( 0 ), 'end' );
  449. writer.removeSelectionAttribute( 'bold' );
  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 p = viewRoot.getChild( 0 );
  454. const domP = editor.editing.view.domConverter.mapViewToDom( p );
  455. domP.appendChild( document.createTextNode( ' ' ) );
  456. domP.appendChild( document.createElement( 'br' ) );
  457. viewDocument.fire( 'mutations', [
  458. {
  459. type: 'children',
  460. oldChildren: [ ...viewRoot.getChild( 0 ).getChildren() ],
  461. newChildren: [
  462. new ViewElement( viewDocument, 'strong', null, new ViewText( viewDocument, 'Foo' ) ),
  463. new ViewElement( viewDocument, 'br' ),
  464. new ViewElement( viewDocument, 'strong', null, new ViewText( viewDocument, 'Bar' ) ),
  465. new ViewText( viewDocument, ' ' ),
  466. new ViewElement( viewDocument, 'br' )
  467. ],
  468. node: viewRoot.getChild( 0 )
  469. }
  470. ] );
  471. expect( getViewData( view ) ).to.equal( '<p><strong>Foo</strong><br></br><strong>Bar</strong> {}</p>' );
  472. } );
  473. // ckeditor5-typing#170.
  474. it( 'should handle mutations correctly if there was an &nbsp; in model already', () => {
  475. editor.setData( '<p><a href="#"><strong>F</strong></a><strong>oo&nbsp;bar</strong></p>' );
  476. model.change( writer => {
  477. writer.setSelection( modelRoot.getChild( 0 ), 1 );
  478. } );
  479. // We need to change the DOM content manually because typing algorithm actually does not check
  480. // `newChildren` and `oldChildren` list but takes them from DOM and model.
  481. const strong = viewRoot.getChild( 0 ).getChild( 0 ).getChild( 0 );
  482. const domStrong = editor.editing.view.domConverter.mapViewToDom( strong );
  483. domStrong.appendChild( document.createTextNode( 'x' ) );
  484. // The mutation provided here is a bit different than what browser outputs, but browser outputs three mutations
  485. // which changes the order of elements in the DOM so to keep it simple, only one, key mutation is used in the test.
  486. // Also, the only thing that the typing algorithm takes from the mutations is `node`...
  487. viewDocument.fire( 'mutations', [
  488. {
  489. type: 'children',
  490. oldChildren: Array.from( viewRoot.getChild( 0 ).getChild( 0 ).getChildren() ),
  491. newChildren: [
  492. new ViewElement( viewDocument, 'strong', null, new ViewText( viewDocument, 'Fx' ) )
  493. ],
  494. node: viewRoot.getChild( 0 ).getChild( 0 )
  495. }
  496. ] );
  497. expect( getModelData( model ) ).to.equal(
  498. '<paragraph>' +
  499. '<$text bold="true" linkHref="#">Fx[]</$text>' +
  500. '<$text bold="true">oo\u00A0bar</$text>' +
  501. '</paragraph>'
  502. );
  503. expect( getViewData( view ) ).to.equal(
  504. '<p>' +
  505. '<a class="ck-link_selected" href="#"><strong>Fx{}</strong></a>' +
  506. '<strong>oo\u00A0bar</strong>' +
  507. '</p>'
  508. );
  509. } );
  510. it( 'should handle correctly if last element is inline', () => {
  511. editor.model.schema.register( 'placeholder', { allowWhere: '$text', isInline: true } );
  512. editor.conversion.elementToElement( {
  513. model: 'placeholder',
  514. view: 'placeholder'
  515. } );
  516. editor.setData( '<p>foo<placeholder></placeholder></p>' );
  517. editor.model.change( writer => {
  518. writer.setSelection( editor.model.document.getRoot().getChild( 0 ), 'end' );
  519. } );
  520. expect( getViewData( view ) ).to.equal( '<p>foo<placeholder></placeholder>[]</p>' );
  521. // We need to change the DOM content manually because typing algorithm actually does not check
  522. // `newChildren` and `oldChildren` list but takes them from DOM and model.
  523. const p = viewRoot.getChild( 0 );
  524. const domP = editor.editing.view.domConverter.mapViewToDom( p );
  525. domP.appendChild( document.createTextNode( '!' ) );
  526. viewDocument.fire( 'mutations', [
  527. {
  528. type: 'children',
  529. oldChildren: [ ...viewRoot.getChild( 0 ).getChildren() ],
  530. newChildren: [
  531. new ViewText( viewDocument, 'Foo' ),
  532. new ViewContainerElement( viewDocument, 'placeholder' ),
  533. new ViewText( viewDocument, 'f' )
  534. ],
  535. node: viewRoot.getChild( 0 )
  536. }
  537. ] );
  538. expect( getViewData( view ) ).to.equal( '<p>foo<placeholder></placeholder>!{}</p>' );
  539. } );
  540. it( 'should handle correctly if some elements are inline', () => {
  541. editor.model.schema.register( 'placeholder', { allowWhere: '$text', isInline: true } );
  542. editor.conversion.elementToElement( {
  543. model: 'placeholder',
  544. view: 'placeholder'
  545. } );
  546. editor.setData( '<p>foo<placeholder></placeholder>bar<placeholder></placeholder>baz</p>' );
  547. editor.model.change( writer => {
  548. writer.setSelection( editor.model.document.getRoot().getChild( 0 ), 'end' );
  549. } );
  550. // We need to change the DOM content manually because typing algorithm actually does not check
  551. // `newChildren` and `oldChildren` list but takes them from DOM and model.
  552. const p = viewRoot.getChild( 0 );
  553. const domP = editor.editing.view.domConverter.mapViewToDom( p );
  554. domP.appendChild( document.createTextNode( '!' ) );
  555. viewDocument.fire( 'mutations', [
  556. {
  557. type: 'children',
  558. oldChildren: [ ...viewRoot.getChild( 0 ).getChildren() ],
  559. newChildren: [
  560. new ViewText( viewDocument, 'foo' ),
  561. new ViewContainerElement( viewDocument, 'placeholder' ),
  562. new ViewText( viewDocument, 'bar' ),
  563. new ViewContainerElement( viewDocument, 'placeholder' ),
  564. new ViewText( viewDocument, 'baz' )
  565. ],
  566. node: viewRoot.getChild( 0 )
  567. }
  568. ] );
  569. expect( getViewData( view ) ).to.equal( '<p>foo<placeholder></placeholder>bar<placeholder></placeholder>baz!{}</p>' );
  570. } );
  571. // https://github.com/ckeditor/ckeditor5-typing/issues/181
  572. it( 'should not crash if the mutation old text is same as new text', () => {
  573. // It shouldn't matter what data is here, I am putting it like it is in the test scenario, but it is really about
  574. // what mutations are generated.
  575. editor.setData( '<p>Foo<strong> </strong>&nbsp;Bar</p>' );
  576. const p = viewRoot.getChild( 0 );
  577. viewDocument.fire( 'mutations', [
  578. {
  579. type: 'text',
  580. oldText: ' ',
  581. newText: ' ',
  582. node: p.getChild( 1 )
  583. },
  584. {
  585. type: 'text',
  586. oldText: 'Foo',
  587. newText: 'Foox',
  588. node: p.getChild( 0 )
  589. }
  590. ] );
  591. expect( getViewData( view ) ).to.equal( '<p>Foox{}<strong> </strong> Bar</p>' );
  592. } );
  593. describe( 'emoji', () => {
  594. it( 'should properly handle input after multi-byte emoji transformation (in short paragraph)', () => {
  595. testEmojiMutations( 'foo', ':)123456', '🙂123456' );
  596. } );
  597. it( 'should properly handle input after multi-byte emoji transformation (in long paragraph)', () => {
  598. testEmojiMutations( createTestString( 200 ), ':)123456', '🙂123456' );
  599. } );
  600. it( 'should properly handle input in text with multi-byte emoji (in short paragraph)', () => {
  601. testEmojiMutations( 'foo😱bar', '123456' );
  602. } );
  603. it( 'should properly handle input in text with multi-byte emoji (in long paragraph)', () => {
  604. const textInParagraph = createTestString( 200, ( v, idx ) => idx == 60 ? '😱' : v );
  605. testEmojiMutations( textInParagraph, '123456' );
  606. } );
  607. it( 'should properly handle input in middle of text with multi-byte emoji (in short paragraph)', () => {
  608. testEmojiMutations( 'foo😱bar', '123456', 6 );
  609. } );
  610. it( 'should properly handle input in middle of text with multi-byte emoji (in long paragraph)', () => {
  611. const textInParagraph = createTestString( 200, ( v, idx ) => idx == 60 ? '😱' : v );
  612. testEmojiMutations( textInParagraph, '123456', 100 );
  613. } );
  614. it( 'should properly handle input after multi-byte emoji zwj sequence transformation (in short paragraph)', () => {
  615. testEmojiMutations( 'foobar', ':family:123456', '👨‍👩‍👧123456' );
  616. } );
  617. it( 'should properly handle input after multi-byte emoji zwj sequence transformation (in long paragraph)', () => {
  618. testEmojiMutations( createTestString( 200 ), ':family:123456', '👨‍👩‍👧123456' );
  619. } );
  620. function createTestString( length, callback = v => v ) {
  621. const firstCharCode = 'a'.charCodeAt( 0 );
  622. const lettersCount = 'z'.charCodeAt( 0 ) - firstCharCode + 1;
  623. return new Array( length )
  624. .fill( '' )
  625. .map( ( value, idx ) => callback( String.fromCharCode( firstCharCode + idx % lettersCount ), idx ) )
  626. .join( '' );
  627. }
  628. function testEmojiMutations( textInParagraph, typing, expectedOrIndex = typing, index = -1 ) {
  629. let expected = expectedOrIndex;
  630. if ( typeof expectedOrIndex == 'number' ) {
  631. index = expectedOrIndex;
  632. expected = typing;
  633. }
  634. setModelData( model, `<paragraph>${ textInParagraph }[]</paragraph>` );
  635. typing.split( '' ).forEach( ( char, idx ) => simulateMutation( char, index < 0 ? index : index + idx ) );
  636. const expectedResult = index < 0 ? textInParagraph + expected :
  637. textInParagraph.substring( 0, index ) + expected + textInParagraph.substring( index );
  638. expect( getModelData( model, { withoutSelection: true } ) )
  639. .to.equal( `<paragraph>${ expectedResult }</paragraph>` );
  640. }
  641. function simulateMutation( text, index = -1 ) {
  642. const placeOfMutation = viewRoot.getChild( 0 ).getChild( 0 );
  643. const oldText = placeOfMutation.data;
  644. const newText = index < 0 ? oldText + text :
  645. oldText.substring( 0, index ) + text + oldText.substring( index );
  646. viewDocument.fire( 'mutations', [
  647. {
  648. type: 'text',
  649. oldText,
  650. newText,
  651. node: placeOfMutation
  652. }
  653. ] );
  654. }
  655. } );
  656. } );
  657. describe( 'keystroke handling', () => {
  658. it( 'should remove contents', () => {
  659. model.change( writer => {
  660. writer.setSelection(
  661. writer.createRange(
  662. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  663. writer.createPositionAt( modelRoot.getChild( 0 ), 4 )
  664. )
  665. );
  666. } );
  667. listenter.listenTo( viewDocument, 'keydown', () => {
  668. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  669. }, { priority: 'lowest' } );
  670. viewDocument.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  671. } );
  672. // #97
  673. it( 'should remove contents and merge blocks', () => {
  674. setModelData( model, '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  675. listenter.listenTo( viewDocument, 'keydown', () => {
  676. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  677. viewDocument.fire( 'mutations', [
  678. {
  679. type: 'text',
  680. oldText: 'foar',
  681. newText: 'foyar',
  682. node: viewRoot.getChild( 0 ).getChild( 0 )
  683. }
  684. ] );
  685. }, { priority: 'lowest' } );
  686. viewDocument.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  687. expect( getModelData( model ) ).to.equal( '<paragraph>foy[]ar</paragraph>' );
  688. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  689. } );
  690. it( 'should do nothing on arrow key', () => {
  691. model.change( writer => {
  692. writer.setSelection(
  693. writer.createRange(
  694. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  695. writer.createPositionAt( modelRoot.getChild( 0 ), 4 )
  696. )
  697. );
  698. } );
  699. viewDocument.fire( 'keydown', { keyCode: getCode( 'arrowdown' ) } );
  700. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  701. } );
  702. it( 'should do nothing on ctrl combinations', () => {
  703. model.change( writer => {
  704. writer.setSelection(
  705. writer.createRange(
  706. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  707. writer.createPositionAt( modelRoot.getChild( 0 ), 4 )
  708. )
  709. );
  710. } );
  711. viewDocument.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  712. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  713. } );
  714. it( 'should do nothing on non printable keys', () => {
  715. model.change( writer => {
  716. writer.setSelection(
  717. writer.createRange(
  718. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  719. writer.createPositionAt( modelRoot.getChild( 0 ), 4 )
  720. )
  721. );
  722. } );
  723. viewDocument.fire( 'keydown', { keyCode: 16 } ); // Shift
  724. viewDocument.fire( 'keydown', { keyCode: 19 } ); // Pause
  725. viewDocument.fire( 'keydown', { keyCode: 35 } ); // Home
  726. viewDocument.fire( 'keydown', { keyCode: 112 } ); // F1
  727. viewDocument.fire( 'keydown', { keyCode: 255 } ); // Display brightness
  728. // Media control keys
  729. viewDocument.fire( 'keydown', { keyCode: 173 } ); // Mute
  730. viewDocument.fire( 'keydown', { keyCode: 174 } ); // Volume up
  731. viewDocument.fire( 'keydown', { keyCode: 175 } ); // Volume down
  732. viewDocument.fire( 'keydown', { keyCode: 176 } ); // Next song
  733. viewDocument.fire( 'keydown', { keyCode: 177 } ); // Previous song
  734. viewDocument.fire( 'keydown', { keyCode: 179 } ); // Stop
  735. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  736. } );
  737. // #69
  738. it( 'should do nothing on tab key', () => {
  739. model.change( writer => {
  740. writer.setSelection(
  741. writer.createRange(
  742. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  743. writer.createPositionAt( modelRoot.getChild( 0 ), 4 )
  744. )
  745. );
  746. } );
  747. viewDocument.fire( 'keydown', { keyCode: 9 } ); // Tab
  748. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  749. } );
  750. it( 'should do nothing if selection is collapsed', () => {
  751. viewDocument.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  752. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  753. } );
  754. it( 'should lock buffer if selection is not collapsed', () => {
  755. const buffer = editor.commands.get( 'input' )._buffer;
  756. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  757. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  758. model.change( writer => {
  759. writer.setSelection(
  760. writer.createRange(
  761. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  762. writer.createPositionAt( modelRoot.getChild( 0 ), 4 )
  763. )
  764. );
  765. } );
  766. viewDocument.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  767. expect( lockSpy.calledOnce ).to.be.true;
  768. expect( unlockSpy.calledOnce ).to.be.true;
  769. } );
  770. it( 'should not lock buffer on non printable keys', () => {
  771. const buffer = editor.commands.get( 'input' )._buffer;
  772. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  773. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  774. viewDocument.fire( 'keydown', { keyCode: 16 } ); // Shift
  775. viewDocument.fire( 'keydown', { keyCode: 35 } ); // Home
  776. viewDocument.fire( 'keydown', { keyCode: 112 } ); // F1
  777. expect( lockSpy.callCount ).to.equal( 0 );
  778. expect( unlockSpy.callCount ).to.equal( 0 );
  779. } );
  780. it( 'should not lock buffer on collapsed selection', () => {
  781. const buffer = editor.commands.get( 'input' )._buffer;
  782. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  783. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  784. viewDocument.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  785. viewDocument.fire( 'keydown', { keyCode: getCode( 'a' ) } );
  786. viewDocument.fire( 'keydown', { keyCode: getCode( 'z' ) } );
  787. expect( lockSpy.callCount ).to.equal( 0 );
  788. expect( unlockSpy.callCount ).to.equal( 0 );
  789. } );
  790. it( 'should not modify document when input command is disabled and selection is collapsed', () => {
  791. setModelData( model, '<paragraph>foo[]bar</paragraph>' );
  792. editor.commands.get( 'input' ).isEnabled = false;
  793. viewDocument.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  794. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  795. } );
  796. it( 'should not modify document when input command is disabled and selection is non-collapsed', () => {
  797. setModelData( model, '<paragraph>fo[ob]ar</paragraph>' );
  798. editor.commands.get( 'input' ).isEnabled = false;
  799. viewDocument.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  800. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  801. } );
  802. describe( '#83', () => {
  803. it( 'should remove contents on composition start key if not during composition', () => {
  804. model.change( writer => {
  805. writer.setSelection(
  806. writer.createRange(
  807. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  808. writer.createPositionAt( modelRoot.getChild( 0 ), 4 )
  809. )
  810. );
  811. } );
  812. viewDocument.fire( 'keydown', { keyCode: 229 } );
  813. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  814. } );
  815. it( 'should not remove contents on composition start key if during composition', () => {
  816. model.change( writer => {
  817. writer.setSelection(
  818. writer.createRange(
  819. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  820. writer.createPositionAt( modelRoot.getChild( 0 ), 4 )
  821. )
  822. );
  823. } );
  824. viewDocument.fire( 'compositionstart' );
  825. viewDocument.fire( 'keydown', { keyCode: 229 } );
  826. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  827. } );
  828. it( 'should not remove contents on compositionstart event if selection is flat', () => {
  829. editor.setData( '<p><strong>foo</strong> <i>bar</i></p>' );
  830. model.change( writer => {
  831. writer.setSelection(
  832. writer.createRange(
  833. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  834. writer.createPositionAt( modelRoot.getChild( 0 ), 5 )
  835. )
  836. );
  837. } );
  838. viewDocument.fire( 'compositionstart' );
  839. expect( getModelData( model ) ).to.equal(
  840. '<paragraph><$text bold="true">fo[o</$text> <$text italic="true">b]ar</$text></paragraph>' );
  841. } );
  842. it( 'should not remove contents on compositionstart event if no selection', () => {
  843. editor.setData( '<p><strong>foo</strong> <i>bar</i></p>' );
  844. const documentSelection = model.document.selection;
  845. // Create empty selection.
  846. model.document.selection = model.createSelection();
  847. viewDocument.fire( 'compositionstart' );
  848. expect( getModelData( model ) ).to.equal(
  849. '<paragraph><$text bold="true">foo</$text> <$text italic="true">bar</$text></paragraph>' );
  850. // Restore document selection.
  851. model.document.selection = documentSelection;
  852. } );
  853. it( 'should remove contents on compositionstart event if selection is not flat', () => {
  854. editor.setData( '<p><strong>foo</strong></p><p><i>bar</i></p>' );
  855. model.change( writer => {
  856. writer.setSelection(
  857. writer.createRange(
  858. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  859. writer.createPositionAt( modelRoot.getChild( 1 ), 2 )
  860. )
  861. );
  862. } );
  863. viewDocument.fire( 'compositionstart' );
  864. expect( getModelData( model ) ).to.equal(
  865. '<paragraph><$text bold="true">fo[]</$text><$text italic="true">r</$text></paragraph>' );
  866. } );
  867. it( 'should not remove contents on keydown event after compositionend event if selection did not change', () => {
  868. editor.setData( '<p><strong>foo</strong></p><p><i>bar</i></p>' );
  869. model.change( writer => {
  870. writer.setSelection(
  871. writer.createRange(
  872. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  873. writer.createPositionAt( modelRoot.getChild( 1 ), 2 )
  874. )
  875. );
  876. } );
  877. viewDocument.fire( 'compositionend' );
  878. viewDocument.fire( 'keydown', { keyCode: 229 } );
  879. expect( getModelData( model ) ).to.equal(
  880. '<paragraph><$text bold="true">fo[o</$text></paragraph><paragraph><$text italic="true">ba]r</$text></paragraph>' );
  881. } );
  882. it( 'should remove contents on keydown event after compositionend event if selection have changed', () => {
  883. editor.setData( '<p><strong>foo</strong></p><p><i>bar</i></p>' );
  884. model.change( writer => {
  885. writer.setSelection(
  886. writer.createRange(
  887. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  888. writer.createPositionAt( modelRoot.getChild( 1 ), 2 )
  889. )
  890. );
  891. } );
  892. viewDocument.fire( 'compositionend' );
  893. model.change( writer => {
  894. writer.setSelection(
  895. writer.createRange(
  896. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  897. writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
  898. )
  899. );
  900. } );
  901. viewDocument.fire( 'keydown', { keyCode: 229 } );
  902. expect( getModelData( model ) ).to.equal(
  903. '<paragraph><$text bold="true">fo[]</$text><$text italic="true">ar</$text></paragraph>' );
  904. } );
  905. } );
  906. } );
  907. } );
  908. describe( 'Input feature - Android', () => {
  909. let editor, model, modelRoot, view, viewDocument, listenter, oldEnvIsAndroid, domElement;
  910. testUtils.createSinonSandbox();
  911. before( () => {
  912. oldEnvIsAndroid = env.isAndroid;
  913. env.isAndroid = true;
  914. } );
  915. beforeEach( () => {
  916. listenter = Object.create( EmitterMixin );
  917. domElement = document.createElement( 'div' );
  918. document.body.appendChild( domElement );
  919. return ClassicTestEditor.create( domElement, { plugins: [ Input, Paragraph, Bold, Italic, List, ShiftEnter, Link ] } )
  920. .then( newEditor => {
  921. editor = newEditor;
  922. model = editor.model;
  923. modelRoot = model.document.getRoot();
  924. view = editor.editing.view;
  925. viewDocument = view.document;
  926. editor.setData( '<p>foobar</p>' );
  927. model.change( writer => {
  928. writer.setSelection( modelRoot.getChild( 0 ), 3 );
  929. } );
  930. } );
  931. } );
  932. afterEach( () => {
  933. listenter.stopListening();
  934. domElement.remove();
  935. return editor.destroy();
  936. } );
  937. after( () => {
  938. env.isAndroid = oldEnvIsAndroid;
  939. } );
  940. describe( 'keystroke handling', () => {
  941. it( 'should remove contents', () => {
  942. model.change( writer => {
  943. writer.setSelection(
  944. writer.createRange(
  945. writer.createPositionAt( modelRoot.getChild( 0 ), 2 ),
  946. writer.createPositionAt( modelRoot.getChild( 0 ), 4 )
  947. )
  948. );
  949. } );
  950. listenter.listenTo( viewDocument, 'beforeinput', () => {
  951. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  952. }, { priority: 'lowest' } );
  953. // On Android, `keycode` is set to `229` (in scenarios when `keydown` event is not send).
  954. viewDocument.fire( 'beforeinput', { keyCode: 229 } );
  955. } );
  956. it( 'should remove contents and merge blocks', () => {
  957. setModelData( model, '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  958. listenter.listenTo( viewDocument, 'beforeinput', () => {
  959. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  960. }, { priority: 'lowest' } );
  961. // On Android, `keycode` is set to `229` (in scenarios when `keydown` event is not send).
  962. viewDocument.fire( 'beforeinput', { keyCode: 229 } );
  963. } );
  964. it( 'should do nothing if selection is collapsed', () => {
  965. viewDocument.fire( 'beforeinput', { keyCode: 229 } );
  966. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  967. } );
  968. it( 'should not modify document when input command is disabled and selection is collapsed', () => {
  969. setModelData( model, '<paragraph>foo[]bar</paragraph>' );
  970. editor.commands.get( 'input' ).isEnabled = false;
  971. // On Android, `keycode` is set to `229` (in scenarios when `keydown` event is not send).
  972. viewDocument.fire( 'beforeinput', { keyCode: 229 } );
  973. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  974. } );
  975. it( 'should not modify document when input command is disabled and selection is non-collapsed', () => {
  976. setModelData( model, '<paragraph>fo[ob]ar</paragraph>' );
  977. editor.commands.get( 'input' ).isEnabled = false;
  978. // On Android, `keycode` is set to `229` (in scenarios when `keydown` event is not send).
  979. viewDocument.fire( 'beforeinput', { keyCode: 229 } );
  980. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  981. } );
  982. } );
  983. } );