input.js 35 KB

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