8
0

utils.js 10 KB

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