8
0

jumpoveruielement.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 as setViewData } from '../../../src/dev-utils/view';
  10. describe( 'Document', () => {
  11. let viewDocument, domRoot, domSelection;
  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. domSelection = document.getSelection();
  20. domSelection.removeAllRanges();
  21. viewDocument.isFocused = true;
  22. } );
  23. afterEach( () => {
  24. viewDocument.destroy();
  25. domRoot.parentElement.removeChild( domRoot );
  26. } );
  27. function prepare( view, options ) {
  28. setViewData( viewDocument, view );
  29. viewDocument.render();
  30. const eventData = Object.assign( { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) }, options );
  31. viewDocument.fire( 'keydown', eventData );
  32. }
  33. function check( anchorNode, anchorOffset, focusNode, focusOffset ) {
  34. const anchor = domSelection.anchorNode.data ? domSelection.anchorNode.data : domSelection.anchorNode.nodeName.toUpperCase();
  35. expect( anchor, 'anchorNode' ).to.equal( anchorNode );
  36. expect( domSelection.anchorOffset, 'anchorOffset' ).to.equal( anchorOffset );
  37. if ( focusNode ) {
  38. const focus = domSelection.focusNode.data ? domSelection.focusNode.data : domSelection.focusNode.nodeName.toUpperCase();
  39. expect( focus, 'focusNode' ).to.equal( focusNode );
  40. expect( domSelection.focusOffset, 'focusOffset' ).to.equal( focusOffset );
  41. } else {
  42. expect( domSelection.isCollapsed, 'isCollapsed' ).to.be.true;
  43. }
  44. }
  45. describe( 'jump over ui element handler', () => {
  46. describe( 'collapsed selection', () => {
  47. it( 'do nothing when another key is pressed', () => {
  48. prepare( '<container:p>foo<ui:span></ui:span>{}bar</container:p>', { keyCode: keyCodes.arrowleft } );
  49. check( 'bar', 0 );
  50. } );
  51. it( 'jump over ui element when right arrow is pressed before ui element - directly before ui element', () => {
  52. prepare( '<container:p>foo[]<ui:span></ui:span>bar</container:p>' );
  53. check( 'P', 2 );
  54. } );
  55. it( 'jump over ui element when right arrow is pressed before ui element - not directly before ui element', () => {
  56. prepare( '<container:p>foo{}<ui:span></ui:span>bar</container:p>' );
  57. check( 'P', 2 );
  58. } );
  59. it( 'jump over multiple ui elements when right arrow is pressed before ui element', () => {
  60. prepare( '<container:p>foo{}<ui:span></ui:span><ui:span></ui:span>bar</container:p>' );
  61. check( 'P', 3 );
  62. } );
  63. it( 'jump over ui elements at the end of container element', () => {
  64. prepare( '<container:p>foo{}<ui:span></ui:span><ui:span></ui:span></container:p><container:div></container:div>' );
  65. check( 'P', 3 );
  66. } );
  67. it( 'jump over ui element if selection is in attribute element - case 1', () => {
  68. prepare( '<container:p><attribute:b>foo{}</attribute:b><ui:span></ui:span>bar</container:p>' );
  69. check( 'P', 2 );
  70. } );
  71. it( 'jump over ui element if selection is in attribute element - case 2', () => {
  72. prepare( '<container:p><attribute:b>foo{}</attribute:b><ui:span></ui:span>bar</container:p>' );
  73. check( 'P', 2 );
  74. } );
  75. it( 'jump over ui element if selection is in multiple attribute elements', () => {
  76. prepare( '<container:p><attribute:i><attribute:b>foo{}</attribute:b></attribute:i><ui:span></ui:span>bar</container:p>' );
  77. check( 'P', 2 );
  78. } );
  79. it( 'jump over empty attribute elements and ui elements', () => {
  80. prepare(
  81. '<container:p>' +
  82. 'foo{}<attribute:b></attribute:b><ui:span></ui:span><ui:span></ui:span><attribute:b></attribute:b>bar' +
  83. '</container:p>'
  84. );
  85. check( 'P', 5 );
  86. } );
  87. it( 'jump over empty attribute elements and ui elements if shift key is pressed', () => {
  88. prepare(
  89. '<container:p>' +
  90. 'foo{}<attribute:b></attribute:b><ui:span></ui:span><ui:span></ui:span><attribute:b></attribute:b>bar' +
  91. '</container:p>',
  92. { shiftKey: true }
  93. );
  94. check( 'P', 5 );
  95. } );
  96. it( 'do nothing if selection is not directly before ui element', () => {
  97. prepare( '<container:p>fo{}o<ui:span></ui:span>bar</container:p>' );
  98. check( 'foo', 2 );
  99. } );
  100. it( 'do nothing if selection is in attribute element but not before ui element', () => {
  101. prepare( '<container:p><attribute:b>foo{}</attribute:b>bar</container:p>' );
  102. check( 'foo', 3 );
  103. } );
  104. it( 'do nothing if selection is before non-empty attribute element', () => {
  105. prepare( '<container:p>fo{}<attribute:b>o</attribute:b><ui:span></ui:span>bar</container:p>' );
  106. check( 'fo', 2 );
  107. } );
  108. it( 'do nothing if selection is before container element - case 1', () => {
  109. prepare( '<container:p>foo{}</container:p><ui:span></ui:span><container:div>bar</container:div>' );
  110. check( 'foo', 3 );
  111. } );
  112. it( 'do nothing if selection is before container element - case 2', () => {
  113. prepare( '<container:div>foo{}<container:p></container:p><ui:span></ui:span></container:div>' );
  114. check( 'foo', 3 );
  115. } );
  116. it( 'do nothing if selection is at the end of last container element', () => {
  117. prepare( '<container:p>foo{}</container:p>' );
  118. check( 'foo', 3 );
  119. } );
  120. } );
  121. describe( 'non-collapsed selection', () => {
  122. it( 'should do nothing', () => {
  123. prepare( '<container:p>f{oo}<ui:span></ui:span>bar</container:p>' );
  124. check( 'foo', 1, 'foo', 3 );
  125. } );
  126. it( 'should do nothing if selection is not before ui element - shift key pressed', () => {
  127. prepare( '<container:p>f{o}o<ui:span></ui:span>bar</container:p>', { shiftKey: true } );
  128. check( 'foo', 1, 'foo', 2 );
  129. } );
  130. it( 'jump over ui element if shift key is pressed', () => {
  131. prepare( '<container:p>fo{o}<ui:span></ui:span>bar</container:p>', { shiftKey: true } );
  132. check( 'foo', 2, 'P', 2 );
  133. } );
  134. it( 'jump over ui element if selection is in multiple attribute elements', () => {
  135. prepare(
  136. '<container:p><attribute:i><attribute:b>fo{o}</attribute:b></attribute:i><ui:span></ui:span>bar</container:p>',
  137. { shiftKey: true }
  138. );
  139. check( 'foo', 2, 'P', 2 );
  140. } );
  141. it( 'jump over empty attribute elements and ui elements if shift key is pressed', () => {
  142. prepare(
  143. '<container:p>' +
  144. 'fo{o}<attribute:b></attribute:b><ui:span></ui:span><ui:span></ui:span><attribute:b></attribute:b>bar' +
  145. '</container:p>',
  146. { shiftKey: true }
  147. );
  148. check( 'foo', 2, 'P', 5 );
  149. } );
  150. } );
  151. it( 'should do nothing if dom position cannot be converted to view position', () => {
  152. const newDiv = document.createElement( 'div' );
  153. const domSelection = document.getSelection();
  154. document.body.appendChild( newDiv );
  155. domSelection.collapse( newDiv, 0 );
  156. viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) } );
  157. } );
  158. } );
  159. } );