8
0

utils.js 7.0 KB

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