widget.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import Widget from '../src/widget';
  7. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  8. import MouseObserver from '@ckeditor/ckeditor5-engine/src/view/observer/mouseobserver';
  9. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  10. import { toWidget } from '../src/utils';
  11. import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
  12. import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
  13. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  15. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  16. /* global document */
  17. describe( 'Widget', () => {
  18. let editor, model, view, viewDocument;
  19. beforeEach( () => {
  20. return VirtualTestEditor.create( { plugins: [ Widget, Typing ] } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. view = editor.editing.view;
  25. viewDocument = view.document;
  26. model.schema.register( 'widget', {
  27. inheritAllFrom: '$block',
  28. isObject: true
  29. } );
  30. model.schema.register( 'paragraph', {
  31. inheritAllFrom: '$block',
  32. allowIn: 'div'
  33. } );
  34. model.schema.register( 'inline', {
  35. allowWhere: '$text',
  36. isObject: true
  37. } );
  38. model.schema.register( 'nested', {
  39. allowIn: 'widget',
  40. isLimit: true
  41. } );
  42. model.schema.extend( '$text', {
  43. allowIn: [ 'nested', 'editable' ]
  44. } );
  45. model.schema.register( 'editable', {
  46. allowIn: [ 'widget', '$root' ]
  47. } );
  48. // Image feature.
  49. model.schema.register( 'image', {
  50. allowIn: '$root',
  51. isObject: true,
  52. isBlock: true
  53. } );
  54. // Block-quote feature.
  55. model.schema.register( 'blockQuote', {
  56. allowIn: '$root'
  57. } );
  58. model.schema.extend( '$block', { allowIn: 'blockQuote' } );
  59. // Div element which helps nesting elements.
  60. model.schema.register( 'div', {
  61. allowIn: [ 'blockQuote', 'div' ]
  62. } );
  63. editor.conversion.for( 'downcast' )
  64. .add( downcastElementToElement( { model: 'paragraph', view: 'p' } ) )
  65. .add( downcastElementToElement( { model: 'inline', view: 'figure' } ) )
  66. .add( downcastElementToElement( { model: 'image', view: 'img' } ) )
  67. .add( downcastElementToElement( { model: 'blockQuote', view: 'blockquote' } ) )
  68. .add( downcastElementToElement( { model: 'div', view: 'div' } ) )
  69. .add( downcastElementToElement( {
  70. model: 'widget',
  71. view: ( modelItem, viewWriter ) => {
  72. const b = viewWriter.createAttributeElement( 'b' );
  73. const div = viewWriter.createContainerElement( 'div' );
  74. viewWriter.insert( ViewPosition.createAt( div ), b );
  75. return toWidget( div, viewWriter, { label: 'element label' } );
  76. }
  77. } ) )
  78. .add( downcastElementToElement( {
  79. model: 'nested',
  80. view: ( modelItem, viewWriter ) => viewWriter.createEditableElement( 'figcaption', { contenteditable: true } )
  81. } ) )
  82. .add( downcastElementToElement( {
  83. model: 'editable',
  84. view: ( modelItem, viewWriter ) => viewWriter.createEditableElement( 'figcaption', { contenteditable: true } )
  85. } ) );
  86. } );
  87. } );
  88. it( 'should be loaded', () => {
  89. expect( editor.plugins.get( Widget ) ).to.be.instanceOf( Widget );
  90. } );
  91. it( 'should add MouseObserver', () => {
  92. expect( view.getObserver( MouseObserver ) ).to.be.instanceof( MouseObserver );
  93. } );
  94. it( 'should create selection over clicked widget', () => {
  95. setModelData( model, '[]<widget></widget>' );
  96. const viewDiv = viewDocument.getRoot().getChild( 0 );
  97. const domEventDataMock = {
  98. target: viewDiv,
  99. preventDefault: sinon.spy()
  100. };
  101. viewDocument.fire( 'mousedown', domEventDataMock );
  102. expect( getModelData( model ) ).to.equal( '[<widget></widget>]' );
  103. sinon.assert.calledOnce( domEventDataMock.preventDefault );
  104. } );
  105. it( 'should create selection when clicked in nested element', () => {
  106. setModelData( model, '[]<widget></widget>' );
  107. const viewDiv = viewDocument.getRoot().getChild( 0 );
  108. const viewB = viewDiv.getChild( 0 );
  109. const domEventDataMock = {
  110. target: viewB,
  111. preventDefault: sinon.spy()
  112. };
  113. viewDocument.fire( 'mousedown', domEventDataMock );
  114. expect( getModelData( model ) ).to.equal( '[<widget></widget>]' );
  115. sinon.assert.calledOnce( domEventDataMock.preventDefault );
  116. } );
  117. it( 'should do nothing if clicked inside nested editable', () => {
  118. setModelData( model, '[]<widget><nested>foo bar</nested></widget>' );
  119. const viewDiv = viewDocument.getRoot().getChild( 0 );
  120. const viewFigcaption = viewDiv.getChild( 0 );
  121. const domEventDataMock = {
  122. target: viewFigcaption,
  123. preventDefault: sinon.spy()
  124. };
  125. viewDocument.fire( 'mousedown', domEventDataMock );
  126. sinon.assert.notCalled( domEventDataMock.preventDefault );
  127. } );
  128. it( 'should do nothing if clicked in non-widget element', () => {
  129. setModelData( model, '<paragraph>[]foo bar</paragraph><widget></widget>' );
  130. const viewP = viewDocument.getRoot().getChild( 0 );
  131. const domEventDataMock = {
  132. target: viewP,
  133. preventDefault: sinon.spy()
  134. };
  135. view.focus();
  136. viewDocument.fire( 'mousedown', domEventDataMock );
  137. expect( getModelData( model ) ).to.equal( '<paragraph>[]foo bar</paragraph><widget></widget>' );
  138. sinon.assert.notCalled( domEventDataMock.preventDefault );
  139. } );
  140. it( 'should not focus editable if already is focused', () => {
  141. setModelData( model, '<widget></widget>' );
  142. const widget = viewDocument.getRoot().getChild( 0 );
  143. const domEventDataMock = {
  144. target: widget,
  145. preventDefault: sinon.spy()
  146. };
  147. const focusSpy = sinon.spy( view, 'focus' );
  148. viewDocument.isFocused = true;
  149. viewDocument.fire( 'mousedown', domEventDataMock );
  150. sinon.assert.calledOnce( domEventDataMock.preventDefault );
  151. sinon.assert.notCalled( focusSpy );
  152. expect( getModelData( model ) ).to.equal( '[<widget></widget>]' );
  153. } );
  154. it( 'should apply fake view selection if model selection is on widget element', () => {
  155. setModelData( model, '[<widget>foo bar</widget>]' );
  156. expect( getViewData( view ) ).to.equal(
  157. '[<div class="ck-widget ck-widget_selected" contenteditable="false">foo bar<b></b></div>]'
  158. );
  159. expect( viewDocument.selection.isFake ).to.be.true;
  160. } );
  161. it( 'should use element\'s label to set fake selection if one is provided', () => {
  162. setModelData( model, '[<widget>foo bar</widget>]' );
  163. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'element label' );
  164. } );
  165. it( 'should add selected class when no only a widget is selected', () => {
  166. setModelData( model, '[<paragraph>foo</paragraph><widget></widget><widget></widget>]' );
  167. expect( viewDocument.selection.isFake ).to.be.false;
  168. expect( getViewData( view ) ).to.equal(
  169. '[' +
  170. '<p>foo</p>' +
  171. '<div class="ck-widget ck-widget_selected" contenteditable="false"><b></b></div>' +
  172. '<div class="ck-widget ck-widget_selected" contenteditable="false"><b></b></div>' +
  173. ']'
  174. );
  175. } );
  176. it( 'fake selection should be empty if widget is not selected', () => {
  177. setModelData( model, '<paragraph>foo</paragraph><widget>foo bar</widget>' );
  178. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( '' );
  179. } );
  180. it( 'should toggle selected class', () => {
  181. setModelData( model, '<paragraph>foo</paragraph>[<widget>foo</widget>]' );
  182. expect( getViewData( view ) ).to.equal(
  183. '<p>foo</p>[<div class="ck-widget ck-widget_selected" contenteditable="false">foo<b></b></div>]'
  184. );
  185. model.change( writer => {
  186. writer.setSelection( null );
  187. } );
  188. expect( getViewData( view ) ).to.equal(
  189. '<p>{}foo</p><div class="ck-widget" contenteditable="false">foo<b></b></div>'
  190. );
  191. } );
  192. it( 'should do nothing when selection is placed in other editable', () => {
  193. setModelData( model, '<widget><editable>foo bar</editable></widget><editable>[baz]</editable>' );
  194. expect( getViewData( view ) ).to.equal(
  195. '<div class="ck-widget" contenteditable="false">' +
  196. '<figcaption contenteditable="true">foo bar</figcaption>' +
  197. '<b></b>' +
  198. '</div>' +
  199. '<figcaption contenteditable="true">{baz}</figcaption>'
  200. );
  201. } );
  202. describe( 'keys handling', () => {
  203. describe( 'arrows', () => {
  204. test(
  205. 'should move selection forward from selected object - right arrow',
  206. '[<widget></widget>]<paragraph>foo</paragraph>',
  207. keyCodes.arrowright,
  208. '<widget></widget><paragraph>[]foo</paragraph>'
  209. );
  210. test(
  211. 'should move selection forward from selected object - down arrow',
  212. '[<widget></widget>]<paragraph>foo</paragraph>',
  213. keyCodes.arrowdown,
  214. '<widget></widget><paragraph>[]foo</paragraph>'
  215. );
  216. test(
  217. 'should move selection backward from selected object - left arrow',
  218. '<paragraph>foo</paragraph>[<widget></widget>]',
  219. keyCodes.arrowleft,
  220. '<paragraph>foo[]</paragraph><widget></widget>'
  221. );
  222. test(
  223. 'should move selection backward from selected object - up arrow',
  224. '<paragraph>foo</paragraph>[<widget></widget>]',
  225. keyCodes.arrowup,
  226. '<paragraph>foo[]</paragraph><widget></widget>'
  227. );
  228. test(
  229. 'should move selection to next widget - right arrow',
  230. '[<widget></widget>]<widget></widget>',
  231. keyCodes.arrowright,
  232. '<widget></widget>[<widget></widget>]'
  233. );
  234. test(
  235. 'should move selection to next widget - down arrow',
  236. '[<widget></widget>]<widget></widget>',
  237. keyCodes.arrowdown,
  238. '<widget></widget>[<widget></widget>]'
  239. );
  240. test(
  241. 'should move selection to previous widget - left arrow',
  242. '<widget></widget>[<widget></widget>]',
  243. keyCodes.arrowleft,
  244. '[<widget></widget>]<widget></widget>'
  245. );
  246. test(
  247. 'should move selection to previous widget - up arrow',
  248. '<widget></widget>[<widget></widget>]',
  249. keyCodes.arrowup,
  250. '[<widget></widget>]<widget></widget>'
  251. );
  252. test(
  253. 'should do nothing on non-collapsed selection next to object - right arrow',
  254. '<paragraph>ba[r]</paragraph><widget></widget>',
  255. keyCodes.arrowright,
  256. '<paragraph>ba[r]</paragraph><widget></widget>'
  257. );
  258. test(
  259. 'should do nothing on non-collapsed selection next to object - down arrow',
  260. '<paragraph>ba[r]</paragraph><widget></widget>',
  261. keyCodes.arrowdown,
  262. '<paragraph>ba[r]</paragraph><widget></widget>'
  263. );
  264. test(
  265. 'should do nothing on non-collapsed selection next to object - left arrow',
  266. '<widget></widget><paragraph>[b]ar</paragraph>',
  267. keyCodes.arrowleft,
  268. '<widget></widget><paragraph>[b]ar</paragraph>'
  269. );
  270. test(
  271. 'should do nothing on non-collapsed selection next to object - up arrow',
  272. '<widget></widget><paragraph>[b]ar</paragraph>',
  273. keyCodes.arrowup,
  274. '<widget></widget><paragraph>[b]ar</paragraph>'
  275. );
  276. test(
  277. 'should not move selection if there is no correct location - right arrow',
  278. '<paragraph>foo</paragraph>[<widget></widget>]',
  279. keyCodes.arrowright,
  280. '<paragraph>foo</paragraph>[<widget></widget>]'
  281. );
  282. test(
  283. 'should not move selection if there is no correct location - down arrow',
  284. '<paragraph>foo</paragraph>[<widget></widget>]',
  285. keyCodes.arrowdown,
  286. '<paragraph>foo</paragraph>[<widget></widget>]'
  287. );
  288. test(
  289. 'should not move selection if there is no correct location - left arrow',
  290. '[<widget></widget>]<paragraph>foo</paragraph>',
  291. keyCodes.arrowleft,
  292. '[<widget></widget>]<paragraph>foo</paragraph>'
  293. );
  294. test(
  295. 'should not move selection if there is no correct location - up arrow',
  296. '[<widget></widget>]<paragraph>foo</paragraph>',
  297. keyCodes.arrowup,
  298. '[<widget></widget>]<paragraph>foo</paragraph>'
  299. );
  300. test(
  301. 'should do nothing if other key is pressed',
  302. '[<widget></widget>]<paragraph>foo</paragraph>',
  303. // Use a safe key (alt) to not trigger the Input features "unsafe keys" handler.
  304. 18,
  305. '[<widget></widget>]<paragraph>foo</paragraph>'
  306. );
  307. it( 'should prevent default behaviour when there is no correct location - document end', () => {
  308. const keydownHandler = sinon.spy();
  309. const domEventDataMock = {
  310. keyCode: keyCodes.arrowright,
  311. preventDefault: sinon.spy(),
  312. };
  313. setModelData( model, '<paragraph>foo</paragraph>[<widget></widget>]' );
  314. viewDocument.on( 'keydown', keydownHandler );
  315. viewDocument.fire( 'keydown', domEventDataMock );
  316. expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<widget></widget>]' );
  317. sinon.assert.calledOnce( domEventDataMock.preventDefault );
  318. sinon.assert.notCalled( keydownHandler );
  319. } );
  320. it( 'should prevent default behaviour when there is no correct location - document start', () => {
  321. const keydownHandler = sinon.spy();
  322. const domEventDataMock = {
  323. keyCode: keyCodes.arrowleft,
  324. preventDefault: sinon.spy(),
  325. };
  326. setModelData( model, '[<widget></widget>]<paragraph>foo</paragraph>' );
  327. viewDocument.on( 'keydown', keydownHandler );
  328. viewDocument.fire( 'keydown', domEventDataMock );
  329. expect( getModelData( model ) ).to.equal( '[<widget></widget>]<paragraph>foo</paragraph>' );
  330. sinon.assert.calledOnce( domEventDataMock.preventDefault );
  331. sinon.assert.notCalled( keydownHandler );
  332. } );
  333. test(
  334. 'should move selection to object element - right arrow',
  335. '<paragraph>foo[]</paragraph><widget></widget>',
  336. keyCodes.arrowright,
  337. '<paragraph>foo</paragraph>[<widget></widget>]'
  338. );
  339. test(
  340. 'should move selection to object element - down arrow',
  341. '<paragraph>foo[]</paragraph><widget></widget>',
  342. keyCodes.arrowdown,
  343. '<paragraph>foo</paragraph>[<widget></widget>]'
  344. );
  345. test(
  346. 'should move selection to object element - left arrow',
  347. '<widget></widget><paragraph>[]foo</paragraph>',
  348. keyCodes.arrowleft,
  349. '[<widget></widget>]<paragraph>foo</paragraph>'
  350. );
  351. test(
  352. 'should move selection to object element - up arrow',
  353. '<widget></widget><paragraph>[]foo</paragraph>',
  354. keyCodes.arrowup,
  355. '[<widget></widget>]<paragraph>foo</paragraph>'
  356. );
  357. test(
  358. 'do nothing on non objects - right arrow',
  359. '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>',
  360. keyCodes.arrowright,
  361. '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>'
  362. );
  363. test(
  364. 'do nothing on non objects - down arrow',
  365. '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>',
  366. keyCodes.arrowdown,
  367. '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>'
  368. );
  369. test(
  370. 'do nothing on non objects - left arrow',
  371. '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>',
  372. keyCodes.arrowleft,
  373. '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>'
  374. );
  375. test(
  376. 'do nothing on non objects - up arrow',
  377. '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>',
  378. keyCodes.arrowup,
  379. '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>'
  380. );
  381. test(
  382. 'should work correctly with modifier key: right arrow + ctrl',
  383. '[<widget></widget>]<paragraph>foo</paragraph>',
  384. { keyCode: keyCodes.arrowright, ctrlKey: true },
  385. '<widget></widget><paragraph>[]foo</paragraph>'
  386. );
  387. test(
  388. 'should work correctly with modifier key: right arrow + alt',
  389. '[<widget></widget>]<paragraph>foo</paragraph>',
  390. { keyCode: keyCodes.arrowright, altKey: true },
  391. '<widget></widget><paragraph>[]foo</paragraph>'
  392. );
  393. test(
  394. 'should work correctly with modifier key: right arrow + shift',
  395. '[<widget></widget>]<paragraph>foo</paragraph>',
  396. { keyCode: keyCodes.arrowright, shiftKey: true },
  397. '<widget></widget><paragraph>[]foo</paragraph>'
  398. );
  399. test(
  400. 'should work correctly with modifier key: down arrow + ctrl',
  401. '[<widget></widget>]<paragraph>foo</paragraph>',
  402. { keyCode: keyCodes.arrowdown, ctrlKey: true },
  403. '<widget></widget><paragraph>[]foo</paragraph>'
  404. );
  405. test(
  406. 'should work correctly with modifier key: down arrow + alt',
  407. '[<widget></widget>]<paragraph>foo</paragraph>',
  408. { keyCode: keyCodes.arrowdown, altKey: true },
  409. '<widget></widget><paragraph>[]foo</paragraph>'
  410. );
  411. test(
  412. 'should work correctly with modifier key: down arrow + shift',
  413. '[<widget></widget>]<paragraph>foo</paragraph>',
  414. { keyCode: keyCodes.arrowdown, shiftKey: true },
  415. '<widget></widget><paragraph>[]foo</paragraph>'
  416. );
  417. test(
  418. 'should work correctly with modifier key: left arrow + ctrl',
  419. '<paragraph>foo</paragraph>[<widget></widget>]',
  420. { keyCode: keyCodes.arrowleft, ctrlKey: true },
  421. '<paragraph>foo[]</paragraph><widget></widget>'
  422. );
  423. test(
  424. 'should work correctly with modifier key: left arrow + alt',
  425. '<paragraph>foo</paragraph>[<widget></widget>]',
  426. { keyCode: keyCodes.arrowleft, altKey: true },
  427. '<paragraph>foo[]</paragraph><widget></widget>'
  428. );
  429. test(
  430. 'should work correctly with modifier key: left arrow + shift',
  431. '<paragraph>foo</paragraph>[<widget></widget>]',
  432. { keyCode: keyCodes.arrowleft, shiftKey: true },
  433. '<paragraph>foo[]</paragraph><widget></widget>'
  434. );
  435. test(
  436. 'should work correctly with modifier key: up arrow + ctrl',
  437. '<paragraph>foo</paragraph>[<widget></widget>]',
  438. { keyCode: keyCodes.arrowup, ctrlKey: true },
  439. '<paragraph>foo[]</paragraph><widget></widget>'
  440. );
  441. test(
  442. 'should work correctly with modifier key: up arrow + alt',
  443. '<paragraph>foo</paragraph>[<widget></widget>]',
  444. { keyCode: keyCodes.arrowup, altKey: true },
  445. '<paragraph>foo[]</paragraph><widget></widget>'
  446. );
  447. test(
  448. 'should work correctly with modifier key: up arrow + shift',
  449. '<paragraph>foo</paragraph>[<widget></widget>]',
  450. { keyCode: keyCodes.arrowup, shiftKey: true },
  451. '<paragraph>foo[]</paragraph><widget></widget>'
  452. );
  453. test(
  454. 'should do nothing if there is more than one selection in model',
  455. '<paragraph>[foo]</paragraph><widget></widget><paragraph>[bar]</paragraph>',
  456. keyCodes.arrowright,
  457. '<paragraph>[foo]</paragraph><widget></widget><paragraph>[bar]</paragraph>'
  458. );
  459. test(
  460. 'should work if selection is in nested element (left arrow)',
  461. '<paragraph>foo</paragraph>' +
  462. '<image></image>' +
  463. '<blockQuote>' +
  464. '<div>' +
  465. '<div>' +
  466. '<paragraph>[]</paragraph>' +
  467. '</div>' +
  468. '</div>' +
  469. '</blockQuote>' +
  470. '<paragraph>foo</paragraph>',
  471. keyCodes.arrowleft,
  472. '<paragraph>foo</paragraph>' +
  473. '[<image></image>]' +
  474. '<blockQuote>' +
  475. '<div>' +
  476. '<div>' +
  477. '<paragraph></paragraph>' +
  478. '</div>' +
  479. '</div>' +
  480. '</blockQuote>' +
  481. '<paragraph>foo</paragraph>'
  482. );
  483. test(
  484. 'should work if selection is in nested element (up arrow)',
  485. '<paragraph>foo</paragraph>' +
  486. '<image></image>' +
  487. '<blockQuote>' +
  488. '<div>' +
  489. '<div>' +
  490. '<paragraph>[]</paragraph>' +
  491. '</div>' +
  492. '</div>' +
  493. '</blockQuote>' +
  494. '<paragraph>foo</paragraph>',
  495. keyCodes.arrowup,
  496. '<paragraph>foo</paragraph>' +
  497. '[<image></image>]' +
  498. '<blockQuote>' +
  499. '<div>' +
  500. '<div>' +
  501. '<paragraph></paragraph>' +
  502. '</div>' +
  503. '</div>' +
  504. '</blockQuote>' +
  505. '<paragraph>foo</paragraph>'
  506. );
  507. test(
  508. 'should work if selection is in nested element (right arrow)',
  509. '<paragraph>foo</paragraph>' +
  510. '<blockQuote>' +
  511. '<div>' +
  512. '<div>' +
  513. '<paragraph>[]</paragraph>' +
  514. '</div>' +
  515. '</div>' +
  516. '</blockQuote>' +
  517. '<image></image>' +
  518. '<paragraph>foo</paragraph>',
  519. keyCodes.arrowright,
  520. '<paragraph>foo</paragraph>' +
  521. '<blockQuote>' +
  522. '<div>' +
  523. '<div>' +
  524. '<paragraph></paragraph>' +
  525. '</div>' +
  526. '</div>' +
  527. '</blockQuote>' +
  528. '[<image></image>]' +
  529. '<paragraph>foo</paragraph>'
  530. );
  531. test(
  532. 'should work if selection is in nested element (down arrow)',
  533. '<paragraph>foo</paragraph>' +
  534. '<blockQuote>' +
  535. '<div>' +
  536. '<div>' +
  537. '<paragraph>[]</paragraph>' +
  538. '</div>' +
  539. '</div>' +
  540. '</blockQuote>' +
  541. '<image></image>' +
  542. '<paragraph>foo</paragraph>',
  543. keyCodes.arrowdown,
  544. '<paragraph>foo</paragraph>' +
  545. '<blockQuote>' +
  546. '<div>' +
  547. '<div>' +
  548. '<paragraph></paragraph>' +
  549. '</div>' +
  550. '</div>' +
  551. '</blockQuote>' +
  552. '[<image></image>]' +
  553. '<paragraph>foo</paragraph>'
  554. );
  555. } );
  556. describe( 'Ctrl+A', () => {
  557. test(
  558. 'should select the entire content of the nested editable',
  559. '<widget><nested>foo[]</nested></widget><paragraph>bar</paragraph>',
  560. { keyCode: keyCodes.a, ctrlKey: true },
  561. '<widget><nested>[foo]</nested></widget><paragraph>bar</paragraph>'
  562. );
  563. test(
  564. 'should not change the selection if outside of the nested editable',
  565. '<widget><nested>foo</nested></widget><paragraph>[]bar</paragraph>',
  566. { keyCode: keyCodes.a, ctrlKey: true },
  567. '<widget><nested>foo</nested></widget><paragraph>[]bar</paragraph>'
  568. );
  569. test(
  570. 'should selected whole content when widget is selected',
  571. '<paragraph>foo</paragraph>[<widget></widget>]<paragraph>bar</paragraph>',
  572. { keyCode: keyCodes.a, ctrlKey: true },
  573. '[<paragraph>foo</paragraph><widget></widget><paragraph>bar</paragraph>]',
  574. '[<p>foo</p><div class="ck-widget ck-widget_selected" contenteditable="false"><b></b></div><p>bar</p>]'
  575. );
  576. } );
  577. describe( 'enter', () => {
  578. test(
  579. 'should insert a paragraph after the selected widget upon Enter',
  580. '[<widget></widget>]',
  581. keyCodes.enter,
  582. '<widget></widget><paragraph>[]</paragraph>'
  583. );
  584. test(
  585. 'should insert a paragraph before the selected widget upon Shift+Enter',
  586. '[<widget></widget>]',
  587. { keyCode: keyCodes.enter, shiftKey: true },
  588. '<paragraph>[]</paragraph><widget></widget>'
  589. );
  590. test(
  591. 'should insert a paragraph when not a first-child of the root',
  592. '[<widget></widget>]<paragraph>foo</paragraph>',
  593. keyCodes.enter,
  594. '<widget></widget><paragraph>[]</paragraph><paragraph>foo</paragraph>'
  595. );
  596. test(
  597. 'should insert a paragraph when not a last-child of the root',
  598. '<paragraph>foo</paragraph>[<widget></widget>]',
  599. { keyCode: keyCodes.enter, shiftKey: true },
  600. '<paragraph>foo</paragraph><paragraph>[]</paragraph><widget></widget>'
  601. );
  602. test(
  603. 'should insert a paragraph only when an entire widget is selected (#1)',
  604. '<widget><nested>[foo] bar</nested></widget>',
  605. keyCodes.enter,
  606. '<widget><nested>[] bar</nested></widget>'
  607. );
  608. test(
  609. 'should insert a paragraph only when an entire widget is selected (#2)',
  610. '<paragraph>f[oo</paragraph><widget></widget><paragraph>b]ar</paragraph>',
  611. keyCodes.enter,
  612. '<paragraph>f[]ar</paragraph>'
  613. );
  614. } );
  615. function test( name, data, keyCodeOrMock, expected, expectedView ) {
  616. it( name, () => {
  617. const domEventDataMock = ( typeof keyCodeOrMock == 'object' ) ? keyCodeOrMock : {
  618. keyCode: keyCodeOrMock
  619. };
  620. setModelData( model, data );
  621. viewDocument.fire( 'keydown', new DomEventData(
  622. viewDocument,
  623. { target: document.createElement( 'div' ), preventDefault() {} },
  624. domEventDataMock
  625. ) );
  626. expect( getModelData( model ) ).to.equal( expected );
  627. if ( expectedView ) {
  628. expect( getViewData( view ) ).to.equal( expectedView );
  629. }
  630. } );
  631. }
  632. } );
  633. describe( 'delete integration', () => {
  634. function test( name, input, direction, expected ) {
  635. it( name, () => {
  636. setModelData( model, input );
  637. const scrollStub = sinon.stub( view, 'scrollToTheSelection' );
  638. const domEventDataMock = {
  639. keyCode: direction == 'backward' ? keyCodes.backspace : keyCodes.delete,
  640. };
  641. viewDocument.fire( 'keydown', new DomEventData(
  642. viewDocument,
  643. { target: document.createElement( 'div' ), preventDefault() {} },
  644. domEventDataMock
  645. ) );
  646. expect( getModelData( model ) ).to.equal( expected );
  647. scrollStub.restore();
  648. } );
  649. }
  650. // Let's make this integration tests real which will help covering
  651. // cases like https://github.com/ckeditor/ckeditor5/issues/753.
  652. // Originally, this test file used the Delete feature only which was not "integrational" enough.
  653. it( 'tests are executed with the Typing feature', () => {
  654. expect( editor.plugins.get( 'Typing' ) ).to.not.be.undefined;
  655. } );
  656. test(
  657. 'should select widget when backspace is pressed',
  658. '<widget></widget><paragraph>[]foo</paragraph>',
  659. 'backward',
  660. '[<widget></widget>]<paragraph>foo</paragraph>'
  661. );
  662. test(
  663. 'should remove empty element after selecting widget when backspace is pressed',
  664. '<widget></widget><paragraph>[]</paragraph>',
  665. 'backward',
  666. '[<widget></widget>]'
  667. );
  668. test(
  669. 'should select widget when delete is pressed',
  670. '<paragraph>foo[]</paragraph><widget></widget>',
  671. 'forward',
  672. '<paragraph>foo</paragraph>[<widget></widget>]'
  673. );
  674. test(
  675. 'should remove empty element after selecting widget when delete is pressed',
  676. '<paragraph>[]</paragraph><widget></widget>',
  677. 'forward',
  678. '[<widget></widget>]'
  679. );
  680. test(
  681. 'should not select widget on non-collapsed selection',
  682. '<widget></widget><paragraph>[f]oo</paragraph>',
  683. 'backward',
  684. '<widget></widget><paragraph>[]oo</paragraph>'
  685. );
  686. test(
  687. 'should not affect non-object elements',
  688. '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>',
  689. 'backward',
  690. '<paragraph>foo[]bar</paragraph>'
  691. );
  692. test(
  693. 'should not modify backward delete default behaviour in single paragraph boundaries',
  694. '<paragraph>[]foo</paragraph>',
  695. 'backward',
  696. '<paragraph>[]foo</paragraph>'
  697. );
  698. test(
  699. 'should not modify forward delete default behaviour in single paragraph boundaries',
  700. '<paragraph>foo[]</paragraph>',
  701. 'forward',
  702. '<paragraph>foo[]</paragraph>'
  703. );
  704. test(
  705. 'should delete selected widget with paragraph before - backward',
  706. '<paragraph>foo</paragraph>[<widget></widget>]',
  707. 'backward',
  708. '<paragraph>foo</paragraph><paragraph>[]</paragraph>'
  709. );
  710. test(
  711. 'should delete selected widget with paragraph before - forward',
  712. '<paragraph>foo</paragraph>[<widget></widget>]',
  713. 'forward',
  714. '<paragraph>foo</paragraph><paragraph>[]</paragraph>'
  715. );
  716. test(
  717. 'should delete selected widget with paragraph after - backward',
  718. '[<widget></widget>]<paragraph>foo</paragraph>',
  719. 'backward',
  720. '<paragraph>[]</paragraph><paragraph>foo</paragraph>'
  721. );
  722. test(
  723. 'should delete selected widget with paragraph after - forward',
  724. '[<widget></widget>]<paragraph>foo</paragraph>',
  725. 'forward',
  726. '<paragraph>[]</paragraph><paragraph>foo</paragraph>'
  727. );
  728. test(
  729. 'should delete selected widget between paragraphs - backward',
  730. '<paragraph>bar</paragraph>[<widget></widget>]<paragraph>foo</paragraph>',
  731. 'backward',
  732. '<paragraph>bar</paragraph><paragraph>[]</paragraph><paragraph>foo</paragraph>'
  733. );
  734. test(
  735. 'should delete selected widget between paragraphs - forward',
  736. '<paragraph>bar</paragraph>[<widget></widget>]<paragraph>foo</paragraph>',
  737. 'forward',
  738. '<paragraph>bar</paragraph><paragraph>[]</paragraph><paragraph>foo</paragraph>'
  739. );
  740. test(
  741. 'should delete selected widget preceded by another widget - backward',
  742. '<widget></widget>[<widget></widget>]',
  743. 'backward',
  744. '<widget></widget><paragraph>[]</paragraph>'
  745. );
  746. test(
  747. 'should delete selected widget preceded by another widget - forward',
  748. '<widget></widget>[<widget></widget>]',
  749. 'forward',
  750. '<widget></widget><paragraph>[]</paragraph>'
  751. );
  752. test(
  753. 'should delete selected widget before another widget - forward',
  754. '[<widget></widget>]<widget></widget>',
  755. 'forward',
  756. '<paragraph>[]</paragraph><widget></widget>'
  757. );
  758. test(
  759. 'should delete selected widget before another widget - backward',
  760. '[<widget></widget>]<widget></widget>',
  761. 'backward',
  762. '<paragraph>[]</paragraph><widget></widget>'
  763. );
  764. test(
  765. 'should delete selected widget between other widgets - forward',
  766. '<widget></widget>[<widget></widget>]<widget></widget>',
  767. 'forward',
  768. '<widget></widget><paragraph>[]</paragraph><widget></widget>'
  769. );
  770. test(
  771. 'should delete selected widget between other widgets - backward',
  772. '<widget></widget>[<widget></widget>]<widget></widget>',
  773. 'backward',
  774. '<widget></widget><paragraph>[]</paragraph><widget></widget>'
  775. );
  776. test(
  777. 'should select inline objects - backward',
  778. '<paragraph>foo<inline></inline>[]bar</paragraph>',
  779. 'backward',
  780. '<paragraph>foo[<inline></inline>]bar</paragraph>'
  781. );
  782. test(
  783. 'should select inline objects - forward',
  784. '<paragraph>foo[]<inline></inline>bar</paragraph>',
  785. 'forward',
  786. '<paragraph>foo[<inline></inline>]bar</paragraph>'
  787. );
  788. test(
  789. 'should delete selected inline objects - backward',
  790. '<paragraph>foo[<inline></inline>]bar</paragraph>',
  791. 'backward',
  792. '<paragraph>foo[]bar</paragraph>'
  793. );
  794. test(
  795. 'should delete selected inline objects - forward',
  796. '<paragraph>foo[<inline></inline>]bar</paragraph>',
  797. 'forward',
  798. '<paragraph>foo[]bar</paragraph>'
  799. );
  800. test(
  801. 'should use standard delete behaviour when after first letter - backward',
  802. '<paragraph>a[]</paragraph>',
  803. 'backward',
  804. '<paragraph>[]</paragraph>'
  805. );
  806. test(
  807. 'should use standard delete behaviour when before first letter - forward',
  808. '<paragraph>[]a</paragraph>',
  809. 'forward',
  810. '<paragraph>[]</paragraph>'
  811. );
  812. it( 'should prevent default behaviour and stop event propagation', () => {
  813. setModelData( model, '<paragraph>foo[]</paragraph><widget></widget>' );
  814. const scrollStub = sinon.stub( view, 'scrollToTheSelection' );
  815. const deleteSpy = sinon.spy();
  816. viewDocument.on( 'delete', deleteSpy );
  817. const domEventDataMock = { target: document.createElement( 'div' ), preventDefault: sinon.spy() };
  818. viewDocument.fire( 'delete', new DomEventData(
  819. viewDocument,
  820. domEventDataMock,
  821. { direction: 'forward', unit: 'character', sequence: 0 }
  822. ) );
  823. sinon.assert.calledOnce( domEventDataMock.preventDefault );
  824. sinon.assert.notCalled( deleteSpy );
  825. scrollStub.restore();
  826. } );
  827. test(
  828. 'should remove the entire empty element if it is next to a widget',
  829. '<paragraph>foo</paragraph>' +
  830. '<image></image>' +
  831. '<blockQuote><paragraph>[]</paragraph></blockQuote>' +
  832. '<paragraph>foo</paragraph>',
  833. 'backward',
  834. '<paragraph>foo</paragraph>[<image></image>]<paragraph>foo</paragraph>'
  835. );
  836. test(
  837. 'should remove the entire empty element (deeper structure) if it is next to a widget',
  838. '<paragraph>foo</paragraph>' +
  839. '<image></image>' +
  840. '<blockQuote><div><div><paragraph>[]</paragraph></div></div></blockQuote>' +
  841. '<paragraph>foo</paragraph>',
  842. 'backward',
  843. '<paragraph>foo</paragraph>' +
  844. '[<image></image>]' +
  845. '<paragraph>foo</paragraph>'
  846. );
  847. test(
  848. 'should remove the entire empty element (deeper structure) if it is next to a widget (forward delete)',
  849. '<paragraph>foo</paragraph>' +
  850. '<blockQuote><div><div><paragraph>[]</paragraph></div></div></blockQuote>' +
  851. '<image></image>' +
  852. '<paragraph>foo</paragraph>',
  853. 'forward',
  854. '<paragraph>foo</paragraph>' +
  855. '[<image></image>]' +
  856. '<paragraph>foo</paragraph>'
  857. );
  858. test(
  859. 'should not remove the entire element which is not empty and the element is next to a widget',
  860. '<paragraph>foo</paragraph>' +
  861. '<image></image>' +
  862. '<blockQuote><paragraph>[]</paragraph><paragraph></paragraph></blockQuote>' +
  863. '<paragraph>foo</paragraph>',
  864. 'backward',
  865. '<paragraph>foo</paragraph>' +
  866. '[<image></image>]' +
  867. '<blockQuote><paragraph></paragraph></blockQuote>' +
  868. '<paragraph>foo</paragraph>'
  869. );
  870. test(
  871. 'should not remove the entire element which is not empty and the element is next to a widget (forward delete)',
  872. '<paragraph>foo</paragraph>' +
  873. '<blockQuote><paragraph>Foo</paragraph><paragraph>[]</paragraph></blockQuote>' +
  874. '<image></image>' +
  875. '<paragraph>foo</paragraph>',
  876. 'forward',
  877. '<paragraph>foo</paragraph>' +
  878. '<blockQuote><paragraph>Foo</paragraph></blockQuote>' +
  879. '[<image></image>]' +
  880. '<paragraph>foo</paragraph>'
  881. );
  882. test(
  883. 'should not remove the entire element (deeper structure) which is not empty and the element is next to a widget',
  884. '<paragraph>foo</paragraph>' +
  885. '<image></image>' +
  886. '<blockQuote>' +
  887. '<div>' +
  888. '<div>' +
  889. '<paragraph>[]</paragraph>' +
  890. '</div>' +
  891. '</div>' +
  892. '<paragraph></paragraph>' +
  893. '</blockQuote>' +
  894. '<paragraph>foo</paragraph>',
  895. 'backward',
  896. '<paragraph>foo</paragraph>' +
  897. '[<image></image>]' +
  898. '<blockQuote>' +
  899. '<paragraph></paragraph>' +
  900. '</blockQuote>' +
  901. '<paragraph>foo</paragraph>'
  902. );
  903. test(
  904. 'should do nothing if the nested element is not empty and the element is next to a widget',
  905. '<paragraph>foo</paragraph>' +
  906. '<image></image>' +
  907. '<blockQuote>' +
  908. '<div>' +
  909. '<div>' +
  910. '<paragraph>Foo[]</paragraph>' +
  911. '</div>' +
  912. '</div>' +
  913. '</blockQuote>' +
  914. '<paragraph>foo</paragraph>',
  915. 'backward',
  916. '<paragraph>foo</paragraph>' +
  917. '<image></image>' +
  918. '<blockQuote>' +
  919. '<div>' +
  920. '<div>' +
  921. '<paragraph>Fo[]</paragraph>' +
  922. '</div>' +
  923. '</div>' +
  924. '</blockQuote>' +
  925. '<paragraph>foo</paragraph>'
  926. );
  927. it( 'does nothing when editor when read only mode is enabled (delete)', () => {
  928. const scrollStub = sinon.stub( view, 'scrollToTheSelection' );
  929. setModelData( model,
  930. '<paragraph>foo</paragraph>' +
  931. '<image></image>' +
  932. '<blockQuote><paragraph>[]</paragraph></blockQuote>' +
  933. '<paragraph>foo</paragraph>'
  934. );
  935. editor.isReadOnly = true;
  936. const domEventDataMock = { target: document.createElement( 'div' ), preventDefault: sinon.spy() };
  937. viewDocument.fire( 'delete', new DomEventData(
  938. viewDocument,
  939. domEventDataMock,
  940. { direction: 'backward', unit: 'character', sequence: 0 }
  941. ) );
  942. expect( getModelData( model ) ).to.equal(
  943. '<paragraph>foo</paragraph>' +
  944. '<image></image>' +
  945. '<blockQuote><paragraph>[]</paragraph></blockQuote>' +
  946. '<paragraph>foo</paragraph>'
  947. );
  948. scrollStub.restore();
  949. } );
  950. it( 'does nothing when editor when read only mode is enabled (forward delete)', () => {
  951. const scrollStub = sinon.stub( view, 'scrollToTheSelection' );
  952. setModelData( model,
  953. '<paragraph>foo</paragraph>' +
  954. '<image></image>' +
  955. '<blockQuote><paragraph>[]</paragraph></blockQuote>' +
  956. '<paragraph>foo</paragraph>'
  957. );
  958. editor.isReadOnly = true;
  959. const domEventDataMock = { target: document.createElement( 'div' ), preventDefault: sinon.spy() };
  960. viewDocument.fire( 'delete', new DomEventData(
  961. viewDocument,
  962. domEventDataMock,
  963. { direction: 'forward', unit: 'character', sequence: 0 }
  964. ) );
  965. expect( getModelData( model ) ).to.equal(
  966. '<paragraph>foo</paragraph>' +
  967. '<image></image>' +
  968. '<blockQuote><paragraph>[]</paragraph></blockQuote>' +
  969. '<paragraph>foo</paragraph>'
  970. );
  971. scrollStub.restore();
  972. } );
  973. } );
  974. } );