input.js 36 KB

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