jumpoveruielement.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ViewDocument from '../../../src/view/document';
  7. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  8. import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
  9. import { setData } from '../../../src/dev-utils/view';
  10. describe( 'Document', () => {
  11. let viewDocument, domRoot;
  12. beforeEach( () => {
  13. domRoot = createElement( document, 'div', {
  14. contenteditable: 'true'
  15. } );
  16. document.body.appendChild( domRoot );
  17. viewDocument = new ViewDocument();
  18. viewDocument.createRoot( domRoot );
  19. document.getSelection().removeAllRanges();
  20. viewDocument.isFocused = true;
  21. } );
  22. afterEach( () => {
  23. viewDocument.destroy();
  24. domRoot.parentElement.removeChild( domRoot );
  25. } );
  26. describe( 'jump over ui element handler', () => {
  27. it( 'jump over ui element when right arrow is pressed before ui element', () => {
  28. setData( viewDocument, '<container:p>foo{}<ui:span></ui:span>bar</container:p>' );
  29. viewDocument.render();
  30. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) } );
  31. const domSelection = document.getSelection();
  32. expect( domSelection.anchorNode.nodeName.toUpperCase() ).to.equal( 'P' );
  33. expect( domSelection.anchorOffset ).to.equal( 2 );
  34. expect( domSelection.isCollapsed ).to.be.true;
  35. } );
  36. it( 'should do nothing when another key is pressed', () => {
  37. setData( viewDocument, '<container:p>foo<ui:span></ui:span>{}bar</container:p>' );
  38. viewDocument.render();
  39. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: viewDocument.domRoots.get( 'main' ) } );
  40. const domSelection = document.getSelection();
  41. expect( domSelection.anchorNode.data ).to.equal( 'bar' );
  42. expect( domSelection.anchorOffset ).to.equal( 0 );
  43. expect( domSelection.isCollapsed ).to.be.true;
  44. } );
  45. it( 'should do nothing if range is not collapsed', () => {
  46. setData( viewDocument, '<container:p>f{oo}<ui:span></ui:span>bar</container:p>' );
  47. viewDocument.render();
  48. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) } );
  49. const domSelection = document.getSelection();
  50. expect( domSelection.anchorNode.data ).to.equal( 'foo' );
  51. expect( domSelection.anchorOffset ).to.equal( 1 );
  52. expect( domSelection.focusNode.data ).to.equal( 'foo' );
  53. expect( domSelection.focusOffset ).to.equal( 3 );
  54. expect( domSelection.isCollapsed ).to.be.false;
  55. } );
  56. it( 'should do nothing if caret is not directly before ui element', () => {
  57. setData( viewDocument, '<container:p>fo{}o<ui:span></ui:span>bar</container:p>' );
  58. viewDocument.render();
  59. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) } );
  60. const domSelection = document.getSelection();
  61. expect( domSelection.anchorNode.data ).to.equal( 'foo' );
  62. expect( domSelection.anchorOffset ).to.equal( 2 );
  63. expect( domSelection.isCollapsed ).to.be.true;
  64. } );
  65. it( 'should do nothing if dom position cannot be converted to view position', () => {
  66. const newDiv = document.createElement( 'div' );
  67. const domSelection = document.getSelection();
  68. document.body.appendChild( newDiv );
  69. domSelection.collapse( newDiv, 0 );
  70. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) } );
  71. } );
  72. } );
  73. } );