8
0

utils.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. import env from '@ckeditor/ckeditor5-utils/src/env';
  21. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  22. describe( 'widget utils', () => {
  23. let element, writer, viewDocument;
  24. testUtils.createSinonSandbox();
  25. beforeEach( () => {
  26. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  27. testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
  28. viewDocument = new ViewDocument();
  29. writer = new Writer( viewDocument );
  30. element = writer.createContainerElement( 'div' );
  31. toWidget( element, writer );
  32. } );
  33. describe( 'toWidget()', () => {
  34. it( 'should set contenteditable to "false"', () => {
  35. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  36. } );
  37. it( 'should define getFillerOffset method', () => {
  38. expect( element.getFillerOffset ).to.be.a( 'function' );
  39. expect( element.getFillerOffset() ).to.be.null;
  40. } );
  41. it( 'should add proper CSS class', () => {
  42. expect( element.hasClass( WIDGET_CLASS_NAME ) ).to.be.true;
  43. } );
  44. it( 'should add element\'s label if one is provided', () => {
  45. toWidget( element, writer, { label: 'foo bar baz label' } );
  46. expect( getLabel( element ) ).to.equal( 'foo bar baz label' );
  47. } );
  48. it( 'should add element\'s label if one is provided as function', () => {
  49. toWidget( element, writer, { label: () => 'foo bar baz label' } );
  50. expect( getLabel( element ) ).to.equal( 'foo bar baz label' );
  51. } );
  52. it( 'should set default highlight handling methods', () => {
  53. toWidget( element, writer );
  54. const set = element.getCustomProperty( 'addHighlight' );
  55. const remove = element.getCustomProperty( 'removeHighlight' );
  56. expect( typeof set ).to.equal( 'function' );
  57. expect( typeof remove ).to.equal( 'function' );
  58. set( element, { priority: 1, classes: 'highlight', id: 'highlight' }, writer );
  59. expect( element.hasClass( 'highlight' ) ).to.be.true;
  60. remove( element, 'highlight', writer );
  61. expect( element.hasClass( 'highlight' ) ).to.be.false;
  62. } );
  63. it( 'should set default highlight handling methods - CSS classes array', () => {
  64. toWidget( element, writer );
  65. const set = element.getCustomProperty( 'addHighlight' );
  66. const remove = element.getCustomProperty( 'removeHighlight' );
  67. expect( typeof set ).to.equal( 'function' );
  68. expect( typeof remove ).to.equal( 'function' );
  69. set( element, { priority: 1, classes: [ 'highlight', 'foo' ], id: 'highlight' }, writer );
  70. expect( element.hasClass( 'highlight' ) ).to.be.true;
  71. expect( element.hasClass( 'foo' ) ).to.be.true;
  72. remove( element, 'highlight', writer );
  73. expect( element.hasClass( 'highlight' ) ).to.be.false;
  74. expect( element.hasClass( 'foo' ) ).to.be.false;
  75. } );
  76. it( 'should add element a selection handler to widget if hasSelectionHandler=true is passed', () => {
  77. toWidget( element, writer, { hasSelectionHandler: true } );
  78. expect( element.hasClass( 'ck-widget_selectable' ) ).to.be.true;
  79. const selectionHandler = element.getChild( 0 );
  80. expect( selectionHandler ).to.be.instanceof( UIElement );
  81. const domSelectionHandler = selectionHandler.render( document );
  82. expect( domSelectionHandler.classList.contains( 'ck' ) ).to.be.true;
  83. expect( domSelectionHandler.classList.contains( 'ck-widget__selection-handler' ) ).to.be.true;
  84. const icon = domSelectionHandler.firstChild;
  85. expect( icon.nodeName ).to.equal( 'svg' );
  86. expect( icon.classList.contains( 'ck' ) ).to.be.true;
  87. expect( icon.classList.contains( 'ck-icon' ) ).to.be.true;
  88. } );
  89. describe( 'on Edge', () => {
  90. beforeEach( () => {
  91. testUtils.sinon.stub( env, 'isEdge' ).get( () => true );
  92. element = writer.createContainerElement( 'div' );
  93. toWidget( element, writer );
  94. } );
  95. it( 'should not set contenteditable onEdge', () => {
  96. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  97. } );
  98. } );
  99. } );
  100. describe( 'isWidget()', () => {
  101. it( 'should return true for widgetized elements', () => {
  102. expect( isWidget( element ) ).to.be.true;
  103. } );
  104. it( 'should return false for non-widgetized elements', () => {
  105. expect( isWidget( new ViewElement( 'p' ) ) ).to.be.false;
  106. } );
  107. } );
  108. describe( 'label utils', () => {
  109. it( 'should allow to set label for element', () => {
  110. const element = new ViewElement( 'p' );
  111. setLabel( element, 'foo bar baz', writer );
  112. expect( getLabel( element ) ).to.equal( 'foo bar baz' );
  113. } );
  114. it( 'should return empty string for elements without label', () => {
  115. const element = new ViewElement( 'div' );
  116. expect( getLabel( element ) ).to.equal( '' );
  117. } );
  118. it( 'should allow to use a function as label creator', () => {
  119. const element = new ViewElement( 'p' );
  120. let caption = 'foo';
  121. setLabel( element, () => caption, writer );
  122. expect( getLabel( element ) ).to.equal( 'foo' );
  123. caption = 'bar';
  124. expect( getLabel( element ) ).to.equal( 'bar' );
  125. } );
  126. } );
  127. describe( 'toWidgetEditable', () => {
  128. let viewDocument, element;
  129. beforeEach( () => {
  130. viewDocument = new ViewDocument();
  131. element = new ViewEditableElement( 'div' );
  132. element._document = viewDocument;
  133. toWidgetEditable( element, writer );
  134. } );
  135. it( 'should be created in context of proper document', () => {
  136. expect( element.document ).to.equal( viewDocument );
  137. } );
  138. it( 'should add proper class', () => {
  139. expect( element.hasClass( 'ck-editor__editable', 'ck-editor__nested-editable' ) ).to.be.true;
  140. } );
  141. it( 'should add proper contenteditable value when element is read-only - initialization', () => {
  142. const element = new ViewEditableElement( 'div' );
  143. element._document = viewDocument;
  144. element.isReadOnly = true;
  145. toWidgetEditable( element, writer );
  146. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  147. } );
  148. it( 'should add proper contenteditable value when element is read-only - when changing', () => {
  149. element.isReadOnly = true;
  150. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  151. element.isReadOnly = false;
  152. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'true' );
  153. } );
  154. it( 'should add proper class when element is focused', () => {
  155. element.isFocused = true;
  156. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.true;
  157. element.isFocused = false;
  158. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.false;
  159. } );
  160. describe( 'on Edge', () => {
  161. beforeEach( () => {
  162. testUtils.sinon.stub( env, 'isEdge' ).get( () => true );
  163. viewDocument = new ViewDocument();
  164. element = new ViewEditableElement( 'div' );
  165. element._document = viewDocument;
  166. toWidgetEditable( element, writer );
  167. } );
  168. it( 'should add contenteditable attribute when element is read-only - initialization', () => {
  169. const element = new ViewEditableElement( 'div' );
  170. element._document = viewDocument;
  171. element.isReadOnly = true;
  172. toWidgetEditable( element, writer );
  173. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  174. } );
  175. it( 'should add contenteditable attribute when element is read-only - when changing', () => {
  176. element.isReadOnly = true;
  177. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  178. element.isReadOnly = false;
  179. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  180. } );
  181. } );
  182. } );
  183. describe( 'addHighlightHandling()', () => {
  184. let element, addSpy, removeSpy, set, remove;
  185. beforeEach( () => {
  186. element = new ViewElement( 'p' );
  187. addSpy = sinon.spy();
  188. removeSpy = sinon.spy();
  189. setHighlightHandling( element, writer, addSpy, removeSpy );
  190. set = element.getCustomProperty( 'addHighlight' );
  191. remove = element.getCustomProperty( 'removeHighlight' );
  192. } );
  193. it( 'should set highlight handling methods', () => {
  194. expect( typeof set ).to.equal( 'function' );
  195. expect( typeof remove ).to.equal( 'function' );
  196. } );
  197. it( 'should call highlight methods when descriptor is added and removed', () => {
  198. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight' };
  199. set( element, descriptor, writer );
  200. remove( element, descriptor.id, writer );
  201. sinon.assert.calledOnce( addSpy );
  202. sinon.assert.calledWithExactly( addSpy, element, descriptor, writer );
  203. sinon.assert.calledOnce( removeSpy );
  204. sinon.assert.calledWithExactly( removeSpy, element, descriptor, writer );
  205. } );
  206. it( 'should call highlight methods when next descriptor is added', () => {
  207. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  208. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  209. set( element, descriptor );
  210. set( element, secondDescriptor );
  211. sinon.assert.calledTwice( addSpy );
  212. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  213. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  214. } );
  215. it( 'should not call highlight methods when descriptor with lower priority is added', () => {
  216. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  217. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  218. set( element, descriptor );
  219. set( element, secondDescriptor );
  220. sinon.assert.calledOnce( addSpy );
  221. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  222. } );
  223. it( 'should call highlight methods when descriptor is removed changing active descriptor', () => {
  224. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  225. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  226. set( element, descriptor );
  227. set( element, secondDescriptor );
  228. remove( element, secondDescriptor.id );
  229. sinon.assert.calledThrice( addSpy );
  230. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  231. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  232. expect( addSpy.thirdCall.args[ 1 ] ).to.equal( descriptor );
  233. sinon.assert.calledTwice( removeSpy );
  234. expect( removeSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  235. expect( removeSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  236. } );
  237. it( 'should call highlight methods when descriptor is removed not changing active descriptor', () => {
  238. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  239. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  240. set( element, descriptor );
  241. set( element, secondDescriptor );
  242. remove( element, secondDescriptor );
  243. sinon.assert.calledOnce( addSpy );
  244. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  245. sinon.assert.notCalled( removeSpy );
  246. } );
  247. it( 'should call highlight methods - CSS class array', () => {
  248. const descriptor = { priority: 10, classes: [ 'highlight', 'a' ], id: 'highlight-1' };
  249. const secondDescriptor = { priority: 10, classes: [ 'highlight', 'b' ], id: 'highlight-2' };
  250. set( element, descriptor );
  251. set( element, secondDescriptor );
  252. sinon.assert.calledTwice( addSpy );
  253. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  254. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  255. } );
  256. } );
  257. } );