input.js 31 KB

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