utils.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.equal( '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 - initialization', () => {
  109. const element = new ViewEditableElement( 'div' );
  110. element.document = viewDocument;
  111. element.isReadOnly = true;
  112. toWidgetEditable( element );
  113. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  114. } );
  115. it( 'should add proper contenteditable value when element is read-only - when changing', () => {
  116. element.isReadOnly = true;
  117. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  118. element.isReadOnly = false;
  119. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'true' );
  120. } );
  121. it( 'should add proper class when element is focused', () => {
  122. element.isFocused = true;
  123. expect( element.hasClass( 'ck-editable_focused' ) ).to.be.true;
  124. element.isFocused = false;
  125. expect( element.hasClass( 'ck-editable_focused' ) ).to.be.false;
  126. } );
  127. } );
  128. describe( 'addHighlightHandling()', () => {
  129. let element, addSpy, removeSpy, set, remove;
  130. beforeEach( () => {
  131. element = new ViewElement( 'p' );
  132. addSpy = sinon.spy();
  133. removeSpy = sinon.spy();
  134. setHighlightHandling( element, addSpy, removeSpy );
  135. set = element.getCustomProperty( 'addHighlight' );
  136. remove = element.getCustomProperty( 'removeHighlight' );
  137. } );
  138. it( 'should set highlight handling methods', () => {
  139. expect( typeof set ).to.equal( 'function' );
  140. expect( typeof remove ).to.equal( 'function' );
  141. } );
  142. it( 'should call highlight methods when descriptor is added and removed', () => {
  143. const descriptor = { priority: 10, class: 'highlight', id: 'highlight' };
  144. set( element, descriptor );
  145. remove( element, descriptor.id );
  146. sinon.assert.calledOnce( addSpy );
  147. sinon.assert.calledWithExactly( addSpy, element, descriptor );
  148. sinon.assert.calledOnce( removeSpy );
  149. sinon.assert.calledWithExactly( removeSpy, element, descriptor );
  150. } );
  151. it( 'should call highlight methods when next descriptor is added', () => {
  152. const descriptor = { priority: 10, class: 'highlight', id: 'highlight-1' };
  153. const secondDescriptor = { priority: 11, class: 'highlight', id: 'highlight-2' };
  154. set( element, descriptor );
  155. set( element, secondDescriptor );
  156. sinon.assert.calledTwice( addSpy );
  157. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  158. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  159. } );
  160. it( 'should not call highlight methods when descriptor with lower priority is added', () => {
  161. const descriptor = { priority: 10, class: 'highlight', id: 'highlight-1' };
  162. const secondDescriptor = { priority: 9, class: 'highlight', id: 'highlight-2' };
  163. set( element, descriptor );
  164. set( element, secondDescriptor );
  165. sinon.assert.calledOnce( addSpy );
  166. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  167. } );
  168. it( 'should call highlight methods when descriptor is removed changing active descriptor', () => {
  169. const descriptor = { priority: 10, class: 'highlight', id: 'highlight-1' };
  170. const secondDescriptor = { priority: 11, class: 'highlight', id: 'highlight-2' };
  171. set( element, descriptor );
  172. set( element, secondDescriptor );
  173. remove( element, secondDescriptor.id );
  174. sinon.assert.calledThrice( addSpy );
  175. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  176. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  177. expect( addSpy.thirdCall.args[ 1 ] ).to.equal( descriptor );
  178. sinon.assert.calledTwice( removeSpy );
  179. expect( removeSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  180. expect( removeSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  181. } );
  182. it( 'should call highlight methods when descriptor is removed not changing active descriptor', () => {
  183. const descriptor = { priority: 10, class: 'highlight', id: 'highlight-1' };
  184. const secondDescriptor = { priority: 9, class: 'highlight', id: 'highlight-2' };
  185. set( element, descriptor );
  186. set( element, secondDescriptor );
  187. remove( element, secondDescriptor );
  188. sinon.assert.calledOnce( addSpy );
  189. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  190. sinon.assert.notCalled( removeSpy );
  191. } );
  192. it( 'should call highlight methods - CSS class array', () => {
  193. const descriptor = { priority: 10, class: [ 'highlight', 'a' ], id: 'highlight-1' };
  194. const secondDescriptor = { priority: 10, class: [ 'highlight', 'b' ], id: 'highlight-2' };
  195. set( element, descriptor );
  196. set( element, secondDescriptor );
  197. sinon.assert.calledTwice( addSpy );
  198. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  199. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  200. } );
  201. } );
  202. } );