utils.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Writer from '@ckeditor/ckeditor5-engine/src/view/writer';
  6. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  7. import ViewEditableElement from '@ckeditor/ckeditor5-engine/src/view/editableelement';
  8. import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  9. import {
  10. toWidget,
  11. isWidget,
  12. setLabel,
  13. getLabel,
  14. toWidgetEditable,
  15. setHighlightHandling,
  16. WIDGET_CLASS_NAME
  17. } from '../src/utils';
  18. describe( 'widget utils', () => {
  19. let element, writer, viewDocument;
  20. beforeEach( () => {
  21. viewDocument = new ViewDocument();
  22. writer = new Writer( viewDocument );
  23. element = writer.createContainerElement( 'div' );
  24. toWidget( element, writer );
  25. } );
  26. describe( 'toWidget()', () => {
  27. it( 'should set contenteditable to "false"', () => {
  28. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  29. } );
  30. it( 'should define getFillerOffset method', () => {
  31. expect( element.getFillerOffset ).to.be.a( 'function' );
  32. expect( element.getFillerOffset() ).to.be.null;
  33. } );
  34. it( 'should add proper CSS class', () => {
  35. expect( element.hasClass( WIDGET_CLASS_NAME ) ).to.be.true;
  36. } );
  37. it( 'should add element\'s label if one is provided', () => {
  38. toWidget( element, writer, { label: 'foo bar baz label' } );
  39. expect( getLabel( element ) ).to.equal( 'foo bar baz label' );
  40. } );
  41. it( 'should add element\'s label if one is provided as function', () => {
  42. toWidget( element, writer, { label: () => 'foo bar baz label' } );
  43. expect( getLabel( element ) ).to.equal( 'foo bar baz label' );
  44. } );
  45. it( 'should set default highlight handling methods', () => {
  46. toWidget( element, writer );
  47. const set = element.getCustomProperty( 'addHighlight' );
  48. const remove = element.getCustomProperty( 'removeHighlight' );
  49. expect( typeof set ).to.equal( 'function' );
  50. expect( typeof remove ).to.equal( 'function' );
  51. set( element, { priority: 1, classes: 'highlight', id: 'highlight' }, writer );
  52. expect( element.hasClass( 'highlight' ) ).to.be.true;
  53. remove( element, 'highlight', writer );
  54. expect( element.hasClass( 'highlight' ) ).to.be.false;
  55. } );
  56. it( 'should set default highlight handling methods - CSS classes array', () => {
  57. toWidget( element, writer );
  58. const set = element.getCustomProperty( 'addHighlight' );
  59. const remove = element.getCustomProperty( 'removeHighlight' );
  60. expect( typeof set ).to.equal( 'function' );
  61. expect( typeof remove ).to.equal( 'function' );
  62. set( element, { priority: 1, classes: [ 'highlight', 'foo' ], id: 'highlight' }, writer );
  63. expect( element.hasClass( 'highlight' ) ).to.be.true;
  64. expect( element.hasClass( 'foo' ) ).to.be.true;
  65. remove( element, 'highlight', writer );
  66. expect( element.hasClass( 'highlight' ) ).to.be.false;
  67. expect( element.hasClass( 'foo' ) ).to.be.false;
  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', writer );
  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, writer );
  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, writer );
  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-editor__editable', 'ck-editor__nested-editable' ) ).to.be.true;
  110. } );
  111. it( 'should add proper contenteditable value when element is read-only - initialization', () => {
  112. const element = new ViewEditableElement( 'div' );
  113. element._document = viewDocument;
  114. element.isReadOnly = true;
  115. toWidgetEditable( element, writer );
  116. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  117. } );
  118. it( 'should add proper contenteditable value when element is read-only - when changing', () => {
  119. element.isReadOnly = true;
  120. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  121. element.isReadOnly = false;
  122. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'true' );
  123. } );
  124. it( 'should add proper class when element is focused', () => {
  125. element.isFocused = true;
  126. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.true;
  127. element.isFocused = false;
  128. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.false;
  129. } );
  130. } );
  131. describe( 'addHighlightHandling()', () => {
  132. let element, addSpy, removeSpy, set, remove;
  133. beforeEach( () => {
  134. element = new ViewElement( 'p' );
  135. addSpy = sinon.spy();
  136. removeSpy = sinon.spy();
  137. setHighlightHandling( element, writer, addSpy, removeSpy );
  138. set = element.getCustomProperty( 'addHighlight' );
  139. remove = element.getCustomProperty( 'removeHighlight' );
  140. } );
  141. it( 'should set highlight handling methods', () => {
  142. expect( typeof set ).to.equal( 'function' );
  143. expect( typeof remove ).to.equal( 'function' );
  144. } );
  145. it( 'should call highlight methods when descriptor is added and removed', () => {
  146. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight' };
  147. set( element, descriptor, writer );
  148. remove( element, descriptor.id, writer );
  149. sinon.assert.calledOnce( addSpy );
  150. sinon.assert.calledWithExactly( addSpy, element, descriptor, writer );
  151. sinon.assert.calledOnce( removeSpy );
  152. sinon.assert.calledWithExactly( removeSpy, element, descriptor, writer );
  153. } );
  154. it( 'should call highlight methods when next descriptor is added', () => {
  155. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  156. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  157. set( element, descriptor );
  158. set( element, secondDescriptor );
  159. sinon.assert.calledTwice( addSpy );
  160. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  161. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  162. } );
  163. it( 'should not call highlight methods when descriptor with lower priority is added', () => {
  164. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  165. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  166. set( element, descriptor );
  167. set( element, secondDescriptor );
  168. sinon.assert.calledOnce( addSpy );
  169. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  170. } );
  171. it( 'should call highlight methods when descriptor is removed changing active descriptor', () => {
  172. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  173. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  174. set( element, descriptor );
  175. set( element, secondDescriptor );
  176. remove( element, secondDescriptor.id );
  177. sinon.assert.calledThrice( addSpy );
  178. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  179. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  180. expect( addSpy.thirdCall.args[ 1 ] ).to.equal( descriptor );
  181. sinon.assert.calledTwice( removeSpy );
  182. expect( removeSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  183. expect( removeSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  184. } );
  185. it( 'should call highlight methods when descriptor is removed not changing active descriptor', () => {
  186. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  187. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  188. set( element, descriptor );
  189. set( element, secondDescriptor );
  190. remove( element, secondDescriptor );
  191. sinon.assert.calledOnce( addSpy );
  192. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  193. sinon.assert.notCalled( removeSpy );
  194. } );
  195. it( 'should call highlight methods - CSS class array', () => {
  196. const descriptor = { priority: 10, classes: [ 'highlight', 'a' ], id: 'highlight-1' };
  197. const secondDescriptor = { priority: 10, classes: [ 'highlight', 'b' ], id: 'highlight-2' };
  198. set( element, descriptor );
  199. set( element, secondDescriptor );
  200. sinon.assert.calledTwice( addSpy );
  201. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  202. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  203. } );
  204. } );
  205. } );