input.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import Bold from '@ckeditor/ckeditor5-basic-styles/src/boldengine';
  11. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italicengine';
  12. import LinkEngine from '@ckeditor/ckeditor5-link/src/linkengine';
  13. import Input from '../src/input';
  14. import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  15. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  16. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  17. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  18. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  19. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  20. import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
  21. import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
  22. import MutationObserver from '@ckeditor/ckeditor5-engine/src/view/observer/mutationobserver';
  23. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  24. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  25. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  26. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  27. describe( 'Input feature', () => {
  28. let editor, model, modelRoot, view, viewRoot, listenter;
  29. testUtils.createSinonSandbox();
  30. before( () => {
  31. listenter = Object.create( EmitterMixin );
  32. return VirtualTestEditor
  33. .create( {
  34. plugins: [ Input, Paragraph, Bold ]
  35. } )
  36. .then( newEditor => {
  37. // Mock image feature.
  38. newEditor.document.schema.registerItem( 'image', '$inline' );
  39. buildModelConverter().for( newEditor.data.modelToView, newEditor.editing.modelToView )
  40. .fromElement( 'image' )
  41. .toElement( 'img' );
  42. buildViewConverter().for( newEditor.data.viewToModel )
  43. .fromElement( 'img' )
  44. .toElement( 'image' );
  45. editor = newEditor;
  46. model = editor.editing.model;
  47. modelRoot = model.getRoot();
  48. view = editor.editing.view;
  49. viewRoot = view.getRoot();
  50. } );
  51. } );
  52. after( () => {
  53. return editor.destroy();
  54. } );
  55. beforeEach( () => {
  56. editor.setData( '<p>foobar</p>' );
  57. model.enqueueChanges( () => {
  58. model.selection.setRanges( [
  59. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 0 ), 3 )
  60. ] );
  61. } );
  62. } );
  63. afterEach( () => {
  64. listenter.stopListening();
  65. } );
  66. describe( 'mutations handling', () => {
  67. it( 'should handle text mutation', () => {
  68. view.fire( 'mutations', [
  69. {
  70. type: 'text',
  71. oldText: 'foobar',
  72. newText: 'fooxbar',
  73. node: viewRoot.getChild( 0 ).getChild( 0 )
  74. }
  75. ] );
  76. expect( getModelData( model ) ).to.equal( '<paragraph>foox[]bar</paragraph>' );
  77. expect( getViewData( view ) ).to.equal( '<p>foox{}bar</p>' );
  78. } );
  79. it( 'should handle text mutation change', () => {
  80. view.fire( 'mutations', [
  81. {
  82. type: 'text',
  83. oldText: 'foobar',
  84. newText: 'foodar',
  85. node: viewRoot.getChild( 0 ).getChild( 0 )
  86. }
  87. ] );
  88. expect( getModelData( model ) ).to.equal( '<paragraph>food[]ar</paragraph>' );
  89. expect( getViewData( view ) ).to.equal( '<p>food{}ar</p>' );
  90. } );
  91. it( 'should handle text node insertion', () => {
  92. editor.setData( '<p></p>' );
  93. view.fire( 'mutations', [
  94. {
  95. type: 'children',
  96. oldChildren: [],
  97. newChildren: [ new ViewText( 'x' ) ],
  98. node: viewRoot.getChild( 0 )
  99. }
  100. ] );
  101. expect( getModelData( model ) ).to.equal( '<paragraph>x[]</paragraph>' );
  102. expect( getViewData( view ) ).to.equal( '<p>x{}</p>' );
  103. } );
  104. it( 'should handle multiple text mutations', () => {
  105. editor.setData( '<p>foo<strong>bar</strong></p>' );
  106. view.fire( 'mutations', [
  107. {
  108. type: 'text',
  109. oldText: 'foo',
  110. newText: 'foob',
  111. node: viewRoot.getChild( 0 ).getChild( 0 )
  112. },
  113. {
  114. type: 'text',
  115. oldText: 'bar',
  116. newText: 'ar',
  117. node: viewRoot.getChild( 0 ).getChild( 1 ).getChild( 0 )
  118. }
  119. ] );
  120. expect( getModelData( model ) ).to.equal( '<paragraph>foob[]<$text bold="true">ar</$text></paragraph>' );
  121. expect( getViewData( view ) ).to.equal( '<p>foob{}<strong>ar</strong></p>' );
  122. } );
  123. it( 'should handle multiple text node insertion', () => {
  124. editor.setData( '<p></p><p></p>' );
  125. view.fire( 'mutations', [
  126. {
  127. type: 'children',
  128. oldChildren: [],
  129. newChildren: [ new ViewText( 'x' ) ],
  130. node: viewRoot.getChild( 0 )
  131. },
  132. {
  133. type: 'children',
  134. oldChildren: [],
  135. newChildren: [ new ViewText( 'y' ) ],
  136. node: viewRoot.getChild( 1 )
  137. }
  138. ] );
  139. expect( getModelData( model ) ).to.equal( '<paragraph>x</paragraph><paragraph>y[]</paragraph>' );
  140. expect( getViewData( view ) ).to.equal( '<p>x</p><p>y{}</p>' );
  141. } );
  142. it( 'should do nothing when two nodes were inserted', () => {
  143. editor.setData( '<p></p>' );
  144. view.fire( 'mutations', [
  145. {
  146. type: 'children',
  147. oldChildren: [],
  148. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  149. node: viewRoot.getChild( 0 )
  150. }
  151. ] );
  152. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  153. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  154. } );
  155. it( 'should do nothing when two nodes were inserted and one removed', () => {
  156. view.fire( 'mutations', [
  157. {
  158. type: 'children',
  159. oldChildren: [ new ViewText( 'foobar' ) ],
  160. newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
  161. node: viewRoot.getChild( 0 )
  162. }
  163. ] );
  164. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  165. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  166. } );
  167. it( 'should handle multiple children in the node', () => {
  168. editor.setData( '<p>foo<img></img></p>' );
  169. view.fire( 'mutations', [
  170. {
  171. type: 'children',
  172. oldChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ) ],
  173. newChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ), new ViewText( 'x' ) ],
  174. node: viewRoot.getChild( 0 )
  175. }
  176. ] );
  177. expect( getModelData( model ) ).to.equal( '<paragraph>foo<image></image>x[]</paragraph>' );
  178. expect( getViewData( view ) ).to.equal( '<p>foo<img></img>x{}</p>' );
  179. } );
  180. it( 'should do nothing when node was removed', () => {
  181. view.fire( 'mutations', [
  182. {
  183. type: 'children',
  184. oldChildren: [ new ViewText( 'foobar' ) ],
  185. newChildren: [],
  186. node: viewRoot.getChild( 0 )
  187. }
  188. ] );
  189. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  190. expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
  191. } );
  192. it( 'should do nothing when element was inserted', () => {
  193. editor.setData( '<p></p>' );
  194. view.fire( 'mutations', [
  195. {
  196. type: 'children',
  197. oldChildren: [],
  198. newChildren: [ new ViewElement( 'img' ) ],
  199. node: viewRoot.getChild( 0 )
  200. }
  201. ] );
  202. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  203. expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
  204. } );
  205. it( 'should set model selection appropriately to view selection passed in mutations event', () => {
  206. // This test case emulates spellchecker correction.
  207. const viewSelection = new ViewSelection();
  208. viewSelection.setCollapsedAt( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  209. view.fire( 'mutations',
  210. [ {
  211. type: 'text',
  212. oldText: 'foobar',
  213. newText: 'foodar',
  214. node: viewRoot.getChild( 0 ).getChild( 0 )
  215. } ],
  216. viewSelection
  217. );
  218. expect( getModelData( model ) ).to.equal( '<paragraph>foodar[]</paragraph>' );
  219. expect( getViewData( view ) ).to.equal( '<p>foodar{}</p>' );
  220. } );
  221. it( 'should use up to one insert and remove operations (spellchecker)', () => {
  222. // This test case emulates spellchecker correction.
  223. const viewSelection = new ViewSelection();
  224. viewSelection.setCollapsedAt( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
  225. testUtils.sinon.spy( Batch.prototype, 'weakInsert' );
  226. testUtils.sinon.spy( Batch.prototype, 'remove' );
  227. view.fire( 'mutations',
  228. [ {
  229. type: 'text',
  230. oldText: 'foobar',
  231. newText: 'fxobxr',
  232. node: viewRoot.getChild( 0 ).getChild( 0 )
  233. } ],
  234. viewSelection
  235. );
  236. expect( Batch.prototype.weakInsert.calledOnce ).to.be.true;
  237. expect( Batch.prototype.remove.calledOnce ).to.be.true;
  238. } );
  239. it( 'should place selection after when correcting to longer word (spellchecker)', () => {
  240. // This test case emulates spellchecker correction.
  241. editor.setData( '<p>Foo hous a</p>' );
  242. const viewSelection = new ViewSelection();
  243. viewSelection.setCollapsedAt( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  244. view.fire( 'mutations',
  245. [ {
  246. type: 'text',
  247. oldText: 'Foo hous a',
  248. newText: 'Foo house a',
  249. node: viewRoot.getChild( 0 ).getChild( 0 )
  250. } ],
  251. viewSelection
  252. );
  253. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[] a</paragraph>' );
  254. expect( getViewData( view ) ).to.equal( '<p>Foo house{} a</p>' );
  255. } );
  256. it( 'should place selection after when correcting to shorter word (spellchecker)', () => {
  257. // This test case emulates spellchecker correction.
  258. editor.setData( '<p>Bar athat foo</p>' );
  259. const viewSelection = new ViewSelection();
  260. viewSelection.setCollapsedAt( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  261. view.fire( 'mutations',
  262. [ {
  263. type: 'text',
  264. oldText: 'Bar athat foo',
  265. newText: 'Bar that foo',
  266. node: viewRoot.getChild( 0 ).getChild( 0 )
  267. } ],
  268. viewSelection
  269. );
  270. expect( getModelData( model ) ).to.equal( '<paragraph>Bar that[] foo</paragraph>' );
  271. expect( getViewData( view ) ).to.equal( '<p>Bar that{} foo</p>' );
  272. } );
  273. it( 'should place selection after when merging two words (spellchecker)', () => {
  274. // This test case emulates spellchecker correction.
  275. editor.setData( '<p>Foo hous e</p>' );
  276. const viewSelection = new ViewSelection();
  277. viewSelection.setCollapsedAt( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  278. view.fire( 'mutations',
  279. [ {
  280. type: 'text',
  281. oldText: 'Foo hous e',
  282. newText: 'Foo house',
  283. node: viewRoot.getChild( 0 ).getChild( 0 )
  284. } ],
  285. viewSelection
  286. );
  287. expect( getModelData( model ) ).to.equal( '<paragraph>Foo house[]</paragraph>' );
  288. expect( getViewData( view ) ).to.equal( '<p>Foo house{}</p>' );
  289. } );
  290. it( 'should place non-collapsed selection after changing single character (composition)', () => {
  291. editor.setData( '<p>Foo house</p>' );
  292. const viewSelection = new ViewSelection();
  293. viewSelection.setCollapsedAt( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
  294. viewSelection.moveFocusTo( viewRoot.getChild( 0 ).getChild( 0 ), 9 );
  295. view.fire( 'mutations',
  296. [ {
  297. type: 'text',
  298. oldText: 'Foo house',
  299. newText: 'Foo housa',
  300. node: viewRoot.getChild( 0 ).getChild( 0 )
  301. } ],
  302. viewSelection
  303. );
  304. expect( getModelData( model ) ).to.equal( '<paragraph>Foo hous[a]</paragraph>' );
  305. expect( getViewData( view ) ).to.equal( '<p>Foo hous{a}</p>' );
  306. } );
  307. it( 'should replace last &nbsp; with space', () => {
  308. model.enqueueChanges( () => {
  309. model.selection.setRanges( [
  310. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  311. ] );
  312. } );
  313. view.fire( 'mutations', [
  314. {
  315. type: 'text',
  316. oldText: 'foobar',
  317. newText: 'foobar\u00A0',
  318. node: viewRoot.getChild( 0 ).getChild( 0 )
  319. }
  320. ] );
  321. expect( getModelData( model ) ).to.equal( '<paragraph>foobar []</paragraph>' );
  322. expect( getViewData( view ) ).to.equal( '<p>foobar {}</p>' );
  323. } );
  324. it( 'should replace first &nbsp; with space', () => {
  325. model.enqueueChanges( () => {
  326. model.selection.setRanges( [
  327. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 0 ), 0 )
  328. ] );
  329. } );
  330. view.fire( 'mutations', [
  331. {
  332. type: 'text',
  333. oldText: 'foobar',
  334. newText: '\u00A0foobar',
  335. node: viewRoot.getChild( 0 ).getChild( 0 )
  336. }
  337. ] );
  338. expect( getModelData( model ) ).to.equal( '<paragraph> []foobar</paragraph>' );
  339. expect( getViewData( view ) ).to.equal( '<p> {}foobar</p>' );
  340. } );
  341. it( 'should replace all &nbsp; with spaces', () => {
  342. model.enqueueChanges( () => {
  343. model.selection.setRanges( [
  344. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
  345. ] );
  346. } );
  347. view.fire( 'mutations', [
  348. {
  349. type: 'text',
  350. oldText: 'foobar',
  351. newText: 'foobar\u00A0\u00A0\u00A0baz',
  352. node: viewRoot.getChild( 0 ).getChild( 0 )
  353. }
  354. ] );
  355. expect( getModelData( model ) ).to.equal( '<paragraph>foobar baz[]</paragraph>' );
  356. expect( getViewData( view ) ).to.equal( '<p>foobar baz{}</p>' );
  357. } );
  358. } );
  359. describe( 'keystroke handling', () => {
  360. it( 'should remove contents', () => {
  361. model.enqueueChanges( () => {
  362. model.selection.setRanges( [
  363. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  364. } );
  365. listenter.listenTo( view, 'keydown', () => {
  366. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  367. }, { priority: 'lowest' } );
  368. } );
  369. // #97
  370. it( 'should remove contents and merge blocks', () => {
  371. setModelData( model, '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
  372. listenter.listenTo( view, 'keydown', () => {
  373. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  374. view.fire( 'mutations', [
  375. {
  376. type: 'text',
  377. oldText: 'foar',
  378. newText: 'foyar',
  379. node: viewRoot.getChild( 0 ).getChild( 0 )
  380. }
  381. ] );
  382. }, { priority: 'lowest' } );
  383. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  384. expect( getModelData( model ) ).to.equal( '<paragraph>foy[]ar</paragraph>' );
  385. expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
  386. } );
  387. it( 'should do nothing on arrow key', () => {
  388. model.enqueueChanges( () => {
  389. model.selection.setRanges( [
  390. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  391. } );
  392. view.fire( 'keydown', { keyCode: getCode( 'arrowdown' ) } );
  393. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  394. } );
  395. it( 'should do nothing on ctrl combinations', () => {
  396. model.enqueueChanges( () => {
  397. model.selection.setRanges( [
  398. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  399. } );
  400. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  401. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  402. } );
  403. it( 'should do nothing on non printable keys', () => {
  404. model.enqueueChanges( () => {
  405. model.selection.setRanges( [
  406. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  407. } );
  408. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  409. view.fire( 'keydown', { keyCode: 35 } ); // Home
  410. view.fire( 'keydown', { keyCode: 112 } ); // F1
  411. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  412. } );
  413. // #69
  414. it( 'should do nothing on tab key', () => {
  415. model.enqueueChanges( () => {
  416. model.selection.setRanges( [
  417. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  418. } );
  419. view.fire( 'keydown', { keyCode: 9 } ); // Tab
  420. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  421. } );
  422. // #82
  423. it( 'should do nothing on composition start key', () => {
  424. model.enqueueChanges( () => {
  425. model.selection.setRanges( [
  426. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  427. } );
  428. view.fire( 'keydown', { keyCode: 229 } );
  429. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  430. } );
  431. it( 'should do nothing if selection is collapsed', () => {
  432. view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
  433. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  434. } );
  435. it( 'should lock buffer if selection is not collapsed', () => {
  436. const buffer = editor.commands.get( 'input' )._buffer;
  437. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  438. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  439. model.enqueueChanges( () => {
  440. model.selection.setRanges( [
  441. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
  442. } );
  443. view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
  444. expect( lockSpy.calledOnce ).to.be.true;
  445. expect( unlockSpy.calledOnce ).to.be.true;
  446. } );
  447. it( 'should not lock buffer on non printable keys', () => {
  448. const buffer = editor.commands.get( 'input' )._buffer;
  449. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  450. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  451. view.fire( 'keydown', { keyCode: 16 } ); // Shift
  452. view.fire( 'keydown', { keyCode: 35 } ); // Home
  453. view.fire( 'keydown', { keyCode: 112 } ); // F1
  454. expect( lockSpy.callCount ).to.be.equal( 0 );
  455. expect( unlockSpy.callCount ).to.be.equal( 0 );
  456. } );
  457. it( 'should not lock buffer on collapsed selection', () => {
  458. const buffer = editor.commands.get( 'input' )._buffer;
  459. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  460. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  461. view.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  462. view.fire( 'keydown', { keyCode: getCode( 'a' ) } );
  463. view.fire( 'keydown', { keyCode: getCode( 'z' ) } );
  464. expect( lockSpy.callCount ).to.be.equal( 0 );
  465. expect( unlockSpy.callCount ).to.be.equal( 0 );
  466. } );
  467. it( 'should not modify document when input command is disabled and selection is collapsed', () => {
  468. setModelData( model, '<paragraph>foo[]bar</paragraph>' );
  469. editor.commands.get( 'input' ).isEnabled = false;
  470. view.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  471. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  472. } );
  473. it( 'should not modify document when input command is disabled and selection is non-collapsed', () => {
  474. setModelData( model, '<paragraph>fo[ob]ar</paragraph>' );
  475. editor.commands.get( 'input' ).isEnabled = false;
  476. view.fire( 'keydown', { keyCode: getCode( 'b' ) } );
  477. expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  478. } );
  479. } );
  480. // NOTE: In all these tests we need to simulate the mutations. However, it's really tricky to tell what
  481. // should be in "newChildren" because we don't have yet access to these nodes. We pass new instances,
  482. // but this means that DomConverter which is used somewhere internally may return a different instance
  483. // (which wouldn't happen in practice because it'd cache it). Besides, it's really hard to tell if the
  484. // browser will keep the instances of the old elements when modifying the tree when the user is typing
  485. // or if it will create new instances itself too.
  486. // However, the code handling these mutations doesn't really care what's inside new/old children. It
  487. // just needs the mutations common ancestor to understand how big fragment of the tree has changed.
  488. describe( '#100', () => {
  489. let domElement, domRoot;
  490. beforeEach( () => {
  491. domElement = document.createElement( 'div' );
  492. document.body.appendChild( domElement );
  493. return ClassicTestEditor.create( domElement, { plugins: [ Input, Paragraph, Bold, Italic, LinkEngine ] } )
  494. .then( newEditor => {
  495. editor = newEditor;
  496. model = editor.document;
  497. modelRoot = model.getRoot();
  498. view = editor.editing.view;
  499. viewRoot = view.getRoot();
  500. domRoot = view.getDomRoot();
  501. // Mock image feature.
  502. newEditor.document.schema.registerItem( 'image', '$inline' );
  503. buildModelConverter().for( newEditor.data.modelToView, newEditor.editing.modelToView )
  504. .fromElement( 'image' )
  505. .toElement( 'img' );
  506. buildViewConverter().for( newEditor.data.viewToModel )
  507. .fromElement( 'img' )
  508. .toElement( 'image' );
  509. // Disable MO completely and in a way it won't be reenabled on some Document#render() call.
  510. const mutationObserver = view.getObserver( MutationObserver );
  511. mutationObserver.disable();
  512. mutationObserver.enable = () => {};
  513. } );
  514. } );
  515. afterEach( () => {
  516. domElement.remove();
  517. return editor.destroy();
  518. } );
  519. // This happens when browser automatically switches parent and child nodes.
  520. it( 'should handle mutations switching inner and outer node when adding new text node after', () => {
  521. setModelData( model,
  522. '<paragraph>' +
  523. '<$text italic="true" linkHref="foo">' +
  524. 'text[]' +
  525. '</$text>' +
  526. '</paragraph>'
  527. );
  528. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>text{}</i></a></p>' );
  529. const paragraph = viewRoot.getChild( 0 );
  530. const link = paragraph.getChild( 0 );
  531. const italic = link.getChild( 0 );
  532. const text = italic.getChild( 0 );
  533. // Simulate mutations and DOM change.
  534. domRoot.childNodes[ 0 ].innerHTML = '<i><a href="foo">text</a>x</i>';
  535. view.fire( 'mutations', [
  536. // First mutation - remove all children from link element.
  537. {
  538. type: 'children',
  539. node: link,
  540. oldChildren: [ italic ],
  541. newChildren: []
  542. },
  543. // Second mutation - remove link from paragraph and put italic there.
  544. {
  545. type: 'children',
  546. node: paragraph,
  547. oldChildren: [ link ],
  548. newChildren: [ new ViewElement( 'i' ) ]
  549. },
  550. // Third mutation - italic's new children.
  551. {
  552. type: 'children',
  553. node: italic,
  554. oldChildren: [ text ],
  555. newChildren: [ new ViewElement( 'a', null, text.clone() ), new ViewText( 'x' ) ]
  556. }
  557. ] );
  558. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>textx{}</i></a></p>' );
  559. } );
  560. it( 'should handle mutations switching inner and outer node when adding new text node before', () => {
  561. setModelData( model,
  562. '<paragraph>' +
  563. '<$text italic="true" linkHref="foo">' +
  564. '[]text' +
  565. '</$text>' +
  566. '</paragraph>'
  567. );
  568. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>{}text</i></a></p>' );
  569. const paragraph = viewRoot.getChild( 0 );
  570. const link = paragraph.getChild( 0 );
  571. const italic = link.getChild( 0 );
  572. const text = italic.getChild( 0 );
  573. // Simulate mutations and DOM change.
  574. domRoot.childNodes[ 0 ].innerHTML = '<i>x<a href="foo">text</a></i>';
  575. view.fire( 'mutations', [
  576. // First mutation - remove all children from link element.
  577. {
  578. type: 'children',
  579. node: link,
  580. oldChildren: [ italic ],
  581. newChildren: []
  582. },
  583. // Second mutation - remove link from paragraph and put italic there.
  584. {
  585. type: 'children',
  586. node: paragraph,
  587. oldChildren: [ link ],
  588. newChildren: [ new ViewElement( 'i' ) ]
  589. },
  590. // Third mutation - italic's new children.
  591. {
  592. type: 'children',
  593. node: italic,
  594. oldChildren: [ text ],
  595. newChildren: [ new ViewText( 'x' ), new ViewElement( 'a', null, 'text' ) ]
  596. }
  597. ] );
  598. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>x{}text</i></a></p>' );
  599. } );
  600. it( 'should handle mutations switching inner and outer node - with text before', () => {
  601. setModelData( model,
  602. '<paragraph>' +
  603. 'xxx<$text italic="true" linkHref="foo">' +
  604. 'text[]' +
  605. '</$text>' +
  606. '</paragraph>'
  607. );
  608. expect( getViewData( view ) ).to.equal( '<p>xxx<a href="foo"><i>text{}</i></a></p>' );
  609. const paragraph = viewRoot.getChild( 0 );
  610. const textBefore = paragraph.getChild( 0 );
  611. const link = paragraph.getChild( 1 );
  612. const italic = link.getChild( 0 );
  613. const text = italic.getChild( 0 );
  614. // Simulate mutations and DOM change.
  615. domRoot.childNodes[ 0 ].innerHTML = 'xxx<i><a href="foo">text</a>x</i>';
  616. view.fire( 'mutations', [
  617. // First mutation - remove all children from link element.
  618. {
  619. type: 'children',
  620. node: link,
  621. oldChildren: [ italic ],
  622. newChildren: []
  623. },
  624. // Second mutation - remove link from paragraph and put italic there.
  625. {
  626. type: 'children',
  627. node: paragraph,
  628. oldChildren: [ textBefore, link ],
  629. newChildren: [ new ViewText( 'xxx' ), new ViewElement( 'i' ) ]
  630. },
  631. // Third mutation - italic's new children.
  632. {
  633. type: 'children',
  634. node: italic,
  635. oldChildren: [ text ],
  636. newChildren: [ new ViewElement( 'a', null, 'text' ), new ViewText( 'x' ) ]
  637. }
  638. ] );
  639. expect( getViewData( view ) ).to.equal( '<p>xxx<a href="foo"><i>textx{}</i></a></p>' );
  640. } );
  641. // This happens when spell checker is applied on <strong> element and changes it to <b>.
  642. it( 'should handle mutations replacing node', () => {
  643. setModelData( model,
  644. '<paragraph>' +
  645. '<$text bold="true">' +
  646. 'text[]' +
  647. '</$text>' +
  648. '</paragraph>'
  649. );
  650. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  651. const paragraph = viewRoot.getChild( 0 );
  652. const strong = paragraph.getChild( 0 );
  653. // Simulate mutations and DOM change.
  654. domRoot.childNodes[ 0 ].innerHTML = '<b>fixed text</b>';
  655. view.fire( 'mutations', [
  656. // Replace `<strong>` with `<b>`.
  657. {
  658. type: 'children',
  659. node: paragraph,
  660. oldChildren: [ strong ],
  661. newChildren: [ new ViewElement( 'b', null, 'fixed text' ) ]
  662. }
  663. ] );
  664. expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '<p><strong>fixed text</strong></p>' );
  665. } );
  666. // Spell checker splits text inside attributes to two text nodes.
  667. it( 'should handle mutations inside attribute element', () => {
  668. setModelData( model,
  669. '<paragraph>' +
  670. '<$text bold="true">' +
  671. 'this is foo text[]' +
  672. '</$text>' +
  673. '</paragraph>'
  674. );
  675. expect( getViewData( view ) ).to.equal( '<p><strong>this is foo text{}</strong></p>' );
  676. const paragraph = viewRoot.getChild( 0 );
  677. const strong = paragraph.getChild( 0 );
  678. const text = strong.getChild( 0 );
  679. // Simulate mutations and DOM change.
  680. domRoot.childNodes[ 0 ].childNodes[ 0 ].innerHTML = 'this is bar text';
  681. view.fire( 'mutations', [
  682. {
  683. type: 'children',
  684. node: strong,
  685. oldChildren: [ text ],
  686. newChildren: [ new ViewText( 'this is bar' ), new ViewText( ' text' ) ]
  687. }
  688. ] );
  689. expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '<p><strong>this is bar text</strong></p>' );
  690. } );
  691. it( 'should do nothing if elements mutated', () => {
  692. setModelData( model,
  693. '<paragraph>' +
  694. '<$text bold="true">' +
  695. 'text[]' +
  696. '</$text>' +
  697. '</paragraph>'
  698. );
  699. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  700. const paragraph = viewRoot.getChild( 0 );
  701. const strong = paragraph.getChild( 0 );
  702. // Simulate mutations and DOM change.
  703. domRoot.childNodes[ 0 ].innerHTML = '<strong>text</strong><img />';
  704. view.fire( 'mutations', [
  705. {
  706. type: 'children',
  707. node: paragraph,
  708. oldChildren: [ strong ],
  709. newChildren: [
  710. new ViewElement( 'strong', null, new ViewText( 'text' ) ),
  711. new ViewElement( 'img' )
  712. ]
  713. }
  714. ] );
  715. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  716. } );
  717. it( 'should do nothing if text is not changed', () => {
  718. setModelData( model,
  719. '<paragraph>' +
  720. '<$text bold="true">' +
  721. 'text[]' +
  722. '</$text>' +
  723. '</paragraph>'
  724. );
  725. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  726. const paragraph = viewRoot.getChild( 0 );
  727. const strong = paragraph.getChild( 0 );
  728. // Simulate mutations and DOM change.
  729. domRoot.childNodes[ 0 ].innerHTML = '<strong>text</strong>';
  730. view.fire( 'mutations', [
  731. {
  732. type: 'children',
  733. node: paragraph,
  734. oldChildren: [ strong ],
  735. newChildren: [ new ViewElement( 'strong', null, new ViewText( 'text' ) ) ]
  736. }
  737. ] );
  738. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  739. } );
  740. it( 'should do nothing on empty mutations', () => {
  741. setModelData( model,
  742. '<paragraph>' +
  743. '<$text bold="true">' +
  744. 'text[]' +
  745. '</$text>' +
  746. '</paragraph>'
  747. );
  748. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  749. // Simulate mutations and DOM change.
  750. domRoot.childNodes[ 0 ].innerHTML = '<strong>text</strong>';
  751. view.fire( 'mutations', [] );
  752. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  753. } );
  754. it( 'should do nothing if mutations does not have common ancestor', () => {
  755. setModelData( model,
  756. '<paragraph>' +
  757. '<$text bold="true">' +
  758. 'text[]' +
  759. '</$text>' +
  760. '</paragraph>'
  761. );
  762. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  763. const paragraph = viewRoot.getChild( 0 );
  764. const strong = paragraph.getChild( 0 );
  765. // Simulate mutations and DOM change.
  766. domRoot.childNodes[ 0 ].innerHTML = '<strong>text</strong>';
  767. view.fire( 'mutations', [
  768. {
  769. type: 'children',
  770. node: paragraph,
  771. oldChildren: [ strong ],
  772. newChildren: [ strong ]
  773. },
  774. {
  775. type: 'children',
  776. node: new ViewContainerElement( 'div' ),
  777. oldChildren: [],
  778. newChildren: [ new ViewText( 'foo' ), new ViewText( 'bar' ) ]
  779. }
  780. ] );
  781. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  782. } );
  783. it( 'should handle view selection if one is returned from mutations', () => {
  784. setModelData( model,
  785. '<paragraph>' +
  786. '<$text bold="true">' +
  787. 'text[]' +
  788. '</$text>' +
  789. '</paragraph>'
  790. );
  791. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  792. const paragraph = viewRoot.getChild( 0 );
  793. const strong = paragraph.getChild( 0 );
  794. const viewSelection = new ViewSelection();
  795. viewSelection.setCollapsedAt( paragraph, 0 );
  796. // Simulate mutations and DOM change.
  797. domRoot.childNodes[ 0 ].innerHTML = '<b>textx</b>';
  798. view.fire( 'mutations', [
  799. // Replace `<strong>` with `<b>`.
  800. {
  801. type: 'children',
  802. node: paragraph,
  803. oldChildren: [ strong ],
  804. newChildren: [ new ViewElement( 'b', null, new ViewText( 'textx' ) ) ]
  805. }
  806. ], viewSelection );
  807. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">[]textx</$text></paragraph>' );
  808. expect( getViewData( view ) ).to.equal( '<p><strong>{}textx</strong></p>' );
  809. } );
  810. // #117.
  811. it( 'should handle mixed mutations', () => {
  812. setModelData( model, '<paragraph><$text bold="true">Foo bar aple</$text></paragraph>' );
  813. const paragraph = viewRoot.getChild( 0 );
  814. const strong = paragraph.getChild( 0 );
  815. const viewSelection = new ViewSelection();
  816. viewSelection.setCollapsedAt( paragraph, 0 );
  817. // Simulate mutations and DOM change.
  818. domRoot.childNodes[ 0 ].innerHTML = '<strong>Foo bar </strong><b>apple</b>';
  819. view.fire( 'mutations', [
  820. {
  821. type: 'text',
  822. oldText: 'Foo bar aple',
  823. newText: 'Foo bar ',
  824. node: viewRoot.getChild( 0 ).getChild( 0 )
  825. },
  826. {
  827. type: 'children',
  828. oldChildren: [ strong ],
  829. newChildren: [ strong, new ViewElement( 'b', null, new ViewText( 'apple' ) ) ],
  830. node: paragraph
  831. }
  832. ], viewSelection );
  833. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">[]Foo bar apple</$text></paragraph>' );
  834. expect( getViewData( view ) ).to.equal( '<p><strong>{}Foo bar apple</strong></p>' );
  835. } );
  836. } );
  837. } );