utils.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. setHighlightHandling,
  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 highlight handling methods', () => {
  43. toWidget( element );
  44. const set = element.getCustomProperty( 'addHighlight' );
  45. const remove = element.getCustomProperty( 'removeHighlight' );
  46. expect( typeof set ).to.equal( 'function' );
  47. expect( typeof remove ).to.equal( 'function' );
  48. set( element, { priority: 1, class: 'highlight', id: 'highlight' } );
  49. expect( element.hasClass( 'highlight' ) ).to.be.true;
  50. remove( element, 'highlight' );
  51. expect( element.hasClass( 'highlight' ) ).to.be.false;
  52. } );
  53. it( 'should set default highlight handling methods - CSS classes array', () => {
  54. toWidget( element );
  55. const set = element.getCustomProperty( 'addHighlight' );
  56. const remove = element.getCustomProperty( 'removeHighlight' );
  57. expect( typeof set ).to.equal( 'function' );
  58. expect( typeof remove ).to.equal( 'function' );
  59. set( element, { priority: 1, class: [ 'highlight', 'foo' ], id: 'highlight' } );
  60. expect( element.hasClass( 'highlight' ) ).to.be.true;
  61. expect( element.hasClass( 'foo' ) ).to.be.true;
  62. remove( element, 'highlight' );
  63. expect( element.hasClass( 'highlight' ) ).to.be.false;
  64. expect( element.hasClass( 'foo' ) ).to.be.false;
  65. } );
  66. } );
  67. describe( 'isWidget()', () => {
  68. it( 'should return true for widgetized elements', () => {
  69. expect( isWidget( element ) ).to.be.true;
  70. } );
  71. it( 'should return false for non-widgetized elements', () => {
  72. expect( isWidget( new ViewElement( 'p' ) ) ).to.be.false;
  73. } );
  74. } );
  75. describe( 'label utils', () => {
  76. it( 'should allow to set label for element', () => {
  77. const element = new ViewElement( 'p' );
  78. setLabel( element, 'foo bar baz' );
  79. expect( getLabel( element ) ).to.equal( 'foo bar baz' );
  80. } );
  81. it( 'should return empty string for elements without label', () => {
  82. const element = new ViewElement( 'div' );
  83. expect( getLabel( element ) ).to.equal( '' );
  84. } );
  85. it( 'should allow to use a function as label creator', () => {
  86. const element = new ViewElement( 'p' );
  87. let caption = 'foo';
  88. setLabel( element, () => caption );
  89. expect( getLabel( element ) ).to.equal( 'foo' );
  90. caption = 'bar';
  91. expect( getLabel( element ) ).to.equal( 'bar' );
  92. } );
  93. } );
  94. describe( 'toWidgetEditable', () => {
  95. let viewDocument, element;
  96. beforeEach( () => {
  97. viewDocument = new ViewDocument();
  98. element = new ViewEditableElement( 'div' );
  99. element.document = viewDocument;
  100. toWidgetEditable( element );
  101. } );
  102. it( 'should be created in context of proper document', () => {
  103. expect( element.document ).to.equal( viewDocument );
  104. } );
  105. it( 'should add proper class', () => {
  106. expect( element.hasClass( 'ck-editable' ) ).to.be.true;
  107. } );
  108. it( 'should add proper contenteditable value when element is read-only', () => {
  109. element.isReadOnly = false;
  110. expect( element.getAttribute( 'contenteditable' ) ).to.true;
  111. element.isReadOnly = true;
  112. expect( element.getAttribute( 'contenteditable' ) ).to.false;
  113. } );
  114. it( 'should add proper class when element is focused', () => {
  115. element.isFocused = true;
  116. expect( element.hasClass( 'ck-editable_focused' ) ).to.be.true;
  117. element.isFocused = false;
  118. expect( element.hasClass( 'ck-editable_focused' ) ).to.be.false;
  119. } );
  120. } );
  121. describe( 'addHighlightHandling()', () => {
  122. let element, addSpy, removeSpy, set, remove;
  123. beforeEach( () => {
  124. element = new ViewElement( 'p' );
  125. addSpy = sinon.spy();
  126. removeSpy = sinon.spy();
  127. setHighlightHandling( element, addSpy, removeSpy );
  128. set = element.getCustomProperty( 'addHighlight' );
  129. remove = element.getCustomProperty( 'removeHighlight' );
  130. } );
  131. it( 'should set highlight handling methods', () => {
  132. expect( typeof set ).to.equal( 'function' );
  133. expect( typeof remove ).to.equal( 'function' );
  134. } );
  135. it( 'should call highlight methods when descriptor is added and removed', () => {
  136. const descriptor = { priority: 10, class: 'highlight', id: 'highlight' };
  137. set( element, descriptor );
  138. remove( element, descriptor.id );
  139. sinon.assert.calledOnce( addSpy );
  140. sinon.assert.calledWithExactly( addSpy, element, descriptor );
  141. sinon.assert.calledOnce( removeSpy );
  142. sinon.assert.calledWithExactly( removeSpy, element, descriptor );
  143. } );
  144. it( 'should call highlight methods when next descriptor is added', () => {
  145. const descriptor = { priority: 10, class: 'highlight', id: 'highlight-1' };
  146. const secondDescriptor = { priority: 11, class: 'highlight', id: 'highlight-2' };
  147. set( element, descriptor );
  148. set( element, secondDescriptor );
  149. sinon.assert.calledTwice( addSpy );
  150. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  151. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  152. } );
  153. it( 'should not call highlight methods when descriptor with lower priority is added', () => {
  154. const descriptor = { priority: 10, class: 'highlight', id: 'highlight-1' };
  155. const secondDescriptor = { priority: 9, class: 'highlight', id: 'highlight-2' };
  156. set( element, descriptor );
  157. set( element, secondDescriptor );
  158. sinon.assert.calledOnce( addSpy );
  159. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  160. } );
  161. it( 'should call highlight methods when descriptor is removed changing active descriptor', () => {
  162. const descriptor = { priority: 10, class: 'highlight', id: 'highlight-1' };
  163. const secondDescriptor = { priority: 11, class: 'highlight', id: 'highlight-2' };
  164. set( element, descriptor );
  165. set( element, secondDescriptor );
  166. remove( element, secondDescriptor.id );
  167. sinon.assert.calledThrice( addSpy );
  168. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  169. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  170. expect( addSpy.thirdCall.args[ 1 ] ).to.equal( descriptor );
  171. sinon.assert.calledTwice( removeSpy );
  172. expect( removeSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  173. expect( removeSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  174. } );
  175. it( 'should call highlight methods when descriptor is removed not changing active descriptor', () => {
  176. const descriptor = { priority: 10, class: 'highlight', id: 'highlight-1' };
  177. const secondDescriptor = { priority: 9, class: 'highlight', id: 'highlight-2' };
  178. set( element, descriptor );
  179. set( element, secondDescriptor );
  180. remove( element, secondDescriptor );
  181. sinon.assert.calledOnce( addSpy );
  182. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  183. sinon.assert.notCalled( removeSpy );
  184. } );
  185. it( 'should call highlight methods - CSS class array', () => {
  186. const descriptor = { priority: 10, class: [ 'highlight', 'a' ], id: 'highlight-1' };
  187. const secondDescriptor = { priority: 10, class: [ 'highlight', 'b' ], id: 'highlight-2' };
  188. set( element, descriptor );
  189. set( element, secondDescriptor );
  190. sinon.assert.calledTwice( addSpy );
  191. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  192. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  193. } );
  194. } );
  195. } );