utils.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  6. import ViewEditableElement from '@ckeditor/ckeditor5-engine/src/view/editableelement';
  7. import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  8. import {
  9. toWidget,
  10. isWidget,
  11. setLabel,
  12. getLabel,
  13. toWidgetEditable,
  14. setVirtualSelectionHandling,
  15. WIDGET_CLASS_NAME
  16. } from '../src/utils';
  17. describe( 'widget utils', () => {
  18. let element;
  19. beforeEach( () => {
  20. element = new ViewElement( 'div' );
  21. toWidget( element );
  22. } );
  23. describe( 'toWidget()', () => {
  24. it( 'should set contenteditable to false', () => {
  25. expect( element.getAttribute( 'contenteditable' ) ).to.be.false;
  26. } );
  27. it( 'should define getFillerOffset method', () => {
  28. expect( element.getFillerOffset ).to.be.a( 'function' );
  29. expect( element.getFillerOffset() ).to.be.null;
  30. } );
  31. it( 'should add proper CSS class', () => {
  32. expect( element.hasClass( WIDGET_CLASS_NAME ) ).to.be.true;
  33. } );
  34. it( 'should add element\'s label if one is provided', () => {
  35. toWidget( element, { label: 'foo bar baz label' } );
  36. expect( getLabel( element ) ).to.equal( 'foo bar baz label' );
  37. } );
  38. it( 'should add element\'s label if one is provided as function', () => {
  39. toWidget( element, { label: () => 'foo bar baz label' } );
  40. expect( getLabel( element ) ).to.equal( 'foo bar baz label' );
  41. } );
  42. it( 'should set default virtual selection methods', () => {
  43. toWidget( element );
  44. const set = element.getCustomProperty( 'setVirtualSelection' );
  45. const remove = element.getCustomProperty( 'removeVirtualSelection' );
  46. expect( typeof set ).to.equal( 'function' );
  47. expect( typeof remove ).to.equal( 'function' );
  48. set( element, { priority: 1, class: 'virtual-selection' } );
  49. expect( element.hasClass( 'virtual-selection' ) ).to.be.true;
  50. remove( element, { priority: 1, class: 'virtual-selection' } );
  51. expect( element.hasClass( 'virtual-selection' ) ).to.be.false;
  52. } );
  53. it( 'should use provided virtual selection methods', () => {
  54. const setSpy = sinon.spy();
  55. const removeSpy = sinon.spy();
  56. const descriptor = { priority: 1, class: 'virtual-selection' };
  57. toWidget( element, { setVirtualSelection: setSpy, removeVirtualSelection: removeSpy } );
  58. const set = element.getCustomProperty( 'setVirtualSelection' );
  59. const remove = element.getCustomProperty( 'removeVirtualSelection' );
  60. expect( typeof set ).to.equal( 'function' );
  61. expect( typeof remove ).to.equal( 'function' );
  62. set( element, descriptor );
  63. sinon.assert.calledOnce( setSpy );
  64. sinon.assert.calledWithExactly( setSpy, element, descriptor );
  65. remove( element, descriptor );
  66. sinon.assert.calledOnce( removeSpy );
  67. sinon.assert.calledWithExactly( removeSpy, element, descriptor );
  68. } );
  69. } );
  70. describe( 'isWidget()', () => {
  71. it( 'should return true for widgetized elements', () => {
  72. expect( isWidget( element ) ).to.be.true;
  73. } );
  74. it( 'should return false for non-widgetized elements', () => {
  75. expect( isWidget( new ViewElement( 'p' ) ) ).to.be.false;
  76. } );
  77. } );
  78. describe( 'label utils', () => {
  79. it( 'should allow to set label for element', () => {
  80. const element = new ViewElement( 'p' );
  81. setLabel( element, 'foo bar baz' );
  82. expect( getLabel( element ) ).to.equal( 'foo bar baz' );
  83. } );
  84. it( 'should return empty string for elements without label', () => {
  85. const element = new ViewElement( 'div' );
  86. expect( getLabel( element ) ).to.equal( '' );
  87. } );
  88. it( 'should allow to use a function as label creator', () => {
  89. const element = new ViewElement( 'p' );
  90. let caption = 'foo';
  91. setLabel( element, () => caption );
  92. expect( getLabel( element ) ).to.equal( 'foo' );
  93. caption = 'bar';
  94. expect( getLabel( element ) ).to.equal( 'bar' );
  95. } );
  96. } );
  97. describe( 'toWidgetEditable', () => {
  98. let viewDocument, element;
  99. beforeEach( () => {
  100. viewDocument = new ViewDocument();
  101. element = new ViewEditableElement( 'div' );
  102. element.document = viewDocument;
  103. toWidgetEditable( element );
  104. } );
  105. it( 'should be created in context of proper document', () => {
  106. expect( element.document ).to.equal( viewDocument );
  107. } );
  108. it( 'should add proper class', () => {
  109. expect( element.hasClass( 'ck-editable' ) ).to.be.true;
  110. } );
  111. it( 'should add proper contenteditable value when element is read-only', () => {
  112. element.isReadOnly = false;
  113. expect( element.getAttribute( 'contenteditable' ) ).to.true;
  114. element.isReadOnly = true;
  115. expect( element.getAttribute( 'contenteditable' ) ).to.false;
  116. } );
  117. it( 'should add proper class when element is focused', () => {
  118. element.isFocused = true;
  119. expect( element.hasClass( 'ck-editable_focused' ) ).to.be.true;
  120. element.isFocused = false;
  121. expect( element.hasClass( 'ck-editable_focused' ) ).to.be.false;
  122. } );
  123. } );
  124. describe( 'setVirtualSelectionHandling()', () => {
  125. let element, addSpy, removeSpy, set, remove;
  126. beforeEach( () => {
  127. element = new ViewElement( 'p' );
  128. addSpy = sinon.spy();
  129. removeSpy = sinon.spy();
  130. setVirtualSelectionHandling( element, addSpy, removeSpy );
  131. set = element.getCustomProperty( 'setVirtualSelection' );
  132. remove = element.getCustomProperty( 'removeVirtualSelection' );
  133. } );
  134. it( 'should set virtual selection methods', () => {
  135. expect( typeof set ).to.equal( 'function' );
  136. expect( typeof remove ).to.equal( 'function' );
  137. } );
  138. it( 'should call virtual selection methods when descriptor is added and removed', () => {
  139. const descriptor = { priority: 10, class: 'virtual-selection' };
  140. set( element, descriptor );
  141. remove( element, descriptor );
  142. sinon.assert.calledOnce( addSpy );
  143. sinon.assert.calledWithExactly( addSpy, element, descriptor );
  144. sinon.assert.calledOnce( removeSpy );
  145. sinon.assert.calledWithExactly( removeSpy, element, descriptor );
  146. } );
  147. it( 'should call virtual selection methods when next descriptor is added', () => {
  148. const descriptor = { priority: 10, class: 'virtual-selection' };
  149. const secondDescriptor = { priority: 11, class: 'virtual-selection' };
  150. set( element, descriptor );
  151. set( element, secondDescriptor );
  152. sinon.assert.calledTwice( addSpy );
  153. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  154. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  155. } );
  156. it( 'should not call virtual selection methods when descriptor with lower priority is added', () => {
  157. const descriptor = { priority: 10, class: 'virtual-selection' };
  158. const secondDescriptor = { priority: 9, class: 'virtual-selection' };
  159. set( element, descriptor );
  160. set( element, secondDescriptor );
  161. sinon.assert.calledOnce( addSpy );
  162. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  163. } );
  164. it( 'should call virtual selection methods when descriptor is removed changing active descriptor', () => {
  165. const descriptor = { priority: 10, class: 'virtual-selection' };
  166. const secondDescriptor = { priority: 11, class: 'virtual-selection' };
  167. set( element, descriptor );
  168. set( element, secondDescriptor );
  169. remove( element, secondDescriptor );
  170. sinon.assert.calledThrice( addSpy );
  171. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  172. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  173. expect( addSpy.thirdCall.args[ 1 ] ).to.equal( descriptor );
  174. sinon.assert.calledTwice( removeSpy );
  175. expect( removeSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  176. expect( removeSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  177. } );
  178. it( 'should call virtual selection methods when descriptor is removed not changing active descriptor', () => {
  179. const descriptor = { priority: 10, class: 'virtual-selection' };
  180. const secondDescriptor = { priority: 9, class: 'virtual-selection' };
  181. set( element, descriptor );
  182. set( element, secondDescriptor );
  183. remove( element, secondDescriptor );
  184. sinon.assert.calledOnce( addSpy );
  185. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  186. sinon.assert.notCalled( removeSpy );
  187. } );
  188. } );
  189. } );