jumpoveruielement.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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( 'jump over ui element if selection is not collapsed but shift key is pressed', () => {
  57. setData( viewDocument, '<container:p>fo{o}<ui:span></ui:span>bar</container:p>' );
  58. viewDocument.render();
  59. viewDocument.fire(
  60. 'keydown',
  61. { keyCode: keyCodes.arrowright, shiftKey: true, domTarget: viewDocument.domRoots.get( 'main' ) }
  62. );
  63. const domSelection = document.getSelection();
  64. expect( domSelection.anchorNode.nodeName.toUpperCase() ).to.equal( '#TEXT' );
  65. expect( domSelection.anchorOffset ).to.equal( 2 );
  66. expect( domSelection.focusNode.nodeName.toUpperCase() ).to.equal( 'P' );
  67. expect( domSelection.focusOffset ).to.equal( 2 );
  68. } );
  69. it( 'jump over ui element if selection is in attribute element', () => {
  70. setData( viewDocument, '<container:p><attribute:b>foo{}</attribute:b><ui:span></ui:span>bar</container:p>' );
  71. viewDocument.render();
  72. viewDocument.fire(
  73. 'keydown',
  74. { keyCode: keyCodes.arrowright, shiftKey: true, domTarget: viewDocument.domRoots.get( 'main' ) }
  75. );
  76. const domSelection = document.getSelection();
  77. expect( domSelection.anchorNode.nodeName.toUpperCase() ).to.equal( 'P' );
  78. expect( domSelection.anchorOffset ).to.equal( 2 );
  79. expect( domSelection.isCollapsed ).to.be.true;
  80. } );
  81. it( 'should do nothing if caret is not directly before ui element', () => {
  82. setData( viewDocument, '<container:p>fo{}o<ui:span></ui:span>bar</container:p>' );
  83. viewDocument.render();
  84. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) } );
  85. const domSelection = document.getSelection();
  86. expect( domSelection.anchorNode.data ).to.equal( 'foo' );
  87. expect( domSelection.anchorOffset ).to.equal( 2 );
  88. expect( domSelection.isCollapsed ).to.be.true;
  89. } );
  90. it( 'should do nothing if dom position cannot be converted to view position', () => {
  91. const newDiv = document.createElement( 'div' );
  92. const domSelection = document.getSelection();
  93. document.body.appendChild( newDiv );
  94. domSelection.collapse( newDiv, 0 );
  95. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) } );
  96. } );
  97. } );
  98. } );